Skip to content

Commit

Permalink
Sketch out compute shader support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Apr 17, 2024
1 parent 0ed14f5 commit 8c76a36
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
8 changes: 8 additions & 0 deletions modules/graphics/src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ impl GraphicsBackend for HeadlessGraphicsBackend {
Ok(())
}

fn shader_dispatch_compute(&self, shader: ShaderId, x: u32, y: u32, z: u32) -> Result<(), ShaderError> {
Ok(())
}

fn shader_memory_barrier(&self, barrier: MemoryBarrier) -> Result<(), ShaderError> {
Ok(())
}

fn shader_delete(&self, shader: ShaderId) -> Result<(), ShaderError> {
Ok(())
}
Expand Down
8 changes: 8 additions & 0 deletions modules/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ pub enum TargetError {
FailedToBuildAttachments,
}

/// A memory barrier for synchronising memory access in a shader.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MemoryBarrier {
ImageAccess,
}

/// An abstraction on top of the underlying graphics API.
///
/// This is a mid-level abstraction that makes use of 'opaque' resource IDs to
Expand Down Expand Up @@ -139,6 +145,8 @@ pub trait GraphicsBackend {
fn shader_uniform_location(&self, shader: ShaderId, name: &str) -> Option<usize>;
fn shader_set_uniform(&self, shader: ShaderId, location: usize, value: &ShaderUniform) -> Result<(), ShaderError>;
fn shader_activate(&self, shader: ShaderId) -> Result<(), ShaderError>;
fn shader_dispatch_compute(&self, shader: ShaderId, x: u32, y: u32, z: u32) -> Result<(), ShaderError>;
fn shader_memory_barrier(&self, barrier: MemoryBarrier) -> Result<(), ShaderError>;
fn shader_delete(&self, shader: ShaderId) -> Result<(), ShaderError>;

// meshes
Expand Down
21 changes: 21 additions & 0 deletions modules/graphics/src/opengl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,27 @@ impl GraphicsBackend for OpenGLGraphicsBackend {
}
}

#[profiling]
fn shader_dispatch_compute(&self, shader: ShaderId, x: u32, y: u32, z: u32) -> Result<(), ShaderError> {
unsafe {
gl::UseProgram(shader.into());
gl::DispatchCompute(x, y, z);

Ok(())
}
}

#[profiling]
fn shader_memory_barrier(&self, barrier: MemoryBarrier) -> Result<(), ShaderError> {
unsafe {
gl::MemoryBarrier(match barrier {
MemoryBarrier::ImageAccess => gl::SHADER_IMAGE_ACCESS_BARRIER_BIT,
});

Ok(())
}
}

#[profiling]
fn shader_delete(&self, shader: ShaderId) -> Result<(), ShaderError> {
unsafe {
Expand Down
29 changes: 29 additions & 0 deletions modules/graphics/src/rendering/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ enum RenderCommand {
vertex_count: usize,
index_count: usize,
},
/// Dispatches a compute shader for execution.
DispatchCompute {
shader_id: ShaderId,
group_count: (u32, u32, u32),
},
/// Issues a memory barrier, which can be used to synchronize memory access.
MemoryBarrier { barrier: MemoryBarrier },
/// Blits the given render target to the active render target.
BlitRenderTargetToActive { target_id: TargetId, filter: TextureFilter },
}
Expand Down Expand Up @@ -134,6 +141,19 @@ impl RenderQueue {
});
}

/// Dispatches a compute shader for execution.
pub fn dispatch_compute(&mut self, shader: &ShaderProgram, group_count: (u32, u32, u32)) {
self.enqueue(RenderCommand::DispatchCompute {
shader_id: shader.id(),
group_count,
});
}

/// Issues a memory barrier, which can be used to synchronize memory access.
pub fn memory_barrier(&mut self, barrier: MemoryBarrier) {
self.enqueue(RenderCommand::MemoryBarrier { barrier });
}

/// Clears all [`RenderCommand`] from the queue.
pub fn clear(&mut self) {
let mut commands = self.commands.lock().unwrap();
Expand Down Expand Up @@ -200,6 +220,15 @@ impl RenderQueue {
} => {
graphics.shader_set_uniform(shader_id, location, &uniform)?;
}
RenderCommand::DispatchCompute {
shader_id,
group_count: (x, y, z),
} => {
graphics.shader_dispatch_compute(shader_id, x, y, z)?;
}
RenderCommand::MemoryBarrier { barrier } => {
graphics.shader_memory_barrier(barrier)?;
}
RenderCommand::DrawMesh {
mesh_id,
topology,
Expand Down
11 changes: 3 additions & 8 deletions modules/graphics/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Texture {
self.state.borrow().height
}

/// Sets the the texture's options on the GPU.
/// Sets the texture's options on the GPU.
pub fn set_options(&mut self, options: TextureOptions) {
let mut state = self.state.borrow_mut();

Expand Down Expand Up @@ -249,7 +249,7 @@ impl Texture {
.expect("Failed to write texture data");
}

/// Uploads a sub-section of pixel data to the texture.
/// Uploads a subsection of pixel data to the texture.
pub fn write_sub_pixels<T: Texel>(&self, region: &Rectangle, pixels: &[T]) {
let state = self.state.borrow();

Expand All @@ -264,11 +264,6 @@ impl Texture {
)
.expect("Failed to write texture data");
}

/// Blits this texture to the display.
pub fn blit_to_display(&self) {
todo!()
}
}

impl Drop for TextureState {
Expand Down Expand Up @@ -317,7 +312,7 @@ impl TextureRegion {
}
}

/// An atlas of textures, which is a sub-division of a texture into a smaller
/// An atlas of textures, which is a subdivision of a texture into a smaller
/// grid of [`TextureRegion`]s.
#[derive(Clone)]
pub struct TextureAtlas {
Expand Down
12 changes: 12 additions & 0 deletions modules/physics/src/internal/world2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,4 +960,16 @@ mod tests {

assert_eq!(world.joint_get_bodies(joint), Some((body1, body2)));
}

#[test]
fn it_should_create_a_simple_material() {
let world = InternalPhysicsWorld2D::default();

let material = world.material_create().unwrap();
let body = world.body_create(BodyKind::Kinematic, Vec2::ZERO).unwrap();

world.body_set_material(body, Some(material)).unwrap();

assert_eq!(world.body_get_material(body), Some(material));
}
}

0 comments on commit 8c76a36

Please sign in to comment.