1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use std::sync::Arc;
use std::marker::PhantomData;
use smallvec::SmallVec;
use ::{VdResult, Device, PipelineLayoutHandle, PipelineHandle, RenderPassHandle,
    Handle, GraphicsPipelineCreateInfo};


#[derive(Debug)]
struct Inner {
    handle: PipelineHandle,
    device: Device,
}

impl Drop for Inner {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_pipeline(self.handle, None);
        }
    }
}


/// A graphics pipeline.
///
///
/// ### Destruction
/// 
/// Dropping this `GraphicsPipeline` will cause `Device::destroy_pipeline` to be called, 
/// automatically releasing any resources associated with it.
///
#[derive(Debug, Clone)]
pub struct GraphicsPipeline {
    inner: Arc<Inner>,
}

impl GraphicsPipeline {
    /// Returns a new `GraphicsPipelineBuilder`.
    pub fn builder<'b>() -> GraphicsPipelineBuilder<'b> {
        GraphicsPipelineBuilder::new()
    }

    /// Creates several graphics pipelines at once.
    #[deprecated(note = "use `Device::create_graphics_pipelines`")]
    pub fn create<'b, Gpb>(device: &Device, builders: &[Gpb])
            -> VdResult<SmallVec<[GraphicsPipeline; 8]>>
            where Gpb: AsRef<::GraphicsPipelineCreateInfo<'b>> {
        let mut create_infos = SmallVec::<[GraphicsPipelineCreateInfo; 8]>::new();
        let mut pipelines = SmallVec::<[GraphicsPipeline; 8]>::new();
        create_infos.reserve_exact(builders.len());
        pipelines.reserve_exact(builders.len());

        for builder in builders {
            create_infos.push(builder.as_ref().clone());
        }

        let pipeline_handles = unsafe { device.create_graphics_pipelines(None, &create_infos, None)? };

        for handle in pipeline_handles {
            pipelines.push(
                GraphicsPipeline {
                    inner: Arc::new(Inner {
                        handle,
                        device: device.clone(),
                    })
                }
            );
        }

        Ok(pipelines)
    }

    /// Returns this object's handle.
    pub fn handle(&self) -> PipelineHandle {
        self.inner.handle
    }

    /// Returns a reference to the associated device.
    pub fn device(&self) -> &Device {
        &self.inner.device
    }
}

unsafe impl<'g> Handle for &'g GraphicsPipeline {
    type Target = PipelineHandle;

    fn handle(&self) -> Self::Target {
        self.inner.handle
    }
}


/// A builder for `GraphicsPipeline`.
#[derive(Debug, Clone)]
#[repr(C)]
pub struct GraphicsPipelineBuilder<'b> {
    create_info: GraphicsPipelineCreateInfo<'b>,
    _p: PhantomData<&'b ()>,
}

impl<'b> GraphicsPipelineBuilder<'b> {
    /// Returns a new graphics pipeline builder.
    pub fn new() -> GraphicsPipelineBuilder<'b> {
        GraphicsPipelineBuilder {
            create_info: GraphicsPipelineCreateInfo::default(),
            _p: PhantomData,
        }
    }

    /// Specifies how the pipeline will be generated.
    pub fn flags<'s>(&'s mut self, flags: ::PipelineCreateFlags)
            -> &'s mut GraphicsPipelineBuilder<'b> {
        self.create_info.set_flags(flags);
        self
    }

    /// Specifies the number of entries in the pStages array. `stages` is a
    /// list of  structures describing the set of the shader stages to be
    /// included in the graphics pipeline.
    pub fn stages<'s, 'p>(&'s mut self,
            stages: &'p [::PipelineShaderStageCreateInfo])
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        // self.create_info.stageCount(stages.len() as u32);
        self.create_info.set_stages(stages);
        self
    }

    /// Specifies the vertex input state details.
    pub fn vertex_input_state<'s, 'p>(&'s mut self,
            vertex_input_state: &'p ::PipelineVertexInputStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_vertex_input_state(vertex_input_state);
        self
    }

    /// Specifies the input assembly behavior, as described in Drawing
    /// Commands.
    pub fn input_assembly_state<'s, 'p>(&'s mut self, input_assembly_state:
            &'p ::PipelineInputAssemblyStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_input_assembly_state(input_assembly_state);
        self
    }

    /// Specifies the tessellation state and is ignored if the pipeline does
    /// not include a tessellation control shader stage and tessellation
    /// evaluation shader stage.
    pub fn tessellation_state<'s, 'p>(&'s mut self,
            tessellation_state: &'p ::PipelineTessellationStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_tessellation_state(tessellation_state);
        self
    }

    /// Specifies the viewport state and is ignored if the pipeline has
    /// rasterization disabled.
    pub fn viewport_state<'s, 'p>(&'s mut self,
            viewport_state: &'p ::PipelineViewportStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_viewport_state(viewport_state);
        self
    }

    /// Specifies the rasterization state.
    pub fn rasterization_state<'s, 'p>(&'s mut self,
            rasterization_state: &'p ::PipelineRasterizationStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_rasterization_state(rasterization_state);
        self
    }

    /// Specifies the multisample state and is ignored if the pipeline has
    /// rasterization disabled.
    pub fn multisample_state<'s, 'p>(&'s mut self,
            multisample_state: &'p ::PipelineMultisampleStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_multisample_state(multisample_state);
        self
    }

    /// Specifies the depth stencil state and is ignored if the pipeline has
    /// rasterization disabled or if the subpass of the render pass the
    /// pipeline is created against does not use a depth/stencil attachment.
    pub fn depth_stencil_state<'s, 'p>(&'s mut self,
            depth_stencil_state: &'p ::PipelineDepthStencilStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_depth_stencil_state(depth_stencil_state);
        self
    }

    /// Specifies the color blend state and is ignored if the pipeline has
    /// rasterization disabled or if the subpass of the render pass the
    /// pipeline is created against does not use any color attachments.
    pub fn color_blend_state<'s, 'p>(&'s mut self,
            color_blend_state: &'p ::PipelineColorBlendStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_color_blend_state(color_blend_state);
        self
    }

    /// Specifies which properties of the pipeline state object are dynamic
    /// and can be changed independently of the pipeline state. If not
    /// specified, no state in the pipeline is considered dynamic.
    pub fn dynamic_state<'s, 'p>(&'s mut self,
            dynamic_state: &'p ::PipelineDynamicStateCreateInfo)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where 'p: 'b {
        self.create_info.set_dynamic_state(dynamic_state);
        self
    }

    /// Specifies the binding locations used by both the pipeline and
    /// descriptor sets used with the pipeline.
    pub fn layout<'s, H>(&'s mut self, layout: H) -> &'s mut GraphicsPipelineBuilder<'b>
            where H: Handle<Target=PipelineLayoutHandle> {
        self.create_info.set_layout(layout);
        self
    }

    /// Specifies the environment in which the pipeline will be used; the
    /// pipeline must only be used with an instance of any render pass
    /// compatible with the one provided.
    pub fn render_pass<'s, H>(&'s mut self, render_pass: H)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where H: Handle<Target=RenderPassHandle> {
        self.create_info.set_render_pass(render_pass);
        self
    }

    /// Specifies the index of the subpass in the render pass where this
    /// pipeline will be used.
    pub fn subpass<'s>(&'s mut self, subpass: u32)
            -> &'s mut GraphicsPipelineBuilder<'b> {
        self.create_info.set_subpass(subpass);
        self
    }

    /// Specifies the pipeline to derive from.
    pub fn base_pipeline<'s, H>(&'s mut self, base_pipeline: H)
            -> &'s mut GraphicsPipelineBuilder<'b>
            where H: Handle<Target=PipelineHandle> {
        self.create_info.set_base_pipeline_handle(base_pipeline);
        self
    }

    /// Specifies the index into the pCreateInfos parameter to use as a
    /// pipeline to derive from.
    pub fn base_pipeline_index<'s>(&'s mut self, base_pipeline_index: i32)
            -> &'s mut GraphicsPipelineBuilder<'b> {
        self.create_info.set_base_pipeline_index(base_pipeline_index);
        self
    }

    /// Creates and returns a new `GraphicsPipeline`. Use
    /// `GraphicsPipeline::create` to create multiple pipelines in one call.
    pub fn build(&self, device: Device) -> VdResult<GraphicsPipeline> {
        let handle = unsafe {
            let create_infos = ::std::slice::from_raw_parts(&self.create_info, 1);
            *device.create_graphics_pipelines(None, create_infos, None)?.get_unchecked(0)
        };

        Ok(GraphicsPipeline {
            inner: Arc::new(Inner {
                handle,
                device,
            })
        })
    }
}

impl<'b> AsRef<GraphicsPipelineBuilder<'b>> for GraphicsPipelineBuilder<'b> {
    fn as_ref(&self) -> &GraphicsPipelineBuilder<'b> {
        self
    }
}

impl<'b> AsRef<::GraphicsPipelineCreateInfo<'b>> for GraphicsPipelineBuilder<'b> {
    fn as_ref(&self) -> &::GraphicsPipelineCreateInfo<'b> {
        &self.create_info
    }
}