Skip to content

Commit

Permalink
Compute shader bind group can specify textures without samplers (#6216)
Browse files Browse the repository at this point in the history
* Compute shader bind group can specify textures without samplers

* hasSampler

---------

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed Apr 2, 2024
1 parent 44e4ad3 commit b27ce45
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 33 deletions.
3 changes: 1 addition & 2 deletions examples/src/examples/compute/histogram/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export default {
'compute-shader.wgsl': /* wgsl */ `
@group(0) @binding(0) var inputTexture: texture_2d<f32>;
// @group(0) @binding(1) is a sampler of the inputTexture, but we don't need it in the shader.
@group(0) @binding(2) var<storage, read_write> bins: array<atomic<u32>>;
@group(0) @binding(1) var<storage, read_write> bins: array<atomic<u32>>;
fn luminance(color: vec3f) -> f32 {
return saturate(dot(color, vec3f(0.2126, 0.7152, 0.0722)));
Expand Down
4 changes: 2 additions & 2 deletions examples/src/examples/compute/histogram/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ assetListLoader.load(() => {
computeBindGroupFormat: new pc.BindGroupFormat(device, [
// no uniform buffer
], [
// input texture - the scene color map
new pc.BindTextureFormat('uSceneColorMap', pc.SHADERSTAGE_COMPUTE)
// input texture - the scene color map, without a sampler
new pc.BindTextureFormat('uSceneColorMap', pc.SHADERSTAGE_COMPUTE, undefined, undefined, false)
], [
// no storage textures
], [
Expand Down
4 changes: 1 addition & 3 deletions examples/src/examples/compute/texture-gen/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ export default {
}
@group(0) @binding(0) var<uniform> ubCompute : ub_compute;
@group(0) @binding(1) var inputTexture: texture_2d<f32>;
// @group(0) @binding(2) is a sampler of the inputTexture, but we don't need it in the shader.
@group(0) @binding(3) var outputTexture: texture_storage_2d<rgba8unorm, write>;
@group(0) @binding(2) var outputTexture: texture_storage_2d<rgba8unorm, write>;
@compute @workgroup_size(1, 1, 1)
fn main(@builtin(global_invocation_id) global_id : vec3u) {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/examples/compute/texture-gen/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ assetListLoader.load(() => {
new pc.BindBufferFormat(pc.UNIFORM_BUFFER_DEFAULT_SLOT_NAME, pc.SHADERSTAGE_COMPUTE)
], [
// input textures
new pc.BindTextureFormat('inTexture', pc.SHADERSTAGE_COMPUTE)
new pc.BindTextureFormat('inTexture', pc.SHADERSTAGE_COMPUTE, undefined, undefined, false)
], [
// output storage textures
new pc.BindStorageTextureFormat('outTexture', pc.PIXELFORMAT_RGBA8, pc.TEXTUREDIMENSION_2D)
Expand Down
5 changes: 4 additions & 1 deletion src/platform/graphics/bind-group-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BindTextureFormat {
/** @type {import('./scope-id.js').ScopeId} */
scopeId;

constructor(name, visibility, textureDimension = TEXTUREDIMENSION_2D, sampleType = SAMPLETYPE_FLOAT) {
constructor(name, visibility, textureDimension = TEXTUREDIMENSION_2D, sampleType = SAMPLETYPE_FLOAT, hasSampler = true) {
/** @type {string} */
this.name = name;

Expand All @@ -63,6 +63,9 @@ class BindTextureFormat {

// SAMPLETYPE_***
this.sampleType = sampleType;

// whether to use a sampler with this texture
this.hasSampler = hasSampler;
}
}

Expand Down
30 changes: 16 additions & 14 deletions src/platform/graphics/webgpu/webgpu-bind-group-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,22 @@ class WebgpuBindGroupFormat {
});

// sampler
const gpuSamplerType = samplerTypes[sampleType];
Debug.assert(gpuSamplerType);

key += `#${index}S:${visibility}-${gpuSamplerType}`;

entries.push({
binding: index++,
visibility: visibility,
sampler: {
// Indicates the required type of a sampler bound to this bindings
// 'filtering', 'non-filtering', 'comparison'
type: gpuSamplerType
}
});
if (textureFormat.hasSampler) {
const gpuSamplerType = samplerTypes[sampleType];
Debug.assert(gpuSamplerType);

key += `#${index}S:${visibility}-${gpuSamplerType}`;

entries.push({
binding: index++,
visibility: visibility,
sampler: {
// Indicates the required type of a sampler bound to this bindings
// 'filtering', 'non-filtering', 'comparison'
type: gpuSamplerType
}
});
}
});

// storage textures
Expand Down
22 changes: 12 additions & 10 deletions src/platform/graphics/webgpu/webgpu-bind-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ class WebgpuBindGroup {
});

// sampler
const sampler = wgpuTexture.getSampler(device, textureFormat.sampleType);
Debug.assert(sampler, 'NULL sampler cannot be used by the bind group');
Debug.call(() => {
this.debugFormat += `${index}: ${sampler.label}\n`;
});

entries.push({
binding: index++,
resource: sampler
});
if (textureFormat.hasSampler) {
const sampler = wgpuTexture.getSampler(device, textureFormat.sampleType);
Debug.assert(sampler, 'NULL sampler cannot be used by the bind group');
Debug.call(() => {
this.debugFormat += `${index}: ${sampler.label}\n`;
});

entries.push({
binding: index++,
resource: sampler
});
}
});

// storage textures
Expand Down

0 comments on commit b27ce45

Please sign in to comment.