vello/piet-gpu/shader/image.comp
Raph Levien 2ed89dd65e First draft of kernel 1
Output of kernel 1 is validated by simple inspection, next step is to
wire it up properly.
2020-04-20 18:07:18 -07:00

55 lines
1.7 KiB
Plaintext

// A simple kernel to create an image.
// Right now, this kernel stores the image in a buffer, but a better
// plan is to use a texture. This is because of limited support.
#version 450
#extension GL_GOOGLE_include_directive : enable
layout(local_size_x = 16, local_size_y = 16) in;
layout(set = 0, binding = 0) readonly buffer SceneBuf {
uint[] scene;
};
layout(set = 0, binding = 1) buffer ImageBuf {
uint[] image;
};
#include "scene.h"
// TODO: make the image size dynamic.
#define IMAGE_WIDTH 2048
#define IMAGE_HEIGHT 1535
void main() {
uvec2 xy_uint = gl_GlobalInvocationID.xy;
vec2 xy = vec2(xy_uint);
vec2 uv = xy * vec2(1.0 / IMAGE_WIDTH, 1.0 / IMAGE_HEIGHT);
vec3 rgb = uv.xyy;
// Render the scene. Right now, every pixel traverses the scene graph,
// which is horribly wasteful, but the goal is to get *some* output and
// then optimize.
SimpleGroup group = PietItem_Group_read(PietItemRef(0));
for (uint i = 0; i < group.n_items; i++) {
PietItemRef item_ref = PietItem_index(group.items, i);
uint tag = PietItem_tag(item_ref);
tag = PietItem_Circle;
if (tag == PietItem_Circle) {
PietCircle circle = PietItem_Circle_read(item_ref);
float r = length(xy + vec2(0.5, 0.5) - circle.center.xy);
float alpha = clamp(circle.radius - r, 0.0, 1.0);
vec4 fg_rgba = unpackUnorm4x8(circle.rgba_color);
// TODO: sRGB
rgb = mix(rgb, fg_rgba.rgb, alpha * fg_rgba.a);
}
}
// TODO: sRGB
uvec4 s = uvec4(round(vec4(rgb, 1.0) * 255.0));
uint rgba_packed = s.r | (s.g << 8) | (s.b << 16) | (s.a << 24);
image[xy_uint.y * IMAGE_WIDTH + xy_uint.x] = rgba_packed;
}