Fixed issue where quads of different types were not ordered.

This commit is contained in:
Bingus 2023-05-25 10:27:27 -07:00
parent 75110b9c0e
commit 3f141459a6
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
4 changed files with 87 additions and 13 deletions

View file

@ -265,8 +265,12 @@ impl Backend {
}
if !layer.quads.is_empty() {
self.quad_pipeline
.render(quad_layer, bounds, &mut render_pass);
self.quad_pipeline.render(
quad_layer,
bounds,
&layer.quads.order,
&mut render_pass,
);
quad_layer += 1;
}

View file

@ -43,6 +43,12 @@ pub struct Quads {
/// The gradient quads of the [`Layer`].
pub gradients: Vec<quad::Gradient>,
/// The quad order of the [`Layer`]; stored as a tuple of the quad type & its count.
pub order: Vec<(quad::Order, usize)>,
// The last index of quad ordering.
index: usize,
}
impl Quads {
@ -174,12 +180,13 @@ impl<'a> Layer<'a> {
border_width: *border_width,
};
match background {
let quad_order = match background {
Background::Color(color) => {
layer.quads.solids.push(quad::Solid {
color: color.into_linear(),
quad,
});
quad::Order::Solid
}
Background::Gradient(gradient) => {
let quad = quad::Gradient {
@ -194,8 +201,41 @@ impl<'a> Layer<'a> {
};
layer.quads.gradients.push(quad);
quad::Order::Gradient
}
};
match (layer.quads.order.get_mut(layer.quads.index), quad_order)
{
(Some((quad_order, count)), quad::Order::Solid) => {
match quad_order {
quad::Order::Solid => {
*count += 1;
}
quad::Order::Gradient => {
layer.quads.order.push((quad::Order::Solid, 1));
layer.quads.index += 1;
}
}
}
(Some((quad_order, count)), quad::Order::Gradient) => {
match quad_order {
quad::Order::Solid => {
layer
.quads
.order
.push((quad::Order::Gradient, 1));
layer.quads.index += 1;
}
quad::Order::Gradient => {
*count += 1;
}
}
}
(None, _) => {
layer.quads.order.push((quad_order, 1));
}
}
}
Primitive::Image { handle, bounds } => {
let layer = &mut layers[current_layer];

View file

@ -49,3 +49,12 @@ unsafe impl Pod for Gradient {}
#[allow(unsafe_code)]
unsafe impl Zeroable for Gradient {}
#[derive(Debug, Copy, Clone)]
/// The identifier of a quad, used for ordering.
pub enum Order {
/// A solid quad
Solid,
/// A gradient quad
Gradient,
}

View file

@ -1,6 +1,6 @@
use crate::core::Rectangle;
use crate::graphics::Transformation;
use crate::layer;
use crate::layer::{self, quad};
use std::mem;
use wgpu::util::DeviceExt;
@ -87,6 +87,7 @@ impl Pipeline {
&'a self,
layer: usize,
bounds: Rectangle<u32>,
ordering: &Vec<(quad::Order, usize)>,
render_pass: &mut wgpu::RenderPass<'a>,
) {
if let Some(layer) = self.layers.get(layer) {
@ -102,14 +103,30 @@ impl Pipeline {
);
render_pass.set_vertex_buffer(0, self.vertices.slice(..));
if layer.solid.instance_count > 0 {
render_pass.set_pipeline(&self.solid.pipeline);
layer.solid.draw(&layer.constants, render_pass);
}
let mut solid_offset = 0;
let mut gradient_offset = 0;
if layer.gradient.instance_count > 0 {
render_pass.set_pipeline(&self.gradient.pipeline);
layer.gradient.draw(&layer.constants, render_pass);
for (quad_order, count) in ordering {
match quad_order {
quad::Order::Solid => {
render_pass.set_pipeline(&self.solid.pipeline);
layer.solid.draw(
&layer.constants,
render_pass,
solid_offset..(solid_offset + count),
);
solid_offset += count;
}
quad::Order::Gradient => {
render_pass.set_pipeline(&self.gradient.pipeline);
layer.gradient.draw(
&layer.constants,
render_pass,
gradient_offset..(gradient_offset + count),
);
gradient_offset += count;
}
}
}
}
}
@ -198,6 +215,7 @@ mod solid {
use crate::layer::quad;
use crate::quad::{color_target_state, Vertex, INDICES, INITIAL_INSTANCES};
use crate::Buffer;
use std::ops::Range;
#[derive(Debug)]
pub struct Pipeline {
@ -229,6 +247,7 @@ mod solid {
&'a self,
constants: &'a wgpu::BindGroup,
render_pass: &mut wgpu::RenderPass<'a>,
range: Range<usize>,
) {
#[cfg(feature = "tracing")]
let _ = tracing::info_span!("Wgpu::Quad::Solid", "DRAW").entered();
@ -239,7 +258,7 @@ mod solid {
render_pass.draw_indexed(
0..INDICES.len() as u32,
0,
0..self.instance_count as u32,
range.start as u32..range.end as u32,
);
}
}
@ -327,6 +346,7 @@ mod gradient {
use crate::layer::quad;
use crate::quad::{color_target_state, Vertex, INDICES, INITIAL_INSTANCES};
use crate::Buffer;
use std::ops::Range;
#[derive(Debug)]
pub struct Pipeline {
@ -358,6 +378,7 @@ mod gradient {
&'a self,
constants: &'a wgpu::BindGroup,
render_pass: &mut wgpu::RenderPass<'a>,
range: Range<usize>,
) {
#[cfg(feature = "tracing")]
let _ =
@ -369,7 +390,7 @@ mod gradient {
render_pass.draw_indexed(
0..INDICES.len() as u32,
0,
0..self.instance_count as u32,
range.start as u32..range.end as u32,
);
}
}