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

Stencil Additions #242

Merged
merged 12 commits into from
Jan 10, 2025
8 changes: 8 additions & 0 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@
self.commands
.push(super::Command::SetViewport(viewport.clone()));
}

fn set_stencil_reference(&mut self, reference: u32) {

Check warning on line 312 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `reference`

Check warning on line 312 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `reference`
unimplemented!()
}
}

impl super::PassEncoder<'_, super::RenderPipeline> {
Expand Down Expand Up @@ -460,6 +464,10 @@
self.commands
.push(super::Command::SetViewport(viewport.clone()));
}

fn set_stencil_reference(&mut self, reference: u32) {

Check warning on line 468 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `reference`

Check warning on line 468 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `reference`
unimplemented!()
}
}

#[hidden_trait::expose]
Expand Down Expand Up @@ -813,10 +821,10 @@
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
}
Self::CopyTextureToBuffer {
ref src,

Check warning on line 824 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `src`

Check warning on line 824 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `src`
ref dst,

Check warning on line 825 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `dst`

Check warning on line 825 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `dst`
bytes_per_row,

Check warning on line 826 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `bytes_per_row`

Check warning on line 826 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `bytes_per_row`
ref size,

Check warning on line 827 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `size`

Check warning on line 827 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `size`
} => unimplemented!(),
Self::ResetFramebuffer => {
for &attachment in COLOR_ATTACHMENTS.iter() {
Expand Down Expand Up @@ -1040,9 +1048,9 @@
gl.scissor(rect.x, rect.y, rect.w as i32, rect.h as i32);
}
Self::SetStencilFunc {
face,

Check warning on line 1051 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `face`

Check warning on line 1051 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `face`
function,

Check warning on line 1052 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `function`

Check warning on line 1052 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `function`
reference,

Check warning on line 1053 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown, false)

unused variable: `reference`

Check warning on line 1053 in blade-graphics/src/gles/command.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu, true)

unused variable: `reference`
read_mask,
} => unimplemented!(),
Self::SetStencilOps {
Expand Down
10 changes: 10 additions & 0 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,16 @@ fn describe_texture_format(format: crate::TextureFormat) -> FormatInfo {
Tf::Rg32Uint => (glow::RG32UI, glow::RG, glow::UNSIGNED_INT),
Tf::Rgba32Uint => (glow::RGBA32UI, glow::RGBA, glow::UNSIGNED_INT),
Tf::Depth32Float => (glow::DEPTH_COMPONENT32F, glow::DEPTH_COMPONENT, glow::FLOAT),
Tf::Depth32FloatStencil8Uint => (
glow::DEPTH32F_STENCIL8,
glow::DEPTH_STENCIL,
glow::FLOAT_32_UNSIGNED_INT_24_8_REV,
),
Tf::Stencil8Uint => (
glow::STENCIL_INDEX8,
glow::STENCIL_INDEX,
glow::UNSIGNED_BYTE,
),
Tf::Bc1Unorm => (glow::COMPRESSED_RGBA_S3TC_DXT1_EXT, glow::RGBA, 0),
Tf::Bc1UnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, glow::RGBA, 0),
Tf::Bc2Unorm => (glow::COMPRESSED_RGBA_S3TC_DXT3_EXT, glow::RGBA, 0),
Expand Down
2 changes: 2 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ pub enum TextureFormat {
Rgba32Uint,
// depth and stencil
Depth32Float,
Depth32FloatStencil8Uint,
Stencil8Uint,
// S3TC block compression
Bc1Unorm,
Bc1UnormSrgb,
Expand Down
86 changes: 62 additions & 24 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,30 +256,61 @@ impl super::CommandEncoder {
}

if let Some(ref rt) = targets.depth_stencil {
let at_descriptor = descriptor.depthAttachment();
at_descriptor.setTexture(Some(rt.view.as_ref()));
let load_action = match rt.init_op {
crate::InitOp::Load => metal::MTLLoadAction::Load,
crate::InitOp::Clear(color) => {
let clear_depth = match color {
crate::TextureColor::TransparentBlack
| crate::TextureColor::OpaqueBlack => 0.0,
crate::TextureColor::White => 1.0,
};
at_descriptor.setClearDepth(clear_depth);
metal::MTLLoadAction::Clear
}
crate::InitOp::DontCare => metal::MTLLoadAction::DontCare,
};
let store_action = match rt.finish_op {
crate::FinishOp::Store | crate::FinishOp::Ignore => {
metal::MTLStoreAction::Store
}
crate::FinishOp::Discard => metal::MTLStoreAction::DontCare,
crate::FinishOp::ResolveTo(_) => panic!("Can't resolve depth texture"),
};
at_descriptor.setLoadAction(load_action);
at_descriptor.setStoreAction(store_action);
if rt.view.aspects.contains(crate::TexelAspects::DEPTH) {
let at_descriptor = descriptor.depthAttachment();
at_descriptor.setTexture(Some(rt.view.as_ref()));
let load_action = match rt.init_op {
crate::InitOp::Load => metal::MTLLoadAction::Load,
crate::InitOp::Clear(color) => {
let clear_depth = match color {
crate::TextureColor::TransparentBlack
| crate::TextureColor::OpaqueBlack => 0.0,
crate::TextureColor::White => 1.0,
};
at_descriptor.setClearDepth(clear_depth);
metal::MTLLoadAction::Clear
}
crate::InitOp::DontCare => metal::MTLLoadAction::DontCare,
};
let store_action = match rt.finish_op {
crate::FinishOp::Store | crate::FinishOp::Ignore => {
metal::MTLStoreAction::Store
}
crate::FinishOp::Discard => metal::MTLStoreAction::DontCare,
crate::FinishOp::ResolveTo(_) => panic!("Can't resolve depth texture"),
};
at_descriptor.setLoadAction(load_action);
at_descriptor.setStoreAction(store_action);
}

if rt.view.aspects.contains(crate::TexelAspects::STENCIL) {
let at_descriptor = descriptor.stencilAttachment();
at_descriptor.setTexture(Some(rt.view.as_ref()));

let load_action = match rt.init_op {
crate::InitOp::Load => metal::MTLLoadAction::Load,
crate::InitOp::Clear(color) => {
let clear_stencil = match color {
crate::TextureColor::TransparentBlack
| crate::TextureColor::OpaqueBlack => 0,
crate::TextureColor::White => !0,
};
at_descriptor.setClearStencil(clear_stencil);
metal::MTLLoadAction::Clear
}
crate::InitOp::DontCare => metal::MTLLoadAction::DontCare,
};
let store_action = match rt.finish_op {
crate::FinishOp::Store | crate::FinishOp::Ignore => {
metal::MTLStoreAction::Store
}
crate::FinishOp::Discard => metal::MTLStoreAction::DontCare,
crate::FinishOp::ResolveTo(_) => panic!("Can't resolve stencil texture"),
};

at_descriptor.setLoadAction(load_action);
at_descriptor.setStoreAction(store_action);
}
}

if let Some(ref mut td_array) = self.timing_datas {
Expand Down Expand Up @@ -604,6 +635,10 @@ impl crate::traits::RenderEncoder for super::RenderCommandEncoder<'_> {
fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.raw.setViewport(viewport.to_metal());
}

fn set_stencil_reference(&mut self, stencil_reference: u32) {
self.raw.setStencilReferenceValue(stencil_reference);
}
}

impl super::RenderCommandEncoder<'_> {
Expand Down Expand Up @@ -724,6 +759,9 @@ impl crate::traits::RenderEncoder for super::RenderPipelineContext<'_> {
fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.encoder.setViewport(viewport.to_metal());
}
fn set_stencil_reference(&mut self, stencil_reference: u32) {
self.encoder.setStencilReferenceValue(stencil_reference);
}
}

#[hidden_trait::expose]
Expand Down
11 changes: 10 additions & 1 deletion blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Frame {
pub fn texture_view(&self) -> TextureView {
TextureView {
raw: Retained::as_ptr(&self.texture) as *mut _,
aspects: crate::TexelAspects::COLOR,
}
}
}
Expand Down Expand Up @@ -116,6 +117,7 @@ impl Texture {
#[derive(Clone, Copy, Debug, Hash, PartialEq)]
pub struct TextureView {
raw: *mut ProtocolObject<dyn metal::MTLTexture>,
aspects: crate::TexelAspects,
}

unsafe impl Send for TextureView {}
Expand All @@ -125,6 +127,7 @@ impl Default for TextureView {
fn default() -> Self {
Self {
raw: ptr::null_mut(),
aspects: crate::TexelAspects::COLOR,
}
}
}
Expand All @@ -136,9 +139,13 @@ impl TextureView {

/// Create a TextureView from a raw Metal Texture.
/// Does not keep a reference, need not being destoryed.
pub fn from_metal_texture(raw: &Retained<ProtocolObject<dyn metal::MTLTexture>>) -> Self {
pub fn from_metal_texture(
raw: &Retained<ProtocolObject<dyn metal::MTLTexture>>,
aspects: crate::TexelAspects,
) -> Self {
Self {
raw: Retained::into_raw(raw.clone()),
aspects,
}
}
}
Expand Down Expand Up @@ -335,6 +342,8 @@ fn map_texture_format(format: crate::TextureFormat) -> metal::MTLPixelFormat {
Tf::Rg32Uint => Mpf::RG32Uint,
Tf::Rgba32Uint => Mpf::RGBA32Uint,
Tf::Depth32Float => Mpf::Depth32Float,
Tf::Depth32FloatStencil8Uint => Mpf::Depth32Float_Stencil8,
Tf::Stencil8Uint => Mpf::Stencil8,
Tf::Bc1Unorm => Mpf::BC1_RGBA,
Tf::Bc1UnormSrgb => Mpf::BC1_RGBA_sRGB,
Tf::Bc2Unorm => Mpf::BC2_RGBA,
Expand Down
4 changes: 3 additions & 1 deletion blade-graphics/src/metal/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ impl crate::traits::ShaderDevice for super::Context {
Some(ref ds) => {
let raw_format = super::map_texture_format(ds.format);
descriptor.setDepthAttachmentPixelFormat(raw_format);
//TODO: descriptor.set_stencil_attachment_pixel_format(raw_format);
if ds.format.aspects().contains(crate::TexelAspects::STENCIL) {
descriptor.setStencilAttachmentPixelFormat(raw_format);
}

let ds_descriptor = create_depth_stencil_desc(ds);
let raw = self
Expand Down
1 change: 1 addition & 0 deletions blade-graphics/src/metal/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl crate::traits::ResourceDevice for super::Context {
}
super::TextureView {
raw: Retained::into_raw(object),
aspects: desc.format.aspects(),
}
}

Expand Down
1 change: 1 addition & 0 deletions blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub trait AccelerationStructureEncoder {
pub trait RenderEncoder {
fn set_scissor_rect(&mut self, rect: &super::ScissorRect);
fn set_viewport(&mut self, viewport: &super::Viewport);
fn set_stencil_reference(&mut self, reference: u32);
}

pub trait PipelineEncoder {
Expand Down
9 changes: 9 additions & 0 deletions blade-graphics/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl super::TextureFormat {
Self::Rg32Uint => uncompressed(8),
Self::Rgba32Uint => uncompressed(16),
Self::Depth32Float => uncompressed(4),
Self::Depth32FloatStencil8Uint => uncompressed(5),
EriKWDev marked this conversation as resolved.
Show resolved Hide resolved
Self::Stencil8Uint => uncompressed(1),
Self::Bc1Unorm => cx_bc(8),
Self::Bc1UnormSrgb => cx_bc(8),
Self::Bc2Unorm => cx_bc(16),
Expand All @@ -97,6 +99,13 @@ impl super::TextureFormat {
pub fn aspects(&self) -> super::TexelAspects {
match *self {
Self::Depth32Float => super::TexelAspects::DEPTH,

Self::Depth32FloatStencil8Uint => {
super::TexelAspects::DEPTH | super::TexelAspects::STENCIL
}

Self::Stencil8Uint => super::TexelAspects::STENCIL,

_ => super::TexelAspects::COLOR,
}
}
Expand Down
20 changes: 20 additions & 0 deletions blade-graphics/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@ impl crate::traits::RenderEncoder for super::RenderCommandEncoder<'_> {
.cmd_set_viewport(self.cmd_buf.raw, 0, &[vk_viewport])
};
}

fn set_stencil_reference(&mut self, reference: u32) {
unsafe {
self.device.core.cmd_set_stencil_reference(
self.cmd_buf.raw,
vk::StencilFaceFlags::FRONT_AND_BACK,
reference,
)
};
}
}

#[hidden_trait::expose]
Expand Down Expand Up @@ -953,6 +963,16 @@ impl crate::traits::RenderEncoder for super::PipelineEncoder<'_, '_> {
.cmd_set_viewport(self.cmd_buf.raw, 0, &[vk_viewport])
};
}

fn set_stencil_reference(&mut self, reference: u32) {
unsafe {
self.device.core.cmd_set_stencil_reference(
self.cmd_buf.raw,
vk::StencilFaceFlags::FRONT_AND_BACK,
reference,
)
};
}
}

#[hidden_trait::expose]
Expand Down
2 changes: 2 additions & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ fn map_texture_format(format: crate::TextureFormat) -> vk::Format {
Tf::Rg32Uint => vk::Format::R32G32_UINT,
Tf::Rgba32Uint => vk::Format::R32G32B32A32_UINT,
Tf::Depth32Float => vk::Format::D32_SFLOAT,
Tf::Depth32FloatStencil8Uint => vk::Format::D32_SFLOAT_S8_UINT,
Tf::Stencil8Uint => vk::Format::S8_UINT,
Tf::Bc1Unorm => vk::Format::BC1_RGBA_SRGB_BLOCK,
Tf::Bc1UnormSrgb => vk::Format::BC1_RGBA_UNORM_BLOCK,
Tf::Bc2Unorm => vk::Format::BC2_UNORM_BLOCK,
Expand Down
6 changes: 3 additions & 3 deletions blade-graphics/src/vulkan/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ impl crate::traits::ShaderDevice for super::Context {
}
if ds.stencil != crate::StencilState::default() {
let s = &ds.stencil;
let front = map_stencil_face(&s.front, s.read_mask, s.write_mask);
let back = map_stencil_face(&s.back, s.read_mask, s.write_mask);
let front = map_stencil_face_state(&s.front, s.read_mask, s.write_mask);
let back = map_stencil_face_state(&s.back, s.read_mask, s.write_mask);
vk_depth_stencil = vk_depth_stencil
.stencil_test_enable(true)
.front(front)
Expand Down Expand Up @@ -705,7 +705,7 @@ fn map_stencil_op(op: crate::StencilOperation) -> vk::StencilOp {
}
}

fn map_stencil_face(
fn map_stencil_face_state(
face: &crate::StencilFaceState,
compare_mask: u32,
write_mask: u32,
Expand Down
Loading