gpu: Turn globals into macros

This way, we can be more flexible in refactoring how we handle globals
(guess what we're gonna do next).
This commit is contained in:
Benjamin Otte
2023-10-05 03:17:51 +02:00
parent 74ac95dc32
commit 64a67ac3a8
9 changed files with 32 additions and 27 deletions

View File

@@ -46,7 +46,7 @@
*
* 3. the scaled coordinate system
* converts on CPU: NodeProcessor.scale
* converts on GPU: push.scale
* converts on GPU: GSK_GLOBAL_SCALE
* This includes the current scale of the transform. It is usually equal to the scale factor
* of the window we are rendering to (which is bad because devs without hidpi screens can
* forget this and then everyone else will see bugs). We make decisions about pixel sizes in
@@ -65,7 +65,7 @@
*
* 5. the GL coordinate system
* converts on CPU: NodeProcessor.projection
* converts on GPU: push.mvp (from scaled coordinate system)
* converts on GPU: GSK_GLOBAL_MVP (from scaled coordinate system)
* This coordinate system is what GL (or Vulkan) expect coordinates to appear in, and is usually
* (-1, -1) => (1, 1), but may be flipped etc depending on the render target. The CPU essentially
* never uses it, other than to allow the vertex shaders to emit its vertices.

View File

@@ -8,12 +8,15 @@ layout(std140, binding = 0)
uniform PushConstants
{
mat4 mvp;
vec4 clip_bounds;
vec4 clip_widths;
vec4 clip_heights;
mat3x4 clip;
vec2 scale;
} push;
#define GSK_GLOBAL_MVP push.mvp
#define GSK_GLOBAL_CLIP push.clip
#define GSK_GLOBAL_CLIP_RECT push.clip[0]
#define GSK_GLOBAL_SCALE push.scale
#if defined(GSK_GLES) && __VERSION__ < 310
layout(std140)
#else

View File

@@ -4,14 +4,17 @@
layout(push_constant) uniform PushConstants {
mat4 mvp;
vec4 clip_bounds;
vec4 clip_widths;
vec4 clip_heights;
mat3x4 clip;
vec2 scale;
} push;
layout(constant_id=0) const uint GSK_SHADER_CLIP = GSK_GPU_SHADER_CLIP_NONE;
#define GSK_GLOBAL_MVP push.mvp
#define GSK_GLOBAL_CLIP push.clip
#define GSK_GLOBAL_CLIP_RECT push.clip[0]
#define GSK_GLOBAL_SCALE push.scale
#define GSK_VERTEX_INDEX gl_VertexIndex
#ifdef GSK_VERTEX_SHADER

View File

@@ -25,7 +25,7 @@ rect_clip (Rect r)
if (GSK_SHADER_CLIP == GSK_GPU_SHADER_CLIP_NONE)
return r;
else
return rect_intersect (r, rect_from_gsk (push.clip_bounds));
return rect_intersect (r, rect_from_gsk (GSK_GLOBAL_CLIP_RECT));
}
#ifdef GSK_VERTEX_SHADER
@@ -40,7 +40,7 @@ const vec2 offsets[6] = vec2[6](vec2(0.0, 0.0),
void
gsk_set_position (vec2 pos)
{
gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
gl_Position = GSK_GLOBAL_MVP * vec4 (pos, 0.0, 1.0);
}
vec2
@@ -145,7 +145,7 @@ main_clip_rect (void)
run (color, pos);
Rect clip = rect_from_gsk (push.clip_bounds);
Rect clip = rect_from_gsk (GSK_GLOBAL_CLIP_RECT);
float coverage = rect_coverage (clip, pos);
color *= coverage;
@@ -161,8 +161,7 @@ main_clip_rounded (void)
run (color, pos);
RoundedRect clip = RoundedRect(vec4(push.clip_bounds.xy, push.clip_bounds.xy + push.clip_bounds.zw), push.clip_widths, push.clip_heights);
rounded_rect_scale (clip, push.scale);
RoundedRect clip = rounded_rect_from_gsk (GSK_GLOBAL_CLIP);
float coverage = rounded_rect_coverage (clip, pos);
color *= coverage;

View File

@@ -34,8 +34,8 @@ run (out vec2 pos)
_tex_coord = rect_get_coord (tex_rect, pos);
_tex_id = in_tex_id;
float blur_radius = length (push.scale * in_blur_direction);
_tex_blur_step = push.scale * in_blur_direction / blur_radius / rect_size (tex_rect);
float blur_radius = length (GSK_GLOBAL_SCALE * in_blur_direction);
_tex_blur_step = GSK_GLOBAL_SCALE * in_blur_direction / blur_radius / rect_size (tex_rect);
_samples_per_side = uint (floor (blur_radius));
float sigma = blur_radius / 2.0;
_initial_gaussian.x = 1.0 / (sqrt (2.0 * PI) * sigma);

View File

@@ -54,7 +54,7 @@ compute_color (void)
void
run (out vec2 pos)
{
vec4 border_widths = in_border_widths * push.scale.yxyx;
vec4 border_widths = in_border_widths * GSK_GLOBAL_SCALE.yxyx;
RoundedRect outside = rounded_rect_from_gsk (in_outline);
RoundedRect inside = rounded_rect_shrink (outside, border_widths);
rounded_rect_offset (inside, in_offset);

View File

@@ -203,7 +203,7 @@ glyphs_pattern (inout uint reader,
float coverage = rect_coverage (glyph_bounds, pos);
if (coverage > 0.0)
opacity += coverage * gsk_texture (tex_id, (pos - push.scale * tex_rect.xy) / (push.scale * tex_rect.zw)).a;
opacity += coverage * gsk_texture (tex_id, (pos - GSK_GLOBAL_SCALE * tex_rect.xy) / (GSK_GLOBAL_SCALE * tex_rect.zw)).a;
}
return color * opacity;
@@ -216,7 +216,7 @@ texture_pattern (inout uint reader,
uint tex_id = read_uint (reader);
vec4 tex_rect = read_vec4 (reader);
return gsk_texture (tex_id, (pos - push.scale * tex_rect.xy) / (push.scale * tex_rect.zw));
return gsk_texture (tex_id, (pos - GSK_GLOBAL_SCALE * tex_rect.xy) / (GSK_GLOBAL_SCALE * tex_rect.zw));
}
vec4
@@ -224,8 +224,8 @@ linear_gradient_pattern (inout uint reader,
vec2 pos,
bool repeating)
{
vec2 start = read_vec2 (reader) * push.scale;
vec2 end = read_vec2 (reader) * push.scale;
vec2 start = read_vec2 (reader) * GSK_GLOBAL_SCALE;
vec2 end = read_vec2 (reader) * GSK_GLOBAL_SCALE;
Gradient gradient = read_gradient (reader);
vec2 line = end - start;
@@ -244,8 +244,8 @@ radial_gradient_pattern (inout uint reader,
vec2 pos,
bool repeating)
{
vec2 center = read_vec2 (reader) * push.scale;
vec2 radius = read_vec2 (reader) * push.scale;
vec2 center = read_vec2 (reader) * GSK_GLOBAL_SCALE;
vec2 radius = read_vec2 (reader) * GSK_GLOBAL_SCALE;
float start = read_float (reader);
float end = read_float (reader);
Gradient gradient = read_gradient (reader);
@@ -269,7 +269,7 @@ conic_gradient_pattern (inout uint reader,
Gradient gradient = read_gradient (reader);
/* scaling modifies angles, so be sure to use right coordinate system */
pos = pos / push.scale - center;
pos = pos / GSK_GLOBAL_SCALE - center;
float offset = atan (pos.y, pos.x);
offset = degrees (offset + angle) / 360.0;
float overflow = fract (offset + 0.5);

View File

@@ -18,7 +18,7 @@ Rect
rect_from_gsk (vec4 coords)
{
Rect result = rect_new_size (coords);
result.bounds *= push.scale.xyxy;
result.bounds *= GSK_GLOBAL_SCALE.xyxy;
return result;
}

View File

@@ -14,9 +14,9 @@ struct RoundedRect
RoundedRect
rounded_rect_from_gsk (mat3x4 gsk_rounded_rect)
{
return RoundedRect ((gsk_rounded_rect[0].xyxy + vec4 (0.0, 0.0, gsk_rounded_rect[0].zw)) * push.scale.xyxy,
gsk_rounded_rect[1] * push.scale.xxxx,
gsk_rounded_rect[2] * push.scale.yyyy);
return RoundedRect ((gsk_rounded_rect[0].xyxy + vec4 (0.0, 0.0, gsk_rounded_rect[0].zw)) * GSK_GLOBAL_SCALE.xyxy,
gsk_rounded_rect[1] * GSK_GLOBAL_SCALE.xxxx,
gsk_rounded_rect[2] * GSK_GLOBAL_SCALE.yyyy);
}
float