-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from linebender/indirect
Add indirect dispatch and CPU shaders
- Loading branch information
Showing
9 changed files
with
540 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2023 The Vello authors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! Support for CPU implementations of compute shaders. | ||
|
||
use std::{ | ||
cell::{RefCell, RefMut}, | ||
ops::Deref, | ||
}; | ||
|
||
#[derive(Clone, Copy)] | ||
pub enum CpuBinding<'a> { | ||
Buffer(&'a [u8]), | ||
BufferRW(&'a RefCell<Vec<u8>>), | ||
#[allow(unused)] | ||
Texture(&'a CpuTexture), | ||
} | ||
|
||
pub enum CpuBufGuard<'a> { | ||
Slice(&'a [u8]), | ||
Interior(RefMut<'a, Vec<u8>>), | ||
} | ||
|
||
impl<'a> Deref for CpuBufGuard<'a> { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
match self { | ||
CpuBufGuard::Slice(s) => s, | ||
CpuBufGuard::Interior(r) => r, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> CpuBufGuard<'a> { | ||
/// Get a mutable reference to the buffer. | ||
/// | ||
/// Panics if the underlying resource is read-only. | ||
pub fn as_mut(&mut self) -> &mut [u8] { | ||
match self { | ||
CpuBufGuard::Interior(r) => &mut *r, | ||
_ => panic!("tried to borrow immutable buffer as mutable"), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> CpuBinding<'a> { | ||
pub fn as_buf(&self) -> CpuBufGuard { | ||
match self { | ||
CpuBinding::Buffer(b) => CpuBufGuard::Slice(b), | ||
CpuBinding::BufferRW(b) => CpuBufGuard::Interior(b.borrow_mut()), | ||
_ => panic!("resource type mismatch"), | ||
} | ||
} | ||
|
||
// TODO: same guard as buf to make mutable | ||
#[allow(unused)] | ||
pub fn as_tex(&self) -> &CpuTexture { | ||
match self { | ||
CpuBinding::Texture(t) => t, | ||
_ => panic!("resource type mismatch"), | ||
} | ||
} | ||
} | ||
|
||
/// Structure used for binding textures to CPU shaders. | ||
pub struct CpuTexture { | ||
pub width: usize, | ||
pub height: usize, | ||
// In RGBA format. May expand in the future. | ||
pub pixels: Vec<u32>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright 2023 The Vello authors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! CPU implementations of shader stages. | ||
|
||
mod pathtag_reduce; | ||
|
||
pub use pathtag_reduce::pathtag_reduce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2023 The Vello authors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use vello_encoding::{ConfigUniform, Monoid, PathMonoid}; | ||
|
||
use crate::cpu_dispatch::CpuBinding; | ||
|
||
const WG_SIZE: usize = 256; | ||
|
||
fn pathtag_reduce_main( | ||
n_wg: u32, | ||
config: &ConfigUniform, | ||
scene: &[u32], | ||
reduced: &mut [PathMonoid], | ||
) { | ||
let pathtag_base = config.layout.path_tag_base; | ||
for i in 0..n_wg { | ||
let mut m = PathMonoid::default(); | ||
for j in 0..WG_SIZE { | ||
let tag = scene[(pathtag_base + i * WG_SIZE as u32) as usize + j]; | ||
m = m.combine(&PathMonoid::new(tag)); | ||
} | ||
reduced[i as usize] = m; | ||
} | ||
} | ||
|
||
pub fn pathtag_reduce(n_wg: u32, resources: &[CpuBinding]) { | ||
let r0 = resources[0].as_buf(); | ||
let r1 = resources[1].as_buf(); | ||
let mut r2 = resources[2].as_buf(); | ||
let config = bytemuck::from_bytes(&r0); | ||
let scene = bytemuck::cast_slice(&r1); | ||
let reduced = bytemuck::cast_slice_mut(r2.as_mut()); | ||
pathtag_reduce_main(n_wg, config, scene, reduced); | ||
} |
Oops, something went wrong.