Skip to content

Commit

Permalink
Merge pull request #13678 from newbeea/set-multirendertarget-format
Browse files Browse the repository at this point in the history
feat: support formats option for multirendertarget
  • Loading branch information
sebavan committed Apr 5, 2023
2 parents 884108c + e3ac0b7 commit 45503bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
16 changes: 12 additions & 4 deletions packages/dev/core/src/Engines/Extensions/engine.multiRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
const defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
const defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
const defaultUseSRGBBuffer = false;
const defaultFormat = Constants.TEXTUREFORMAT_RGBA;
const defaultTarget = Constants.TEXTURE_2D;

let types = new Array<number>();
let samplingModes = new Array<number>();
let useSRGBBuffers = new Array<boolean>();
let formats = new Array<number>();
let targets = new Array<number>();
let faceIndex = new Array<number>();
let layerIndex = new Array<number>();
Expand All @@ -197,6 +199,9 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
if (options.useSRGBBuffers) {
useSRGBBuffers = options.useSRGBBuffers;
}
if (options.formats) {
formats = options.formats;
}
if (options.targetTypes) {
targets = options.targetTypes;
}
Expand Down Expand Up @@ -249,6 +254,7 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
let samplingMode = samplingModes[i] || defaultSamplingMode;
let type = types[i] || defaultType;
let useSRGBBuffer = useSRGBBuffers[i] || defaultUseSRGBBuffer;
const format = formats[i] || defaultFormat;

const target = targets[i] || defaultTarget;
const layerCount = layers[i] ?? 1;
Expand Down Expand Up @@ -289,7 +295,8 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

const internalSizedFormat = this._getRGBABufferInternalSizedFormat(type, Constants.TEXTUREFORMAT_RGBA, useSRGBBuffer);
const internalSizedFormat = this._getRGBABufferInternalSizedFormat(type, format, useSRGBBuffer);
const internalFormat = this._getInternalFormat(format);
const webGLTextureType = this._getWebGLTextureType(type);

if (isWebGL2 && (target === Constants.TEXTURE_2D_ARRAY || target === Constants.TEXTURE_3D)) {
Expand All @@ -301,15 +308,15 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o

texture.baseDepth = texture.depth = layerCount;

gl.texImage3D(target, 0, internalSizedFormat, width, height, layerCount, 0, gl.RGBA, webGLTextureType, null);
gl.texImage3D(target, 0, internalSizedFormat, width, height, layerCount, 0, internalFormat, webGLTextureType, null);
} else if (target === Constants.TEXTURE_CUBE_MAP) {
// We have to generate all faces to complete the framebuffer
for (let i = 0; i < 6; i++) {
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalSizedFormat, width, height, 0, gl.RGBA, webGLTextureType, null);
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalSizedFormat, width, height, 0, internalFormat, webGLTextureType, null);
}
texture.isCube = true;
} else {
gl.texImage2D(gl.TEXTURE_2D, 0, internalSizedFormat, width, height, 0, gl.RGBA, webGLTextureType, null);
gl.texImage2D(gl.TEXTURE_2D, 0, internalSizedFormat, width, height, 0, internalFormat, webGLTextureType, null);
}

if (generateMipMaps) {
Expand All @@ -329,6 +336,7 @@ ThinEngine.prototype.createMultipleRenderTarget = function (size: TextureSize, o
texture.samplingMode = samplingMode;
texture.type = type;
texture._useSRGBBuffer = useSRGBBuffer;
texture.format = format;

this._internalTexturesCache.push(texture);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,
const defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
const defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
const defaultUseSRGBBuffer = false;
const defaultFormat = Constants.TEXTUREFORMAT_RGBA;

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

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

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

const width = (<{ width: number; height: number }>size).width || <number>size;
Expand Down Expand Up @@ -110,6 +115,7 @@ WebGPUEngine.prototype.createMultipleRenderTarget = function (size: TextureSize,
let samplingMode = samplingModes[i] || defaultSamplingMode;
let type = types[i] || defaultType;
const useSRGBBuffer = useSRGBBuffers[i] || defaultUseSRGBBuffer;
const format = formats[i] || defaultFormat;

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

this._internalTexturesCache.push(texture);

Expand Down
3 changes: 3 additions & 0 deletions packages/dev/core/src/Engines/renderTargetWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class RenderTargetWrapper {

const samplingModes: number[] = [];
const types: number[] = [];
const formats: number[] = [];
const targetTypes: number[] = [];
const faceIndex: number[] = [];
const layerIndex: number[] = [];
Expand All @@ -330,6 +331,7 @@ export class RenderTargetWrapper {

samplingModes.push(texture.samplingMode);
types.push(texture.type);
formats.push(texture.format);

const index = internalTexture2Index[texture.uniqueId];
if (index !== undefined) {
Expand Down Expand Up @@ -370,6 +372,7 @@ export class RenderTargetWrapper {
generateStencilBuffer: this._generateStencilBuffer,
generateDepthTexture,
types,
formats,
textureCount,
targetTypes,
faceIndex,
Expand Down
19 changes: 17 additions & 2 deletions packages/dev/core/src/Materials/Textures/multiRenderTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface IMultiRenderTargetOptions {
* Define if a depth texture is required instead of a depth buffer
*/
generateDepthTexture?: boolean;
/**
* Define the internal format of the buffer in the RTT (RED, RG, RGB, RGBA (default), ALPHA...) of all the draw buffers we want to create
*/
formats?: number[];
/**
* Define depth texture format to use
*/
Expand Down Expand Up @@ -178,11 +182,12 @@ export class MultiRenderTarget extends RenderTargetTexture {
const types: number[] = [];
const samplingModes: number[] = [];
const useSRGBBuffers: boolean[] = [];
const formats: number[] = [];
const targetTypes: number[] = [];
const faceIndex: number[] = [];
const layerIndex: number[] = [];
const layerCounts: number[] = [];
this._initTypes(count, types, samplingModes, useSRGBBuffers, targetTypes, faceIndex, layerIndex, layerCounts, options);
this._initTypes(count, types, samplingModes, useSRGBBuffers, formats, targetTypes, faceIndex, layerIndex, layerCounts, options);

const generateDepthBuffer = !options || options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
const generateStencilBuffer = !options || options.generateStencilBuffer === undefined ? false : options.generateStencilBuffer;
Expand All @@ -198,6 +203,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
types: types,
textureCount: count,
useSRGBBuffers: useSRGBBuffers,
formats: formats,
targetTypes: targetTypes,
faceIndex: faceIndex,
layerIndex: layerIndex,
Expand All @@ -218,6 +224,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
types: number[],
samplingModes: number[],
useSRGBBuffers: boolean[],
formats: number[],
targets: number[],
faceIndex: number[],
layerIndex: number[],
Expand All @@ -243,6 +250,12 @@ export class MultiRenderTarget extends RenderTargetTexture {
useSRGBBuffers.push(false);
}

if (options && options.formats && options.formats[i] !== undefined) {
formats.push(options.formats[i]);
} else {
formats.push(Constants.TEXTUREFORMAT_RGBA);
}

if (options && options.targetTypes && options.targetTypes[i] !== undefined) {
targets.push(options.targetTypes[i]);
} else {
Expand Down Expand Up @@ -491,17 +504,19 @@ export class MultiRenderTarget extends RenderTargetTexture {
const types: number[] = [];
const samplingModes: number[] = [];
const useSRGBBuffers: boolean[] = [];
const formats: number[] = [];
const targetTypes: number[] = [];
const faceIndex: number[] = [];
const layerIndex: number[] = [];
const layerCounts: number[] = [];

this._textureNames = textureNames;

this._initTypes(count, types, samplingModes, useSRGBBuffers, targetTypes, faceIndex, layerIndex, layerCounts, options);
this._initTypes(count, types, samplingModes, useSRGBBuffers, formats, targetTypes, faceIndex, layerIndex, layerCounts, options);
this._multiRenderTargetOptions.types = types;
this._multiRenderTargetOptions.samplingModes = samplingModes;
this._multiRenderTargetOptions.useSRGBBuffers = useSRGBBuffers;
this._multiRenderTargetOptions.formats = formats;
this._multiRenderTargetOptions.targetTypes = targetTypes;
this._multiRenderTargetOptions.faceIndex = faceIndex;
this._multiRenderTargetOptions.layerIndex = layerIndex;
Expand Down

0 comments on commit 45503bf

Please sign in to comment.