From 9c6ff95bb6b26cf916c29bc45bf14ed47da84374 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 15 Dec 2019 23:13:59 +0100 Subject: [PATCH] [hud] Don't use vertex shader for scaling Instead, do it on the CPU. --- src/dxvk/hud/dxvk_hud_renderer.cpp | 43 ++++++++++++++----------- src/dxvk/hud/shaders/hud_line_vert.vert | 7 +--- src/dxvk/hud/shaders/hud_text_vert.vert | 7 +--- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/dxvk/hud/dxvk_hud_renderer.cpp b/src/dxvk/hud/dxvk_hud_renderer.cpp index e6f5de15..977e9ccb 100644 --- a/src/dxvk/hud/dxvk_hud_renderer.cpp +++ b/src/dxvk/hud/dxvk_hud_renderer.cpp @@ -47,6 +47,9 @@ namespace dxvk::hud { const std::string& text) { beginTextRendering(); + const float xscale = 1.0f / std::max(float(m_surfaceSize.width), 1.0f); + const float yscale = 1.0f / std::max(float(m_surfaceSize.height), 1.0f); + uint32_t vertexCount = 6 * text.size(); auto vertexSlice = allocVertexBuffer(vertexCount * sizeof(HudTextVertex)); @@ -57,30 +60,25 @@ namespace dxvk::hud { auto vertexData = reinterpret_cast( vertexSlice.getSliceHandle().mapPtr); - const float sizeFactor = size / static_cast(g_hudFont.size); + const float sizeFactor = size / float(g_hudFont.size); for (size_t i = 0; i < text.size(); i++) { const HudGlyph& glyph = g_hudFont.glyphs[ - m_charMap[static_cast(text[i])]]; + m_charMap[uint8_t(text[i])]]; - const HudPos size = { - sizeFactor * static_cast(glyph.w), - sizeFactor * static_cast(glyph.h) }; + HudPos size = { + sizeFactor * float(glyph.w), + sizeFactor * float(glyph.h) }; - const HudPos origin = { - pos.x - sizeFactor * static_cast(glyph.originX), - pos.y - sizeFactor * static_cast(glyph.originY) }; + HudPos origin = { + pos.x - sizeFactor * float(glyph.originX), + pos.y - sizeFactor * float(glyph.originY) }; - const HudPos posTl = { origin.x, origin.y }; - const HudPos posBr = { origin.x + size.x, origin.y + size.y }; + HudPos posTl = { xscale * (origin.x), yscale * (origin.y) }; + HudPos posBr = { xscale * (origin.x + size.x), yscale * (origin.y + size.y) }; - const HudTexCoord texTl = { - static_cast(glyph.x), - static_cast(glyph.y), }; - - const HudTexCoord texBr = { - static_cast(glyph.x + glyph.w), - static_cast(glyph.y + glyph.h) }; + HudTexCoord texTl = { uint32_t(glyph.x), uint32_t(glyph.y) }; + HudTexCoord texBr = { uint32_t(glyph.x + glyph.w), uint32_t(glyph.y + glyph.h) }; vertexData[6 * i + 0].position = { posTl.x, posTl.y }; vertexData[6 * i + 0].texcoord = { texTl.u, texTl.v }; @@ -110,6 +108,9 @@ namespace dxvk::hud { const HudLineVertex* vertexData) { beginLineRendering(); + const float xscale = 1.0f / std::max(float(m_surfaceSize.width), 1.0f); + const float yscale = 1.0f / std::max(float(m_surfaceSize.height), 1.0f); + auto vertexSlice = allocVertexBuffer(vertexCount * sizeof(HudLineVertex)); m_context->bindVertexBuffer(0, vertexSlice, sizeof(HudLineVertex)); m_context->draw(vertexCount, 1, 0, 0); @@ -117,8 +118,12 @@ namespace dxvk::hud { auto dstVertexData = reinterpret_cast( vertexSlice.getSliceHandle().mapPtr); - for (size_t i = 0; i < vertexCount; i++) - dstVertexData[i] = vertexData[i]; + for (size_t i = 0; i < vertexCount; i++) { + dstVertexData[i].position = { + xscale * vertexData[i].position.x, + yscale * vertexData[i].position.y }; + dstVertexData[i].color = vertexData[i].color; + } } diff --git a/src/dxvk/hud/shaders/hud_line_vert.vert b/src/dxvk/hud/shaders/hud_line_vert.vert index 09139158..6154baf3 100644 --- a/src/dxvk/hud/shaders/hud_line_vert.vert +++ b/src/dxvk/hud/shaders/hud_line_vert.vert @@ -1,10 +1,5 @@ #version 450 -layout(set = 0, binding = 0, std140) -uniform u_hud { - uvec2 size; -} g_hud; - layout(location = 0) in vec2 v_position; layout(location = 1) in vec4 v_color; @@ -13,6 +8,6 @@ layout(location = 0) out vec4 o_color; void main() { o_color = v_color; - vec2 pos = 2.0f * (v_position / vec2(g_hud.size)) - 1.0f; + vec2 pos = 2.0f * v_position - 1.0f; gl_Position = vec4(pos, 0.0f, 1.0f); } \ No newline at end of file diff --git a/src/dxvk/hud/shaders/hud_text_vert.vert b/src/dxvk/hud/shaders/hud_text_vert.vert index 3011664e..27764283 100644 --- a/src/dxvk/hud/shaders/hud_text_vert.vert +++ b/src/dxvk/hud/shaders/hud_text_vert.vert @@ -1,10 +1,5 @@ #version 450 -layout(set = 0, binding = 0, std140) -uniform u_hud { - uvec2 size; -} g_hud; - layout(location = 0) in vec2 v_position; layout(location = 1) in uvec2 v_texcoord; @@ -13,6 +8,6 @@ layout(location = 0) out vec2 o_texcoord; void main() { o_texcoord = vec2(v_texcoord); - vec2 pos = 2.0f * (v_position / vec2(g_hud.size)) - 1.0f; + vec2 pos = 2.0f * v_position - 1.0f; gl_Position = vec4(pos, 0.0f, 1.0f); } \ No newline at end of file