split shader compilation to dedicated function
This commit is contained in:
parent
c9d750f583
commit
5fad1822d0
1 changed files with 89 additions and 122 deletions
211
src/main.zig
211
src/main.zig
|
@ -8,47 +8,6 @@ const gl = @cImport({
|
|||
@cInclude("GLFW/glfw3.h");
|
||||
});
|
||||
|
||||
const vertexShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ layout (location = 0) in vec3 aPos;
|
||||
\\ layout (location = 1) in vec3 aColor;
|
||||
\\ out vec2 vertexColor;
|
||||
\\
|
||||
\\ void main() {
|
||||
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
\\ vertexColor = vec2(aColor.x, aColor.y);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const fragementShaderSource: [*c]const u8 =
|
||||
\\ #version 330 core
|
||||
\\ out vec4 FragColor;
|
||||
\\
|
||||
\\ uniform float zed_c;
|
||||
\\ in vec2 vertexColor;
|
||||
\\ void main() {
|
||||
\\ FragColor = vec4(vertexColor.x, vertexColor.y, zed_c, 1.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const rectShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ layout (location = 0) in vec3 aPos;
|
||||
\\ out vec2 vertexColor;
|
||||
\\ void main() {
|
||||
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
\\ vertexColor = vec2(aPos.x * 2.0f, aPos.y * 2.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const yellowShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ out vec4 FragColor;
|
||||
\\ void main() {
|
||||
\\ FragColor = vec4(0.8f, 0.8f, 0.1f, 1.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const ShaderInitError = error{
|
||||
VertexCompilation,
|
||||
FragementCompilation,
|
||||
|
@ -121,7 +80,7 @@ pub fn main() void {
|
|||
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, 2 * 3 * @sizeOf(c_uint), @ptrCast(&draw_indices), gl.GL_STATIC_DRAW);
|
||||
|
||||
// ----- Simple Triangle ------
|
||||
const verticies: [3][6]f32 = .{ .{ -0.5, -0.5, 0.0, 1.0, 0.2, 0.0 }, .{ 0.5, -0.5, 0.0, 0.2, 1.0, 0.0 }, .{ 0.0, 0.5, 0.0, 0.2, 0.2, 0.0 } };
|
||||
const verticies: [3][6]f32 = .{ .{ -0.8, 0.0, 0.0, 1.0, 0.2, 0.0 }, .{ -0.4, 0.8, 0.0, 0.2, 1.0, 0.0 }, .{ 0.0, 0.0, 0.0, 0.2, 0.2, 0.0 } };
|
||||
|
||||
// Vertex array object
|
||||
var vao: c_uint = undefined;
|
||||
|
@ -223,87 +182,95 @@ fn handle_input(window: *gl.GLFWwindow) void {
|
|||
}
|
||||
}
|
||||
|
||||
const vertexShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ layout (location = 0) in vec3 aPos;
|
||||
\\ layout (location = 1) in vec3 aColor;
|
||||
\\ out vec2 vertexColor;
|
||||
\\
|
||||
\\ void main() {
|
||||
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
\\ vertexColor = vec2(aColor.x, aColor.y);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const fragementShaderSource: [*c]const u8 =
|
||||
\\ #version 330 core
|
||||
\\ out vec4 FragColor;
|
||||
\\
|
||||
\\ uniform float zed_c;
|
||||
\\ in vec2 vertexColor;
|
||||
\\ void main() {
|
||||
\\ FragColor = vec4(vertexColor.x, vertexColor.y, zed_c, 1.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const rectShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ layout (location = 0) in vec3 aPos;
|
||||
\\ out vec2 vertexColor;
|
||||
\\ void main() {
|
||||
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
\\ vertexColor = vec2(aPos.x * 2.0f, aPos.y * 2.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
const yellowShaderSource: [*c]const u8 =
|
||||
\\ #version 460 core
|
||||
\\ out vec4 FragColor;
|
||||
\\ void main() {
|
||||
\\ FragColor = vec4(0.8f, 0.8f, 0.1f, 1.0f);
|
||||
\\ }
|
||||
;
|
||||
|
||||
fn init_shader_program(orange_program: *const c_uint, yellow_program: *const c_uint) ShaderInitError!void {
|
||||
var success: c_int = undefined;
|
||||
var infoLog: [512:0]u8 = undefined;
|
||||
const triangle_verts = try compile_shader(gl.GL_VERTEX_SHADER, &vertexShaderSource, "Triangle Vertex", ShaderInitError.VertexCompilation);
|
||||
const triangle_frag = try compile_shader(gl.GL_FRAGMENT_SHADER, &fragementShaderSource, "Orange", ShaderInitError.FragementCompilation);
|
||||
try link_program(orange_program.*, triangle_verts, triangle_frag, "Triangle");
|
||||
|
||||
const vertex_shader: c_uint = gl.glCreateShader(gl.GL_VERTEX_SHADER);
|
||||
const rect_shader: c_uint = gl.glCreateShader(gl.GL_VERTEX_SHADER);
|
||||
const orange_shader: c_uint = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
|
||||
const yellow_shader: c_uint = gl.glCreateShader(gl.GL_FRAGMENT_SHADER);
|
||||
const rect_verts = try compile_shader(gl.GL_VERTEX_SHADER, &rectShaderSource, "Rectangle Vertex", ShaderInitError.VertexCompilation);
|
||||
const rect_frag = try compile_shader(gl.GL_FRAGMENT_SHADER, &yellowShaderSource, "Yellow", ShaderInitError.FragementCompilation);
|
||||
try link_program(yellow_program.*, rect_verts, rect_frag, "Rectangle");
|
||||
|
||||
std.debug.print("Shaders compiled\n", .{});
|
||||
}
|
||||
|
||||
// you know, I really don't like that basically everything is just a c_uint. It sucks to not have nice types
|
||||
fn compile_shader(shader_type: c_uint, source: *const [*c]const u8, debug_name: []const u8, err_type: ShaderInitError) ShaderInitError!c_uint {
|
||||
var success: c_int = undefined;
|
||||
|
||||
const shader: c_uint = gl.glCreateShader(shader_type);
|
||||
|
||||
gl.glShaderSource(shader, 1, source, null);
|
||||
gl.glCompileShader(shader);
|
||||
gl.glGetShaderiv(shader, gl.GL_COMPILE_STATUS, &success);
|
||||
std.debug.print("{s} shader compiled!\n", .{debug_name});
|
||||
if (success == 0) {
|
||||
var infoLog: [512:0]u8 = undefined;
|
||||
std.debug.print("{s} shader compile failed!\n", .{debug_name});
|
||||
gl.glGetShaderInfoLog(shader, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return err_type;
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
fn link_program(program: c_uint, vertex_shader: c_uint, fragment_shader: c_uint, debug_name: []const u8) ShaderInitError!void {
|
||||
var success: c_int = undefined;
|
||||
|
||||
gl.glAttachShader(program, vertex_shader);
|
||||
gl.glAttachShader(program, fragment_shader);
|
||||
gl.glLinkProgram(program);
|
||||
std.debug.print("{s} Program Linking Complete\n", .{debug_name});
|
||||
gl.glGetProgramiv(program, gl.GL_LINK_STATUS, &success);
|
||||
if (success == 0) {
|
||||
var infoLog: [512:0]u8 = undefined;
|
||||
std.debug.print("Yellow Shader linking failed!\n", .{});
|
||||
gl.glGetProgramInfoLog(program, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return ShaderInitError.ShaderProgramLinking;
|
||||
}
|
||||
gl.glDeleteShader(vertex_shader);
|
||||
gl.glDeleteShader(fragment_shader);
|
||||
|
||||
gl.glShaderSource(vertex_shader, 1, &vertexShaderSource, null);
|
||||
gl.glCompileShader(vertex_shader);
|
||||
gl.glGetShaderiv(vertex_shader, gl.GL_COMPILE_STATUS, &success);
|
||||
std.debug.print("Vertex Shader compiled\n", .{});
|
||||
if (success == 0) {
|
||||
std.debug.print("Shader compile failed!\n", .{});
|
||||
gl.glGetShaderInfoLog(vertex_shader, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}\n", .{infoLog});
|
||||
return ShaderInitError.VertexCompilation;
|
||||
}
|
||||
|
||||
gl.glShaderSource(orange_shader, 1, &fragementShaderSource, null);
|
||||
gl.glCompileShader(orange_shader);
|
||||
gl.glGetShaderiv(orange_shader, gl.GL_COMPILE_STATUS, &success);
|
||||
std.debug.print("Fragment Shader compiled\n", .{});
|
||||
if (success == 0) {
|
||||
std.debug.print("Shader compile failed!\n", .{});
|
||||
gl.glGetShaderInfoLog(orange_shader, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}\n", .{infoLog});
|
||||
return ShaderInitError.FragementCompilation;
|
||||
}
|
||||
|
||||
gl.glAttachShader(orange_program.*, vertex_shader);
|
||||
gl.glAttachShader(orange_program.*, orange_shader);
|
||||
gl.glLinkProgram(orange_program.*);
|
||||
std.debug.print("Shader Linking Complete\n", .{});
|
||||
gl.glGetProgramiv(orange_program.*, gl.GL_LINK_STATUS, &success);
|
||||
if (success == 0) {
|
||||
std.debug.print("Shader linking failed!\n", .{});
|
||||
gl.glGetProgramInfoLog(orange_program.*, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return ShaderInitError.ShaderProgramLinking;
|
||||
}
|
||||
|
||||
gl.glShaderSource(rect_shader, 1, &rectShaderSource, null);
|
||||
gl.glCompileShader(rect_shader);
|
||||
gl.glGetShaderiv(rect_shader, gl.GL_COMPILE_STATUS, &success);
|
||||
std.debug.print("Rectangle shader compiled!\n", .{});
|
||||
if (success == 0) {
|
||||
std.debug.print("Rectangle shader compile failed!\n", .{});
|
||||
gl.glGetShaderInfoLog(rect_shader, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return ShaderInitError.VertexCompilation;
|
||||
}
|
||||
|
||||
gl.glShaderSource(yellow_shader, 1, &yellowShaderSource, null);
|
||||
gl.glCompileShader(yellow_shader);
|
||||
gl.glGetShaderiv(yellow_shader, gl.GL_COMPILE_STATUS, &success);
|
||||
std.debug.print("Yellow Fragment shader compiled!\n", .{});
|
||||
if (success == 0) {
|
||||
std.debug.print("Yellow fragement shader compile failed!\n", .{});
|
||||
gl.glGetShaderInfoLog(yellow_shader, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return ShaderInitError.FragementCompilation;
|
||||
}
|
||||
|
||||
gl.glAttachShader(yellow_program.*, rect_shader);
|
||||
gl.glAttachShader(yellow_program.*, yellow_shader);
|
||||
gl.glLinkProgram(yellow_program.*);
|
||||
std.debug.print("Yellow Shader Linking Complete\n", .{});
|
||||
gl.glGetProgramiv(orange_program.*, gl.GL_LINK_STATUS, &success);
|
||||
if (success == 0) {
|
||||
std.debug.print("Yellow Shader linking failed!\n", .{});
|
||||
gl.glGetProgramInfoLog(orange_program.*, 512, null, &infoLog);
|
||||
std.debug.print("Error Log: {s}", .{infoLog});
|
||||
return ShaderInitError.ShaderProgramLinking;
|
||||
}
|
||||
|
||||
gl.glDeleteShader(rect_shader);
|
||||
gl.glDeleteShader(vertex_shader);
|
||||
gl.glDeleteShader(orange_shader);
|
||||
gl.glDeleteShader(yellow_shader);
|
||||
|
||||
std.debug.print("Shader program selected, cleaned up compiled shaders\n", .{});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue