Skip to content

Commit 68f5330

Browse files
mvaligurskyMartin Valigurskywilleastcott
authored
Render targets can use sRGB color buffer (playcanvas#6752)
* Support for compressed sRGB formats * lint * Update src/framework/parsers/glb-parser.js Co-authored-by: Will Eastcott <will@playcanvas.com> * feedback * clarified docs * updates * Render targets can use sRGB color buffer * cleanup * Update src/platform/graphics/render-target.js Co-authored-by: Will Eastcott <will@playcanvas.com> --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com> Co-authored-by: Will Eastcott <will@playcanvas.com>
1 parent 5eb8c08 commit 68f5330

File tree

16 files changed

+74
-20
lines changed

16 files changed

+74
-20
lines changed

examples/src/examples/graphics/reflection-planar.shader.frag

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ uniform vec4 uScreenSize;
44
// reflection texture
55
uniform sampler2D uDiffuseMap;
66

7+
vec3 gammaCorrectOutput(vec3 color) {
8+
return pow(color + 0.0000001, vec3(1.0 / 2.2));
9+
}
10+
711
void main(void)
812
{
913
// sample reflection texture
1014
vec2 coord = gl_FragCoord.xy * uScreenSize.zw;
1115
coord.y = 1.0 - coord.y;
1216
vec4 reflection = texture2D(uDiffuseMap, coord);
1317

14-
gl_FragColor = vec4(reflection.xyz * 0.7, 1);
18+
vec3 linearColor = reflection.xyz * 0.4;
19+
gl_FragColor.rgb = gammaCorrectOutput(linearColor);
20+
gl_FragColor.a = 1.0;
1521
}

examples/src/examples/graphics/render-to-texture.example.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ assetListLoader.load(() => {
142142
const texture = new pc.Texture(app.graphicsDevice, {
143143
width: 512,
144144
height: 256,
145-
format: pc.PIXELFORMAT_RGB8,
145+
format: pc.PIXELFORMAT_SRGBA8,
146146
mipmaps: true,
147147
minFilter: pc.FILTER_LINEAR,
148148
magFilter: pc.FILTER_LINEAR,

scripts/utils/cubemap-renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ CubemapRenderer.prototype.initialize = function () {
4949
name: this.entity.name + ':CubemapRenderer-' + resolution,
5050
width: resolution,
5151
height: resolution,
52-
format: pc.PIXELFORMAT_RGBA8,
52+
format: pc.PIXELFORMAT_SRGBA8,
5353
cubemap: true,
5454
mipmaps: this.mipmaps,
5555
minFilter: pc.FILTER_LINEAR_MIPMAP_LINEAR,

scripts/utils/planar-renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ PlanarRenderer.prototype.updateRenderTarget = function () {
102102
name: this.entity.name + ':PlanarRenderer-',
103103
width: width,
104104
height: height,
105-
format: pc.PIXELFORMAT_RGBA8,
105+
format: pc.PIXELFORMAT_SRGBA8,
106106
mipmaps: this.mipmaps,
107107
addressU: pc.ADDRESS_CLAMP_TO_EDGE,
108108
addressV: pc.ADDRESS_CLAMP_TO_EDGE,

src/framework/graphics/render-pass-picker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class RenderPassPicker extends RenderPass {
9595
renderer.setupViewUniformBuffers(this.viewBindGroups, renderer.viewUniformFormat, renderer.viewBindGroupFormat, 1);
9696
}
9797

98-
renderer.renderForward(camera.camera, tempMeshInstances, lights, SHADER_PICK, (meshInstance) => {
98+
renderer.renderForward(camera.camera, renderTarget, tempMeshInstances, lights, SHADER_PICK, (meshInstance) => {
9999
device.setBlendState(BlendState.NOBLEND);
100100
});
101101

src/framework/lightmapper/lightmapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ class Lightmapper {
11261126
this.renderer._forwardTime = 0;
11271127
this.renderer._shadowMapTime = 0;
11281128

1129-
this.renderer.renderForward(this.camera, rcv, lightArray, SHADER_FORWARD);
1129+
this.renderer.renderForward(this.camera, tempRT, rcv, lightArray, SHADER_FORWARD);
11301130

11311131
device.updateEnd();
11321132
}

src/platform/graphics/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,10 @@ export const isCompressedPixelFormat = (format) => {
11161116
return pixelFormatInfo.get(format)?.blockSize !== undefined;
11171117
};
11181118

1119+
export const isSrgbPixelFormat = (format) => {
1120+
return pixelFormatInfo.get(format)?.srgb === true;
1121+
};
1122+
11191123
export const isIntegerPixelFormat = (format) => {
11201124
return pixelFormatInfo.get(format)?.isInt === true;
11211125
};

src/platform/graphics/render-target.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Debug } from '../../core/debug.js';
22
import { TRACEID_RENDER_TARGET_ALLOC } from '../../core/constants.js';
3-
import { PIXELFORMAT_DEPTH, PIXELFORMAT_DEPTHSTENCIL } from './constants.js';
3+
import { PIXELFORMAT_DEPTH, PIXELFORMAT_DEPTHSTENCIL, isSrgbPixelFormat } from './constants.js';
44
import { DebugGraphics } from './debug-graphics.js';
55
import { GraphicsDevice } from './graphics-device.js';
66

@@ -482,6 +482,16 @@ class RenderTarget {
482482
get height() {
483483
return this._colorBuffer?.height || this._depthBuffer?.height || this._device.height;
484484
}
485+
486+
/**
487+
* Gets whether the first color buffer format is sRGB.
488+
*
489+
* @type {boolean}
490+
* @ignore
491+
*/
492+
get srgb() {
493+
return this.colorBuffer ? isSrgbPixelFormat(this.colorBuffer.format) : false;
494+
}
485495
}
486496

487497
export { RenderTarget };

src/scene/gsplat/gsplat-compressed-material.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ const createGSplatCompressedMaterial = (options = {}) => {
369369

370370
const programOptions = {
371371
pass: pass,
372-
gamma: renderParams.gammaCorrection,
372+
gamma: renderParams.shaderOutputGamma,
373373
toneMapping: renderParams.toneMapping,
374374
vertex: options.vertex ?? splatMainVS,
375375
fragment: options.fragment ?? splatMainFS,

src/scene/gsplat/gsplat-material.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const createGSplatMaterial = (options = {}) => {
7171

7272
const programOptions = {
7373
pass: pass,
74-
gamma: renderParams.gammaCorrection,
74+
gamma: renderParams.shaderOutputGamma,
7575
toneMapping: renderParams.toneMapping,
7676
vertex: options.vertex ?? splatMainVS,
7777
fragment: options.fragment ?? splatMainFS,

0 commit comments

Comments
 (0)