From 7031fbc702355fb5a928712227ec447fa74eec21 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 9 Oct 2022 12:21:26 +0000 Subject: [PATCH] Reduces the number of SetVertexAttributeFormat calls in Canvas2D. git-svn-id: https://svn.wildfiregames.com/public/ps/trunk@27138 3db68df2-c116-0410-a063-a993310a9797 --- source/graphics/Canvas2D.cpp | 27 ++++-------- source/graphics/TextRenderer.cpp | 90 +++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 70 deletions(-) diff --git a/source/graphics/Canvas2D.cpp b/source/graphics/Canvas2D.cpp index a1db994a2a..26853460bf 100644 --- a/source/graphics/Canvas2D.cpp +++ b/source/graphics/Canvas2D.cpp @@ -67,15 +67,6 @@ inline void DrawTextureImpl( deviceCommandContext->SetUniform(bindingSlots.colorMul, multiply.AsFloatArray()); deviceCommandContext->SetUniform(bindingSlots.grayscaleFactor, grayscaleFactor); - deviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::POSITION, - Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0); - deviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::UV0, - Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 1); - deviceCommandContext->SetVertexBufferData( 0, vertices.data(), vertices.size() * sizeof(vertices[0])); deviceCommandContext->SetVertexBufferData( @@ -126,6 +117,15 @@ public: transform._11, transform._21, transform._12, transform._22); DeviceCommandContext->SetUniform( BindingSlots.translation, Translation.AsFloatArray()); + + DeviceCommandContext->SetVertexAttributeFormat( + Renderer::Backend::VertexAttributeStream::POSITION, + Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2, + Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0); + DeviceCommandContext->SetVertexAttributeFormat( + Renderer::Backend::VertexAttributeStream::UV0, + Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2, + Renderer::Backend::VertexAttributeRate::PER_VERTEX, 1); } void UnbindTech() @@ -326,15 +326,6 @@ void CCanvas2D::DrawLine(const std::vector& points, const float width m->DeviceCommandContext->SetUniform( m->BindingSlots.grayscaleFactor, 0.0f); - m->DeviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::POSITION, - Renderer::Backend::Format::R32G32_SFLOAT, 0, 0, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0); - m->DeviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::UV0, - Renderer::Backend::Format::R32G32_SFLOAT, 0, 0, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 1); - m->DeviceCommandContext->SetVertexBufferData(0, vertices.data(), vertices.size() * sizeof(vertices[0])); m->DeviceCommandContext->SetVertexBufferData(1, uvs.data(), uvs.size() * sizeof(uvs[0])); diff --git a/source/graphics/TextRenderer.cpp b/source/graphics/TextRenderer.cpp index 6175d1fc96..21db8fd1b6 100644 --- a/source/graphics/TextRenderer.cpp +++ b/source/graphics/TextRenderer.cpp @@ -182,14 +182,6 @@ void CTextRenderer::PutString(float x, float y, const std::wstring* buf, bool ow m_Batches.back().chars += buf->size(); } - -struct t2f_v2i -{ - t2f_v2i() : u(0), v(0), x(0), y(0) { } - float u, v; - i16 x, y; -}; - struct SBatchCompare { bool operator()(const CTextRenderer::SBatch& a, const CTextRenderer::SBatch& b) @@ -206,8 +198,9 @@ void CTextRenderer::Render( Renderer::Backend::IShaderProgram* shader, const CVector2D& transformScale, const CVector2D& translation) { - std::vector indexes; - std::vector vertexes; + std::vector indices; + std::vector positions; + std::vector uvs; // Try to merge non-consecutive batches that share the same font/color/translate: // sort the batch list by font, then merge the runs of adjacent compatible batches @@ -263,30 +256,23 @@ void CTextRenderer::Render( deviceCommandContext->SetUniform(colorMulBindingSlot, batch.color.AsFloatArray()); - vertexes.resize(std::min(MAX_CHAR_COUNT_PER_BATCH, batch.chars) * 4); - indexes.resize(std::min(MAX_CHAR_COUNT_PER_BATCH, batch.chars) * 6); + positions.resize(std::min(MAX_CHAR_COUNT_PER_BATCH, batch.chars) * 4); + uvs.resize(std::min(MAX_CHAR_COUNT_PER_BATCH, batch.chars) * 4); + indices.resize(std::min(MAX_CHAR_COUNT_PER_BATCH, batch.chars) * 6); size_t idx = 0; - auto flush = [deviceCommandContext, &idx, &vertexes, &indexes]() -> void + auto flush = [deviceCommandContext, &idx, &positions, &uvs, &indices]() -> void { if (idx == 0) return; - const uint32_t stride = sizeof(t2f_v2i); - - deviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::POSITION, - Renderer::Backend::Format::R16G16_SINT, offsetof(t2f_v2i, x), stride, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0); - deviceCommandContext->SetVertexAttributeFormat( - Renderer::Backend::VertexAttributeStream::UV0, - Renderer::Backend::Format::R32G32_SFLOAT, offsetof(t2f_v2i, u), stride, - Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0); - deviceCommandContext->SetVertexBufferData( - 0, vertexes.data(), vertexes.size() * sizeof(vertexes[0])); - deviceCommandContext->SetIndexBufferData(indexes.data(), indexes.size() * sizeof(indexes[0])); + 0, positions.data(), positions.size() * sizeof(positions[0])); + deviceCommandContext->SetVertexBufferData( + 1, uvs.data(), uvs.size() * sizeof(uvs[0])); + deviceCommandContext->SetIndexBufferData( + indices.data(), indices.size() * sizeof(indices[0])); deviceCommandContext->DrawIndexed(0, idx * 6, 0); idx = 0; @@ -306,32 +292,32 @@ void CTextRenderer::Render( if (!g) // Missing the missing glyph symbol - give up continue; - vertexes[idx*4].u = g->u1; - vertexes[idx*4].v = g->v0; - vertexes[idx*4].x = g->x1 + x; - vertexes[idx*4].y = g->y0 + y; - - vertexes[idx*4+1].u = g->u0; - vertexes[idx*4+1].v = g->v0; - vertexes[idx*4+1].x = g->x0 + x; - vertexes[idx*4+1].y = g->y0 + y; - - vertexes[idx*4+2].u = g->u0; - vertexes[idx*4+2].v = g->v1; - vertexes[idx*4+2].x = g->x0 + x; - vertexes[idx*4+2].y = g->y1 + y; - - vertexes[idx*4+3].u = g->u1; - vertexes[idx*4+3].v = g->v1; - vertexes[idx*4+3].x = g->x1 + x; - vertexes[idx*4+3].y = g->y1 + y; - - indexes[idx*6+0] = static_cast(idx*4+0); - indexes[idx*6+1] = static_cast(idx*4+1); - indexes[idx*6+2] = static_cast(idx*4+2); - indexes[idx*6+3] = static_cast(idx*4+2); - indexes[idx*6+4] = static_cast(idx*4+3); - indexes[idx*6+5] = static_cast(idx*4+0); + uvs[idx*4].X = g->u1; + uvs[idx*4].Y = g->v0; + positions[idx*4].X = g->x1 + x; + positions[idx*4].Y = g->y0 + y; + + uvs[idx*4+1].X = g->u0; + uvs[idx*4+1].Y = g->v0; + positions[idx*4+1].X = g->x0 + x; + positions[idx*4+1].Y = g->y0 + y; + + uvs[idx*4+2].X = g->u0; + uvs[idx*4+2].Y = g->v1; + positions[idx*4+2].X = g->x0 + x; + positions[idx*4+2].Y = g->y1 + y; + + uvs[idx*4+3].X = g->u1; + uvs[idx*4+3].Y = g->v1; + positions[idx*4+3].X = g->x1 + x; + positions[idx*4+3].Y = g->y1 + y; + + indices[idx*6+0] = static_cast(idx*4+0); + indices[idx*6+1] = static_cast(idx*4+1); + indices[idx*6+2] = static_cast(idx*4+2); + indices[idx*6+3] = static_cast(idx*4+2); + indices[idx*6+4] = static_cast(idx*4+3); + indices[idx*6+5] = static_cast(idx*4+0); x += g->xadvance; -- 2.11.4.GIT