Use glBufferStorage to tell the driver that certain buffers are only uploaded once and never changed.

Should fix #144
This commit is contained in:
IntegratedQuantum 2023-11-15 17:41:07 +01:00
parent bfb6466f7e
commit d6ab3d587b
3 changed files with 37 additions and 19 deletions

View File

@ -345,9 +345,9 @@ pub const meshes = struct {
break :blk names; break :blk names;
}; };
var animationSSBO: SSBO = undefined; var animationSSBO: ?SSBO = null;
var textureDataSSBO: SSBO = undefined; var textureDataSSBO: ?SSBO = null;
var animatedTextureDataSSBO: SSBO = undefined; var animatedTextureDataSSBO: ?SSBO = null;
var animationShader: Shader = undefined; var animationShader: Shader = undefined;
var animationUniforms: struct { var animationUniforms: struct {
@ -366,12 +366,6 @@ pub const meshes = struct {
const emptyImage = Image{.width = 1, .height = 1, .imageData = emptyTexture[0..]}; const emptyImage = Image{.width = 1, .height = 1, .imageData = emptyTexture[0..]};
pub fn init() !void { pub fn init() !void {
animationSSBO = SSBO.init();
animationSSBO.bind(0);
textureDataSSBO = SSBO.init();
textureDataSSBO.bind(6);
animatedTextureDataSSBO = SSBO.init();
animatedTextureDataSSBO.bind(1);
animationShader = try Shader.initComputeAndGetUniforms("assets/cubyz/shaders/animation_pre_processing.glsl", &animationUniforms); animationShader = try Shader.initComputeAndGetUniforms("assets/cubyz/shaders/animation_pre_processing.glsl", &animationUniforms);
blockTextureArray = TextureArray.init(); blockTextureArray = TextureArray.init();
emissionTextureArray = TextureArray.init(); emissionTextureArray = TextureArray.init();
@ -384,9 +378,15 @@ pub const meshes = struct {
} }
pub fn deinit() void { pub fn deinit() void {
animationSSBO.deinit(); if(animationSSBO) |ssbo| {
textureDataSSBO.deinit(); ssbo.deinit();
animatedTextureDataSSBO.deinit(); }
if(textureDataSSBO) |ssbo| {
ssbo.deinit();
}
if(animatedTextureDataSSBO) |ssbo| {
ssbo.deinit();
}
animationShader.deinit(); animationShader.deinit();
blockTextureArray.deinit(); blockTextureArray.deinit();
emissionTextureArray.deinit(); emissionTextureArray.deinit();
@ -576,8 +576,20 @@ pub const meshes = struct {
try emissionTextureArray.generate(emissionTextures.items, true); try emissionTextureArray.generate(emissionTextures.items, true);
// Also generate additional buffers: // Also generate additional buffers:
animationSSBO.bufferData(AnimationData, animation.items); if(animationSSBO) |ssbo| {
textureDataSSBO.bufferData(TextureData, textureData[0..meshes.size]); ssbo.deinit();
animatedTextureDataSSBO.bufferData(TextureData, textureData[0..meshes.size]); }
if(textureDataSSBO) |ssbo| {
ssbo.deinit();
}
if(animatedTextureDataSSBO) |ssbo| {
ssbo.deinit();
}
animationSSBO = SSBO.initStatic(AnimationData, animation.items);
animationSSBO.?.bind(0);
textureDataSSBO = SSBO.initStatic(TextureData, textureData[0..meshes.size]);
textureDataSSBO.?.bind(6);
animatedTextureDataSSBO = SSBO.initStatic(TextureData, textureData[0..meshes.size]);
animatedTextureDataSSBO.?.bind(1);
} }
}; };

View File

@ -1170,6 +1170,14 @@ pub const SSBO = struct {
return self; return self;
} }
pub fn initStatic(comptime T: type, data: []const T) SSBO {
var self = SSBO{.bufferID = undefined};
c.glGenBuffers(1, &self.bufferID);
c.glBindBuffer(c.GL_SHADER_STORAGE_BUFFER, self.bufferID);
c.glBufferStorage(c.GL_SHADER_STORAGE_BUFFER, @intCast(data.len*@sizeOf(T)), data.ptr, 0);
return self;
}
pub fn deinit(self: SSBO) void { pub fn deinit(self: SSBO) void {
c.glDeleteBuffers(1, &self.bufferID); c.glDeleteBuffers(1, &self.bufferID);
} }

View File

@ -174,9 +174,6 @@ pub var fullCube: u16 = 0;
// TODO: Allow loading from world assets. // TODO: Allow loading from world assets.
// TODO: Editable player models. // TODO: Editable player models.
pub fn init() !void { pub fn init() !void {
voxelModelSSBO = graphics.SSBO.init();
voxelModelSSBO.bind(4);
voxelModels = std.ArrayList(VoxelModel).init(main.threadAllocator); voxelModels = std.ArrayList(VoxelModel).init(main.threadAllocator);
nameToIndex = std.StringHashMap(u16).init(main.threadAllocator); nameToIndex = std.StringHashMap(u16).init(main.threadAllocator);
@ -199,7 +196,8 @@ pub fn init() !void {
try nameToIndex.put("octahedron", @intCast(voxelModels.items.len)); try nameToIndex.put("octahedron", @intCast(voxelModels.items.len));
(try voxelModels.addOne()).init(octahedron); (try voxelModels.addOne()).init(octahedron);
voxelModelSSBO.bufferData(VoxelModel, voxelModels.items); voxelModelSSBO = graphics.SSBO.initStatic(VoxelModel, voxelModels.items);
voxelModelSSBO.bind(4);
} }
pub fn deinit() void { pub fn deinit() void {