Skip to content

Commit f7aaebf

Browse files
mvaligurskyMartin Valigursky
andauthored
Compute shaders preprocess the provided source code (#7783)
Co-authored-by: Martin Valigursky <[email protected]>
1 parent b7f9efb commit f7aaebf

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

examples/src/examples/compute/indirect-draw.compute-shader.wgsl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
// Indexed draw call parameters
2-
struct DrawIndexedIndirectArgs {
3-
indexCount: u32,
4-
instanceCount: u32,
5-
firstIndex: u32,
6-
baseVertex: i32,
7-
firstInstance: u32
8-
};
1+
// include built-in engine chunk, which gives us DrawIndexedIndirectArgs (but also DrawIndirectArgs)
2+
// structs, defining the format of the indirect draw parameters / buffer
3+
#include "indirectCoreCS"
94

105
// Binding 0: uniform buffer holding draw call metadata and runtime config
116
struct Uniforms {

examples/src/examples/compute/indirect-draw.example.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ assetListLoader.load(() => {
121121
shaderLanguage: pc.SHADERLANGUAGE_WGSL,
122122
cshader: files['compute-shader.wgsl'],
123123

124+
// include all WGSL chunks to be available for including in the compute shader
125+
cincludes: pc.ShaderChunks.get(device, pc.SHADERLANGUAGE_WGSL),
126+
124127
// format of a uniform buffer used by the compute shader
125128
computeUniformBufferFormats: {
126129
ub: new pc.UniformBufferFormat(device, [

src/platform/graphics/shader.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class Shader {
7575
* @param {Map<string, string>} [definition.fincludes] - A map containing key-value pairs
7676
* of include names and their content. These are used for resolving #include directives in the
7777
* fragment shader source.
78+
* @param {Map<string, string>} [definition.cincludes] - A map containing key-value pairs
79+
* of include names and their content. These are used for resolving #include directives in the
80+
* compute shader source.
7881
* @param {boolean} [definition.useTransformFeedback] - Specifies that this shader outputs
7982
* post-VS data to a buffer.
8083
* @param {string | string[]} [definition.fragmentOutputTypes] - Fragment shader output types,
@@ -123,6 +126,13 @@ class Shader {
123126
if (definition.cshader) {
124127
Debug.assert(graphicsDevice.supportsCompute, 'Compute shaders are not supported on this device.');
125128
Debug.assert(!definition.vshader && !definition.fshader, 'Vertex and fragment shaders are not supported when creating a compute shader.');
129+
130+
// pre-process compute shader source
131+
definition.cshader = Preprocessor.run(definition.cshader, definition.cincludes, {
132+
sourceName: `compute shader for ${this.label}`,
133+
stripDefines: true
134+
});
135+
126136
} else {
127137
Debug.assert(definition.vshader, 'No vertex shader has been specified when creating a shader.');
128138
Debug.assert(definition.fshader, 'No fragment shader has been specified when creating a shader.');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default /* wgsl */`
2+
// Indirect indexed draw call parameters
3+
struct DrawIndexedIndirectArgs {
4+
indexCount: u32, // Number of indices to draw
5+
instanceCount: u32, // Number of instances to draw
6+
firstIndex: u32, // Index of the first index in the index buffer
7+
baseVertex: i32, // Offset added to each index before fetching vertex
8+
firstInstance: u32 // First instance ID
9+
};
10+
11+
// Indirect non-indexed draw call parameters
12+
struct DrawIndirectArgs {
13+
vertexCount: u32, // Number of vertices to draw
14+
instanceCount: u32, // Number of instances to draw
15+
firstVertex: u32, // Index of the first vertex
16+
firstInstance: u32, // First instance ID
17+
_pad: u32 // Padding to match indexed size (unused)
18+
};
19+
`;

src/scene/shader-lib/wgsl/collections/shader-chunks-wgsl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import gsplatSHVS from '../chunks/gsplat/vert/gsplatSH.js';
5959
import gsplatSourceVS from '../chunks/gsplat/vert/gsplatSource.js';
6060
import gsplatVS from '../chunks/gsplat/vert/gsplat.js';
6161
import quadVS from '../chunks/common/vert/quad.js';
62+
import indirectCoreCS from '../chunks/common/comp/indirect-core.js';
6263
import immediateLinePS from '../chunks/internal/frag/immediateLine.js';
6364
import immediateLineVS from '../chunks/internal/vert/immediateLine.js';
6465
import iridescenceDiffractionPS from '../chunks/lit/frag/iridescenceDiffraction.js';
@@ -234,6 +235,7 @@ const shaderChunksWGSL = {
234235
gsplatSourceVS,
235236
gsplatVS,
236237
quadVS,
238+
indirectCoreCS,
237239
immediateLinePS,
238240
immediateLineVS,
239241
iridescenceDiffractionPS,

0 commit comments

Comments
 (0)