aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2026-04-10 22:23:19 +0000
committers-ol <s+removethis@s-ol.nu>2026-04-10 22:23:19 +0000
commitf2641e05588349d58ef6feaa2e174ca28f842450 (patch)
treeb3771ca9fd32c4c335e4a84c64c0850f10de7652 /src
parentsupport arrays of uniforms, remove finished sources (diff)
downloadglsl-view-f2641e05588349d58ef6feaa2e174ca28f842450.tar.gz
glsl-view-f2641e05588349d58ef6feaa2e174ca28f842450.zip
add some tests
Diffstat (limited to 'src')
-rw-r--r--src/gl.zig122
-rw-r--r--src/main.zig21
2 files changed, 133 insertions, 10 deletions
diff --git a/src/gl.zig b/src/gl.zig
index b5716c1..f8d3bff 100644
--- a/src/gl.zig
+++ b/src/gl.zig
@@ -735,6 +735,128 @@ pub const CachedUniform = struct {
}
};
+fn errorCallback(err: c_int, description: [*c]const u8) callconv(.c) void {
+ std.debug.panic("Error {}: {s}\n", .{ err, description });
+}
+
+fn setupTestContext() !*c.GLFWwindow {
+ _ = c.glfwSetErrorCallback(errorCallback);
+ try std.testing.expectEqual(c.GL_TRUE, c.glfwInit());
+
+ c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 4);
+ c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 2);
+ c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE);
+ c.glfwWindowHint(c.GLFW_OPENGL_DEBUG_CONTEXT, c.GL_TRUE);
+ c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE);
+ c.glfwWindowHint(c.GLFW_DEPTH_BITS, 0);
+ c.glfwWindowHint(c.GLFW_STENCIL_BITS, 0);
+ c.glfwWindowHint(c.GLFW_VISIBLE, c.GLFW_FALSE);
+
+ const window = c.glfwCreateWindow(800, 600, "glsl-view-test", null, null) orelse {
+ std.debug.panic("unable to create window\n", .{});
+ };
+
+ c.glfwMakeContextCurrent(window);
+ c.glfwSwapInterval(1);
+
+ return window;
+}
+
+fn teardownTestContext(window: *c.GLFWwindow) void {
+ c.glfwDestroyWindow(window);
+}
+
+test "shared uniform" {
+ const allocator = std.testing.allocator;
+
+ const ctx = try setupTestContext();
+ defer teardownTestContext(ctx);
+
+ const shader1 = try ShaderProgram.create(
+ \\#version 330 core
+ \\
+ \\uniform vec2 test_a;
+ \\uniform vec4 test_b[4];
+ \\uniform vec3 test_c = vec3(3, 2, 1);
+ \\
+ \\layout(location = 0) in vec2 position;
+ \\out vec2 uv;
+ \\
+ \\void main() {
+ \\ uv = position * test_a * test_c.xy;
+ \\ gl_Position = vec4(position, test_b[3]);
+ \\}
+ ,
+ \\#version 330 core
+ \\
+ \\in vec2 uv;
+ \\out vec4 color;
+ \\
+ \\uniform sampler2D colorTexture;
+ \\
+ \\void main(){
+ \\ color = texture(colorTexture, uv);
+ \\}
+ );
+ defer shader1.destroy();
+
+ var test_a1 = try CachedUniform.init(allocator, shader1, "test_a");
+ var test_b1 = try CachedUniform.init(allocator, shader1, "test_b");
+ var test_c1 = try CachedUniform.init(allocator, shader1, "test_c");
+
+ try std.testing.expectEqual(1, test_a1.value.FLOAT_VEC2.len);
+ test_a1.value.FLOAT_VEC2[0][0] = 1;
+ test_a1.value.FLOAT_VEC2[0][1] = 2;
+
+ try std.testing.expectEqualSlices(f32, &.{ 1, 2 }, test_a1.value.flatten().FLOAT);
+
+ try std.testing.expectEqual(4, test_b1.value.FLOAT_VEC4.len);
+
+ try std.testing.expectEqual(1, test_c1.value.FLOAT_VEC3.len);
+ test_c1.getShaderValue(shader1);
+ try std.testing.expectEqualSlices(f32, &.{ 3, 2, 1 }, &test_c1.value.FLOAT_VEC3[0]);
+ test_c1.deinit(allocator);
+
+ // iteration 2
+
+ const shader2 = try ShaderProgram.create(
+ \\#version 330 core
+ \\
+ \\uniform vec2 test_a;
+ \\uniform vec2 test_b[4];
+ \\
+ \\layout(location = 0) in vec2 position;
+ \\out vec2 uv;
+ \\
+ \\void main() {
+ \\ uv = position * test_a;
+ \\ gl_Position = vec4(position, test_b[0]);
+ \\}
+ ,
+ \\#version 330 core
+ \\
+ \\in vec2 uv;
+ \\out vec4 color;
+ \\
+ \\uniform sampler2D colorTexture;
+ \\
+ \\void main(){
+ \\ color = texture(colorTexture, uv);
+ \\}
+ );
+ defer shader2.destroy();
+
+ var test_a2 = try CachedUniform.init(allocator, shader2, "test_a");
+ var test_b2 = try CachedUniform.init(allocator, shader2, "test_b");
+
+ try std.testing.expectEqual(true, test_a1.tryMove(&test_a2, allocator));
+ try std.testing.expectEqualSlices(f32, &.{ 1, 2 }, test_a2.value.flatten().FLOAT);
+
+ try std.testing.expectEqual(false, test_b1.tryMove(&test_b2, allocator));
+ test_a2.deinit(allocator);
+ test_b2.deinit(allocator);
+}
+
pub const ShaderProgram = struct {
program_id: c.GLuint,
vert_id: c.GLuint,
diff --git a/src/main.zig b/src/main.zig
index fbe006e..f64c385 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,18 +1,15 @@
const std = @import("std");
-const debug = std.debug;
-const panic = debug.panic;
const c = @import("c.zig").c;
const debug_gl = @import("debug_gl.zig");
const cfg = @import("config.zig");
const out = @import("output.zig");
const gl = @import("gl.zig");
const ctrl = @import("control.zig");
-const c_allocator = std.heap.c_allocator;
var window: *c.GLFWwindow = undefined;
fn errorCallback(err: c_int, description: [*c]const u8) callconv(.c) void {
- panic("Error {}: {s}\n", .{ err, description });
+ std.debug.panic("Error {}: {s}\n", .{ err, description });
}
pub fn main() !void {
@@ -28,14 +25,14 @@ pub fn main() !void {
_ = c.glfwSetErrorCallback(errorCallback);
if (c.glfwInit() == c.GL_FALSE) {
- panic("GLFW init failure\n", .{});
+ std.debug.panic("GLFW init failure\n", .{});
}
defer c.glfwTerminate();
var monitor_count: c_int = 0;
const monitors = c.glfwGetMonitors(&monitor_count);
for (monitors[0..@as(usize, @intCast(monitor_count))], 0..) |monitor, i| {
- debug.print(
+ std.debug.print(
"monitor {}: '{s}'\n",
.{ i, @as([*:0]const u8, @ptrCast(c.glfwGetMonitorName(monitor))) },
);
@@ -51,7 +48,7 @@ pub fn main() !void {
c.glfwWindowHint(c.GLFW_VISIBLE, c.GLFW_FALSE);
window = c.glfwCreateWindow(config.width, config.height, "glsl-view", null, null) orelse {
- panic("unable to create window\n", .{});
+ std.debug.panic("unable to create window\n", .{});
};
defer c.glfwDestroyWindow(window);
@@ -111,7 +108,11 @@ pub fn main() !void {
// var last_stat = try shader_file.stat();
// try main_program.loadFile(config.fragment, last_stat.size);
- var cache = gl.UniformCache.init(std.heap.c_allocator, &main_program);
+ var debug_allocator = std.heap.DebugAllocator(.{}).init;
+ defer _ = debug_allocator.deinit();
+ const allocator = debug_allocator.allocator(); // std.heap.c_allocator
+
+ var cache = gl.UniformCache.init(allocator, &main_program);
defer cache.deinit();
const control = try ctrl.ControlServer.init(cfg_allocator, progress, &cache, &config, &constants);
@@ -123,9 +124,9 @@ pub fn main() !void {
var tex_buffer: ?[]u8 = null;
if (!stdout.isTty()) {
- tex_buffer = try std.heap.c_allocator.alloc(u8, @intCast(config.width * config.height * 4));
+ tex_buffer = try allocator.alloc(u8, @intCast(config.width * config.height * 4));
}
- defer if (tex_buffer) |buf| std.heap.c_allocator.free(buf);
+ defer if (tex_buffer) |buf| allocator.free(buf);
while (c.glfwWindowShouldClose(window) == c.GL_FALSE) {
c.glfwMakeContextCurrent(window);