From 808e38b1928afe58708693f1c46844924d34b660 Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Tue, 1 Aug 2023 15:22:49 +0200 Subject: [PATCH] VideoTexture: Restore inline sRGB decode. --- .../shaders/ShaderChunk/map_fragment.glsl.js | 12 +++++++++++- src/renderers/shaders/ShaderLib/background.glsl.js | 8 ++++++++ src/renderers/webgl/WebGLProgram.js | 2 ++ src/renderers/webgl/WebGLPrograms.js | 6 +++++- src/renderers/webgl/WebGLTextures.js | 4 ++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/renderers/shaders/ShaderChunk/map_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/map_fragment.glsl.js index 50998dba988e1d..393bf0b5a274cf 100644 --- a/src/renderers/shaders/ShaderChunk/map_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/map_fragment.glsl.js @@ -1,7 +1,17 @@ export default /* glsl */` #ifdef USE_MAP - diffuseColor *= texture2D( map, vMapUv ); + vec4 sampledDiffuseColor = texture2D( map, vMapUv ); + + #ifdef DECODE_VIDEO_TEXTURE + + // use inline sRGB decode until browsers properly support SRGB8_APLHA8 with video textures + + sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w ); + + #endif + + diffuseColor *= sampledDiffuseColor; #endif `; diff --git a/src/renderers/shaders/ShaderLib/background.glsl.js b/src/renderers/shaders/ShaderLib/background.glsl.js index a3a44685420715..4d87ca728ec0d2 100644 --- a/src/renderers/shaders/ShaderLib/background.glsl.js +++ b/src/renderers/shaders/ShaderLib/background.glsl.js @@ -21,6 +21,14 @@ void main() { vec4 texColor = texture2D( t2D, vUv ); + #ifdef DECODE_VIDEO_TEXTURE + + // use inline sRGB decode until browsers properly support SRGB8_APLHA8 with video textures + + texColor = vec4( mix( pow( texColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), texColor.rgb * 0.0773993808, vec3( lessThanEqual( texColor.rgb, vec3( 0.04045 ) ) ) ), texColor.w ); + + #endif + texColor.rgb *= backgroundIntensity; gl_FragColor = texColor; diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index 768db42e807945..c10f0a4dff468e 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -765,6 +765,8 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { parameters.useLegacyLights ? '#define LEGACY_LIGHTS' : '', + parameters.decodeVideoTexture ? '#define DECODE_VIDEO_TEXTURE' : '', + parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', ( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '', diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index ca9d518781fc84..0457aa0ebb1616 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -1,4 +1,4 @@ -import { BackSide, DoubleSide, CubeUVReflectionMapping, ObjectSpaceNormalMap, TangentSpaceNormalMap, NoToneMapping, NormalBlending, LinearSRGBColorSpace } from '../../constants.js'; +import { BackSide, DoubleSide, CubeUVReflectionMapping, ObjectSpaceNormalMap, TangentSpaceNormalMap, NoToneMapping, NormalBlending, LinearSRGBColorSpace, SRGBColorSpace } from '../../constants.js'; import { Layers } from '../../core/Layers.js'; import { WebGLProgram } from './WebGLProgram.js'; import { WebGLShaderCache } from './WebGLShaderCache.js'; @@ -335,6 +335,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities toneMapping: toneMapping, useLegacyLights: renderer._useLegacyLights, + decodeVideoTexture: HAS_MAP && ( material.map.isVideoTexture === true ) && ( material.map.colorSpace === SRGBColorSpace ), + premultipliedAlpha: material.premultipliedAlpha, doubleSided: material.side === DoubleSide, @@ -536,6 +538,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities _programLayers.enable( 17 ); if ( parameters.pointsUvs ) _programLayers.enable( 18 ); + if ( parameters.decodeVideoTexture ) + _programLayers.enable( 19 ); array.push( _programLayers.mask ); diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index c4c411d0336fa8..c54cd5a9ce93a1 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -748,7 +748,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, glFormat = utils.convert( texture.format, texture.colorSpace ); let glType = utils.convert( texture.type ), - glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace ); + glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture ); setTextureParameters( textureType, texture, supportsMips ); @@ -2027,7 +2027,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, const format = texture.format; const type = texture.type; - if ( texture.isCompressedTexture === true || texture.format === _SRGBAFormat ) return image; + if ( texture.isCompressedTexture === true || texture.isVideoTexture === true || texture.format === _SRGBAFormat ) return image; if ( colorSpace !== LinearSRGBColorSpace && colorSpace !== NoColorSpace ) {