diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-08 17:29:25 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-04-08 17:29:25 -0400 |
| commit | 63ce7559ce34505eec576fcf43c7cb62a814f31a (patch) | |
| tree | ddca93af438f4336fa72eac1c60cc2f9252a75ef | |
| parent | 8c0dbef6b21a0331916ae96ea5cd3b5613e50b6b (diff) | |
pke: add bounding region to glyphs for ui overdraw
| -rw-r--r-- | assets/shaders/glyph.frag | 11 | ||||
| -rw-r--r-- | assets/shaders/glyph.vert | 14 | ||||
| -rw-r--r-- | src/font.cpp | 4 | ||||
| -rw-r--r-- | src/window.cpp | 30 |
4 files changed, 47 insertions, 12 deletions
diff --git a/assets/shaders/glyph.frag b/assets/shaders/glyph.frag index 867c58c..9867925 100644 --- a/assets/shaders/glyph.frag +++ b/assets/shaders/glyph.frag @@ -2,8 +2,11 @@ layout(location = 0) in vec4 in_fg_color; layout(location = 1) in vec4 in_bg_color; -layout(location = 2) in vec2 in_uv; -layout(location = 3) in float in_width; +layout(location = 2) in vec2 in_bounding_region_min; +layout(location = 3) in vec2 in_bounding_region_max; +layout(location = 4) in vec2 in_uv; +layout(location = 5) in vec2 in_screen_px; +layout(location = 6) in float in_width; layout(location = 0) out vec4 out_color; @@ -14,6 +17,10 @@ float median(float r, float g, float b) { } void main() { + if (in_screen_px.x < in_bounding_region_min.x) discard; + if (in_screen_px.y < in_bounding_region_min.y) discard; + if (in_screen_px.x > in_bounding_region_max.x) discard; + if (in_screen_px.y > in_bounding_region_max.y) discard; vec4 msd = texture(mtsdf_sampler, in_uv); float sd = median(msd.r, msd.g, msd.b); float screenPxDistance = in_width * (sd - 0.5); diff --git a/assets/shaders/glyph.vert b/assets/shaders/glyph.vert index 3f4b28a..4275251 100644 --- a/assets/shaders/glyph.vert +++ b/assets/shaders/glyph.vert @@ -11,12 +11,17 @@ layout(location = 7) in vec4 in_fg_color; layout(location = 8) in vec4 in_bg_color; layout(location = 9) in vec2 in_sprite_region_min; layout(location = 10) in vec2 in_sprite_region_max; -layout(location = 11) in float in_width; +layout(location = 11) in vec2 in_bounding_region_min; +layout(location = 12) in vec2 in_bounding_region_max; +layout(location = 13) in float in_width; layout(location = 0) out vec4 out_fg_color; layout(location = 1) out vec4 out_bg_color; -layout(location = 2) out vec2 out_uv; -layout(location = 3) out float out_width; +layout(location = 2) out vec2 out_bounding_region_min; +layout(location = 3) out vec2 out_bounding_region_max; +layout(location = 4) out vec2 out_uv; +layout(location = 5) out vec2 out_screen_px; +layout(location = 6) out float out_width; out gl_PerVertex { @@ -29,6 +34,9 @@ void main() gl_Position = vec4(transformed_position.xy, 0.0, 1.0); out_fg_color = in_fg_color; out_bg_color = in_bg_color; + out_bounding_region_min = in_bounding_region_min; + out_bounding_region_max = in_bounding_region_max; out_uv = mix(in_sprite_region_min, in_sprite_region_max, in_uv) / in_atlas_size; + out_screen_px = gl_Position.xy; out_width = in_width; } diff --git a/src/font.cpp b/src/font.cpp index aa7fbe6..0335017 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -43,6 +43,8 @@ struct FontInstanceBufferItem { glm::vec4 bg_color; glm::vec2 sprite_region_min; glm::vec2 sprite_region_max; + glm::vec2 bounding_region_min; + glm::vec2 bounding_region_max; float width; float padding[3]; }; @@ -216,6 +218,8 @@ void FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta buf_item->bg_color = glm::vec4(0.0, 0.0, 0.0, 0.0); buf_item->sprite_region_min = fgc->sprite_region_min; buf_item->sprite_region_max = fgc->sprite_region_max; + buf_item->bounding_region_min = glm::vec2(-1.1, -1.1); + buf_item->bounding_region_max = glm::vec2(1.1, 1.1); buf_item->width = (fr->settings.char_scale / ft->msdf_settings.minimum_scale) * ft->msdf_settings.px_range; diff --git a/src/window.cpp b/src/window.cpp index 85ea8d3..4132577 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2147,7 +2147,7 @@ void CreateGraphicsPipelines() { long offset = 0; const long vertexBindingCount_glyph = 4; VkVertexInputBindingDescription vertInputBD_glyph[vertexBindingCount_glyph]; - const long vertexAttrDescCount_glyph = 12; + const long vertexAttrDescCount_glyph = 14; VkVertexInputAttributeDescription vertAttrDesc_glyph[vertexAttrDescCount_glyph]; VkPipelineVertexInputStateCreateInfo vkPipelineVertexInputStateCreateInfo_glyph{vkPipelineVertexInputStateCreateInfo_txtr}; { @@ -2172,12 +2172,14 @@ void CreateGraphicsPipelines() { // instance - total vertInputBD_glyph[index].binding = index; vertInputBD_glyph[index].stride = 0 - + sizeof(glm::mat4) - + sizeof(glm::vec4) - + sizeof(glm::vec4) - + sizeof(glm::vec2) - + sizeof(glm::vec2) - + sizeof(float) + + sizeof(glm::vec4) * 4 // mat4 pos_scale + + sizeof(glm::vec4) // in_fg_color + + sizeof(glm::vec4) // in_bg_color + + sizeof(glm::vec2) // in_sprite_region_min + + sizeof(glm::vec2) // in_sprite_region_max + + sizeof(glm::vec2) // in_bounding_region_min + + sizeof(glm::vec2) // in_bounding_region_max + + sizeof(float) // in_width + (sizeof(float) * 3) // padding + 0; vertInputBD_glyph[index].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE; @@ -2243,6 +2245,20 @@ void CreateGraphicsPipelines() { offset += sizeof(glm::vec2); index += 1; + // instance - in_bounding_region_min + vertAttrDesc_glyph[index].binding = 3; + vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; + vertAttrDesc_glyph[index].offset = offset; + offset += sizeof(glm::vec2); + index += 1; + + // instance - in_bounding_region_max + vertAttrDesc_glyph[index].binding = 3; + vertAttrDesc_glyph[index].format = VK_FORMAT_R32G32_SFLOAT; + vertAttrDesc_glyph[index].offset = offset; + offset += sizeof(glm::vec2); + index += 1; + // instance - in_width vertAttrDesc_glyph[index].binding = 3; vertAttrDesc_glyph[index].format = VK_FORMAT_R32_SFLOAT; |
