diff options
author | chai <chaifix@163.com> | 2021-10-31 14:27:26 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-31 14:27:26 +0800 |
commit | 601442f94fc0dcfdc5a117c5f87d90b156d53045 (patch) | |
tree | b006bcd6a28a965a900c64f4716007fcb45eee98 /Runtime | |
parent | 26f05c6e3dcac9995345fb5a2b031be7e3ea79e9 (diff) |
+static initiator
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/GUI/TextGenerator.cpp | 36 | ||||
-rw-r--r-- | Runtime/GUI/TextGenerator.h | 12 | ||||
-rw-r--r-- | Runtime/Graphics/CustomVertexLayout.h | 10 | ||||
-rw-r--r-- | Runtime/Graphics/SharedVertexBuffer.cpp | 2 | ||||
-rw-r--r-- | Runtime/Graphics/Texture.cpp | 15 | ||||
-rw-r--r-- | Runtime/Graphics/VertexAttribute.h | 17 | ||||
-rw-r--r-- | Runtime/Rendering/DynamicMesh.h | 10 | ||||
-rw-r--r-- | Runtime/Rendering/Quad.cpp (renamed from Runtime/Graphics/Quad.cpp) | 17 | ||||
-rw-r--r-- | Runtime/Rendering/Quad.h (renamed from Runtime/Graphics/Quad.h) | 5 | ||||
-rw-r--r-- | Runtime/Rendering/UIQuad.cpp (renamed from Runtime/Graphics/UIQuad.cpp) | 10 | ||||
-rw-r--r-- | Runtime/Rendering/UIQuad.h (renamed from Runtime/Graphics/UIQuad.h) | 7 | ||||
-rw-r--r-- | Runtime/Scripting/Rendering/Rendering.bind.cpp | 2 | ||||
-rw-r--r-- | Runtime/Utilities/StaticInitiator.h | 27 |
13 files changed, 128 insertions, 42 deletions
diff --git a/Runtime/GUI/TextGenerator.cpp b/Runtime/GUI/TextGenerator.cpp index a518772..1b34d1b 100644 --- a/Runtime/GUI/TextGenerator.cpp +++ b/Runtime/GUI/TextGenerator.cpp @@ -38,7 +38,7 @@ character::Hash TextGenerator::GetHash(Codepoint codepoint, int pixelSize) return hash; } -Character TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelSize) +const Character* TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); auto iter = m_Characters.find(hash); @@ -48,14 +48,22 @@ Character TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelS iter = m_Characters.find(hash); } Assert(iter != m_Characters.end()); - return iter->second; + return &iter->second; +} + +void TextGenerator::RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize) +{ + for (int i = 0; i < n; ++i) + { + RenderCharacter(codepoint[i], pixelSize); + } } void TextGenerator::RenderCharacter(character::Codepoint codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); -// if (m_Characters.count(hash) != 0) -// return; + if (m_Characters.count(hash) != 0) + return; FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize); if (FT_Load_Char(m_FTFace, codepoint, FT_LOAD_RENDER)) { @@ -88,12 +96,12 @@ void TextGenerator::RenderCharacter(character::Codepoint codepoint, int pixelSiz character.atlas = atlas->index; character.position = rect; character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top); - character.advance = m_FTFace->glyph->advance.x; + character.advance = m_FTFace->glyph->advance.x * 1/64.f; m_Characters.insert(std::pair<character::Hash, Character>(hash, character)); } -GlyphAtals* TextGenerator::GetGlyphAtlas(int index) +const GlyphAtals* TextGenerator::GetGlyphAtlas(int index) { if (index >= m_Atlases.size()) return NULL; @@ -104,25 +112,25 @@ Internal::Rect TextGenerator::GetRenderChartAndMove(GlyphAtals* atlas, Internal: { Internal::Rect rect; Internal::Vector2 space; - space.x = atlas->width - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.x; - space.y = atlas->height - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.y; + space.x = atlas->width - atlas->cursor.x - m_AtlasMargin; + space.y = atlas->height - atlas->cursor.y - m_AtlasMargin; if (space.x > preferSize.x && space.y > preferSize.y) { rect.x = atlas->cursor.x; rect.y = atlas->cursor.y; rect.width = preferSize.x; rect.height = preferSize.y; - atlas->cursor.x += rect.width; + atlas->cursor.x += rect.width + m_GlyphPadding; atlas->rowHeight = max(atlas->rowHeight, preferSize.y); } - else if (space.y - atlas->rowHeight > preferSize.y) + else if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y) { rect.x = m_AtlasMargin; rect.y = atlas->cursor.y + m_GlyphPadding + atlas->rowHeight; rect.width = preferSize.x; rect.height = preferSize.y; atlas->cursor.x = m_AtlasMargin; - atlas->cursor.y += atlas->rowHeight; + atlas->cursor.y += atlas->rowHeight + m_GlyphPadding; atlas->rowHeight = rect.height; } else @@ -178,11 +186,11 @@ bool TextGenerator::HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSi if (atlas == NULL) return false; Internal::Vector2 space; - space.x = atlas->width - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.x; - space.y = atlas->height - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.y; + space.x = atlas->width - atlas->cursor.x - m_AtlasMargin; + space.y = atlas->height - atlas->cursor.y - m_AtlasMargin; if (space.x > preferSize.x && space.y > preferSize.y) return true; - if (space.y - atlas->rowHeight > preferSize.y) + if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y) return true; return false; } diff --git a/Runtime/GUI/TextGenerator.h b/Runtime/GUI/TextGenerator.h index 1065acf..ef78eb6 100644 --- a/Runtime/GUI/TextGenerator.h +++ b/Runtime/GUI/TextGenerator.h @@ -51,6 +51,7 @@ struct GlyphAtals int index; Texture* altas; // 贴图 int width, height; // 尺寸 + Internal::Vector2 cursor; // 游标,从左上角(0,0)开始 int rowHeight; // 当前行的高度 }; @@ -67,9 +68,12 @@ class TextGenerator : public Singleton<TextGenerator> public: void Setup(TextGeneratingSettings settings); - Character GetCharacter(character::Codepoint codepoint, int pixelSize); + const Character* GetCharacter(character::Codepoint codepoint, int pixelSize); + const GlyphAtals* GetGlyphAtlas(int index); + + // pre-bake void RenderCharacter(character::Codepoint codepoint, int pixelSize); - GlyphAtals* GetGlyphAtlas(int index); + void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize); private: @@ -79,6 +83,8 @@ private: Internal::Rect GetRenderChartAndMove(GlyphAtals* atlas, Internal::Vector2 preferSize); bool HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSize); + character::Hash GetHash(character::Codepoint Codepoint, int pixelSize); + std::unordered_map<character::Hash, Character> m_Characters; // 渲染完的文字 std::vector<GlyphAtals> m_Atlases; // 当前所有的atlas @@ -91,8 +97,6 @@ private: FT_Library m_FTLibrary; FT_Face m_FTFace; - character::Hash GetHash(character::Codepoint Codepoint, int pixelSize); - }; #define g_TextGenerator (*TextGenerator::Instance()) diff --git a/Runtime/Graphics/CustomVertexLayout.h b/Runtime/Graphics/CustomVertexLayout.h index 86dcf90..d72c71d 100644 --- a/Runtime/Graphics/CustomVertexLayout.h +++ b/Runtime/Graphics/CustomVertexLayout.h @@ -11,6 +11,15 @@ struct CustomVertexLayout { GLuint buffer; // 创建时留空 std::vector<VertexAttributeDescriptor> attributes; + + // 重置pointer(startOffset)为0 + void ResetPointer() + { + for (int i = 0; i < attributes.size(); ++i) + { + attributes[i].pointer = 0; + } + } }; namespace VertexLayout @@ -19,4 +28,3 @@ namespace VertexLayout extern void SetupCustomVertexLayout(CustomVertexLayout& layout); } - diff --git a/Runtime/Graphics/SharedVertexBuffer.cpp b/Runtime/Graphics/SharedVertexBuffer.cpp index 464a919..e787630 100644 --- a/Runtime/Graphics/SharedVertexBuffer.cpp +++ b/Runtime/Graphics/SharedVertexBuffer.cpp @@ -173,6 +173,8 @@ void SharedVertexBuffer::DrawChunk(CustomVertexLayout& layout) VertexLayout::SetupCustomVertexLayout(layout); + layout.ResetPointer(); + const void* indexPtr = m_CurIB ? 0 : (m_CurIBData.empty() ? 0 : &m_CurIBData[0]); if (m_CurIB) diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp index f92c094..eed0b90 100644 --- a/Runtime/Graphics/Texture.cpp +++ b/Runtime/Graphics/Texture.cpp @@ -158,12 +158,13 @@ Texture::Texture(TextureSetting setting, int w, int h) if (m_Format == ETextureFormat::R8)
{
internalFormat = GL_RED;
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glDeleteTextures(1, &m_GPUID);
glBindTexture(GL_TEXTURE_2D, 0);
@@ -185,6 +186,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo throw TextureException(error);
);
+ int alignment = 4;
+ if (m_Format == ETextureFormat::R8)
+ {
+ alignment = 1;
+ }
+
GLenum fmt = GL_RED;
switch (format)
{
@@ -205,8 +212,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo case EPixelElementType::PixelType_FLOAT: t = GL_FLOAT; break;
}
+ glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width, rect.height, fmt, t, data);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glBindTexture(GL_TEXTURE_2D, 0);
throw TextureException(error);
diff --git a/Runtime/Graphics/VertexAttribute.h b/Runtime/Graphics/VertexAttribute.h index 4e50a1a..2757c70 100644 --- a/Runtime/Graphics/VertexAttribute.h +++ b/Runtime/Graphics/VertexAttribute.h @@ -21,6 +21,15 @@ enum VertexAttrFormat struct VertexAttributeDescriptor { + //union { + const void* pointer; // 内存地址,刚创建时留空 + int startOffset; // 显存中相对VBO的偏移值 + //}; + uint componentNum; // 向量维度1,2,3,4 + uint componentFormat; // 每个分量的类型 + uint16 stride; // 间隔 + bool normalize; // 是否归一化,只用于CustomVertexLayout + // for default vertex layout VertexAttributeDescriptor() {} // for custom vertex layout @@ -33,14 +42,6 @@ struct VertexAttributeDescriptor normalize = normalized; } - union { - const void* pointer; // 内存地址或显存中相对VBO的偏移值 - int startOffset; - }; - uint componentNum; // 向量维度1,2,3,4 - uint componentFormat; // 每个分量的类型 - uint16 stride; // 间隔 - bool normalize; // 是否归一化,只用于CustomVertexLayout }; namespace VertexAttribute diff --git a/Runtime/Rendering/DynamicMesh.h b/Runtime/Rendering/DynamicMesh.h new file mode 100644 index 0000000..3cbb6f6 --- /dev/null +++ b/Runtime/Rendering/DynamicMesh.h @@ -0,0 +1,10 @@ +#pragma once +#include "Runtime/Utilities/StaticInitiator.h" + +// 填充g_SharedVBO的动态mesh +class DynamicMesh +{ +public: + virtual void Draw() = 0; + +}; diff --git a/Runtime/Graphics/Quad.cpp b/Runtime/Rendering/Quad.cpp index d769dd4..b4f81ee 100644 --- a/Runtime/Graphics/Quad.cpp +++ b/Runtime/Rendering/Quad.cpp @@ -1,7 +1,7 @@ #include "../Math/Vector2.h" #include "../Math/Vector3.h" #include "Quad.h" -#include "GfxDevice.h" +#include "../Graphics/GfxDevice.h" struct QuadVBOLayout { @@ -9,6 +9,16 @@ struct QuadVBOLayout Internal::Vector2 uv; }; +static CustomVertexLayout layout; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 3, VertexAttrFormat_Float, sizeof(QuadVBOLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector3), 2, VertexAttrFormat_Float, sizeof(QuadVBOLayout)); + + layout.attributes.push_back(POSITION); + layout.attributes.push_back(UV); +}); + void Quad::Draw() { const int nVerts = 4; @@ -34,8 +44,7 @@ void Quad::Draw() uint8* vb; uint16* ib; - uint attrs = Mask(VertexAttr_Position) | Mask(VertexAttr_UV); - g_SharedVBO.GetChunk(attrs, 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + g_SharedVBO.GetChunk(sizeof(QuadVBOLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); QuadVBOLayout* dst = (QuadVBOLayout*)vb; for (int i = 0; i < nVerts; ++i) @@ -48,5 +57,5 @@ void Quad::Draw() ib[i] = indices[i]; g_SharedVBO.ReleaseChunk(4, 6); - g_SharedVBO.DrawChunk(); + g_SharedVBO.DrawChunk(layout); }
\ No newline at end of file diff --git a/Runtime/Graphics/Quad.h b/Runtime/Rendering/Quad.h index 0e38ec4..ee6c5be 100644 --- a/Runtime/Graphics/Quad.h +++ b/Runtime/Rendering/Quad.h @@ -2,8 +2,9 @@ #define QUAD_H #include "../Utilities/UtilMacros.h" +#include "DynamicMesh.h" -class Quad +class Quad : public DynamicMesh { public: Quad(float l, float r, float t, float b); @@ -15,7 +16,7 @@ public: GET_SET(float, Top, m_Top); GET_SET(float, Bottom, m_Bottom); - void Draw(); + void Draw() override; private: float m_Left, m_Right, m_Top, m_Bottom; diff --git a/Runtime/Graphics/UIQuad.cpp b/Runtime/Rendering/UIQuad.cpp index 73cf645..35dcf7e 100644 --- a/Runtime/Graphics/UIQuad.cpp +++ b/Runtime/Rendering/UIQuad.cpp @@ -1,5 +1,5 @@ #include "../Math/Vector2.h" -#include "GfxDevice.h" +#include "../Graphics/GfxDevice.h" #include "UIQuad.h" struct UIQuadLayout @@ -8,16 +8,18 @@ struct UIQuadLayout Internal::Vector2 uv; }; -void UIQuad::Draw() -{ - CustomVertexLayout layout; +static CustomVertexLayout layout; +InitializeStaticVariables([](){ VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector2), 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); layout.attributes.push_back(POSITION); layout.attributes.push_back(UV); +}); +void UIQuad::Draw() +{ const int nVerts = 4; const int nIndices = 6; diff --git a/Runtime/Graphics/UIQuad.h b/Runtime/Rendering/UIQuad.h index a0d77a5..f6d3a98 100644 --- a/Runtime/Graphics/UIQuad.h +++ b/Runtime/Rendering/UIQuad.h @@ -1,6 +1,8 @@ #pragma once +#include "DynamicMesh.h" +#include "../Utilities/StaticInitiator.h" -class UIQuad +class UIQuad : public DynamicMesh { public : UIQuad(float l, float r, float t, float b) @@ -10,7 +12,8 @@ public : m_Top = t; m_Bottom = b; } - void Draw(); + + void Draw() override; private: float m_Left, m_Right, m_Top, m_Bottom; diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp index cb294a3..9fb9a48 100644 --- a/Runtime/Scripting/Rendering/Rendering.bind.cpp +++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp @@ -1,8 +1,8 @@ #include "Runtime/Graphics/Shader.h" #include "Runtime/Graphics/Texture.h" #include "Runtime/Graphics/ImageData.h" -#include "Runtime/Graphics/UIQuad.h" #include "Runtime/Graphics/GfxDevice.h" +#include "Runtime/Rendering/UIQuad.h" // Rendering.DrawUIQuad({}) static int DrawUIQuad(lua_State* L) diff --git a/Runtime/Utilities/StaticInitiator.h b/Runtime/Utilities/StaticInitiator.h new file mode 100644 index 0000000..bbcdbee --- /dev/null +++ b/Runtime/Utilities/StaticInitiator.h @@ -0,0 +1,27 @@ +#pragma once + +// 静态构造函数 +#include "ThirdParty/StaticConstructor/include/StaticConstructor.h" + +typedef void(*StaticFunc) (); + +// 自动初始化静态数据 +class StaticFuncInvoker +{ +public: + // Default Constructor: + StaticFuncInvoker(StaticFunc func) + { + if (func) + func(); + } +}; + +// 调用具名的static函数 +#define InvokeStaticFunc(func)\ + static StaticFuncInvoker staticInvokerOf_##func(func); + +// 用来初始化当前cpp里的静态变量 +#define InitializeStaticVariables(lambda)\ + static StaticFuncInvoker staticInvoker(lambda); + |