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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Render pipeline management.
use crate::backend::{GpuBackend, PipelineHandle};
use crate::device::Device;
use crate::shader::ShaderModule;
use crate::types::*;
use anyhow::Result;
use std::sync::{Arc, Mutex};
/// Description for creating a render pipeline.
///
/// # Format Matching
///
/// **Important**: `target_format` must match the format of the render target
/// you will render to. Mismatched formats cause undefined behavior or errors.
///
/// - For scheme-leased render targets: use the format passed to [`crate::Scheme::lease_render_target`]
///
/// # Example
///
/// ```rust,no_run
/// # use goldy::{RenderPipelineDesc, TextureFormat};
/// let desc = RenderPipelineDesc {
/// target_format: TextureFormat::Rgba8Unorm,
/// ..Default::default()
/// };
/// ```
#[derive(Clone, Default, Debug)]
pub struct RenderPipelineDesc {
/// Vertex buffer layout.
pub vertex_layout: VertexBufferLayout,
/// Primitive topology.
pub topology: PrimitiveTopology,
/// Target texture format.
///
/// **Must match** the format of the swapchain or scheme-leased render target you render to.
/// Use the format passed to [`crate::Scheme::lease_render_target`].
pub target_format: TextureFormat,
/// Depth/stencil state (optional, None = no depth testing).
pub depth_stencil: Option<DepthStencilState>,
}
impl Default for VertexBufferLayout {
/// Returns an empty layout (no vertex attributes, stride 0).
///
/// Use this for passes whose vertex shader generates geometry from `SV_VertexID`
/// / `VertexId` without reading a vertex buffer (fullscreen quads, procedural
/// triangles, etc.). For typed vertex input use `Vertex2D::layout()` or
/// `VertexBufferLayout::from_formats::<T>(&[…])` explicitly.
fn default() -> Self {
Self {
stride: 0,
attributes: Vec::new(),
}
}
}
/// A render pipeline.
pub struct RenderPipeline {
_device: Device,
backend: Arc<Mutex<Box<dyn GpuBackend>>>,
pub(crate) handle: PipelineHandle,
/// Per push-constant resource slot (shader-signature order), the descriptor
/// access the shader signature requires. Used by [`crate::Scheme`] render-pass
/// recording to pick the correct SRV/UAV descriptor independent of graph access.
pub(crate) slot_access: Vec<Option<crate::types::ResourceAccess>>,
}
impl RenderPipeline {
/// Create a new render pipeline.
pub fn new(
device: &Device,
vertex_shader: &ShaderModule,
fragment_shader: &ShaderModule,
desc: &RenderPipelineDesc,
) -> Result<Self> {
tracing::debug!(
target_format = ?desc.target_format,
topology = ?desc.topology,
has_depth = desc.depth_stencil.is_some(),
"Creating render pipeline"
);
let mut backend = device.inner.backend.lock().unwrap();
let handle = if desc.depth_stencil.is_some() {
backend.create_pipeline_with_depth(
device.inner.handle,
vertex_shader.handle,
fragment_shader.handle,
&desc.vertex_layout,
desc.topology,
desc.target_format,
desc.depth_stencil.as_ref(),
)?
} else {
backend.create_pipeline(
device.inner.handle,
vertex_shader.handle,
fragment_shader.handle,
&desc.vertex_layout,
desc.topology,
desc.target_format,
)?
};
tracing::debug!("Render pipeline created");
let slot_access = backend.render_pipeline_slot_access(handle);
Ok(Self {
_device: device.clone(),
backend: Arc::clone(&device.inner.backend),
handle,
slot_access,
})
}
}
impl Drop for RenderPipeline {
fn drop(&mut self) {
tracing::trace!("Destroying render pipeline");
let mut backend = self.backend.lock().unwrap();
backend.destroy_pipeline(self.handle);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::backend::mock::MockBackend;
fn create_test_device() -> Device {
Device::from_backend(Box::new(MockBackend::new())).unwrap()
}
fn create_test_shader(device: &Device) -> ShaderModule {
// Mock backend doesn't actually compile shaders, so any source works
ShaderModule::from_slang(device, "mock shader source").unwrap()
}
#[test]
fn test_render_pipeline_desc_default() {
let desc = RenderPipelineDesc::default();
assert_eq!(desc.target_format, TextureFormat::Rgba8Unorm);
assert!(desc.depth_stencil.is_none());
}
#[test]
fn test_render_pipeline_desc_debug() {
let desc = RenderPipelineDesc {
target_format: TextureFormat::Rgba8Unorm,
..Default::default()
};
let debug_str = format!("{:?}", desc);
assert!(debug_str.contains("RenderPipelineDesc"));
assert!(debug_str.contains("Rgba8Unorm"));
}
#[test]
fn test_simple_pipeline_creation() {
let device = create_test_device();
let shader = create_test_shader(&device);
let pipeline = RenderPipeline::new(&device, &shader, &shader, &RenderPipelineDesc::default()).unwrap();
assert!(pipeline.handle > 0);
}
#[test]
fn test_pipeline_with_custom_format() {
let device = create_test_device();
let shader = create_test_shader(&device);
let pipeline = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
target_format: TextureFormat::Bgra8UnormSrgb,
..Default::default()
},
)
.unwrap();
assert!(pipeline.handle > 0);
}
#[test]
fn test_pipeline_with_topology() {
let device = create_test_device();
let shader = create_test_shader(&device);
let pipeline = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
topology: PrimitiveTopology::LineList,
..Default::default()
},
)
.unwrap();
assert!(pipeline.handle > 0);
}
#[test]
fn test_pipeline_with_depth_stencil() {
let device = create_test_device();
let shader = create_test_shader(&device);
let pipeline = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
depth_stencil: Some(DepthStencilState {
format: DepthFormat::Depth24Plus,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
}),
..Default::default()
},
)
.unwrap();
assert!(pipeline.handle > 0);
}
#[test]
fn test_pipeline_with_vertex_layout() {
use crate::types::Vertex2D;
let device = create_test_device();
let shader = create_test_shader(&device);
// Test with Vertex2D layout
let pipeline_2d = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
vertex_layout: Vertex2D::layout(),
..Default::default()
},
)
.unwrap();
assert!(pipeline_2d.handle > 0);
}
#[test]
fn test_pipeline_different_vertex_fragment_shaders() {
let device = create_test_device();
// In real use, these would be different shaders
let vertex_shader = create_test_shader(&device);
let fragment_shader = create_test_shader(&device);
let pipeline = RenderPipeline::new(
&device,
&vertex_shader,
&fragment_shader,
&RenderPipelineDesc::default(),
)
.unwrap();
assert!(pipeline.handle > 0);
}
#[test]
fn test_multiple_pipelines() {
let device = create_test_device();
let shader = create_test_shader(&device);
// Create multiple pipelines with different configurations
let pipeline1 = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
target_format: TextureFormat::Rgba8Unorm,
..Default::default()
},
)
.unwrap();
let pipeline2 = RenderPipeline::new(
&device,
&shader,
&shader,
&RenderPipelineDesc {
target_format: TextureFormat::Bgra8UnormSrgb,
..Default::default()
},
)
.unwrap();
// Pipelines should have different handles
assert_ne!(pipeline1.handle, pipeline2.handle);
}
}