Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for storage textures on WebGPU platform #5760

Merged
merged 2 commits into from Oct 18, 2023

Conversation

mvaligursky
Copy link
Contributor

@mvaligursky mvaligursky commented Oct 17, 2023

Support for storage textures, which represent textures that can be written to by a compute shader.

New API:

Texture.constructor takes options.storage as an optional parameter, to construct a texture that is useable as a storage texture for compute shader. Ignored on WebGL.

Example

Using compute shader to fill texture with a single color:

const texture = new Texture(app.graphicsDevice, {
    name: 'outputTexture',
    width: 128,
    height: 128,
    format: PIXELFORMAT_RGBA8,
    mipmaps: false,
    storage: true
});

device.scope.resolve("outTexture").setValue(texture);

const shader = new Shader(device, {
    name: 'ComputeShader',
    shaderLanguage: SHADERLANGUAGE_WGSL,
    cshader: `

        @group(0) @binding(0) var outputTexture: texture_storage_2d<rgba8unorm, write>;

        @compute @workgroup_size(1, 1, 1)
        fn main(@builtin(global_invocation_id) global_id : vec3u) {

            let clearColor: vec4<f32> = vec4<f32>(1.0, 0.0, 0.0, 1.0);
            textureStore(outputTexture, vec2<i32>(global_id.xy), clearColor);
        }
    `
});

// data biding for the shader using a single storage texture (no UBs nor other textures)
// Note: the API here is temporary (the `impl`)
shader.impl.computeBindGroupFormat = new BindGroupFormat(device,[], [], [
    new BindStorageTextureFormat('outTexture', PIXELFORMAT_RGBA8, TEXTUREDIMENSION_2D),
], {
    compute: true
});

const compute = new Compute(device, this.shader);

// run the compute shader
compute.dispatch(texture.width, texture.height);

@mvaligursky mvaligursky merged commit e815adf into main Oct 18, 2023
7 checks passed
@mvaligursky mvaligursky deleted the mv-webgpu-storage-textures branch October 18, 2023 15:12
marklundin pushed a commit that referenced this pull request Nov 3, 2023
* Support for storage textures on WebGPU platform

* lint

---------

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue feature request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants