Skip to content

Commit 162221c

Browse files
committed
Auto merge of #1249 - ebkalderon:pso_derives, r=kvark
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
2 parents 22b0d06 + b31869a commit 162221c

File tree

15 files changed

+187
-84
lines changed

15 files changed

+187
-84
lines changed

src/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ path = "src/lib.rs"
3030
[dependencies]
3131
bitflags = "0.8"
3232
cgmath = { version = "0.14", optional = true }
33+
derivative = "1.0"
3334
draw_state = "0.7"
3435
log = "0.3"
3536
serde = { version = "1.0", optional = true }

src/core/src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl From<u32> for ClearColor {
136136
}
137137

138138
/// Informations about what is accessed by a bunch of commands.
139-
#[derive(Debug)]
139+
#[derive(Clone, Debug, Eq, PartialEq)]
140140
pub struct AccessInfo<R: Resources> {
141141
mapped_reads: HashSet<handle::RawBuffer<R>>,
142142
mapped_writes: HashSet<handle::RawBuffer<R>>,

src/core/src/handle.rs

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

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

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

3434
/// Type-safe buffer handle
35-
#[derive(Clone, Debug)]
36-
pub struct Buffer<R: Resources, T>(RawBuffer<R>, PhantomData<T>);
37-
38-
impl<R: Resources, T> cmp::PartialEq for Buffer<R, T> {
39-
fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
40-
}
41-
42-
impl<R: Resources, T> cmp::Eq for Buffer<R, T> {}
43-
44-
impl<R: Resources, T> hash::Hash for Buffer<R, T> {
45-
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.0.hash(state) }
46-
}
35+
#[derive(Derivative)]
36+
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
37+
pub struct Buffer<R: Resources, T>(
38+
RawBuffer<R>,
39+
#[derivative(Hash = "ignore", PartialEq = "ignore")]
40+
PhantomData<T>
41+
);
4742

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

77-
impl<R: Resources> ops::Deref for Program<R> {
72+
impl<R: Resources> Deref for Program<R> {
7873
type Target = shade::Program<R>;
7974
fn deref(&self) -> &Self::Target { &self.0 }
8075
}
8176

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

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

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

9590
/// Typed texture object
96-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
97-
// TODO: manual Eq & Hash impl because PhantomData
98-
pub struct Texture<R: Resources, S>(RawTexture<R>, PhantomData<S>);
91+
#[derive(Derivative)]
92+
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
93+
pub struct Texture<R: Resources, S>(
94+
RawTexture<R>,
95+
#[derivative(Hash = "ignore", PartialEq = "ignore")]
96+
PhantomData<S>
97+
);
9998

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

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

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

142145
/// Type-safe Unordered Access View Handle
143-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
144-
// TODO: manual Eq & Hash impl because PhantomData
145-
pub struct UnorderedAccessView<R: Resources, T>(RawUnorderedAccessView<R>, PhantomData<T>);
146+
#[derive(Derivative)]
147+
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
148+
pub struct UnorderedAccessView<R: Resources, T>(
149+
RawUnorderedAccessView<R>,
150+
#[derivative(Hash = "ignore", PartialEq = "ignore")]
151+
PhantomData<T>
152+
);
146153

147154
impl<R: Resources, T> Typed for UnorderedAccessView<R, T> {
148155
type Raw = RawUnorderedAccessView<R>;
@@ -180,9 +187,13 @@ impl<R: Resources> RawDepthStencilView<R> {
180187
}
181188

182189
/// Typed RTV
183-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
184-
// TODO: manual Eq & Hash impl because PhantomData
185-
pub struct RenderTargetView<R: Resources, T>(RawRenderTargetView<R>, PhantomData<T>);
190+
#[derive(Derivative)]
191+
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
192+
pub struct RenderTargetView<R: Resources, T>(
193+
RawRenderTargetView<R>,
194+
#[derivative(Hash = "ignore", PartialEq = "ignore")]
195+
PhantomData<T>
196+
);
186197

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

201212
/// Typed DSV
202-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
203-
// TODO: manual Eq & Hash impl because PhantomData
204-
pub struct DepthStencilView<R: Resources, T>(RawDepthStencilView<R>, PhantomData<T>);
213+
#[derive(Derivative)]
214+
#[derivative(Clone, Debug, Eq, Hash, PartialEq)]
215+
pub struct DepthStencilView<R: Resources, T>(
216+
RawDepthStencilView<R>,
217+
#[derivative(Hash = "ignore", PartialEq = "ignore")]
218+
PhantomData<T>
219+
);
205220

206221
impl<R: Resources, T> DepthStencilView<R, T> {
207222
/// Get target dimensions
208-
pub fn get_dimensions(&self) -> texture::Dimensions { self.raw().get_dimensions() }
223+
pub fn get_dimensions(&self) -> texture::Dimensions {
224+
self.raw().get_dimensions()
225+
}
209226
}
210227

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

220237
/// Sampler Handle
221238
// TODO: Arc it all
222-
#[derive(Clone, Debug, Eq, PartialEq)]
239+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
223240
pub struct Sampler<R: Resources>(Arc<R::Sampler>, texture::SamplerInfo);
224241

225242
impl<R: Resources> Sampler<R> {
@@ -228,7 +245,7 @@ impl<R: Resources> Sampler<R> {
228245
}
229246

230247
/// Fence Handle
231-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
248+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
232249
pub struct Fence<R: Resources>(Arc<R::Fence>);
233250

234251
/// Stores reference-counted resources used in a command buffer.

src/core/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
2020
#[macro_use]
2121
extern crate bitflags;
22+
#[macro_use]
23+
extern crate derivative;
2224
extern crate draw_state;
2325
extern crate log;
2426

@@ -85,10 +87,12 @@ pub type ColorSlot = u8;
8587
pub type SamplerSlot = u8;
8688

8789
macro_rules! define_shaders {
88-
($($name:ident),+) => {$(
90+
( $($name:ident),+ ) => {
91+
$(
8992
#[allow(missing_docs)]
9093
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
9194
pub struct $name<R: Resources>(handle::Shader<R>);
95+
9296
impl<R: Resources> $name<R> {
9397
#[allow(missing_docs)]
9498
pub fn reference(&self, man: &mut handle::Manager<R>) -> &R::Shader {
@@ -100,7 +104,8 @@ macro_rules! define_shaders {
100104
$name(shader)
101105
}
102106
}
103-
)+}
107+
)+
108+
}
104109
}
105110

106111
define_shaders!(VertexShader, HullShader, DomainShader, GeometryShader, PixelShader);
@@ -114,7 +119,6 @@ pub enum ShaderSet<R: Resources> {
114119
Geometry(VertexShader<R>, GeometryShader<R>, PixelShader<R>),
115120
/// Tessellated TODO: Tessellated, TessellatedGeometry, TransformFeedback
116121
Tessellated(VertexShader<R>, HullShader<R>, DomainShader<R>, PixelShader<R>),
117-
118122
}
119123

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

311315
/// Information about a backend adapater.
312-
#[derive(Clone, Debug)]
316+
#[derive(Clone, Debug, Eq, PartialEq)]
313317
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
314318
pub struct AdapterInfo {
315319
/// Adapter name

src/core/src/pso.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ use shade::Usage;
2828
use std::error::Error;
2929
use std::fmt;
3030

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

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

3837
/// Error types happening upon PSO creation on the device side.
39-
#[derive(Clone, PartialEq, Debug)]
38+
#[derive(Clone, Debug, PartialEq)]
4039
pub struct CreationError;
4140

4241
impl fmt::Display for CreationError {
@@ -213,7 +212,7 @@ impl Descriptor {
213212
}
214213

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

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

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

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

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

245244
/// A complete set of render targets to be used for pixel export in PSO.

src/core/src/shade.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl TextureType {
8787
}
8888

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

@@ -348,7 +348,7 @@ impl From<Stage> for Usage {
348348
}
349349

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

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

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

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

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

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

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

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

516516
/// Error type for trying to store a UniformValue in a ConstVar.
517-
#[derive(Clone, Copy, Debug)]
517+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
518518
pub enum CompatibilityError {
519519
/// Array sizes differ between the value and the var (trying to upload a vec2 as a vec4, etc)
520520
ErrorArraySize,

src/render/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ unstable = []
3636

3737
[dependencies]
3838
cgmath = { version = "0.14", optional = true }
39+
derivative = "1.0"
3940
draw_state = "0.7"
4041
gfx_core = { path = "../core", version = "0.7" }
4142
log = "0.3"

0 commit comments

Comments
 (0)