diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 115c88d1..aafbd8fd 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -18,6 +18,8 @@ void iron_memory_emergency(); bool gpu_transpose_mat = false; +bool gpu_in_use = false; +static bool gpu_thrown = false; static ID3D12Device *device = NULL; static ID3D12CommandQueue *queue; static struct IDXGISwapChain *window_swapchain; @@ -473,9 +475,14 @@ int gpu_max_bound_textures(void) { } void gpu_begin(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { + if (gpu_in_use && !gpu_thrown) { + gpu_thrown = true; + iron_log("End before you begin"); + } + gpu_in_use = true; - gpu_wait(); if (!command_list_open) { + gpu_wait(); command_allocator->lpVtbl->Reset(command_allocator); command_list->lpVtbl->Reset(command_list, command_allocator, NULL); command_list_open = true; @@ -542,18 +549,17 @@ void gpu_begin(gpu_texture_t **targets, int count, unsigned flags, unsigned colo } void gpu_end() { + if (!gpu_in_use && !gpu_thrown) { + gpu_thrown = true; + iron_log("Begin before you end"); + } + gpu_in_use = false; + for (int i = 0; i < current_render_targets_count; ++i) { gpu_barrier(current_render_targets[i], current_render_targets[i] == &framebuffers[framebuffer_index] ? D3D12_RESOURCE_STATE_PRESENT : D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); } current_render_targets_count = 0; - - command_list->lpVtbl->Close(command_list); - command_list_open = false; - - ID3D12CommandList *command_lists[] = {(ID3D12CommandList *)command_list}; - queue->lpVtbl->ExecuteCommandLists(queue, 1, command_lists); - queue->lpVtbl->Signal(queue, fence, ++fence_value); } void gpu_wait() { @@ -561,6 +567,13 @@ void gpu_wait() { } void gpu_present() { + command_list->lpVtbl->Close(command_list); + command_list_open = false; + + ID3D12CommandList *command_lists[] = {(ID3D12CommandList *)command_list}; + queue->lpVtbl->ExecuteCommandLists(queue, 1, command_lists); + queue->lpVtbl->Signal(queue, fence, ++fence_value); + window_swapchain->lpVtbl->Present(window_swapchain, window_vsync, 0); queue->lpVtbl->Signal(queue, fence, ++fence_value); @@ -1354,7 +1367,7 @@ void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count) { buffer->impl.last_count = count; D3D12_RANGE range = { .Begin = start, - .End = range.Begin + count, + .End = start + count, }; uint8_t *p; buffer->impl.buffer->lpVtbl->Map(buffer->impl.buffer, 0, &range, (void **)&p); @@ -1364,7 +1377,7 @@ void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count) { void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = buffer->impl.last_start, - .End = range.Begin + buffer->impl.last_count, + .End = buffer->impl.last_start + buffer->impl.last_count, }; buffer->impl.buffer->lpVtbl->Unmap(buffer->impl.buffer, 0, &range); buffer->data = NULL; diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index 67c73ea0..85716492 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -18,6 +18,7 @@ void gpu_draw() { ++constant_buffer_index; if (constant_buffer_index >= CONSTANT_BUFFER_MULTIPLE) { constant_buffer_index = 0; + // gpu_wait(); } gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); } @@ -93,8 +94,8 @@ void gpu_set_bool(int location, bool value) { ints[0] = value ? 1 : 0; } -static void gpu_internal_set_matrix3(uint8_t *constants, int offset, iron_matrix3x3_t *value) { - float *floats = (float *)(&constants[offset]); +static void gpu_internal_set_matrix3(int offset, iron_matrix3x3_t *value) { + float *floats = (float *)(&constant_buffer.data[offset]); for (int y = 0; y < 3; ++y) { for (int x = 0; x < 3; ++x) { floats[x + y * 4] = iron_matrix3x3_get(value, x, y); @@ -102,8 +103,8 @@ static void gpu_internal_set_matrix3(uint8_t *constants, int offset, iron_matrix } } -static void gpu_internal_set_matrix4(uint8_t *constants, int offset, iron_matrix4x4_t *value) { - float *floats = (float *)(&constants[offset]); +static void gpu_internal_set_matrix4(int offset, iron_matrix4x4_t *value) { + float *floats = (float *)(&constant_buffer.data[offset]); for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { floats[x + y * 4] = iron_matrix4x4_get(value, x, y); @@ -115,10 +116,10 @@ void gpu_set_matrix3(int location, iron_matrix3x3_t value) { if (gpu_transpose_mat) { iron_matrix3x3_t m = value; iron_matrix3x3_transpose(&m); - gpu_internal_set_matrix3(constant_buffer.data, location, &m); + gpu_internal_set_matrix3(location, &m); } else { - gpu_internal_set_matrix3(constant_buffer.data, location, &value); + gpu_internal_set_matrix3(location, &value); } } @@ -126,10 +127,10 @@ void gpu_set_matrix4(int location, iron_matrix4x4_t value) { if (gpu_transpose_mat) { iron_matrix4x4_t m = value; iron_matrix4x4_transpose(&m); - gpu_internal_set_matrix4(constant_buffer.data, location, &m); + gpu_internal_set_matrix4(location, &m); } else { - gpu_internal_set_matrix4(constant_buffer.data, location, &value); + gpu_internal_set_matrix4(location, &value); } }