Skip to content

Commit

Permalink
chore(rendering): migrate reworking glsl osx (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored Jun 26, 2021
1 parent 8bf8345 commit b6292a2
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 71 deletions.
6 changes: 4 additions & 2 deletions assets/shaders/blur_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ const vec2 taps[12] = vec2[12](
vec2(-0.32194,-0.932615), vec2(-0.791559,-0.59771)
);

layout(location = 0) out vec4 outColor;

void main() {
vec4 sampleAccum = vec4(0.0, 0.0, 0.0, 0.0);

for (int nTapIndex = 0; nTapIndex < 12; nTapIndex++) {
vec2 tapcoord = v_uv0.xy + texelSize * taps[nTapIndex] * radius;
sampleAccum += texture2D(tex, tapcoord);
sampleAccum += texture(tex, tapcoord);
}

gl_FragData[0].rgba = sampleAccum / 12.0;
outColor = sampleAccum / 12.0;
}
53 changes: 28 additions & 25 deletions assets/shaders/chunk_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ mat2 inverse2(mat2 m) {
return mat2(m[1][1], -m[0][1], -m[1][0], m[0][0]) / det;
}

layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outNormal;
layout(location = 2) out vec4 outLight;

void main() {

// Active for worldReflectionNode only.
Expand Down Expand Up @@ -102,7 +106,7 @@ void main() {
#if defined (PARALLAX_MAPPING)
vec2 viewDirectionUvProjection = -normalizedViewPos * uvToView;

float height = parallaxScale * texture2D(textureAtlasHeight, texCoord).r - parallaxBias;
float height = parallaxScale * texture(textureAtlasHeight, texCoord).r - parallaxBias;
// Ideally this should be divided by dot(normal, normalizedViewPos), as the offset for texCoord
// is the component parallel to the surface of a vector along the view's forward axis,
// the other component being a vector perpendicular to the surface and having magnitude "height".
Expand All @@ -119,10 +123,10 @@ void main() {
// Normalised but not orthonormalised. It should be orthogonal anyway (except for some non-rectangular
// block shapes like torches), but it's not obvious what's the best thing to do when it isn't.
mat3 uvnSpaceToViewSpace = mat3(normalize(uvToView[0]), normalize(uvToView[1]), normal);
normalOpaque = normalize(texture2D(textureAtlasNormal, texCoord).xyz * 2.0 - 1.0);
normalOpaque = normalize(texture(textureAtlasNormal, texCoord).xyz * 2.0 - 1.0);
normalOpaque = normalize(uvnSpaceToViewSpace * normalOpaque);

shininess = texture2D(textureAtlasNormal, texCoord).w;
shininess = texture(textureAtlasNormal, texCoord).w;
#endif
#endif

Expand All @@ -138,8 +142,8 @@ void main() {
vec2 waterOffset = vec2(scaledVertexWorldPos.x + timeToTick(time, 0.0075), scaledVertexWorldPos.y + timeToTick(time, 0.0075));
vec2 waterOffset2 = vec2(scaledVertexWorldPos.x + timeToTick(time, 0.005), scaledVertexWorldPos.y - timeToTick(time, 0.005));

normalWaterOffset = (texture2D(textureWaterNormal, waterOffset).xyz * 2.0 - 1.0).xy;
normalWaterOffset += (texture2D(textureWaterNormalAlt, waterOffset2).xyz * 2.0 - 1.0).xy;
normalWaterOffset = (texture(textureWaterNormal, waterOffset).xyz * 2.0 - 1.0).xy;
normalWaterOffset += (texture(textureWaterNormalAlt, waterOffset2).xyz * 2.0 - 1.0).xy;
normalWaterOffset *= 0.5 * (1.0 / vertexViewPos.z * waterNormalBias);

normalWater.xy += normalWaterOffset;
Expand All @@ -163,7 +167,7 @@ void main() {
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

#if !defined (FEATURE_REFRACTIVE_PASS)
color = texture2D(textureAtlas, texCoord.xy);
color = texture(textureAtlas, texCoord.xy);

#if defined FEATURE_ALPHA_REJECT
if (color.a < 0.1) {
Expand Down Expand Up @@ -211,9 +215,8 @@ void main() {
float specularHighlight = WATER_SPEC * calcDayAndNightLightingFactor(daylightValue, daylight) * calcSpecLightNormalized(normalWater, sunVecViewAdjusted, normalizedViewPos, waterSpecExp);
color.xyz += vec3(specularHighlight, specularHighlight, specularHighlight);

vec4 reflectionColor = vec4(texture2D(textureWaterReflection, projectedPos + normalWaterOffset.xy * waterRefraction).xyz, 1.0);
vec4 refractionColor = vec4(texture2D(texSceneOpaque, projectedPos + normalWaterOffset.xy * waterRefraction).xyz, 1.0);

vec4 reflectionColor = vec4(texture(textureWaterReflection, projectedPos + normalWaterOffset.xy * waterRefraction).xyz, 1.0);
vec4 refractionColor = vec4(texture(texSceneOpaque, projectedPos + normalWaterOffset.xy * waterRefraction).xyz, 1.0);
vec4 litWaterTint = vec4(WATER_TINT) * vec4(combinedLightValue.x, combinedLightValue.y, combinedLightValue.z, 1.0);

/* FRESNEL */
Expand All @@ -230,17 +233,17 @@ void main() {
texCoord.x = mod(texCoord.x, TEXTURE_OFFSET) * (1.0 / TEXTURE_OFFSET);
texCoord.y = mod(texCoord.y, TEXTURE_OFFSET) / (128.0 / (1.0 / TEXTURE_OFFSET));
texCoord.y += mod(timeToTick(time, -0.1), 127.0) * (1.0/128.0);

vec4 albedoColor = texture2D(textureWater, texCoord.xy).rgba;
vec4 albedoColor = texture(textureWater, texCoord.xy).rgba;
albedoColor.rgb *= combinedLightValue;

vec3 refractionColor = texture2D(texSceneOpaque, projectedPos + albedoColor.rg * 0.05).rgb;
vec3 refractionColor = texture(texSceneOpaque, projectedPos + albedoColor.rg * 0.05).rgb;


color.rgb += mix(refractionColor, albedoColor.rgb, albedoColor.a);
color.a = 1.0;
} else {
vec3 refractionColor = texture2D(texSceneOpaque, projectedPos).rgb;
vec4 albedoColor = texture2D(textureAtlas, texCoord.xy);
vec3 refractionColor = texture(texSceneOpaque, projectedPos).rgb;
vec4 albedoColor = texture(textureAtlas, texCoord.xy);
albedoColor.rgb *= combinedLightValue;

// TODO: Add support for actual refraction here
Expand All @@ -251,26 +254,26 @@ void main() {
// Apply the final lighting mix
color.xyz *= combinedLightValue * occlusionValue;
#else
gl_FragData[2].rgba = vec4(blocklightColorBrightness, daylightValue, 0.0, 0.0);
outLight.rgba = vec4(blocklightColorBrightness, daylightValue, 0.0, 0.0);
#endif

#if defined (FEATURE_REFRACTIVE_PASS) || defined (FEATURE_USE_FORWARD_LIGHTING)
gl_FragData[0].rgba = color.rgba;
#if defined (FEATURE_REFRACTIVE_PASS)
// Encode "reflection" intensity into normal alpha
gl_FragData[1].a = 1.0;
#endif
outColor.rgba = color.rgba;
#if defined (FEATURE_REFRACTIVE_PASS)
// Encode "reflection" intensity into normal alpha
outNormal.a = 1.0;
#endif
#else
gl_FragData[0].rgb = color.rgb;
outColor.rgb = color.rgb;
// Encode occlusion value into the alpha channel
gl_FragData[0].a = occlusionValue;
outColor.a = occlusionValue;
// Encode shininess value into the normal alpha channel
gl_FragData[1].a = shininess;
outNormal.a = shininess;
#endif

#if !defined (FEATURE_REFRACTIVE_PASS) && !defined (FEATURE_USE_FORWARD_LIGHTING)
gl_FragData[1].rgb = vec3(normalOpaque.x / 2.0 + 0.5, normalOpaque.y / 2.0 + 0.5, normalOpaque.z / 2.0 + 0.5);
outNormal.rgb = vec3(normalOpaque.x / 2.0 + 0.5, normalOpaque.y / 2.0 + 0.5, normalOpaque.z / 2.0 + 0.5);
#elif defined (FEATURE_REFRACTIVE_PASS)
gl_FragData[1].rgb = vec3(normalWater.x / 2.0 + 0.5, normalWater.y / 2.0 + 0.5, normalWater.z / 2.0 + 0.5);
outNormal.rgb = vec3(normalWater.x / 2.0 + 0.5, normalWater.y / 2.0 + 0.5, normalWater.z / 2.0 + 0.5);
#endif
}
14 changes: 8 additions & 6 deletions assets/shaders/downSampler_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ const vec2 s2 = vec2( 1, 1);
const vec2 s3 = vec2( 1,-1);
const vec2 s4 = vec2(-1,-1);

layout(location = 0) out vec4 outColor;

void main() {
vec2 texCoordSample = vec2(0.0);
vec2 texCoordSample = vec2(0.0);

texCoordSample = v_uv0.xy + s1 / size;
vec4 color = texture2D(tex, texCoordSample);
vec4 color = texture(tex, texCoordSample);

texCoordSample = v_uv0.xy + s2 / size;
color += texture2D(tex, texCoordSample);
color += texture(tex, texCoordSample);

texCoordSample = v_uv0.xy + s3 / size;
color += texture2D(tex, texCoordSample);
color += texture(tex, texCoordSample);

texCoordSample = v_uv0.xy + s4 / size;
color += texture2D(tex, texCoordSample);
color += texture(tex, texCoordSample);

gl_FragData[0].rgba = color * 0.25;
outColor = color * 0.25;
}
6 changes: 4 additions & 2 deletions assets/shaders/highPass_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ in vec2 v_uv0;
uniform sampler2D tex;
uniform float highPassThreshold;

layout(location = 0) out vec4 outColor;

void main() {
vec4 color = texture(tex, v_uv0.xy);

Expand All @@ -15,8 +17,8 @@ void main() {
// bright = smoothstep(0.0, 0.5, bright);

if(relativeLuminance * highPassThreshold > 1.0) {
gl_FragData[0].rgba = vec4(color.rgb, 1);
outColor.rgba = vec4(color.rgb, 1);
} else {
gl_FragData[0].rgba = vec4(0);
outColor.rgba = vec4(0);
}
}
4 changes: 3 additions & 1 deletion assets/shaders/initialPost_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ uniform sampler2D texLightShafts;

in vec2 v_uv0;

layout(location = 0) out vec4 outColor;

void main() {

vec4 color = texture(texScene, v_uv0.xy);
Expand All @@ -29,5 +31,5 @@ void main() {
color += colorBloom * bloomFactor;
#endif

gl_FragData[0].rgba = color.rgba;
outColor.rgba = color.rgba;
}
14 changes: 8 additions & 6 deletions assets/shaders/lightBufferPass_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ uniform sampler2D texSceneOpaqueDepth;
uniform sampler2D texSceneOpaqueNormals;
uniform sampler2D texSceneOpaqueLightBuffer;

layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outNormal;
layout(location = 2) out vec4 outLight;

void main() {
vec4 colorOpaque = texture(texSceneOpaque, v_uv0.xy);
float depthOpaque = texture(texSceneOpaqueDepth, v_uv0.xy).r * 2.0 - 1.0;
Expand All @@ -28,11 +32,9 @@ void main() {
colorOpaque.rgb += lightBufferOpaque.aaa;
}

gl_FragData[0].rgba = colorOpaque.rgba;
gl_FragData[1].rgba = normalBuffer.rgba;
gl_FragData[2].rgb = blocklightColor.rgb;
gl_FragData[2].a = sunlightIntensity;


outColor.rgba = colorOpaque.rgba;
outNormal.rgba = normalBuffer.rgba;
outLight.rgb = blocklightColor.rgb;
outLight.a = sunlightIntensity;
gl_FragDepth = depthOpaque * 0.5 + 0.5;
}
14 changes: 8 additions & 6 deletions assets/shaders/lightGeometryPass_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ uniform vec4 lightExtendedProperties;

uniform mat4 invProjMatrix;

layout(location = 0) out vec4 outLight;

void main() {

#if defined (FEATURE_LIGHT_POINT)
Expand All @@ -54,10 +56,10 @@ void main() {
vec2 projectedPos = vec2(0.0);
#endif

vec4 normalBuffer = texture2D(texSceneOpaqueNormals, projectedPos.xy).rgba;
vec4 normalBuffer = texture(texSceneOpaqueNormals, projectedPos.xy).rgba;
vec3 normal = normalize(normalBuffer.xyz * 2.0 - 1.0);
float shininess = normalBuffer.a;
highp float depth = texture2D(texSceneOpaqueDepth, projectedPos.xy).r * 2.0 - 1.0;
highp float depth = texture(texSceneOpaqueDepth, projectedPos.xy).r * 2.0 - 1.0;

vec3 lightDir;
// TODO: Costly - would be nice to use Crytek's view frustum ray method at this point
Expand Down Expand Up @@ -98,7 +100,7 @@ void main() {
// TODO: Add shader parameters for this...
// Get the preconfigured value from the randomized texture, sampling a value from it to determine how much cloud shadow there will be.
// Clamp the value so that clouds do not turn the surface black.
float cloudOcclusion = clamp(texture2D(texSceneClouds, (worldPosition.xz + cameraPosition.xz) * 0.005 + timeToTick(time, 0.002)).r * 10,0.6,1);
float cloudOcclusion = clamp(texture(texSceneClouds, (worldPosition.xz + cameraPosition.xz) * 0.005 + timeToTick(time, 0.002)).r * 10,0.6,1);

// Combine the cloud shadow with the dynamic shadows
shadowTerm *= rescaleRange(cloudOcclusion,0,1,0,shadowTerm);
Expand Down Expand Up @@ -138,7 +140,7 @@ void main() {
vec3 color = ambTerm * lightColorAmbient;
color *= lightColorDiffuse * lightDiffuseIntensity * lambTerm;
#elif defined (FEATURE_LIGHT_DIRECTIONAL)
vec4 lightBuffer = texture2D(texSceneOpaqueLightBuffer, projectedPos.xy);
vec4 lightBuffer = texture(texSceneOpaqueLightBuffer, projectedPos.xy);
float sunlightIntensity = lightBuffer.y;
vec3 color = calcSunlightColorDeferred(sunlightIntensity, lambTerm, ambTerm, lightDiffuseIntensity, lightColorAmbient, lightColorDiffuse);
#else
Expand All @@ -165,9 +167,9 @@ void main() {

// TODO A 3D wizard should take a look at this. Configurable for the moment to make better comparisons possible.
#if defined (CLAMP_LIGHTING)
gl_FragData[0].rgba = clamp(vec4(color.r, color.g, color.b, specular), 0.0, 1.0);
outLight.rgba = clamp(vec4(color.r, color.g, color.b, specular), 0.0, 1.0);
#else
gl_FragData[0].rgba = vec4(color.r, color.g, color.b, specular);
outLight.rgba = vec4(color.r, color.g, color.b, specular);
#endif

}
8 changes: 5 additions & 3 deletions assets/shaders/lightShafts_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ uniform sampler2D texScene;
uniform float lightDirDotViewDir;
uniform vec2 lightScreenPos;

layout(location = 0) out vec4 outExposure;

void main() {
gl_FragData[0].rgba = vec4(0.0, 0.0, 0.0, 1.0);
outExposure.rgba = vec4(0.0, 0.0, 0.0, 1.0);

if (lightDirDotViewDir > 0.0) {
vec2 uv0 = v_uv0;
Expand All @@ -35,10 +37,10 @@ void main() {
vec3 sampler = texture(texScene, uv0).rgb;

sampler *= illuminationDecay * weight;
gl_FragData[0].rgb += sampler;
outExposure.rgb += sampler;
illuminationDecay *= decay;
}

gl_FragData[0].rgb *= exposure * lightDirDotViewDir;
outExposure.rgb *= exposure * lightDirDotViewDir;
}
}
4 changes: 3 additions & 1 deletion assets/shaders/outputPass_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ in vec2 v_uv0;

uniform sampler2D target;

layout(location = 0) out vec4 outColor;

void main() {
vec4 diffColor = texture(target, v_uv0.xy);

Expand All @@ -15,5 +17,5 @@ void main() {
}
#endif

gl_FragData[0].rgba = diffColor;
outColor.rgba = diffColor;
}
4 changes: 3 additions & 1 deletion assets/shaders/post_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ uniform mat4 invViewProjMatrix;
uniform mat4 prevViewProjMatrix;
#endif

layout(location = 0) out vec4 outColor;

void main() {
#if !defined (NO_BLUR)
vec4 colorBlur = texture(texBlur, v_uv0.xy);
Expand Down Expand Up @@ -112,5 +114,5 @@ void main() {
vec3 lutOffset = vec3(1.0 / 32.0);
finalColor.rgb = texture(texColorGradingLut, lutScale * finalColor.rgb + lutOffset).rgb;

gl_FragData[0].rgba = finalColor;
outColor.rgba = finalColor;
}
10 changes: 7 additions & 3 deletions assets/shaders/prePostComposite_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ uniform vec3 volumetricFogSettings;
uniform vec3 fogWorldPosition;
#endif

layout(location = 0) out vec4 outColor;
layout(location = 1) out vec4 outNormal;
layout(location = 2) out vec4 outLight;

void main() {
vec4 colorOpaque = texture(texSceneOpaque, v_uv0.xy);
float depthOpaque = texture(texSceneOpaqueDepth, v_uv0.xy).r * 2.0 - 1.0;
Expand Down Expand Up @@ -164,8 +168,8 @@ void main() {
float fade = clamp(1.0 - colorTransparent.a, 0.0, 1.0);
vec4 color = mix(colorTransparent, colorOpaque, fade);

gl_FragData[0].rgba = color.rgba;
gl_FragData[1].rgba = normalOpaque.rgba;
gl_FragData[2].rgba = lightBufferOpaque.rgba;
outColor.rgba = color.rgba;
outNormal.rgba = normalOpaque.rgba;
outLight.rgba = lightBufferOpaque.rgba;
gl_FragDepth = depthOpaque * 0.5 + 0.5;
}
4 changes: 3 additions & 1 deletion assets/shaders/sky_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const vec4 eyePos = vec4(0.0, 0.0, 0.0, 1.0);
#define SUN_HIGHLIGHT_INTENSITY_FACTOR 1.0
#define MOON_HIGHLIGHT_INTENSITY_FACTOR 1.0

layout(location = 0) out vec4 outColor;

void main () {
vec3 v = normalize(v_position.xyz);
vec3 l = normalize(sunVec.xyz);
Expand Down Expand Up @@ -61,5 +63,5 @@ void main () {

skyColor.rgb += vec3((1.0 - cloudsColor.r) * sunHighlight + (1.0 - cloudsColor.r) * moonHighlight);

gl_FragData[0].rgba = skyColor.rgba;
outColor.rgba = skyColor.rgba;
}
Loading

0 comments on commit b6292a2

Please sign in to comment.