Hello Square!

This commit is contained in:
abux 2026-07-29 01:15:31 +01:00
parent 7700000bd7
commit 14dc99d212
13 changed files with 180 additions and 23 deletions

View file

@ -14,6 +14,7 @@ paru -S --needed sdl3 wgpu-native
- [x] Device - [x] Device
- [x] Cache - [x] Cache
- [ ] Renderer - [ ] Renderer
- [ ] Hot Reloading
### Engine ### Engine
- [ ] ClassDB - [ ] ClassDB

View file

@ -1,19 +1,11 @@
@vertex @vertex
fn vs_main( fn vs_main(
@builtin(vertex_index) in_vertex_index: u32 @location(0) in_vertex_position: vec2f
) -> @builtin(position) vec4f { ) -> @builtin(position) vec4f {
var p = vec2f(0.0, 0.0); return vec4f(in_vertex_position, 0.0, 1.0);
if in_vertex_index == 0u {
p = vec2f(-0.5, -0.5);
} else if in_vertex_index == 1u {
p = vec2f(0.5, -0.5);
} else {
p = vec2f(0.0, 0.5);
}
return vec4f(p, 0.0, 1.0);
} }
@fragment @fragment
fn fs_main() -> @location(0) vec4f { fn fs_main() -> @location(0) vec4f {
return vec4f(0.0, 0.4, 1.0, 1.0); return vec4f(0.001, 0.001, 0.001, 1.0);
} }

View file

@ -0,0 +1,118 @@
//! ----------------------------------------------------
//! ----------------------------------------------------
const std = @import("std");
const gtl = @import("gtl");
const wgpu = @import("wgpu");
const Self = @This();
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const Vertex = struct {
position: @Vector(3, f32),
normal: @Vector(3, f32),
uv: @Vector(2, f32),
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const MeshID = union(enum) {
cube,
quad,
square,
triangle,
custom: u32, // TODO: idk, someday
};
/// ----------------------------------------------------
/// ----------------------------------------------------
pub const GPUMesh = struct {
vbuf: *wgpu.WGPUBufferImpl,
ibuf: ?*wgpu.WGPUBufferImpl = null,
};
//
// FIELDS
//
data: gtl.KeyedSlotMap(MeshID, GPUMesh),
device: *wgpu.WGPUDeviceImpl,
queue: *wgpu.WGPUQueueImpl,
alloc: std.mem.Allocator,
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn init(
device: *wgpu.WGPUDeviceImpl,
queue: *wgpu.WGPUQueueImpl,
alloc: std.mem.Allocator,
) Self {
return .{
.data = .init(alloc),
.device = device,
.queue = queue,
.alloc = alloc,
};
}
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn deinit(self: *Self) void {
// --- FREE BUFFERS ---
for (self.data.items()) |entry| {
wgpu.wgpuBufferRelease(entry.value.vbuf);
if (entry.value.ibuf) |ibuf| {
wgpu.wgpuBufferRelease(ibuf);
}
}
// --- # ---
self.data.deinit();
}
/// ----------------------------------------------------
/// ----------------------------------------------------
pub fn getOrCreate(
self: *Self,
mesh: MeshID,
) !*GPUMesh {
return try self.data.getOrPut(mesh, .{
.vbuf = blk: {
// --- DATA ---
const vertices: []const f32 = switch (mesh) {
.triangle => &.{
-0.5, -0.5,
0.5, -0.5,
0.0, 0.5,
},
.square => &.{
-0.5, -0.5,
0.5, -0.5,
0.5, 0.5,
-0.5, -0.5,
0.5, 0.5,
-0.5, 0.5,
},
else => &.{},
};
// --- GET SIZE ---
const size = vertices.len * @sizeOf(f32);
// --- CREATE ---
const buff = wgpu.wgpuDeviceCreateBuffer(self.device, &wgpu.WGPUBufferDescriptor{
.size = size,
.usage = wgpu.WGPUBufferUsage_CopyDst | wgpu.WGPUBufferUsage_Vertex,
}) orelse return error.FailedToCreateBuffer;
// --- WRITE ---
wgpu.wgpuQueueWriteBuffer(self.queue, buff, 0, vertices.ptr, size);
// --- RESULT ---
break :blk buff;
},
.ibuf = null,
});
}

View file

@ -82,13 +82,24 @@ fn createPipeline(
.vertex = .{ .vertex = .{
.module = shader_module.raw, .module = shader_module.raw,
.entryPoint = .{ .data = "vs_main", .length = wgpu.WGPU_STRLEN }, .entryPoint = .{ .data = "vs_main", .length = wgpu.WGPU_STRLEN },
.bufferCount = 1,
.buffers = &wgpu.WGPUVertexBufferLayout{
.attributeCount = 1,
.attributes = &wgpu.WGPUVertexAttribute{ // TODO: Use Vertex attributes instead | Mabe mabe allow for custom, mabe mabe later tho
.shaderLocation = 0,
.format = wgpu.WGPUVertexFormat_Float32x2,
.offset = 0,
},
.arrayStride = 2 * @sizeOf(f32),
.stepMode = wgpu.WGPUVertexStepMode_Vertex,
},
}, },
.fragment = &wgpu.WGPUFragmentState{ .fragment = &wgpu.WGPUFragmentState{
.module = shader_module.raw, .module = shader_module.raw,
.entryPoint = .{ .data = "fs_main", .length = wgpu.WGPU_STRLEN }, .entryPoint = .{ .data = "fs_main", .length = wgpu.WGPU_STRLEN },
.targetCount = 1, .targetCount = 1,
.targets = &wgpu.WGPUColorTargetState{ .targets = &wgpu.WGPUColorTargetState{
.format = 28, .format = 28, // FIX: Use proper format
.blend = &wgpu.WGPUBlendState{ .blend = &wgpu.WGPUBlendState{
.color = .{ .color = .{
.srcFactor = wgpu.WGPUBlendFactor_SrcAlpha, .srcFactor = wgpu.WGPUBlendFactor_SrcAlpha,

View file

@ -14,6 +14,7 @@ const lifetime = @import("system/lifetime.zig");
pub const ShaderCache = @import("resource/shader_cache.zig"); pub const ShaderCache = @import("resource/shader_cache.zig");
pub const PipelineCache = @import("resource/pipeline_cache.zig"); pub const PipelineCache = @import("resource/pipeline_cache.zig");
pub const MeshCache = @import("resource/mesh_cache.zig");
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------

View file

@ -5,8 +5,10 @@ const std = @import("std");
const gtl = @import("gtl"); const gtl = @import("gtl");
const App = @import("app"); const App = @import("app");
const WGPUDevice = @import("../../device/resource/wgpu_device.zig"); const WGPUDevice = @import("../../device/resource/wgpu_device.zig");
const ShaderCache = @import("../resource/shader_cache.zig"); const ShaderCache = @import("../resource/shader_cache.zig");
const PipelineCache = @import("../resource/pipeline_cache.zig"); const PipelineCache = @import("../resource/pipeline_cache.zig");
const MeshCache = @import("../resource/mesh_cache.zig");
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
@ -30,6 +32,16 @@ pub fn init(app: *App) !void {
app.alloc, app.alloc,
)); ));
// --- MESH CACHE ---
const meshes = try app.resources.getOrSet(MeshCache.init(
res.device.raw,
res.device.queue,
app.alloc,
));
// _ = meshes;
_ = try meshes.getOrCreate(.cube);
// TODO: Remove or place it somewhere else, idk // TODO: Remove or place it somewhere else, idk
// --- TEST --- // --- TEST ---
@ -84,9 +96,11 @@ pub fn deinit(app: *App) !void {
const res = try app.resources.getMany(struct { const res = try app.resources.getMany(struct {
shaders: *ShaderCache, shaders: *ShaderCache,
pipelines: *PipelineCache, pipelines: *PipelineCache,
meshes: *MeshCache,
}); });
// --- # --- // --- # ---
res.shaders.deinit(); res.shaders.deinit();
res.pipelines.deinit(); res.pipelines.deinit();
res.meshes.deinit();
} }

View file

@ -0,0 +1,3 @@
const MeshID = @import("../../cache/resource/mesh_cache.zig").MeshID;
value: MeshID,

View file

@ -0,0 +1,3 @@
position: @Vector(2, f32) = .{ 0, 0 },
scale: @Vector(2, f32) = .{ 32, 32 },
angle: f32 = 0,

View file

@ -0,0 +1,3 @@
position: @Vector(3, f32) = .{ 0, 0, 0 },
rotation: @Vector(3, f32) = .{ 0, 0, 0 },
scale: @Vector(3, f32) = .{ 1, 1, 1 },

View file

@ -5,4 +5,10 @@
// COMPONENTS // COMPONENTS
// //
// --- POSITION ---
pub const Transform3D = @import("component/transform3D.zig");
pub const Transform2D = @import("component/transform2D.zig");
// --- STYLE ---
pub const Material = @import("component/material.zig"); pub const Material = @import("component/material.zig");
pub const Mesh = @import("component/mesh.zig");

View file

@ -4,23 +4,23 @@
const std = @import("std"); const std = @import("std");
const gtl = @import("gtl"); const gtl = @import("gtl");
const Material = @import("../../common/component/material.zig"); const Material = @import("../../common/component/material.zig");
const Mesh = @import("../../cache/resource/mesh_cache.zig").MeshID;
const Handle = gtl.Handle; const Handle = gtl.Handle;
const Self = @This(); const Self = @This();
// TODO: // TODO:
const Mesh = struct {};
const Camera = struct {}; const Camera = struct {};
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
pub const DrawCMD = union(enum) { pub const DrawCMD = union(enum) {
draw: struct { draw: struct {
mesh: Handle(Mesh), mesh: Mesh,
material: Material.Unique, material: Material.Unique,
}, },
draw_indexed: struct { draw_indexed: struct {
mesh: Handle(Mesh), mesh: Mesh,
material: Material.Unique, material: Material.Unique,
}, },

View file

@ -16,19 +16,22 @@ pub fn begin(app: *App) !void {
res.renderer.clear(); res.renderer.clear();
{ // TEST { // TEST
// --- DRAW ---
try res.renderer.draw(.{ try res.renderer.draw(.{
.begin_pass = .{ .begin_pass = .{
.camera = undefined, .camera = undefined,
.color = .{ 0.2, 0.8, 0.2, 1.0 }, .color = .{ 0.08, 0.08, 0.8, 1.0 },
}, },
}); });
defer res.renderer.draw(.end_pass) catch unreachable;
// --- # ---
try res.renderer.draw(.{ try res.renderer.draw(.{
.draw = .{ .draw = .{
.mesh = undefined, .mesh = .square,
.material = .{}, .material = .{},
}, },
}); });
try res.renderer.draw(.end_pass);
} }
} }

View file

@ -9,6 +9,7 @@ const Window = @import("../../window/resource/window.zig");
const Device = @import("../../device/resource/wgpu_device.zig"); const Device = @import("../../device/resource/wgpu_device.zig");
const Renderer = @import("../resource/renderer.zig"); const Renderer = @import("../resource/renderer.zig");
const PipelineCache = @import("../../cache/resource/pipeline_cache.zig"); const PipelineCache = @import("../../cache/resource/pipeline_cache.zig");
const MeshCache = @import("../../cache/resource/mesh_cache.zig");
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
@ -19,6 +20,7 @@ pub fn draw(app: *App) !void {
device: *Device, device: *Device,
renderer: *Renderer, renderer: *Renderer,
pipeline_cache: *PipelineCache, pipeline_cache: *PipelineCache,
mesh_cache: *MeshCache,
}); });
// --- SURFACE TEXTURE --- // --- SURFACE TEXTURE ---
@ -91,14 +93,14 @@ pub fn draw(app: *App) !void {
} }
}, },
.draw => |v| { .draw => |v| {
// --- ENSURE PIPELINE --- // --- ENSURE ---
const pipeline = try res.pipeline_cache.getOrCreate( const pipeline = try res.pipeline_cache.getOrCreate(v.material);
v.material, const mesh = try res.mesh_cache.getOrCreate(v.mesh);
);
// --- DRAW --- // --- DRAW ---
wgpu.wgpuRenderPassEncoderSetPipeline(render_pass, pipeline.raw); wgpu.wgpuRenderPassEncoderSetPipeline(render_pass, pipeline.raw);
wgpu.wgpuRenderPassEncoderDraw(render_pass, 3, 1, 0, 0); wgpu.wgpuRenderPassEncoderSetVertexBuffer(render_pass, 0, mesh.vbuf, 0, wgpu.wgpuBufferGetSize(mesh.vbuf));
wgpu.wgpuRenderPassEncoderDraw(render_pass, @intCast(wgpu.wgpuBufferGetSize(mesh.vbuf) / (2 * @sizeOf(f32))), 1, 0, 0);
}, },
else => {}, else => {},
} }