Revert "Try interleaved vertex attributes" it's slower :(

This reverts commit f21bc2245a9629981ce9719c3c500c8614da83fa.
This commit is contained in:
sergeypdev 2024-07-26 17:04:01 +04:00
parent b0163b1f6b
commit 6a0d8348f1
4 changed files with 97 additions and 41 deletions

View File

@ -282,7 +282,17 @@ const NullMesh = LoadedMesh{
.offset = 0, .offset = 0,
.stride = 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, .buffer = 0,
.offset = 0, .offset = 0,
.stride = 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)); gl.namedBufferSubData(self.vertex_heap.vertices.buffer, @intCast(vertex_offset * @sizeOf(formats.Vector3)), @intCast(vertices_len * @sizeOf(formats.Vector3)), @ptrCast(mesh.vertices.ptr));
checkGLError(); 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(); 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; 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)); 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)), .offset = @intCast(vertex_offset * @sizeOf(formats.Vector3)),
.stride = @sizeOf(formats.Vector3), .stride = @sizeOf(formats.Vector3),
}, },
.ps_data = .{ .normals = .{
.buffer = self.vertex_heap.ps_data.buffer, .buffer = self.vertex_heap.normals.buffer,
.offset = @intCast(vertex_offset * @sizeOf(formats.VertexPSData)), .offset = @intCast(vertex_offset * @sizeOf(formats.Vector3)),
.stride = @sizeOf(formats.VertexPSData), .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 = .{ .indices = .{
.buffer = self.vertex_heap.indices.buffer, .buffer = self.vertex_heap.indices.buffer,
@ -519,7 +544,9 @@ const LoadedMesh = struct {
aabb: AABB, aabb: AABB,
heap_handle: VertexBufferHeap.Alloc, heap_handle: VertexBufferHeap.Alloc,
positions: BufferSlice, positions: BufferSlice,
ps_data: BufferSlice, normals: BufferSlice,
tangents: BufferSlice,
uvs: BufferSlice,
indices: IndexSlice, indices: IndexSlice,
material: formats.Material, material: formats.Material,
}; };
@ -727,15 +754,17 @@ const VertexBufferHeap = struct {
}; };
} }
pub fn bind(self: *const Buffer, index: gl.GLuint, offset: gl.GLintptr) void { pub fn bind(self: *const Buffer, index: gl.GLuint) void {
gl.bindVertexBuffer(index, self.buffer, offset, self.stride); gl.bindVertexBuffer(index, self.buffer, 0, self.stride);
} }
}; };
vertex_buddy: BuddyAllocator, vertex_buddy: BuddyAllocator,
index_buddy: BuddyAllocator, index_buddy: BuddyAllocator,
vertices: Buffer, vertices: Buffer,
ps_data: Buffer, normals: Buffer,
tangents: Buffer,
uvs: Buffer,
indices: Buffer, indices: Buffer,
pub fn init(allocator: std.mem.Allocator) !Self { pub fn init(allocator: std.mem.Allocator) !Self {
@ -751,7 +780,7 @@ const VertexBufferHeap = struct {
const vertex_buf_size = vertex_buddy.getSize(); const vertex_buf_size = vertex_buddy.getSize();
const index_buf_size = index_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); gl.createBuffers(bufs.len, &bufs);
errdefer gl.deleteBuffers(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 vertices = Buffer.init(bufs[0], @sizeOf(formats.Vector3));
const ps_data = Buffer.init(bufs[1], @sizeOf(formats.VertexPSData)); const normals = Buffer.init(bufs[1], @sizeOf(formats.Vector3));
const indices = Buffer.init(bufs[2], @sizeOf(formats.Index)); 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( gl.namedBufferStorage(
vertices.buffer, vertices.buffer,
@ -772,8 +803,20 @@ const VertexBufferHeap = struct {
gl.DYNAMIC_STORAGE_BIT, gl.DYNAMIC_STORAGE_BIT,
); );
gl.namedBufferStorage( gl.namedBufferStorage(
ps_data.buffer, normals.buffer,
@intCast(vertex_buf_size * @sizeOf(formats.VertexPSData)), @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, null,
gl.DYNAMIC_STORAGE_BIT, gl.DYNAMIC_STORAGE_BIT,
); );
@ -788,7 +831,9 @@ const VertexBufferHeap = struct {
.vertex_buddy = vertex_buddy, .vertex_buddy = vertex_buddy,
.index_buddy = index_buddy, .index_buddy = index_buddy,
.vertices = vertices, .vertices = vertices,
.ps_data = ps_data, .normals = normals,
.tangents = tangents,
.uvs = uvs,
.indices = indices, .indices = indices,
}; };
} }
@ -797,7 +842,7 @@ const VertexBufferHeap = struct {
self.index_buddy.deinit(); self.index_buddy.deinit();
self.vertex_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); gl.deleteBuffers(bufs.len, &bufs);
} }

View File

@ -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.ShadowMap2D.value(), self.shadow_texture_handle);
gl.GL_ARB_bindless_texture.uniformHandleui64ARB(Uniform.ShadowMapCube.value(), self.cube_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(); 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(); 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(); 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(); checkGLError();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, self.assetman.vertex_heap.indices.buffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, self.assetman.vertex_heap.indices.buffer);
checkGLError(); checkGLError();

View File

@ -23,18 +23,14 @@ pub const AABB = extern struct {
}; };
pub const Index = u32; pub const Index = u32;
pub const VertexPSData = extern struct {
normal: Vector3,
tangent: Vector3,
uv: Vector2,
};
pub const Mesh = struct { pub const Mesh = struct {
aabb: AABB, aabb: AABB,
material: Material, material: Material,
vertices: []align(1) Vector3, 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, indices: []align(1) Index,
// This will panic if data is incorrect // This will panic if data is incorrect
@ -67,9 +63,13 @@ pub const Mesh = struct {
size = vert_len * @sizeOf(Vector3); size = vert_len * @sizeOf(Vector3);
const vertices = std.mem.bytesAsSlice(Vector3, buffer[offset .. offset + size]); const vertices = std.mem.bytesAsSlice(Vector3, buffer[offset .. offset + size]);
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); size = vert_len * @sizeOf(Vector2);
const ps_data = std.mem.bytesAsSlice(VertexPSData, buffer[offset .. offset + size]); const uvs = std.mem.bytesAsSlice(Vector2, buffer[offset .. offset + size]);
offset += size; offset += size;
size = ind_len * @sizeOf(Index); size = ind_len * @sizeOf(Index);
@ -80,14 +80,16 @@ pub const Mesh = struct {
.aabb = aabb, .aabb = aabb,
.material = material.*, .material = material.*,
.vertices = vertices, .vertices = vertices,
.ps_data = ps_data, .normals = normals,
.tangents = tangents,
.uvs = uvs,
.indices = indices, .indices = indices,
}; };
} }
}; };
pub fn writeMesh(writer: anytype, value: Mesh, endian: std.builtin.Endian) !void { 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 // AABB
{ {
@ -104,10 +106,14 @@ pub fn writeMesh(writer: anytype, value: Mesh, endian: std.builtin.Endian) !void
for (value.vertices) |v| { for (value.vertices) |v| {
try writeVector3(writer, v, endian); try writeVector3(writer, v, endian);
} }
for (value.ps_data) |data| { for (value.normals) |n| {
try writeVector3(writer, data.normal, endian); try writeVector3(writer, n, endian);
try writeVector3(writer, data.tangent, endian); }
try writeVector2(writer, data.uv, endian); for (value.tangents) |t| {
try writeVector3(writer, t, endian);
}
for (value.uvs) |uv| {
try writeVector2(writer, uv, endian);
} }
for (value.indices) |i| { for (value.indices) |i| {
try writer.writeInt(Index, i, endian); try writer.writeInt(Index, i, endian);

View File

@ -426,7 +426,10 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o
if (mesh.mNumUVComponents[0] != 2) return error.WrongUVComponents; if (mesh.mNumUVComponents[0] != 2) return error.WrongUVComponents;
var vertices = try allocator.alloc(Vector3, @intCast(mesh.mNumVertices)); 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 var indices = try allocator.alloc(formats.Index, @intCast(mesh.mNumFaces * 3)); // triangles
for (0..mesh.mNumVertices) |i| { 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, .y = mesh.mVertices[i].y,
.z = mesh.mVertices[i].z, .z = mesh.mVertices[i].z,
}; };
ps_data[i].normal = .{ normals[i] = .{
.x = mesh.mNormals[i].x, .x = mesh.mNormals[i].x,
.y = mesh.mNormals[i].y, .y = mesh.mNormals[i].y,
.z = mesh.mNormals[i].z, .z = mesh.mNormals[i].z,
}; };
ps_data[i].tangent = .{ tangents[i] = .{
.x = mesh.mTangents[i].x, .x = mesh.mTangents[i].x,
.y = mesh.mTangents[i].y, .y = mesh.mTangents[i].y,
.z = mesh.mTangents[i].z, .z = mesh.mTangents[i].z,
}; };
ps_data[i].uv = .{ uvs[i] = .{
.x = mesh.mTextureCoords[0][i].x, .x = mesh.mTextureCoords[0][i].x,
.y = mesh.mTextureCoords[0][i].y, .y = mesh.mTextureCoords[0][i].y,
}; };
@ -479,7 +482,9 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o
}, },
}, },
.vertices = vertices, .vertices = vertices,
.ps_data = ps_data, .normals = normals,
.tangents = tangents,
.uvs = uvs,
.indices = indices, .indices = indices,
.material = material, .material = material,
}; };