diff --git a/engine/rend/shaders/lit_mesh.frag.hlsl b/engine/rend/shaders/lit_mesh.frag.hlsl index fdcc1e2..ec0888f 100644 --- a/engine/rend/shaders/lit_mesh.frag.hlsl +++ b/engine/rend/shaders/lit_mesh.frag.hlsl @@ -59,9 +59,11 @@ float4 main( float3 Normal: TEXCOORD2 ) : SV_Target0 { + float3 lightPos = float3(0, 20 + -4, -5); float3 lightColor = float3(0.8, 0.8, 0.5); - float3 ambient = float3(0.01, 0.01, 0.003) * 0.2; + + float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) ); float4 s = Texture.Sample(Sampler, UV); float alpha = s.w; diff --git a/engine/rend/src/sgpu/DebugDrawSystem.zig b/engine/rend/src/sgpu/DebugDrawSystem.zig new file mode 100644 index 0000000..371baad --- /dev/null +++ b/engine/rend/src/sgpu/DebugDrawSystem.zig @@ -0,0 +1,98 @@ +pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This()); + +allocator: std.mem.Allocator, +device: *gpu.GPUDevice = undefined, + +const assetReferences = [_]assets.AssetImportReference{ + assets.MakeImportRefOptions( + "Mesh", + "m_debug_box", + .{ .path = "embedded:debug_box.obj" }, + ), + assets.MakeImportRefOptions( + "Mesh", + "m_debug_line", + .{ .path = "embedded:debug_line.obj" }, + ), + assets.MakeImportRefOptions( + "Mesh", + "m_debug_sphere", + .{ .path = "embedded:debug_sphere.obj" }, + ), +}; + +pub fn create(allocator: std.mem.Allocator) !*@This() { + const self = try allocator.create(@This()); + self.* = .{ + .allocator = allocator, + }; + core.engine_logs("[DebugDrawSystem] starting up..."); + + _ = try core.fs().installFileBytesMount("embedded:debug_box.obj", @constCast(&primitive_box), true); + _ = try core.fs().installFileBytesMount("embedded:debug_line.obj", @constCast(&primitive_line), true); + _ = try core.fs().installFileBytesMount("embedded:debug_sphere.obj", @constCast(&primitive_sphere), true); + + try assets.loadList(assetReferences); + + return self; +} + +pub fn setup(self: *@This(), device: *gpu.GPUDevice) !void { + self.device = device; +} + +pub fn destroy(self: *@This()) void { + self.allocator.destroy(self); +} + +pub fn debugSphere(position: core.Vectorf, radius: f32, params: DebugDrawParams) void { + _ = position; + _ = radius; + _ = params; + // gDebugDrawSys.debugDraws.push(.{ + // .primitive = .{ .sphere = .{ .position = position, .radius = radius, .rotation = params.rotation } }, + // .color = params.color, + // .duration = params.duration, + // }) catch return; +} + +pub fn debugLine(start: core.Vectorf, end: core.Vectorf, params: DebugDrawParams) void { + _ = start; + _ = end; + _ = params; + // gDebugDrawSys.debugDraws.push(.{ + // .primitive = .{ .line = .{ + // .start = start, + // .end = end, + // } }, + // .color = params.color, + // .duration = params.duration, + // }) catch return; +} + +pub fn debugBox(position: core.Vectorf, extents: core.Vectorf, params: DebugDrawParams) void { + _ = position; + _ = extents; + _ = params; + // gDebugDrawSys.debugDraws.push(.{ + // .primitive = .{ + // .box = .{ .position = position, .extents = extents, .rotation = params.rotation }, + // }, + // .color = params.color, + // .duration = params.duration, + // }) catch return; +} + +const DebugDrawParams = core.DebugDrawParams; + +const core = @import("core"); +const rend = @import("../rend.zig"); +const assets = @import("assets"); +const texture = rend.texture; +const sdl3 = @import("sdl3"); +const gpu = sdl3.gpu; +const std = @import("std"); + +const primitive_box align(8) = @embedFile("embedded/primitive_box.obj").*; +const primitive_line align(8) = @embedFile("embedded/primitive_line.obj").*; +const primitive_sphere align(8) = @embedFile("embedded/primitive_sphere.obj").*; diff --git a/engine/rend/src/sgpu/embedded/primitive_box.obj b/engine/rend/src/sgpu/embedded/primitive_box.obj new file mode 100644 index 0000000..a23b13b --- /dev/null +++ b/engine/rend/src/sgpu/embedded/primitive_box.obj @@ -0,0 +1,40 @@ +# Blender v3.1.2 OBJ File: '' +# www.blender.org +mtllib primitive_box.mtl +o Cube +v 1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +vt 0.625000 0.500000 +vt 0.875000 0.500000 +vt 0.875000 0.750000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.625000 1.000000 +vt 0.375000 1.000000 +vt 0.375000 0.000000 +vt 0.625000 0.000000 +vt 0.625000 0.250000 +vt 0.375000 0.250000 +vt 0.125000 0.500000 +vt 0.375000 0.500000 +vt 0.125000 0.750000 +vn 0.0000 1.0000 0.0000 +vn 0.0000 0.0000 1.0000 +vn -1.0000 0.0000 0.0000 +vn 0.0000 -1.0000 0.0000 +vn 1.0000 0.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +usemtl Material +s off +f 1/1/1 5/2/1 7/3/1 3/4/1 +f 4/5/2 3/4/2 7/6/2 8/7/2 +f 8/8/3 7/9/3 5/10/3 6/11/3 +f 6/12/4 2/13/4 4/5/4 8/14/4 +f 2/13/5 1/1/5 3/4/5 4/5/5 +f 6/11/6 5/10/6 1/1/6 2/13/6 diff --git a/engine/rend/src/sgpu/embedded/primitive_line.obj b/engine/rend/src/sgpu/embedded/primitive_line.obj new file mode 100644 index 0000000..e9c3ea3 --- /dev/null +++ b/engine/rend/src/sgpu/embedded/primitive_line.obj @@ -0,0 +1,14 @@ +# Blender v3.1.2 OBJ File: '' +# www.blender.org +mtllib primitive_line.mtl +o Plane +v 1.000000 -0.000001 -0.000001 +v -0.000000 0.000001 0.000000 +v 1.000000 -0.000001 -0.000001 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vn 0.0000 0.0000 1.0000 +usemtl None +s off +f 2/1/1 3/2/1 1/3/1 diff --git a/engine/rend/src/sgpu/embedded/primitive_sphere.obj b/engine/rend/src/sgpu/embedded/primitive_sphere.obj new file mode 100644 index 0000000..1e50f95 --- /dev/null +++ b/engine/rend/src/sgpu/embedded/primitive_sphere.obj @@ -0,0 +1,271 @@ +# Blender v3.1.2 OBJ File: '' +# www.blender.org +mtllib primitive_sphere.mtl +o Sphere +v 0.000000 0.923880 -0.382683 +v 0.000000 0.707107 -0.707107 +v 0.000000 0.382683 -0.923880 +v 0.000000 -0.000000 -1.000000 +v 0.000000 -0.382683 -0.923880 +v 0.000000 -0.707107 -0.707107 +v 0.000000 -0.923880 -0.382683 +v 0.270598 0.923880 -0.270598 +v 0.500000 0.707107 -0.500000 +v 0.653282 0.382683 -0.653281 +v 0.707107 -0.000000 -0.707107 +v 0.653282 -0.382683 -0.653282 +v 0.500000 -0.707107 -0.500000 +v 0.270598 -0.923880 -0.270598 +v 0.382684 0.923880 0.000000 +v 0.707107 0.707107 0.000000 +v 0.923880 0.382683 0.000000 +v 1.000000 -0.000000 0.000000 +v 0.923880 -0.382683 0.000000 +v 0.707107 -0.707107 0.000000 +v 0.382684 -0.923880 0.000000 +v 0.270598 0.923880 0.270598 +v 0.500000 0.707107 0.500000 +v 0.653282 0.382683 0.653282 +v 0.707107 -0.000000 0.707107 +v 0.653282 -0.382683 0.653282 +v 0.500000 -0.707107 0.500000 +v 0.270598 -0.923880 0.270598 +v 0.000000 0.923880 0.382684 +v 0.000000 0.707107 0.707107 +v 0.000000 0.382683 0.923880 +v 0.000000 -0.000000 1.000000 +v 0.000000 -0.382683 0.923880 +v 0.000000 -0.707107 0.707107 +v 0.000000 -0.923880 0.382684 +v -0.000000 1.000000 0.000000 +v -0.270598 0.923880 0.270598 +v -0.500000 0.707107 0.500000 +v -0.653281 0.382683 0.653282 +v -0.707107 -0.000000 0.707107 +v -0.653282 -0.382683 0.653282 +v -0.500000 -0.707107 0.500000 +v -0.270598 -0.923880 0.270598 +v -0.382684 0.923880 0.000000 +v -0.707107 0.707107 0.000000 +v -0.923879 0.382683 0.000000 +v -1.000000 -0.000000 0.000000 +v -0.923880 -0.382683 0.000000 +v -0.707107 -0.707107 0.000000 +v -0.382684 -0.923880 0.000000 +v 0.000000 -1.000000 0.000000 +v -0.270598 0.923880 -0.270598 +v -0.500000 0.707107 -0.500000 +v -0.653281 0.382683 -0.653281 +v -0.707107 -0.000000 -0.707107 +v -0.653282 -0.382683 -0.653281 +v -0.500000 -0.707107 -0.500000 +v -0.270598 -0.923880 -0.270598 +vt 0.750000 0.375000 +vt 0.750000 0.500000 +vt 0.625000 0.500000 +vt 0.625000 0.375000 +vt 0.750000 0.625000 +vt 0.750000 0.750000 +vt 0.625000 0.750000 +vt 0.625000 0.625000 +vt 0.750000 0.875000 +vt 0.687500 1.000000 +vt 0.625000 0.875000 +vt 0.687500 0.000000 +vt 0.750000 0.125000 +vt 0.625000 0.125000 +vt 0.750000 0.250000 +vt 0.625000 0.250000 +vt 0.562500 0.000000 +vt 0.500000 0.125000 +vt 0.500000 0.375000 +vt 0.500000 0.250000 +vt 0.500000 0.625000 +vt 0.500000 0.500000 +vt 0.500000 0.875000 +vt 0.500000 0.750000 +vt 0.562500 1.000000 +vt 0.375000 0.875000 +vt 0.375000 0.750000 +vt 0.375000 0.250000 +vt 0.375000 0.125000 +vt 0.375000 0.500000 +vt 0.375000 0.375000 +vt 0.375000 0.625000 +vt 0.437500 1.000000 +vt 0.437500 0.000000 +vt 0.250000 0.250000 +vt 0.250000 0.125000 +vt 0.250000 0.500000 +vt 0.250000 0.375000 +vt 0.250000 0.750000 +vt 0.250000 0.625000 +vt 0.312500 1.000000 +vt 0.250000 0.875000 +vt 0.312500 0.000000 +vt 0.187500 1.000000 +vt 0.125000 0.875000 +vt 0.187500 0.000000 +vt 0.125000 0.125000 +vt 0.125000 0.375000 +vt 0.125000 0.250000 +vt 0.125000 0.625000 +vt 0.125000 0.500000 +vt 0.125000 0.750000 +vt 0.000000 0.375000 +vt 0.000000 0.250000 +vt 0.000000 0.625000 +vt 0.000000 0.500000 +vt 0.000000 0.875000 +vt 0.000000 0.750000 +vt 0.000000 0.125000 +vt 0.062500 1.000000 +vt 0.062500 0.000000 +vt 1.000000 0.125000 +vt 1.000000 0.250000 +vt 0.875000 0.250000 +vt 0.875000 0.125000 +vt 1.000000 0.375000 +vt 1.000000 0.500000 +vt 0.875000 0.500000 +vt 0.875000 0.375000 +vt 1.000000 0.625000 +vt 1.000000 0.750000 +vt 0.875000 0.750000 +vt 0.875000 0.625000 +vt 1.000000 0.875000 +vt 0.937500 1.000000 +vt 0.875000 0.875000 +vt 0.937500 0.000000 +vt 0.812500 1.000000 +vt 0.812500 0.000000 +vn 0.3764 -0.1807 -0.9087 +vn 0.3256 0.5253 -0.7862 +vn 0.0805 0.9776 -0.1945 +vn 0.0805 -0.9776 -0.1945 +vn 0.3256 -0.5253 -0.7862 +vn 0.3764 0.1807 -0.9087 +vn 0.2243 0.8103 -0.5414 +vn 0.2243 -0.8103 -0.5414 +vn 0.1945 -0.9776 -0.0805 +vn 0.7862 -0.5253 -0.3256 +vn 0.9087 0.1807 -0.3764 +vn 0.5414 0.8103 -0.2243 +vn 0.5414 -0.8103 -0.2243 +vn 0.9087 -0.1807 -0.3764 +vn 0.7862 0.5253 -0.3256 +vn 0.1945 0.9776 -0.0805 +vn 0.5414 0.8103 0.2243 +vn 0.5414 -0.8103 0.2243 +vn 0.9087 -0.1807 0.3764 +vn 0.7862 0.5253 0.3256 +vn 0.1945 0.9776 0.0805 +vn 0.1945 -0.9776 0.0805 +vn 0.7862 -0.5253 0.3256 +vn 0.9087 0.1807 0.3764 +vn 0.2243 -0.8103 0.5414 +vn 0.3764 -0.1807 0.9087 +vn 0.3256 0.5253 0.7862 +vn 0.0805 0.9776 0.1945 +vn 0.0805 -0.9776 0.1945 +vn 0.3256 -0.5253 0.7862 +vn 0.3764 0.1807 0.9087 +vn 0.2243 0.8103 0.5414 +vn -0.0805 0.9776 0.1945 +vn -0.0805 -0.9776 0.1945 +vn -0.3256 -0.5253 0.7862 +vn -0.3764 0.1807 0.9087 +vn -0.2243 0.8103 0.5414 +vn -0.2243 -0.8103 0.5414 +vn -0.3764 -0.1807 0.9087 +vn -0.3256 0.5253 0.7862 +vn -0.7862 -0.5253 0.3256 +vn -0.9087 0.1807 0.3764 +vn -0.5414 0.8103 0.2243 +vn -0.5414 -0.8103 0.2243 +vn -0.9087 -0.1807 0.3764 +vn -0.7862 0.5253 0.3256 +vn -0.1945 0.9776 0.0805 +vn -0.1945 -0.9776 0.0805 +vn -0.5414 -0.8103 -0.2243 +vn -0.9087 -0.1807 -0.3764 +vn -0.7862 0.5253 -0.3256 +vn -0.1945 0.9776 -0.0805 +vn -0.1945 -0.9776 -0.0805 +vn -0.7862 -0.5253 -0.3256 +vn -0.9087 0.1807 -0.3764 +vn -0.5414 0.8103 -0.2243 +vn -0.3764 -0.1807 -0.9087 +vn -0.3256 0.5253 -0.7862 +vn -0.0805 0.9776 -0.1945 +vn -0.0805 -0.9776 -0.1945 +vn -0.3256 -0.5253 -0.7862 +vn -0.3764 0.1807 -0.9087 +vn -0.2243 0.8103 -0.5414 +vn -0.2243 -0.8103 -0.5414 +usemtl None +s off +f 5/1/1 4/2/1 11/3/1 12/4/1 +f 3/5/2 2/6/2 9/7/2 10/8/2 +f 1/9/3 36/10/3 8/11/3 +f 51/12/4 7/13/4 14/14/4 +f 6/15/5 5/1/5 12/4/5 13/16/5 +f 4/2/6 3/5/6 10/8/6 11/3/6 +f 2/6/7 1/9/7 8/11/7 9/7/7 +f 7/13/8 6/15/8 13/16/8 14/14/8 +f 51/17/9 14/14/9 21/18/9 +f 13/16/10 12/4/10 19/19/10 20/20/10 +f 11/3/11 10/8/11 17/21/11 18/22/11 +f 9/7/12 8/11/12 15/23/12 16/24/12 +f 14/14/13 13/16/13 20/20/13 21/18/13 +f 12/4/14 11/3/14 18/22/14 19/19/14 +f 10/8/15 9/7/15 16/24/15 17/21/15 +f 8/11/16 36/25/16 15/23/16 +f 16/24/17 15/23/17 22/26/17 23/27/17 +f 21/18/18 20/20/18 27/28/18 28/29/18 +f 19/19/19 18/22/19 25/30/19 26/31/19 +f 17/21/20 16/24/20 23/27/20 24/32/20 +f 15/23/21 36/33/21 22/26/21 +f 51/34/22 21/18/22 28/29/22 +f 20/20/23 19/19/23 26/31/23 27/28/23 +f 18/22/24 17/21/24 24/32/24 25/30/24 +f 28/29/25 27/28/25 34/35/25 35/36/25 +f 26/31/26 25/30/26 32/37/26 33/38/26 +f 24/32/27 23/27/27 30/39/27 31/40/27 +f 22/26/28 36/41/28 29/42/28 +f 51/43/29 28/29/29 35/36/29 +f 27/28/30 26/31/30 33/38/30 34/35/30 +f 25/30/31 24/32/31 31/40/31 32/37/31 +f 23/27/32 22/26/32 29/42/32 30/39/32 +f 29/42/33 36/44/33 37/45/33 +f 51/46/34 35/36/34 43/47/34 +f 34/35/35 33/38/35 41/48/35 42/49/35 +f 32/37/36 31/40/36 39/50/36 40/51/36 +f 30/39/37 29/42/37 37/45/37 38/52/37 +f 35/36/38 34/35/38 42/49/38 43/47/38 +f 33/38/39 32/37/39 40/51/39 41/48/39 +f 31/40/40 30/39/40 38/52/40 39/50/40 +f 42/49/41 41/48/41 48/53/41 49/54/41 +f 40/51/42 39/50/42 46/55/42 47/56/42 +f 38/52/43 37/45/43 44/57/43 45/58/43 +f 43/47/44 42/49/44 49/54/44 50/59/44 +f 41/48/45 40/51/45 47/56/45 48/53/45 +f 39/50/46 38/52/46 45/58/46 46/55/46 +f 37/45/47 36/60/47 44/57/47 +f 51/61/48 43/47/48 50/59/48 +f 50/62/49 49/63/49 57/64/49 58/65/49 +f 48/66/50 47/67/50 55/68/50 56/69/50 +f 46/70/51 45/71/51 53/72/51 54/73/51 +f 44/74/52 36/75/52 52/76/52 +f 51/77/53 50/62/53 58/65/53 +f 49/63/54 48/66/54 56/69/54 57/64/54 +f 47/67/55 46/70/55 54/73/55 55/68/55 +f 45/71/56 44/74/56 52/76/56 53/72/56 +f 56/69/57 55/68/57 4/2/57 5/1/57 +f 54/73/58 53/72/58 2/6/58 3/5/58 +f 52/76/59 36/78/59 1/9/59 +f 51/79/60 58/65/60 7/13/60 +f 57/64/61 56/69/61 5/1/61 6/15/61 +f 55/68/62 54/73/62 3/5/62 4/2/62 +f 53/72/63 52/76/63 1/9/63 2/6/63 +f 58/65/64 57/64/64 6/15/64 7/13/64 diff --git a/engine/rend/src/sgpu/renderer.zig b/engine/rend/src/sgpu/renderer.zig index 15afa91..2ed8dbc 100644 --- a/engine/rend/src/sgpu/renderer.zig +++ b/engine/rend/src/sgpu/renderer.zig @@ -40,12 +40,13 @@ pub const Renderer = struct { textureList: *TextureList = undefined, blockySampler: *gpu.GPUSampler = undefined, + defaultTexture: *rend.Texture = undefined, + + debugDrawSys: *DebugDrawSystem = undefined, // transients DO NOT TOUCH swapchainTexture: *gpu.GPUTexture = undefined, - defaultTexture: *rend.Texture = undefined, - pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This()); pub const MaxObjectCount = 50000; @@ -106,6 +107,8 @@ pub const Renderer = struct { var defaultTextureName = core.MakeName("t_default"); self.defaultTexture = getTexture(&defaultTextureName).?; + + self.debugDrawSys = try core.createObject(DebugDrawSystem, .{}); } pub fn createSamplers(self: *@This()) !void { @@ -549,6 +552,8 @@ pub const MeshPool = mesh_pool.MeshPool; pub const TextureList = @import("TextureList.zig"); +const DebugDrawSystem = @import("DebugDrawSystem.zig"); + // debug api pub fn reloadShaders() !void { diff --git a/lib/packer/src/packerfs.zig b/lib/packer/src/packerfs.zig index e98d14b..3d80af3 100644 --- a/lib/packer/src/packerfs.zig +++ b/lib/packer/src/packerfs.zig @@ -74,12 +74,19 @@ pub const PackerFS = struct { mountRefs: std.ArrayListUnmanaged(usize) = .{}, contentOffset: usize = 0, inMemory: bool = false, + embedded: bool = false, pub fn isFileMounted(self: @This()) bool { - return self.mounted; + return self.mounted or self.embedded; } pub fn unmount(self: *@This(), allocator: std.mem.Allocator) void { + if (self.embedded) { + self.mountRefs.deinit(allocator); + self.mountRefs = .{}; + return; + } + if (self.mounted) { // std.debug.print("unmounting file:{s} bytes: 0x{x}\n", .{ self.filePath, @intFromPtr(self.bytes.ptr) }); allocator.free(self.bytes); @@ -90,6 +97,9 @@ pub const PackerFS = struct { } pub fn removeMapping(self: *@This(), allocator: std.mem.Allocator, fileRef: usize) void { + if (self.embedded) + return; + for (0..self.mountRefs.items.len) |i| { if (self.mountRefs.items[i] == fileRef) { _ = self.mountRefs.swapRemove(i); @@ -237,6 +247,10 @@ pub const PackerFS = struct { }, }); + return try self.installFileBytesMount(fullPath, fileBytes, false); + } + + pub fn installFileBytesMount(self: *@This(), path: []const u8, fileBytes: []align(8) u8, embedded: bool) !?PackerBytesMapping { const pakMountIndex = self.pakMountings.items.len; try self.pakMountings.append(self.allocator, .{ .filePath = try self.stringAlloc().dupe(u8, path), @@ -252,6 +266,7 @@ pub const PackerFS = struct { self.pakMountings.items[pakMountIndex].bytes = fileBytes; try self.pakMountings.items[pakMountIndex].mountRefs.append(self.allocator, headerIndex); self.pakMountings.items[pakMountIndex].mounted = true; + self.pakMountings.items[pakMountIndex].embedded = embedded; // std.debug.print("loadFileDirect {s} {d} 0x{x}\n", .{ path, headerIndex, @intFromPtr(fileBytes.ptr) }); return .{