Skip to content

Commit 7280517

Browse files
mvaligurskyMartin Valigursky
andauthored
Outline renderer WGSL chunk (#8225)
Co-authored-by: Martin Valigursky <[email protected]>
1 parent 27bb84d commit 7280517

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/extras/renderers/outline-renderer.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
ADDRESS_CLAMP_TO_EDGE, BLENDEQUATION_ADD, BLENDMODE_ONE_MINUS_SRC_ALPHA, BLENDMODE_SRC_ALPHA,
66
CULLFACE_NONE,
77
FILTER_LINEAR, FILTER_LINEAR_MIPMAP_LINEAR, PIXELFORMAT_SRGBA8,
8-
SEMANTIC_POSITION,
9-
SHADERLANGUAGE_GLSL
8+
SEMANTIC_POSITION
109
} from '../../platform/graphics/constants.js';
1110
import { DepthState } from '../../platform/graphics/depth-state.js';
1211
import { RenderTarget } from '../../platform/graphics/render-target.js';
@@ -15,7 +14,6 @@ import { drawQuadWithShader } from '../../scene/graphics/quad-render-utils.js';
1514
import { QuadRender } from '../../scene/graphics/quad-render.js';
1615
import { StandardMaterialOptions } from '../../scene/materials/standard-material-options.js';
1716
import { StandardMaterial } from '../../scene/materials/standard-material.js';
18-
import { ShaderChunks } from '../../scene/shader-lib/shader-chunks.js';
1917
import { ShaderUtils } from '../../scene/shader-lib/shader-utils.js';
2018

2119
/**
@@ -60,6 +58,46 @@ const shaderOutlineExtendPS = /* glsl */ `
6058
}
6159
`;
6260

61+
// WGSL version of the outline extend shader
62+
const shaderOutlineExtendWGSL = /* wgsl */ `
63+
64+
varying vUv0: vec2f;
65+
66+
uniform uOffset: vec2f;
67+
uniform uSrcMultiplier: f32;
68+
var source: texture_2d<f32>;
69+
var sourceSampler: sampler;
70+
71+
@fragment
72+
fn fragmentMain(input: FragmentInput) -> FragmentOutput {
73+
var output: FragmentOutput;
74+
75+
var pixel: vec4f;
76+
var texel = textureSample(source, sourceSampler, input.vUv0);
77+
let firstTexel = texel;
78+
var diff = texel.a * uniform.uSrcMultiplier;
79+
80+
pixel = textureSample(source, sourceSampler, input.vUv0 + uniform.uOffset * -2.0);
81+
texel = max(texel, pixel);
82+
diff = max(diff, length(firstTexel.rgb - pixel.rgb));
83+
84+
pixel = textureSample(source, sourceSampler, input.vUv0 + uniform.uOffset * -1.0);
85+
texel = max(texel, pixel);
86+
diff = max(diff, length(firstTexel.rgb - pixel.rgb));
87+
88+
pixel = textureSample(source, sourceSampler, input.vUv0 + uniform.uOffset * 1.0);
89+
texel = max(texel, pixel);
90+
diff = max(diff, length(firstTexel.rgb - pixel.rgb));
91+
92+
pixel = textureSample(source, sourceSampler, input.vUv0 + uniform.uOffset * 2.0);
93+
texel = max(texel, pixel);
94+
diff = max(diff, length(firstTexel.rgb - pixel.rgb));
95+
96+
output.color = vec4f(texel.rgb, min(diff, 1.0));
97+
return output;
98+
}
99+
`;
100+
63101
const _tempFloatArray = new Float32Array(2);
64102
const _tempColor = new Color();
65103

@@ -119,8 +157,9 @@ class OutlineRenderer {
119157
this.shaderExtend = ShaderUtils.createShader(device, {
120158
uniqueName: 'OutlineExtendShader',
121159
attributes: { vertex_position: SEMANTIC_POSITION },
122-
vertexGLSL: ShaderChunks.get(device, SHADERLANGUAGE_GLSL).get('fullscreenQuadVS'),
123-
fragmentGLSL: shaderOutlineExtendPS
160+
vertexChunk: 'fullscreenQuadVS',
161+
fragmentGLSL: shaderOutlineExtendPS,
162+
fragmentWGSL: shaderOutlineExtendWGSL
124163
});
125164

126165
this.shaderBlend = ShaderUtils.createShader(device, {

src/platform/graphics/webgpu/webgpu-graphics-device.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,10 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
12561256
return true;
12571257
}
12581258

1259+
get hasTranspilers() {
1260+
return this.glslang && this.twgsl;
1261+
}
1262+
12591263
// #if _DEBUG
12601264
pushMarker(name) {
12611265
this.passEncoder?.pushDebugGroup(name);

src/scene/shader-lib/programs/lit-shader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ class LitShader {
104104

105105
// shader language
106106
const userChunks = options.shaderChunks;
107-
this.shaderLanguage = (device.isWebGPU && allowWGSL && userChunks?.useWGSL) ? SHADERLANGUAGE_WGSL : SHADERLANGUAGE_GLSL;
107+
this.shaderLanguage = (device.isWebGPU && allowWGSL && (!userChunks || userChunks.useWGSL)) ? SHADERLANGUAGE_WGSL : SHADERLANGUAGE_GLSL;
108+
109+
if (device.isWebGPU && this.shaderLanguage === SHADERLANGUAGE_GLSL) {
110+
if (!device.hasTranspilers) {
111+
Debug.errorOnce('Cannot use GLSL shader on WebGPU without transpilers', {
112+
litShader: this
113+
});
114+
}
115+
}
108116

109117
// resolve custom chunk attributes
110118
this.attributes = {

0 commit comments

Comments
 (0)