From 2f007eabde2d2c9c8a8c7e0e84b368c62e251a3b Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 1 Jul 2025 14:46:40 +0200 Subject: [PATCH] Depth buffer cleanup --- armorlab/sources/make_mesh.ts | 2 +- armorlab/sources/nodes/inpaint_node.ts | 2 +- armorlab/sources/render_path_paint.ts | 15 +- armorpaint/sources/layers_ext.ts | 6 +- armorpaint/sources/make_mesh.ts | 2 +- armorpaint/sources/make_mesh_preview.ts | 2 +- armorpaint/sources/render_path_paint.ts | 24 +-- armorsculpt/sources/make_mesh.ts | 2 +- armorsculpt/sources/make_mesh_preview.ts | 2 +- armorsculpt/sources/render_path_sculpt.ts | 2 +- base/assets/Scene.arm | Bin 178103 -> 178091 bytes base/assets/shader_datas.arm | Bin 14908 -> 14896 bytes base/sources/backends/direct3d12_gpu.c | 174 ++++-------------- base/sources/backends/direct3d12_gpu.h | 4 - base/sources/backends/ios_system.h | 1 - base/sources/backends/macos_system.m | 2 +- base/sources/backends/metal_gpu.h | 1 - base/sources/backends/metal_gpu.m | 52 ++---- base/sources/backends/vulkan_gpu.c | 179 ++++++------------- base/sources/backends/vulkan_gpu.h | 5 - base/sources/backends/webgpu_gpu.c | 10 +- base/sources/backends/windows_system.c | 7 +- base/sources/iron.h | 11 +- base/sources/iron_draw.c | 4 +- base/sources/iron_gpu.c | 14 +- base/sources/iron_gpu.h | 15 +- base/sources/iron_system.c | 3 +- base/sources/iron_system.h | 4 - base/sources/iron_ui.c | 12 +- base/sources/iron_ui_nodes.c | 2 +- base/sources/ts/config.ts | 3 +- base/sources/ts/export_texture.ts | 6 +- base/sources/ts/import_arm.ts | 4 +- base/sources/ts/iron/iron.ts | 7 +- base/sources/ts/iron/render_path.ts | 135 +++----------- base/sources/ts/iron/scene.ts | 2 +- base/sources/ts/iron/shader_data.ts | 2 +- base/sources/ts/iron/sys.ts | 3 - base/sources/ts/iron/uniforms.ts | 29 +-- base/sources/ts/layers.ts | 8 +- base/sources/ts/line_draw.ts | 2 +- base/sources/ts/render_path_base.ts | 28 +-- base/sources/ts/render_path_deferred.ts | 11 +- base/sources/ts/render_path_forward.ts | 4 +- base/sources/ts/render_path_preview.ts | 57 +++--- base/sources/ts/render_path_raytrace_bake.ts | 2 +- base/sources/ts/slot_layer.ts | 6 +- base/sources/ts/util_uv.ts | 2 +- base/tests/cube/sources/main.ts | 4 +- base/tests/fall/sources/main.ts | 4 +- base/tests/triangle/main.ts | 4 +- 51 files changed, 269 insertions(+), 613 deletions(-) diff --git a/armorlab/sources/make_mesh.ts b/armorlab/sources/make_mesh.ts index 4d356930..add3f76f 100644 --- a/armorlab/sources/make_mesh.ts +++ b/armorlab/sources/make_mesh.ts @@ -31,7 +31,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte "RGBA64", "RGBA64" ], - depth_attachment: "DEPTH32" + depth_attachment: "D32" }; let con_mesh: node_shader_context_t = node_shader_context_create(data, props); let kong: node_shader_t = node_shader_context_make_kong(con_mesh); diff --git a/armorlab/sources/nodes/inpaint_node.ts b/armorlab/sources/nodes/inpaint_node.ts index 985ae96e..97d99b63 100644 --- a/armorlab/sources/nodes/inpaint_node.ts +++ b/armorlab/sources/nodes/inpaint_node.ts @@ -33,7 +33,7 @@ function inpaint_node_init() { if (inpaint_node_mask == null) { inpaint_node_mask = gpu_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8); sys_notify_on_next_frame(function () { - _gpu_begin(inpaint_node_mask, null, clear_flag_t.COLOR, color_from_floats(1.0, 1.0, 1.0, 1.0)); + _gpu_begin(inpaint_node_mask, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 1.0, 1.0, 1.0)); gpu_end(); }); } diff --git a/armorlab/sources/render_path_paint.ts b/armorlab/sources/render_path_paint.ts index 9397645d..9fa0c968 100644 --- a/armorlab/sources/render_path_paint.ts +++ b/armorlab/sources/render_path_paint.ts @@ -127,7 +127,7 @@ function render_path_paint_commands_paint(dilation: bool = true) { let additional: string[] = ["texpaint_nor" + tid, "texpaint_pack" + tid, "texpaint_blend0"]; render_path_set_target(texpaint, additional); - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); render_path_bind_target("texpaint_blend1", "paintmask"); @@ -177,10 +177,11 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 render_path_set_target(""); 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); + let rt: render_target_t = map_get(render_path_render_targets, "main"); + let main: gpu_texture_t = rt._image; + gpu_set_texture(pipes_cursor_gbufferd, main); 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_float2(pipes_cursor_tex_step, 1 / main.width, 1 / main.height); gpu_set_float(pipes_cursor_radius, radius); let right: vec4_t = vec4_norm(camera_object_right_world(scene_camera)); gpu_set_float3(pipes_cursor_camera_right, right.x, right.y, right.z); @@ -226,11 +227,11 @@ function render_path_paint_draw() { if (context_raw.brush_blend_dirty) { context_raw.brush_blend_dirty = false; ///if arm_metal - render_path_set_target("texpaint_blend0", null, clear_flag_t.COLOR, 0x00000000); - render_path_set_target("texpaint_blend1", null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend0", null, null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend1", null, null, clear_flag_t.COLOR, 0x00000000); ///else let additional: string[] = ["texpaint_blend1"]; - render_path_set_target("texpaint_blend0", additional, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend0", additional, null, clear_flag_t.COLOR, 0x00000000); ///end } } diff --git a/armorpaint/sources/layers_ext.ts b/armorpaint/sources/layers_ext.ts index 9cf7e4a8..d2930523 100644 --- a/armorpaint/sources/layers_ext.ts +++ b/armorpaint/sources/layers_ext.ts @@ -10,11 +10,11 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t let empty: gpu_texture_t = empty_rt._image; // Clear export layer - _gpu_begin(layers_expa, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expa, null, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); gpu_end(); - _gpu_begin(layers_expb, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(layers_expb, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); gpu_end(); - _gpu_begin(layers_expc, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expc, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); gpu_end(); // Flatten layers diff --git a/armorpaint/sources/make_mesh.ts b/armorpaint/sources/make_mesh.ts index 58ac25cc..94cce1eb 100644 --- a/armorpaint/sources/make_mesh.ts +++ b/armorpaint/sources/make_mesh.ts @@ -27,7 +27,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte "RGBA64", "RGBA64" ], - depth_attachment: "DEPTH32" + depth_attachment: "D32" }; let con_mesh: node_shader_context_t = node_shader_context_create(data, props); diff --git a/armorpaint/sources/make_mesh_preview.ts b/armorpaint/sources/make_mesh_preview.ts index 67bd707a..36ef08e1 100644 --- a/armorpaint/sources/make_mesh_preview.ts +++ b/armorpaint/sources/make_mesh_preview.ts @@ -23,7 +23,7 @@ function make_mesh_preview_run(data: material_t, matcon: material_context_t): no } ], color_attachments: ["RGBA64", "RGBA64", "RGBA64"], - depth_attachment: "DEPTH32" + depth_attachment: "D32" }; let con_mesh: node_shader_context_t = node_shader_context_create(data, props); diff --git a/armorpaint/sources/render_path_paint.ts b/armorpaint/sources/render_path_paint.ts index f1a091e0..b2550d0b 100644 --- a/armorpaint/sources/render_path_paint.ts +++ b/armorpaint/sources/render_path_paint.ts @@ -120,7 +120,7 @@ function render_path_paint_commands_paint(dilation: bool = true) { ///end if (context_raw.tool == workspace_tool_t.COLORID) { - render_path_set_target("texpaint_colorid", null, clear_flag_t.COLOR, 0xff000000); + render_path_set_target("texpaint_colorid", null, null, clear_flag_t.COLOR, 0xff000000); render_path_bind_target("gbuffer2", "gbuffer2"); render_path_paint_draw_fullscreen_triangle("paint"); ui_header_handle.redraws = 2; @@ -129,13 +129,13 @@ function render_path_paint_commands_paint(dilation: bool = true) { if (context_raw.pick_pos_nor_tex) { if (context_raw.paint2d) { let additional: string[] = ["gbuffer1", "gbuffer2"]; - render_path_set_target("gbuffer0", additional); + render_path_set_target("gbuffer0", additional, "main"); render_path_draw_meshes("mesh"); } let additional: string[] = ["texpaint_posnortex_picker1"]; render_path_set_target("texpaint_posnortex_picker0", additional); render_path_bind_target("gbuffer2", "gbuffer2"); - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); render_path_draw_meshes("paint"); let picker0: render_target_t = map_get(render_path_render_targets, "texpaint_posnortex_picker0"); let picker1: render_target_t = map_get(render_path_render_targets, "texpaint_posnortex_picker1"); @@ -230,7 +230,7 @@ function render_path_paint_commands_paint(dilation: bool = true) { let texpaint: string = "texpaint" + tid; if (context_raw.tool == workspace_tool_t.BAKE && context_raw.brush_time == sys_delta()) { // Clear to black on bake start - render_path_set_target(texpaint, null, clear_flag_t.COLOR, 0xff000000); + render_path_set_target(texpaint, null, null, clear_flag_t.COLOR, 0xff000000); } render_path_set_target("texpaint_blend1"); @@ -253,7 +253,7 @@ function render_path_paint_commands_paint(dilation: bool = true) { let additional: string[] = ["texpaint_nor" + tid, "texpaint_pack" + tid, "texpaint_blend0"]; render_path_set_target(texpaint, additional); } - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); if ((context_raw.xray || config_raw.brush_angle_reject) && config_raw.brush_3d) { render_path_bind_target("gbuffer0", "gbuffer0"); } @@ -440,11 +440,11 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 render_path_set_target(""); gpu_set_pipeline(pipes_cursor); - let rt: render_target_t = map_get(render_path_render_targets, "gbuffer0"); - let gbuffer0: gpu_texture_t = rt._image; - gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0); + let rt: render_target_t = map_get(render_path_render_targets, "main"); + let main: gpu_texture_t = rt._image; + gpu_set_texture(pipes_cursor_gbufferd, main); gpu_set_float2(pipes_cursor_mouse, mx, my); - gpu_set_float2(pipes_cursor_tex_step, 1 / gbuffer0.width, 1 / gbuffer0.height); + gpu_set_float2(pipes_cursor_tex_step, 1 / main.width, 1 / main.height); gpu_set_float(pipes_cursor_radius, radius); let right: vec4_t = vec4_norm(camera_object_right_world(scene_camera)); gpu_set_float3(pipes_cursor_camera_right, right.x, right.y, right.z); @@ -706,11 +706,11 @@ function render_path_paint_draw() { if (context_raw.brush_blend_dirty) { context_raw.brush_blend_dirty = false; ///if arm_metal - render_path_set_target("texpaint_blend0", null, clear_flag_t.COLOR, 0x00000000); - render_path_set_target("texpaint_blend1", null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend0", null, null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend1", null, null, clear_flag_t.COLOR, 0x00000000); ///else let additional: string[] = ["texpaint_blend1"]; - render_path_set_target("texpaint_blend0", additional, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend0", additional, null, clear_flag_t.COLOR, 0x00000000); ///end } diff --git a/armorsculpt/sources/make_mesh.ts b/armorsculpt/sources/make_mesh.ts index ebc2520b..100ef58a 100644 --- a/armorsculpt/sources/make_mesh.ts +++ b/armorsculpt/sources/make_mesh.ts @@ -23,7 +23,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte "RGBA64", "RGBA64" ], - depth_attachment: "DEPTH32" + depth_attachment: "D32" }; let con_mesh: node_shader_context_t = node_shader_context_create(data, props); diff --git a/armorsculpt/sources/make_mesh_preview.ts b/armorsculpt/sources/make_mesh_preview.ts index 353ad718..e3cb66c6 100644 --- a/armorsculpt/sources/make_mesh_preview.ts +++ b/armorsculpt/sources/make_mesh_preview.ts @@ -28,7 +28,7 @@ function make_mesh_preview_run(data: material_t, matcon: material_context_t): no "RGBA64", "RGBA64" ], - depth_attachment: "DEPTH32" + depth_attachment: "D32" }; let con_mesh: node_shader_context_t = node_shader_context_create(data, props); diff --git a/armorsculpt/sources/render_path_sculpt.ts b/armorsculpt/sources/render_path_sculpt.ts index c4bd79da..8b2f131d 100644 --- a/armorsculpt/sources/render_path_sculpt.ts +++ b/armorsculpt/sources/render_path_sculpt.ts @@ -55,7 +55,7 @@ function render_path_sculpt_begin() { render_path_draw_shader("shader_datas/copy_pass/copy_pass"); render_path_set_target("gbufferD_undo"); - render_path_bind_target("_main", "tex"); + render_path_bind_target("main", "tex"); render_path_draw_shader("shader_datas/copy_pass/copy_pass"); } diff --git a/base/assets/Scene.arm b/base/assets/Scene.arm index 398274d3baba5d7301fec24449ab2137610b2660..ba6508bce6f5fffdc33d001d5b1d0765a0cc3bb1 100644 GIT binary patch delta 55 ycmdn~o@@1cu7(!IEllTDu`)9-Ft}`w*~(Fm0-~E& IT1;jH007DqH2?qr delta 64 zcmdl`vZrLj6<$tu1_lNf*MJa@$?w&(C(l(f-dxUCEQt`_{9b*M0D=otCBAvC#bibR DrNa~% diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index d1b77e78..78ad4473 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -24,15 +24,18 @@ static ID3D12RootSignature *root_signature = NULL; static struct ID3D12CommandAllocator *command_allocator; static struct ID3D12GraphicsCommandList *command_list; static gpu_pipeline_t *current_pipeline; -static gpu_texture_t *current_textures[TEXTURE_COUNT]; +static gpu_texture_t *current_textures[TEXTURE_COUNT] = { + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL +}; static bool window_vsync; -static int index_count; +static int index_count = 0; static struct ID3D12DescriptorHeap *srv_heap; -static int srv_heap_index; +static int srv_heap_index = 0; static UINT64 fence_value; static ID3D12Fence *fence; static HANDLE fence_event; -static UINT64 frame_fence_values[GPU_FRAMEBUFFER_COUNT]; +static UINT64 frame_fence_values[GPU_FRAMEBUFFER_COUNT] = {0, 0}; static D3D12_BLEND convert_blend_factor(gpu_blending_factor_t factor) { switch (factor) { @@ -94,6 +97,8 @@ static DXGI_FORMAT convert_format(gpu_texture_format_t format) { return DXGI_FORMAT_R16_FLOAT; case GPU_TEXTURE_FORMAT_R8: return DXGI_FORMAT_R8_UNORM; + case GPU_TEXTURE_FORMAT_D32: + return DXGI_FORMAT_D32_FLOAT; case GPU_TEXTURE_FORMAT_RGBA32: default: return DXGI_FORMAT_R8G8B8A8_UNORM; @@ -174,11 +179,10 @@ void gpu_destroy() { device->lpVtbl->Release(device); } -static void render_target_init(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int depth_buffer_bits, int framebuffer_index) { - render_target->width = render_target->width = width; - render_target->height = render_target->height = height; +void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int framebuffer_index) { + render_target->width = width; + render_target->height = height; render_target->impl.stage = 0; - render_target->impl.stage_depth = -1; render_target->impl.readback = NULL; render_target->impl.image = NULL; render_target->impl.upload_image = NULL; @@ -194,6 +198,7 @@ static void render_target_init(gpu_texture_t *render_target, int width, int heig clear_value.Color[1] = 0.0f; clear_value.Color[2] = 0.0f; clear_value.Color[3] = 0.0f; + clear_value.DepthStencil.Depth = 1.0f; D3D12_HEAP_PROPERTIES heap_properties = { .Type = D3D12_HEAP_TYPE_DEFAULT, @@ -214,12 +219,12 @@ static void render_target_init(gpu_texture_t *render_target, int width, int heig .SampleDesc.Count = 1, .SampleDesc.Quality = 0, .Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, - .Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, + .Flags = format == GPU_TEXTURE_FORMAT_D32 ? D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL : D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, }; D3D12_DESCRIPTOR_HEAP_DESC heap_desc = { .NumDescriptors = 1, - .Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV, + .Type = format == GPU_TEXTURE_FORMAT_D32 ? D3D12_DESCRIPTOR_HEAP_TYPE_DSV : D3D12_DESCRIPTOR_HEAP_TYPE_RTV, .Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE, }; device->lpVtbl->CreateDescriptorHeap(device, &heap_desc, &IID_ID3D12DescriptorHeap, &render_target->impl.descriptor_heap); @@ -252,7 +257,12 @@ static void render_target_init(gpu_texture_t *render_target, int width, int heig D3D12_CPU_DESCRIPTOR_HANDLE handle; render_target->impl.descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(render_target->impl.descriptor_heap, &handle); - device->lpVtbl->CreateRenderTargetView(device, render_target->impl.render_target, &view_desc, handle); + if (format == GPU_TEXTURE_FORMAT_D32) { + device->lpVtbl->CreateDepthStencilView(device, render_target->impl.render_target, NULL, handle); + } + else { + device->lpVtbl->CreateRenderTargetView(device, render_target->impl.render_target, &view_desc, handle); + } D3D12_DESCRIPTOR_HEAP_DESC descriptor_heap_desc = { .NumDescriptors = 1, @@ -266,7 +276,7 @@ static void render_target_init(gpu_texture_t *render_target, int width, int heig D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = { .ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D, .Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, - .Format = dxgi_format, + .Format = format == GPU_TEXTURE_FORMAT_D32 ? DXGI_FORMAT_R32_FLOAT : dxgi_format, .Texture2D.MipLevels = 1, .Texture2D.MostDetailedMip = 0, .Texture2D.ResourceMinLODClamp = 0.0f, @@ -275,85 +285,6 @@ static void render_target_init(gpu_texture_t *render_target, int width, int heig render_target->impl.srv_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(render_target->impl.srv_descriptor_heap, &handle); device->lpVtbl->CreateShaderResourceView(device, render_target->impl.render_target, &srv_desc, handle); - if (depth_buffer_bits > 0) { - D3D12_DESCRIPTOR_HEAP_DESC dsv_heap_desc = { - .NumDescriptors = 1, - .Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV, - .Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE, - }; - device->lpVtbl->CreateDescriptorHeap(device, &dsv_heap_desc, &IID_ID3D12DescriptorHeap, &render_target->impl.depth_descriptor_heap); - - D3D12_RESOURCE_DESC depth_resource_desc = { - .Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D, - .Alignment = 0, - .Width = width, - .Height = height, - .DepthOrArraySize = 1, - .MipLevels = 1, - .Format = DXGI_FORMAT_D32_FLOAT, - .SampleDesc.Count = 1, - .SampleDesc.Quality = 0, - .Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, - .Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, - }; - - D3D12_CLEAR_VALUE clear_value = { - .Format = DXGI_FORMAT_D32_FLOAT, - .DepthStencil.Depth = 1.0f, - .DepthStencil.Stencil = 0, - }; - - D3D12_HEAP_PROPERTIES heap_properties = { - .Type = D3D12_HEAP_TYPE_DEFAULT, - .CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN, - .MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN, - .CreationNodeMask = 1, - .VisibleNodeMask = 1, - }; - - HRESULT result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &depth_resource_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, - &clear_value, &IID_ID3D12Resource, &render_target->impl.depth_texture); - if (result != S_OK) { - for (int i = 0; i < 10; ++i) { - iron_memory_emergency(); - result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &depth_resource_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, - &clear_value, &IID_ID3D12Resource, &render_target->impl.depth_texture); - if (result == S_OK) { - break; - } - } - } - - D3D12_CPU_DESCRIPTOR_HANDLE handle; - render_target->impl.depth_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(render_target->impl.depth_descriptor_heap, &handle); - device->lpVtbl->CreateDepthStencilView(device, render_target->impl.depth_texture, NULL, handle); - - D3D12_DESCRIPTOR_HEAP_DESC srv_depth_heap_desc = { - .NumDescriptors = 1, - .Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, - .NodeMask = 0, - .Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE, - }; - device->lpVtbl->CreateDescriptorHeap(device, &srv_depth_heap_desc, &IID_ID3D12DescriptorHeap, &render_target->impl.srv_depth_descriptor_heap); - - D3D12_SHADER_RESOURCE_VIEW_DESC srv_depth_view_desc = { - .ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D, - .Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, - .Format = DXGI_FORMAT_R32_FLOAT, - .Texture2D.MipLevels = 1, - .Texture2D.MostDetailedMip = 0, - .Texture2D.ResourceMinLODClamp = 0.0f, - }; - - render_target->impl.srv_depth_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(render_target->impl.srv_depth_descriptor_heap, &handle); - device->lpVtbl->CreateShaderResourceView(device, render_target->impl.depth_texture, &srv_depth_view_desc, handle); - } - else { - render_target->impl.depth_descriptor_heap = NULL; - render_target->impl.depth_texture = NULL; - render_target->impl.srv_depth_descriptor_heap = NULL; - } - render_target->impl.scissor.top = 0; render_target->impl.scissor.left = 0; render_target->impl.scissor.right = width; @@ -451,16 +382,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { fence_event = CreateEvent(NULL, FALSE, FALSE, NULL); device->lpVtbl->CreateFence(device, 0, D3D12_FENCE_FLAG_NONE, &IID_ID3D12Fence, &fence); - for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { - frame_fence_values[i] = 0; - render_target_init(&framebuffers[i], iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_RGBA32, depth_buffer_bits, i); - } - - for (int i = 0; i < TEXTURE_COUNT; ++i) { - current_textures[i] = NULL; - } - srv_heap_index = 0; - index_count = 0; + gpu_create_framebuffers(depth_buffer_bits); D3D12_DESCRIPTOR_HEAP_DESC heap_desc = { .NumDescriptors = HEAP_SIZE, @@ -477,7 +399,7 @@ int gpu_max_bound_textures(void) { return TEXTURE_COUNT; } -void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { +void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[16]; for (int i = 0; i < current_render_targets_count; ++i) { @@ -486,10 +408,10 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsi gpu_texture_t *target = current_render_targets[0]; - if (target->impl.depth_descriptor_heap != NULL) { - D3D12_CPU_DESCRIPTOR_HANDLE heapStart; - target->impl.depth_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(target->impl.depth_descriptor_heap, &heapStart); - command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, &heapStart); + if (depth_buffer != NULL) { + D3D12_CPU_DESCRIPTOR_HANDLE depth_handle; + depth_buffer->impl.descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(depth_buffer->impl.descriptor_heap, &depth_handle); + command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, &depth_handle); } else { command_list->lpVtbl->OMSetRenderTargets(command_list, current_render_targets_count, &target_descriptors[0], false, NULL); @@ -508,13 +430,10 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsi target->impl.descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(target->impl.descriptor_heap, &handle); command_list->lpVtbl->ClearRenderTargetView(command_list, handle, clearColor, 0, NULL); } - if (flags & GPU_CLEAR_DEPTH) { - D3D12_CLEAR_FLAGS d3dflags = D3D12_CLEAR_FLAG_DEPTH; - if (target->impl.depth_descriptor_heap != NULL) { - D3D12_CPU_DESCRIPTOR_HANDLE handle; - target->impl.depth_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(target->impl.depth_descriptor_heap, &handle); - command_list->lpVtbl->ClearDepthStencilView(command_list, handle, d3dflags, depth, 0, 0, NULL); - } + if (flags & GPU_CLEAR_DEPTH && depth_buffer != NULL) { + D3D12_CPU_DESCRIPTOR_HANDLE handle; + depth_buffer->impl.descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(depth_buffer->impl.descriptor_heap, &handle); + command_list->lpVtbl->ClearDepthStencilView(command_list, handle, D3D12_CLEAR_FLAG_DEPTH, depth, 0, 0, NULL); } } @@ -560,7 +479,7 @@ void gpu_internal_resize(int width, int height) { for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { gpu_texture_destroy(&framebuffers[i]); - render_target_init(&framebuffers[i], width, height, GPU_TEXTURE_FORMAT_RGBA32, 0, i); + gpu_render_target_init2(&framebuffers[i], width, height, GPU_TEXTURE_FORMAT_RGBA32, i); } window_swapchain->lpVtbl->ResizeBuffers(window_swapchain, GPU_FRAMEBUFFER_COUNT, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0); } @@ -597,9 +516,7 @@ void gpu_internal_set_textures() { } D3D12_CPU_DESCRIPTOR_HANDLE source_cpu; - ID3D12DescriptorHeap *source_heap = (texture->impl.stage_depth == i) ? - texture->impl.srv_depth_descriptor_heap : - texture->impl.srv_descriptor_heap; + ID3D12DescriptorHeap *source_heap = texture->impl.srv_descriptor_heap; source_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(source_heap, &source_cpu); device->lpVtbl->CopyDescriptorsSimple(device, 1, cpu_base, source_cpu, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); @@ -612,7 +529,6 @@ void gpu_internal_set_textures() { continue; } current_textures[i]->impl.stage = 0; - current_textures[i]->impl.stage_depth = -1; current_textures[i] = NULL; } @@ -824,11 +740,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) { current_textures[texture->impl.stage] = texture; } -void gpu_set_texture_depth(int unit, gpu_texture_t *texture) { - texture->impl.stage_depth = unit; - current_textures[texture->impl.stage_depth] = texture; -} - void gpu_pipeline_init(gpu_pipeline_t *pipe) { gpu_internal_pipeline_init(pipe); } @@ -959,7 +870,6 @@ void gpu_shader_destroy(gpu_shader_t *shader) { void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) { memset(&texture->impl, 0, sizeof(texture->impl)); texture->impl.stage = 0; - texture->impl.stage_depth = -1; texture->impl.mipmap = true; texture->width = width; texture->height = height; @@ -1080,7 +990,6 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, void gpu_texture_init(gpu_texture_t *texture, int width, int height, gpu_texture_format_t format) { memset(&texture->impl, 0, sizeof(texture->impl)); texture->impl.stage = 0; - texture->impl.stage_depth = -1; texture->impl.mipmap = true; texture->width = width; texture->height = height; @@ -1196,11 +1105,6 @@ void gpu_texture_destroy(gpu_texture_t *render_target) { render_target->impl.render_target->lpVtbl->Release(render_target->impl.render_target); render_target->impl.descriptor_heap->lpVtbl->Release(render_target->impl.descriptor_heap); render_target->impl.srv_descriptor_heap->lpVtbl->Release(render_target->impl.srv_descriptor_heap); - if (render_target->impl.depth_texture != NULL) { - render_target->impl.depth_texture->lpVtbl->Release(render_target->impl.depth_texture); - render_target->impl.depth_descriptor_heap->lpVtbl->Release(render_target->impl.depth_descriptor_heap); - render_target->impl.srv_depth_descriptor_heap->lpVtbl->Release(render_target->impl.srv_depth_descriptor_heap); - } if (render_target->impl.readback != NULL) { render_target->impl.readback->lpVtbl->Release(render_target->impl.readback); } @@ -1220,14 +1124,8 @@ void gpu_texture_generate_mipmaps(gpu_texture_t *texture, int levels) {} void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {} -void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depth_buffer_bits) { - render_target_init(target, width, height, format, depth_buffer_bits, -1); -} - -void gpu_render_target_set_depth_from(gpu_texture_t *render_target, gpu_texture_t *source) { - render_target->impl.depth_descriptor_heap = source->impl.depth_descriptor_heap; - render_target->impl.srv_depth_descriptor_heap = source->impl.srv_depth_descriptor_heap; - render_target->impl.depth_texture = source->impl.depth_texture; +void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) { + gpu_render_target_init2(target, width, height, format, -1); } void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure) { diff --git a/base/sources/backends/direct3d12_gpu.h b/base/sources/backends/direct3d12_gpu.h index 2de7f74e..7485dfd9 100644 --- a/base/sources/backends/direct3d12_gpu.h +++ b/base/sources/backends/direct3d12_gpu.h @@ -53,7 +53,6 @@ typedef struct { typedef struct { bool mipmap; int stage; - int stage_depth; int stride; struct ID3D12Resource *image; struct ID3D12Resource *upload_image; @@ -61,9 +60,6 @@ typedef struct { struct ID3D12Resource *render_target; struct ID3D12Resource *readback; struct ID3D12DescriptorHeap *descriptor_heap; - struct ID3D12DescriptorHeap *depth_descriptor_heap; - struct ID3D12DescriptorHeap *srv_depth_descriptor_heap; - struct ID3D12Resource *depth_texture; struct D3D12Viewport viewport; struct D3D12Rect scissor; } gpu_texture_impl_t; diff --git a/base/sources/backends/ios_system.h b/base/sources/backends/ios_system.h index ff411b03..38f9ff35 100644 --- a/base/sources/backends/ios_system.h +++ b/base/sources/backends/ios_system.h @@ -15,7 +15,6 @@ struct gpu_texture; id drawable; id library; MTLRenderPassDescriptor *renderPassDescriptor; - id depthTexture; CMMotionManager *motionManager; bool hasAccelerometer; diff --git a/base/sources/backends/macos_system.m b/base/sources/backends/macos_system.m index 72b98887..8ab5d7ff 100644 --- a/base/sources/backends/macos_system.m +++ b/base/sources/backends/macos_system.m @@ -365,7 +365,7 @@ static bool controlKeyMouseButton = false; return YES; } -- (void)update { // window resizes, moves and display changes (resize, depth and display config change) +- (void)update { } - (id)initWithFrame:(NSRect)frameRect { diff --git a/base/sources/backends/metal_gpu.h b/base/sources/backends/metal_gpu.h index 43a65246..815a629d 100644 --- a/base/sources/backends/metal_gpu.h +++ b/base/sources/backends/metal_gpu.h @@ -24,7 +24,6 @@ typedef struct { void *data; bool has_mipmaps; void *_texReadback; - void *_depthTex; } gpu_texture_impl_t; typedef struct { diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index eb9b80b2..449dfd43 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -83,6 +83,8 @@ static MTLPixelFormat convert_render_target_format(gpu_texture_format_t format) return MTLPixelFormatR16Float; case GPU_TEXTURE_FORMAT_R8: return MTLPixelFormatR8Unorm; + case GPU_TEXTURE_FORMAT_D32: + return MTLPixelFormatDepth32Float; case GPU_TEXTURE_FORMAT_RGBA32: default: return MTLPixelFormatBGRA8Unorm; @@ -138,7 +140,7 @@ static int format_byte_size(gpu_texture_format_t format) { } } -static void render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depth_buffer_bits, int framebuffer_index) { +void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int framebuffer_index) { id device = getMetalDevice(); memset(target, 0, sizeof(gpu_texture_t)); target->width = width; @@ -147,7 +149,6 @@ static void render_target_init(gpu_texture_t *target, int width, int height, gpu target->uploaded = true; target->state = GPU_TEXTURE_STATE_RENDER_TARGET; target->impl._texReadback = NULL; - target->impl._depthTex = NULL; if (framebuffer_index < 0) { id device = getMetalDevice(); @@ -163,20 +164,6 @@ static void render_target_init(gpu_texture_t *target, int width, int height, gpu descriptor.resourceOptions = MTLResourceStorageModePrivate; target->impl._tex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; } - - if (depth_buffer_bits > 0) { - MTLTextureDescriptor *depthDescriptor = [MTLTextureDescriptor new]; - depthDescriptor.textureType = MTLTextureType2D; - depthDescriptor.width = width; - depthDescriptor.height = height; - depthDescriptor.depth = 1; - depthDescriptor.pixelFormat = MTLPixelFormatDepth32Float; - depthDescriptor.arrayLength = 1; - depthDescriptor.mipmapLevelCount = 1; - depthDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; - depthDescriptor.resourceOptions = MTLResourceStorageModePrivate; - target->impl._depthTex = (__bridge_retained void *)[device newTextureWithDescriptor:depthDescriptor]; - } } void gpu_destroy(void) { @@ -261,16 +248,14 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { // argument_buffer_step += (align - (argument_buffer_step % align)) % align; argument_buffer = [device newBufferWithLength:(argument_buffer_step * 2048) options:MTLResourceStorageModeShared]; - for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { - render_target_init(&framebuffers[i], iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_RGBA32, depth_buffer_bits, i); - } + gpu_create_framebuffers(depth_buffer_bits); next_drawable(); } -void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { +void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { - has_depth = current_render_targets[0]->impl._depthTex != nil; + has_depth = depth_buffer != NULL; MTLRenderPassDescriptor *desc = [MTLRenderPassDescriptor renderPassDescriptor]; for (int i = 0; i < current_render_targets_count; ++i) { @@ -289,7 +274,10 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsi } } - desc.depthAttachment.texture = (__bridge id)current_render_targets[0]->impl._depthTex; + if (depth_buffer != NULL) { + desc.depthAttachment.texture = (__bridge id)depth_buffer->impl._tex; + } + if (flags & GPU_CLEAR_DEPTH) { desc.depthAttachment.clearDepth = depth; desc.depthAttachment.loadAction = MTLLoadActionClear; @@ -466,14 +454,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) { [command_encoder useResource:tex usage:MTLResourceUsageRead stages:MTLRenderStageVertex|MTLRenderStageFragment]; } -void gpu_set_texture_depth(int unit, gpu_texture_t *target) { - id depth_tex = (__bridge id)target->impl._depthTex; - int i = constant_buffer_index; - [argument_encoder setArgumentBuffer:argument_buffer offset:argument_buffer_step * i]; - [argument_encoder setTexture:depth_tex atIndex:unit + 2]; - [command_encoder useResource:depth_tex usage:MTLResourceUsageRead stages:MTLRenderStageVertex|MTLRenderStageFragment]; -} - void gpu_pipeline_init(gpu_pipeline_t *pipeline) { memset(&pipeline->impl, 0, sizeof(pipeline->impl)); gpu_internal_pipeline_init(pipeline); @@ -674,10 +654,6 @@ void gpu_texture_destroy(gpu_texture_t *target) { tex = nil; target->impl._tex = NULL; - id depthTex = (__bridge_transfer id)target->impl._depthTex; - depthTex = nil; - target->impl._depthTex = NULL; - id texReadback = (__bridge_transfer id)target->impl._texReadback; texReadback = nil; target->impl._texReadback = NULL; @@ -755,18 +731,14 @@ void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int l bytesPerRow:mipmap->width * format_byte_size(mipmap->format)]; } -void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depth_buffer_bits) { - render_target_init(target, width, height, format, depth_buffer_bits, -1); +void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) { + gpu_render_target_init2(target, width, height, format, -1); target->width = width; target->height = height; target->state = GPU_TEXTURE_STATE_RENDER_TARGET; target->uploaded = true; } -void gpu_render_target_set_depth_from(gpu_texture_t *target, gpu_texture_t *source) { - target->impl._depthTex = source->impl._depthTex; -} - void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure) { memset(&buffer->impl, 0, sizeof(buffer->impl)); buffer->count = count; diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index cae4c09b..63824bd7 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -85,6 +85,8 @@ static VkFormat convert_image_format(gpu_texture_format_t format) { case GPU_TEXTURE_FORMAT_RGBA32: // return VK_FORMAT_R8G8B8A8_UNORM; return VK_FORMAT_B8G8R8A8_UNORM; + case GPU_TEXTURE_FORMAT_D32: + return VK_FORMAT_D32_SFLOAT; default: return VK_FORMAT_B8G8R8A8_UNORM; } @@ -259,25 +261,27 @@ void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after) }; vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, NULL, 0, NULL, 1, &barrier); - if (render_target->impl.depth_buffer_bits > 0) { - VkImageMemoryBarrier barrier = { - .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .pNext = NULL, - .srcAccessMask = access_mask(convert_texture_state(render_target->state)), - .dstAccessMask = access_mask(convert_texture_state(state_after)), - .oldLayout = convert_texture_state(render_target->state) == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ? VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL : convert_texture_state(render_target->state), - .newLayout = convert_texture_state(state_after) == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ? VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL : convert_texture_state(state_after), - .image = render_target->impl.depthImage, - .subresourceRange = { - .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }, - }; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, NULL, 0, NULL, 1, &barrier); - } + // if (render_target->format == GPU_TEXTURE_FORMAT_D32) { + // VkImageMemoryBarrier barrier = { + // .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, + // .pNext = NULL, + // .srcAccessMask = access_mask(convert_texture_state(render_target->state)), + // .dstAccessMask = access_mask(convert_texture_state(state_after)), + // .oldLayout = convert_texture_state(render_target->state) == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ? VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL : convert_texture_state(render_target->state), + // .newLayout = convert_texture_state(state_after) == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ? VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL : convert_texture_state(state_after), + // .image = render_target->impl.image, + // .subresourceRange = { + // .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, + // .baseMipLevel = 0, + // .levelCount = 1, + // .baseArrayLayer = 0, + // .layerCount = 1, + // }, + // }; + // vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, NULL, 0, NULL, 1, &barrier); + // } + // else { + // } render_target->state = state_after; } @@ -409,16 +413,13 @@ VkSwapchainKHR cleanup_swapchain() { return chain; } -static void render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depth_bits, int framebuffer_index) { +void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int framebuffer_index) { target->width = width; target->height = height; target->data = NULL; target->impl.format = convert_image_format(format); - target->impl.depth_buffer_bits = depth_bits; target->impl.stage = 0; - target->impl.stage_depth = -1; target->impl.readback_buffer_created = false; - target->depth_state = GPU_TEXTURE_STATE_SHADER_RESOURCE; target->uploaded = true; target->state = (framebuffer_index >= 0) ? GPU_TEXTURE_STATE_PRESENT : GPU_TEXTURE_STATE_SHADER_RESOURCE; @@ -441,17 +442,24 @@ static void render_target_init(gpu_texture_t *target, int width, int height, gpu .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, - .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, .flags = 0, }; + if (format == GPU_TEXTURE_FORMAT_D32) { + image.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + } + else { + .usage = , + image.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + } + VkImageViewCreateInfo color_image_view = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .pNext = NULL, .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = target->impl.format, .flags = 0, - .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, + .subresourceRange.aspectMask = format == GPU_TEXTURE_FORMAT_D32 ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT, .subresourceRange.baseMipLevel = 0, .subresourceRange.levelCount = 1, .subresourceRange.baseArrayLayer = 0, @@ -471,60 +479,9 @@ static void render_target_init(gpu_texture_t *target, int width, int height, gpu memory_type_from_properties(memory_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &allocation_nfo.memoryTypeIndex); vkAllocateMemory(device, &allocation_nfo, NULL, &target->impl.mem); vkBindImageMemory(device, target->impl.image, target->impl.mem, 0); - set_image_layout(target->impl.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + set_image_layout(target->impl.image, format == GPU_TEXTURE_FORMAT_D32 ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); color_image_view.image = target->impl.image; vkCreateImageView(device, &color_image_view, NULL, &target->impl.view); - - if (depth_bits > 0) { - const VkFormat depth_format = VK_FORMAT_D32_SFLOAT; - VkImageCreateInfo image = { - .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, - .pNext = NULL, - .imageType = VK_IMAGE_TYPE_2D, - .format = depth_format, - .extent.width = width, - .extent.height = height, - .extent.depth = 1, - .mipLevels = 1, - .arrayLayers = 1, - .samples = VK_SAMPLE_COUNT_1_BIT, - .tiling = VK_IMAGE_TILING_OPTIMAL, - .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, - .flags = 0, - }; - - vkCreateImage(device, &image, NULL, &target->impl.depthImage); - - VkMemoryAllocateInfo mem_alloc = { - .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, - .pNext = NULL, - .allocationSize = 0, - .memoryTypeIndex = 0, - }; - - VkImageViewCreateInfo view = { - .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .pNext = NULL, - .image = target->impl.depthImage, - .format = depth_format, - .subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, - .subresourceRange.baseMipLevel = 0, - .subresourceRange.levelCount = 1, - .subresourceRange.baseArrayLayer = 0, - .subresourceRange.layerCount = 1, - .flags = 0, - .viewType = VK_IMAGE_VIEW_TYPE_2D, - }; - - VkMemoryRequirements mem_reqs = {0}; - vkGetImageMemoryRequirements(device, target->impl.depthImage, &mem_reqs); - mem_alloc.allocationSize = mem_reqs.size; - memory_type_from_properties(mem_reqs.memoryTypeBits, 0, /* No requirements */ &mem_alloc.memoryTypeIndex); - vkAllocateMemory(device, &mem_alloc, NULL, &target->impl.depthMemory); - vkBindImageMemory(device, target->impl.depthImage, target->impl.depthMemory, 0); - set_image_layout(target->impl.depthImage, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - vkCreateImageView(device, &view, NULL, &target->impl.depthView); - } } static void create_swapchain() { @@ -680,16 +637,16 @@ static void create_swapchain() { }; VkMemoryRequirements mem_reqs = {0}; - vkCreateImage(device, &image, NULL, &framebuffers[0].impl.depthImage); - vkGetImageMemoryRequirements(device, framebuffers[0].impl.depthImage, &mem_reqs); + vkCreateImage(device, &image, NULL, &framebuffer_depth.impl.image); + vkGetImageMemoryRequirements(device, framebuffer_depth.impl.image, &mem_reqs); mem_alloc.allocationSize = mem_reqs.size; memory_type_from_properties(mem_reqs.memoryTypeBits, 0, &mem_alloc.memoryTypeIndex); - vkAllocateMemory(device, &mem_alloc, NULL, &framebuffers[0].impl.depthMemory); - vkBindImageMemory(device, framebuffers[0].impl.depthImage, framebuffers[0].impl.depthMemory, 0); - set_image_layout(framebuffers[0].impl.depthImage, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + vkAllocateMemory(device, &mem_alloc, NULL, &framebuffer_depth.impl.mem); + vkBindImageMemory(device, framebuffer_depth.impl.image, framebuffer_depth.impl.memory, 0); + set_image_layout(framebuffer_depth.impl.image, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - view.image = framebuffers[0].impl.depthImage; - vkCreateImageView(device, &view, NULL, &framebuffers[0].impl.depthView); + view.image = framebuffer_depth.impl.image; + vkCreateImageView(device, &view, NULL, &framebuffer_depth.impl.view); } } @@ -1075,9 +1032,7 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { }; vkBeginCommandBuffer(command_buffer, &begin_info); - for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { - render_target_init(&framebuffers[i], iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_RGBA32, depth_buffer_bits, i); - } + gpu_create_framebuffers(depth_buffer_bits); create_swapchain(); @@ -1143,7 +1098,7 @@ static void set_viewport_and_scissor() { vkCmdSetScissor(command_buffer, 0, 1, &scissor); } -void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { +void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * depth_buffer, unsigned flags, unsigned color, float depth) { gpu_texture_t *target = current_render_targets[0]; @@ -1175,11 +1130,11 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsi }; } - if (target->impl.depth_buffer_bits > 0) { + if (depth_buffer != NULL) { current_depth_attachment_info = (VkRenderingAttachmentInfo) { .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .pNext = NULL, - .imageView = target->impl.depthView, + .imageView = depth_buffer->impl.view, .imageLayout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL, .resolveMode = VK_RESOLVE_MODE_NONE, .resolveImageView = VK_NULL_HANDLE, @@ -1199,7 +1154,7 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsi .viewMask = 0, .colorAttachmentCount = (uint32_t)current_render_targets_count, .pColorAttachments = current_color_attachment_infos, - .pDepthAttachment = target->impl.depth_buffer_bits == 0 ? VK_NULL_HANDLE : ¤t_depth_attachment_info, + .pDepthAttachment = depth_buffer == NULL ? VK_NULL_HANDLE : ¤t_depth_attachment_info, .pStencilAttachment = VK_NULL_HANDLE, }; vkCmdBeginRendering(command_buffer, ¤t_rendering_info); @@ -1453,13 +1408,7 @@ static int write_tex_descs(VkDescriptorImageInfo *tex_descs) { int texture_count = 0; for (int i = 0; i < 16; ++i) { if (current_textures[i] != NULL) { - if (current_textures[i]->impl.stage_depth == i) { - tex_descs[i].imageView = current_textures[i]->impl.depthView; - current_textures[i]->impl.stage_depth = -1; - } - else { - tex_descs[i].imageView = current_textures[i]->impl.view; - } + tex_descs[i].imageView = current_textures[i]->impl.view; tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; texture_count++; } @@ -1521,13 +1470,7 @@ static VkDescriptorSet get_descriptor_set() { int texture_count = 0; for (int i = 0; i < 16; ++i) { if (current_textures[i] != NULL) { - if (current_textures[i]->impl.stage_depth == i) { - tex_desc[i].imageView = current_textures[i]->impl.depthView; - current_textures[i]->impl.stage_depth = -1; - } - else { - tex_desc[i].imageView = current_textures[i]->impl.view; - } + tex_desc[i].imageView = current_textures[i]->impl.view; texture_count++; } tex_desc[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; @@ -1577,11 +1520,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) { current_textures[unit] = texture; } -void gpu_set_texture_depth(int unit, gpu_texture_t *target) { - target->impl.stage_depth = unit; - current_textures[unit] = target; -} - void gpu_pipeline_init(gpu_pipeline_t *pipeline) { gpu_internal_pipeline_init(pipeline); } @@ -1951,9 +1889,7 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, texture->format = format; texture->data = data; texture->impl.stage = 0; - texture->impl.stage_depth = -1; texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE; - texture->depth_state = GPU_TEXTURE_STATE_SHADER_RESOURCE; const VkFormat tex_format = convert_image_format(format); VkFormatProperties props; @@ -2033,9 +1969,7 @@ void gpu_texture_init(gpu_texture_t *texture, int width, int height, gpu_texture texture->format = format; texture->data = NULL; texture->impl.stage = 0; - texture->impl.stage_depth = -1; texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE; - texture->depth_state = GPU_TEXTURE_STATE_SHADER_RESOURCE; VkFormat tex_format = convert_image_format(format); VkFormatProperties props; @@ -2075,11 +2009,6 @@ void gpu_texture_destroy(gpu_texture_t *target) { if (target->impl.view != NULL) { vkDestroyImageView(device, target->impl.view, NULL); } - if (target->impl.depth_buffer_bits > 0) { - vkDestroyImageView(device, target->impl.depthView, NULL); - vkDestroyImage(device, target->impl.depthImage, NULL); - vkFreeMemory(device, target->impl.depthMemory, NULL); - } } int gpu_texture_stride(gpu_texture_t *texture) { @@ -2153,16 +2082,8 @@ void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int l // texture->uploaded = true; } -void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depth_bits) { - render_target_init(target, width, height, format, depth_bits, -1); -} - -void gpu_render_target_set_depth_from(gpu_texture_t *target, gpu_texture_t *source) { - target->impl.depthImage = source->impl.depthImage; - target->impl.depthMemory = source->impl.depthMemory; - target->impl.depthView = source->impl.depthView; - target->impl.depth_buffer_bits = source->impl.depth_buffer_bits; - target->depth_state = source->depth_state; +void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) { + gpu_render_target_init2(target, width, height, format, -1); } void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure) { diff --git a/base/sources/backends/vulkan_gpu.h b/base/sources/backends/vulkan_gpu.h index 4dd7532b..a45eba63 100644 --- a/base/sources/backends/vulkan_gpu.h +++ b/base/sources/backends/vulkan_gpu.h @@ -29,16 +29,11 @@ typedef struct { VkImage image; VkDeviceMemory mem; VkImageView view; - VkImage depthImage; - VkDeviceMemory depthMemory; - VkImageView depthView; - int depth_buffer_bits; VkFormat format; VkBuffer readback_buffer; VkDeviceMemory readback_memory; bool readback_buffer_created; int stage; - int stage_depth; } gpu_texture_impl_t; typedef struct { diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index d43f74f0..8c51d559 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -23,7 +23,7 @@ gpu_buffer_t *gpu_internal_current_index_buffer = NULL; void gpu_destroy() {} -void gpu_init_internal(int depthBufferBits, bool vsync) { +void gpu_init_internal(int depth_bits, bool vsync) { newRenderTargetWidth = renderTargetWidth = iron_window_width(); newRenderTargetHeight = renderTargetHeight = iron_window_height(); @@ -50,7 +50,7 @@ void gpu_init_internal(int depthBufferBits, bool vsync) { swapChain = wgpuDeviceCreateSwapChain(device, surface, &scDesc); } -void gpu_begin_internal(struct gpu_texture **targets, int count, unsigned flags, unsigned color, float depth) { +void gpu_begin_internal(struct gpu_texture **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { WGPUCommandEncoderDescriptor ceDesc; memset(&ceDesc, 0, sizeof(ceDesc)); encoder = wgpuDeviceCreateCommandEncoder(device, &ceDesc); @@ -197,7 +197,7 @@ int gpu_texture_stride(gpu_texture_t *texture) { void gpu_texture_generate_mipmaps(gpu_texture_t *texture, int levels) {} void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {} -void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depthBufferBits) { +void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) { target->width = target->width = width; target->height = target->height = height; target->state = GPU_TEXTURE_STATE_RENDER_TARGET; @@ -205,8 +205,7 @@ void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_te target->uploaded = true; } -void gpu_render_target_init_framebuffer(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depthBufferBits) {} -void gpu_render_target_set_depth_from(gpu_texture_t *renderTarget, gpu_texture_t *source) {} +void gpu_render_target_init_framebuffer(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {} void gpu_pipeline_init(gpu_pipeline_t *pipe) { gpu_internal_pipeline_init(pipe); @@ -354,4 +353,3 @@ void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) { void gpu_wait() {} void gpu_set_constant_buffer(struct gpu_buffer *buffer, int offset, size_t size) {} void gpu_set_texture(int unit, gpu_texture_t *texture) {} -void gpu_set_texture_depth(int unit, gpu_texture_t *renderTarget) {} diff --git a/base/sources/backends/windows_system.c b/base/sources/backends/windows_system.c index c98025ae..582d3880 100644 --- a/base/sources/backends/windows_system.c +++ b/base/sources/backends/windows_system.c @@ -2064,12 +2064,9 @@ void iron_window_create(iron_window_options_t *win) { wchar_t wbuffer[1024]; MultiByteToWideChar(CP_UTF8, 0, win->title, -1, wbuffer, 1024); - createWindow(wbuffer, win->x, win->y, win->width, win->height, win->color_bits, win->frequency, win->features, win->mode, - win->display_index); + createWindow(wbuffer, win->x, win->y, win->width, win->height, win->color_bits, win->frequency, win->features, win->mode, win->display_index); - bool vsync = win->vsync; - - gpu_init(win->depth_bits, vsync); + gpu_init(win->depth_bits, win->vsync); if (win->visible) { iron_window_show(); diff --git a/base/sources/iron.h b/base/sources/iron.h index 90915521..78efbec3 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -780,7 +780,6 @@ void _iron_init(iron_window_options_t *ops) { ops->display_index = -1; ops->visible = enable_window; ops->color_bits = 32; - ops->depth_bits = ops->use_depth ? 24 : 0; iron_init(ops->title, ops->width, ops->height, ops); iron_random_init((int)(iron_time() * 1000)); @@ -1264,9 +1263,9 @@ bool iron_display_is_primary(i32 index) { return index == iron_primary_display(); } -gpu_texture_t *gpu_create_render_target(i32 width, i32 height, i32 format, i32 depth_buffer_bits) { +gpu_texture_t *gpu_create_render_target(i32 width, i32 height, i32 format) { gpu_texture_t *render_target = (gpu_texture_t *)malloc(sizeof(gpu_texture_t)); - gpu_render_target_init(render_target, width, height, (gpu_texture_format_t)format, depth_buffer_bits); + gpu_render_target_init(render_target, width, height, (gpu_texture_format_t)format); render_target->buffer = NULL; return render_target; } @@ -1395,9 +1394,9 @@ void gpu_set_mipmaps(gpu_texture_t *texture, any_array_t *mipmaps) { } } -void _gpu_begin(gpu_texture_t *render_target, any_array_t *additional, unsigned flags, unsigned color, float depth) { +void _gpu_begin(gpu_texture_t *render_target, any_array_t *additional, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { if (render_target == NULL) { - gpu_begin(NULL, 0, flags, color, depth); + gpu_begin(NULL, 0, NULL, flags, color, depth); } else { int32_t length = 1; @@ -1408,7 +1407,7 @@ void _gpu_begin(gpu_texture_t *render_target, any_array_t *additional, unsigned render_targets[i] = additional->buffer[i - 1]; } } - gpu_begin(render_targets, length, flags, color, depth); + gpu_begin(render_targets, length, depth_buffer, flags, color, depth); } } diff --git a/base/sources/iron_draw.c b/base/sources/iron_draw.c index 18567e1b..1d4c58a9 100644 --- a/base/sources/iron_draw.c +++ b/base/sources/iron_draw.c @@ -216,11 +216,11 @@ void draw_begin(gpu_texture_t *target, bool clear, unsigned color) { draw_set_color(0xffffffff); _draw_current = target; if (target == NULL) { - gpu_begin(NULL, 0, clear ? GPU_CLEAR_COLOR : GPU_CLEAR_NONE, color, 0.0); + gpu_begin(NULL, 0, NULL, clear ? GPU_CLEAR_COLOR : GPU_CLEAR_NONE, color, 0.0); } else { gpu_texture_t *targets[1] = { target }; - gpu_begin(targets, 1, clear ? GPU_CLEAR_COLOR : GPU_CLEAR_NONE, color, 0.0); + gpu_begin(targets, 1, NULL, clear ? GPU_CLEAR_COLOR : GPU_CLEAR_NONE, color, 0.0); } } diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index 76f910c5..b2fdec66 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -12,6 +12,7 @@ bool gpu_in_use = false; gpu_texture_t *current_render_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int current_render_targets_count = 0; gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT]; +gpu_texture_t framebuffer_depth; int framebuffer_index = 0; void gpu_init(int depth_buffer_bits, bool vsync) { @@ -20,7 +21,7 @@ void gpu_init(int depth_buffer_bits, bool vsync) { gpu_constant_buffer_lock(&constant_buffer, 0, CONSTANT_BUFFER_SIZE); } -void gpu_begin(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { +void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) { if (gpu_in_use && !gpu_thrown) { gpu_thrown = true; iron_log("End before you begin"); @@ -48,7 +49,7 @@ void gpu_begin(gpu_texture_t **targets, int count, unsigned flags, unsigned colo gpu_barrier(current_render_targets[i], GPU_TEXTURE_STATE_RENDER_TARGET); } - gpu_begin_internal(targets, count, flags, color, depth); + gpu_begin_internal(targets, count, depth_buffer, flags, color, depth); } void gpu_draw() { @@ -213,3 +214,12 @@ void gpu_internal_pipeline_init(gpu_pipeline_t *pipe) { pipe->color_attachment_count = 1; pipe->depth_attachment_bits = 0; } + +void gpu_create_framebuffers(int depth_buffer_bits) { + for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { + gpu_render_target_init2(&framebuffers[i], iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_RGBA32, i); + } + if (depth_buffer_bits > 0) { + gpu_render_target_init(&framebuffer_depth, iron_window_width(), iron_window_height(), GPU_TEXTURE_FORMAT_D32); + } +} diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index 830ee7bc..84eb2dd5 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -33,7 +33,8 @@ typedef enum gpu_texture_format { GPU_TEXTURE_FORMAT_RGBA128, GPU_TEXTURE_FORMAT_R8, GPU_TEXTURE_FORMAT_R16, - GPU_TEXTURE_FORMAT_R32 + GPU_TEXTURE_FORMAT_R32, + GPU_TEXTURE_FORMAT_D32 } gpu_texture_format_t; typedef enum gpu_vertex_data { @@ -84,7 +85,6 @@ typedef struct gpu_texture { void *data; bool uploaded; gpu_texture_state_t state; - gpu_texture_state_t depth_state; buffer_t *buffer; gpu_texture_impl_t impl; } gpu_texture_t; @@ -142,13 +142,14 @@ typedef struct gpu_raytrace_acceleration_structure { } gpu_raytrace_acceleration_structure_t; int gpu_max_bound_textures(void); -void gpu_begin(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth); -void gpu_begin_internal(gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth); +void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth); +void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth); void gpu_end(void); void gpu_end_internal(void); void gpu_wait(void); void gpu_present(void); void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after); +void gpu_create_framebuffers(int depth_buffer_bits); void gpu_init(int depth_buffer_bits, bool vsync); void gpu_init_internal(int depth_buffer_bits, bool vsync); void gpu_destroy(void); @@ -174,8 +175,8 @@ void gpu_texture_destroy(gpu_texture_t *texture); void gpu_texture_generate_mipmaps(gpu_texture_t *texture, int levels); void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level); int gpu_texture_stride(gpu_texture_t *texture); -void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int depthBufferBits); -void gpu_render_target_set_depth_from(gpu_texture_t *target, gpu_texture_t *source); +void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format); +void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int framebuffer_index); void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure); float *gpu_vertex_buffer_lock(gpu_buffer_t *buffer); void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer); @@ -210,7 +211,6 @@ void gpu_upload_texture(gpu_texture_t *texture); void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size); void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data); void gpu_set_texture(int unit, gpu_texture_t *texture); -void gpu_set_texture_depth(int unit, gpu_texture_t *target); bool gpu_raytrace_supported(void); void gpu_raytrace_pipeline_init(gpu_raytrace_pipeline_t *pipeline, void *ray_shader, int ray_shader_size, gpu_buffer_t *constant_buffer); @@ -256,4 +256,5 @@ extern gpu_texture_t *current_render_targets[8]; extern int current_render_targets_count; extern int constant_buffer_index; extern gpu_texture_t framebuffers[GPU_FRAMEBUFFER_COUNT]; +extern gpu_texture_t framebuffer_depth; extern int framebuffer_index; diff --git a/base/sources/iron_system.c b/base/sources/iron_system.c index 55b568ba..ea21196b 100644 --- a/base/sources/iron_system.c +++ b/base/sources/iron_system.c @@ -88,11 +88,10 @@ void iron_window_options_set_defaults(iron_window_options_t *win) { win->height = 600; win->visible = true; win->features = IRON_WINDOW_FEATURE_RESIZEABLE | IRON_WINDOW_FEATURE_MINIMIZABLE | IRON_WINDOW_FEATURE_MAXIMIZABLE; - win->frequency = 60; win->vsync = true; win->color_bits = 32; - win->depth_bits = 16; + win->depth_bits = 32; } #if !defined(IRON_WASM) && !defined(IRON_ANDROID) && !defined(IRON_WINDOWS) diff --git a/base/sources/iron_system.h b/base/sources/iron_system.h index 6fba00b5..1b988572 100644 --- a/base/sources/iron_system.h +++ b/base/sources/iron_system.h @@ -49,14 +49,10 @@ typedef struct iron_window_options { iron_window_mode_t mode; int frequency; bool vsync; - - bool use_depth; - int display_index; bool visible; int color_bits; int depth_bits; - } iron_window_options_t; void iron_window_create(iron_window_options_t *win); diff --git a/base/sources/iron_ui.c b/base/sources/iron_ui.c index c6d4431c..09e47f2a 100644 --- a/base/sources/iron_ui.c +++ b/base/sources/iron_ui.c @@ -512,7 +512,7 @@ void ui_resize(ui_handle_t *handle, int w, int h) { if (h < 1) { h = 1; } - gpu_render_target_init(&handle->texture, w, h, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(&handle->texture, w, h, GPU_TEXTURE_FORMAT_RGBA32); } bool ui_input_in_rect(float x, float y, float w, float h) { @@ -901,7 +901,7 @@ void ui_bake_elements() { gpu_texture_destroy(¤t->check_select_image); } float r = UI_CHECK_SELECT_SIZE(); - gpu_render_target_init(¤t->check_select_image, r, r, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(¤t->check_select_image, r, r, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(¤t->check_select_image, true, 0x00000000); draw_set_color(0xffffffff); draw_line(0, r / 2.0, r / 2.0 - 2.0 * UI_SCALE(), r - 2.0 * UI_SCALE(), 2.0 * UI_SCALE()); @@ -912,7 +912,7 @@ void ui_bake_elements() { gpu_texture_destroy(¤t->radio_image); } r = UI_CHECK_SIZE(); - gpu_render_target_init(¤t->radio_image, r, r, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(¤t->radio_image, r, r, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(¤t->radio_image, true, 0x00000000); draw_set_color(0xffaaaaaa); draw_filled_circle(r / 2.0, r / 2.0, r / 2.0, 0); @@ -924,7 +924,7 @@ void ui_bake_elements() { gpu_texture_destroy(¤t->radio_select_image); } r = UI_CHECK_SELECT_SIZE(); - gpu_render_target_init(¤t->radio_select_image, r, r, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(¤t->radio_select_image, r, r, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(¤t->radio_select_image, true, 0x00000000); draw_set_color(0xffaaaaaa); draw_filled_circle(r / 2.0, r / 2.0, 4.5 * UI_SCALE(), 0); @@ -937,7 +937,7 @@ void ui_bake_elements() { gpu_texture_destroy(¤t->filled_round_corner_image); } r = 4.0 * UI_SCALE(); - gpu_render_target_init(¤t->filled_round_corner_image, r, r, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(¤t->filled_round_corner_image, r, r, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(¤t->filled_round_corner_image, true, 0x00000000); draw_set_color(0xffffffff); draw_filled_circle(r, r, r, 0); @@ -946,7 +946,7 @@ void ui_bake_elements() { if (current->round_corner_image.width != 0) { gpu_texture_destroy(¤t->round_corner_image); } - gpu_render_target_init(¤t->round_corner_image, r, r, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(¤t->round_corner_image, r, r, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(¤t->round_corner_image, true, 0x00000000); draw_set_color(0xffffffff); draw_circle(r, r, r, 0, 1); diff --git a/base/sources/iron_ui_nodes.c b/base/sources/iron_ui_nodes.c index 056b4c19..d4455d15 100644 --- a/base/sources/iron_ui_nodes.c +++ b/base/sources/iron_ui_nodes.c @@ -238,7 +238,7 @@ void ui_nodes_bake_elements() { if (ui_socket_image.width != 0) { gpu_texture_destroy(&ui_socket_image); } - gpu_render_target_init(&ui_socket_image, 24, 24, GPU_TEXTURE_FORMAT_RGBA32, 0); + gpu_render_target_init(&ui_socket_image, 24, 24, GPU_TEXTURE_FORMAT_RGBA32); draw_begin(&ui_socket_image, true, 0x00000000); draw_set_color(0xff111111); draw_filled_circle(12, 12, 11, 0); diff --git a/base/sources/ts/config.ts b/base/sources/ts/config.ts index 9f4c8523..ee283ac3 100644 --- a/base/sources/ts/config.ts +++ b/base/sources/ts/config.ts @@ -211,8 +211,7 @@ function config_get_options(): iron_window_options_t { mode: window_mode, features: features, vsync: config_raw.window_vsync, - frequency: config_raw.window_frequency, - use_depth: false + frequency: config_raw.window_frequency }; return ops; } diff --git a/base/sources/ts/export_texture.ts b/base/sources/ts/export_texture.ts index 90d11a67..fdd7eccb 100644 --- a/base/sources/ts/export_texture.ts +++ b/base/sources/ts/export_texture.ts @@ -190,11 +190,11 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ } // Clear export layer - _gpu_begin(layers_expa, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expa, null, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); gpu_end(); - _gpu_begin(layers_expb, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(layers_expb, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); gpu_end(); - _gpu_begin(layers_expc, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expc, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); gpu_end(); // Flatten layers diff --git a/base/sources/ts/import_arm.ts b/base/sources/ts/import_arm.ts index 59499064..63e92468 100644 --- a/base/sources/ts/import_arm.ts +++ b/base/sources/ts/import_arm.ts @@ -202,7 +202,7 @@ function import_arm_run_project(path: string) { }, _texpaint_blend0); blend0.width = config_get_texture_res_x(); blend0.height = config_get_texture_res_y(); - blend0._image = gpu_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8, 0); + blend0._image = gpu_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8); let blend1: render_target_t = map_get(rts, "texpaint_blend1"); let _texpaint_blend1: gpu_texture_t = blend1._image; sys_notify_on_next_frame(function (_texpaint_blend1: gpu_texture_t) { @@ -210,7 +210,7 @@ function import_arm_run_project(path: string) { }, _texpaint_blend1); blend1.width = config_get_texture_res_x(); blend1.height = config_get_texture_res_y(); - blend1._image = gpu_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8, 0); + blend1._image = gpu_create_render_target(config_get_texture_res_x(), config_get_texture_res_y(), tex_format_t.R8); context_raw.brush_blend_dirty = true; } diff --git a/base/sources/ts/iron/iron.ts b/base/sources/ts/iron/iron.ts index 98a3c7a4..4a8d63cf 100644 --- a/base/sources/ts/iron/iron.ts +++ b/base/sources/ts/iron/iron.ts @@ -241,7 +241,6 @@ declare function iron_load_url(url: string): void; declare function iron_copy_to_clipboard(text: string): void; declare function gpu_set_texture(stage: i32, texture: gpu_texture_t): void; -declare function gpu_set_texture_depth(unit: i32, texture: gpu_texture_t): void; declare function gpu_set_bool(location: i32, value: bool): void; declare function gpu_set_int(location: i32, value: i32): void; declare function gpu_set_float(location: i32, value: f32): void; @@ -271,17 +270,16 @@ declare function iron_display_y(index: i32): i32; declare function iron_display_frequency(index: i32): i32; declare function iron_display_is_primary(index: i32): bool; -declare function gpu_create_render_target(width: i32, height: i32, format: i32 = tex_format_t.RGBA32, depth_buffer_bits: i32 = 0): any; +declare function gpu_create_render_target(width: i32, height: i32, format: i32 = tex_format_t.RGBA32): any; declare function gpu_create_texture_from_bytes(data: buffer_t, width: i32, height: i32, format: i32 = tex_format_t.RGBA32, readable: bool = true): any; declare function gpu_create_texture_from_encoded_bytes(data: buffer_t, format: string, readable: bool = false): any; declare function gpu_get_texture_pixels(texture: any): buffer_t; declare function gpu_texture_generate_mipmaps(texture: any, levels: i32): void; declare function gpu_set_mipmaps(texture: any, mipmaps: gpu_texture_t[]): void; -declare function gpu_render_target_set_depth_from(target: any, source: any): void; declare function gpu_viewport(x: i32, y: i32, width: i32, height: i32): void; declare function gpu_scissor(x: i32, y: i32, width: i32, height: i32): void; declare function gpu_disable_scissor(): void; -declare function _gpu_begin(render_target: gpu_texture_t, additional: gpu_texture_t[] = null, flags: i32 = clear_flag_t.NONE, color: i32 = 0, depth: f32 = 0.0): void; +declare function _gpu_begin(render_target: gpu_texture_t, additional: gpu_texture_t[] = null, depth_buffer: gpu_texture_t = null, flags: i32 = clear_flag_t.NONE, color: i32 = 0, depth: f32 = 0.0): void; declare function gpu_end(): void; declare function gpu_present(): void; declare function iron_file_save_bytes(path: string, bytes: buffer_t, length?: i32): void; @@ -654,6 +652,7 @@ enum tex_format_t { R8, R16, R32, + D32, } enum vertex_data_t { diff --git a/base/sources/ts/iron/render_path.ts b/base/sources/ts/iron/render_path.ts index 05f3b61a..54003167 100644 --- a/base/sources/ts/iron/render_path.ts +++ b/base/sources/ts/iron/render_path.ts @@ -6,24 +6,15 @@ type render_target_t = { // Opt format?: string; scale?: f32; - depth_buffer?: string; // 2D texture mipmaps?: bool; // Runtime - _depth_buffer_bits?: i32; - _depth_from?: string; - _image?: gpu_texture_t; // RT or image - _has_depth?: bool; + _image?: gpu_texture_t; }; type cached_shader_context_t = { context?: shader_context_t; }; -type depth_buffer_desc_t = { - name?: string; - format?: string; -}; - enum draw_order_t { DIST, // Early-z SHADER, // Less state changes @@ -39,7 +30,6 @@ let _render_path_current_target: render_target_t = null; let _render_path_current_image: gpu_texture_t = null; let _render_path_draw_order: draw_order_t = draw_order_t.DIST; let _render_path_paused: bool = false; -let _render_path_depth_to_render_target: map_t = map_create(); let _render_path_last_w: i32 = 0; let _render_path_last_h: i32 = 0; let _render_path_bind_params: string[]; @@ -48,18 +38,17 @@ let _render_path_scissor_set: bool = false; let _render_path_last_frame_time: f32 = 0.0; let _render_path_loading: i32 = 0; let _render_path_cached_shader_contexts: map_t = map_create(); -let _render_path_depth_buffers: depth_buffer_desc_t[] = []; function render_path_ready(): bool { return _render_path_loading == 0; } function render_path_render_frame() { - if (!render_path_ready() ||_render_path_paused || sys_w() == 0 || sys_h() == 0) { + if (!render_path_ready() || _render_path_paused || sys_w() == 0 || sys_h() == 0) { return; } - if (_render_path_last_w > 0 && (_render_path_last_w != sys_w() ||_render_path_last_h != sys_h())) { + if (_render_path_last_w > 0 && (_render_path_last_w != sys_w() || _render_path_last_h != sys_h())) { render_path_resize(); } _render_path_last_w = sys_w(); @@ -77,12 +66,17 @@ function render_path_render_frame() { _render_path_frame++; } -function render_path_set_target(target: string, additional: string[] = null, flags: i32 = clear_flag_t.NONE, color: i32 = 0, depth: f32 = 0.0) { +function render_path_set_target(target: string, additional: string[] = null, depth_buffer: string = null, flags: i32 = clear_flag_t.NONE, color: i32 = 0, depth: f32 = 0.0) { + if (_render_path_current_image != null) { + render_path_end(); + } + _render_path_current_image = render_target; + if (target == "") { // Framebuffer _render_path_current_target = null; render_path_current_w = sys_w(); render_path_current_h = sys_h(); - render_path_begin(null, null, flags, color, depth); + _gpu_begin(null, null, null, flags, color, depth); render_path_set_current_viewport(sys_w(), sys_h()); render_path_set_current_scissor(sys_w(), sys_h()); } @@ -100,25 +94,12 @@ function render_path_set_target(target: string, additional: string[] = null, fla } render_path_current_w = rt._image.width; render_path_current_h = rt._image.height; - render_path_begin(rt._image, additional_images, flags, color, depth); + let db: render_target_t = map_get(render_path_render_targets, depth_buffer); + _gpu_begin(rt._image, additional_images, db != null ? db._image : null, flags, color, depth); } _render_path_bind_params = null; } -function render_path_set_depth_from(target: string, from: string) { - let rt: render_target_t = map_get(render_path_render_targets, target); - let rt_from: render_target_t = map_get(render_path_render_targets, from); - gpu_render_target_set_depth_from(rt._image, rt_from._image); -} - -function render_path_begin(render_target: gpu_texture_t, additional_targets: gpu_texture_t[], flags: i32, color: i32, depth: f32) { - if (_render_path_current_image != null) { - render_path_end(); - } - _render_path_current_image = render_target; - _gpu_begin(render_target, additional_targets, flags, color, depth); -} - function render_path_end() { if (_render_path_scissor_set) { gpu_disable_scissor(); @@ -281,105 +262,29 @@ function render_path_unload() { } } -function _render_path_resize_on_init(_image: gpu_texture_t) { - iron_unload_image(_image); -} - function render_path_resize() { if (iron_window_width() == 0 || iron_window_height() == 0) { return; } - // Make sure depth buffer is attached to single target only and gets released once let render_targets_keys: string[] = map_keys(render_path_render_targets); - for (let i: i32 = 0; i < render_targets_keys.length; ++i) { - let rt: render_target_t = map_get(render_path_render_targets, render_targets_keys[i]); - if (rt == null || rt.width > 0 || rt._depth_from == "" || rt == map_get(_render_path_depth_to_render_target, rt._depth_from)) { - continue; - } - - let nodepth: render_target_t = null; - for (let j: i32 = 0; j < render_targets_keys.length; ++j) { - let rt2: render_target_t = map_get(render_path_render_targets, render_targets_keys[j]); - if (rt2 == null || rt2.width > 0 || rt2._depth_from != "" || map_get(_render_path_depth_to_render_target, rt2.depth_buffer) != null) { - continue; - } - - nodepth = rt2; - break; - } - - if (nodepth != null) { - gpu_render_target_set_depth_from(rt._image, nodepth._image); - } - } - - // Resize textures for (let i: i32 = 0; i < render_targets_keys.length; ++i) { let rt: render_target_t = map_get(render_path_render_targets, render_targets_keys[i]); if (rt != null && rt.width == 0) { let _image: gpu_texture_t = rt._image; - sys_notify_on_init(_render_path_resize_on_init, _image); - rt._image = render_path_create_image(rt, rt._depth_buffer_bits); - } - } - - // Attach depth buffers - for (let i: i32 = 0; i < render_targets_keys.length; ++i) { - let rt: render_target_t = map_get(render_path_render_targets, render_targets_keys[i]); - if (rt != null && rt._depth_from != "") { - let depth_from: render_target_t = map_get(_render_path_depth_to_render_target, rt._depth_from); - gpu_render_target_set_depth_from(rt._image, depth_from._image); + sys_notify_on_init(iron_unload_image, _image); + rt._image = render_path_create_image(rt); } } } function render_path_create_render_target(t: render_target_t): render_target_t { - render_path_create_target(t); + t._image = render_path_create_image(t); map_set(render_path_render_targets, t.name, t); return t; } -function render_path_create_depth_buffer(name: string, format: string = null) { - let desc: depth_buffer_desc_t = {}; - desc.name = name; - desc.format = format; - array_push(_render_path_depth_buffers, desc); -} - -function render_path_create_target(t: render_target_t): render_target_t { - // With depth buffer - if (t.depth_buffer != null) { - t._has_depth = true; - let depth_target: render_target_t = map_get(_render_path_depth_to_render_target, t.depth_buffer); - - if (depth_target == null) { // Create new one - for (let i: i32 = 0; i < _render_path_depth_buffers.length; ++i) { - let db: depth_buffer_desc_t = _render_path_depth_buffers[i]; - if (db.name == t.depth_buffer) { - map_set(_render_path_depth_to_render_target, db.name, t); - t._depth_buffer_bits = 24; - t._image = render_path_create_image(t, t._depth_buffer_bits); - break; - } - } - } - else { // Reuse - t._depth_buffer_bits = 0; - t._depth_from = t.depth_buffer; - t._image = render_path_create_image(t, t._depth_buffer_bits); - gpu_render_target_set_depth_from(t._image, depth_target._image); - } - } - else { // No depth buffer - t._has_depth = false; - t._depth_buffer_bits = 0; - t._image = render_path_create_image(t, t._depth_buffer_bits); - } - return t; -} - -function render_path_create_image(t: render_target_t, depth_buffer_bits: i32): gpu_texture_t { +function render_path_create_image(t: render_target_t): gpu_texture_t { let width: i32 = t.width == 0 ? sys_w() : t.width; let height: i32 = t.height == 0 ? sys_h() : t.height; width = math_floor(width * t.scale); @@ -391,8 +296,7 @@ function render_path_create_image(t: render_target_t, depth_buffer_bits: i32): g height = 1; } return gpu_create_render_target(width, height, - t.format != null ? render_path_get_tex_format(t.format) : tex_format_t.RGBA32, - depth_buffer_bits); + t.format != null ? render_path_get_tex_format(t.format) : tex_format_t.RGBA32); } function render_path_get_tex_format(s: string): tex_format_t { @@ -414,6 +318,9 @@ function render_path_get_tex_format(s: string): tex_format_t { if (s == "R8") { return tex_format_t.R8; } + if (s == "D32") { + return tex_format_t.D32; + } return tex_format_t.RGBA32; } @@ -421,8 +328,6 @@ function render_target_create(): render_target_t { let raw: render_target_t = {}; raw.scale = 1.0; raw.mipmaps = false; - raw._depth_from = ""; - raw._has_depth = false; return raw; } diff --git a/base/sources/ts/iron/scene.ts b/base/sources/ts/iron/scene.ts index 95ee573b..e89e549c 100644 --- a/base/sources/ts/iron/scene.ts +++ b/base/sources/ts/iron/scene.ts @@ -586,7 +586,7 @@ type shader_context_t = { color_writes_blue?: bool[]; color_writes_alpha?: bool[]; color_attachments?: string[]; // RGBA32, RGBA64, R8 - depth_attachment?: string; // DEPTH32 + depth_attachment?: string; // D32 vertex_elements?: vertex_element_t[]; constants?: shader_const_t[]; texture_units?: tex_unit_t[]; diff --git a/base/sources/ts/iron/shader_data.ts b/base/sources/ts/iron/shader_data.ts index 30492378..43ec3d39 100644 --- a/base/sources/ts/iron/shader_data.ts +++ b/base/sources/ts/iron/shader_data.ts @@ -130,7 +130,7 @@ function shader_context_compile(raw: shader_context_t): shader_context_t { // Depth attachment format if (raw.depth_attachment != null) { - raw._.pipe_state.depth_attachment_bits = raw.depth_attachment == "NONE" ? 0 : 24; + raw._.pipe_state.depth_attachment_bits = raw.depth_attachment == "NONE" ? 0 : 32; } // Shaders diff --git a/base/sources/ts/iron/sys.ts b/base/sources/ts/iron/sys.ts index 9ff900b0..3f08bfd3 100644 --- a/base/sources/ts/iron/sys.ts +++ b/base/sources/ts/iron/sys.ts @@ -34,9 +34,6 @@ declare type iron_window_options_t = { mode?: window_mode_t; frequency?: i32; vsync?: bool; - - use_depth?: bool; - display_index?: i32; visible?: bool; color_bits?: i32; diff --git a/base/sources/ts/iron/uniforms.ts b/base/sources/ts/iron/uniforms.ts index 589aeed5..7d0487aa 100644 --- a/base/sources/ts/iron/uniforms.ts +++ b/base/sources/ts/iron/uniforms.ts @@ -31,20 +31,8 @@ function uniforms_set_context_consts(context: shader_context_t, bind_params: str let pos: i32 = i * 2; // bind params = [texture, sampler_id] let rt_id: string = bind_params[pos]; let sampler_id: string = bind_params[pos + 1]; - let attach_depth: bool = false; // Attach texture depth if "_" is prepended - let c: string = char_at(rt_id, 0); - if (c == "_") { - attach_depth = true; - rt_id = substring(rt_id, 1, rt_id.length); - } - let rt: render_target_t; - if (attach_depth) { - rt = map_get(_render_path_depth_to_render_target, rt_id); - } - else { - rt = map_get(render_path_render_targets, rt_id); - } - uniforms_bind_render_target(rt, context, sampler_id, attach_depth); + let rt: render_target_t = map_get(render_path_render_targets, rt_id); + uniforms_bind_render_target(rt, context, sampler_id); } } @@ -95,16 +83,14 @@ function uniforms_set_obj_consts(context: shader_context_t, object: object_t) { let image: 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(context._.tex_units[j], image); + gpu_set_texture(context._.tex_units[j], image); } } } } } -function uniforms_bind_render_target(rt: render_target_t, context: shader_context_t, sampler_id: string, attach_depth: bool) { +function uniforms_bind_render_target(rt: render_target_t, context: shader_context_t, sampler_id: string) { if (rt == null) { return; } @@ -112,12 +98,7 @@ function uniforms_bind_render_target(rt: render_target_t, context: shader_contex let tus: tex_unit_t[] = context.texture_units; for (let j: i32 = 0; j < tus.length; ++j) { // Set texture if (sampler_id == tus[j].name) { - if (attach_depth) { - gpu_set_texture_depth(context._.tex_units[j], rt._image); // sampler2D - } - else { - gpu_set_texture(context._.tex_units[j], rt._image); // sampler2D - } + gpu_set_texture(context._.tex_units[j], rt._image); } } } diff --git a/base/sources/ts/layers.ts b/base/sources/ts/layers.ts index ea5365bb..4ea4b3ad 100644 --- a/base/sources/ts/layers.ts +++ b/base/sources/ts/layers.ts @@ -33,18 +33,18 @@ function layers_init() { draw_scaled_image(resource_get("placeholder.k"), 0, 0, config_get_texture_res_x(), config_get_texture_res_y()); // Base draw_end(); // Nor - _gpu_begin(texpaint_nor._image, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(texpaint_nor._image, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); gpu_end(); // Occ, rough, met - _gpu_begin(texpaint_pack._image, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.4, 0.0, 0.0)); + _gpu_begin(texpaint_pack._image, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.4, 0.0, 0.0)); gpu_end(); let texpaint_nor_empty: render_target_t = map_get(render_path_render_targets, "texpaint_nor_empty"); let texpaint_pack_empty: render_target_t = map_get(render_path_render_targets, "texpaint_pack_empty"); // Nor - _gpu_begin(texpaint_nor_empty._image, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(texpaint_nor_empty._image, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); gpu_end(); // Occ, rough, met - _gpu_begin(texpaint_pack_empty._image, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.4, 0.0, 0.0)); + _gpu_begin(texpaint_pack_empty._image, null, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.4, 0.0, 0.0)); gpu_end(); ///end } diff --git a/base/sources/ts/line_draw.ts b/base/sources/ts/line_draw.ts index 03e097f8..ffc4392f 100644 --- a/base/sources/ts/line_draw.ts +++ b/base/sources/ts/line_draw.ts @@ -53,7 +53,7 @@ function line_draw_init() { ARRAY_ACCESS(line_draw_pipeline.color_attachment, 0) = tex_format_t.RGBA64; ARRAY_ACCESS(line_draw_pipeline.color_attachment, 1) = tex_format_t.RGBA64; ARRAY_ACCESS(line_draw_pipeline.color_attachment, 2) = tex_format_t.RGBA64; - line_draw_pipeline.depth_attachment_bits = 24; + line_draw_pipeline.depth_attachment_bits = 32; gpu_pipeline_compile(line_draw_pipeline); pipes_offset = 0; line_draw_color_loc = pipes_get_constant_location("vec3"); diff --git a/base/sources/ts/render_path_base.ts b/base/sources/ts/render_path_base.ts index c9f2dc3a..10914dbb 100644 --- a/base/sources/ts/render_path_base.ts +++ b/base/sources/ts/render_path_base.ts @@ -192,7 +192,7 @@ function render_path_base_draw_bloom() { for (let i: i32 = 0; i < num_mips; ++i) { render_path_base_bloom_current_mip = i; - render_path_set_target(render_path_base_bloom_mipmaps[i].name, null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target(render_path_base_bloom_mipmaps[i].name, null, null, clear_flag_t.COLOR, 0x00000000); render_path_bind_target(i == 0 ? "buf" : render_path_base_bloom_mipmaps[i - 1].name, "tex"); render_path_draw_shader("shader_datas/bloom_pass/bloom_downsample_pass"); } @@ -262,7 +262,7 @@ function render_path_base_draw_ssao() { } render_path_set_target("singlea"); - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); render_path_bind_target("gbuffer0", "gbuffer0"); render_path_draw_shader("shader_datas/ssao_pass/ssao_pass"); @@ -280,7 +280,7 @@ function render_path_base_draw_ssao() { function render_path_base_draw_deferred_light() { render_path_set_target("buf"); - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); render_path_bind_target("gbuffer0", "gbuffer0"); render_path_bind_target("gbuffer1", "gbuffer1"); let ssao: bool = config_raw.rp_ssao != false && context_raw.camera_type == camera_type_t.PERSPECTIVE; @@ -291,15 +291,10 @@ function render_path_base_draw_deferred_light() { render_path_bind_target("empty_white", "ssaotex"); } - render_path_draw_shader("shader_datas/deferred_light/deferred_light"); - render_path_set_depth_from("buf", "gbuffer0"); // Bind depth for world pass - - render_path_set_target("buf"); + render_path_set_target("buf", null, "main"); render_path_draw_skydome("shader_datas/world_pass/world_pass"); - - render_path_set_depth_from("buf", "gbuffer1"); // Unbind depth } // function render_path_base_draw_histogram() { @@ -350,9 +345,9 @@ function render_path_base_draw_taa(bufa: string, bufb: string) { } function render_path_base_draw_gbuffer() { - render_path_set_target("gbuffer0", null, clear_flag_t.DEPTH, 0, 1.0); // Only clear gbuffer0 + render_path_set_target("gbuffer0", null, "main", clear_flag_t.DEPTH, 0, 1.0); // Only clear gbuffer0 let additional: string[] = ["gbuffer1", "gbuffer2"]; - render_path_set_target("gbuffer0", additional); + render_path_set_target("gbuffer0", additional, "main"); render_path_paint_bind_layers(); render_path_draw_meshes("mesh"); render_path_paint_unbind_layers(); @@ -362,12 +357,12 @@ function render_path_base_draw_gbuffer() { let ping: string = i % 2 == 1 ? "_copy" : ""; let pong: string = i % 2 == 1 ? "" : "_copy"; if (i == make_mesh_layer_pass_count - 1) { - render_path_set_target("gbuffer2" + ping, null, clear_flag_t.COLOR, 0xff000000); + render_path_set_target("gbuffer2" + ping, null, null, clear_flag_t.COLOR, 0xff000000); } let g1ping: string = "gbuffer1" + ping; let g2ping: string = "gbuffer2" + ping; let additional: string[] = [g1ping, g2ping]; - render_path_set_target("gbuffer0" + ping, additional); + render_path_set_target("gbuffer0" + ping, additional, "main"); render_path_bind_target("gbuffer0" + pong, "gbuffer0"); render_path_bind_target("gbuffer1" + pong, "gbuffer1"); render_path_bind_target("gbuffer2" + pong, "gbuffer2"); @@ -400,7 +395,6 @@ function render_path_base_make_gbuffer_copy_textures() { t.height = 0; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); - t.depth_buffer = "main"; render_path_create_render_target(t); } { @@ -421,12 +415,6 @@ function render_path_base_make_gbuffer_copy_textures() { t.scale = render_path_base_get_super_sampling(); render_path_create_render_target(t); } - - ///if arm_metal - // TODO: Fix depth attach for gbuffer0_copy on metal - // Use resize to re-create buffers from scratch for now - render_path_resize(); - ///end } } diff --git a/base/sources/ts/render_path_deferred.ts b/base/sources/ts/render_path_deferred.ts index 48660bc0..0539db23 100644 --- a/base/sources/ts/render_path_deferred.ts +++ b/base/sources/ts/render_path_deferred.ts @@ -1,7 +1,15 @@ function render_path_deferred_init() { - render_path_create_depth_buffer("main", "DEPTH24"); + { + let t: render_target_t = render_target_create(); + t.name = "main"; + t.width = 0; + t.height = 0; + t.format = "D32"; + t.scale = render_path_base_get_super_sampling(); + render_path_create_render_target(t); + } { let t: render_target_t = render_target_create(); t.name = "gbuffer0"; @@ -9,7 +17,6 @@ function render_path_deferred_init() { t.height = 0; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); - t.depth_buffer = "main"; render_path_create_render_target(t); } { diff --git a/base/sources/ts/render_path_forward.ts b/base/sources/ts/render_path_forward.ts index 766fc96b..ddbcad02 100644 --- a/base/sources/ts/render_path_forward.ts +++ b/base/sources/ts/render_path_forward.ts @@ -10,10 +10,8 @@ function render_path_forward_commands() { } function render_path_forward_draw_forward() { - render_path_set_depth_from("gbuffer1", "gbuffer0"); - render_path_set_target("gbuffer1"); + render_path_set_target("gbuffer1", null, "main"); render_path_draw_skydome("shader_datas/world_pass/world_pass"); - render_path_set_depth_from("gbuffer1", "gbuffer2"); render_path_set_target("buf"); render_path_bind_target("gbuffer1", "tex"); diff --git a/base/sources/ts/render_path_preview.ts b/base/sources/ts/render_path_preview.ts index dc9cc613..daaeab1d 100644 --- a/base/sources/ts/render_path_preview.ts +++ b/base/sources/ts/render_path_preview.ts @@ -18,13 +18,23 @@ function render_path_preview_init() { render_path_create_render_target(t); } - render_path_create_depth_buffer("mmain", "DEPTH24"); + let size: i32 = math_floor(util_render_material_preview_size * 2.0); + + { + let t: render_target_t = render_target_create(); + t.name = "mmain"; + t.width = size; + t.height = size; + t.format = "D32"; + t.scale = render_path_base_get_super_sampling(); + render_path_create_render_target(t); + } { let t: render_target_t = render_target_create(); t.name = "mtex"; - t.width = math_floor(util_render_material_preview_size * 2.0); - t.height = math_floor(util_render_material_preview_size * 2.0); + t.width = size; + t.height = size; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); render_path_create_render_target(t); @@ -33,19 +43,18 @@ function render_path_preview_init() { { let t: render_target_t = render_target_create(); t.name = "mgbuffer0"; - t.width = math_floor(util_render_material_preview_size * 2.0); - t.height = math_floor(util_render_material_preview_size * 2.0); + t.width = size; + t.height = size; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); - t.depth_buffer = "mmain"; render_path_create_render_target(t); } { let t: render_target_t = render_target_create(); t.name = "mgbuffer1"; - t.width = math_floor(util_render_material_preview_size * 2.0); - t.height = math_floor(util_render_material_preview_size * 2.0); + t.width = size; + t.height = size; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); render_path_create_render_target(t); @@ -54,8 +63,8 @@ function render_path_preview_init() { { let t: render_target_t = render_target_create(); t.name = "mgbuffer2"; - t.width = math_floor(util_render_material_preview_size * 2.0); - t.height = math_floor(util_render_material_preview_size * 2.0); + t.width = size; + t.height = size; t.format = "RGBA64"; t.scale = render_path_base_get_super_sampling(); render_path_create_render_target(t); @@ -63,15 +72,15 @@ function render_path_preview_init() { } function render_path_preview_commands_preview() { - render_path_set_target("mgbuffer2", null, clear_flag_t.COLOR, 0xff000000); - render_path_set_target("mgbuffer0", null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xffffffff, 1.0); + render_path_set_target("mgbuffer2", null, null, clear_flag_t.COLOR, 0xff000000); + render_path_set_target("mgbuffer0", null, "mmain", clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xffffffff, 1.0); let additional: string[] = ["mgbuffer1", "mgbuffer2"]; - render_path_set_target("mgbuffer0", additional); + render_path_set_target("mgbuffer0", additional, "mmain"); render_path_draw_meshes("mesh"); // Deferred light render_path_set_target("mtex"); - render_path_bind_target("_mmain", "gbufferD"); + render_path_bind_target("mmain", "gbufferD"); render_path_bind_target("mgbuffer0", "gbuffer0"); render_path_bind_target("mgbuffer1", "gbuffer1"); { @@ -79,13 +88,9 @@ function render_path_preview_commands_preview() { } render_path_draw_shader("shader_datas/deferred_light/deferred_light"); - render_path_set_depth_from("mtex", "mgbuffer0"); // Bind depth for world pass - - render_path_set_target("mtex"); // Re-binds depth + render_path_set_target("mtex", null, "mmain"); render_path_draw_skydome("shader_datas/world_pass/world_pass"); - render_path_set_depth_from("mtex", "mgbuffer1"); // Unbind depth - let framebuffer: string = "texpreview"; let selected_mat: slot_material_t = context_raw.material; let texpreview: render_target_t = map_get(render_path_render_targets, "texpreview"); @@ -103,15 +108,15 @@ function render_path_preview_commands_preview() { } function render_path_preview_commands_decal() { - render_path_set_target("gbuffer2", null, clear_flag_t.COLOR, 0xff000000); - render_path_set_target("gbuffer0", null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xffffffff, 1.0); + render_path_set_target("gbuffer2", null, null, clear_flag_t.COLOR, 0xff000000); + render_path_set_target("gbuffer0", null, "main", clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xffffffff, 1.0); let additional: string[] = ["gbuffer1", "gbuffer2"]; - render_path_set_target("gbuffer0", additional); + render_path_set_target("gbuffer0", additional, "main"); render_path_draw_meshes("mesh"); // Deferred light render_path_set_target("buf"); - render_path_bind_target("_main", "gbufferD"); + render_path_bind_target("main", "gbufferD"); render_path_bind_target("gbuffer0", "gbuffer0"); render_path_bind_target("gbuffer1", "gbuffer1"); { @@ -119,13 +124,9 @@ function render_path_preview_commands_decal() { } render_path_draw_shader("shader_datas/deferred_light/deferred_light"); - render_path_set_depth_from("buf", "gbuffer0"); // Bind depth for world pass - - render_path_set_target("buf"); + render_path_set_target("buf", null, "main"); render_path_draw_skydome("shader_datas/world_pass/world_pass"); - render_path_set_depth_from("buf", "gbuffer1"); // Unbind depth - let framebuffer: string = "texpreview"; let texpreview: render_target_t = map_get(render_path_render_targets, "texpreview"); texpreview._image = context_raw.decal_image; diff --git a/base/sources/ts/render_path_raytrace_bake.ts b/base/sources/ts/render_path_raytrace_bake.ts index ecf0206f..d3e47fab 100644 --- a/base/sources/ts/render_path_raytrace_bake.ts +++ b/base/sources/ts/render_path_raytrace_bake.ts @@ -54,7 +54,7 @@ function render_path_raytrace_bake_commands(parse_paint_material: (b?: bool)=>vo let _bake_type: bake_type_t = context_raw.bake_type; context_raw.bake_type = bake_type_t.INIT; parse_paint_material(true); - render_path_set_target("baketex0", null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("baketex0", null, null, clear_flag_t.COLOR, 0x00000000); // Pixels with alpha of 0.0 are skipped during raytracing let additional: string[] = ["baketex1"]; render_path_set_target("baketex0", additional); diff --git a/base/sources/ts/slot_layer.ts b/base/sources/ts/slot_layer.ts index ff5f9ac6..3c4a8b2a 100644 --- a/base/sources/ts/slot_layer.ts +++ b/base/sources/ts/slot_layer.ts @@ -245,7 +245,7 @@ function slot_layer_swap(raw: slot_layer_t, other: slot_layer_t) { function slot_layer_clear(raw: slot_layer_t, base_color: i32 = 0x00000000, base_image: gpu_texture_t = null, occlusion: f32 = 1.0, roughness: f32 = layers_default_rough, metallic: f32 = 0.0) { // Base - _gpu_begin(raw.texpaint, null, clear_flag_t.COLOR, base_color); + _gpu_begin(raw.texpaint, null, null, clear_flag_t.COLOR, base_color); gpu_end(); if (base_image != null) { draw_begin(raw.texpaint); @@ -255,10 +255,10 @@ function slot_layer_clear(raw: slot_layer_t, base_color: i32 = 0x00000000, base_ if (slot_layer_is_layer(raw)) { // Nor - _gpu_begin(raw.texpaint_nor, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(raw.texpaint_nor, null, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); gpu_end(); // Occ, rough, met - _gpu_begin(raw.texpaint_pack, null, clear_flag_t.COLOR, color_from_floats(occlusion, roughness, metallic, 0.0)); + _gpu_begin(raw.texpaint_pack, null, null, clear_flag_t.COLOR, color_from_floats(occlusion, roughness, metallic, 0.0)); gpu_end(); } diff --git a/base/sources/ts/util_uv.ts b/base/sources/ts/util_uv.ts index 0a788d2d..e8e6faf8 100644 --- a/base/sources/ts/util_uv.ts +++ b/base/sources/ts/util_uv.ts @@ -124,7 +124,7 @@ function util_uv_cache_dilate_map() { mask = context_raw.layer_filter; } let geom: mesh_data_t = mask == 0 && context_raw.merged_object != null ? context_raw.merged_object.data : context_raw.paint_object.data; - _gpu_begin(util_uv_dilatemap, null, clear_flag_t.COLOR, 0x00000000); + _gpu_begin(util_uv_dilatemap, null, null, clear_flag_t.COLOR, 0x00000000); gpu_set_pipeline(util_uv_pipe_dilate); gpu_set_vertex_buffer(geom._.vertex_buffer); gpu_set_index_buffer(geom._.index_buffers[0]); diff --git a/base/tests/cube/sources/main.ts b/base/tests/cube/sources/main.ts index d3de8a01..2cd543b9 100644 --- a/base/tests/cube/sources/main.ts +++ b/base/tests/cube/sources/main.ts @@ -11,7 +11,7 @@ function main() { mode: window_mode_t.WINDOWED, frequency: 60, vsync: true, - use_depth: true + depth_bits: 32 }; sys_start(ops); sys_init(); @@ -19,7 +19,7 @@ function main() { } function render_commands() { - render_path_set_target("", null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff6495ed, 1.0); + render_path_set_target("", null, null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff6495ed, 1.0); render_path_draw_meshes("mesh"); } diff --git a/base/tests/fall/sources/main.ts b/base/tests/fall/sources/main.ts index c676c115..4bcfad32 100644 --- a/base/tests/fall/sources/main.ts +++ b/base/tests/fall/sources/main.ts @@ -15,7 +15,7 @@ function main() { mode: window_mode_t.WINDOWED, frequency: 60, vsync: true, - use_depth: true + depth_bits: 32 }; sys_start(ops); sys_init(); @@ -23,7 +23,7 @@ function main() { } function render_commands() { - render_path_set_target("", null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff6495ed, 1.0); + render_path_set_target("", null, null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff6495ed, 1.0); render_path_draw_meshes("mesh"); } diff --git a/base/tests/triangle/main.ts b/base/tests/triangle/main.ts index d933c89f..216abb22 100644 --- a/base/tests/triangle/main.ts +++ b/base/tests/triangle/main.ts @@ -7,7 +7,7 @@ let vb: any; let ib: any; function render() { - _gpu_begin(null, null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff000000, 1.0); + _gpu_begin(null, null, null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff000000, 1.0); gpu_set_pipeline(pipeline); gpu_set_vertex_buffer(vb); gpu_set_index_buffer(ib); @@ -26,7 +26,7 @@ function main() { features: window_features_t.RESIZABLE | window_features_t.MINIMIZABLE | window_features_t.MAXIMIZABLE, vsync: true, frequency: 60, - use_depth: true + depth_bits: 32 }; _iron_init(ops);