Skip to content

Commit efae386

Browse files
committed
Set wgpu viewport and scissoring before Primitive::draw
1 parent 949852e commit efae386

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

wgpu/src/lib.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,22 +544,54 @@ impl Renderer {
544544
let mut need_render = Vec::new();
545545

546546
for instance in &layer.primitives {
547+
let bounds = instance.bounds * scale;
548+
547549
if let Some(clip_bounds) = (instance.bounds * scale)
548550
.intersection(&physical_bounds)
549551
.and_then(Rectangle::snap)
550552
{
551-
let drawn = instance.primitive.draw(
552-
&primitive_storage,
553-
&mut render_pass,
554-
&clip_bounds,
553+
render_pass.set_viewport(
554+
bounds.x,
555+
bounds.y,
556+
bounds.width,
557+
bounds.height,
558+
0.0,
559+
1.0,
560+
);
561+
562+
render_pass.set_scissor_rect(
563+
clip_bounds.x,
564+
clip_bounds.y,
565+
clip_bounds.width,
566+
clip_bounds.height,
555567
);
556568

569+
let drawn = instance
570+
.primitive
571+
.draw(&primitive_storage, &mut render_pass);
572+
557573
if !drawn {
558574
need_render.push((instance, clip_bounds));
559575
}
560576
}
561577
}
562578

579+
render_pass.set_viewport(
580+
0.0,
581+
0.0,
582+
viewport.physical_width() as f32,
583+
viewport.physical_height() as f32,
584+
0.0,
585+
1.0,
586+
);
587+
588+
render_pass.set_scissor_rect(
589+
0,
590+
0,
591+
viewport.physical_width(),
592+
viewport.physical_height(),
593+
);
594+
563595
if !need_render.is_empty() {
564596
let _ = ManuallyDrop::into_inner(render_pass);
565597

wgpu/src/primitive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
4848
/// since reusing the existing render pass should be considerably more
4949
/// efficient than issuing a new one.
5050
///
51+
/// The viewport and scissor rect of the render pass provided is set
52+
/// to the bounds and clip bounds of the [`Primitive`], respectively.
53+
///
5154
/// If you have complex composition needs, then you can leverage
5255
/// [`render`](Self::render) by returning `false` here.
5356
///
@@ -56,7 +59,6 @@ pub trait Primitive: Debug + MaybeSend + MaybeSync + 'static {
5659
&self,
5760
_renderer: &Self::Renderer,
5861
_render_pass: &mut wgpu::RenderPass<'_>,
59-
_clip_bounds: &Rectangle<u32>,
6062
) -> bool {
6163
false
6264
}
@@ -93,7 +95,6 @@ pub(crate) trait Stored:
9395
&self,
9496
storage: &Storage,
9597
render_pass: &mut wgpu::RenderPass<'_>,
96-
clip_bounds: &Rectangle<u32>,
9798
) -> bool;
9899

99100
fn render(
@@ -140,15 +141,14 @@ impl<P: Primitive> Stored for BlackBox<P> {
140141
&self,
141142
storage: &Storage,
142143
render_pass: &mut wgpu::RenderPass<'_>,
143-
clip_bounds: &Rectangle<u32>,
144144
) -> bool {
145145
let renderer = storage
146146
.get::<P>()
147147
.expect("renderer should be initialized")
148148
.downcast_ref::<P::Renderer>()
149149
.expect("renderer should have the proper type");
150150

151-
self.primitive.draw(renderer, render_pass, clip_bounds)
151+
self.primitive.draw(renderer, render_pass)
152152
}
153153

154154
fn render(

0 commit comments

Comments
 (0)