Skip to content

Commit

Permalink
optimise shaders slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Jul 27, 2023
1 parent 9a2a784 commit 96b28a3
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 76 deletions.
8 changes: 4 additions & 4 deletions crogine/src/graphics/shaders/Billboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace cro::Shaders::Billboard
gl_Position = viewProj * vec4(position, 1.0);
gl_Position /= gl_Position.w;
gl_Position.xy += a_position.xy * (a_texCoord1 / u_screenSize);
gl_Position.xy = (a_position.xy * (a_texCoord1 / u_screenSize)) + gl_Position.xy;
#if defined (VERTEX_LIT)
v_normalVector = vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]);
Expand All @@ -115,8 +115,8 @@ namespace cro::Shaders::Billboard
#else
vec3 camUp = vec3(viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1]) * -u_clipPlane.y;
#endif
position = position + camRight * a_position.x
+ camUp * a_position.y;
position = position + (camRight * a_position.x)
+ (camUp * a_position.y);
gl_Position = viewProj * vec4(position, 1.0);
Expand Down Expand Up @@ -300,7 +300,7 @@ namespace cro::Shaders::Billboard
{
for(int y = 0; y < filterSize; ++y)
{
float pcfDepth = TEXTURE(u_shadowMap, vec3(projectionCoords.xy + kernel[y * filterSize + x] * texelSize, cascadeIndex)).r;
float pcfDepth = TEXTURE(u_shadowMap, vec3((kernel[y * filterSize + x] * texelSize) + projectionCoords.xy, cascadeIndex)).r;
shadow += (projectionCoords.z - 0.001) > pcfDepth ? 0.4 : 0.0;
}
}
Expand Down
12 changes: 7 additions & 5 deletions crogine/src/graphics/shaders/ShaderIncludes.inl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ R"(
static const std::string SkinMatrix =
R"(
mat4 skinMatrix = a_boneWeights.x * u_boneMatrices[int(a_boneIndices.x)];
skinMatrix += a_boneWeights.y * u_boneMatrices[int(a_boneIndices.y)];
skinMatrix += a_boneWeights.z * u_boneMatrices[int(a_boneIndices.z)];
skinMatrix += a_boneWeights.w * u_boneMatrices[int(a_boneIndices.w)];
skinMatrix = a_boneWeights.y * u_boneMatrices[int(a_boneIndices.y)] + skinMatrix;
skinMatrix = a_boneWeights.z * u_boneMatrices[int(a_boneIndices.z)] + skinMatrix;
skinMatrix = a_boneWeights.w * u_boneMatrices[int(a_boneIndices.w)] + skinMatrix;
)";


Expand Down Expand Up @@ -194,7 +196,7 @@ R"(
{
for(int y = 0; y < filterSize; ++y)
{
float pcfDepth = TEXTURE(u_shadowMap, vec3(projectionCoords.xy + kernel[y * filterSize + x] * texelSize, cascadeIndex)).r;
float pcfDepth = TEXTURE(u_shadowMap, vec3((kernel[y * filterSize + x] * texelSize) + projectionCoords.xy, cascadeIndex)).r;
shadow += (projectionCoords.z - bias) > pcfDepth ? 0.4 : 0.0;
}
}
Expand All @@ -216,7 +218,7 @@ R"(
{
for(int y = 0; y < filterSize; ++y)
{
float pcfDepth = TEXTURE(u_shadowMap, vec3(projectionCoords.xy + kernel[y * filterSize + x] * texelSize, cascadeIndex)).r;
float pcfDepth = TEXTURE(u_shadowMap, vec3((kernel[y * filterSize + x] * texelSize) + projectionCoords.xy, cascadeIndex)).r;
shadow += (projectionCoords.z - 0.001) > pcfDepth ? 0.4 : 0.0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/golf/buildnumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef BUILD_NUMBER_H_
#define BUILD_NUMBER_H_

#define BUILDNUMBER 970
#define BUILDNUMBER_STR "970"
#define BUILDNUMBER 973
#define BUILDNUMBER_STR "973"

#endif /* BUILD_NUMBER_H_ */
4 changes: 2 additions & 2 deletions samples/golf/src/GolfGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,9 @@ bool GolfGame::initialise()
#ifdef CRO_DEBUG_
//m_stateStack.pushState(StateID::DrivingRange); //can't go straight to this because menu needs to parse avatar data
//m_stateStack.pushState(StateID::Bush);
m_stateStack.pushState(StateID::Clubhouse);
//m_stateStack.pushState(StateID::Clubhouse);
//m_stateStack.pushState(StateID::SplashScreen);
//m_stateStack.pushState(StateID::Menu);
m_stateStack.pushState(StateID::Menu);
//m_stateStack.pushState(StateID::SQLite);
#else
m_stateStack.pushState(StateID::SplashScreen);
Expand Down
17 changes: 9 additions & 8 deletions samples/golf/src/golf/BillboardShader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static const std::string BillboardVertexShader = R"(
#endif
vec3 camRight = vec3(viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0]);
vec3 camUp = vec3(0.0, 1.0, 0.0);
position = position + camRight * relPos.x
+ camUp * relPos.y
+ cross(camRight, camUp) * relPos.z;
position = position + (camRight * relPos.x)
+ (camUp * relPos.y)
+ (cross(camRight, camUp) * relPos.z);
//---generic wind added to tall billboards---//
Expand All @@ -118,21 +118,21 @@ static const std::string BillboardVertexShader = R"(
vec2 uv = a_normal.xz;
uv.x += u_windData.w * xFreq;
uv.x = (u_windData.w * xFreq) + uv.x;
float windX = TEXTURE(u_noiseTexture, uv).r;
windX *= 2.0;
windX -= 1.0;
uv = a_normal.xz;
uv.y += u_windData.w * yFreq;
uv.y = (u_windData.w * yFreq) + uv.y;
float windZ = TEXTURE(u_noiseTexture, uv).r;
windZ *= 2.0;
windZ -= 1.0;
position.x += windX * totalScale;
position.z += windZ * totalScale;
position.xz += (u_windData.xz * strength * 2.0) * totalScale;
position.x = (windX * totalScale) + position.x;
position.z = (windZ * totalScale) + position.z;
position.xz = ((u_windData.xz * strength * 2.0) * totalScale) + position.xz;
//------------------------------//
//wind from billboard vertex colour (above)
Expand All @@ -149,6 +149,7 @@ static const std::string BillboardVertexShader = R"(
vertPos.xy = floor(vertPos.xy);
vertPos.xy = ((vertPos.xy / u_scaledResolution) * 2.0) - 1.0;
vertPos.xyz *= vertPos.w;*/
gl_Position = vertPos;
v_texCoord0 = a_texCoord0;
Expand Down
14 changes: 7 additions & 7 deletions samples/golf/src/golf/CelShader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static const std::string CelVertexShader = R"(
vec2 texCoord = a_texCoord1;
float scale = texCoord.y;
float instanceOffset = mod(gl_InstanceID, MAX_INSTANCE) * u_offsetMultiplier;
texCoord.y = mod(u_time + (0.15 * instanceOffset), u_maxTime);
texCoord.y = mod((0.15 * instanceOffset)+ u_time, u_maxTime);
vec4 position = vec4(decodeVector(u_vatsPosition, texCoord) * scale, 1.0);
#else
Expand Down Expand Up @@ -203,7 +203,7 @@ static const std::string CelVertexShader = R"(
v_texCoord = a_texCoord0;
#if defined (VATS)
v_texCoord.x /= MAX_INSTANCE;
v_texCoord.x += (1.0 / MAX_INSTANCE) * mod(gl_InstanceID, MAX_INSTANCE);
v_texCoord.x = ((1.0 / MAX_INSTANCE) * mod(gl_InstanceID, MAX_INSTANCE)) + v_texCoord.x;
#endif
#endif
Expand Down Expand Up @@ -371,7 +371,7 @@ static const std::string CelFragmentShader = R"(
{
for(int y = 0; y < filterSize; ++y)
{
float pcfDepth = TEXTURE(u_shadowMap, vec3(projectionCoords.xy + kernel[y * filterSize + x] * texelSize, cascadeIndex)).r;
float pcfDepth = TEXTURE(u_shadowMap, vec3((kernel[y * filterSize + x] * texelSize) + projectionCoords.xy, cascadeIndex)).r;
shadow += (projectionCoords.z - 0.001) > pcfDepth ? 0.4 : 0.0;
}
}
Expand Down Expand Up @@ -488,8 +488,8 @@ float greenTerrain = step(0.065, v_colour.r) * (1.0 - step(0.13, v_colour.r));
//complementaryColour(colour.rgb)
vec3 holeColour = mix(colour.rgb * vec3(0.67, 0.757, 0.41), colour.rgb, 0.65 + (0.35 * holeHeight));
holeColour.r += smoothstep(0.45, 0.99, holeHeight) * 0.01;
holeColour.g += smoothstep(0.65, 0.999, holeHeight) * 0.01;
holeColour.r = (smoothstep(0.45, 0.99, holeHeight) * 0.01) + holeColour.r;
holeColour.g = (smoothstep(0.65, 0.999, holeHeight) * 0.01) + holeColour.g;
//distances are sqr
float holeHeightFade = (1.0 - smoothstep(100.0, 400.0, dot(viewDirection, viewDirection)));
Expand Down Expand Up @@ -517,7 +517,7 @@ float greenTerrain = step(0.065, v_colour.r) * (1.0 - step(0.13, v_colour.r));
int texY = int(mod(texCheck.y, MatrixSize));
float facing = dot(normal, vec3(0.0, 1.0, 0.0));
float waterFade = (1.0 - smoothstep(WaterLevel, WaterLevel + (1.15 * (1.0 - smoothstep(0.89, 0.99, facing))), v_worldPosition.y));
float waterFade = (1.0 - smoothstep(WaterLevel, (1.15 * (1.0 - smoothstep(0.89, 0.99, facing))) + WaterLevel, v_worldPosition.y));
float waterDither = findClosest(texX, texY, waterFade) * waterFade * (1.0 - step(0.96, facing));
#if defined(COMP_SHADE)
Expand Down Expand Up @@ -546,7 +546,7 @@ float greenTerrain = step(0.065, v_colour.r) * (1.0 - step(0.13, v_colour.r));
#endif
#if defined(REFLECTIONS)
colour.rgb += TEXTURE_CUBE(u_reflectMap, reflect(-viewDirection, normal)).rgb * 0.25;
colour.rgb = (TEXTURE_CUBE(u_reflectMap, reflect(-viewDirection, normal)).rgb * 0.25) + colour.rgb;
#endif
FRAG_OUT = vec4(colour.rgb, 1.0);
Expand Down
4 changes: 2 additions & 2 deletions samples/golf/src/golf/CloudShader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static const std::string CloudOverheadFragment = R"(
float rimAmount = dot(vec3(0.0, -1.0, 0.0), normal);
rimAmount += 1.0;
rimAmount /= 2.0;
rimAmount *= 0.5;
rim *= smoothstep(0.5, 0.9, rimAmount);
Expand Down Expand Up @@ -306,7 +306,7 @@ void main()
check *= 2.0;
check -= 1.0;
lightAmount += (0.05 * check);
lightAmount = (0.05 * check) + lightAmount;
FRAG_OUT = mix(u_colourA, u_colourB, step(0.5, lightAmount));
})";
6 changes: 3 additions & 3 deletions samples/golf/src/golf/ProfileState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ void ProfileState::buildScene()
#ifdef USE_GNS
randomButton.getComponent<cro::UIInput>().setNextIndex(ButtonCancel, ButtonWorkshop);
#else
entity.getComponent<cro::UIInput>().setNextIndex(ButtonCancel, ButtonHairColour);
randomButton.getComponent<cro::UIInput>().setNextIndex(ButtonCancel, ButtonHairColour);
#endif
if (!Social::isSteamdeck())
{
Expand Down Expand Up @@ -1085,8 +1085,8 @@ void ProfileState::buildScene()
nameButton.getComponent<cro::UIInput>().setNextIndex(ButtonWorkshop, ButtonBallSelect);
nameButton.getComponent<cro::UIInput>().setPrevIndex(ButtonWorkshop, ButtonCancel);
#else
entity.getComponent<cro::UIInput>().setNextIndex(ButtonDescUp, ButtonBallSelect);
entity.getComponent<cro::UIInput>().setPrevIndex(ButtonNextHair, ButtonBallSelect);
nameButton.getComponent<cro::UIInput>().setNextIndex(ButtonDescUp, ButtonBallSelect);
nameButton.getComponent<cro::UIInput>().setPrevIndex(ButtonNextHair, ButtonBallSelect);
#endif

//ball arrow buttons
Expand Down
12 changes: 6 additions & 6 deletions samples/golf/src/golf/ShaderIncludes.inl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static inline const std::string HSV = R"(
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec4 K = vec4(1.0, 0.666667, 0.333333, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
Expand Down Expand Up @@ -154,12 +154,12 @@ static inline const std::string WindCalc = R"(
WindResult retVal = WindResult(vec2(0.0), vec2(0.0), 0.0);
vec2 uv = localPos;
//uv.x += u_windData.w * (hFreq + (hFreq * u_windData.y));
uv.x += u_windData.w * hFreq;
uv.x = (u_windData.w * hFreq) + uv.x;
retVal.highFreq.x = TEXTURE(u_noiseTexture, uv).r;
uv = localPos;
//uv.y += u_windData.w * (hFreq + (hFreq * u_windData.y));
uv.y += u_windData.w * hFreq;
uv.y = (u_windData.w * hFreq) + uv.y;
retVal.highFreq.y = TEXTURE(u_noiseTexture, uv).r;
uv = worldPos;
Expand All @@ -175,13 +175,13 @@ static inline const std::string WindCalc = R"(
retVal.highFreq -= 1.0;
retVal.highFreq *= u_windData.y;
//retVal.highFreq *= hMagnitude;
retVal.highFreq *= hMagnitude + (hMagnitude * u_windData.y);
retVal.highFreq *= (hMagnitude * u_windData.y) + hMagnitude;
retVal.lowFreq *= 2.0;
retVal.lowFreq -= 1.0;
retVal.lowFreq *= (0.6 + (0.4 * u_windData.y));
retVal.lowFreq *= ((0.4 * u_windData.y)+ 0.6);
//retVal.lowFreq *= lMagnitude;
retVal.lowFreq *= lMagnitude + (lMagnitude * u_windData.y);
retVal.lowFreq *= (lMagnitude * u_windData.y) + lMagnitude;
retVal.strength = u_windData.y;
retVal.strength *= dirMagnitude;
Expand Down
9 changes: 2 additions & 7 deletions samples/golf/src/golf/TerrainShader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,11 @@ static const std::string TerrainVertexShader = R"(
//VARYING_OUT float v_viewDepth;
#endif
vec3 lerp(vec3 a, vec3 b, float t)
{
return a + ((b - a) * t);
}
void main()
{
v_cameraWorldPosition = u_cameraWorldPosition;
vec4 position = u_worldMatrix * vec4(lerp(a_position.xyz, a_tangent, u_morphTime), 1.0);
vec4 position = u_worldMatrix * vec4(mix(a_position.xyz, a_tangent, u_morphTime), 1.0);
//gl_Position = u_viewProjectionMatrix * position;
vec4 vertPos = u_viewProjectionMatrix * position;
Expand All @@ -105,7 +100,7 @@ static const std::string TerrainVertexShader = R"(
#endif
//this should be a slerp really but lerp is good enough for low spec shenanigans
v_normal = u_normalMatrix * lerp(a_normal, a_bitangent, u_morphTime);
v_normal = u_normalMatrix * mix(a_normal, a_bitangent, u_morphTime);
v_colour = a_colour;
v_worldPosition = position.xyz;
v_texCoord = vec2(position.x / 100.0, position.z / -62.5);
Expand Down
Loading

0 comments on commit 96b28a3

Please sign in to comment.