Skip to content

Commit

Permalink
Merge branch 'main' into gsterbrant_degamma_specular_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GSterbrant committed Jul 14, 2022
2 parents 8a2369c + 8869c36 commit 33d1a72
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 62 deletions.
10 changes: 10 additions & 0 deletions src/graphics/program-lib/chunk-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class ChunkBuilder {
}
});
}

prepend(...chunks) {
chunks.forEach((chunk) => {
if (chunk.endsWith('\n')) {
this.code = chunk + this.code;
} else {
this.code = chunk + '\n' + this.code;
}
});
}
}

export { ChunkBuilder };
8 changes: 4 additions & 4 deletions src/graphics/program-lib/chunk-utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const decodeTable = {
'linear': 'decodeLinear',
'srgb': 'decodeGamma',
'rgbm': 'decodeRGBM',
'rgbe': 'decodeRGBE'
linear: 'decodeLinear',
srgb: 'decodeGamma',
rgbm: 'decodeRGBM',
rgbe: 'decodeRGBE'
};

class ChunkUtils {
Expand Down
2 changes: 2 additions & 0 deletions src/graphics/program-lib/chunks/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ import TBNPS from './lit/frag/TBN.js';
import TBNderivativePS from './lit/frag/TBNderivative.js';
import TBNfastPS from './lit/frag/TBNfast.js';
import TBNObjectSpacePS from './lit/frag/TBNObjectSpace.js';
import textureSamplePS from './lit/frag/textureSample.js';
import tonemappingAcesPS from './common/frag/tonemappingAces.js';
import tonemappingAces2PS from './common/frag/tonemappingAces2.js';
import tonemappingFilmicPS from './common/frag/tonemappingFilmic.js';
Expand Down Expand Up @@ -379,6 +380,7 @@ const shaderChunks = {
TBNderivativePS,
TBNfastPS,
TBNObjectSpacePS,
textureSamplePS,
tonemappingAcesPS,
tonemappingAces2PS,
tonemappingFilmicPS,
Expand Down
8 changes: 8 additions & 0 deletions src/graphics/program-lib/chunks/common/frag/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ vec3 decodeLinear(vec4 raw) {
return raw.rgb;
}
float decodeGamma(float raw) {
return pow(raw, 2.2);
}
vec3 decodeGamma(vec3 raw) {
return pow(raw, vec3(2.2));
}
vec3 decodeGamma(vec4 raw) {
return pow(raw.xyz, vec3(2.2));
}
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/program-lib/chunks/common/frag/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ vec4 encodeLinear(vec3 source) {
return vec4(source, 1.0);
}
vec4 encodeGamma(vec3 source) {
return vec4(pow(source + 0.0000001, vec3(1.0 / 2.2)), 1.0);
vec3 encodeGamma(vec3 source) {
return pow(source + 0.0000001, vec3(1.0 / 2.2));
}
vec4 encodeRGBM(vec3 source) { // modified RGBM
Expand Down
18 changes: 3 additions & 15 deletions src/graphics/program-lib/chunks/common/frag/gamma1_0.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
export default /* glsl */`
vec4 texture2DSRGB(sampler2D tex, vec2 uv) {
return texture2D(tex, uv);
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv, float bias) {
return texture2D(tex, uv, bias);
}
vec4 textureCubeSRGB(samplerCube tex, vec3 uvw) {
return textureCube(tex, uvw);
}
vec3 gammaCorrectOutput(vec3 color) {
float gammaCorrectInput(float color) {
return color;
}
vec3 gammaCorrectInput(vec3 color) {
return color;
}
float gammaCorrectInput(float color) {
vec4 gammaCorrectInput(vec4 color) {
return color;
}
vec4 gammaCorrectInput(vec4 color) {
vec3 gammaCorrectOutput(vec3 color) {
return color;
}
`;
37 changes: 9 additions & 28 deletions src/graphics/program-lib/chunks/common/frag/gamma2_2.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
export default /* glsl */`
vec3 gammaCorrectInput(vec3 color) {
return pow(color, vec3(2.2));
}
float gammaCorrectInput(float color) {
return pow(color, 2.2);
}
vec4 gammaCorrectInput(vec4 color) {
return vec4(pow(color.rgb, vec3(2.2)), color.a);
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv) {
vec4 rgba = texture2D(tex, uv);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
return decodeGamma(color);
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv, float bias) {
vec4 rgba = texture2D(tex, uv, bias);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
vec3 gammaCorrectInput(vec3 color) {
return decodeGamma(color);
}
vec4 textureCubeSRGB(samplerCube tex, vec3 uvw) {
vec4 rgba = textureCube(tex, uvw);
rgba.rgb = gammaCorrectInput(rgba.rgb);
return rgba;
vec4 gammaCorrectInput(vec4 color) {
return vec4(decodeGamma(color.xyz), color.w);
}
vec3 gammaCorrectOutput(vec3 color) {
#ifdef HDR
#ifdef HDR
return color;
#else
color += vec3(0.0000001);
return pow(color, vec3(0.45));
#endif
#else
return pow(color + 0.0000001, vec3(1.0 / 2.2));
#endif
}
`;
7 changes: 3 additions & 4 deletions src/graphics/program-lib/chunks/lit/frag/lightmapDirAdd.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
export default /* glsl */`
void addLightMap() {
if (dot(dLightmapDir, vec3(1.0)) < 0.00001) {
if (dot(dLightmapDir, dLightmapDir) < 0.0001) {
dDiffuseLight += dLightmap;
} else {
dLightDirNormW = normalize(dLightmapDir * 2.0 - vec3(1.0));
dLightDirNormW = dLightmapDir;
float vlight = saturate(dot(dLightDirNormW, -dVertexNormalW));
float flight = saturate(dot(dLightDirNormW, -dNormalW));
float nlight = (flight / max(vlight, 0.01)) * 0.5;
dDiffuseLight += dLightmap * nlight * 2.0;
dSpecularLight += dLightmap * getLightSpecular(normalize(-dLightmapDir + dViewDirW));
}
dSpecularLight += dLightmap * getLightSpecular();
}
`;
25 changes: 25 additions & 0 deletions src/graphics/program-lib/chunks/lit/frag/textureSample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default /* glsl */`
vec4 texture2DSRGB(sampler2D tex, vec2 uv) {
return gammaCorrectInput(texture2D(tex, uv));
}
vec4 texture2DSRGB(sampler2D tex, vec2 uv, float bias) {
return gammaCorrectInput(texture2D(tex, uv, bias));
}
vec3 texture2DRGBM(sampler2D tex, vec2 uv) {
return decodeRGBM(texture2D(tex, uv));
}
vec3 texture2DRGBM(sampler2D tex, vec2 uv, float bias) {
return decodeRGBM(texture2D(tex, uv, bias));
}
vec3 texture2DRGBE(sampler2D tex, vec2 uv) {
return decodeRGBM(texture2D(tex, uv));
}
vec3 texture2DRGBE(sampler2D tex, vec2 uv, float bias) {
return decodeRGBM(texture2D(tex, uv, bias));
}
`;
4 changes: 2 additions & 2 deletions src/graphics/program-lib/chunks/particle/frag/particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ float unpackFloat(vec4 rgbaDepth) {
#endif
void main(void) {
vec4 tex = texture2DSRGB(colorMap, vec2(texCoordsAlphaLife.x, 1.0 - texCoordsAlphaLife.y));
vec4 ramp = texture2DSRGB(colorParam, vec2(texCoordsAlphaLife.w, 0.0));
vec4 tex = gammaCorrectInput(texture2D(colorMap, vec2(texCoordsAlphaLife.x, 1.0 - texCoordsAlphaLife.y)));
vec4 ramp = gammaCorrectInput(texture2D(colorParam, vec2(texCoordsAlphaLife.w, 0.0)));
ramp.rgb *= colorMult;
ramp.a += texCoordsAlphaLife.z;
Expand Down
5 changes: 4 additions & 1 deletion src/graphics/program-lib/chunks/standard/frag/lightmapDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ uniform sampler2D texture_dirLightMap;
void getLightMap() {
dLightmap = $DECODE(texture2D(texture_lightMap, $UV, textureBias)).$CH;
dLightmapDir = texture2D(texture_dirLightMap, $UV).xyz;
vec3 dir = texture2D(texture_dirLightMap, $UV, textureBias).xyz * 2.0 - 1.0;
float dirDot = dot(dir, dir);
dLightmapDir = (dirDot > 0.001) ? dir / sqrt(dirDot) : vec3(0.0);
}
`;
2 changes: 1 addition & 1 deletion src/graphics/program-lib/chunks/standard/frag/specular.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void getSpecularity() {
#ifdef MAPTEXTURE
#ifdef DEGAMMA_SPECULAR
specularColor *= texture2DSRGB(texture_specularMap, $UV, textureBias).$CH;
specularColor *= gammaCorrectInput(texture2D(texture_specularMap, $UV, textureBias).$CH);
#else
specularColor *= texture2D(texture_specularMap, $UV, textureBias).$CH;
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/program-lib/programs/lit-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,13 @@ class LitShader {
}
}

// FIXME: only add these when needed
code += chunks.sphericalPS;
code += chunks.decodePS;
code += gammaCode(options.gamma, chunks);
code += tonemapCode(options.toneMap, chunks);
code += fogCode(options.fog, chunks);

// FIXME: only add decode when needed
code += chunks.decodePS;
code += chunks.sphericalPS;

// frontend
code += this.frontendCode;
Expand Down
1 change: 1 addition & 0 deletions src/graphics/program-lib/programs/particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const particle = {
if (options.soft) fshader += "\nvarying float vDepth;\n";

if ((options.normal === 0) && (options.fog === "none")) options.srgb = false; // don't have to perform all gamma conversions when no lighting and fogging is used
fshader += shaderChunks.decodePS;
fshader += gammaCode(options.gamma);
fshader += tonemapCode(options.toneMap);

Expand Down
4 changes: 2 additions & 2 deletions src/graphics/program-lib/programs/skybox.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ const skybox = {
fshader = precisionCode(device);
fshader += options.mip ? shaderChunks.fixCubemapSeamsStretchPS : shaderChunks.fixCubemapSeamsNonePS;
fshader += options.useIntensity ? shaderChunks.envMultiplyPS : shaderChunks.envConstPS;
fshader += shaderChunks.decodePS;
fshader += gammaCode(options.gamma);
fshader += tonemapCode(options.toneMapping);
fshader += shaderChunks.decodePS;
fshader += shaderChunks.skyboxHDRPS
.replace(/\$DECODE/g, ChunkUtils.decodeFunc(options.encoding))
.replace(/\$FIXCONST/g, (1 - 1 / mip2size[options.mip]) + "");
} else {
fshader = precisionCode(device);
fshader += options.useIntensity ? shaderChunks.envMultiplyPS : shaderChunks.envConstPS;
fshader += shaderChunks.decodePS;
fshader += gammaCode(options.gamma);
fshader += tonemapCode(options.toneMapping);
fshader += shaderChunks.decodePS;
fshader += shaderChunks.sphericalPS;
fshader += shaderChunks.envAtlasPS;
fshader += shaderChunks.skyboxEnvPS.replace(/\$DECODE/g, ChunkUtils.decodeFunc(options.encoding));
Expand Down
22 changes: 22 additions & 0 deletions src/graphics/program-lib/programs/standard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { hashCode } from '../../../core/hash.js';
import { Debug } from '../../../core/debug.js';

import {
BLEND_NONE, FRESNEL_SCHLICK, LIGHTTYPE_DIRECTIONAL,
Expand Down Expand Up @@ -155,6 +156,18 @@ const standard = {

if (encoding) {
subCode = subCode.replace(/\$DECODE/g, ChunkUtils.decodeFunc((!options.gamma && encoding === 'srgb') ? 'linear' : encoding));

// continue to support $texture2DSAMPLE
if (subCode.indexOf('$texture2DSAMPLE')) {
const decodeTable = {
linear: 'texture2D',
srgb: 'texture2DSRGB',
rgbm: 'texture2DRGBM',
rgbe: 'texture2DRGBE'
};

subCode = subCode.replace(/\$texture2DSAMPLE/g, decodeTable[encoding] || 'texture2D');
}
}
}

Expand Down Expand Up @@ -374,6 +387,15 @@ const standard = {
code.append(this._addMap("light", lightmapChunkPropName, options, litShader.chunks, options.lightMapEncoding));
func.append("getLightMap();");
}

// only add the legacy chunk if it's referenced
if (code.code.indexOf('texture2DSRGB') !== -1 ||
code.code.indexOf('texture2DRGBM') !== -1 ||
code.code.indexOf('texture2DRGBE') !== -1) {
Debug.deprecated('Shader chunk macro $texture2DSAMPLE(XXX) is deprecated. Please use $DECODE(texture2D(XXX)) instead.');
code.prepend(litShader.chunks.textureSamplePS);
}

} else {
// all other passes require only opacity
if (options.alphaTest) {
Expand Down

0 comments on commit 33d1a72

Please sign in to comment.