Gpu cleanup

This commit is contained in:
luboslenco
2025-09-09 19:41:55 +02:00
parent 6d659ca425
commit c270fefac3
8 changed files with 59 additions and 51 deletions
+7 -16
View File
@@ -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 *)&current_vb->impl.vertex_buffer_view);
command_list->lpVtbl->IASetIndexBuffer(command_list, (D3D12_INDEX_BUFFER_VIEW *)&current_ib->impl.index_buffer_view);
@@ -561,13 +556,9 @@ void gpu_disable_scissor() {
command_list->lpVtbl->RSSetScissorRects(command_list, 1, &current_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) {
+1 -1
View File
@@ -41,7 +41,7 @@ struct D3D12IndexBufferView {
};
typedef struct {
struct ID3D12PipelineState *pso;
struct ID3D12PipelineState *pipeline;
} gpu_pipeline_impl_t;
typedef struct {
+2 -2
View File
@@ -1,8 +1,8 @@
#pragma once
typedef struct {
void *_pipeline;
void *_depth;
void *pipeline;
void *depth;
} gpu_pipeline_impl_t;
typedef struct {
+21 -23
View File
@@ -22,15 +22,10 @@ static id<MTLSamplerState> 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<MTLRenderPipelineState> pipe = (__bridge id<MTLRenderPipelineState>)current_pipeline->impl._pipeline;
id<MTLRenderPipelineState> pipe = (__bridge id<MTLRenderPipelineState>)current_pipeline->impl.pipeline;
[command_encoder setRenderPipelineState:pipe];
id<MTLDepthStencilState> depth_state = (__bridge id<MTLDepthStencilState>)current_pipeline->impl._depth;
id<MTLDepthStencilState> depth_state = (__bridge id<MTLDepthStencilState>)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<MTLRenderPipelineState> pipe = (__bridge id<MTLRenderPipelineState>)pipeline->impl._pipeline;
[command_encoder setRenderPipelineState:pipe];
id<MTLDepthStencilState> depth_state = (__bridge id<MTLDepthStencilState>)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<MTLRenderPipelineState> pipe = (__bridge id<MTLRenderPipelineState>)pipeline->impl.pipeline;
[command_encoder setRenderPipelineState:pipe];
id<MTLDepthStencilState> depth_state = (__bridge id<MTLDepthStencilState>)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<MTLRenderPipelineState> pipe = (__bridge_transfer id<MTLRenderPipelineState>)pipeline->impl._pipeline;
id<MTLRenderPipelineState> pipe = (__bridge_transfer id<MTLRenderPipelineState>)pipeline->impl.pipeline;
pipe = nil;
pipeline->impl._pipeline = NULL;
pipeline->impl.pipeline = NULL;
id<MTLDepthStencilState> depth_state = (__bridge_transfer id<MTLDepthStencilState>)pipeline->impl._depth;
id<MTLDepthStencilState> depth_state = (__bridge_transfer id<MTLDepthStencilState>)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) {
+6 -8
View File
@@ -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, &current_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) {
+1 -1
View File
@@ -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);
}
+18
View File
@@ -1,4 +1,5 @@
#include "iron_gpu.h"
#include <string.h>
#include <iron_system.h>
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++;
+3
View File
@@ -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;