diff --git a/armorpaint/sources/make_mesh.ts b/armorpaint/sources/make_mesh.ts index 458ea068..f9b82f11 100644 --- a/armorpaint/sources/make_mesh.ts +++ b/armorpaint/sources/make_mesh.ts @@ -54,7 +54,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte } num_layers++; texture_count++; - node_shader_add_texture(kong, "texpaint_pack_vert" + l.id, "_texpaint_pack_vert" + l.id, true); + node_shader_add_texture(kong, "texpaint_pack_vert" + l.id, "_texpaint_pack_vert" + l.id); node_shader_write_vert(kong, "height += sample_lod(texpaint_pack_vert" + l.id + ", sampler_linear, input.tex, 0.0).a;"); let masks: slot_layer_t[] = slot_layer_get_masks(l); if (masks != null) { diff --git a/armorpaint/sources/make_paint.ts b/armorpaint/sources/make_paint.ts index 3522b5c9..35c8a6d0 100644 --- a/armorpaint/sources/make_paint.ts +++ b/armorpaint/sources/make_paint.ts @@ -31,8 +31,7 @@ function make_paint_run(data: material_t, matcon: material_context_t): node_shad let props: shader_context_t = { name: context_id, depth_write: false, - compare_mode: "always", // TODO: align texcoords winding order - // cull_mode: "counter_clockwise", + compare_mode: "always", cull_mode: "none", vertex_elements: [ { diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index b9bfc12d..1b8bd11b 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -1,9 +1,7 @@ -#include #define WIN32_LEAN_AND_MEAN -#define MAX_SHADER_THING 32 #define HEAP_SIZE 1024 - +#include #include #include #include @@ -23,7 +21,18 @@ static int framebuffer_count = 0; static bool began = false; bool iron_gpu_transpose_mat = false; -struct dx_window windows[1]; +struct IDXGISwapChain *window_swapChain; +UINT64 window_current_fence_value; +UINT64 window_fence_values[QUEUE_SLOT_COUNT]; +HANDLE window_frame_fence_events[QUEUE_SLOT_COUNT]; +struct ID3D12Fence *window_frame_fences[QUEUE_SLOT_COUNT]; +int window_width; +int window_height; +int window_new_width; +int window_new_height; +int window_current_backbuffer; +bool window_vsync; + ID3D12Device *device = NULL; ID3D12CommandQueue *queue; @@ -45,18 +54,6 @@ static D3D12_BLEND convert_blend_factor(iron_gpu_blending_factor_t factor) { return D3D12_BLEND_INV_SRC_ALPHA; case IRON_GPU_BLEND_INV_DEST_ALPHA: return D3D12_BLEND_INV_DEST_ALPHA; - case IRON_GPU_BLEND_SOURCE_COLOR: - return D3D12_BLEND_SRC_COLOR; - case IRON_GPU_BLEND_DEST_COLOR: - return D3D12_BLEND_DEST_COLOR; - case IRON_GPU_BLEND_INV_SOURCE_COLOR: - return D3D12_BLEND_INV_SRC_COLOR; - case IRON_GPU_BLEND_INV_DEST_COLOR: - return D3D12_BLEND_INV_DEST_COLOR; - case IRON_GPU_BLEND_CONSTANT: - return D3D12_BLEND_BLEND_FACTOR; - case IRON_GPU_BLEND_INV_CONSTANT: - return D3D12_BLEND_INV_BLEND_FACTOR; } } @@ -64,14 +61,6 @@ static D3D12_BLEND_OP convert_blend_operation(iron_gpu_blending_operation_t op) switch (op) { case IRON_GPU_BLENDOP_ADD: return D3D12_BLEND_OP_ADD; - case IRON_GPU_BLENDOP_SUBTRACT: - return D3D12_BLEND_OP_SUBTRACT; - case IRON_GPU_BLENDOP_REVERSE_SUBTRACT: - return D3D12_BLEND_OP_REV_SUBTRACT; - case IRON_GPU_BLENDOP_MIN: - return D3D12_BLEND_OP_MIN; - case IRON_GPU_BLENDOP_MAX: - return D3D12_BLEND_OP_MAX; } } @@ -94,18 +83,8 @@ static D3D12_COMPARISON_FUNC convert_compare_mode(iron_gpu_compare_mode_t compar return D3D12_COMPARISON_FUNC_ALWAYS; case IRON_GPU_COMPARE_MODE_NEVER: return D3D12_COMPARISON_FUNC_NEVER; - case IRON_GPU_COMPARE_MODE_EQUAL: - return D3D12_COMPARISON_FUNC_EQUAL; - case IRON_GPU_COMPARE_MODE_NOT_EQUAL: - return D3D12_COMPARISON_FUNC_NOT_EQUAL; case IRON_GPU_COMPARE_MODE_LESS: return D3D12_COMPARISON_FUNC_LESS; - case IRON_GPU_COMPARE_MODE_LESS_EQUAL: - return D3D12_COMPARISON_FUNC_LESS_EQUAL; - case IRON_GPU_COMPARE_MODE_GREATER: - return D3D12_COMPARISON_FUNC_GREATER; - case IRON_GPU_COMPARE_MODE_GREATER_EQUAL: - return D3D12_COMPARISON_FUNC_GREATER_EQUAL; } } @@ -168,12 +147,12 @@ static void wait_for_fence(ID3D12Fence *fence, UINT64 completionValue, HANDLE wa } } -void setup_swapchain(struct dx_window *window) { +void setup_swapchain() { D3D12_RESOURCE_DESC depthTexture = { .Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D, .Alignment = 0, - .Width = window->width, - .Height = window->height, + .Width = window_width, + .Height = window_height, .DepthOrArraySize = 1, .MipLevels = 1, .Format = DXGI_FORMAT_D32_FLOAT, @@ -197,12 +176,12 @@ void setup_swapchain(struct dx_window *window) { .VisibleNodeMask = 1, }; - window->current_fence_value = 0; + window_current_fence_value = 0; for (int i = 0; i < QUEUE_SLOT_COUNT; ++i) { - window->frame_fence_events[i] = CreateEvent(NULL, FALSE, FALSE, NULL); - window->fence_values[i] = 0; - device->lpVtbl->CreateFence(device, window->current_fence_value, D3D12_FENCE_FLAG_NONE, &IID_ID3D12Fence, &window->frame_fences[i]); + window_frame_fence_events[i] = CreateEvent(NULL, FALSE, FALSE, NULL); + window_fence_values[i] = 0; + device->lpVtbl->CreateFence(device, window_current_fence_value, D3D12_FENCE_FLAG_NONE, &IID_ID3D12Fence, &window_frame_fences[i]); } } @@ -307,10 +286,9 @@ void iron_gpu_internal_init() { } void iron_gpu_internal_init_window(int depthBufferBits, bool vsync) { - struct dx_window *window = &windows[0]; - window->vsync = vsync; - window->width = window->new_width = iron_window_width(); - window->height = window->new_height = iron_window_height(); + window_vsync = vsync; + window_width = window_new_width = iron_window_width(); + window_height = window_new_height = iron_window_height(); HWND hwnd = iron_windows_window_handle(); @@ -328,13 +306,13 @@ void iron_gpu_internal_init_window(int depthBufferBits, bool vsync) { IDXGIFactory4 *dxgiFactory = NULL; CreateDXGIFactory1(&IID_IDXGIFactory4, &dxgiFactory); - dxgiFactory->lpVtbl->CreateSwapChain(dxgiFactory, (IUnknown *)queue, &swapChainDesc, &window->swapChain); + dxgiFactory->lpVtbl->CreateSwapChain(dxgiFactory, (IUnknown *)queue, &swapChainDesc, &window_swapChain); - setup_swapchain(window); + setup_swapchain(); } int iron_gpu_max_bound_textures(void) { - return D3D12_COMMONSHADER_SAMPLER_SLOT_COUNT; + return IRON_INTERNAL_G5_TEXTURE_COUNT; } void iron_gpu_begin(iron_gpu_texture_t *renderTarget) { @@ -343,23 +321,22 @@ void iron_gpu_begin(iron_gpu_texture_t *renderTarget) { } began = true; - struct dx_window *window = &windows[0]; - window->current_backbuffer = (window->current_backbuffer + 1) % QUEUE_SLOT_COUNT; - if (window->new_width != window->width || window->new_height != window->height) { - window->swapChain->lpVtbl->ResizeBuffers(window->swapChain, QUEUE_SLOT_COUNT, window->new_width, window->new_height, DXGI_FORMAT_R8G8B8A8_UNORM, 0); - setup_swapchain(window); - window->width = window->new_width; - window->height = window->new_height; - window->current_backbuffer = 0; + window_current_backbuffer = (window_current_backbuffer + 1) % QUEUE_SLOT_COUNT; + if (window_new_width != window_width || window_new_height != window_height) { + window_swapChain->lpVtbl->ResizeBuffers(window_swapChain, QUEUE_SLOT_COUNT, window_new_width, window_new_height, DXGI_FORMAT_R8G8B8A8_UNORM, 0); + setup_swapchain(); + window_width = window_new_width; + window_height = window_new_height; + window_current_backbuffer = 0; } - const UINT64 fenceValue = window->current_fence_value; - queue->lpVtbl->Signal(queue, window->frame_fences[window->current_backbuffer], fenceValue); - window->fence_values[window->current_backbuffer] = fenceValue; - ++window->current_fence_value; + const UINT64 fenceValue = window_current_fence_value; + queue->lpVtbl->Signal(queue, window_frame_fences[window_current_backbuffer], fenceValue); + window_fence_values[window_current_backbuffer] = fenceValue; + ++window_current_fence_value; - wait_for_fence(window->frame_fences[window->current_backbuffer], window->fence_values[window->current_backbuffer], - window->frame_fence_events[window->current_backbuffer]); + wait_for_fence(window_frame_fences[window_current_backbuffer], window_fence_values[window_current_backbuffer], + window_frame_fence_events[window_current_backbuffer]); } void iron_gpu_end() { @@ -370,16 +347,14 @@ void iron_internal_resize(int width, int height) { if (width == 0 || height == 0) { return; } - struct dx_window *window = &windows[0]; - window->new_width = width; - window->new_height = height; + window_new_width = width; + window_new_height = height; iron_gpu_internal_resize(width, height); } bool iron_gpu_swap_buffers() { - struct dx_window *window = &windows[0]; - if (window->swapChain) { - window->swapChain->lpVtbl->Present(window->swapChain, window->vsync, 0); + if (window_swapChain) { + window_swapChain->lpVtbl->Present(window_swapChain, window_vsync, 0); } return true; } @@ -507,7 +482,7 @@ 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_buffer_t *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_constant_buffer(struct iron_gpu_command_list *list, iron_gpu_buffer_t *buffer, int offset, size_t size) { list->impl._commandList->lpVtbl->SetGraphicsRootConstantBufferView(list->impl._commandList, 1, buffer->impl.constant_buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.constant_buffer) + offset); } @@ -823,23 +798,13 @@ void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *lis } void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture) { - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - texture->impl.stage = unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]; - } - else if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - texture->impl.stage = unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]; - } + texture->impl.stage = unit.offset; list->impl.currentTextures[texture->impl.stage] = texture; iron_gpu_internal_set_textures(list); } void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture) { - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - texture->impl.stage_depth = unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]; - } - else if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - texture->impl.stage_depth = unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]; - } + texture->impl.stage_depth = unit.offset; list->impl.currentTextures[texture->impl.stage_depth] = texture; } @@ -861,9 +826,7 @@ iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(struct iron iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *pipe, const char *name) { iron_gpu_texture_unit_t unit; - for (int i = 0; i < IRON_GPU_SHADER_TYPE_COUNT; ++i) { - unit.stages[i] = -1; - } + unit.offset = -1; return unit; } @@ -1014,10 +977,6 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipe) { } void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *_data, size_t length, iron_gpu_shader_type_t type) { - memset(shader->impl.constants, 0, sizeof(shader->impl.constants)); - memset(shader->impl.attributes, 0, sizeof(shader->impl.attributes)); - memset(shader->impl.textures, 0, sizeof(shader->impl.textures)); - uint8_t *data = (uint8_t *)_data; shader->impl.length = (int)length; shader->impl.data = (uint8_t *)malloc(shader->impl.length); @@ -1359,7 +1318,7 @@ static void render_target_init(iron_gpu_texture_t *render_target, int width, int device->lpVtbl->CreateDescriptorHeap(device, &heapDesc, &IID_ID3D12DescriptorHeap, &render_target->impl.renderTargetDescriptorHeap); if (framebuffer_index >= 0) { - IDXGISwapChain *swapChain = windows[0].swapChain; + IDXGISwapChain *swapChain = window_swapChain; swapChain->lpVtbl->GetBuffer(swapChain, framebuffer_index, &IID_ID3D12Resource, &render_target->impl.renderTarget); wchar_t buffer[128]; wsprintf(buffer, L"Backbuffer (index %i)", framebuffer_index); diff --git a/base/sources/backends/direct3d12_gpu.h b/base/sources/backends/direct3d12_gpu.h index 7841070e..3b48fcf0 100644 --- a/base/sources/backends/direct3d12_gpu.h +++ b/base/sources/backends/direct3d12_gpu.h @@ -40,34 +40,16 @@ struct D3D12Rect { long bottom; }; -struct dx_window { - struct IDXGISwapChain *swapChain; - UINT64 current_fence_value; - UINT64 fence_values[QUEUE_SLOT_COUNT]; - HANDLE frame_fence_events[QUEUE_SLOT_COUNT]; - struct ID3D12Fence *frame_fences[QUEUE_SLOT_COUNT]; - int width; - int height; - int new_width; - int new_height; - int current_backbuffer; - bool vsync; -}; - typedef struct { struct ID3D12CommandAllocator *_commandAllocator; struct ID3D12GraphicsCommandList *_commandList; struct iron_gpu_pipeline *_currentPipeline; int _indexCount; - struct D3D12Rect current_full_scissor; - uint64_t fence_value; struct ID3D12Fence *fence; HANDLE fence_event; - struct iron_gpu_texture *currentTextures[IRON_INTERNAL_G5_TEXTURE_COUNT]; - int heapIndex; struct ID3D12DescriptorHeap *srvHeap; } gpu_command_list_impl_t; @@ -97,9 +79,6 @@ typedef struct { } ShaderTexture; typedef struct { - ShaderConstant constants[32]; - ShaderAttribute attributes[32]; - ShaderTexture textures[32]; int texturesCount; void *shader; uint8_t *data; @@ -113,7 +92,6 @@ typedef struct { struct ID3D12Resource *image; struct ID3D12Resource *uploadImage; struct ID3D12DescriptorHeap *srvDescriptorHeap; - struct ID3D12Resource *renderTarget; struct ID3D12Resource *renderTargetReadback; struct ID3D12DescriptorHeap *renderTargetDescriptorHeap; @@ -122,7 +100,6 @@ typedef struct { struct ID3D12Resource *depthStencilTexture; struct D3D12Viewport viewport; struct D3D12Rect scissor; - int stage_depth; int framebuffer_index; } gpu_texture_impl_t; @@ -146,7 +123,6 @@ typedef struct { int myStride; int lastStart; int lastCount; - struct ID3D12Resource *index_buffer; struct D3D12IindexBufferView index_buffer_view; struct ID3D12Resource *upload_buffer; @@ -154,7 +130,6 @@ typedef struct { bool gpu_memory; int last_start; int last_count; - struct ID3D12Resource *constant_buffer; int mySize; } gpu_buffer_impl_t; diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index f535ff78..40385acd 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -455,7 +455,7 @@ 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_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_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]; @@ -472,23 +472,15 @@ void iron_gpu_command_list_texture_to_render_target_barrier(iron_gpu_command_lis void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture) { id tex = (__bridge id)texture->impl._tex; - if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - [render_command_encoder setVertexTexture:tex atIndex:unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]]; - } - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - [render_command_encoder setFragmentTexture:tex atIndex:unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]]; - } + [render_command_encoder setVertexTexture:tex atIndex:unit.offset]; + [render_command_encoder setFragmentTexture:tex atIndex:unit.offset]; } void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *target) { id encoder = getMetalEncoder(); id depth_tex = (__bridge id)target->impl._depthTex; - if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - [encoder setVertexTexture:depth_tex atIndex:unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]]; - } - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - [encoder setFragmentTexture:depth_tex atIndex:unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]]; - } + [encoder setVertexTexture:depth_tex atIndex:unit.offset]; + [encoder setFragmentTexture:depth_tex atIndex:unit.offset]; } static MTLBlendFactor convert_blending_factor(iron_gpu_blending_factor_t factor) { @@ -802,25 +794,7 @@ iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pi iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *pipeline, const char *name) { iron_gpu_texture_unit_t unit = {0}; - for (int i = 0; i < IRON_GPU_SHADER_TYPE_COUNT; ++i) { - unit.stages[i] = -1; - } - - MTLRenderPipelineReflection *reflection = (__bridge MTLRenderPipelineReflection *)pipeline->impl._reflection; - for (MTLArgument *arg in reflection.fragmentArguments) { - if ([arg type] == MTLArgumentTypeTexture && strcmp([[arg name] UTF8String], name) == 0) { - unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] = (int)[arg index]; - break; - } - } - - for (MTLArgument *arg in reflection.vertexArguments) { - if ([arg type] == MTLArgumentTypeTexture && strcmp([[arg name] UTF8String], name) == 0) { - unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] = (int)[arg index]; - break; - } - } - + unit.offset = -1; return unit; } diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index bedbdffa..531712ac 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -2005,7 +2005,7 @@ VkDescriptorSet get_descriptor_set() { return descriptor_set; } -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_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { last_vertex_constant_buffer_offset = offset; last_fragment_constant_buffer_offset = offset; @@ -2054,25 +2054,13 @@ void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t } void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture) { - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - texture->impl.stage = unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]; - vulkan_textures[unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]] = texture; - } - else if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - texture->impl.stage = unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]; - vulkan_textures[unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]] = texture; - } + texture->impl.stage = unit.offset; + vulkan_textures[unit.offset] = texture; } void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *target) { - if (unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] >= 0) { - target->impl.stage_depth = unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]; - vulkan_textures[unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT]] = target; - } - else if (unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] >= 0) { - target->impl.stage_depth = unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]; - vulkan_textures[unit.stages[IRON_GPU_SHADER_TYPE_VERTEX]] = target; - } + target->impl.stage_depth = unit.offset; + vulkan_textures[unit.offset] = target; } static bool has_number(iron_internal_named_number *named_numbers, const char *name) { @@ -2319,21 +2307,7 @@ iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pi iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *pipeline, const char *name) { iron_gpu_texture_unit_t unit; - for (int i = 0; i < IRON_GPU_SHADER_TYPE_COUNT; ++i) { - unit.stages[i] = -1; - } - - int number = find_number(pipeline->impl.vertexTextureBindings, name); - if (number >= 0) { - unit.stages[IRON_GPU_SHADER_TYPE_VERTEX] = number - 2; - } - else { - number = find_number(pipeline->impl.fragmentTextureBindings, name); - if (number >= 0) { - unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] = number - 2; - } - } - + unit.offset = -1; return unit; } diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index 0b76ecef..f5699807 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -452,7 +452,7 @@ 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_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { } diff --git a/base/sources/iron.h b/base/sources/iron.h index ca4529dd..1f4a5d4d 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -325,7 +325,6 @@ string_t *iron_get_arg(i32 index) { #include #ifdef IRON_DIRECT3D12 #include -extern bool waitAfterNextDraw; #endif #ifdef WITH_D3DCOMPILER #include @@ -959,10 +958,6 @@ buffer_t *gpu_lock_vertex_buffer(iron_gpu_buffer_t *buffer) { } void gpu_draw_indexed_vertices(i32 start, i32 count) { - #ifdef IRON_DIRECT3D12 - // TODO: Prevent heapIndex overflow in iron_gpu_internal_set_textures - waitAfterNextDraw = true; - #endif if (count < 0) { iron_gpu_draw_indexed_vertices(); } diff --git a/base/sources/iron_draw.c b/base/sources/iron_draw.c index 96b0cf66..1ebe29a3 100644 --- a/base/sources/iron_draw.c +++ b/base/sources/iron_draw.c @@ -9,9 +9,6 @@ #include "iron_string.h" #include "iron_vec2.h" #include "iron_gc.h" -#ifdef IRON_DIRECT3D12 -extern bool waitAfterNextDraw; -#endif #define MATH_PI 3.14159265358979323846 #define DRAW_BUFFER_SIZE 1000 @@ -157,7 +154,7 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *colored_ver iron_gpu_pipeline_compile(&image_pipeline); image_tex_unit = iron_gpu_pipeline_get_texture_unit(&image_pipeline, "tex"); - image_tex_unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] = 0; + image_tex_unit.offset = 0; image_proj_loc = iron_gpu_pipeline_get_constant_location(&image_pipeline, "P"); image_proj_loc.impl.vertexOffset = 0; @@ -253,7 +250,7 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *colored_ver iron_gpu_pipeline_compile(&text_pipeline); text_tex_unit = iron_gpu_pipeline_get_texture_unit(&text_pipeline, "tex"); - text_tex_unit.stages[IRON_GPU_SHADER_TYPE_FRAGMENT] = 0; + text_tex_unit.offset = 0; text_proj_loc = iron_gpu_pipeline_get_constant_location(&text_pipeline, "P"); text_proj_loc.impl.vertexOffset = 0; @@ -402,9 +399,6 @@ void draw_texture(iron_gpu_texture_t *tex, float x, float y) { } void draw_scaled_sub_image(iron_gpu_texture_t *image, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh) { - #ifdef IRON_DIRECT3D12 - waitAfterNextDraw = true; - #endif draw_scaled_sub_texture(image, sx, sy, sw, sh, dx, dy, dw, dh); } diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index 29b74b00..fedb0939 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -6,260 +6,119 @@ #include "iron_math.h" #include "iron_file.h" -#define CONSTANT_BUFFER_SIZE 4096 -#define CONSTANT_BUFFER_MULTIPLY 100 +#define CONSTANT_BUFFER_SIZE 256 +#define CONSTANT_BUFFER_MULTIPLE 1024 #define FRAMEBUFFER_COUNT 2 #define MAX_TEXTURES 16 iron_gpu_command_list_t commandList; -bool waitAfterNextDraw = false; - static iron_gpu_buffer_t vertexConstantBuffer; static int constantBufferIndex = 0; - -static struct { - int currentBuffer; - iron_gpu_texture_t framebuffers[FRAMEBUFFER_COUNT]; - iron_gpu_texture_t *current_render_targets[8]; - int current_render_target_count; - bool resized; -} windows[1] = {0}; - -typedef struct render_state { - iron_gpu_pipeline_t *pipeline; - - iron_gpu_buffer_t *index_buffer; - iron_gpu_buffer_t *vertex_buffer; - - bool viewport_set; - int viewport_x; - int viewport_y; - int viewport_width; - int viewport_height; - - bool scissor_set; - int scissor_x; - int scissor_y; - int scissor_width; - int scissor_height; - - iron_gpu_texture_t *textures[MAX_TEXTURES]; - iron_gpu_texture_unit_t texture_units[MAX_TEXTURES]; - int texture_count; - - iron_gpu_texture_t *depth_render_targets[MAX_TEXTURES]; - iron_gpu_texture_unit_t depth_render_target_units[MAX_TEXTURES]; - int depth_render_target_count; - - uint8_t vertex_constant_data[CONSTANT_BUFFER_SIZE]; -} render_state; - -static render_state current_state; - -bool iron_gpu_texture_unit_equals(iron_gpu_texture_unit_t *unit1, iron_gpu_texture_unit_t *unit2) { - for (int i = 0; i < IRON_GPU_SHADER_TYPE_COUNT; ++i) { - if (unit1->stages[i] != unit2->stages[i]) { - return false; - } - } - return true; -} +static int window_currentBuffer; +static iron_gpu_texture_t window_framebuffers[FRAMEBUFFER_COUNT]; +static iron_gpu_texture_t *window_current_render_targets[8]; +static int window_current_render_target_count; +static bool window_resized = false; void iron_gpu_internal_resize(int width, int height) { - windows[0].resized = true; -} - -void iron_gpu_internal_restore_render_target(void) { - windows[0].current_render_targets[0] = NULL; - iron_gpu_texture_t *render_target = &windows[0].framebuffers[windows[0].currentBuffer]; - iron_gpu_command_list_set_render_targets(&commandList, &render_target, 1); - windows[0].current_render_target_count = 1; + window_resized = true; } void gpu_internal_init_window(int depthBufferBits, bool vsync) { iron_gpu_internal_init_window(depthBufferBits, vsync); - iron_gpu_command_list_init(&commandList); - windows[0].currentBuffer = -1; + window_currentBuffer = -1; for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - iron_gpu_render_target_init_framebuffer(&windows[0].framebuffers[i], iron_window_width(), iron_window_height(), - IRON_IMAGE_FORMAT_RGBA32, depthBufferBits); + iron_gpu_render_target_init_framebuffer(&window_framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, depthBufferBits); } - iron_gpu_constant_buffer_init(&vertexConstantBuffer, CONSTANT_BUFFER_SIZE * CONSTANT_BUFFER_MULTIPLY); - - // to support doing work after gpu_end and before gpu_begin - iron_gpu_command_list_begin(&commandList); + iron_gpu_constant_buffer_init(&vertexConstantBuffer, CONSTANT_BUFFER_SIZE * CONSTANT_BUFFER_MULTIPLE); } -static void iron_internal_start_draw(bool compute) { - if ((constantBufferIndex + 1) >= CONSTANT_BUFFER_MULTIPLY || waitAfterNextDraw) { - memcpy(current_state.vertex_constant_data, vertexConstantBuffer.data, CONSTANT_BUFFER_SIZE); - } +static void iron_internal_start_draw() { iron_gpu_constant_buffer_unlock(&vertexConstantBuffer); - iron_gpu_command_list_set_vertex_constant_buffer(&commandList, &vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); + iron_gpu_command_list_set_constant_buffer(&commandList, &vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); } -static void iron_internal_end_draw(bool compute) { +static void iron_internal_end_draw() { ++constantBufferIndex; - if (constantBufferIndex >= CONSTANT_BUFFER_MULTIPLY || waitAfterNextDraw) { - iron_gpu_command_list_end(&commandList); - iron_gpu_command_list_execute(&commandList); - iron_gpu_command_list_wait_for_execution_to_finish(&commandList); - iron_gpu_command_list_begin(&commandList); - if (windows[0].current_render_targets[0] == NULL) { - iron_gpu_internal_restore_render_target(); - } - else { - const int count = windows[0].current_render_target_count; - iron_gpu_texture_t *render_targets[16]; - for (int i = 0; i < count; ++i) { - render_targets[i] = windows[0].current_render_targets[i]; - } - iron_gpu_command_list_set_render_targets(&commandList, render_targets, count); - } - - if (current_state.pipeline != NULL) { - iron_gpu_command_list_set_pipeline(&commandList, current_state.pipeline); - } - if (current_state.index_buffer != NULL) { - iron_gpu_command_list_set_index_buffer(&commandList, current_state.index_buffer); - } - if (current_state.vertex_buffer != NULL) { - iron_gpu_command_list_set_vertex_buffer(&commandList, current_state.vertex_buffer); - } - if (current_state.viewport_set) { - iron_gpu_command_list_viewport(&commandList, current_state.viewport_x, current_state.viewport_y, current_state.viewport_width, - current_state.viewport_height); - } - if (current_state.scissor_set) { - iron_gpu_command_list_scissor(&commandList, current_state.scissor_x, current_state.scissor_y, current_state.scissor_width, - current_state.scissor_height); - } - for (int i = 0; i < current_state.texture_count; ++i) { - iron_gpu_command_list_set_texture(&commandList, current_state.texture_units[i], current_state.textures[i]); - } - for (int i = 0; i < current_state.depth_render_target_count; ++i) { - iron_gpu_command_list_set_texture_from_render_target_depth(&commandList, current_state.depth_render_target_units[i], - current_state.depth_render_targets[i]); - } - constantBufferIndex = 0; - waitAfterNextDraw = false; - - iron_gpu_constant_buffer_lock(&vertexConstantBuffer, 0, CONSTANT_BUFFER_SIZE); - - memcpy(vertexConstantBuffer.data, current_state.vertex_constant_data, CONSTANT_BUFFER_SIZE); - } - else { - iron_gpu_constant_buffer_lock(&vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); - } + iron_gpu_constant_buffer_lock(&vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); } void iron_gpu_draw_indexed_vertices(void) { - iron_internal_start_draw(false); + iron_internal_start_draw(); iron_gpu_command_list_draw_indexed_vertices(&commandList); - iron_internal_end_draw(false); + iron_internal_end_draw(); } void iron_gpu_draw_indexed_vertices_from_to(int start, int count) { - iron_internal_start_draw(false); + iron_internal_start_draw(); iron_gpu_command_list_draw_indexed_vertices_from_to(&commandList, start, count); - iron_internal_end_draw(false); + iron_internal_end_draw(); } void iron_gpu_clear(unsigned color, float depth, unsigned flags) { - if (windows[0].current_render_target_count > 0) { - if (windows[0].current_render_targets[0] == NULL) { - iron_gpu_command_list_clear(&commandList, &windows[0].framebuffers[windows[0].currentBuffer], flags, color, depth); + if (window_current_render_target_count > 0) { + if (window_current_render_targets[0] == NULL) { + iron_gpu_command_list_clear(&commandList, &window_framebuffers[window_currentBuffer], flags, color, depth); } else { - if (windows[0].current_render_targets[0]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { - iron_gpu_command_list_texture_to_render_target_barrier(&commandList, windows[0].current_render_targets[0]); - windows[0].current_render_targets[0]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; + if (window_current_render_targets[0]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { + iron_gpu_command_list_texture_to_render_target_barrier(&commandList, window_current_render_targets[0]); + window_current_render_targets[0]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; } - iron_gpu_command_list_clear(&commandList, windows[0].current_render_targets[0], flags, color, depth); + iron_gpu_command_list_clear(&commandList, window_current_render_targets[0], flags, color, depth); } } } void gpu_begin() { - // to support doing work after gpu_end and before gpu_begin - iron_gpu_command_list_end(&commandList); - iron_gpu_command_list_execute(&commandList); + constantBufferIndex = 0; + iron_gpu_constant_buffer_lock(&vertexConstantBuffer, 0, CONSTANT_BUFFER_SIZE); - windows[0].currentBuffer = (windows[0].currentBuffer + 1) % FRAMEBUFFER_COUNT; + window_currentBuffer = (window_currentBuffer + 1) % FRAMEBUFFER_COUNT; - bool resized = windows[0].resized; - if (resized) { - for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - iron_gpu_texture_destroy(&windows[0].framebuffers[i]); - } - windows[0].currentBuffer = 0; - } + // if (window_resized) { + // for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { + // iron_gpu_texture_destroy(&window_framebuffers[i]); + // } + // window_currentBuffer = 0; + // } - iron_gpu_begin(&windows[0].framebuffers[windows[0].currentBuffer]); + iron_gpu_begin(&window_framebuffers[window_currentBuffer]); - if (resized) { - for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - iron_gpu_render_target_init_framebuffer(&windows[0].framebuffers[i], iron_window_width(), iron_window_height(), - IRON_IMAGE_FORMAT_RGBA32, 16); - } - windows[0].resized = false; - } - - windows[0].current_render_targets[0] = NULL; - windows[0].current_render_target_count = 1; - - current_state.pipeline = NULL; - current_state.index_buffer = NULL; - current_state.vertex_buffer = NULL; - current_state.viewport_set = false; - current_state.scissor_set = false; - current_state.texture_count = 0; - current_state.depth_render_target_count = 0; + // if (window_resized) { + // for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { + // iron_gpu_render_target_init_framebuffer(&window_framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, 0); + // } + // window_resized = false; + // } + window_current_render_targets[0] = NULL; + window_current_render_target_count = 1; iron_gpu_command_list_begin(&commandList); - - // Currently we do not necessarily wait at the end of a frame so for now it's iron_internal_end_draw - iron_internal_end_draw(false); - - iron_gpu_command_list_framebuffer_to_render_target_barrier(&commandList, &windows[0].framebuffers[windows[0].currentBuffer]); + iron_gpu_command_list_framebuffer_to_render_target_barrier(&commandList, &window_framebuffers[window_currentBuffer]); gpu_restore_render_target(); } -void gpu_viewport(int x, int y, int width, int height) { - current_state.viewport_x = x; - current_state.viewport_y = y; - current_state.viewport_width = width; - current_state.viewport_height = height; - current_state.viewport_set = true; - iron_gpu_command_list_viewport(&commandList, x, y, width, height); -} - -void gpu_scissor(int x, int y, int width, int height) { - current_state.scissor_x = x; - current_state.scissor_y = y; - current_state.scissor_width = width; - current_state.scissor_height = height; - current_state.scissor_set = true; - iron_gpu_command_list_scissor(&commandList, x, y, width, height); -} - -void gpu_disable_scissor(void) { - current_state.scissor_set = false; - iron_gpu_command_list_disable_scissor(&commandList); -} - void gpu_end() { iron_gpu_constant_buffer_unlock(&vertexConstantBuffer); - - iron_gpu_command_list_render_target_to_framebuffer_barrier(&commandList, &windows[0].framebuffers[windows[0].currentBuffer]); + iron_gpu_command_list_render_target_to_framebuffer_barrier(&commandList, &window_framebuffers[window_currentBuffer]); iron_gpu_command_list_end(&commandList); iron_gpu_command_list_execute(&commandList); - + iron_gpu_command_list_wait_for_execution_to_finish(&commandList); iron_gpu_end(); +} - // to support doing work after gpu_end and before gpu_begin - iron_gpu_command_list_begin(&commandList); +void gpu_viewport(int x, int y, int width, int height) { + iron_gpu_command_list_viewport(&commandList, x, y, width, height); +} + +void gpu_scissor(int x, int y, int width, int height) { + iron_gpu_command_list_scissor(&commandList, x, y, width, height); +} + +void gpu_disable_scissor(void) { + iron_gpu_command_list_disable_scissor(&commandList); } void gpu_set_int(iron_gpu_constant_location_t *location, int value) { @@ -351,17 +210,6 @@ static void iron_internal_set_matrix4(uint8_t *constants, int offset, iron_matri } } -void gpu_set_matrix4(iron_gpu_constant_location_t *location, iron_matrix4x4_t value) { - if (iron_gpu_transpose_mat) { - iron_matrix4x4_t m = value; - iron_matrix4x4_transpose(&m); - iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &m); - } - else { - iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &value); - } -} - void gpu_set_matrix3(iron_gpu_constant_location_t *location, iron_matrix3x3_t value) { if (iron_gpu_transpose_mat) { iron_matrix3x3_t m = value; @@ -373,42 +221,49 @@ void gpu_set_matrix3(iron_gpu_constant_location_t *location, iron_matrix3x3_t va } } +void gpu_set_matrix4(iron_gpu_constant_location_t *location, iron_matrix4x4_t value) { + if (iron_gpu_transpose_mat) { + iron_matrix4x4_t m = value; + iron_matrix4x4_transpose(&m); + iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &m); + } + else { + iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &value); + } +} + void gpu_restore_render_target(void) { - iron_gpu_internal_restore_render_target(); - current_state.viewport_set = false; - current_state.scissor_set = false; + window_current_render_targets[0] = NULL; + iron_gpu_texture_t *render_target = &window_framebuffers[window_currentBuffer]; + iron_gpu_command_list_set_render_targets(&commandList, &render_target, 1); + window_current_render_target_count = 1; } void gpu_set_render_targets(iron_gpu_texture_t **targets, int count) { for (int i = 0; i < count; ++i) { - windows[0].current_render_targets[i] = targets[i]; - if (windows[0].current_render_targets[i]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { - iron_gpu_command_list_texture_to_render_target_barrier(&commandList, windows[0].current_render_targets[i]); - windows[0].current_render_targets[i]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; + window_current_render_targets[i] = targets[i]; + if (window_current_render_targets[i]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { + iron_gpu_command_list_texture_to_render_target_barrier(&commandList, window_current_render_targets[i]); + window_current_render_targets[i]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; } } - windows[0].current_render_target_count = count; + window_current_render_target_count = count; iron_gpu_texture_t *render_targets[16]; for (int i = 0; i < count; ++i) { render_targets[i] = targets[i]; } iron_gpu_command_list_set_render_targets(&commandList, render_targets, count); - current_state.viewport_set = false; - current_state.scissor_set = false; } 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_buffer_t *buffer) { - current_state.index_buffer = buffer; iron_gpu_command_list_set_index_buffer(&commandList, buffer); } void iron_gpu_set_pipeline(iron_gpu_pipeline_t *pipeline) { - current_state.pipeline = pipeline; iron_gpu_command_list_set_pipeline(&commandList, pipeline); } @@ -417,27 +272,10 @@ void gpu_set_texture(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_t 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; } - - bool found = false; - for (int i = 0; i < current_state.texture_count; ++i) { - if (iron_gpu_texture_unit_equals(¤t_state.texture_units[i], unit)) { - current_state.textures[i] = render_target; - 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] = *unit; - current_state.texture_count += 1; - } - iron_gpu_command_list_set_texture(&commandList, *unit, render_target); } @@ -446,23 +284,6 @@ void gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *re iron_gpu_command_list_render_target_to_texture_barrier(&commandList, render_target); render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; } - - 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], unit)) { - current_state.depth_render_targets[i] = render_target; - current_state.depth_render_target_units[i] = *unit; - found = true; - break; - } - } - 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] = *unit; - current_state.depth_render_target_count += 1; - } - iron_gpu_command_list_set_texture_from_render_target_depth(&commandList, *unit, render_target); } @@ -496,12 +317,10 @@ void gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_verte } float *gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { - waitAfterNextDraw = true; return iron_gpu_vertex_buffer_lock_all(buffer); } 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); } @@ -509,18 +328,15 @@ void iron_gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipe) { pipe->input_layout = NULL; pipe->vertex_shader = NULL; pipe->fragment_shader = NULL; - pipe->cull_mode = IRON_GPU_CULL_MODE_NEVER; pipe->depth_write = false; pipe->depth_mode = IRON_GPU_COMPARE_MODE_ALWAYS; - pipe->blend_source = IRON_GPU_BLEND_ONE; pipe->blend_destination = IRON_GPU_BLEND_ZERO; pipe->blend_operation = IRON_GPU_BLENDOP_ADD; pipe->alpha_blend_source = IRON_GPU_BLEND_ONE; pipe->alpha_blend_destination = IRON_GPU_BLEND_ZERO; pipe->alpha_blend_operation = IRON_GPU_BLENDOP_ADD; - for (int i = 0; i < 8; ++i) { pipe->color_write_mask_red[i] = true; pipe->color_write_mask_green[i] = true; @@ -528,7 +344,6 @@ void iron_gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipe) { pipe->color_write_mask_alpha[i] = true; pipe->color_attachment[i] = IRON_IMAGE_FORMAT_RGBA32; } - pipe->color_attachment_count = 1; pipe->depth_attachment_bits = 0; } diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index d221c127..280b98a8 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -11,11 +11,12 @@ #define IRON_GPU_CLEAR_COLOR 1 #define IRON_GPU_CLEAR_DEPTH 2 +#define IRON_GPU_MAX_VERTEX_ELEMENTS 16 -enum iron_internal_render_target_state { +typedef enum iron_internal_render_target_state { IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET, IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE -}; +} iron_internal_render_target_state_t; typedef enum iron_image_compression { IRON_IMAGE_COMPRESSION_NONE, @@ -33,199 +34,25 @@ typedef enum iron_image_format { IRON_IMAGE_FORMAT_BGRA32 } iron_image_format_t; -typedef struct iron_gpu_texture { - int width; - int height; - - iron_image_format_t format; - iron_image_compression_t compression; - void *data; - bool _uploaded; - - int framebuffer_index; - bool isDepthAttachment; - enum iron_internal_render_target_state state; - - buffer_t *buffer; - - gpu_texture_impl_t impl; -} iron_gpu_texture_t; - -typedef struct iron_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_begin(iron_gpu_texture_t *renderTarget); -void iron_gpu_end(void); -bool iron_gpu_swap_buffers(void); - -void iron_gpu_internal_init(void); -void iron_gpu_internal_init_window(int depth_buffer_bits, bool vsync); -void iron_gpu_internal_destroy_window(void); -void iron_gpu_internal_destroy(void); - -struct iron_gpu_pipeline; -struct iron_gpu_texture; -struct iron_gpu_texture_unit; -struct iron_gpu_constant_location; -struct iron_gpu_buffer; - typedef enum { GPU_USAGE_STATIC, GPU_USAGE_DYNAMIC, GPU_USAGE_READABLE } gpu_usage_t; -void gpu_begin(); -void gpu_end(); -void iron_gpu_clear(unsigned color, float depth, unsigned flags); -void gpu_viewport(int x, int y, int width, int height); -void gpu_scissor(int x, int y, int width, int height); -void gpu_disable_scissor(void); -void iron_gpu_draw_indexed_vertices(void); -void iron_gpu_draw_indexed_vertices_from_to(int start, int count); -void iron_gpu_set_pipeline(struct iron_gpu_pipeline *pipeline); -void gpu_set_int(struct iron_gpu_constant_location *location, int value); -void gpu_set_int2(struct iron_gpu_constant_location *location, int value1, int value2); -void gpu_set_int3(struct iron_gpu_constant_location *location, int value1, int value2, int value3); -void gpu_set_int4(struct iron_gpu_constant_location *location, int value1, int value2, int value3, int value4); -void gpu_set_ints(struct iron_gpu_constant_location *location, int *values, int count); -void gpu_set_float(struct iron_gpu_constant_location *location, float value); -void gpu_set_float2(struct iron_gpu_constant_location *location, float value1, float value2); -void gpu_set_float3(struct iron_gpu_constant_location *location, float value1, float value2, float value3); -void gpu_set_float4(struct iron_gpu_constant_location *location, float value1, float value2, float value3, float value4); -void gpu_set_floats(struct iron_gpu_constant_location *location, f32_array_t *values); -void gpu_set_bool(struct iron_gpu_constant_location *location, bool value); -void gpu_set_matrix3(struct iron_gpu_constant_location *location, iron_matrix3x3_t value); -void gpu_set_matrix4(struct iron_gpu_constant_location *location, iron_matrix4x4_t value); -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(iron_gpu_buffer_t *buffer); - -void gpu_internal_init_window(int depth_buffer_bits, bool vsync); - -void gpu_set_texture_depth(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *renderTarget); - -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 - typedef enum iron_gpu_vertex_data { - IRON_GPU_VERTEX_DATA_F32_1X = 0, - IRON_GPU_VERTEX_DATA_F32_2X = 1, - IRON_GPU_VERTEX_DATA_F32_3X = 2, - IRON_GPU_VERTEX_DATA_F32_4X = 3, - IRON_GPU_VERTEX_DATA_U8_4X_NORM = 4, - IRON_GPU_VERTEX_DATA_I16_2X_NORM = 5, - IRON_GPU_VERTEX_DATA_I16_4X_NORM = 6, + IRON_GPU_VERTEX_DATA_F32_1X, + IRON_GPU_VERTEX_DATA_F32_2X, + IRON_GPU_VERTEX_DATA_F32_3X, + IRON_GPU_VERTEX_DATA_F32_4X, + IRON_GPU_VERTEX_DATA_U8_4X_NORM, + IRON_GPU_VERTEX_DATA_I16_2X_NORM, + IRON_GPU_VERTEX_DATA_I16_4X_NORM, } iron_gpu_vertex_data_t; -typedef struct iron_gpu_vertex_element { - const char *name; - iron_gpu_vertex_data_t data; -} iron_gpu_vertex_element_t; - -typedef struct iron_gpu_vertex_structure { - iron_gpu_vertex_element_t elements[IRON_GPU_MAX_VERTEX_ELEMENTS]; - int size; -} iron_gpu_vertex_structure_t; - -static inline int iron_gpu_vertex_data_size(iron_gpu_vertex_data_t data) { - switch (data) { - case IRON_GPU_VERTEX_DATA_F32_1X: - return 1 * 4; - case IRON_GPU_VERTEX_DATA_F32_2X: - return 2 * 4; - case IRON_GPU_VERTEX_DATA_F32_3X: - return 3 * 4; - case IRON_GPU_VERTEX_DATA_F32_4X: - return 4 * 4; - case IRON_GPU_VERTEX_DATA_U8_4X_NORM: - return 4 * 1; - case IRON_GPU_VERTEX_DATA_I16_2X_NORM: - return 2 * 2; - case IRON_GPU_VERTEX_DATA_I16_4X_NORM: - return 4 * 2; - } -} - -static inline int iron_gpu_vertex_struct_size(iron_gpu_vertex_structure_t *s) { - int size = 0; - for (int i = 0; i < s->size; ++i) { - size += iron_gpu_vertex_data_size(s->elements[i].data); - } - return size; -} - -void iron_gpu_vertex_structure_init(iron_gpu_vertex_structure_t *structure); -void iron_gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data); - -void iron_gpu_texture_init(iron_gpu_texture_t *texture, int width, int height, iron_image_format_t format); -void iron_gpu_texture_init_from_bytes(iron_gpu_texture_t *texture, void *data, int width, int height, iron_image_format_t format); -void iron_gpu_texture_destroy(iron_gpu_texture_t *texture); -void iron_gpu_texture_generate_mipmaps(iron_gpu_texture_t *texture, int levels); -void iron_gpu_texture_set_mipmap(iron_gpu_texture_t *texture, iron_gpu_texture_t *mipmap, int level); -int iron_gpu_texture_stride(iron_gpu_texture_t *texture); - -struct iron_gpu_texture_unit; - -void iron_gpu_render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits); -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); - -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 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 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; - -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, IRON_GPU_SHADER_TYPE_VERTEX, + IRON_GPU_SHADER_TYPE_FRAGMENT, IRON_GPU_SHADER_TYPE_COUNT } iron_gpu_shader_type_t; @@ -235,21 +62,11 @@ typedef enum { IRON_GPU_BLEND_SOURCE_ALPHA, IRON_GPU_BLEND_DEST_ALPHA, IRON_GPU_BLEND_INV_SOURCE_ALPHA, - IRON_GPU_BLEND_INV_DEST_ALPHA, - IRON_GPU_BLEND_SOURCE_COLOR, - IRON_GPU_BLEND_DEST_COLOR, - IRON_GPU_BLEND_INV_SOURCE_COLOR, - IRON_GPU_BLEND_INV_DEST_COLOR, - IRON_GPU_BLEND_CONSTANT, - IRON_GPU_BLEND_INV_CONSTANT + IRON_GPU_BLEND_INV_DEST_ALPHA } iron_gpu_blending_factor_t; typedef enum { - IRON_GPU_BLENDOP_ADD, - IRON_GPU_BLENDOP_SUBTRACT, - IRON_GPU_BLENDOP_REVERSE_SUBTRACT, - IRON_GPU_BLENDOP_MIN, - IRON_GPU_BLENDOP_MAX + IRON_GPU_BLENDOP_ADD } iron_gpu_blending_operation_t; typedef enum iron_gpu_cull_mode { @@ -261,20 +78,45 @@ typedef enum iron_gpu_cull_mode { typedef enum iron_gpu_compare_mode { IRON_GPU_COMPARE_MODE_ALWAYS, IRON_GPU_COMPARE_MODE_NEVER, - IRON_GPU_COMPARE_MODE_EQUAL, - IRON_GPU_COMPARE_MODE_NOT_EQUAL, - IRON_GPU_COMPARE_MODE_LESS, - IRON_GPU_COMPARE_MODE_LESS_EQUAL, - IRON_GPU_COMPARE_MODE_GREATER, - IRON_GPU_COMPARE_MODE_GREATER_EQUAL + IRON_GPU_COMPARE_MODE_LESS } iron_gpu_compare_mode_t; +typedef struct iron_gpu_texture { + int width; + int height; + iron_image_format_t format; + iron_image_compression_t compression; + void *data; + bool _uploaded; + int framebuffer_index; + bool isDepthAttachment; + iron_internal_render_target_state_t state; + buffer_t *buffer; + gpu_texture_impl_t impl; +} iron_gpu_texture_t; + +typedef struct iron_gpu_buffer { + int myCount; + uint8_t *data; + gpu_buffer_impl_t impl; +} iron_gpu_buffer_t; + +typedef struct iron_gpu_vertex_element { + const char *name; + iron_gpu_vertex_data_t data; +} iron_gpu_vertex_element_t; + +typedef struct iron_gpu_vertex_structure { + iron_gpu_vertex_element_t elements[IRON_GPU_MAX_VERTEX_ELEMENTS]; + int size; +} iron_gpu_vertex_structure_t; + typedef struct iron_gpu_constant_location { gpu_constant_location_impl_t impl; } iron_gpu_constant_location_t; typedef struct iron_gpu_texture_unit { - int stages[IRON_GPU_SHADER_TYPE_COUNT]; + int offset; } iron_gpu_texture_unit_t; typedef struct iron_gpu_shader { @@ -310,25 +152,123 @@ typedef struct iron_gpu_pipeline { gpu_pipeline_impl_t impl; } iron_gpu_pipeline_t; +typedef struct iron_gpu_command_list { + gpu_command_list_impl_t impl; +} iron_gpu_command_list_t; + +typedef struct iron_gpu_raytrace_pipeline { + iron_gpu_buffer_t *_constant_buffer; + gpu_raytrace_pipeline_impl_t impl; +} iron_gpu_raytrace_pipeline_t; + +typedef struct iron_gpu_raytrace_acceleration_structure { + gpu_raytrace_acceleration_structure_impl_t impl; +} iron_gpu_raytrace_acceleration_structure_t; + +struct iron_gpu_pipeline; +struct iron_gpu_texture; +struct iron_gpu_texture_unit; +struct iron_gpu_constant_location; +struct iron_gpu_buffer; + +int iron_gpu_max_bound_textures(void); +void iron_gpu_begin(iron_gpu_texture_t *renderTarget); +void iron_gpu_end(void); +bool iron_gpu_swap_buffers(void); +void iron_gpu_internal_init(void); +void iron_gpu_internal_init_window(int depth_buffer_bits, bool vsync); +void iron_gpu_internal_destroy_window(void); +void iron_gpu_internal_destroy(void); +void gpu_begin(); +void gpu_end(); +void iron_gpu_clear(unsigned color, float depth, unsigned flags); +void gpu_viewport(int x, int y, int width, int height); +void gpu_scissor(int x, int y, int width, int height); +void gpu_disable_scissor(void); +void iron_gpu_draw_indexed_vertices(void); +void iron_gpu_draw_indexed_vertices_from_to(int start, int count); +void iron_gpu_set_pipeline(struct iron_gpu_pipeline *pipeline); +void gpu_set_int(struct iron_gpu_constant_location *location, int value); +void gpu_set_int2(struct iron_gpu_constant_location *location, int value1, int value2); +void gpu_set_int3(struct iron_gpu_constant_location *location, int value1, int value2, int value3); +void gpu_set_int4(struct iron_gpu_constant_location *location, int value1, int value2, int value3, int value4); +void gpu_set_ints(struct iron_gpu_constant_location *location, int *values, int count); +void gpu_set_float(struct iron_gpu_constant_location *location, float value); +void gpu_set_float2(struct iron_gpu_constant_location *location, float value1, float value2); +void gpu_set_float3(struct iron_gpu_constant_location *location, float value1, float value2, float value3); +void gpu_set_float4(struct iron_gpu_constant_location *location, float value1, float value2, float value3, float value4); +void gpu_set_floats(struct iron_gpu_constant_location *location, f32_array_t *values); +void gpu_set_bool(struct iron_gpu_constant_location *location, bool value); +void gpu_set_matrix3(struct iron_gpu_constant_location *location, iron_matrix3x3_t value); +void gpu_set_matrix4(struct iron_gpu_constant_location *location, iron_matrix4x4_t value); +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(iron_gpu_buffer_t *buffer); +void gpu_internal_init_window(int depth_buffer_bits, bool vsync); +void gpu_set_texture_depth(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *renderTarget); +void gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer); +void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count); + +void iron_gpu_vertex_structure_init(iron_gpu_vertex_structure_t *structure); +void iron_gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data); +void iron_gpu_texture_init(iron_gpu_texture_t *texture, int width, int height, iron_image_format_t format); +void iron_gpu_texture_init_from_bytes(iron_gpu_texture_t *texture, void *data, int width, int height, iron_image_format_t format); +void iron_gpu_texture_destroy(iron_gpu_texture_t *texture); +void iron_gpu_texture_generate_mipmaps(iron_gpu_texture_t *texture, int levels); +void iron_gpu_texture_set_mipmap(iron_gpu_texture_t *texture, iron_gpu_texture_t *mipmap, int level); +int iron_gpu_texture_stride(iron_gpu_texture_t *texture); +void iron_gpu_render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits); +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); +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 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 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); +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); + void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipeline); void iron_gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipeline); void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipeline); void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline); iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pipeline_t *pipeline, const char *name); iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *pipeline, const char *name); - void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *source, size_t length, iron_gpu_shader_type_t type); void iron_gpu_shader_destroy(iron_gpu_shader_t *shader); - -struct iron_gpu_pipeline; -struct iron_gpu_texture; -struct iron_gpu_command_list; -struct iron_gpu_buffer; - -typedef struct iron_gpu_command_list { - gpu_command_list_impl_t impl; -} iron_gpu_command_list_t; - void iron_gpu_command_list_init(iron_gpu_command_list_t *list); void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list); void iron_gpu_command_list_begin(iron_gpu_command_list_t *list); @@ -350,38 +290,52 @@ 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, iron_gpu_buffer_t *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, iron_gpu_buffer_t *buffer, int offset, size_t size); +void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *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); void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture); void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *target); - void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data); -typedef struct iron_gpu_raytrace_pipeline { - iron_gpu_buffer_t *_constant_buffer; - gpu_raytrace_pipeline_impl_t impl; -} iron_gpu_raytrace_pipeline_t; - -typedef struct iron_gpu_raytrace_acceleration_structure { - gpu_raytrace_acceleration_structure_impl_t impl; -} iron_gpu_raytrace_acceleration_structure_t; - 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_buffer *constant_buffer); +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_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_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_buffer *_vb_full, struct iron_gpu_buffer *_ib_full); +void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_acceleration_structure_t *accel, struct iron_gpu_command_list *command_list, 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, - struct iron_gpu_texture *texenv, struct iron_gpu_texture *texsobol, struct iron_gpu_texture *texscramble, struct iron_gpu_texture *texrank); +void iron_gpu_raytrace_set_textures(struct iron_gpu_texture *texpaint0, struct iron_gpu_texture *texpaint1, struct iron_gpu_texture *texpaint2, struct iron_gpu_texture *texenv, struct iron_gpu_texture *texsobol, struct iron_gpu_texture *texscramble, struct iron_gpu_texture *texrank); void iron_gpu_raytrace_set_acceleration_structure(iron_gpu_raytrace_acceleration_structure_t *accel); void iron_gpu_raytrace_set_pipeline(iron_gpu_raytrace_pipeline_t *pipeline); void iron_gpu_raytrace_set_target(struct iron_gpu_texture *output); void iron_gpu_raytrace_dispatch_rays(struct iron_gpu_command_list *command_list); + +extern bool iron_gpu_transpose_mat; + +static inline int iron_gpu_vertex_data_size(iron_gpu_vertex_data_t data) { + switch (data) { + case IRON_GPU_VERTEX_DATA_F32_1X: + return 1 * 4; + case IRON_GPU_VERTEX_DATA_F32_2X: + return 2 * 4; + case IRON_GPU_VERTEX_DATA_F32_3X: + return 3 * 4; + case IRON_GPU_VERTEX_DATA_F32_4X: + return 4 * 4; + case IRON_GPU_VERTEX_DATA_U8_4X_NORM: + return 4 * 1; + case IRON_GPU_VERTEX_DATA_I16_2X_NORM: + return 2 * 2; + case IRON_GPU_VERTEX_DATA_I16_4X_NORM: + return 4 * 2; + } +} + +static inline int iron_gpu_vertex_struct_size(iron_gpu_vertex_structure_t *s) { + int size = 0; + for (int i = 0; i < s->size; ++i) { + size += iron_gpu_vertex_data_size(s->elements[i].data); + } + return size; +} diff --git a/base/sources/ts/import_envmap.ts b/base/sources/ts/import_envmap.ts index 7a34cbc4..00ef5cef 100644 --- a/base/sources/ts/import_envmap.ts +++ b/base/sources/ts/import_envmap.ts @@ -28,7 +28,7 @@ function import_envmap_run(path: string, image: iron_gpu_texture_t) { ptr.vertexOffset = 0; import_envmap_radiance_loc = gpu_get_texture_unit(import_envmap_pipeline, "radiance"); - ARRAY_ACCESS(import_envmap_radiance_loc.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + import_envmap_radiance_loc.offset = 0; import_envmap_radiance = gpu_create_render_target(1024, 512, tex_format_t.RGBA128); diff --git a/base/sources/ts/iron/iron.ts b/base/sources/ts/iron/iron.ts index 1dc19613..98e93c59 100644 --- a/base/sources/ts/iron/iron.ts +++ b/base/sources/ts/iron/iron.ts @@ -677,21 +677,12 @@ enum blend_factor_t { DEST_ALPHA, INV_SOURCE_ALPHA, INV_DEST_ALPHA, - SOURCE_COLOR, - DEST_COLOR, - INV_SOURCE_COLOR, - INV_DEST_COLOR, } enum compare_mode_t { ALWAYS, NEVER, - EQUAL, - NOT_EQUAL, LESS, - LESS_EQUAL, - GREATER, - GREATER_EQUAL, } enum cull_mode_t { @@ -701,8 +692,8 @@ enum cull_mode_t { } enum shader_type_t { - FRAGMENT, VERTEX, + FRAGMENT, } declare let ui_nodes_enum_texts: (s: string)=>string[]; diff --git a/base/sources/ts/iron/scene.ts b/base/sources/ts/iron/scene.ts index 10fdeec4..f9a1d568 100644 --- a/base/sources/ts/iron/scene.ts +++ b/base/sources/ts/iron/scene.ts @@ -614,7 +614,6 @@ type shader_const_t = { type tex_unit_t = { name?: string; link?: string; - vert?: bool; }; type speaker_data_t = { diff --git a/base/sources/ts/iron/shader_data.ts b/base/sources/ts/iron/shader_data.ts index e1a6a583..1648969c 100644 --- a/base/sources/ts/iron/shader_data.ts +++ b/base/sources/ts/iron/shader_data.ts @@ -252,24 +252,6 @@ function shader_context_get_compare_mode(s: string): compare_mode_t { if (s == "never") { return compare_mode_t.NEVER; } - if (s == "less") { - return compare_mode_t.LESS; - } - if (s == "less_equal") { - return compare_mode_t.LESS_EQUAL; - } - if (s == "greater") { - return compare_mode_t.GREATER; - } - if (s == "greater_equal") { - return compare_mode_t.GREATER_EQUAL; - } - if (s == "equal") { - return compare_mode_t.EQUAL; - } - if (s == "not_equal") { - return compare_mode_t.NOT_EQUAL; - } return compare_mode_t.LESS; } @@ -302,18 +284,6 @@ function shader_context_get_blend_fac(s: string): blend_factor_t { if (s == "inverse_destination_alpha") { return blend_factor_t.INV_DEST_ALPHA; } - if (s == "source_color") { - return blend_factor_t.SOURCE_COLOR; - } - if (s == "destination_color") { - return blend_factor_t.DEST_COLOR; - } - if (s == "inverse_source_color") { - return blend_factor_t.INV_SOURCE_COLOR; - } - if (s == "inverse_destination_color") { - return blend_factor_t.INV_DEST_COLOR; - } return blend_factor_t.BLEND_ONE; } @@ -348,13 +318,6 @@ function shader_context_add_const(raw: shader_context_t, c: shader_const_t, offs function shader_context_add_tex(raw: shader_context_t, tu: tex_unit_t, i: i32) { let unit: iron_gpu_texture_unit_t = gpu_get_texture_unit(raw._.pipe_state, tu.name); - - if (tu.vert) { - ARRAY_ACCESS(unit.stages, IRON_GPU_SHADER_TYPE_VERTEX) = i; - } - else { - ARRAY_ACCESS(unit.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = i; - } - + unit.offset = i; array_push(raw._.tex_units, unit); } diff --git a/base/sources/ts/node_shader.ts b/base/sources/ts/node_shader.ts index 4929b733..b38405da 100644 --- a/base/sources/ts/node_shader.ts +++ b/base/sources/ts/node_shader.ts @@ -89,14 +89,14 @@ function node_shader_add_constant(raw: node_shader_t, s: string, link: string = } } -function node_shader_add_texture(raw: node_shader_t, s: string, link: string = null, vert: bool = false) { +function node_shader_add_texture(raw: node_shader_t, s: string, link: string = null) { // mytex: tex2d if (array_index_of(raw.textures, s) == -1) { let ar: string[] = string_split(s, ": "); let uname: string = ar[0]; let utype: string = ar[1]; array_push(raw.textures, s); - node_shader_context_add_texture_unit(raw.context, utype, uname, link, vert); + node_shader_context_add_texture_unit(raw.context, utype, uname, link); } } diff --git a/base/sources/ts/node_shader_context.ts b/base/sources/ts/node_shader_context.ts index 91d46ce0..dfe1e430 100644 --- a/base/sources/ts/node_shader_context.ts +++ b/base/sources/ts/node_shader_context.ts @@ -108,7 +108,7 @@ function node_shader_context_add_constant(raw: node_shader_context_t, ctype: str array_push(raw.data.constants, c); } -function node_shader_context_add_texture_unit(raw: node_shader_context_t, ctype: string, name: string, link: string = null, vert: bool = false) { +function node_shader_context_add_texture_unit(raw: node_shader_context_t, ctype: string, name: string, link: string = null) { for (let i: i32 = 0; i < raw.data.texture_units.length; ++i) { let c: tex_unit_t = raw.data.texture_units[i]; if (c.name == name) { @@ -116,7 +116,7 @@ function node_shader_context_add_texture_unit(raw: node_shader_context_t, ctype: } } - let c: tex_unit_t = { name: name, link: link, vert: vert }; + let c: tex_unit_t = { name: name, link: link }; array_push(raw.data.texture_units, c); } diff --git a/base/sources/ts/pipes.ts b/base/sources/ts/pipes.ts index 49d29c5a..53245563 100644 --- a/base/sources/ts/pipes.ts +++ b/base/sources/ts/pipes.ts @@ -74,13 +74,13 @@ function pipes_init() { pipes_merge_g = _pipes_make_merge(false, true, false, false); pipes_merge_b = _pipes_make_merge(false, false, true, false); pipes_tex0 = gpu_get_texture_unit(pipes_merge, "tex0"); // Always binding texpaint.a for blending - ARRAY_ACCESS(pipes_tex0.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_tex0.offset = 0; pipes_tex1 = gpu_get_texture_unit(pipes_merge, "tex1"); - ARRAY_ACCESS(pipes_tex1.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 1; + pipes_tex1.offset = 1; pipes_texmask = gpu_get_texture_unit(pipes_merge, "texmask"); - ARRAY_ACCESS(pipes_texmask.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 2; + pipes_texmask.offset = 2; pipes_texa = gpu_get_texture_unit(pipes_merge, "texa"); - ARRAY_ACCESS(pipes_texa.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 3; + pipes_texa.offset = 3; pipes_offset = 0; pipes_opac = pipes_get_constant_location(pipes_merge, "opac", "float"); pipes_blending = pipes_get_constant_location(pipes_merge, "blending", "int"); @@ -163,9 +163,9 @@ function pipes_init() { pipes_apply_mask.input_layout = vs; gpu_compile_pipeline(pipes_apply_mask); pipes_tex0_mask = gpu_get_texture_unit(pipes_apply_mask, "tex0"); - ARRAY_ACCESS(pipes_tex0_mask.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_tex0_mask.offset = 0; pipes_texa_mask = gpu_get_texture_unit(pipes_apply_mask, "texa"); - ARRAY_ACCESS(pipes_texa_mask.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 1; + pipes_texa_mask.offset = 1; } { @@ -177,9 +177,9 @@ function pipes_init() { pipes_merge_mask.input_layout = vs; gpu_compile_pipeline(pipes_merge_mask); pipes_tex0_merge_mask = gpu_get_texture_unit(pipes_merge_mask, "tex0"); - ARRAY_ACCESS(pipes_tex0_merge_mask.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_tex0_merge_mask.offset = 0; pipes_texa_merge_mask = gpu_get_texture_unit(pipes_merge_mask, "texa"); - ARRAY_ACCESS(pipes_texa_merge_mask.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 1; + pipes_texa_merge_mask.offset = 1; pipes_offset = 0; pipes_opac_merge_mask = pipes_get_constant_location(pipes_merge_mask, "opac", "float"); pipes_blending_merge_mask = pipes_get_constant_location(pipes_merge_mask, "blending", "int"); @@ -194,9 +194,9 @@ function pipes_init() { pipes_colorid_to_mask.input_layout = vs; gpu_compile_pipeline(pipes_colorid_to_mask); pipes_texpaint_colorid = gpu_get_texture_unit(pipes_colorid_to_mask, "texpaint_colorid"); - ARRAY_ACCESS(pipes_texpaint_colorid.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_texpaint_colorid.offset = 0; pipes_tex_colorid = gpu_get_texture_unit(pipes_colorid_to_mask, "texcolorid"); - ARRAY_ACCESS(pipes_tex_colorid.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 1; + pipes_tex_colorid.offset = 1; } ///end @@ -240,9 +240,9 @@ function pipes_init() { pipes_inpaint_preview.input_layout = vs; gpu_compile_pipeline(pipes_inpaint_preview); pipes_tex0_inpaint_preview = gpu_get_texture_unit(pipes_inpaint_preview, "tex0"); - ARRAY_ACCESS(pipes_tex0_inpaint_preview.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_tex0_inpaint_preview.offset = 0; pipes_texa_inpaint_preview = gpu_get_texture_unit(pipes_inpaint_preview, "texa"); - ARRAY_ACCESS(pipes_texa_inpaint_preview.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 1; + pipes_texa_inpaint_preview.offset = 1; } { @@ -257,7 +257,7 @@ function pipes_init() { ARRAY_ACCESS(pipes_copy_a.color_write_mask_blue, 0) = false; gpu_compile_pipeline(pipes_copy_a); pipes_copy_a_tex = gpu_get_texture_unit(pipes_copy_a, "tex"); - ARRAY_ACCESS(pipes_copy_a_tex.stages, IRON_GPU_SHADER_TYPE_FRAGMENT) = 0; + pipes_copy_a_tex.offset = 0; } ///end @@ -297,7 +297,7 @@ function pipes_init() { pipes_cursor_camera_right = pipes_get_constant_location(pipes_cursor, "camera_right", "vec3"); pipes_cursor_tint = pipes_get_constant_location(pipes_cursor, "tint", "vec3"); pipes_cursor_gbufferd = gpu_get_texture_unit(pipes_cursor, "gbufferD"); - ARRAY_ACCESS(pipes_cursor_gbufferd.stages, IRON_GPU_SHADER_TYPE_VERTEX) = 0; + pipes_cursor_gbufferd.offset = 0; } }