Skip to content
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
21 changes: 18 additions & 3 deletions src/platform/graphics/graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ class GraphicsDevice extends EventHandler {
/**
* True if the device can read from StorageTexture in the compute shader. By default, the
* storage texture can be only used with the write operation.
* When a shader uses this feature, it's recommended to use a `requires` directive to signal the
* potential for non-portability at the top of the WGSL shader code:
* ```javascript
* When a shader uses this feature, add a `requires` directive to signal non-portability at the
* top of the WGSL shader code. The shader define `CAPS_STORAGE_TEXTURE_READ` is set when this
* capability is available.
* ```wgsl
* requires readonly_and_readwrite_storage_textures;
* ```
*
Expand Down Expand Up @@ -282,6 +283,18 @@ class GraphicsDevice extends EventHandler {
*/
supportsSubgroupId = false;

/**
* True if the device supports the WGSL `linear_indexing` extension, which provides the
* `global_invocation_index` and `workgroup_index` built-in values in compute shaders. The
* `requires linear_indexing;` directive is then automatically injected for compute shader
* modules, and the shader define `CAPS_LINEAR_INDEXING` is set for conditional
* compilation.
*
* @type {boolean}
* @readonly
*/
supportsLinearIndexing = false;

/**
* Maximum subgroup (warp/wavefront) size supported by the device. Zero if subgroups are
* not supported. Used internally to gate algorithms that assume a specific subgroup size.
Expand Down Expand Up @@ -668,6 +681,8 @@ class GraphicsDevice extends EventHandler {
if (this.supportsShaderF16) capsDefines.set('CAPS_SHADER_F16', '');
if (this.supportsSubgroups) capsDefines.set('CAPS_SUBGROUPS', '');
if (this.supportsSubgroupId) capsDefines.set('CAPS_SUBGROUP_ID', '');
if (this.supportsLinearIndexing) capsDefines.set('CAPS_LINEAR_INDEXING', '');
if (this.supportsStorageTextureRead) capsDefines.set('CAPS_STORAGE_TEXTURE_READ', '');

// Platform defines
if (platform.desktop) capsDefines.set('PLATFORM_DESKTOP', '');
Expand Down
5 changes: 4 additions & 1 deletion src/platform/graphics/shader-definition-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class ShaderDefinitionUtils {
}

/**
* Generates WGSL enable directives based on device capabilities. Enable directives must come
* Generates WGSL `enable` / `requires` directives based on device capabilities. They must come
* before all global declarations in WGSL shaders.
*
* @param {GraphicsDevice} device - The graphics device.
Expand All @@ -227,6 +227,9 @@ class ShaderDefinitionUtils {
if (device.supportsSubgroupId) {
code += 'requires subgroup_id;\n';
}
if (shaderType === 'compute' && device.supportsLinearIndexing) {
code += 'requires linear_indexing;\n';
}
return code;
}

Expand Down
1 change: 1 addition & 0 deletions src/platform/graphics/webgpu/webgpu-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
this.supportsStorageTextureRead = wgslFeatures?.has('readonly_and_readwrite_storage_textures');
this.supportsSubgroupUniformity = wgslFeatures?.has('subgroup_uniformity');
this.supportsSubgroupId = wgslFeatures?.has('subgroup_id');
this.supportsLinearIndexing = wgslFeatures?.has('linear_indexing');

this.initCapsDefines();
}
Expand Down