Skip to content

Commit 2fd9392

Browse files
mvaligurskyMartin Valigursky
andauthored
Defines map can be specified when creating compute shader (playcanvas#8273)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 502fee1 commit 2fd9392

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/platform/graphics/shader-definition-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class ShaderDefinitionUtils {
204204
* @param {GraphicsDevice} device - The graphics device.
205205
* @param {Map<string, string>} [defines] - A map containing key-value pairs.
206206
* @returns {string} The shader code for the defines.
207-
* @private
207+
* @ignore
208208
*/
209209
static getDefinesCode(device, defines) {
210210
let code = '';

src/platform/graphics/shader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class Shader {
8383
* @param {Map<string, string>} [definition.cincludes] - A map containing key-value pairs
8484
* of include names and their content. These are used for resolving #include directives in the
8585
* compute shader source.
86+
* @param {Map<string, string>} [definition.cdefines] - A map containing key-value pairs of
87+
* define names and their values. These are used for resolving defines in the compute shader.
8688
* @param {boolean} [definition.useTransformFeedback] - Specifies that this shader outputs
8789
* post-VS data to a buffer.
8890
* @param {string | string[]} [definition.fragmentOutputTypes] - Fragment shader output types,
@@ -132,8 +134,17 @@ class Shader {
132134
Debug.assert(graphicsDevice.supportsCompute, 'Compute shaders are not supported on this device.');
133135
Debug.assert(!definition.vshader && !definition.fshader, 'Vertex and fragment shaders are not supported when creating a compute shader.');
134136

137+
// keep reference to unmodified shader in debug mode
138+
Debug.call(() => {
139+
this.cUnmodified = definition.cshader;
140+
});
141+
142+
// Prepend defines to compute shader source
143+
const definesCode = ShaderDefinitionUtils.getDefinesCode(graphicsDevice, definition.cdefines);
144+
const cshader = definesCode + definition.cshader;
145+
135146
// pre-process compute shader source
136-
definition.cshader = Preprocessor.run(definition.cshader, definition.cincludes, {
147+
definition.cshader = Preprocessor.run(cshader, definition.cincludes, {
137148
sourceName: `compute shader for ${this.label}`,
138149
stripDefines: true
139150
});

0 commit comments

Comments
 (0)