Skip to content

Commit

Permalink
#15 some RPI shader optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed May 20, 2018
1 parent fdf6821 commit 95e4c0c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
17 changes: 11 additions & 6 deletions src/gapi_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
PFNGLPROGRAMBINARYPROC glProgramBinary;
#endif

#if defined(_GAPI_GLES)
#if defined(_GAPI_GLES) && !defined(_OS_RPI)
PFNGLDISCARDFRAMEBUFFEREXTPROC glDiscardFramebufferEXT;
#endif

Expand Down Expand Up @@ -369,6 +369,11 @@ namespace GAPI {
}
sprintf(defines, "%s#define PASS_%s\n", defines, passNames[pass]);

#ifdef _OS_RPI
strcat(defines, "#define OPT_VLIGHTPROJ\n");
strcat(defines, "#define OPT_SHADOW_ONETAP\n");
#endif

char fileName[255];
// generate shader file path
if (Core::support.shaderBinary) {
Expand Down Expand Up @@ -648,7 +653,7 @@ namespace GAPI {
bool dynamic;

Mesh(bool dynamic) : iBuffer(NULL), vBuffer(NULL), VAO(NULL), dynamic(dynamic) {
ID[0] = ID[1] = NULL;
ID[0] = ID[1] = 0;
}

void init(Index *indices, int iCount, ::Vertex *vertices, int vCount, int aCount) {
Expand Down Expand Up @@ -796,7 +801,7 @@ namespace GAPI {
void *libGL = dlopen("libGLESv2.so", RTLD_LAZY);
#endif

#if defined(_OS_WIN) || (defined(_OS_LINUX) && !defined(_OS_RPI)) || defined(_OS_ANDROID)
#if defined(_OS_WIN) || defined(_OS_LINUX) || defined(_OS_ANDROID)
#ifdef _OS_WIN
GetProcOGL(glActiveTexture);
#endif
Expand Down Expand Up @@ -929,8 +934,8 @@ namespace GAPI {
support.texHalf = support.texHalfLinear || extSupport(ext, "_texture_half_float");

#ifdef PROFILE
support.profMarker = extSupport(ext, "_KHR_debug");
support.profTiming = extSupport(ext, "_timer_query");
support.profMarker = extSupport(ext, "_KHR_debug");
support.profTiming = extSupport(ext, "_timer_query");
#endif

if (support.maxAniso)
Expand Down Expand Up @@ -1198,4 +1203,4 @@ namespace GAPI {
}
}

#endif
#endif
3 changes: 2 additions & 1 deletion src/platform/rpi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,11 @@ int main(int argc, char **argv) {
}
};

inputFree();

sndFree();
Game::deinit();

inputFree();
eglFree(display, surface, context);
wndFree(dmDisplay, dmWindow);

Expand Down
4 changes: 2 additions & 2 deletions src/shaders/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define WATER_FOG_DIST (1.0 / (6.0 * 1024.0))
#define UNDERWATER_COLOR float3(0.6, 0.9, 0.9)
#define SHADOW_NORMAL_BIAS 16.0
#define SHADOW_CONST_BIAS 0.04
#define SHADOW_CONST_BIAS 0.05
#define PI 3.141592653589793

static const float3 SHADOW_TEXEL = float3(1.0 / 1024.0, 1.0 / 1024.0, 0.0);
Expand Down Expand Up @@ -73,4 +73,4 @@ float4 pack(float value) {

float unpack(float4 value) {
return dot(value, float4(1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0));
}
}
52 changes: 37 additions & 15 deletions src/shaders/shader.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ R"====(
#define UNDERWATER_COLOR vec3(0.6, 0.9, 0.9)

#define SHADOW_NORMAL_BIAS 16.0
#define SHADOW_CONST_BIAS 0.04
#define SHADOW_CONST_BIAS 0.05

#if (defined(PASS_AMBIENT) || defined(PASS_COMPOSE)) && !defined(TYPE_FLASH)
varying vec3 vCoord;
Expand All @@ -25,6 +25,10 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - trapezoidal correction
#ifdef OPT_SHADOW
#define SHADOW_TEXEL vec3(1.0 / 1024.0, 1.0 / 1024.0, 0.0)
uniform mat4 uLightProj;

#ifdef OPT_VLIGHTPROJ
varying vec4 vLightProj;
#endif
#endif

uniform mat4 uViewProj;
Expand All @@ -37,7 +41,7 @@ uniform vec4 uMaterial; // x - diffuse, y - ambient, z - specular, w - alpha
uniform vec4 uFogParams;

#ifndef PASS_SHADOW
varying vec4 vViewVec; // xyz - dir * dist, w - coord.y * clipPlaneSign
varying vec4 vViewVec; // xyz - dir * dist, w - coord.y * clipPlaneSign
varying vec4 vDiffuse;
varying vec4 vNormal; // xyz - normal dir, w - fog factor

Expand All @@ -53,6 +57,14 @@ uniform vec4 uFogParams;
#endif
#endif

#ifdef OPT_SHADOW
vec4 calcLightProj(vec3 coord, vec3 lightVec, vec3 normal) {
float factor = clamp(1.0 - dot(normalize(lightVec), normal), 0.0, 1.0);
factor *= SHADOW_NORMAL_BIAS;
return uLightProj * vec4(coord + normal * factor, 1.0);
}
#endif

#ifdef VERTEX

#if defined(TYPE_ENTITY) || defined(TYPE_MIRROR)
Expand Down Expand Up @@ -208,6 +220,11 @@ uniform vec4 uFogParams;
vAmbient = ambient;
vLight = light;
vLightMap = aLight * light.x;

#ifdef OPT_VLIGHTPROJ
vLightProj = calcLightProj(coord, lv0, vNormal.xyz);
#endif

#else
vLight.xyz = uLightColor[1].xyz * light.y + uLightColor[2].xyz * light.z + uLightColor[3].xyz * light.w;
vLight.w = 0.0;
Expand Down Expand Up @@ -296,26 +313,31 @@ uniform vec4 uFogParams;
#ifdef SHADOW_SAMPLER
float rShadow = SHADOW(p);
#else
vec4 samples = vec4(SHADOW( p.xy),
SHADOW(SHADOW_TEXEL.xz + p.xy),
SHADOW(SHADOW_TEXEL.zy + p.xy),
SHADOW(SHADOW_TEXEL.xy + p.xy));
#ifndef OPT_SHADOW_ONETAP
vec4 samples = vec4(SHADOW( p.xy),
SHADOW(SHADOW_TEXEL.xz + p.xy),
SHADOW(SHADOW_TEXEL.zy + p.xy),
SHADOW(SHADOW_TEXEL.xy + p.xy));

samples = step(vec4(p.z), samples);
samples = step(vec4(p.z), samples);

vec2 f = fract(p.xy / SHADOW_TEXEL.xy);
samples.xy = mix(samples.xz, samples.yw, f.x);
float rShadow = mix(samples.x, samples.y, f.y);
vec2 f = fract(p.xy / SHADOW_TEXEL.xy);
samples.xy = mix(samples.xz, samples.yw, f.x);
float rShadow = mix(samples.x, samples.y, f.y);
#else
float rShadow = step(p.z, SHADOW(p.xy));
#endif
#endif

float fade = clamp(dot(lightVec, lightVec), 0.0, 1.0);
return rShadow + (1.0 - rShadow) * fade;
}

float getShadow(vec3 lightVec, vec3 normal) {
float factor = clamp(1.0 - dot(normalize(lightVec), normal), 0.0, 1.0);
factor *= SHADOW_NORMAL_BIAS;
return getShadow(lightVec, normal, uLightProj * vec4(vCoord + normal * factor, 1.0));
#ifndef OPT_VLIGHTPROJ
vec4 vLightProj = calcLightProj(vCoord, lightVec, normal);
#endif
return getShadow(lightVec, normal, vLightProj);
}
#endif

Expand Down Expand Up @@ -414,7 +436,7 @@ uniform vec4 uFogParams;
#endif

#ifdef TYPE_ROOM
light += mix(vAmbient.xyz, vLightMap.xyz, rShadow);
light += mix(vAmbient, vLightMap.xyz, rShadow);
#endif

#ifdef TYPE_SPRITE
Expand Down Expand Up @@ -451,4 +473,4 @@ uniform vec4 uFogParams;
#endif
}
#endif
)===="
)===="

0 comments on commit 95e4c0c

Please sign in to comment.