From 2237762d06b6c5049899897e31fc733ffccdcae1 Mon Sep 17 00:00:00 2001 From: jacob Date: Sat, 26 Jul 2025 04:20:41 -0500 Subject: [PATCH] vertex pull shape verts --- src/dxc.cpp | 22 ++++++++++++++++++++++ src/gp_dx12.c | 29 +++++++++++------------------ src/sh/common.hlsl | 4 ++-- src/sh/sh_common.h | 21 +++++++++++++++------ src/sh/shape.hlsl_rs | 11 ++++++----- src/sh/ui.hlsl_rs | 2 +- 6 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/dxc.cpp b/src/dxc.cpp index 60397c5e..0e036b7c 100644 --- a/src/dxc.cpp +++ b/src/dxc.cpp @@ -1,3 +1,23 @@ + +#if !RESOURCE_RELOADING + +extern "C" +{ +#include "dxc.h" +} + +struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_source, i32 num_args, struct string *args) +{ + (UNUSED)arena; + (UNUSED)shader_source; + (UNUSED)num_args; + (UNUSED)args; + struct dxc_compile_result res = ZI; + return res; +} + +#else + extern "C" { #include "dxc.h" @@ -67,3 +87,5 @@ struct dxc_compile_result dxc_compile(struct arena *arena, struct string shader_ scratch_end(scratch); return res; } + +#endif diff --git a/src/gp_dx12.c b/src/gp_dx12.c index adb5b588..01b914b5 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -36,6 +36,7 @@ #pragma comment(lib, "d3d12") #pragma comment(lib, "dxgi") #pragma comment(lib, "dxguid") +#pragma comment(lib, "d3dcompiler") #if PROFILING_GPU /* For RegOpenKeyEx */ @@ -98,7 +99,6 @@ struct pipeline_desc { struct string ps_dxc; struct string cs_dxc; - D3D12_INPUT_ELEMENT_DESC ia[8]; struct pipeline_rtv_desc rtvs[8]; }; @@ -738,8 +738,6 @@ INTERNAL void dx12_init_pipelines(void) { struct pipeline_desc *desc = arena_push(G.pipelines_arena, struct pipeline_desc); desc->name = LIT("shape"); - desc->ia[0] = (D3D12_INPUT_ELEMENT_DESC) { "pos", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }; - desc->ia[1] = (D3D12_INPUT_ELEMENT_DESC) { "color_srgb", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }; desc->rtvs[0].format = DXGI_FORMAT_R8G8B8A8_UNORM; desc->rtvs[0].blending = 1; dict_set(G.pipelines_arena, G.pipeline_descs, hash_fnv64(HASH_FNV64_BASIS, desc->name), (u64)desc); @@ -869,6 +867,8 @@ INTERNAL void dx12_init_noise(void) * Shader compilation * ========================== */ +#if RESOURCE_RELOADING + struct shader_compile_desc { struct string src; struct string friendly_name; @@ -932,6 +932,8 @@ INTERNAL SYS_JOB_DEF(shader_compile_job, job) scratch_end(scratch); } +#endif + /* ========================== * * Pipeline * ========================== */ @@ -1115,18 +1117,8 @@ INTERNAL SYS_JOB_DEF(pipeline_alloc_job, job) .ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF }; - /* Input layout */ - u32 num_input_layout_elements = 0; - for (u32 i = 0; i < countof(desc->ia); ++i) { - if (desc->ia[i].SemanticName == 0) { - break; - } - ++num_input_layout_elements; - } - D3D12_INPUT_LAYOUT_DESC input_layout_desc = { - .pInputElementDescs = desc->ia, - .NumElements = num_input_layout_elements - }; + /* Empty input layout */ + D3D12_INPUT_LAYOUT_DESC input_layout_desc = ZI; /* Blend state */ D3D12_BLEND_DESC blend_desc = { @@ -2874,11 +2866,11 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp /* Upload buffers */ u64 num_ui_shape_verts = rsig->ui_shape_verts_arena->pos / sizeof(struct sh_shape_vert); - u64 num_ui_shape_indices = rsig->ui_shape_indices_arena->pos / sizeof(u16); + u64 num_ui_shape_indices = rsig->ui_shape_indices_arena->pos / sizeof(u32); struct command_buffer *material_instance_buffer = command_list_push_buffer(cl, rsig->num_material_instance_descs, material_instances); struct command_buffer *ui_rect_instance_buffer = command_list_push_buffer(cl, rsig->num_ui_rect_instance_descs, ui_rect_instances); struct command_buffer *ui_shape_verts_buffer = command_list_push_buffer(cl, num_ui_shape_verts, (struct sh_shape_vert *)arena_base(rsig->ui_shape_verts_arena)); - struct command_buffer *ui_shape_indices_buffer = command_list_push_buffer(cl, num_ui_shape_indices, (u16 *)arena_base(rsig->ui_shape_indices_arena)); + struct command_buffer *ui_shape_indices_buffer = command_list_push_buffer(cl, num_ui_shape_indices, (u32 *)arena_base(rsig->ui_shape_indices_arena)); struct command_buffer *grid_buffer = command_list_push_buffer(cl, rsig->num_material_grid_descs, grids); /* Upload descriptor heap */ @@ -3178,11 +3170,12 @@ struct gp_resource *gp_run_render(struct gp_render_sig *gp_render_sig, struct gp /* Set sig */ struct sh_shape_sig sig = ZI; sig.projection = sh_float4x4_from_mat4x4(ui_vp_matrix); + sig.verts_urid = sh_uint_from_u32(ui_shape_verts_buffer->resource->srv_descriptor->index); command_list_set_sig(cl, &sig, sizeof(sig)); /* Draw */ u32 index_count = ui_shape_indices_buffer->size / sizeof(u32); - D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(ui_shape_verts_buffer, sizeof(struct sh_shape_vert)); + D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(ui_shape_indices_buffer, DXGI_FORMAT_R32_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); diff --git a/src/sh/common.hlsl b/src/sh/common.hlsl index ce10a916..5be9c381 100644 --- a/src/sh/common.hlsl +++ b/src/sh/common.hlsl @@ -13,7 +13,7 @@ # define INLINE /* For intellisense */ #endif -INLINE float4 float4_norm_from_uint(uint v) +INLINE float4 float4_from_uint_norm(uint v) { float4 res; res.r = ((v >> 0) & 0xFF) / 255.0; @@ -32,7 +32,7 @@ INLINE float4 linear_from_srgb(float4 srgb) /* Linear color from R8G8B8A8 sRGB */ INLINE float4 linear_from_srgb32(uint srgb32) { - return linear_from_srgb(float4_norm_from_uint(srgb32)); + return linear_from_srgb(float4_from_uint_norm(srgb32)); } /* ========================== * diff --git a/src/sh/sh_common.h b/src/sh/sh_common.h index b8c1f862..5231ad08 100644 --- a/src/sh/sh_common.h +++ b/src/sh/sh_common.h @@ -104,7 +104,8 @@ SH_STRUCT(sh_material_sig { /* ----------------------------------------------------- */ SH_DECL(uint, instances_urid); /* 01 consts */ SH_DECL(uint, grids_urid); /* 01 consts */ - SH_DECL(uint2, _pad0); /* 02 consts (padding) */ + SH_DECL(uint, _pad0); /* 01 consts (padding) */ + SH_DECL(uint, _pad1); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_material_sig, 20); @@ -144,7 +145,8 @@ SH_STRUCT(sh_flood_sig { /* ----------------------------------------------------- */ SH_DECL(uint, tex_width); /* 01 consts */ SH_DECL(uint, tex_height); /* 01 consts */ - SH_DECL(uint2, _pad0); /* 02 consts (padding) */ + SH_DECL(uint, _pad0); /* 01 consts (padding) */ + SH_DECL(uint, _pad1); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_flood_sig, 8); @@ -185,12 +187,17 @@ SH_STRUCT(sh_shape_sig { /* ----------------------------------------------------- */ SH_DECL(float4x4, projection); /* 16 consts */ /* ----------------------------------------------------- */ + SH_DECL(uint, verts_urid); /* 01 consts */ + SH_DECL(uint, _pad0); /* 01 consts (padding) */ + SH_DECL(uint, _pad1); /* 01 consts (padding) */ + SH_DECL(uint, _pad2); /* 01 consts (padding) */ + /* ----------------------------------------------------- */ }); -SH_ASSERT_ROOT_CONST(struct sh_shape_sig, 16); +SH_ASSERT_ROOT_CONST(struct sh_shape_sig, 20); SH_STRUCT(sh_shape_vert { - SH_DECLS(float2, pos); - SH_DECLS(uint, color_srgb); + SH_DECL(float2, pos); + SH_DECL(uint, color_srgb); }); /* ========================== * @@ -202,7 +209,9 @@ SH_STRUCT(sh_ui_sig { SH_DECL(float4x4, projection); /* 16 consts */ /* ----------------------------------------------------- */ SH_DECL(uint, instances_urid); /* 01 consts */ - SH_DECL(uint3, _pad0); /* 03 consts (padding) */ + SH_DECL(uint, _pad0); /* 01 consts (padding) */ + SH_DECL(uint, _pad1); /* 01 consts (padding) */ + SH_DECL(uint, _pad2); /* 01 consts (padding) */ /* ----------------------------------------------------- */ }); SH_ASSERT_ROOT_CONST(struct sh_ui_sig, 20); diff --git a/src/sh/shape.hlsl_rs b/src/sh/shape.hlsl_rs index 810147b7..57139204 100644 --- a/src/sh/shape.hlsl_rs +++ b/src/sh/shape.hlsl_rs @@ -1,14 +1,13 @@ #include "common.hlsl" -ConstantBuffer sig : register(b0); +ConstantBuffer sig : register(b0); /* ========================== * * Vertex shader * ========================== */ struct vs_input { - DECLS(float2, pos); - DECLS(float4, color_srgb); + DECLS(uint, SV_VertexID); }; struct vs_output { @@ -18,9 +17,11 @@ struct vs_output { SH_ENTRY struct vs_output vs(struct vs_input input) { + StructuredBuffer verts = resource_from_urid(sig.verts_urid); + struct sh_shape_vert vert = verts[input.SV_VertexID]; struct vs_output output; - output.SV_Position = mul(sig.projection, float4(input.pos.xy, 0, 1)); - output.color_srgb = input.color_srgb; + output.SV_Position = mul(sig.projection, float4(vert.pos.xy, 0, 1)); + output.color_srgb = float4_from_uint_norm(vert.color_srgb); return output; } diff --git a/src/sh/ui.hlsl_rs b/src/sh/ui.hlsl_rs index fabd9c00..eaf6d0bf 100644 --- a/src/sh/ui.hlsl_rs +++ b/src/sh/ui.hlsl_rs @@ -36,7 +36,7 @@ SH_ENTRY struct vs_output vs(struct vs_input input) output.SV_Position = mul(sig.projection, float4(world_pos, 0, 1)); output.tex_nurid = instance.tex_nurid; output.uv = instance.uv0 + ((vert + 0.5) * (instance.uv1 - instance.uv0)); - output.tint_srgb = float4_norm_from_uint(instance.tint_srgb); + output.tint_srgb = float4_from_uint_norm(instance.tint_srgb); return output; }