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

WebGPU: Fix PGs errors / warnings #13154

Merged
merged 14 commits into from
Oct 24, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ ThinEngine.prototype.createRenderTargetTexture = function (this: ThinEngine, siz
fullOptions.generateDepthBuffer = !!options.generateDepthBuffer;
fullOptions.generateStencilBuffer = !!options.generateStencilBuffer;
fullOptions.noColorTarget = !!options.noColorTarget;
fullOptions.samples = options.samples;
} else {
fullOptions.generateDepthBuffer = true;
fullOptions.generateStencilBuffer = false;
Expand Down Expand Up @@ -97,6 +98,8 @@ ThinEngine.prototype.createRenderTargetTexture = function (this: ThinEngine, siz

rtWrapper.setTextures(texture);

this.updateRenderTargetTextureSampleCount(rtWrapper, fullOptions.samples ?? 1);

return rtWrapper;
};

Expand Down Expand Up @@ -133,7 +136,8 @@ ThinEngine.prototype._createDepthStencilTexture = function (size: TextureSize, o
size,
internalOptions.generateStencil,
internalOptions.comparisonFunction === 0 ? false : internalOptions.bilinearFiltering,
internalOptions.comparisonFunction
internalOptions.comparisonFunction,
internalOptions.samples
);

if (internalOptions.depthTextureFormat !== undefined) {
Expand Down Expand Up @@ -276,6 +280,7 @@ ThinEngine.prototype.updateRenderTargetTextureSampleCount = function (rtWrapper:
}

rtWrapper.texture.samples = samples;
rtWrapper._samples = samples;
rtWrapper._depthStencilBuffer = this._setupFramebufferDepthAttachments(
rtWrapper._generateStencilBuffer,
rtWrapper._generateDepthBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ WebGPUEngine.prototype.createRenderTargetTexture = function (size: TextureSize,
fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
fullOptions.creationFlags = options.creationFlags ?? 0;
fullOptions.noColorTarget = !!options.noColorTarget;
fullOptions.samples = options.samples;
} else {
fullOptions.generateMipMaps = <boolean>options;
fullOptions.generateDepthBuffer = true;
Expand All @@ -35,6 +36,7 @@ WebGPUEngine.prototype.createRenderTargetTexture = function (size: TextureSize,

const texture = fullOptions.noColorTarget ? null : this._createInternalTexture(size, options, true, InternalTextureSource.RenderTarget);

rtWrapper._samples = fullOptions.samples ?? 1;
rtWrapper._generateDepthBuffer = fullOptions.generateDepthBuffer;
rtWrapper._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;

Expand Down Expand Up @@ -144,6 +146,7 @@ WebGPUEngine.prototype.updateRenderTargetTextureSampleCount = function (rtWrappe
rtWrapper._depthStencilTexture.samples = samples;
}

rtWrapper._samples = samples;
rtWrapper.texture.samples = samples;

return samples;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ export abstract class WebGPUCacheRenderPipeline {
const depthStencilFormatHasStencil = this._webgpuDepthStencilFormat ? WebGPUTextureHelper.HasStencilAspect(this._webgpuDepthStencilFormat) : false;

return this._device.createRenderPipeline({
label: `RenderPipeline_${colorStates[0]?.format ?? "nooutput"}_${this._webgpuDepthStencilFormat ?? "nodepth"}_samples${sampleCount}`,
layout: pipelineLayout,
vertex: {
module: webgpuPipelineContext.stages!.vertexStage.module,
Expand Down
10 changes: 3 additions & 7 deletions packages/dev/core/src/Engines/WebGPU/webgpuShaderProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,9 @@ export abstract class WebGPUShaderProcessor implements IShaderProcessor {
// eslint-disable-next-line no-empty
while (idx++ < code.length && code.charAt(idx) != "{") {}
if (idx < code.length) {
// eslint-disable-next-line no-empty
while (idx++ < code.length && code.charAt(idx) != "\n") {}
if (idx < code.length) {
const part1 = code.substring(0, idx + 1);
const part2 = code.substring(idx + 1);
code = part1 + startingCode + part2;
}
const part1 = code.substring(0, idx + 1);
const part2 = code.substring(idx + 1);
code = part1 + startingCode + part2;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dev/core/src/Engines/WebGPU/webgpuTextureHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ export class WebGPUTextureHelper {
}

const gpuTexture = this._device.createTexture({
label: `Texture_${textureSize.width}x${textureSize.height}x${textureSize.depthOrArrayLayers}_${hasMipmaps ? "wmips" : "womips"}_${format}_samples${sampleCount}`,
size: textureSize,
dimension: is3D ? WebGPUConstants.TextureDimension.E3d : WebGPUConstants.TextureDimension.E2d,
format,
Expand Down Expand Up @@ -1296,6 +1297,7 @@ export class WebGPUTextureHelper {
}

const gpuTexture = this._device.createTexture({
label: `TextureCube_${width}x${height}x6_${hasMipmaps ? "wmips" : "womips"}_${format}_samples${sampleCount}`,
size: {
width,
height,
Expand Down
9 changes: 8 additions & 1 deletion packages/dev/core/src/Engines/WebGPU/webgpuTintWASM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class WebGPUTintWASM {
wasmPath: "https://preview.babylonjs.com/twgsl/twgsl.wasm",
};

public static ShowWGSLShaderCode = false;

private _twgsl: any = null;

public async initTwgsl(twgslOptions?: TwgslOptions): Promise<void> {
Expand Down Expand Up @@ -60,6 +62,11 @@ export class WebGPUTintWASM {
}

public convertSpirV2WGSL(code: Uint32Array): string {
return this._twgsl.convertSpirV2WGSL(code);
const ccode = this._twgsl.convertSpirV2WGSL(code);
if (WebGPUTintWASM.ShowWGSLShaderCode) {
console.log(ccode);
console.log("***********************************************");
}
return ccode;
}
}
4 changes: 3 additions & 1 deletion packages/dev/core/src/Engines/thinEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3743,12 +3743,14 @@ export class ThinEngine {
fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
fullOptions.format = options.format === undefined ? Constants.TEXTUREFORMAT_RGBA : options.format;
fullOptions.useSRGBBuffer = options.useSRGBBuffer === undefined ? false : options.useSRGBBuffer;
fullOptions.samples = options.samples ?? 1;
} else {
fullOptions.generateMipMaps = <boolean>options;
fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
fullOptions.format = Constants.TEXTUREFORMAT_RGBA;
fullOptions.useSRGBBuffer = false;
fullOptions.samples = 1;
}

fullOptions.useSRGBBuffer = fullOptions.useSRGBBuffer && this._caps.supportSRGBBuffers && (this.webGLVersion > 1 || this.isWebGPU);
Expand Down Expand Up @@ -3805,7 +3807,7 @@ export class ThinEngine {
texture.height = height;
texture.depth = layers;
texture.isReady = true;
texture.samples = 1;
texture.samples = fullOptions.samples;
texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
texture.samplingMode = fullOptions.samplingMode;
texture.type = fullOptions.type;
Expand Down
28 changes: 14 additions & 14 deletions packages/dev/core/src/Engines/webgpuEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ export class WebGPUEngine extends Engine {
public _device: GPUDevice;
private _deviceEnabledExtensions: GPUFeatureName[];
private _context: GPUCanvasContext;
private _swapChainTexture: GPUTexture;
private _mainPassSampleCount: number;
/** @internal */
public _textureHelper: WebGPUTextureHelper;
Expand Down Expand Up @@ -277,7 +276,6 @@ export class WebGPUEngine extends Engine {
// This happens mainly during clear for the state
// And when the frame starts to swap the target texture from the swap chain
private _mainTexture: GPUTexture;
private _mainTextureLastCopy: GPUTexture;
private _depthTexture: GPUTexture;
private _mainTextureExtends: GPUExtent3D;
private _depthTextureFormat: GPUTextureFormat | undefined;
Expand Down Expand Up @@ -858,6 +856,8 @@ export class WebGPUEngine extends Engine {
return;
}

this.flushFramebuffer(false);

this._mainTextureExtends = {
width: this.getRenderWidth(),
height: this.getRenderHeight(),
Expand All @@ -873,6 +873,7 @@ export class WebGPUEngine extends Engine {

if (this._options.antialiasing) {
const mainTextureDescriptor: GPUTextureDescriptor = {
label: "Texture_MainColor_antialiasing",
size: this._mainTextureExtends,
mipLevelCount: 1,
sampleCount: this._mainPassSampleCount,
Expand All @@ -881,7 +882,9 @@ export class WebGPUEngine extends Engine {
usage: WebGPUConstants.TextureUsage.RenderAttachment,
};

this._mainTexture?.destroy();
if (this._mainTexture) {
this._textureHelper.releaseTexture(this._mainTexture);
}
this._mainTexture = this._device.createTexture(mainTextureDescriptor);
mainColorAttachments = [
{
Expand All @@ -907,6 +910,7 @@ export class WebGPUEngine extends Engine {
this._setDepthTextureFormat(this._mainRenderPassWrapper);

const depthTextureDescriptor: GPUTextureDescriptor = {
label: "Texture_MainDepthStencil",
size: this._mainTextureExtends,
mipLevelCount: 1,
sampleCount: this._mainPassSampleCount,
Expand All @@ -916,7 +920,7 @@ export class WebGPUEngine extends Engine {
};

if (this._depthTexture) {
this._depthTexture.destroy();
this._textureHelper.releaseTexture(this._depthTexture);
}
this._depthTexture = this._device.createTexture(depthTextureDescriptor);
const mainDepthAttachment: GPURenderPassDepthStencilAttachment = {
Expand All @@ -934,10 +938,6 @@ export class WebGPUEngine extends Engine {
colorAttachments: mainColorAttachments,
depthStencilAttachment: mainDepthAttachment,
};

if (this._mainRenderPassWrapper.renderPass !== null) {
this._endMainRenderPass();
}
}

private _configureContext(): void {
Expand Down Expand Up @@ -1724,6 +1724,7 @@ export class WebGPUEngine extends Engine {
console.log(defines);
console.log(vertexSourceCode);
console.log(fragmentSourceCode);
console.log("***********************************************");
}

webGpuContext.sources = {
Expand Down Expand Up @@ -2839,7 +2840,7 @@ export class WebGPUEngine extends Engine {

private _startMainRenderPass(setClearStates: boolean, clearColor?: Nullable<IColor4Like>, clearDepth?: boolean, clearStencil?: boolean): void {
if (this._mainRenderPassWrapper.renderPass) {
this._endMainRenderPass();
this.flushFramebuffer(false);
}

if (this.useReverseDepthBuffer) {
Expand All @@ -2866,14 +2867,14 @@ export class WebGPUEngine extends Engine {
: WebGPUConstants.LoadOp.Load;
this._mainRenderPassWrapper.renderPassDescriptor!.occlusionQuerySet = this._occlusionQuery?.hasQueries ? this._occlusionQuery.querySet : undefined;

this._swapChainTexture = this._context.getCurrentTexture();
this._mainRenderPassWrapper.colorAttachmentGPUTextures[0]!.set(this._swapChainTexture);
const swapChainTexture = this._context.getCurrentTexture();
this._mainRenderPassWrapper.colorAttachmentGPUTextures[0]!.set(swapChainTexture);

// Resolve in case of MSAA
if (this._options.antialiasing) {
this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments[0]!.resolveTarget = this._swapChainTexture.createView();
this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments[0]!.resolveTarget = swapChainTexture.createView();
} else {
this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments[0]!.view = this._swapChainTexture.createView();
this._mainRenderPassWrapper.renderPassDescriptor!.colorAttachments[0]!.view = swapChainTexture.createView();
}

if (this.dbgVerboseLogsForFirstFrames) {
Expand Down Expand Up @@ -3370,7 +3371,6 @@ export class WebGPUEngine extends Engine {
*/
public dispose(): void {
this._mainTexture?.destroy();
this._mainTextureLastCopy?.destroy();
this._depthTexture?.destroy();
super.dispose();
}
Expand Down
12 changes: 6 additions & 6 deletions packages/dev/core/src/Misc/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,15 @@ export class Observable<T> {
}

if (obs.mask & mask) {
if (obs.unregisterOnNextCall) {
this._deferUnregister(obs);
}

if (obs.scope) {
state.lastReturnValue = obs.callback.apply(obs.scope, [eventData, state]);
} else {
state.lastReturnValue = obs.callback(eventData, state);
}

if (obs.unregisterOnNextCall) {
this._deferUnregister(obs);
}
}
if (state.skipNextObservers) {
return false;
Expand All @@ -350,11 +350,11 @@ export class Observable<T> {
state.mask = mask;
state.skipNextObservers = false;

observer.callback(eventData, state);

if (observer.unregisterOnNextCall) {
this._deferUnregister(observer);
}

observer.callback(eventData, state);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/dev/core/src/Misc/screenshotTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ export function CreateScreenshotUsingRenderTarget(

const scene = camera.getScene();

scene.render(); // make sure the scene is ready to be rendered in the RTT with the right list of active meshes (which depends on the camera, that may have been changed above)

// At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
const texture = new RenderTargetTexture(
"screenShot",
Expand Down
8 changes: 4 additions & 4 deletions packages/dev/core/src/PostProcesses/postProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ export class PostProcess {
this._samples = Math.min(n, this._engine.getCaps().maxMSAASamples);

this._textures.forEach((texture) => {
if (texture.samples !== this._samples) {
this._engine.updateRenderTargetTextureSampleCount(texture, this._samples);
}
texture.setSamples(this._samples);
});
}

Expand Down Expand Up @@ -533,7 +531,8 @@ export class PostProcess {
this._textureCache[i].texture.width === textureSize.width &&
this._textureCache[i].texture.height === textureSize.height &&
this._textureCache[i].postProcessChannel === channel &&
this._textureCache[i].texture._generateDepthBuffer === textureOptions.generateDepthBuffer
this._textureCache[i].texture._generateDepthBuffer === textureOptions.generateDepthBuffer &&
this._textureCache[i].texture.samples === textureOptions.samples
) {
return this._textureCache[i].texture;
}
Expand Down Expand Up @@ -590,6 +589,7 @@ export class PostProcess {
samplingMode: this.renderTargetSamplingMode,
type: this._textureType,
format: this._textureFormat,
samples: this._samples,
};

this._textures.push(this._createRenderTargetTexture(textureSize, textureOptions, 0));
Expand Down
Loading