From 6f83b811321575af3baa74addbaade8ce44549d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 20 Mar 2024 19:41:12 +0100 Subject: [PATCH] preprocess shaders --- demos/06-material.c | 7 +++++++ engine/art/shaders/fs_32_4_model.glsl | 7 ++++--- engine/joint/v4k.h | 25 ++++++++++++++++++++----- engine/split/v4k_render.c | 25 ++++++++++++++++++++----- engine/v4k.c | 25 ++++++++++++++++++++----- 5 files changed, 71 insertions(+), 18 deletions(-) diff --git a/demos/06-material.c b/demos/06-material.c index 00256c0..86615a0 100644 --- a/demos/06-material.c +++ b/demos/06-material.c @@ -12,6 +12,10 @@ int main() { // create camera camera_t cam = camera(); + + // fx: load all post fx files in all subdirs. + fx_load("fx**.fs"); + // load video, RGB texture, no audio video_t *v = video( "pexels-pachon-in-motion-17486489.mp4", VIDEO_RGB | VIDEO_NO_AUDIO | VIDEO_LOOP ); video_seek(v, 30); // load texture @@ -60,6 +64,7 @@ int main() { // load skybox scene_get_active()->skybox = skybox_pbr("hdr/Tokyo_BigSight_1k.hdr","hdr/Tokyo_BigSight_Env.hdr"); + while(window_swap() && !input(KEY_ESC)) { // draw environment ddraw_grid(0); @@ -71,7 +76,9 @@ int main() { light_teleport(l, cam.position); // draw scene + fx_begin(); scene_render(SCENE_FOREGROUND|SCENE_BACKGROUND|SCENE_UPDATE_SH_COEF); + fx_end(); // fps camera bool active = ui_active() || ui_hover() || gizmo_active() ? false : input(MOUSE_L) || input(MOUSE_M) || input(MOUSE_R); diff --git a/engine/art/shaders/fs_32_4_model.glsl b/engine/art/shaders/fs_32_4_model.glsl index 85c38e7..f7f953e 100644 --- a/engine/art/shaders/fs_32_4_model.glsl +++ b/engine/art/shaders/fs_32_4_model.glsl @@ -1,3 +1,4 @@ +#version 400 uniform mat4 model, view; uniform sampler2D u_texture2d; uniform vec3 u_coefficients_sh[9]; @@ -139,10 +140,10 @@ uniform ColorMap map_emissive; uniform sampler2D map_emissive_tex; #define sample_colormap(ColorMap_, uv_) \ (ColorMap_.has_tex ? texture( ColorMap_##_tex, uv_ ) : ColorMap_.color) -uniform float skysphere_rotation; +uniform float skysphere_rotation=-90; uniform float skysphere_mip_count; uniform float exposure=1; -// uniform uint frame_count; +uniform uint frame_count; uniform float specular_shininess; uniform sampler2D tex_skysphere; @@ -687,7 +688,7 @@ void main(void) // float dither = random( uvec3( floatBitsToUint( gl_FragCoord.xy ), frame_count ) ); // color += BOOST_NOISE * vec3( (-1.0/256.) + (2./256.) * dither ); -#if 0 // original +#if 1 // original // basic tonemap and gamma correction color = color / ( vec3(1.) + color ); color = pow( color, vec3(1. / 2.2) ); diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index edcd68d..b0b1877 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -370120,6 +370120,22 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); } +static inline +char *shader_preprocess(const char *src, const char *defines) { + const char *glsl_version = va("#version %s", ifdef(ems, "300 es", "150")); + + if (!src) return NULL; + + // detect GLSL version if set + if (src[0] == '#' && src[1] == 'v') { + const char *end = strstri(src, "\n"); + glsl_version = va("%.*s", (int)(end-src), src); + src = end+1; + } + + return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); +} + unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { PRINTF(/*"!"*/"Compiling shader\n"); @@ -370130,12 +370146,10 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char } } - const char *glsl_version = ifdef(ems, "300 es", "400"); - if(gs) - gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : ""); - vs = vs && vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, vs ? vs : ""); - fs = fs && fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, fs ? fs : ""); + gs = shader_preprocess(gs, glsl_defines); + vs = shader_preprocess(vs, glsl_defines); + fs = shader_preprocess(fs, glsl_defines); #if is(ems) { @@ -373053,6 +373067,7 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, shader_texture( "tex_skyenv", m.sky_env ); } shader_texture( "tex_brdf_lut", brdf_lut() ); + shader_uint( "frame_count", (unsigned)window_frame() ); } } static diff --git a/engine/split/v4k_render.c b/engine/split/v4k_render.c index 6940a77..ad641b5 100644 --- a/engine/split/v4k_render.c +++ b/engine/split/v4k_render.c @@ -92,6 +92,22 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); } +static inline +char *shader_preprocess(const char *src, const char *defines) { + const char *glsl_version = va("#version %s", ifdef(ems, "300 es", "150")); + + if (!src) return NULL; + + // detect GLSL version if set + if (src[0] == '#' && src[1] == 'v') { + const char *end = strstri(src, "\n"); + glsl_version = va("%.*s", (int)(end-src), src); + src = end+1; + } + + return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); +} + unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { PRINTF(/*"!"*/"Compiling shader\n"); @@ -102,12 +118,10 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char } } - const char *glsl_version = ifdef(ems, "300 es", "400"); - if(gs) - gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : ""); - vs = vs && vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, vs ? vs : ""); - fs = fs && fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, fs ? fs : ""); + gs = shader_preprocess(gs, glsl_defines); + vs = shader_preprocess(vs, glsl_defines); + fs = shader_preprocess(fs, glsl_defines); #if is(ems) { @@ -3025,6 +3039,7 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, shader_texture( "tex_skyenv", m.sky_env ); } shader_texture( "tex_brdf_lut", brdf_lut() ); + shader_uint( "frame_count", (unsigned)window_frame() ); } } static diff --git a/engine/v4k.c b/engine/v4k.c index 285bc81..ecc4b35 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -17266,6 +17266,22 @@ unsigned shader(const char *vs, const char *fs, const char *attribs, const char return shader_geom(NULL, vs, fs, attribs, fragcolor, defines); } +static inline +char *shader_preprocess(const char *src, const char *defines) { + const char *glsl_version = va("#version %s", ifdef(ems, "300 es", "150")); + + if (!src) return NULL; + + // detect GLSL version if set + if (src[0] == '#' && src[1] == 'v') { + const char *end = strstri(src, "\n"); + glsl_version = va("%.*s", (int)(end-src), src); + src = end+1; + } + + return va("%s\n%s\n%s", glsl_version, defines ? defines : "", src); +} + unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char *attribs, const char *fragcolor, const char *defines) { PRINTF(/*"!"*/"Compiling shader\n"); @@ -17276,12 +17292,10 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char } } - const char *glsl_version = ifdef(ems, "300 es", "400"); - if(gs) - gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : ""); - vs = vs && vs[0] == '#' && vs[1] == 'v' ? vs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, vs ? vs : ""); - fs = fs && fs[0] == '#' && fs[1] == 'v' ? fs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, fs ? fs : ""); + gs = shader_preprocess(gs, glsl_defines); + vs = shader_preprocess(vs, glsl_defines); + fs = shader_preprocess(fs, glsl_defines); #if is(ems) { @@ -20199,6 +20213,7 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view, shader_texture( "tex_skyenv", m.sky_env ); } shader_texture( "tex_brdf_lut", brdf_lut() ); + shader_uint( "frame_count", (unsigned)window_frame() ); } } static