diff --git a/bind/v4k.lua b/bind/v4k.lua index 9319a63..8ce8872 100644 --- a/bind/v4k.lua +++ b/bind/v4k.lua @@ -1076,7 +1076,7 @@ enum TEXTURE_FLAGS { TEXTURE_RGB = IMAGE_RGB, TEXTURE_RGBA = IMAGE_RGBA, TEXTURE_FLIP = IMAGE_FLIP, - TEXTURE_NO_SRGB = 1 << 24, + TEXTURE_SRGB = 1 << 24, TEXTURE_BGR = 1 << 25, TEXTURE_BGRA = TEXTURE_BGR, TEXTURE_ARRAY = 1 << 26, @@ -1111,10 +1111,10 @@ typedef struct colormap_t { texture_t *texture; } colormap_t; bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ); - void fullscreen_quad_rgb( texture_t texture_rgb, float gamma ); - void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ); - void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3], float gamma ); - void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3], float gamma ); + void fullscreen_quad_rgb( texture_t texture_rgb ); + void fullscreen_quad_rgb_flipped( texture_t texture ); + void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3] ); + void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3] ); typedef struct cubemap_t { unsigned id; vec3 sh[9]; @@ -1668,9 +1668,6 @@ typedef struct spine_t spine_t; void spine_render(spine_t *p, vec3 offset, unsigned flags); void spine_animate(spine_t *p, float delta); void ui_spine(spine_t *p); -enum ATLAS_FLAGS { - ATLAS_SRGB = 2, -}; typedef struct atlas_frame_t { unsigned delay; vec4 sheet; @@ -1758,7 +1755,7 @@ typedef struct skinned_t { atlas_t atlas; float scale; } skinned_t; - guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb); + guiskin_t gui_skinned(const char *asefile, float scale); bool steam_init(unsigned app_id); void steam_tick(); void steam_trophy(const char *trophy_id, bool redeem); diff --git a/demos/01-demo2d.c b/demos/01-demo2d.c index 5fe4b2c..d474c66 100644 --- a/demos/01-demo2d.c +++ b/demos/01-demo2d.c @@ -50,7 +50,6 @@ int main() { // window (80% sized, MSAA x4 flag) window_create(80.0, WINDOW_MSAA4); window_title(__FILE__); - window_gamma(0); // tiled map tiled_t tmx = tiled(vfs_read("castle.tmx")); diff --git a/demos/01-font.c b/demos/01-font.c index 981dade..c8caafd 100644 --- a/demos/01-font.c +++ b/demos/01-font.c @@ -2,7 +2,6 @@ int main() { window_create(75.0, WINDOW_MSAA8); - window_gamma(0); // @fixme: disable gamma correction for now // style: our aliases #define FONT_REGULAR FONT_FACE1 diff --git a/demos/01-ui.c b/demos/01-ui.c index 7035670..384c0af 100644 --- a/demos/01-ui.c +++ b/demos/01-ui.c @@ -32,7 +32,7 @@ int main() { // video api: decode frame and get associated textures (audio is sent to audiomixer automatically) textures = video_decode( v ); // fullscreen video - // if(video_is_rgb(v)) fullscreen_quad_rgb( textures[0], 1.3f ); + // if(video_is_rgb(v)) fullscreen_quad_rgb( textures[0] ); // else fullscreen_quad_ycbcr( textures, 1.3f ); } diff --git a/demos/08-video.c b/demos/08-video.c index 65babd1..5ae6211 100644 --- a/demos/08-video.c +++ b/demos/08-video.c @@ -20,8 +20,8 @@ int main() { // present decoded textures as a fullscreen composed quad profile( "Video quad" ) { - if(is_rgb) fullscreen_quad_rgb( textures[0], 1.0 ); - else fullscreen_quad_ycbcr( textures, 1.0f/2.2f ); + if(is_rgb) fullscreen_quad_rgb( textures[0] ); + else fullscreen_quad_ycbcr( textures ); } // input controls diff --git a/demos/99-compute.c b/demos/99-compute.c index 8fc0e84..3bc1c9e 100644 --- a/demos/99-compute.c +++ b/demos/99-compute.c @@ -29,7 +29,7 @@ int main() { compute_dispatch(TEX_WIDTH/10, TEX_WIDTH/10, 1); write_barrier_image(); - fullscreen_quad_rgb(tex, 2.2); + fullscreen_quad_rgb(tex); } return 0; diff --git a/demos/99-gui.c b/demos/99-gui.c index 9dc0af3..dded0fd 100644 --- a/demos/99-gui.c +++ b/demos/99-gui.c @@ -3,7 +3,7 @@ int main() { window_create(75.0, 0 ); - gui_pushskin(gui_skinned("golden.ase", 3.0f, 1)); // x3 scale, sRGB enabled + gui_pushskin(gui_skinned("golden.ase", 3.0f)); // x3 scale skinned_t *skinned = (skinned_t*)gui_userdata(); vec4 pos = vec4(400,400,100, 30); diff --git a/demos/99-temperature.c b/demos/99-temperature.c index 8d930b1..e22e052 100644 --- a/demos/99-temperature.c +++ b/demos/99-temperature.c @@ -81,7 +81,7 @@ int main() { temp_calc(img); texture_update(&tex, TEX_WIDTH, TEX_WIDTH, 4, img, TEXTURE_LINEAR|TEXTURE_FLOAT); } - fullscreen_quad_rgb(tex, 2.2); + fullscreen_quad_rgb(tex); ddraw_text2d(vec2(0,0), va("mode: %s\nimage res: %d\ninputs: %.01f %.01f %.01f %.01f %s", TEMP_GPU?"GPU compute":"CPU", TEX_WIDTH, mouse.x, mouse.y, mouse.z, mouse.w, strong>1?"lshift":"")); ddraw_text2d(vec2(0,window_height() - 20), va("delta: %.2f ms", window_delta()*1000)); } diff --git a/demos/html5/demo_video.c b/demos/html5/demo_video.c index e39122a..3c6996b 100644 --- a/demos/html5/demo_video.c +++ b/demos/html5/demo_video.c @@ -11,7 +11,7 @@ void render(void *arg) { // present decoded textures as a fullscreen composed quad profile( "Video quad" ) { - fullscreen_quad_rgb( textures[0], 1.3f ); + fullscreen_quad_rgb( textures[0] ); } // ui video diff --git a/demos/ports/anarch/game.c b/demos/ports/anarch/game.c index 534ab46..5ee84cd 100644 --- a/demos/ports/anarch/game.c +++ b/demos/ports/anarch/game.c @@ -139,7 +139,7 @@ int main() { while( window_swap() && SFG_mainLoopBody() ) { texture_update(&t, SFG_SCREEN_RESOLUTION_X, SFG_SCREEN_RESOLUTION_Y, 4, screen, TEXTURE_RGB|TEXTURE_NEAREST); - fullscreen_quad_rgb(t, 1.0f); + fullscreen_quad_rgb(t); uint16_t samples[128]; // 8 KHz 16-bit mono = 8000/60 = 133 samples/frame -> 256 samples/frame audioFillCallback(NULL, (uint8_t*)samples, sizeof(samples)); diff --git a/demos/ports/doom/game.c b/demos/ports/doom/game.c index 9f0c9d3..c028b2b 100644 --- a/demos/ports/doom/game.c +++ b/demos/ports/doom/game.c @@ -129,7 +129,7 @@ void logic() { void blit() { static texture_t screen; do_once screen = texture_checker(); texture_update(&screen, DOOM_WIDTH, DOOM_HEIGHT, 4, doom_get_framebuffer(4), TEXTURE_RGBA/*|TEXTURE_LINEAR*/); - fullscreen_quad_rgb(screen, 1.0f); // gamma(1) + fullscreen_quad_rgb(screen); // gamma(1) } void audio() { diff --git a/engine/art/fx/fxGamma.fs b/engine/art/fx/fxGamma.fs index b4e1c1f..eb71a34 100644 --- a/engine/art/fx/fxGamma.fs +++ b/engine/art/fx/fxGamma.fs @@ -1,8 +1,8 @@ -uniform float u_gamma; +uniform float u_gamma; /// set:2.2 out vec4 color; void main(void) { vec2 uv = TEXCOORD.st; vec4 src = texture2D(iChannel0, uv); - color = vec4( pow(src.xyz, vec3(u_gamma)), src.a); // gamma correction + color = vec4( pow(src.xyz, vec3(1.0/u_gamma)), src.a); // gamma correction } diff --git a/engine/art/shaders/fs_2_4_texel_inv_gamma.glsl b/engine/art/shaders/fs_2_4_texel_inv_gamma.glsl index d7abfb0..946ec8e 100644 --- a/engine/art/shaders/fs_2_4_texel_inv_gamma.glsl +++ b/engine/art/shaders/fs_2_4_texel_inv_gamma.glsl @@ -1,13 +1,9 @@ uniform sampler2D texture0; /*unit0*/ -uniform float u_inv_gamma; - in vec2 uv; out vec4 fragcolor; - void main() { vec4 texel = texture( texture0, uv ); fragcolor = texel; - fragcolor.rgb = pow( fragcolor.rgb, vec3( u_inv_gamma ) ); // defaults: 1.0/2.2 gamma } \ No newline at end of file diff --git a/engine/art/shaders/fs_2_4_texel_ycbr_gamma_saturation.glsl b/engine/art/shaders/fs_2_4_texel_ycbr_gamma_saturation.glsl index 5f7e2dd..60d4d2d 100644 --- a/engine/art/shaders/fs_2_4_texel_ycbr_gamma_saturation.glsl +++ b/engine/art/shaders/fs_2_4_texel_ycbr_gamma_saturation.glsl @@ -1,7 +1,7 @@ uniform sampler2D u_texture_y; /*unit0*/ uniform sampler2D u_texture_cb; /*unit1*/ uniform sampler2D u_texture_cr; /*unit2*/ -uniform float u_gamma; +// uniform float u_gamma; /// set:2.2 in vec2 uv; @@ -27,13 +27,11 @@ void main() { dot( vec3( 1.0, 1.772, 0.0 ), yCbCr ), 1.0); */ // gamma correction - texel.rgb = pow(texel.rgb, vec3(1.0 / u_gamma)); + // texel.rgb = pow(texel.rgb, vec3(u_gamma)); // saturation (algorithm from Chapter 16 of OpenGL Shading Language) if(false) { float saturation = 2.0; const vec3 W = vec3(0.2125, 0.7154, 0.0721); vec3 intensity = vec3(dot(texel.rgb, W)); -texel.rgb = mix(intensity, texel.rgb, saturation); } - - -fragcolor = vec4(texel.rgb, 1.0); + texel.rgb = mix(intensity, texel.rgb, saturation); } + fragcolor = vec4(texel.rgb, 1.0); } \ No newline at end of file diff --git a/engine/art/shaders/fs_32_4_model.glsl b/engine/art/shaders/fs_32_4_model.glsl index 2133565..4f3445c 100644 --- a/engine/art/shaders/fs_32_4_model.glsl +++ b/engine/art/shaders/fs_32_4_model.glsl @@ -702,8 +702,8 @@ void main(void) float e = 0.14f; color = clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0); // gamma correction - color = pow( color, vec3(1. / 2.2) ); #endif + color = pow( color, vec3(1. / 2.2) ); // Technically this alpha may be too transparent, if there is a lot of reflected light we wouldn't // see the background, maybe we can approximate it well enough by adding a fresnel term diff --git a/engine/art/shaders/fs_font.glsl b/engine/art/shaders/fs_font.glsl index b398e3d..0021e20 100644 --- a/engine/art/shaders/fs_font.glsl +++ b/engine/art/shaders/fs_font.glsl @@ -4,7 +4,6 @@ in float color_index; uniform sampler2D sampler_font; uniform sampler1D sampler_colors; uniform float num_colors; -uniform float u_gamma; /// set:2.2 out vec4 outColor; @@ -12,5 +11,4 @@ void main() { vec4 col = texture(sampler_colors, (color_index+0.5)/num_colors); float s = texture(sampler_font, uv).r; outColor = vec4(col.rgb, s*col.a); - outColor.rgb = pow(outColor.rgb, vec3(u_gamma)); } diff --git a/engine/art/shaders/fs_main_shadertoy.glsl b/engine/art/shaders/fs_main_shadertoy.glsl index 164adb7..ed48631 100644 --- a/engine/art/shaders/fs_main_shadertoy.glsl +++ b/engine/art/shaders/fs_main_shadertoy.glsl @@ -1,5 +1,4 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord ); void main() { mainImage(fragColor, texcoord.xy * iResolution); - fragColor.rgb = pow(fragColor.rgb, vec3(1.0/2.2)); } \ No newline at end of file diff --git a/engine/art/shaders/header_shadertoy.glsl b/engine/art/shaders/header_shadertoy.glsl index 19f96eb..9930886 100644 --- a/engine/art/shaders/header_shadertoy.glsl +++ b/engine/art/shaders/header_shadertoy.glsl @@ -16,11 +16,10 @@ uniform sampler2D iChannel0; // input channel 0 /*sampler%s*/ uniform sampler2D iChannel1; // input channel 1 uniform sampler2D iChannel2; // input channel 2 uniform sampler2D iChannel3; // input channel 3 -uniform float iGamma; /// set:2.2 + in vec2 texCoord; out vec4 fragColor; void mainImage( out vec4 fragColor, in vec2 fragCoord ); void main() { mainImage(fragColor, texCoord.xy); - fragColor.rgb = pow(fragColor.rgb, vec3(iGamma)); } diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index a21e282..f1271d2 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -17130,7 +17130,7 @@ enum TEXTURE_FLAGS { TEXTURE_FLIP = IMAGE_FLIP, // @fixme - TEXTURE_NO_SRGB = 1 << 24, + TEXTURE_SRGB = 1 << 24, TEXTURE_BGR = 1 << 25, TEXTURE_BGRA = TEXTURE_BGR, TEXTURE_ARRAY = 1 << 26, @@ -17184,10 +17184,10 @@ API bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) // ----------------------------------------------------------------------------- // fullscreen quads -API void fullscreen_quad_rgb( texture_t texture_rgb, float gamma ); -API void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ); -API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3], float gamma ); -API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3], float gamma ); +API void fullscreen_quad_rgb( texture_t texture_rgb ); +API void fullscreen_quad_rgb_flipped( texture_t texture ); +API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3] ); +API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3] ); // ----------------------------------------------------------------------------- // cubemaps @@ -18105,10 +18105,6 @@ API void ui_spine(spine_t *p); // ---------------------------------------------------------------------------- // atlas api -enum ATLAS_FLAGS { - ATLAS_SRGB = 2, -}; - typedef struct atlas_frame_t { unsigned delay; vec4 sheet; @@ -18250,7 +18246,7 @@ typedef struct skinned_t { // - "_hover" (ex. "slider_cursor_hover") // - "_press" // -API guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb); +API guiskin_t gui_skinned(const char *asefile, float scale); #line 0 #line 1 "v4k_steam.h" @@ -363509,7 +363505,6 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor); glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x); glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor); - glUniform1f(glGetUniformLocation(f->program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); GLint dims[4] = {0}; glGetIntegerv(GL_VIEWPORT, dims); @@ -364100,7 +364095,7 @@ API void gui_drawrect( texture_t spritesheet, vec2 tex_start, vec2 tex_end, int void gui_drawrect( texture_t texture, vec2 tex_start, vec2 tex_end, int rgba, vec2 start, vec2 end ) { static int program = -1, vbo = -1, vao = -1, u_tint = -1, u_has_tex = -1, u_window_width = -1, u_window_height = -1; - float gamma = window_get_gamma(); + float gamma = 1; vec2 dpi = ifdef(osx, window_dpi(), vec2(1,1)); if( program < 0 ) { const char* vs = vfs_read("shaders/rect_2d.vs"); @@ -364554,9 +364549,9 @@ void skinned_getscissorrect(void* userdata, const char *skin, const char *fallba dims->w -= (skinsize.y - coresize.y); } -guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb) { +guiskin_t gui_skinned(const char *asefile, float scale) { skinned_t *a = REALLOC(0, sizeof(skinned_t)); - a->atlas = atlas_create(asefile, load_as_srgb?ATLAS_SRGB:0); + a->atlas = atlas_create(asefile, 0); a->scale = scale?scale:1.0f; guiskin_t skin={0}; skin.userdata = a; @@ -370575,7 +370570,12 @@ void shader_vec3(const char *uniform, vec3 v) { glUniform3fv(shader_uniform(un void shader_vec3v(const char *uniform, int count, vec3 *v) { glUniform3fv(shader_uniform(uniform), count, &v[0].x); } void shader_vec4(const char *uniform, vec4 v) { glUniform4fv(shader_uniform(uniform), 1, &v.x); } void shader_mat44(const char *uniform, mat44 m) { glUniformMatrix4fv(shader_uniform(uniform), 1, GL_FALSE/*GL_TRUE*/, m); } -void shader_cubemap(const char *sampler, unsigned texture) { glUniform1i(shader_uniform(sampler), 0); glBindTexture(GL_TEXTURE_CUBE_MAP, texture); } +void shader_cubemap(const char *sampler, unsigned texture) { + int id = texture_unit(); + glUniform1i(shader_uniform(sampler), id); + glActiveTexture(GL_TEXTURE0 + id); + glBindTexture(GL_TEXTURE_CUBE_MAP, texture); +} void shader_bool(const char *uniform, bool x) { glUniform1i(shader_uniform(uniform), x); } void shader_uint(const char *uniform, unsigned x ) { glUniform1ui(shader_uniform(uniform), x); } void shader_texture(const char *sampler, texture_t t) { shader_texture_unit(sampler, t.id, texture_unit()); } @@ -370743,8 +370743,8 @@ unsigned texture_update(texture_t *t, unsigned w, unsigned h, unsigned n, const if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGB ) pixel_type = GL_BGR; if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGBA ) pixel_type = GL_BGRA; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? if( flags & TEXTURE_BC1 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; if( flags & TEXTURE_BC2 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; @@ -371345,7 +371345,7 @@ void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float bottom, f // usage: bind empty vao & commit call for 6 (quad) or 3 vertices (tri). // ie, glBindVertexArray(empty_vao); glDrawArrays(GL_TRIANGLES, 0, 3); -void fullscreen_quad_rgb( texture_t texture, float gamma ) { +void fullscreen_quad_rgb( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -371359,7 +371359,8 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -371376,7 +371377,7 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { +void fullscreen_quad_rgb_flipped( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -371390,7 +371391,8 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -371407,7 +371409,7 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -371425,7 +371427,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -371451,7 +371453,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -371469,7 +371471,7 @@ void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -371574,12 +371576,11 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { glGenTextures(1, &c.id); glBindTexture(GL_TEXTURE_CUBE_MAP, c.id); - float gammabg = window_get_gamma() + !window_get_gamma(); int samples = 0; for (int i = 0; i < 6; i++) { image_t img = images[i]; //image(textures[i], IMAGE_RGB); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); // calculate SH coefficients (@ands) const vec3 skyDir[] = {{ 1, 0, 0},{-1, 0, 0},{ 0, 1, 0},{ 0,-1, 0},{ 0, 0, 1},{ 0, 0,-1}}; @@ -371596,9 +371597,6 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { skyDir[i]); // texelDirection; float l = len3(n); vec3 light = scale3(vec3(p[0], p[1], p[2]), 1 / (255.0f * l * l * l)); // texelSolidAngle * texel_radiance; - light.x = powf(light.x, gammabg); - light.y = powf(light.y, gammabg); - light.z = powf(light.z, gammabg); n = norm3(n); c.sh[0] = add3(c.sh[0], scale3(light, 0.282095f)); c.sh[1] = add3(c.sh[1], scale3(light, -0.488603f * n.y * 2.0 / 3.0)); @@ -371890,8 +371888,6 @@ int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view) { shader_mat44("u_mvp", mvp); if( sky->flags ) { shader_cubemap("u_cubemap", sky->cubemap.id); - } else { - shader_float("u_gamma", window_get_gamma() + !window_get_gamma()); } return 0; // @fixme: return sortable hash here? } @@ -372149,6 +372145,9 @@ void viewport_clip(vec2 from, vec2 to) { // fbos unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { + int last_fb; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &last_fb); + GLuint fbo; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); @@ -372197,14 +372196,21 @@ unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { } #endif - glBindFramebuffer (GL_FRAMEBUFFER, 0); + glBindFramebuffer (GL_FRAMEBUFFER, last_fb); return fbo; } +static __thread array(handle) fbos; void fbo_bind(unsigned id) { glBindFramebuffer(GL_FRAMEBUFFER, id); + array_push(fbos, id); } void fbo_unbind() { - fbo_bind(0); + handle id = 0; + if (array_count(fbos)) { + array_pop(fbos); + id = *array_back(fbos); + } + glBindFramebuffer(GL_FRAMEBUFFER, id); } void fbo_destroy(unsigned id) { // glDeleteRenderbuffers(1, &renderbuffer); @@ -372348,7 +372354,7 @@ int postfx_load_from_mem( postfx *fx, const char *name, const char *fs ) { if( p->uniforms[u_channelres1x] == -1 ) p->uniforms[u_channelres1x] = glGetUniformLocation(p->program, "iChannelRes1x"); if( p->uniforms[u_channelres1y] == -1 ) p->uniforms[u_channelres1y] = glGetUniformLocation(p->program, "iChannelRes1y"); - + // set quad glGenVertexArrays(1, &p->m.vao); return slot; @@ -372383,20 +372389,13 @@ int ui_postfx(postfx *fx, int pass) { return rc; } -static __thread array(handle) last_fb; -static postfx gamma_fx; - bool postfx_begin(postfx *fx, int width, int height) { // reset clear color: needed in case transparent window is being used (alpha != 0) - if (fx != &gamma_fx) glClearColor(0,0,0,0); // @transparent + glClearColor(0,0,0,0); // @transparent width += !width; height += !height; - int fb; - glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fb); - array_push(last_fb, fb); - // resize if needed if( fx->diffuse[0].w != width || fx->diffuse[0].h != height ) { texture_destroy(&fx->diffuse[0]); @@ -372420,20 +372419,18 @@ bool postfx_begin(postfx *fx, int width, int height) { uint64_t num_active_passes = popcnt64(fx->mask); bool active = fx->enabled && num_active_passes; if( !active ) { - array_pop(last_fb); - fbo_bind(fb); return false; } fbo_bind(fx->fb[1]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + fbo_unbind(); fbo_bind(fx->fb[0]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + // we keep fbo_0 bound so that user can render into it. return true; } @@ -372445,9 +372442,8 @@ bool postfx_end(postfx *fx) { return false; } - handle fb = *array_back(last_fb); - array_pop(last_fb); - fbo_bind(fb); + // unbind postfx fbo + fbo_unbind(); // disable depth test in 2d rendering bool is_depth_test_enabled = glIsEnabled(GL_DEPTH_TEST); @@ -372474,7 +372470,7 @@ bool postfx_end(postfx *fx) { glUniform1f(pass->uniforms[u_channelres0x], fx->diffuse[frame].w); glUniform1f(pass->uniforms[u_channelres0y], fx->diffuse[frame].h); - + // bind depth to texture unit 1 // shader_texture_unit(fx->depth[frame], 1); glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, fx->depth[frame].id); @@ -372492,7 +372488,7 @@ bool postfx_end(postfx *fx) { // bind the vao int bound = --num_active_passes; - if( bound ) fbo_bind(fx->fb[frame ^= 1]); + if (bound) fbo_bind(fx->fb[frame ^= 1]); // fullscreen quad glBindVertexArray(pass->m.vao); @@ -372501,10 +372497,10 @@ bool postfx_end(postfx *fx) { profile_incstat("Render.num_triangles", +2); glBindVertexArray(0); - if( bound ) fbo_bind(fb); - else glUseProgram(0); + if (bound) fbo_unbind(); } } + glUseProgram(0); if(is_depth_test_enabled); glEnable(GL_DEPTH_TEST); @@ -372611,9 +372607,9 @@ bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) { FREE(cm->texture), cm->texture = NULL; } - int srgb = !load_as_srgb ? TEXTURE_NO_SRGB : 0; - int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0; - texture_t t = texture_compressed(texture_name, TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); + int srgb = load_as_srgb ? TEXTURE_SRGB : 0; + int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT : 0; + texture_t t = texture_compressed(texture_name, TEXTURE_RGBA | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); if( t.id == texture_checker().id ) { cm->texture = NULL; @@ -372652,7 +372648,6 @@ enum shadertoy_uniforms { iSampleRate, iChannelResolution, iChannelTime, - iGamma, // iCameraScreen // iCameraPosition // iCameraActive @@ -372694,7 +372689,6 @@ shadertoy_t shadertoy( const char *shaderfile, unsigned flags ) { s.uniforms[iSampleRate] = glGetUniformLocation(s.program, "iSampleRate"); s.uniforms[iChannelResolution] = glGetUniformLocation(s.program, "iChannelResolution"); s.uniforms[iChannelTime] = glGetUniformLocation(s.program, "iChannelTime"); - s.uniforms[iGamma] = glGetUniformLocation(s.program, "iGamma"); return s; } @@ -372722,7 +372716,6 @@ shadertoy_t* shadertoy_render(shadertoy_t *s, float delta) { glUniform1i(s->uniforms[iFrame], (int)window_frame()); glUniform1f(s->uniforms[iTime], time_ss()); - glUniform1f(s->uniforms[iGamma], window_get_gamma() + !window_get_gamma()); glUniform4f(s->uniforms[iDate], tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_sec + tm->tm_min * 60 + tm->tm_hour * 3600); int unit = 0; @@ -374046,10 +374039,8 @@ static const char *dd_fs = "//" FILELINE "\n" // "precision mediump float;\n" "in vec3 out_color;\n" "out vec4 fragcolor;\n" - "uniform float u_gamma; /// set:2.2\n" "void main() {\n" " fragcolor = vec4(out_color, 1.0);\n" - " fragcolor.rgb = pow(fragcolor.rgb, vec3(u_gamma));\n" "}"; #define X(x) RGBX(x,255) @@ -374109,7 +374100,6 @@ void ddraw_flush_projview(mat44 proj, mat44 view) { glUseProgram(dd_program); glUniformMatrix4fv(glGetUniformLocation(dd_program, "u_MVP"), 1, GL_FALSE, mvp); - glUniform1f(glGetUniformLocation(dd_program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); static GLuint vao, vbo; if(!vao) glGenVertexArrays(1, &vao); glBindVertexArray(vao); @@ -376956,7 +376946,7 @@ atlas_t atlas_create(const char *inifile, unsigned flags) { else if( strend(k, "bitmap") ) { const char *text = v; array(char) bin = base64_decode(text, strlen(text)); - a.tex = texture_from_mem(bin, array_count(bin), flags&ATLAS_SRGB ? 0 : TEXTURE_NO_SRGB); + a.tex = texture_from_mem(bin, array_count(bin), 0); array_free(bin); } #if 0 @@ -377080,7 +377070,7 @@ void sprite_edit(sprite_t *s) { sprite_t* sprite_new(const char *ase, int bindings[6]) { sprite_t *s = obj_new(sprite_t, {bindings[0],bindings[1],bindings[2],bindings[3]}, {bindings[4],bindings[5]}); - atlas_t own = atlas_create(ase, ATLAS_SRGB); + atlas_t own = atlas_create(ase, 0); memcpy(s->a = MALLOC(sizeof(atlas_t)), &own, sizeof(atlas_t)); // s->a = &s->own; return s; } @@ -379341,7 +379331,6 @@ static char title[128] = {0}; static char screenshot_file[DIR_MAX]; static int locked_aspect_ratio = 0; static vec4 winbgcolor = {0,0,0,1}; -static float gamma = 2.2f; vec4 window_getcolor_() { return winbgcolor; } // internal @@ -379498,13 +379487,7 @@ void glNewFrame() { glViewport(0, 0, window_width(), window_height()); // GLfloat bgColor[4]; glGetFloatv(GL_COLOR_CLEAR_VALUE, bgColor); - vec4 bgcolor = winbgcolor; - float gammabg = gamma + !gamma; - bgcolor.x = powf(bgcolor.x, gammabg); - bgcolor.y = powf(bgcolor.y, gammabg); - bgcolor.z = powf(bgcolor.z, gammabg); - bgcolor.w = powf(bgcolor.w, gammabg); - glClearColor(bgcolor.r, bgcolor.g, bgcolor.b, window_has_transparent() ? 0 : bgcolor.a); // @transparent + glClearColor(winbgcolor.r, winbgcolor.g, winbgcolor.b, window_has_transparent() ? 0 : winbgcolor.a); // @transparent //glClearColor(0.15,0.15,0.15,1); //glClearColor( clearColor.r, clearColor.g, clearColor.b, clearColor.a ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); @@ -379818,10 +379801,6 @@ int window_frame_begin() { void input_update(); input_update(); - if (gamma) { - postfx_begin(&gamma_fx, window_width(), window_height()); - } - return 1; } @@ -379838,10 +379817,6 @@ void window_frame_end() { glClear(GL_DEPTH_BUFFER_BIT); dd_ontop = 1; ddraw_flush(); - - if (gamma) { - postfx_end(&gamma_fx); - } ui_render(); } @@ -380030,14 +380005,6 @@ void window_color(unsigned color) { unsigned a = (color >> 24) & 255; winbgcolor = vec4(r / 255.0, g / 255.0, b / 255.0, a / 255.0); } -void window_gamma(float _gamma) { - gamma = _gamma; - shader_bind(gamma_fx.pass[postfx_find(&gamma_fx, "fxGamma.fs")].program); - shader_float("u_gamma", 1.0f / gamma); -} -float window_get_gamma() { - return gamma; -} static int has_icon; int window_has_icon() { return has_icon; @@ -382647,11 +382614,6 @@ static void v4k_post_init(float refresh_rate) { hz = refresh_rate; // t = glfwGetTime(); - do_once { - postfx_load_from_mem(&gamma_fx, "fxGamma.fs", vfs_read("fxGamma.fs")); - postfx_enable(&gamma_fx, 0, 1); - window_gamma(2.2f); - } } // ---------------------------------------------------------------------------- diff --git a/engine/split/v4k_font.c b/engine/split/v4k_font.c index 1b91f7b..ab16ccc 100644 --- a/engine/split/v4k_font.c +++ b/engine/split/v4k_font.c @@ -1957,7 +1957,6 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor); glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x); glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor); - glUniform1f(glGetUniformLocation(f->program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); GLint dims[4] = {0}; glGetIntegerv(GL_VIEWPORT, dims); diff --git a/engine/split/v4k_gui.c b/engine/split/v4k_gui.c index 094abb0..4686933 100644 --- a/engine/split/v4k_gui.c +++ b/engine/split/v4k_gui.c @@ -7,7 +7,7 @@ API void gui_drawrect( texture_t spritesheet, vec2 tex_start, vec2 tex_end, int void gui_drawrect( texture_t texture, vec2 tex_start, vec2 tex_end, int rgba, vec2 start, vec2 end ) { static int program = -1, vbo = -1, vao = -1, u_tint = -1, u_has_tex = -1, u_window_width = -1, u_window_height = -1; - float gamma = window_get_gamma(); + float gamma = 1; vec2 dpi = ifdef(osx, window_dpi(), vec2(1,1)); if( program < 0 ) { const char* vs = vfs_read("shaders/rect_2d.vs"); @@ -461,9 +461,9 @@ void skinned_getscissorrect(void* userdata, const char *skin, const char *fallba dims->w -= (skinsize.y - coresize.y); } -guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb) { +guiskin_t gui_skinned(const char *asefile, float scale) { skinned_t *a = REALLOC(0, sizeof(skinned_t)); - a->atlas = atlas_create(asefile, load_as_srgb?ATLAS_SRGB:0); + a->atlas = atlas_create(asefile, 0); a->scale = scale?scale:1.0f; guiskin_t skin={0}; skin.userdata = a; diff --git a/engine/split/v4k_gui.h b/engine/split/v4k_gui.h index fabc288..870a2fb 100644 --- a/engine/split/v4k_gui.h +++ b/engine/split/v4k_gui.h @@ -62,4 +62,4 @@ typedef struct skinned_t { // - "_hover" (ex. "slider_cursor_hover") // - "_press" // -API guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb); +API guiskin_t gui_skinned(const char *asefile, float scale); diff --git a/engine/split/v4k_main.c b/engine/split/v4k_main.c index 8d6f455..2e36d84 100644 --- a/engine/split/v4k_main.c +++ b/engine/split/v4k_main.c @@ -48,11 +48,6 @@ static void v4k_post_init(float refresh_rate) { hz = refresh_rate; // t = glfwGetTime(); - do_once { - postfx_load_from_mem(&gamma_fx, "fxGamma.fs", vfs_read("fxGamma.fs")); - postfx_enable(&gamma_fx, 0, 1); - window_gamma(2.2f); - } } // ---------------------------------------------------------------------------- diff --git a/engine/split/v4k_render.c b/engine/split/v4k_render.c index 6f11b1f..3c70ed7 100644 --- a/engine/split/v4k_render.c +++ b/engine/split/v4k_render.c @@ -552,7 +552,12 @@ void shader_vec3(const char *uniform, vec3 v) { glUniform3fv(shader_uniform(un void shader_vec3v(const char *uniform, int count, vec3 *v) { glUniform3fv(shader_uniform(uniform), count, &v[0].x); } void shader_vec4(const char *uniform, vec4 v) { glUniform4fv(shader_uniform(uniform), 1, &v.x); } void shader_mat44(const char *uniform, mat44 m) { glUniformMatrix4fv(shader_uniform(uniform), 1, GL_FALSE/*GL_TRUE*/, m); } -void shader_cubemap(const char *sampler, unsigned texture) { glUniform1i(shader_uniform(sampler), 0); glBindTexture(GL_TEXTURE_CUBE_MAP, texture); } +void shader_cubemap(const char *sampler, unsigned texture) { + int id = texture_unit(); + glUniform1i(shader_uniform(sampler), id); + glActiveTexture(GL_TEXTURE0 + id); + glBindTexture(GL_TEXTURE_CUBE_MAP, texture); +} void shader_bool(const char *uniform, bool x) { glUniform1i(shader_uniform(uniform), x); } void shader_uint(const char *uniform, unsigned x ) { glUniform1ui(shader_uniform(uniform), x); } void shader_texture(const char *sampler, texture_t t) { shader_texture_unit(sampler, t.id, texture_unit()); } @@ -720,8 +725,8 @@ unsigned texture_update(texture_t *t, unsigned w, unsigned h, unsigned n, const if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGB ) pixel_type = GL_BGR; if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGBA ) pixel_type = GL_BGRA; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? if( flags & TEXTURE_BC1 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; if( flags & TEXTURE_BC2 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; @@ -1322,7 +1327,7 @@ void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float bottom, f // usage: bind empty vao & commit call for 6 (quad) or 3 vertices (tri). // ie, glBindVertexArray(empty_vao); glDrawArrays(GL_TRIANGLES, 0, 3); -void fullscreen_quad_rgb( texture_t texture, float gamma ) { +void fullscreen_quad_rgb( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -1336,7 +1341,8 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -1353,7 +1359,7 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { +void fullscreen_quad_rgb_flipped( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -1367,7 +1373,8 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -1384,7 +1391,7 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -1402,7 +1409,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -1428,7 +1435,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -1446,7 +1453,7 @@ void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -1551,12 +1558,11 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { glGenTextures(1, &c.id); glBindTexture(GL_TEXTURE_CUBE_MAP, c.id); - float gammabg = window_get_gamma() + !window_get_gamma(); int samples = 0; for (int i = 0; i < 6; i++) { image_t img = images[i]; //image(textures[i], IMAGE_RGB); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); // calculate SH coefficients (@ands) const vec3 skyDir[] = {{ 1, 0, 0},{-1, 0, 0},{ 0, 1, 0},{ 0,-1, 0},{ 0, 0, 1},{ 0, 0,-1}}; @@ -1573,9 +1579,6 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { skyDir[i]); // texelDirection; float l = len3(n); vec3 light = scale3(vec3(p[0], p[1], p[2]), 1 / (255.0f * l * l * l)); // texelSolidAngle * texel_radiance; - light.x = powf(light.x, gammabg); - light.y = powf(light.y, gammabg); - light.z = powf(light.z, gammabg); n = norm3(n); c.sh[0] = add3(c.sh[0], scale3(light, 0.282095f)); c.sh[1] = add3(c.sh[1], scale3(light, -0.488603f * n.y * 2.0 / 3.0)); @@ -1867,8 +1870,6 @@ int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view) { shader_mat44("u_mvp", mvp); if( sky->flags ) { shader_cubemap("u_cubemap", sky->cubemap.id); - } else { - shader_float("u_gamma", window_get_gamma() + !window_get_gamma()); } return 0; // @fixme: return sortable hash here? } @@ -2126,6 +2127,9 @@ void viewport_clip(vec2 from, vec2 to) { // fbos unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { + int last_fb; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &last_fb); + GLuint fbo; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); @@ -2174,14 +2178,21 @@ unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { } #endif - glBindFramebuffer (GL_FRAMEBUFFER, 0); + glBindFramebuffer (GL_FRAMEBUFFER, last_fb); return fbo; } +static __thread array(handle) fbos; void fbo_bind(unsigned id) { glBindFramebuffer(GL_FRAMEBUFFER, id); + array_push(fbos, id); } void fbo_unbind() { - fbo_bind(0); + handle id = 0; + if (array_count(fbos)) { + array_pop(fbos); + id = *array_back(fbos); + } + glBindFramebuffer(GL_FRAMEBUFFER, id); } void fbo_destroy(unsigned id) { // glDeleteRenderbuffers(1, &renderbuffer); @@ -2325,7 +2336,7 @@ int postfx_load_from_mem( postfx *fx, const char *name, const char *fs ) { if( p->uniforms[u_channelres1x] == -1 ) p->uniforms[u_channelres1x] = glGetUniformLocation(p->program, "iChannelRes1x"); if( p->uniforms[u_channelres1y] == -1 ) p->uniforms[u_channelres1y] = glGetUniformLocation(p->program, "iChannelRes1y"); - + // set quad glGenVertexArrays(1, &p->m.vao); return slot; @@ -2360,20 +2371,13 @@ int ui_postfx(postfx *fx, int pass) { return rc; } -static __thread array(handle) last_fb; -static postfx gamma_fx; - bool postfx_begin(postfx *fx, int width, int height) { // reset clear color: needed in case transparent window is being used (alpha != 0) - if (fx != &gamma_fx) glClearColor(0,0,0,0); // @transparent + glClearColor(0,0,0,0); // @transparent width += !width; height += !height; - int fb; - glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fb); - array_push(last_fb, fb); - // resize if needed if( fx->diffuse[0].w != width || fx->diffuse[0].h != height ) { texture_destroy(&fx->diffuse[0]); @@ -2397,20 +2401,18 @@ bool postfx_begin(postfx *fx, int width, int height) { uint64_t num_active_passes = popcnt64(fx->mask); bool active = fx->enabled && num_active_passes; if( !active ) { - array_pop(last_fb); - fbo_bind(fb); return false; } fbo_bind(fx->fb[1]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + fbo_unbind(); fbo_bind(fx->fb[0]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + // we keep fbo_0 bound so that user can render into it. return true; } @@ -2422,9 +2424,8 @@ bool postfx_end(postfx *fx) { return false; } - handle fb = *array_back(last_fb); - array_pop(last_fb); - fbo_bind(fb); + // unbind postfx fbo + fbo_unbind(); // disable depth test in 2d rendering bool is_depth_test_enabled = glIsEnabled(GL_DEPTH_TEST); @@ -2451,7 +2452,7 @@ bool postfx_end(postfx *fx) { glUniform1f(pass->uniforms[u_channelres0x], fx->diffuse[frame].w); glUniform1f(pass->uniforms[u_channelres0y], fx->diffuse[frame].h); - + // bind depth to texture unit 1 // shader_texture_unit(fx->depth[frame], 1); glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, fx->depth[frame].id); @@ -2469,7 +2470,7 @@ bool postfx_end(postfx *fx) { // bind the vao int bound = --num_active_passes; - if( bound ) fbo_bind(fx->fb[frame ^= 1]); + if (bound) fbo_bind(fx->fb[frame ^= 1]); // fullscreen quad glBindVertexArray(pass->m.vao); @@ -2478,10 +2479,10 @@ bool postfx_end(postfx *fx) { profile_incstat("Render.num_triangles", +2); glBindVertexArray(0); - if( bound ) fbo_bind(fb); - else glUseProgram(0); + if (bound) fbo_unbind(); } } + glUseProgram(0); if(is_depth_test_enabled); glEnable(GL_DEPTH_TEST); @@ -2588,9 +2589,9 @@ bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) { FREE(cm->texture), cm->texture = NULL; } - int srgb = !load_as_srgb ? TEXTURE_NO_SRGB : 0; - int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0; - texture_t t = texture_compressed(texture_name, TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); + int srgb = load_as_srgb ? TEXTURE_SRGB : 0; + int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT : 0; + texture_t t = texture_compressed(texture_name, TEXTURE_RGBA | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); if( t.id == texture_checker().id ) { cm->texture = NULL; @@ -2629,7 +2630,6 @@ enum shadertoy_uniforms { iSampleRate, iChannelResolution, iChannelTime, - iGamma, // iCameraScreen // iCameraPosition // iCameraActive @@ -2671,7 +2671,6 @@ shadertoy_t shadertoy( const char *shaderfile, unsigned flags ) { s.uniforms[iSampleRate] = glGetUniformLocation(s.program, "iSampleRate"); s.uniforms[iChannelResolution] = glGetUniformLocation(s.program, "iChannelResolution"); s.uniforms[iChannelTime] = glGetUniformLocation(s.program, "iChannelTime"); - s.uniforms[iGamma] = glGetUniformLocation(s.program, "iGamma"); return s; } @@ -2699,7 +2698,6 @@ shadertoy_t* shadertoy_render(shadertoy_t *s, float delta) { glUniform1i(s->uniforms[iFrame], (int)window_frame()); glUniform1f(s->uniforms[iTime], time_ss()); - glUniform1f(s->uniforms[iGamma], window_get_gamma() + !window_get_gamma()); glUniform4f(s->uniforms[iDate], tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_sec + tm->tm_min * 60 + tm->tm_hour * 3600); int unit = 0; diff --git a/engine/split/v4k_render.h b/engine/split/v4k_render.h index 61536f9..8de3286 100644 --- a/engine/split/v4k_render.h +++ b/engine/split/v4k_render.h @@ -108,7 +108,7 @@ enum TEXTURE_FLAGS { TEXTURE_FLIP = IMAGE_FLIP, // @fixme - TEXTURE_NO_SRGB = 1 << 24, + TEXTURE_SRGB = 1 << 24, TEXTURE_BGR = 1 << 25, TEXTURE_BGRA = TEXTURE_BGR, TEXTURE_ARRAY = 1 << 26, @@ -162,10 +162,10 @@ API bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) // ----------------------------------------------------------------------------- // fullscreen quads -API void fullscreen_quad_rgb( texture_t texture_rgb, float gamma ); -API void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ); -API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3], float gamma ); -API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3], float gamma ); +API void fullscreen_quad_rgb( texture_t texture_rgb ); +API void fullscreen_quad_rgb_flipped( texture_t texture ); +API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3] ); +API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3] ); // ----------------------------------------------------------------------------- // cubemaps diff --git a/engine/split/v4k_renderdd.c b/engine/split/v4k_renderdd.c index 5d6b2f0..179c734 100644 --- a/engine/split/v4k_renderdd.c +++ b/engine/split/v4k_renderdd.c @@ -13,10 +13,8 @@ static const char *dd_fs = "//" FILELINE "\n" // "precision mediump float;\n" "in vec3 out_color;\n" "out vec4 fragcolor;\n" - "uniform float u_gamma; /// set:2.2\n" "void main() {\n" " fragcolor = vec4(out_color, 1.0);\n" - " fragcolor.rgb = pow(fragcolor.rgb, vec3(u_gamma));\n" "}"; #define X(x) RGBX(x,255) @@ -76,7 +74,6 @@ void ddraw_flush_projview(mat44 proj, mat44 view) { glUseProgram(dd_program); glUniformMatrix4fv(glGetUniformLocation(dd_program, "u_MVP"), 1, GL_FALSE, mvp); - glUniform1f(glGetUniformLocation(dd_program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); static GLuint vao, vbo; if(!vao) glGenVertexArrays(1, &vao); glBindVertexArray(vao); diff --git a/engine/split/v4k_sprite.c b/engine/split/v4k_sprite.c index 709e89d..2aa3ef4 100644 --- a/engine/split/v4k_sprite.c +++ b/engine/split/v4k_sprite.c @@ -1363,7 +1363,7 @@ atlas_t atlas_create(const char *inifile, unsigned flags) { else if( strend(k, "bitmap") ) { const char *text = v; array(char) bin = base64_decode(text, strlen(text)); - a.tex = texture_from_mem(bin, array_count(bin), flags&ATLAS_SRGB ? 0 : TEXTURE_NO_SRGB); + a.tex = texture_from_mem(bin, array_count(bin), 0); array_free(bin); } #if 0 @@ -1487,7 +1487,7 @@ void sprite_edit(sprite_t *s) { sprite_t* sprite_new(const char *ase, int bindings[6]) { sprite_t *s = obj_new(sprite_t, {bindings[0],bindings[1],bindings[2],bindings[3]}, {bindings[4],bindings[5]}); - atlas_t own = atlas_create(ase, ATLAS_SRGB); + atlas_t own = atlas_create(ase, 0); memcpy(s->a = MALLOC(sizeof(atlas_t)), &own, sizeof(atlas_t)); // s->a = &s->own; return s; } diff --git a/engine/split/v4k_sprite.h b/engine/split/v4k_sprite.h index c1ba308..7d4e675 100644 --- a/engine/split/v4k_sprite.h +++ b/engine/split/v4k_sprite.h @@ -84,10 +84,6 @@ API void ui_spine(spine_t *p); // ---------------------------------------------------------------------------- // atlas api -enum ATLAS_FLAGS { - ATLAS_SRGB = 2, -}; - typedef struct atlas_frame_t { unsigned delay; vec4 sheet; diff --git a/engine/split/v4k_window.c b/engine/split/v4k_window.c index 0ab93ec..4b59b6a 100644 --- a/engine/split/v4k_window.c +++ b/engine/split/v4k_window.c @@ -107,7 +107,6 @@ static char title[128] = {0}; static char screenshot_file[DIR_MAX]; static int locked_aspect_ratio = 0; static vec4 winbgcolor = {0,0,0,1}; -static float gamma = 2.2f; vec4 window_getcolor_() { return winbgcolor; } // internal @@ -264,13 +263,7 @@ void glNewFrame() { glViewport(0, 0, window_width(), window_height()); // GLfloat bgColor[4]; glGetFloatv(GL_COLOR_CLEAR_VALUE, bgColor); - vec4 bgcolor = winbgcolor; - float gammabg = gamma + !gamma; - bgcolor.x = powf(bgcolor.x, gammabg); - bgcolor.y = powf(bgcolor.y, gammabg); - bgcolor.z = powf(bgcolor.z, gammabg); - bgcolor.w = powf(bgcolor.w, gammabg); - glClearColor(bgcolor.r, bgcolor.g, bgcolor.b, window_has_transparent() ? 0 : bgcolor.a); // @transparent + glClearColor(winbgcolor.r, winbgcolor.g, winbgcolor.b, window_has_transparent() ? 0 : winbgcolor.a); // @transparent //glClearColor(0.15,0.15,0.15,1); //glClearColor( clearColor.r, clearColor.g, clearColor.b, clearColor.a ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); @@ -584,10 +577,6 @@ int window_frame_begin() { void input_update(); input_update(); - if (gamma) { - postfx_begin(&gamma_fx, window_width(), window_height()); - } - return 1; } @@ -604,10 +593,6 @@ void window_frame_end() { glClear(GL_DEPTH_BUFFER_BIT); dd_ontop = 1; ddraw_flush(); - - if (gamma) { - postfx_end(&gamma_fx); - } ui_render(); } @@ -796,14 +781,6 @@ void window_color(unsigned color) { unsigned a = (color >> 24) & 255; winbgcolor = vec4(r / 255.0, g / 255.0, b / 255.0, a / 255.0); } -void window_gamma(float _gamma) { - gamma = _gamma; - shader_bind(gamma_fx.pass[postfx_find(&gamma_fx, "fxGamma.fs")].program); - shader_float("u_gamma", 1.0f / gamma); -} -float window_get_gamma() { - return gamma; -} static int has_icon; int window_has_icon() { return has_icon; diff --git a/engine/v4k.c b/engine/v4k.c index 9911005..cf9baa3 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -10659,7 +10659,6 @@ void font_draw_cmd(font_t *f, const float *glyph_data, int glyph_idx, float fact glUniform1f(glGetUniformLocation(f->program, "scale_factor"), factor); glUniform2fv(glGetUniformLocation(f->program, "string_offset"), 1, &offset.x); glUniform1f(glGetUniformLocation(f->program, "offset_firstline"), f->ascent*f->factor); - glUniform1f(glGetUniformLocation(f->program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); GLint dims[4] = {0}; glGetIntegerv(GL_VIEWPORT, dims); @@ -11250,7 +11249,7 @@ API void gui_drawrect( texture_t spritesheet, vec2 tex_start, vec2 tex_end, int void gui_drawrect( texture_t texture, vec2 tex_start, vec2 tex_end, int rgba, vec2 start, vec2 end ) { static int program = -1, vbo = -1, vao = -1, u_tint = -1, u_has_tex = -1, u_window_width = -1, u_window_height = -1; - float gamma = window_get_gamma(); + float gamma = 1; vec2 dpi = ifdef(osx, window_dpi(), vec2(1,1)); if( program < 0 ) { const char* vs = vfs_read("shaders/rect_2d.vs"); @@ -11704,9 +11703,9 @@ void skinned_getscissorrect(void* userdata, const char *skin, const char *fallba dims->w -= (skinsize.y - coresize.y); } -guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb) { +guiskin_t gui_skinned(const char *asefile, float scale) { skinned_t *a = REALLOC(0, sizeof(skinned_t)); - a->atlas = atlas_create(asefile, load_as_srgb?ATLAS_SRGB:0); + a->atlas = atlas_create(asefile, 0); a->scale = scale?scale:1.0f; guiskin_t skin={0}; skin.userdata = a; @@ -17725,7 +17724,12 @@ void shader_vec3(const char *uniform, vec3 v) { glUniform3fv(shader_uniform(un void shader_vec3v(const char *uniform, int count, vec3 *v) { glUniform3fv(shader_uniform(uniform), count, &v[0].x); } void shader_vec4(const char *uniform, vec4 v) { glUniform4fv(shader_uniform(uniform), 1, &v.x); } void shader_mat44(const char *uniform, mat44 m) { glUniformMatrix4fv(shader_uniform(uniform), 1, GL_FALSE/*GL_TRUE*/, m); } -void shader_cubemap(const char *sampler, unsigned texture) { glUniform1i(shader_uniform(sampler), 0); glBindTexture(GL_TEXTURE_CUBE_MAP, texture); } +void shader_cubemap(const char *sampler, unsigned texture) { + int id = texture_unit(); + glUniform1i(shader_uniform(sampler), id); + glActiveTexture(GL_TEXTURE0 + id); + glBindTexture(GL_TEXTURE_CUBE_MAP, texture); +} void shader_bool(const char *uniform, bool x) { glUniform1i(shader_uniform(uniform), x); } void shader_uint(const char *uniform, unsigned x ) { glUniform1ui(shader_uniform(uniform), x); } void shader_texture(const char *sampler, texture_t t) { shader_texture_unit(sampler, t.id, texture_unit()); } @@ -17893,8 +17897,8 @@ unsigned texture_update(texture_t *t, unsigned w, unsigned h, unsigned n, const if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGB ) pixel_type = GL_BGR; if( flags & TEXTURE_BGR ) if( pixel_type == GL_RGBA ) pixel_type = GL_BGRA; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; - if( !(flags & TEXTURE_NO_SRGB) ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGB ) texel_type = GL_SRGB; + if( flags & TEXTURE_SRGB ) if( texel_type == GL_RGBA ) texel_type = GL_SRGB_ALPHA; // GL_SRGB8_ALPHA8 ? if( flags & TEXTURE_BC1 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; if( flags & TEXTURE_BC2 ) texel_type = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; @@ -18495,7 +18499,7 @@ void shadowmatrix_ortho(mat44 shm_proj, float left, float right, float bottom, f // usage: bind empty vao & commit call for 6 (quad) or 3 vertices (tri). // ie, glBindVertexArray(empty_vao); glDrawArrays(GL_TRIANGLES, 0, 3); -void fullscreen_quad_rgb( texture_t texture, float gamma ) { +void fullscreen_quad_rgb( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -18509,7 +18513,8 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -18526,7 +18531,7 @@ void fullscreen_quad_rgb( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { +void fullscreen_quad_rgb_flipped( texture_t texture ) { static int program = -1, vao = -1, u_inv_gamma = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -18540,7 +18545,8 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { GLenum texture_type = texture.flags & TEXTURE_ARRAY ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_inv_gamma, 1.0f / (gamma + !gamma) ); + float gamma = 1; + glUniform1f( u_inv_gamma, gamma ); glBindVertexArray( vao ); @@ -18557,7 +18563,7 @@ void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B_flipped.glsl"); @@ -18575,7 +18581,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -18601,7 +18607,7 @@ void fullscreen_quad_ycbcr( texture_t textureYCbCr[3], float gamma ) { // glDisable( GL_BLEND ); } -void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { +void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3] ) { static int program = -1, vao = -1, u_gamma = -1, uy = -1, ucb = -1, ucr = -1; if( program < 0 ) { const char* vs = vfs_read("shaders/vs_0_2_fullscreen_quad_B.glsl"); @@ -18619,7 +18625,7 @@ void fullscreen_quad_ycbcr_flipped( texture_t textureYCbCr[3], float gamma ) { // glEnable( GL_BLEND ); glUseProgram( program ); - glUniform1f( u_gamma, gamma ); + // glUniform1f( u_gamma, gamma ); glBindVertexArray( vao ); @@ -18724,12 +18730,11 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { glGenTextures(1, &c.id); glBindTexture(GL_TEXTURE_CUBE_MAP, c.id); - float gammabg = window_get_gamma() + !window_get_gamma(); int samples = 0; for (int i = 0; i < 6; i++) { image_t img = images[i]; //image(textures[i], IMAGE_RGB); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_SRGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, img.w, img.h, 0, img.n == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, img.pixels); // calculate SH coefficients (@ands) const vec3 skyDir[] = {{ 1, 0, 0},{-1, 0, 0},{ 0, 1, 0},{ 0,-1, 0},{ 0, 0, 1},{ 0, 0,-1}}; @@ -18746,9 +18751,6 @@ cubemap_t cubemap6( const image_t images[6], int flags ) { skyDir[i]); // texelDirection; float l = len3(n); vec3 light = scale3(vec3(p[0], p[1], p[2]), 1 / (255.0f * l * l * l)); // texelSolidAngle * texel_radiance; - light.x = powf(light.x, gammabg); - light.y = powf(light.y, gammabg); - light.z = powf(light.z, gammabg); n = norm3(n); c.sh[0] = add3(c.sh[0], scale3(light, 0.282095f)); c.sh[1] = add3(c.sh[1], scale3(light, -0.488603f * n.y * 2.0 / 3.0)); @@ -19040,8 +19042,6 @@ int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view) { shader_mat44("u_mvp", mvp); if( sky->flags ) { shader_cubemap("u_cubemap", sky->cubemap.id); - } else { - shader_float("u_gamma", window_get_gamma() + !window_get_gamma()); } return 0; // @fixme: return sortable hash here? } @@ -19299,6 +19299,9 @@ void viewport_clip(vec2 from, vec2 to) { // fbos unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { + int last_fb; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &last_fb); + GLuint fbo; glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); @@ -19347,14 +19350,21 @@ unsigned fbo(unsigned color_texture_id, unsigned depth_texture_id, int flags) { } #endif - glBindFramebuffer (GL_FRAMEBUFFER, 0); + glBindFramebuffer (GL_FRAMEBUFFER, last_fb); return fbo; } +static __thread array(handle) fbos; void fbo_bind(unsigned id) { glBindFramebuffer(GL_FRAMEBUFFER, id); + array_push(fbos, id); } void fbo_unbind() { - fbo_bind(0); + handle id = 0; + if (array_count(fbos)) { + array_pop(fbos); + id = *array_back(fbos); + } + glBindFramebuffer(GL_FRAMEBUFFER, id); } void fbo_destroy(unsigned id) { // glDeleteRenderbuffers(1, &renderbuffer); @@ -19498,7 +19508,7 @@ int postfx_load_from_mem( postfx *fx, const char *name, const char *fs ) { if( p->uniforms[u_channelres1x] == -1 ) p->uniforms[u_channelres1x] = glGetUniformLocation(p->program, "iChannelRes1x"); if( p->uniforms[u_channelres1y] == -1 ) p->uniforms[u_channelres1y] = glGetUniformLocation(p->program, "iChannelRes1y"); - + // set quad glGenVertexArrays(1, &p->m.vao); return slot; @@ -19533,20 +19543,13 @@ int ui_postfx(postfx *fx, int pass) { return rc; } -static __thread array(handle) last_fb; -static postfx gamma_fx; - bool postfx_begin(postfx *fx, int width, int height) { // reset clear color: needed in case transparent window is being used (alpha != 0) - if (fx != &gamma_fx) glClearColor(0,0,0,0); // @transparent + glClearColor(0,0,0,0); // @transparent width += !width; height += !height; - int fb; - glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fb); - array_push(last_fb, fb); - // resize if needed if( fx->diffuse[0].w != width || fx->diffuse[0].h != height ) { texture_destroy(&fx->diffuse[0]); @@ -19570,20 +19573,18 @@ bool postfx_begin(postfx *fx, int width, int height) { uint64_t num_active_passes = popcnt64(fx->mask); bool active = fx->enabled && num_active_passes; if( !active ) { - array_pop(last_fb); - fbo_bind(fb); return false; } fbo_bind(fx->fb[1]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + fbo_unbind(); fbo_bind(fx->fb[0]); - viewport_clear(true, true); viewport_clip(vec2(0,0), vec2(width, height)); + // we keep fbo_0 bound so that user can render into it. return true; } @@ -19595,9 +19596,8 @@ bool postfx_end(postfx *fx) { return false; } - handle fb = *array_back(last_fb); - array_pop(last_fb); - fbo_bind(fb); + // unbind postfx fbo + fbo_unbind(); // disable depth test in 2d rendering bool is_depth_test_enabled = glIsEnabled(GL_DEPTH_TEST); @@ -19624,7 +19624,7 @@ bool postfx_end(postfx *fx) { glUniform1f(pass->uniforms[u_channelres0x], fx->diffuse[frame].w); glUniform1f(pass->uniforms[u_channelres0y], fx->diffuse[frame].h); - + // bind depth to texture unit 1 // shader_texture_unit(fx->depth[frame], 1); glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_2D, fx->depth[frame].id); @@ -19642,7 +19642,7 @@ bool postfx_end(postfx *fx) { // bind the vao int bound = --num_active_passes; - if( bound ) fbo_bind(fx->fb[frame ^= 1]); + if (bound) fbo_bind(fx->fb[frame ^= 1]); // fullscreen quad glBindVertexArray(pass->m.vao); @@ -19651,10 +19651,10 @@ bool postfx_end(postfx *fx) { profile_incstat("Render.num_triangles", +2); glBindVertexArray(0); - if( bound ) fbo_bind(fb); - else glUseProgram(0); + if (bound) fbo_unbind(); } } + glUseProgram(0); if(is_depth_test_enabled); glEnable(GL_DEPTH_TEST); @@ -19761,9 +19761,9 @@ bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) { FREE(cm->texture), cm->texture = NULL; } - int srgb = !load_as_srgb ? TEXTURE_NO_SRGB : 0; - int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0; - texture_t t = texture_compressed(texture_name, TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); + int srgb = load_as_srgb ? TEXTURE_SRGB : 0; + int hdr = strendi(texture_name, ".hdr") ? TEXTURE_FLOAT : 0; + texture_t t = texture_compressed(texture_name, TEXTURE_RGBA | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | hdr | srgb); if( t.id == texture_checker().id ) { cm->texture = NULL; @@ -19802,7 +19802,6 @@ enum shadertoy_uniforms { iSampleRate, iChannelResolution, iChannelTime, - iGamma, // iCameraScreen // iCameraPosition // iCameraActive @@ -19844,7 +19843,6 @@ shadertoy_t shadertoy( const char *shaderfile, unsigned flags ) { s.uniforms[iSampleRate] = glGetUniformLocation(s.program, "iSampleRate"); s.uniforms[iChannelResolution] = glGetUniformLocation(s.program, "iChannelResolution"); s.uniforms[iChannelTime] = glGetUniformLocation(s.program, "iChannelTime"); - s.uniforms[iGamma] = glGetUniformLocation(s.program, "iGamma"); return s; } @@ -19872,7 +19870,6 @@ shadertoy_t* shadertoy_render(shadertoy_t *s, float delta) { glUniform1i(s->uniforms[iFrame], (int)window_frame()); glUniform1f(s->uniforms[iTime], time_ss()); - glUniform1f(s->uniforms[iGamma], window_get_gamma() + !window_get_gamma()); glUniform4f(s->uniforms[iDate], tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_sec + tm->tm_min * 60 + tm->tm_hour * 3600); int unit = 0; @@ -21196,10 +21193,8 @@ static const char *dd_fs = "//" FILELINE "\n" // "precision mediump float;\n" "in vec3 out_color;\n" "out vec4 fragcolor;\n" - "uniform float u_gamma; /// set:2.2\n" "void main() {\n" " fragcolor = vec4(out_color, 1.0);\n" - " fragcolor.rgb = pow(fragcolor.rgb, vec3(u_gamma));\n" "}"; #define X(x) RGBX(x,255) @@ -21259,7 +21254,6 @@ void ddraw_flush_projview(mat44 proj, mat44 view) { glUseProgram(dd_program); glUniformMatrix4fv(glGetUniformLocation(dd_program, "u_MVP"), 1, GL_FALSE, mvp); - glUniform1f(glGetUniformLocation(dd_program, "u_gamma"), (window_get_gamma() + !window_get_gamma())); static GLuint vao, vbo; if(!vao) glGenVertexArrays(1, &vao); glBindVertexArray(vao); @@ -24106,7 +24100,7 @@ atlas_t atlas_create(const char *inifile, unsigned flags) { else if( strend(k, "bitmap") ) { const char *text = v; array(char) bin = base64_decode(text, strlen(text)); - a.tex = texture_from_mem(bin, array_count(bin), flags&ATLAS_SRGB ? 0 : TEXTURE_NO_SRGB); + a.tex = texture_from_mem(bin, array_count(bin), 0); array_free(bin); } #if 0 @@ -24230,7 +24224,7 @@ void sprite_edit(sprite_t *s) { sprite_t* sprite_new(const char *ase, int bindings[6]) { sprite_t *s = obj_new(sprite_t, {bindings[0],bindings[1],bindings[2],bindings[3]}, {bindings[4],bindings[5]}); - atlas_t own = atlas_create(ase, ATLAS_SRGB); + atlas_t own = atlas_create(ase, 0); memcpy(s->a = MALLOC(sizeof(atlas_t)), &own, sizeof(atlas_t)); // s->a = &s->own; return s; } @@ -26491,7 +26485,6 @@ static char title[128] = {0}; static char screenshot_file[DIR_MAX]; static int locked_aspect_ratio = 0; static vec4 winbgcolor = {0,0,0,1}; -static float gamma = 2.2f; vec4 window_getcolor_() { return winbgcolor; } // internal @@ -26648,13 +26641,7 @@ void glNewFrame() { glViewport(0, 0, window_width(), window_height()); // GLfloat bgColor[4]; glGetFloatv(GL_COLOR_CLEAR_VALUE, bgColor); - vec4 bgcolor = winbgcolor; - float gammabg = gamma + !gamma; - bgcolor.x = powf(bgcolor.x, gammabg); - bgcolor.y = powf(bgcolor.y, gammabg); - bgcolor.z = powf(bgcolor.z, gammabg); - bgcolor.w = powf(bgcolor.w, gammabg); - glClearColor(bgcolor.r, bgcolor.g, bgcolor.b, window_has_transparent() ? 0 : bgcolor.a); // @transparent + glClearColor(winbgcolor.r, winbgcolor.g, winbgcolor.b, window_has_transparent() ? 0 : winbgcolor.a); // @transparent //glClearColor(0.15,0.15,0.15,1); //glClearColor( clearColor.r, clearColor.g, clearColor.b, clearColor.a ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); @@ -26968,10 +26955,6 @@ int window_frame_begin() { void input_update(); input_update(); - if (gamma) { - postfx_begin(&gamma_fx, window_width(), window_height()); - } - return 1; } @@ -26988,10 +26971,6 @@ void window_frame_end() { glClear(GL_DEPTH_BUFFER_BIT); dd_ontop = 1; ddraw_flush(); - - if (gamma) { - postfx_end(&gamma_fx); - } ui_render(); } @@ -27180,14 +27159,6 @@ void window_color(unsigned color) { unsigned a = (color >> 24) & 255; winbgcolor = vec4(r / 255.0, g / 255.0, b / 255.0, a / 255.0); } -void window_gamma(float _gamma) { - gamma = _gamma; - shader_bind(gamma_fx.pass[postfx_find(&gamma_fx, "fxGamma.fs")].program); - shader_float("u_gamma", 1.0f / gamma); -} -float window_get_gamma() { - return gamma; -} static int has_icon; int window_has_icon() { return has_icon; @@ -29797,11 +29768,6 @@ static void v4k_post_init(float refresh_rate) { hz = refresh_rate; // t = glfwGetTime(); - do_once { - postfx_load_from_mem(&gamma_fx, "fxGamma.fs", vfs_read("fxGamma.fs")); - postfx_enable(&gamma_fx, 0, 1); - window_gamma(2.2f); - } } // ---------------------------------------------------------------------------- diff --git a/engine/v4k.h b/engine/v4k.h index 7f90fb5..69834f1 100644 --- a/engine/v4k.h +++ b/engine/v4k.h @@ -3197,7 +3197,7 @@ enum TEXTURE_FLAGS { TEXTURE_FLIP = IMAGE_FLIP, // @fixme - TEXTURE_NO_SRGB = 1 << 24, + TEXTURE_SRGB = 1 << 24, TEXTURE_BGR = 1 << 25, TEXTURE_BGRA = TEXTURE_BGR, TEXTURE_ARRAY = 1 << 26, @@ -3251,10 +3251,10 @@ API bool colormap( colormap_t *cm, const char *texture_name, bool load_as_srgb ) // ----------------------------------------------------------------------------- // fullscreen quads -API void fullscreen_quad_rgb( texture_t texture_rgb, float gamma ); -API void fullscreen_quad_rgb_flipped( texture_t texture, float gamma ); -API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3], float gamma ); -API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3], float gamma ); +API void fullscreen_quad_rgb( texture_t texture_rgb ); +API void fullscreen_quad_rgb_flipped( texture_t texture ); +API void fullscreen_quad_ycbcr( texture_t texture_YCbCr[3] ); +API void fullscreen_quad_ycbcr_flipped( texture_t texture_YCbCr[3] ); // ----------------------------------------------------------------------------- // cubemaps @@ -4172,10 +4172,6 @@ API void ui_spine(spine_t *p); // ---------------------------------------------------------------------------- // atlas api -enum ATLAS_FLAGS { - ATLAS_SRGB = 2, -}; - typedef struct atlas_frame_t { unsigned delay; vec4 sheet; @@ -4317,7 +4313,7 @@ typedef struct skinned_t { // - "_hover" (ex. "slider_cursor_hover") // - "_press" // -API guiskin_t gui_skinned(const char *asefile, float scale, bool load_as_srgb); +API guiskin_t gui_skinned(const char *asefile, float scale); #line 0 #line 1 "v4k_steam.h"