make arbitrary triangles work with a command queue

This commit is contained in:
Tuomas Katajisto 2025-05-02 14:56:03 +03:00
parent 0d3c8106cf
commit b0045107cb
2 changed files with 74 additions and 15 deletions

View File

@ -1,20 +1,51 @@
Arb_Tri :: struct {
pos: [3]Vector3;
col: [3]Vector4;
uv: [3]Vector2;
pos : [3]Vector3;
col : [3]Vector4;
uv : [3]Vector2;
}
Arb_Draw_Command_Type :: enum {
INVALID;
FLUSH_TRI;
SET_TEXTURE;
REMOVE_TEXTURE;
}
Arb_Draw_Command :: struct {
type : Arb_Draw_Command_Type = .INVALID;
tri_offset : int = 0;
tri_count : int = 0;
texture : *Ui_Texture;
}
Arb_Tri_State :: struct {
active : bool = false;
trilist : [..]Arb_Tri;
command_list : [..]Arb_Draw_Command;
latest_flush : int = 0;
}
arbTriState : Arb_Tri_State;
init_arb_state :: () {
gPipelines.arbtri.bind.images[0] = default_texture.tex;
array_reset_keeping_memory(*arbTriState.trilist);
array_reset_keeping_memory(*arbTriState.command_list);
arbTriState.active = true;
arbTriState.latest_flush = 0;
}
arb_tri_command_add :: (command: Arb_Draw_Command) {
if !arbTriState.active {
init_arb_state();
}
array_add(*arbTriState.command_list, command);
}
arb_tri_add :: (tri: Arb_Tri) {
if !arbTriState.active {
array_reset_keeping_memory(*arbTriState.trilist);
arbTriState.active = true;
init_arb_state();
}
array_add(*arbTriState.trilist, tri);
@ -55,7 +86,21 @@ arb_tri_flush :: () {
sg_update_buffer(gPipelines.arbtri.bind.vertex_buffers[0], *(sg_range.{ ptr = gArbtriMem.data, size = size_of(type_of(gArbtriMem)) }));
flush_arb_commands();
}
flush_arb_commands :: () {
for arbTriState.command_list {
if it.type == {
case .SET_TEXTURE;
gPipelines.arbtri.bind.images[0] = it.texture.tex;
gCurrentTexture = it.texture;
case .REMOVE_TEXTURE;
gCurrentTexture = null;
case .FLUSH_TRI;
sg_apply_pipeline(gPipelines.arbtri.pipeline);
sg_apply_bindings(*gPipelines.arbtri.bind);
sg_draw(0, xx (arbTriState.trilist.count * 3), 1);
sg_draw(xx (it.tri_offset * 3), xx (it.tri_count * 3), 1);
}
}
}

View File

@ -1,6 +1,7 @@
GR :: #import "GetRect_LeftHanded"()(Type_Indicator = Ui_Type_Indicator);
Input :: #import "Input";
default_texture : *Ui_Texture = null;
Ui_Font_Glyph :: struct {
advance : u32 = 1;
@ -76,6 +77,7 @@ texture_load_from_memory :: (texture: *Ui_Texture, memory: []u8, srgb: bool, bui
stbi.stbi_image_free(data);
texture.tex = img;
if !default_texture then default_texture = texture;
return true;
}
@ -96,15 +98,22 @@ clear_scissor :: () {
}
gCurrentTexture : *Ui_Texture = null;
add_uvs : bool = false;
set_shader_for_color :: (enable_blend := false) {
gCurrentTexture = null;
arb_tri_command_add(.{
type = .REMOVE_TEXTURE
});
add_uvs = false;
}
set_shader_for_images :: (texture: *Ui_Texture) {
gPipelines.arbtri.bind.images[0] = texture.tex;
gCurrentTexture = texture;
arb_tri_command_add(.{
type = .SET_TEXTURE,
texture = texture
});
add_uvs = true;
}
gPreppedText: string;
@ -163,7 +172,7 @@ immediate_triangle :: (p0: Vector3, p1: Vector3, p2: Vector3, c0 := Vector4.{1,1
// This UV symbolizes that the sampler should not be used.
nullUV : Vector2 = .{-4, -2};
if gCurrentTexture == null {
if !add_uvs {
tri.uv[0] = nullUV;
tri.uv[1] = nullUV;
tri.uv[2] = nullUV;
@ -185,7 +194,12 @@ immediate_quad :: (p0: Vector2, p1: Vector2, p2: Vector2, p3: Vector2, color :=
}
immediate_flush :: () {
// arb_tri_flush();
arb_tri_command_add(.{
type = .FLUSH_TRI,
tri_offset = arbTriState.latest_flush,
tri_count = arbTriState.trilist.count - arbTriState.latest_flush
});
arbTriState.latest_flush = arbTriState.trilist.count;
}
init_ui :: () {