From 737e640a68d538320657f1771431c5d8a2bcc24f Mon Sep 17 00:00:00 2001 From: luboslenco Date: Thu, 17 Jul 2025 23:42:23 +0200 Subject: [PATCH] Gpu cleanup --- base/sources/backends/direct3d12_gpu.c | 28 ++++++++----------- base/sources/backends/metal_gpu.m | 38 ++++++++++++-------------- base/sources/backends/vulkan_gpu.c | 22 ++++++--------- base/sources/iron_gpu.h | 4 ++- 4 files changed, 42 insertions(+), 50 deletions(-) diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 51deef73..e8c1a337 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -1,6 +1,4 @@ #define WIN32_LEAN_AND_MEAN -#define HEAP_SIZE 2048 -#define TEXTURE_COUNT 16 #include #include #include @@ -25,10 +23,10 @@ static D3D12_VIEWPORT current_viewport; static D3D12_RECT current_scissor; static gpu_buffer_t *current_vb; static gpu_buffer_t *current_ib; -static D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[16]; +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[TEXTURE_COUNT] = { +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 }; @@ -76,6 +74,8 @@ static D3D12_COMPARISON_FUNC convert_compare_mode(gpu_compare_mode_t compare) { return D3D12_COMPARISON_FUNC_ALWAYS; case GPU_COMPARE_MODE_NEVER: return D3D12_COMPARISON_FUNC_NEVER; + case GPU_COMPARE_MODE_EQUAL: + return D3D12_COMPARISON_FUNC_EQUAL; case GPU_COMPARE_MODE_LESS: return D3D12_COMPARISON_FUNC_LESS; } @@ -280,7 +280,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { D3D12_ROOT_PARAMETER parameters[2] = {}; D3D12_DESCRIPTOR_RANGE range = { .RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV, - .NumDescriptors = (UINT)TEXTURE_COUNT, + .NumDescriptors = (UINT)GPU_MAX_TEXTURES, .BaseShaderRegister = 0, .RegisterSpace = 0, .OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, @@ -293,8 +293,8 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { parameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; parameters[1].Descriptor.ShaderRegister = 0; parameters[1].Descriptor.RegisterSpace = 0; - D3D12_STATIC_SAMPLER_DESC samplers[TEXTURE_COUNT]; - for (int i = 0; i < TEXTURE_COUNT; ++i) { + D3D12_STATIC_SAMPLER_DESC samplers[GPU_MAX_TEXTURES]; + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { samplers[i].ShaderRegister = i; samplers[i].Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; samplers[i].AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; @@ -312,7 +312,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { D3D12_ROOT_SIGNATURE_DESC root_signature_desc = { .NumParameters = 2, .pParameters = parameters, - .NumStaticSamplers = TEXTURE_COUNT, + .NumStaticSamplers = GPU_MAX_TEXTURES, .pStaticSamplers = samplers, .Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT, }; @@ -350,7 +350,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { gpu_create_framebuffers(depth_buffer_bits); D3D12_DESCRIPTOR_HEAP_DESC heap_desc = { - .NumDescriptors = HEAP_SIZE, + .NumDescriptors = GPU_CONSTANT_BUFFER_MULTIPLE, .Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, .Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, }; @@ -360,10 +360,6 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { device->lpVtbl->CreateCommandList(device, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, command_allocator, NULL, &IID_ID3D12CommandList, &command_list); } -int gpu_max_bound_textures(void) { - return TEXTURE_COUNT; -} - void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { for (int i = 0; i < current_render_targets_count; ++i) { current_render_targets[i]->impl.rtv_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(current_render_targets[i]->impl.rtv_descriptor_heap, &target_descriptors[i]); @@ -495,7 +491,7 @@ void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size) { void gpu_internal_set_textures() { UINT srv_step = device->lpVtbl->GetDescriptorHandleIncrementSize(device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - if (srv_heap_index + TEXTURE_COUNT > HEAP_SIZE) { + if (srv_heap_index + GPU_MAX_TEXTURES > GPU_CONSTANT_BUFFER_MULTIPLE) { srv_heap_index = 0; } @@ -506,7 +502,7 @@ void gpu_internal_set_textures() { cpu_base.ptr += srv_heap_index * srv_step; gpu_base.ptr += srv_heap_index * srv_step; - for (int i = 0; i < TEXTURE_COUNT; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { if (current_textures[i] != NULL) { D3D12_CPU_DESCRIPTOR_HANDLE source_cpu; ID3D12DescriptorHeap *source_heap = current_textures[i]->impl.srv_descriptor_heap; @@ -565,7 +561,7 @@ void gpu_set_pipeline(gpu_pipeline_t *pipeline) { current_pipeline = pipeline; command_list->lpVtbl->SetPipelineState(command_list, pipeline->impl.pso); command_list->lpVtbl->SetGraphicsRootSignature(command_list, root_signature); - for (int i = 0; i < TEXTURE_COUNT; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { current_textures[i] = NULL; } } diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index 276466d1..22cf9c0c 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -26,7 +26,7 @@ static MTLViewport current_viewport; static MTLScissorRect current_scissor; static MTLRenderPassDescriptor *render_pass_desc; static bool resized = false; -static gpu_texture_t *current_textures[16] = { +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 }; @@ -54,6 +54,8 @@ static MTLCompareFunction convert_compare_mode(gpu_compare_mode_t compare) { return MTLCompareFunctionAlways; case GPU_COMPARE_MODE_NEVER: return MTLCompareFunctionNever; + case GPU_COMPARE_MODE_EQUAL: + return MTLCompareFunctionEqual; case GPU_COMPARE_MODE_LESS: return MTLCompareFunctionLess; } @@ -152,23 +154,23 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { linear_desc.supportArgumentBuffers = true; linear_sampler = [device newSamplerStateWithDescriptor:linear_desc]; - MTLArgumentDescriptor *constantsDesc = [MTLArgumentDescriptor argumentDescriptor]; - constantsDesc.dataType = MTLDataTypePointer; - constantsDesc.index = 0; + MTLArgumentDescriptor *constants_desc = [MTLArgumentDescriptor argumentDescriptor]; + constants_desc.dataType = MTLDataTypePointer; + constants_desc.index = 0; - MTLArgumentDescriptor *samplerDesc = [MTLArgumentDescriptor argumentDescriptor]; - samplerDesc.dataType = MTLDataTypeSampler; - samplerDesc.index = 1; + MTLArgumentDescriptor *sampler_desc = [MTLArgumentDescriptor argumentDescriptor]; + sampler_desc.dataType = MTLDataTypeSampler; + sampler_desc.index = 1; - MTLArgumentDescriptor *textureDesc[16]; - for (int i = 0; i < 16; ++i) { - textureDesc[i] = [MTLArgumentDescriptor argumentDescriptor]; - textureDesc[i].dataType = MTLDataTypeTexture; - textureDesc[i].index = i + 2; - textureDesc[i].textureType = MTLTextureType2D; + MTLArgumentDescriptor *texture_desc[GPU_MAX_TEXTURES]; + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { + texture_desc[i] = [MTLArgumentDescriptor argumentDescriptor]; + texture_desc[i].dataType = MTLDataTypeTexture; + texture_desc[i].index = i + 2; + texture_desc[i].textureType = MTLTextureType2D; } - NSArray *arguments = [NSArray arrayWithObjects:constantsDesc, samplerDesc, textureDesc[0], textureDesc[1], textureDesc[2], textureDesc[3], textureDesc[4], textureDesc[5], textureDesc[6], textureDesc[7], textureDesc[8], textureDesc[9], textureDesc[10], textureDesc[11], textureDesc[12], textureDesc[13], textureDesc[14], textureDesc[15], nil]; + NSArray *arguments = [NSArray arrayWithObjects:constants_desc, sampler_desc, texture_desc[0], texture_desc[1], texture_desc[2], texture_desc[3], texture_desc[4], texture_desc[5], texture_desc[6], texture_desc[7], texture_desc[8], texture_desc[9], texture_desc[10], texture_desc[11], texture_desc[12], texture_desc[13], texture_desc[14], texture_desc[15], nil]; argument_encoder = [device newArgumentEncoderWithArguments:arguments]; argument_buffer_step = [argument_encoder encodedLength]; argument_buffer = [device newBufferWithLength:(argument_buffer_step * GPU_CONSTANT_BUFFER_MULTIPLE) options:MTLResourceStorageModeShared]; @@ -290,10 +292,6 @@ void gpu_present_internal() { void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after) { } -int gpu_max_bound_textures(void) { - return 16; -} - void gpu_draw_internal() { id index_buffer = (__bridge id)current_ib->impl.metal_buffer; [command_encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle @@ -339,7 +337,7 @@ void gpu_set_pipeline(gpu_pipeline_t *pipeline) { [command_encoder setDepthStencilState:depth_state]; [command_encoder setFrontFacingWinding:MTLWindingClockwise]; [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { current_textures[i] = NULL; } } @@ -405,7 +403,7 @@ void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size) { [command_encoder setVertexBuffer:argument_buffer offset:argument_buffer_step * constant_buffer_index atIndex:1]; [command_encoder setFragmentBuffer:argument_buffer offset:argument_buffer_step * constant_buffer_index atIndex:1]; [command_encoder useResource:buf usage:MTLResourceUsageRead stages:MTLRenderStageVertex|MTLRenderStageFragment]; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { if (current_textures[i] == NULL) { break; } diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index 4370fef6..4c121648 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -20,7 +20,7 @@ bool gpu_transpose_mat = true; extern int constant_buffer_index; -static gpu_texture_t *current_textures[16] = { +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 }; @@ -116,6 +116,8 @@ static VkCompareOp convert_compare_mode(gpu_compare_mode_t compare) { return VK_COMPARE_OP_ALWAYS; case GPU_COMPARE_MODE_NEVER: return VK_COMPARE_OP_NEVER; + case GPU_COMPARE_MODE_EQUAL: + return VK_COMPARE_OP_EQUAL; case GPU_COMPARE_MODE_LESS: return VK_COMPARE_OP_LESS; } @@ -318,7 +320,7 @@ static void create_descriptors(void) { bindings[1].stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; bindings[1].pImmutableSamplers = &immutable_sampler; - for (int i = 2; i < 18; ++i) { + for (int i = 2; i < 2 + GPU_MAX_TEXTURES; ++i) { bindings[i].binding = i; bindings[i].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; bindings[i].descriptorCount = 1; @@ -329,7 +331,7 @@ static void create_descriptors(void) { VkDescriptorSetLayoutCreateInfo layout_create_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .pNext = NULL, - .bindingCount = 18, + .bindingCount = 2 + GPU_MAX_TEXTURES, .pBindings = bindings, }; @@ -345,7 +347,7 @@ static void create_descriptors(void) { type_counts[1].descriptorCount = 1; type_counts[2].type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; - type_counts[2].descriptorCount = 16; + type_counts[2].descriptorCount = GPU_MAX_TEXTURES; VkDescriptorPoolCreateInfo pool_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, @@ -1042,12 +1044,6 @@ bool iron_vulkan_internal_get_size(int *width, int *height) { return false; } -int gpu_max_bound_textures(void) { - VkPhysicalDeviceProperties props; - vkGetPhysicalDeviceProperties(gpu, &props); - return props.limits.maxPerStageDescriptorSamplers; -} - void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * depth_buffer, unsigned flags, unsigned color, float depth) { if (!framebuffer_acquired) { acquire_next_image(); @@ -1374,9 +1370,9 @@ static VkDescriptorSet get_descriptor_set(VkBuffer buffer) { buffer_descs[0].offset = 0; buffer_descs[0].range = 256 ; - VkDescriptorImageInfo tex_desc[16]; + VkDescriptorImageInfo tex_desc[GPU_MAX_TEXTURES]; memset(&tex_desc, 0, sizeof(tex_desc)); - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { if (current_textures[i] != NULL) { tex_desc[i].imageView = current_textures[i]->impl.view; tex_desc[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; @@ -1395,7 +1391,7 @@ static VkDescriptorSet get_descriptor_set(VkBuffer buffer) { writes[0].pBufferInfo = &buffer_descs[0]; write_count++; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < GPU_MAX_TEXTURES; ++i) { if (current_textures[i] != NULL) { writes[write_count].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writes[write_count].dstSet = descriptor_set; diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index 8225d51b..24f3fa4c 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -13,6 +13,7 @@ #define GPU_CLEAR_COLOR 1 #define GPU_CLEAR_DEPTH 2 #define GPU_MAX_VERTEX_ELEMENTS 16 +#define GPU_MAX_TEXTURES 16 #define GPU_FRAMEBUFFER_COUNT 2 #define GPU_CONSTANT_BUFFER_SIZE 256 #define GPU_CONSTANT_BUFFER_MULTIPLE 2048 @@ -73,7 +74,8 @@ typedef enum gpu_cull_mode { typedef enum gpu_compare_mode { GPU_COMPARE_MODE_ALWAYS, GPU_COMPARE_MODE_NEVER, - GPU_COMPARE_MODE_LESS + GPU_COMPARE_MODE_LESS, + GPU_COMPARE_MODE_EQUAL } gpu_compare_mode_t; typedef struct gpu_texture {