implemented instanced rendering

This commit is contained in:
Alessandro Mauri 2025-06-15 18:54:35 +02:00
parent 0ef2bceeec
commit f30db0dd47
9 changed files with 190 additions and 203 deletions

View file

@ -13,7 +13,7 @@ layout(location = 0) out vec4 fragColor;
void main()
{
ivec2 ts = textureSize(tx, 0);
vec2 fts = vec2(float(ts.x), float(ts.y));
vec2 fts = vec2(ts);
vec2 real_uv = uv / fts;
vec4 opacity = texture(tx, real_uv);

View file

@ -24,7 +24,7 @@ float median(float r, float g, float b) {
void main() {
ivec2 ts = textureSize(tx, 0);
vec2 fts = vec2(float(ts.x), float(ts.y));
vec2 fts = vec2(ts);
vec2 real_uv = uv / fts;
vec3 msd = texture(tx, real_uv).rgb;

View file

@ -4,56 +4,24 @@ layout(set = 3, binding = 0) uniform Viewport {
ivec2 view;
};
layout(location = 0) in vec4 color;
layout(location = 1) in vec2 local_position;
layout(location = 2) in vec2 global_position;
layout(location = 3) in float radius;
layout(location = 0) in vec4 in_color;
layout(location = 1) in vec4 in_quad_size; // x,y, w,h
layout(location = 2) in float in_radius;
layout(location = 0) out vec4 fragColor;
// SDF for a rounded rectangle given the centerpoint, half size and radius, all in pixels
float sdf_rr(vec2 p, vec2 center, vec2 half_size, float radius) {
// Translate fragment position to rectangle's coordinate system
p -= center;
// Adjust for rounded corners: shrink the rectangle by the radius
float sdf_rr(vec2 p, vec2 half_size, float radius) {
vec2 q = abs(p) - half_size + radius;
// Combine distance components:
// - max(q, 0.0) handles regions outside the rounded corners
// - min(max(q.x, q.y), 0.0) handles regions inside the rectangle
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius;
}
void main()
{
// local_position are normalized coordinates in the rectangle, passed from the
// vertex shader
/*
* Window
* +-----------------------+
* | (1,1) |
* | +----------x |
* | | | |
* | | | |
* | | Rect | |
* | | | |
* | | | |
* | x----------+ |
* | (-1,-1) |
* | |
* +-----------------------+
*/
vec2 centerpoint = in_quad_size.xy + in_quad_size.zw * 0.5;
vec2 half_size = in_quad_size.zw * 0.5;
float distance = sdf_rr(vec2(gl_FragCoord) - centerpoint, half_size, in_radius);
float alpha = 1.0 - smoothstep(0.0, 1.0, distance);
vec2 dx = dFdx(local_position);
vec2 dy = dFdy(local_position);
// Conversion from normalized coordinates to pixels
vec2 norm_to_px = 1.0 / vec2(length(dx), length(dy));
vec2 centerpoint = global_position - local_position * norm_to_px;
// the half size of the rectangle is also norm_to_px
vec2 half_size = 1.0 * norm_to_px;
float distance = sdf_rr(global_position, centerpoint, half_size, radius);
float alpha = 1.0 - smoothstep(0.0, 1.0, max(distance, 0.0));
fragColor = vec4(color.rgb, color.a * alpha);
fragColor = vec4(in_color.rgb, in_color.a * alpha);
}

View file

@ -5,26 +5,25 @@ layout(set = 1, binding = 0) uniform Viewport {
};
layout(location = 0) in ivec2 position;
layout(location = 1) in ivec2 uv;
layout(location = 2) in ivec4 color;
layout(location = 1) in ivec4 attr; // quad x,y,w,h
layout(location = 2) in ivec2 uv; // x,y in the texture
layout(location = 3) in ivec4 color;
layout(location = 0) out vec4 col;
layout(location = 1) out vec2 local_position;
layout(location = 2) out vec2 global_position;
layout(location = 3) out float radius;
layout(location = 0) out vec4 out_color;
layout(location = 1) out vec4 out_quad_size;
layout(location = 2) out float out_radius;
void main()
{
// vertex position
vec2 pos;
pos.x = float(position.x)*2.0 / view.x - 1.0;
pos.y = -(float(position.y)*2.0 / view.y - 1.0);
ivec2 px_pos = attr.xy + position.xy * attr.zw;
vec2 clip_pos;
clip_pos.x = float(px_pos.x)*2.0 / view.x - 1.0;
clip_pos.y = -(float(px_pos.y)*2.0 / view.y - 1.0);
gl_Position = vec4(pos, 0.0, 1.0);
gl_Position = vec4(clip_pos, 0.0, 1.0);
local_position = vec2(sign(uv));
global_position = gl_Position.xy;
radius = abs(float(uv.x));
col = vec4(color) / 255.0;
out_color = vec4(color) / 255.0;
out_quad_size = vec4(attr);
out_radius = float(abs(uv.x));
}

View file

@ -12,7 +12,7 @@ layout(set = 2, binding = 0) uniform sampler2D tx;
void main()
{
ivec2 ts = textureSize(tx, 0);
vec2 fts = vec2(float(ts.x), float(ts.y));
vec2 fts = vec2(ts);
vec2 real_uv = uv / fts;
fragColor = texture(tx, real_uv);
}

View file

@ -5,19 +5,24 @@ layout(set = 1, binding = 0) uniform Viewport {
};
layout(location = 0) in ivec2 position;
layout(location = 1) in ivec2 in_uv;
layout(location = 2) in ivec4 color;
layout(location = 1) in ivec4 attr; // quad x,y,w,h
layout(location = 2) in ivec2 in_uv;
layout(location = 3) in ivec4 color;
layout(location = 0) out vec2 out_uv;
layout(location = 1) out vec4 out_color;
void main()
{
vec2 pos;
pos.x = float(position.x)*2.0 / view.x - 1.0;
pos.y = -(float(position.y)*2.0 / view.y - 1.0);
gl_Position = vec4(pos, 0.0, 1.0);
// vertex position
ivec2 px_pos = attr.xy + position.xy * attr.zw;
vec2 clip_pos;
clip_pos.x = float(px_pos.x)*2.0 / view.x - 1.0;
clip_pos.y = -(float(px_pos.y)*2.0 / view.y - 1.0);
out_uv = vec2(float(in_uv.x), float(in_uv.y));
gl_Position = vec4(clip_pos, 0.0, 1.0);
vec2 px_uv = in_uv.xy + position.xy * attr.zw;
out_uv = vec2(px_uv);
out_color = vec4(color) / 255.0;
}