diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 50452f08..c3e63543 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -21,12 +21,18 @@ static ID3D12RootSignature *root_signature = NULL; static struct ID3D12CommandAllocator *command_allocator; static struct 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; +static gpu_buffer_t *current_ib; +static D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[16]; +static D3D12_CPU_DESCRIPTOR_HANDLE depth_handle; +static D3D12_CPU_DESCRIPTOR_HANDLE *current_depth_handle; static gpu_texture_t *current_textures[TEXTURE_COUNT] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; static bool window_vsync; -static int index_count = 0; static struct ID3D12DescriptorHeap *srv_heap; static int srv_heap_index = 0; static UINT64 fence_value; @@ -367,23 +373,21 @@ int gpu_max_bound_textures(void) { } void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { - - D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[16]; 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]); } - gpu_texture_t *target = current_render_targets[0]; - if (depth_buffer != NULL) { - D3D12_CPU_DESCRIPTOR_HANDLE depth_handle; depth_buffer->impl.rtv_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(depth_buffer->impl.rtv_descriptor_heap, &depth_handle); - command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, &depth_handle); + current_depth_handle = &depth_handle; } else { - command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, NULL); + current_depth_handle = NULL; } + command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, current_depth_handle); + + gpu_texture_t *target = current_render_targets[0]; gpu_viewport(0, 0, target->width, target->height); gpu_scissor(0, 0, target->width, target->height); @@ -418,18 +422,25 @@ void gpu_wait() { void gpu_flush() { command_list->lpVtbl->Close(command_list); - ID3D12CommandList *command_lists[] = {(ID3D12CommandList *)command_list}; queue->lpVtbl->ExecuteCommandLists(queue, 1, command_lists); queue->lpVtbl->Signal(queue, fence, ++fence_value); - gpu_wait(); - command_allocator->lpVtbl->Reset(command_allocator); command_list->lpVtbl->Reset(command_list, command_allocator, NULL); + + 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->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); + command_list->lpVtbl->RSSetViewports(command_list, 1, ¤t_viewport); + command_list->lpVtbl->RSSetScissorRects(command_list, 1, ¤t_scissor); + } } -void gpu_present() { +void gpu_present_internal() { command_list->lpVtbl->Close(command_list); ID3D12CommandList *command_lists[] = {(ID3D12CommandList *)command_list}; @@ -523,11 +534,11 @@ void gpu_internal_set_textures() { void gpu_draw_internal() { gpu_internal_set_textures(); command_list->lpVtbl->IASetPrimitiveTopology(command_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - command_list->lpVtbl->DrawIndexedInstanced(command_list, index_count, 1, 0, 0, 0); + command_list->lpVtbl->DrawIndexedInstanced(command_list, current_ib->count, 1, 0, 0, 0); } void gpu_viewport(int x, int y, int width, int height) { - D3D12_VIEWPORT viewport = { + current_viewport = (D3D12_VIEWPORT){ .TopLeftX = (float)x, .TopLeftY = (float)y, .Width = (float)width, @@ -535,49 +546,45 @@ void gpu_viewport(int x, int y, int width, int height) { .MinDepth = 0.0f, .MaxDepth = 1.0f, }; - command_list->lpVtbl->RSSetViewports(command_list, 1, &viewport); + command_list->lpVtbl->RSSetViewports(command_list, 1, ¤t_viewport); } void gpu_scissor(int x, int y, int width, int height) { - D3D12_RECT scissor = { + current_scissor = (D3D12_RECT){ .left = x, .top = y, .right = x + width, .bottom = y + height, }; - command_list->lpVtbl->RSSetScissorRects(command_list, 1, &scissor); + command_list->lpVtbl->RSSetScissorRects(command_list, 1, ¤t_scissor); } void gpu_disable_scissor() { - D3D12_RECT scissor = { + current_scissor = (D3D12_RECT){ .left = 0, .top = 0, .right = current_render_targets[0]->width, .bottom = current_render_targets[0]->height, }; - command_list->lpVtbl->RSSetScissorRects(command_list, 1, &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); + command_list->lpVtbl->SetGraphicsRootSignature(command_list, root_signature); for (int i = 0; i < TEXTURE_COUNT; ++i) { current_textures[i] = NULL; } - command_list->lpVtbl->SetGraphicsRootSignature(command_list, root_signature); } void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { - D3D12_VERTEX_BUFFER_VIEW view = { - .BufferLocation = buffer->impl.buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.buffer), - .SizeInBytes = (gpu_vertex_buffer_count(buffer)) * gpu_vertex_buffer_stride(buffer), - .StrideInBytes = gpu_vertex_buffer_stride(buffer), - }; - command_list->lpVtbl->IASetVertexBuffers(command_list, 0, 1, &view); + current_vb = buffer; + command_list->lpVtbl->IASetVertexBuffers(command_list, 0, 1, (D3D12_VERTEX_BUFFER_VIEW *)&buffer->impl.vertex_buffer_view); } void gpu_set_index_buffer(gpu_buffer_t *buffer) { - index_count = gpu_index_buffer_count(buffer); + current_ib = buffer; command_list->lpVtbl->IASetIndexBuffer(command_list, (D3D12_INDEX_BUFFER_VIEW *)&buffer->impl.index_buffer_view); } @@ -949,12 +956,12 @@ void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_te void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure) { buffer->count = count; - buffer->impl.stride = 0; + buffer->stride = 0; for (int i = 0; i < structure->size; ++i) { - buffer->impl.stride += gpu_vertex_data_size(structure->elements[i].data); + buffer->stride += gpu_vertex_data_size(structure->elements[i].data); } - int upload_buffer_size = buffer->impl.stride * buffer->count; + int upload_buffer_size = buffer->stride * buffer->count; D3D12_HEAP_PROPERTIES heap_properties = { .Type = D3D12_HEAP_TYPE_UPLOAD, @@ -983,13 +990,13 @@ void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structur buffer->impl.vertex_buffer_view.BufferLocation = buffer->impl.buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.buffer); buffer->impl.vertex_buffer_view.SizeInBytes = upload_buffer_size; - buffer->impl.vertex_buffer_view.StrideInBytes = buffer->impl.stride; + buffer->impl.vertex_buffer_view.StrideInBytes = buffer->stride; } float *gpu_vertex_buffer_lock(gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = 0, - .End = gpu_vertex_buffer_count(buffer) * buffer->impl.stride, + .End = buffer->count * buffer->stride, }; void *p; buffer->impl.buffer->lpVtbl->Map(buffer->impl.buffer, 0, &range, &p); @@ -999,19 +1006,11 @@ float *gpu_vertex_buffer_lock(gpu_buffer_t *buffer) { void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = 0, - .End = gpu_vertex_buffer_count(buffer) * buffer->impl.stride, + .End = buffer->count * buffer->stride, }; buffer->impl.buffer->lpVtbl->Unmap(buffer->impl.buffer, 0, &range); } -int gpu_vertex_buffer_count(gpu_buffer_t *buffer) { - return buffer->count; -} - -int gpu_vertex_buffer_stride(gpu_buffer_t *buffer) { - return buffer->impl.stride; -} - void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) { buffer->count = size; buffer->data = NULL; @@ -1066,10 +1065,6 @@ void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) { buffer->data = NULL; } -int gpu_constant_buffer_size(gpu_buffer_t *buffer) { - return buffer->count; -} - void gpu_index_buffer_init(gpu_buffer_t *buffer, int count) { buffer->count = count; @@ -1110,14 +1105,10 @@ void gpu_buffer_destroy(gpu_buffer_t *buffer) { buffer->impl.buffer = NULL; } -static int gpu_internal_index_buffer_stride(gpu_buffer_t *buffer) { - return 4; -} - void *gpu_index_buffer_lock(gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = 0, - .End = gpu_index_buffer_count(buffer) * gpu_internal_index_buffer_stride(buffer), + .End = buffer->count * 4, }; void *p; buffer->impl.buffer->lpVtbl->Map(buffer->impl.buffer, 0, &range, &p); @@ -1127,15 +1118,11 @@ void *gpu_index_buffer_lock(gpu_buffer_t *buffer) { void gpu_index_buffer_unlock(gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = 0, - .End = gpu_index_buffer_count(buffer) * gpu_internal_index_buffer_stride(buffer), + .End = buffer->count * 4, }; buffer->impl.buffer->lpVtbl->Unmap(buffer->impl.buffer, 0, &range); } -int gpu_index_buffer_count(gpu_buffer_t *buffer) { - return buffer->count; -} - static const wchar_t *hit_group_name = L"hitgroup"; static const wchar_t *raygen_shader_name = L"raygeneration"; static const wchar_t *closesthit_shader_name = L"closesthit"; @@ -1657,10 +1644,10 @@ void gpu_raytrace_acceleration_structure_build(gpu_raytrace_acceleration_structu #ifdef is_forge create_srv_ib(_ib_full, _ib_full->count, 0); - create_srv_vb(_vb_full, _vb_full->count, vb[0]->impl.stride); + create_srv_vb(_vb_full, _vb_full->count, vb[0]->stride); #else create_srv_ib(ib[0], ib[0]->count, 0); - create_srv_vb(vb[0], vb[0]->count, vb[0]->impl.stride); + create_srv_vb(vb[0], vb[0]->count, vb[0]->stride); #endif // Reset the command list for the acceleration structure construction diff --git a/base/sources/backends/direct3d12_gpu.h b/base/sources/backends/direct3d12_gpu.h index f55b0d49..b2cf2698 100644 --- a/base/sources/backends/direct3d12_gpu.h +++ b/base/sources/backends/direct3d12_gpu.h @@ -61,7 +61,6 @@ typedef struct { struct ID3D12Resource *buffer; struct D3D12VertexBufferView vertex_buffer_view; struct D3D12IndexBufferView index_buffer_view; - int stride; int last_start; int last_count; } gpu_buffer_impl_t; diff --git a/base/sources/backends/metal_gpu.h b/base/sources/backends/metal_gpu.h index 275ccd06..da16ed3d 100644 --- a/base/sources/backends/metal_gpu.h +++ b/base/sources/backends/metal_gpu.h @@ -6,15 +6,12 @@ typedef struct { struct gpu_shader *vertex_shader; struct gpu_shader *fragment_shader; void *_pipeline; - void *_pipelineDepth; - void *_reflection; - void *_depthStencil; - void *_depthStencilNone; + void *_depth; } gpu_pipeline_impl_t; typedef struct { char name[1024]; - void *mtlFunction; + void *mtl_function; char *source; int length; } gpu_shader_impl_t; @@ -22,12 +19,10 @@ typedef struct { typedef struct { void *_tex; void *data; - void *_texReadback; + void *_readback; } gpu_texture_impl_t; typedef struct { - int myStride; - int count; void *metal_buffer; } gpu_buffer_impl_t; diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index 6ee257a0..9925a7bf 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -13,7 +13,6 @@ id getMetalLayer(void); id getMetalDevice(void); id getMetalQueue(void); - bool gpu_transpose_mat = true; static id command_buffer = nil; static id command_encoder = nil; @@ -21,9 +20,13 @@ static id argument_encoder = nil; static id argument_buffer = nil; static id drawable; static id linear_sampler; -static bool has_depth = false; static int argument_buffer_step; -static gpu_buffer_t *current_index_buffer; +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 MTLBlendFactor convert_blending_factor(gpu_blending_factor_t factor) { @@ -148,7 +151,7 @@ void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_t target->width = width; target->height = height; target->state = GPU_TEXTURE_STATE_RENDER_TARGET; - target->impl._texReadback = NULL; + target->impl._readback = NULL; if (framebuffer_index < 0) { id device = getMetalDevice(); @@ -255,48 +258,45 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { } void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { - - has_depth = depth_buffer != NULL; - - MTLRenderPassDescriptor *desc = [MTLRenderPassDescriptor renderPassDescriptor]; + render_pass_desc = [MTLRenderPassDescriptor renderPassDescriptor]; for (int i = 0; i < current_render_targets_count; ++i) { - desc.colorAttachments[i].texture = (__bridge id)current_render_targets[i]->impl._tex; + render_pass_desc.colorAttachments[i].texture = (__bridge id)current_render_targets[i]->impl._tex; if (flags & GPU_CLEAR_COLOR) { float red, green, blue, alpha; iron_color_components(color, &red, &green, &blue, &alpha); - desc.colorAttachments[i].loadAction = MTLLoadActionClear; - desc.colorAttachments[i].storeAction = MTLStoreActionStore; - desc.colorAttachments[i].clearColor = MTLClearColorMake(red, green, blue, alpha); + render_pass_desc.colorAttachments[i].loadAction = MTLLoadActionClear; + render_pass_desc.colorAttachments[i].storeAction = MTLStoreActionStore; + render_pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(red, green, blue, alpha); } else { - desc.colorAttachments[i].loadAction = MTLLoadActionLoad; - desc.colorAttachments[i].storeAction = MTLStoreActionStore; - desc.colorAttachments[i].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + render_pass_desc.colorAttachments[i].loadAction = MTLLoadActionLoad; + render_pass_desc.colorAttachments[i].storeAction = MTLStoreActionStore; + render_pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); } } if (depth_buffer != NULL) { - desc.depthAttachment.texture = (__bridge id)depth_buffer->impl._tex; + render_pass_desc.depthAttachment.texture = (__bridge id)depth_buffer->impl._tex; } if (flags & GPU_CLEAR_DEPTH) { - desc.depthAttachment.clearDepth = depth; - desc.depthAttachment.loadAction = MTLLoadActionClear; - desc.depthAttachment.storeAction = MTLStoreActionStore; + render_pass_desc.depthAttachment.clearDepth = depth; + render_pass_desc.depthAttachment.loadAction = MTLLoadActionClear; + render_pass_desc.depthAttachment.storeAction = MTLStoreActionStore; } else { - desc.depthAttachment.clearDepth = 1; - desc.depthAttachment.loadAction = MTLLoadActionLoad; - desc.depthAttachment.storeAction = MTLStoreActionStore; + render_pass_desc.depthAttachment.clearDepth = 1; + render_pass_desc.depthAttachment.loadAction = MTLLoadActionLoad; + render_pass_desc.depthAttachment.storeAction = MTLStoreActionStore; } - id commandQueue = getMetalQueue(); + id queue = getMetalQueue(); if (command_buffer == nil) { - command_buffer = [commandQueue commandBuffer]; + command_buffer = [queue commandBuffer]; } - command_encoder = [command_buffer renderCommandEncoderWithDescriptor:desc]; + command_encoder = [command_buffer renderCommandEncoderWithDescriptor:render_pass_desc]; } void gpu_end_internal() { @@ -311,11 +311,24 @@ void gpu_wait() { void gpu_flush() { [command_buffer commit]; gpu_wait(); - id commandQueue = getMetalQueue(); - command_buffer = [commandQueue commandBuffer]; + id queue = getMetalQueue(); + command_buffer = [queue commandBuffer]; + + if (gpu_in_use) { + command_encoder = [command_buffer renderCommandEncoderWithDescriptor:render_pass_desc]; + id pipe = (__bridge id)pipeline->impl._pipeline; + [command_encoder setRenderPipelineState:pipe]; + id depthStencil = (__bridge id)pipeline->impl._depth; + [command_encoder setDepthStencilState:depthStencil]; + [command_encoder setFrontFacingWinding:MTLWindingClockwise]; + [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; + [command_encoder setVertexBuffer:current_vb offset:0 atIndex:0]; + [command_encoder setViewport:current_viewport]; + [command_encoder setScissorRect:current_scissor]; + } } -void gpu_present() { +void gpu_present_internal() { [command_buffer presentDrawable:drawable]; [command_buffer commit]; [command_buffer waitUntilCompleted]; @@ -345,76 +358,67 @@ int gpu_max_bound_textures(void) { } void gpu_draw_internal() { - id indexBuffer = (__bridge id)current_index_buffer->impl.metal_buffer; + id index_buffer = (__bridge id)current_ib->impl.metal_buffer; [command_encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle - indexCount:gpu_index_buffer_count(current_index_buffer) + indexCount:current_ib->count indexType:MTLIndexTypeUInt32 - indexBuffer:indexBuffer + indexBuffer:index_buffer indexBufferOffset:0]; } void gpu_viewport(int x, int y, int width, int height) { - MTLViewport viewport; - viewport.originX = x; - viewport.originY = y; - viewport.width = width; - viewport.height = height; - viewport.znear = 0.1; - viewport.zfar = 100.0; - [command_encoder setViewport:viewport]; + current_viewport.originX = x; + current_viewport.originY = y; + current_viewport.width = width; + current_viewport.height = height; + current_viewport.znear = 0.1; + current_viewport.zfar = 100.0; + [command_encoder setViewport:current_viewport]; } void gpu_scissor(int x, int y, int width, int height) { - MTLScissorRect scissor; - scissor.x = x; - scissor.y = y; + current_scissor.x = x; + current_scissor.y = y; int target_w = current_render_targets[0]->width; int target_h = current_render_targets[0]->height; - scissor.width = (x + width <= target_w) ? width : target_w - x; - scissor.height = (y + height <= target_h) ? height : target_h - y; - [command_encoder setScissorRect:scissor]; + current_scissor.width = (x + width <= target_w) ? width : target_w - x; + current_scissor.height = (y + height <= target_h) ? height : target_h - y; + [command_encoder setScissorRect:current_scissor]; } void gpu_disable_scissor() { - MTLScissorRect scissor; - scissor.x = 0; - scissor.y = 0; - scissor.width = current_render_targets[0]->width; - scissor.height = current_render_targets[0]->height; - [command_encoder setScissorRect:scissor]; + current_scissor.x = 0; + current_scissor.y = 0; + current_scissor.width = current_render_targets[0]->width; + current_scissor.height = current_render_targets[0]->height; + [command_encoder setScissorRect:current_scissor]; } void gpu_set_pipeline(gpu_pipeline_t *pipeline) { - if (has_depth) { - id pipe = (__bridge id)pipeline->impl._pipelineDepth; - [command_encoder setRenderPipelineState:pipe]; - id depthStencil = (__bridge id)pipeline->impl._depthStencil; - [command_encoder setDepthStencilState:depthStencil]; - } - else { - id pipe = (__bridge id)pipeline->impl._pipeline; - [command_encoder setRenderPipelineState:pipe]; - id depthStencil = (__bridge id)pipeline->impl._depthStencilNone; - [command_encoder setDepthStencilState:depthStencil]; - } + current_pipeline = pipeline; + id pipe = (__bridge id)pipeline->impl._pipeline; + [command_encoder setRenderPipelineState:pipe]; + id depthStencil = (__bridge id)pipeline->impl._depth; + [command_encoder setDepthStencilState:depthStencil]; [command_encoder setFrontFacingWinding:MTLWindingClockwise]; [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; } -void gpu_set_vertex_buffer(gpu_buffer_t *buf) { - id buffer = (__bridge id)buf->impl.metal_buffer; - [command_encoder setVertexBuffer:buffer offset:0 atIndex:0]; +void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { + current_vb = buffer; + id buf = (__bridge id)buffer->impl.metal_buffer; + [command_encoder setVertexBuffer:buf offset:0 atIndex:0]; } void gpu_set_index_buffer(gpu_buffer_t *buffer) { - current_index_buffer = buffer; + current_ib = buffer; } void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) { gpu_flush(); // Create readback buffer - if (render_target->impl._texReadback == NULL) { + if (render_target->impl._readback == NULL) { id device = getMetalDevice(); MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new]; descriptor.textureType = MTLTextureType2D; @@ -426,19 +430,19 @@ void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) { descriptor.mipmapLevelCount = 1; descriptor.usage = MTLTextureUsageUnknown; descriptor.resourceOptions = MTLResourceStorageModeShared; - render_target->impl._texReadback = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; + render_target->impl._readback = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; } // Copy render target to readback buffer - id commandQueue = getMetalQueue(); - id commandBuffer = [commandQueue commandBuffer]; + id queue = getMetalQueue(); + id commandBuffer = [queue commandBuffer]; id commandEncoder = [commandBuffer blitCommandEncoder]; [commandEncoder copyFromTexture:(__bridge id)render_target->impl._tex sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(render_target->width, render_target->height, 1) - toTexture:(__bridge id)render_target->impl._texReadback + toTexture:(__bridge id)render_target->impl._readback destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)]; @@ -447,7 +451,7 @@ void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) { [commandBuffer waitUntilCompleted]; // Read buffer - id tex = (__bridge id)render_target->impl._texReadback; + id tex = (__bridge id)render_target->impl._readback; int format_byte_size = format_size([(__bridge id)render_target->impl._tex pixelFormat]); MTLRegion region = MTLRegionMake2D(0, 0, render_target->width, render_target->height); [tex getBytes:data bytesPerRow:format_byte_size * render_target->width fromRegion:region mipmapLevel:0]; @@ -482,17 +486,9 @@ void gpu_pipeline_destroy(gpu_pipeline_t *pipeline) { pipe = nil; pipeline->impl._pipeline = NULL; - id pipeDepth = (__bridge_transfer id)pipeline->impl._pipelineDepth; - pipeDepth = nil; - pipeline->impl._pipelineDepth = NULL; - - id depthStencil = (__bridge_transfer id)pipeline->impl._depthStencil; + id depthStencil = (__bridge_transfer id)pipeline->impl._depth; depthStencil = nil; - pipeline->impl._depthStencil = NULL; - - id depthStencilNone = (__bridge_transfer id)pipeline->impl._depthStencilNone; - depthStencilNone = nil; - pipeline->impl._depthStencilNone = NULL; + pipeline->impl._depth = NULL; } void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { @@ -503,15 +499,15 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { iron_error("%s", error.localizedDescription.UTF8String); } - pipeline->vertex_shader->impl.mtlFunction = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->vertex_shader->impl.name encoding:NSUTF8StringEncoding]]; - assert(pipeline->vertex_shader->impl.mtlFunction); + pipeline->vertex_shader->impl.mtl_function = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->vertex_shader->impl.name encoding:NSUTF8StringEncoding]]; + assert(pipeline->vertex_shader->impl.mtl_function); - pipeline->fragment_shader->impl.mtlFunction = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->fragment_shader->impl.name encoding:NSUTF8StringEncoding]]; - assert(pipeline->fragment_shader->impl.mtlFunction); + pipeline->fragment_shader->impl.mtl_function = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->fragment_shader->impl.name encoding:NSUTF8StringEncoding]]; + assert(pipeline->fragment_shader->impl.mtl_function); MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; - renderPipelineDesc.vertexFunction = (__bridge id)pipeline->vertex_shader->impl.mtlFunction; - renderPipelineDesc.fragmentFunction = (__bridge id)pipeline->fragment_shader->impl.mtlFunction; + renderPipelineDesc.vertexFunction = (__bridge id)pipeline->vertex_shader->impl.mtl_function; + renderPipelineDesc.fragmentFunction = (__bridge id)pipeline->fragment_shader->impl.mtl_function; for (int i = 0; i < pipeline->color_attachment_count; ++i) { renderPipelineDesc.colorAttachments[i].pixelFormat = convert_render_target_format(pipeline->color_attachment[i]); @@ -530,7 +526,7 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { (pipeline->color_write_mask_blue[i] ? MTLColorWriteMaskBlue : 0) | (pipeline->color_write_mask_alpha[i] ? MTLColorWriteMaskAlpha : 0); } - renderPipelineDesc.depthAttachmentPixelFormat = MTLPixelFormatInvalid; + renderPipelineDesc.depthAttachmentPixelFormat = depth_attachment_bits > 0 ? MTLPixelFormatDepth32Float : MTLPixelFormatInvalid; float offset = 0; MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init]; @@ -576,27 +572,16 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) { reflection:&reflection error:&errors]; - renderPipelineDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float; - pipeline->impl._pipelineDepth = (__bridge_retained void *)[ - device newRenderPipelineStateWithDescriptor:renderPipelineDesc - options:MTLPipelineOptionBufferTypeInfo - reflection:&reflection - error:&errors]; - MTLDepthStencilDescriptor *depthStencilDescriptor = [MTLDepthStencilDescriptor new]; depthStencilDescriptor.depthCompareFunction = convert_compare_mode(pipeline->depth_mode); depthStencilDescriptor.depthWriteEnabled = pipeline->depth_write; - pipeline->impl._depthStencil = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; - - depthStencilDescriptor.depthCompareFunction = MTLCompareFunctionAlways; - depthStencilDescriptor.depthWriteEnabled = false; - pipeline->impl._depthStencilNone = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; + pipeline->impl._depth = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; } void gpu_shader_destroy(gpu_shader_t *shader) { - id function = (__bridge_transfer id)shader->impl.mtlFunction; + id function = (__bridge_transfer id)shader->impl.mtl_function; function = nil; - shader->impl.mtlFunction = NULL; + shader->impl.mtl_function = NULL; } void gpu_shader_init(gpu_shader_t *shader, const void *data, size_t length, gpu_shader_type_t type) { @@ -650,9 +635,9 @@ void gpu_texture_destroy(gpu_texture_t *target) { tex = nil; target->impl._tex = NULL; - id texReadback = (__bridge_transfer id)target->impl._texReadback; - texReadback = nil; - target->impl._texReadback = NULL; + id readback = (__bridge_transfer id)target->impl._readback; + readback = nil; + target->impl._readback = NULL; } void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) { @@ -667,14 +652,14 @@ void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structur buffer->count = count; for (int i = 0; i < structure->size; ++i) { gpu_vertex_element_t element = structure->elements[i]; - buffer->impl.myStride += gpu_vertex_data_size(element.data); + buffer->stride += gpu_vertex_data_size(element.data); } id device = getMetalDevice(); MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; options |= MTLResourceStorageModeShared; - id buf = [device newBufferWithLength:count * buffer->impl.myStride options:options]; + id buf = [device newBufferWithLength:count * buffer->stride options:options]; buffer->impl.metal_buffer = (__bridge_retained void *)buf; } @@ -687,16 +672,8 @@ float *gpu_vertex_buffer_lock(gpu_buffer_t *buf) { void gpu_vertex_buffer_unlock(gpu_buffer_t *buf) { } -int gpu_vertex_buffer_count(gpu_buffer_t *buffer) { - return buffer->count; -} - -int gpu_vertex_buffer_stride(gpu_buffer_t *buffer) { - return buffer->impl.myStride; -} - void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) { - buffer->impl.count = size; + buffer->count = size; buffer->data = NULL; buffer->impl.metal_buffer = (__bridge_retained void *)[getMetalDevice() newBufferWithLength:size options:MTLResourceOptionCPUCacheModeDefault]; } @@ -716,12 +693,8 @@ void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count) { void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) { } -int gpu_constant_buffer_size(gpu_buffer_t *buffer) { - return buffer->impl.count; -} - void gpu_index_buffer_init(gpu_buffer_t *buffer, int indexCount) { - buffer->impl.count = indexCount; + buffer->count = indexCount; id device = getMetalDevice(); MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; @@ -747,10 +720,6 @@ void *gpu_index_buffer_lock(gpu_buffer_t *buffer) { void gpu_index_buffer_unlock(gpu_buffer_t *buffer) { } -int gpu_index_buffer_count(gpu_buffer_t *buffer) { - return buffer->impl.count; -} - typedef struct inst { iron_matrix4x4_t m; int i; @@ -910,8 +879,8 @@ void gpu_raytrace_acceleration_structure_build(gpu_raytrace_acceleration_structu descriptor.indexType = MTLIndexTypeUInt32; descriptor.indexBuffer = (__bridge id)ib[0]->impl.metal_buffer; descriptor.vertexBuffer = (__bridge id)vb[0]->impl.metal_buffer; - descriptor.vertexStride = vb[0]->impl.myStride; - descriptor.triangleCount = ib[0]->impl.count / 3; + descriptor.vertexStride = vb[0]->stride; + descriptor.triangleCount = ib[0]->count / 3; descriptor.vertexFormat = MTLAttributeFormatShort4Normalized; MTLPrimitiveAccelerationStructureDescriptor *accel_descriptor = [MTLPrimitiveAccelerationStructureDescriptor descriptor]; diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index eac0ba4b..ced59f8b 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -28,6 +28,10 @@ static VkSemaphore framebuffer_available_semaphore; static VkSemaphore rendering_finished_semaphore; static VkFence fence; static gpu_pipeline_t *current_pipeline = NULL; +static VkViewport current_viewport; +static VkRect2D current_scissor; +static gpu_buffer_t *current_vb; +static gpu_buffer_t *current_ib; static VkDescriptorSetLayout descriptor_layout; static VkDescriptorSet descriptor_sets[MAX_DESCRIPTOR_SETS]; static VkRenderingInfo current_rendering_info; @@ -35,7 +39,6 @@ static VkRenderingAttachmentInfo current_color_attachment_infos[8]; static VkRenderingAttachmentInfo current_depth_attachment_info; static VkPhysicalDeviceMemoryProperties memory_properties; static VkSampler immutable_sampler; -static int index_count = 0; static VkCommandBuffer command_buffer; static VkInstance instance; @@ -1050,25 +1053,6 @@ int gpu_max_bound_textures(void) { return props.limits.maxPerStageDescriptorSamplers; } -static void set_viewport_and_scissor() { - VkViewport viewport = { - .x = 0, - .y = (float)current_render_targets[0]->height, - .width = (float)current_render_targets[0]->width, - .height = -(float)current_render_targets[0]->height, - .minDepth = (float)0.0f, - .maxDepth = (float)1.0f, - }; - VkRect2D scissor = { - .extent.width = current_render_targets[0]->width, - .extent.height = current_render_targets[0]->height, - .offset.x = 0, - .offset.y = 0, - }; - vkCmdSetViewport(command_buffer, 0, 1, &viewport); - vkCmdSetScissor(command_buffer, 0, 1, &scissor); -} - 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(); @@ -1134,11 +1118,11 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * dept }; vkCmdBeginRendering(command_buffer, ¤t_rendering_info); - set_viewport_and_scissor(); + gpu_viewport(0, 0, current_render_targets[0]->width, current_render_targets[0]->height); + gpu_scissor(0, 0, current_render_targets[0]->width, current_render_targets[0]->height); - if (current_pipeline != NULL) { - vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.pipeline); - } + vkCmdSetViewport(command_buffer, 0, 1, &viewport); + vkCmdSetScissor(command_buffer, 0, 1, &scissor); if (flags != GPU_CLEAR_NONE) { int count = 0; @@ -1196,7 +1180,6 @@ void gpu_flush() { .commandBufferCount = 1, .pCommandBuffers = &command_buffer, }; - vkQueueSubmit(queue, 1, &submit_info, fence); gpu_wait(); @@ -1208,9 +1191,22 @@ void gpu_flush() { .pInheritanceInfo = NULL, }; vkBeginCommandBuffer(command_buffer, &begin_info); + + if (gpu_in_use) { + vkCmdBeginRendering(command_buffer, ¤t_rendering_info); + vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.pipeline); + VkBuffer buffers[1]; + VkDeviceSize offsets[1]; + buffers[0] = current_vb->impl.buf; + offsets[0] = (VkDeviceSize)(0); + vkCmdBindVertexBuffers(command_buffer, 0, 1, buffers, offsets); + vkCmdBindIndexBuffer(command_buffer, current_ib->impl.buf, 0, VK_INDEX_TYPE_UINT32); + vkCmdSetViewport(command_buffer, 0, 1, ¤t_viewport); + vkCmdSetScissor(command_buffer, 0, 1, ¤t_scissor); + } } -void gpu_present() { +void gpu_present_internal() { vkEndCommandBuffer(command_buffer); vkResetFences(device, 1, &fence); @@ -1255,37 +1251,37 @@ void gpu_present() { } void gpu_draw_internal() { - vkCmdDrawIndexed(command_buffer, index_count, 1, 0, 0, 0); + vkCmdDrawIndexed(command_buffer, current_ib->count, 1, 0, 0, 0); } void gpu_viewport(int x, int y, int width, int height) { - VkViewport viewport; - memset(&viewport, 0, sizeof(viewport)); - viewport.x = (float)x; - viewport.y = y + (float)height; - viewport.width = (float)width; - viewport.height = (float)-height; - viewport.minDepth = (float)0.0f; - viewport.maxDepth = (float)1.0f; - vkCmdSetViewport(command_buffer, 0, 1, &viewport); + current_viewport = (VkViewport){ + .x = (float)x, + .y = y + (float)height, + .width = (float)width, + .height = (float)-height, + .minDepth = (float)0.0f, + .maxDepth = (float)1.0f, + }; + vkCmdSetViewport(command_buffer, 0, 1, ¤t_viewport); } void gpu_scissor(int x, int y, int width, int height) { - VkRect2D scissor; - memset(&scissor, 0, sizeof(scissor)); - scissor.extent.width = width; - scissor.extent.height = height; - scissor.offset.x = x; - scissor.offset.y = y; - vkCmdSetScissor(command_buffer, 0, 1, &scissor); + current_scissor = (VkRect2D){ + .offset.x = x, + .offset.y = y, + .extent.width = width, + .extent.height = height, + }; + vkCmdSetScissor(command_buffer, 0, 1, ¤t_scissor); } void gpu_disable_scissor() { - VkRect2D scissor; - memset(&scissor, 0, sizeof(scissor)); - scissor.extent.width = current_render_targets[0]->width; - scissor.extent.height = current_render_targets[0]->height; - vkCmdSetScissor(command_buffer, 0, 1, &scissor); + current_scissor = (VkRect2D){ + .extent.width = current_render_targets[0]->width, + .extent.height = current_render_targets[0]->height, + }; + vkCmdSetScissor(command_buffer, 0, 1, ¤t_scissor); } void gpu_set_pipeline(gpu_pipeline_t *pipeline) { @@ -1294,6 +1290,7 @@ void gpu_set_pipeline(gpu_pipeline_t *pipeline) { } void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { + current_vb = buffer; VkBuffer buffers[1]; VkDeviceSize offsets[1]; buffers[0] = buffer->impl.buf; @@ -1302,7 +1299,7 @@ void gpu_set_vertex_buffer(gpu_buffer_t *buffer) { } void gpu_set_index_buffer(gpu_buffer_t *buffer) { - index_count = gpu_index_buffer_count(buffer); + current_ib = buffer; vkCmdBindIndexBuffer(command_buffer, buffer->impl.buf, 0, VK_INDEX_TYPE_UINT32); } @@ -1923,16 +1920,8 @@ void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer) { vkUnmapMemory(device, buffer->impl.mem); } -int gpu_vertex_buffer_count(gpu_buffer_t *buffer) { - return buffer->count; -} - -int gpu_vertex_buffer_stride(gpu_buffer_t *buffer) { - return buffer->impl.stride; -} - void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) { - buffer->impl.count = size; + buffer->count = size; buffer->data = NULL; VkBufferCreateInfo buf_info; @@ -1979,10 +1968,6 @@ void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) { buffer->data = NULL; } -int gpu_constant_buffer_size(gpu_buffer_t *buffer) { - return buffer->impl.count; -} - void gpu_index_buffer_init(gpu_buffer_t *buffer, int count) { buffer->count = count; @@ -2040,10 +2025,6 @@ void gpu_index_buffer_unlock(gpu_buffer_t *buffer) { vkUnmapMemory(device, buffer->impl.mem); } -int gpu_index_buffer_count(gpu_buffer_t *buffer) { - return buffer->count; -} - static const int INDEX_RAYGEN = 0; static const int INDEX_MISS = 1; static const int INDEX_CLOSEST_HIT = 2; diff --git a/base/sources/backends/vulkan_gpu.h b/base/sources/backends/vulkan_gpu.h index df211506..24aa27d3 100644 --- a/base/sources/backends/vulkan_gpu.h +++ b/base/sources/backends/vulkan_gpu.h @@ -34,16 +34,10 @@ typedef struct { } gpu_texture_impl_t; typedef struct { - int count; VkBuffer buf; VkDeviceMemory mem; VkMemoryAllocateInfo mem_alloc; float *data; - int stride; - unsigned bufferId; - int lastStart; - int lastCount; - int mySize; } gpu_buffer_impl_t; typedef struct { diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index 775c3e7e..278e62a5 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -79,8 +79,7 @@ void gpu_end_internal() { wgpuQueueSubmit(queue, 1, &commands); } -void gpu_present() { - +void gpu_present_internal() { } bool gpu_raytrace_supported() { @@ -89,17 +88,16 @@ bool gpu_raytrace_supported() { void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure) { buffer->count = count; - buffer->impl.count = count; - buffer->impl.stride = 0; + buffer->stride = 0; for (int i = 0; i < structure->size; ++i) { - buffer->impl.stride += gpu_vertex_data_size(structure->elements[i].data); + buffer->stride += gpu_vertex_data_size(structure->elements[i].data); } } float *gpu_vertex_buffer_lock(gpu_buffer_t *buffer) { WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); - bDesc.size = buffer->impl.count * buffer->impl.stride * sizeof(float); + bDesc.size = buffer->count * buffer->stride * sizeof(float); bDesc.usage = WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst; bDesc.mappedAtCreation = true; buffer->impl.buffer = wgpuDeviceCreateBuffer(device, &bDesc); @@ -110,55 +108,33 @@ void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -int gpu_vertex_buffer_count(gpu_buffer_t *buffer) { - return buffer->impl.count; -} - -int gpu_vertex_buffer_stride(gpu_buffer_t *buffer) { - return buffer->impl.stride; -} - void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) {} void gpu_constant_buffer_destroy(gpu_buffer_t *buffer) {} void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count) {} void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) {} -int gpu_constant_buffer_size(gpu_buffer_t *buffer) { - return 0; -} - void gpu_index_buffer_init(gpu_buffer_t *buffer, int count) { - buffer->impl.count = count; + buffer->count = count; } void gpu_buffer_destroy(gpu_buffer_t *buffer) {} -static int gpu_internal_index_buffer_stride(gpu_buffer_t *buffer) { - return 4; -} - void *gpu_index_buffer_lock(gpu_buffer_t *buffer) { int start = 0; - int count = gpu_index_buffer_count(buffer); + int count = buffer->count; WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); - bDesc.size = count * gpu_internal_index_buffer_stride(buffer); + bDesc.size = count * 4; bDesc.usage = WGPUBufferUsage_Index | WGPUBufferUsage_CopyDst; bDesc.mappedAtCreation = true; buffer->impl.buffer = wgpuDeviceCreateBuffer(device, &bDesc); - return wgpuBufferGetMappedRange(buffer->impl.buffer, start * gpu_internal_index_buffer_stride(buffer), bDesc.size); + return wgpuBufferGetMappedRange(buffer->impl.buffer, start * 4, bDesc.size); } void gpu_index_buffer_unlock(gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -void gpu_internal_index_buffer_set(gpu_buffer_t *buffer) {} - -int gpu_index_buffer_count(gpu_buffer_t *buffer) { - return buffer->impl.count; -} - void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) {} void gpu_texture_destroy(gpu_texture_t *texture) {} @@ -303,17 +279,18 @@ void gpu_set_pipeline(struct gpu_pipeline *pipeline) { void gpu_set_pipeline_layout() {} void gpu_set_vertex_buffer(struct gpu_buffer *buffer) { - uint64_t size = (gpu_vertex_buffer_count(buffer)) * gpu_vertex_buffer_stride(buffer); + uint64_t size = buffer->count * buffer->stride; wgpuRenderPassEncoderSetVertexBuffer(pass, 0, buffer->impl.buffer, 0, size); } void gpu_set_index_buffer(struct gpu_buffer *buffer) { - indexCount = gpu_index_buffer_count(buffer); - uint64_t size = gpu_index_buffer_count(buffer) * sizeof(int); + indexCount = buffer->count; + uint64_t size = buffer->count * sizeof(int); wgpuRenderPassEncoderSetIndexBuffer(pass, buffer->impl.buffer, WGPUIndexFormat_Uint32, 0, size); } void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) {} void gpu_wait() {} +void gpu_flush() {} void gpu_set_constant_buffer(struct gpu_buffer *buffer, int offset, size_t size) {} void gpu_set_texture(int unit, gpu_texture_t *texture) {} diff --git a/base/sources/backends/webgpu_gpu.h b/base/sources/backends/webgpu_gpu.h index c3f113e5..9c76a2d1 100644 --- a/base/sources/backends/webgpu_gpu.h +++ b/base/sources/backends/webgpu_gpu.h @@ -6,7 +6,6 @@ typedef struct { WGPUBuffer buffer; - int count; int stride; } gpu_buffer_impl_t; diff --git a/base/sources/iron.h b/base/sources/iron.h index 733014e3..9bc70cf2 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -930,7 +930,7 @@ u32_array_t *gpu_lock_index_buffer(gpu_buffer_t *buffer) { void *vertices = gpu_index_buffer_lock(buffer); u32_array_t *ar = (u32_array_t *)malloc(sizeof(u32_array_t)); ar->buffer = vertices; - ar->length = gpu_index_buffer_count(buffer); + ar->length = buffer->count; return ar; } @@ -949,7 +949,7 @@ buffer_t *gpu_lock_vertex_buffer(gpu_buffer_t *buffer) { float *vertices = gpu_vertex_buffer_lock(buffer); buffer_t *b = (buffer_t *)malloc(sizeof(buffer_t)); b->buffer = vertices; - b->length = buffer->count * gpu_vertex_buffer_stride(buffer); + b->length = buffer->count * buffer->stride; return b; } @@ -1362,9 +1362,9 @@ buffer_t *gpu_get_texture_pixels(gpu_texture_t *image) { image->impl.readback->lpVtbl->Release(image->impl.readback); image->impl.readback = NULL; #elif defined(IRON_METAL) - // id texReadback = (__bridge_transfer id)image->impl._texReadback; - // texReadback = nil; - // image->impl._texReadback = NULL; + // id readback = (__bridge_transfer id)image->impl._readback; + // readback = nil; + // image->impl._readback = NULL; #endif return image->buffer; @@ -2104,7 +2104,7 @@ void iron_raytrace_set_textures(gpu_texture_t *tex0, gpu_texture_t *tex1, gpu_te void iron_raytrace_dispatch_rays(gpu_texture_t *render_target, buffer_t *buffer) { float *cb = (float *)buffer->buffer; - gpu_constant_buffer_lock(&constant_buffer, 0, gpu_constant_buffer_size(&constant_buffer)); + gpu_constant_buffer_lock(&constant_buffer, 0, constant_buffer.count); for (int i = 0; i < constant_buffer_size; ++i) { float *floats = (float *)(&constant_buffer.data[i * 4]); floats[0] = cb[i]; diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index c152ef66..040cebe6 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -8,6 +8,8 @@ static gpu_buffer_t constant_buffer; static bool gpu_thrown = false; int constant_buffer_index = 0; +int draw_calls = 0; +int draw_calls_last = 0; bool gpu_in_use = false; gpu_texture_t *current_render_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int current_render_targets_count = 0; @@ -64,11 +66,18 @@ void gpu_draw() { gpu_constant_buffer_unlock(&constant_buffer); gpu_set_constant_buffer(&constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); gpu_draw_internal(); - ++constant_buffer_index; + + constant_buffer_index++; if (constant_buffer_index >= CONSTANT_BUFFER_MULTIPLE) { constant_buffer_index = 0; - // gpu_wait(); } + + draw_calls++; + if (draw_calls + draw_calls_last >= CONSTANT_BUFFER_MULTIPLE) { + draw_calls = draw_calls_last = constant_buffer_index = 0; + gpu_flush(); + } + gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); } @@ -78,10 +87,15 @@ void gpu_end() { iron_log("Begin before you end"); } gpu_in_use = false; - gpu_end_internal(); } +void gpu_present() { + gpu_present_internal(); + draw_calls_last = draw_calls; + draw_calls = 0; +} + void gpu_resize(int width, int height) { if (width == 0 || height == 0) { return; diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index dcfbba15..08445125 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -90,6 +90,7 @@ typedef struct gpu_texture { typedef struct gpu_buffer { int count; + int stride; uint8_t *data; gpu_buffer_impl_t impl; } gpu_buffer_t; @@ -148,6 +149,7 @@ void gpu_end_internal(void); void gpu_wait(void); void gpu_flush(void); void gpu_present(void); +void gpu_present_internal(void); void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after); void gpu_create_framebuffers(int depth_buffer_bits); void gpu_init(int depth_buffer_bits, bool vsync); @@ -178,18 +180,14 @@ void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure); float *gpu_vertex_buffer_lock(gpu_buffer_t *buffer); void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer); -int gpu_vertex_buffer_count(gpu_buffer_t *buffer); -int gpu_vertex_buffer_stride(gpu_buffer_t *buffer); void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size); void gpu_constant_buffer_destroy(gpu_buffer_t *buffer); void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count); void gpu_constant_buffer_unlock(gpu_buffer_t *buffer); -int gpu_constant_buffer_size(gpu_buffer_t *buffer); void gpu_index_buffer_init(gpu_buffer_t *buffer, int count); void gpu_buffer_destroy(gpu_buffer_t *buffer); void *gpu_index_buffer_lock(gpu_buffer_t *buffer); void gpu_index_buffer_unlock(gpu_buffer_t *buffer); -int gpu_index_buffer_count(gpu_buffer_t *buffer); void gpu_pipeline_init(gpu_pipeline_t *pipeline); void gpu_internal_pipeline_init(gpu_pipeline_t *pipeline);