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 crashes in Firefox and Safari #16045

Merged
merged 2 commits into from
Jan 8, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { WebGPUPipelineContext } from "./webgpuPipelineContext";
import { WebGPUTextureHelper } from "./webgpuTextureHelper";
import { renderableTextureFormatToIndex } from "./webgpuTextureManager";
import { checkNonFloatVertexBuffers } from "core/Buffers/buffer.nonFloatVertexBuffers";
import { Logger } from "core/Misc/logger";

enum StatePosition {
StencilReadMask = 0,
Expand Down Expand Up @@ -63,6 +64,8 @@ const stencilOpToIndex: { [name: number]: number } = {

/** @internal */
export abstract class WebGPUCacheRenderPipeline {
public static LogErrorIfNoVertexBuffer = false;

public static NumCacheHitWithoutHash = 0;
public static NumCacheHitWithHash = 0;
public static NumCacheMiss = 0;
Expand Down Expand Up @@ -791,6 +794,11 @@ export abstract class WebGPUCacheRenderPipeline {
// In WebGL it's valid to not bind a vertex buffer to an attribute, but it's not valid in WebGPU
// So we must bind a dummy buffer when we are not given one for a specific attribute
vertexBuffer = this._emptyVertexBuffer;
if (WebGPUCacheRenderPipeline.LogErrorIfNoVertexBuffer) {
Logger.Error(
`No vertex buffer is provided for the "${attributes[index]}" attribute. A default empty vertex buffer will be used, but this may generate errors in some browsers.`
);
}
}

const buffer = vertexBuffer.effectiveBuffer?.underlyingResource;
Expand Down
7 changes: 6 additions & 1 deletion packages/dev/core/src/Engines/WebGPU/webgpuTextureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,18 @@ export class WebGPUTextureManager {
(processorOptions.processor as WebGPUShaderProcessorWGSL).pureMode = false;

const vertexModule = this._device.createShaderModule({
label: `BabylonWebGPUDevice${this._engine.uniqueId}_InternalVertexShader_${index}`,
code: final.vertexCode,
});
const fragmentModule = this._device.createShaderModule({
label: `BabylonWebGPUDevice${this._engine.uniqueId}_InternalFragmentShader_${index}`,
code: final.fragmentCode,
});
modules = this._compiledShaders[index] = [vertexModule, fragmentModule];
}

const pipeline = this._device.createRenderPipeline({
label: `BabylonWebGPUDevice${this._engine.uniqueId}_InternalPipeline_${format}_${index}`,
layout: WebGPUConstants.AutoLayoutMode.Auto,
vertex: {
module: modules[0],
Expand Down Expand Up @@ -468,15 +471,17 @@ export class WebGPUTextureManager {
if (!modules) {
const vertexModule = this._device.createShaderModule({
code: copyVideoToTextureVertexSource,
label: `BabylonWebGPUDevice${this._engine.uniqueId}_CopyVideoToTexture_VertexShader`,
});
const fragmentModule = this._device.createShaderModule({
code: index === 0 ? copyVideoToTextureFragmentSource : copyVideoToTextureInvertYFragmentSource,
label: `BabylonWebGPUDevice${this._engine.uniqueId}_CopyVideoToTexture_FragmentShader_${index === 0 ? "DontInvertY" : "InvertY"}`,
});
modules = this._videoCompiledShaders[index] = [vertexModule, fragmentModule];
}

const pipeline = this._device.createRenderPipeline({
label: `BabylonWebGPUDevice${this._engine.uniqueId}_CopyVideoToTexture_${format}_${index === 0 ? "DontInvertY" : "InvertY"}`,
label: `BabylonWebGPUDevice${this._engine.uniqueId}_InternalVideoPipeline_${format}_${index === 0 ? "DontInvertY" : "InvertY"}`,
layout: WebGPUConstants.AutoLayoutMode.Auto,
vertex: {
module: modules[0],
Expand Down
35 changes: 33 additions & 2 deletions packages/dev/core/src/Materials/Node/Blocks/Input/inputBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ const attributeAsUniform: { [name: string]: boolean } = {
particle_texturemask: true,
};

const attributeDefine: { [name: string]: string } = {
normal: "NORMAL",
tangent: "TANGENT",
uv: "UV1",
uv2: "UV2",
uv3: "UV3",
uv4: "UV4",
uv5: "UV5",
uv6: "UV6",
uv7: "UV7",
uv8: "UV8",
};

/**
* Block used to expose an input value
*/
Expand Down Expand Up @@ -602,12 +615,30 @@ export class InputBlock extends NodeMaterialBlock {
}
if (state.shaderLanguage === ShaderLanguage.WGSL) {
if (!alreadyDeclared) {
state._attributeDeclaration += `attribute ${this.declarationVariableName}: ${state._getShaderType(this.type)};\n`;
const defineName = attributeDefine[this.name];
if (defineName) {
state._attributeDeclaration += `#ifdef ${defineName}\n`;
state._attributeDeclaration += `attribute ${this.declarationVariableName}: ${state._getShaderType(this.type)};\n`;
state._attributeDeclaration += `#else\n`;
state._attributeDeclaration += `let ${this.declarationVariableName}: ${state._getShaderType(this.type)} = ${state._getShaderType(this.type)}(0.);\n`;
state._attributeDeclaration += `#endif\n`;
} else {
state._attributeDeclaration += `attribute ${this.declarationVariableName}: ${state._getShaderType(this.type)};\n`;
}
}
this._prefix = `vertexInputs.`;
} else {
if (!alreadyDeclared) {
state._attributeDeclaration += `attribute ${state._getShaderType(this.type)} ${this.declarationVariableName};\n`;
const defineName = attributeDefine[this.name];
if (defineName) {
state._attributeDeclaration += `#ifdef ${defineName}\n`;
state._attributeDeclaration += `attribute ${state._getShaderType(this.type)} ${this.declarationVariableName};\n`;
state._attributeDeclaration += `#else\n`;
state._attributeDeclaration += `${state._getShaderType(this.type)} ${this.declarationVariableName} = ${state._getShaderType(this.type)}(0.);\n`;
state._attributeDeclaration += `#endif\n`;
} else {
state._attributeDeclaration += `attribute ${state._getShaderType(this.type)} ${this.declarationVariableName};\n`;
}
}
}
if (define && !alreadyDeclared) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export abstract class GreasedLineBaseMesh extends Mesh {
this._updateColorPointers();
}
this._createVertexBuffers(this._options.ribbonOptions?.smoothShading);
this._createOffsetsBuffer(this._offsets || []);
!this.doNotSyncBoundingInfo && this.refreshBoundingInfo();

this.greasedLineMaterial?.updateLazy();
Expand Down