Compare commits

...

7 Commits

9 changed files with 272 additions and 219 deletions

View File

@ -68,7 +68,7 @@ const Rect DIV_FILL = { .x = 0, .y = 0, .w = 0, .h = 0 };
const uint STACK_STEP = 10; const uint STACK_STEP = 10;
const uint MAX_ELEMS = 128; const uint MAX_ELEMS = 128;
const uint MAX_CMDS = 256; const uint MAX_CMDS = 2048;
const uint ROOT_ID = 1; const uint ROOT_ID = 1;
const uint TEXT_MAX = 64; const uint TEXT_MAX = 64;
@ -197,7 +197,7 @@ fn void? Ctx.init(&ctx)
ctx.cache.init()!; ctx.cache.init()!;
defer catch { (void)ctx.cache.free(); } defer catch { (void)ctx.cache.free(); }
ctx.cmd_queue.init(MAX_ELEMENTS)!; ctx.cmd_queue.init(MAX_CMDS)!;
defer catch { (void)ctx.cmd_queue.free(); } defer catch { (void)ctx.cmd_queue.free(); }
ctx.active_div = 0; ctx.active_div = 0;

View File

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

View File

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

View File

@ -4,56 +4,24 @@ layout(set = 3, binding = 0) uniform Viewport {
ivec2 view; ivec2 view;
}; };
layout(location = 0) in vec4 color; layout(location = 0) in vec4 in_color;
layout(location = 1) in vec2 local_position; layout(location = 1) in vec4 in_quad_size; // x,y, w,h
layout(location = 2) in vec2 global_position; layout(location = 2) in float in_radius;
layout(location = 3) in float radius;
layout(location = 0) out vec4 fragColor; layout(location = 0) out vec4 fragColor;
// SDF for a rounded rectangle given the centerpoint, half size and radius, all in pixels // 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) { float sdf_rr(vec2 p, 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
vec2 q = abs(p) - half_size + 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; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius;
} }
void main() void main()
{ {
// local_position are normalized coordinates in the rectangle, passed from the vec2 centerpoint = in_quad_size.xy + in_quad_size.zw * 0.5;
// vertex shader vec2 half_size = in_quad_size.zw * 0.5;
/* float distance = sdf_rr(vec2(gl_FragCoord) - centerpoint, half_size, in_radius);
* Window float alpha = 1.0 - smoothstep(0.0, 1.0, distance);
* +-----------------------+
* | (1,1) |
* | +----------x |
* | | | |
* | | | |
* | | Rect | |
* | | | |
* | | | |
* | x----------+ |
* | (-1,-1) |
* | |
* +-----------------------+
*/
vec2 dx = dFdx(local_position); fragColor = vec4(in_color.rgb, in_color.a * alpha);
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);
} }

View File

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

View File

@ -12,7 +12,7 @@ layout(set = 2, binding = 0) uniform sampler2D tx;
void main() void main()
{ {
ivec2 ts = textureSize(tx, 0); ivec2 ts = textureSize(tx, 0);
vec2 fts = vec2(float(ts.x), float(ts.y)); vec2 fts = vec2(ts);
vec2 real_uv = uv / fts; vec2 real_uv = uv / fts;
fragColor = texture(tx, real_uv); 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 = 0) in ivec2 position;
layout(location = 1) in ivec2 in_uv; layout(location = 1) in ivec4 attr; // quad x,y,w,h
layout(location = 2) in ivec4 color; layout(location = 2) in ivec2 in_uv;
layout(location = 3) in ivec4 color;
layout(location = 0) out vec2 out_uv; layout(location = 0) out vec2 out_uv;
layout(location = 1) out vec4 out_color; layout(location = 1) out vec4 out_color;
void main() void main()
{ {
vec2 pos; // vertex position
pos.x = float(position.x)*2.0 / view.x - 1.0; ivec2 px_pos = attr.xy + position.xy * attr.zw;
pos.y = -(float(position.y)*2.0 / view.y - 1.0); vec2 clip_pos;
gl_Position = vec4(pos, 0.0, 1.0); 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; out_color = vec4(color) / 255.0;
} }

View File

@ -61,7 +61,7 @@ fn int main(String[] args)
defer ui.free(); defer ui.free();
ren::Renderer ren; ren::Renderer ren;
ren.init("Ugui Test", 640, 480); ren.init("Ugui Test", 640, 480, false);
defer ren.free(); defer ren.free();
ui.input_window_size(640, 480)!!; ui.input_window_size(640, 480)!!;
@ -113,8 +113,10 @@ fn int main(String[] args)
isz frame; isz frame;
double fps;
bool toggle = true; bool toggle = true;
time::Clock clock; time::Clock clock;
time::Clock fps_clock;
Times ui_times; Times ui_times;
Times draw_times; Times draw_times;
@ -126,6 +128,7 @@ fn int main(String[] args)
bool quit = false; bool quit = false;
while (!quit) { while (!quit) {
clock.mark(); clock.mark();
fps_clock.mark();
// FIXME: modkeys input is broken // FIXME: modkeys input is broken
ugui::ModKeys mod; ugui::ModKeys mod;
@ -268,6 +271,7 @@ fn int main(String[] args)
ui.div_begin("fps", {0, ui.height-100, -300, 100})!!; ui.div_begin("fps", {0, ui.height-100, -300, 100})!!;
{ {
ui.layout_set_column()!!; ui.layout_set_column()!!;
ui.text_unbounded("frame number", string::tformat("frame %d, fps = %.2f", frame, fps))!!;
ui.text_unbounded("draw times", string::tformat("ui avg: %s\ndraw avg: %s\nTOT: %s", uts.avg, dts.avg, uts.avg+dts.avg))!!; ui.text_unbounded("draw times", string::tformat("ui avg: %s\ndraw avg: %s\nTOT: %s", uts.avg, dts.avg, uts.avg+dts.avg))!!;
ui.text_unbounded("ui text input", (String)ui.input.keyboard.text[..])!!; ui.text_unbounded("ui text input", (String)ui.input.keyboard.text[..])!!;
}; };
@ -290,6 +294,10 @@ fn int main(String[] args)
draw_times.push(clock.mark()); draw_times.push(clock.mark());
//draw_times.print_stats(); //draw_times.print_stats();
/* End Drawing */ /* End Drawing */
fps = 1.0 / fps_clock.mark().to_sec();
frame++;
} }
return 0; return 0;

View File

@ -51,14 +51,20 @@ struct Texture {
} }
// The GPU buffers that contain quad info, the size is determined by MAX_QUAD_BATCH // The GPU buffers that contain quad info, the size is determined by MAX_QUAD_BATCH
const int MAX_QUAD_BATCH = 256; const int MAX_QUAD_BATCH = 2048;
struct QuadBuffer { struct QuadBuffer {
sdl::GPUBuffer* vert_buf; sdl::GPUBuffer* vert_buf; // on-gpu vertex buffer
sdl::GPUBuffer* idx_buf; sdl::GPUBuffer* idx_buf; // on-gpu index buffer
sdl::GPUTransferBuffer* transfer_buffer; sdl::GPUBuffer* attr_buf; // on-gpu quad attribute buffer
bool initialized;
sdl::GPUTransferBuffer* attr_ts;
QuadAttributes[] attr_ts_mapped;
// how many quads are currently stored
int count; int count;
int off; // the offset to draw from
bool initialized;
} }
alias ShaderList = IdList{Shader}; alias ShaderList = IdList{Shader};
@ -79,26 +85,28 @@ struct Renderer {
Id sprite_atlas_id; Id sprite_atlas_id;
Id font_atlas_id; Id font_atlas_id;
uint scissor_x, scissor_y, scissor_w, scissor_h;
} }
// how each vertex is represented in the gpu // How each vertex is represented in the gpu
struct Vertex @packed { struct Vertex {
short x, y;
}
// Attributes of each quad instance
struct QuadAttributes {
struct pos { struct pos {
short x, y; short x, y, w, h;
} }
struct uv { struct uv {
short u, v; short u, v;
} }
struct col { // FIXME: this is shit uint color;
union {
char r, g, b, a;
char[4] arr;
uint u;
}
}
} }
struct Quad @packed { // A single quad
struct Quad {
struct vertices { struct vertices {
Vertex v1,v2,v3,v4; Vertex v1,v2,v3,v4;
} }
@ -114,7 +122,7 @@ struct ViewsizeUniform @align(16) {
const int DEBUG = 1; const int DEBUG = 1;
const bool CYCLE = true; const bool CYCLE = true;
fn void Renderer.init(&self, ZString title, uint width, uint height) fn void Renderer.init(&self, ZString title, uint width, uint height, bool vsync)
{ {
// set wayland hint automagically // set wayland hint automagically
$if DEBUG == 0: $if DEBUG == 0:
@ -159,33 +167,115 @@ $endif
} }
// set swapchain parameters, like vsync // set swapchain parameters, like vsync
sdl::set_gpu_swapchain_parameters(self.gpu, self.win, GPU_SWAPCHAINCOMPOSITION_SDR, GPU_PRESENTMODE_IMMEDIATE); GPUPresentMode present_mode = vsync ? GPU_PRESENTMODE_VSYNC : GPU_PRESENTMODE_IMMEDIATE;
sdl::set_gpu_swapchain_parameters(self.gpu, self.win, GPU_SWAPCHAINCOMPOSITION_SDR, present_mode);
// //
// initialize the quad buffer // initialize the quad buffer
// ========================== // ==========================
self.quad_buffer.vert_buf = sdl::create_gpu_buffer(self.gpu, QuadBuffer* qb = &self.quad_buffer;
&&(GPUBufferCreateInfo){.usage = GPU_BUFFERUSAGE_VERTEX, .size = Quad.vertices.sizeof * MAX_QUAD_BATCH}
// since instanced rendering is used, on the gpu there is only one mesh, a single quad.
// create the vertex and index buffer on the gpu
qb.vert_buf = sdl::create_gpu_buffer(self.gpu,
&&(GPUBufferCreateInfo){.usage = GPU_BUFFERUSAGE_VERTEX, .size = Quad.vertices.sizeof}
); );
if (self.quad_buffer.vert_buf == null) { if (qb.vert_buf == null) {
unreachable("failed to initialize quad buffer (vertex): %s", sdl::get_error()); unreachable("failed to initialize quad buffer (vertex): %s", sdl::get_error());
} }
self.quad_buffer.idx_buf = sdl::create_gpu_buffer(self.gpu, qb.idx_buf = sdl::create_gpu_buffer(self.gpu,
&&(GPUBufferCreateInfo){.usage = GPU_BUFFERUSAGE_INDEX, .size = Quad.indices.sizeof * MAX_QUAD_BATCH} &&(GPUBufferCreateInfo){.usage = GPU_BUFFERUSAGE_INDEX, .size = Quad.indices.sizeof}
); );
if (self.quad_buffer.idx_buf == null) { if (qb.idx_buf == null) {
unreachable("failed to initialize quad buffer (index): %s", sdl::get_error()); unreachable("failed to initialize quad buffer (index): %s", sdl::get_error());
} }
self.quad_buffer.transfer_buffer = sdl::create_gpu_transfer_buffer(self.gpu, qb.attr_buf = sdl::create_gpu_buffer(self.gpu,
&&(GPUTransferBufferCreateInfo){.usage = GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = Quad.sizeof} &&(GPUBufferCreateInfo){.usage = GPU_BUFFERUSAGE_VERTEX, .size = QuadAttributes.sizeof * MAX_QUAD_BATCH}
); );
if (self.quad_buffer.transfer_buffer == null) { if (qb.attr_buf == null) {
unreachable("failed to create gpu transfer buffer: %s", sdl::get_error()); unreachable("failed to initialize quad buffer (index): %s", sdl::get_error());
} }
self.quad_buffer.initialized = true;
// upload the quad mesh
GPUTransferBuffer *ts = sdl::create_gpu_transfer_buffer(self.gpu,
&&(GPUTransferBufferCreateInfo){.usage = GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = Quad.sizeof}
);
if (ts == null) {
unreachable("failed to create gpu transfer buffer: %s", sdl::get_error());
}
Quad* quad = (Quad*)sdl::map_gpu_transfer_buffer(self.gpu, ts, false);
/* v1 v4
* +-------------+
* | _/|
* | _/ |
* | 1 _/ |
* | _/ |
* | _/ |
* | _/ 2 |
* |/ |
* +-------------+
* v2 v3
*/
quad.vertices.v1 = {.x = 0, .y = 0};
quad.vertices.v2 = {.x = 0, .y = 1};
quad.vertices.v3 = {.x = 1, .y = 1};
quad.vertices.v4 = {.x = 1, .y = 0};
// triangle 1 indices
quad.indices.i1 = 0; // v1
quad.indices.i2 = 1; // v2
quad.indices.i3 = 3; // v4
// triangle 2 indices
quad.indices.i4 = 1; // v2
quad.indices.i5 = 2; // v3
quad.indices.i6 = 3; // v4
sdl::unmap_gpu_transfer_buffer(self.gpu, ts);
GPUCommandBuffer* cmd = sdl::acquire_gpu_command_buffer(self.gpu);
if (cmd == null) {
unreachable("failed to upload quad at acquiring command buffer: %s", sdl::get_error());
}
GPUCopyPass* cpy = sdl::begin_gpu_copy_pass(cmd);
// upload vertices
sdl::upload_to_gpu_buffer(cpy,
&&(GPUTransferBufferLocation){.transfer_buffer = ts, .offset = Quad.vertices.offsetof},
&&(GPUBufferRegion){.buffer = qb.vert_buf, .offset = 0, .size = Quad.vertices.sizeof},
false
);
// upload indices
sdl::upload_to_gpu_buffer(cpy,
&&(GPUTransferBufferLocation){.transfer_buffer = ts, .offset = Quad.indices.offsetof},
&&(GPUBufferRegion){.buffer = qb.idx_buf, .offset = 0, .size = Quad.indices.sizeof},
false
);
sdl::end_gpu_copy_pass(cpy);
if (!sdl::submit_gpu_command_buffer(cmd)) {
unreachable("failed to upload quads at submit command buffer: %s", sdl::get_error());
}
sdl::release_gpu_transfer_buffer(self.gpu, ts);
// create and map the quad attributes transfer buffer
qb.attr_ts = sdl::create_gpu_transfer_buffer(self.gpu,
&&(GPUTransferBufferCreateInfo){.usage = GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = QuadAttributes.sizeof * MAX_QUAD_BATCH}
);
if (qb.attr_ts == null) {
unreachable("failed to create gpu transfer buffer: %s", sdl::get_error());
}
qb.attr_ts_mapped = ((QuadAttributes*)sdl::map_gpu_transfer_buffer(self.gpu, qb.attr_ts, false))[:MAX_QUAD_BATCH];
if (qb.attr_ts_mapped.ptr == null) {
unreachable("failed to map vertex or index buffers: %s", sdl::get_error());
}
qb.initialized = true;
} }
fn void Renderer.free(&self) fn void Renderer.free(&self)
@ -326,35 +416,47 @@ fn void Renderer.create_pipeline(&self, String shader_name, PipelineType type)
// is represented by two floats, the color as 32 bit rgba and the uv also as intgers. // is represented by two floats, the color as 32 bit rgba and the uv also as intgers.
.vertex_input_state = { .vertex_input_state = {
// the description of each vertex buffer, for now I use only one buffer // the description of each vertex buffer, for now I use only one buffer
.vertex_buffer_descriptions = (GPUVertexBufferDescription[]){{ .vertex_buffer_descriptions = (GPUVertexBufferDescription[]){
.slot = 0, { // first slot, per-vertex attributes
.pitch = Vertex.sizeof, .slot = 0,
.input_rate = GPU_VERTEXINPUTRATE_VERTEX, .pitch = Vertex.sizeof,
.instance_step_rate = 0, .input_rate = GPU_VERTEXINPUTRATE_VERTEX,
}}, },
.num_vertex_buffers = 1, { // second slot, per-instance attributes
// the description of each vertex, each vertex has three properties .slot = 1,
.pitch = QuadAttributes.sizeof,
.input_rate = GPU_VERTEXINPUTRATE_INSTANCE,
}
},
.num_vertex_buffers = 2,
// the description of each vertex
.vertex_attributes = (GPUVertexAttribute[]){ .vertex_attributes = (GPUVertexAttribute[]){
{ // at location zero there is the position of the vertex { // at location zero there is the position of the vertex
.location = 0, .location = 0,
.buffer_slot = 0, // only one buffer so always slot zero .buffer_slot = 0, // buffer slot zero so per-vertex
.format = GPU_VERTEXELEMENTFORMAT_SHORT2, .format = GPU_VERTEXELEMENTFORMAT_SHORT2, // x,y
.offset = Vertex.pos.offsetof, .offset = 0,
}, },
{ // at location one there are the uv coordinates { // at location one there is the per-quad position
.location = 1, .location = 1,
.buffer_slot = 0, .buffer_slot = 1, // buffer slot one so per-instance
.format = GPU_VERTEXELEMENTFORMAT_SHORT2, .format = GPU_VERTEXELEMENTFORMAT_SHORT4, // x,y,w,h
.offset = Vertex.uv.offsetof, .offset = QuadAttributes.pos.offsetof,
}, },
{ // at location two there is the color { // at location two there are the per-quad uv coordinates
.location = 2, .location = 2,
.buffer_slot = 0, .buffer_slot = 1,
.format = GPU_VERTEXELEMENTFORMAT_UBYTE4, // 4x8bit unsigned rgba format .format = GPU_VERTEXELEMENTFORMAT_SHORT2,
.offset = Vertex.col.offsetof, .offset = QuadAttributes.uv.offsetof,
},
{ // at location three there is the quad color
.location = 3,
.buffer_slot = 1,
.format = GPU_VERTEXELEMENTFORMAT_UBYTE4,
.offset = QuadAttributes.color.offsetof,
} }
}, },
.num_vertex_attributes = 3, .num_vertex_attributes = 4,
}, },
// the pipeline's primitive type and rasterizer state differs based on what needs to // the pipeline's primitive type and rasterizer state differs based on what needs to
// be drawn // be drawn
@ -527,73 +629,31 @@ fn void Renderer.update_texture_by_id(&self, Id id, char[] pixels, uint width, u
fn bool Renderer.push_sprite(&self, short x, short y, short w, short h, short u, short v, uint color = 0xffffffff) fn bool Renderer.push_sprite(&self, short x, short y, short w, short h, short u, short v, uint color = 0xffffffff)
{ {
Quad quad; QuadAttributes qa = {
/* v1 v4 .pos = {.x = x, .y = y, .w = w, .h = h},
* +-------------+ .uv = {.u = u, .v = v},
* | _/| .color = color
* | _/ | };
* | 1 _/ |
* | _/ |
* | _/ |
* | _/ 2 |
* |/ |
* +-------------+
* v2 v3
*/
quad.vertices.v1 = {.pos = {.x = x, .y = y}, .uv = {.u = u, .v = v}, .col.u = color};
quad.vertices.v2 = {.pos = {.x = x, .y = y+h}, .uv = {.u = u, .v = v+h}, .col.u = color};
quad.vertices.v3 = {.pos = {.x = x+w, .y = y+h}, .uv = {.u = u+w, .v = v+h}, .col.u = color};
quad.vertices.v4 = {.pos = {.x = x+w, .y = y}, .uv = {.u = u+w, .v = v}, .col.u = color};
// triangle 1 indices
quad.indices.i1 = 0; // v1
quad.indices.i2 = 1; // v2
quad.indices.i3 = 3; // v4
// triangle 2 indices
quad.indices.i4 = 1; // v2
quad.indices.i5 = 2; // v3
quad.indices.i6 = 3; // v4
return self.upload_quad(&quad); return self.map_quad(qa);
} }
// Push a quad into the quad buffer, return true on success and false on failure
fn bool Renderer.push_quad(&self, short x, short y, short w, short h, uint color, ushort radius = 0) fn bool Renderer.push_quad(&self, short x, short y, short w, short h, uint color, ushort radius = 0)
{ {
Quad quad; QuadAttributes qa = {
/* v1 v4 .pos = {.x = x, .y = y, .w = w, .h = h},
* +-------------+ .uv = {.u = radius, .v = radius},
* | _/| .color = color
* | _/ | };
* | 1 _/ |
* | _/ |
* | _/ |
* | _/ 2 |
* |/ |
* +-------------+
* v2 v3
*/
// the wanted radius is pushed into the uv coordinates, the vertex shader then extracts the absolute value
// and passes it to the fragment shader, then it uses the sign to give the fragment shader local coordinates
// into the quad.
quad.vertices.v1 = {.pos = {.x = x, .y = y}, .uv = {.u = -radius, .v = +radius}, .col.u = color};
quad.vertices.v2 = {.pos = {.x = x, .y = y+h}, .uv = {.u = -radius, .v = -radius}, .col.u = color};
quad.vertices.v3 = {.pos = {.x = x+w, .y = y+h}, .uv = {.u = +radius, .v = -radius}, .col.u = color};
quad.vertices.v4 = {.pos = {.x = x+w, .y = y}, .uv = {.u = +radius, .v = +radius}, .col.u = color};
// triangle 1 indices
quad.indices.i1 = 0; // v1
quad.indices.i2 = 1; // v2
quad.indices.i3 = 3; // v4
// triangle 2 indices
quad.indices.i4 = 1; // v2
quad.indices.i5 = 2; // v3
quad.indices.i6 = 3; // v4
return self.upload_quad(&quad); return self.map_quad(qa);
} }
fn bool Renderer.upload_quad(&self, Quad* source_quad) // this does not upload a quad, but it simply copies the quad data to the correct transfer buffers.
// Data transfer to the GPU only happens in draw_quads() to save time
fn bool Renderer.map_quad(&self, QuadAttributes qa)
{ {
if (self.quad_buffer.count >= MAX_QUAD_BATCH || source_quad == null) { if (self.quad_buffer.count >= MAX_QUAD_BATCH) {
return false; return false;
} }
QuadBuffer* qb = &self.quad_buffer; QuadBuffer* qb = &self.quad_buffer;
@ -603,14 +663,16 @@ fn bool Renderer.upload_quad(&self, Quad* source_quad)
unreachable("quad buffer not initialized"); unreachable("quad buffer not initialized");
} }
Quad* quad = (Quad*)sdl::map_gpu_transfer_buffer(self.gpu, qb.transfer_buffer, CYCLE); qb.attr_ts_mapped[qb.count] = qa;
if (quad == null) {
unreachable("failed to map gpu transfer buffer: %s", sdl::get_error());
}
*quad = *source_quad; qb.count++;
sdl::unmap_gpu_transfer_buffer(self.gpu, qb.transfer_buffer); return true;
}
fn void Renderer.upload_quads(&self)
{
QuadBuffer* qb = &self.quad_buffer;
GPUCommandBuffer* cmd = sdl::acquire_gpu_command_buffer(self.gpu); GPUCommandBuffer* cmd = sdl::acquire_gpu_command_buffer(self.gpu);
if (cmd == null) { if (cmd == null) {
@ -618,16 +680,10 @@ fn bool Renderer.upload_quad(&self, Quad* source_quad)
} }
GPUCopyPass* cpy = sdl::begin_gpu_copy_pass(cmd); GPUCopyPass* cpy = sdl::begin_gpu_copy_pass(cmd);
// upload vertices // upload quad attributes
sdl::upload_to_gpu_buffer(cpy, sdl::upload_to_gpu_buffer(cpy,
&&(GPUTransferBufferLocation){.transfer_buffer = qb.transfer_buffer, .offset = Quad.vertices.offsetof}, &&(GPUTransferBufferLocation){.transfer_buffer = qb.attr_ts, .offset = 0},
&&(GPUBufferRegion){.buffer = qb.vert_buf, .offset = qb.count * Quad.vertices.sizeof, .size = Quad.vertices.sizeof}, &&(GPUBufferRegion){.buffer = qb.attr_buf, .offset = 0, .size = QuadAttributes.sizeof * qb.count},
false
);
// upload indices
sdl::upload_to_gpu_buffer(cpy,
&&(GPUTransferBufferLocation){.transfer_buffer = qb.transfer_buffer, .offset = Quad.indices.offsetof},
&&(GPUBufferRegion){.buffer = qb.idx_buf, .offset = qb.count * Quad.indices.sizeof, .size = Quad.indices.sizeof},
false false
); );
@ -635,35 +691,32 @@ fn bool Renderer.upload_quad(&self, Quad* source_quad)
if (!sdl::submit_gpu_command_buffer(cmd)) { if (!sdl::submit_gpu_command_buffer(cmd)) {
unreachable("failed to upload quads at submit command buffer: %s", sdl::get_error()); unreachable("failed to upload quads at submit command buffer: %s", sdl::get_error());
} }
qb.count++;
return true;
} }
// draw all quads in the quad buffer, since uniforms are per-drawcall it makes no sense // draw all quads in the quad buffer, since uniforms are per-drawcall it makes no sense
// to draw them one a the time // to draw them one a the time
fn void Renderer.draw_quads(&self) fn void Renderer.draw_quads(&self, uint off, uint count)
{ {
QuadBuffer* qb = &self.quad_buffer; QuadBuffer* qb = &self.quad_buffer;
if (qb.off == qb.count) return; // too many quads to draw
if (off >= qb.count || count > qb.count - off) {
sdl::bind_gpu_vertex_buffers(self.render_pass, 0, (GPUBufferBinding[]){{.buffer = qb.vert_buf, .offset = qb.off*Quad.vertices.sizeof}}, 1); unreachable("too many quads, have %d, requested %d, offset %d", qb.count, count, off);
sdl::bind_gpu_index_buffer(self.render_pass, &&(GPUBufferBinding){.buffer = qb.idx_buf, .offset = qb.off*Quad.indices.sizeof}, GPU_INDEXELEMENTSIZE_16BIT);
// we need instancing to not do this
for (int i = 0; i < qb.count - qb.off; i++) {
sdl::draw_gpu_indexed_primitives(self.render_pass, 6, 1, i*6, i*4, 0);
} }
qb.off = qb.count; sdl::bind_gpu_vertex_buffers(self.render_pass, 0,
(GPUBufferBinding[]){
{.buffer = qb.vert_buf, .offset = 0},
{.buffer = qb.attr_buf, .offset = 0},
}, 2);
sdl::bind_gpu_index_buffer(self.render_pass, &&(GPUBufferBinding){.buffer = qb.idx_buf, .offset = 0}, GPU_INDEXELEMENTSIZE_16BIT);
sdl::draw_gpu_indexed_primitives(self.render_pass, 6, count, 0, 0, off);
} }
fn void Renderer.reset_quads(&self) fn void Renderer.reset_quads(&self)
{ {
self.quad_buffer.count = 0; self.quad_buffer.count = 0;
self.quad_buffer.off = 0;
} }
@ -793,35 +846,53 @@ fn void Renderer.reset_scissor(&self)
fn void Renderer.render_ugui(&self, CmdQueue* queue) fn void Renderer.render_ugui(&self, CmdQueue* queue)
{ {
// upload pass
foreach (&c : queue) {
if (c.type == CMD_RECT) {
CmdRect r = c.rect;
self.push_quad(r.rect.x, r.rect.y, r.rect.w, r.rect.h, r.color.to_uint(), r.radius);
} else if (c.type == CMD_SPRITE) {
CmdSprite s = c.sprite;
self.push_sprite(s.rect.x, s.rect.y, s.texture_rect.w, s.texture_rect.h, s.texture_rect.x, s.texture_rect.y, s.hue.to_uint());
}
}
self.upload_quads();
Cmd* last_command; Cmd* last_command;
uint off;
uint count;
for (Cmd* cmd; (cmd = queue.dequeue() ?? null) != null;) { for (Cmd* cmd; (cmd = queue.dequeue() ?? null) != null;) {
if (last_command == null || last_command.type != cmd.type) { if (last_command == null || last_command.type != cmd.type) {
self.end_command(last_command); self.end_command(last_command, off, count);
self.begin_command(cmd); self.begin_command(cmd);
off += count;
count = 0;
} }
switch (cmd.type) { switch (cmd.type) {
case CMD_RECT: case CMD_RECT: nextcase;
CmdRect r = cmd.rect;
self.push_quad(r.rect.x, r.rect.y, r.rect.w, r.rect.h, r.color.to_uint(), r.radius);
case CMD_SPRITE: case CMD_SPRITE:
// TODO: support hue in sprite count++;
CmdSprite s = cmd.sprite;
self.push_sprite(s.rect.x, s.rect.y, s.texture_rect.w, s.texture_rect.h, s.texture_rect.x, s.texture_rect.y);
case CMD_UPDATE_ATLAS: case CMD_UPDATE_ATLAS:
// TODO: verify the correct type // TODO: verify the correct type
CmdUpdateAtlas u = cmd.update_atlas; CmdUpdateAtlas u = cmd.update_atlas;
char[] pixels = u.raw_buffer[..u.width*u.height*u.bpp]; char[] pixels = u.raw_buffer[..u.width*u.height*u.bpp];
self.update_texture(u.id, pixels, u.width, u.height); self.update_texture(u.id, pixels, u.width, u.height);
case CMD_SCISSOR: break; // FIXME: ugui sends a scissor event before any rect event, this cannot be done and needs different handling case CMD_SCISSOR:
ugui::Rect s = cmd.scissor.rect; ugui::Rect s = cmd.scissor.rect;
self.set_scissor(s.x, s.y, s.w, s.h); if (s.x == 0 && s.y == 0 && s.w == 0 && s.h == 0) {
self.get_window_size((int*)&s.w, (int*)&s.h);
}
self.scissor_x = s.x;
self.scissor_y = s.y;
self.scissor_w = s.w;
self.scissor_h = s.h;
default: unreachable("unknown command: %s", cmd.type); default: unreachable("unknown command: %s", cmd.type);
} }
last_command = cmd; last_command = cmd;
} }
self.end_command(last_command); self.end_command(last_command, off, count);
} }
fn void Renderer.begin_command(&self, Cmd* cmd) fn void Renderer.begin_command(&self, Cmd* cmd)
@ -831,6 +902,7 @@ fn void Renderer.begin_command(&self, Cmd* cmd)
switch (cmd.type) { switch (cmd.type) {
case CMD_RECT: case CMD_RECT:
self.start_render_pass("UGUI_PIPELINE_RECT"); self.start_render_pass("UGUI_PIPELINE_RECT");
self.set_scissor(self.scissor_x, self.scissor_y, self.scissor_w, self.scissor_h);
case CMD_SPRITE: case CMD_SPRITE:
// TODO: support multiple sprite and font atlases // TODO: support multiple sprite and font atlases
CmdSprite s = cmd.sprite; CmdSprite s = cmd.sprite;
@ -851,20 +923,21 @@ fn void Renderer.begin_command(&self, Cmd* cmd)
} }
self.start_render_pass(pipeline); self.start_render_pass(pipeline);
self.bind_texture(texture); self.bind_texture(texture);
self.set_scissor(self.scissor_x, self.scissor_y, self.scissor_w, self.scissor_h);
case CMD_UPDATE_ATLAS: break; case CMD_UPDATE_ATLAS: break;
case CMD_SCISSOR: break; case CMD_SCISSOR: break;
default: unreachable("unknown command: %s", cmd.type); default: unreachable("unknown command: %s", cmd.type);
} }
} }
fn void Renderer.end_command(&self, Cmd* cmd) fn void Renderer.end_command(&self, Cmd* cmd, uint off, uint count)
{ {
if (cmd == null) return; if (cmd == null) return;
switch (cmd.type) { switch (cmd.type) {
case CMD_RECT: nextcase; case CMD_RECT: nextcase;
case CMD_SPRITE: case CMD_SPRITE:
self.draw_quads(); self.draw_quads(off, count);
self.end_render_pass(); self.end_render_pass();
case CMD_UPDATE_ATLAS: break; case CMD_UPDATE_ATLAS: break;
case CMD_SCISSOR: break; case CMD_SCISSOR: break;