Skip to content

Commit

Permalink
Auto merge of #1249 - ebkalderon:pso_derives, r=kvark
Browse files Browse the repository at this point in the history
Derive convenience traits for PSO components and a few other types

### Changed

* Derived `Clone`, `Copy`, `Debug`, `Eq`, `Hash`, and/or `PartialEq` for a bunch of structs, mostly within the `gfx::pso::*` module.
* Updated the `gfx_pipeline!` macro to automatically derive `Clone`, `Debug`, and `PartialEq` for the resulting `Data`, `Meta`, and `Init` structs.
* Updated the `gfx_impl_struct_meta!` macro to automatically derive `Clone`, `Copy`, `Debug`, and `PartialEq` for the resulting struct.
* Minor reformatting.

### Fixed

* Eliminated TODOs for manually implementing `Eq`, `Hash`, and `PartialEq` on several structs which contain `PhantomData<T>` as data members.

CC @kvark @msiglreith
  • Loading branch information
homu committed May 4, 2017
2 parents 22b0d06 + b31869a commit 162221c
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "src/lib.rs"
[dependencies]
bitflags = "0.8"
cgmath = { version = "0.14", optional = true }
derivative = "1.0"
draw_state = "0.7"
log = "0.3"
serde = { version = "1.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl From<u32> for ClearColor {
}

/// Informations about what is accessed by a bunch of commands.
#[derive(Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AccessInfo<R: Resources> {
mapped_reads: HashSet<handle::RawBuffer<R>>,
mapped_writes: HashSet<handle::RawBuffer<R>>,
Expand Down
87 changes: 52 additions & 35 deletions src/core/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

//! Resource handles
use std::{ops, cmp, hash};
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::Arc;
use {buffer, shade, texture, Resources};
use memory::Typed;
Expand All @@ -26,24 +26,19 @@ use memory::Typed;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RawBuffer<R: Resources>(Arc<buffer::Raw<R>>);

impl<R: Resources> ops::Deref for RawBuffer<R> {
impl<R: Resources> Deref for RawBuffer<R> {
type Target = buffer::Raw<R>;
fn deref(&self) -> &Self::Target { &self.0 }
}

/// Type-safe buffer handle
#[derive(Clone, Debug)]
pub struct Buffer<R: Resources, T>(RawBuffer<R>, PhantomData<T>);

impl<R: Resources, T> cmp::PartialEq for Buffer<R, T> {
fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
}

impl<R: Resources, T> cmp::Eq for Buffer<R, T> {}

impl<R: Resources, T> hash::Hash for Buffer<R, T> {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.0.hash(state) }
}
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Buffer<R: Resources, T>(
RawBuffer<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<T>
);

impl<R: Resources, T> Typed for Buffer<R, T> {
type Raw = RawBuffer<R>;
Expand Down Expand Up @@ -74,28 +69,32 @@ pub struct Shader<R: Resources>(Arc<R::Shader>);
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Program<R: Resources>(Arc<shade::Program<R>>);

impl<R: Resources> ops::Deref for Program<R> {
impl<R: Resources> Deref for Program<R> {
type Target = shade::Program<R>;
fn deref(&self) -> &Self::Target { &self.0 }
}

/// Raw Pipeline State Handle
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RawPipelineState<R: Resources>(Arc<R::PipelineStateObject>, Program<R>);

/// Raw texture handle
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RawTexture<R: Resources>(Arc<texture::Raw<R>>);

impl<R: Resources> ops::Deref for RawTexture<R> {
impl<R: Resources> Deref for RawTexture<R> {
type Target = texture::Raw<R>;
fn deref(&self) -> &Self::Target { &self.0 }
}

/// Typed texture object
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
// TODO: manual Eq & Hash impl because PhantomData
pub struct Texture<R: Resources, S>(RawTexture<R>, PhantomData<S>);
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Texture<R: Resources, S>(
RawTexture<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<S>
);

impl<R: Resources, S> Typed for Texture<R, S> {
type Raw = RawTexture<R>;
Expand All @@ -122,9 +121,13 @@ enum ViewSource<R: Resources> {
pub struct RawShaderResourceView<R: Resources>(Arc<R::ShaderResourceView>, ViewSource<R>);

/// Type-safe Shader Resource View Handle
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
// TODO: manual Eq & Hash impl because PhantomData
pub struct ShaderResourceView<R: Resources, T>(RawShaderResourceView<R>, PhantomData<T>);
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ShaderResourceView<R: Resources, T>(
RawShaderResourceView<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<T>
);

impl<R: Resources, T> Typed for ShaderResourceView<R, T> {
type Raw = RawShaderResourceView<R>;
Expand All @@ -140,9 +143,13 @@ impl<R: Resources, T> Typed for ShaderResourceView<R, T> {
pub struct RawUnorderedAccessView<R: Resources>(Arc<R::UnorderedAccessView>, ViewSource<R>);

/// Type-safe Unordered Access View Handle
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
// TODO: manual Eq & Hash impl because PhantomData
pub struct UnorderedAccessView<R: Resources, T>(RawUnorderedAccessView<R>, PhantomData<T>);
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct UnorderedAccessView<R: Resources, T>(
RawUnorderedAccessView<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<T>
);

impl<R: Resources, T> Typed for UnorderedAccessView<R, T> {
type Raw = RawUnorderedAccessView<R>;
Expand Down Expand Up @@ -180,9 +187,13 @@ impl<R: Resources> RawDepthStencilView<R> {
}

/// Typed RTV
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
// TODO: manual Eq & Hash impl because PhantomData
pub struct RenderTargetView<R: Resources, T>(RawRenderTargetView<R>, PhantomData<T>);
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RenderTargetView<R: Resources, T>(
RawRenderTargetView<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<T>
);

impl<R: Resources, T> RenderTargetView<R, T> {
/// Get target dimensions
Expand All @@ -199,13 +210,19 @@ impl<R: Resources, T> Typed for RenderTargetView<R, T> {
}

/// Typed DSV
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
// TODO: manual Eq & Hash impl because PhantomData
pub struct DepthStencilView<R: Resources, T>(RawDepthStencilView<R>, PhantomData<T>);
#[derive(Derivative)]
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
pub struct DepthStencilView<R: Resources, T>(
RawDepthStencilView<R>,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
PhantomData<T>
);

impl<R: Resources, T> DepthStencilView<R, T> {
/// Get target dimensions
pub fn get_dimensions(&self) -> texture::Dimensions { self.raw().get_dimensions() }
pub fn get_dimensions(&self) -> texture::Dimensions {
self.raw().get_dimensions()
}
}

impl<R: Resources, T> Typed for DepthStencilView<R, T> {
Expand All @@ -219,7 +236,7 @@ impl<R: Resources, T> Typed for DepthStencilView<R, T> {

/// Sampler Handle
// TODO: Arc it all
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Sampler<R: Resources>(Arc<R::Sampler>, texture::SamplerInfo);

impl<R: Resources> Sampler<R> {
Expand All @@ -228,7 +245,7 @@ impl<R: Resources> Sampler<R> {
}

/// Fence Handle
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Fence<R: Resources>(Arc<R::Fence>);

/// Stores reference-counted resources used in a command buffer.
Expand Down
14 changes: 9 additions & 5 deletions src/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate derivative;
extern crate draw_state;
extern crate log;

Expand Down Expand Up @@ -85,10 +87,12 @@ pub type ColorSlot = u8;
pub type SamplerSlot = u8;

macro_rules! define_shaders {
($($name:ident),+) => {$(
( $($name:ident),+ ) => {
$(
#[allow(missing_docs)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct $name<R: Resources>(handle::Shader<R>);

impl<R: Resources> $name<R> {
#[allow(missing_docs)]
pub fn reference(&self, man: &mut handle::Manager<R>) -> &R::Shader {
Expand All @@ -100,7 +104,8 @@ macro_rules! define_shaders {
$name(shader)
}
}
)+}
)+
}
}

define_shaders!(VertexShader, HullShader, DomainShader, GeometryShader, PixelShader);
Expand All @@ -114,7 +119,6 @@ pub enum ShaderSet<R: Resources> {
Geometry(VertexShader<R>, GeometryShader<R>, PixelShader<R>),
/// Tessellated TODO: Tessellated, TessellatedGeometry, TransformFeedback
Tessellated(VertexShader<R>, HullShader<R>, DomainShader<R>, PixelShader<R>),

}

impl<R: Resources> ShaderSet<R> {
Expand All @@ -131,7 +135,7 @@ impl<R: Resources> ShaderSet<R> {
//TODO: use the appropriate units for max vertex count, etc
/// Features that the device supports.
#[allow(missing_docs)] // pretty self-explanatory fields!
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Capabilities {
pub max_vertex_count: usize,
Expand Down Expand Up @@ -309,7 +313,7 @@ pub trait Adapter: Sized {
}

/// Information about a backend adapater.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct AdapterInfo {
/// Adapter name
Expand Down
13 changes: 6 additions & 7 deletions src/core/src/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ use shade::Usage;
use std::error::Error;
use std::fmt;


/// Maximum number of vertex buffers used in a PSO definition.
pub const MAX_VERTEX_BUFFERS: usize = 4;

/// An offset inside a vertex buffer, in bytes.
pub type BufferOffset = usize;

/// Error types happening upon PSO creation on the device side.
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct CreationError;

impl fmt::Display for CreationError {
Expand Down Expand Up @@ -213,7 +212,7 @@ impl Descriptor {
}

/// A complete set of vertex buffers to be used for vertex import in PSO.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct VertexBufferSet<R: Resources>(
/// Array of buffer handles with offsets in them
pub [Option<(R::Buffer, BufferOffset)>; MAX_VERTEX_ATTRIBUTES]
Expand All @@ -227,19 +226,19 @@ impl<R: Resources> VertexBufferSet<R> {
}

/// A constant buffer run-time parameter for PSO.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ConstantBufferParam<R: Resources>(pub R::Buffer, pub Usage, pub ConstantBufferSlot);

/// A shader resource view (SRV) run-time parameter for PSO.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ResourceViewParam<R: Resources>(pub R::ShaderResourceView, pub Usage, pub ResourceViewSlot);

/// An unordered access view (UAV) run-time parameter for PSO.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct UnorderedViewParam<R: Resources>(pub R::UnorderedAccessView, pub Usage, pub UnorderedViewSlot);

/// A sampler run-time parameter for PSO.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SamplerParam<R: Resources>(pub R::Sampler, pub Usage, pub SamplerSlot);

/// A complete set of render targets to be used for pixel export in PSO.
Expand Down
20 changes: 10 additions & 10 deletions src/core/src/shade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl TextureType {
}

/// A type of the sampler variable.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct SamplerType(pub IsComparison, pub IsRect);

Expand Down Expand Up @@ -348,7 +348,7 @@ impl From<Stage> for Usage {
}

/// Vertex information that a shader takes as input.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct AttributeVar {
/// Name of this attribute.
Expand All @@ -363,7 +363,7 @@ pub struct AttributeVar {

/// A constant in the shader - a bit of data that doesn't vary
// between the shader execution units (vertices/pixels/etc).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ConstVar {
/// Name of this constant.
Expand All @@ -380,7 +380,7 @@ pub struct ConstVar {
}

/// A constant buffer.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ConstantBufferVar {
/// Name of this constant buffer.
Expand All @@ -396,7 +396,7 @@ pub struct ConstantBufferVar {
}

/// Texture shader parameter.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct TextureVar {
/// Name of this texture variable.
Expand All @@ -412,7 +412,7 @@ pub struct TextureVar {
}

/// Unordered access shader parameter.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct UnorderedVar {
/// Name of this unordered variable.
Expand All @@ -424,7 +424,7 @@ pub struct UnorderedVar {
}

/// Sampler shader parameter.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct SamplerVar {
/// Name of this sampler variable.
Expand All @@ -438,7 +438,7 @@ pub struct SamplerVar {
}

/// Target output variable.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct OutputVar {
/// Name of this output variable.
Expand All @@ -452,7 +452,7 @@ pub struct OutputVar {
}

/// Metadata about a program.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ProgramInfo {
/// Attributes in the program
Expand Down Expand Up @@ -514,7 +514,7 @@ impl<R: Resources + hash::Hash> hash::Hash for Program<R> {
}

/// Error type for trying to store a UniformValue in a ConstVar.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CompatibilityError {
/// Array sizes differ between the value and the var (trying to upload a vec2 as a vec4, etc)
ErrorArraySize,
Expand Down
1 change: 1 addition & 0 deletions src/render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ unstable = []

[dependencies]
cgmath = { version = "0.14", optional = true }
derivative = "1.0"
draw_state = "0.7"
gfx_core = { path = "../core", version = "0.7" }
log = "0.3"
Loading

0 comments on commit 162221c

Please sign in to comment.