diff --git a/src/editor/editor.jai b/src/editor/editor.jai index f94928c..e2fde33 100644 --- a/src/editor/editor.jai +++ b/src/editor/editor.jai @@ -1,6 +1,18 @@ #load "console.jai"; #load "trile_editor.jai"; +#load "level_editor.jai"; +#scope_file + +Editor_View :: enum { + Closed_Editor; + Trile_Editor; + Level_Editor; +}; + +current_editor_view : Editor_View = .Trile_Editor; + +#scope_export draw_editor_ui :: (theme: *GR.Overall_Theme) { r := GR.get_rect(0,0,0,0); @@ -10,11 +22,40 @@ draw_editor_ui :: (theme: *GR.Overall_Theme) { r.w = 15*vw; GR.label(r, tprint("Trueno v%.%", V_MAJOR, V_MINOR), *t_label_left(theme)); - draw_trile_editor_ui(theme); + r.x = 100*vw - r.w; + if GR.button(r, "Level editor", *t_button_selectable(theme, current_editor_view == .Level_Editor)) + then current_editor_view = .Level_Editor; + + r.x -= r.w; + if GR.button(r, "Trile editor", *t_button_selectable(theme, current_editor_view == .Trile_Editor)) + then current_editor_view = .Trile_Editor; + + + if current_editor_view == { + case .Trile_Editor; + draw_trile_editor_ui(theme); + case .Level_Editor; + draw_level_editor_ui(theme); + } draw_console(theme); } +draw_editor :: () { + if current_editor_view == { + case .Trile_Editor; + draw_trile_editor(); + case .Level_Editor; + draw_level_editor(); + } + +} + tick_editor_ui :: () { - tick_trile_editor(); + if current_editor_view == { + case .Trile_Editor; + tick_trile_editor(); + case .Level_Editor; + tick_level_editor(); + } tick_console(); } diff --git a/src/editor/level_editor.jai b/src/editor/level_editor.jai new file mode 100644 index 0000000..0519c01 --- /dev/null +++ b/src/editor/level_editor.jai @@ -0,0 +1,45 @@ +#scope_file + +get_level_editor_camera :: () -> Camera { + camera: Camera; + camera.near = 0.1; + camera.far = 100; + + camera.position = .{5,5,5}; + camera.target = .{0,0,0}; + return camera; +} + +#scope_export + +tick_level_editor :: () { + +} + +draw_level_editor :: () { + draw_sky(*get_level_editor_camera()); + cam := get_level_editor_camera(); + mvp := create_viewproj(*cam); + + vs_params : Trile_Vs_Params; + vs_params.mvp = mvp.floats; + + mesh := get_trile("test"); + + sg_apply_pipeline(gPipelines.trile.pipeline); + + + bindings : sg_bindings; + bindings.vertex_buffers[0] = mesh.vertex_buffer; + bindings.vertex_buffers[1] = mesh.normal_buffer; + + sg_apply_bindings(*bindings); + sg_apply_uniforms(UB_trile_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params))})); + + sg_draw(0, cast(s32) mesh.vertex_count, 1); + + +} + +draw_level_editor_ui :: (theme: *GR.Overall_Theme) { +} diff --git a/src/editor/trile_editor.jai b/src/editor/trile_editor.jai index 54e337f..5d08577 100644 --- a/src/editor/trile_editor.jai +++ b/src/editor/trile_editor.jai @@ -45,7 +45,7 @@ current_trile : Trile; apply_tool_to_trixel :: (x: s64, y: s64, z: s64) { //temptest: print("Testing meshgen!\n"); - generate_mesh_matias(*current_trile); + set_trile("test", generate_mesh_matias(*current_trile)); if current_tool == .PAINT { current_trile.trixels[x][y][z].material.color = current_color; @@ -222,21 +222,9 @@ draw_material_tab :: (theme: *GR.Overall_Theme, area: GR.Rect) { } -draw_sky :: () { - cam := get_trile_editor_camera(); - - mvp := create_viewproj(*cam); - vs_params : Sky_Vs_Params; - vs_params.mvp = mvp.floats; - sg_apply_pipeline(gPipelines.sky.pipeline); - sg_apply_bindings(*gPipelines.sky.bind); - sg_apply_uniforms(UB_sky_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) })); - sg_draw(0, 36, 1); - -} draw_trile_editor :: () { - draw_sky(); + draw_sky(*get_trile_editor_camera()); draw_trile(); } diff --git a/src/main.jai b/src/main.jai index 8c08189..a08acf7 100644 --- a/src/main.jai +++ b/src/main.jai @@ -6,6 +6,7 @@ stbi :: #import "stb_image"; #load "trile.jai"; +#load "rendering/rendering.jai"; #load "ui/ui.jai"; #load "editor/editor.jai"; #load "pipelines.jai"; @@ -114,7 +115,7 @@ frame :: () { sg_begin_pass(*(sg_pass.{ action = state.pass_action_clear, swapchain = cast,force(sg_swapchain) sglue_swapchain() })); tick_ui(); - draw_trile_editor(); + draw_editor(); ui_pass(); sg_end_pass(); sg_commit(); diff --git a/src/meshgen.jai b/src/meshgen.jai index cfe16e9..6b9cc26 100644 --- a/src/meshgen.jai +++ b/src/meshgen.jai @@ -394,9 +394,9 @@ generate_optimized_quad_mesh :: (trilept: *Trile, vecs: *[..]float, normals: *[. } Pool :: #import "Pool"; -meshgenpool : Pool.Pool; -generate_mesh_matias :: (trileptr : *Trile) { +generate_mesh_matias :: (trileptr : *Trile) -> Mesh { + meshgenpool : Pool.Pool; Pool.set_allocators(*meshgenpool); new_context := context; new_context.allocator = .{Pool.pool_allocator_proc, *meshgenpool}; @@ -410,10 +410,18 @@ generate_mesh_matias :: (trileptr : *Trile) { print("Quad vecs size: %\n", quadVecs.count); quads_to_triangles(*quadVecs, *triangleVecs,*quadNorms, *triangleNorms); + + trile_vert_buffer_info := sg_buffer_desc.{ data = .{ ptr = triangleVecs.data, size = xx (triangleVecs.count * 4) } }; + trile_vert_buffer := sg_make_buffer(*trile_vert_buffer_info); + + trile_normal_buffer_info := sg_buffer_desc.{ data = .{ ptr = triangleNorms.data, size = xx (triangleNorms.count * 4) } }; + trile_normal_buffer := sg_make_buffer(*trile_normal_buffer_info); print("Triangle vecs size: %\n", triangleVecs.count); + Pool.reset(*meshgenpool); + + return .{ trile_vert_buffer, trile_normal_buffer, triangleVecs.count / 3 }; } - Pool.reset(*meshgenpool); } /* diff --git a/src/pipelines.jai b/src/pipelines.jai index a8ae35b..eebb501 100644 --- a/src/pipelines.jai +++ b/src/pipelines.jai @@ -1,6 +1,7 @@ #load "./shaders/jai/shader_triangle.jai"; #load "./shaders/jai/shader_trixel.jai"; #load "./shaders/jai/shader_sky.jai"; +#load "./shaders/jai/shader_trile.jai"; Pipeline_Binding :: struct { pipeline : sg_pipeline; @@ -20,11 +21,14 @@ gPipelines : struct { // Sky rendering. sky : Pipeline_Binding; + trile : Pipeline_Binding; + } create_pipelines :: () { create_arbtri_pipeline(); create_trixel_pipeline(); + create_trile_pipeline(); create_sky_pipeline(); } @@ -153,6 +157,36 @@ create_trixel_pipeline :: () { gPipelines.trixel.bind.vertex_buffers[2] = sg_make_buffer(*instance_buffer); } +create_trile_pipeline :: () { + pipeline: sg_pipeline_desc; + shader_desc := trixel_shader_desc(sg_query_backend()); + pipeline.shader = sg_make_shader(*shader_desc); + pipeline.layout.buffers[0].stride = 4*3; + pipeline.layout.buffers[1].stride = 4*3; + + pipeline.layout.attrs[ATTR_trile_position] = .{ format = .FLOAT3, buffer_index = 0 }; + pipeline.layout.attrs[ATTR_trile_normal] = .{ format = .FLOAT3, buffer_index = 1 }; + pipeline.depth = .{ + write_enabled = true, + compare = .LESS_EQUAL, + }; + + color_state := sg_color_target_state.{ + pixel_format = .RGBA8, + blend = .{ + enabled = true, + src_factor_rgb = .SRC_ALPHA, + dst_factor_rgb = .ONE_MINUS_SRC_ALPHA + } + }; + + + pipeline.color_count = 1; + pipeline.colors[0] = color_state; + + gPipelines.trile.pipeline = sg_make_pipeline(*pipeline); +} + create_sky_pipeline :: () { pipeline: sg_pipeline_desc; shader_desc := sky_shader_desc(sg_query_backend()); diff --git a/src/rendering/rendering.jai b/src/rendering/rendering.jai new file mode 100644 index 0000000..8d2daa4 --- /dev/null +++ b/src/rendering/rendering.jai @@ -0,0 +1 @@ +#load "sky.jai"; diff --git a/src/rendering/sky.jai b/src/rendering/sky.jai new file mode 100644 index 0000000..c49ab62 --- /dev/null +++ b/src/rendering/sky.jai @@ -0,0 +1,9 @@ +draw_sky :: (cam: *Camera) { + mvp := create_viewproj(cam); + vs_params : Sky_Vs_Params; + vs_params.mvp = mvp.floats; + sg_apply_pipeline(gPipelines.sky.pipeline); + sg_apply_bindings(*gPipelines.sky.bind); + sg_apply_uniforms(UB_sky_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) })); + sg_draw(0, 36, 1); +} diff --git a/src/shaders/jai/shader_trile.jai b/src/shaders/jai/shader_trile.jai new file mode 100644 index 0000000..6f160e7 --- /dev/null +++ b/src/shaders/jai/shader_trile.jai @@ -0,0 +1,320 @@ +/* + #version:1# (machine generated, don't edit!) + + Generated by sokol-shdc (https://github.com/floooh/sokol-tools) + + Cmdline: + sokol-shdc -i shader_trile.glsl -o ./jai/shader_trile.jai -l glsl430:glsl300es:metal_macos -f sokol_jai + + Overview: + ========= + Shader program: 'trile': + Get shader desc: trile_shader_desc(sg_query_backend()) + Vertex Shader: vs_trile + Fragment Shader: fs_trile + Attributes: + ATTR_trile_position => 0 + ATTR_trile_normal => 1 + Bindings: + Uniform block 'trile_vs_params': + Jai struct: Trile_Vs_Params + Bind slot: UB_trile_vs_params => 0 +*/ +ATTR_trile_position :: 0; +ATTR_trile_normal :: 1; +UB_trile_vs_params :: 0; +Trile_Vs_Params :: struct { + mvp: [16]float; +}; +/* + #version 430 + + uniform vec4 trile_vs_params[4]; + layout(location = 0) in vec4 position; + layout(location = 0) out vec4 fnormal; + layout(location = 1) in vec4 normal; + + void main() + { + gl_Position = mat4(trile_vs_params[0], trile_vs_params[1], trile_vs_params[2], trile_vs_params[3]) * vec4(position.xyz, 1.0); + fnormal = normal; + } + +*/ +vs_trile_source_glsl430 := u8.[ + 0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x34,0x33,0x30,0x0a,0x0a,0x75,0x6e, + 0x69,0x66,0x6f,0x72,0x6d,0x20,0x76,0x65,0x63,0x34,0x20,0x74,0x72,0x69,0x6c,0x65, + 0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x34,0x5d,0x3b,0x0a,0x6c, + 0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d, + 0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x70,0x6f,0x73,0x69, + 0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63, + 0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x76, + 0x65,0x63,0x34,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x6c,0x61,0x79, + 0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x31, + 0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x6e,0x6f,0x72,0x6d,0x61,0x6c, + 0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b, + 0x0a,0x20,0x20,0x20,0x20,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e, + 0x20,0x3d,0x20,0x6d,0x61,0x74,0x34,0x28,0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73, + 0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x30,0x5d,0x2c,0x20,0x74,0x72,0x69,0x6c, + 0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x31,0x5d,0x2c,0x20, + 0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b, + 0x32,0x5d,0x2c,0x20,0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72, + 0x61,0x6d,0x73,0x5b,0x33,0x5d,0x29,0x20,0x2a,0x20,0x76,0x65,0x63,0x34,0x28,0x70, + 0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x2e,0x78,0x79,0x7a,0x2c,0x20,0x31,0x2e,0x30, + 0x29,0x3b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x20,0x3d, + 0x20,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x7d,0x0a,0x0a,0x00, +]; +/* + #version 430 + + layout(location = 0) out vec4 frag_color; + layout(location = 0) in vec4 fnormal; + + void main() + { + frag_color = vec4(fnormal.xyz, 1.0); + } + +*/ +fs_trile_source_glsl430 := u8.[ + 0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x34,0x33,0x30,0x0a,0x0a,0x6c,0x61, + 0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20, + 0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x76,0x65,0x63,0x34,0x20,0x66,0x72,0x61,0x67, + 0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c, + 0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20, + 0x76,0x65,0x63,0x34,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x0a,0x76, + 0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20, + 0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65, + 0x63,0x34,0x28,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x2e,0x78,0x79,0x7a,0x2c,0x20, + 0x31,0x2e,0x30,0x29,0x3b,0x0a,0x7d,0x0a,0x0a,0x00, +]; +/* + #version 300 es + + uniform vec4 trile_vs_params[4]; + layout(location = 0) in vec4 position; + out vec4 fnormal; + layout(location = 1) in vec4 normal; + + void main() + { + gl_Position = mat4(trile_vs_params[0], trile_vs_params[1], trile_vs_params[2], trile_vs_params[3]) * vec4(position.xyz, 1.0); + fnormal = normal; + } + +*/ +vs_trile_source_glsl300es := u8.[ + 0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a, + 0x0a,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x20,0x76,0x65,0x63,0x34,0x20,0x74,0x72, + 0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x34,0x5d, + 0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f, + 0x6e,0x20,0x3d,0x20,0x30,0x29,0x20,0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x70, + 0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x6f,0x75,0x74,0x20,0x76,0x65,0x63, + 0x34,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x6c,0x61,0x79,0x6f,0x75, + 0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x31,0x29,0x20, + 0x69,0x6e,0x20,0x76,0x65,0x63,0x34,0x20,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a, + 0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20, + 0x20,0x20,0x20,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3d, + 0x20,0x6d,0x61,0x74,0x34,0x28,0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70, + 0x61,0x72,0x61,0x6d,0x73,0x5b,0x30,0x5d,0x2c,0x20,0x74,0x72,0x69,0x6c,0x65,0x5f, + 0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x31,0x5d,0x2c,0x20,0x74,0x72, + 0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x5b,0x32,0x5d, + 0x2c,0x20,0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d, + 0x73,0x5b,0x33,0x5d,0x29,0x20,0x2a,0x20,0x76,0x65,0x63,0x34,0x28,0x70,0x6f,0x73, + 0x69,0x74,0x69,0x6f,0x6e,0x2e,0x78,0x79,0x7a,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b, + 0x0a,0x20,0x20,0x20,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x20,0x3d,0x20,0x6e, + 0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x7d,0x0a,0x0a,0x00, +]; +/* + #version 300 es + precision mediump float; + precision highp int; + + layout(location = 0) out highp vec4 frag_color; + in highp vec4 fnormal; + + void main() + { + frag_color = vec4(fnormal.xyz, 1.0); + } + +*/ +fs_trile_source_glsl300es := u8.[ + 0x23,0x76,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x33,0x30,0x30,0x20,0x65,0x73,0x0a, + 0x70,0x72,0x65,0x63,0x69,0x73,0x69,0x6f,0x6e,0x20,0x6d,0x65,0x64,0x69,0x75,0x6d, + 0x70,0x20,0x66,0x6c,0x6f,0x61,0x74,0x3b,0x0a,0x70,0x72,0x65,0x63,0x69,0x73,0x69, + 0x6f,0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x69,0x6e,0x74,0x3b,0x0a,0x0a,0x6c, + 0x61,0x79,0x6f,0x75,0x74,0x28,0x6c,0x6f,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x3d, + 0x20,0x30,0x29,0x20,0x6f,0x75,0x74,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65, + 0x63,0x34,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x69, + 0x6e,0x20,0x68,0x69,0x67,0x68,0x70,0x20,0x76,0x65,0x63,0x34,0x20,0x66,0x6e,0x6f, + 0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x0a,0x76,0x6f,0x69,0x64,0x20,0x6d,0x61,0x69,0x6e, + 0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f, + 0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65,0x63,0x34,0x28,0x66,0x6e,0x6f,0x72,0x6d, + 0x61,0x6c,0x2e,0x78,0x79,0x7a,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x7d,0x0a, + 0x0a,0x00, +]; +/* + #include + #include + + using namespace metal; + + struct trile_vs_params + { + float4x4 mvp; + }; + + struct main0_out + { + float4 fnormal [[user(locn0)]]; + float4 gl_Position [[position]]; + }; + + struct main0_in + { + float4 position [[attribute(0)]]; + float4 normal [[attribute(1)]]; + }; + + vertex main0_out main0(main0_in in [[stage_in]], constant trile_vs_params& _19 [[buffer(0)]]) + { + main0_out out = {}; + out.gl_Position = _19.mvp * float4(in.position.xyz, 1.0); + out.fnormal = in.normal; + return out; + } + +*/ +vs_trile_source_metal_macos := u8.[ + 0x23,0x69,0x6e,0x63,0x6c,0x75,0x64,0x65,0x20,0x3c,0x6d,0x65,0x74,0x61,0x6c,0x5f, + 0x73,0x74,0x64,0x6c,0x69,0x62,0x3e,0x0a,0x23,0x69,0x6e,0x63,0x6c,0x75,0x64,0x65, + 0x20,0x3c,0x73,0x69,0x6d,0x64,0x2f,0x73,0x69,0x6d,0x64,0x2e,0x68,0x3e,0x0a,0x0a, + 0x75,0x73,0x69,0x6e,0x67,0x20,0x6e,0x61,0x6d,0x65,0x73,0x70,0x61,0x63,0x65,0x20, + 0x6d,0x65,0x74,0x61,0x6c,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x74, + 0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73,0x0a,0x7b, + 0x0a,0x20,0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x78,0x34,0x20,0x6d,0x76, + 0x70,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x6d,0x61, + 0x69,0x6e,0x30,0x5f,0x6f,0x75,0x74,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6c, + 0x6f,0x61,0x74,0x34,0x20,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x20,0x5b,0x5b,0x75, + 0x73,0x65,0x72,0x28,0x6c,0x6f,0x63,0x6e,0x30,0x29,0x5d,0x5d,0x3b,0x0a,0x20,0x20, + 0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69, + 0x74,0x69,0x6f,0x6e,0x20,0x5b,0x5b,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x5d, + 0x5d,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x6d,0x61, + 0x69,0x6e,0x30,0x5f,0x69,0x6e,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6c,0x6f, + 0x61,0x74,0x34,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x5b,0x5b,0x61, + 0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x28,0x30,0x29,0x5d,0x5d,0x3b,0x0a,0x20, + 0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x6e,0x6f,0x72,0x6d,0x61,0x6c, + 0x20,0x5b,0x5b,0x61,0x74,0x74,0x72,0x69,0x62,0x75,0x74,0x65,0x28,0x31,0x29,0x5d, + 0x5d,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x76,0x65,0x72,0x74,0x65,0x78,0x20,0x6d,0x61, + 0x69,0x6e,0x30,0x5f,0x6f,0x75,0x74,0x20,0x6d,0x61,0x69,0x6e,0x30,0x28,0x6d,0x61, + 0x69,0x6e,0x30,0x5f,0x69,0x6e,0x20,0x69,0x6e,0x20,0x5b,0x5b,0x73,0x74,0x61,0x67, + 0x65,0x5f,0x69,0x6e,0x5d,0x5d,0x2c,0x20,0x63,0x6f,0x6e,0x73,0x74,0x61,0x6e,0x74, + 0x20,0x74,0x72,0x69,0x6c,0x65,0x5f,0x76,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,0x73, + 0x26,0x20,0x5f,0x31,0x39,0x20,0x5b,0x5b,0x62,0x75,0x66,0x66,0x65,0x72,0x28,0x30, + 0x29,0x5d,0x5d,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x6d,0x61,0x69,0x6e,0x30, + 0x5f,0x6f,0x75,0x74,0x20,0x6f,0x75,0x74,0x20,0x3d,0x20,0x7b,0x7d,0x3b,0x0a,0x20, + 0x20,0x20,0x20,0x6f,0x75,0x74,0x2e,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69, + 0x6f,0x6e,0x20,0x3d,0x20,0x5f,0x31,0x39,0x2e,0x6d,0x76,0x70,0x20,0x2a,0x20,0x66, + 0x6c,0x6f,0x61,0x74,0x34,0x28,0x69,0x6e,0x2e,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f, + 0x6e,0x2e,0x78,0x79,0x7a,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x20,0x20,0x20, + 0x20,0x6f,0x75,0x74,0x2e,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x20,0x3d,0x20,0x69, + 0x6e,0x2e,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x3b,0x0a,0x20,0x20,0x20,0x20,0x72,0x65, + 0x74,0x75,0x72,0x6e,0x20,0x6f,0x75,0x74,0x3b,0x0a,0x7d,0x0a,0x0a,0x00, +]; +/* + #include + #include + + using namespace metal; + + struct main0_out + { + float4 frag_color [[color(0)]]; + }; + + struct main0_in + { + float4 fnormal [[user(locn0)]]; + }; + + fragment main0_out main0(main0_in in [[stage_in]]) + { + main0_out out = {}; + out.frag_color = float4(in.fnormal.xyz, 1.0); + return out; + } + +*/ +fs_trile_source_metal_macos := u8.[ + 0x23,0x69,0x6e,0x63,0x6c,0x75,0x64,0x65,0x20,0x3c,0x6d,0x65,0x74,0x61,0x6c,0x5f, + 0x73,0x74,0x64,0x6c,0x69,0x62,0x3e,0x0a,0x23,0x69,0x6e,0x63,0x6c,0x75,0x64,0x65, + 0x20,0x3c,0x73,0x69,0x6d,0x64,0x2f,0x73,0x69,0x6d,0x64,0x2e,0x68,0x3e,0x0a,0x0a, + 0x75,0x73,0x69,0x6e,0x67,0x20,0x6e,0x61,0x6d,0x65,0x73,0x70,0x61,0x63,0x65,0x20, + 0x6d,0x65,0x74,0x61,0x6c,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x6d, + 0x61,0x69,0x6e,0x30,0x5f,0x6f,0x75,0x74,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66, + 0x6c,0x6f,0x61,0x74,0x34,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72, + 0x20,0x5b,0x5b,0x63,0x6f,0x6c,0x6f,0x72,0x28,0x30,0x29,0x5d,0x5d,0x3b,0x0a,0x7d, + 0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x6d,0x61,0x69,0x6e,0x30,0x5f, + 0x69,0x6e,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20, + 0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c,0x20,0x5b,0x5b,0x75,0x73,0x65,0x72,0x28,0x6c, + 0x6f,0x63,0x6e,0x30,0x29,0x5d,0x5d,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x66,0x72,0x61, + 0x67,0x6d,0x65,0x6e,0x74,0x20,0x6d,0x61,0x69,0x6e,0x30,0x5f,0x6f,0x75,0x74,0x20, + 0x6d,0x61,0x69,0x6e,0x30,0x28,0x6d,0x61,0x69,0x6e,0x30,0x5f,0x69,0x6e,0x20,0x69, + 0x6e,0x20,0x5b,0x5b,0x73,0x74,0x61,0x67,0x65,0x5f,0x69,0x6e,0x5d,0x5d,0x29,0x0a, + 0x7b,0x0a,0x20,0x20,0x20,0x20,0x6d,0x61,0x69,0x6e,0x30,0x5f,0x6f,0x75,0x74,0x20, + 0x6f,0x75,0x74,0x20,0x3d,0x20,0x7b,0x7d,0x3b,0x0a,0x20,0x20,0x20,0x20,0x6f,0x75, + 0x74,0x2e,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x66, + 0x6c,0x6f,0x61,0x74,0x34,0x28,0x69,0x6e,0x2e,0x66,0x6e,0x6f,0x72,0x6d,0x61,0x6c, + 0x2e,0x78,0x79,0x7a,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x20,0x20,0x20,0x20, + 0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x75,0x74,0x3b,0x0a,0x7d,0x0a,0x0a,0x00, + +]; +trile_shader_desc :: (backend: sg_backend) -> sg_shader_desc { + desc: sg_shader_desc; + desc.label = "trile_shader"; + if backend == { + case .GLCORE; + desc.vertex_func.source = xx *vs_trile_source_glsl430; + desc.vertex_func.entry = "main"; + desc.fragment_func.source = xx *fs_trile_source_glsl430; + desc.fragment_func.entry = "main"; + desc.attrs[0].base_type = .FLOAT; + desc.attrs[0].glsl_name = "position"; + desc.attrs[1].base_type = .FLOAT; + desc.attrs[1].glsl_name = "normal"; + desc.uniform_blocks[0].stage = .VERTEX; + desc.uniform_blocks[0].layout = .STD140; + desc.uniform_blocks[0].size = 64; + desc.uniform_blocks[0].glsl_uniforms[0].type = .FLOAT4; + desc.uniform_blocks[0].glsl_uniforms[0].array_count = 4; + desc.uniform_blocks[0].glsl_uniforms[0].glsl_name = "trile_vs_params"; + case .GLES3; + desc.vertex_func.source = xx *vs_trile_source_glsl300es; + desc.vertex_func.entry = "main"; + desc.fragment_func.source = xx *fs_trile_source_glsl300es; + desc.fragment_func.entry = "main"; + desc.attrs[0].base_type = .FLOAT; + desc.attrs[0].glsl_name = "position"; + desc.attrs[1].base_type = .FLOAT; + desc.attrs[1].glsl_name = "normal"; + desc.uniform_blocks[0].stage = .VERTEX; + desc.uniform_blocks[0].layout = .STD140; + desc.uniform_blocks[0].size = 64; + desc.uniform_blocks[0].glsl_uniforms[0].type = .FLOAT4; + desc.uniform_blocks[0].glsl_uniforms[0].array_count = 4; + desc.uniform_blocks[0].glsl_uniforms[0].glsl_name = "trile_vs_params"; + case .METAL_MACOS; + desc.vertex_func.source = xx *vs_trile_source_metal_macos; + desc.vertex_func.entry = "main0"; + desc.fragment_func.source = xx *fs_trile_source_metal_macos; + desc.fragment_func.entry = "main0"; + desc.attrs[0].base_type = .FLOAT; + desc.attrs[1].base_type = .FLOAT; + desc.uniform_blocks[0].stage = .VERTEX; + desc.uniform_blocks[0].layout = .STD140; + desc.uniform_blocks[0].size = 64; + desc.uniform_blocks[0].msl_buffer_n = 0; + } + return desc; +} diff --git a/src/shaders/shader_trile.glsl b/src/shaders/shader_trile.glsl new file mode 100644 index 0000000..816813d --- /dev/null +++ b/src/shaders/shader_trile.glsl @@ -0,0 +1,32 @@ +@module bla + +@vs vs_trile + +in vec4 position; +in vec4 normal; + +layout(binding=0) uniform trile_vs_params { + mat4 mvp; +}; + + +out vec4 fnormal; + +void main() { + gl_Position = mvp * vec4(position.xyz, 1.0); + fnormal = normal; +} +@end + +@fs fs_trile + +in vec4 fnormal; +out vec4 frag_color; + + +void main() { + frag_color = vec4(fnormal.xyz, 1.0); +} +@end + +@program trile vs_trile fs_trile diff --git a/src/trile.jai b/src/trile.jai index 79b5fcd..725999f 100644 --- a/src/trile.jai +++ b/src/trile.jai @@ -1,5 +1,28 @@ #load "meshgen.jai"; +#scope_file + +trile_table : Table(string, Mesh); + +#scope_export + +Mesh :: struct { + vertex_buffer : sg_buffer; + normal_buffer : sg_buffer; + vertex_count : s64; +}; + +get_trile :: (name: string) -> (Mesh, success: bool) { + success, value := table_find(*trile_table, name); + return success, value; +} + +set_trile :: (name: string, meshptr: Mesh) { + // @ToDo: free old mesh... + // @ToDo: consider if string should be copied.. + table_set(*trile_table, name, meshptr); +} + Material :: struct { roughness : float = 0.5; metallic : float = 0;