Skip to content

Commit

Permalink
Refactor viewport/scissor API
Browse files Browse the repository at this point in the history
Merge the API invocations into RenderEncoder.
Move the depth range inside the viewport.
  • Loading branch information
kvark committed Dec 23, 2024
1 parent a99fd95 commit e30587a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 141 deletions.
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 @@ impl super::CommandEncoder {
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 @@ impl super::PassEncoder<'_, super::ComputePipeline> {
}
}

#[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 @@ impl crate::traits::ComputePipelineEncoder for super::PipelineEncoder<'_> {
}

#[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 @@ -1020,17 +1032,9 @@ impl super::Command {
(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);
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

0 comments on commit e30587a

Please sign in to comment.