vulkan: Rewrite AA shaders to respect scale
authorBenjamin Otte <otte@redhat.com>
Sat, 13 May 2023 01:14:16 +0000 (03:14 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 4 Jun 2023 17:42:01 +0000 (19:42 +0200)
The border and color shaders - the ones that do AA - now multiply their
coordinates by the scale factor, which gives them better rounding
capabilities.

This in particular improves the case where they are used in fractional
scaling situations, where the scale is defined at the root element.

gsk/vulkan/resources/border.frag
gsk/vulkan/resources/border.vert
gsk/vulkan/resources/clip.frag.glsl
gsk/vulkan/resources/color.frag
gsk/vulkan/resources/color.vert
gsk/vulkan/resources/rect.glsl
gsk/vulkan/resources/rounded-rect.glsl

index 193ccfa6eb96a902f2de740cccb5efebcbb4196a..460d8262ffa8785a2672d5be9fa7bce858ea67e8 100644 (file)
@@ -5,20 +5,18 @@
 
 layout(location = 0) in vec2 inPos;
 layout(location = 1) in vec4 inColor;
-layout(location = 2) in vec4 inRect;
-layout(location = 3) in vec4 inCornerWidths;
-layout(location = 4) in vec4 inCornerHeights;
+layout(location = 2) in RoundedRect inRect;
 layout(location = 5) in vec4 inBorderWidths;
 
 layout(location = 0) out vec4 color;
 
 void main()
 {
-  RoundedRect routside = RoundedRect (vec4(inRect.xy, inRect.xy + inRect.zw), inCornerWidths, inCornerHeights);
+  RoundedRect routside = inRect;
   RoundedRect rinside = rounded_rect_shrink (routside, inBorderWidths);
   
   float alpha = clamp (rounded_rect_coverage (routside, inPos) -
                        rounded_rect_coverage (rinside, inPos),
                        0.0, 1.0);
-  color = clip (inPos, vec4(inColor.rgb * inColor.a, inColor.a) * alpha);
+  color = clip_scaled (inPos, vec4(inColor.rgb * inColor.a, inColor.a) * alpha);
 }
index 56dfd1292d6b609426fd364fc62a3051a1f7a82a..95f0b2464525abf1ccbeeefaaa9deacc0a3e28a7 100644 (file)
@@ -1,6 +1,7 @@
 #version 420 core
 
 #include "clip.vert.glsl"
+#include "rounded-rect.glsl"
 
 layout(location = 0) in vec4 inRect;
 layout(location = 1) in vec4 inCornerWidths;
@@ -10,9 +11,7 @@ layout(location = 4) in mat4 inBorderColors;
 
 layout(location = 0) out vec2 outPos;
 layout(location = 1) out flat vec4 outColor;
-layout(location = 2) out flat vec4 outRect;
-layout(location = 3) out flat vec4 outCornerWidths;
-layout(location = 4) out flat vec4 outCornerHeights;
+layout(location = 2) out flat RoundedRect outRect;
 layout(location = 5) out flat vec4 outBorderWidths;
 
 vec2 offsets[6] = { vec2(0.0, 0.0),
@@ -97,11 +96,10 @@ void main() {
     pos = mix (rect.bounds.xy, rect.bounds.zw, offsets[vert_index]);
   else
     pos = mix (rect.bounds.zy, rect.bounds.xw, offsets[vert_index]);
-  gl_Position = push.mvp * vec4 (push.scale * pos, 0.0, 1.0);
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
   outColor = inBorderColors[((gl_VertexIndex / 3 + 15) / 4) % 4];
   outPos = pos;
-  outRect = inRect;
-  outCornerWidths = inCornerWidths;
-  outCornerHeights = inCornerHeights;
-  outBorderWidths = inBorderWidths;
+  outRect = RoundedRect(inRect.xyxy + vec4(0,0,inRect.zw), inCornerWidths, inCornerHeights);
+  outRect = rounded_rect_scale (outRect, push.scale);
+  outBorderWidths = inBorderWidths * push.scale.yxyx;
 }
index 4f4755c5bbb57e9608391cd4ba0b9b6194dece60..5ab526de868ecfe12d01dbc6ec660c7ff9615923 100644 (file)
@@ -5,25 +5,41 @@
 #define _CLIP_
 
 #ifdef CLIP_ROUNDED_RECT
-vec4 clip(vec2 pos, vec4 color)
+
+vec4
+clip_scaled (vec2 pos, vec4 color)
 {
   RoundedRect r = RoundedRect(vec4(push.clip_bounds.xy, push.clip_bounds.xy + push.clip_bounds.zw), push.clip_widths, push.clip_heights);
 
+  r = rounded_rect_scale (r, push.scale);
+
   return color * rounded_rect_coverage (r, pos);
 }
+
 #elif defined(CLIP_RECT)
-vec4 clip(vec2 pos, vec4 color)
+
+vec4
+clip_scaled (vec2 pos, vec4 color)
 {
-  /* clipped in vertex shader already */
   return color;
 }
+
 #elif defined(CLIP_NONE)
-vec4 clip(vec2 pos, vec4 color)
+
+vec4
+clip_scaled (vec2 pos, vec4 color)
 {
   return color;
 }
+
 #else
 #error "No clipping define given. Need CLIP_NONE, CLIP_RECT or CLIP_ROUNDED_RECT"
 #endif
 
+vec4
+clip (vec2 pos, vec4 color)
+{
+  return clip_scaled (pos * push.scale, color);
+}
+
 #endif
index 1f988b651751534f095a532b97004c01485ac402..e0371362a1089e66a1afd8b433a2f4d3bc54fbfd 100644 (file)
@@ -12,5 +12,5 @@ layout(location = 0) out vec4 color;
 void main()
 {
   float alpha = inColor.a * rect_coverage (inRect, inPos);
-  color = clip (inPos, vec4(inColor.rgb, 1) * alpha);
+  color = clip_scaled (inPos, vec4(inColor.rgb, 1) * alpha);
 }
index 6a09ff5f990aa20678a88dec0f225e70a0656ebe..cc2a853b5c3f1b928f67267164a1673d23a8d35f 100644 (file)
@@ -21,7 +21,7 @@ void main() {
   Rect rect = rect_round_larger (clip_rect (rect_from_gsk (inRect)));
 
   vec2 pos = mix (rect.bounds.xy, rect.bounds.zw, offsets[gl_VertexIndex]);
-  gl_Position = push.mvp * vec4 (push.scale * pos, 0.0, 1.0);
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
   outPos = pos;
   outRect = rect_from_gsk (inRect);
   outColor = inColor;
index 31c8d476f901d1f7f6dbd28cc06a23feec77e294..51a116c412afa433cbed267d5ebb171571b6e3c3 100644 (file)
@@ -11,7 +11,7 @@ struct Rect
 Rect
 rect_from_gsk (vec4 xywh)
 {
-  return Rect(xywh.xyxy + vec4(0,0,xywh.zw));
+  return Rect((xywh.xyxy + vec4(0,0,xywh.zw)) * push.scale.xyxy);
 }
 
 float
index 38e8cad9411822fb6745d96b5faaa4c11448ecbe..0f7a13e119b21f44b3c5abd1254ed7a24b2af565 100644 (file)
@@ -42,6 +42,16 @@ rounded_rect_distance (RoundedRect r, vec2 p)
   return max (max2.x, max2.y);
 }
 
+RoundedRect
+rounded_rect_scale (RoundedRect r, vec2 scale)
+{
+  r.bounds *= scale.xyxy;
+  r.corner_widths *= scale.xxxx;
+  r.corner_heights *= scale.yyyy;
+
+  return r;
+}
+
 RoundedRect
 rounded_rect_shrink (RoundedRect r, vec4 amount)
 {