diff --git a/src/AssetManager.zig b/src/AssetManager.zig index 992c5b2..9c8f0da 100644 --- a/src/AssetManager.zig +++ b/src/AssetManager.zig @@ -282,7 +282,17 @@ const NullMesh = LoadedMesh{ .offset = 0, .stride = 0, }, - .ps_data = BufferSlice{ + .normals = BufferSlice{ + .buffer = 0, + .offset = 0, + .stride = 0, + }, + .tangents = BufferSlice{ + .buffer = 0, + .offset = 0, + .stride = 0, + }, + .uvs = BufferSlice{ .buffer = 0, .offset = 0, .stride = 0, @@ -329,8 +339,13 @@ fn loadMeshErr(self: *AssetManager, id: AssetId) !LoadedMesh { gl.namedBufferSubData(self.vertex_heap.vertices.buffer, @intCast(vertex_offset * @sizeOf(formats.Vector3)), @intCast(vertices_len * @sizeOf(formats.Vector3)), @ptrCast(mesh.vertices.ptr)); checkGLError(); - gl.namedBufferSubData(self.vertex_heap.ps_data.buffer, @intCast(vertex_offset * @sizeOf(formats.VertexPSData)), @intCast(vertices_len * @sizeOf(formats.VertexPSData)), @ptrCast(mesh.ps_data.ptr)); + gl.namedBufferSubData(self.vertex_heap.normals.buffer, @intCast(vertex_offset * @sizeOf(formats.Vector3)), @intCast(vertices_len * @sizeOf(formats.Vector3)), @ptrCast(mesh.normals.ptr)); checkGLError(); + gl.namedBufferSubData(self.vertex_heap.tangents.buffer, @intCast(vertex_offset * @sizeOf(formats.Vector3)), @intCast(vertices_len * @sizeOf(formats.Vector3)), @ptrCast(mesh.tangents.ptr)); + checkGLError(); + gl.namedBufferSubData(self.vertex_heap.uvs.buffer, @intCast(vertex_offset * @sizeOf(formats.Vector2)), @intCast(vertices_len * @sizeOf(formats.Vector2)), @ptrCast(mesh.uvs.ptr)); + checkGLError(); + const index_offset = allocation.index.offset; gl.namedBufferSubData(self.vertex_heap.indices.buffer, @intCast(index_offset * @sizeOf(formats.Index)), @intCast(mesh.indices.len * @sizeOf(formats.Index)), @ptrCast(mesh.indices.ptr)); @@ -346,10 +361,20 @@ fn loadMeshErr(self: *AssetManager, id: AssetId) !LoadedMesh { .offset = @intCast(vertex_offset * @sizeOf(formats.Vector3)), .stride = @sizeOf(formats.Vector3), }, - .ps_data = .{ - .buffer = self.vertex_heap.ps_data.buffer, - .offset = @intCast(vertex_offset * @sizeOf(formats.VertexPSData)), - .stride = @sizeOf(formats.VertexPSData), + .normals = .{ + .buffer = self.vertex_heap.normals.buffer, + .offset = @intCast(vertex_offset * @sizeOf(formats.Vector3)), + .stride = @sizeOf(formats.Vector3), + }, + .tangents = .{ + .buffer = self.vertex_heap.tangents.buffer, + .offset = @intCast(vertex_offset * @sizeOf(formats.Vector3)), + .stride = @sizeOf(formats.Vector3), + }, + .uvs = .{ + .buffer = self.vertex_heap.uvs.buffer, + .offset = @intCast(vertex_offset * @sizeOf(formats.Vector2)), + .stride = @sizeOf(formats.Vector2), }, .indices = .{ .buffer = self.vertex_heap.indices.buffer, @@ -519,7 +544,9 @@ const LoadedMesh = struct { aabb: AABB, heap_handle: VertexBufferHeap.Alloc, positions: BufferSlice, - ps_data: BufferSlice, + normals: BufferSlice, + tangents: BufferSlice, + uvs: BufferSlice, indices: IndexSlice, material: formats.Material, }; @@ -727,15 +754,17 @@ const VertexBufferHeap = struct { }; } - pub fn bind(self: *const Buffer, index: gl.GLuint, offset: gl.GLintptr) void { - gl.bindVertexBuffer(index, self.buffer, offset, self.stride); + pub fn bind(self: *const Buffer, index: gl.GLuint) void { + gl.bindVertexBuffer(index, self.buffer, 0, self.stride); } }; vertex_buddy: BuddyAllocator, index_buddy: BuddyAllocator, vertices: Buffer, - ps_data: Buffer, + normals: Buffer, + tangents: Buffer, + uvs: Buffer, indices: Buffer, pub fn init(allocator: std.mem.Allocator) !Self { @@ -751,7 +780,7 @@ const VertexBufferHeap = struct { const vertex_buf_size = vertex_buddy.getSize(); const index_buf_size = index_buddy.getSize(); - var bufs = [_]gl.GLuint{ 0, 0, 0 }; + var bufs = [_]gl.GLuint{ 0, 0, 0, 0, 0 }; gl.createBuffers(bufs.len, &bufs); errdefer gl.deleteBuffers(bufs.len, &bufs); @@ -762,8 +791,10 @@ const VertexBufferHeap = struct { } const vertices = Buffer.init(bufs[0], @sizeOf(formats.Vector3)); - const ps_data = Buffer.init(bufs[1], @sizeOf(formats.VertexPSData)); - const indices = Buffer.init(bufs[2], @sizeOf(formats.Index)); + const normals = Buffer.init(bufs[1], @sizeOf(formats.Vector3)); + const tangents = Buffer.init(bufs[2], @sizeOf(formats.Vector3)); + const uvs = Buffer.init(bufs[3], @sizeOf(formats.Vector2)); + const indices = Buffer.init(bufs[4], @sizeOf(formats.Index)); gl.namedBufferStorage( vertices.buffer, @@ -772,8 +803,20 @@ const VertexBufferHeap = struct { gl.DYNAMIC_STORAGE_BIT, ); gl.namedBufferStorage( - ps_data.buffer, - @intCast(vertex_buf_size * @sizeOf(formats.VertexPSData)), + normals.buffer, + @intCast(vertex_buf_size * @sizeOf(formats.Vector3)), + null, + gl.DYNAMIC_STORAGE_BIT, + ); + gl.namedBufferStorage( + tangents.buffer, + @intCast(vertex_buf_size * @sizeOf(formats.Vector3)), + null, + gl.DYNAMIC_STORAGE_BIT, + ); + gl.namedBufferStorage( + uvs.buffer, + @intCast(vertex_buf_size * @sizeOf(formats.Vector2)), null, gl.DYNAMIC_STORAGE_BIT, ); @@ -788,7 +831,9 @@ const VertexBufferHeap = struct { .vertex_buddy = vertex_buddy, .index_buddy = index_buddy, .vertices = vertices, - .ps_data = ps_data, + .normals = normals, + .tangents = tangents, + .uvs = uvs, .indices = indices, }; } @@ -797,7 +842,7 @@ const VertexBufferHeap = struct { self.index_buddy.deinit(); self.vertex_buddy.deinit(); - const bufs = [_]gl.GLuint{ self.vertices, self.ps_data, self.indices }; + const bufs = [_]gl.GLuint{ self.vertices, self.normals, self.tangents, self.uvs, self.indices }; gl.deleteBuffers(bufs.len, &bufs); } diff --git a/src/Render.zig b/src/Render.zig index cc0d6b8..f2713d5 100644 --- a/src/Render.zig +++ b/src/Render.zig @@ -830,13 +830,13 @@ pub fn finish(self: *Render) void { gl.GL_ARB_bindless_texture.uniformHandleui64ARB(Uniform.ShadowMap2D.value(), self.shadow_texture_handle); gl.GL_ARB_bindless_texture.uniformHandleui64ARB(Uniform.ShadowMapCube.value(), self.cube_shadow_texture_handle); - self.assetman.vertex_heap.vertices.bind(Render.Attrib.Position.value(), 0); + self.assetman.vertex_heap.normals.bind(Render.Attrib.Normal.value()); checkGLError(); - self.assetman.vertex_heap.ps_data.bind(Render.Attrib.Normal.value(), @offsetOf(formats.VertexPSData, "normal")); + self.assetman.vertex_heap.tangents.bind(Render.Attrib.Tangent.value()); checkGLError(); - self.assetman.vertex_heap.ps_data.bind(Render.Attrib.Tangent.value(), @offsetOf(formats.VertexPSData, "tangent")); + self.assetman.vertex_heap.uvs.bind(Render.Attrib.UV.value()); checkGLError(); - self.assetman.vertex_heap.ps_data.bind(Render.Attrib.UV.value(), @offsetOf(formats.VertexPSData, "uv")); + self.assetman.vertex_heap.vertices.bind(Render.Attrib.Position.value()); checkGLError(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, self.assetman.vertex_heap.indices.buffer); checkGLError(); diff --git a/src/formats.zig b/src/formats.zig index a7c17c1..37b17b3 100644 --- a/src/formats.zig +++ b/src/formats.zig @@ -23,18 +23,14 @@ pub const AABB = extern struct { }; pub const Index = u32; -pub const VertexPSData = extern struct { - normal: Vector3, - tangent: Vector3, - uv: Vector2, -}; - pub const Mesh = struct { aabb: AABB, material: Material, vertices: []align(1) Vector3, - ps_data: []align(1) VertexPSData, + normals: []align(1) Vector3, + tangents: []align(1) Vector3, + uvs: []align(1) Vector2, indices: []align(1) Index, // This will panic if data is incorrect @@ -67,9 +63,13 @@ pub const Mesh = struct { size = vert_len * @sizeOf(Vector3); const vertices = std.mem.bytesAsSlice(Vector3, buffer[offset .. offset + size]); offset += size; + const normals = std.mem.bytesAsSlice(Vector3, buffer[offset .. offset + size]); + offset += size; + const tangents = std.mem.bytesAsSlice(Vector3, buffer[offset .. offset + size]); + offset += size; - size = vert_len * @sizeOf(VertexPSData); - const ps_data = std.mem.bytesAsSlice(VertexPSData, buffer[offset .. offset + size]); + size = vert_len * @sizeOf(Vector2); + const uvs = std.mem.bytesAsSlice(Vector2, buffer[offset .. offset + size]); offset += size; size = ind_len * @sizeOf(Index); @@ -80,14 +80,16 @@ pub const Mesh = struct { .aabb = aabb, .material = material.*, .vertices = vertices, - .ps_data = ps_data, + .normals = normals, + .tangents = tangents, + .uvs = uvs, .indices = indices, }; } }; pub fn writeMesh(writer: anytype, value: Mesh, endian: std.builtin.Endian) !void { - std.debug.assert(value.vertices.len == value.ps_data.len); + std.debug.assert(value.vertices.len == value.normals.len); // AABB { @@ -104,10 +106,14 @@ pub fn writeMesh(writer: anytype, value: Mesh, endian: std.builtin.Endian) !void for (value.vertices) |v| { try writeVector3(writer, v, endian); } - for (value.ps_data) |data| { - try writeVector3(writer, data.normal, endian); - try writeVector3(writer, data.tangent, endian); - try writeVector2(writer, data.uv, endian); + for (value.normals) |n| { + try writeVector3(writer, n, endian); + } + for (value.tangents) |t| { + try writeVector3(writer, t, endian); + } + for (value.uvs) |uv| { + try writeVector2(writer, uv, endian); } for (value.indices) |i| { try writer.writeInt(Index, i, endian); diff --git a/tools/asset_compiler.zig b/tools/asset_compiler.zig index f6dcf46..b030708 100644 --- a/tools/asset_compiler.zig +++ b/tools/asset_compiler.zig @@ -426,7 +426,10 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o if (mesh.mNumUVComponents[0] != 2) return error.WrongUVComponents; var vertices = try allocator.alloc(Vector3, @intCast(mesh.mNumVertices)); - var ps_data = try allocator.alloc(formats.VertexPSData, @intCast(mesh.mNumVertices)); + var normals = try allocator.alloc(Vector3, @intCast(mesh.mNumVertices)); + var tangents = try allocator.alloc(Vector3, @intCast(mesh.mNumVertices)); + var uvs = try allocator.alloc(Vector2, @intCast(mesh.mNumVertices)); + var indices = try allocator.alloc(formats.Index, @intCast(mesh.mNumFaces * 3)); // triangles for (0..mesh.mNumVertices) |i| { @@ -435,17 +438,17 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o .y = mesh.mVertices[i].y, .z = mesh.mVertices[i].z, }; - ps_data[i].normal = .{ + normals[i] = .{ .x = mesh.mNormals[i].x, .y = mesh.mNormals[i].y, .z = mesh.mNormals[i].z, }; - ps_data[i].tangent = .{ + tangents[i] = .{ .x = mesh.mTangents[i].x, .y = mesh.mTangents[i].y, .z = mesh.mTangents[i].z, }; - ps_data[i].uv = .{ + uvs[i] = .{ .x = mesh.mTextureCoords[0][i].x, .y = mesh.mTextureCoords[0][i].y, }; @@ -479,7 +482,9 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o }, }, .vertices = vertices, - .ps_data = ps_data, + .normals = normals, + .tangents = tangents, + .uvs = uvs, .indices = indices, .material = material, };