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

feature: added and implemented set_viewport #227

Merged
merged 2 commits into from
Dec 20, 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
32 changes: 26 additions & 6 deletions blade-graphics/src/gles/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str, time::Duration};
use std::{ops::Range, str, time::Duration};

const COLOR_ATTACHMENTS: &[u32] = &[
glow::COLOR_ATTACHMENT0,
Expand Down Expand Up @@ -210,8 +210,13 @@
targets.colors.len() as _
));
self.commands.push(super::Command::SetViewport {
size: target_size,
depth: 0.0..1.0,
viewport: crate::Viewport {
x: 0.0,
y: 0.0,
w: target_size[0] as _,
h: target_size[1] as _,
},
depth_range: 0.0..1.0,
});
self.commands
.push(super::Command::SetScissor(crate::ScissorRect {
Expand Down Expand Up @@ -442,6 +447,13 @@
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 bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
assert_eq!(index, 0);
self.commands.push(super::Command::BindVertex {
Expand Down Expand Up @@ -789,10 +801,10 @@
gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None);
}
Self::CopyTextureToBuffer {
ref src,

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

View workflow job for this annotation

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

unused variable: `src`

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

View workflow job for this annotation

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

unused variable: `src`
ref dst,

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

View workflow job for this annotation

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

unused variable: `dst`

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

View workflow job for this annotation

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

unused variable: `dst`
bytes_per_row,

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

View workflow job for this annotation

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

unused variable: `bytes_per_row`

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

View workflow job for this annotation

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

unused variable: `bytes_per_row`
ref size,

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

View workflow job for this annotation

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

unused variable: `size`

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

View workflow job for this annotation

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

unused variable: `size`
} => unimplemented!(),
Self::ResetFramebuffer => {
for &attachment in COLOR_ATTACHMENTS.iter() {
Expand Down Expand Up @@ -1008,21 +1020,29 @@
(None, None) => (),
},
Self::Barrier => unimplemented!(),
Self::SetViewport { size, ref depth } => {
gl.viewport(0, 0, size[0] as i32, size[1] as i32);
gl.depth_range_f32(depth.start, depth.end);
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::SetScissor(ref rect) => {
gl.scissor(rect.x, rect.y, rect.w as i32, rect.h as i32);
}
Self::SetStencilFunc {
face,

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

View workflow job for this annotation

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

unused variable: `face`

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

View workflow job for this annotation

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

unused variable: `face`
function,

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

View workflow job for this annotation

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

unused variable: `function`

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

View workflow job for this annotation

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

unused variable: `function`
reference,

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

View workflow job for this annotation

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

unused variable: `reference`

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

View workflow job for this annotation

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

unused variable: `reference`
read_mask,

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

View workflow job for this annotation

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

unused variable: `read_mask`

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

View workflow job for this annotation

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

unused variable: `read_mask`
} => unimplemented!(),
Self::SetStencilOps {
face,

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)

unused variable: `face`

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

View workflow job for this annotation

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

unused variable: `face`
write_mask,
//ops: crate::StencilOps,
} => unimplemented!(),
Expand Down
4 changes: 2 additions & 2 deletions blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ enum Command {
},
Barrier,
SetViewport {
size: [u16; 2],
depth: Range<f32>,
viewport: crate::Viewport,
depth_range: Range<f32>,
},
SetScissor(crate::ScissorRect),
SetStencilFunc {
Expand Down
8 changes: 8 additions & 0 deletions blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,4 +1142,12 @@ pub struct ScissorRect {
pub h: u32,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Viewport {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}

pub type Timings = std::collections::HashMap<String, std::time::Duration>;
26 changes: 25 additions & 1 deletion blade-graphics/src/metal/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, mem, time::Duration};
use std::{marker::PhantomData, mem, ops::Range, time::Duration};

impl<T: bytemuck::Pod> crate::ShaderBindable for T {
fn bind_to(&self, ctx: &mut super::PipelineContext, index: u32) {
Expand Down Expand Up @@ -529,6 +529,18 @@ impl super::RenderCommandEncoder<'_> {
self.raw.set_scissor_rect(scissor);
}

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.set_viewport(viewport);
}

pub fn with<'p>(
&'p mut self,
pipeline: &'p super::RenderPipeline,
Expand Down Expand Up @@ -646,6 +658,18 @@ impl crate::traits::RenderPipelineEncoder for super::RenderPipelineContext<'_> {
self.encoder.set_scissor_rect(scissor);
}

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.set_viewport(viewport);
}

fn bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
self.encoder.set_vertex_buffer(
index as u64,
Expand Down
3 changes: 2 additions & 1 deletion 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};
use std::{fmt::Debug, hash::Hash, ops::Range};

pub trait ResourceDevice {
type Buffer: Send + Sync + Clone + Copy + Debug + Hash + PartialEq;
Expand Down Expand Up @@ -121,6 +121,7 @@ pub trait RenderPipelineEncoder: PipelineEncoder {

//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>);
fn bind_vertex(&mut self, index: u32, vertex_buf: Self::BufferPiece);
fn draw(
&mut self,
Expand Down
34 changes: 33 additions & 1 deletion blade-graphics/src/vulkan/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ash::vk;
use std::{str, time::Duration};
use std::{ops::Range, str, time::Duration};

impl super::CrashHandler {
fn add_marker(&mut self, marker: &str) -> u32 {
Expand Down Expand Up @@ -816,6 +816,22 @@ impl<'a> super::RenderCommandEncoder<'a> {
};
}

pub fn set_viewport(&mut self, viewport: &crate::Viewport, depth_range: Range<f32>) {
let vk_viewports = [vk::Viewport {
x: viewport.x,
y: viewport.y,
width: viewport.w,
height: -viewport.h, // flip Y
min_depth: depth_range.start,
max_depth: depth_range.end,
}];
unsafe {
self.device
.core
.cmd_set_viewport(self.cmd_buf.raw, 0, &vk_viewports)
};
}

pub fn with<'b, 'p>(
&'b mut self,
pipeline: &'p super::RenderPipeline,
Expand Down Expand Up @@ -922,6 +938,22 @@ impl crate::traits::RenderPipelineEncoder for super::PipelineEncoder<'_, '_> {
};
}

fn set_viewport(&mut self, viewport: &crate::Viewport, depth_range: Range<f32>) {
let vk_viewports = [vk::Viewport {
x: viewport.x,
y: viewport.y,
width: viewport.w,
height: -viewport.h, // flip Y
min_depth: depth_range.start,
max_depth: depth_range.end,
}];
unsafe {
self.device
.core
.cmd_set_viewport(self.cmd_buf.raw, 0, &vk_viewports)
};
}

fn bind_vertex(&mut self, index: u32, vertex_buf: crate::BufferPiece) {
unsafe {
self.device.core.cmd_bind_vertex_buffers(
Expand Down
Loading