diff options
author | chai <chaifix@163.com> | 2021-11-02 19:44:01 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-02 19:44:01 +0800 |
commit | 72812a7b47f90f9460e54e8149ba9199a7841244 (patch) | |
tree | 9ebdb4b65d7cbbc0a3d7c436a9b45d4eff351b62 | |
parent | b7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed (diff) |
! Text mesh done
27 files changed, 320 insertions, 75 deletions
diff --git a/Data/Resources/Font/msyh.ttc b/Data/Resources/Font/msyh.ttc Binary files differnew file mode 100644 index 0000000..37c28de --- /dev/null +++ b/Data/Resources/Font/msyh.ttc diff --git a/Data/Resources/Shaders/Editor-Text.glsl b/Data/Resources/Shaders/Editor-Text.glsl index e2c2ae7..c58bd48 100644 --- a/Data/Resources/Shaders/Editor-Text.glsl +++ b/Data/Resources/Shaders/Editor-Text.glsl @@ -7,8 +7,7 @@ DepthTest Off CMD_END uniform mat4 gamelab_mat_mvp; -uniform sampler2D uiTex; -uniform vec2 uiText_ST; +uniform sampler2D gamelab_main_tex; VSH_BEGIN layout (location = 0) in vec2 vPos; @@ -16,24 +15,30 @@ layout (location = 1) in vec2 vUV; layout (location = 2) in vec4 vColor; out vec2 uv; +out vec4 color; void main() { - vec4 clip = gamelab_mat_mvp * vec4(vPos, -1, 1.0); + vec2 pos = vPos + vec2(10, 40); + vec4 clip = gamelab_mat_mvp * vec4(pos, -1, 1.0); gl_Position = clip; uv = vUV; + color = vColor; } VSH_END FSH_BEGIN in vec2 uv; +in vec4 color; out vec4 FragColor; void main() { - vec2 uv = vec2(uv.x, 1 - uv.y); - vec4 sampled = vec4(1,1,1,texture(uiTex, uv).r); - FragColor = sampled; + //vec2 uv = vec2(uv.x, 1 - uv.y); + vec4 sampled = vec4(1,1,1,texture(gamelab_main_tex, uv).r); + sampled *= color; + // vec4 sampled = vec4(1,1,1,1); + FragColor = sampled; } FSH_END diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua index d726654..ecaecda 100644 --- a/Data/Scripts/Editor/AssetBrowser.lua +++ b/Data/Scripts/Editor/AssetBrowser.lua @@ -27,24 +27,17 @@ AssetBrowser.OnGUI = function(self) end
local ortho = Matrix44.New()
- ortho:SetOrtho(0, 400, 400, 0, 0.1, 10)
+ ortho:SetOrtho(0, 400, 0, 400, 0.1, 10)
Debug.Log("AssetBrowser.OnGUI()")
GL.ClearColor({0.1,0.1,0.1,1})
GL.Clear(GL.EBufferType.ColorBuffer)
- -- GL.Color({1,1,0,1})
- -- GL.LoadPixelMatrix(-250, 250, -300, 300)
- -- GL.Begin(GL.EPrimitiveType.Triangles)
- -- GL.Vertex({0,0,-1})
- -- GL.Vertex({0,300,-1})
- -- GL.Vertex({250,0,-1})
- -- GL.End()
Engine.Rendering.UseShader(shader)
Engine.Rendering.SetMatrix44("gamelab_mat_mvp", ortho)
- Engine.Rendering.SetTexture("uiTex", tex)
- Engine.Rendering.DrawUIQuad({10, 30, 300, 300})
-
+ --Engine.Rendering.SetTexture("gamelab_main_tex", tex)
+ --Engine.Rendering.DrawUIQuad({0, 0, 10, 20})
+ _G["default_font"]:GetCharacters("你好世界!Hello,World! Project Window Properties", 12)
Engine.Rendering.ResetUniformState()
end
diff --git a/Data/Scripts/EditorApplication.lua b/Data/Scripts/EditorApplication.lua index eb3c7d8..535136b 100644 --- a/Data/Scripts/EditorApplication.lua +++ b/Data/Scripts/EditorApplication.lua @@ -113,11 +113,8 @@ local fsh = [[ BeforeMainLoop() -local font = Engine.GUI.Font.New("./Resources/Font/deng.ttf", {256, 256}, 5, 5) +local font = Engine.GUI.Font.New("./Resources/Font/msyh.ttc", {512, 512}, 5, 5) _G["default_font"] = font -font:GetCharacters([[ -UTF-8是Unicode的一种实现,是一种变长字节编码方式。对于某一个字符的UTF-8编码,如果只有一个字节则其最高二进制位为0;如果是多字节,其第一个字节从最高位开始,连续的二进制位值为1的个数决定了其编码的位数,其余各字节均以10开头。UTF-8最多可用到6个字节。 -]], 12) while true do diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index 54aba66..8cf22cc 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -202,7 +202,7 @@ <ClCompile Include="..\..\..\Runtime\GUI\Font.cpp" />
<ClCompile Include="..\..\..\Runtime\GUI\TextMesh.cpp" />
<ClCompile Include="..\..\..\Runtime\GUI\TextMeshGenerator.cpp" />
- <ClCompile Include="..\..\..\Runtime\GUI\utf8.cpp" />
+ <ClCompile Include="..\..\..\Runtime\GUI\utf8_decode.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindCFunctions.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.cpp" />
<ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindEnum.cpp" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 90f759a..5d2fb1c 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -402,15 +402,15 @@ <ClCompile Include="..\..\..\Runtime\Scripting\GUI\GUI.bind.cpp">
<Filter>Runtime\Scripting\GUI</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\GUI\utf8.cpp">
- <Filter>Runtime\GUI</Filter>
- </ClCompile>
<ClCompile Include="..\..\..\Runtime\GUI\TextMesh.cpp">
<Filter>Runtime\GUI</Filter>
</ClCompile>
<ClCompile Include="..\..\..\Runtime\GUI\TextMeshGenerator.cpp">
<Filter>Runtime\GUI</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\Runtime\GUI\utf8_decode.cpp">
+ <Filter>Runtime\GUI</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Editor\GUI\Dock.h">
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index e5444f1..8cdfdf3 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -184,8 +184,8 @@ const GlyphAtals* Font::GetGlyphAtlas(int index) Internal::Rect Font::GetRenderChartAndMove(GlyphAtals* atlas, Internal::Vector2 preferSize) { - Internal::Rect rect; - Internal::Vector2 space; + Rect rect; + Vector2 space; 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) diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index 9fea14e..e55e78f 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -16,7 +16,7 @@ struct Character { unsigned int atlas; // atlas - Rect position; // altasλ + Rect position; // altasλãϽΪԭģGLY Vector2 bearing; // Ͻԭƫ unsigned int advance; // ܿ˼ }; @@ -110,6 +110,8 @@ public: void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize); void RenderCharacters(std::vector<character::Codepoint>& codepoint, int pixelSize); + GET(Vector2, AtlasSize, m_AtlasSize); + private: Texture* CreateAtlas(); GlyphAtals* RequestAtlas(int pixelSize, Vector2 preferSize); diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/TextMesh.cpp index 6f3b3d6..de9a195 100644 --- a/Runtime/GUI/TextMesh.cpp +++ b/Runtime/GUI/TextMesh.cpp @@ -3,8 +3,11 @@ #include "../Math/Math.h" #include "../Graphics/Color.h" #include "TextMesh.h" +#include "../Graphics/DefaultVertexLayout.h" +#include "Runtime/Debug/Log.h" #include <vector> #include <unordered_map> +#include "Runtime/Graphics/GfxDevice.h" using namespace std; @@ -20,6 +23,7 @@ static CustomVertexLayout s_TextMeshVBOLayout; static unsigned int s_VertexPerText; static unsigned int s_SizePerVertex; static unsigned int s_SizePerText; +static unsigned int s_SizePerIndex; static unsigned int s_IndicesPerText; struct TextInfo { @@ -39,33 +43,36 @@ InitializeStaticVariables([]() { s_VertexPerText = 4; s_SizePerVertex = sizeof(TextMeshVBOLayout); + + s_IndicesPerText = 6; + s_SizePerIndex = VertexLayout::GetDefaultIndexSize(); + s_SizePerText = sizeof(TextMeshVBOLayout) * 4; - s_IndicesPerText = 6; }); TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment) { m_Font = font; s_TextInfos.clear(); + const Vector2 atlasSize = font->GetAtlasSize(); + // ղͬatlasൽs_TextInfos float offset = 0; for (int i = 0; i < str.length; ++i) { character::Codepoint c = str.str[i]; const Character* ch = font->GetCharacter(c, pixelSize); - unsigned int index = ch->atlas; + unsigned int atlasIndex = ch->atlas; TextInfo info; info.ch = ch; info.offset = offset; - auto list = s_TextInfos.find(index); + auto list = s_TextInfos.find(atlasIndex); if (list == s_TextInfos.end()) - { - s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(index, vector<TextInfo>())); - list = s_TextInfos.find(index); - } - vector<TextInfo>& v = list->second; + s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(atlasIndex, vector<TextInfo>())); + + vector<TextInfo>& v = s_TextInfos[atlasIndex]; v.push_back(info); offset += ch->advance; @@ -74,17 +81,86 @@ TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnch if (s_TextInfos.size() == 0) return; + // VBOIBO for (auto iter : s_TextInfos) { - unsigned int index = iter.first; + unsigned int atlasIndex = iter.first; // atlas atlasIndex vector<TextInfo>& texts = iter.second; - int count = texts.size(); - - VertexBuffer* vb = new VertexBuffer(count * s_SizePerText, count * s_IndicesPerText, VertexBuffer::VertexBufferType_Static); - + int textCount = texts.size(); + + VertexBuffer* vb = new VertexBuffer(textCount * s_SizePerText, textCount * s_IndicesPerText * s_SizePerIndex, VertexBuffer::VertexBufferType_Static); + void* pVB; + uint16* pIB; + + vb->GetChunk(s_SizePerVertex, s_SizePerIndex, s_VertexPerText * textCount, s_IndicesPerText * textCount, EPrimitive::Primitive_Triangle, &pVB,(void**) &pIB); + + TextMeshVBOLayout* dst = (TextMeshVBOLayout*)pVB; + for (int i = 0; i < textCount; ++i) + { + TextInfo& text = texts[i]; + + int vOff = i * s_VertexPerText; + float pos[] = { + text.offset + text.ch->bearing.x, text.ch->bearing.y - text.ch->position.height, // bottom-left + text.offset + text.ch->bearing.x + text.ch->position.width, text.ch->bearing.y - text.ch->position.height, // bottom-right + text.offset + text.ch->bearing.x + text.ch->position.width, text.ch->bearing.y, // top-right + text.offset + text.ch->bearing.x, text.ch->bearing.y, // top-left + }; + // ϽΪUVԭ㣬shaderʱҪy + Vector4 uvQuad = Vector4(text.ch->position.x / atlasSize.x, text.ch->position.y / atlasSize.y, text.ch->position.width / atlasSize.x, text.ch->position.height / atlasSize.y); + float uv[] = { + uvQuad.x, uvQuad.y + uvQuad.w, + uvQuad.x + uvQuad.z, uvQuad.y + uvQuad.w, + uvQuad.x + uvQuad.z, uvQuad.y, + uvQuad.x, uvQuad.y, + }; + for (int j = 0; j < s_VertexPerText; ++j) + { + dst[vOff + j].position.Set(pos[2 * j], pos[2 * j + 1]); + dst[vOff + j].uv.Set(uv[2 * j], uv[2 * j + 1]); + dst[vOff + j].color.Set(255 , 255, 255, 255); + } + + int iOff = i * s_IndicesPerText; + int indices[] = { + 0, 1, 3, // right-top + 1, 2, 3, // left-bottom + }; + for (int j = 0; j < s_IndicesPerText; ++j) + pIB[iOff + j] = vOff + indices[j]; + } + + vb->FlushChunk(s_VertexPerText * textCount, s_IndicesPerText * textCount); +
+ m_VBOs.insert(std::pair<int, VertexBuffer*>(atlasIndex, vb)); } + + WipeGLError(); } void TextMesh::Draw() { - + for (auto subText : m_VBOs) + { + int atlasIndex = subText.first; // atlasIndex of atlas + VertexBuffer* vbo = subText.second; + + const GlyphAtals* atlas = m_Font->GetGlyphAtlas(atlasIndex); + if (atlas == NULL) + { + log_error("Render text failed, no glyph atlas."); + continue; + } + + g_GfxDevice.SetUniformTexture("gamelab_main_tex", atlas->altas); + + CheckGLError( + throw GLException(error); + ); + vbo->Draw(s_TextMeshVBOLayout); + + CheckGLError( + throw GLException(error); + ); + g_GfxDevice.ResetUniformsState(); + } } diff --git a/Runtime/GUI/TextMesh.h b/Runtime/GUI/TextMesh.h index 8a29c92..c091a78 100644 --- a/Runtime/GUI/TextMesh.h +++ b/Runtime/GUI/TextMesh.h @@ -49,5 +49,4 @@ private: Font* m_Font; std::unordered_map<int/*IndexOfAtlas*/, VertexBuffer*> m_VBOs; - -}; +};
\ No newline at end of file diff --git a/Runtime/GUI/utf8.cpp b/Runtime/GUI/utf8_decode.cpp index 8a3a086..8a3a086 100644 --- a/Runtime/GUI/utf8.cpp +++ b/Runtime/GUI/utf8_decode.cpp diff --git a/Runtime/Graphics/Color.h b/Runtime/Graphics/Color.h index af8d3ba..ebccafe 100644 --- a/Runtime/Graphics/Color.h +++ b/Runtime/Graphics/Color.h @@ -14,6 +14,13 @@ namespace Internal this->b = b; this->a = a; } + void Set(float r = 0, float g = 0, float b = 0, float a = 0) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } float r, g, b, a; }; @@ -26,6 +33,13 @@ namespace Internal this->b = b; this->a = a; } + void Set(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 0) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } unsigned char r, g, b, a; }; diff --git a/Runtime/Graphics/DynamicVertexBuffer.cpp b/Runtime/Graphics/DynamicVertexBuffer.cpp index 203aa26..cb59a8b 100644 --- a/Runtime/Graphics/DynamicVertexBuffer.cpp +++ b/Runtime/Graphics/DynamicVertexBuffer.cpp @@ -1,4 +1,5 @@ #include "DynamicVertexBuffer.h" +#include "CustomVertexLayout.h" #include "../Profiling/FrameStats.h" DynamicVertexBuffer::DynamicVertexBuffer() @@ -109,11 +110,12 @@ void DynamicVertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxV { Assert(out_vb && out_ib); + m_CurStride = sizePerVert; uint vbufferSize = sizePerVert * maxVerts; uint ibufferSize = sizePerIndex * maxIndices; - const bool mapVertexBuffer = vbufferSize >= DataBufferThreshold; - const bool mapIndexBuffer = ibufferSize >= DataBufferThreshold; + const bool mapVertexBuffer = vbufferSize >= kDataBufferThreshold; + const bool mapIndexBuffer = ibufferSize >= kDataBufferThreshold; GLenum usage = GL_STREAM_DRAW; @@ -221,7 +223,7 @@ void DynamicVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices) m_CurVB->FlushMapedRange(0, actualVBufferSize); m_CurVB->UnMap(); } - else if (actualVBufferSize >= DataBufferThreshold) + else if (actualVBufferSize >= kDataBufferThreshold) { m_CurVB = GPU::ClaimBuffer(actualVBufferSize, usage); m_CurVB->RestoreWithData(actualVBufferSize, usage, &m_CurVBData[0]); @@ -232,7 +234,7 @@ void DynamicVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices) m_CurIB->FlushMapedRange(0, actualIBufferSize); m_CurIB->UnMap(); } - else if (actualIBufferSize >= DataBufferThreshold) + else if (actualIBufferSize >= kDataBufferThreshold) { m_CurIB = GPU::ClaimBuffer(0, usage); m_CurIB->RestoreWithData(0, usage, &m_CurIBData[0]); diff --git a/Runtime/Graphics/DynamicVertexBuffer.h b/Runtime/Graphics/DynamicVertexBuffer.h index 12aa48c..6f21010 100644 --- a/Runtime/Graphics/DynamicVertexBuffer.h +++ b/Runtime/Graphics/DynamicVertexBuffer.h @@ -35,8 +35,8 @@ private: void Clean(); - // ݴСƣڴݣglBufferData - enum { DataBufferThreshold = 1024 }; + // ݴССƣڴݣglBufferData + static const int kDataBufferThreshold = 1024; GPU::DataBuffer *m_CurVB; GPU::DataBuffer *m_CurIB; @@ -52,4 +52,4 @@ private: uint m_CurVertexCount; uint m_CurIndexCount; -}; +};
\ No newline at end of file diff --git a/Runtime/Graphics/GPUDataBuffer.h b/Runtime/Graphics/GPUDataBuffer.h index f66d951..3d24514 100644 --- a/Runtime/Graphics/GPUDataBuffer.h +++ b/Runtime/Graphics/GPUDataBuffer.h @@ -15,6 +15,9 @@ namespace GPU class DataBuffer { public: + DataBuffer(); + ~DataBuffer(); + void Upload(int offset, int size, const void* data); void* Map(uint32 access); void* MapRange(int offset, int size, uint32 access); // @@ -34,9 +37,6 @@ namespace GPU private: friend class BufferPool; - DataBuffer(); - ~DataBuffer(); - GLuint m_Handle; int m_Size; GLenum m_Usage; diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp index 9abea12..2c740ed 100644 --- a/Runtime/Graphics/GfxDevice.cpp +++ b/Runtime/Graphics/GfxDevice.cpp @@ -137,8 +137,7 @@ void GfxDevice::SetUniformTexture(const char* name, Texture* texture) int texUnit = s_TextureUnitBucket.back(); s_TextureUnitBucket.pop_back(); glActiveTexture(GL_TEXTURE0 + texUnit);
- //glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
- glBindTexture(GL_TEXTURE_2D, 2);
+ glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
GLint loc = glGetUniformLocation(m_Shader.GetID(), name); glUniform1i(loc, texUnit); diff --git a/Runtime/Graphics/OpenGL.h b/Runtime/Graphics/OpenGL.h index b1b777e..d93fb5a 100644 --- a/Runtime/Graphics/OpenGL.h +++ b/Runtime/Graphics/OpenGL.h @@ -15,6 +15,14 @@ if(true){ \ } \ } +#define WipeGLError() \ +if(true){\ + GLenum error; \ + while ((error = glGetError()) != GL_NO_ERROR) { \ + throw GLException(error); \ + } \ +} + extern std::string g_sharedGLErrorMsg; class GLException : public std::exception diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp index 60e165f..013b1ba 100644 --- a/Runtime/Graphics/Shader.cpp +++ b/Runtime/Graphics/Shader.cpp @@ -88,7 +88,7 @@ void Shader::CompileProgram(const char* vert, const char* frag, bool keepSrc) glAttachShader(m_ProgramID, m_VertID); glAttachShader(m_ProgramID, m_FragID); glLinkProgram(m_ProgramID); - checkCompileshaderErrorors(m_FragID, "PROGRAM"); + checkCompileshaderErrorors(m_ProgramID, "PROGRAM"); } Shader::~Shader() diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp index eed0b90..a1f4181 100644 --- a/Runtime/Graphics/Texture.cpp +++ b/Runtime/Graphics/Texture.cpp @@ -106,6 +106,8 @@ Texture::Texture(TextureSetting setting, int w, int h) m_FilterMode = setting.filterMode;
m_KeepPixelData = setting.keepImageData;
+ WipeGLError();
+
glGenTextures(1, &m_GPUID);
glBindTexture(GL_TEXTURE_2D, m_GPUID);
@@ -170,6 +172,8 @@ Texture::Texture(TextureSetting setting, int w, int h) glBindTexture(GL_TEXTURE_2D, 0);
throw TextureException(error);
);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture()
diff --git a/Runtime/Graphics/VertexBuffer.cpp b/Runtime/Graphics/VertexBuffer.cpp index 5d136ee..16b29d5 100644 --- a/Runtime/Graphics/VertexBuffer.cpp +++ b/Runtime/Graphics/VertexBuffer.cpp @@ -3,8 +3,10 @@ VertexBuffer::VertexBuffer(int vbSize, int ibSize, VertexBufferType type) { - m_VB = GPU::ClaimBuffer(vbSize, GL_ARRAY_BUFFER); - m_IB = GPU::ClaimBuffer(ibSize, GL_ELEMENT_ARRAY_BUFFER); + m_VB = new GPU::DataBuffer(); + m_IB = new GPU::DataBuffer(); + //m_VB = GPU::ClaimBuffer(vbSize, GL_STATIC_DRAW); + //m_IB = GPU::ClaimBuffer(ibSize, GL_STATIC_DRAW); } VertexBuffer::~VertexBuffer() @@ -12,3 +14,104 @@ VertexBuffer::~VertexBuffer() GPU::ReleaseBuffer(m_VB); GPU::ReleaseBuffer(m_IB); } +// GetChunk +// <fill> +// FlushChunk +// DrawChunk +void VertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib) +{ + m_SizePerVertex = sizePerVert; + + uint vbufferSize = sizePerVert * maxVerts; + uint ibufferSize = sizePerIndex * maxIndices; + + GLenum usage = GL_STATIC_DRAW; + m_VB->Restore(vbufferSize, usage); + m_IB->Restore(ibufferSize, usage); + + const GLenum access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT; + + *out_vb = m_VB->MapRange(0, vbufferSize, access); + *out_ib = m_IB->MapRange(0, ibufferSize, access); + + m_Primitive = primitive; + + WipeGLError(); +} + +void VertexBuffer::FlushChunk(int actualVerts, int actualIndices) +{ + int actualVBufferSize = m_SizePerVertex * actualVerts; + int actualIBufferSize = VertexLayout::GetDefaultIndexSize() * actualIndices; + + m_CurIndexCount = actualIndices; + + m_VB->FlushMapedRange(0, actualVBufferSize); + m_IB->FlushMapedRange(0, actualIBufferSize); + + // һҪunmap + m_VB->UnMap(); + m_IB->UnMap(); + + WipeGLError(); +} + +void VertexBuffer::Draw(CustomVertexLayout& layout) +{ + const byte* basepointer = 0; + const GLuint buffer = m_VB->GetHandle(); + + FillCustomVertexLayout(layout); + + VertexLayout::SetupCustomVertexLayout(layout); + + layout.RestorePointer(); + + const void* indexPtr = 0; + + CheckGLError( + throw GLException(error); + ); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB->GetHandle()); + + CheckGLError( + throw GLException(error); + ); + GLenum indexFormat = VertexLayout::GetDefaultIndexFormat(); + + switch (m_Primitive) + { + case Primitive_Triangle: + glDrawElements(GL_TRIANGLES, m_CurIndexCount, indexFormat, indexPtr); + g_FrameStats.AddDrawCall(); + g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3); + break; + case Primitive_Line: + glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr); + g_FrameStats.AddDrawCall(); + break; + case Primitive_Point: + glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr); + g_FrameStats.AddDrawCall(); + break; + } + + CheckGLError( + throw GLException(error); + ); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +void VertexBuffer::FillCustomVertexLayout(CustomVertexLayout& dst) +{ + const byte* basepointer = 0; + const GLuint buffer = m_VB->GetHandle(); + + dst.buffer = buffer; + + for (int i = 0; i < dst.attributes.size(); ++i) + { + int offset = dst.attributes[i].startOffset; + dst.attributes[i].pointer = basepointer + offset; + } +}
\ No newline at end of file diff --git a/Runtime/Graphics/VertexBuffer.h b/Runtime/Graphics/VertexBuffer.h index c651bdb..2d3a9f5 100644 --- a/Runtime/Graphics/VertexBuffer.h +++ b/Runtime/Graphics/VertexBuffer.h @@ -26,16 +26,28 @@ public: VertexBuffer(int vbSize, int ibSize, VertexBufferType type); ~VertexBuffer(); + // + void GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib); + // ύ + void FlushChunk(int actualVerts, int actualIndices); + GET(GPU::DataBuffer*, VB, m_VB); + GET(GPU::DataBuffer*, IB, m_IB); - void Draw(); + void Draw(CustomVertexLayout& layout); private: + void FillCustomVertexLayout(CustomVertexLayout& dst); + VertexBufferType m_Type; GPU::DataBuffer* m_VB; GPU::DataBuffer* m_IB; + int m_SizePerVertex; + + int m_CurIndexCount; + EPrimitive m_Primitive; }; diff --git a/Runtime/Lua/LuaBind/LuaBindUtility.h b/Runtime/Lua/LuaBind/LuaBindUtility.h index 6d04272..e88dd68 100644 --- a/Runtime/Lua/LuaBind/LuaBindUtility.h +++ b/Runtime/Lua/LuaBind/LuaBindUtility.h @@ -3,9 +3,8 @@ // nativeӿ -// RegisterClass עķͳԱö١ȵclass table GetNativeClassName ù +// RegisterNativeClass עķͳԱö١ȵclass table GetNativeClassName ù // ͬʱעʱעΪsingletonͨʱ -// RegisterClasswin32ĺͻԼӸ» #define LUA_BIND_DECL_CLASS(type, ...) \ friend class LuaBind::State; \ friend class LuaBind::NativeClass<type,##__VA_ARGS__>; \ diff --git a/Runtime/Scripting/GUI/Font.bind.cpp b/Runtime/Scripting/GUI/Font.bind.cpp index 8259a60..733fcf1 100644 --- a/Runtime/Scripting/GUI/Font.bind.cpp +++ b/Runtime/Scripting/GUI/Font.bind.cpp @@ -5,6 +5,7 @@ #include "Runtime/Common/DataBuffer.h"
#include "Runtime/GUI/utf8.h"
#include "Runtime/Utilities/StaticInitiator.h"
+#include "Runtime/GUI/TextMesh.h"
static std::vector<character::Codepoint>* s_Codepoints;
@@ -127,6 +128,15 @@ LUA_BIND_IMPL_METHOD(Font, _GetCharacters) self->RenderCharacters(*s_Codepoints, size);
+ UnicodeString str;
+ str.str = s_Codepoints->data();
+ str.length = s_Codepoints->size();
+
+ WipeGLError();
+
+ TextMesh* tm = new TextMesh(str, self, size, ETextAnchor::TextAnchor_MiddleLeft, ETextAlignment::TextAlignment_Left);
+ tm->Draw();
+
return 0;
}
diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp index 482efb9..08c96f9 100644 --- a/Runtime/Scripting/Rendering/Shader.bind.cpp +++ b/Runtime/Scripting/Rendering/Shader.bind.cpp @@ -63,7 +63,8 @@ LUA_BIND_IMPL_METHOD(Shader, _New) if (shader != NULL) shader->PushUserdata(state); else - state.PushNil(); + state.PushNil();
+ return 1; } catch (ShaderCompileExecption e) @@ -72,6 +73,7 @@ LUA_BIND_IMPL_METHOD(Shader, _New) state.PushNil(); return 1; } + Assert(false); return 0; } diff --git a/ThirdParty/glad/glad.c b/ThirdParty/glad/glad.c index b6fea1f..c0703ae 100644 --- a/ThirdParty/glad/glad.c +++ b/ThirdParty/glad/glad.c @@ -1,22 +1,23 @@ /* - OpenGL loader generated by glad 0.1.33 on Fri Mar 20 11:13:06 2020. + OpenGL loader generated by glad 0.1.34 on Tue Nov 2 08:57:31 2021. Language/Generator: C/C++ Specification: gl APIs: gl=3.3 Profile: compatibility Extensions: - + GL_ARB_map_buffer_alignment, + GL_ARB_map_buffer_range Loader: True - Local files: False + Local files: True Omit khrplatform: False Reproducible: False Commandline: - --profile="compatibility" --api="gl=3.3" --generator="c" --spec="gl" --extensions="" + --profile="compatibility" --api="gl=3.3" --generator="c" --spec="gl" --local-files --extensions="GL_ARB_map_buffer_alignment,GL_ARB_map_buffer_range" Online: - https://glad.dav1d.de/#profile=compatibility&language=c&specification=gl&loader=on&api=gl%3D3.3 + https://glad.dav1d.de/#profile=compatibility&language=c&specification=gl&loader=on&api=gl%3D3.3&extensions=GL_ARB_map_buffer_alignment&extensions=GL_ARB_map_buffer_range */ #include <stdio.h> @@ -991,6 +992,8 @@ PFNGLWINDOWPOS3IPROC glad_glWindowPos3i = NULL; PFNGLWINDOWPOS3IVPROC glad_glWindowPos3iv = NULL; PFNGLWINDOWPOS3SPROC glad_glWindowPos3s = NULL; PFNGLWINDOWPOS3SVPROC glad_glWindowPos3sv = NULL; +int GLAD_GL_ARB_map_buffer_alignment = 0; +int GLAD_GL_ARB_map_buffer_range = 0; static void load_GL_VERSION_1_0(GLADloadproc load) { if(!GLAD_GL_VERSION_1_0) return; glad_glCullFace = (PFNGLCULLFACEPROC)load("glCullFace"); @@ -1754,9 +1757,15 @@ static void load_GL_VERSION_3_3(GLADloadproc load) { glad_glSecondaryColorP3ui = (PFNGLSECONDARYCOLORP3UIPROC)load("glSecondaryColorP3ui"); glad_glSecondaryColorP3uiv = (PFNGLSECONDARYCOLORP3UIVPROC)load("glSecondaryColorP3uiv"); } +static void load_GL_ARB_map_buffer_range(GLADloadproc load) { + if(!GLAD_GL_ARB_map_buffer_range) return; + glad_glMapBufferRange = (PFNGLMAPBUFFERRANGEPROC)load("glMapBufferRange"); + glad_glFlushMappedBufferRange = (PFNGLFLUSHMAPPEDBUFFERRANGEPROC)load("glFlushMappedBufferRange"); +} static int find_extensionsGL(void) { if (!get_exts()) return 0; - (void)&has_ext; + GLAD_GL_ARB_map_buffer_alignment = has_ext("GL_ARB_map_buffer_alignment"); + GLAD_GL_ARB_map_buffer_range = has_ext("GL_ARB_map_buffer_range"); free_exts(); return 1; } @@ -1835,6 +1844,7 @@ int gladLoadGLLoader(GLADloadproc load) { load_GL_VERSION_3_3(load); if (!find_extensionsGL()) return 0; + load_GL_ARB_map_buffer_range(load); return GLVersion.major != 0 || GLVersion.minor != 0; } diff --git a/ThirdParty/glad/glad.h b/ThirdParty/glad/glad.h index cf56144..646b24f 100644 --- a/ThirdParty/glad/glad.h +++ b/ThirdParty/glad/glad.h @@ -1,22 +1,23 @@ /* - OpenGL loader generated by glad 0.1.33 on Fri Mar 20 11:13:06 2020. + OpenGL loader generated by glad 0.1.34 on Tue Nov 2 08:57:31 2021. Language/Generator: C/C++ Specification: gl APIs: gl=3.3 Profile: compatibility Extensions: - + GL_ARB_map_buffer_alignment, + GL_ARB_map_buffer_range Loader: True - Local files: False + Local files: True Omit khrplatform: False Reproducible: False Commandline: - --profile="compatibility" --api="gl=3.3" --generator="c" --spec="gl" --extensions="" + --profile="compatibility" --api="gl=3.3" --generator="c" --spec="gl" --local-files --extensions="GL_ARB_map_buffer_alignment,GL_ARB_map_buffer_range" Online: - https://glad.dav1d.de/#profile=compatibility&language=c&specification=gl&loader=on&api=gl%3D3.3 + https://glad.dav1d.de/#profile=compatibility&language=c&specification=gl&loader=on&api=gl%3D3.3&extensions=GL_ARB_map_buffer_alignment&extensions=GL_ARB_map_buffer_range */ @@ -3603,6 +3604,15 @@ typedef void (APIENTRYP PFNGLSECONDARYCOLORP3UIVPROC)(GLenum type, const GLuint GLAPI PFNGLSECONDARYCOLORP3UIVPROC glad_glSecondaryColorP3uiv; #define glSecondaryColorP3uiv glad_glSecondaryColorP3uiv #endif +#define GL_MIN_MAP_BUFFER_ALIGNMENT 0x90BC +#ifndef GL_ARB_map_buffer_alignment +#define GL_ARB_map_buffer_alignment 1 +GLAPI int GLAD_GL_ARB_map_buffer_alignment; +#endif +#ifndef GL_ARB_map_buffer_range +#define GL_ARB_map_buffer_range 1 +GLAPI int GLAD_GL_ARB_map_buffer_range; +#endif #ifdef __cplusplus } diff --git a/ThirdParty/glad/khrplatform.h b/ThirdParty/glad/khrplatform.h index 5b55ea2..dd22d92 100644 --- a/ThirdParty/glad/khrplatform.h +++ b/ThirdParty/glad/khrplatform.h @@ -119,7 +119,7 @@ * This follows the return type of the function and precedes the function * name in the function prototype. */ -#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(KHRONOS_STATIC) +#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__) /* Win32 but not WinCE */ # define KHRONOS_APIENTRY __stdcall #else |