Skip to content

Commit 72aae1a

Browse files
committed
Make Batch generic over primitive type
1 parent afef09e commit 72aae1a

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

core/src/render/batch.rs

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ use crate::{
88
math::{Mat4x4, Vary},
99
};
1010

11-
use super::{Context, NdcToScreen, Shader, Target};
11+
use super::{Clip, Context, NdcToScreen, Render, Shader, Target};
1212

1313
/// A builder for rendering a chunk of geometry as a batch.
1414
///
1515
/// Several values must be assigned before the [`render`][Batch::render]
1616
/// method can be called:
17-
/// * [faces][Batch::faces]: A list of triangles, each a triplet of indices
18-
/// into the list of vertices (TODO: handling oob)
17+
/// * [primitives][Batch::primitives]: A list of primitives, each a tuple
18+
/// of indices into the list of vertices (TODO: handling oob)
1919
/// * [vertices][Batch::vertices]: A list of vertices
2020
/// * [shader][Batch::shader]: The combined vertex and fragment shader used
2121
/// * [target][Batch::target]: The render target to render into
22-
/// * [context][Batch::context]: The rendering context and settings used. (TODO: optional?)
22+
/// * [context][Batch::context]: The rendering context and settings used.
23+
/// (TODO: optional?)
2324
///
2425
/// Additionally, setting the following values is optional:
2526
/// * [uniform][Batch::uniform]: The uniform value passed to the vertex shader
@@ -29,8 +30,8 @@ use super::{Context, NdcToScreen, Shader, Target};
2930
// using the same configuration, or several [instances] of the same geometry.
3031
// [instances]: https://en.wikipedia.org/wiki/Geometry_instancing
3132
#[derive(Clone, Debug, Default)]
32-
pub struct Batch<Vtx, Uni, Shd, Tgt, Ctx> {
33-
faces: Vec<Tri<usize>>,
33+
pub struct Batch<Prim, Vtx, Uni, Shd, Tgt, Ctx> {
34+
prims: Vec<Prim>,
3435
verts: Vec<Vtx>,
3536
uniform: Uni,
3637
shader: Shd,
@@ -46,21 +47,22 @@ macro_rules! update {
4647
}};
4748
}
4849

49-
impl Batch<(), (), (), (), Context> {
50+
impl Batch<(), (), (), (), (), Context> {
5051
pub fn new() -> Self {
5152
Self::default()
5253
}
5354
}
5455

55-
impl<Vtx, Uni, Shd, Tgt, Ctx> Batch<Vtx, Uni, Shd, Tgt, Ctx> {
56-
/// Sets the faces to be rendered.
56+
impl<Prim, Vtx, Uni, Shd, Tgt, Ctx> Batch<Prim, Vtx, Uni, Shd, Tgt, Ctx> {
57+
/// Sets the primitives to be rendered.
5758
///
58-
/// The faces are copied into the batch.
59-
pub fn faces(self, faces: impl AsRef<[Tri<usize>]>) -> Self {
60-
Self {
61-
faces: faces.as_ref().to_vec(),
62-
..self
63-
}
59+
/// The primitives are copied into the batch.
60+
pub fn primitives<P: Clone>(
61+
self,
62+
prims: impl AsRef<[P]>,
63+
) -> Batch<P, Vtx, Uni, Shd, Tgt, Ctx> {
64+
let prims = prims.as_ref().to_vec();
65+
update!(prims; self verts uniform shader viewport target ctx)
6466
}
6567

6668
/// Sets the vertices to be rendered.
@@ -70,69 +72,78 @@ impl<Vtx, Uni, Shd, Tgt, Ctx> Batch<Vtx, Uni, Shd, Tgt, Ctx> {
7072
pub fn vertices<V: Clone>(
7173
self,
7274
verts: impl AsRef<[V]>,
73-
) -> Batch<V, Uni, Shd, Tgt, Ctx> {
75+
) -> Batch<Prim, V, Uni, Shd, Tgt, Ctx> {
7476
let verts = verts.as_ref().to_vec();
75-
update!(verts; self faces uniform shader viewport target ctx)
77+
update!(verts; self prims uniform shader viewport target ctx)
7678
}
7779

7880
/// Clones faces and vertices from a mesh to this batch.
7981
pub fn mesh<A: Clone>(
8082
self,
8183
mesh: &Mesh<A>,
82-
) -> Batch<Vertex3<A>, Uni, Shd, Tgt, Ctx> {
83-
let faces = mesh.faces.clone();
84+
) -> Batch<Tri<usize>, Vertex3<A>, Uni, Shd, Tgt, Ctx> {
85+
let prims = mesh.faces.clone();
8486
let verts = mesh.verts.clone();
85-
update!(verts faces; self uniform shader viewport target ctx)
87+
update!(verts prims; self uniform shader viewport target ctx)
8688
}
8789

8890
/// Sets the uniform data to be passed to the vertex shaders.
89-
pub fn uniform<U: Copy>(self, uniform: U) -> Batch<Vtx, U, Shd, Tgt, Ctx> {
90-
update!(uniform; self verts faces shader viewport target ctx)
91+
pub fn uniform<U: Copy>(
92+
self,
93+
uniform: U,
94+
) -> Batch<Prim, Vtx, U, Shd, Tgt, Ctx> {
95+
update!(uniform; self verts prims shader viewport target ctx)
9196
}
9297

9398
/// Sets the combined vertex and fragment shader.
94-
pub fn shader<V: Vary, S: Shader<Vtx, V, Uni>>(
99+
pub fn shader<V: Vary, U, S: Shader<Vtx, V, U>>(
95100
self,
96101
shader: S,
97-
) -> Batch<Vtx, Uni, S, Tgt, Ctx> {
98-
update!(shader; self verts faces uniform viewport target ctx)
102+
) -> Batch<Prim, Vtx, Uni, S, Tgt, Ctx> {
103+
update!(shader; self verts prims uniform viewport target ctx)
99104
}
100105

101106
/// Sets the viewport matrix.
102107
pub fn viewport(self, viewport: Mat4x4<NdcToScreen>) -> Self {
103-
update!(viewport; self verts faces uniform shader target ctx)
108+
update!(viewport; self verts prims uniform shader target ctx)
104109
}
105110

106111
/// Sets the render target.
107112
// TODO what bound for T?
108-
pub fn target<T>(self, target: T) -> Batch<Vtx, Uni, Shd, T, Ctx> {
109-
update!(target; self verts faces uniform shader viewport ctx)
113+
pub fn target<T>(self, target: T) -> Batch<Prim, Vtx, Uni, Shd, T, Ctx> {
114+
update!(target; self verts prims uniform shader viewport ctx)
110115
}
111116

112117
/// Sets the rendering context.
113-
pub fn context(self, ctx: &Context) -> Batch<Vtx, Uni, Shd, Tgt, &Context> {
114-
update!(ctx; self verts faces uniform shader viewport target)
118+
pub fn context(
119+
self,
120+
ctx: &Context,
121+
) -> Batch<Prim, Vtx, Uni, Shd, Tgt, &Context> {
122+
update!(ctx; self verts prims uniform shader viewport target)
115123
}
116124
}
117125

118-
impl<Vtx, Uni, Shd, Tgt, Ctx> Batch<Vtx, Uni, Shd, &mut Tgt, Ctx> {
126+
impl<Prim, Vtx, Uni, Shd, Tgt, Ctx> Batch<Prim, Vtx, Uni, Shd, &mut Tgt, Ctx> {
119127
/// Renders this batch of geometry.
120128
#[rustfmt::skip]
121-
pub fn render<V: Vary>(&mut self)
129+
pub fn render<Var>(&mut self)
122130
where
131+
Var: Vary,
132+
Prim: Render<Var> + Clone,
123133
Vtx: Clone,
124134
Uni: Copy,
125-
Shd: Shader<Vtx, V, Uni>,
135+
[<Prim>::Clip]: Clip<Item= Prim::Clip>,
136+
Shd: Shader<Vtx, Var, Uni>,
126137
Tgt: Target,
127138
Ctx: Borrow<Context>
128139
{
129140
let Self {
130-
faces, verts, shader, uniform, viewport, target, ctx,
141+
prims, verts, shader, uniform, viewport, target, ctx,
131142
} = self;
132143

133144
super::render(
134-
faces, verts, shader, *uniform, *viewport,
135-
*target, (*ctx).borrow(),
145+
prims, verts, shader, *uniform, *viewport, *target,
146+
(*ctx).borrow(),
136147
);
137148
}
138149
}

0 commit comments

Comments
 (0)