renderer/opengl: wip

This commit is contained in:
Mitchell Hashimoto
2023-11-17 08:27:06 -08:00
parent 3aa217ad2e
commit 46dd084ee9
3 changed files with 41 additions and 6 deletions

View File

@ -8,6 +8,7 @@ const assert = std.debug.assert;
const testing = std.testing; const testing = std.testing;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator; const ArenaAllocator = std.heap.ArenaAllocator;
const shadertoy = @import("shadertoy.zig");
const apprt = @import("../apprt.zig"); const apprt = @import("../apprt.zig");
const configpkg = @import("../config.zig"); const configpkg = @import("../config.zig");
const font = @import("../font/main.zig"); const font = @import("../font/main.zig");
@ -320,7 +321,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL {
options.config.font_thicken, 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(); errdefer gl_state.deinit();
return OpenGL{ return OpenGL{
@ -439,7 +440,7 @@ pub fn displayRealize(self: *OpenGL) !void {
); );
// Make our new state // 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(); errdefer gl_state.deinit();
// Unrealize if we have to // Unrealize if we have to
@ -1579,7 +1580,33 @@ const GLState = struct {
texture: gl.Texture, texture: gl.Texture,
texture_color: 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 // Blending for text. We use GL_ONE here because we should be using
// premultiplied alpha for all our colors in our fragment shaders. // premultiplied alpha for all our colors in our fragment shaders.
// This avoids having a blurry border where transparency is expected on // This avoids having a blurry border where transparency is expected on

View File

@ -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);
}

View File

@ -9,7 +9,7 @@ const spvcross = @import("spirv_cross");
const log = std.log.scoped(.shadertoy); const log = std.log.scoped(.shadertoy);
/// The target to load shaders for. /// 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 /// Load a set of shaders from files and convert them to the target
/// format. The shader order is preserved. /// format. The shader order is preserved.
@ -87,6 +87,7 @@ pub fn loadFromFile(
return switch (target) { return switch (target) {
// Important: using the alloc_gpa here on purpose because this // Important: using the alloc_gpa here on purpose because this
// is the final result that will be returned to the caller. // is the final result that will be returned to the caller.
.glsl => try glslFromSpv(alloc_gpa, spirv),
.msl => try mslFromSpv(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( if (c.spvc_compiler_options_set_uint(
options, options,
c.SPVC_COMPILER_OPTION_GLSL_VERSION, c.SPVC_COMPILER_OPTION_GLSL_VERSION,
330, 430,
) != c.SPVC_SUCCESS) { ) != c.SPVC_SUCCESS) {
return error.SpvcFailed; return error.SpvcFailed;
} }
@ -349,7 +350,7 @@ test "shadertoy to glsl" {
const glsl = try glslFromSpv(alloc, spvlist.items); const glsl = try glslFromSpv(alloc, spvlist.items);
defer alloc.free(glsl); defer alloc.free(glsl);
//log.warn("glsl={s}", .{glsl}); log.warn("glsl={s}", .{glsl});
} }
const test_crt = @embedFile("shaders/test_shadertoy_crt.glsl"); const test_crt = @embedFile("shaders/test_shadertoy_crt.glsl");