From c270fefac336dce1e55260f904f1b283af4fe578 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 9 Sep 2025 19:41:55 +0200 Subject: [PATCH] Gpu cleanup --- base/sources/backends/direct3d12_gpu.c | 23 ++++---------- base/sources/backends/direct3d12_gpu.h | 2 +- base/sources/backends/metal_gpu.h | 4 +-- base/sources/backends/metal_gpu.m | 44 ++++++++++++-------------- base/sources/backends/vulkan_gpu.c | 14 ++++---- base/sources/backends/webgpu_gpu.c | 2 +- base/sources/iron_gpu.c | 18 +++++++++++ base/sources/iron_gpu.h | 3 ++ 8 files changed, 59 insertions(+), 51 deletions(-) diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 3abd95c4..fe15fd65 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -19,7 +19,6 @@ static IDXGISwapChain *window_swapchain; static ID3D12RootSignature *root_signature = NULL; static ID3D12CommandAllocator *command_allocator; static ID3D12GraphicsCommandList *command_list; -static gpu_pipeline_t *current_pipeline; static D3D12_VIEWPORT current_viewport; static D3D12_RECT current_scissor; static gpu_buffer_t *current_vb; @@ -27,10 +26,6 @@ static gpu_buffer_t *current_ib; static D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[GPU_MAX_TEXTURES]; static D3D12_CPU_DESCRIPTOR_HANDLE depth_handle; static D3D12_CPU_DESCRIPTOR_HANDLE *current_depth_handle; -static gpu_texture_t *current_textures[GPU_MAX_TEXTURES] = { - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL -}; static bool window_vsync; static ID3D12DescriptorHeap *sampler_heap; static ID3D12DescriptorHeap *srv_heap; @@ -423,7 +418,7 @@ void gpu_execute_and_wait() { if (gpu_in_use) { command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, current_depth_handle); - command_list->lpVtbl->SetPipelineState(command_list, current_pipeline->impl.pso); + command_list->lpVtbl->SetPipelineState(command_list, current_pipeline->impl.pipeline); command_list->lpVtbl->SetGraphicsRootSignature(command_list, root_signature); command_list->lpVtbl->IASetVertexBuffers(command_list, 0, 1, (D3D12_VERTEX_BUFFER_VIEW *)¤t_vb->impl.vertex_buffer_view); command_list->lpVtbl->IASetIndexBuffer(command_list, (D3D12_INDEX_BUFFER_VIEW *)¤t_ib->impl.index_buffer_view); @@ -561,13 +556,9 @@ void gpu_disable_scissor() { command_list->lpVtbl->RSSetScissorRects(command_list, 1, ¤t_scissor); } -void gpu_set_pipeline(gpu_pipeline_t *pipeline) { - current_pipeline = pipeline; - command_list->lpVtbl->SetPipelineState(command_list, pipeline->impl.pso); +void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline) { + command_list->lpVtbl->SetPipelineState(command_list, pipeline->impl.pipeline); command_list->lpVtbl->SetGraphicsRootSignature(command_list, root_signature); - for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { - current_textures[i] = NULL; - } } void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { @@ -705,9 +696,9 @@ void gpu_use_linear_sampling(bool b) { } void gpu_pipeline_destroy_internal(gpu_pipeline_t *pipe) { - if (pipe->impl.pso != NULL) { - pipe->impl.pso->lpVtbl->Release(pipe->impl.pso); - pipe->impl.pso = NULL; + if (pipe->impl.pipeline != NULL) { + pipe->impl.pipeline->lpVtbl->Release(pipe->impl.pipeline); + pipe->impl.pipeline = NULL; } } @@ -813,7 +804,7 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipe) { (pipe->color_write_mask_alpha[i] ? D3D12_COLOR_WRITE_ENABLE_ALPHA : 0); } - device->lpVtbl->CreateGraphicsPipelineState(device, &psoDesc, &IID_ID3D12PipelineState, &pipe->impl.pso); + device->lpVtbl->CreateGraphicsPipelineState(device, &psoDesc, &IID_ID3D12PipelineState, &pipe->impl.pipeline); } void gpu_shader_init(gpu_shader_t *shader, const void *_data, size_t length, gpu_shader_type_t type) { diff --git a/base/sources/backends/direct3d12_gpu.h b/base/sources/backends/direct3d12_gpu.h index efd30dd1..4d724cfb 100644 --- a/base/sources/backends/direct3d12_gpu.h +++ b/base/sources/backends/direct3d12_gpu.h @@ -41,7 +41,7 @@ struct D3D12IndexBufferView { }; typedef struct { - struct ID3D12PipelineState *pso; + struct ID3D12PipelineState *pipeline; } gpu_pipeline_impl_t; typedef struct { diff --git a/base/sources/backends/metal_gpu.h b/base/sources/backends/metal_gpu.h index cd08054a..56aa1b64 100644 --- a/base/sources/backends/metal_gpu.h +++ b/base/sources/backends/metal_gpu.h @@ -1,8 +1,8 @@ #pragma once typedef struct { - void *_pipeline; - void *_depth; + void *pipeline; + void *depth; } gpu_pipeline_impl_t; typedef struct { diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index 356cc61b..07249317 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -22,15 +22,10 @@ static id point_sampler; static int argument_buffer_step; static gpu_buffer_t *current_vb; static gpu_buffer_t *current_ib; -static gpu_pipeline_t *current_pipeline; static MTLViewport current_viewport; static MTLScissorRect current_scissor; static MTLRenderPassDescriptor *render_pass_desc; static bool resized = false; -static gpu_texture_t *current_textures[GPU_MAX_TEXTURES] = { - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL -}; static void *readback_buffer; static int readback_buffer_size = 0; static bool linear_sampling = true; @@ -250,9 +245,9 @@ void gpu_execute_and_wait() { } render_pass_desc.depthAttachment.loadAction = MTLLoadActionLoad; command_encoder = [command_buffer renderCommandEncoderWithDescriptor:render_pass_desc]; - id pipe = (__bridge id)current_pipeline->impl._pipeline; + id pipe = (__bridge id)current_pipeline->impl.pipeline; [command_encoder setRenderPipelineState:pipe]; - id depth_state = (__bridge id)current_pipeline->impl._depth; + id depth_state = (__bridge id)current_pipeline->impl.depth; [command_encoder setDepthStencilState:depth_state]; [command_encoder setFrontFacingWinding:MTLWindingClockwise]; [command_encoder setCullMode:convert_cull_mode(current_pipeline->cull_mode)]; @@ -325,17 +320,20 @@ void gpu_disable_scissor() { [command_encoder setScissorRect:current_scissor]; } -void gpu_set_pipeline(gpu_pipeline_t *pipeline) { - current_pipeline = pipeline; - id pipe = (__bridge id)pipeline->impl._pipeline; - [command_encoder setRenderPipelineState:pipe]; - id depth_state = (__bridge id)pipeline->impl._depth; - [command_encoder setDepthStencilState:depth_state]; - [command_encoder setFrontFacingWinding:MTLWindingClockwise]; - [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; +void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline) { for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { current_textures[i] = NULL; } + if (pipeline->impl.pipeline == NULL) { + return; + } + current_pipeline = pipeline; + id pipe = (__bridge id)pipeline->impl.pipeline; + [command_encoder setRenderPipelineState:pipe]; + id depth_state = (__bridge id)pipeline->impl.depth; + [command_encoder setDepthStencilState:depth_state]; + [command_encoder setFrontFacingWinding:MTLWindingClockwise]; + [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; } void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { @@ -415,13 +413,13 @@ void gpu_use_linear_sampling(bool b) { } void gpu_pipeline_destroy_internal(gpu_pipeline_t *pipeline) { - id pipe = (__bridge_transfer id)pipeline->impl._pipeline; + id pipe = (__bridge_transfer id)pipeline->impl.pipeline; pipe = nil; - pipeline->impl._pipeline = NULL; + pipeline->impl.pipeline = NULL; - id depth_state = (__bridge_transfer id)pipeline->impl._depth; + id depth_state = (__bridge_transfer id)pipeline->impl.depth; depth_state = nil; - pipeline->impl._depth = NULL; + pipeline->impl.depth = NULL; } void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { @@ -499,7 +497,7 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { NSError *errors = nil; MTLRenderPipelineReflection *reflection = nil; - pipeline->impl._pipeline = (__bridge_retained void *)[ + pipeline->impl.pipeline = (__bridge_retained void *)[ device newRenderPipelineStateWithDescriptor:render_pipeline_desc options:MTLPipelineOptionBufferTypeInfo reflection:&reflection @@ -508,7 +506,7 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { MTLDepthStencilDescriptor *depth_descriptor = [MTLDepthStencilDescriptor new]; depth_descriptor.depthCompareFunction = convert_compare_mode(pipeline->depth_mode); depth_descriptor.depthWriteEnabled = pipeline->depth_write; - pipeline->impl._depth = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depth_descriptor]; + pipeline->impl.depth = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depth_descriptor]; } void gpu_shader_destroy(gpu_shader_t *shader) { @@ -851,8 +849,8 @@ void gpu_raytrace_set_acceleration_structure(gpu_raytrace_acceleration_structure accel = _accel; } -void gpu_raytrace_set_pipeline(gpu_raytrace_pipeline_t *_pipeline) { - pipeline = _pipeline; +void gpu_raytrace_set_pipeline(gpu_raytrace_pipeline_t *_rt_pipeline) { + pipeline = _rt_pipeline; } void gpu_raytrace_set_target(gpu_texture_t *_output) { diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index 86e65b0d..b586c92d 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -17,14 +17,9 @@ bool gpu_transpose_mat = true; extern int constant_buffer_index; -static gpu_texture_t *current_textures[GPU_MAX_TEXTURES] = { - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL -}; static VkSemaphore framebuffer_available_semaphore; static VkSemaphore rendering_finished_semaphores[GPU_FRAMEBUFFER_COUNT]; static VkFence fence; -static gpu_pipeline_t *current_pipeline = NULL; static VkViewport current_viewport; static VkRect2D current_scissor; static gpu_buffer_t *current_vb; @@ -1243,12 +1238,15 @@ void gpu_disable_scissor() { vkCmdSetScissor(command_buffer, 0, 1, ¤t_scissor); } -void gpu_set_pipeline(gpu_pipeline_t *pipeline) { - current_pipeline = pipeline; - vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.pipeline); +void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline) { for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { current_textures[i] = NULL; } + if (pipeline->impl.pipeline == NULL) { + return; + } + current_pipeline = pipeline; + vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.pipeline); } void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index e20c3229..d7eed703 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -267,7 +267,7 @@ void gpu_viewport(int x, int y, int width, int height) {} void gpu_scissor(int x, int y, int width, int height) {} void gpu_disable_scissor() {} -void gpu_set_pipeline(struct gpu_pipeline *pipeline) { +void gpu_set_pipeline_internal(struct gpu_pipeline *pipeline) { wgpuRenderPassEncoderSetPipeline(pass, pipeline->impl.pipeline); } diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index 44df2fba..a9914223 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -1,4 +1,5 @@ #include "iron_gpu.h" +#include #include static gpu_buffer_t constant_buffer; @@ -14,9 +15,14 @@ int constant_buffer_index = 0; int draw_calls = 0; int draw_calls_last = 0; bool gpu_in_use = false; +gpu_texture_t *current_textures[GPU_MAX_TEXTURES] = { + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +}; gpu_texture_t *current_render_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int current_render_targets_count = 0; gpu_texture_t *current_depth_buffer = NULL; +gpu_pipeline_t *current_pipeline = NULL; gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT]; gpu_texture_t framebuffer_depth; int framebuffer_index = 0; @@ -263,6 +269,7 @@ void gpu_pipeline_init(gpu_pipeline_t *pipe) { } pipe->color_attachment_count = 1; pipe->depth_attachment_bits = 0; + memset(&pipe->impl, 0, sizeof(gpu_pipeline_impl_t)); } void gpu_create_framebuffers(int depth_buffer_bits) { @@ -286,6 +293,17 @@ void gpu_texture_destroy(gpu_texture_t *texture) { } } +void gpu_set_pipeline(gpu_pipeline_t *pipeline) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { + current_textures[i] = NULL; + } + if (pipeline->impl.pipeline == NULL) { + return; + } + current_pipeline = pipeline; + gpu_set_pipeline_internal(pipeline); +} + void gpu_pipeline_destroy(gpu_pipeline_t *pipeline) { pipelines_to_destroy[pipelines_to_destroy_count] = *pipeline; pipelines_to_destroy_count++; diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index 975e8fdc..b627d029 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -203,6 +203,7 @@ void gpu_viewport(int x, int y, int width, int height); void gpu_scissor(int x, int y, int width, int height); void gpu_disable_scissor(); void gpu_set_pipeline(gpu_pipeline_t *pipeline); +void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline); void gpu_set_vertex_buffer(gpu_buffer_t *buffer); void gpu_set_index_buffer(gpu_buffer_t *buffer); void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size); @@ -230,9 +231,11 @@ int gpu_texture_format_size(gpu_texture_format_t format); extern bool gpu_transpose_mat; extern bool gpu_in_use; +extern gpu_texture_t *current_textures[GPU_MAX_TEXTURES]; extern gpu_texture_t *current_render_targets[8]; extern int current_render_targets_count; extern gpu_texture_t *current_depth_buffer; +extern gpu_pipeline_t *current_pipeline; extern int constant_buffer_index; extern gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT]; extern gpu_texture_t framebuffer_depth;