Skip to content

More associated resources stuff #590

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

Merged
merged 3 commits into from
Feb 23, 2015
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
10 changes: 7 additions & 3 deletions src/render/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::num::from_uint;
use std::cmp::Ordering;
use std::marker::PhantomData;
use device::back;
use device::{PrimitiveType, ProgramHandle};
use device::{Resources, PrimitiveType, ProgramHandle};
use device::shade::ProgramInfo;
use render::mesh;
use render::mesh::ToSlice;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub enum BatchError {

/// Match mesh attributes against shader inputs, produce a mesh link.
/// Exposed to public to allow external `Batch` implementations to use it.
pub fn link_mesh(mesh: &mesh::Mesh<back::GlResources>, pinfo: &ProgramInfo) -> Result<mesh::Link, MeshError> {
pub fn link_mesh<R: Resources>(mesh: &mesh::Mesh<R>, pinfo: &ProgramInfo) -> Result<mesh::Link, MeshError> {
let mut indices = Vec::new();
for sat in pinfo.attributes.iter() {
match mesh.attributes.iter().enumerate()
Expand All @@ -74,17 +74,19 @@ pub type BatchData<'a> = (&'a mesh::Mesh<back::GlResources>, mesh::AttributeIter

/// Abstract batch trait
pub trait Batch {
type Resources: Resources;
/// Possible errors occurring at batch access
type Error: fmt::Debug;
/// Obtain information about the mesh, program, and state
fn get_data(&self) -> Result<BatchData, Self::Error>;
/// Fill shader parameter values
fn fill_params(&self, ::shade::ParamValues)
-> Result<&ProgramHandle<back::GlResources>, Self::Error>;
-> Result<&ProgramHandle<Self::Resources>, Self::Error>;
}

impl<'a, T: ShaderParam> Batch for (&'a mesh::Mesh<back::GlResources>, mesh::Slice<back::GlResources>,
&'a ProgramHandle<back::GlResources>, &'a T, &'a DrawState) {
type Resources = back::GlResources;
type Error = BatchError;

fn get_data(&self) -> Result<BatchData, BatchError> {
Expand Down Expand Up @@ -148,6 +150,7 @@ impl<T: ShaderParam> OwnedBatch<T> {
}

impl<T: ShaderParam> Batch for OwnedBatch<T> {
type Resources = back::GlResources;
type Error = ();

fn get_data(&self) -> Result<BatchData, ()> {
Expand Down Expand Up @@ -363,6 +366,7 @@ impl Context {
}

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

fn get_data(&self) -> Result<BatchData, OutOfBounds> {
Expand Down
8 changes: 4 additions & 4 deletions src/render/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ impl<D: Device> Renderer<D> {
}

/// Draw a `batch` into the specified `frame`
pub fn draw<B: Batch>(&mut self, batch: &B, frame: &target::Frame<back::GlResources>)
pub fn draw<B: Batch<Resources = back::GlResources>>(&mut self, batch: &B, frame: &target::Frame<back::GlResources>)
-> Result<(), DrawError<B::Error>> {
self.draw_all(batch, None, frame)
}

/// Draw a `batch` multiple times using instancing
pub fn draw_instanced<B: Batch>(&mut self, batch: &B,
pub fn draw_instanced<B: Batch<Resources = back::GlResources>>(&mut self, batch: &B,
count: device::InstanceCount,
base: device::VertexCount,
frame: &target::Frame<back::GlResources>)
Expand All @@ -203,7 +203,7 @@ impl<D: Device> Renderer<D> {
}

/// Draw a 'batch' with all known parameters specified, internal use only.
fn draw_all<B: Batch>(&mut self, batch: &B, instances: Option<Instancing>,
fn draw_all<B: Batch<Resources = back::GlResources>>(&mut self, batch: &B, instances: Option<Instancing>,
frame: &target::Frame<back::GlResources>) -> Result<(), DrawError<B::Error>> {
let (mesh, attrib_iter, slice, state) = match batch.get_data() {
Ok(data) => data,
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<D: Device> Renderer<D> {
self.render_state.draw = *state;
}

fn bind_program<'a, B: Batch>(&mut self, batch: &'a B)
fn bind_program<'a, B: Batch<Resources = back::GlResources>>(&mut self, batch: &'a B)
-> Result<&'a device::ProgramHandle<back::GlResources>, B::Error> {
let program = match batch.fill_params(self.parameters.get_mut()) {
Ok(p) => p,
Expand Down
10 changes: 5 additions & 5 deletions src/render/shade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::cell::Cell;
use device::{back, shade};
use device::shade::UniformValue;
use device::{RawBufferHandle, TextureHandle, SamplerHandle};
use device::{Resources, RawBufferHandle, TextureHandle, SamplerHandle};

pub use device::shade::{Stage, CreateShaderError};

Expand Down Expand Up @@ -131,11 +131,11 @@ pub struct NamedCell<T> {
}

/// A dictionary of parameters, meant to be shared between different programs
pub struct ParamDictionary {
pub struct ParamDictionary<R: Resources> {
/// Uniform dictionary
pub uniforms: Vec<NamedCell<shade::UniformValue>>,
/// Block dictionary
pub blocks: Vec<NamedCell<RawBufferHandle<back::GlResources>>>,
pub blocks: Vec<NamedCell<RawBufferHandle<R>>>,
/// Texture dictionary
pub textures: Vec<NamedCell<TextureParam>>,
}
Expand All @@ -147,10 +147,10 @@ pub struct ParamDictionaryLink {
textures: Vec<usize>,
}

impl ShaderParam for ParamDictionary {
impl ShaderParam for ParamDictionary<back::GlResources> {
type Link = ParamDictionaryLink;

fn create_link(this: Option<&ParamDictionary>, info: &shade::ProgramInfo)
fn create_link(this: Option<&ParamDictionary<back::GlResources>>, info: &shade::ProgramInfo)
-> Result<ParamDictionaryLink, ParameterError> {
let this = match this {
Some(d) => d,
Expand Down