Skip to content

Commit 726df8e

Browse files
committed
Merge pull request #592 from bjz/associated-resources
Parametrise batches over Resources
2 parents cab5f9f + e96a485 commit 726df8e

File tree

7 files changed

+58
-52
lines changed

7 files changed

+58
-52
lines changed

examples/cube/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ fn main() {
174174

175175
let state = gfx::DrawState::new().depth(gfx::state::Comparison::LessEqual, true);
176176

177-
let batch: RefBatch<Params> = context.make_batch(&program, &mesh, slice, &state)
178-
.ok().expect("Failed to make batch.");
177+
let batch: RefBatch<Params, gfx::GlResources> = {
178+
context.make_batch(&program, &mesh, slice, &state)
179+
.ok().expect("Failed to make batch.")
180+
};
179181

180182
let view: AffineMatrix3<f32> = Transform::look_at(
181183
&Point3::new(1.5f32, -5.0, 3.0),

examples/deferred/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ fn main() {
402402
};
403403

404404
let terrain_scale = Vector3::new(25.0, 25.0, 25.0);
405-
let terrain_batch: RefBatch<TerrainParams> = {
405+
let terrain_batch: RefBatch<TerrainParams, gfx::GlResources> = {
406406
let plane = genmesh::generators::Plane::subdivide(256, 256);
407407
let vertex_data: Vec<TerrainVertex> = plane.shared_vertex_iter()
408408
.map(|(x, y)| {
@@ -435,7 +435,7 @@ fn main() {
435435
.ok().expect("Failed to match back")
436436
};
437437

438-
let blit_batch: RefBatch<BlitParams> = {
438+
let blit_batch: RefBatch<BlitParams, gfx::GlResources> = {
439439
let vertex_data = [
440440
BlitVertex { pos: [-1, -1, 0], tex_coord: [0, 0] },
441441
BlitVertex { pos: [ 1, -1, 0], tex_coord: [1, 0] },
@@ -507,15 +507,15 @@ fn main() {
507507
.depth(gfx::state::Comparison::LessEqual, false)
508508
.blend(gfx::BlendPreset::Additive);
509509

510-
let light_batch: RefBatch<LightParams> = {
510+
let light_batch: RefBatch<LightParams, gfx::GlResources> = {
511511
let program = device.link_program(LIGHT_VERTEX_SRC, LIGHT_FRAGMENT_SRC)
512512
.ok().expect("Failed to link program.");
513513

514514
context.make_batch(&program, &mesh, slice, &state)
515515
.ok().expect("Failed to create batch")
516516
};
517517

518-
let emitter_batch: RefBatch<EmitterParams> = {
518+
let emitter_batch: RefBatch<EmitterParams, gfx::GlResources> = {
519519
let program = device.link_program(EMITTER_VERTEX_SRC, EMITTER_FRAGMENT_SRC)
520520
.ok().expect("Failed to link program.");
521521

examples/performance/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ fn gfx_main(mut glfw: glfw::Glfw,
130130
};
131131

132132
let mut graphics = gfx::Graphics::new(device);
133-
let batch: RefBatch<Params> = graphics.make_batch(&program, &mesh, slice, &state)
134-
.ok().expect("Failed to make batch");
133+
let batch: RefBatch<Params, gfx::GlResources> = {
134+
graphics.make_batch(&program, &mesh, slice, &state)
135+
.ok().expect("Failed to make batch")
136+
};
135137

136138
while !window.should_close() {
137139
glfw.poll_events();

examples/terrain/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ fn main() {
163163
let state = gfx::DrawState::new().depth(gfx::state::Comparison::LessEqual, true);
164164

165165
let mut graphics = gfx::Graphics::new(device);
166-
let batch: RefBatch<Params> = graphics.make_batch(&program, &mesh, slice, &state)
167-
.ok().expect("Failed to make batch.");
166+
let batch: RefBatch<Params, gfx::GlResources> = {
167+
graphics.make_batch(&program, &mesh, slice, &state)
168+
.ok().expect("Failed to make batch.")
169+
};
168170

169171
let aspect = w as f32 / h as f32;
170172
let mut data = Params {

src/gfx/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct Graphics<D: device::Device> {
5757
/// Renderer front-end.
5858
pub renderer: Renderer<D>,
5959
/// Hidden batch context.
60-
context: batch::Context,
60+
context: batch::Context<GlResources>,
6161
}
6262

6363
impl<D: device::Device> Graphics<D> {
@@ -77,7 +77,7 @@ impl<D: device::Device> Graphics<D> {
7777
mesh: &Mesh<GlResources>,
7878
slice: Slice<GlResources>,
7979
state: &DrawState)
80-
-> Result<batch::RefBatch<T>, batch::BatchError> {
80+
-> Result<batch::RefBatch<T, GlResources>, batch::BatchError> {
8181
self.context.make_batch(program, mesh, slice, state)
8282
}
8383

@@ -88,7 +88,7 @@ impl<D: device::Device> Graphics<D> {
8888

8989
/// Draw a ref batch.
9090
pub fn draw<'a, T: shade::ShaderParam>(&'a mut self,
91-
batch: &'a batch::RefBatch<T>, data: &'a T, frame: &Frame<GlResources>)
91+
batch: &'a batch::RefBatch<T, GlResources>, data: &'a T, frame: &Frame<GlResources>)
9292
-> Result<(), DrawError<batch::OutOfBounds>> {
9393
self.renderer.draw(&(batch, data, &self.context), frame)
9494
}

src/render/batch.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,23 @@ impl<'a, T: ShaderParam> Batch for (&'a mesh::Mesh<back::GlResources>, mesh::Sli
111111
}
112112

113113
/// Owned batch - self-contained, but has heap-allocated data
114-
pub struct OwnedBatch<T: ShaderParam> {
115-
mesh: mesh::Mesh<back::GlResources>,
114+
pub struct OwnedBatch<T: ShaderParam, R: Resources> {
115+
mesh: mesh::Mesh<R>,
116116
mesh_link: mesh::Link,
117117
/// Mesh slice
118-
pub slice: mesh::Slice<back::GlResources>,
118+
pub slice: mesh::Slice<R>,
119119
/// Parameter data.
120120
pub param: T,
121-
program: ProgramHandle<back::GlResources>,
121+
program: ProgramHandle<R>,
122122
param_link: T::Link,
123123
/// Draw state
124124
pub state: DrawState,
125125
}
126126

127-
impl<T: ShaderParam> OwnedBatch<T> {
127+
impl<T: ShaderParam, R: Resources> OwnedBatch<T, R> {
128128
/// Create a new owned batch
129-
pub fn new(mesh: mesh::Mesh<back::GlResources>, program: ProgramHandle<back::GlResources>, param: T)
130-
-> Result<OwnedBatch<T>, BatchError> {
129+
pub fn new(mesh: mesh::Mesh<R>, program: ProgramHandle<R>, param: T)
130+
-> Result<OwnedBatch<T, R>, BatchError> {
131131
let slice = mesh.to_slice(PrimitiveType::TriangleList);
132132
let mesh_link = match link_mesh(&mesh, program.get_info()) {
133133
Ok(l) => l,
@@ -149,7 +149,7 @@ impl<T: ShaderParam> OwnedBatch<T> {
149149
}
150150
}
151151

152-
impl<T: ShaderParam> Batch for OwnedBatch<T> {
152+
impl<T: ShaderParam> Batch for OwnedBatch<T, back::GlResources> {
153153
type Resources = back::GlResources;
154154
type Error = ();
155155

@@ -250,73 +250,73 @@ impl<T: Clone + PartialEq> Array<T> {
250250
/// Ref batch - copyable and smaller, but depends on the `Context`.
251251
/// It has references to the resources (mesh, program, state), that are held
252252
/// by the context that created the batch, so these have to be used together.
253-
pub struct RefBatch<T: ShaderParam> {
254-
mesh_id: Id<mesh::Mesh<back::GlResources>>,
253+
pub struct RefBatch<T: ShaderParam, R: Resources> {
254+
mesh_id: Id<mesh::Mesh<R>>,
255255
mesh_link: mesh::Link,
256256
/// Mesh slice
257-
pub slice: mesh::Slice<back::GlResources>,
258-
program_id: Id<ProgramHandle<back::GlResources>>,
257+
pub slice: mesh::Slice<R>,
258+
program_id: Id<ProgramHandle<R>>,
259259
param_link: T::Link,
260260
state_id: Id<DrawState>,
261261
}
262262

263-
impl<T: ShaderParam> Copy for RefBatch<T> where T::Link: Copy {}
263+
impl<T: ShaderParam, R: Resources> Copy for RefBatch<T, R> where T::Link: Copy {}
264264

265-
impl<T: ShaderParam> fmt::Debug for RefBatch<T> {
265+
impl<T: ShaderParam, R: Resources> fmt::Debug for RefBatch<T, R> {
266266
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
267267
write!(f, "RefBatch(mesh: {:?}, slice: {:?}, program: {:?}, state: {:?})",
268268
self.mesh_id, self.slice, self.program_id, self.state_id)
269269
}
270270
}
271271

272-
impl<T: ShaderParam> PartialEq for RefBatch<T> {
273-
fn eq(&self, other: &RefBatch<T>) -> bool {
272+
impl<T: ShaderParam, R: Resources> PartialEq for RefBatch<T, R> {
273+
fn eq(&self, other: &RefBatch<T, R>) -> bool {
274274
self.program_id == other.program_id &&
275275
self.state_id == other.state_id &&
276276
self.mesh_id == other.mesh_id
277277
}
278278
}
279279

280-
impl<T: ShaderParam> Eq for RefBatch<T> {}
280+
impl<T: ShaderParam, R: Resources> Eq for RefBatch<T, R> {}
281281

282-
impl<T: ShaderParam> PartialOrd for RefBatch<T> {
283-
fn partial_cmp(&self, other: &RefBatch<T>) -> Option<Ordering> {
282+
impl<T: ShaderParam, R: Resources> PartialOrd for RefBatch<T, R> {
283+
fn partial_cmp(&self, other: &RefBatch<T, R>) -> Option<Ordering> {
284284
Some(self.cmp(other))
285285
}
286286
}
287287

288-
impl<T: ShaderParam> Ord for RefBatch<T> {
289-
fn cmp(&self, other: &RefBatch<T>) -> Ordering {
288+
impl<T: ShaderParam, R: Resources> Ord for RefBatch<T, R> {
289+
fn cmp(&self, other: &RefBatch<T, R>) -> Ordering {
290290
(&self.program_id, &self.state_id, &self.mesh_id).cmp(
291291
&(&other.program_id, &other.state_id, &other.mesh_id))
292292
}
293293
}
294294

295-
impl<T: ShaderParam> RefBatch<T> {
295+
impl<T: ShaderParam, R: Resources> RefBatch<T, R> {
296296
/// Compare meshes by Id
297-
pub fn cmp_mesh(&self, other: &RefBatch<T>) -> Ordering {
297+
pub fn cmp_mesh(&self, other: &RefBatch<T, R>) -> Ordering {
298298
self.mesh_id.cmp(&other.mesh_id)
299299
}
300300
/// Compare programs by Id
301-
pub fn cmp_program(&self, other: &RefBatch<T>) -> Ordering {
301+
pub fn cmp_program(&self, other: &RefBatch<T, R>) -> Ordering {
302302
self.program_id.cmp(&other.program_id)
303303
}
304304
/// Compare draw states by Id
305-
pub fn cmp_state(&self, other: &RefBatch<T>) -> Ordering {
305+
pub fn cmp_state(&self, other: &RefBatch<T, R>) -> Ordering {
306306
self.state_id.cmp(&other.state_id)
307307
}
308308
}
309309

310310
/// Factory of ref batches, required to always be used with them.
311-
pub struct Context {
312-
meshes: Array<mesh::Mesh<back::GlResources>>,
313-
programs: Array<ProgramHandle<back::GlResources>>,
311+
pub struct Context<R: Resources> {
312+
meshes: Array<mesh::Mesh<R>>,
313+
programs: Array<ProgramHandle<R>>,
314314
states: Array<DrawState>,
315315
}
316316

317-
impl Context {
317+
impl<R: Resources> Context<R> {
318318
/// Create a new empty `Context`
319-
pub fn new() -> Context {
319+
pub fn new() -> Context<R> {
320320
Context {
321321
meshes: Array::new(),
322322
programs: Array::new(),
@@ -325,14 +325,14 @@ impl Context {
325325
}
326326
}
327327

328-
impl Context {
328+
impl<R: Resources> Context<R> {
329329
/// Produce a new ref batch
330330
pub fn make_batch<T: ShaderParam>(&mut self,
331-
program: &ProgramHandle<back::GlResources>,
332-
mesh: &mesh::Mesh<back::GlResources>,
333-
slice: mesh::Slice<back::GlResources>,
331+
program: &ProgramHandle<R>,
332+
mesh: &mesh::Mesh<R>,
333+
slice: mesh::Slice<R>,
334334
state: &DrawState)
335-
-> Result<RefBatch<T>, BatchError> {
335+
-> Result<RefBatch<T, R>, BatchError> {
336336
let mesh_link = match link_mesh(mesh, program.get_info()) {
337337
Ok(l) => l,
338338
Err(e) => return Err(BatchError::Mesh(e)),
@@ -365,7 +365,7 @@ impl Context {
365365
}
366366
}
367367

368-
impl<'a, T: ShaderParam> Batch for (&'a RefBatch<T>, &'a T, &'a Context) {
368+
impl<'a, T: ShaderParam> Batch for (&'a RefBatch<T, back::GlResources>, &'a T, &'a Context<back::GlResources>) {
369369
type Resources = back::GlResources;
370370
type Error = OutOfBounds;
371371

tests/shader_param.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ struct TestParam {
3636
fn test_link_copy() {
3737
// testing if RefBatch is copyable
3838
fn _is_copy<T: Copy>(_t: T) {}
39-
fn _ref_copy(batch: gfx::batch::RefBatch<TestParam>) {
39+
fn _ref_copy(batch: gfx::batch::RefBatch<TestParam, gfx::GlResources>) {
4040
_is_copy(batch)
4141
}
4242
}
4343

4444
#[test]
4545
fn test_shader_param() {
4646
// testing if RefBatch can be constructed
47-
let _ref: gfx::batch::RefBatch<TestParam>;
47+
let _ref: gfx::batch::RefBatch<TestParam, gfx::GlResources>;
4848
// testing if OwnedBatch can be constructed
49-
let _owned: gfx::batch::OwnedBatch<TestParam>;
49+
let _owned: gfx::batch::OwnedBatch<TestParam, gfx::GlResources>;
5050
}

0 commit comments

Comments
 (0)