Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sRGB option to MultiRenderTarget #12516

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/dev/core/src/Engines/Extensions/engine.multiRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o

const defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
const defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
const defaultUseSRGBBuffer = false;

let types = new Array<number>();
let samplingModes = new Array<number>();
let useSRGBBuffers = new Array<boolean>();

const rtWrapper = this._createHardwareRenderTargetWrapper(true, false, size) as WebGLRenderTargetWrapper;

Expand All @@ -187,6 +189,9 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
if (options.samplingModes) {
samplingModes = options.samplingModes;
}
if (options.useSRGBBuffers) {
useSRGBBuffers = options.useSRGBBuffers;
}
if (
this.webGLVersion > 1 &&
(options.depthTextureFormat === Constants.TEXTUREFORMAT_DEPTH24_STENCIL8 ||
Expand Down Expand Up @@ -219,6 +224,7 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
for (let i = 0; i < textureCount; i++) {
let samplingMode = samplingModes[i] || defaultSamplingMode;
let type = types[i] || defaultType;
let useSRGBBuffer = useSRGBBuffers[i] || defaultUseSRGBBuffer;

if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloatLinearFiltering) {
// if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
Expand All @@ -234,6 +240,8 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
Logger.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
}

useSRGBBuffer = useSRGBBuffer && this._caps.supportSRGBBuffers && (this.webGLVersion > 1 || this.isWebGPU);

const texture = new InternalTexture(this, InternalTextureSource.MultiRenderTarget);
const attachment = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];

Expand All @@ -248,7 +256,8 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

gl.texImage2D(gl.TEXTURE_2D, 0, this._getRGBABufferInternalSizedFormat(type), width, height, 0, gl.RGBA, this._getWebGLTextureType(type), null);
const internalSizedFormat = this._getRGBABufferInternalSizedFormat(type, Constants.TEXTUREFORMAT_RGBA, useSRGBBuffer);
gl.texImage2D(gl.TEXTURE_2D, 0, internalSizedFormat, width, height, 0, gl.RGBA, this._getWebGLTextureType(type), null);

gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, attachment, gl.TEXTURE_2D, texture._hardwareTexture!.underlyingResource, 0);

Expand All @@ -268,6 +277,7 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
texture.generateMipMaps = generateMipMaps;
texture.samplingMode = samplingMode;
texture.type = type;
texture._useSRGBBuffer = useSRGBBuffer;

this._internalTexturesCache.push(texture);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,

const defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
const defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
const defaultUseSRGBBuffer = false;

let types = new Array<number>();
let samplingModes = new Array<number>();
let useSRGBBuffers = new Array<boolean>();

const rtWrapper = this._createHardwareRenderTargetWrapper(true, false, size) as WebGPURenderTargetWrapper;

Expand All @@ -71,6 +73,9 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,
if (options.samplingModes) {
samplingModes = options.samplingModes;
}
if (options.useSRGBBuffers) {
useSRGBBuffers = options.useSRGBBuffers;
}
}

const width = (<{ width: number; height: number }>size).width || <number>size;
Expand All @@ -93,6 +98,7 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,
for (let i = 0; i < textureCount; i++) {
let samplingMode = samplingModes[i] || defaultSamplingMode;
let type = types[i] || defaultType;
const useSRGBBuffer = useSRGBBuffers[i] || defaultUseSRGBBuffer;

if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloatLinearFiltering) {
// if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
Expand Down Expand Up @@ -124,6 +130,7 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,
texture.type = type;
texture._cachedWrapU = Constants.TEXTURE_CLAMP_ADDRESSMODE;
texture._cachedWrapV = Constants.TEXTURE_CLAMP_ADDRESSMODE;
texture._useSRGBBuffer = useSRGBBuffer;

this._internalTexturesCache.push(texture);

Expand Down
23 changes: 20 additions & 3 deletions packages/dev/core/src/Materials/Textures/multiRenderTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export interface IMultiRenderTargetOptions {
* Define the sampling modes of all the draw buffers we want to create
*/
samplingModes?: number[];
/**
* Define if sRGB format should be used for each of the draw buffers we want to create
*/
useSRGBBuffers?: boolean[];
/**
* Define if a depth buffer is required
*/
Expand Down Expand Up @@ -149,7 +153,8 @@ export class MultiRenderTarget extends RenderTargetTexture {

const types: number[] = [];
const samplingModes: number[] = [];
this._initTypes(count, types, samplingModes, options);
const useSRGBBuffers: boolean[] = [];
this._initTypes(count, types, samplingModes, useSRGBBuffers, options);

const generateDepthBuffer = !options || options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
const generateStencilBuffer = !options || options.generateStencilBuffer === undefined ? false : options.generateStencilBuffer;
Expand All @@ -164,6 +169,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
depthTextureFormat: depthTextureFormat,
types: types,
textureCount: count,
useSRGBBuffers: useSRGBBuffers,
};

this._count = count;
Expand All @@ -175,7 +181,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
}
}

private _initTypes(count: number, types: number[], samplingModes: number[], options?: IMultiRenderTargetOptions) {
private _initTypes(count: number, types: number[], samplingModes: number[], useSRGBBuffers: boolean[], options?: IMultiRenderTargetOptions) {
for (let i = 0; i < count; i++) {
if (options && options.types && options.types[i] !== undefined) {
types.push(options.types[i]);
Expand All @@ -188,6 +194,12 @@ export class MultiRenderTarget extends RenderTargetTexture {
} else {
samplingModes.push(Texture.BILINEAR_SAMPLINGMODE);
}

if (options && options.useSRGBBuffers && options.useSRGBBuffers[i] !== undefined) {
useSRGBBuffers.push(options.useSRGBBuffers[i]);
} else {
useSRGBBuffers.push(false);
}
}
}

Expand Down Expand Up @@ -277,6 +289,9 @@ export class MultiRenderTarget extends RenderTargetTexture {
if (this._multiRenderTargetOptions.samplingModes) {
this._multiRenderTargetOptions.samplingModes[index] = texture.samplingMode;
}
if (this._multiRenderTargetOptions.useSRGBBuffers) {
this._multiRenderTargetOptions.useSRGBBuffers[index] = texture._useSRGBBuffer;
}
}

/**
Expand Down Expand Up @@ -318,10 +333,12 @@ export class MultiRenderTarget extends RenderTargetTexture {

const types: number[] = [];
const samplingModes: number[] = [];
const useSRGBBuffers: boolean[] = [];

this._initTypes(count, types, samplingModes, options);
this._initTypes(count, types, samplingModes, useSRGBBuffers, options);
this._multiRenderTargetOptions.types = types;
this._multiRenderTargetOptions.samplingModes = samplingModes;
this._multiRenderTargetOptions.useSRGBBuffers = useSRGBBuffers;
this._rebuild(true, textureNames);
}

Expand Down