diff --git a/glfw/CMakeLists.txt b/glfw/CMakeLists.txt index e81ec0a5..5d7d88ae 100644 --- a/glfw/CMakeLists.txt +++ b/glfw/CMakeLists.txt @@ -8,105 +8,110 @@ if (FIPS_CLANG OR FIPS_GCC) target_compile_options(flextgl12 PRIVATE -Wno-sign-conversion) endif() +fips_begin_lib(glfw_glue) + fips_files(glfw_glue.c glfw_glue.h) + fips_deps(glfw3) +fips_end_lib() + fips_begin_app(clear-glfw cmdline) fips_files(clear-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(triangle-glfw cmdline) fips_files(triangle-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(quad-glfw cmdline) fips_files(quad-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(cube-glfw cmdline) fips_files(cube-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(texcube-glfw cmdline) fips_files(texcube-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(instancing-glfw cmdline) fips_files(instancing-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(offscreen-glfw cmdline) fips_files(offscreen-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(mrt-glfw cmdline) fips_files(mrt-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(arraytex-glfw cmdline) fips_files(arraytex-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(imgui-glfw cmdline) fips_files(imgui-glfw.cc) - fips_deps(glfw3 imgui) + fips_deps(glfw_glue imgui) fips_end_app() fips_begin_app(dyntex-glfw cmdline) fips_files(dyntex-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(mipmap-glfw cmdline) fips_files(mipmap-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(blend-glfw cmdline) fips_files(blend-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(inject-glfw cmdline) fips_files(inject-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(bufferoffsets-glfw cmdline) fips_files(bufferoffsets-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(noninterleaved-glfw cmdline) fips_files(noninterleaved-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(multiwindow-glfw cmdline) fips_files(multiwindow-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(uniformarrays-glfw cmdline) fips_files(uniformarrays-glfw.c) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_end_app() fips_begin_app(sgl-test-glfw cmdline) fips_files(sgl-test-glfw.c) - fips_deps(glfw3 flextgl12) + fips_deps(glfw_glue flextgl12) fips_end_app() if (FIPS_MACOS) fips_begin_app(metal-glfw cmdline) fips_files(metal-glfw.m) - fips_deps(glfw3) + fips_deps(glfw_glue) fips_frameworks_osx(Cocoa QuartzCore Metal MetalKit AudioToolbox) fips_end_app() endif() diff --git a/glfw/arraytex-glfw.c b/glfw/arraytex-glfw.c index 2890cffc..fad88f85 100644 --- a/glfw/arraytex-glfw.c +++ b/glfw/arraytex-glfw.c @@ -8,8 +8,7 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" typedef struct { hmm_mat4 mvp; @@ -19,8 +18,6 @@ typedef struct { } params_t; enum { - WIDTH = 800, - HEIGHT = 600, IMG_LAYERS = 3, IMG_WIDTH = 16, IMG_HEIGHT = 16 @@ -28,19 +25,12 @@ enum { int main() { // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Textured Cube GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); - + glfw_init("arraytex-glfw.c", 800, 600, 1); // setup sokol_gfx - sg_desc desc = { .logger.func = slog_func }; - sg_setup(&desc); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); assert(sg_isvalid()); // a 16x16 array texture with 3 layers and a checkerboard pattern @@ -208,15 +198,15 @@ int main() { .colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.0f, 0.0f, 0.0f, 1.0f} } }; - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 10.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - params_t vs_params; float rx = 0.0f, ry = 0.0f; int frame_index = 0; - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(60.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 10.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + // rotated model matrix rx += 0.25f; ry += 0.5f; hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f)); @@ -231,16 +221,14 @@ int main() { vs_params.offset1 = HMM_Vec2(offset, -offset); vs_params.offset2 = HMM_Vec2(0.0f, 0.0f); - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params)); sg_draw(0, 36, 1); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); frame_index++; } diff --git a/glfw/blend-glfw.c b/glfw/blend-glfw.c index c6b37750..52940cfe 100644 --- a/glfw/blend-glfw.c +++ b/glfw/blend-glfw.c @@ -9,8 +9,7 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" typedef struct { hmm_mat4 mvp; @@ -24,24 +23,12 @@ enum { NUM_BLEND_FACTORS = 15 }; sg_pipeline pips[NUM_BLEND_FACTORS][NUM_BLEND_FACTORS]; int main() { - const int WIDTH = 800; - const int HEIGHT = 600; - const int MSAA_SAMPLES = 4; - // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, MSAA_SAMPLES); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Blend GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); - + glfw_init("blend-glfw.c", 800, 600, 4); // setup sokol_gfx (need to increase pipeline pool size) sg_desc desc = { .pipeline_pool_size = NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1, + .environment = glfw_environment(), .logger.func = slog_func, }; sg_setup(&desc); @@ -156,11 +143,6 @@ int main() { .stencil.load_action = SG_LOADACTION_DONTCARE }; - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(90.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 100.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 0.0f, 25.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - sg_bindings bind = { .vertex_buffers[0] = vbuf }; @@ -168,10 +150,13 @@ int main() { fs_params_t fs_params; float r = 0.0f; fs_params.tick = 0.0f; - while (!glfwWindowShouldClose(w)) { - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(90.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 100.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 0.0f, 25.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); // draw a background quad sg_apply_pipeline(bg_pip); @@ -198,7 +183,7 @@ int main() { } sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); r += 0.6f; fs_params.tick += 1.0f; diff --git a/glfw/bufferoffsets-glfw.c b/glfw/bufferoffsets-glfw.c index d2795617..586a025a 100644 --- a/glfw/bufferoffsets-glfw.c +++ b/glfw/bufferoffsets-glfw.c @@ -7,31 +7,21 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" typedef struct { float x, y, r, g, b; } vertex_t; int main() { - const int WIDTH = 640; - const int HEIGHT = 480; - // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Buffer Offsets GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("bufferoffsets-glfw.c", 640, 480, 1); // setup sokol_gfx - sg_desc desc = { .logger.func = slog_func }; - sg_setup(&desc); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); assert(sg_isvalid()); // a 2D triangle and quad in 1 vertex buffer and 1 index buffer @@ -104,10 +94,8 @@ int main() { } }; - while (!glfwWindowShouldClose(w)) { - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + while (!glfwWindowShouldClose(glfw_window())) { + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); // render the triangle bind.vertex_buffer_offsets[0] = 0; @@ -121,7 +109,7 @@ int main() { sg_draw(0, 6, 1); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } sg_shutdown(); diff --git a/glfw/clear-glfw.c b/glfw/clear-glfw.c index 6fc93ec8..83b09e36 100644 --- a/glfw/clear-glfw.c +++ b/glfw/clear-glfw.c @@ -6,26 +6,16 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" int main() { - const int WIDTH = 640; - const int HEIGHT = 480; - - // create window and GL context via GLFW - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Clear GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); - - // setup sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + // setup GLFW and sokol-gfx + glfw_init("clear-glfw.c", 640, 460, 1); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); assert(sg_isvalid()); // default pass action, clear to red @@ -34,16 +24,14 @@ int main() { }; // draw loop - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { float g = (float)(pass_action.colors[0].clear_value.g + 0.01); if (g > 1.0f) g = 0.0f; pass_action.colors[0].clear_value.g = g; - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } diff --git a/glfw/cube-glfw.c b/glfw/cube-glfw.c index 1dd26a47..95e97849 100644 --- a/glfw/cube-glfw.c +++ b/glfw/cube-glfw.c @@ -9,8 +9,7 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" // a uniform block with a model-view-projection matrix typedef struct { @@ -18,22 +17,14 @@ typedef struct { } params_t; int main() { - const int WIDTH = 800; - const int HEIGHT = 600; - // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Cube GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("cube-glfw.c", 800, 600, 4); // setup sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); assert(sg_isvalid()); // cube vertex buffer @@ -144,14 +135,14 @@ int main() { // default pass action sg_pass_action pass_action = { 0 }; - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 10.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - params_t vs_params; float rx = 0.0f, ry = 0.0f; - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(60.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 10.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + // rotated model matrix rx += 1.0f; ry += 2.0f; hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f)); @@ -161,16 +152,14 @@ int main() { // model-view-projection matrix for vertex shader vs_params.mvp = HMM_MultiplyMat4(view_proj, model); - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params)); sg_draw(0, 36, 1); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } diff --git a/glfw/dyntex-glfw.c b/glfw/dyntex-glfw.c index 2bab0d12..d69d3161 100644 --- a/glfw/dyntex-glfw.c +++ b/glfw/dyntex-glfw.c @@ -9,11 +9,7 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" - -const int DISPLAY_WIDTH = 640; -const int DISPLAY_HEIGHT = 480; +#include "glfw_glue.h" typedef struct { hmm_mat4 mvp; @@ -31,18 +27,13 @@ void game_of_life_update(); int main() { // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(DISPLAY_WIDTH, DISPLAY_HEIGHT, "Sokol Dynamic Texture GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("dyntex-glfw.c", 640, 480, 4); // init sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); // a 128x128 image with streaming-update strategy sg_image img = sg_make_image(&(sg_image_desc){ @@ -179,17 +170,17 @@ int main() { // default pass action (clear to grey) sg_pass_action pass_action = {0}; - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(60.0f, (float)DISPLAY_WIDTH/(float)DISPLAY_HEIGHT, 0.01f, 10.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - // initial game-of-life seed state game_of_life_init(); vs_params_t vs_params; float rx = 0.0f, ry = 0.0f; - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(60.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 10.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + // model-view-projection matrix from rotated model matrix rx += 0.1f; ry += 0.2f; hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f)); @@ -208,18 +199,14 @@ int main() { } }); - // get current window canvas size for the default pass - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params)); sg_draw(0, 36, 1); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } diff --git a/glfw/glfw_glue.c b/glfw/glfw_glue.c new file mode 100644 index 00000000..60302526 --- /dev/null +++ b/glfw/glfw_glue.c @@ -0,0 +1,61 @@ +#include "sokol_gfx.h" +#include "glfw_glue.h" + +static int _sample_count; +GLFWwindow* _window; + +void glfw_init(const char* title, int width, int height, int sample_count) { + _sample_count = sample_count; + glfwInit(); + glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, 0); + glfwWindowHint(GLFW_SAMPLES, (sample_count == 1) ? 0 : sample_count); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + _window = glfwCreateWindow(width, height, title, 0, 0); + glfwMakeContextCurrent(_window); + glfwSwapInterval(1); +} + +GLFWwindow* glfw_window(void) { + return _window; +} + +int glfw_width(void) { + int width, height; + glfwGetFramebufferSize(_window, &width, &height); + return width; +} + +int glfw_height(void) { + int width, height; + glfwGetFramebufferSize(_window, &width, &height); + return height; +} + +sg_environment glfw_environment(void) { + return (sg_environment) { + .defaults = { + .color_format = SG_PIXELFORMAT_RGBA8, + .depth_format = SG_PIXELFORMAT_DEPTH_STENCIL, + .sample_count = _sample_count, + }, + }; +} + +sg_swapchain glfw_swapchain(void) { + int width, height; + glfwGetFramebufferSize(_window, &width, &height); + return (sg_swapchain) { + .width = width, + .height = height, + .sample_count = _sample_count, + .color_format = SG_PIXELFORMAT_RGBA8, + .depth_format = SG_PIXELFORMAT_DEPTH_STENCIL, + .gl = { + // we just assume here that the GL framebuffer is always 0 + .framebuffer = 0, + } + }; +} diff --git a/glfw/glfw_glue.h b/glfw/glfw_glue.h new file mode 100644 index 00000000..53fd4b35 --- /dev/null +++ b/glfw/glfw_glue.h @@ -0,0 +1,15 @@ +#define GLFW_INCLUDE_NONE +#include + +#if defined(__cplusplus) +extern "C" { +#endif +void glfw_init(const char* title, int w, int h, int sample_count); +GLFWwindow* glfw_window(void); +int glfw_width(void); +int glfw_height(void); +sg_environment glfw_environment(void); +sg_swapchain glfw_swapchain(void); +#if defined(__cplusplus) +} // extern "C" +#endif diff --git a/glfw/imgui-glfw.cc b/glfw/imgui-glfw.cc index 84af8d95..e42f2082 100644 --- a/glfw/imgui-glfw.cc +++ b/glfw/imgui-glfw.cc @@ -9,8 +9,7 @@ #include "sokol_gfx.h" #include "sokol_time.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" const int Width = 1024; const int Height = 768; @@ -34,15 +33,8 @@ static void draw_imgui(ImDrawData*); int main() { // window and GL context via GLFW and flextGL - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE); - GLFWwindow* w = glfwCreateWindow(Width, Height, "Sokol+ImGui+GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("imgui-glfw.cc", Width, Height, 1); + GLFWwindow* w = glfw_window(); // GLFW to ImGui input forwarding glfwSetMouseButtonCallback(w, [](GLFWwindow*, int btn, int action, int /*mods*/) { @@ -73,6 +65,7 @@ int main() { // setup sokol_gfx and sokol_time stm_setup(); sg_desc desc = { }; + desc.environment = glfw_environment(); desc.logger.func = slog_func; sg_setup(&desc); assert(sg_isvalid()); @@ -187,12 +180,9 @@ int main() { // draw loop while (!glfwWindowShouldClose(w)) { - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - // this is standard ImGui demo code ImGuiIO& io = ImGui::GetIO(); - io.DisplaySize = ImVec2(float(cur_width), float(cur_height)); + io.DisplaySize = ImVec2(float(glfw_width()), float(glfw_height())); io.DeltaTime = (float) stm_sec(stm_laptime(&last_time)); ImGui::NewFrame(); @@ -221,7 +211,8 @@ int main() { } // the sokol_gfx draw pass - sg_begin_default_pass(&pass_action, cur_width, cur_height); + const sg_pass pass = { .action = pass_action, .swapchain = glfw_swapchain() }; + sg_begin_pass(pass); ImGui::Render(); draw_imgui(ImGui::GetDrawData()); sg_end_pass(); diff --git a/glfw/inject-glfw.c b/glfw/inject-glfw.c index bc8f5044..0b1ee340 100644 --- a/glfw/inject-glfw.c +++ b/glfw/inject-glfw.c @@ -9,13 +9,10 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" // constants (VS doesn't like "const int" for array size) enum { - WIDTH = 640, - HEIGHT = 480, IMG_WIDTH = 32, IMG_HEIGHT = 32, }; @@ -29,18 +26,13 @@ typedef struct { int main() { /* create GLFW window and initialize GL */ - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Injected Resources GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("inject-glfw.c", 640, 480, 4); // setup sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func + }); // create a native GL vertex and index buffer float vertices[] = { @@ -220,15 +212,15 @@ int main() { // default pass action sg_pass_action pass_action = { 0 }; - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 10.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - vs_params_t vs_params; float rx = 0.0f, ry = 0.0f; uint32_t counter = 0; - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(60.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 10.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + // rotated model matrix rx += 0.5f; ry += 0.75f; hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f)); @@ -251,16 +243,14 @@ int main() { counter++; sg_update_image(img, &(sg_image_data){ .subimage[0][0] = SG_RANGE(pixels) }); - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params)); sg_draw(0, 36, 1); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } diff --git a/glfw/instancing-glfw.c b/glfw/instancing-glfw.c index be3c6da5..f4f7365e 100644 --- a/glfw/instancing-glfw.c +++ b/glfw/instancing-glfw.c @@ -10,12 +10,9 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" enum { - WIDTH = 800, - HEIGHT = 600, MAX_PARTICLES = 512*1024, NUM_PARTICLES_EMITTED_PER_FRAME = 10 }; @@ -32,19 +29,13 @@ hmm_vec3 vel[MAX_PARTICLES]; int main() { - /* create window and GL context via GLFW */ - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Instancing GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); - + // create window and GL context via GLFW + glfw_init("instancing-glfw.c", 800, 600, 4); // setup sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); assert(sg_isvalid()); // vertex buffer for static geometry (goes into vertex buffer bind slot 0) @@ -138,19 +129,16 @@ int main() { .index_buffer = ibuf }; - // default pass action (clear to grey) - sg_pass_action pass_action = { 0 }; - - // view-projection matrix - hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 50.0f); - hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 12.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); - hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); - // draw loop vs_params_t vs_params; float roty = 0.0f; const float frame_time = 1.0f / 60.0f; - while (!glfwWindowShouldClose(w)) { + while (!glfwWindowShouldClose(glfw_window())) { + // view-projection matrix + hmm_mat4 proj = HMM_Perspective(60.0f, (float)glfw_width()/(float)glfw_height(), 0.01f, 50.0f); + hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 12.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); + hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view); + // emit new particles for (int i = 0; i < NUM_PARTICLES_EMITTED_PER_FRAME; i++) { if (cur_num_particles < MAX_PARTICLES) { @@ -189,16 +177,14 @@ int main() { roty += 1.0f; vs_params.mvp = HMM_MultiplyMat4(view_proj, HMM_Rotate(roty, HMM_Vec3(0.0f, 1.0f, 0.0f)));; - int cur_width, cur_height; - glfwGetFramebufferSize(w, &cur_width, &cur_height); - sg_begin_default_pass(&pass_action, cur_width, cur_height); + sg_begin_pass(&(sg_pass){ .swapchain = glfw_swapchain() }); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params)); sg_draw(0, 24, cur_num_particles); sg_end_pass(); sg_commit(); - glfwSwapBuffers(w); + glfwSwapBuffers(glfw_window()); glfwPollEvents(); } diff --git a/glfw/mipmap-glfw.c b/glfw/mipmap-glfw.c index e8b649a2..386eef04 100644 --- a/glfw/mipmap-glfw.c +++ b/glfw/mipmap-glfw.c @@ -11,8 +11,7 @@ #define SOKOL_GLCORE33 #include "sokol_gfx.h" #include "sokol_log.h" -#define GLFW_INCLUDE_NONE -#include "GLFW/glfw3.h" +#include "glfw_glue.h" typedef struct { hmm_mat4 mvp; @@ -43,24 +42,14 @@ uint32_t mip_colors[9] = { }; int main() { - const int WIDTH = 800; - const int HEIGHT = 600; - const int MSAA_SAMPLES = 4; - // create GLFW window and initialize GL - glfwInit(); - glfwWindowHint(GLFW_SAMPLES, MSAA_SAMPLES); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE); - GLFWwindow* w = glfwCreateWindow(WIDTH, HEIGHT, "Sokol Mipmapping GLFW", 0, 0); - glfwMakeContextCurrent(w); - glfwSwapInterval(1); + glfw_init("mipmap-glfw.c", 800, 600, 4); // setup sokol_gfx - sg_setup(&(sg_desc){ .logger.func = slog_func }); + sg_setup(&(sg_desc){ + .environment = glfw_environment(), + .logger.func = slog_func, + }); // a plane vertex buffer float vertices[] = { @@ -133,8 +122,8 @@ int main() { // the last 4 samplers use different anistropy levels smp_desc.min_lod = 0.0f; smp_desc.max_lod = 0.0f; // for max_lod, zero-initialized means "FLT_MAX" - smp_desc.min_filter = SG_FILTER_NEAREST; - smp_desc.mag_filter = SG_FILTER_NEAREST; + smp_desc.min_filter = SG_FILTER_LINEAR; + smp_desc.mag_filter = SG_FILTER_LINEAR; smp_desc.mipmap_filter = SG_FILTER_LINEAR; for (int i = 0; i < 4; i++) { smp_desc.max_anisotropy = 1<