Skip to content

Commit

Permalink
Merge pull request #14833 from Popov72/computeshader-databuffer
Browse files Browse the repository at this point in the history
ComputeShader: Allow DataBuffer to be passed for uniform / storage buffer
  • Loading branch information
sebavan authored Mar 5, 2024
2 parents 19e4c2e + 77a9688 commit 88a438a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions packages/dev/core/src/Compute/computeShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ export class ComputeShader {
* @param name Binding name of the buffer
* @param buffer Buffer to bind
*/
public setUniformBuffer(name: string, buffer: UniformBuffer): void {
public setUniformBuffer(name: string, buffer: UniformBuffer | DataBuffer): void {
const current = this._bindings[name];

this._contextIsDirty ||= !current || current.object !== buffer;

this._bindings[name] = {
type: ComputeBindingType.UniformBuffer,
type: ComputeShader._BufferIsDataBuffer(buffer) ? ComputeBindingType.DataBuffer : ComputeBindingType.UniformBuffer,
object: buffer,
indexInGroupEntries: current?.indexInGroupEntries,
};
Expand All @@ -247,13 +247,13 @@ export class ComputeShader {
* @param name Binding name of the buffer
* @param buffer Buffer to bind
*/
public setStorageBuffer(name: string, buffer: StorageBuffer): void {
public setStorageBuffer(name: string, buffer: StorageBuffer | DataBuffer): void {
const current = this._bindings[name];

this._contextIsDirty ||= !current || current.object !== buffer;

this._bindings[name] = {
type: ComputeBindingType.StorageBuffer,
type: ComputeShader._BufferIsDataBuffer(buffer) ? ComputeBindingType.DataBuffer : ComputeBindingType.StorageBuffer,
object: buffer,
indexInGroupEntries: current?.indexInGroupEntries,
};
Expand Down Expand Up @@ -498,6 +498,10 @@ export class ComputeShader {

return compute;
}

protected static _BufferIsDataBuffer(buffer: UniformBuffer | StorageBuffer | DataBuffer): buffer is DataBuffer {
return (buffer as DataBuffer).underlyingResource !== undefined;
}
}

RegisterClass("BABYLON.ComputeShader", ComputeShader);
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum ComputeBindingType {
TextureWithoutSampler = 4,
Sampler = 5,
ExternalTexture = 6,
DataBuffer = 7,
}

/** @internal */
Expand Down
12 changes: 9 additions & 3 deletions packages/dev/core/src/Engines/WebGPU/webgpuComputeContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DataBuffer } from "core/Buffers/dataBuffer";
import type { StorageBuffer } from "../../Buffers/storageBuffer";
import type { IComputeContext } from "../../Compute/IComputeContext";
import type { BaseTexture } from "../../Materials/Textures/baseTexture";
Expand Down Expand Up @@ -116,9 +117,14 @@ export class WebGPUComputeContext implements IComputeContext {
}

case ComputeBindingType.UniformBuffer:
case ComputeBindingType.StorageBuffer: {
const buffer = type === ComputeBindingType.UniformBuffer ? (object as UniformBuffer) : (object as StorageBuffer);
const dataBuffer = buffer.getBuffer()!;
case ComputeBindingType.StorageBuffer:
case ComputeBindingType.DataBuffer: {
const dataBuffer =
type === ComputeBindingType.DataBuffer
? (object as DataBuffer)
: type === ComputeBindingType.UniformBuffer
? (object as UniformBuffer).getBuffer()!
: (object as StorageBuffer).getBuffer()!;
const webgpuBuffer = dataBuffer.underlyingResource as GPUBuffer;
if (indexInGroupEntries !== undefined && bindGroupEntriesExist) {
(entries[indexInGroupEntries].resource as GPUBufferBinding).buffer = webgpuBuffer;
Expand Down

0 comments on commit 88a438a

Please sign in to comment.