cleaned up OGRE shaders
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 6 Dec 2022 03:46:51 +0000 (21:46 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 6 Dec 2022 03:46:51 +0000 (21:46 -0600)
14 files changed:
.gitignore
run_tree/assets/shaders/font.glsl [deleted file]
run_tree/assets/shaders/imgui.glsl [deleted file]
src/entity/entities.onyx
src/game.onyx
src/main.onyx
src/ogre/font.onyx
src/ogre/immediate.onyx
src/ogre/ogre.onyx
src/ogre/shader.onyx
src/ogre/shaders/font.glsl [new file with mode: 0644]
src/ogre/shaders/imgui.glsl [new file with mode: 0644]
src/ogre/texture.onyx
src/ogre/window.onyx

index 2f3539408e1e938cf106d82fa5f7ec4209156c62..3b21f3401d90d47db8abc1fc5db87468a3bdbef8 100644 (file)
@@ -4,3 +4,4 @@
 src/config.onyx
 *.wasm
 run_tree/lib/
+lib/
diff --git a/run_tree/assets/shaders/font.glsl b/run_tree/assets/shaders/font.glsl
deleted file mode 100644 (file)
index 8fdf5c9..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-precision mediump float;
-
-COMM vec2 v_texture;
-
-#ifdef VERTEX_SHADER
-
-layout(location = 0) in vec2 a_interp;
-layout(location = 1) in vec2 a_pos_top_left;
-layout(location = 2) in vec2 a_pos_bottom_right;
-layout(location = 3) in vec2 a_tex_top_left;
-layout(location = 4) in vec2 a_tex_bottom_right;
-
-layout(std140) uniform u_window_matrix_block {
-    mat4 u_window;
-};
-
-layout(std140) uniform u_world_matrix_block {
-    mat4 u_world;
-    mat4 u_model;
-};
-
-void main() {
-    gl_Position = u_window * u_world * u_model * vec4(mix(a_pos_top_left, a_pos_bottom_right, a_interp), 0, 1);
-    v_texture = mix(a_tex_top_left, a_tex_bottom_right, a_interp);
-}
-
-#endif
-
-#ifdef FRAGMENT_SHADER
-
-uniform sampler2D u_texture;
-uniform vec4      u_color;
-
-out vec4 fragColor;
-
-void main() {
-    fragColor = vec4(u_color.rgb, u_color.a * texture(u_texture, v_texture).a);
-}
-
-#endif
diff --git a/run_tree/assets/shaders/imgui.glsl b/run_tree/assets/shaders/imgui.glsl
deleted file mode 100644 (file)
index 26c0260..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-precision mediump float;
-
-COMM vec4 v_col;
-COMM vec2 v_tex;
-
-#ifdef VERTEX_SHADER
-
-layout(location = 0) in vec2 a_pos;
-layout(location = 1) in vec2 a_tex;
-layout(location = 2) in vec4 a_col;
-
-layout(std140) uniform u_window_matrix_block {
-    mat4 u_window;
-};
-
-layout(std140) uniform u_world_matrix_block {
-    mat4 u_world;
-    mat4 u_model;
-};
-
-void main() {
-    gl_Position = u_window * u_world * u_model * vec4(a_pos, 0, 1);
-    v_tex = a_tex;
-    v_col = a_col;
-}
-
-#endif
-
-#ifdef FRAGMENT_SHADER
-
-uniform sampler2D u_texture;
-uniform float u_texture_enabled;
-
-out vec4 fragColor;
-
-void main() {
-    fragColor = v_col * mix(vec4(1), texture(u_texture, v_tex), vec4(u_texture_enabled));
-}
-
-#endif
index 820d84bfdc726addcc28d11e35a07a67e53a482a..f191b82f13f9b60691fd1194362628052b621e30 100644 (file)
@@ -1,6 +1,5 @@
 
 use core
-use glfw3
 use ogre
 
 @Entity_Schematic.{ "Wall" }
index f798106474d0cb563f44b41e3da6f91fd493f629..b4bc5a30fccdd926530e9b8088272c8cedb7992f 100644 (file)
@@ -26,10 +26,7 @@ game_init :: () {
     Audio_Manager.set_volume(0.1);
 
     distortion_shader = shader_make("./assets/shaders/crt.glsl");
-    shader_link_window_matrix_block(distortion_shader);
-    shader_link_world_matrix_block(distortion_shader);
-
-    scene_canvas = canvas_make(800, 608);
+    scene_canvas      = canvas_make(800, 608);
 
     // This process of queueing the asset bucket should
     // be made automatic somehow...
index 70d80980ccaae2a06cef7c428cf2beb558d64dfb..0ee681ba046bea9126b74c7eda3a73b42f483d36 100644 (file)
@@ -12,10 +12,15 @@ window: Window
 #if DEBUG { debug_font: Font; }
 
 init :: () {
+    if !ogre_init() {
+        debug_log(.Critical, "Failed to initialize OGRE!");
+        os.exit(1);
+    }
+
     window = window_create(1200, 900, #cstr "Bar Simulator");
     window_use(^window);
 
-    ogre_init();
+    ogre_setup();
     editor_init();
     game_init();
 
@@ -30,7 +35,7 @@ update :: (dt: f32) {
     input_update();
 
     if is_key_just_up(GLFW_KEY_ESCAPE) {
-        glfwSetWindowShouldClose(window.glfw_window, true);
+        window->set_should_close(true);
         return;
     }
 
@@ -60,7 +65,7 @@ draw :: () {
             font_print(debug_font, ~~window.width - font_get_width(debug_font, version_str), 16, version_str);
         }
 
-        glfwSwapBuffers(window.glfw_window);
+        window->swap_buffers();
     }
 
     game_draw();
@@ -84,8 +89,8 @@ run :: () {
     seconds := 0.0;
     frame_count := 0;
 
-    while !glfwWindowShouldClose(window.glfw_window) {
-        glfwPollEvents();
+    while !(window->should_close()) {
+        window->poll_events();
 
         now = glfwGetTime();
         dt := now - last;
@@ -109,11 +114,6 @@ main :: () {
     random.set_seed(os.time());
 
     debug_set_level(.Info);
-    if !glfwInit() {
-        debug_log(.Critical, "Failed to initialize GLFW!");
-        os.exit(1);
-    }
-
     init();
     run();
     deinit();
index 9a9f0901f7a5c9ae8a3953639838b8a1366991f9..9e95308f80e7562f9094552af75b711b562ad92b 100644 (file)
@@ -16,10 +16,8 @@ use opengles
 fonts_init :: () {
     map.init(^font_registry);
 
-    font_shader = shader_make("./assets/shaders/font.glsl");
+    font_shader = shader_make_from_source(#file_contents "./shaders/font.glsl");
     shader_use(font_shader);
-    shader_link_window_matrix_block(font_shader);
-    shader_link_world_matrix_block(font_shader);
 
     glGenVertexArrays(1, ^font_vao);
     glBindVertexArray(font_vao);
index 0cd05056c4660cdec57eb40498e753740f22acaa..0f37c76ec5c96cac1661f1d7e1ae4063e5bc4703 100644 (file)
@@ -8,10 +8,7 @@ immediate_init :: () {
     memory.alloc_slice(^vertex_data, Maximum_Vertex_Count);
     vertex_count = 0;
 
-    imgui_shader = shader_make(Shader_Path);
-    shader_use(imgui_shader);
-    shader_link_window_matrix_block(imgui_shader);
-    shader_link_world_matrix_block(imgui_shader);
+    imgui_shader = shader_make_from_source(#file_contents "./shaders/imgui.glsl");
     shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 0.0f);
     shader_set_uniform(imgui_shader, #cstr "u_texture", 0);
 
@@ -172,7 +169,6 @@ Immediate_Vertex :: struct {
 }
 
 #local {
-    Shader_Path   :: "./assets/shaders/imgui.glsl"
     imgui_shader: Shader;
 
     Maximum_Vertex_Count :: 1023;
index 936d000bd7f32402d33b7a65d47b1441bfdb60f6..4fd10822e2d059aa0b90f0e69f40ae04a6032420 100644 (file)
@@ -1,6 +1,16 @@
 package ogre
 
-ogre_init :: () {
+use glfw3 {glfwInit}
+
+ogre_init :: () -> bool {
+    if !glfwInit() {
+        return false;
+    }
+
+    return true;
+}
+
+ogre_setup :: () {
     shaders_init();
     fonts_init();
     immediate_init();
index 449f6c856f660823542f80361602bd723df3510d..b6bf3fa6f4cd1b11ed6c1a1bd2e90b0d52439195 100644 (file)
@@ -38,7 +38,11 @@ shader_make_from_source :: (shader_source: str) -> Shader {
 
     prog := link_program(vs, fs);
 
-    return Shader.{vs, fs, prog};
+    s := Shader.{vs, fs, prog};
+    shader_link_window_matrix_block(s);
+    shader_link_world_matrix_block(s);
+
+    return s;
 }
 
 shader_use :: (shader: Shader) {
@@ -52,11 +56,15 @@ shader_use :: (shader: Shader) {
 
 shader_link_window_matrix_block :: (use shader: Shader) {
     matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_window_matrix_block");
+    if matrix_block_index == GL_INVALID_INDEX do return;
+
     glUniformBlockBinding(prog, matrix_block_index, WINDOW_MATRIX_BLOCK);
 }
 
 shader_link_world_matrix_block :: (use shader: Shader) {
     matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_world_matrix_block");
+    if matrix_block_index == GL_INVALID_INDEX do return;
+
     glUniformBlockBinding(prog, matrix_block_index, WORLD_MATRIX_BLOCK);
 }
 
diff --git a/src/ogre/shaders/font.glsl b/src/ogre/shaders/font.glsl
new file mode 100644 (file)
index 0000000..8fdf5c9
--- /dev/null
@@ -0,0 +1,40 @@
+precision mediump float;
+
+COMM vec2 v_texture;
+
+#ifdef VERTEX_SHADER
+
+layout(location = 0) in vec2 a_interp;
+layout(location = 1) in vec2 a_pos_top_left;
+layout(location = 2) in vec2 a_pos_bottom_right;
+layout(location = 3) in vec2 a_tex_top_left;
+layout(location = 4) in vec2 a_tex_bottom_right;
+
+layout(std140) uniform u_window_matrix_block {
+    mat4 u_window;
+};
+
+layout(std140) uniform u_world_matrix_block {
+    mat4 u_world;
+    mat4 u_model;
+};
+
+void main() {
+    gl_Position = u_window * u_world * u_model * vec4(mix(a_pos_top_left, a_pos_bottom_right, a_interp), 0, 1);
+    v_texture = mix(a_tex_top_left, a_tex_bottom_right, a_interp);
+}
+
+#endif
+
+#ifdef FRAGMENT_SHADER
+
+uniform sampler2D u_texture;
+uniform vec4      u_color;
+
+out vec4 fragColor;
+
+void main() {
+    fragColor = vec4(u_color.rgb, u_color.a * texture(u_texture, v_texture).a);
+}
+
+#endif
diff --git a/src/ogre/shaders/imgui.glsl b/src/ogre/shaders/imgui.glsl
new file mode 100644 (file)
index 0000000..26c0260
--- /dev/null
@@ -0,0 +1,40 @@
+precision mediump float;
+
+COMM vec4 v_col;
+COMM vec2 v_tex;
+
+#ifdef VERTEX_SHADER
+
+layout(location = 0) in vec2 a_pos;
+layout(location = 1) in vec2 a_tex;
+layout(location = 2) in vec4 a_col;
+
+layout(std140) uniform u_window_matrix_block {
+    mat4 u_window;
+};
+
+layout(std140) uniform u_world_matrix_block {
+    mat4 u_world;
+    mat4 u_model;
+};
+
+void main() {
+    gl_Position = u_window * u_world * u_model * vec4(a_pos, 0, 1);
+    v_tex = a_tex;
+    v_col = a_col;
+}
+
+#endif
+
+#ifdef FRAGMENT_SHADER
+
+uniform sampler2D u_texture;
+uniform float u_texture_enabled;
+
+out vec4 fragColor;
+
+void main() {
+    fragColor = v_col * mix(vec4(1), texture(u_texture, v_tex), vec4(u_texture_enabled));
+}
+
+#endif
index 64773cc7a0eae2ccad82e8361da8380bf8682486..dbf3157732995146d48df9a1386b024384b178ec 100644 (file)
@@ -12,8 +12,10 @@ Texture :: struct {
     filename: str;
 }
 
-texture_lookup :: #match {}
-#match texture_lookup (filename: str) -> (Texture, bool) {
+texture_lookup :: #match #local {}
+
+#overload
+texture_lookup :: (filename: str) -> (Texture, bool) {
     if texture_cache->has(filename) {
         return texture_cache[filename], true;
     }
@@ -23,7 +25,8 @@ texture_lookup :: #match {}
     return texture_lookup(cast(cstr) buffer);
 }
 
-#match texture_lookup (path: cstr) -> (Texture, bool) {
+#overload
+texture_lookup :: (path: cstr) -> (Texture, bool) {
     filename := string.from_cstr(path);
     if texture_cache->has(filename) {
         return texture_cache[filename], true;
index 486cf37feba62da12070633f57cb24e92c23e404..3c30bc055c1609baaea1cb3582b8eb5e88014636 100644 (file)
@@ -50,6 +50,29 @@ window_use :: (w: ^Window) {
     global_window = w;
 }
 
+window_should_close :: (w: ^Window) -> bool {
+    return glfwWindowShouldClose(w.glfw_window);
+}
+
+window_set_should_close :: (w: ^Window, b: bool) {
+    return glfwSetWindowShouldClose(w.glfw_window, b);
+}
+
+window_swap_buffers :: (w: ^Window) {
+    glfwSwapBuffers(w.glfw_window);
+}
+
+window_poll_events :: (_: ^Window) {
+    glfwPollEvents();
+}
+
+#inject Window {
+    should_close :: window_should_close;
+    set_should_close :: window_set_should_close;
+    swap_buffers :: window_swap_buffers;
+    poll_events :: window_poll_events;
+}
+
 //
 // These have to be macros because '#export_name' is used on the argument,
 // which requires that they are compile time known functions.