diff --git a/.gitignore b/.gitignore index 6da73f25..4deddbd1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,7 @@ .vs/* .vscode/* +imgui.ini + unused/ build/ diff --git a/imgui.ini b/imgui.ini deleted file mode 100644 index 4a5c2014..00000000 --- a/imgui.ini +++ /dev/null @@ -1,5 +0,0 @@ -[Window][Debug##Default] -Pos=60,60 -Size=400,400 -Collapsed=0 - diff --git a/res/sh/shape.hlsl b/res/sh/shape.hlsl index 55b041ee..d9460d77 100644 --- a/res/sh/shape.hlsl +++ b/res/sh/shape.hlsl @@ -5,38 +5,30 @@ * ========================== */ #define ROOTSIG \ - "RootConstants(num32BitConstants=16, b0), " \ - "SRV(t0), " \ - "SRV(t1), " \ - "DescriptorTable(SRV(t2, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)), " \ - "StaticSampler(s0, " \ - "filter = FILTER_MIN_MAG_MIP_POINT, " \ - "addressU = TEXTURE_ADDRESS_CLAMP, " \ - "addressV = TEXTURE_ADDRESS_CLAMP, " \ - "addressW = TEXTURE_ADDRESS_CLAMP, " \ - "maxAnisotropy = 1)" + "RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), " \ + "RootConstants(num32BitConstants=16, b0)" ConstantBuffer g_constants : register(b0); -StructuredBuffer g_instances : register(t0); -StructuredBuffer g_grids : register(t1); -Texture2D g_textures[] : register(t2); - -SamplerState g_sampler : register(s0); /* ========================== * * Vertex shader * ========================== */ +struct vs_input { + DECLS(float2, pos); + DECLS(float4, color_srgb); +}; + struct vs_output { DECLS(float4, SV_Position); DECLS(float4, color_lin); }; -SH_ENTRY(ROOTSIG) struct vs_output vs(struct sh_shape_vert input) +SH_ENTRY(ROOTSIG) struct vs_output vs(struct vs_input input) { struct vs_output output; - output.SV_Position = mul(g_constants.projection, float4(input.pos, 0, 1)); - output.color_lin = linear_from_srgb32(input.color_srgb); + output.SV_Position = mul(g_constants.projection, float4(input.pos.xy, 0, 1)); + output.color_lin = linear_from_srgb(input.color_srgb); return output; } diff --git a/src/gp_dx12.c b/src/gp_dx12.c index bbefe7ad..fcfdb8f4 100644 --- a/src/gp_dx12.c +++ b/src/gp_dx12.c @@ -74,7 +74,7 @@ struct pipeline_desc { struct string name; struct shader_desc vs; struct shader_desc ps; - u32 flags; + D3D12_INPUT_ELEMENT_DESC ia[8]; }; struct pipeline { @@ -94,9 +94,9 @@ struct pipeline { /* Lock global pipelines mutex when accessing */ i64 refcount; - struct pipeline_desc desc; ID3D12PipelineState *pso; ID3D12RootSignature *rootsig; + struct pipeline_desc desc; }; struct pipeline_error { @@ -702,6 +702,8 @@ INTERNAL void dx12_init_pipelines(void) desc->ps.file = LIT("sh/shape.hlsl"); desc->vs.func = LIT("vs"); desc->ps.func = LIT("ps"); + 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 }; dict_set(G.pipelines_arena, G.pipeline_descs, hash_fnv64(HASH_FNV64_BASIS, desc->name), (u64)desc); } /* Blit pipeilne */ @@ -784,12 +786,6 @@ INTERNAL HRESULT dx12_include_open(ID3DInclude *d3d_handler, D3D_INCLUDE_TYPE in result = S_OK; } -#if 0 -#if RESOURCE_RELOADING - shader_add_include(&G.shader_info[handler->shader->kind], name); -#endif -#endif - return result; } @@ -927,7 +923,7 @@ INTERNAL WORK_TASK_FUNC_DEF(pipeline_load_task, load_arg_raw) { __prof; struct pipeline *pipeline = (struct pipeline *)load_arg_raw; - struct pipeline_desc desc = pipeline->desc; + struct pipeline_desc *desc = &pipeline->desc; struct arena_temp scratch = scratch_begin_no_conflict(); { @@ -939,21 +935,21 @@ INTERNAL WORK_TASK_FUNC_DEF(pipeline_load_task, load_arg_raw) struct string error_str = LIT("Unknown error"); - b32 ps_res_is_shared = string_eq(desc.vs.file, desc.ps.file); - struct resource vs_res = resource_open(desc.vs.file); + b32 ps_res_is_shared = string_eq(desc->vs.file, desc->ps.file); + struct resource vs_res = resource_open(desc->vs.file); struct resource ps_res = vs_res; if (!ps_res_is_shared) { - ps_res = resource_open(desc.ps.file); + ps_res = resource_open(desc->ps.file); } - dict_set(pipeline->arena, pipeline->dependencies, hash_fnv64(HASH_FNV64_BASIS, desc.vs.file), 1); - dict_set(pipeline->arena, pipeline->dependencies, hash_fnv64(HASH_FNV64_BASIS, desc.ps.file), 1); + dict_set(pipeline->arena, pipeline->dependencies, hash_fnv64(HASH_FNV64_BASIS, desc->vs.file), 1); + dict_set(pipeline->arena, pipeline->dependencies, hash_fnv64(HASH_FNV64_BASIS, desc->ps.file), 1); if (success) { if (!resource_exists(&vs_res)) { - error_str = string_format(scratch.arena, LIT("Shader source \"%F\" not found"), FMT_STR(desc.vs.file)); + error_str = string_format(scratch.arena, LIT("Shader source \"%F\" not found"), FMT_STR(desc->vs.file)); success = false; } else if (!resource_exists(&ps_res)) { - error_str = string_format(scratch.arena, LIT("Shader source \"%F\" not found"), FMT_STR(desc.ps.file)); + error_str = string_format(scratch.arena, LIT("Shader source \"%F\" not found"), FMT_STR(desc->ps.file)); success = false; } } @@ -961,13 +957,13 @@ INTERNAL WORK_TASK_FUNC_DEF(pipeline_load_task, load_arg_raw) struct shader_compile_task_arg vs = ZI; vs.kind = SHADER_COMPILE_TASK_KIND_VS; vs.pipeline = pipeline; - vs.shader_desc = desc.vs; + vs.shader_desc = desc->vs; vs.shader_res = &vs_res; struct shader_compile_task_arg ps = ZI; ps.kind = SHADER_COMPILE_TASK_KIND_PS; ps.pipeline = pipeline; - ps.shader_desc = desc.ps; + ps.shader_desc = desc->ps; ps.shader_res = &ps_res; /* Compile shaders */ @@ -1038,7 +1034,7 @@ INTERNAL WORK_TASK_FUNC_DEF(pipeline_load_task, load_arg_raw) __profscope(Create PSO); D3D12_RASTERIZER_DESC raster_desc = { .FillMode = D3D12_FILL_MODE_SOLID, - .CullMode = D3D12_CULL_MODE_BACK, + .CullMode = D3D12_CULL_MODE_NONE, .FrontCounterClockwise = FALSE, .DepthBias = D3D12_DEFAULT_DEPTH_BIAS, .DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP, @@ -1050,10 +1046,17 @@ INTERNAL WORK_TASK_FUNC_DEF(pipeline_load_task, load_arg_raw) .ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF }; - /* No input layout */ + /* Input layout */ + u32 num_input_layout_elements = 0; + for (u32 i = 0; i < ARRAY_COUNT(desc->ia); ++i) { + if (desc->ia[i].SemanticName == NULL) { + break; + } + ++num_input_layout_elements; + } D3D12_INPUT_LAYOUT_DESC input_layout_desc = { - .pInputElementDescs = NULL, - .NumElements = 0 + .pInputElementDescs = desc->ia, + .NumElements = num_input_layout_elements }; /* Blend state */ @@ -1162,7 +1165,7 @@ INTERNAL void pipeline_alloc_from_desc(u64 num_pipelines, struct pipeline_desc * __prof; struct work_slate ws = work_slate_begin(); for (u64 i = 0; i < num_pipelines; ++i) { - struct pipeline_desc desc = descs[i]; + struct pipeline_desc *desc = &descs[i]; struct pipeline *pipeline = NULL; { struct arena *pipeline_arena = arena_alloc(MEGABYTE(64)); @@ -1170,8 +1173,8 @@ INTERNAL void pipeline_alloc_from_desc(u64 num_pipelines, struct pipeline_desc * pipeline->arena = pipeline_arena; pipelines_out[i] = pipeline; } - pipeline->desc = desc; - pipeline->name = string_copy(pipeline->arena, desc.name); + pipeline->desc = *desc; + pipeline->name = string_copy(pipeline->arena, desc->name); pipeline->hash = hash_fnv64(HASH_FNV64_BASIS, pipeline->name); pipeline->dependencies = dict_init(pipeline->arena, 64); work_slate_push_task(&ws, pipeline_load_task, pipeline); @@ -2345,6 +2348,7 @@ void gp_dispatch(struct gp_dispatch_params params) struct command_list *cl = command_list_open(G.cq_direct); { struct dx12_resource *target = handle_get_data(params.draw_target, DX12_HANDLE_KIND_RESOURCE); + struct mat4x4 vp_matrix = calculate_vp(params.draw_target_view, params.draw_target_viewport.width, params.draw_target_viewport.height); /* Upload dummmy vert & index buffer */ /* TODO: Make these static */ @@ -2359,16 +2363,9 @@ void gp_dispatch(struct gp_dispatch_params params) struct command_buffer *shape_verts_buffer = command_list_push_buffer(cl, STRING_FROM_ARENA(flow->shape_verts_arena)); struct command_buffer *shape_indices_buffer = command_list_push_buffer(cl, STRING_FROM_ARENA(flow->shape_indices_arena)); - /* Upload descriptor heap */ struct command_descriptor_heap *descriptor_heap = command_list_push_descriptor_heap(cl, G.cbv_srv_uav_heap); - struct mat4x4 vp_matrix = calculate_vp(params.draw_target_view, params.draw_target_viewport.width, params.draw_target_viewport.height); - - /* Create temporary descriptor heap */ - /* NOTE: This should always occur after buffers are submitted */ - - /* Transition render target */ enum D3D12_RESOURCE_STATES target_old_state = dx12_resource_barrier(cl->cl, target, D3D12_RESOURCE_STATE_RENDER_TARGET); { @@ -2382,24 +2379,23 @@ void gp_dispatch(struct gp_dispatch_params params) /* Material pass */ if (material_pipeline->success) { __profscope(Material pass); - u32 instance_count = material_instance_buffer->size / sizeof(struct sh_material_instance); /* Bind pipeline */ ID3D12GraphicsCommandList_SetPipelineState(cl->cl, material_pipeline->pso); ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, material_pipeline->rootsig); - /* Set root constants */ + /* Set constants */ struct sh_material_constants constants = ZI; constants.projection = sh_float4x4_from_mat4x4(vp_matrix); command_list_set_root_constant(cl, &constants, sizeof(constants)); - /* Bind instance buffer */ + /* Set instance buffer */ ID3D12GraphicsCommandList_SetGraphicsRootShaderResourceView(cl->cl, 1, material_instance_buffer->resource->gpu_address); - /* Bind grid buffer */ + /* Set grid buffer */ ID3D12GraphicsCommandList_SetGraphicsRootShaderResourceView(cl->cl, 2, material_grid_buffer->resource->gpu_address); - /* Bind descriptor heap */ + /* Set descriptor heap */ ID3D12DescriptorHeap *heaps[] = { descriptor_heap->heap }; ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, ARRAY_COUNT(heaps), heaps); ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(cl->cl, 3, descriptor_heap->gp_handle); @@ -2411,6 +2407,7 @@ void gp_dispatch(struct gp_dispatch_params params) ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); /* Draw */ + u32 instance_count = material_instance_buffer->size / sizeof(struct sh_material_instance); D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(dummy_vertex_buffer, 0); D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(quad_index_buffer, DXGI_FORMAT_R16_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); @@ -2424,19 +2421,14 @@ void gp_dispatch(struct gp_dispatch_params params) __profscope(Shape pass); /* Bind pipeline */ - ID3D12GraphicsCommandList_SetPipelineState(cl->cl, material_pipeline->pso); - ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, material_pipeline->rootsig); + ID3D12GraphicsCommandList_SetPipelineState(cl->cl, shape_pipeline->pso); + ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, shape_pipeline->rootsig); - /* Set root constants */ + /* Set constants */ struct sh_shape_constants constants = ZI; constants.projection = sh_float4x4_from_mat4x4(vp_matrix); command_list_set_root_constant(cl, &constants, sizeof(constants)); - /* Bind descriptor heap */ - ID3D12DescriptorHeap *heaps[] = { descriptor_heap->heap }; - ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, ARRAY_COUNT(heaps), heaps); - ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(cl->cl, 3, descriptor_heap->gp_handle); - /* Setup Rasterizer State */ D3D12_VIEWPORT viewport = viewport_from_rect(params.draw_target_viewport); D3D12_RECT scissor = scissor_from_rect(params.draw_target_viewport); @@ -2444,12 +2436,13 @@ void gp_dispatch(struct gp_dispatch_params params) ID3D12GraphicsCommandList_RSSetScissorRects(cl->cl, 1, &scissor); /* Draw */ + u32 index_count = shape_indices_buffer->size / sizeof(u32); D3D12_VERTEX_BUFFER_VIEW vbv = vbv_from_command_buffer(shape_verts_buffer, sizeof(struct sh_shape_vert)); D3D12_INDEX_BUFFER_VIEW ibv = ibv_from_command_buffer(shape_indices_buffer, DXGI_FORMAT_R32_UINT); ID3D12GraphicsCommandList_IASetPrimitiveTopology(cl->cl, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ID3D12GraphicsCommandList_IASetVertexBuffers(cl->cl, 0, 1, &vbv); ID3D12GraphicsCommandList_IASetIndexBuffer(cl->cl, &ibv); - ID3D12GraphicsCommandList_DrawIndexedInstanced(cl->cl, 6, 1, 0, 0, 0); + ID3D12GraphicsCommandList_DrawIndexedInstanced(cl->cl, index_count, 1, 0, 0, 0); } /* Reset render target */ @@ -2605,14 +2598,14 @@ INTERNAL void present_blit(struct dx12_resource *dst, struct dx12_resource *src, ID3D12GraphicsCommandList_SetPipelineState(cl->cl, blit_pipeline->pso); ID3D12GraphicsCommandList_SetGraphicsRootSignature(cl->cl, blit_pipeline->rootsig); - /* Set root constants */ + /* Set constants */ struct sh_blit_constants constants = ZI; constants.projection = sh_float4x4_from_mat4x4(vp_matrix); constants.tex_urid = sh_uint_from_u32(src->srv_descriptor->index); constants.gamma = sh_float_from_f32(2.2); command_list_set_root_constant(cl, &constants, sizeof(constants)); - /* Bind descriptor heap */ + /* Set descriptor heap */ ID3D12DescriptorHeap *heaps[] = { descriptor_heap->heap }; ID3D12GraphicsCommandList_SetDescriptorHeaps(cl->cl, ARRAY_COUNT(heaps), heaps); ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(cl->cl, 1, descriptor_heap->gp_handle); diff --git a/src/sys_win32.c b/src/sys_win32.c index 0abf42e3..f8f43bc7 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -1196,8 +1196,10 @@ INTERNAL void win32_update_window_from_settings(struct win32_window *window, str SetWindowPlacement(hwnd, &wp); /* Make window always on top when debugging */ +#if 0 #if RTC SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); +#endif #endif {