diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-03-16 16:46:52 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-16 19:10:16 +0000 |
| commit | f4ec8103c59b3e67273cd7a95ce42c752ec054e1 (patch) | |
| tree | 149011f38a225a308ae8361bb3dd40e7e7370bda /src | |
| parent | formatting (diff) | |
| download | glsl-view-f4ec8103c59b3e67273cd7a95ce42c752ec054e1.tar.gz glsl-view-f4ec8103c59b3e67273cd7a95ce42c752ec054e1.zip | |
cache textures by filename
Diffstat (limited to 'src')
| -rw-r--r-- | src/control.zig | 25 | ||||
| -rw-r--r-- | src/gl.zig | 31 |
2 files changed, 27 insertions, 29 deletions
diff --git a/src/control.zig b/src/control.zig index 72abf6f..2c272db 100644 --- a/src/control.zig +++ b/src/control.zig @@ -86,6 +86,7 @@ fn set_array( fn set_texture( progress: std.Progress.Node, + cache: *gl.UniformCache, dest: *?gl.Texture, texture_type: gl.Texture.Type, argv: [][*c]c.lo_arg, @@ -93,16 +94,18 @@ fn set_texture( ) !void { if (types.len != 1 or types[0] != 's') return error.invalidType; - if (dest.*) |old| { - old.destroy(); - dest.* = null; - } + const filenameZ: [*:0]const u8 = @ptrCast(&argv[0].*.s); + const filename = std.mem.span(filenameZ); - var buffer: [1024]u8 = undefined; - const filepath = try std.fs.cwd().realpathZ(@ptrCast(&argv[0].*.s), buffer[0..]); - buffer[filepath.len] = 0; + if (!cache.textures.contains(filename)) { + const key = try cache.allocator.dupe(u8, filename); + errdefer cache.allocator.free(key); + + const texture = try video.loadVideo(progress, filenameZ, texture_type); + try cache.textures.put(key, texture); + } - dest.* = try video.loadVideo(progress, @ptrCast(filepath), texture_type); + dest.* = cache.textures.get(filename); } pub const ControlServer = struct { @@ -237,18 +240,18 @@ pub const ControlServer = struct { .SAMPLER_2D_SHADOW, .INT_SAMPLER_2D, .UNSIGNED_INT_SAMPLER_2D, - => |val| set_texture(self.progress, val, .TEXTURE_2D, argv, types), + => |val| set_texture(self.progress, self.cache, val, .TEXTURE_2D, argv, types), .SAMPLER_2D_ARRAY, .SAMPLER_2D_ARRAY_SHADOW, .INT_SAMPLER_2D_ARRAY, .UNSIGNED_INT_SAMPLER_2D_ARRAY, - => |val| set_texture(self.progress, val, .TEXTURE_2D_ARRAY, argv, types), + => |val| set_texture(self.progress, self.cache, val, .TEXTURE_2D_ARRAY, argv, types), .SAMPLER_3D, .INT_SAMPLER_3D, .UNSIGNED_INT_SAMPLER_3D, - => |val| set_texture(self.progress, val, .TEXTURE_3D, argv, types), + => |val| set_texture(self.progress, self.cache, val, .TEXTURE_3D, argv, types), else => error.uniformNotSupported, }; @@ -377,7 +377,8 @@ const UniformValue = union(UniformType) { .UNSIGNED_INT_SAMPLER_BUFFER, .UNSIGNED_INT_SAMPLER_2D_RECT, => |value| { - if (value) |texture| texture.destroy(); + // if (value) |texture| texture.destroy(); + _ = value; }, else => {}, } @@ -529,21 +530,6 @@ pub const CachedUniform = struct { } } - // deinit self WITHOUT freeing contained resources - pub fn move( - self: *CachedUniform, - to: *CachedUniform, - allocator: std.mem.Allocator, - ) CachedUniform { - to.deinit(allocator); - to.buffer = self.buffer; - to.value = self.value; - - self.buffer = null; - - return self; - } - pub fn tryMove( self: *CachedUniform, dest: *CachedUniform, @@ -895,24 +881,33 @@ pub const ShaderProgram = struct { pub const UniformCache = struct { allocator: std.mem.Allocator, uniforms: std.StringHashMap(CachedUniform), + textures: std.StringHashMap(Texture), shader: *ShaderProgram, pub fn init(allocator: std.mem.Allocator, shader: *ShaderProgram) UniformCache { return UniformCache{ .allocator = allocator, .uniforms = std.StringHashMap(CachedUniform).init(allocator), + .textures = std.StringHashMap(Texture).init(allocator), .shader = shader, }; } pub fn deinit(self: *UniformCache) void { - var it = self.uniforms.iterator(); - while (it.next()) |entry| { + var uit = self.uniforms.iterator(); + while (uit.next()) |entry| { entry.value_ptr.deinit(self.allocator); self.allocator.free(entry.key_ptr.*); } + var tit = self.textures.iterator(); + while (tit.next()) |entry| { + entry.value_ptr.destroy(); + self.allocator.free(entry.key_ptr.*); + } + self.uniforms.deinit(); + self.textures.deinit(); } pub fn get(self: *UniformCache, name: []const u8) !?*CachedUniform { |
