From a8a05deffcd491c317aaa8a5346da84d7dc559f6 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 18 Mar 2025 10:39:23 +0100 Subject: [PATCH] Cleanup --- armorlab/sources/render_path_paint.ts | 2 +- armorlab/sources/ui_nodes_ext.ts | 2 +- armorpaint/sources/render_path_paint.ts | 2 +- base/sources/backends/direct3d12_gpu.c | 80 +++++----- base/sources/backends/direct3d12_gpu.h | 34 ++-- base/sources/backends/metal_gpu.h | 18 +-- base/sources/backends/metal_gpu.m | 79 +++++----- base/sources/backends/vulkan_gpu.c | 199 +++++++++++------------- base/sources/backends/vulkan_gpu.h | 30 ++-- base/sources/backends/webgpu_gpu.c | 64 ++++---- base/sources/backends/webgpu_gpu.h | 11 +- base/sources/iron.h | 28 ++-- base/sources/iron_draw.c | 28 ++-- base/sources/iron_gpu.c | 127 +++++---------- base/sources/iron_gpu.h | 142 ++++++++--------- base/sources/ts/iron/const_data.ts | 12 +- base/sources/ts/iron/iron.ts | 10 +- base/sources/ts/iron/mesh_data.ts | 14 +- base/sources/ts/iron/scene.ts | 6 +- base/sources/ts/iron/uniforms.ts | 4 +- base/sources/ts/line_draw.ts | 12 +- base/sources/ts/render_path_raytrace.ts | 8 +- base/sources/ts/util_mesh.ts | 10 +- base/sources/ts/util_render.ts | 6 +- base/tests/triangle/main.ts | 2 +- 25 files changed, 407 insertions(+), 523 deletions(-) diff --git a/armorlab/sources/render_path_paint.ts b/armorlab/sources/render_path_paint.ts index b6cccf15..1267b5e5 100644 --- a/armorlab/sources/render_path_paint.ts +++ b/armorlab/sources/render_path_paint.ts @@ -190,7 +190,7 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 render_path_set_target(""); iron_gpu_set_pipeline(pipes_cursor); let gbuffer0: render_target_t = map_get(render_path_render_targets, "gbuffer0"); - gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0._image); + _gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0._image); _gpu_set_float2(pipes_cursor_mouse, mx, my); _gpu_set_float2(pipes_cursor_tex_step, 1 / gbuffer0._image.width, 1 / gbuffer0._image.height); _gpu_set_float(pipes_cursor_radius, radius); diff --git a/armorlab/sources/ui_nodes_ext.ts b/armorlab/sources/ui_nodes_ext.ts index 2d2ed04e..9aceb99e 100644 --- a/armorlab/sources/ui_nodes_ext.ts +++ b/armorlab/sources/ui_nodes_ext.ts @@ -105,7 +105,7 @@ function ui_nodes_ext_run() { buffer_set_i16(vertices, i * 2, buffer_get_i16(ui_nodes_ext_last_vertices, i * 2)); } } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); // Apply displacement if (config_raw.displace_strength > 0) { diff --git a/armorpaint/sources/render_path_paint.ts b/armorpaint/sources/render_path_paint.ts index baea79a7..f3837516 100644 --- a/armorpaint/sources/render_path_paint.ts +++ b/armorpaint/sources/render_path_paint.ts @@ -442,7 +442,7 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 iron_gpu_set_pipeline(pipes_cursor); let rt: render_target_t = map_get(render_path_render_targets, "gbuffer0"); let gbuffer0: iron_gpu_texture_t = rt._image; - gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0); + _gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0); _gpu_set_float2(pipes_cursor_mouse, mx, my); _gpu_set_float2(pipes_cursor_tex_step, 1 / gbuffer0.width, 1 / gbuffer0.height); _gpu_set_float(pipes_cursor_radius, radius); diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 5cc16bde..6bf17873 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -442,8 +442,6 @@ bool iron_gpu_swap_buffers() { return true; } -void iron_gpu_flush() {} - bool iron_gpu_raytrace_supported() { D3D12_FEATURE_DATA_D3D12_OPTIONS5 options; if (device->lpVtbl->CheckFeatureSupport(device, D3D12_FEATURE_D3D12_OPTIONS5, &options, sizeof(options)) == S_OK) { @@ -611,12 +609,12 @@ void iron_gpu_command_list_render_target_to_texture_barrier(struct iron_gpu_comm list->impl._commandList->lpVtbl->ResourceBarrier(list->impl._commandList, 1, &barrier); } -void iron_gpu_command_list_set_vertex_constant_buffer(struct iron_gpu_command_list *list, iron_gpu_constant_buffer_t *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_vertex_constant_buffer(struct iron_gpu_command_list *list, iron_gpu_buffer_t *buffer, int offset, size_t size) { assert(list->impl.open); list->impl._commandList->lpVtbl->SetGraphicsRootConstantBufferView(list->impl._commandList, 2, buffer->impl.constant_buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.constant_buffer) + offset); } -void iron_gpu_command_list_set_fragment_constant_buffer(struct iron_gpu_command_list *list, iron_gpu_constant_buffer_t *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_fragment_constant_buffer(struct iron_gpu_command_list *list, iron_gpu_buffer_t *buffer, int offset, size_t size) { assert(list->impl.open); list->impl._commandList->lpVtbl->SetGraphicsRootConstantBufferView(list->impl._commandList, 3, buffer->impl.constant_buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.constant_buffer) + offset); } @@ -700,7 +698,7 @@ void iron_gpu_command_list_set_pipeline(struct iron_gpu_command_list *list, iron iron_gpu_internal_setConstants(list, list->impl._currentPipeline); } -void iron_gpu_command_list_set_vertex_buffer(struct iron_gpu_command_list *list, iron_gpu_vertex_buffer_t *buffer) { +void iron_gpu_command_list_set_vertex_buffer(struct iron_gpu_command_list *list, iron_gpu_buffer_t *buffer) { assert(list->impl.open); D3D12_VERTEX_BUFFER_VIEW *views = (D3D12_VERTEX_BUFFER_VIEW *)alloca(sizeof(D3D12_VERTEX_BUFFER_VIEW) * 1); @@ -713,7 +711,7 @@ void iron_gpu_command_list_set_vertex_buffer(struct iron_gpu_command_list *list, list->impl._commandList->lpVtbl->IASetVertexBuffers(list->impl._commandList, 0, 1, views); } -void iron_gpu_command_list_set_index_buffer(struct iron_gpu_command_list *list, iron_gpu_index_buffer_t *buffer) { +void iron_gpu_command_list_set_index_buffer(struct iron_gpu_command_list *list, iron_gpu_buffer_t *buffer) { assert(list->impl.open); list->impl._indexCount = iron_gpu_index_buffer_count(buffer); @@ -748,9 +746,9 @@ void iron_gpu_command_list_set_render_targets(struct iron_gpu_command_list *list list->impl.current_full_scissor = render_target->impl.scissor; } -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) {} +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, iron_gpu_index_buffer_t *buffer) { +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer) { assert(list->impl.open); iron_gpu_internal_index_buffer_upload(buffer, list->impl._commandList); } @@ -2102,9 +2100,9 @@ void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *render_target, ir render_target->impl.depthStencilTexture = source->impl.depthStencilTexture; } -iron_gpu_vertex_buffer_t *_current_vertex_buffer = NULL; +iron_gpu_buffer_t *_current_vertex_buffer = NULL; -void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { buffer->impl.myCount = count; buffer->impl.myStride = 0; @@ -2145,16 +2143,16 @@ void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, ir buffer->impl.lastCount = iron_gpu_vertex_buffer_count(buffer); } -void iron_gpu_vertex_buffer_destroy(iron_gpu_vertex_buffer_t *buffer) { +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { // buffer->impl.vertexBuffer->lpVtbl->Release(); buffer->impl.uploadBuffer->lpVtbl->Release(buffer->impl.uploadBuffer); } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buffer) { +float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { return iron_gpu_vertex_buffer_lock(buffer, 0, iron_gpu_vertex_buffer_count(buffer)); } -float *iron_gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buffer, int start, int count) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.lastStart = start; buffer->impl.lastCount = count; @@ -2169,36 +2167,36 @@ float *iron_gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buffer, int start, return (float *)bytes; } -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_vertex_buffer_t *buffer) { +void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { D3D12_RANGE range; range.Begin = buffer->impl.lastStart * buffer->impl.myStride; range.End = (buffer->impl.lastStart + buffer->impl.lastCount) * buffer->impl.myStride; buffer->impl.uploadBuffer->lpVtbl->Unmap(buffer->impl.uploadBuffer, 0, &range); } -void iron_gpu_vertex_buffer_unlock(iron_gpu_vertex_buffer_t *buffer, int count) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { D3D12_RANGE range; range.Begin = buffer->impl.lastStart * buffer->impl.myStride; range.End = (buffer->impl.lastStart + count) * buffer->impl.myStride; buffer->impl.uploadBuffer->lpVtbl->Unmap(buffer->impl.uploadBuffer, 0, &range); } -int iron_gpu_internal_vertex_buffer_set(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buffer) { _current_vertex_buffer = buffer; return 0; } -int iron_gpu_vertex_buffer_count(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.myCount; } -int iron_gpu_vertex_buffer_stride(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { return buffer->impl.myStride; } bool iron_gpu_transpose_mat = false; -void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) { +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { buffer->impl.mySize = size; buffer->data = NULL; @@ -2232,15 +2230,15 @@ void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) buffer->impl.constant_buffer->lpVtbl->Unmap(buffer->impl.constant_buffer, 0, NULL); } -void iron_gpu_constant_buffer_destroy(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { buffer->impl.constant_buffer->lpVtbl->Release(buffer->impl.constant_buffer); } -void iron_gpu_constant_buffer_lock_all(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); } -void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start, int count) { +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.lastStart = start; buffer->impl.lastCount = count; D3D12_RANGE range; @@ -2251,7 +2249,7 @@ void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start buffer->data = &p[start]; } -void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { D3D12_RANGE range; range.Begin = buffer->impl.lastStart; range.End = range.Begin + buffer->impl.lastCount; @@ -2259,11 +2257,11 @@ void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer) { buffer->data = NULL; } -int iron_gpu_constant_buffer_size(iron_gpu_constant_buffer_t *buffer) { +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { return buffer->impl.mySize; } -void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int count, bool gpuMemory) { +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int count, bool gpuMemory) { buffer->impl.count = count; buffer->impl.gpu_memory = gpuMemory; @@ -2317,7 +2315,7 @@ void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int count, bool buffer->impl.last_count = iron_gpu_index_buffer_count(buffer); } -void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { if (buffer->impl.index_buffer != NULL) { buffer->impl.index_buffer->lpVtbl->Release(buffer->impl.index_buffer); buffer->impl.index_buffer = NULL; @@ -2327,15 +2325,15 @@ void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer) { buffer->impl.upload_buffer = NULL; } -static int iron_gpu_internal_index_buffer_stride(iron_gpu_index_buffer_t *buffer) { +static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_index_buffer_t *buffer) { +void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); } -void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.last_start = start; buffer->impl.last_count = count; @@ -2350,7 +2348,7 @@ void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int return bytes; } -void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { D3D12_RANGE range; range.Begin = buffer->impl.last_start * iron_gpu_internal_index_buffer_stride(buffer); range.End = (buffer->impl.last_start + buffer->impl.last_count) * iron_gpu_internal_index_buffer_stride(buffer); @@ -2358,7 +2356,7 @@ void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { buffer->impl.upload_buffer->lpVtbl->Unmap(buffer->impl.upload_buffer, 0, &range); } -void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { D3D12_RANGE range; range.Begin = buffer->impl.last_start * iron_gpu_internal_index_buffer_stride(buffer); range.End = (buffer->impl.last_start + count) * iron_gpu_internal_index_buffer_stride(buffer); @@ -2366,7 +2364,7 @@ void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { buffer->impl.upload_buffer->lpVtbl->Unmap(buffer->impl.upload_buffer, 0, &range); } -void iron_gpu_internal_index_buffer_upload(iron_gpu_index_buffer_t *buffer, ID3D12GraphicsCommandList *commandList) { +void iron_gpu_internal_index_buffer_upload(iron_gpu_buffer_t *buffer, ID3D12GraphicsCommandList *commandList) { if (!buffer->impl.gpu_memory) return; @@ -2383,7 +2381,7 @@ void iron_gpu_internal_index_buffer_upload(iron_gpu_index_buffer_t *buffer, ID3D commandList->lpVtbl->ResourceBarrier(commandList, 1, barriers); } -int iron_gpu_index_buffer_count(iron_gpu_index_buffer_t *buffer) { +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } @@ -2418,16 +2416,16 @@ static D3D12_GPU_DESCRIPTOR_HANDLE texrankgpuDescriptorHandle; static int descriptorsAllocated = 0; static UINT descriptorSize; -static iron_gpu_vertex_buffer_t *vb[16]; -static iron_gpu_vertex_buffer_t *vb_last[16]; -static iron_gpu_index_buffer_t *ib[16]; +static iron_gpu_buffer_t *vb[16]; +static iron_gpu_buffer_t *vb_last[16]; +static iron_gpu_buffer_t *ib[16]; static int vb_count = 0; static int vb_count_last = 0; static inst_t instances[1024]; static int instances_count = 0; void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iron_gpu_command_list_t *command_list, void *ray_shader, int ray_shader_size, - iron_gpu_constant_buffer_t *constant_buffer) { + iron_gpu_buffer_t *constant_buffer) { output = NULL; descriptorsAllocated = 0; pipeline->_constant_buffer = constant_buffer; @@ -2750,7 +2748,7 @@ void iron_gpu_raytrace_pipeline_destroy(iron_gpu_raytrace_pipeline_t *pipeline) pipeline->impl.hitgroup_shader_table->lpVtbl->Release(pipeline->impl.hitgroup_shader_table); } -UINT create_srv_vb(iron_gpu_vertex_buffer_t *vb, UINT numElements, UINT elementSize) { +UINT create_srv_vb(iron_gpu_buffer_t *vb, UINT numElements, UINT elementSize) { D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; @@ -2784,7 +2782,7 @@ UINT create_srv_vb(iron_gpu_vertex_buffer_t *vb, UINT numElements, UINT elementS return descriptorIndex; } -UINT create_srv_ib(iron_gpu_index_buffer_t *ib, UINT numElements, UINT elementSize) { +UINT create_srv_ib(iron_gpu_buffer_t *ib, UINT numElements, UINT elementSize) { D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; @@ -2822,7 +2820,7 @@ void iron_gpu_raytrace_acceleration_structure_init(iron_gpu_raytrace_acceleratio instances_count = 0; } -void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_vertex_buffer_t *_vb, iron_gpu_index_buffer_t *_ib, +void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_buffer_t *_vb, iron_gpu_buffer_t *_ib, iron_matrix4x4_t _transform) { int vb_i = -1; @@ -2855,7 +2853,7 @@ void _iron_gpu_raytrace_acceleration_structure_destroy_top(iron_gpu_raytrace_acc } void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_command_list_t *command_list, - iron_gpu_vertex_buffer_t *_vb_full, iron_gpu_index_buffer_t *_ib_full) { + iron_gpu_buffer_t *_vb_full, iron_gpu_buffer_t *_ib_full) { bool build_bottom = false; for (int i = 0; i < 16; ++i) { diff --git a/base/sources/backends/direct3d12_gpu.h b/base/sources/backends/direct3d12_gpu.h index 67a6cd5f..e2eb6640 100644 --- a/base/sources/backends/direct3d12_gpu.h +++ b/base/sources/backends/direct3d12_gpu.h @@ -182,23 +182,6 @@ struct D3D12VertexBufferView { unsigned int StrideInBytes; }; -typedef struct { - struct ID3D12Resource *uploadBuffer; - struct D3D12VertexBufferView view; - - int myCount; - int myStride; - int lastStart; - int lastCount; -} gpu_vertex_buffer_impl_t; - -typedef struct { - struct ID3D12Resource *constant_buffer; - int lastStart; - int lastCount; - int mySize; -} gpu_constant_buffer_impl_t; - struct D3D12IindexBufferView { __int64 BufferLocation; unsigned int SizeInBytes; @@ -206,6 +189,13 @@ struct D3D12IindexBufferView { }; typedef struct { + struct ID3D12Resource *uploadBuffer; + struct D3D12VertexBufferView view; + int myCount; + int myStride; + int lastStart; + int lastCount; + struct ID3D12Resource *index_buffer; struct D3D12IindexBufferView index_buffer_view; struct ID3D12Resource *upload_buffer; @@ -213,11 +203,15 @@ typedef struct { bool gpu_memory; int last_start; int last_count; -} gpu_index_buffer_impl_t; -struct iron_gpu_index_buffer; + struct ID3D12Resource *constant_buffer; + int mySize; -void iron_gpu_internal_index_buffer_upload(struct iron_gpu_index_buffer *buffer, struct ID3D12GraphicsCommandList *commandList); +} gpu_buffer_impl_t; + +struct iron_gpu_buffer; + +void iron_gpu_internal_index_buffer_upload(struct iron_gpu_buffer *buffer, struct ID3D12GraphicsCommandList *commandList); struct ID3D12StateObject; struct ID3D12Resource; diff --git a/base/sources/backends/metal_gpu.h b/base/sources/backends/metal_gpu.h index 065ac6ab..0ed80c3c 100644 --- a/base/sources/backends/metal_gpu.h +++ b/base/sources/backends/metal_gpu.h @@ -3,10 +3,10 @@ #include #include -struct iron_gpu_index_buffer; +struct iron_gpu_buffer; typedef struct { - struct iron_gpu_index_buffer *current_index_buffer; + struct iron_gpu_buffer *current_index_buffer; } gpu_command_list_impl_t; struct iron_gpu_shader; @@ -64,19 +64,13 @@ typedef struct { bool gpuMemory; int lastStart; int lastCount; -} gpu_vertex_buffer_impl_t; -typedef struct { - void *_buffer; - int lastStart; - int lastCount; - int mySize; -} gpu_constant_buffer_impl_t; - -typedef struct { void *metal_buffer; int count; bool gpu_memory; int last_start; int last_count; -} gpu_index_buffer_impl_t; + + void *_buffer; + int mySize; +} gpu_buffer_impl_t; diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index abfd3854..5c788fa3 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -41,7 +41,7 @@ bool iron_internal_current_render_target_has_depth(void); static iron_gpu_raytrace_acceleration_structure_t *accel; static iron_gpu_raytrace_pipeline_t *pipeline; static iron_gpu_texture_t *output = NULL; -static iron_gpu_constant_buffer_t *constant_buf; +static iron_gpu_buffer_t *constant_buf; typedef struct inst { iron_matrix4x4_t m; @@ -61,15 +61,15 @@ static iron_gpu_texture_t *_texsobol; static iron_gpu_texture_t *_texscramble; static iron_gpu_texture_t *_texrank; -static iron_gpu_vertex_buffer_t *vb[16]; -static iron_gpu_vertex_buffer_t *vb_last[16]; -static iron_gpu_index_buffer_t *ib[16]; +static iron_gpu_buffer_t *vb[16]; +static iron_gpu_buffer_t *vb_last[16]; +static iron_gpu_buffer_t *ib[16]; static int vb_count = 0; static int vb_count_last = 0; static inst_t instances[1024]; static int instances_count = 0; -iron_gpu_vertex_buffer_t *currentVertexBuffer = NULL; +iron_gpu_buffer_t *currentVertexBuffer = NULL; bool iron_gpu_transpose_mat = true; id getMetalEncoder(void) { @@ -96,9 +96,6 @@ void iron_gpu_internal_init_window(int depthBufferBits, bool vsync) { iron_gpu_render_target_init(&fallback_render_target, 32, 32, IRON_IMAGE_FORMAT_RGBA32, 0); } -void iron_gpu_flush(void) { -} - bool iron_internal_current_render_target_has_depth(void) { return iron_internal_metal_has_depth; } @@ -368,11 +365,11 @@ void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct ir lastPipeline = pipeline; } -void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) { +void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { iron_gpu_internal_vertex_buffer_set(buffer); } -void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer) { +void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { list->impl.current_index_buffer = buffer; } @@ -391,9 +388,9 @@ void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, str } } -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer) {} +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) {} +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture) {} @@ -459,13 +456,13 @@ void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t [commandBuffer waitUntilCompleted]; } -void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { id buf = (__bridge id)buffer->impl._buffer; id encoder = getMetalEncoder(); [encoder setVertexBuffer:buf offset:offset atIndex:1]; } -void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { id buf = (__bridge id)buffer->impl._buffer; id encoder = getMetalEncoder(); [encoder setFragmentBuffer:buf offset:offset atIndex:0]; @@ -953,7 +950,7 @@ bool iron_gpu_raytrace_supported() { } void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iron_gpu_command_list_t *command_list, void *ray_shader, int ray_shader_size, - iron_gpu_constant_buffer_t *constant_buffer) { + iron_gpu_buffer_t *constant_buffer) { id device = getMetalDevice(); if (!device.supportsRaytracing) return; constant_buf = constant_buffer; @@ -1012,7 +1009,7 @@ void iron_gpu_raytrace_acceleration_structure_init(iron_gpu_raytrace_acceleratio instances_count = 0; } -void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_vertex_buffer_t *_vb, iron_gpu_index_buffer_t *_ib, +void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_buffer_t *_vb, iron_gpu_buffer_t *_ib, iron_matrix4x4_t _transform) { int vb_i = -1; @@ -1045,7 +1042,7 @@ void _iron_gpu_raytrace_acceleration_structure_destroy_top(iron_gpu_raytrace_acc } void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_command_list_t *command_list, - iron_gpu_vertex_buffer_t *_vb_full, iron_gpu_index_buffer_t *_ib_full) { + iron_gpu_buffer_t *_vb_full, iron_gpu_buffer_t *_ib_full) { bool build_bottom = false; for (int i = 0; i < 16; ++i) { @@ -1432,12 +1429,12 @@ void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_ target->impl._depthTex = source->impl._depthTex; } -static void vertex_buffer_unset(iron_gpu_vertex_buffer_t *buffer) { +static void vertex_buffer_unset(iron_gpu_buffer_t *buffer) { if (currentVertexBuffer == buffer) currentVertexBuffer = NULL; } -void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { memset(&buffer->impl, 0, sizeof(buffer->impl)); buffer->impl.myCount = count; buffer->impl.gpuMemory = gpuMemory; @@ -1457,14 +1454,14 @@ void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, ir buffer->impl.lastCount = 0; } -void iron_gpu_vertex_buffer_destroy(iron_gpu_vertex_buffer_t *buf) { +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buf) { id buffer = (__bridge_transfer id)buf->impl.mtlBuffer; buffer = nil; buf->impl.mtlBuffer = NULL; vertex_buffer_unset(buf); } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buf) { +float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buf) { buf->impl.lastStart = 0; buf->impl.lastCount = iron_gpu_vertex_buffer_count(buf); id buffer = (__bridge id)buf->impl.mtlBuffer; @@ -1472,7 +1469,7 @@ float *iron_gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buf) { return floats; } -float *iron_gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buf, int start, int count) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buf, int start, int count) { buf->impl.lastStart = start; buf->impl.lastCount = count; id buffer = (__bridge id)buf->impl.mtlBuffer; @@ -1480,13 +1477,13 @@ float *iron_gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buf, int start, int return &floats[start * buf->impl.myStride / sizeof(float)]; } -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_vertex_buffer_t *buf) { +void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buf) { } -void iron_gpu_vertex_buffer_unlock(iron_gpu_vertex_buffer_t *buf, int count) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buf, int count) { } -int iron_gpu_internal_vertex_buffer_set(iron_gpu_vertex_buffer_t *buf) { +int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buf) { currentVertexBuffer = buf; id encoder = getMetalEncoder(); @@ -1496,31 +1493,31 @@ int iron_gpu_internal_vertex_buffer_set(iron_gpu_vertex_buffer_t *buf) { return 0; } -int iron_gpu_vertex_buffer_count(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.myCount; } -int iron_gpu_vertex_buffer_stride(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { return buffer->impl.myStride; } -void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) { +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { buffer->impl.mySize = size; buffer->data = NULL; buffer->impl._buffer = (__bridge_retained void *)[getMetalDevice() newBufferWithLength:size options:MTLResourceOptionCPUCacheModeDefault]; } -void iron_gpu_constant_buffer_destroy(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { id buf = (__bridge_transfer id)buffer->impl._buffer; buf = nil; buffer->impl._buffer = NULL; } -void iron_gpu_constant_buffer_lock_all(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); } -void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start, int count) { +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.lastStart = start; buffer->impl.lastCount = count; id buf = (__bridge id)buffer->impl._buffer; @@ -1528,15 +1525,15 @@ void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start buffer->data = &data[start]; } -void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { buffer->data = NULL; } -int iron_gpu_constant_buffer_size(iron_gpu_constant_buffer_t *buffer) { +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { return buffer->impl.mySize; } -void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int indexCount, bool gpuMemory) { +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int indexCount, bool gpuMemory) { buffer->impl.count = indexCount; buffer->impl.gpu_memory = gpuMemory; buffer->impl.last_start = 0; @@ -1551,21 +1548,21 @@ void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int indexCount, options:options]; } -void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { id buf = (__bridge_transfer id)buffer->impl.metal_buffer; buf = nil; buffer->impl.metal_buffer = NULL; } -static int iron_gpu_internal_index_buffer_stride(iron_gpu_index_buffer_t *buffer) { +static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_index_buffer_t *buffer) { +void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); } -void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.last_start = start; buffer->impl.last_count = count; @@ -1574,13 +1571,13 @@ void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int return &data[start * iron_gpu_internal_index_buffer_stride(buffer)]; } -void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { iron_gpu_index_buffer_unlock(buffer, buffer->impl.last_count); } -void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { } -int iron_gpu_index_buffer_count(iron_gpu_index_buffer_t *buffer) { +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index e612cce7..522f7997 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -1187,10 +1187,6 @@ void iron_gpu_end() { began = false; } -void iron_gpu_flush() { - vkDeviceWaitIdle(vk_ctx.device); -} - bool iron_vulkan_internal_get_size(int *width, int *height) { // this is exclusively used by the Android backend at the moment if (vk_ctx.windows[0].surface) { @@ -1308,7 +1304,7 @@ void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list) { } void iron_gpu_command_list_begin(iron_gpu_command_list_t *list) { - vkWaitForFences(vk_ctx.device, 1, &list->impl.fence, VK_TRUE, UINT64_MAX); + iron_gpu_command_list_wait_for_execution_to_finish(list); vkResetCommandBuffer(list->impl._buffer, 0); VkCommandBufferBeginInfo cmd_buf_info = { @@ -1501,15 +1497,15 @@ void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct ir } } -void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, gpu_vertex_buffer_impl_t *vertexBuffer) { +void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *vertexBuffer) { VkBuffer buffers[1]; VkDeviceSize offsets[1]; - buffers[0] = vertexBuffer->buf; + buffers[0] = vertexBuffer->impl.buf; offsets[0] = (VkDeviceSize)(0); vkCmdBindVertexBuffers(list->impl._buffer, 0, 1, buffers, offsets); } -void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *indexBuffer) { +void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *indexBuffer) { list->impl._indexCount = iron_gpu_index_buffer_count(indexBuffer); vkCmdBindIndexBuffer(list->impl._buffer, indexBuffer->impl.buf, 0, VK_INDEX_TYPE_UINT32); } @@ -1758,9 +1754,9 @@ void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, str } } -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer) {} +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) {} +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture) {} @@ -1844,7 +1840,7 @@ void iron_gpu_command_list_render_target_to_texture_barrier(iron_gpu_command_lis // render-passes are used to transition render-targets } -void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { last_vertex_constant_buffer_offset = offset; } @@ -2021,7 +2017,7 @@ VkDescriptorSet get_descriptor_set() { return descriptor_set; } -void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { last_fragment_constant_buffer_offset = offset; VkDescriptorSet descriptor_set = get_descriptor_set(); @@ -2032,7 +2028,7 @@ void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t void iron_gpu_command_list_execute(iron_gpu_command_list_t *list) { // Make sure the previous execution is done, so we can reuse the fence // Not optimal of course - vkWaitForFences(vk_ctx.device, 1, &list->impl.fence, VK_TRUE, UINT64_MAX); + iron_gpu_command_list_wait_for_execution_to_finish(list); vkResetFences(vk_ctx.device, 1, &list->impl.fence); VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; @@ -2600,15 +2596,18 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { VkPipelineShaderStageCreateInfo shaderStages[2]; memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); + char *vs_main = "main"; + char *fs_main = "main"; + shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; shaderStages[0].module = prepare_vs(&pipeline->impl.vert_shader_module, pipeline->vertex_shader); - shaderStages[0].pName = "main"; + shaderStages[0].pName = vs_main; shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; shaderStages[1].module = prepare_fs(&pipeline->impl.frag_shader_module, pipeline->fragment_shader); - shaderStages[1].pName = "main"; + shaderStages[1].pName = fs_main; pipeline_info.pVertexInputState = &vi; pipeline_info.pInputAssemblyState = &ia; @@ -3390,18 +3389,18 @@ void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_ } } -void iron_gpu_vertex_buffer_init(gpu_vertex_buffer_impl_t *buffer, int vertexCount, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { - buffer->myCount = vertexCount; - buffer->myStride = 0; +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int vertexCount, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { + buffer->impl.myCount = vertexCount; + buffer->impl.myStride = 0; for (int i = 0; i < structure->size; ++i) { iron_gpu_vertex_element_t element = structure->elements[i]; - buffer->myStride += iron_gpu_vertex_data_size(element.data); + buffer->impl.myStride += iron_gpu_vertex_data_size(element.data); } VkBufferCreateInfo buf_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = NULL, - .size = vertexCount * buffer->myStride, + .size = vertexCount * buffer->impl.myStride, .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, .flags = 0, }; @@ -3412,75 +3411,78 @@ void iron_gpu_vertex_buffer_init(gpu_vertex_buffer_impl_t *buffer, int vertexCou buf_info.usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR; } - memset(&buffer->mem_alloc, 0, sizeof(VkMemoryAllocateInfo)); - buffer->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - buffer->mem_alloc.pNext = NULL; - buffer->mem_alloc.allocationSize = 0; - buffer->mem_alloc.memoryTypeIndex = 0; + memset(&buffer->impl.mem_alloc, 0, sizeof(VkMemoryAllocateInfo)); + buffer->impl.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + buffer->impl.mem_alloc.pNext = NULL; + buffer->impl.mem_alloc.allocationSize = 0; + buffer->impl.mem_alloc.memoryTypeIndex = 0; VkMemoryRequirements mem_reqs = {0}; - buffer->buf = NULL; - buffer->mem = NULL; + buffer->impl.buf = NULL; + buffer->impl.mem = NULL; - vkCreateBuffer(vk_ctx.device, &buf_info, NULL, &buffer->buf); + vkCreateBuffer(vk_ctx.device, &buf_info, NULL, &buffer->impl.buf); - vkGetBufferMemoryRequirements(vk_ctx.device, buffer->buf, &mem_reqs); + vkGetBufferMemoryRequirements(vk_ctx.device, buffer->impl.buf, &mem_reqs); - buffer->mem_alloc.allocationSize = mem_reqs.size; - memory_type_from_properties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &buffer->mem_alloc.memoryTypeIndex); + buffer->impl.mem_alloc.allocationSize = mem_reqs.size; + memory_type_from_properties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &buffer->impl.mem_alloc.memoryTypeIndex); VkMemoryAllocateFlagsInfo memory_allocate_flags_info = {0}; if (iron_gpu_raytrace_supported()) { memory_allocate_flags_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO; memory_allocate_flags_info.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR; - buffer->mem_alloc.pNext = &memory_allocate_flags_info; + buffer->impl.mem_alloc.pNext = &memory_allocate_flags_info; } - vkAllocateMemory(vk_ctx.device, &buffer->mem_alloc, NULL, &buffer->mem); - vkBindBufferMemory(vk_ctx.device, buffer->buf, buffer->mem, 0); + vkAllocateMemory(vk_ctx.device, &buffer->impl.mem_alloc, NULL, &buffer->impl.mem); + vkBindBufferMemory(vk_ctx.device, buffer->impl.buf, buffer->impl.mem, 0); } -static void unset_vertex_buffer(gpu_vertex_buffer_impl_t *buffer) { +static void unset_vertex_buffer(iron_gpu_buffer_t *buffer) { } -void iron_gpu_vertex_buffer_destroy(gpu_vertex_buffer_impl_t *buffer) { +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { unset_vertex_buffer(buffer); - vkFreeMemory(vk_ctx.device, buffer->mem, NULL); - vkDestroyBuffer(vk_ctx.device, buffer->buf, NULL); + vkFreeMemory(vk_ctx.device, buffer->impl.mem, NULL); + vkDestroyBuffer(vk_ctx.device, buffer->impl.buf, NULL); } -float *iron_gpu_vertex_buffer_lock_all(gpu_vertex_buffer_impl_t *buffer) { - return iron_gpu_vertex_buffer_lock(buffer, 0, buffer->myCount); +float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { + return iron_gpu_vertex_buffer_lock(buffer, 0, buffer->impl.myCount); } -float *iron_gpu_vertex_buffer_lock(gpu_vertex_buffer_impl_t *buffer, int start, int count) { - vkMapMemory(vk_ctx.device, buffer->mem, start * buffer->myStride, count * buffer->myStride, 0, (void **)&buffer->data); - return buffer->data; +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { + vkMapMemory(vk_ctx.device, buffer->impl.mem, start * buffer->impl.myStride, count * buffer->impl.myStride, 0, (void **)&buffer->impl.data); + return buffer->impl.data; } -void iron_gpu_vertex_buffer_unlock_all(gpu_vertex_buffer_impl_t *buffer) { - vkUnmapMemory(vk_ctx.device, buffer->mem); +void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { + vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } -void iron_gpu_vertex_buffer_unlock(gpu_vertex_buffer_impl_t *buffer, int count) { - vkUnmapMemory(vk_ctx.device, buffer->mem); +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { + vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } -int iron_gpu_internal_vertex_buffer_set(gpu_vertex_buffer_impl_t *buffer) { +int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buffer) { return 0; } -int iron_gpu_vertex_buffer_count(gpu_vertex_buffer_impl_t *buffer) { - return buffer->myCount; +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { + return buffer->impl.myCount; } -int iron_gpu_vertex_buffer_stride(gpu_vertex_buffer_impl_t *buffer) { - return buffer->myStride; +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { + return buffer->impl.myStride; } -static void create_uniform_buffer(VkBuffer *buf, VkMemoryAllocateInfo *mem_alloc, VkDeviceMemory *mem, VkDescriptorBufferInfo *buffer_info, int size) { +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { + buffer->impl.mySize = size; + buffer->data = NULL; + VkBufferCreateInfo buf_info; memset(&buf_info, 0, sizeof(buf_info)); buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; @@ -3491,30 +3493,19 @@ static void create_uniform_buffer(VkBuffer *buf, VkMemoryAllocateInfo *mem_alloc } buf_info.size = size; - vkCreateBuffer(vk_ctx.device, &buf_info, NULL, buf); + vkCreateBuffer(vk_ctx.device, &buf_info, NULL, &buffer->impl.buf); VkMemoryRequirements mem_reqs; - vkGetBufferMemoryRequirements(vk_ctx.device, *buf, &mem_reqs); + vkGetBufferMemoryRequirements(vk_ctx.device, buffer->impl.buf, &mem_reqs); - mem_alloc->sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - mem_alloc->pNext = NULL; - mem_alloc->allocationSize = mem_reqs.size; - mem_alloc->memoryTypeIndex = 0; + buffer->impl.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + buffer->impl.mem_alloc.pNext = NULL; + buffer->impl.mem_alloc.allocationSize = mem_reqs.size; + buffer->impl.mem_alloc.memoryTypeIndex = 0; - memory_type_from_properties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &mem_alloc->memoryTypeIndex); - vkAllocateMemory(vk_ctx.device, mem_alloc, NULL, mem); - vkBindBufferMemory(vk_ctx.device, *buf, *mem, 0); - - buffer_info->buffer = *buf; - buffer_info->offset = 0; - buffer_info->range = size; -} - -void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) { - buffer->impl.mySize = size; - buffer->data = NULL; - - create_uniform_buffer(&buffer->impl.buf, &buffer->impl.mem_alloc, &buffer->impl.mem, &buffer->impl.buffer_info, size); + memory_type_from_properties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &buffer->impl.mem_alloc.memoryTypeIndex); + vkAllocateMemory(vk_ctx.device, &buffer->impl.mem_alloc, NULL, &buffer->impl.mem); + vkBindBufferMemory(vk_ctx.device, buffer->impl.buf, buffer->impl.mem, 0); // buffer hack if (vk_ctx.vertex_uniform_buffer == NULL) { @@ -3530,30 +3521,30 @@ void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } -void iron_gpu_constant_buffer_destroy(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { vkFreeMemory(vk_ctx.device, buffer->impl.mem, NULL); vkDestroyBuffer(vk_ctx.device, buffer->impl.buf, NULL); } -void iron_gpu_constant_buffer_lock_all(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); } -void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start, int count) { +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { vkMapMemory(vk_ctx.device, buffer->impl.mem, start, count, 0, (void **)&buffer->data); } -void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { vkUnmapMemory(vk_ctx.device, buffer->impl.mem); buffer->data = NULL; } -int iron_gpu_constant_buffer_size(iron_gpu_constant_buffer_t *buffer) { +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { return buffer->impl.mySize; } -void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int indexCount, bool gpuMemory) { - buffer->impl.count = indexCount; +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int indexCount, bool gpuMemory) { + buffer->impl.myCount = indexCount; VkBufferCreateInfo buf_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, @@ -3597,36 +3588,36 @@ void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int indexCount, vkBindBufferMemory(vk_ctx.device, buffer->impl.buf, buffer->impl.mem, 0); } -void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { // unset(buffer); vkFreeMemory(vk_ctx.device, buffer->impl.mem, NULL); vkDestroyBuffer(vk_ctx.device, buffer->impl.buf, NULL); } -static int iron_gpu_internal_index_buffer_stride(iron_gpu_index_buffer_t *buffer) { +static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_index_buffer_t *buffer) { +void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); } -void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { uint8_t *data; vkMapMemory(vk_ctx.device, buffer->impl.mem, 0, buffer->impl.mem_alloc.allocationSize, 0, (void **)&data); return &data[start * iron_gpu_internal_index_buffer_stride(buffer)]; } -void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } -void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { iron_gpu_index_buffer_unlock_all(buffer); } -int iron_gpu_index_buffer_count(iron_gpu_index_buffer_t *buffer) { - return buffer->impl.count; +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { + return buffer->impl.myCount; } static const int INDEX_RAYGEN = 0; @@ -3647,9 +3638,9 @@ static iron_gpu_texture_t *texenv; static iron_gpu_texture_t *texsobol; static iron_gpu_texture_t *texscramble; static iron_gpu_texture_t *texrank; -static gpu_vertex_buffer_impl_t *vb[16]; -static gpu_vertex_buffer_impl_t *vb_last[16]; -static iron_gpu_index_buffer_t *ib[16]; +static iron_gpu_buffer_t *vb[16]; +static iron_gpu_buffer_t *vb_last[16]; +static iron_gpu_buffer_t *ib[16]; static int vb_count = 0; static int vb_count_last = 0; static inst_t instances[1024]; @@ -3670,7 +3661,7 @@ static PFN_vkDestroyAccelerationStructureKHR _vkDestroyAccelerationStructureKHR static PFN_vkCmdTraceRaysKHR _vkCmdTraceRaysKHR = NULL; void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iron_gpu_command_list_t *command_list, void *ray_shader, int ray_shader_size, - iron_gpu_constant_buffer_t *constant_buffer) { + struct iron_gpu_buffer *constant_buffer) { output = NULL; pipeline->_constant_buffer = constant_buffer; @@ -4031,7 +4022,7 @@ void iron_gpu_raytrace_acceleration_structure_init(iron_gpu_raytrace_acceleratio instances_count = 0; } -void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_vertex_buffer *_vb, iron_gpu_index_buffer_t *_ib, +void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_buffer *_vb, struct iron_gpu_buffer *_ib, iron_matrix4x4_t _transform) { int vb_i = -1; @@ -4072,7 +4063,7 @@ void _iron_gpu_raytrace_acceleration_structure_destroy_top(iron_gpu_raytrace_acc } void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_acceleration_structure_t *accel, iron_gpu_command_list_t *command_list, - struct iron_gpu_vertex_buffer *_vb_full, iron_gpu_index_buffer_t *_ib_full) { + struct iron_gpu_buffer *_vb_full, struct iron_gpu_buffer *_ib_full) { bool build_bottom = false; for (int i = 0; i < 16; ++i) { @@ -4099,13 +4090,13 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati if (build_bottom) { for (int i = 0; i < vb_count; ++i) { - uint32_t prim_count = ib[i]->impl.count / 3; - uint32_t vert_count = vb[i]->myCount; + uint32_t prim_count = ib[i]->impl.myCount / 3; + uint32_t vert_count = vb[i]->impl.myCount; VkDeviceOrHostAddressConstKHR vertex_data_device_address = {0}; VkDeviceOrHostAddressConstKHR index_data_device_address = {0}; - vertex_data_device_address.deviceAddress = get_buffer_device_address(vb[i]->buf); + vertex_data_device_address.deviceAddress = get_buffer_device_address(vb[i]->impl.buf); index_data_device_address.deviceAddress = get_buffer_device_address(ib[i]->impl.buf); VkAccelerationStructureGeometryKHR acceleration_geometry = { @@ -4115,8 +4106,8 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati .geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR, .geometry.triangles.vertexFormat = VK_FORMAT_R16G16B16A16_SNORM, .geometry.triangles.vertexData.deviceAddress = vertex_data_device_address.deviceAddress, - .geometry.triangles.vertexStride = vb[i]->myStride, - .geometry.triangles.maxVertex = vb[i]->myCount, + .geometry.triangles.vertexStride = vb[i]->impl.myStride, + .geometry.triangles.maxVertex = vb[i]->impl.myCount, .geometry.triangles.indexType = VK_INDEX_TYPE_UINT32, .geometry.triangles.indexData.deviceAddress = index_data_device_address.deviceAddress, }; @@ -4337,7 +4328,7 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati int ib_off = 0; for (int j = 0; j < instances[i].i; ++j) { - ib_off += ib[j]->impl.count * 4; + ib_off += ib[j]->impl.myCount * 4; } instance.instanceCustomIndex = ib_off; @@ -4569,13 +4560,13 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati #ifdef is_forge - vb_full = _vb_full->buf; - vb_full_mem = _vb_full->mem; + vb_full = _vb_full->impl.buf; + vb_full_mem = _vb_full->impl.mem; #else - vb_full = vb[0]->buf; - vb_full_mem = vb[0]->mem; + vb_full = vb[0]->impl.buf; + vb_full_mem = vb[0]->impl.mem; #endif } diff --git a/base/sources/backends/vulkan_gpu.h b/base/sources/backends/vulkan_gpu.h index 89a20fab..68347056 100644 --- a/base/sources/backends/vulkan_gpu.h +++ b/base/sources/backends/vulkan_gpu.h @@ -179,32 +179,20 @@ typedef struct { } gpu_texture_impl_t; typedef struct { + int myCount; + VkBuffer buf; - VkDescriptorBufferInfo buffer_info; - VkMemoryAllocateInfo mem_alloc; VkDeviceMemory mem; + VkMemoryAllocateInfo mem_alloc; + + float *data; + int myStride; + unsigned bufferId; + int lastStart; int lastCount; int mySize; -} gpu_constant_buffer_impl_t; - -typedef struct { - float *data; - int myCount; - int myStride; - unsigned bufferId; - VkMemoryAllocateInfo mem_alloc; - - VkBuffer buf; - VkDeviceMemory mem; -} gpu_vertex_buffer_impl_t; - -typedef struct { - int count; - VkBuffer buf; - VkDeviceMemory mem; - VkMemoryAllocateInfo mem_alloc; -} gpu_index_buffer_impl_t; +} gpu_buffer_impl_t; typedef struct { VkPipeline pipeline; diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index fda26e5a..60793ac8 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -59,17 +59,15 @@ bool iron_gpu_swap_buffers() { return true; } -void iron_gpu_flush() {} - bool iron_gpu_raytrace_supported() { return false; } extern WGPUDevice device; -iron_gpu_vertex_buffer_t *iron_gpu_internal_current_vertex_buffer = NULL; +iron_gpu_buffer_t *iron_gpu_internal_current_vertex_buffer = NULL; -void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { buffer->impl.count = count; buffer->impl.stride = 0; for (int i = 0; i < structure->size; ++i) { @@ -77,11 +75,11 @@ void iron_gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, ir } } -void iron_gpu_vertex_buffer_destroy(iron_gpu_vertex_buffer_t *buffer) { +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buffer) { +float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); bDesc.size = buffer->impl.count * buffer->impl.stride * sizeof(float); @@ -91,67 +89,67 @@ float *iron_gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buffer) { return wgpuBufferGetMappedRange(buffer->impl.buffer, 0, bDesc.size); } -float *iron_gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buffer, int start, int count) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { return NULL; } -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_vertex_buffer_t *buffer) { +void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -void iron_gpu_vertex_buffer_unlock(iron_gpu_vertex_buffer_t* buffer, int count) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t* buffer, int count) { } -int iron_gpu_vertex_buffer_count(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } -int iron_gpu_vertex_buffer_stride(iron_gpu_vertex_buffer_t *buffer) { +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { return buffer->impl.stride; } bool iron_gpu_transpose_mat = false; -void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size) { +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { } -void iron_gpu_constant_buffer_destroy(iron_gpu_constant_buffer_t *buffer) {} +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) {} -void iron_gpu_constant_buffer_lock_all(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); } -void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start, int count) {} +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) {} -void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer) { +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { } -int iron_gpu_constant_buffer_size(iron_gpu_constant_buffer_t *buffer) { +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { return 0; } -iron_gpu_index_buffer_t *iron_gpu_internal_current_index_buffer = NULL; +iron_gpu_buffer_t *iron_gpu_internal_current_index_buffer = NULL; -void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int count, bool gpuMemory) { +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int count, bool gpuMemory) { buffer->impl.count = count; } -void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { } -static int iron_gpu_internal_index_buffer_stride(iron_gpu_index_buffer_t *buffer) { +static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_index_buffer_t *buffer) { +void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); } -void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); bDesc.size = count * iron_gpu_internal_index_buffer_stride(buffer); @@ -161,19 +159,19 @@ void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int return wgpuBufferGetMappedRange(buffer->impl.buffer, start * iron_gpu_internal_index_buffer_stride(buffer), bDesc.size); } -void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { iron_gpu_index_buffer_unlock_all(buffer); } -void iron_gpu_internal_index_buffer_set(iron_gpu_index_buffer_t *buffer) { +void iron_gpu_internal_index_buffer_set(iron_gpu_buffer_t *buffer) { } -int iron_gpu_index_buffer_count(iron_gpu_index_buffer_t *buffer) { +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } @@ -443,12 +441,12 @@ void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct ir void iron_gpu_command_list_set_pipeline_layout(iron_gpu_command_list_t *list) {} -void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) { +void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { uint64_t size = (iron_gpu_vertex_buffer_count(buffer)) * iron_gpu_vertex_buffer_stride(buffer); wgpuRenderPassEncoderSetVertexBuffer(list->impl.pass, 0, buffer->impl.buffer, 0, size); } -void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer) { +void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { list->impl.indexCount = iron_gpu_index_buffer_count(buffer); uint64_t size = iron_gpu_index_buffer_count(buffer) * sizeof(int); wgpuRenderPassEncoderSetIndexBuffer(list->impl.pass, buffer->impl.buffer, WGPUIndexFormat_Uint32, 0, size); @@ -458,8 +456,8 @@ void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, str } -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer) {} -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer) {} +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture) {} void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *list, iron_gpu_texture_t *render_target, uint8_t *data) {} @@ -471,11 +469,11 @@ void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t } -void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { } -void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { } diff --git a/base/sources/backends/webgpu_gpu.h b/base/sources/backends/webgpu_gpu.h index ca731949..02c67bcc 100644 --- a/base/sources/backends/webgpu_gpu.h +++ b/base/sources/backends/webgpu_gpu.h @@ -8,16 +8,7 @@ typedef struct { WGPUBuffer buffer; int count; int stride; -} gpu_vertex_buffer_impl_t; - -typedef struct { - WGPUBuffer buffer; -} gpu_constant_buffer_impl_t; - -typedef struct { - WGPUBuffer buffer; - int count; -} gpu_index_buffer_impl_t; +} gpu_buffer_impl_t; typedef struct { WGPUTexture texture; diff --git a/base/sources/iron.h b/base/sources/iron.h index 3a73f78b..f6a594e9 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -379,7 +379,7 @@ int krafix_compile(const char *source, char *output, int *length, const char *ta #endif extern iron_gpu_command_list_t commandList; -static iron_gpu_constant_buffer_t constant_buffer; +static iron_gpu_buffer_t constant_buffer; static iron_gpu_texture_t *render_target; static iron_gpu_raytrace_pipeline_t rt_pipeline; static iron_gpu_raytrace_acceleration_structure_t accel; @@ -924,17 +924,17 @@ void iron_show_keyboard(bool show) { } any gpu_create_index_buffer(i32 count) { - iron_gpu_index_buffer_t *buffer = (iron_gpu_index_buffer_t *)malloc(sizeof(iron_gpu_index_buffer_t)); + iron_gpu_buffer_t *buffer = (iron_gpu_buffer_t *)malloc(sizeof(iron_gpu_buffer_t)); iron_gpu_index_buffer_init(buffer, count, GPU_USAGE_STATIC); return buffer; } -void gpu_delete_index_buffer(iron_gpu_index_buffer_t *buffer) { +void gpu_delete_index_buffer(iron_gpu_buffer_t *buffer) { iron_gpu_index_buffer_destroy(buffer); free(buffer); } -u32_array_t *gpu_lock_index_buffer(iron_gpu_index_buffer_t *buffer) { +u32_array_t *gpu_lock_index_buffer(iron_gpu_buffer_t *buffer) { void *vertices = iron_gpu_index_buffer_lock_all(buffer); u32_array_t *ar = (u32_array_t *)malloc(sizeof(u32_array_t)); ar->buffer = vertices; @@ -943,21 +943,21 @@ u32_array_t *gpu_lock_index_buffer(iron_gpu_index_buffer_t *buffer) { } any gpu_create_vertex_buffer(i32 count, iron_gpu_vertex_structure_t *structure, i32 usage) { - iron_gpu_vertex_buffer_t *buffer = (iron_gpu_vertex_buffer_t *)malloc(sizeof(iron_gpu_vertex_buffer_t)); + iron_gpu_buffer_t *buffer = (iron_gpu_buffer_t *)malloc(sizeof(iron_gpu_buffer_t)); gpu_vertex_buffer_init(buffer, count, structure, (gpu_usage_t)usage); return buffer; } -void gpu_delete_vertex_buffer(iron_gpu_vertex_buffer_t *buffer) { - gpu_vertex_buffer_destroy(buffer); +void gpu_delete_vertex_buffer(iron_gpu_buffer_t *buffer) { + iron_gpu_vertex_buffer_destroy(buffer); free(buffer); } -buffer_t *gpu_lock_vertex_buffer(iron_gpu_vertex_buffer_t *buffer) { +buffer_t *gpu_lock_vertex_buffer(iron_gpu_buffer_t *buffer) { float *vertices = gpu_vertex_buffer_lock_all(buffer); buffer_t *b = (buffer_t *)malloc(sizeof(buffer_t)); b->buffer = vertices; - b->length = gpu_vertex_buffer_count(buffer) * gpu_vertex_buffer_stride(buffer); + b->length = buffer->myCount * iron_gpu_vertex_buffer_stride(buffer); return b; } @@ -1295,11 +1295,11 @@ iron_gpu_texture_unit_t *gpu_get_texture_unit(iron_gpu_pipeline_t *pipeline, str } void _gpu_set_texture(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *texture) { - gpu_set_texture(*unit, texture); + gpu_set_texture(unit, texture); } -void gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { - gpu_render_target_use_depth_as_texture(render_target, *unit); +void _gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { + gpu_set_texture_depth(unit, render_target); } void gpu_set_texture_parameters(iron_gpu_texture_unit_t *unit, i32 u_addr, i32 v_addr, i32 min_filter, i32 mag_filter, i32 mip_filter) { @@ -2252,11 +2252,11 @@ void iron_raytrace_as_init() { iron_gpu_raytrace_acceleration_structure_init(&accel); } -void iron_raytrace_as_add(struct iron_gpu_vertex_buffer *vb, iron_gpu_index_buffer_t *ib, iron_matrix4x4_t transform) { +void iron_raytrace_as_add(struct iron_gpu_buffer *vb, iron_gpu_buffer_t *ib, iron_matrix4x4_t transform) { iron_gpu_raytrace_acceleration_structure_add(&accel, vb, ib, transform); } -void iron_raytrace_as_build(struct iron_gpu_vertex_buffer *vb_full, iron_gpu_index_buffer_t *ib_full) { +void iron_raytrace_as_build(struct iron_gpu_buffer *vb_full, iron_gpu_buffer_t *ib_full) { iron_gpu_raytrace_acceleration_structure_build(&accel, &commandList, vb_full, ib_full); } diff --git a/base/sources/iron_draw.c b/base/sources/iron_draw.c index 66fee402..ad12cec7 100644 --- a/base/sources/iron_draw.c +++ b/base/sources/iron_draw.c @@ -29,8 +29,8 @@ static iron_gpu_pipeline_t *draw_custom_pipeline = NULL; static iron_matrix3x3_t draw_transform; static bool _draw_thrown = false; -static iron_gpu_vertex_buffer_t image_vertex_buffer; -static iron_gpu_index_buffer_t image_index_buffer; +static iron_gpu_buffer_t image_vertex_buffer; +static iron_gpu_buffer_t image_index_buffer; static iron_gpu_shader_t image_vert_shader; static iron_gpu_shader_t image_frag_shader; static iron_gpu_pipeline_t image_pipeline; @@ -41,10 +41,10 @@ static int image_buffer_index = 0; static int image_buffer_start = 0; static iron_gpu_texture_t *image_last_texture = NULL; -static iron_gpu_vertex_buffer_t colored_rect_vertex_buffer; -static iron_gpu_index_buffer_t colored_rect_index_buffer; -static iron_gpu_vertex_buffer_t colored_tris_vertex_buffer; -static iron_gpu_index_buffer_t colored_tris_index_buffer; +static iron_gpu_buffer_t colored_rect_vertex_buffer; +static iron_gpu_buffer_t colored_rect_index_buffer; +static iron_gpu_buffer_t colored_tris_vertex_buffer; +static iron_gpu_buffer_t colored_tris_index_buffer; static iron_gpu_shader_t colored_vert_shader; static iron_gpu_shader_t colored_frag_shader; static iron_gpu_pipeline_t colored_pipeline; @@ -56,8 +56,8 @@ static int colored_rect_buffer_start = 0; static int colored_tris_buffer_index = 0; static int colored_tris_buffer_start = 0; -static iron_gpu_vertex_buffer_t text_vertex_buffer; -static iron_gpu_index_buffer_t text_index_buffer; +static iron_gpu_buffer_t text_vertex_buffer; +static iron_gpu_buffer_t text_index_buffer; static iron_gpu_shader_t text_vert_shader; static iron_gpu_shader_t text_frag_shader; static iron_gpu_pipeline_t text_pipeline; @@ -340,12 +340,12 @@ void draw_set_image_rect_colors(uint32_t color) { void draw_image_buffer(bool end) { if (image_buffer_index - image_buffer_start == 0) return; - gpu_vertex_buffer_unlock(&image_vertex_buffer, (image_buffer_index - image_buffer_start) * 4); + iron_gpu_vertex_buffer_unlock(&image_vertex_buffer, (image_buffer_index - image_buffer_start) * 4); iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &image_pipeline); gpu_set_matrix4(image_proj_loc, &draw_projection_matrix); gpu_set_vertex_buffer(&image_vertex_buffer); gpu_set_index_buffer(&image_index_buffer); - gpu_set_texture(image_tex_unit, image_last_texture); + gpu_set_texture(&image_tex_unit, image_last_texture); gpu_set_texture_addressing(image_tex_unit, GPU_TEXTURE_DIRECTION_U, GPU_TEXTURE_ADDRESSING_CLAMP); gpu_set_texture_addressing(image_tex_unit, GPU_TEXTURE_DIRECTION_V, GPU_TEXTURE_ADDRESSING_CLAMP); // gpu_set_texture_mipmap_filter(image_tex_unit, draw_bilinear_filter ? GPU_MIPMAP_FILTER_LINEAR : GPU_MIPMAP_FILTER_NONE); @@ -449,7 +449,7 @@ void draw_colored_rect_draw_buffer(bool tris_done) { if (!tris_done) draw_colored_tris_end(true); - gpu_vertex_buffer_unlock(&colored_rect_vertex_buffer, (colored_rect_buffer_index - colored_rect_buffer_start) * 4); + iron_gpu_vertex_buffer_unlock(&colored_rect_vertex_buffer, (colored_rect_buffer_index - colored_rect_buffer_start) * 4); iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &colored_pipeline); gpu_set_matrix4(colored_proj_loc, &draw_projection_matrix); gpu_set_vertex_buffer(&colored_rect_vertex_buffer); @@ -497,7 +497,7 @@ void draw_colored_tris_draw_buffer(bool rect_done) { if (!rect_done) draw_colored_rect_end(true); - gpu_vertex_buffer_unlock(&colored_tris_vertex_buffer, (colored_tris_buffer_index - colored_tris_buffer_start) * 3); + iron_gpu_vertex_buffer_unlock(&colored_tris_vertex_buffer, (colored_tris_buffer_index - colored_tris_buffer_start) * 3); iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &colored_pipeline); gpu_set_matrix4(colored_proj_loc, &draw_projection_matrix); gpu_set_vertex_buffer(&colored_tris_vertex_buffer); @@ -662,12 +662,12 @@ void draw_text_set_rect_colors(uint32_t color) { void draw_text_draw_buffer(bool end) { if (text_buffer_index - text_buffer_start == 0) return; - gpu_vertex_buffer_unlock(&text_vertex_buffer, text_buffer_index * 4); + iron_gpu_vertex_buffer_unlock(&text_vertex_buffer, text_buffer_index * 4); iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline); gpu_set_matrix4(text_proj_loc, &draw_projection_matrix); gpu_set_vertex_buffer(&text_vertex_buffer); gpu_set_index_buffer(&text_index_buffer); - gpu_set_texture(text_tex_unit, text_last_texture); + gpu_set_texture(&text_tex_unit, text_last_texture); gpu_set_texture_addressing(text_tex_unit, GPU_TEXTURE_DIRECTION_U, GPU_TEXTURE_ADDRESSING_CLAMP); gpu_set_texture_addressing(text_tex_unit, GPU_TEXTURE_DIRECTION_V, GPU_TEXTURE_ADDRESSING_CLAMP); gpu_set_texture_mipmap_filter(text_tex_unit, GPU_MIPMAP_FILTER_NONE); diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index e314c5f2..852eab26 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -29,8 +29,8 @@ iron_gpu_sampler_t *iron_internal_get_current_sampler(int stage, int unit); iron_gpu_command_list_t commandList; bool waitAfterNextDraw = false; -static iron_gpu_constant_buffer_t vertexConstantBuffer; -static iron_gpu_constant_buffer_t fragmentConstantBuffer; +static iron_gpu_buffer_t vertexConstantBuffer; +static iron_gpu_buffer_t fragmentConstantBuffer; static int constantBufferIndex = 0; static struct { @@ -44,8 +44,8 @@ static struct { typedef struct render_state { iron_gpu_pipeline_t *pipeline; - iron_gpu_index_buffer_t *index_buffer; - gpu_vertex_buffer_impl_t *vertex_buffer; + iron_gpu_buffer_t *index_buffer; + iron_gpu_buffer_t *vertex_buffer; bool viewport_set; int viewport_x; @@ -461,13 +461,12 @@ void gpu_set_render_targets(iron_gpu_texture_t **targets, int count) { current_state.scissor_set = false; } -void gpu_set_vertex_buffer(iron_gpu_vertex_buffer_t *buffer) { - gpu_vertex_buffer_impl_t *vb = &buffer->_buffer[buffer->_currentIndex]; - current_state.vertex_buffer = vb; - iron_gpu_command_list_set_vertex_buffer(&commandList, vb); +void gpu_set_vertex_buffer(iron_gpu_buffer_t *buffer) { + current_state.vertex_buffer = buffer; + iron_gpu_command_list_set_vertex_buffer(&commandList, buffer); } -void gpu_set_index_buffer(iron_gpu_index_buffer_t *buffer) { +void gpu_set_index_buffer(iron_gpu_buffer_t *buffer) { current_state.index_buffer = buffer; iron_gpu_command_list_set_index_buffer(&commandList, buffer); } @@ -477,54 +476,46 @@ void iron_gpu_set_pipeline(iron_gpu_pipeline_t *pipeline) { iron_gpu_command_list_set_pipeline(&commandList, pipeline); } -void gpu_set_texture(iron_gpu_texture_unit_t unit, iron_gpu_texture_t *render_target) { - +void gpu_set_texture(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { if (!render_target->_uploaded) { iron_gpu_command_list_upload_texture(&commandList, render_target); render_target->_uploaded = true; } - if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) { iron_gpu_command_list_render_target_to_texture_barrier(&commandList, render_target); render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; } - iron_gpu_texture_unit_t g5_unit; - memcpy(&g5_unit.stages[0], &unit.stages[0], IRON_GPU_SHADER_TYPE_COUNT * sizeof(int)); - bool found = false; for (int i = 0; i < current_state.texture_count; ++i) { - if (iron_gpu_texture_unit_equals(¤t_state.texture_units[i], &g5_unit)) { + if (iron_gpu_texture_unit_equals(¤t_state.texture_units[i], unit)) { current_state.textures[i] = render_target; - current_state.texture_units[i] = g5_unit; + current_state.texture_units[i] = *unit; found = true; break; } } if (!found) { current_state.textures[current_state.texture_count] = render_target; - current_state.texture_units[current_state.texture_count] = g5_unit; + current_state.texture_units[current_state.texture_count] = *unit; current_state.texture_count += 1; } - iron_gpu_command_list_set_texture(&commandList, g5_unit, render_target); + iron_gpu_command_list_set_texture(&commandList, *unit, render_target); } -void gpu_render_target_use_depth_as_texture(iron_gpu_texture_t *render_target, iron_gpu_texture_unit_t unit) { +void gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) { iron_gpu_command_list_render_target_to_texture_barrier(&commandList, render_target); render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; } - iron_gpu_texture_unit_t g5_unit; - memcpy(&g5_unit.stages[0], &unit.stages[0], IRON_GPU_SHADER_TYPE_COUNT * sizeof(int)); - bool found = false; for (int i = 0; i < current_state.depth_render_target_count; ++i) { - if (iron_gpu_texture_unit_equals(¤t_state.depth_render_target_units[i], &g5_unit)) { + if (iron_gpu_texture_unit_equals(¤t_state.depth_render_target_units[i], unit)) { current_state.depth_render_targets[i] = render_target; - current_state.depth_render_target_units[i] = g5_unit; + current_state.depth_render_target_units[i] = *unit; found = true; break; } @@ -532,23 +523,23 @@ void gpu_render_target_use_depth_as_texture(iron_gpu_texture_t *render_target, i if (!found) { assert(current_state.depth_render_target_count < MAX_TEXTURES); current_state.depth_render_targets[current_state.depth_render_target_count] = render_target; - current_state.depth_render_target_units[current_state.depth_render_target_count] = g5_unit; + current_state.depth_render_target_units[current_state.depth_render_target_count] = *unit; current_state.depth_render_target_count += 1; } - iron_gpu_command_list_set_texture_from_render_target_depth(&commandList, g5_unit, render_target); + iron_gpu_command_list_set_texture_from_render_target_depth(&commandList, *unit, render_target); } void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data) { iron_gpu_command_list_get_render_target_pixels(&commandList, render_target, data); } -void gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer) { +void gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { iron_gpu_index_buffer_unlock_all(buffer); iron_gpu_command_list_upload_index_buffer(&commandList, buffer); } -void gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count) { +void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { iron_gpu_index_buffer_unlock(buffer, count); iron_gpu_command_list_upload_index_buffer(&commandList, buffer); } @@ -563,76 +554,40 @@ void iron_gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const structure->size++; } -void gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage) { - buffer->_multiple = usage == GPU_USAGE_STATIC ? 1 : 2; - buffer->_currentIndex = 0; +void gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage) { buffer->myCount = count; - for (int i = 0; i < buffer->_multiple; ++i) { - iron_gpu_vertex_buffer_init(&buffer->_buffer[i], count, structure, usage == GPU_USAGE_STATIC); - } + iron_gpu_vertex_buffer_init(buffer, count, structure, usage == GPU_USAGE_STATIC); } -void gpu_vertex_buffer_destroy(iron_gpu_vertex_buffer_t *buffer) { - for (int i = 0; i < buffer->_multiple; ++i) { - iron_gpu_vertex_buffer_destroy(&buffer->_buffer[i]); - } +float *gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { + waitAfterNextDraw = true; + return iron_gpu_vertex_buffer_lock_all(buffer); } -static void iron_internal_prepare_lock(iron_gpu_vertex_buffer_t *buffer) { - ++buffer->_currentIndex; - if (buffer->_currentIndex >= buffer->_multiple - 1) { - waitAfterNextDraw = true; - } - if (buffer->_currentIndex >= buffer->_multiple) { - buffer->_currentIndex = 0; - } +float *gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { + waitAfterNextDraw = true; + return iron_gpu_vertex_buffer_lock(buffer, start, count); } -float *gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buffer) { - iron_internal_prepare_lock(buffer); - return iron_gpu_vertex_buffer_lock_all(&buffer->_buffer[buffer->_currentIndex]); -} - -float *gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buffer, int start, int count) { - iron_internal_prepare_lock(buffer); - return iron_gpu_vertex_buffer_lock(&buffer->_buffer[buffer->_currentIndex], start, count); -} - -void gpu_vertex_buffer_unlock_all(iron_gpu_vertex_buffer_t *buffer) { - iron_gpu_vertex_buffer_unlock_all(&buffer->_buffer[buffer->_currentIndex]); -} - -void gpu_vertex_buffer_unlock(iron_gpu_vertex_buffer_t *buffer, int count) { - iron_gpu_vertex_buffer_unlock(&buffer->_buffer[buffer->_currentIndex], count); -} - -int gpu_vertex_buffer_count(iron_gpu_vertex_buffer_t *buffer) { - return buffer->myCount; -} - -int gpu_vertex_buffer_stride(iron_gpu_vertex_buffer_t *buffer) { - return iron_gpu_vertex_buffer_stride(&buffer->_buffer[buffer->_currentIndex]); -} - -void iron_gpu_constant_buffer_set_int(iron_gpu_constant_buffer_t *buffer, int offset, int value) { +void iron_gpu_constant_buffer_set_int(iron_gpu_buffer_t *buffer, int offset, int value) { int *ints = (int *)(&buffer->data[offset]); ints[0] = value; } -void iron_gpu_constant_buffer_set_int2(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2) { +void iron_gpu_constant_buffer_set_int2(iron_gpu_buffer_t *buffer, int offset, int value1, int value2) { int *ints = (int *)(&buffer->data[offset]); ints[0] = value1; ints[1] = value2; } -void iron_gpu_constant_buffer_set_int3(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2, int value3) { +void iron_gpu_constant_buffer_set_int3(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3) { int *ints = (int *)(&buffer->data[offset]); ints[0] = value1; ints[1] = value2; ints[2] = value3; } -void iron_gpu_constant_buffer_set_int4(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2, int value3, int value4) { +void iron_gpu_constant_buffer_set_int4(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3, int value4) { int *ints = (int *)(&buffer->data[offset]); ints[0] = value1; ints[1] = value2; @@ -640,32 +595,32 @@ void iron_gpu_constant_buffer_set_int4(iron_gpu_constant_buffer_t *buffer, int o ints[3] = value4; } -void iron_gpu_constant_buffer_set_ints(iron_gpu_constant_buffer_t *buffer, int offset, int *values, int count) { +void iron_gpu_constant_buffer_set_ints(iron_gpu_buffer_t *buffer, int offset, int *values, int count) { int *ints = (int *)(&buffer->data[offset]); for (int i = 0; i < count; ++i) { ints[i] = values[i]; } } -void iron_gpu_constant_buffer_set_float(iron_gpu_constant_buffer_t *buffer, int offset, float value) { +void iron_gpu_constant_buffer_set_float(iron_gpu_buffer_t *buffer, int offset, float value) { float *floats = (float *)(&buffer->data[offset]); floats[0] = value; } -void iron_gpu_constant_buffer_set_float2(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2) { +void iron_gpu_constant_buffer_set_float2(iron_gpu_buffer_t *buffer, int offset, float value1, float value2) { float *floats = (float *)(&buffer->data[offset]); floats[0] = value1; floats[1] = value2; } -void iron_gpu_constant_buffer_set_float3(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2, float value3) { +void iron_gpu_constant_buffer_set_float3(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3) { float *floats = (float *)(&buffer->data[offset]); floats[0] = value1; floats[1] = value2; floats[2] = value3; } -void iron_gpu_constant_buffer_set_float4(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2, float value3, float value4) { +void iron_gpu_constant_buffer_set_float4(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3, float value4) { float *floats = (float *)(&buffer->data[offset]); floats[0] = value1; floats[1] = value2; @@ -673,14 +628,14 @@ void iron_gpu_constant_buffer_set_float4(iron_gpu_constant_buffer_t *buffer, int floats[3] = value4; } -void iron_gpu_constant_buffer_set_floats(iron_gpu_constant_buffer_t *buffer, int offset, float *values, int count) { +void iron_gpu_constant_buffer_set_floats(iron_gpu_buffer_t *buffer, int offset, float *values, int count) { float *floats = (float *)(&buffer->data[offset]); for (int i = 0; i < count; ++i) { floats[i] = values[i]; } } -void iron_gpu_constant_buffer_set_bool(iron_gpu_constant_buffer_t *buffer, int offset, bool value) { +void iron_gpu_constant_buffer_set_bool(iron_gpu_buffer_t *buffer, int offset, bool value) { int *ints = (int *)(&buffer->data[offset]); ints[0] = value ? 1 : 0; } @@ -694,7 +649,7 @@ static void iron_internal_set_matrix3(uint8_t *constants, int offset, iron_matri } } -void iron_gpu_constant_buffer_set_matrix3(iron_gpu_constant_buffer_t *buffer, int offset, iron_matrix3x3_t *value) { +void iron_gpu_constant_buffer_set_matrix3(iron_gpu_buffer_t *buffer, int offset, iron_matrix3x3_t *value) { if (iron_gpu_transpose_mat) { iron_matrix3x3_t m = *value; iron_matrix3x3_transpose(&m); @@ -714,7 +669,7 @@ static void iron_internal_set_matrix4(uint8_t *constants, int offset, iron_matri } } -void iron_gpu_constant_buffer_set_matrix4(iron_gpu_constant_buffer_t *buffer, int offset, iron_matrix4x4_t *value) { +void iron_gpu_constant_buffer_set_matrix4(iron_gpu_buffer_t *buffer, int offset, iron_matrix4x4_t *value) { if (iron_gpu_transpose_mat) { iron_matrix4x4_t m = *value; iron_matrix4x4_transpose(&m); diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index e6135085..7229fc7b 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -51,8 +51,13 @@ typedef struct iron_gpu_texture { gpu_texture_impl_t impl; } iron_gpu_texture_t; +typedef struct gpu_buffer { + int myCount; + uint8_t *data; + gpu_buffer_impl_t impl; +} iron_gpu_buffer_t; + int iron_gpu_max_bound_textures(void); -void iron_gpu_flush(void); void iron_gpu_begin(iron_gpu_texture_t *renderTarget); void iron_gpu_end(void); bool iron_gpu_swap_buffers(void); @@ -66,7 +71,7 @@ struct iron_gpu_pipeline; struct iron_gpu_texture; struct iron_gpu_texture_unit; struct iron_gpu_constant_location; -struct iron_gpu_index_buffer; +struct iron_gpu_buffer; typedef enum { GPU_TEXTURE_ADDRESSING_REPEAT, @@ -126,15 +131,15 @@ void gpu_set_texture_minification_filter(struct iron_gpu_texture_unit unit, gpu_ void gpu_set_texture_mipmap_filter(struct iron_gpu_texture_unit unit, gpu_mipmap_filter_t filter); void gpu_restore_render_target(void); void gpu_set_render_targets(struct iron_gpu_texture **targets, int count); -void gpu_set_texture(struct iron_gpu_texture_unit unit, struct iron_gpu_texture *texture); -void gpu_set_index_buffer(struct iron_gpu_index_buffer *buffer); +void gpu_set_texture(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *texture); +void gpu_set_index_buffer(iron_gpu_buffer_t *buffer); void gpu_internal_init_window(int depth_buffer_bits, bool vsync); -void gpu_render_target_use_depth_as_texture(struct iron_gpu_texture *renderTarget, struct iron_gpu_texture_unit unit); +void gpu_set_texture_depth(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *renderTarget); -void gpu_index_buffer_unlock_all(struct iron_gpu_index_buffer *buffer); -void gpu_index_buffer_unlock(struct iron_gpu_index_buffer *buffer, int count); +void gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer); +void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count); #define IRON_GPU_MAX_VERTEX_ELEMENTS 16 @@ -201,71 +206,50 @@ void iron_gpu_render_target_init(iron_gpu_texture_t *target, int width, int heig void iron_gpu_render_target_init_framebuffer(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits); void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_texture_t *source); -typedef struct gpu_vertex_buffer { - int myCount; - int _currentIndex; - int _multiple; - gpu_vertex_buffer_impl_t _buffer[2]; -} iron_gpu_vertex_buffer_t; +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpu_memory); +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer); +float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer); +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); +void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer); +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count); +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer); +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer); +int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buffer); -void iron_gpu_vertex_buffer_init(gpu_vertex_buffer_impl_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpu_memory); -void iron_gpu_vertex_buffer_destroy(gpu_vertex_buffer_impl_t *buffer); -float *iron_gpu_vertex_buffer_lock_all(gpu_vertex_buffer_impl_t *buffer); -float *iron_gpu_vertex_buffer_lock(gpu_vertex_buffer_impl_t *buffer, int start, int count); -void iron_gpu_vertex_buffer_unlock_all(gpu_vertex_buffer_impl_t *buffer); -void iron_gpu_vertex_buffer_unlock(gpu_vertex_buffer_impl_t *buffer, int count); -int iron_gpu_vertex_buffer_count(gpu_vertex_buffer_impl_t *buffer); -int iron_gpu_vertex_buffer_stride(gpu_vertex_buffer_impl_t *buffer); -int iron_gpu_internal_vertex_buffer_set(gpu_vertex_buffer_impl_t *buffer); +void gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage); +float *gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer); +float *gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); +void gpu_set_vertex_buffer(iron_gpu_buffer_t *buffer); -void gpu_vertex_buffer_init(iron_gpu_vertex_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage); -void gpu_vertex_buffer_destroy(iron_gpu_vertex_buffer_t *buffer); -float *gpu_vertex_buffer_lock_all(iron_gpu_vertex_buffer_t *buffer); -float *gpu_vertex_buffer_lock(iron_gpu_vertex_buffer_t *buffer, int start, int count); -void gpu_vertex_buffer_unlock_all(iron_gpu_vertex_buffer_t *buffer); -void gpu_vertex_buffer_unlock(iron_gpu_vertex_buffer_t *buffer, int count); -int gpu_vertex_buffer_count(iron_gpu_vertex_buffer_t *buffer); -int gpu_vertex_buffer_stride(iron_gpu_vertex_buffer_t *buffer); -void gpu_set_vertex_buffer(iron_gpu_vertex_buffer_t *buffer); - -typedef struct iron_gpu_constant_buffer { - uint8_t *data; - gpu_constant_buffer_impl_t impl; -} iron_gpu_constant_buffer_t; - -void iron_gpu_constant_buffer_init(iron_gpu_constant_buffer_t *buffer, int size); -void iron_gpu_constant_buffer_destroy(iron_gpu_constant_buffer_t *buffer); -void iron_gpu_constant_buffer_lock_all(iron_gpu_constant_buffer_t *buffer); -void iron_gpu_constant_buffer_lock(iron_gpu_constant_buffer_t *buffer, int start, int count); -void iron_gpu_constant_buffer_unlock(iron_gpu_constant_buffer_t *buffer); -int iron_gpu_constant_buffer_size(iron_gpu_constant_buffer_t *buffer); -void iron_gpu_constant_buffer_set_bool(iron_gpu_constant_buffer_t *buffer, int offset, bool value); -void iron_gpu_constant_buffer_set_int(iron_gpu_constant_buffer_t *buffer, int offset, int value); -void iron_gpu_constant_buffer_set_int2(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2); -void iron_gpu_constant_buffer_set_int3(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2, int value3); -void iron_gpu_constant_buffer_set_int4(iron_gpu_constant_buffer_t *buffer, int offset, int value1, int value2, int value3, int value4); -void iron_gpu_constant_buffer_set_ints(iron_gpu_constant_buffer_t *buffer, int offset, int *values, int count); -void iron_gpu_constant_buffer_set_float(iron_gpu_constant_buffer_t *buffer, int offset, float value); -void iron_gpu_constant_buffer_set_float2(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2); -void iron_gpu_constant_buffer_set_float3(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2, float value3); -void iron_gpu_constant_buffer_set_float4(iron_gpu_constant_buffer_t *buffer, int offset, float value1, float value2, float value3, float value4); -void iron_gpu_constant_buffer_set_floats(iron_gpu_constant_buffer_t *buffer, int offset, float *values, int count); -void iron_gpu_constant_buffer_set_matrix3(iron_gpu_constant_buffer_t *buffer, int offset, iron_matrix3x3_t *value); -void iron_gpu_constant_buffer_set_matrix4(iron_gpu_constant_buffer_t *buffer, int offset, iron_matrix4x4_t *value); +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size); +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer); +void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer); +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer); +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer); +void iron_gpu_constant_buffer_set_bool(iron_gpu_buffer_t *buffer, int offset, bool value); +void iron_gpu_constant_buffer_set_int(iron_gpu_buffer_t *buffer, int offset, int value); +void iron_gpu_constant_buffer_set_int2(iron_gpu_buffer_t *buffer, int offset, int value1, int value2); +void iron_gpu_constant_buffer_set_int3(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3); +void iron_gpu_constant_buffer_set_int4(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3, int value4); +void iron_gpu_constant_buffer_set_ints(iron_gpu_buffer_t *buffer, int offset, int *values, int count); +void iron_gpu_constant_buffer_set_float(iron_gpu_buffer_t *buffer, int offset, float value); +void iron_gpu_constant_buffer_set_float2(iron_gpu_buffer_t *buffer, int offset, float value1, float value2); +void iron_gpu_constant_buffer_set_float3(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3); +void iron_gpu_constant_buffer_set_float4(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3, float value4); +void iron_gpu_constant_buffer_set_floats(iron_gpu_buffer_t *buffer, int offset, float *values, int count); +void iron_gpu_constant_buffer_set_matrix3(iron_gpu_buffer_t *buffer, int offset, iron_matrix3x3_t *value); +void iron_gpu_constant_buffer_set_matrix4(iron_gpu_buffer_t *buffer, int offset, iron_matrix4x4_t *value); extern bool iron_gpu_transpose_mat; -typedef struct iron_gpu_index_buffer { - gpu_index_buffer_impl_t impl; -} iron_gpu_index_buffer_t; - -void iron_gpu_index_buffer_init(iron_gpu_index_buffer_t *buffer, int count, bool gpu_memory); -void iron_gpu_index_buffer_destroy(iron_gpu_index_buffer_t *buffer); -void *iron_gpu_index_buffer_lock_all(iron_gpu_index_buffer_t *buffer); -void *iron_gpu_index_buffer_lock(iron_gpu_index_buffer_t *buffer, int start, int count); -void iron_gpu_index_buffer_unlock_all(iron_gpu_index_buffer_t *buffer); -void iron_gpu_index_buffer_unlock(iron_gpu_index_buffer_t *buffer, int count); -int iron_gpu_index_buffer_count(iron_gpu_index_buffer_t *buffer); +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int count, bool gpu_memory); +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer); +void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer); +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); +void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer); +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count); +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer); typedef enum iron_gpu_shader_type { IRON_GPU_SHADER_TYPE_FRAGMENT, @@ -406,9 +390,7 @@ void iron_gpu_internal_pipeline_set_defaults(iron_gpu_pipeline_t *pipeline); struct iron_gpu_pipeline; struct iron_gpu_texture; struct iron_gpu_command_list; -struct iron_gpu_constant_buffer; -struct iron_gpu_index_buffer; -struct iron_gpu_vertex_buffer; +struct iron_gpu_buffer; typedef struct iron_gpu_command_list { gpu_command_list_impl_t impl; @@ -429,14 +411,14 @@ void iron_gpu_command_list_viewport(iron_gpu_command_list_t *list, int x, int y, void iron_gpu_command_list_scissor(iron_gpu_command_list_t *list, int x, int y, int width, int height); void iron_gpu_command_list_disable_scissor(iron_gpu_command_list_t *list); void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct iron_gpu_pipeline *pipeline); -void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, gpu_vertex_buffer_impl_t *buffer); -void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer); +void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer); +void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer); void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count); -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_index_buffer *buffer); -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_vertex_buffer *buffer); +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer); +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer); void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture); -void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size); -void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_constant_buffer *buffer, int offset, size_t size); +void iron_gpu_command_list_set_vertex_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size); +void iron_gpu_command_list_set_fragment_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size); void iron_gpu_command_list_execute(iron_gpu_command_list_t *list); void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list); void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *list, struct iron_gpu_texture *render_target, uint8_t *data); @@ -447,7 +429,7 @@ void iron_gpu_command_list_set_sampler(iron_gpu_command_list_t *list, iron_gpu_t void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data); typedef struct iron_gpu_raytrace_pipeline { - struct iron_gpu_constant_buffer *_constant_buffer; + iron_gpu_buffer_t *_constant_buffer; gpu_raytrace_pipeline_impl_t impl; } iron_gpu_raytrace_pipeline_t; @@ -457,13 +439,13 @@ typedef struct iron_gpu_raytrace_acceleration_structure { bool iron_gpu_raytrace_supported(void); void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, struct iron_gpu_command_list *command_list, - void *ray_shader, int ray_shader_size, struct iron_gpu_constant_buffer *constant_buffer); + void *ray_shader, int ray_shader_size, struct iron_gpu_buffer *constant_buffer); void iron_gpu_raytrace_pipeline_destroy(iron_gpu_raytrace_pipeline_t *pipeline); void iron_gpu_raytrace_acceleration_structure_init(iron_gpu_raytrace_acceleration_structure_t *accel); -void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_vertex_buffer *vb, struct iron_gpu_index_buffer *ib, iron_matrix4x4_t transform); +void iron_gpu_raytrace_acceleration_structure_add(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_buffer *vb, struct iron_gpu_buffer *ib, iron_matrix4x4_t transform); void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_command_list *command_list, - struct iron_gpu_vertex_buffer *_vb_full, struct iron_gpu_index_buffer *_ib_full); + struct iron_gpu_buffer *_vb_full, struct iron_gpu_buffer *_ib_full); void iron_gpu_raytrace_acceleration_structure_destroy(iron_gpu_raytrace_acceleration_structure_t *accel); void iron_gpu_raytrace_set_textures(struct iron_gpu_texture *texpaint0, struct iron_gpu_texture *texpaint1, struct iron_gpu_texture *texpaint2, diff --git a/base/sources/ts/iron/const_data.ts b/base/sources/ts/iron/const_data.ts index f3ed0d5e..60ae4d5b 100644 --- a/base/sources/ts/iron/const_data.ts +++ b/base/sources/ts/iron/const_data.ts @@ -1,8 +1,8 @@ -let const_data_screen_aligned_vb: iron_gpu_vertex_buffer_t = null; -let const_data_screen_aligned_ib: iron_gpu_index_buffer_t = null; -let const_data_skydome_vb: iron_gpu_vertex_buffer_t = null; -let const_data_skydome_ib: iron_gpu_index_buffer_t = null; +let const_data_screen_aligned_vb: iron_gpu_buffer_t = null; +let const_data_screen_aligned_ib: iron_gpu_buffer_t = null; +let const_data_skydome_vb: iron_gpu_buffer_t = null; +let const_data_skydome_ib: iron_gpu_buffer_t = null; function const_data_create_screen_aligned_data() { // Over-sized triangle @@ -17,7 +17,7 @@ function const_data_create_screen_aligned_data() { for (let i: i32 = 0; i < math_floor((vertices.length) / 4); ++i) { buffer_set_f32(vertices, i * 4, data[i]); } - gpu_vertex_buffer_unlock_all(const_data_screen_aligned_vb); + iron_gpu_vertex_buffer_unlock_all(const_data_screen_aligned_vb); const_data_screen_aligned_ib = gpu_create_index_buffer(indices.length); let id: u32_array_t = gpu_lock_index_buffer(const_data_screen_aligned_ib); @@ -50,7 +50,7 @@ function const_data_create_skydome_data() { buffer_set_f32(vertices, (i * struct_length + 4) * 4, ARRAY_ACCESS(_const_data_skydome_nor, i * 3 + 1)); buffer_set_f32(vertices, (i * struct_length + 5) * 4, ARRAY_ACCESS(_const_data_skydome_nor, i * 3 + 2)); } - gpu_vertex_buffer_unlock_all(const_data_skydome_vb); + iron_gpu_vertex_buffer_unlock_all(const_data_skydome_vb); const_data_skydome_ib = gpu_create_index_buffer(_const_data_skydome_indices_count); let id: u32_array_t = gpu_lock_index_buffer(const_data_skydome_ib); diff --git a/base/sources/ts/iron/iron.ts b/base/sources/ts/iron/iron.ts index 9de31eae..824c55b6 100644 --- a/base/sources/ts/iron/iron.ts +++ b/base/sources/ts/iron/iron.ts @@ -219,7 +219,7 @@ declare function gpu_set_index_buffer(buffer: any): void; declare function gpu_create_vertex_buffer(count: i32, structure: iron_gpu_vertex_structure_t, usage: i32): any; declare function gpu_delete_vertex_buffer(buffer: any): void; declare function gpu_lock_vertex_buffer(buffer: any): buffer_t; -declare function gpu_vertex_buffer_unlock_all(buffer: any): void; +declare function iron_gpu_vertex_buffer_unlock_all(buffer: any): void; declare function gpu_set_vertex_buffer(buffer: any): void; declare function gpu_draw_indexed_vertices(start: i32 = 0, count: i32 = -1): void; declare function gpu_create_shader(data: buffer_t, type: i32): iron_gpu_shader_t; @@ -243,7 +243,7 @@ declare function iron_copy_to_clipboard(text: string): void; declare function gpu_get_constant_location(pipeline: any, name: string): any; declare function gpu_get_texture_unit(pipeline: any, name: string): any; declare function _gpu_set_texture(stage: any, texture: any): void; -declare function gpu_set_texture_depth(unit: any, texture: any): void; +declare function _gpu_set_texture_depth(unit: any, texture: any): void; declare function gpu_set_texture_parameters(tex_unit: any, u_addr: i32, v_addr: i32, min_filter: i32, mag_filter: i32, mip_filter: i32): void; declare function _gpu_set_bool(location: any, value: bool): void; declare function _gpu_set_int(location: any, value: i32): void; @@ -634,11 +634,7 @@ declare type iron_gpu_vertex_structure_t = { size?: i32; }; -declare type iron_gpu_index_buffer_t = { - impl?: any; -}; - -declare type iron_gpu_vertex_buffer_t = { +declare type iron_gpu_buffer_t = { impl?: any; }; diff --git a/base/sources/ts/iron/mesh_data.ts b/base/sources/ts/iron/mesh_data.ts index aa12c41b..90ad12da 100644 --- a/base/sources/ts/iron/mesh_data.ts +++ b/base/sources/ts/iron/mesh_data.ts @@ -165,13 +165,13 @@ function mesh_data_get_vertex_array(raw: mesh_data_t, name: string): vertex_arra return null; } -function mesh_data_get(raw: mesh_data_t, vs: vertex_element_t[]): iron_gpu_vertex_buffer_t { +function mesh_data_get(raw: mesh_data_t, vs: vertex_element_t[]): iron_gpu_buffer_t { let key: string = ""; for (let i: i32 = 0; i < vs.length; ++i) { let e: vertex_element_t = vs[i]; key += e.name; } - let vb: iron_gpu_vertex_buffer_t = map_get(raw._.vertex_buffer_map, key); + let vb: iron_gpu_buffer_t = map_get(raw._.vertex_buffer_map, key); if (vb == null) { let vertex_arrays: vertex_array_t[] = []; let has_tex: bool = false; @@ -201,7 +201,7 @@ function mesh_data_get(raw: mesh_data_t, vs: vertex_element_t[]): iron_gpu_verte vb = gpu_create_vertex_buffer(math_floor(positions.values.length / size), vstruct, usage_t.STATIC); raw._.vertices = gpu_lock_vertex_buffer(vb); mesh_data_build_vertices(raw._.vertices, vertex_arrays, 0, has_tex && uvs == null, tex_offset); - gpu_vertex_buffer_unlock_all(vb); + iron_gpu_vertex_buffer_unlock_all(vb); map_set(raw._.vertex_buffer_map, key, vb); if (has_tex && uvs == null) { iron_log("Geometry " + raw.name + " is missing UV map"); @@ -223,7 +223,7 @@ function mesh_data_build(raw: mesh_data_t) { raw._.vertex_buffer = gpu_create_vertex_buffer(math_floor(positions.values.length / size), raw._.structure, usage_t.STATIC); raw._.vertices = gpu_lock_vertex_buffer(raw._.vertex_buffer); mesh_data_build_vertices(raw._.vertices, raw.vertex_arrays); - gpu_vertex_buffer_unlock_all(raw._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(raw._.vertex_buffer); let struct_str: string = ""; for (let i: i32 = 0; i < raw._.structure.size; ++i) { @@ -239,7 +239,7 @@ function mesh_data_build(raw: mesh_data_t) { if (id.length == 0) { continue; } - let index_buffer: iron_gpu_index_buffer_t = gpu_create_index_buffer(id.length); + let index_buffer: iron_gpu_buffer_t = gpu_create_index_buffer(id.length); let indices_array: u32_array_t = gpu_lock_index_buffer(index_buffer); for (let i: i32 = 0; i < indices_array.length; ++i) { @@ -340,13 +340,13 @@ function mesh_data_calculate_aabb(raw: mesh_data_t): vec4_t { function mesh_data_delete(raw: mesh_data_t) { let vertex_buffer_keys: string[] = map_keys(raw._.vertex_buffer_map); for (let i: i32 = 0; i < vertex_buffer_keys.length; ++i) { - let buf: iron_gpu_vertex_buffer_t = map_get(raw._.vertex_buffer_map, vertex_buffer_keys[i]); + let buf: iron_gpu_buffer_t = map_get(raw._.vertex_buffer_map, vertex_buffer_keys[i]); if (buf != null) { gpu_delete_vertex_buffer(buf); } } for (let i: i32 = 0; i < raw._.index_buffers.length; ++i) { - let buf: iron_gpu_index_buffer_t = raw._.index_buffers[i]; + let buf: iron_gpu_buffer_t = raw._.index_buffers[i]; gpu_delete_index_buffer(buf); } } diff --git a/base/sources/ts/iron/scene.ts b/base/sources/ts/iron/scene.ts index 9545a14b..b670eb1a 100644 --- a/base/sources/ts/iron/scene.ts +++ b/base/sources/ts/iron/scene.ts @@ -475,9 +475,9 @@ type mesh_data_t = { type mesh_data_runtime_t = { refcount?: i32; // Number of users handle?: string; // Handle used to retrieve this object in Data - vertex_buffer?: iron_gpu_vertex_buffer_t; - vertex_buffer_map?: map_t; - index_buffers?: iron_gpu_index_buffer_t[]; + vertex_buffer?: iron_gpu_buffer_t; + vertex_buffer_map?: map_t; + index_buffers?: iron_gpu_buffer_t[]; ready?: bool; vertices?: buffer_t; indices?: u32_array_t[]; diff --git a/base/sources/ts/iron/uniforms.ts b/base/sources/ts/iron/uniforms.ts index 2722dc30..7bc9134b 100644 --- a/base/sources/ts/iron/uniforms.ts +++ b/base/sources/ts/iron/uniforms.ts @@ -99,7 +99,7 @@ function uniforms_set_obj_consts(context: shader_context_t, object: object_t) { let image: iron_gpu_texture_t = uniforms_tex_links(object, current_material(object), tu.link); if (image != null) { ends_with(tu.link, "_depth") ? - gpu_set_texture_depth(context._.tex_units[j], image) : + _gpu_set_texture_depth(context._.tex_units[j], image) : _gpu_set_texture(context._.tex_units[j], image); gpu_set_texture_parameters(context._.tex_units[j], tex_addressing_t.REPEAT, tex_addressing_t.REPEAT, tex_filter_t.LINEAR, tex_filter_t.LINEAR, mip_map_filter_t.NONE); } @@ -119,7 +119,7 @@ function uniforms_bind_render_target(rt: render_target_t, context: shader_contex if (sampler_id == tus[j].name) { if (attach_depth) { - gpu_set_texture_depth(context._.tex_units[j], rt._image); // sampler2D + _gpu_set_texture_depth(context._.tex_units[j], rt._image); // sampler2D } else { _gpu_set_texture(context._.tex_units[j], rt._image); // sampler2D diff --git a/base/sources/ts/line_draw.ts b/base/sources/ts/line_draw.ts index 3fee9627..2fa7fe01 100644 --- a/base/sources/ts/line_draw.ts +++ b/base/sources/ts/line_draw.ts @@ -4,8 +4,8 @@ let line_draw_strength: f32 = 0.005; let line_draw_mat: mat4_t = mat4_nan(); let line_draw_dim: vec4_t = vec4_nan(); -let line_draw_vertex_buffer: iron_gpu_vertex_buffer_t; -let line_draw_index_buffer: iron_gpu_index_buffer_t; +let line_draw_vertex_buffer: iron_gpu_buffer_t; +let line_draw_index_buffer: iron_gpu_buffer_t; let line_draw_pipeline: iron_gpu_pipeline_t = null; let line_draw_overlay_pipeline: iron_gpu_pipeline_t = null; @@ -227,7 +227,7 @@ function line_draw_begin() { } function line_draw_end(overlay: bool = false) { - gpu_vertex_buffer_unlock_all(line_draw_vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(line_draw_vertex_buffer); gpu_index_buffer_unlock_all(line_draw_index_buffer); gpu_set_vertex_buffer(line_draw_vertex_buffer); @@ -245,8 +245,8 @@ function line_draw_end(overlay: bool = false) { gpu_draw_indexed_vertices(0, line_draw_lines * 6); } -let _shape_draw_sphere_vb: iron_gpu_vertex_buffer_t = null; -let _shape_draw_sphere_ib: iron_gpu_index_buffer_t = null; +let _shape_draw_sphere_vb: iron_gpu_buffer_t = null; +let _shape_draw_sphere_ib: iron_gpu_buffer_t = null; function shape_draw_sphere(mat: mat4_t) { line_draw_init(); @@ -265,7 +265,7 @@ function shape_draw_sphere(mat: mat4_t) { buffer_set_f32(data, (i * 3 + 1) * 4, posa[i * 4 + 1] / 32767); buffer_set_f32(data, (i * 3 + 2) * 4, posa[i * 4 + 2] / 32767); } - gpu_vertex_buffer_unlock_all(_shape_draw_sphere_vb); + iron_gpu_vertex_buffer_unlock_all(_shape_draw_sphere_vb); _shape_draw_sphere_ib = md._.index_buffers[0]; } diff --git a/base/sources/ts/render_path_raytrace.ts b/base/sources/ts/render_path_raytrace.ts index c1b2974f..38a88df6 100644 --- a/base/sources/ts/render_path_raytrace.ts +++ b/base/sources/ts/render_path_raytrace.ts @@ -7,8 +7,8 @@ let render_path_raytrace_first: bool = true; let render_path_raytrace_f32a: f32_array_t = f32_array_create(24); let render_path_raytrace_help_mat: mat4_t = mat4_identity(); let render_path_raytrace_transform: mat4_t; -let render_path_raytrace_vb: iron_gpu_vertex_buffer_t; -let render_path_raytrace_ib: iron_gpu_index_buffer_t; +let render_path_raytrace_vb: iron_gpu_buffer_t; +let render_path_raytrace_ib: iron_gpu_buffer_t; let render_path_raytrace_last_envmap: iron_gpu_texture_t = null; let render_path_raytrace_is_bake: bool = false; @@ -168,8 +168,8 @@ function render_path_raytrace_raytrace_init(shader_name: string, build: bool = t iron_raytrace_as_add(render_path_raytrace_vb, render_path_raytrace_ib, render_path_raytrace_transform); ///end - let vb_full: iron_gpu_vertex_buffer_t = context_raw.merged_object.data._.vertex_buffer; - let ib_full: iron_gpu_index_buffer_t = context_raw.merged_object.data._.index_buffers[0]; + let vb_full: iron_gpu_buffer_t = context_raw.merged_object.data._.vertex_buffer; + let ib_full: iron_gpu_buffer_t = context_raw.merged_object.data._.index_buffers[0]; iron_raytrace_as_build(vb_full, ib_full); } diff --git a/base/sources/ts/util_mesh.ts b/base/sources/ts/util_mesh.ts index bae8288c..68971a91 100644 --- a/base/sources/ts/util_mesh.ts +++ b/base/sources/ts/util_mesh.ts @@ -169,7 +169,7 @@ function util_mesh_swap_axis(a: i32, b: i32) { buffer_set_i16(vertices, (i * l + 4) * 2, vas[1].values[i * 2 ]); buffer_set_i16(vertices, (i * l + 5) * 2, vas[1].values[i * 2 + 1]); } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); } util_mesh_remove_merged(); @@ -194,7 +194,7 @@ function util_mesh_flip_normals() { buffer_set_i16(vertices, (i * l + 4) * 2, -buffer_get_i16(vertices, (i * l + 4) * 2)); buffer_set_i16(vertices, (i * l + 5) * 2, -buffer_get_i16(vertices, (i * l + 5) * 2)); } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); } render_path_raytrace_ready = false; @@ -286,7 +286,7 @@ function util_mesh_calc_normals(smooth: bool = false) { } } } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); let va0: i16_array_t = o.data.vertex_arrays[0].values; let va1: i16_array_t = o.data.vertex_arrays[1].values; @@ -377,7 +377,7 @@ function util_mesh_to_origin() { buffer_set_i16(vertices, (i * l + 1) * 2, va[i * 4 + 1]); buffer_set_i16(vertices, (i * l + 2) * 2, va[i * 4 + 2]); } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); } util_mesh_merge(); @@ -402,7 +402,7 @@ function util_mesh_apply_displacement(texpaint_pack: iron_gpu_texture_t, strengt buffer_set_i16(vertices, (i * l + 1) * 2, buffer_get_i16(vertices, (i * l + 1) * 2) - math_floor(buffer_get_i16(vertices, (i * l + 5) * 2) * h)); buffer_set_i16(vertices, (i * l + 2) * 2, buffer_get_i16(vertices, (i * l + 2) * 2) - math_floor(buffer_get_i16(vertices, (i * l + 3) * 2) * h)); } - gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); let va0: i16_array_t = o.data.vertex_arrays[0].values; for (let i: i32 = 0; i < math_floor((vertices.length) / 4 / l); ++i) { diff --git a/base/sources/ts/util_render.ts b/base/sources/ts/util_render.ts index 5d8e35f5..4a59e5a1 100644 --- a/base/sources/ts/util_render.ts +++ b/base/sources/ts/util_render.ts @@ -3,8 +3,8 @@ let util_render_material_preview_size: i32 = 256; let util_render_decal_preview_size: i32 = 512; let util_render_layer_preview_size: i32 = 200; let util_render_font_preview_size: i32 = 200; -let util_render_screen_aligned_full_vb: iron_gpu_vertex_buffer_t = null; -let util_render_screen_aligned_full_ib: iron_gpu_index_buffer_t = null; +let util_render_screen_aligned_full_vb: iron_gpu_buffer_t = null; +let util_render_screen_aligned_full_ib: iron_gpu_buffer_t = null; function util_render_make_material_preview() { context_raw.material_preview = true; @@ -468,7 +468,7 @@ function util_render_create_screen_aligned_full_data() { for (let i: i32 = 0; i < math_floor((vertices.length) / 2); ++i) { buffer_set_i16(vertices, i * 2, data[i]); } - gpu_vertex_buffer_unlock_all(util_render_screen_aligned_full_vb); + iron_gpu_vertex_buffer_unlock_all(util_render_screen_aligned_full_vb); util_render_screen_aligned_full_ib = gpu_create_index_buffer(indices.length); let id: u32_array_t = gpu_lock_index_buffer(util_render_screen_aligned_full_ib); diff --git a/base/tests/triangle/main.ts b/base/tests/triangle/main.ts index fcfc3aa8..5f5b180f 100644 --- a/base/tests/triangle/main.ts +++ b/base/tests/triangle/main.ts @@ -76,7 +76,7 @@ function main() { for (let i: i32 = 0; i < vertices.length; i++) { buffer_set_f32(vb_data, i * 4, vertices[i]); } - gpu_vertex_buffer_unlock_all(vb); + iron_gpu_vertex_buffer_unlock_all(vb); ib = gpu_create_index_buffer(indices.length); let ib_data: u32_array_t = gpu_lock_index_buffer(ib);