Bevy: Add compute shaders

Created on 11 Aug 2020  路  1Comment  路  Source: bevyengine/bevy

Allowing users or even internal rendering logic access to compute shaders will provide a way of processing arbitrary data on the GPU. This can be used for everything from advanced lighting techniques, fast boid's calculations, and image processing.

Some key questions:

  1. How does compute fit within the current pipeline and graph implementation?
  2. Compute shaders run on their own and are not associated with fragment or vertex shaders. Currently the ShaderStages type expects to always have a vertex shader. I would suggest perhaps breaking this type up?
  3. A clear API around how users can interact with compute shaders. If a compute shader keeps all of its data on the GPU the API can be relatively simple, however if a user needs access to the data that was processed in the compute shader we would need some sort of non-blocking(async) access to a wgpu buffer, but perhaps that's a separate issue?

As a reference for how a compute pass works here is a simple example of wgpu compute logic:

let pass = encoder.begin_compute_pass();
pass.set_pipeline(&pipeline.compute_pipeline);
pass.set_bind_group(0, &self.bind_group, &[]);
pass.dispatch(8, 8, 1); // The number of work groups(x, y, z).

I'm more than willing to work on adding compute shaders to bevy I just need some direction as to how I move forward considering the questions above. 馃檪

enhancement rendering

Most helpful comment

  1. I think it can exist inside the current graph implementation. I dont think we'll need to change nodes at all. We might need to extend what data types can be passed between nodes, but short term we can also use the global RenderResourceBindings to share data between nodes.
  2. If they are 100% orthogonal then we should definitely create a new type. If theres only one stage then maybe we can just omit a ComputeShaderStages type and use the shader handle directly.
  3. I think there should be two ways to retrieve data from a buffer. The low level RenderResourceContext::map_buffer approach, and a higher level Render Graph Node that uses map_buffer internally and copies to a specific resource or component.

In general you have all of the compute shader experience here, so I think the best i can do at this point (without fully diving in) is give you the Bevy context you need. Feel free to ask for clarity where it is needed!

>All comments

  1. I think it can exist inside the current graph implementation. I dont think we'll need to change nodes at all. We might need to extend what data types can be passed between nodes, but short term we can also use the global RenderResourceBindings to share data between nodes.
  2. If they are 100% orthogonal then we should definitely create a new type. If theres only one stage then maybe we can just omit a ComputeShaderStages type and use the shader handle directly.
  3. I think there should be two ways to retrieve data from a buffer. The low level RenderResourceContext::map_buffer approach, and a higher level Render Graph Node that uses map_buffer internally and copies to a specific resource or component.

In general you have all of the compute shader experience here, so I think the best i can do at this point (without fully diving in) is give you the Bevy context you need. Feel free to ask for clarity where it is needed!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreasterrius picture andreasterrius  路  27Comments

fopsdev picture fopsdev  路  14Comments

cart picture cart  路  80Comments

kakoeimon picture kakoeimon  路  13Comments

cart picture cart  路  22Comments