From 46dd084ee9ba47997ed1f895fc2b0fc7bf39c480 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Nov 2023 08:27:06 -0800 Subject: [PATCH] renderer/opengl: wip --- src/renderer/OpenGL.zig | 33 +++++++++++++++++++++++++++--- src/renderer/shaders/custom.v.glsl | 7 +++++++ src/renderer/shadertoy.zig | 7 ++++--- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/renderer/shaders/custom.v.glsl diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index ce442ed48..7d91cc9a9 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -8,6 +8,7 @@ const assert = std.debug.assert; const testing = std.testing; const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; +const shadertoy = @import("shadertoy.zig"); const apprt = @import("../apprt.zig"); const configpkg = @import("../config.zig"); const font = @import("../font/main.zig"); @@ -320,7 +321,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL { options.config.font_thicken, ); - var gl_state = try GLState.init(options.font_group); + var gl_state = try GLState.init(alloc, options.config, options.font_group); errdefer gl_state.deinit(); return OpenGL{ @@ -439,7 +440,7 @@ pub fn displayRealize(self: *OpenGL) !void { ); // Make our new state - var gl_state = try GLState.init(self.font_group); + var gl_state = try GLState.init(self.alloc, self.config, self.font_group); errdefer gl_state.deinit(); // Unrealize if we have to @@ -1579,7 +1580,33 @@ const GLState = struct { texture: gl.Texture, texture_color: gl.Texture, - pub fn init(font_group: *font.GroupCache) !GLState { + pub fn init( + alloc: Allocator, + config: DerivedConfig, + font_group: *font.GroupCache, + ) !GLState { + var arena = ArenaAllocator.init(alloc); + defer arena.deinit(); + const arena_alloc = arena.allocator(); + + // Load our custom shaders + const custom_shaders: []const [:0]const u8 = shadertoy.loadFromFiles( + arena_alloc, + config.custom_shaders.items, + .glsl, + ) catch |err| err: { + log.warn("error loading custom shaders err={}", .{err}); + break :err &.{}; + }; + + if (custom_shaders.len > 0) { + const cp = try gl.Program.createVF( + @embedFile("shaders/custom.v.glsl"), + custom_shaders[0], + ); + _ = cp; + } + // Blending for text. We use GL_ONE here because we should be using // premultiplied alpha for all our colors in our fragment shaders. // This avoids having a blurry border where transparency is expected on diff --git a/src/renderer/shaders/custom.v.glsl b/src/renderer/shaders/custom.v.glsl new file mode 100644 index 000000000..ff6f60541 --- /dev/null +++ b/src/renderer/shaders/custom.v.glsl @@ -0,0 +1,7 @@ +#version 330 core + +layout (location = 0) in vec2 position; + +void main(){ + gl_Position = vec4(position.x, position.y, 0.0f, 1.0f); +} diff --git a/src/renderer/shadertoy.zig b/src/renderer/shadertoy.zig index 92b340e76..90541fda9 100644 --- a/src/renderer/shadertoy.zig +++ b/src/renderer/shadertoy.zig @@ -9,7 +9,7 @@ const spvcross = @import("spirv_cross"); const log = std.log.scoped(.shadertoy); /// The target to load shaders for. -pub const Target = enum { msl }; +pub const Target = enum { glsl, msl }; /// Load a set of shaders from files and convert them to the target /// format. The shader order is preserved. @@ -87,6 +87,7 @@ pub fn loadFromFile( return switch (target) { // Important: using the alloc_gpa here on purpose because this // is the final result that will be returned to the caller. + .glsl => try glslFromSpv(alloc_gpa, spirv), .msl => try mslFromSpv(alloc_gpa, spirv), }; } @@ -203,7 +204,7 @@ pub fn glslFromSpv(alloc: Allocator, spv: []const u8) ![:0]const u8 { if (c.spvc_compiler_options_set_uint( options, c.SPVC_COMPILER_OPTION_GLSL_VERSION, - 330, + 430, ) != c.SPVC_SUCCESS) { return error.SpvcFailed; } @@ -349,7 +350,7 @@ test "shadertoy to glsl" { const glsl = try glslFromSpv(alloc, spvlist.items); defer alloc.free(glsl); - //log.warn("glsl={s}", .{glsl}); + log.warn("glsl={s}", .{glsl}); } const test_crt = @embedFile("shaders/test_shadertoy_crt.glsl");