Skip to content

Commit

Permalink
add sRGBToLinearWithShader to Texture
Browse files Browse the repository at this point in the history
  • Loading branch information
nianxy committed Nov 24, 2023
1 parent b0715d5 commit 26680da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ function WebGLRenderer( parameters = {} ) {

}

if ( capabilities.isWebGL2 && Texture.useSrgbTextures && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.encoding === sRGBEncoding ) {
if ( capabilities.isWebGL2 && map && map.isTexture && map.format === RGBAFormat && map.type === UnsignedByteType && map.encoding === sRGBEncoding && !map.sRGBToLinearWithShader ) {

Check failure on line 602 in src/renderers/WebGLRenderer.js

View workflow job for this annotation

GitHub Actions / Linting

Unary operator '!' must be followed by whitespace

encoding = LinearEncoding; // disable inline decode for sRGB textures in WebGL 2

Expand Down
14 changes: 7 additions & 7 deletions src/renderers/webgl/WebGLTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

}

function getInternalFormat( internalFormatName, glFormat, glType, encoding ) {
function getInternalFormat( internalFormatName, glFormat, glType, encoding, sRGBToLinearWithShader = !Texture.useSrgbTextures ) {

Check failure on line 134 in src/renderers/webgl/WebGLTextures.js

View workflow job for this annotation

GitHub Actions / Linting

Unary operator '!' must be followed by whitespace

if ( isWebGL2 === false ) return glFormat;

Expand Down Expand Up @@ -165,7 +165,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

if ( glType === _gl.FLOAT ) internalFormat = _gl.RGBA32F;
if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGBA16F;
if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = ( isWebGL2 && Texture.useSrgbTextures && encoding === sRGBEncoding ) ? _gl.SRGB8_ALPHA8 : _gl.RGBA8;
if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = ( isWebGL2 && encoding === sRGBEncoding && !sRGBToLinearWithShader ) ? _gl.SRGB8_ALPHA8 : _gl.RGBA8;

Check failure on line 168 in src/renderers/webgl/WebGLTextures.js

View workflow job for this annotation

GitHub Actions / Linting

Unary operator '!' must be followed by whitespace
if ( glType === _gl.UNSIGNED_SHORT_4_4_4_4 ) internalFormat = _gl.RGBA4;
if ( glType === _gl.UNSIGNED_SHORT_5_5_5_1 ) internalFormat = _gl.RGB5_A1;

Expand Down Expand Up @@ -562,7 +562,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
glFormat = utils.convert( texture.format, texture.encoding );

let glType = utils.convert( texture.type ),
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.sRGBToLinearWithShader );

setTextureParameters( textureType, texture, supportsMips );

Expand Down Expand Up @@ -921,7 +921,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
supportsMips = isPowerOfTwo( image ) || isWebGL2,
glFormat = utils.convert( texture.format, texture.encoding ),
glType = utils.convert( texture.type ),
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.sRGBToLinearWithShader );

const useTexStorage = ( isWebGL2 && texture.isVideoTexture !== true );
const allocateMemory = ( textureProperties.__version === undefined );
Expand Down Expand Up @@ -1086,7 +1086,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

const glFormat = utils.convert( texture.format, texture.encoding );
const glType = utils.convert( texture.type );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.sRGBToLinearWithShader );
const renderTargetProperties = properties.get( renderTarget );

if ( ! renderTargetProperties.__hasExternalTextures ) {
Expand Down Expand Up @@ -1194,7 +1194,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

const glFormat = utils.convert( texture.format, texture.encoding );
const glType = utils.convert( texture.type );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.sRGBToLinearWithShader );
const samples = getRenderTargetSamples( renderTarget );

if ( isMultisample && renderTarget.useRenderbuffer ) {
Expand Down Expand Up @@ -1419,7 +1419,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

const glFormat = utils.convert( texture.format, texture.encoding );
const glType = utils.convert( texture.type );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding );
const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.encoding, texture.sRGBToLinearWithShader );
const samples = getRenderTargetSamples( renderTarget );
_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );

Expand Down
5 changes: 5 additions & 0 deletions src/textures/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Texture extends EventDispatcher {
this.isRenderTargetTexture = false; // indicates whether a texture belongs to a render target or not
this.needsPMREMUpdate = false; // indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target textures)

// In WebGL2, sRGB textures can be trancoded to Linear automatically, but reference to this issue: https://github.com/mrdoob/three.js/issues/26183
// there're performance issues when doing this, so you can set the sRGBToLinearWithShader to false to transcode the sRGB texture to Linear with
// shader, especially to those textures that are updated in each frame
this.sRGBToLinearWithShader = !Texture.useSrgbTextures;

Check failure on line 79 in src/textures/Texture.js

View workflow job for this annotation

GitHub Actions / Linting

Unary operator '!' must be followed by whitespace

}

updateMatrix() {
Expand Down

0 comments on commit 26680da

Please sign in to comment.