From 7a4554ba99a4ecb27413b9e6fd275370c92d9cd8 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 23 Jun 2025 15:08:41 -0500 Subject: [PATCH] clear render target --- res/sh/material.hlsl | 6 +++--- res/sh/sh_common.h | 11 +---------- src/gp.h | 7 ++++++- src/gp_dx12.c | 32 ++++++++++++++++++++------------ src/user.c | 1 + 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/res/sh/material.hlsl b/res/sh/material.hlsl index ae38f056..1129a1e7 100644 --- a/res/sh/material.hlsl +++ b/res/sh/material.hlsl @@ -17,7 +17,7 @@ cbuffer cb : register(b0) { struct sh_material_constants g_constants; }; StructuredBuffer g_instances : register(t0); -Texture2D g_textures[] : register(t1); +Texture2D g_nuri_textures[] : register(t1); SamplerState g_sampler : register(s0); /* ========================== * @@ -45,7 +45,7 @@ SH_ENTRY(ROOTSIG) struct vs_output vs(struct vs_input input) float2(-0.5f, 0.5f) }; - struct sh_material_instance instance = g_instances[g_constants.instance_offset + input.SV_InstanceID]; + struct sh_material_instance instance = g_instances[input.SV_InstanceID]; float2 vert = unit_quad_verts[input.SV_VertexID]; float2 world_pos = mul(instance.xf, float3(vert, 1)).xy; @@ -72,6 +72,6 @@ struct ps_output { SH_ENTRY(ROOTSIG) struct ps_output ps(struct ps_input input) { struct ps_output output; - output.SV_Target = g_textures[NURI(input.vs.texture_nuri)].Sample(g_sampler, input.vs.uv) * input.vs.tint_lin; + output.SV_Target = g_nuri_textures[NURI(input.vs.texture_nuri)].Sample(g_sampler, input.vs.uv) * input.vs.tint_lin; return output; } diff --git a/res/sh/sh_common.h b/res/sh/sh_common.h index dd2d4e6b..9ddbd6dd 100644 --- a/res/sh/sh_common.h +++ b/res/sh/sh_common.h @@ -1,9 +1,5 @@ #if SH_CPU -/* ========================== * - * CPU - * ========================== */ - #define SH_STRUCT(s) PACK(struct s) #define SH_DECL(t, n) struct CAT(sh_, t) n #define SH_ENTRY(rootsig) static @@ -46,13 +42,9 @@ INLINE struct sh_float2x3 sh_float2x3_from_xform(struct xform v) #else -/* ========================== * - * GPU - * ========================== */ - #define SH_STRUCT(s) struct s #define SH_DECL(t, n) t n -# define SH_ENTRY(rootsig) [RootSignature(rootsig)] +#define SH_ENTRY(rootsig) [RootSignature(rootsig)] #endif @@ -62,7 +54,6 @@ INLINE struct sh_float2x3 sh_float2x3_from_xform(struct xform v) SH_STRUCT(sh_material_constants { SH_DECL(float4x4, projection); - SH_DECL(uint, instance_offset); }); SH_STRUCT(sh_material_instance { diff --git a/src/gp.h b/src/gp.h index d9e68178..e4a268ef 100644 --- a/src/gp.h +++ b/src/gp.h @@ -32,6 +32,10 @@ struct gp_handle_array { struct gp_handle **handles; }; +/* NOTE: Internally, the layer will make sure to not release any resources + * until after the GPU finishes using them. However, it is up to the caller + * to make sure the released resources aren't referenced in any flows before + * dispatching. */ void gp_release(struct gp_handle handle); /* ========================== * @@ -114,6 +118,7 @@ struct gp_dispatch_params { struct gp_handle draw_target; struct rect draw_target_viewport; struct xform draw_target_view; + b32 clear_target; }; struct gp_handle gp_flow_alloc(void); @@ -126,7 +131,7 @@ void gp_dispatch(struct gp_dispatch_params params); * Present * ========================== */ -/* 1. Ensures the backbuffer is at size `backbuffer_resolution` +/* 1. Clears the backbuffer and ensures its at size `backbuffer_resolution` * 2. Blits `texture` to the backbuffer using `texture_xf` (applied to centered unit square) * 3. Presents the backbuffer */ void gp_present(struct sys_window *window, struct v2i32 backbuffer_resolution, struct gp_handle texture, struct xform texture_xf, i32 vsync); diff --git a/src/gp_dx12.c b/src/gp_dx12.c index d2ff45b9..36f9d6f4 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -2006,7 +2006,18 @@ void gp_dispatch(struct gp_dispatch_params params) /* Create temporary descriptor heap */ /* NOTE: This should always occur after buffers are submitted */ - /* Push commands */ + + /* Transition render target */ + enum D3D12_RESOURCE_STATES target_old_state = dx12_resource_barrier(cl->cl, target, D3D12_RESOURCE_STATE_RENDER_TARGET); + { + ID3D12GraphicsCommandList_OMSetRenderTargets(cl->cl, 1, &target->rtv_descriptor->handle, false, NULL); + if (params.clear_target) { + f32 clear_color[] = { 0.0f, 0.0f, 0.0f, 0.0f }; + ID3D12GraphicsCommandList_ClearRenderTargetView(cl->cl, target->rtv_descriptor->handle, clear_color, 0, NULL); + } + } + + /* Material pass */ { //struct pipeline *pipeline = dx12_get_pipeline(pipeline_scope, LIT("material")); struct pipeline *pipeline = &G.test_pipeline; @@ -2017,9 +2028,9 @@ void gp_dispatch(struct gp_dispatch_params params) ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, pipeline->rootsig); /* Fill & bind constant buffer */ + /* TODO: Move into root constant */ struct sh_material_constants constants = ZI; constants.projection = sh_float4x4_from_mat4x4(vp_matrix); - constants.instance_offset = sh_uint_from_u32(0); struct command_buffer *constant_buffer = command_list_push_buffer(cl, STRING_FROM_STRUCT(&constants)); ID3D12GraphicsCommandList_SetGraphicsRootConstantBufferView(cl->cl, 0, constant_buffer->resource->gpu_address); @@ -2035,20 +2046,16 @@ void gp_dispatch(struct gp_dispatch_params params) ID3D12GraphicsCommandList_RSSetViewports(cl->cl, 1, &d3d12_viewport); ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &d3d12_scissor); - /* Transition render target */ - enum D3D12_RESOURCE_STATES old_state = dx12_resource_barrier(cl->cl, target, D3D12_RESOURCE_STATE_RENDER_TARGET); - ID3D12GraphicsCommandList_OMSetRenderTargets(cl->cl, 1, &target->rtv_descriptor->handle, false, NULL); - //f32 clear_color[] = { 0.0f, 0.0f, 0.0f, 0.0f }; - //ID3D12GraphicsCommandList_ClearRenderTargetView(cl->cl, target->rtv_descriptor->handle, clear_color, 0, NULL); - /* Draw */ ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &dummy_vertex_buffer_view); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &quad_index_buffer_view); ID3D12GraphicsCommandList_DrawIndexedInstanced(cl->cl, 6, instance_count, 0, 0, 0); + } - /* Reset render target */ - dx12_resource_barrier(cl->cl, target, old_state); + /* Reset render target */ + { + dx12_resource_barrier(cl->cl, target, target_old_state); } } command_list_close(cl); @@ -2067,7 +2074,6 @@ void gp_dispatch(struct gp_dispatch_params params) * Present * ========================== */ -#if 0 #if GSTAT_ENABLED || PROFILING INTERNAL void query_memory_info(void) { @@ -2116,7 +2122,6 @@ INTERNAL void query_memory_info(void) { } #endif -#endif INTERNAL struct dx12_resource *update_swapchain(struct sys_window *window, struct v2i32 resolution) { @@ -2218,11 +2223,14 @@ INTERNAL void present_blit(struct dx12_resource *dst, struct dx12_resource *src, params.draw_target = dst_texture_handle; params.draw_target_viewport = RECT_FROM_V2(V2(0, 0), V2(dst->texture_size.x, dst->texture_size.y)); params.draw_target_view = XFORM_IDENT; + params.clear_target = true; gp_dispatch(params); } void gp_present(struct sys_window *window, struct v2i32 backbuffer_resolution, struct gp_handle texture, struct xform texture_xf, i32 vsync) { + query_memory_info(); + //sys_sleep(0.1); struct dx12_resource *backbuffer_resource = update_swapchain(window, backbuffer_resolution); diff --git a/src/user.c b/src/user.c index 7eb7030d..be580b5f 100644 --- a/src/user.c +++ b/src/user.c @@ -2079,6 +2079,7 @@ INTERNAL void user_update(void) params.draw_target = G.user_texture; params.draw_target_viewport = user_viewport; params.draw_target_view = G.world_to_user_xf; + params.clear_target = true; gp_dispatch(params); }