From bf2dda291df5b189e937b35bc1e477eaee9d5d02 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Thu, 24 Apr 2025 22:19:44 +0200 Subject: [PATCH] Make metal work --- armorlab/shaders/inpaint_preview.kong | 6 +- armorlab/shaders/layer_copy_rrrr.kong | 6 +- armorlab/sources/make_mesh.ts | 2 +- armorlab/sources/nodes/inpaint_node.ts | 7 +- armorlab/sources/render_path_paint.ts | 35 +- armorlab/sources/ui_nodes_ext.ts | 6 +- armorpaint/shaders/dilate_pass.kong | 6 +- armorpaint/shaders/layer_invert.kong | 4 +- armorpaint/shaders/layer_merge.kong | 6 +- armorpaint/shaders/layer_view.kong | 4 +- armorpaint/shaders/mask_colorid.kong | 6 +- armorpaint/shaders/mask_merge.kong | 6 +- armorpaint/sources/layers_ext.ts | 24 +- armorpaint/sources/make_mesh.ts | 8 +- armorpaint/sources/render_path_paint.ts | 29 +- armorpaint/sources/ui_header_ext.ts | 4 +- armorsculpt/sources/make_mesh.ts | 6 +- armorsculpt/sources/render_path_sculpt.ts | 4 +- base/assets/Scene.arm | Bin 178066 -> 178114 bytes base/assets/shader_datas.arm | Bin 14863 -> 14908 bytes base/project.js | 12 +- base/shaders/bloom_downsample_pass.kong | 4 +- base/shaders/bloom_upsample_pass.kong | 4 +- base/shaders/compositor_pass.kong | 6 +- base/shaders/copy_mrt3_pass.kong | 6 +- base/shaders/copy_pass.kong | 6 +- base/shaders/cursor.kong | 4 +- base/shaders/deferred_light.kong | 6 +- base/shaders/draw/draw_image.kong | 4 +- base/shaders/draw/draw_rect.kong | 8 +- base/shaders/draw/draw_text.kong | 4 +- base/shaders/histogram_pass.kong | 6 +- base/shaders/layer_copy.kong | 4 +- base/shaders/layer_copy_bgra.kong | 4 +- base/shaders/mask_apply.kong | 6 +- base/shaders/prefilter_envmap.kong | 4 +- base/shaders/ssao_blur_pass.kong | 6 +- base/shaders/ssao_pass.kong | 6 +- base/shaders/supersample_resolve.kong | 4 +- base/shaders/taa_pass.kong | 6 +- base/shaders/world_pass.kong | 4 +- base/sources/backends/android_system.c | 5 +- base/sources/backends/direct3d12_gpu.c | 127 +- base/sources/backends/linux_system.c | 6 +- base/sources/backends/metal_gpu.h | 8 - base/sources/backends/metal_gpu.m | 1614 ++++++++---------- base/sources/backends/vulkan_gpu.c | 199 +-- base/sources/backends/webgpu_gpu.c | 58 +- base/sources/backends/windows_system.c | 5 +- base/sources/iron.h | 59 +- base/sources/iron_draw.c | 78 +- base/sources/iron_draw.h | 5 +- base/sources/iron_gpu.c | 213 +-- base/sources/iron_gpu.h | 60 +- base/sources/iron_ui.c | 47 +- base/sources/iron_ui_nodes.c | 5 +- base/sources/libs/kong/backends/metal.c | 1 - base/sources/ts/export_texture.ts | 20 +- base/sources/ts/import_envmap.ts | 4 +- base/sources/ts/iron/const_data.ts | 8 +- base/sources/ts/iron/iron.ts | 14 +- base/sources/ts/iron/mesh_data.ts | 6 +- base/sources/ts/iron/mesh_object.ts | 4 +- base/sources/ts/iron/render_path.ts | 22 +- base/sources/ts/iron/shader_data.ts | 4 + base/sources/ts/layers.ts | 36 +- base/sources/ts/line_draw.ts | 14 +- base/sources/ts/node_shader.ts | 10 +- base/sources/ts/render_path_base.ts | 16 +- base/sources/ts/render_path_preview.ts | 18 +- base/sources/ts/render_path_raytrace_bake.ts | 4 +- base/sources/ts/slot_layer.ts | 15 +- base/sources/ts/slot_material.ts | 4 +- base/sources/ts/ui_base.ts | 6 +- base/sources/ts/ui_files.ts | 6 +- base/sources/ts/ui_nodes.ts | 3 +- base/sources/ts/util_mesh.ts | 10 +- base/sources/ts/util_render.ts | 17 +- base/sources/ts/util_uv.ts | 27 +- base/tests/cube/shaders/mesh.kong | 12 +- base/tests/cube/sources/main.ts | 3 +- base/tests/fall/shaders/mesh.kong | 13 +- base/tests/fall/sources/main.ts | 3 +- base/tests/triangle/main.ts | 11 +- base/tools/pad/sources/main.ts | 3 +- 85 files changed, 1292 insertions(+), 1794 deletions(-) diff --git a/armorlab/shaders/inpaint_preview.kong b/armorlab/shaders/inpaint_preview.kong index fb805f0d..e9e797bf 100644 --- a/armorlab/shaders/inpaint_preview.kong +++ b/armorlab/shaders/inpaint_preview.kong @@ -1,13 +1,13 @@ +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex0: tex2d; #[set(everything)] const texa: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/armorlab/shaders/layer_copy_rrrr.kong b/armorlab/shaders/layer_copy_rrrr.kong index 57e6b3b8..b0183f43 100644 --- a/armorlab/shaders/layer_copy_rrrr.kong +++ b/armorlab/shaders/layer_copy_rrrr.kong @@ -1,10 +1,10 @@ -#[set(everything)] -const tex: tex2d; - #[set(everything)] const sampler_linear: sampler; +#[set(everything)] +const tex: tex2d; + struct vert_in { pos: float2; } diff --git a/armorlab/sources/make_mesh.ts b/armorlab/sources/make_mesh.ts index 701847c0..a5182fad 100644 --- a/armorlab/sources/make_mesh.ts +++ b/armorlab/sources/make_mesh.ts @@ -193,7 +193,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte node_shader_write_frag(kong, "n /= (abs(n.x) + abs(n.y) + abs(n.z));"); // node_shader_write_frag(kong, "n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);"); node_shader_write_frag(kong, "if (n.z < 0.0) { n.xy = octahedron_wrap(n.xy); }"); - node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % 3)));"); + node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % float(3))));"); node_shader_write_frag(kong, "output[2] = float4(0.0, 0.0, input.tex_coord.xy);"); parser_material_finalize(con_mesh); diff --git a/armorlab/sources/nodes/inpaint_node.ts b/armorlab/sources/nodes/inpaint_node.ts index 51603f54..8543bf82 100644 --- a/armorlab/sources/nodes/inpaint_node.ts +++ b/armorlab/sources/nodes/inpaint_node.ts @@ -33,8 +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); - iron_gpu_clear(color_from_floats(1.0, 1.0, 1.0, 1.0)); + _gpu_begin(inpaint_node_mask, null, clear_flag_t.COLOR, color_from_floats(1.0, 1.0, 1.0, 1.0)); _gpu_end(); }); } @@ -88,12 +87,12 @@ function inpaint_node_get_cached_image(self: inpaint_node_t): iron_gpu_texture_t sys_notify_on_next_frame(function (self: inpaint_node_t) { let source: iron_gpu_texture_t = logic_node_input_get_as_image(self.base.inputs[0]); _gpu_begin(inpaint_node_image); - iron_gpu_set_pipeline(pipes_inpaint_preview); + gpu_set_pipeline(pipes_inpaint_preview); gpu_set_texture(pipes_tex0_inpaint_preview, source); gpu_set_texture(pipes_texa_inpaint_preview, inpaint_node_mask); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); }, self); return inpaint_node_image; diff --git a/armorlab/sources/render_path_paint.ts b/armorlab/sources/render_path_paint.ts index e5d2e3b2..d61ed03b 100644 --- a/armorlab/sources/render_path_paint.ts +++ b/armorlab/sources/render_path_paint.ts @@ -63,20 +63,8 @@ function render_path_paint_commands_paint(dilation: bool = true) { if (context_raw.pdirty > 0) { if (context_raw.tool == workspace_tool_t.PICKER) { - let additional: string[] = ["texpaint_nor_picker", "texpaint_pack_picker", "texpaint_uv_picker"]; - ///if arm_metal - // render_path_set_target("texpaint_picker"); - // render_path_clear_target(0xff000000); - // render_path_set_target("texpaint_nor_picker"); - // render_path_clear_target(0xff000000); - // render_path_set_target("texpaint_pack_picker"); - // render_path_clear_target(0xff000000); render_path_set_target("texpaint_picker", additional); - ///else - render_path_set_target("texpaint_picker", additional); - // render_path_clear_target(0xff000000); - ///end render_path_bind_target("gbuffer2", "gbuffer2"); // tid = context_raw.layer.id; render_path_bind_target("texpaint" + tid, "texpaint"); @@ -188,7 +176,7 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 let geom: mesh_data_t = plane.data; render_path_set_target(""); - iron_gpu_set_pipeline(pipes_cursor); + gpu_set_pipeline(pipes_cursor); let gbuffer0: render_target_t = map_get(render_path_render_targets, "gbuffer0"); gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0._image); gpu_set_float2(pipes_cursor_mouse, mx, my); @@ -200,19 +188,9 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 gpu_set_matrix4(pipes_cursor_vp, scene_camera.vp); let inv_vp: mat4_t = mat4_inv(scene_camera.vp); gpu_set_matrix4(pipes_cursor_inv_vp, inv_vp); - ////if (arm_metal || arm_vulkan) - // let vs: vertex_element_t[] = [ - // { - // name: "tex", - // data: "short2norm" - // } - // ]; - // gpu_set_vertex_buffer(mesh_data_get(geom, vs)); - ////else gpu_set_vertex_buffer(geom._.vertex_buffer); - ////end gpu_set_index_buffer(geom._.index_buffers[0]); - gpu_draw_indexed_vertices(); + gpu_draw(); gpu_disable_scissor(); render_path_end(); @@ -249,14 +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"); - render_path_clear_target(0x00000000); - render_path_set_target("texpaint_blend1"); - render_path_clear_target(0x00000000); + render_path_set_target("texpaint_blend0", null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend1", null, clear_flag_t.COLOR, 0x00000000); ///else let additional: string[] = ["texpaint_blend1"]; - render_path_set_target("texpaint_blend0", additional); - render_path_clear_target(0x00000000); + render_path_set_target("texpaint_blend0", additional, clear_flag_t.COLOR, 0x00000000); ///end } } diff --git a/armorlab/sources/ui_nodes_ext.ts b/armorlab/sources/ui_nodes_ext.ts index 4854cdb1..aad77e06 100644 --- a/armorlab/sources/ui_nodes_ext.ts +++ b/armorlab/sources/ui_nodes_ext.ts @@ -79,11 +79,11 @@ function ui_nodes_ext_run() { if (texheight != null) { _gpu_begin(texpaint_pack._image); - iron_gpu_set_pipeline(pipes_copy_a); + gpu_set_pipeline(pipes_copy_a); gpu_set_texture(pipes_copy_a_tex, texheight); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); let is_float_node: bool = context_raw.brush_output_node_inst.base.inputs[channel_type_t.HEIGHT].node.base.get == float_node_get; @@ -105,7 +105,7 @@ function ui_nodes_ext_run() { buffer_set_i16(vertices, i * 2, buffer_get_i16(ui_nodes_ext_last_vertices, i * 2)); } } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); // Apply displacement if (config_raw.displace_strength > 0) { diff --git a/armorpaint/shaders/dilate_pass.kong b/armorpaint/shaders/dilate_pass.kong index 634c806e..efb06a67 100644 --- a/armorpaint/shaders/dilate_pass.kong +++ b/armorpaint/shaders/dilate_pass.kong @@ -5,15 +5,15 @@ const constants: { tex_size: float2; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex: tex2d; #[set(everything)] const texdilate: tex2d; -#[set(everything)] -const sampler_linear: sampler; - // const offsets: float2[8] = { // float2(-1.0, 0.0), float2( 1.0, 0.0), float2( 0.0, 1.0), float2( 0.0, -1.0), // float2(-1.0, 1.0), float2( 1.0, 1.0), float2( 1.0, -1.0), float2(-1.0, -1.0) diff --git a/armorpaint/shaders/layer_invert.kong b/armorpaint/shaders/layer_invert.kong index 65bff574..3f0399f8 100644 --- a/armorpaint/shaders/layer_invert.kong +++ b/armorpaint/shaders/layer_invert.kong @@ -8,10 +8,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/armorpaint/shaders/layer_merge.kong b/armorpaint/shaders/layer_merge.kong index 464038ec..1241ddf1 100644 --- a/armorpaint/shaders/layer_merge.kong +++ b/armorpaint/shaders/layer_merge.kong @@ -6,6 +6,9 @@ const constants: { tex1w: float; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex0: tex2d; @@ -18,9 +21,6 @@ const texmask: tex2d; #[set(everything)] const texa: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/armorpaint/shaders/layer_view.kong b/armorpaint/shaders/layer_view.kong index 3d7f5ae0..40091fcc 100644 --- a/armorpaint/shaders/layer_view.kong +++ b/armorpaint/shaders/layer_view.kong @@ -9,10 +9,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/armorpaint/shaders/mask_colorid.kong b/armorpaint/shaders/mask_colorid.kong index 91b2f616..f79c4fd5 100644 --- a/armorpaint/shaders/mask_colorid.kong +++ b/armorpaint/shaders/mask_colorid.kong @@ -1,15 +1,15 @@ // Turn picked color id into mask +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const texpaint_colorid: tex2d; // 1x1 picked color #[set(everything)] const texcolorid: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/armorpaint/shaders/mask_merge.kong b/armorpaint/shaders/mask_merge.kong index fa071f35..020b72bf 100644 --- a/armorpaint/shaders/mask_merge.kong +++ b/armorpaint/shaders/mask_merge.kong @@ -5,15 +5,15 @@ const constants: { blending: int; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex0: tex2d; #[set(everything)] const texa: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/armorpaint/sources/layers_ext.ts b/armorpaint/sources/layers_ext.ts index 438ae83d..64937b61 100644 --- a/armorpaint/sources/layers_ext.ts +++ b/armorpaint/sources/layers_ext.ts @@ -10,14 +10,11 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t let empty: iron_gpu_texture_t = empty_rt._image; // Clear export layer - _gpu_begin(layers_expa); - iron_gpu_clear(color_from_floats(0.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expa, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); _gpu_end(); - _gpu_begin(layers_expb); - iron_gpu_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(layers_expb, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); _gpu_end(); - _gpu_begin(layers_expc); - iron_gpu_clear(color_from_floats(1.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expc, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); _gpu_end(); // Flatten layers @@ -35,8 +32,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t if (l1masks != null) { if (l1masks.length > 1) { layers_make_temp_mask_img(); - draw_begin(pipes_temp_mask_image); - iron_gpu_clear(0x00000000); + draw_begin(pipes_temp_mask_image, clear_flag_t.COLOR, 0x00000000); draw_end(); let l1: slot_layer_t = { texpaint: pipes_temp_mask_image }; for (let i: i32 = 0; i < l1masks.length; ++i) { @@ -65,7 +61,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t draw_end(); ///else _gpu_begin(layers_expa); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, empty); gpu_set_texture(pipes_texmask, mask); @@ -75,7 +71,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t gpu_set_int(pipes_blending, layers.length > 1 ? l1.blending : 0); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); ///end } @@ -88,7 +84,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t draw_end(); _gpu_begin(layers_expb); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, l1.texpaint_nor); gpu_set_texture(pipes_texmask, mask); @@ -98,7 +94,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t gpu_set_int(pipes_blending, l1.paint_nor_blend ? -2 : -1); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } @@ -152,7 +148,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t draw_end(); _gpu_begin(l0.texpaint_nor); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, layers_temp_image); gpu_set_texture(pipes_tex1, l0.texpaint_pack); gpu_set_texture(pipes_texmask, empty); @@ -162,7 +158,7 @@ function layers_ext_flatten(height_to_normal: bool = false, layers: slot_layer_t gpu_set_int(pipes_blending, -4); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } diff --git a/armorpaint/sources/make_mesh.ts b/armorpaint/sources/make_mesh.ts index f9b82f11..d77df2cb 100644 --- a/armorpaint/sources/make_mesh.ts +++ b/armorpaint/sources/make_mesh.ts @@ -394,7 +394,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte } else { // Deferred, Pathtraced if (make_material_emis_used) { - node_shader_write_frag(kong, "if (int(matid * 255.0) % 3 == 1) { basecol *= 10.0; }"); // Boost for bloom + node_shader_write_frag(kong, "if (int(matid * 255.0) % float(3) == 1) { basecol *= 10.0; }"); // Boost for bloom } node_shader_write_frag(kong, "output[1] = float4(basecol, occlusion);"); } @@ -421,11 +421,11 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte node_shader_write_frag(kong, "output[1] = float4(float3(height, height, height), 1.0);"); } else if (context_raw.viewport_mode == viewport_mode_t.EMISSION) { - node_shader_write_frag(kong, "float emis = int(matid * 255.0) % 3 == 1 ? 1.0 : 0.0;"); + node_shader_write_frag(kong, "float emis = int(matid * 255.0) % float(3) == 1 ? 1.0 : 0.0;"); node_shader_write_frag(kong, "output[1] = float4(float3(emis, emis, emis), 1.0);"); } else if (context_raw.viewport_mode == viewport_mode_t.SUBSURFACE) { - node_shader_write_frag(kong, "float subs = int(matid * 255.0) % 3 == 2 ? 1.0 : 0.0;"); + node_shader_write_frag(kong, "float subs = int(matid * 255.0) % float(3) == 2 ? 1.0 : 0.0;"); node_shader_write_frag(kong, "output[1] = float4(float3(subs, subs, subs), 1.0);"); } else if (context_raw.viewport_mode == viewport_mode_t.TEXCOORD) { @@ -484,7 +484,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte node_shader_write_frag(kong, "n /= (abs(n.x) + abs(n.y) + abs(n.z));"); // node_shader_write_frag(kong, "n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);"); node_shader_write_frag(kong, "if (n.z < 0.0) { n.xy = octahedron_wrap(n.xy); }"); - node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % 3)));"); + node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % float(3))));"); } node_shader_write_frag(kong, "output[2] = float4(0.0, 0.0, input.tex_coord.xy);"); diff --git a/armorpaint/sources/render_path_paint.ts b/armorpaint/sources/render_path_paint.ts index 03cbb249..b46a257e 100644 --- a/armorpaint/sources/render_path_paint.ts +++ b/armorpaint/sources/render_path_paint.ts @@ -120,8 +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"); - render_path_clear_target(0xff000000); + render_path_set_target("texpaint_colorid", null, clear_flag_t.COLOR, 0xff000000); render_path_bind_target("gbuffer2", "gbuffer2"); render_path_paint_draw_fullscreen_triangle("paint"); ui_header_handle.redraws = 2; @@ -231,8 +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); - render_path_clear_target(0xff000000); + render_path_set_target(texpaint, null, clear_flag_t.COLOR, 0xff000000); } render_path_set_target("texpaint_blend1"); @@ -439,7 +437,7 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 let geom: mesh_data_t = plane.data; render_path_set_target(""); - iron_gpu_set_pipeline(pipes_cursor); + gpu_set_pipeline(pipes_cursor); let rt: render_target_t = map_get(render_path_render_targets, "gbuffer0"); let gbuffer0: iron_gpu_texture_t = rt._image; gpu_set_texture_depth(pipes_cursor_gbufferd, gbuffer0); @@ -452,19 +450,9 @@ function render_path_paint_draw_cursor(mx: f32, my: f32, radius: f32, tint_r: f3 gpu_set_matrix4(pipes_cursor_vp, scene_camera.vp); let inv_vp: mat4_t = mat4_inv(scene_camera.vp); gpu_set_matrix4(pipes_cursor_inv_vp, inv_vp); - ////if (arm_metal || arm_vulkan) - // let vs: vertex_element_t[] = [ - // { - // name: "tex", - // data: "short2norm" - // } - // ]; - // gpu_set_vertex_buffer(mesh_data_get(geom, vs)); - ////else gpu_set_vertex_buffer(geom._.vertex_buffer); - ////end gpu_set_index_buffer(geom._.index_buffers[0]); - gpu_draw_indexed_vertices(); + gpu_draw(); gpu_disable_scissor(); render_path_end(); @@ -717,14 +705,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"); - render_path_clear_target(0x00000000); - render_path_set_target("texpaint_blend1"); - render_path_clear_target(0x00000000); + render_path_set_target("texpaint_blend0", null, clear_flag_t.COLOR, 0x00000000); + render_path_set_target("texpaint_blend1", null, clear_flag_t.COLOR, 0x00000000); ///else let additional: string[] = ["texpaint_blend1"]; - render_path_set_target("texpaint_blend0", additional); - render_path_clear_target(0x00000000); + render_path_set_target("texpaint_blend0", additional, clear_flag_t.COLOR, 0x00000000); ///end } diff --git a/armorpaint/sources/ui_header_ext.ts b/armorpaint/sources/ui_header_ext.ts index e3ed2b9d..088fdfa8 100644 --- a/armorpaint/sources/ui_header_ext.ts +++ b/armorpaint/sources/ui_header_ext.ts @@ -53,13 +53,13 @@ function ui_header_draw_tool_properties(ui: ui_t) { let m: slot_layer_t = layers_new_mask(false, context_raw.layer); sys_notify_on_next_frame(function (m: slot_layer_t) { _gpu_begin(m.texpaint); - iron_gpu_set_pipeline(pipes_colorid_to_mask); + gpu_set_pipeline(pipes_colorid_to_mask); let rt: render_target_t = map_get(render_path_render_targets, "texpaint_colorid"); gpu_set_texture(pipes_texpaint_colorid, rt._image); gpu_set_texture(pipes_tex_colorid, project_get_image(project_assets[context_raw.colorid_handle.position])); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); context_raw.colorid_picked = false; ui_toolbar_handle.redraws = 1; diff --git a/armorsculpt/sources/make_mesh.ts b/armorsculpt/sources/make_mesh.ts index 0453e6fa..fad57c7d 100644 --- a/armorsculpt/sources/make_mesh.ts +++ b/armorsculpt/sources/make_mesh.ts @@ -52,7 +52,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte node_shader_add_constant(kong, "N: float3x3", "_normal_matrix"); node_shader_add_out(kong, "wnormal: float3"); - node_shader_write_attrib_vert(kong, "var base_vertex0: int = vertex_id() - (vertex_id() % 3);"); + node_shader_write_attrib_vert(kong, "var base_vertex0: int = vertex_id() - (vertex_id() % float(3));"); node_shader_write_attrib_vert(kong, "var base_vertex1: int = base_vertex0 + 1;"); node_shader_write_attrib_vert(kong, "var base_vertex2: int = base_vertex0 + 2;"); node_shader_write_attrib_vert(kong, "var meshpos0: float3 = sample_lod(texpaint_vert, sampler_linear, uint2(base_vertex0 % constants.texpaint_vert_size.x, base_vertex0 / constants.texpaint_vert_size.y), 0).xyz;"); @@ -287,7 +287,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte } else { // Deferred, Pathtraced if (make_material_emis_used) { - node_shader_write_frag(kong, "if (int(matid * 255.0) % 3 == 1) { basecol *= 10.0; }"); // Boost for bloom + node_shader_write_frag(kong, "if (int(matid * 255.0) % float(3) == 1) { basecol *= 10.0; }"); // Boost for bloom } node_shader_write_frag(kong, "output[1] = float4(basecol, occlusion);"); } @@ -315,7 +315,7 @@ function make_mesh_run(data: material_t, layer_pass: i32 = 0): node_shader_conte node_shader_write_frag(kong, "n /= (abs(n.x) + abs(n.y) + abs(n.z));"); // node_shader_write_frag(kong, "n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);"); node_shader_write_frag(kong, "if (n.z < 0.0) { n.xy = octahedron_wrap(n.xy); }"); - node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % 3)));"); + node_shader_write_frag(kong, "output[0] = float4(n.xy, roughness, pack_f32_i16(metallic, uint(int(matid * 255.0) % float(3))));"); } node_shader_write_frag(kong, "output[2] = float4(0.0, 0.0, input.tex_coord.xy);"); diff --git a/armorsculpt/sources/render_path_sculpt.ts b/armorsculpt/sources/render_path_sculpt.ts index ebff2a80..c4bd79da 100644 --- a/armorsculpt/sources/render_path_sculpt.ts +++ b/armorsculpt/sources/render_path_sculpt.ts @@ -29,13 +29,13 @@ function render_path_sculpt_commands() { mesh_object_get_contexts(project_paint_objects[0], "paint", mats, material_contexts, shader_contexts); let cc_context: shader_context_t = shader_contexts[0]; - iron_gpu_set_pipeline(cc_context._.pipe_state); + gpu_set_pipeline(cc_context._.pipe_state); uniforms_set_context_consts(cc_context,_render_path_bind_params); uniforms_set_obj_consts(cc_context, project_paint_objects[0].base); uniforms_set_material_consts(cc_context, material_contexts[0]); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); render_path_end(); } diff --git a/base/assets/Scene.arm b/base/assets/Scene.arm index a1f4c4fc8ae3805868539be16b247d3ea5dae0cc..45f6c6370a59fb3b864900482cb5f78d09198917 100644 GIT binary patch delta 85 zcmbRAp6k$iu7(!IEliVFF)~m8zlBL^I@d-fhwXk_nIsvv|5?rSo00V{BLf4&bl$B@ X3fp&3u7(!IEliVFu`n?(Ficn8$mFyA-6|##X2t{Ch1M}KF@wdI1I1a8RMc%{ Ha$y1hk(Cl1 diff --git a/base/assets/shader_datas.arm b/base/assets/shader_datas.arm index d645e6c344a609377fbda76ff397ac9ad91e15fd..ca49261fa5397f142b4b897b199941f8445b18f5 100644 GIT binary patch delta 49 ycmeA#*;BHinQ1bwUd1GB;m!Y<_Ji4z*Gma+wqn!g0!e^4lfTJ2Z`Kfet^okwtP*Yj delta 54 wcmdl}(qFQnnQ8J~Ua`pwgo7sw$O&)eW8RMt_%9&5If6}}3#MZ8e*s%f06V)B4FCWD diff --git a/base/project.js b/base/project.js index cbb2e2c1..f1840014 100644 --- a/base/project.js +++ b/base/project.js @@ -15,12 +15,14 @@ let dir = flags.name.toLowerCase(); if (!flags.lite) { project.add_define("IDLE_SLEEP"); - project.add_cfiles('sources/libs/kong/libs/*.c'); - project.add_cfiles('sources/libs/kong/*.c'); - project.add_cfiles('sources/libs/kong/backends/*.c'); + project.add_cfiles("sources/libs/kong/libs/*.c"); + project.add_cfiles("sources/libs/kong/*.c"); + project.add_cfiles("sources/libs/kong/backends/*.c"); + project.add_define("WITH_KONG"); + if (platform === "windows") { - project.add_define('_CRT_SECURE_NO_WARNINGS'); - project.add_lib('d3dcompiler'); + project.add_define("_CRT_SECURE_NO_WARNINGS"); + project.add_lib("d3dcompiler"); } if (flags.with_onnx) { diff --git a/base/shaders/bloom_downsample_pass.kong b/base/shaders/bloom_downsample_pass.kong index 565c9e45..e9f2feef 100644 --- a/base/shaders/bloom_downsample_pass.kong +++ b/base/shaders/bloom_downsample_pass.kong @@ -6,10 +6,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/bloom_upsample_pass.kong b/base/shaders/bloom_upsample_pass.kong index c08146b9..6649561c 100644 --- a/base/shaders/bloom_upsample_pass.kong +++ b/base/shaders/bloom_upsample_pass.kong @@ -7,10 +7,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/compositor_pass.kong b/base/shaders/compositor_pass.kong index 3e429d36..acbbc635 100644 --- a/base/shaders/compositor_pass.kong +++ b/base/shaders/compositor_pass.kong @@ -5,15 +5,15 @@ const constants: { grain_strength: float; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex: tex2d; // #[set(everything)] // const histogram: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/base/shaders/copy_mrt3_pass.kong b/base/shaders/copy_mrt3_pass.kong index e65d4c03..9ba8f8d5 100644 --- a/base/shaders/copy_mrt3_pass.kong +++ b/base/shaders/copy_mrt3_pass.kong @@ -1,4 +1,7 @@ +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex0: tex2d; @@ -8,9 +11,6 @@ const tex1: tex2d; #[set(everything)] const tex2: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/base/shaders/copy_pass.kong b/base/shaders/copy_pass.kong index ccb31a2a..838bcee5 100644 --- a/base/shaders/copy_pass.kong +++ b/base/shaders/copy_pass.kong @@ -1,10 +1,10 @@ -#[set(everything)] -const tex: tex2d; - #[set(everything)] const sampler_linear: sampler; +#[set(everything)] +const tex: tex2d; + struct vert_in { pos: float2; } diff --git a/base/shaders/cursor.kong b/base/shaders/cursor.kong index 752e1300..fec19f9e 100644 --- a/base/shaders/cursor.kong +++ b/base/shaders/cursor.kong @@ -11,10 +11,10 @@ const constants: { }; #[set(everything)] -const gbufferD: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const gbufferD: tex2d; struct vert_in { pos: float4; // hlsl diff --git a/base/shaders/deferred_light.kong b/base/shaders/deferred_light.kong index d761ff47..a8ca8174 100644 --- a/base/shaders/deferred_light.kong +++ b/base/shaders/deferred_light.kong @@ -16,6 +16,9 @@ const constants: { shirr6: float4; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const gbufferD: tex2d; @@ -34,9 +37,6 @@ const senvmap_radiance: tex2d; #[set(everything)] const ssaotex: tex2d; -#[set(everything)] -const sampler_linear: sampler; - const PI: float = 3.14159265358979; const PI2: float = 6.28318530718; diff --git a/base/shaders/draw/draw_image.kong b/base/shaders/draw/draw_image.kong index c33e0a57..eca79cd2 100644 --- a/base/shaders/draw/draw_image.kong +++ b/base/shaders/draw/draw_image.kong @@ -8,10 +8,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/draw/draw_rect.kong b/base/shaders/draw/draw_rect.kong index 2a5f0807..e6c26d4f 100644 --- a/base/shaders/draw/draw_rect.kong +++ b/base/shaders/draw/draw_rect.kong @@ -15,7 +15,7 @@ struct vert_out { col: float4; } -fun draw_colored_vert(input: vert_in): vert_out { +fun draw_rect_vert(input: vert_in): vert_out { var output: vert_out; output.pos = float4(input.pos, 0.0, 1.0); output.pos.xy = output.pos.xy * constants.pos.zw + constants.pos.xy; @@ -25,12 +25,12 @@ fun draw_colored_vert(input: vert_in): vert_out { return output; } -fun draw_colored_frag(input: vert_out): float4 { +fun draw_rect_frag(input: vert_out): float4 { return input.col; } #[pipe] struct pipe { - vertex = draw_colored_vert; - fragment = draw_colored_frag; + vertex = draw_rect_vert; + fragment = draw_rect_frag; } diff --git a/base/shaders/draw/draw_text.kong b/base/shaders/draw/draw_text.kong index 56ab3f1e..c9b4076a 100644 --- a/base/shaders/draw/draw_text.kong +++ b/base/shaders/draw/draw_text.kong @@ -8,10 +8,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/histogram_pass.kong b/base/shaders/histogram_pass.kong index ad2bc699..ba6640e5 100644 --- a/base/shaders/histogram_pass.kong +++ b/base/shaders/histogram_pass.kong @@ -1,10 +1,10 @@ -#[set(everything)] -const tex: tex2d; - #[set(everything)] const sampler_linear: sampler; +#[set(everything)] +const tex: tex2d; + struct vert_in { pos: float2; } diff --git a/base/shaders/layer_copy.kong b/base/shaders/layer_copy.kong index ee87e236..be4860be 100644 --- a/base/shaders/layer_copy.kong +++ b/base/shaders/layer_copy.kong @@ -8,10 +8,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/layer_copy_bgra.kong b/base/shaders/layer_copy_bgra.kong index e2c5feae..e143f6e4 100644 --- a/base/shaders/layer_copy_bgra.kong +++ b/base/shaders/layer_copy_bgra.kong @@ -8,10 +8,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/mask_apply.kong b/base/shaders/mask_apply.kong index ecf0566e..1ca63a24 100644 --- a/base/shaders/mask_apply.kong +++ b/base/shaders/mask_apply.kong @@ -1,13 +1,13 @@ +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex0: tex2d; #[set(everything)] const texa: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/base/shaders/prefilter_envmap.kong b/base/shaders/prefilter_envmap.kong index bb9053be..e35f8235 100644 --- a/base/shaders/prefilter_envmap.kong +++ b/base/shaders/prefilter_envmap.kong @@ -5,10 +5,10 @@ const constants: { }; #[set(everything)] -const radiance: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const radiance: tex2d; const PI: float = 3.14159265358979; const PI2: float = 6.28318530718; diff --git a/base/shaders/ssao_blur_pass.kong b/base/shaders/ssao_blur_pass.kong index ccadd8e4..eae2de76 100644 --- a/base/shaders/ssao_blur_pass.kong +++ b/base/shaders/ssao_blur_pass.kong @@ -4,15 +4,15 @@ const constants: { dir_inv: float2; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex: tex2d; #[set(everything)] const gbuffer0: tex2d; -#[set(everything)] -const sampler_linear: sampler; - // const blur_weights: float[] = { // 0.132572, 0.125472, 0.106373, 0.08078, 0.05495, 0.033482, 0.018275, 0.008934, 0.003912, 0.001535 // }; diff --git a/base/shaders/ssao_pass.kong b/base/shaders/ssao_pass.kong index 132113e5..881551eb 100644 --- a/base/shaders/ssao_pass.kong +++ b/base/shaders/ssao_pass.kong @@ -7,15 +7,15 @@ const constants: { camera_proj: float2; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const gbufferD: tex2d; #[set(everything)] const gbuffer0: tex2d; -#[set(everything)] -const sampler_linear: sampler; - const max_steps: float = 32; const ray_step: float = 0.001; diff --git a/base/shaders/supersample_resolve.kong b/base/shaders/supersample_resolve.kong index 0d0c7e40..5f963882 100644 --- a/base/shaders/supersample_resolve.kong +++ b/base/shaders/supersample_resolve.kong @@ -5,10 +5,10 @@ const constants: { }; #[set(everything)] -const tex: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const tex: tex2d; struct vert_in { pos: float2; diff --git a/base/shaders/taa_pass.kong b/base/shaders/taa_pass.kong index 0da09246..60f2621c 100644 --- a/base/shaders/taa_pass.kong +++ b/base/shaders/taa_pass.kong @@ -4,15 +4,15 @@ const constants: { taa_blend: float; }; +#[set(everything)] +const sampler_linear: sampler; + #[set(everything)] const tex: tex2d; #[set(everything)] const tex2: tex2d; -#[set(everything)] -const sampler_linear: sampler; - struct vert_in { pos: float2; } diff --git a/base/shaders/world_pass.kong b/base/shaders/world_pass.kong index 184167f4..60c812f4 100644 --- a/base/shaders/world_pass.kong +++ b/base/shaders/world_pass.kong @@ -6,10 +6,10 @@ const constants: { }; #[set(everything)] -const envmap: tex2d; +const sampler_linear: sampler; #[set(everything)] -const sampler_linear: sampler; +const envmap: tex2d; const PI: float = 3.1415926535; const PI2: float = 6.283185307; diff --git a/base/sources/backends/android_system.c b/base/sources/backends/android_system.c index 1da62944..a76f40a6 100644 --- a/base/sources/backends/android_system.c +++ b/base/sources/backends/android_system.c @@ -847,7 +847,6 @@ static void cmd(struct android_app *app, int32_t cmd) { } else { initDisplay(); - iron_gpu_swap_buffers(); } updateAppForegroundStatus(true, appIsForeground); @@ -1070,7 +1069,7 @@ double iron_time() { return (double)(now.tv_sec - start_sec) + (now.tv_usec / 1000000.0); } -void iron_internal_resize(int width, int height); +void iron_gpu_internal_resize(int width, int height); bool iron_internal_handle_messages(void) { iron_mutex_lock(&unicode_mutex); @@ -1115,7 +1114,7 @@ bool iron_internal_handle_messages(void) { int32_t width = iron_android_width(); int32_t height = iron_android_height(); #ifdef IRON_VULKAN - iron_internal_resize(width, height); + iron_gpu_internal_resize(width, height); #endif iron_internal_call_resize_callback(width, height); } diff --git a/base/sources/backends/direct3d12_gpu.c b/base/sources/backends/direct3d12_gpu.c index 6a453cea..c196e0e4 100644 --- a/base/sources/backends/direct3d12_gpu.c +++ b/base/sources/backends/direct3d12_gpu.c @@ -13,7 +13,7 @@ #include #include -void iron_gpu_internal_resize(int, int); +void gpu_internal_resize(int, int); void iron_memory_emergency(); static ID3D12RootSignature *globalRootSignature = NULL; @@ -131,7 +131,6 @@ static int formatByteSize(iron_image_format_t format) { return 1; case IRON_IMAGE_FORMAT_R16: return 2; - case IRON_IMAGE_FORMAT_BGRA32: case IRON_IMAGE_FORMAT_RGBA32: case IRON_IMAGE_FORMAT_R32: return 4; @@ -238,8 +237,6 @@ static void create_root_signature() { &globalRootSignature); } -void iron_gpu_internal_destroy_window() {} - void iron_gpu_internal_destroy() { if (device) { device->lpVtbl->Release(device); @@ -341,22 +338,18 @@ void iron_gpu_begin(iron_gpu_texture_t *renderTarget) { void iron_gpu_end() { began = false; + if (window_swapChain) { + window_swapChain->lpVtbl->Present(window_swapChain, window_vsync, 0); + } } -void iron_internal_resize(int width, int height) { +void iron_gpu_internal_resize(int width, int height) { if (width == 0 || height == 0) { return; } window_new_width = width; window_new_height = height; - iron_gpu_internal_resize(width, height); -} - -bool iron_gpu_swap_buffers() { - if (window_swapChain) { - window_swapChain->lpVtbl->Present(window_swapChain, window_vsync, 0); - } - return true; + gpu_internal_resize(width, height); } bool iron_gpu_raytrace_supported() { @@ -411,27 +404,10 @@ void iron_gpu_command_list_begin(struct iron_gpu_command_list *list) { void iron_gpu_command_list_end(struct iron_gpu_command_list *list) { list->impl._commandList->lpVtbl->Close(list->impl._commandList); -} -void iron_gpu_command_list_clear(struct iron_gpu_command_list *list, iron_gpu_texture_t *renderTarget, unsigned flags, unsigned color, float depth) { - if (flags & IRON_GPU_CLEAR_COLOR) { - float clearColor[] = {((color & 0x00ff0000) >> 16) / 255.0f, - ((color & 0x0000ff00) >> 8) / 255.0f, - (color & 0x000000ff) / 255.0f, - ((color & 0xff000000) >> 24) / 255.0f}; - - D3D12_CPU_DESCRIPTOR_HANDLE handle; - renderTarget->impl.renderTargetDescriptorHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(renderTarget->impl.renderTargetDescriptorHeap, &handle); - list->impl._commandList->lpVtbl->ClearRenderTargetView(list->impl._commandList, handle, clearColor, 0, NULL); - } - if (flags & IRON_GPU_CLEAR_DEPTH) { - D3D12_CLEAR_FLAGS d3dflags = D3D12_CLEAR_FLAG_DEPTH; - if (renderTarget->impl.depthStencilDescriptorHeap != NULL) { - D3D12_CPU_DESCRIPTOR_HANDLE handle; - renderTarget->impl.depthStencilDescriptorHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(renderTarget->impl.depthStencilDescriptorHeap, &handle); - list->impl._commandList->lpVtbl->ClearDepthStencilView(list->impl._commandList, handle, d3dflags, depth, 0, 0, NULL); - } - } + ID3D12CommandList *commandLists[] = {(ID3D12CommandList *)list->impl._commandList}; + queue->lpVtbl->ExecuteCommandLists(queue, 1, commandLists); + queue->lpVtbl->Signal(queue, list->impl.fence, ++list->impl.fence_value); } void iron_gpu_command_list_render_target_to_framebuffer_barrier(struct iron_gpu_command_list *list, iron_gpu_texture_t *renderTarget) { @@ -486,22 +462,14 @@ void iron_gpu_command_list_set_constant_buffer(struct iron_gpu_command_list *lis list->impl._commandList->lpVtbl->SetGraphicsRootConstantBufferView(list->impl._commandList, 1, buffer->impl.constant_buffer->lpVtbl->GetGPUVirtualAddress(buffer->impl.constant_buffer) + offset); } -void iron_gpu_command_list_draw_indexed_vertices(struct iron_gpu_command_list *list) { - iron_gpu_command_list_draw_indexed_vertices_from_to(list, 0, list->impl._indexCount); -} - -void iron_gpu_command_list_draw_indexed_vertices_from_to(struct iron_gpu_command_list *list, int start, int count) { +void iron_gpu_command_list_draw(struct iron_gpu_command_list *list) { + int start = 0; + int count = list->impl._indexCount; list->impl._commandList->lpVtbl->IASetPrimitiveTopology(list->impl._commandList, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); list->impl._commandList->lpVtbl->DrawIndexedInstanced(list->impl._commandList, count, 1, start, 0, 0); } -void iron_gpu_command_list_execute(iron_gpu_command_list_t *list) { - ID3D12CommandList *commandLists[] = {(ID3D12CommandList *)list->impl._commandList}; - queue->lpVtbl->ExecuteCommandLists(queue, 1, commandLists); - queue->lpVtbl->Signal(queue, list->impl.fence, ++list->impl.fence_value); -} - -void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list) { +void iron_gpu_command_list_wait(iron_gpu_command_list_t *list) { wait_for_fence(list->impl.fence, list->impl.fence_value, list->impl.fence_event); } @@ -610,7 +578,7 @@ void iron_gpu_command_list_set_index_buffer(struct iron_gpu_command_list *list, list->impl._commandList->lpVtbl->IASetIndexBuffer(list->impl._commandList, (D3D12_INDEX_BUFFER_VIEW *) & buffer->impl.index_buffer_view); } -void iron_gpu_command_list_set_render_targets(struct iron_gpu_command_list *list, iron_gpu_texture_t **targets, int count) { +void iron_gpu_command_list_set_render_targets(struct iron_gpu_command_list *list, iron_gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { iron_gpu_texture_t *render_target = targets[0]; D3D12_CPU_DESCRIPTOR_HANDLE target_descriptors[16]; @@ -630,6 +598,25 @@ void iron_gpu_command_list_set_render_targets(struct iron_gpu_command_list *list list->impl._commandList->lpVtbl->RSSetViewports(list->impl._commandList, 1, (D3D12_VIEWPORT *)&render_target->impl.viewport); list->impl._commandList->lpVtbl->RSSetScissorRects(list->impl._commandList, 1, (D3D12_RECT *)&render_target->impl.scissor); list->impl.current_full_scissor = render_target->impl.scissor; + + if (flags & IRON_GPU_CLEAR_COLOR) { + float clearColor[] = {((color & 0x00ff0000) >> 16) / 255.0f, + ((color & 0x0000ff00) >> 8) / 255.0f, + (color & 0x000000ff) / 255.0f, + ((color & 0xff000000) >> 24) / 255.0f}; + + D3D12_CPU_DESCRIPTOR_HANDLE handle; + renderTarget->impl.renderTargetDescriptorHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(renderTarget->impl.renderTargetDescriptorHeap, &handle); + list->impl._commandList->lpVtbl->ClearRenderTargetView(list->impl._commandList, handle, clearColor, 0, NULL); + } + if (flags & IRON_GPU_CLEAR_DEPTH) { + D3D12_CLEAR_FLAGS d3dflags = D3D12_CLEAR_FLAG_DEPTH; + if (renderTarget->impl.depthStencilDescriptorHeap != NULL) { + D3D12_CPU_DESCRIPTOR_HANDLE handle; + renderTarget->impl.depthStencilDescriptorHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(renderTarget->impl.depthStencilDescriptorHeap, &handle); + list->impl._commandList->lpVtbl->ClearDepthStencilView(list->impl._commandList, handle, d3dflags, depth, 0, 0, NULL); + } + } } void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} @@ -786,8 +773,7 @@ void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *lis } iron_gpu_command_list_end(list); - iron_gpu_command_list_execute(list); - iron_gpu_command_list_wait_for_execution_to_finish(list); + iron_gpu_command_list_wait(list); iron_gpu_command_list_begin(list); // Read buffer @@ -809,7 +795,7 @@ void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command } void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipe) { - iron_gpu_internal_pipeline_init(pipe); + gpu_internal_pipeline_init(pipe); } void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipe) { @@ -1545,11 +1531,9 @@ void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { buffer->impl.uploadBuffer->lpVtbl->Release(buffer->impl.uploadBuffer); } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_vertex_buffer_lock(buffer, 0, iron_gpu_vertex_buffer_count(buffer)); -} - -float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0l + int count = iron_gpu_vertex_buffer_count(buffer); buffer->impl.lastStart = start; buffer->impl.lastCount = count; @@ -1565,7 +1549,7 @@ float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int cou return (float *)bytes; } -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = buffer->impl.lastStart * buffer->impl.myStride, .End = (buffer->impl.lastStart + buffer->impl.lastCount) * buffer->impl.myStride, @@ -1573,14 +1557,6 @@ void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { buffer->impl.uploadBuffer->lpVtbl->Unmap(buffer->impl.uploadBuffer, 0, &range); } -void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { - D3D12_RANGE range = { - .Begin = buffer->impl.lastStart * buffer->impl.myStride, - .End = (buffer->impl.lastStart + count) * buffer->impl.myStride, - }; - buffer->impl.uploadBuffer->lpVtbl->Unmap(buffer->impl.uploadBuffer, 0, &range); -} - int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buffer) { return 0; } @@ -1631,10 +1607,6 @@ void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { buffer->impl.constant_buffer->lpVtbl->Release(buffer->impl.constant_buffer); } -void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); -} - void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { buffer->impl.lastStart = start; buffer->impl.lastCount = count; @@ -1729,11 +1701,9 @@ static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); -} - -void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0; + int count = iron_gpu_index_buffer_count(buffer); buffer->impl.last_start = start; buffer->impl.last_count = count; @@ -1749,7 +1719,7 @@ void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count return bytes; } -void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) { D3D12_RANGE range = { .Begin = buffer->impl.last_start * iron_gpu_internal_index_buffer_stride(buffer), .End = (buffer->impl.last_start + buffer->impl.last_count) * iron_gpu_internal_index_buffer_stride(buffer), @@ -1757,14 +1727,6 @@ void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { buffer->impl.upload_buffer->lpVtbl->Unmap(buffer->impl.upload_buffer, 0, &range); } -void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { - D3D12_RANGE range = { - .Begin = buffer->impl.last_start * iron_gpu_internal_index_buffer_stride(buffer), - .End = (buffer->impl.last_start + count) * iron_gpu_internal_index_buffer_stride(buffer), - }; - buffer->impl.upload_buffer->lpVtbl->Unmap(buffer->impl.upload_buffer, 0, &range); -} - int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } @@ -2513,8 +2475,7 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati dxrCommandList->lpVtbl->BuildRaytracingAccelerationStructure(dxrCommandList, &topLevelBuildDesc, 0, NULL); iron_gpu_command_list_end(command_list); - iron_gpu_command_list_execute(command_list); - iron_gpu_command_list_wait_for_execution_to_finish(command_list); + iron_gpu_command_list_wait(command_list); iron_gpu_command_list_begin(command_list); scratchResource->lpVtbl->Release(scratchResource); diff --git a/base/sources/backends/linux_system.c b/base/sources/backends/linux_system.c index f0acc9f9..e7e361c3 100644 --- a/base/sources/backends/linux_system.c +++ b/base/sources/backends/linux_system.c @@ -340,8 +340,6 @@ int iron_window_display() { } void iron_window_destroy() { - iron_gpu_internal_destroy_window(); - xlib.XFlush(x11_ctx.display); struct iron_x11_window *window = &x11_ctx.windows[0]; xlib.XDestroyIC(window->xInputContext); @@ -475,7 +473,7 @@ static char *clipboardString = NULL; char buffer[1024]; -void iron_internal_resize(int width, int height); +void iron_gpu_internal_resize(int width, int height); static void init_pen_device(XDeviceInfo *info, struct x11_pen_device *pen, bool eraser); static void load_lib(void **lib, const char *name) { @@ -1200,7 +1198,7 @@ static bool _handle_messages() { if (event.xconfigure.width != k_window->width || event.xconfigure.height != k_window->height) { k_window->width = event.xconfigure.width; k_window->height = event.xconfigure.height; - iron_internal_resize(event.xconfigure.width, event.xconfigure.height); + iron_gpu_internal_resize(event.xconfigure.width, event.xconfigure.height); iron_internal_call_resize_callback(event.xconfigure.width, event.xconfigure.height); } break; diff --git a/base/sources/backends/metal_gpu.h b/base/sources/backends/metal_gpu.h index 636a1304..b288d52a 100644 --- a/base/sources/backends/metal_gpu.h +++ b/base/sources/backends/metal_gpu.h @@ -52,17 +52,9 @@ typedef struct { typedef struct { int myCount; int myStride; - void *mtlBuffer; - bool gpuMemory; - int lastStart; - int lastCount; - void *metal_buffer; int count; bool gpu_memory; - int last_start; - int last_count; - void *_buffer; int mySize; } gpu_buffer_impl_t; diff --git a/base/sources/backends/metal_gpu.m b/base/sources/backends/metal_gpu.m index 40385acd..4bd7509a 100644 --- a/base/sources/backends/metal_gpu.m +++ b/base/sources/backends/metal_gpu.m @@ -1,57 +1,44 @@ #include #include #include -#import -#import #include #include #include - -static id command_buffer = nil; -static id render_command_encoder = nil; - -static void start_render_pass(void); -static void end_render_pass(void); +#import +#import id getMetalLayer(void); id getMetalDevice(void); id getMetalQueue(void); +void gpu_internal_resize(int, int); -int renderTargetWidth; -int renderTargetHeight; -int newRenderTargetWidth; -int newRenderTargetHeight; -id drawable; -id depthTexture; -int depthBits; -bool iron_internal_metal_has_depth = false; -static iron_gpu_texture_t fallback_render_target; +extern int constant_buffer_index; +bool iron_gpu_transpose_mat = true; +static id command_buffer = nil; +static id command_encoder = nil; +static id argument_encoder = nil; +static id argument_buffer = nil; +static int argument_buffer_step; +static id drawable; +static id depth_texture; +static int depth_bits; +static bool has_depth = false; static int framebuffer_count = 0; - -void iron_gpu_internal_new_render_pass(iron_gpu_texture_t **renderTargets, int count, bool wait, unsigned clear_flags, unsigned color, float depth); -void iron_gpu_internal_pipeline_set(iron_gpu_pipeline_t *pipeline); -static iron_gpu_texture_t *lastRenderTargets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; -static iron_gpu_pipeline_t *lastPipeline = NULL; -extern bool iron_internal_metal_has_depth; -id getMetalDevice(void); -id getMetalQueue(void); -id getMetalEncoder(void); -bool iron_internal_current_render_target_has_depth(void); -static iron_gpu_raytrace_acceleration_structure_t *accel; -static iron_gpu_raytrace_pipeline_t *pipeline; -static iron_gpu_texture_t *output = NULL; -static iron_gpu_buffer_t *constant_buf; +static iron_gpu_texture_t *render_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; typedef struct inst { iron_matrix4x4_t m; int i; } inst_t; -id _raytracing_pipeline; -NSMutableArray *_primitive_accels; -id _instance_accel; -dispatch_semaphore_t _sem; - +static iron_gpu_raytrace_acceleration_structure_t *accel; +static iron_gpu_raytrace_pipeline_t *pipeline; +static iron_gpu_texture_t *output = NULL; +static iron_gpu_buffer_t *constant_buf; +static id _raytracing_pipeline; +static NSMutableArray *_primitive_accels; +static id _instance_accel; +static dispatch_semaphore_t _semaphore; static iron_gpu_texture_t *_texpaint0; static iron_gpu_texture_t *_texpaint1; static iron_gpu_texture_t *_texpaint2; @@ -59,7 +46,6 @@ static iron_gpu_texture_t *_texenv; static iron_gpu_texture_t *_texsobol; static iron_gpu_texture_t *_texscramble; static iron_gpu_texture_t *_texrank; - static iron_gpu_buffer_t *vb[16]; static iron_gpu_buffer_t *vb_last[16]; static iron_gpu_buffer_t *ib[16]; @@ -68,200 +54,87 @@ static int vb_count_last = 0; static inst_t instances[1024]; static int instances_count = 0; -iron_gpu_buffer_t *currentVertexBuffer = NULL; -bool iron_gpu_transpose_mat = true; - -id getMetalEncoder(void) { - return render_command_encoder; -} - -void iron_gpu_internal_destroy_window() { -} - -void iron_gpu_internal_destroy(void) { -} - -void iron_gpu_internal_resize(int, int); - -void iron_internal_resize(int width, int height) { - iron_gpu_internal_resize(width, height); -} - -void iron_gpu_internal_init(void) { -} - -void iron_gpu_internal_init_window(int depthBufferBits, bool vsync) { - depthBits = depthBufferBits; - iron_gpu_render_target_init(&fallback_render_target, 32, 32, IRON_IMAGE_FORMAT_RGBA32, 0); -} - -bool iron_internal_current_render_target_has_depth(void) { - return iron_internal_metal_has_depth; -} - -static void start_render_pass(void) { - id texture = drawable.texture; - MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; - renderPassDescriptor.colorAttachments[0].texture = texture; - renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; - renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; - renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); - renderPassDescriptor.depthAttachment.clearDepth = 1; - renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; - renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; - renderPassDescriptor.depthAttachment.texture = depthTexture; - renderPassDescriptor.stencilAttachment.clearStencil = 0; - renderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionDontCare; - renderPassDescriptor.stencilAttachment.storeAction = MTLStoreActionDontCare; - renderPassDescriptor.stencilAttachment.texture = depthTexture; - - render_command_encoder = [command_buffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; -} - -static void end_render_pass(void) { - [render_command_encoder endEncoding]; - render_command_encoder = nil; -} - -void iron_gpu_begin(iron_gpu_texture_t *renderTarget) { - CAMetalLayer *metalLayer = getMetalLayer(); - drawable = [metalLayer nextDrawable]; - - if (depthBits > 0 && (depthTexture == nil || depthTexture.width != drawable.texture.width || depthTexture.height != drawable.texture.height)) { - MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new]; - descriptor.textureType = MTLTextureType2D; - descriptor.width = drawable.texture.width; - descriptor.height = drawable.texture.height; - descriptor.depth = 1; - descriptor.pixelFormat = MTLPixelFormatDepth32Float_Stencil8; - descriptor.arrayLength = 1; - descriptor.mipmapLevelCount = 1; - descriptor.resourceOptions = MTLResourceStorageModePrivate; - descriptor.usage = MTLTextureUsageRenderTarget; - id device = getMetalDevice(); - depthTexture = [device newTextureWithDescriptor:descriptor]; - iron_internal_metal_has_depth = true; +static MTLBlendFactor convert_blending_factor(iron_gpu_blending_factor_t factor) { + switch (factor) { + case IRON_GPU_BLEND_ONE: + return MTLBlendFactorOne; + case IRON_GPU_BLEND_ZERO: + return MTLBlendFactorZero; + case IRON_GPU_BLEND_SOURCE_ALPHA: + return MTLBlendFactorSourceAlpha; + case IRON_GPU_BLEND_DEST_ALPHA: + return MTLBlendFactorDestinationAlpha; + case IRON_GPU_BLEND_INV_SOURCE_ALPHA: + return MTLBlendFactorOneMinusSourceAlpha; + case IRON_GPU_BLEND_INV_DEST_ALPHA: + return MTLBlendFactorOneMinusDestinationAlpha; } - else { - iron_internal_metal_has_depth = false; - } - - id texture = drawable.texture; - MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; - renderPassDescriptor.colorAttachments[0].texture = texture; - renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; - renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; - renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); - renderPassDescriptor.depthAttachment.clearDepth = 1; - renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; - renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; - renderPassDescriptor.depthAttachment.texture = depthTexture; - renderPassDescriptor.stencilAttachment.clearStencil = 0; - renderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionDontCare; - renderPassDescriptor.stencilAttachment.storeAction = MTLStoreActionDontCare; - renderPassDescriptor.stencilAttachment.texture = depthTexture; - - if (command_buffer != nil && render_command_encoder != nil) { - [render_command_encoder endEncoding]; - [command_buffer commit]; - } - - id commandQueue = getMetalQueue(); - command_buffer = [commandQueue commandBuffer]; - render_command_encoder = [command_buffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; } -void iron_gpu_end() { +static MTLBlendOperation convert_blending_operation(iron_gpu_blending_operation_t op) { + switch (op) { + case IRON_GPU_BLENDOP_ADD: + return MTLBlendOperationAdd; + } } -bool iron_gpu_swap_buffers(void) { - if (command_buffer != nil && render_command_encoder != nil) { - [render_command_encoder endEncoding]; - [command_buffer presentDrawable:drawable]; - [command_buffer commit]; +static MTLCompareFunction convert_compare_mode(iron_gpu_compare_mode_t compare) { + switch (compare) { + case IRON_GPU_COMPARE_MODE_ALWAYS: + return MTLCompareFunctionAlways; + case IRON_GPU_COMPARE_MODE_NEVER: + return MTLCompareFunctionNever; + case IRON_GPU_COMPARE_MODE_LESS: + return MTLCompareFunctionLess; } - drawable = nil; - command_buffer = nil; - render_command_encoder = nil; - - return true; } -void iron_gpu_internal_new_render_pass(iron_gpu_texture_t **renderTargets, int count, bool wait, unsigned clear_flags, unsigned color, float depth) { - if (command_buffer != nil && render_command_encoder != nil) { - [render_command_encoder endEncoding]; - [command_buffer commit]; - if (wait) { - [command_buffer waitUntilCompleted]; - } +static MTLCullMode convert_cull_mode(iron_gpu_cull_mode_t cull) { + switch (cull) { + case IRON_GPU_CULL_MODE_CLOCKWISE: + return MTLCullModeFront; + case IRON_GPU_CULL_MODE_COUNTERCLOCKWISE: + return MTLCullModeBack; + case IRON_GPU_CULL_MODE_NEVER: + return MTLCullModeNone; } - - MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; - for (int i = 0; i < count; ++i) { - if (renderTargets == NULL) { - if (drawable == nil) { - renderPassDescriptor.colorAttachments[i].texture = (__bridge id)fallback_render_target.impl._tex; - renderPassDescriptor.depthAttachment.texture = nil; - renderPassDescriptor.stencilAttachment.texture = nil; - iron_internal_metal_has_depth = false; - } - else { - renderPassDescriptor.colorAttachments[i].texture = drawable.texture; - renderPassDescriptor.depthAttachment.texture = depthTexture; - renderPassDescriptor.stencilAttachment.texture = depthTexture; - iron_internal_metal_has_depth = depthTexture != nil; - } - } - else { - renderPassDescriptor.colorAttachments[i].texture = (__bridge id)renderTargets[i]->impl._tex; - renderPassDescriptor.depthAttachment.texture = (__bridge id)renderTargets[0]->impl._depthTex; - renderPassDescriptor.stencilAttachment.texture = (__bridge id)renderTargets[0]->impl._depthTex; - iron_internal_metal_has_depth = renderTargets[0]->impl._depthTex != nil; - } - if (clear_flags & IRON_GPU_CLEAR_COLOR) { - float red, green, blue, alpha; - iron_color_components(color, &red, &green, &blue, &alpha); - renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; - renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; - renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(red, green, blue, alpha); - } - else { - renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionLoad; - renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; - renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); - } - } - - if (clear_flags & IRON_GPU_CLEAR_DEPTH) { - renderPassDescriptor.depthAttachment.clearDepth = depth; - renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; - renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; - } - else { - renderPassDescriptor.depthAttachment.clearDepth = 1; - renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionLoad; - renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; - } - - renderPassDescriptor.stencilAttachment.clearStencil = 0; - renderPassDescriptor.stencilAttachment.loadAction = MTLLoadActionDontCare; - renderPassDescriptor.stencilAttachment.storeAction = MTLStoreActionDontCare; - - id commandQueue = getMetalQueue(); - command_buffer = [commandQueue commandBuffer]; - render_command_encoder = [command_buffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; } -int iron_gpu_max_bound_textures(void) { - return 16; +static MTLPixelFormat convert_render_target_format(iron_image_format_t format) { + switch (format) { + case IRON_IMAGE_FORMAT_RGBA128: + return MTLPixelFormatRGBA32Float; + case IRON_IMAGE_FORMAT_RGBA64: + return MTLPixelFormatRGBA16Float; + case IRON_IMAGE_FORMAT_R32: + return MTLPixelFormatR32Float; + case IRON_IMAGE_FORMAT_R16: + return MTLPixelFormatR16Float; + case IRON_IMAGE_FORMAT_R8: + return MTLPixelFormatR8Unorm; + case IRON_IMAGE_FORMAT_RGBA32: + default: + return MTLPixelFormatBGRA8Unorm; + } } -void iron_gpu_command_list_init(iron_gpu_command_list_t *list) { - list->impl.current_index_buffer = NULL; +static MTLPixelFormat convert_image_format(iron_image_format_t format) { + switch (format) { + case IRON_IMAGE_FORMAT_RGBA32: + return MTLPixelFormatRGBA8Unorm; + case IRON_IMAGE_FORMAT_R8: + return MTLPixelFormatR8Unorm; + case IRON_IMAGE_FORMAT_R16: + return MTLPixelFormatR16Float; + case IRON_IMAGE_FORMAT_R32: + return MTLPixelFormatR32Float; + case IRON_IMAGE_FORMAT_RGBA128: + return MTLPixelFormatRGBA32Float; + case IRON_IMAGE_FORMAT_RGBA64: + return MTLPixelFormatRGBA16Float; + } } -void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list) {} - static int formatSize(MTLPixelFormat format) { switch (format) { case MTLPixelFormatRGBA32Float: @@ -277,42 +150,186 @@ static int formatSize(MTLPixelFormat format) { } } -void iron_gpu_command_list_begin(iron_gpu_command_list_t *list) { - list->impl.current_index_buffer = NULL; - lastRenderTargets[0] = NULL; +static int formatByteSize(iron_image_format_t format) { + switch (format) { + case IRON_IMAGE_FORMAT_RGBA128: + return 16; + case IRON_IMAGE_FORMAT_RGBA64: + return 8; + case IRON_IMAGE_FORMAT_R16: + return 2; + case IRON_IMAGE_FORMAT_R8: + return 1; + case IRON_IMAGE_FORMAT_RGBA32: + case IRON_IMAGE_FORMAT_R32: + default: + return 4; + } } -void iron_gpu_command_list_end(iron_gpu_command_list_t *list) {} +void iron_gpu_internal_destroy(void) { +} -void iron_gpu_command_list_clear(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget, unsigned flags, unsigned color, float depth) { - if (renderTarget->framebuffer_index >= 0) { - iron_gpu_internal_new_render_pass(NULL, 1, false, flags, color, depth); +void iron_gpu_internal_resize(int width, int height) { + gpu_internal_resize(width, height); +} + +void iron_gpu_internal_init(void) { +} + +id dummy_tex; + +void iron_gpu_internal_init_window(int depth_buffer_bits, bool vsync) { + depth_bits = depth_buffer_bits; + id device = getMetalDevice(); + + MTLSamplerDescriptor *linear_desc = [MTLSamplerDescriptor new]; + linear_desc.minFilter = MTLSamplerMinMagFilterLinear; + linear_desc.magFilter = MTLSamplerMinMagFilterLinear; + linear_desc.mipFilter = MTLSamplerMipFilterLinear; + linear_desc.sAddressMode = MTLSamplerAddressModeRepeat; + linear_desc.tAddressMode = MTLSamplerAddressModeRepeat; + linear_desc.supportArgumentBuffers = true; + id linear_sampler = [device newSamplerStateWithDescriptor:linear_desc]; + + MTLTextureDescriptor *dummy_desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:1 height:1 mipmapped:NO]; + dummy_desc.usage = MTLTextureUsageShaderRead; + dummy_tex = [device newTextureWithDescriptor:dummy_desc]; + uint8_t dummy_data[4] = {0, 0, 0, 255}; + [dummy_tex replaceRegion:MTLRegionMake2D(0, 0, 1, 1) mipmapLevel:0 withBytes:dummy_data bytesPerRow:4]; + + MTLArgumentDescriptor *constantsDesc = [MTLArgumentDescriptor argumentDescriptor]; + constantsDesc.dataType = MTLDataTypePointer; + constantsDesc.index = 0; + + MTLArgumentDescriptor *samplerDesc = [MTLArgumentDescriptor argumentDescriptor]; + samplerDesc.dataType = MTLDataTypeSampler; + samplerDesc.index = 1; + + MTLArgumentDescriptor *textureDesc0 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc0.dataType = MTLDataTypeTexture; + textureDesc0.index = 2; + textureDesc0.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc1 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc1.dataType = MTLDataTypeTexture; + textureDesc1.index = 3; + textureDesc1.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc2 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc2.dataType = MTLDataTypeTexture; + textureDesc2.index = 4; + textureDesc2.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc3 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc3.dataType = MTLDataTypeTexture; + textureDesc3.index = 5; + textureDesc3.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc4 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc4.dataType = MTLDataTypeTexture; + textureDesc4.index = 6; + textureDesc4.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc5 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc5.dataType = MTLDataTypeTexture; + textureDesc5.index = 7; + textureDesc5.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc6 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc6.dataType = MTLDataTypeTexture; + textureDesc6.index = 8; + textureDesc6.textureType = MTLTextureType2D; + + MTLArgumentDescriptor *textureDesc7 = [MTLArgumentDescriptor argumentDescriptor]; + textureDesc7.dataType = MTLDataTypeTexture; + textureDesc7.index = 9; + textureDesc7.textureType = MTLTextureType2D; + + NSArray *arguments = [NSArray arrayWithObjects:constantsDesc, samplerDesc, textureDesc0, textureDesc1, textureDesc2, textureDesc3, textureDesc4, textureDesc5, textureDesc6, textureDesc7, nil]; + argument_encoder = [device newArgumentEncoderWithArguments:arguments]; + + // device.makeArgumentEncoder(bufferIndex:) + + argument_buffer_step = [argument_encoder encodedLength]; + argument_buffer = [device newBufferWithLength:(argument_buffer_step * 2048) options:MTLResourceStorageModeShared]; + for (int i = 0; i < 2048; ++i) { + [argument_encoder setArgumentBuffer:argument_buffer offset:argument_buffer_step * i]; + [argument_encoder setSamplerState:linear_sampler atIndex:1]; + for (int j = 2; j < 10; ++j) { + [argument_encoder setTexture:dummy_tex atIndex:j]; + } + } +} + +void iron_gpu_begin(iron_gpu_texture_t *target) { + CAMetalLayer *layer = getMetalLayer(); + drawable = [layer nextDrawable]; + + if (depth_bits > 0 && (depth_texture == nil || depth_texture.width != drawable.texture.width || depth_texture.height != drawable.texture.height)) { + MTLTextureDescriptor *desc = [MTLTextureDescriptor new]; + desc.textureType = MTLTextureType2D; + desc.width = drawable.texture.width; + desc.height = drawable.texture.height; + desc.depth = 1; + desc.pixelFormat = MTLPixelFormatDepth32Float; + desc.arrayLength = 1; + desc.mipmapLevelCount = 1; + desc.resourceOptions = MTLResourceStorageModePrivate; + desc.usage = MTLTextureUsageRenderTarget; + id device = getMetalDevice(); + depth_texture = [device newTextureWithDescriptor:desc]; + has_depth = true; } else { - iron_gpu_internal_new_render_pass(&renderTarget, 1, false, flags, color, depth); + has_depth = false; } } -void iron_gpu_command_list_render_target_to_framebuffer_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} +void iron_gpu_end() { + [command_encoder endEncoding]; + [command_buffer presentDrawable:drawable]; + [command_buffer commit]; + [command_buffer waitUntilCompleted]; -void iron_gpu_command_list_framebuffer_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} - -void iron_gpu_command_list_draw_indexed_vertices(iron_gpu_command_list_t *list) { - iron_gpu_command_list_draw_indexed_vertices_from_to(list, 0, iron_gpu_index_buffer_count(list->impl.current_index_buffer)); + drawable = nil; + command_buffer = nil; + command_encoder = nil; } -void iron_gpu_command_list_draw_indexed_vertices_from_to(iron_gpu_command_list_t *list, int start, int count) { +int iron_gpu_max_bound_textures(void) { + return 16; +} + +void iron_gpu_command_list_init(iron_gpu_command_list_t *list) { +} + +void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list) { +} + +void iron_gpu_command_list_begin(iron_gpu_command_list_t *list) { + render_targets[0] = NULL; +} + +void iron_gpu_command_list_end(iron_gpu_command_list_t *list) { +} + +void iron_gpu_command_list_render_target_to_framebuffer_barrier(iron_gpu_command_list_t *list, iron_gpu_texture_t *renderTarget) { +} + +void iron_gpu_command_list_framebuffer_to_render_target_barrier(iron_gpu_command_list_t *list, iron_gpu_texture_t *renderTarget) { +} + +void iron_gpu_command_list_draw(iron_gpu_command_list_t *list) { id indexBuffer = (__bridge id)list->impl.current_index_buffer->impl.metal_buffer; - id encoder = getMetalEncoder(); - [encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle - indexCount:count - indexType:MTLIndexTypeUInt32 - indexBuffer:indexBuffer - indexBufferOffset:start * 4]; + [command_encoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle + indexCount:iron_gpu_index_buffer_count(list->impl.current_index_buffer) + indexType:MTLIndexTypeUInt32 + indexBuffer:indexBuffer + indexBufferOffset:0]; } void iron_gpu_command_list_viewport(iron_gpu_command_list_t *list, int x, int y, int width, int height) { - id encoder = getMetalEncoder(); MTLViewport viewport; viewport.originX = x; viewport.originY = y; @@ -320,19 +337,18 @@ void iron_gpu_command_list_viewport(iron_gpu_command_list_t *list, int x, int y, viewport.height = height; viewport.znear = 0.1; viewport.zfar = 100.0; - [encoder setViewport:viewport]; + [command_encoder setViewport:viewport]; } void iron_gpu_command_list_scissor(iron_gpu_command_list_t *list, int x, int y, int width, int height) { - id encoder = getMetalEncoder(); MTLScissorRect scissor; scissor.x = x; scissor.y = y; int target_w = -1; int target_h = -1; - if (lastRenderTargets[0] != NULL) { - target_w = lastRenderTargets[0]->width; - target_h = lastRenderTargets[0]->height; + if (render_targets[0] != NULL) { + target_w = render_targets[0]->width; + target_h = render_targets[0]->height; } else { target_w = iron_window_width(); @@ -340,58 +356,123 @@ void iron_gpu_command_list_scissor(iron_gpu_command_list_t *list, int x, int y, } scissor.width = (x + width <= target_w) ? width : target_w - x; scissor.height = (y + height <= target_h) ? height : target_h - y; - [encoder setScissorRect:scissor]; + [command_encoder setScissorRect:scissor]; } void iron_gpu_command_list_disable_scissor(iron_gpu_command_list_t *list) { - id encoder = getMetalEncoder(); MTLScissorRect scissor; scissor.x = 0; scissor.y = 0; - if (lastRenderTargets[0] != NULL) { - scissor.width = lastRenderTargets[0]->width; - scissor.height = lastRenderTargets[0]->height; + if (render_targets[0] != NULL) { + scissor.width = render_targets[0]->width; + scissor.height = render_targets[0]->height; } else { scissor.width = iron_window_width(); scissor.height = iron_window_height(); } - [encoder setScissorRect:scissor]; + [command_encoder setScissorRect:scissor]; } -void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct iron_gpu_pipeline *pipeline) { - iron_gpu_internal_pipeline_set(pipeline); - lastPipeline = pipeline; +void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, iron_gpu_pipeline_t *pipeline) { + if (has_depth) { + id pipe = (__bridge id)pipeline->impl._pipelineDepth; + [command_encoder setRenderPipelineState:pipe]; + id depthStencil = (__bridge id)pipeline->impl._depthStencil; + [command_encoder setDepthStencilState:depthStencil]; + } + else { + id pipe = (__bridge id)pipeline->impl._pipeline; + [command_encoder setRenderPipelineState:pipe]; + id depthStencil = (__bridge id)pipeline->impl._depthStencilNone; + [command_encoder setDepthStencilState:depthStencil]; + } + [command_encoder setFrontFacingWinding:MTLWindingClockwise]; + [command_encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; } -void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { - iron_gpu_internal_vertex_buffer_set(buffer); +void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buf) { + id buffer = (__bridge id)buf->impl.metal_buffer; + [command_encoder setVertexBuffer:buffer offset:0 atIndex:0]; } -void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) { +void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer) { list->impl.current_index_buffer = buffer; } -void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count) { +void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, iron_gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { if (targets[0]->framebuffer_index >= 0) { - for (int i = 0; i < 8; ++i) - lastRenderTargets[i] = NULL; - iron_gpu_internal_new_render_pass(NULL, 1, false, 0, 0, 0.0f); + for (int i = 0; i < 8; ++i) { + render_targets[i] = NULL; + } + targets = NULL; + count = 1; } else { - for (int i = 0; i < count; ++i) - lastRenderTargets[i] = targets[i]; - for (int i = count; i < 8; ++i) - lastRenderTargets[i] = NULL; - iron_gpu_internal_new_render_pass(targets, count, false, 0, 0, 0.0f); + for (int i = 0; i < count; ++i) { + render_targets[i] = targets[i]; + } + for (int i = count; i < 8; ++i) { + render_targets[i] = NULL; + } } + + if (command_buffer != nil && command_encoder != nil) { + [command_encoder endEncoding]; + [command_buffer commit]; + [command_buffer waitUntilCompleted]; + } + + MTLRenderPassDescriptor *desc = [MTLRenderPassDescriptor renderPassDescriptor]; + for (int i = 0; i < count; ++i) { + if (targets == NULL) { + desc.colorAttachments[i].texture = drawable.texture; + desc.depthAttachment.texture = depth_texture; + has_depth = depth_texture != nil; + } + else { + desc.colorAttachments[i].texture = (__bridge id)targets[i]->impl._tex; + desc.depthAttachment.texture = (__bridge id)targets[0]->impl._depthTex; + has_depth = targets[0]->impl._depthTex != nil; + } + if (flags & IRON_GPU_CLEAR_COLOR) { + float red, green, blue, alpha; + iron_color_components(color, &red, &green, &blue, &alpha); + desc.colorAttachments[i].loadAction = MTLLoadActionClear; + desc.colorAttachments[i].storeAction = MTLStoreActionStore; + desc.colorAttachments[i].clearColor = MTLClearColorMake(red, green, blue, alpha); + } + else { + desc.colorAttachments[i].loadAction = MTLLoadActionLoad; + desc.colorAttachments[i].storeAction = MTLStoreActionStore; + desc.colorAttachments[i].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0); + } + } + + if (flags & IRON_GPU_CLEAR_DEPTH) { + desc.depthAttachment.clearDepth = depth; + desc.depthAttachment.loadAction = MTLLoadActionClear; + desc.depthAttachment.storeAction = MTLStoreActionStore; + } + else { + desc.depthAttachment.clearDepth = 1; + desc.depthAttachment.loadAction = MTLLoadActionLoad; + desc.depthAttachment.storeAction = MTLStoreActionStore; + } + + id commandQueue = getMetalQueue(); + command_buffer = [commandQueue commandBuffer]; + command_encoder = [command_buffer renderCommandEncoderWithDescriptor:desc]; } -void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} +void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer) { +} -void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} +void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer) { +} -void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture) {} +void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, iron_gpu_texture_t *texture) { +} void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *list, iron_gpu_texture_t *render_target, uint8_t *data) { // Create readback buffer @@ -434,166 +515,56 @@ void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *lis [tex getBytes:data bytesPerRow:formatByteSize * render_target->width fromRegion:region mipmapLevel:0]; } -void iron_gpu_command_list_execute(iron_gpu_command_list_t *list) { - if (lastRenderTargets[0] == NULL) { - iron_gpu_internal_new_render_pass(NULL, 1, false, 0, 0, 0.0f); - } - else { - int count = 1; - while (lastRenderTargets[count] != NULL) - count++; - iron_gpu_internal_new_render_pass(lastRenderTargets, count, false, 0, 0, 0.0f); - } - if (lastPipeline != NULL) - iron_gpu_internal_pipeline_set(lastPipeline); +void iron_gpu_command_list_wait(iron_gpu_command_list_t *list) { + } -void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list) { - id commandQueue = getMetalQueue(); - id commandBuffer = [commandQueue commandBuffer]; - [commandBuffer commit]; - [commandBuffer waitUntilCompleted]; -} - -void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer, int offset, size_t size) { +void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer, int offset, size_t size) { id buf = (__bridge id)buffer->impl._buffer; - id encoder = getMetalEncoder(); - [encoder setVertexBuffer:buf offset:offset atIndex:1]; - - id buf = (__bridge id)buffer->impl._buffer; - id encoder = getMetalEncoder(); - [encoder setFragmentBuffer:buf offset:offset atIndex:1]; + int i = constant_buffer_index; + [argument_encoder setArgumentBuffer:argument_buffer offset:argument_buffer_step * i]; + [argument_encoder setBuffer:buf offset:offset atIndex:0 ]; + [command_encoder setVertexBuffer:argument_buffer offset:argument_buffer_step * i atIndex:1]; + [command_encoder setFragmentBuffer:argument_buffer offset:argument_buffer_step * i atIndex:1]; + [command_encoder useResource:buf usage:MTLResourceUsageRead]; + [command_encoder useResource:argument_buffer usage:MTLResourceUsageRead]; + [command_encoder useResource:dummy_tex usage:MTLResourceUsageRead]; } -void iron_gpu_command_list_render_target_to_texture_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) { +void iron_gpu_command_list_render_target_to_texture_barrier(iron_gpu_command_list_t *list, iron_gpu_texture_t *renderTarget) { } -void iron_gpu_command_list_texture_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} +void iron_gpu_command_list_texture_to_render_target_barrier(iron_gpu_command_list_t *list, iron_gpu_texture_t *renderTarget) { +} void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture) { id tex = (__bridge id)texture->impl._tex; - [render_command_encoder setVertexTexture:tex atIndex:unit.offset]; - [render_command_encoder setFragmentTexture:tex atIndex:unit.offset]; + int i = constant_buffer_index; + [argument_encoder setArgumentBuffer:argument_buffer offset:argument_buffer_step * i]; + [argument_encoder setTexture:tex atIndex:unit.offset + 2]; + [command_encoder useResource:tex usage:MTLResourceUsageRead]; } void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *target) { - id encoder = getMetalEncoder(); id depth_tex = (__bridge id)target->impl._depthTex; - [encoder setVertexTexture:depth_tex atIndex:unit.offset]; - [encoder setFragmentTexture:depth_tex atIndex:unit.offset]; -} - -static MTLBlendFactor convert_blending_factor(iron_gpu_blending_factor_t factor) { - switch (factor) { - case IRON_GPU_BLEND_ONE: - return MTLBlendFactorOne; - case IRON_GPU_BLEND_ZERO: - return MTLBlendFactorZero; - case IRON_GPU_BLEND_SOURCE_ALPHA: - return MTLBlendFactorSourceAlpha; - case IRON_GPU_BLEND_DEST_ALPHA: - return MTLBlendFactorDestinationAlpha; - case IRON_GPU_BLEND_INV_SOURCE_ALPHA: - return MTLBlendFactorOneMinusSourceAlpha; - case IRON_GPU_BLEND_INV_DEST_ALPHA: - return MTLBlendFactorOneMinusDestinationAlpha; - case IRON_GPU_BLEND_SOURCE_COLOR: - return MTLBlendFactorSourceColor; - case IRON_GPU_BLEND_DEST_COLOR: - return MTLBlendFactorDestinationColor; - case IRON_GPU_BLEND_INV_SOURCE_COLOR: - return MTLBlendFactorOneMinusSourceColor; - case IRON_GPU_BLEND_INV_DEST_COLOR: - return MTLBlendFactorOneMinusDestinationColor; - case IRON_GPU_BLEND_CONSTANT: - return MTLBlendFactorBlendColor; - case IRON_GPU_BLEND_INV_CONSTANT: - return MTLBlendFactorOneMinusBlendColor; - } -} - -static MTLBlendOperation convert_blending_operation(iron_gpu_blending_operation_t op) { - switch (op) { - case IRON_GPU_BLENDOP_ADD: - return MTLBlendOperationAdd; - case IRON_GPU_BLENDOP_SUBTRACT: - return MTLBlendOperationSubtract; - case IRON_GPU_BLENDOP_REVERSE_SUBTRACT: - return MTLBlendOperationReverseSubtract; - case IRON_GPU_BLENDOP_MIN: - return MTLBlendOperationMin; - case IRON_GPU_BLENDOP_MAX: - return MTLBlendOperationMax; - } -} - -static MTLCompareFunction convert_compare_mode(iron_gpu_compare_mode_t compare) { - switch (compare) { - case IRON_GPU_COMPARE_MODE_ALWAYS: - return MTLCompareFunctionAlways; - case IRON_GPU_COMPARE_MODE_NEVER: - return MTLCompareFunctionNever; - case IRON_GPU_COMPARE_MODE_EQUAL: - return MTLCompareFunctionEqual; - case IRON_GPU_COMPARE_MODE_NOT_EQUAL: - return MTLCompareFunctionNotEqual; - case IRON_GPU_COMPARE_MODE_LESS: - return MTLCompareFunctionLess; - case IRON_GPU_COMPARE_MODE_LESS_EQUAL: - return MTLCompareFunctionLessEqual; - case IRON_GPU_COMPARE_MODE_GREATER: - return MTLCompareFunctionGreater; - case IRON_GPU_COMPARE_MODE_GREATER_EQUAL: - return MTLCompareFunctionGreaterEqual; - } -} - -static MTLCullMode convert_cull_mode(iron_gpu_cull_mode_t cull) { - switch (cull) { - case IRON_GPU_CULL_MODE_CLOCKWISE: - return MTLCullModeFront; - case IRON_GPU_CULL_MODE_COUNTERCLOCKWISE: - return MTLCullModeBack; - case IRON_GPU_CULL_MODE_NEVER: - return MTLCullModeNone; - } -} - -static MTLPixelFormat convert_render_target_format(iron_image_format_t format) { - switch (format) { - case IRON_IMAGE_FORMAT_RGBA128: - return MTLPixelFormatRGBA32Float; - case IRON_IMAGE_FORMAT_RGBA64: - return MTLPixelFormatRGBA16Float; - case IRON_IMAGE_FORMAT_R32: - return MTLPixelFormatR32Float; - case IRON_IMAGE_FORMAT_R16: - return MTLPixelFormatR16Float; - case IRON_IMAGE_FORMAT_R8: - return MTLPixelFormatR8Unorm; - case IRON_IMAGE_FORMAT_RGBA32: - default: - return MTLPixelFormatBGRA8Unorm; - } + int i = constant_buffer_index; + [argument_encoder setArgumentBuffer:argument_buffer offset:argument_buffer_step * i]; + [argument_encoder setTexture:depth_tex atIndex:unit.offset + 2]; + [command_encoder useResource:depth_tex usage:MTLResourceUsageRead]; } void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipeline) { memset(&pipeline->impl, 0, sizeof(pipeline->impl)); - iron_gpu_internal_pipeline_init(pipeline); + gpu_internal_pipeline_init(pipeline); } void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipeline) { - pipeline->impl._reflection = NULL; pipeline->impl._depthStencil = NULL; id pipe = (__bridge_transfer id)pipeline->impl._pipeline; pipe = nil; pipeline->impl._pipeline = NULL; - MTLRenderPipelineReflection *reflection = (__bridge_transfer MTLRenderPipelineReflection *)pipeline->impl._reflection; - reflection = nil; - pipeline->impl._reflection = NULL; - id pipeDepth = (__bridge_transfer id)pipeline->impl._pipelineDepth; pipeDepth = nil; pipeline->impl._pipelineDepth = NULL; @@ -607,15 +578,6 @@ void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipeline) { pipeline->impl._depthStencilNone = NULL; } -static int findAttributeIndex(NSArray *attributes, const char *name) { - for (MTLVertexAttribute *attribute in attributes) { - if (strcmp(name, [[attribute name] UTF8String]) == 0) { - return (int)[attribute attributeIndex]; - } - } - return -1; -} - void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { id device = getMetalDevice(); @@ -634,6 +596,7 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; renderPipelineDesc.vertexFunction = (__bridge id)pipeline->vertex_shader->impl.mtlFunction; renderPipelineDesc.fragmentFunction = (__bridge id)pipeline->fragment_shader->impl.mtlFunction; + for (int i = 0; i < pipeline->color_attachment_count; ++i) { renderPipelineDesc.colorAttachments[i].pixelFormat = convert_render_target_format(pipeline->color_attachment[i]); renderPipelineDesc.colorAttachments[i].blendingEnabled = @@ -652,22 +615,14 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { (pipeline->color_write_mask_alpha[i] ? MTLColorWriteMaskAlpha : 0); } renderPipelineDesc.depthAttachmentPixelFormat = MTLPixelFormatInvalid; - renderPipelineDesc.stencilAttachmentPixelFormat = MTLPixelFormatInvalid; float offset = 0; MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init]; for (int i = 0; i < pipeline->input_layout->size; ++i) { - int index = findAttributeIndex(renderPipelineDesc.vertexFunction.vertexAttributes, pipeline->input_layout->elements[i].name); - - if (index < 0) { - iron_log("Could not find vertex attribute %s\n", pipeline->input_layout->elements[i].name); - } - - if (index >= 0) { - vertexDescriptor.attributes[index].bufferIndex = 0; - vertexDescriptor.attributes[index].offset = offset; - } + int index = i; + vertexDescriptor.attributes[index].bufferIndex = 0; + vertexDescriptor.attributes[index].offset = offset; offset += iron_gpu_vertex_data_size(pipeline->input_layout->elements[i].data); if (index >= 0) { @@ -705,25 +660,18 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { NSError *errors = nil; MTLRenderPipelineReflection *reflection = nil; - pipeline->impl._pipeline = (__bridge_retained void *)[device newRenderPipelineStateWithDescriptor:renderPipelineDesc - options:MTLPipelineOptionBufferTypeInfo - reflection:&reflection - error:&errors]; - if (errors != nil) - NSLog(@"%@", [errors localizedDescription]); - assert(pipeline->impl._pipeline && !errors); + pipeline->impl._pipeline = (__bridge_retained void *)[ + device newRenderPipelineStateWithDescriptor:renderPipelineDesc + options:MTLPipelineOptionBufferTypeInfo + reflection:&reflection + error:&errors]; - renderPipelineDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8; - renderPipelineDesc.stencilAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8; - pipeline->impl._pipelineDepth = (__bridge_retained void *)[device newRenderPipelineStateWithDescriptor:renderPipelineDesc - options:MTLPipelineOptionBufferTypeInfo - reflection:&reflection - error:&errors]; - if (errors != nil) - NSLog(@"%@", [errors localizedDescription]); - assert(pipeline->impl._pipelineDepth && !errors); - - pipeline->impl._reflection = (__bridge_retained void *)reflection; + renderPipelineDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float; + pipeline->impl._pipelineDepth = (__bridge_retained void *)[ + device newRenderPipelineStateWithDescriptor:renderPipelineDesc + options:MTLPipelineOptionBufferTypeInfo + reflection:&reflection + error:&errors]; MTLDepthStencilDescriptor *depthStencilDescriptor = [MTLDepthStencilDescriptor new]; depthStencilDescriptor.depthCompareFunction = convert_compare_mode(pipeline->depth_mode); @@ -735,65 +683,13 @@ void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline) { pipeline->impl._depthStencilNone = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depthStencilDescriptor]; } -void iron_gpu_internal_pipeline_set(iron_gpu_pipeline_t *pipeline) { - id encoder = getMetalEncoder(); - if (iron_internal_current_render_target_has_depth()) { - id pipe = (__bridge id)pipeline->impl._pipelineDepth; - [encoder setRenderPipelineState:pipe]; - id depthStencil = (__bridge id)pipeline->impl._depthStencil; - [encoder setDepthStencilState:depthStencil]; - } - else { - id pipe = (__bridge id)pipeline->impl._pipeline; - [encoder setRenderPipelineState:pipe]; - id depthStencil = (__bridge id)pipeline->impl._depthStencilNone; - [encoder setDepthStencilState:depthStencil]; - } - [encoder setFrontFacingWinding:MTLWindingClockwise]; - [encoder setCullMode:convert_cull_mode(pipeline->cull_mode)]; -} - iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pipeline_t *pipeline, const char *name) { iron_gpu_constant_location_t location; - location.impl.vertexOffset = -1; - - MTLRenderPipelineReflection *reflection = (__bridge MTLRenderPipelineReflection *)pipeline->impl._reflection; - - for (MTLArgument *arg in reflection.vertexArguments) { - if (arg.type == MTLArgumentTypeBuffer && [arg.name isEqualToString:@"uniforms"]) { - if ([arg bufferDataType] == MTLDataTypeStruct) { - MTLStructType *structObj = [arg bufferStructType]; - for (MTLStructMember *member in structObj.members) { - if (strcmp([[member name] UTF8String], name) == 0) { - location.impl.vertexOffset = (int)[member offset]; - break; - } - } - } - break; - } - } - - for (MTLArgument *arg in reflection.fragmentArguments) { - if ([arg type] == MTLArgumentTypeBuffer && [[arg name] isEqualToString:@"uniforms"]) { - if ([arg bufferDataType] == MTLDataTypeStruct) { - MTLStructType *structObj = [arg bufferStructType]; - for (MTLStructMember *member in structObj.members) { - if (strcmp([[member name] UTF8String], name) == 0) { - location.impl.vertexOffset = (int)[member offset]; - break; - } - } - } - break; - } - } - return location; } iron_gpu_texture_unit_t iron_gpu_pipeline_get_texture_unit(iron_gpu_pipeline_t *pipeline, const char *name) { - iron_gpu_texture_unit_t unit = {0}; + iron_gpu_texture_unit_t unit; unit.offset = -1; return unit; } @@ -820,13 +716,7 @@ void iron_gpu_shader_init(iron_gpu_shader_t *shader, const void *data, size_t le shader->impl.length = length; } -bool iron_gpu_raytrace_supported() { - id device = getMetalDevice(); - return device.supportsRaytracing; -} - -void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iron_gpu_command_list_t *command_list, void *ray_shader, int ray_shader_size, - iron_gpu_buffer_t *constant_buffer) { +void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iron_gpu_command_list_t *command_list, void *ray_shader, int ray_shader_size, iron_gpu_buffer_t *constant_buffer) { id device = getMetalDevice(); if (!device.supportsRaytracing) return; constant_buf = constant_buffer; @@ -843,12 +733,320 @@ void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, iro descriptor.computeFunction = [library newFunctionWithName:@"raytracingKernel"]; descriptor.threadGroupSizeIsMultipleOfThreadExecutionWidth = YES; _raytracing_pipeline = [device newComputePipelineStateWithDescriptor:descriptor options:0 reflection:nil error:&error]; - _sem = dispatch_semaphore_create(2); + _semaphore = dispatch_semaphore_create(2); } void iron_gpu_raytrace_pipeline_destroy(iron_gpu_raytrace_pipeline_t *pipeline) { } +static void create(iron_gpu_texture_t *texture, int width, int height, int format, bool writable) { + texture->impl.has_mipmaps = false; + id device = getMetalDevice(); + + MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((iron_image_format_t)format) + width:width + height:height + mipmapped:NO]; + descriptor.textureType = MTLTextureType2D; + descriptor.width = width; + descriptor.height = height; + descriptor.depth = 1; + descriptor.pixelFormat = convert_image_format((iron_image_format_t)format); + descriptor.arrayLength = 1; + descriptor.mipmapLevelCount = 1; + // TODO: Make less textures writable + if (writable) { + descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; + } + + texture->impl._tex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; +} + +void iron_gpu_texture_init(iron_gpu_texture_t *texture, int width, int height, iron_image_format_t format) { + texture->width = width; + texture->height = height; + texture->format = format; + texture->impl.data = malloc(width * height * (format == IRON_IMAGE_FORMAT_R8 ? 1 : 4)); + create(texture, width, height, format, true); + texture->_uploaded = true; + texture->data = NULL; + texture->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; + texture->framebuffer_index = -1; +} + +void iron_gpu_texture_init_from_bytes(iron_gpu_texture_t *texture, void *data, int width, int height, iron_image_format_t format) { + texture->width = width; + texture->height = height; + texture->format = format; + texture->data = data; + texture->_uploaded = false; + texture->impl.data = NULL; + texture->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; + texture->framebuffer_index = -1; + create(texture, width, height, format, true); + id tex = (__bridge id)texture->impl._tex; + [tex replaceRegion:MTLRegionMake2D(0, 0, texture->width, texture->height) + mipmapLevel:0 + slice:0 + withBytes:data + bytesPerRow:iron_gpu_texture_stride(texture) + bytesPerImage:iron_gpu_texture_stride(texture) * texture->height]; +} + +void iron_gpu_texture_destroy(iron_gpu_texture_t *target) { + id tex = (__bridge_transfer id)target->impl._tex; + 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; + + if (target->framebuffer_index >= 0) { + framebuffer_count -= 1; + } + + if (target->impl.data != NULL) { + free(target->impl.data); + target->impl.data = NULL; + } +} + +int iron_gpu_texture_stride(iron_gpu_texture_t *texture) { + switch (texture->format) { + case IRON_IMAGE_FORMAT_R8: + return texture->width; + case IRON_IMAGE_FORMAT_RGBA32: + default: + return texture->width * 4; + case IRON_IMAGE_FORMAT_RGBA64: + return texture->width * 8; + case IRON_IMAGE_FORMAT_RGBA128: + return texture->width * 16; + } +} + +void iron_gpu_texture_generate_mipmaps(iron_gpu_texture_t *texture, int levels) {} + +void iron_gpu_texture_set_mipmap(iron_gpu_texture_t *texture, iron_gpu_texture_t *mipmap, int level) { + if (!texture->impl.has_mipmaps) { + id device = getMetalDevice(); + MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((iron_image_format_t)texture->format) + width:texture->width + height:texture->height + mipmapped:YES]; + descriptor.textureType = MTLTextureType2D; + descriptor.width = texture->width; + descriptor.height = texture->height; + descriptor.depth = 1; + descriptor.pixelFormat = convert_image_format((iron_image_format_t)texture->format); + descriptor.arrayLength = 1; + bool writable = true; + if (writable) { + descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; + } + void *mipmaptex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; + + id commandQueue = getMetalQueue(); + id commandBuffer = [commandQueue commandBuffer]; + id commandEncoder = [commandBuffer blitCommandEncoder]; + [commandEncoder copyFromTexture:(__bridge id)texture->impl._tex + sourceSlice:0 + sourceLevel:0 + sourceOrigin:MTLOriginMake(0, 0, 0) + sourceSize:MTLSizeMake(texture->width, texture->height, 1) + toTexture:(__bridge id)mipmaptex + destinationSlice:0 + destinationLevel:0 + destinationOrigin:MTLOriginMake(0, 0, 0)]; + + [commandEncoder endEncoding]; + [commandBuffer commit]; + [commandBuffer waitUntilCompleted]; + + id tex = (__bridge_transfer id)texture->impl._tex; + tex = nil; + texture->impl._tex = mipmaptex; + + texture->impl.has_mipmaps = true; + } + + id tex = (__bridge id)texture->impl._tex; + [tex replaceRegion:MTLRegionMake2D(0, 0, mipmap->width, mipmap->height) + mipmapLevel:level + withBytes:mipmap->data + bytesPerRow:mipmap->width * formatByteSize(mipmap->format)]; +} + +static void render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depth_buffer_bits, int framebuffer_index) { + memset(target, 0, sizeof(iron_gpu_texture_t)); + target->width = width; + target->height = height; + target->data = NULL; + target->_uploaded = true; + target->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; + target->framebuffer_index = framebuffer_index; + + id device = getMetalDevice(); + MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new]; + descriptor.textureType = MTLTextureType2D; + descriptor.width = width; + descriptor.height = height; + descriptor.depth = 1; + descriptor.pixelFormat = convert_render_target_format(format); + descriptor.arrayLength = 1; + descriptor.mipmapLevelCount = 1; + descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; + 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]; + } + + target->impl._texReadback = NULL; +} + +void iron_gpu_render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depth_buffer_bits) { + render_target_init(target, width, height, format, depth_buffer_bits, -1); + target->width = target->width = width; + target->height = target->height = height; + target->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; + target->_uploaded = true; +} + +void iron_gpu_render_target_init_framebuffer(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depth_buffer_bits) { + render_target_init(target, width, height, format, depth_buffer_bits, framebuffer_count); + framebuffer_count += 1; +} + +void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_texture_t *source) { + target->impl._depthTex = source->impl._depthTex; +} + +void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpu_memory) { + memset(&buffer->impl, 0, sizeof(buffer->impl)); + buffer->impl.myCount = count; + buffer->impl.gpu_memory = gpu_memory; + for (int i = 0; i < structure->size; ++i) { + iron_gpu_vertex_element_t element = structure->elements[i]; + buffer->impl.myStride += iron_gpu_vertex_data_size(element.data); + } + + id device = getMetalDevice(); + MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; + options |= MTLResourceStorageModeShared; + + id buf = [device newBufferWithLength:count * buffer->impl.myStride options:options]; + buffer->impl.metal_buffer = (__bridge_retained void *)buf; +} + +void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buf) { + id buffer = (__bridge_transfer id)buf->impl.metal_buffer; + buffer = nil; + buf->impl.metal_buffer = NULL; +} + +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buf) { + id buffer = (__bridge id)buf->impl.metal_buffer; + float *floats = (float *)[buffer contents]; + return floats; +} + +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buf) { +} + +int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { + return buffer->impl.myCount; +} + +int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { + return buffer->impl.myStride; +} + +void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { + buffer->impl.mySize = size; + buffer->data = NULL; + buffer->impl._buffer = (__bridge_retained void *)[getMetalDevice() newBufferWithLength:size options:MTLResourceOptionCPUCacheModeDefault]; +} + +void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { + id buf = (__bridge_transfer id)buffer->impl._buffer; + buf = nil; + buffer->impl._buffer = NULL; +} + +void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { + id buf = (__bridge id)buffer->impl._buffer; + uint8_t *data = (uint8_t *)[buf contents]; + buffer->data = &data[start]; +} + +void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { + // buffer->data = NULL; +} + +int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { + return buffer->impl.mySize; +} + +void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int indexCount, bool gpu_memory) { + buffer->impl.count = indexCount; + buffer->impl.gpu_memory = gpu_memory; + + id device = getMetalDevice(); + MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; + options |= MTLResourceStorageModeShared; + + buffer->impl.metal_buffer = (__bridge_retained void *)[device + newBufferWithLength:sizeof(uint32_t) * indexCount + options:options]; +} + +void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { + id buf = (__bridge_transfer id)buffer->impl.metal_buffer; + buf = nil; + buffer->impl.metal_buffer = NULL; +} + +static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { + return 4; +} + +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0; + int count = iron_gpu_index_buffer_count(buffer); + id metal_buffer = (__bridge id)buffer->impl.metal_buffer; + uint8_t *data = (uint8_t *)[metal_buffer contents]; + return &data[start * iron_gpu_internal_index_buffer_stride(buffer)]; +} + +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) { +} + +int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { + return buffer->impl.count; +} + +bool iron_gpu_raytrace_supported() { + id device = getMetalDevice(); + return device.supportsRaytracing; +} + id create_acceleration_sctructure(MTLAccelerationStructureDescriptor *descriptor) { id device = getMetalDevice(); id queue = getMetalQueue(); @@ -951,7 +1149,7 @@ void iron_gpu_raytrace_acceleration_structure_build(iron_gpu_raytrace_accelerati MTLAccelerationStructureTriangleGeometryDescriptor *descriptor = [MTLAccelerationStructureTriangleGeometryDescriptor descriptor]; descriptor.indexType = MTLIndexTypeUInt32; descriptor.indexBuffer = (__bridge id)ib[0]->impl.metal_buffer; - descriptor.vertexBuffer = (__bridge id)vb[0]->impl.mtlBuffer; + descriptor.vertexBuffer = (__bridge id)vb[0]->impl.metal_buffer; descriptor.vertexStride = vb[0]->impl.myStride; descriptor.triangleCount = ib[0]->impl.count / 3; descriptor.vertexFormat = MTLAttributeFormatShort4Normalized; @@ -1007,11 +1205,11 @@ void iron_gpu_raytrace_set_target(iron_gpu_texture_t *_output) { void iron_gpu_raytrace_dispatch_rays(iron_gpu_command_list_t *command_list) { id device = getMetalDevice(); if (!device.supportsRaytracing) return; - dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER); + dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); id queue = getMetalQueue(); id command_buffer = [queue commandBuffer]; - __block dispatch_semaphore_t sem = _sem; + __block dispatch_semaphore_t sem = _semaphore; [command_buffer addCompletedHandler:^(id buffer) { dispatch_semaphore_signal(sem); }]; @@ -1026,7 +1224,7 @@ void iron_gpu_raytrace_dispatch_rays(iron_gpu_command_list_t *command_list) { [compute_encoder setBuffer:(__bridge id)constant_buf->impl._buffer offset:0 atIndex:0]; [compute_encoder setAccelerationStructure:_instance_accel atBufferIndex:1]; [compute_encoder setBuffer: (__bridge id)ib[0]->impl.metal_buffer offset:0 atIndex:2]; - [compute_encoder setBuffer: (__bridge id)vb[0]->impl.mtlBuffer offset:0 atIndex:3]; + [compute_encoder setBuffer: (__bridge id)vb[0]->impl.metal_buffer offset:0 atIndex:3]; [compute_encoder setTexture:(__bridge id)output->impl._tex atIndex:0]; [compute_encoder setTexture:(__bridge id)_texpaint0->impl._tex atIndex:1]; [compute_encoder setTexture:(__bridge id)_texpaint1->impl._tex atIndex:2]; @@ -1045,415 +1243,3 @@ void iron_gpu_raytrace_dispatch_rays(iron_gpu_command_list_t *command_list) { [compute_encoder endEncoding]; [command_buffer commit]; } - -static MTLPixelFormat convert_image_format(iron_image_format_t format) { - switch (format) { - case IRON_IMAGE_FORMAT_RGBA32: - return MTLPixelFormatRGBA8Unorm; - case IRON_IMAGE_FORMAT_R8: - return MTLPixelFormatR8Unorm; - case IRON_IMAGE_FORMAT_R16: - return MTLPixelFormatR16Float; - case IRON_IMAGE_FORMAT_R32: - return MTLPixelFormatR32Float; - case IRON_IMAGE_FORMAT_RGBA128: - return MTLPixelFormatRGBA32Float; - case IRON_IMAGE_FORMAT_RGBA64: - return MTLPixelFormatRGBA16Float; - case IRON_IMAGE_FORMAT_BGRA32: - return MTLPixelFormatBGRA8Unorm; - } -} - -static int formatByteSize(iron_image_format_t format) { - switch (format) { - case IRON_IMAGE_FORMAT_RGBA128: - return 16; - case IRON_IMAGE_FORMAT_RGBA64: - return 8; - case IRON_IMAGE_FORMAT_R16: - return 2; - case IRON_IMAGE_FORMAT_R8: - return 1; - case IRON_IMAGE_FORMAT_BGRA32: - case IRON_IMAGE_FORMAT_RGBA32: - case IRON_IMAGE_FORMAT_R32: - default: - return 4; - } -} - -static void create(iron_gpu_texture_t *texture, int width, int height, int format, bool writable) { - texture->impl.has_mipmaps = false; - id device = getMetalDevice(); - - MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((iron_image_format_t)format) - width:width - height:height - mipmapped:NO]; - descriptor.textureType = MTLTextureType2D; - descriptor.width = width; - descriptor.height = height; - descriptor.depth = 1; - descriptor.pixelFormat = convert_image_format((iron_image_format_t)format); - descriptor.arrayLength = 1; - descriptor.mipmapLevelCount = 1; - // TODO: Make less textures writable - if (writable) { - descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; - } - - texture->impl._tex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; -} - -void iron_gpu_texture_init(iron_gpu_texture_t *texture, int width, int height, iron_image_format_t format) { - texture->width = width; - texture->height = height; - texture->format = format; - texture->impl.data = malloc(width * height * (format == IRON_IMAGE_FORMAT_R8 ? 1 : 4)); - create(texture, width, height, format, true); - texture->_uploaded = true; - texture->data = NULL; - texture->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; - texture->framebuffer_index = -1; -} - -void iron_gpu_texture_init_from_bytes(iron_gpu_texture_t *texture, void *data, int width, int height, iron_image_format_t format) { - texture->width = width; - texture->height = height; - texture->format = format; - texture->data = data; - texture->_uploaded = false; - texture->impl.data = NULL; - texture->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; - texture->framebuffer_index = -1; - create(texture, width, height, format, true); - id tex = (__bridge id)texture->impl._tex; - [tex replaceRegion:MTLRegionMake2D(0, 0, texture->width, texture->height) - mipmapLevel:0 - slice:0 - withBytes:data - bytesPerRow:iron_gpu_texture_stride(texture) - bytesPerImage:iron_gpu_texture_stride(texture) * texture->height]; -} - -void iron_gpu_texture_destroy(iron_gpu_texture_t *target) { - id tex = (__bridge_transfer id)target->impl._tex; - 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; - - if (target->framebuffer_index >= 0) { - framebuffer_count -= 1; - } - - if (target->impl.data != NULL) { - free(target->impl.data); - target->impl.data = NULL; - } -} - -int iron_gpu_texture_stride(iron_gpu_texture_t *texture) { - switch (texture->format) { - case IRON_IMAGE_FORMAT_R8: - return texture->width; - case IRON_IMAGE_FORMAT_RGBA32: - case IRON_IMAGE_FORMAT_BGRA32: - default: - return texture->width * 4; - case IRON_IMAGE_FORMAT_RGBA64: - return texture->width * 8; - case IRON_IMAGE_FORMAT_RGBA128: - return texture->width * 16; - } -} - -void iron_gpu_texture_generate_mipmaps(iron_gpu_texture_t *texture, int levels) {} - -void iron_gpu_texture_set_mipmap(iron_gpu_texture_t *texture, iron_gpu_texture_t *mipmap, int level) { - if (!texture->impl.has_mipmaps) { - id device = getMetalDevice(); - MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((iron_image_format_t)texture->format) - width:texture->width - height:texture->height - mipmapped:YES]; - descriptor.textureType = MTLTextureType2D; - descriptor.width = texture->width; - descriptor.height = texture->height; - descriptor.depth = 1; - descriptor.pixelFormat = convert_image_format((iron_image_format_t)texture->format); - descriptor.arrayLength = 1; - bool writable = true; - if (writable) { - descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; - } - void *mipmaptex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; - - id commandQueue = getMetalQueue(); - id commandBuffer = [commandQueue commandBuffer]; - id commandEncoder = [commandBuffer blitCommandEncoder]; - [commandEncoder copyFromTexture:(__bridge id)texture->impl._tex - sourceSlice:0 - sourceLevel:0 - sourceOrigin:MTLOriginMake(0, 0, 0) - sourceSize:MTLSizeMake(texture->width, texture->height, 1) - toTexture:(__bridge id)mipmaptex - destinationSlice:0 - destinationLevel:0 - destinationOrigin:MTLOriginMake(0, 0, 0)]; - - [commandEncoder endEncoding]; - [commandBuffer commit]; - [commandBuffer waitUntilCompleted]; - - id tex = (__bridge_transfer id)texture->impl._tex; - tex = nil; - texture->impl._tex = mipmaptex; - - texture->impl.has_mipmaps = true; - } - - id tex = (__bridge id)texture->impl._tex; - [tex replaceRegion:MTLRegionMake2D(0, 0, mipmap->width, mipmap->height) - mipmapLevel:level - withBytes:mipmap->data - bytesPerRow:mipmap->width * formatByteSize(mipmap->format)]; -} - -static MTLPixelFormat convert_format(iron_image_format_t format) { - switch (format) { - case IRON_IMAGE_FORMAT_RGBA128: - return MTLPixelFormatRGBA32Float; - case IRON_IMAGE_FORMAT_RGBA64: - return MTLPixelFormatRGBA16Float; - case IRON_IMAGE_FORMAT_R32: - return MTLPixelFormatR32Float; - case IRON_IMAGE_FORMAT_R16: - return MTLPixelFormatR16Float; - case IRON_IMAGE_FORMAT_R8: - return MTLPixelFormatR8Unorm; - case IRON_IMAGE_FORMAT_RGBA32: - default: - return MTLPixelFormatBGRA8Unorm; - } -} - -static void render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits, int framebuffer_index) { - memset(target, 0, sizeof(iron_gpu_texture_t)); - - target->width = width; - target->height = height; - target->data = NULL; - target->_uploaded = true; - target->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; - target->framebuffer_index = framebuffer_index; - - id device = getMetalDevice(); - - MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new]; - descriptor.textureType = MTLTextureType2D; - descriptor.width = width; - descriptor.height = height; - descriptor.depth = 1; - descriptor.pixelFormat = convert_format(format); - descriptor.arrayLength = 1; - descriptor.mipmapLevelCount = 1; - descriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; - descriptor.resourceOptions = MTLResourceStorageModePrivate; - - target->impl._tex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor]; - - if (depthBufferBits > 0) { - MTLTextureDescriptor *depthDescriptor = [MTLTextureDescriptor new]; - depthDescriptor.textureType = MTLTextureType2D; - depthDescriptor.width = width; - depthDescriptor.height = height; - depthDescriptor.depth = 1; - depthDescriptor.pixelFormat = MTLPixelFormatDepth32Float_Stencil8; - depthDescriptor.arrayLength = 1; - depthDescriptor.mipmapLevelCount = 1; - depthDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; - depthDescriptor.resourceOptions = MTLResourceStorageModePrivate; - - target->impl._depthTex = (__bridge_retained void *)[device newTextureWithDescriptor:depthDescriptor]; - } - - target->impl._texReadback = NULL; -} - -void iron_gpu_render_target_init(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits) { - render_target_init(target, width, height, format, depthBufferBits, -1); - target->width = target->width = width; - target->height = target->height = height; - target->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; - target->_uploaded = true; -} - -void iron_gpu_render_target_init_framebuffer(iron_gpu_texture_t *target, int width, int height, iron_image_format_t format, int depthBufferBits) { - render_target_init(target, width, height, format, depthBufferBits, framebuffer_count); - framebuffer_count += 1; -} - -void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_texture_t *source) { - target->impl._depthTex = source->impl._depthTex; -} - -static void vertex_buffer_unset(iron_gpu_buffer_t *buffer) { - if (currentVertexBuffer == buffer) - currentVertexBuffer = NULL; -} - -void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpuMemory) { - memset(&buffer->impl, 0, sizeof(buffer->impl)); - buffer->impl.myCount = count; - buffer->impl.gpuMemory = gpuMemory; - for (int i = 0; i < structure->size; ++i) { - iron_gpu_vertex_element_t element = structure->elements[i]; - buffer->impl.myStride += iron_gpu_vertex_data_size(element.data); - } - - id device = getMetalDevice(); - MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; - options |= MTLResourceStorageModeShared; - - id buf = [device newBufferWithLength:count * buffer->impl.myStride options:options]; - buffer->impl.mtlBuffer = (__bridge_retained void *)buf; - - buffer->impl.lastStart = 0; - buffer->impl.lastCount = 0; -} - -void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buf) { - id buffer = (__bridge_transfer id)buf->impl.mtlBuffer; - buffer = nil; - buf->impl.mtlBuffer = NULL; - vertex_buffer_unset(buf); -} - -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buf) { - buf->impl.lastStart = 0; - buf->impl.lastCount = iron_gpu_vertex_buffer_count(buf); - id buffer = (__bridge id)buf->impl.mtlBuffer; - float *floats = (float *)[buffer contents]; - return floats; -} - -float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buf, int start, int count) { - buf->impl.lastStart = start; - buf->impl.lastCount = count; - id buffer = (__bridge id)buf->impl.mtlBuffer; - float *floats = (float *)[buffer contents]; - return &floats[start * buf->impl.myStride / sizeof(float)]; -} - -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buf) { -} - -void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buf, int count) { -} - -int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buf) { - currentVertexBuffer = buf; - - id encoder = getMetalEncoder(); - id buffer = (__bridge id)buf->impl.mtlBuffer; - [encoder setVertexBuffer:buffer offset:0 atIndex:0]; - - return 0; -} - -int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { - return buffer->impl.myCount; -} - -int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer) { - return buffer->impl.myStride; -} - -void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { - buffer->impl.mySize = size; - buffer->data = NULL; - buffer->impl._buffer = (__bridge_retained void *)[getMetalDevice() newBufferWithLength:size options:MTLResourceOptionCPUCacheModeDefault]; -} - -void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { - id buf = (__bridge_transfer id)buffer->impl._buffer; - buf = nil; - buffer->impl._buffer = NULL; -} - -void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); -} - -void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { - buffer->impl.lastStart = start; - buffer->impl.lastCount = count; - id buf = (__bridge id)buffer->impl._buffer; - uint8_t *data = (uint8_t *)[buf contents]; - buffer->data = &data[start]; -} - -void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { - buffer->data = NULL; -} - -int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer) { - return buffer->impl.mySize; -} - -void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int indexCount, bool gpuMemory) { - buffer->impl.count = indexCount; - buffer->impl.gpu_memory = gpuMemory; - buffer->impl.last_start = 0; - buffer->impl.last_count = indexCount; - - id device = getMetalDevice(); - MTLResourceOptions options = MTLResourceCPUCacheModeWriteCombined; - options |= MTLResourceStorageModeShared; - - buffer->impl.metal_buffer = (__bridge_retained void *)[device - newBufferWithLength:sizeof(uint32_t) * indexCount - options:options]; -} - -void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer) { - id buf = (__bridge_transfer id)buffer->impl.metal_buffer; - buf = nil; - buffer->impl.metal_buffer = NULL; -} - -static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { - return 4; -} - -void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); -} - -void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { - buffer->impl.last_start = start; - buffer->impl.last_count = count; - - id metal_buffer = (__bridge id)buffer->impl.metal_buffer; - uint8_t *data = (uint8_t *)[metal_buffer contents]; - return &data[start * iron_gpu_internal_index_buffer_stride(buffer)]; -} - -void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_index_buffer_unlock(buffer, buffer->impl.last_count); -} - -void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { -} - -int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { - return buffer->impl.count; -} diff --git a/base/sources/backends/vulkan_gpu.c b/base/sources/backends/vulkan_gpu.c index d25634e2..b26ec3c1 100644 --- a/base/sources/backends/vulkan_gpu.c +++ b/base/sources/backends/vulkan_gpu.c @@ -82,7 +82,7 @@ struct vk_context vk_ctx = {0}; void iron_vulkan_get_instance_extensions(const char **extensions, int *index); VkBool32 iron_vulkan_get_physical_device_presentation_support(VkPhysicalDevice physical_device, uint32_t queue_family_index); VkResult iron_vulkan_create_surface(VkInstance instance, VkSurfaceKHR *surface); -void iron_gpu_internal_resize(int, int); +void gpu_internal_resize(int, int); VkBool32 vkDebugUtilsMessengerCallbackEXT( VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, @@ -248,14 +248,14 @@ void create_descriptor_layout(void) { vkCreateDescriptorPool(vk_ctx.device, &pool_info, NULL, &descriptor_pool); } -void iron_internal_resize(int width, int height) { +void iron_gpu_internal_resize(int width, int height) { struct vk_window *window = &vk_ctx.windows[0]; if (window->width != width || window->height != height) { window->resized = true; window->width = width; window->height = height; } - iron_gpu_internal_resize(width, height); + gpu_internal_resize(width, height); } VkSwapchainKHR cleanup_swapchain() { @@ -1044,7 +1044,13 @@ void iron_gpu_internal_init() { vkCreateSemaphore(vk_ctx.device, &sem_info, NULL, &relay_semaphore); } -void iron_gpu_internal_destroy() {} +void iron_gpu_internal_destroy() { + struct vk_window *window = &vk_ctx.windows[0]; + VkSwapchainKHR swapchain = cleanup_swapchain(); + vkDestroyRenderPass(vk_ctx.device, window->rendertarget_render_pass, NULL); + vk.fpDestroySwapchainKHR(vk_ctx.device, swapchain, NULL); + vk.fpDestroySurfaceKHR(vk_ctx.instance, window->surface, NULL); +} void iron_vulkan_init_window() { // this function is used in the android backend @@ -1103,18 +1109,6 @@ void iron_gpu_internal_init_window(int depthBufferBits, bool vsync) { iron_gpu_begin(NULL); } -void iron_gpu_internal_destroy_window() { - struct vk_window *window = &vk_ctx.windows[0]; - VkSwapchainKHR swapchain = cleanup_swapchain(); - vkDestroyRenderPass(vk_ctx.device, window->rendertarget_render_pass, NULL); - vk.fpDestroySwapchainKHR(vk_ctx.device, swapchain, NULL); - vk.fpDestroySurfaceKHR(vk_ctx.instance, window->surface, NULL); -} - -bool iron_gpu_swap_buffers() { - return true; -} - void iron_gpu_begin(iron_gpu_texture_t *renderTarget) { struct vk_window *window = &vk_ctx.windows[0]; @@ -1297,7 +1291,7 @@ void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list) { } void iron_gpu_command_list_begin(iron_gpu_command_list_t *list) { - iron_gpu_command_list_wait_for_execution_to_finish(list); + iron_gpu_command_list_wait(list); vkResetCommandBuffer(list->impl._buffer, 0); VkCommandBufferBeginInfo cmd_buf_info = { @@ -1393,49 +1387,48 @@ void iron_gpu_command_list_end(iron_gpu_command_list_t *list) { vkCmdPipelineBarrier(list->impl._buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, pmemory_barrier); vkEndCommandBuffer(list->impl._buffer); -} -void iron_gpu_command_list_clear(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget, unsigned flags, unsigned color, float depth) { - VkClearRect clearRect = { - .rect.offset.x = 0, - .rect.offset.y = 0, - .rect.extent.width = renderTarget->width, - .rect.extent.height = renderTarget->height, - .baseArrayLayer = 0, - .layerCount = 1, + // Make sure the previous execution is done, so we can reuse the fence + // Not optimal of course + iron_gpu_command_list_wait(list); + vkResetFences(vk_ctx.device, 1, &list->impl.fence); + + VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + VkSubmitInfo submit_info = { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .pNext = NULL, }; - int count = 0; - VkClearAttachment attachments[2]; - if (flags & IRON_GPU_CLEAR_COLOR) { - VkClearColorValue clearColor = {0}; - clearColor.float32[0] = ((color & 0x00ff0000) >> 16) / 255.0f; - clearColor.float32[1] = ((color & 0x0000ff00) >> 8) / 255.0f; - clearColor.float32[2] = (color & 0x000000ff) / 255.0f; - clearColor.float32[3] = ((color & 0xff000000) >> 24) / 255.0f; - attachments[count].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - attachments[count].colorAttachment = 0; - attachments[count].clearValue.color = clearColor; - count++; + VkSemaphore semaphores[2] = { framebuffer_available, relay_semaphore }; + VkPipelineStageFlags dst_stage_flags[2] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT}; + if (wait_for_framebuffer) { + submit_info.pWaitSemaphores = semaphores; + submit_info.pWaitDstStageMask = dst_stage_flags; + submit_info.waitSemaphoreCount = wait_for_relay ? 2 : 1; + wait_for_framebuffer = false; } - if ((flags & IRON_GPU_CLEAR_DEPTH) && renderTarget->impl.depthBufferBits > 0) { - attachments[count].aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; - attachments[count].clearValue.depthStencil.depth = depth; - attachments[count].clearValue.depthStencil.stencil = 0; - count++; + else if (wait_for_relay) { + submit_info.waitSemaphoreCount = 1; + submit_info.pWaitSemaphores = &semaphores[1]; + submit_info.pWaitDstStageMask = &dst_stage_flags[1]; } - vkCmdClearAttachments(list->impl._buffer, count, attachments, 1, &clearRect); + + submit_info.commandBufferCount = 1; + submit_info.pCommandBuffers = &list->impl._buffer; + submit_info.signalSemaphoreCount = 1; + submit_info.pSignalSemaphores = &relay_semaphore; + wait_for_relay = true; + + vkQueueSubmit(vk_ctx.queue, 1, &submit_info, list->impl.fence); } void iron_gpu_command_list_render_target_to_framebuffer_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} void iron_gpu_command_list_framebuffer_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} -void iron_gpu_command_list_draw_indexed_vertices(iron_gpu_command_list_t *list) { - iron_gpu_command_list_draw_indexed_vertices_from_to(list, 0, list->impl._indexCount); -} - -void iron_gpu_command_list_draw_indexed_vertices_from_to(iron_gpu_command_list_t *list, int start, int count) { +void iron_gpu_command_list_draw(iron_gpu_command_list_t *list) { + int start = 0; + int count = list->impl._indexCount; vkCmdDrawIndexed(list->impl._buffer, count, 1, start, 0, 0); } @@ -1562,7 +1555,7 @@ void iron_internal_restore_render_target(iron_gpu_command_list_t *list, struct i } } -void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count) { +void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count, unsigned flags, unsigned color, float depth) { for (int i = 0; i < count; ++i) { current_render_targets[i] = targets[i]; } @@ -1745,6 +1738,36 @@ void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, str current_vulkan_pipeline = current_pipeline->impl.rendertarget_pipeline; vkCmdBindPipeline(list->impl._buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.rendertarget_pipeline); } + + VkClearRect clearRect = { + .rect.offset.x = 0, + .rect.offset.y = 0, + .rect.extent.width = renderTarget->width, + .rect.extent.height = renderTarget->height, + .baseArrayLayer = 0, + .layerCount = 1, + }; + + int count = 0; + VkClearAttachment attachments[2]; + if (flags & IRON_GPU_CLEAR_COLOR) { + VkClearColorValue clearColor = {0}; + clearColor.float32[0] = ((color & 0x00ff0000) >> 16) / 255.0f; + clearColor.float32[1] = ((color & 0x0000ff00) >> 8) / 255.0f; + clearColor.float32[2] = (color & 0x000000ff) / 255.0f; + clearColor.float32[3] = ((color & 0xff000000) >> 24) / 255.0f; + attachments[count].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + attachments[count].colorAttachment = 0; + attachments[count].clearValue.color = clearColor; + count++; + } + if ((flags & IRON_GPU_CLEAR_DEPTH) && renderTarget->impl.depthBufferBits > 0) { + attachments[count].aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; + attachments[count].clearValue.depthStencil.depth = depth; + attachments[count].clearValue.depthStencil.stencil = 0; + count++; + } + vkCmdClearAttachments(list->impl._buffer, count, attachments, 1, &clearRect); } void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer) {} @@ -1814,8 +1837,7 @@ void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *lis in_render_pass = true; iron_gpu_command_list_end(list); - iron_gpu_command_list_execute(list); - iron_gpu_command_list_wait_for_execution_to_finish(list); + iron_gpu_command_list_wait(list); iron_gpu_command_list_begin(list); // Read buffer @@ -2014,42 +2036,7 @@ void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, st vkCmdBindDescriptorSets(list->impl._buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, current_pipeline->impl.pipeline_layout, 0, 1, &descriptor_set, 2, offsets); } -void iron_gpu_command_list_execute(iron_gpu_command_list_t *list) { - // Make sure the previous execution is done, so we can reuse the fence - // Not optimal of course - iron_gpu_command_list_wait_for_execution_to_finish(list); - vkResetFences(vk_ctx.device, 1, &list->impl.fence); - - VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; - VkSubmitInfo submit_info = { - .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, - .pNext = NULL, - }; - - VkSemaphore semaphores[2] = { framebuffer_available, relay_semaphore }; - VkPipelineStageFlags dst_stage_flags[2] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT}; - if (wait_for_framebuffer) { - submit_info.pWaitSemaphores = semaphores; - submit_info.pWaitDstStageMask = dst_stage_flags; - submit_info.waitSemaphoreCount = wait_for_relay ? 2 : 1; - wait_for_framebuffer = false; - } - else if (wait_for_relay) { - submit_info.waitSemaphoreCount = 1; - submit_info.pWaitSemaphores = &semaphores[1]; - submit_info.pWaitDstStageMask = &dst_stage_flags[1]; - } - - submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = &list->impl._buffer; - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = &relay_semaphore; - wait_for_relay = true; - - vkQueueSubmit(vk_ctx.queue, 1, &submit_info, list->impl.fence); -} - -void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list) { +void iron_gpu_command_list_wait(iron_gpu_command_list_t *list) { vkWaitForFences(vk_ctx.device, 1, &list->impl.fence, VK_TRUE, UINT64_MAX); } @@ -2277,8 +2264,6 @@ static VkFormat convert_image_format(iron_image_format_t format) { return VK_FORMAT_R16_SFLOAT; case IRON_IMAGE_FORMAT_R32: return VK_FORMAT_R32_SFLOAT; - case IRON_IMAGE_FORMAT_BGRA32: - return VK_FORMAT_B8G8R8A8_UNORM; case IRON_IMAGE_FORMAT_RGBA32: return VK_FORMAT_R8G8B8A8_UNORM; default: @@ -2287,7 +2272,7 @@ static VkFormat convert_image_format(iron_image_format_t format) { } void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipeline) { - iron_gpu_internal_pipeline_init(pipeline); + gpu_internal_pipeline_init(pipeline); } void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipeline) { @@ -3333,20 +3318,14 @@ void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { vkDestroyBuffer(vk_ctx.device, buffer->impl.buf, NULL); } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_vertex_buffer_lock(buffer, 0, buffer->impl.myCount); -} - -float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0; + int count = buffer->impl.myCount; vkMapMemory(vk_ctx.device, buffer->impl.mem, start * buffer->impl.myStride, count * buffer->impl.myStride, 0, (void **)&buffer->impl.data); return buffer->impl.data; } -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { - vkUnmapMemory(vk_ctx.device, buffer->impl.mem); -} - -void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer) { vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } @@ -3409,10 +3388,6 @@ void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) { vkDestroyBuffer(vk_ctx.device, buffer->impl.buf, NULL); } -void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); -} - void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { vkMapMemory(vk_ctx.device, buffer->impl.mem, start, count, 0, (void **)&buffer->data); } @@ -3481,24 +3456,18 @@ static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); -} - -void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0; + int count = iron_gpu_index_buffer_count(buffer); uint8_t *data; vkMapMemory(vk_ctx.device, buffer->impl.mem, 0, buffer->impl.mem_alloc.allocationSize, 0, (void **)&data); return &data[start * iron_gpu_internal_index_buffer_stride(buffer)]; } -void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) { vkUnmapMemory(vk_ctx.device, buffer->impl.mem); } -void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { - iron_gpu_index_buffer_unlock_all(buffer); -} - int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.myCount; } diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index 6117982d..d6953b86 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -18,8 +18,6 @@ WGPUDevice device; WGPUQueue queue; WGPUSwapChain swapChain; -void iron_gpu_internal_destroy_window() {} - void iron_gpu_internal_destroy() {} void iron_gpu_internal_init() {} @@ -55,10 +53,6 @@ void iron_gpu_begin(iron_gpu_texture_t *renderTarget) {} void iron_gpu_end() {} -bool iron_gpu_swap_buffers() { - return true; -} - bool iron_gpu_raytrace_supported() { return false; } @@ -80,7 +74,7 @@ void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer) { } -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer) { WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); bDesc.size = buffer->impl.count * buffer->impl.stride * sizeof(float); @@ -90,18 +84,10 @@ float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { return wgpuBufferGetMappedRange(buffer->impl.buffer, 0, bDesc.size); } -float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { - return NULL; -} - -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer) { +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t* buffer, int count) { - -} - int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer) { return buffer->impl.count; } @@ -118,10 +104,6 @@ void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size) { void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer) {} -void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_constant_buffer_lock(buffer, 0, iron_gpu_constant_buffer_size(buffer)); -} - void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) {} void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer) { @@ -146,11 +128,9 @@ static int iron_gpu_internal_index_buffer_stride(iron_gpu_buffer_t *buffer) { return 4; } -void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_index_buffer_lock(buffer, 0, iron_gpu_index_buffer_count(buffer)); -} - -void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count) { +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer) { + int start = 0; + int count = iron_gpu_index_buffer_count(buffer); WGPUBufferDescriptor bDesc; memset(&bDesc, 0, sizeof(bDesc)); bDesc.size = count * iron_gpu_internal_index_buffer_stride(buffer); @@ -160,14 +140,10 @@ void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count return wgpuBufferGetMappedRange(buffer->impl.buffer, start * iron_gpu_internal_index_buffer_stride(buffer), bDesc.size); } -void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) { wgpuBufferUnmap(buffer->impl.buffer); } -void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count) { - iron_gpu_index_buffer_unlock_all(buffer); -} - void iron_gpu_internal_index_buffer_set(iron_gpu_buffer_t *buffer) { } @@ -227,12 +203,10 @@ void iron_gpu_render_target_init_framebuffer(iron_gpu_texture_t *target, int wid void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *renderTarget, iron_gpu_texture_t *source) {} -void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *renderTarget, uint8_t *data) {} - extern WGPUDevice device; void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipe) { - iron_gpu_internal_pipeline_init(pipe); + gpu_internal_pipeline_init(pipe); } iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pipeline_t *pipe, const char* name) { @@ -392,23 +366,15 @@ void iron_gpu_command_list_end(iron_gpu_command_list_t *list) { wgpuQueueSubmit(queue, 1, &commands); } -void iron_gpu_command_list_clear(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget, unsigned flags, unsigned color, float depth) { - -} - void iron_gpu_command_list_render_target_to_framebuffer_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} void iron_gpu_command_list_framebuffer_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} void iron_gpu_command_list_texture_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} void iron_gpu_command_list_render_target_to_texture_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget) {} -void iron_gpu_command_list_draw_indexed_vertices(iron_gpu_command_list_t *list) { +void iron_gpu_command_list_draw(iron_gpu_command_list_t *list) { wgpuRenderPassEncoderDrawIndexed(list->impl.pass, list->impl.indexCount, 1, 0, 0, 0); } -void iron_gpu_command_list_draw_indexed_vertices_from_to(iron_gpu_command_list_t *list, int start, int count) { - -} - void iron_gpu_command_list_viewport(iron_gpu_command_list_t *list, int x, int y, int width, int height) { } @@ -436,7 +402,7 @@ void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, struc wgpuRenderPassEncoderSetIndexBuffer(list->impl.pass, buffer->impl.buffer, WGPUIndexFormat_Uint32, 0, size); } -void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count) { +void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count, unsigned flags, unsigned color, float depth) { } @@ -445,11 +411,7 @@ void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, s void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture) {} void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *list, iron_gpu_texture_t *render_target, uint8_t *data) {} -void iron_gpu_command_list_execute(iron_gpu_command_list_t *list) { - -} - -void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list) { +void iron_gpu_command_list_wait(iron_gpu_command_list_t *list) { } diff --git a/base/sources/backends/windows_system.c b/base/sources/backends/windows_system.c index c39b1413..55097ef7 100644 --- a/base/sources/backends/windows_system.c +++ b/base/sources/backends/windows_system.c @@ -408,7 +408,7 @@ void iron_mouse_get_position(int *x, int *y) { __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001; __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; -void iron_internal_resize(int width, int height); +void iron_gpu_internal_resize(int width, int height); typedef BOOL(WINAPI *GetPointerInfoType)(UINT32 pointerId, POINTER_INFO *pointerInfo); static GetPointerInfoType MyGetPointerInfo = NULL; @@ -653,7 +653,7 @@ LRESULT WINAPI IronWindowsMessageProcedure(HWND hWnd, UINT msg, WPARAM wParam, L case WM_SIZE: { int width = LOWORD(lParam); int height = HIWORD(lParam); - iron_internal_resize(width, height); + iron_gpu_internal_resize(width, height); iron_internal_call_resize_callback(width, height); break; } @@ -2015,7 +2015,6 @@ iron_window_mode_t iron_window_get_mode() { void iron_window_destroy() { WindowData *win = &windows[0]; if (win->handle != NULL) { - iron_gpu_internal_destroy_window(); DestroyWindow(win->handle); win->handle = NULL; } diff --git a/base/sources/iron.h b/base/sources/iron.h index f9d593f7..b41c6ed5 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -374,7 +374,7 @@ const char *iphonegetresourcepath(); char mobile_title[1024]; #endif -extern iron_gpu_command_list_t commandList; +extern iron_gpu_command_list_t gpu_command_list; static iron_gpu_buffer_t constant_buffer; static iron_gpu_texture_t *render_target; static iron_gpu_raytrace_pipeline_t rt_pipeline; @@ -422,7 +422,6 @@ void _update(void *data) { gpu_begin(); iron_update(); gpu_end(); - iron_gpu_swap_buffers(); } char *_copy(void *data) { @@ -931,7 +930,7 @@ void gpu_delete_index_buffer(iron_gpu_buffer_t *buffer) { } u32_array_t *gpu_lock_index_buffer(iron_gpu_buffer_t *buffer) { - void *vertices = iron_gpu_index_buffer_lock_all(buffer); + void *vertices = iron_gpu_index_buffer_lock(buffer); u32_array_t *ar = (u32_array_t *)malloc(sizeof(u32_array_t)); ar->buffer = vertices; ar->length = iron_gpu_index_buffer_count(buffer); @@ -940,7 +939,7 @@ u32_array_t *gpu_lock_index_buffer(iron_gpu_buffer_t *buffer) { any gpu_create_vertex_buffer(i32 count, iron_gpu_vertex_structure_t *structure, i32 usage) { iron_gpu_buffer_t *buffer = (iron_gpu_buffer_t *)malloc(sizeof(iron_gpu_buffer_t)); - gpu_vertex_buffer_init(buffer, count, structure, (gpu_usage_t)usage); + iron_gpu_vertex_buffer_init(buffer, count, structure, (gpu_usage_t)usage == GPU_USAGE_STATIC); return buffer; } @@ -950,23 +949,21 @@ void gpu_delete_vertex_buffer(iron_gpu_buffer_t *buffer) { } buffer_t *gpu_lock_vertex_buffer(iron_gpu_buffer_t *buffer) { - float *vertices = gpu_vertex_buffer_lock_all(buffer); + float *vertices = iron_gpu_vertex_buffer_lock(buffer); buffer_t *b = (buffer_t *)malloc(sizeof(buffer_t)); b->buffer = vertices; b->length = buffer->myCount * iron_gpu_vertex_buffer_stride(buffer); return b; } -void gpu_draw_indexed_vertices() { - iron_gpu_draw_indexed_vertices(); -} - iron_gpu_shader_t *gpu_create_shader(buffer_t *data, i32 shader_type) { iron_gpu_shader_t *shader = (iron_gpu_shader_t *)malloc(sizeof(iron_gpu_shader_t)); iron_gpu_shader_init(shader, data->buffer, data->length, (iron_gpu_shader_type_t)shader_type); return shader; } +#ifdef WITH_KONG + #include "../../sources/libs/kong/analyzer.h" #include "../../sources/libs/kong/compiler.h" #include "../../sources/libs/kong/disasm.h" @@ -1044,6 +1041,8 @@ void gpu_create_shaders_from_kong(char *kong, char **vs, char **fs) { #endif } +#endif + iron_gpu_shader_t *gpu_create_shader_from_source(string_t *source, iron_gpu_shader_type_t shader_type) { iron_gpu_shader_t *shader = NULL; char *temp_string_s = shader_type == IRON_GPU_SHADER_TYPE_VERTEX ? temp_string_vs : temp_string_fs; @@ -1371,7 +1370,6 @@ int _format_byte_size(iron_image_format_t format) { return 1; case IRON_IMAGE_FORMAT_R16: return 2; - case IRON_IMAGE_FORMAT_BGRA32: case IRON_IMAGE_FORMAT_RGBA32: case IRON_IMAGE_FORMAT_R32: default: @@ -1392,7 +1390,7 @@ buffer_t *gpu_get_texture_pixels(iron_gpu_texture_t *image) { } uint8_t *b = (uint8_t *)image->buffer->buffer; - iron_gpu_render_target_get_pixels(image, b); + gpu_render_target_get_pixels(image, b); // Release staging texture immediately to save memory #ifdef IRON_DIRECT3D12 @@ -1418,26 +1416,20 @@ void gpu_set_mipmaps(iron_gpu_texture_t *texture, any_array_t *mipmaps) { } } -void _gpu_begin(iron_gpu_texture_t *render_target, any_array_t *additional) { +void _gpu_begin(iron_gpu_texture_t *render_target, any_array_t *additional, unsigned flags, unsigned color, float depth) { if (render_target == NULL) { - gpu_restore_render_target(); + gpu_set_render_targets(NULL, 0, flags, color, depth); } else { - iron_gpu_texture_t *rt = (iron_gpu_texture_t *)render_target; int32_t length = 1; - iron_gpu_texture_t *render_targets[8] = { rt, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; + iron_gpu_texture_t *render_targets[8] = { render_target, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; if (additional != NULL) { length = additional->length + 1; - if (length > 8) { - length = 8; - } for (int32_t i = 1; i < length; ++i) { - iron_gpu_texture_t *img = additional->buffer[i - 1]; - iron_gpu_texture_t *art = (iron_gpu_texture_t *)img; - render_targets[i] = art; + render_targets[i] = additional->buffer[i - 1]; } } - gpu_set_render_targets(render_targets, length); + gpu_set_render_targets(render_targets, length, flags, color, depth); } } @@ -1446,7 +1438,6 @@ void _gpu_end() { void gpu_swap_buffers() { gpu_end(); - iron_gpu_swap_buffers(); gpu_begin(); } @@ -2142,7 +2133,7 @@ void iron_raytrace_init(buffer_t *shader) { } raytrace_created = true; iron_gpu_constant_buffer_init(&constant_buffer, constant_buffer_size * 4); - iron_gpu_raytrace_pipeline_init(&rt_pipeline, &commandList, shader->buffer, (int)shader->length, &constant_buffer); + iron_gpu_raytrace_pipeline_init(&rt_pipeline, &gpu_command_list, shader->buffer, (int)shader->length, &constant_buffer); } void iron_raytrace_as_init() { @@ -2158,7 +2149,7 @@ void iron_raytrace_as_add(struct iron_gpu_buffer *vb, iron_gpu_buffer_t *ib, iro } void iron_raytrace_as_build(struct iron_gpu_buffer *vb_full, iron_gpu_buffer_t *ib_full) { - iron_gpu_raytrace_acceleration_structure_build(&accel, &commandList, vb_full, ib_full); + iron_gpu_raytrace_acceleration_structure_build(&accel, &gpu_command_list, vb_full, ib_full); } void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *tex1, iron_gpu_texture_t *tex2, iron_gpu_texture_t *texenv, iron_gpu_texture_t *texsobol, iron_gpu_texture_t *texscramble, iron_gpu_texture_t *texrank) { @@ -2173,7 +2164,7 @@ void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *te // if (texpaint0_tex != NULL) { // iron_gpu_texture_t *texture = texpaint0_tex; // if (!texture->_uploaded) { - // iron_gpu_command_list_upload_texture(&commandList, texture); + // iron_gpu_command_list_upload_texture(&gpu_command_list, texture); // texture->_uploaded = true; // } // texpaint0 = (iron_gpu_texture_t *)malloc(sizeof(iron_gpu_texture_t)); @@ -2195,7 +2186,7 @@ void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *te // if (texpaint1_tex != NULL) { // iron_gpu_texture_t *texture = texpaint1_tex; // if (!texture->_uploaded) { - // iron_gpu_command_list_upload_texture(&commandList, texture); + // iron_gpu_command_list_upload_texture(&gpu_command_list, texture); // texture->_uploaded = true; // } // texpaint1 = (iron_gpu_texture_t *)malloc(sizeof(iron_gpu_texture_t)); @@ -2217,7 +2208,7 @@ void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *te // if (texpaint2_tex != NULL) { // iron_gpu_texture_t *texture = (iron_gpu_texture_t *)texpaint2_tex; // if (!texture->_uploaded) { - // iron_gpu_command_list_upload_texture(&commandList, texture); + // iron_gpu_command_list_upload_texture(&gpu_command_list, texture); // texture->_uploaded = true; // } // texpaint2 = (iron_gpu_texture_t *)malloc(sizeof(iron_gpu_texture_t)); @@ -2237,19 +2228,19 @@ void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *te texpaint2 = tex2; if (!texenv->_uploaded) { - iron_gpu_command_list_upload_texture(&commandList, texenv); + iron_gpu_command_list_upload_texture(&gpu_command_list, texenv); texenv->_uploaded = true; } if (!texsobol->_uploaded) { - iron_gpu_command_list_upload_texture(&commandList, texsobol); + iron_gpu_command_list_upload_texture(&gpu_command_list, texsobol); texsobol->_uploaded = true; } if (!texscramble->_uploaded) { - iron_gpu_command_list_upload_texture(&commandList, texscramble); + iron_gpu_command_list_upload_texture(&gpu_command_list, texscramble); texscramble->_uploaded = true; } if (!texrank->_uploaded) { - iron_gpu_command_list_upload_texture(&commandList, texrank); + iron_gpu_command_list_upload_texture(&gpu_command_list, texrank); texrank->_uploaded = true; } @@ -2268,7 +2259,7 @@ void iron_raytrace_set_textures(iron_gpu_texture_t *tex0, iron_gpu_texture_t *te void iron_raytrace_dispatch_rays(iron_gpu_texture_t *render_target, buffer_t *buffer) { float *cb = (float *)buffer->buffer; - iron_gpu_constant_buffer_lock_all(&constant_buffer); + iron_gpu_constant_buffer_lock(&constant_buffer, 0, iron_gpu_constant_buffer_size(&constant_buffer)); for (int i = 0; i < constant_buffer_size; ++i) { float *floats = (float *)(&constant_buffer.data[i * 4]); floats[0] = cb[i]; @@ -2278,7 +2269,7 @@ void iron_raytrace_dispatch_rays(iron_gpu_texture_t *render_target, buffer_t *bu iron_gpu_raytrace_set_acceleration_structure(&accel); iron_gpu_raytrace_set_pipeline(&rt_pipeline); iron_gpu_raytrace_set_target(render_target); - iron_gpu_raytrace_dispatch_rays(&commandList); + iron_gpu_raytrace_dispatch_rays(&gpu_command_list); } #endif diff --git a/base/sources/iron_draw.c b/base/sources/iron_draw.c index 547d98f6..22f8da89 100644 --- a/base/sources/iron_draw.c +++ b/base/sources/iron_draw.c @@ -110,12 +110,12 @@ static void draw_pipeline_init(iron_gpu_pipeline_t *pipe, iron_gpu_shader_t *ver void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, buffer_t *rect_frag, buffer_t *tris_vert, buffer_t *tris_frag, buffer_t *text_vert, buffer_t *text_frag) { draw_transform = iron_matrix3x3_identity(); - iron_gpu_vertex_structure_init(&draw_structure); - iron_gpu_vertex_structure_add(&draw_structure, "pos", IRON_GPU_VERTEX_DATA_F32_2X); + draw_structure.size = 0; + gpu_vertex_structure_add(&draw_structure, "pos", IRON_GPU_VERTEX_DATA_F32_2X); { - gpu_vertex_buffer_init(&rect_vertex_buffer, 4, &draw_structure, GPU_USAGE_STATIC); - float *verts = gpu_vertex_buffer_lock_all(&rect_vertex_buffer); + iron_gpu_vertex_buffer_init(&rect_vertex_buffer, 4, &draw_structure, true); + float *verts = iron_gpu_vertex_buffer_lock(&rect_vertex_buffer); verts[0] = 0.0; // Bottom-left verts[1] = 1.0; verts[2] = 0.0; // Top-left @@ -124,36 +124,36 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, verts[5] = 0.0; verts[6] = 1.0; // Bottom-right verts[7] = 1.0; - iron_gpu_vertex_buffer_unlock_all(&rect_vertex_buffer); + iron_gpu_vertex_buffer_unlock(&rect_vertex_buffer); iron_gpu_index_buffer_init(&rect_index_buffer, 3 * 2, true); - int *indices = iron_gpu_index_buffer_lock_all(&rect_index_buffer); + int *indices = iron_gpu_index_buffer_lock(&rect_index_buffer); indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; - gpu_index_buffer_unlock_all(&rect_index_buffer); + gpu_index_buffer_unlock(&rect_index_buffer); } { - gpu_vertex_buffer_init(&tris_vertex_buffer, 3, &draw_structure, GPU_USAGE_STATIC); - float *verts = gpu_vertex_buffer_lock_all(&tris_vertex_buffer); + iron_gpu_vertex_buffer_init(&tris_vertex_buffer, 3, &draw_structure, true); + float *verts = iron_gpu_vertex_buffer_lock(&tris_vertex_buffer); verts[0] = 0.0; // Bottom-left verts[1] = 1.0; verts[2] = 0.0; // Top-left verts[3] = 0.0; verts[4] = 1.0; // Top-right verts[5] = 0.0; - iron_gpu_vertex_buffer_unlock_all(&tris_vertex_buffer); + iron_gpu_vertex_buffer_unlock(&tris_vertex_buffer); iron_gpu_index_buffer_init(&tris_index_buffer, 3, true); - int *indices = iron_gpu_index_buffer_lock_all(&tris_index_buffer); + int *indices = iron_gpu_index_buffer_lock(&tris_index_buffer); indices[0] = 0; indices[1] = 1; indices[2] = 2; - gpu_index_buffer_unlock_all(&tris_index_buffer); + gpu_index_buffer_unlock(&tris_index_buffer); } // Image painter @@ -236,7 +236,7 @@ void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, } } -void draw_begin(iron_gpu_texture_t *render_target) { +void draw_begin(iron_gpu_texture_t *render_target, bool clear, unsigned color) { if (_draw_in_use && !_draw_thrown) { _draw_thrown = true; iron_log("End before you begin"); @@ -244,17 +244,11 @@ void draw_begin(iron_gpu_texture_t *render_target) { _draw_in_use = true; draw_set_color(0xffffffff); - - if (render_target != NULL) { - draw_set_render_target(render_target); - } - else { - draw_restore_render_target(); - } + draw_set_render_target(render_target, clear, color); } void draw_scaled_sub_image(iron_gpu_texture_t *tex, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh) { - iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &image_pipeline); + gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &image_pipeline); gpu_set_vertex_buffer(&rect_vertex_buffer); gpu_set_index_buffer(&rect_index_buffer); gpu_set_matrix4(&image_p_loc, draw_projection_matrix); @@ -262,7 +256,7 @@ void draw_scaled_sub_image(iron_gpu_texture_t *tex, float sx, float sy, float sw gpu_set_float4(&image_tex_loc, sx / tex->width, sy / tex->height, sw / tex->width, sh / tex->height); gpu_set_float4(&image_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); gpu_set_texture(&image_tex_unit, tex); - iron_gpu_draw_indexed_vertices(); + gpu_draw(); } void draw_scaled_image(iron_gpu_texture_t *tex, float dx, float dy, float dw, float dh) { @@ -278,7 +272,7 @@ void draw_image(iron_gpu_texture_t *tex, float x, float y) { } void draw_filled_triangle(float x0, float y0, float x1, float y1, float x2, float y2) { - iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &tris_pipeline); + gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &tris_pipeline); gpu_set_vertex_buffer(&tris_vertex_buffer); gpu_set_index_buffer(&tris_index_buffer); gpu_set_matrix4(&tris_p_loc, draw_projection_matrix); @@ -286,17 +280,17 @@ void draw_filled_triangle(float x0, float y0, float x1, float y1, float x2, floa gpu_set_float2(&tris_pos1_loc, x1 / vw(), y1 / vh()); gpu_set_float2(&tris_pos2_loc, x2 / vw(), y2 / vh()); gpu_set_float4(&tris_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); - iron_gpu_draw_indexed_vertices(); + gpu_draw(); } void draw_filled_rect(float x, float y, float width, float height) { - iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &rect_pipeline); + gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : &rect_pipeline); gpu_set_vertex_buffer(&rect_vertex_buffer); gpu_set_index_buffer(&rect_index_buffer); gpu_set_matrix4(&rect_p_loc, draw_projection_matrix); gpu_set_float4(&rect_pos_loc, x / vw(), y / vh(), width / vw(), height / vh()); gpu_set_float4(&rect_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); - iron_gpu_draw_indexed_vertices(); + gpu_draw(); } void draw_rect(float x, float y, float width, float height, float strength) { @@ -543,10 +537,10 @@ void draw_string(const char *text, float x, float y) { float ypos = y + img->baseline; draw_font_aligned_quad_t q; - iron_gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline); - gpu_set_vertex_buffer(&rect_vertex_buffer); - gpu_set_index_buffer(&rect_index_buffer); - gpu_set_texture(&text_tex_unit, tex); + // gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline); + // gpu_set_vertex_buffer(&rect_vertex_buffer); + // gpu_set_index_buffer(&rect_index_buffer); + // gpu_set_texture(&text_tex_unit, tex); for (int i = 0; text[i] != 0; ) { int l = 0; @@ -555,12 +549,18 @@ void draw_string(const char *text, float x, float y) { if (draw_font_get_baked_quad(draw_font, draw_font_size, &q, codepoint, xpos, ypos)) { xpos += q.xadvance; + + gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline); + gpu_set_vertex_buffer(&rect_vertex_buffer); + gpu_set_index_buffer(&rect_index_buffer); + gpu_set_texture(&text_tex_unit, tex); + gpu_set_float4(&text_pos_loc, q.x0 / vw(), q.y0 / vh(), (q.x1 - q.x0) / vw(), (q.y1 - q.y0) / vh()); gpu_set_float4(&text_tex_loc, q.s0, q.t0, q.s1 - q.s0, q.t1 - q.t0); gpu_set_matrix4(&text_p_loc, draw_projection_matrix); gpu_set_float4(&text_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0); - iron_gpu_draw_indexed_vertices(); + gpu_draw(); } } } @@ -757,15 +757,15 @@ void draw_set_bilinear_filter(bool bilinear) { draw_bilinear_filter = bilinear; } -void draw_restore_render_target(void) { - _draw_current = NULL; - gpu_restore_render_target(); -} - -void draw_set_render_target(iron_gpu_texture_t *target) { +void draw_set_render_target(iron_gpu_texture_t *target, bool clear, unsigned color) { _draw_current = target; - iron_gpu_texture_t *render_targets[1] = { target }; - gpu_set_render_targets(render_targets, 1); + if (target == NULL) { + gpu_set_render_targets(NULL, 0, clear ? IRON_GPU_CLEAR_COLOR : IRON_GPU_CLEAR_NONE, color, 0.0); + } + else { + iron_gpu_texture_t *render_targets[1] = { target }; + gpu_set_render_targets(render_targets, 1, clear ? IRON_GPU_CLEAR_COLOR : IRON_GPU_CLEAR_NONE, color, 0.0); + } } void draw_filled_circle(float cx, float cy, float radius, int segments) { diff --git a/base/sources/iron_draw.h b/base/sources/iron_draw.h index 4c83b922..92bf63cc 100644 --- a/base/sources/iron_draw.h +++ b/base/sources/iron_draw.h @@ -27,7 +27,7 @@ typedef struct draw_font { } draw_font_t; void draw_init(buffer_t *image_vert, buffer_t *image_frag, buffer_t *rect_vert, buffer_t *rect_frag, buffer_t *tris_vert, buffer_t *tris_frag, buffer_t *text_vert, buffer_t *text_frag); -void draw_begin(iron_gpu_texture_t *target); +void draw_begin(iron_gpu_texture_t *target, bool clear, unsigned color); void draw_scaled_sub_image(iron_gpu_texture_t *img, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh); void draw_scaled_image(iron_gpu_texture_t *tex, float dx, float dy, float dw, float dh); void draw_sub_image(iron_gpu_texture_t *tex, float sx, float sy, float sw, float sh, float x, float y); @@ -55,8 +55,7 @@ int draw_font_height(draw_font_t *font, int font_size); float draw_sub_string_width(draw_font_t *font, int font_size, const char *text, int start, int end); int draw_string_width(draw_font_t *font, int font_size, const char *text); void draw_set_bilinear_filter(bool bilinear); -void draw_restore_render_target(void); -void draw_set_render_target(iron_gpu_texture_t *target); +void draw_set_render_target(iron_gpu_texture_t *target, bool clear, unsigned color); void draw_filled_circle(float cx, float cy, float radius, int segments); void draw_circle(float cx, float cy, float radius, int segments, float strength); diff --git a/base/sources/iron_gpu.c b/base/sources/iron_gpu.c index d313482c..250735f4 100644 --- a/base/sources/iron_gpu.c +++ b/base/sources/iron_gpu.c @@ -7,131 +7,131 @@ #include "iron_file.h" #define CONSTANT_BUFFER_SIZE 256 -// #define CONSTANT_BUFFER_MULTIPLE 1024 -#define CONSTANT_BUFFER_MULTIPLE 10240 +#define CONSTANT_BUFFER_MULTIPLE 2048 #define FRAMEBUFFER_COUNT 2 -iron_gpu_command_list_t commandList; -static iron_gpu_buffer_t vertexConstantBuffer; -static int constantBufferIndex = 0; -static int window_currentBuffer; -static iron_gpu_texture_t window_framebuffers[FRAMEBUFFER_COUNT]; -static iron_gpu_texture_t *window_current_render_targets[8]; +iron_gpu_command_list_t gpu_command_list; +static iron_gpu_buffer_t constant_buffer; +int constant_buffer_index = 0; +static int framebuffer_index; +static iron_gpu_texture_t framebuffers[FRAMEBUFFER_COUNT]; +static iron_gpu_texture_t *render_targets[8]; static bool window_resized = false; -void iron_gpu_internal_resize(int width, int height) { +void gpu_internal_resize(int width, int height) { window_resized = true; } -void gpu_internal_init_window(int depthBufferBits, bool vsync) { - iron_gpu_internal_init_window(depthBufferBits, vsync); - iron_gpu_command_list_init(&commandList); - window_currentBuffer = -1; +void gpu_internal_init_window(int depth_buffer_bits, bool vsync) { + iron_gpu_internal_init_window(depth_buffer_bits, vsync); + iron_gpu_command_list_init(&gpu_command_list); + framebuffer_index = -1; for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - iron_gpu_render_target_init_framebuffer(&window_framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, depthBufferBits); + iron_gpu_render_target_init_framebuffer(&framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, depth_buffer_bits); } - iron_gpu_constant_buffer_init(&vertexConstantBuffer, CONSTANT_BUFFER_SIZE * CONSTANT_BUFFER_MULTIPLE); + iron_gpu_constant_buffer_init(&constant_buffer, CONSTANT_BUFFER_SIZE * CONSTANT_BUFFER_MULTIPLE); } -void iron_gpu_draw_indexed_vertices(void) { - iron_gpu_constant_buffer_unlock(&vertexConstantBuffer); - iron_gpu_command_list_set_constant_buffer(&commandList, &vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); - iron_gpu_command_list_draw_indexed_vertices(&commandList); - ++constantBufferIndex; +void gpu_draw(void) { + iron_gpu_constant_buffer_unlock(&constant_buffer); + iron_gpu_command_list_set_constant_buffer(&gpu_command_list, &constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); + iron_gpu_command_list_draw(&gpu_command_list); + ++constant_buffer_index; - // Flush - if (constantBufferIndex == CONSTANT_BUFFER_MULTIPLE) { - iron_gpu_command_list_end(&commandList); - iron_gpu_command_list_execute(&commandList); - iron_gpu_command_list_wait_for_execution_to_finish(&commandList); - iron_gpu_command_list_begin(&commandList); - constantBufferIndex = 0; - } + // if (constant_buffer_index == CONSTANT_BUFFER_MULTIPLE) { + // iron_gpu_command_list_end(&gpu_command_list); + // iron_gpu_command_list_wait(&gpu_command_list); + // iron_gpu_command_list_begin(&gpu_command_list); + // constant_buffer_index = 0; + // } - iron_gpu_constant_buffer_lock(&vertexConstantBuffer, constantBufferIndex * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); -} - -void iron_gpu_clear(unsigned color, float depth, unsigned flags) { - if (window_current_render_targets[0] == NULL) { - iron_gpu_command_list_clear(&commandList, &window_framebuffers[window_currentBuffer], flags, color, depth); - } - else { - if (window_current_render_targets[0]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { - iron_gpu_command_list_texture_to_render_target_barrier(&commandList, window_current_render_targets[0]); - window_current_render_targets[0]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; - } - iron_gpu_command_list_clear(&commandList, window_current_render_targets[0], flags, color, depth); - } + iron_gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index * CONSTANT_BUFFER_SIZE, CONSTANT_BUFFER_SIZE); } void gpu_begin() { - constantBufferIndex = 0; - iron_gpu_constant_buffer_lock(&vertexConstantBuffer, constantBufferIndex, CONSTANT_BUFFER_SIZE); - window_currentBuffer = (window_currentBuffer + 1) % FRAMEBUFFER_COUNT; + constant_buffer_index = 0; + iron_gpu_constant_buffer_lock(&constant_buffer, constant_buffer_index, CONSTANT_BUFFER_SIZE); + framebuffer_index = (framebuffer_index + 1) % FRAMEBUFFER_COUNT; // if (window_resized) { // for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - // iron_gpu_texture_destroy(&window_framebuffers[i]); + // iron_gpu_texture_destroy(&framebuffers[i]); // } - // window_currentBuffer = 0; + // framebuffer_index = 0; // } - iron_gpu_begin(&window_framebuffers[window_currentBuffer]); + iron_gpu_begin(&framebuffers[framebuffer_index]); // if (window_resized) { // for (int i = 0; i < FRAMEBUFFER_COUNT; ++i) { - // iron_gpu_render_target_init_framebuffer(&window_framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, 0); + // iron_gpu_render_target_init_framebuffer(&framebuffers[i], iron_window_width(), iron_window_height(), IRON_IMAGE_FORMAT_RGBA32, 0); // } // window_resized = false; // } - window_current_render_targets[0] = NULL; - iron_gpu_command_list_begin(&commandList); - iron_gpu_command_list_framebuffer_to_render_target_barrier(&commandList, &window_framebuffers[window_currentBuffer]); - gpu_restore_render_target(); + render_targets[0] = NULL; + iron_gpu_command_list_begin(&gpu_command_list); + iron_gpu_command_list_framebuffer_to_render_target_barrier(&gpu_command_list, &framebuffers[framebuffer_index]); + gpu_set_render_targets(NULL, 0, IRON_GPU_CLEAR_NONE, 0, 0); } void gpu_end() { - iron_gpu_constant_buffer_unlock(&vertexConstantBuffer); - iron_gpu_command_list_render_target_to_framebuffer_barrier(&commandList, &window_framebuffers[window_currentBuffer]); - iron_gpu_command_list_end(&commandList); - iron_gpu_command_list_execute(&commandList); - iron_gpu_command_list_wait_for_execution_to_finish(&commandList); + iron_gpu_constant_buffer_unlock(&constant_buffer); + iron_gpu_command_list_render_target_to_framebuffer_barrier(&gpu_command_list, &framebuffers[framebuffer_index]); + iron_gpu_command_list_end(&gpu_command_list); + iron_gpu_command_list_wait(&gpu_command_list); iron_gpu_end(); } +void gpu_set_render_targets(iron_gpu_texture_t **targets, int count, unsigned flags, unsigned color, float depth) { + iron_gpu_texture_t *_targets[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; + if (targets == NULL) { + _targets[0] = &framebuffers[framebuffer_index]; + targets = _targets; + count = 1; + } + for (int i = 0; i < count; ++i) { + render_targets[i] = targets[i]; + if (render_targets[i]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { + iron_gpu_command_list_texture_to_render_target_barrier(&gpu_command_list, render_targets[i]); + render_targets[i]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; + } + } + iron_gpu_command_list_set_render_targets(&gpu_command_list, render_targets, count, flags, color, depth); +} + void gpu_viewport(int x, int y, int width, int height) { - iron_gpu_command_list_viewport(&commandList, x, y, width, height); + iron_gpu_command_list_viewport(&gpu_command_list, x, y, width, height); } void gpu_scissor(int x, int y, int width, int height) { - iron_gpu_command_list_scissor(&commandList, x, y, width, height); + iron_gpu_command_list_scissor(&gpu_command_list, x, y, width, height); } void gpu_disable_scissor(void) { - iron_gpu_command_list_disable_scissor(&commandList); + iron_gpu_command_list_disable_scissor(&gpu_command_list); } void gpu_set_int(iron_gpu_constant_location_t *location, int value) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); ints[0] = value; } void gpu_set_int2(iron_gpu_constant_location_t *location, int value1, int value2) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); ints[0] = value1; ints[1] = value2; } void gpu_set_int3(iron_gpu_constant_location_t *location, int value1, int value2, int value3) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); ints[0] = value1; ints[1] = value2; ints[2] = value3; } void gpu_set_int4(iron_gpu_constant_location_t *location, int value1, int value2, int value3, int value4) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); ints[0] = value1; ints[1] = value2; ints[2] = value3; @@ -139,32 +139,32 @@ void gpu_set_int4(iron_gpu_constant_location_t *location, int value1, int value2 } void gpu_set_ints(iron_gpu_constant_location_t *location, int *values, int count) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); for (int i = 0; i < count; ++i) { ints[i] = values[i]; } } void gpu_set_float(iron_gpu_constant_location_t *location, float value) { - float *floats = (float *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]); floats[0] = value; } void gpu_set_float2(iron_gpu_constant_location_t *location, float value1, float value2) { - float *floats = (float *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]); floats[0] = value1; floats[1] = value2; } void gpu_set_float3(iron_gpu_constant_location_t *location, float value1, float value2, float value3) { - float *floats = (float *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]); floats[0] = value1; floats[1] = value2; floats[2] = value3; } void gpu_set_float4(iron_gpu_constant_location_t *location, float value1, float value2, float value3, float value4) { - float *floats = (float *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]); floats[0] = value1; floats[1] = value2; floats[2] = value3; @@ -172,14 +172,14 @@ void gpu_set_float4(iron_gpu_constant_location_t *location, float value1, float } void gpu_set_floats(iron_gpu_constant_location_t *location, f32_array_t *values) { - float *floats = (float *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + float *floats = (float *)(&constant_buffer.data[location->impl.vertexOffset]); for (int i = 0; i < values->length; ++i) { floats[i] = values->buffer[i]; } } void gpu_set_bool(iron_gpu_constant_location_t *location, bool value) { - int *ints = (int *)(&vertexConstantBuffer.data[location->impl.vertexOffset]); + int *ints = (int *)(&constant_buffer.data[location->impl.vertexOffset]); ints[0] = value ? 1 : 0; } @@ -205,10 +205,10 @@ void gpu_set_matrix3(iron_gpu_constant_location_t *location, iron_matrix3x3_t va if (iron_gpu_transpose_mat) { iron_matrix3x3_t m = value; iron_matrix3x3_transpose(&m); - iron_internal_set_matrix3(vertexConstantBuffer.data, location->impl.vertexOffset, &m); + iron_internal_set_matrix3(constant_buffer.data, location->impl.vertexOffset, &m); } else { - iron_internal_set_matrix3(vertexConstantBuffer.data, location->impl.vertexOffset, &value); + iron_internal_set_matrix3(constant_buffer.data, location->impl.vertexOffset, &value); } } @@ -216,90 +216,61 @@ void gpu_set_matrix4(iron_gpu_constant_location_t *location, iron_matrix4x4_t va if (iron_gpu_transpose_mat) { iron_matrix4x4_t m = value; iron_matrix4x4_transpose(&m); - iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &m); + iron_internal_set_matrix4(constant_buffer.data, location->impl.vertexOffset, &m); } else { - iron_internal_set_matrix4(vertexConstantBuffer.data, location->impl.vertexOffset, &value); + iron_internal_set_matrix4(constant_buffer.data, location->impl.vertexOffset, &value); } } -void gpu_restore_render_target(void) { - window_current_render_targets[0] = NULL; - iron_gpu_texture_t *render_target = &window_framebuffers[window_currentBuffer]; - iron_gpu_command_list_set_render_targets(&commandList, &render_target, 1); -} - -void gpu_set_render_targets(iron_gpu_texture_t **targets, int count) { - for (int i = 0; i < count; ++i) { - window_current_render_targets[i] = targets[i]; - if (window_current_render_targets[i]->state != IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET) { - iron_gpu_command_list_texture_to_render_target_barrier(&commandList, window_current_render_targets[i]); - window_current_render_targets[i]->state = IRON_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET; - } - } - iron_gpu_command_list_set_render_targets(&commandList, window_current_render_targets, count); -} - void gpu_set_vertex_buffer(iron_gpu_buffer_t *buffer) { - iron_gpu_command_list_set_vertex_buffer(&commandList, buffer); + iron_gpu_command_list_set_vertex_buffer(&gpu_command_list, buffer); } void gpu_set_index_buffer(iron_gpu_buffer_t *buffer) { - iron_gpu_command_list_set_index_buffer(&commandList, buffer); + iron_gpu_command_list_set_index_buffer(&gpu_command_list, buffer); } -void iron_gpu_set_pipeline(iron_gpu_pipeline_t *pipeline) { - iron_gpu_command_list_set_pipeline(&commandList, pipeline); +void gpu_set_pipeline(iron_gpu_pipeline_t *pipeline) { + iron_gpu_command_list_set_pipeline(&gpu_command_list, pipeline); } void gpu_set_texture(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { if (!render_target->_uploaded) { - iron_gpu_command_list_upload_texture(&commandList, render_target); + iron_gpu_command_list_upload_texture(&gpu_command_list, render_target); render_target->_uploaded = true; } if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) { - iron_gpu_command_list_render_target_to_texture_barrier(&commandList, render_target); + iron_gpu_command_list_render_target_to_texture_barrier(&gpu_command_list, render_target); render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; } - iron_gpu_command_list_set_texture(&commandList, *unit, render_target); + iron_gpu_command_list_set_texture(&gpu_command_list, *unit, render_target); } void gpu_set_texture_depth(iron_gpu_texture_unit_t *unit, iron_gpu_texture_t *render_target) { if (render_target->state != IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE) { - iron_gpu_command_list_render_target_to_texture_barrier(&commandList, render_target); + iron_gpu_command_list_render_target_to_texture_barrier(&gpu_command_list, render_target); render_target->state = IRON_INTERNAL_RENDER_TARGET_STATE_TEXTURE; } - iron_gpu_command_list_set_texture_from_render_target_depth(&commandList, *unit, render_target); + iron_gpu_command_list_set_texture_from_render_target_depth(&gpu_command_list, *unit, render_target); } -void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data) { - iron_gpu_command_list_get_render_target_pixels(&commandList, render_target, data); +void gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data) { + iron_gpu_command_list_get_render_target_pixels(&gpu_command_list, render_target, data); } -void gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer) { - iron_gpu_index_buffer_unlock_all(buffer); - iron_gpu_command_list_upload_index_buffer(&commandList, buffer); +void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer) { + iron_gpu_index_buffer_unlock(buffer); + iron_gpu_command_list_upload_index_buffer(&gpu_command_list, buffer); } -void iron_gpu_vertex_structure_init(iron_gpu_vertex_structure_t *structure) { - structure->size = 0; -} - -void iron_gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data) { +void gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data) { structure->elements[structure->size].name = name; structure->elements[structure->size].data = data; structure->size++; } -void gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage) { - iron_gpu_vertex_buffer_init(buffer, count, structure, usage == GPU_USAGE_STATIC); -} - -float *gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer) { - return iron_gpu_vertex_buffer_lock_all(buffer); -} - -void iron_gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipe) { +void gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipe) { pipe->input_layout = NULL; pipe->vertex_shader = NULL; pipe->fragment_shader = NULL; diff --git a/base/sources/iron_gpu.h b/base/sources/iron_gpu.h index 155ab0ad..21dca208 100644 --- a/base/sources/iron_gpu.h +++ b/base/sources/iron_gpu.h @@ -9,6 +9,7 @@ #include #include BACKEND_GPU_H +#define IRON_GPU_CLEAR_NONE 0 #define IRON_GPU_CLEAR_COLOR 1 #define IRON_GPU_CLEAR_DEPTH 2 #define IRON_GPU_MAX_VERTEX_ELEMENTS 16 @@ -30,8 +31,7 @@ typedef enum iron_image_format { IRON_IMAGE_FORMAT_RGBA128, IRON_IMAGE_FORMAT_R8, IRON_IMAGE_FORMAT_R16, - IRON_IMAGE_FORMAT_R32, - IRON_IMAGE_FORMAT_BGRA32 + IRON_IMAGE_FORMAT_R32 } iron_image_format_t; typedef enum { @@ -174,19 +174,16 @@ struct iron_gpu_buffer; int iron_gpu_max_bound_textures(void); void iron_gpu_begin(iron_gpu_texture_t *renderTarget); void iron_gpu_end(void); -bool iron_gpu_swap_buffers(void); void iron_gpu_internal_init(void); void iron_gpu_internal_init_window(int depth_buffer_bits, bool vsync); -void iron_gpu_internal_destroy_window(void); void iron_gpu_internal_destroy(void); void gpu_begin(); void gpu_end(); -void iron_gpu_clear(unsigned color, float depth, unsigned flags); void gpu_viewport(int x, int y, int width, int height); void gpu_scissor(int x, int y, int width, int height); void gpu_disable_scissor(void); -void iron_gpu_draw_indexed_vertices(void); -void iron_gpu_set_pipeline(struct iron_gpu_pipeline *pipeline); +void gpu_draw(void); +void gpu_set_pipeline(struct iron_gpu_pipeline *pipeline); void gpu_set_int(struct iron_gpu_constant_location *location, int value); void gpu_set_int2(struct iron_gpu_constant_location *location, int value1, int value2); void gpu_set_int3(struct iron_gpu_constant_location *location, int value1, int value2, int value3); @@ -200,16 +197,14 @@ void gpu_set_floats(struct iron_gpu_constant_location *location, f32_array_t *va void gpu_set_bool(struct iron_gpu_constant_location *location, bool value); void gpu_set_matrix3(struct iron_gpu_constant_location *location, iron_matrix3x3_t value); void gpu_set_matrix4(struct iron_gpu_constant_location *location, iron_matrix4x4_t value); -void gpu_restore_render_target(void); -void gpu_set_render_targets(struct iron_gpu_texture **targets, int count); +void gpu_set_render_targets(struct iron_gpu_texture **targets, int count, unsigned flags, unsigned color, float depth); void gpu_set_texture(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *texture); void gpu_set_index_buffer(iron_gpu_buffer_t *buffer); void gpu_internal_init_window(int depth_buffer_bits, bool vsync); void gpu_set_texture_depth(struct iron_gpu_texture_unit *unit, struct iron_gpu_texture *renderTarget); -void gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer); +void gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer); -void iron_gpu_vertex_structure_init(iron_gpu_vertex_structure_t *structure); -void iron_gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data); +void gpu_vertex_structure_add(iron_gpu_vertex_structure_t *structure, const char *name, iron_gpu_vertex_data_t data); void iron_gpu_texture_init(iron_gpu_texture_t *texture, int width, int height, iron_image_format_t format); void iron_gpu_texture_init_from_bytes(iron_gpu_texture_t *texture, void *data, int width, int height, iron_image_format_t format); void iron_gpu_texture_destroy(iron_gpu_texture_t *texture); @@ -221,45 +216,25 @@ void iron_gpu_render_target_init_framebuffer(iron_gpu_texture_t *target, int wid void iron_gpu_render_target_set_depth_from(iron_gpu_texture_t *target, iron_gpu_texture_t *source); void iron_gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, bool gpu_memory); void iron_gpu_vertex_buffer_destroy(iron_gpu_buffer_t *buffer); -float *iron_gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer); -float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); -void iron_gpu_vertex_buffer_unlock_all(iron_gpu_buffer_t *buffer); -void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer, int count); +float *iron_gpu_vertex_buffer_lock(iron_gpu_buffer_t *buffer); +void iron_gpu_vertex_buffer_unlock(iron_gpu_buffer_t *buffer); int iron_gpu_vertex_buffer_count(iron_gpu_buffer_t *buffer); int iron_gpu_vertex_buffer_stride(iron_gpu_buffer_t *buffer); int iron_gpu_internal_vertex_buffer_set(iron_gpu_buffer_t *buffer); -void gpu_vertex_buffer_init(iron_gpu_buffer_t *buffer, int count, iron_gpu_vertex_structure_t *structure, gpu_usage_t usage); -float *gpu_vertex_buffer_lock_all(iron_gpu_buffer_t *buffer); void gpu_set_vertex_buffer(iron_gpu_buffer_t *buffer); void iron_gpu_constant_buffer_init(iron_gpu_buffer_t *buffer, int size); void iron_gpu_constant_buffer_destroy(iron_gpu_buffer_t *buffer); -void iron_gpu_constant_buffer_lock_all(iron_gpu_buffer_t *buffer); void iron_gpu_constant_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); void iron_gpu_constant_buffer_unlock(iron_gpu_buffer_t *buffer); int iron_gpu_constant_buffer_size(iron_gpu_buffer_t *buffer); -void iron_gpu_constant_buffer_set_bool(iron_gpu_buffer_t *buffer, int offset, bool value); -void iron_gpu_constant_buffer_set_int(iron_gpu_buffer_t *buffer, int offset, int value); -void iron_gpu_constant_buffer_set_int2(iron_gpu_buffer_t *buffer, int offset, int value1, int value2); -void iron_gpu_constant_buffer_set_int3(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3); -void iron_gpu_constant_buffer_set_int4(iron_gpu_buffer_t *buffer, int offset, int value1, int value2, int value3, int value4); -void iron_gpu_constant_buffer_set_ints(iron_gpu_buffer_t *buffer, int offset, int *values, int count); -void iron_gpu_constant_buffer_set_float(iron_gpu_buffer_t *buffer, int offset, float value); -void iron_gpu_constant_buffer_set_float2(iron_gpu_buffer_t *buffer, int offset, float value1, float value2); -void iron_gpu_constant_buffer_set_float3(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3); -void iron_gpu_constant_buffer_set_float4(iron_gpu_buffer_t *buffer, int offset, float value1, float value2, float value3, float value4); -void iron_gpu_constant_buffer_set_floats(iron_gpu_buffer_t *buffer, int offset, float *values, int count); -void iron_gpu_constant_buffer_set_matrix3(iron_gpu_buffer_t *buffer, int offset, iron_matrix3x3_t *value); -void iron_gpu_constant_buffer_set_matrix4(iron_gpu_buffer_t *buffer, int offset, iron_matrix4x4_t *value); void iron_gpu_index_buffer_init(iron_gpu_buffer_t *buffer, int count, bool gpu_memory); void iron_gpu_index_buffer_destroy(iron_gpu_buffer_t *buffer); -void *iron_gpu_index_buffer_lock_all(iron_gpu_buffer_t *buffer); -void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer, int start, int count); -void iron_gpu_index_buffer_unlock_all(iron_gpu_buffer_t *buffer); -void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer, int count); +void *iron_gpu_index_buffer_lock(iron_gpu_buffer_t *buffer); +void iron_gpu_index_buffer_unlock(iron_gpu_buffer_t *buffer); int iron_gpu_index_buffer_count(iron_gpu_buffer_t *buffer); void iron_gpu_pipeline_init(iron_gpu_pipeline_t *pipeline); -void iron_gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipeline); +void gpu_internal_pipeline_init(iron_gpu_pipeline_t *pipeline); void iron_gpu_pipeline_destroy(iron_gpu_pipeline_t *pipeline); void iron_gpu_pipeline_compile(iron_gpu_pipeline_t *pipeline); iron_gpu_constant_location_t iron_gpu_pipeline_get_constant_location(iron_gpu_pipeline_t *pipeline, const char *name); @@ -270,30 +245,27 @@ void iron_gpu_command_list_init(iron_gpu_command_list_t *list); void iron_gpu_command_list_destroy(iron_gpu_command_list_t *list); void iron_gpu_command_list_begin(iron_gpu_command_list_t *list); void iron_gpu_command_list_end(iron_gpu_command_list_t *list); -void iron_gpu_command_list_clear(iron_gpu_command_list_t *list, struct iron_gpu_texture *render_target, unsigned flags, unsigned color, float depth); void iron_gpu_command_list_render_target_to_framebuffer_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget); void iron_gpu_command_list_framebuffer_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget); void iron_gpu_command_list_texture_to_render_target_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget); void iron_gpu_command_list_render_target_to_texture_barrier(iron_gpu_command_list_t *list, struct iron_gpu_texture *renderTarget); -void iron_gpu_command_list_draw_indexed_vertices(iron_gpu_command_list_t *list); -void iron_gpu_command_list_draw_indexed_vertices_from_to(iron_gpu_command_list_t *list, int start, int count); +void iron_gpu_command_list_draw(iron_gpu_command_list_t *list); void iron_gpu_command_list_viewport(iron_gpu_command_list_t *list, int x, int y, int width, int height); void iron_gpu_command_list_scissor(iron_gpu_command_list_t *list, int x, int y, int width, int height); void iron_gpu_command_list_disable_scissor(iron_gpu_command_list_t *list); void iron_gpu_command_list_set_pipeline(iron_gpu_command_list_t *list, struct iron_gpu_pipeline *pipeline); void iron_gpu_command_list_set_vertex_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer); void iron_gpu_command_list_set_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer); -void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count); +void iron_gpu_command_list_set_render_targets(iron_gpu_command_list_t *list, struct iron_gpu_texture **targets, int count, unsigned flags, unsigned color, float depth); void iron_gpu_command_list_upload_index_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer); void iron_gpu_command_list_upload_vertex_buffer(iron_gpu_command_list_t *list, struct iron_gpu_buffer *buffer); void iron_gpu_command_list_upload_texture(iron_gpu_command_list_t *list, struct iron_gpu_texture *texture); void iron_gpu_command_list_set_constant_buffer(iron_gpu_command_list_t *list, iron_gpu_buffer_t *buffer, int offset, size_t size); -void iron_gpu_command_list_execute(iron_gpu_command_list_t *list); -void iron_gpu_command_list_wait_for_execution_to_finish(iron_gpu_command_list_t *list); +void iron_gpu_command_list_wait(iron_gpu_command_list_t *list); void iron_gpu_command_list_get_render_target_pixels(iron_gpu_command_list_t *list, struct iron_gpu_texture *render_target, uint8_t *data); void iron_gpu_command_list_set_texture(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *texture); void iron_gpu_command_list_set_texture_from_render_target_depth(iron_gpu_command_list_t *list, iron_gpu_texture_unit_t unit, iron_gpu_texture_t *target); -void iron_gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data); +void gpu_render_target_get_pixels(iron_gpu_texture_t *render_target, uint8_t *data); bool iron_gpu_raytrace_supported(void); void iron_gpu_raytrace_pipeline_init(iron_gpu_raytrace_pipeline_t *pipeline, struct iron_gpu_command_list *command_list, void *ray_shader, int ray_shader_size, struct iron_gpu_buffer *constant_buffer); diff --git a/base/sources/iron_ui.c b/base/sources/iron_ui.c index 15358a0d..6dd50b31 100644 --- a/base/sources/iron_ui.c +++ b/base/sources/iron_ui.c @@ -613,7 +613,7 @@ void ui_draw_tooltip_text(bool bind_global_g) { } } current->tooltip_x = fmin(current->tooltip_x, iron_window_width() - tooltip_w - 20); - if (bind_global_g) draw_restore_render_target(); + if (bind_global_g) draw_set_render_target(NULL, false, 0); float font_height = draw_font_height(current->ops->font, current->font_size); float off = 0; if (current->tooltip_img != NULL) { @@ -648,7 +648,7 @@ void ui_draw_tooltip_image(bool bind_global_g) { current->tooltip_x = fmin(current->tooltip_x, iron_window_width() - w - 20); current->tooltip_y = fmin(current->tooltip_y, iron_window_height() - h - 20); if (bind_global_g) { - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); } draw_set_color(0xff000000); draw_filled_rect(current->tooltip_x, current->tooltip_y, w, h); @@ -662,7 +662,7 @@ void ui_draw_tooltip(bool bind_global_g) { static char temp[1024]; if (current->slider_tooltip) { if (bind_global_g) { - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); } draw_set_font(current->ops->font, current->font_size * 2); sprintf(temp, "%f", round(current->scroll_handle->value * 100.0) / 100.0); @@ -678,7 +678,7 @@ void ui_draw_tooltip(bool bind_global_g) { } if (ui_touch_tooltip && current->text_selected_handle != NULL) { if (bind_global_g) { - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); } draw_set_font(current->ops->font, current->font_size * 2.0); float x_off = draw_string_width(current->ops->font, current->font_size * 2.0, current->text_selected) / 2.0; @@ -719,7 +719,7 @@ void ui_draw_combo(bool begin /*= true*/) { } draw_set_color(theme->SEPARATOR_COL); if (begin) { - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); } float combo_h = (current->combo_selected_texts->length + (current->combo_selected_label != NULL ? 1 : 0) + (current->combo_search_bar ? 1 : 0)) * UI_ELEMENT_H(); @@ -898,38 +898,35 @@ void ui_bake_elements() { } float r = UI_CHECK_SELECT_SIZE(); iron_gpu_render_target_init(¤t->check_select_image, r, r, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(¤t->check_select_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(¤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()); draw_line(r / 2.0 - 3.0 * UI_SCALE(), r - 3.0 * UI_SCALE(), r / 2.0 + 5.0 * UI_SCALE(), r - 11.0 * UI_SCALE(), 2.0 * UI_SCALE()); - draw_end(); + // draw_end(); if (current->radio_image.width != 0) { iron_gpu_texture_destroy(¤t->radio_image); } r = UI_CHECK_SIZE(); iron_gpu_render_target_init(¤t->radio_image, r, r, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(¤t->radio_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(¤t->radio_image, true, 0x00000000); draw_set_color(0xffaaaaaa); draw_filled_circle(r / 2.0, r / 2.0, r / 2.0, 0); draw_set_color(0xffffffff); draw_circle(r / 2.0, r / 2.0, r / 2.0, 0, 1.0 * UI_SCALE()); - draw_end(); + // draw_end(); if (current->radio_select_image.width != 0) { iron_gpu_texture_destroy(¤t->radio_select_image); } r = UI_CHECK_SELECT_SIZE(); iron_gpu_render_target_init(¤t->radio_select_image, r, r, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(¤t->radio_select_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(¤t->radio_select_image, true, 0x00000000); draw_set_color(0xffaaaaaa); draw_filled_circle(r / 2.0, r / 2.0, 4.5 * UI_SCALE(), 0); draw_set_color(0xffffffff); draw_filled_circle(r / 2.0, r / 2.0, 4.0 * UI_SCALE(), 0); - draw_end(); + // draw_end(); if (theme->ROUND_CORNERS) { if (current->filled_round_corner_image.width != 0) { @@ -937,24 +934,22 @@ void ui_bake_elements() { } r = 4.0 * UI_SCALE(); iron_gpu_render_target_init(¤t->filled_round_corner_image, r, r, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(¤t->filled_round_corner_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(¤t->filled_round_corner_image, true, 0x00000000); draw_set_color(0xffffffff); draw_filled_circle(r, r, r, 0); - draw_end(); + // draw_end(); if (current->round_corner_image.width != 0) { iron_gpu_texture_destroy(¤t->round_corner_image); } iron_gpu_render_target_init(¤t->round_corner_image, r, r, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(¤t->round_corner_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(¤t->round_corner_image, true, 0x00000000); draw_set_color(0xffffffff); draw_circle(r, r, r, 0, 1); - draw_end(); + // draw_end(); } - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); current->elements_baked = true; } @@ -1544,7 +1539,7 @@ void ui_begin_sticky() { } void ui_end_sticky() { - draw_end(); + // draw_end(); current->sticky = false; current->scissor = true; gpu_scissor(0, current->_y, current->_window_w, current->_window_h - current->_y); @@ -1651,7 +1646,7 @@ void ui_end_window(bool bind_global_g) { // Draw window texture if (ui_always_redraw_window || handle->redraws > -4) { if (bind_global_g) { - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); } draw_set_color(0xffffffff); draw_image(&handle->texture, current->_window_x, current->_window_y); @@ -1677,8 +1672,6 @@ bool _ui_window(ui_handle_t *handle, int x, int y, int w, int h, bool drag) { ui_end_window(true); // End previous window if necessary } current->window_ended = false; - - draw_set_render_target(&handle->texture); current->current_window = handle; current->_window_x = x + handle->drag_x; current->_window_y = y + handle->drag_y; @@ -1726,10 +1719,10 @@ bool _ui_window(ui_handle_t *handle, int x, int y, int w, int h, bool drag) { current->tab_count = 0; if (theme->FILL_WINDOW_BG) { - iron_gpu_clear(theme->WINDOW_BG_COL, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(&handle->texture, true, theme->WINDOW_BG_COL); } else { - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(&handle->texture, true, 0x00000000); draw_set_color(theme->WINDOW_BG_COL); draw_filled_rect(current->_x, current->_y - handle->scroll_offset, handle->last_max_x, handle->last_max_y); } diff --git a/base/sources/iron_ui_nodes.c b/base/sources/iron_ui_nodes.c index 95d7b2dc..a0b3343e 100644 --- a/base/sources/iron_ui_nodes.c +++ b/base/sources/iron_ui_nodes.c @@ -239,15 +239,14 @@ void ui_nodes_bake_elements() { iron_gpu_texture_destroy(&ui_socket_image); } iron_gpu_render_target_init(&ui_socket_image, 24, 24, IRON_IMAGE_FORMAT_RGBA32, 0); - draw_set_render_target(&ui_socket_image); - iron_gpu_clear(0x00000000, 0, IRON_GPU_CLEAR_COLOR); + draw_set_render_target(&ui_socket_image, true, 0x00000000); draw_set_color(0xff111111); draw_filled_circle(12, 12, 11, 0); draw_set_color(0xffffffff); draw_filled_circle(12, 12, 9, 0); - draw_restore_render_target(); + draw_set_render_target(NULL, false, 0); ui_nodes_elements_baked = true; } diff --git a/base/sources/libs/kong/backends/metal.c b/base/sources/libs/kong/backends/metal.c index a43597e7..8265d60b 100644 --- a/base/sources/libs/kong/backends/metal.c +++ b/base/sources/libs/kong/backends/metal.c @@ -404,7 +404,6 @@ static void write_functions(char *code, size_t *offset) { else { descriptor_set_group *set_group = get_descriptor_set_group(0); descriptor_set *set = set_group->values[0]; - *offset += sprintf(&code[*offset], "%s %s(constant %s& argument_buffer0", type_string(f->return_type.type), get_name(f->name), get_name(set->name)); for (uint8_t parameter_index = 0; parameter_index < f->parameters_size; ++parameter_index) { diff --git a/base/sources/ts/export_texture.ts b/base/sources/ts/export_texture.ts index 5b8737ff..f77cbf29 100644 --- a/base/sources/ts/export_texture.ts +++ b/base/sources/ts/export_texture.ts @@ -190,14 +190,11 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ } // Clear export layer - _gpu_begin(layers_expa); - iron_gpu_clear(color_from_floats(0.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expa, null, clear_flag_t.COLOR, color_from_floats(0.0, 0.0, 0.0, 0.0)); _gpu_end(); - _gpu_begin(layers_expb); - iron_gpu_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); + _gpu_begin(layers_expb, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); _gpu_end(); - _gpu_begin(layers_expc); - iron_gpu_clear(color_from_floats(1.0, 0.0, 0.0, 0.0)); + _gpu_begin(layers_expc, null, clear_flag_t.COLOR, color_from_floats(1.0, 0.0, 0.0, 0.0)); _gpu_end(); // Flatten layers @@ -225,8 +222,7 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ if (l1masks != null && !bake_material) { if (l1masks.length > 1) { layers_make_temp_mask_img(); - draw_begin(pipes_temp_mask_image); - iron_gpu_clear(0x00000000); + draw_begin(pipes_temp_mask_image, true, 0x00000000); draw_end(); let l1: slot_layer_t = { texpaint: pipes_temp_mask_image @@ -249,7 +245,7 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ draw_end(); _gpu_begin(layers_expa); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, empty); gpu_set_texture(pipes_texmask, mask); @@ -259,7 +255,7 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ gpu_set_int(pipes_blending, layers.length > 1 ? l1.blending : 0); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } @@ -271,7 +267,7 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ draw_end(); _gpu_begin(layers_expb); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, l1.texpaint_nor); gpu_set_texture(pipes_texmask, mask); @@ -281,7 +277,7 @@ function export_texture_run_layers(path: string, layers: slot_layer_t[], object_ gpu_set_int(pipes_blending, l1.paint_nor_blend ? -2 : -1); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } diff --git a/base/sources/ts/import_envmap.ts b/base/sources/ts/import_envmap.ts index 00ef5cef..1a378493 100644 --- a/base/sources/ts/import_envmap.ts +++ b/base/sources/ts/import_envmap.ts @@ -96,11 +96,11 @@ function import_envmap_get_radiance_mip(mip: iron_gpu_texture_t, level: i32, rad _gpu_begin(mip); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - iron_gpu_set_pipeline(import_envmap_pipeline); + gpu_set_pipeline(import_envmap_pipeline); import_envmap_params.x = 0.1 + level / 8; gpu_set_float4(import_envmap_params_loc, import_envmap_params.x, import_envmap_params.y, import_envmap_params.z, import_envmap_params.w); gpu_set_texture(import_envmap_radiance_loc, radiance); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } diff --git a/base/sources/ts/iron/const_data.ts b/base/sources/ts/iron/const_data.ts index 60ae4d5b..dc85406a 100644 --- a/base/sources/ts/iron/const_data.ts +++ b/base/sources/ts/iron/const_data.ts @@ -17,14 +17,14 @@ function const_data_create_screen_aligned_data() { for (let i: i32 = 0; i < math_floor((vertices.length) / 4); ++i) { buffer_set_f32(vertices, i * 4, data[i]); } - iron_gpu_vertex_buffer_unlock_all(const_data_screen_aligned_vb); + iron_gpu_vertex_buffer_unlock(const_data_screen_aligned_vb); const_data_screen_aligned_ib = gpu_create_index_buffer(indices.length); let id: u32_array_t = gpu_lock_index_buffer(const_data_screen_aligned_ib); for (let i: i32 = 0; i < id.length; ++i) { id[i] = indices[i]; } - gpu_index_buffer_unlock_all(const_data_screen_aligned_ib); + gpu_index_buffer_unlock(const_data_screen_aligned_ib); } ///include "const_data.h" @@ -50,12 +50,12 @@ function const_data_create_skydome_data() { buffer_set_f32(vertices, (i * struct_length + 4) * 4, ARRAY_ACCESS(_const_data_skydome_nor, i * 3 + 1)); buffer_set_f32(vertices, (i * struct_length + 5) * 4, ARRAY_ACCESS(_const_data_skydome_nor, i * 3 + 2)); } - iron_gpu_vertex_buffer_unlock_all(const_data_skydome_vb); + iron_gpu_vertex_buffer_unlock(const_data_skydome_vb); const_data_skydome_ib = gpu_create_index_buffer(_const_data_skydome_indices_count); let id: u32_array_t = gpu_lock_index_buffer(const_data_skydome_ib); for (let i: i32 = 0; i < id.length; ++i) { id[i] = ARRAY_ACCESS(_const_data_skydome_indices, i); } - gpu_index_buffer_unlock_all(const_data_skydome_ib); + gpu_index_buffer_unlock(const_data_skydome_ib); } diff --git a/base/sources/ts/iron/iron.ts b/base/sources/ts/iron/iron.ts index 0a8b8f5b..15dc2843 100644 --- a/base/sources/ts/iron/iron.ts +++ b/base/sources/ts/iron/iron.ts @@ -183,7 +183,6 @@ declare function color_set_ab(c: i32, i: u8): i32; declare function _iron_init(ops: iron_window_options_t): void; declare function iron_set_app_name(name: string): void; declare function iron_log(v: any): void; -declare function iron_gpu_clear(color: i32, depth: f32 = 0.0, flags: i32 = clear_flag_t.COLOR): void; declare function _iron_set_update_callback(callback: ()=>void): void; declare function _iron_set_drop_files_callback(callback: (file: string)=>void): void; declare function iron_set_cut_copy_paste_callback(on_cut: ()=>string, on_copy: ()=>string, on_paste: (text: string)=>void): void; @@ -214,21 +213,21 @@ declare function iron_show_keyboard(show: bool): void; declare function gpu_create_index_buffer(count: i32): any; declare function gpu_delete_index_buffer(buffer: any): void; declare function gpu_lock_index_buffer(buffer: any): u32_array_t; -declare function gpu_index_buffer_unlock_all(buffer: any): void; +declare function gpu_index_buffer_unlock(buffer: any): void; declare function gpu_set_index_buffer(buffer: any): void; declare function gpu_create_vertex_buffer(count: i32, structure: iron_gpu_vertex_structure_t, usage: i32): any; declare function gpu_delete_vertex_buffer(buffer: any): void; declare function gpu_lock_vertex_buffer(buffer: any): buffer_t; -declare function iron_gpu_vertex_buffer_unlock_all(buffer: any): void; +declare function iron_gpu_vertex_buffer_unlock(buffer: any): void; declare function gpu_set_vertex_buffer(buffer: any): void; -declare function gpu_draw_indexed_vertices(): void; +declare function gpu_draw(): void; declare function gpu_create_shader(data: buffer_t, type: i32): iron_gpu_shader_t; declare function gpu_create_shader_from_source(source: string, shader_type: shader_type_t): iron_gpu_shader_t; declare function iron_gpu_shader_destroy(shader: iron_gpu_shader_t): void; declare function gpu_create_pipeline(): any; declare function gpu_delete_pipeline(pipeline: any): void; declare function gpu_compile_pipeline(pipeline: any): void; -declare function iron_gpu_set_pipeline(pipeline: any): void; +declare function gpu_set_pipeline(pipeline: any): void; declare function iron_load_image(file: string, readable: bool): any; declare function iron_unload_image(image: iron_gpu_texture_t): void; declare function iron_load_sound(file: string): any; @@ -283,7 +282,7 @@ declare function iron_gpu_render_target_set_depth_from(target: any, source: any) 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: iron_gpu_texture_t, additional: iron_gpu_texture_t[] = null): void; +declare function _gpu_begin(render_target: iron_gpu_texture_t, additional: iron_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_swap_buffers(): void; declare function iron_file_save_bytes(path: string, bytes: buffer_t, length?: i32): void; @@ -295,7 +294,7 @@ declare function iron_get_files_location(): string; declare function _iron_http_request(url: string, size: i32, callback: (url: string, _: buffer_t)=>void): void; declare function draw_init(image_vert: buffer_t, image_frag: buffer_t, rect_vert: buffer_t, rect_frag: buffer_t, tris_vert: buffer_t, tris_frag: buffer_t, text_vert: buffer_t, text_frag: buffer_t): void; -declare function draw_begin(render_target: iron_gpu_texture_t = null): void; +declare function draw_begin(render_target: iron_gpu_texture_t = null, clear: bool = false, color: u32 = 0): void; declare function draw_end(): void; declare function draw_scaled_sub_image(image: iron_gpu_texture_t, sx: f32, sy: f32, sw: f32, sh: f32, dx: f32, dy: f32, dw: f32, dh: f32): void; declare function draw_sub_image(image: iron_gpu_texture_t, x: f32, y: f32, sx: f32, sy: f32, sw: f32, sh: f32): void; @@ -641,6 +640,7 @@ declare type iron_gpu_constant_location_t = any; declare type iron_gpu_texture_unit_t = any; enum clear_flag_t { + NONE = 0, COLOR = 1, DEPTH = 2, } diff --git a/base/sources/ts/iron/mesh_data.ts b/base/sources/ts/iron/mesh_data.ts index 90ad12da..85718b69 100644 --- a/base/sources/ts/iron/mesh_data.ts +++ b/base/sources/ts/iron/mesh_data.ts @@ -201,7 +201,7 @@ function mesh_data_get(raw: mesh_data_t, vs: vertex_element_t[]): iron_gpu_buffe vb = gpu_create_vertex_buffer(math_floor(positions.values.length / size), vstruct, usage_t.STATIC); raw._.vertices = gpu_lock_vertex_buffer(vb); mesh_data_build_vertices(raw._.vertices, vertex_arrays, 0, has_tex && uvs == null, tex_offset); - iron_gpu_vertex_buffer_unlock_all(vb); + iron_gpu_vertex_buffer_unlock(vb); map_set(raw._.vertex_buffer_map, key, vb); if (has_tex && uvs == null) { iron_log("Geometry " + raw.name + " is missing UV map"); @@ -223,7 +223,7 @@ function mesh_data_build(raw: mesh_data_t) { raw._.vertex_buffer = gpu_create_vertex_buffer(math_floor(positions.values.length / size), raw._.structure, usage_t.STATIC); raw._.vertices = gpu_lock_vertex_buffer(raw._.vertex_buffer); mesh_data_build_vertices(raw._.vertices, raw.vertex_arrays); - iron_gpu_vertex_buffer_unlock_all(raw._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(raw._.vertex_buffer); let struct_str: string = ""; for (let i: i32 = 0; i < raw._.structure.size; ++i) { @@ -246,7 +246,7 @@ function mesh_data_build(raw: mesh_data_t) { indices_array[i] = id[i]; } - gpu_index_buffer_unlock_all(index_buffer); + gpu_index_buffer_unlock(index_buffer); array_push(raw._.index_buffers, index_buffer); } diff --git a/base/sources/ts/iron/mesh_object.ts b/base/sources/ts/iron/mesh_object.ts index bc23a749..f409e896 100644 --- a/base/sources/ts/iron/mesh_object.ts +++ b/base/sources/ts/iron/mesh_object.ts @@ -170,7 +170,7 @@ function mesh_object_render(raw: mesh_object_t, context: string, bind_params: st // Uniforms if (scontext._.pipe_state != _mesh_object_last_pipeline) { - iron_gpu_set_pipeline(scontext._.pipe_state); + gpu_set_pipeline(scontext._.pipe_state); _mesh_object_last_pipeline = scontext._.pipe_state; } uniforms_set_context_consts(scontext, bind_params); @@ -181,7 +181,7 @@ function mesh_object_render(raw: mesh_object_t, context: string, bind_params: st gpu_set_vertex_buffer(mesh_data_get(raw.data, elems)); gpu_set_index_buffer(raw.data._.index_buffers[i]); - gpu_draw_indexed_vertices(); + gpu_draw(); } } diff --git a/base/sources/ts/iron/render_path.ts b/base/sources/ts/iron/render_path.ts index 7c12a904..40108204 100644 --- a/base/sources/ts/iron/render_path.ts +++ b/base/sources/ts/iron/render_path.ts @@ -77,12 +77,12 @@ function render_path_render_frame() { _render_path_frame++; } -function render_path_set_target(target: string, additional: string[] = null) { +function render_path_set_target(target: string, additional: string[] = null, flags: i32 = clear_flag_t.NONE, color: i32 = 0, depth: f32 = 0.0) { if (target == "") { // Framebuffer _render_path_current_target = null; render_path_current_w = sys_w(); render_path_current_h = sys_h(); - render_path_begin(); + render_path_begin(null, null, flags, color, depth); render_path_set_current_viewport(sys_w(), sys_h()); render_path_set_current_scissor(sys_w(), sys_h()); } @@ -100,7 +100,7 @@ function render_path_set_target(target: string, additional: string[] = null) { } render_path_current_w = rt._image.width; render_path_current_h = rt._image.height; - render_path_begin(rt._image, additional_images); + render_path_begin(rt._image, additional_images, flags, color, depth); } _render_path_bind_params = null; } @@ -111,12 +111,12 @@ function render_path_set_depth_from(target: string, from: string) { iron_gpu_render_target_set_depth_from(rt._image, rt_from._image); } -function render_path_begin(render_target: iron_gpu_texture_t = null, additional_targets: iron_gpu_texture_t[] = null) { +function render_path_begin(render_target: iron_gpu_texture_t, additional_targets: iron_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); + _gpu_begin(render_target, additional_targets, flags, color, depth); } function render_path_end() { @@ -143,10 +143,6 @@ function render_path_set_viewport(view_w: i32, view_h: i32) { render_path_set_current_scissor(view_w, view_h); } -function render_path_clear_target(color: color_t = 0x00000000, depth: f32 = 0.0, flags: i32 = clear_flag_t.COLOR) { - iron_gpu_clear(color, depth, flags); -} - function render_path_gen_mipmaps(target: string) { let rt: render_target_t = map_get(render_path_render_targets, target); iron_gpu_texture_generate_mipmaps(rt._image, 1000); @@ -213,12 +209,12 @@ function render_path_draw_skydome(handle: string) { if (cc.context == null) { return; // World data not specified } - iron_gpu_set_pipeline(cc.context._.pipe_state); + gpu_set_pipeline(cc.context._.pipe_state); uniforms_set_context_consts(cc.context, _render_path_bind_params); uniforms_set_obj_consts(cc.context, null); // External hosek gpu_set_vertex_buffer(const_data_skydome_vb); gpu_set_index_buffer(const_data_skydome_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); render_path_end(); } @@ -239,12 +235,12 @@ function render_path_draw_shader(handle: string) { if (const_data_screen_aligned_vb == null) { const_data_create_screen_aligned_data(); } - iron_gpu_set_pipeline(cc.context._.pipe_state); + gpu_set_pipeline(cc.context._.pipe_state); uniforms_set_context_consts(cc.context, _render_path_bind_params); uniforms_set_obj_consts(cc.context, null); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); render_path_end(); } diff --git a/base/sources/ts/iron/shader_data.ts b/base/sources/ts/iron/shader_data.ts index 1648969c..c2bf8291 100644 --- a/base/sources/ts/iron/shader_data.ts +++ b/base/sources/ts/iron/shader_data.ts @@ -163,6 +163,7 @@ function shader_context_type_size(t: string): i32 { if (t == "float") return 4; if (t == "vec2") return 8; if (t == "vec3") return 12; + // if (t == "vec3") return 16; if (t == "vec4") return 16; if (t == "mat3") return 48; if (t == "mat4") return 64; @@ -170,6 +171,9 @@ function shader_context_type_size(t: string): i32 { } function shader_context_type_pad(offset: i32, size: i32): i32 { + ///if arm_metal + // return 0; + ///end let r: i32 = offset % 16; if (r == 0) { return 0; diff --git a/base/sources/ts/layers.ts b/base/sources/ts/layers.ts index 2e1ad1e2..810933f1 100644 --- a/base/sources/ts/layers.ts +++ b/base/sources/ts/layers.ts @@ -32,19 +32,19 @@ function layers_init() { draw_begin(texpaint._image); draw_scaled_image(resource_get("placeholder.k"), 0, 0, config_get_texture_res_x(), config_get_texture_res_y()); // Base draw_end(); - _gpu_begin(texpaint_nor._image); - iron_gpu_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); // Nor + // Nor + _gpu_begin(texpaint_nor._image, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); _gpu_end(); - _gpu_begin(texpaint_pack._image); - iron_gpu_clear(color_from_floats(1.0, 0.4, 0.0, 0.0)); // Occ, rough, met + // 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_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"); - _gpu_begin(texpaint_nor_empty._image); - iron_gpu_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); // Nor + // Nor + _gpu_begin(texpaint_nor_empty._image, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); _gpu_end(); - _gpu_begin(texpaint_pack_empty._image); - iron_gpu_clear(color_from_floats(1.0, 0.4, 0.0, 0.0)); // Occ, rough, met + // 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_end(); ///end } @@ -256,18 +256,18 @@ function layers_apply_mask(l: slot_layer_t, m: slot_layer_t) { // Apply mask _gpu_begin(l.texpaint); - iron_gpu_set_pipeline(pipes_apply_mask); + gpu_set_pipeline(pipes_apply_mask); gpu_set_texture(pipes_tex0_mask, layers_temp_image); gpu_set_texture(pipes_texa_mask, m.texpaint); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } function layers_commands_merge_pack(pipe: iron_gpu_pipeline_t, i0: iron_gpu_texture_t, i1: iron_gpu_texture_t, i1pack: iron_gpu_texture_t, i1mask_opacity: f32, i1texmask: iron_gpu_texture_t, i1blending: i32 = -1) { _gpu_begin(i0); - iron_gpu_set_pipeline(pipe); + gpu_set_pipeline(pipe); gpu_set_texture(pipes_tex0, i1); gpu_set_texture(pipes_tex1, i1pack); gpu_set_texture(pipes_texmask, i1texmask); @@ -277,7 +277,7 @@ function layers_commands_merge_pack(pipe: iron_gpu_pipeline_t, i0: iron_gpu_text gpu_set_int(pipes_blending, i1blending); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } @@ -731,21 +731,21 @@ function layers_merge_layer(l0 : slot_layer_t, l1: slot_layer_t, use_mask: bool if (slot_layer_is_mask(l1)) { _gpu_begin(l0.texpaint); - iron_gpu_set_pipeline(pipes_merge_mask); + gpu_set_pipeline(pipes_merge_mask); gpu_set_texture(pipes_tex0_merge_mask, l1.texpaint); gpu_set_texture(pipes_texa_merge_mask, layers_temp_image); gpu_set_float(pipes_opac_merge_mask, slot_layer_get_opacity(l1)); gpu_set_int(pipes_blending_merge_mask, l1.blending); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } if (slot_layer_is_layer(l1)) { if (l1.paint_base) { _gpu_begin(l0.texpaint); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, empty); gpu_set_texture(pipes_texmask, mask); @@ -755,7 +755,7 @@ function layers_merge_layer(l0 : slot_layer_t, l1: slot_layer_t, use_mask: bool gpu_set_int(pipes_blending, l1.blending); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } @@ -768,7 +768,7 @@ function layers_merge_layer(l0 : slot_layer_t, l1: slot_layer_t, use_mask: bool if (l1.paint_nor) { _gpu_begin(l0.texpaint_nor); - iron_gpu_set_pipeline(pipes_merge); + gpu_set_pipeline(pipes_merge); gpu_set_texture(pipes_tex0, l1.texpaint); gpu_set_texture(pipes_tex1, l1.texpaint_nor); gpu_set_texture(pipes_texmask, mask); @@ -778,7 +778,7 @@ function layers_merge_layer(l0 : slot_layer_t, l1: slot_layer_t, use_mask: bool gpu_set_int(pipes_blending, l1.paint_nor_blend ? -2 : -1); gpu_set_vertex_buffer(const_data_screen_aligned_vb); gpu_set_index_buffer(const_data_screen_aligned_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } } diff --git a/base/sources/ts/line_draw.ts b/base/sources/ts/line_draw.ts index dcc73129..02ccf632 100644 --- a/base/sources/ts/line_draw.ts +++ b/base/sources/ts/line_draw.ts @@ -228,12 +228,12 @@ function line_draw_begin() { } function line_draw_end(overlay: bool = false) { - iron_gpu_vertex_buffer_unlock_all(line_draw_vertex_buffer); - gpu_index_buffer_unlock_all(line_draw_index_buffer); + iron_gpu_vertex_buffer_unlock(line_draw_vertex_buffer); + gpu_index_buffer_unlock(line_draw_index_buffer); gpu_set_vertex_buffer(line_draw_vertex_buffer); gpu_set_index_buffer(line_draw_index_buffer); - iron_gpu_set_pipeline(overlay ? line_draw_overlay_pipeline : line_draw_pipeline); + gpu_set_pipeline(overlay ? line_draw_overlay_pipeline : line_draw_pipeline); let camera: camera_object_t = scene_camera; line_draw_vp = mat4_clone(camera.v); line_draw_vp = mat4_mult_mat(line_draw_vp, camera.p); @@ -244,7 +244,7 @@ function line_draw_end(overlay: bool = false) { color_get_bb(line_draw_color) / 255 ); //// - // gpu_draw_indexed_vertices(0, line_draw_lines * 6); + // gpu_draw(0, line_draw_lines * 6); //// } @@ -268,13 +268,13 @@ function shape_draw_sphere(mat: mat4_t) { buffer_set_f32(data, (i * 3 + 1) * 4, posa[i * 4 + 1] / 32767); buffer_set_f32(data, (i * 3 + 2) * 4, posa[i * 4 + 2] / 32767); } - iron_gpu_vertex_buffer_unlock_all(_shape_draw_sphere_vb); + iron_gpu_vertex_buffer_unlock(_shape_draw_sphere_vb); _shape_draw_sphere_ib = md._.index_buffers[0]; } gpu_set_vertex_buffer(_shape_draw_sphere_vb); gpu_set_index_buffer(_shape_draw_sphere_ib); - iron_gpu_set_pipeline(line_draw_overlay_pipeline); + gpu_set_pipeline(line_draw_overlay_pipeline); let camera: camera_object_t = scene_camera; line_draw_vp = mat4_clone(mat); let f: f32 = line_draw_strength * 50; @@ -287,5 +287,5 @@ function shape_draw_sphere(mat: mat4_t) { color_get_gb(line_draw_color) / 255, color_get_bb(line_draw_color) / 255 ); - gpu_draw_indexed_vertices(); + gpu_draw(); } \ No newline at end of file diff --git a/base/sources/ts/node_shader.ts b/base/sources/ts/node_shader.ts index b38405da..e295a53a 100644 --- a/base/sources/ts/node_shader.ts +++ b/base/sources/ts/node_shader.ts @@ -192,17 +192,17 @@ function node_shader_get(raw: node_shader_t): string { s += "};\n\n"; } + if (raw.textures.length > 0) { + s += "#[set(everything)]\n"; + s += "const sampler_linear: sampler;\n\n"; + } + for (let i: i32 = 0; i < raw.textures.length; ++i) { let a: string = raw.textures[i]; s += "#[set(everything)]\n"; s += "const " + a + ": tex2d;\n"; } - if (raw.textures.length > 0) { - s += "#[set(everything)]\n"; - s += "const sampler_linear: sampler;\n\n"; - } - let keys: string[] = map_keys(raw.functions); for (let i: i32 = 0; i < keys.length; ++i) { let f: string = map_get(raw.functions, keys[i]); diff --git a/base/sources/ts/render_path_base.ts b/base/sources/ts/render_path_base.ts index f1b09bde..d6d1be33 100644 --- a/base/sources/ts/render_path_base.ts +++ b/base/sources/ts/render_path_base.ts @@ -191,8 +191,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); - render_path_clear_target(0x00000000); + render_path_set_target(render_path_base_bloom_mipmaps[i].name, 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"); } @@ -350,15 +349,9 @@ function render_path_base_draw_taa(bufa: string, bufb: string) { } function render_path_base_draw_gbuffer() { - render_path_set_target("gbuffer0"); // Only clear gbuffer0 - ///if arm_metal - render_path_clear_target(0x00000000, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); - ///else - render_path_clear_target(0, 1.0, clear_flag_t.DEPTH); - ///end + render_path_set_target("gbuffer0", null, clear_flag_t.DEPTH, 0, 1.0); // Only clear gbuffer0 if (make_mesh_layer_pass_count == 1) { - render_path_set_target("gbuffer2"); - render_path_clear_target(0xff000000); + render_path_set_target("gbuffer2", null, clear_flag_t.COLOR, 0xff000000); } let additional: string[] = ["gbuffer1", "gbuffer2"]; render_path_set_target("gbuffer0", additional); @@ -371,8 +364,7 @@ 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); - render_path_clear_target(0xff000000); + render_path_set_target("gbuffer2" + ping, null, clear_flag_t.COLOR, 0xff000000); } let g1ping: string = "gbuffer1" + ping; let g2ping: string = "gbuffer2" + ping; diff --git a/base/sources/ts/render_path_preview.ts b/base/sources/ts/render_path_preview.ts index 34a21f8b..dc9cc613 100644 --- a/base/sources/ts/render_path_preview.ts +++ b/base/sources/ts/render_path_preview.ts @@ -6,7 +6,7 @@ function render_path_preview_init() { t.name = "texpreview"; t.width = 1; t.height = 1; - t.format = "RGBA32"; + t.format = "RGBA64"; render_path_create_render_target(t); } { @@ -14,7 +14,7 @@ function render_path_preview_init() { t.name = "texpreview_icon"; t.width = 1; t.height = 1; - t.format = "RGBA32"; + t.format = "RGBA64"; render_path_create_render_target(t); } @@ -63,11 +63,8 @@ function render_path_preview_init() { } function render_path_preview_commands_preview() { - render_path_set_target("mgbuffer2"); - render_path_clear_target(0xff000000); - - render_path_set_target("mgbuffer0"); - render_path_clear_target(0xffffffff, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); + 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); let additional: string[] = ["mgbuffer1", "mgbuffer2"]; render_path_set_target("mgbuffer0", additional); render_path_draw_meshes("mesh"); @@ -106,11 +103,8 @@ function render_path_preview_commands_preview() { } function render_path_preview_commands_decal() { - render_path_set_target("gbuffer2"); - render_path_clear_target(0xff000000); - - render_path_set_target("gbuffer0"); - render_path_clear_target(0xffffffff, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); + 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); let additional: string[] = ["gbuffer1", "gbuffer2"]; render_path_set_target("gbuffer0", additional); render_path_draw_meshes("mesh"); diff --git a/base/sources/ts/render_path_raytrace_bake.ts b/base/sources/ts/render_path_raytrace_bake.ts index c79d8ad5..2c0da60f 100644 --- a/base/sources/ts/render_path_raytrace_bake.ts +++ b/base/sources/ts/render_path_raytrace_bake.ts @@ -54,8 +54,8 @@ 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"); - render_path_clear_target(0x00000000); // Pixels with alpha of 0.0 are skipped during raytracing + render_path_set_target("baketex0", 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); render_path_draw_meshes("paint"); diff --git a/base/sources/ts/slot_layer.ts b/base/sources/ts/slot_layer.ts index 073b7625..b64723d3 100644 --- a/base/sources/ts/slot_layer.ts +++ b/base/sources/ts/slot_layer.ts @@ -244,8 +244,8 @@ 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: iron_gpu_texture_t = null, occlusion: f32 = 1.0, roughness: f32 = layers_default_rough, metallic: f32 = 0.0) { - _gpu_begin(raw.texpaint); - iron_gpu_clear(base_color); // Base + // Base + _gpu_begin(raw.texpaint, null, clear_flag_t.COLOR, base_color); _gpu_end(); if (base_image != null) { draw_begin(raw.texpaint); @@ -254,11 +254,11 @@ function slot_layer_clear(raw: slot_layer_t, base_color: i32 = 0x00000000, base_ } if (slot_layer_is_layer(raw)) { - _gpu_begin(raw.texpaint_nor); - iron_gpu_clear(color_from_floats(0.5, 0.5, 1.0, 0.0)); // Nor + // Nor + _gpu_begin(raw.texpaint_nor, null, clear_flag_t.COLOR, color_from_floats(0.5, 0.5, 1.0, 0.0)); _gpu_end(); - _gpu_begin(raw.texpaint_pack); - iron_gpu_clear(color_from_floats(occlusion, roughness, metallic, 0.0)); // Occ, rough, met + // Occ, rough, met + _gpu_begin(raw.texpaint_pack, null, clear_flag_t.COLOR, color_from_floats(occlusion, roughness, metallic, 0.0)); _gpu_end(); } @@ -337,8 +337,7 @@ function slot_layer_duplicate(raw: slot_layer_t): slot_layer_t { } if (l.texpaint_preview != null) { - draw_begin(l.texpaint_preview); - iron_gpu_clear(0x00000000); + draw_begin(l.texpaint_preview, true, 0x00000000); draw_set_pipeline(pipes_copy); draw_scaled_image(raw.texpaint_preview, 0, 0, raw.texpaint_preview.width, raw.texpaint_preview.height); draw_set_pipeline(null); diff --git a/base/sources/ts/slot_material.ts b/base/sources/ts/slot_material.ts index 30ef5093..4481df19 100644 --- a/base/sources/ts/slot_material.ts +++ b/base/sources/ts/slot_material.ts @@ -45,8 +45,8 @@ function slot_material_create(m: material_data_t = null, c: ui_node_canvas_t = n let w: i32 = util_render_material_preview_size; let w_icon: i32 = 50; - raw.image = gpu_create_render_target(w, w); - raw.image_icon = gpu_create_render_target(w_icon, w_icon); + raw.image = gpu_create_render_target(w, w, tex_format_t.RGBA64); + raw.image_icon = gpu_create_render_target(w_icon, w_icon, tex_format_t.RGBA64); if (c == null) { if (slot_material_default_canvas == null) { // Synchronous diff --git a/base/sources/ts/ui_base.ts b/base/sources/ts/ui_base.ts index cb09ef61..80cfdbef 100644 --- a/base/sources/ts/ui_base.ts +++ b/base/sources/ts/ui_base.ts @@ -1088,8 +1088,7 @@ function ui_base_update_ui() { } let source: iron_gpu_texture_t = l.texpaint; - draw_begin(target); - iron_gpu_clear(0x00000000); + draw_begin(target, true, 0x00000000); // draw_set_pipeline(l.is_mask() ? pipes_copy8 : pipes_copy); draw_set_pipeline(pipes_copy); // texpaint_preview is always RGBA32 for now draw_scaled_image(source, 0, 0, target.width, target.height); @@ -1108,8 +1107,7 @@ function ui_base_update_ui() { if (target != null) { let source: iron_gpu_texture_t = l.texpaint; - draw_begin(target); - iron_gpu_clear(0x00000000); + draw_begin(target, true, 0x00000000); // draw_set_pipeline(raw.layer.is_mask() ? pipes_copy8 : pipes_copy); draw_set_pipeline(pipes_copy); // texpaint_preview is always RGBA32 for now draw_scaled_image(source, 0, 0, target.width, target.height); diff --git a/base/sources/ts/ui_files.ts b/base/sources/ts/ui_files.ts index 459850cc..9c3a3bda 100644 --- a/base/sources/ts/ui_files.ts +++ b/base/sources/ts/ui_files.ts @@ -214,8 +214,7 @@ function ui_files_file_browser(ui: ui_t, handle: ui_handle_t, drag_files: bool = ///end } else { - draw_begin(icon); - iron_gpu_clear(0xffffffff); + draw_begin(icon, true, 0xffffffff); } draw_set_pipeline(pipes_copy_rgb); draw_image(data.image, 0, 0); @@ -432,8 +431,7 @@ function ui_files_make_icon (args: ui_files_make_icon_t) { let sw: i32 = image.width > image.height ? w : math_floor(1.0 * image.width / image.height * w); let sh: i32 = image.width > image.height ? math_floor(1.0 * image.height / image.width * w) : w; let icon: iron_gpu_texture_t = gpu_create_render_target(sw, sh); - draw_begin(icon); - iron_gpu_clear(0xffffffff); + draw_begin(icon, true, 0xffffffff); draw_set_pipeline(pipes_copy_rgb); draw_scaled_image(image, 0, 0, sw, sh); draw_set_pipeline(null); diff --git a/base/sources/ts/ui_nodes.ts b/base/sources/ts/ui_nodes.ts index b2ad5d6c..514ed184 100644 --- a/base/sources/ts/ui_nodes.ts +++ b/base/sources/ts/ui_nodes.ts @@ -741,8 +741,7 @@ function ui_nodes_draw_grid(zoom: f32): iron_gpu_texture_t { } let grid: iron_gpu_texture_t = gpu_create_render_target(w, h); - draw_begin(grid); - iron_gpu_clear(ui_nodes_ui.ops.theme.SEPARATOR_COL); + draw_begin(grid, true, ui_nodes_ui.ops.theme.SEPARATOR_COL); let sep_col: i32 = ui_nodes_ui.ops.theme.SEPARATOR_COL; let line_primary: i32 = sep_col - 0x00050505; diff --git a/base/sources/ts/util_mesh.ts b/base/sources/ts/util_mesh.ts index 68971a91..ec1b861d 100644 --- a/base/sources/ts/util_mesh.ts +++ b/base/sources/ts/util_mesh.ts @@ -169,7 +169,7 @@ function util_mesh_swap_axis(a: i32, b: i32) { buffer_set_i16(vertices, (i * l + 4) * 2, vas[1].values[i * 2 ]); buffer_set_i16(vertices, (i * l + 5) * 2, vas[1].values[i * 2 + 1]); } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); } util_mesh_remove_merged(); @@ -194,7 +194,7 @@ function util_mesh_flip_normals() { buffer_set_i16(vertices, (i * l + 4) * 2, -buffer_get_i16(vertices, (i * l + 4) * 2)); buffer_set_i16(vertices, (i * l + 5) * 2, -buffer_get_i16(vertices, (i * l + 5) * 2)); } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); } render_path_raytrace_ready = false; @@ -286,7 +286,7 @@ function util_mesh_calc_normals(smooth: bool = false) { } } } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); let va0: i16_array_t = o.data.vertex_arrays[0].values; let va1: i16_array_t = o.data.vertex_arrays[1].values; @@ -377,7 +377,7 @@ function util_mesh_to_origin() { buffer_set_i16(vertices, (i * l + 1) * 2, va[i * 4 + 1]); buffer_set_i16(vertices, (i * l + 2) * 2, va[i * 4 + 2]); } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); } util_mesh_merge(); @@ -402,7 +402,7 @@ function util_mesh_apply_displacement(texpaint_pack: iron_gpu_texture_t, strengt buffer_set_i16(vertices, (i * l + 1) * 2, buffer_get_i16(vertices, (i * l + 1) * 2) - math_floor(buffer_get_i16(vertices, (i * l + 5) * 2) * h)); buffer_set_i16(vertices, (i * l + 2) * 2, buffer_get_i16(vertices, (i * l + 2) * 2) - math_floor(buffer_get_i16(vertices, (i * l + 3) * 2) * h)); } - iron_gpu_vertex_buffer_unlock_all(g._.vertex_buffer); + iron_gpu_vertex_buffer_unlock(g._.vertex_buffer); let va0: i16_array_t = o.data.vertex_arrays[0].values; for (let i: i32 = 0; i < math_floor((vertices.length) / 4 / l); ++i) { diff --git a/base/sources/ts/util_render.ts b/base/sources/ts/util_render.ts index 4a59e5a1..f3ec9bb1 100644 --- a/base/sources/ts/util_render.ts +++ b/base/sources/ts/util_render.ts @@ -167,8 +167,7 @@ function util_render_make_text_preview() { context_raw.text_tool_image = gpu_create_render_target(tex_w, tex_w, tex_format_t.R8); ///end } - draw_begin(context_raw.text_tool_image); - iron_gpu_clear(0xff000000); + draw_begin(context_raw.text_tool_image, true, 0xff000000); draw_set_font(font, font_size); draw_set_color(0xffffffff); draw_string(text, tex_w / 2 - text_w / 2, tex_w / 2 - text_h / 2); @@ -191,8 +190,7 @@ function util_render_make_font_preview() { if (context_raw.font.image == null) { context_raw.font.image = gpu_create_render_target(tex_w, tex_w, tex_format_t.RGBA32); } - draw_begin(context_raw.font.image); - iron_gpu_clear(0x00000000); + draw_begin(context_raw.font.image, true, 0x00000000); draw_set_font(font, font_size); draw_set_color(0xffffffff); draw_string(text, tex_w / 2 - text_w / 2, tex_w / 2 - text_h / 2); @@ -366,8 +364,7 @@ function util_render_make_brush_preview() { // Scale layer down to to image preview l = render_path_paint_live_layer; let target: iron_gpu_texture_t = context_raw.brush.image; - draw_begin(target); - iron_gpu_clear(0x00000000); + draw_begin(target, true, 0x00000000); draw_set_pipeline(pipes_copy); draw_scaled_image(l.texpaint, 0, 0, target.width, target.height); draw_set_pipeline(null); @@ -403,14 +400,14 @@ function util_render_make_node_preview(canvas: ui_node_canvas_t, node: ui_node_t transform_build_matrix(context_raw.paint_object.base.transform); _gpu_begin(image); - iron_gpu_set_pipeline(res.scon._.pipe_state); + gpu_set_pipeline(res.scon._.pipe_state); let empty: string[] = [""]; uniforms_set_context_consts(res.scon, empty); uniforms_set_obj_consts(res.scon, context_raw.paint_object.base); uniforms_set_material_consts(res.scon, res.mcon); gpu_set_vertex_buffer(util_render_screen_aligned_full_vb); gpu_set_index_buffer(util_render_screen_aligned_full_ib); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); context_raw.paint_object.base.transform.scale_world = _scale_world; @@ -468,12 +465,12 @@ function util_render_create_screen_aligned_full_data() { for (let i: i32 = 0; i < math_floor((vertices.length) / 2); ++i) { buffer_set_i16(vertices, i * 2, data[i]); } - iron_gpu_vertex_buffer_unlock_all(util_render_screen_aligned_full_vb); + iron_gpu_vertex_buffer_unlock(util_render_screen_aligned_full_vb); util_render_screen_aligned_full_ib = gpu_create_index_buffer(indices.length); let id: u32_array_t = gpu_lock_index_buffer(util_render_screen_aligned_full_ib); for (let i: i32 = 0; i < id.length; ++i) { id[i] = indices[i]; } - gpu_index_buffer_unlock_all(util_render_screen_aligned_full_ib); + gpu_index_buffer_unlock(util_render_screen_aligned_full_ib); } diff --git a/base/sources/ts/util_uv.ts b/base/sources/ts/util_uv.ts index 38f18858..9f636af9 100644 --- a/base/sources/ts/util_uv.ts +++ b/base/sources/ts/util_uv.ts @@ -34,8 +34,7 @@ function util_uv_cache_uv_map() { let texa: i16_array_t = mesh.vertex_arrays[2].values; let inda: u32_array_t = mesh.index_arrays[0].values; - draw_begin(util_uv_uvmap); - iron_gpu_clear(0x00000000); + draw_begin(util_uv_uvmap, true, 0x00000000); draw_set_color(0xffffffff); let strength: f32 = res_x > 2048 ? 2.0 : 1.0; let f: f32 = (1 / 32767) * util_uv_uvmap.width; @@ -73,8 +72,7 @@ function util_uv_cache_triangle_map() { let mesh: mesh_data_t = merged; let texa: i16_array_t = mesh.vertex_arrays[2].values; let inda: u32_array_t = mesh.index_arrays[0].values; - draw_begin(util_uv_trianglemap); - iron_gpu_clear(0xff000000); + draw_begin(util_uv_trianglemap, true, 0xff000000); let f: f32 = (1 / 32767) * util_uv_trianglemap.width; let color: i32 = 0xff000001; for (let i: i32 = 0; i < math_floor(inda.length / 3); ++i) { @@ -110,13 +108,9 @@ function util_uv_cache_dilate_map() { util_uv_pipe_dilate.vertex_shader = sys_get_shader("dilate_map.vert"); util_uv_pipe_dilate.fragment_shader = sys_get_shader("dilate_map.frag"); let vs: iron_gpu_vertex_structure_t = gpu_vertex_struct_create(); - ///if (arm_metal || arm_vulkan) - gpu_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM); - ///else gpu_vertex_struct_add(vs, "pos", vertex_data_t.I16_4X_NORM); gpu_vertex_struct_add(vs, "nor", vertex_data_t.I16_2X_NORM); gpu_vertex_struct_add(vs, "tex", vertex_data_t.I16_2X_NORM); - ///end util_uv_pipe_dilate.input_layout = vs; util_uv_pipe_dilate.depth_write = false; util_uv_pipe_dilate.depth_mode = compare_mode_t.ALWAYS; @@ -130,22 +124,11 @@ 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); - iron_gpu_clear(0x00000000); - iron_gpu_set_pipeline(util_uv_pipe_dilate); - ///if (arm_metal || arm_vulkan) - let vs: vertex_element_t[] = [ - { - name: "tex", - data: "short2norm" - } - ]; - gpu_set_vertex_buffer(mesh_data_get(geom, vs)); - ///else + _gpu_begin(util_uv_dilatemap, null, clear_flag_t.COLOR, 0x00000000); + gpu_set_pipeline(util_uv_pipe_dilate); gpu_set_vertex_buffer(geom._.vertex_buffer); - ///end gpu_set_index_buffer(geom._.index_buffers[0]); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); util_uv_dilatemap_cached = true; util_uv_dilate_bytes = null; diff --git a/base/tests/cube/shaders/mesh.kong b/base/tests/cube/shaders/mesh.kong index 2899b83c..5392f8af 100644 --- a/base/tests/cube/shaders/mesh.kong +++ b/base/tests/cube/shaders/mesh.kong @@ -1,15 +1,15 @@ -#[set(everything)] -const my_texture: tex2d; - -#[set(everything)] -const sampler_linear: sampler; - #[set(everything)] const constants: { WVP: float4x4; }; +#[set(everything)] +const sampler_linear: sampler; + +#[set(everything)] +const my_texture: tex2d; + struct vert_in { pos: float4; tex: float2; diff --git a/base/tests/cube/sources/main.ts b/base/tests/cube/sources/main.ts index 6ec1858a..cc6657f7 100644 --- a/base/tests/cube/sources/main.ts +++ b/base/tests/cube/sources/main.ts @@ -19,8 +19,7 @@ function main() { } function render_commands() { - render_path_set_target(""); - render_path_clear_target(0xff6495ed, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); + render_path_set_target("", null, clear_flag_t.COLOR | clear_flag_t.DEPTH, 0xff6495ed, 1.0); render_path_draw_meshes("mesh"); } diff --git a/base/tests/fall/shaders/mesh.kong b/base/tests/fall/shaders/mesh.kong index efe9b7fd..d976aad5 100644 --- a/base/tests/fall/shaders/mesh.kong +++ b/base/tests/fall/shaders/mesh.kong @@ -1,15 +1,15 @@ -#[set(everything)] -const my_texture: tex2d; - -#[set(everything)] -const sampler_linear: sampler; - #[set(everything)] const constants: { WVP: float4x4; }; +#[set(everything)] +const sampler_linear: sampler; + +#[set(everything)] +const my_texture: tex2d; + struct vert_in { pos: float4; nor: float2; @@ -22,7 +22,6 @@ struct vert_out { tex: float2; } - fun mesh_vert(input: vert_in): vert_out { var output: vert_out; output.nor = float3(input.nor.xy, input.pos.w); diff --git a/base/tests/fall/sources/main.ts b/base/tests/fall/sources/main.ts index 33710575..a5efafa8 100644 --- a/base/tests/fall/sources/main.ts +++ b/base/tests/fall/sources/main.ts @@ -23,8 +23,7 @@ function main() { } function render_commands() { - render_path_set_target(""); - render_path_clear_target(0xff6495ed, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); + render_path_set_target("", 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 90ec3047..79eefeef 100644 --- a/base/tests/triangle/main.ts +++ b/base/tests/triangle/main.ts @@ -7,12 +7,11 @@ let vb: any; let ib: any; function render() { - _gpu_begin(null, null); - iron_gpu_clear(0xff000000, 1.0, clear_flag_t.COLOR | clear_flag_t.DEPTH); - iron_gpu_set_pipeline(pipeline); + _gpu_begin(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); - gpu_draw_indexed_vertices(); + gpu_draw(); _gpu_end(); } @@ -55,14 +54,14 @@ function main() { for (let i: i32 = 0; i < vertices.length; i++) { buffer_set_f32(vb_data, i * 4, vertices[i]); } - iron_gpu_vertex_buffer_unlock_all(vb); + iron_gpu_vertex_buffer_unlock(vb); ib = gpu_create_index_buffer(indices.length); let ib_data: u32_array_t = gpu_lock_index_buffer(ib); for (let i: i32 = 0; i < indices.length; i++) { ib_data[i] = indices[i]; } - gpu_index_buffer_unlock_all(ib); + gpu_index_buffer_unlock(ib); _iron_set_update_callback(render); } diff --git a/base/tools/pad/sources/main.ts b/base/tools/pad/sources/main.ts index 1f6ca6bb..1112d3a6 100644 --- a/base/tools/pad/sources/main.ts +++ b/base/tools/pad/sources/main.ts @@ -320,8 +320,7 @@ function draw_minimap() { minimap = gpu_create_render_target(minimap_w, minimap_h); } - draw_begin(minimap); - iron_gpu_clear(theme.SEPARATOR_COL); + draw_begin(minimap, true, theme.SEPARATOR_COL); draw_set_color(0xff333333); let lines: string[] = string_split(storage.text, "\n"); let minimap_full_h: i32 = lines.length * 2;