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

Merge instances of set viewport/scissor at the API level #230

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Range, str, time::Duration};
use std::{str, time::Duration};

const COLOR_ATTACHMENTS: &[u32] = &[
glow::COLOR_ATTACHMENT0,
Expand Down Expand Up @@ -209,15 +209,14 @@
self.commands.push(super::Command::SetDrawColorBuffers(
targets.colors.len() as _
));
self.commands.push(super::Command::SetViewport {
viewport: crate::Viewport {
self.commands
.push(super::Command::SetViewport(crate::Viewport {
x: 0.0,
y: 0.0,
w: target_size[0] as _,
h: target_size[1] as _,
},
depth_range: 0.0..1.0,
});
depth: 0.0..1.0,
}));
self.commands
.push(super::Command::SetScissor(crate::ScissorRect {
x: 0,
Expand Down Expand Up @@ -299,6 +298,18 @@
}
}

#[hidden_trait::expose]
impl crate::traits::RenderEncoder for super::PassEncoder<'_, super::RenderPipeline> {
fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
self.commands.push(super::Command::SetScissor(rect.clone()));
}

fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.commands
.push(super::Command::SetViewport(viewport.clone()));
}
}

impl super::PassEncoder<'_, super::RenderPipeline> {
pub fn with<'b>(
&'b mut self,
Expand Down Expand Up @@ -440,19 +451,20 @@
}

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::PipelineEncoder<'_> {
type BufferPiece = crate::BufferPiece;

impl crate::traits::RenderEncoder for super::PipelineEncoder<'_> {
fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
self.commands.push(super::Command::SetScissor(rect.clone()));
}

fn set_viewport(&mut self, viewport: &crate::Viewport, depth_range: Range<f32>) {
self.commands.push(super::Command::SetViewport {
viewport: viewport.clone(),
depth_range,
});
fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.commands
.push(super::Command::SetViewport(viewport.clone()));
}
}

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::PipelineEncoder<'_> {
type BufferPiece = crate::BufferPiece;

fn bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
assert_eq!(index, 0);
Expand Down Expand Up @@ -801,10 +813,10 @@
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
}
Self::CopyTextureToBuffer {
ref src,

Check warning on line 816 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 816 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 817 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 817 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 818 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 818 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 819 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 819 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 @@ -1020,29 +1032,21 @@
(None, None) => (),
},
Self::Barrier => unimplemented!(),
Self::SetViewport {
ref viewport,
ref depth_range,
} => {
gl.viewport(
viewport.x as i32,
viewport.y as i32,
viewport.w as i32,
viewport.h as i32,
);
gl.depth_range_f32(depth_range.start, depth_range.end);
Self::SetViewport(ref vp) => {
gl.viewport(vp.x as i32, vp.y as i32, vp.w as i32, vp.h as i32);
gl.depth_range_f32(vp.depth.start, vp.depth.end);
}
Self::SetScissor(ref rect) => {
gl.scissor(rect.x, rect.y, rect.w as i32, rect.h as i32);
}
Self::SetStencilFunc {
face,

Check warning on line 1043 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 1043 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 1044 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 1044 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 1045 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,

Check warning on line 1046 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: `read_mask`
} => unimplemented!(),
Self::SetStencilOps {
face,

Check warning on line 1049 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`
write_mask,
//ops: crate::StencilOps,
} => unimplemented!(),
Expand Down
5 changes: 1 addition & 4 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ enum Command {
stencil: Option<u32>,
},
Barrier,
SetViewport {
viewport: crate::Viewport,
depth_range: Range<f32>,
},
SetViewport(crate::Viewport),
SetScissor(crate::ScissorRect),
SetStencilFunc {
face: u32,
Expand Down
1 change: 1 addition & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,7 @@ pub struct Viewport {
pub y: f32,
pub w: f32,
pub h: f32,
pub depth: std::ops::Range<f32>,
}

pub type Timings = std::collections::HashMap<String, std::time::Duration>;
84 changes: 43 additions & 41 deletions blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use objc2_metal::{
MTLCommandBuffer as _, MTLCommandEncoder, MTLComputeCommandEncoder as _,
MTLCounterSampleBuffer, MTLRenderCommandEncoder,
};
use std::{marker::PhantomData, mem, ops::Range, ptr::NonNull, slice, time::Duration};
use std::{marker::PhantomData, mem, ptr::NonNull, slice, time::Duration};

impl<T: bytemuck::Pod> crate::ShaderBindable for T {
fn bind_to(&self, ctx: &mut super::PipelineContext, index: u32) {
Expand Down Expand Up @@ -570,29 +570,43 @@ impl Drop for super::ComputeCommandEncoder<'_> {
}
}

impl super::RenderCommandEncoder<'_> {
pub fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
let scissor = metal::MTLScissorRect {
x: rect.x as _,
y: rect.y as _,
width: rect.w as _,
height: rect.h as _,
};
self.raw.setScissorRect(scissor);
impl crate::ScissorRect {
const fn to_metal(&self) -> metal::MTLScissorRect {
metal::MTLScissorRect {
x: self.x as _,
y: self.y as _,
width: self.w as _,
height: self.h as _,
}
}
}
impl crate::Viewport {
const fn to_metal(&self) -> metal::MTLViewport {
metal::MTLViewport {
originX: self.x as _,
originY: self.y as _,
width: self.w as _,
height: self.h as _,
znear: self.depth.start as _,
// TODO: broken on some Intel GPUs
// https://github.com/gfx-rs/wgpu/blob/ee3ae0e549fe01c4b699cf68f9b67ae8ea807564/wgpu-hal/src/metal/mod.rs#L298
zfar: self.depth.end as _,
}
}
}

pub fn set_viewport(&mut self, viewport: &crate::Viewport, depth_range: Range<f32>) {
let viewport = metal::MTLViewport {
originX: viewport.x as _,
originY: viewport.y as _,
width: viewport.w as _,
height: viewport.h as _,
znear: depth_range.start as _,
zfar: depth_range.end as _, // TODO: aparently broken on some Intel GPU:s? see wgpu-hal
};
self.raw.setViewport(viewport);
#[hidden_trait::expose]
impl crate::traits::RenderEncoder for super::RenderCommandEncoder<'_> {
fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
self.raw.setScissorRect(rect.to_metal());
}

fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.raw.setViewport(viewport.to_metal());
}
}

impl super::RenderCommandEncoder<'_> {
pub fn with<'p>(
&'p mut self,
pipeline: &'p super::RenderPipeline,
Expand Down Expand Up @@ -703,30 +717,18 @@ impl crate::traits::PipelineEncoder for super::RenderPipelineContext<'_> {
}

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::RenderPipelineContext<'_> {
type BufferPiece = crate::BufferPiece;

impl crate::traits::RenderEncoder for super::RenderPipelineContext<'_> {
fn set_scissor_rect(&mut self, rect: &crate::ScissorRect) {
let scissor = metal::MTLScissorRect {
x: rect.x as _,
y: rect.y as _,
width: rect.w as _,
height: rect.h as _,
};
self.encoder.setScissorRect(scissor);
self.encoder.setScissorRect(rect.to_metal());
}

fn set_viewport(&mut self, viewport: &crate::Viewport, depth_range: Range<f32>) {
let viewport = metal::MTLViewport {
originX: viewport.x as _,
originY: viewport.y as _,
width: viewport.w as _,
height: viewport.h as _,
znear: depth_range.start as _,
zfar: depth_range.end as _, // TODO: aparently broken on some Intel GPU:s? see wgpu-hal
};
self.encoder.setViewport(viewport);
fn set_viewport(&mut self, viewport: &crate::Viewport) {
self.encoder.setViewport(viewport.to_metal());
}
}

#[hidden_trait::expose]
impl crate::traits::RenderPipelineEncoder for super::RenderPipelineContext<'_> {
type BufferPiece = crate::BufferPiece;

fn bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
unsafe {
Expand Down
13 changes: 8 additions & 5 deletions blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, hash::Hash, ops::Range};
use std::{fmt::Debug, hash::Hash};

pub trait ResourceDevice {
type Buffer: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
Expand Down Expand Up @@ -108,6 +108,11 @@ pub trait AccelerationStructureEncoder {
);
}

pub trait RenderEncoder {
fn set_scissor_rect(&mut self, rect: &super::ScissorRect);
fn set_viewport(&mut self, viewport: &super::Viewport);
}

pub trait PipelineEncoder {
fn bind<D: super::ShaderData>(&mut self, group: u32, data: &D);
}
Expand All @@ -116,12 +121,10 @@ pub trait ComputePipelineEncoder: PipelineEncoder {
fn dispatch(&mut self, groups: [u32; 3]);
}

pub trait RenderPipelineEncoder: PipelineEncoder {
pub trait RenderPipelineEncoder: PipelineEncoder + RenderEncoder {
type BufferPiece: Send + Sync + Clone + Copy + Debug;

//TODO: reconsider exposing this here
fn set_scissor_rect(&mut self, rect: &super::ScissorRect);
fn set_viewport(&mut self, viewport: &super::Viewport, depth_range: Range<f32>);
//Note: does this need to be available outside of the pipeline?
fn bind_vertex(&mut self, index: u32, vertex_buf: Self::BufferPiece);
fn draw(
&mut self,
Expand Down
Loading
Loading