pub mod buffer;
pub mod resource;
pub mod target;
pub mod bundle;
use std::default::Default;
use std::error::Error;
use std::fmt;
use core as c;
pub use core::pso::Descriptor;
pub use core::command::AccessInfo;
#[allow(missing_docs)]
#[derive(Clone, Debug, PartialEq)]
pub struct RawDataSet<R: c::Resources>{
pub vertex_buffers: c::pso::VertexBufferSet<R>,
pub constant_buffers: Vec<c::pso::ConstantBufferParam<R>>,
pub global_constants: Vec<(c::shade::Location, c::shade::UniformValue)>,
pub resource_views: Vec<c::pso::ResourceViewParam<R>>,
pub unordered_views: Vec<c::pso::UnorderedViewParam<R>>,
pub samplers: Vec<c::pso::SamplerParam<R>>,
pub pixel_targets: c::pso::PixelTargetSet<R>,
pub ref_values: c::state::RefValues,
pub scissor: c::target::Rect,
}
impl<R: c::Resources> RawDataSet<R> {
pub fn new() -> RawDataSet<R> {
RawDataSet {
vertex_buffers: c::pso::VertexBufferSet::new(),
constant_buffers: Vec::new(),
global_constants: Vec::new(),
resource_views: Vec::new(),
unordered_views: Vec::new(),
samplers: Vec::new(),
pixel_targets: c::pso::PixelTargetSet::new(),
ref_values: Default::default(),
scissor: c::target::Rect{x:0, y:0, w:1, h:1},
}
}
pub fn clear(&mut self) {
self.vertex_buffers = c::pso::VertexBufferSet::new();
self.constant_buffers.clear();
self.global_constants.clear();
self.resource_views.clear();
self.unordered_views.clear();
self.samplers.clear();
self.pixel_targets = c::pso::PixelTargetSet::new();
self.ref_values = Default::default();
self.scissor = c::target::Rect{x:0, y:0, w:1, h:1};
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ElementError<S> {
NotFound(S),
Offset {
name: S,
shader_offset: c::pso::ElemOffset,
code_offset: c::pso::ElemOffset
},
Format {
name: S,
shader_format: c::shade::ConstFormat,
code_format: c::shade::ConstFormat,
},
}
impl<S: fmt::Debug + fmt::Display> fmt::Display for ElementError<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ElementError::NotFound(ref s) => write!(f, "{}: {:?}", self.description(), s),
ElementError::Offset{ ref name, ref shader_offset, ref code_offset } =>
write!(f, "{}: ({:?}, {:?}, {:?})", self.description(), name, shader_offset, code_offset),
ElementError::Format{ ref name, ref shader_format, ref code_format } =>
write!(f, "{}: ({:?}, {:?}, {:?})", self.description(), name, shader_format, code_format),
}
}
}
impl<S: fmt::Debug + fmt::Display> Error for ElementError<S> {
fn description(&self) -> &str {
match *self {
ElementError::NotFound(_) => "Element not found",
ElementError::Offset{..} => "Element offset mismatch",
ElementError::Format{..} => "Element format mismatch",
}
}
}
impl<'a> From<ElementError<&'a str>> for ElementError<String> {
fn from(other: ElementError<&'a str>) -> ElementError<String> {
use self::ElementError::*;
match other {
NotFound(s) => NotFound(s.to_owned()),
Offset{ name, shader_offset, code_offset } => Offset{
name: name.to_owned(),
shader_offset: shader_offset,
code_offset: code_offset,
},
Format{ name, shader_format, code_format } => Format{
name: name.to_owned(),
shader_format: shader_format,
code_format: code_format,
},
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum InitError<S> {
VertexImport(S, Option<c::format::Format>),
ConstantBuffer(S, Option<ElementError<S>>),
GlobalConstant(S, Option<c::shade::CompatibilityError>),
ResourceView(S, Option<()>),
UnorderedView(S, Option<()>),
Sampler(S, Option<()>),
PixelExport(S, Option<c::format::Format>),
}
impl<'a> From<InitError<&'a str>> for InitError<String> {
fn from(other: InitError<&'a str>) -> InitError<String> {
use self::InitError::*;
match other {
VertexImport(s, v) => VertexImport(s.to_owned(), v),
ConstantBuffer(s, v) => ConstantBuffer(s.to_owned(), v.map(|e| e.into())),
GlobalConstant(s, v) => GlobalConstant(s.to_owned(), v),
ResourceView(s, v) => ResourceView(s.to_owned(), v),
UnorderedView(s, v) => UnorderedView(s.to_owned(), v),
Sampler(s, v) => Sampler(s.to_owned(), v),
PixelExport(s, v) => PixelExport(s.to_owned(), v),
}
}
}
impl<S: fmt::Debug + fmt::Display> fmt::Display for InitError<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::InitError::*;
let desc = self.description();
match *self {
VertexImport(ref name, format) => write!(f, "{}: ({}, {:?})", desc, name, format),
ConstantBuffer(ref name, ref opt) => write!(f, "{}: ({}, {:?})", desc, name, opt),
GlobalConstant(ref name, opt) => write!(f, "{}: ({}, {:?})", desc, name, opt),
ResourceView(ref name, opt) => write!(f, "{}: ({}, {:?})", desc, name, opt),
UnorderedView(ref name, opt) => write!(f, "{}: ({}, {:?})", desc, name, opt),
Sampler(ref name, opt) => write!(f, "{}: ({}, {:?})", desc, name, opt),
PixelExport(ref name, format) => write!(f, "{}: ({}, {:?})", desc, name, format),
}
}
}
impl<S: fmt::Debug + fmt::Display> Error for InitError<S> {
fn description(&self) -> &str {
use self::InitError::*;
match *self {
VertexImport(_, None) => "Vertex attribute not found",
VertexImport(..) => "Vertex attribute format mismatch",
ConstantBuffer(_, None) => "Constant buffer not found",
ConstantBuffer(..) => "Constant buffer element mismatch",
GlobalConstant(_, None) => "Global constant not found",
GlobalConstant(..) => "Global constant format mismatch",
ResourceView(_, None) => "Shader resource view not found",
ResourceView(..) => "Shader resource view mismatch",
UnorderedView(_, None) => "Unordered access view not found",
UnorderedView(..) => "Unordered access view mismatch",
Sampler(_, None) => "Sampler not found",
Sampler(..) => "Sampler mismatch",
PixelExport(_, None) => "Pixel target not found",
PixelExport(..) => "Pixel target mismatch",
}
}
fn cause(&self) -> Option<&dyn Error> {
if let InitError::ConstantBuffer(_, Some(ref e)) = *self {
Some(e)
} else {
None
}
}
}
pub trait PipelineInit {
type Meta;
fn link_to<'s>(&self, &mut Descriptor, &'s c::shade::ProgramInfo)
-> Result<Self::Meta, InitError<&'s str>>;
}
pub trait PipelineData<R: c::Resources> {
type Meta;
fn bake_to(&self,
&mut RawDataSet<R>,
&Self::Meta,
&mut c::handle::Manager<R>,
&mut AccessInfo<R>);
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct PipelineState<R: c::Resources, M>(c::handle::RawPipelineState<R>,
c::Primitive, M);
impl<R: c::Resources, M> PipelineState<R, M> {
pub fn new(raw: c::handle::RawPipelineState<R>, prim: c::Primitive, meta: M)
-> PipelineState<R, M> {
PipelineState(raw, prim, meta)
}
pub fn get_handle(&self) -> &c::handle::RawPipelineState<R> {
&self.0
}
pub fn get_meta(&self) -> &M {
&self.2
}
}
pub trait DataLink<'a>: Sized {
type Init: 'a;
fn new() -> Self;
fn is_active(&self) -> bool;
fn link_vertex_buffer(&mut self, _: c::pso::BufferIndex, _: &Self::Init) ->
Option<c::pso::VertexBufferDesc> { None }
fn link_input(&mut self, _: &c::shade::AttributeVar, _: &Self::Init) ->
Option<Result<c::pso::AttributeDesc, c::format::Format>> { None }
fn link_constant_buffer<'b>(&mut self, _: &'b c::shade::ConstantBufferVar, _: &Self::Init) ->
Option<Result<c::pso::ConstantBufferDesc, ElementError<&'b str>>> { None }
fn link_global_constant(&mut self, _: &c::shade::ConstVar, _: &Self::Init) ->
Option<Result<(), c::shade::CompatibilityError>> { None }
fn link_output(&mut self, _: &c::shade::OutputVar, _: &Self::Init) ->
Option<Result<c::pso::ColorTargetDesc, c::format::Format>> { None }
fn link_depth_stencil(&mut self, _: &Self::Init) ->
Option<c::pso::DepthStencilDesc> { None }
fn link_resource_view(&mut self, _: &c::shade::TextureVar, _: &Self::Init) ->
Option<Result<c::pso::ResourceViewDesc, c::format::Format>> { None }
fn link_unordered_view(&mut self, _: &c::shade::UnorderedVar, _: &Self::Init) ->
Option<Result<c::pso::UnorderedViewDesc, c::format::Format>> { None }
fn link_sampler(&mut self, _: &c::shade::SamplerVar, _: &Self::Init)
-> Option<c::pso::SamplerDesc> { None }
fn link_scissor(&mut self) -> bool { false }
}
pub trait DataBind<R: c::Resources> {
type Data;
fn bind_to(&self,
&mut RawDataSet<R>,
&Self::Data,
&mut c::handle::Manager<R>,
&mut AccessInfo<R>);
}