From 828990b9267c12c721d9740dfd027f07cb3bbbc4 Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Tue, 5 Jun 2018 12:18:09 +0200 Subject: [PATCH] Texture based fog table --- core/rend/gles/gles.cpp | 113 ++++++++++++---------------------------- core/rend/gles/gles.h | 2 +- 2 files changed, 34 insertions(+), 81 deletions(-) diff --git a/core/rend/gles/gles.cpp b/core/rend/gles/gles.cpp index 7e1f3ea82..42b9af70f 100755 --- a/core/rend/gles/gles.cpp +++ b/core/rend/gles/gles.cpp @@ -191,7 +191,6 @@ const char* PixelPipelineShader = uniform lowp float cp_AlphaTestValue; \n\ uniform lowp vec4 pp_ClipTest; \n\ uniform lowp vec3 sp_FOG_COL_RAM,sp_FOG_COL_VERT; \n\ -uniform highp vec2 sp_LOG_FOG_COEFS; \n\ uniform highp float sp_FOG_DENSITY; \n\ uniform sampler2D tex,fog_table; \n\ /* Vertex input*/ \n\ @@ -200,9 +199,13 @@ uniform sampler2D tex,fog_table; \n\ " vary " mediump vec2 vtx_uv; \n\ lowp float fog_mode2(highp float w) \n\ { \n\ - highp float fog_idx = clamp(w * sp_FOG_DENSITY, 0.0, 127.99); \n\ - return clamp(sp_LOG_FOG_COEFS.y * log2(fog_idx) + sp_LOG_FOG_COEFS.x, 0.001, 1.0); //the clamp is required due to yet another bug !\n\ -} \n\ + highp float z = clamp(w * sp_FOG_DENSITY, 1.0, 255.9999); \n\ + uint i = uint(floor(log2(z))); \n\ + highp float m = z * 16 / pow(2, i) - 16; \n\ + float idx = floor(m) + i * 16u + 0.5; \n\ + vec4 fog_coef = " TEXLOOKUP "(fog_table, vec2(idx / 128, 0.75 - (m - floor(m)) / 2)); \n\ + return fog_coef.a; \n\ + } \n\ void main() \n\ { \n\ // Clip outside the box \n\ @@ -324,6 +327,7 @@ gl_ctx gl; int screen_width; int screen_height; +GLuint fogTextureId; #if (HOST_OS != OS_DARWIN) && !defined(TARGET_NACL32) #if defined(GLES) && !defined(USE_SDL) @@ -637,7 +641,6 @@ struct ShaderUniforms_t float fog_den_float; float ps_FOG_COL_RAM[3]; float ps_FOG_COL_VERT[3]; - float fog_coefs[2]; void Set(PipelineShader* s) { @@ -658,9 +661,6 @@ struct ShaderUniforms_t if (s->sp_FOG_COL_VERT!=-1) glUniform3fv( s->sp_FOG_COL_VERT, 1, ps_FOG_COL_VERT); - - if (s->sp_LOG_FOG_COEFS!=-1) - glUniform2fv(s->sp_LOG_FOG_COEFS,1, fog_coefs); } } ShaderUniforms; @@ -800,14 +800,15 @@ bool CompilePipelineShader( PipelineShader* s) if (s->pp_FogCtrl==0 || s->pp_FogCtrl==3) { s->sp_FOG_COL_RAM=glGetUniformLocation(s->program, "sp_FOG_COL_RAM"); - s->sp_LOG_FOG_COEFS=glGetUniformLocation(s->program, "sp_LOG_FOG_COEFS"); } else { s->sp_FOG_COL_RAM=-1; - s->sp_LOG_FOG_COEFS=-1; } - + // Setup texture 1 as the fog table + gu = glGetUniformLocation(s->program, "fog_table"); + if (gu != -1) + glUniform1i(gu, 1); ShaderUniforms.Set(s); @@ -956,72 +957,34 @@ bool gles_init() } -void tryfit(float* x,float* y) +void UpdateFogTexture(u8 *fog_table) { - //y=B*ln(x)+A - - double sylnx=0,sy=0,slnx=0,slnx2=0; - - u32 cnt=0; - - for (int i=0;i<128;i++) + glActiveTexture(GL_TEXTURE1); + if (fogTextureId == 0) { - int rep=1; - - //discard values clipped to 0 or 1 - if (i<127 && y[i]==1 && y[i+1]==1) - continue; - - if (i>0 && y[i]==0 && y[i-1]==0) - continue; - - //Add many samples for first and last value (fog-in, fog-out -> important) - if (i>0 && y[i]!=1 && y[i-1]==1) - rep=10000; - - if (i<127 && y[i]!=0 && y[i+1]==0) - rep=10000; - - for (int j=0;j>4)*(1+(i&15)/16.f); - yvals[i]=fog_table[i*4+1]/255.0f; - } - - tryfit(xvals,yvals); + fog_needs_update = false; + UpdateFogTexture((u8 *)FOG_TABLE); } glcache.UseProgram(gl.modvol_shader.program); diff --git a/core/rend/gles/gles.h b/core/rend/gles/gles.h index 36688a5bb..0cbe8761e 100755 --- a/core/rend/gles/gles.h +++ b/core/rend/gles/gles.h @@ -54,7 +54,7 @@ struct PipelineShader GLuint scale,depth_scale; GLuint pp_ClipTest,cp_AlphaTestValue; - GLuint sp_FOG_COL_RAM,sp_FOG_COL_VERT,sp_FOG_DENSITY,sp_LOG_FOG_COEFS; + GLuint sp_FOG_COL_RAM,sp_FOG_COL_VERT,sp_FOG_DENSITY; // u32 cp_AlphaTest; s32 pp_ClipTestMode;