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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Contains implementations of the graphics domain for command buffers
use anyhow::{bail, Result};
use ash::vk;
use crate::{Allocator, BufferView, Error, GfxSupport, GraphicsCmdBuffer, ImageView};
use crate::command_buffer::IncompleteCommandBuffer;
use crate::core::device::ExtensionID;
use crate::sync::domain::ExecutionDomain;
impl<D: GfxSupport + ExecutionDomain, A: Allocator> GraphicsCmdBuffer
for IncompleteCommandBuffer<'_, D, A>
{
/// Sets the viewport and scissor regions to the entire render area. Can only be called inside a renderpass.
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn set_viewport<C: GraphicsCmdBuffer>(cmd: C) -> C {
/// // Now the current viewport and scissor cover the current attachment's entire area
/// cmd.full_viewport_scissor()
/// }
/// ```
fn full_viewport_scissor(self) -> Self {
let area = self.current_render_area;
self.viewport(vk::Viewport {
x: area.offset.x as f32,
y: area.offset.y as f32,
width: area.extent.width as f32,
height: area.extent.height as f32,
min_depth: 0.0,
max_depth: 1.0,
})
.scissor(area)
}
/// Sets the viewport. Directly translates to [`vkCmdSetViewport`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdSetViewport.html).
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn set_viewport<C: GraphicsCmdBuffer>(cmd: C) -> C {
/// cmd.viewport(vk::Viewport {
/// x: 0.0,
/// y: 0.0,
/// width: 1920.0,
/// height: 1080.0,
/// min_depth: 0.0,
/// max_depth: 1.0,
/// })
/// }
/// ```
fn viewport(self, viewport: vk::Viewport) -> Self {
unsafe {
self.device
.cmd_set_viewport(self.handle, 0, std::slice::from_ref(&viewport));
}
self
}
/// Sets the scissor region. Directly translates to [`vkCmdSetScissor`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdSetScissor.html).
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn set_scissor<C: GraphicsCmdBuffer>(cmd: C) -> C {
/// cmd.scissor(vk::Rect2D {
/// offset: Default::default(),
/// extent: vk::Extent2D {
/// width: 1920,
/// height: 1080
/// }
/// })
/// }
/// ```
fn scissor(self, scissor: vk::Rect2D) -> Self {
unsafe {
self.device
.cmd_set_scissor(self.handle, 0, std::slice::from_ref(&scissor));
}
self
}
/// Issue a drawcall. This will flush the current descriptor set state and actually bind the descriptor sets.
/// Directly translates to [`vkCmdDraw`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdDraw.html).
/// # Errors
/// * Fails if flushing the descriptor state fails.
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn draw<C: GraphicsCmdBuffer>(cmd: C, vertex_buffer: &BufferView) -> Result<C> {
/// cmd.full_viewport_scissor()
/// .bind_graphics_pipeline("my_pipeline")?
/// .bind_vertex_buffer(0, vertex_buffer)
/// .draw(6, 1, 0, 0)
/// }
/// ```
fn draw(
mut self,
vertex_count: u32,
instance_count: u32,
first_vertex: u32,
first_instance: u32,
) -> Result<Self> {
self = self.ensure_descriptor_state()?;
unsafe {
self.device.cmd_draw(
self.handle,
vertex_count,
instance_count,
first_vertex,
first_instance,
);
}
Ok(self)
}
/// Issue an indexed drawcall. This will flush the current descriptor state and actually bind the
/// descriptor sets. Directly translates to [`vkCmdDrawIndexed`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdDrawIndexed.html).
/// # Errors
/// * Fails if flushing the descriptor state fails.
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn draw_indexed<C: GraphicsCmdBuffer>(cmd: C, vertex_buffer: &BufferView, index_buffer: &BufferView) -> Result<C> {
/// cmd.full_viewport_scissor()
/// .bind_graphics_pipeline("my_pipeline")?
/// .bind_vertex_buffer(0, vertex_buffer)
/// .bind_index_buffer(index_buffer, vk::IndexType::UINT32)
/// .draw_indexed(6, 1, 0, 0, 0)
/// }
/// ```
fn draw_indexed(
mut self,
index_count: u32,
instance_count: u32,
first_index: u32,
vertex_offset: i32,
first_instance: u32,
) -> Result<Self> {
self = self.ensure_descriptor_state()?;
unsafe {
self.device.cmd_draw_indexed(
self.handle,
index_count,
instance_count,
first_index,
vertex_offset,
first_instance,
)
}
Ok(self)
}
/// Issue a `vkCmdTraceRaysKHR` command. Requires [`ExtensionID::RayTracingPipeline`] to be enabled.
fn trace_rays(mut self, width: u32, height: u32, depth: u32) -> Result<Self>
where
Self: Sized, {
self.device
.require_extension(ExtensionID::RayTracingPipeline)?;
self = self.ensure_descriptor_state()?;
let fns = self.device.raytracing_pipeline().unwrap();
let Some(regions) = self.current_sbt_regions else { bail!("called trace_rays() without a valid raytracing pipeline build"); };
unsafe {
fns.cmd_trace_rays(
self.handle,
regions.get(0).unwrap(),
regions.get(1).unwrap(),
regions.get(2).unwrap(),
regions.get(3).unwrap(),
width,
height,
depth,
)
};
Ok(self)
}
/// Bind a graphics pipeline by name.
/// # Errors
/// * Fails if the pipeline was not previously registered in the pipeline cache.
/// # Example
/// ```
/// # use phobos::*;
/// # use phobos::sync::domain::ExecutionDomain;
/// # use anyhow::Result;
/// // Assumes "my_pipeline" was previously added to the pipeline cache with `PipelineCache::create_named_pipeline()`,
/// // and that cmd was created with this cache.
/// fn compute_pipeline<C: GraphicsCmdBuffer>(cmd: C) -> Result<C> {
/// cmd.bind_graphics_pipeline("my_pipeline")
/// }
/// ```
fn bind_graphics_pipeline(mut self, name: &str) -> Result<Self> {
let Some(rendering_state) = self.current_rendering_state.clone() else { return Err(Error::NoRenderpass.into()) };
let cache = self.pipeline_cache.clone();
cache.with_pipeline(name, rendering_state, |pipeline| {
self.bind_pipeline_impl(
pipeline.handle,
pipeline.layout,
pipeline.set_layouts.clone(),
vk::PipelineBindPoint::GRAPHICS,
)
})?;
Ok(self)
}
/// Bind a ray tracing pipeline by name.
/// # Errors
/// * Fails if the pipeline was not previously registered in the pipeline cache.
fn bind_ray_tracing_pipeline(mut self, name: &str) -> Result<Self>
where
Self: Sized, {
let cache = self.pipeline_cache.clone();
cache.with_raytracing_pipeline(name, |pipeline| {
self.current_sbt_regions = Some(pipeline.shader_binding_table.regions);
self.bind_pipeline_impl(
pipeline.handle,
pipeline.layout,
pipeline.set_layouts.clone(),
vk::PipelineBindPoint::RAY_TRACING_KHR,
)
})?;
Ok(self)
}
/// Binds a vertex buffer to the specified binding point. Note that currently there is no validation as to whether this
/// binding actually exists for the given pipeline. Direct translation of [`vkCmdBindVertexBuffers`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdBindVertexBuffers.html).
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn draw<C: GraphicsCmdBuffer>(cmd: C, vertex_buffer: &BufferView) -> Result<C> {
/// cmd.bind_vertex_buffer(0, vertex_buffer)
/// .draw(6, 1, 0, 0)
/// }
/// ```
fn bind_vertex_buffer(self, binding: u32, buffer: &BufferView) -> Self
where
Self: Sized, {
let handle = unsafe { buffer.handle() };
let offset = buffer.offset();
unsafe {
self.device.cmd_bind_vertex_buffers(
self.handle,
binding,
std::slice::from_ref(&handle),
std::slice::from_ref(&offset),
)
};
self
}
/// Bind the an index buffer. The index type must match the actual type stored in the buffer.
/// Direct translation of [`vkCmdBindIndexBuffer`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdBindIndexBuffer.html)
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn draw_indexed<C: GraphicsCmdBuffer>(cmd: C, vertex_buffer: &BufferView, index_buffer: &BufferView) -> Result<C> {
/// cmd.bind_vertex_buffer(0, vertex_buffer)
/// .bind_index_buffer(index_buffer, vk::IndexType::UINT32)
/// .draw_indexed(6, 1, 0, 0, 0)
/// }
/// ```
fn bind_index_buffer(self, buffer: &BufferView, ty: vk::IndexType) -> Self
where
Self: Sized, {
unsafe {
self.device
.cmd_bind_index_buffer(self.handle, buffer.handle(), buffer.offset(), ty);
}
self
}
/// Blit a source image to a destination image, using the specified offsets into the images and a filter. Direct and thin wrapper around
/// [`vkCmdBlitImage`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdBlitImage.html)
fn blit_image(
self,
src: &ImageView,
dst: &ImageView,
src_offsets: &[vk::Offset3D; 2],
dst_offsets: &[vk::Offset3D; 2],
filter: vk::Filter,
) -> Self
where
Self: Sized, {
let blit = vk::ImageBlit {
src_subresource: vk::ImageSubresourceLayers {
aspect_mask: src.aspect(),
mip_level: src.base_level(),
base_array_layer: src.base_layer(),
layer_count: src.layer_count(),
},
src_offsets: *src_offsets,
dst_subresource: vk::ImageSubresourceLayers {
aspect_mask: dst.aspect(),
mip_level: dst.base_level(),
base_array_layer: dst.base_layer(),
layer_count: dst.layer_count(),
},
dst_offsets: *dst_offsets,
};
unsafe {
self.device.cmd_blit_image(
self.handle,
src.image(),
vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
dst.image(),
vk::ImageLayout::TRANSFER_DST_OPTIMAL,
std::slice::from_ref(&blit),
filter,
);
}
self
}
/// Set the polygon mode. Only available if `VK_EXT_extended_dynamic_state3` was enabled on device creation.
/// This extension is automatically requested when available.
/// Equivalent to [`vkCmdSetPolygonModeEXT`](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCmdSetPolygonModeEXT.html)
/// # Example
/// ```
/// # use anyhow::Result;
/// # use phobos::*;
/// # use phobos::sync::domain::*;
/// fn set_polygon_mode<C: GraphicsCmdBuffer>(cmd: C) -> Result<C> {
/// // Subsequent drawcalls will get a wireframe view.
/// cmd.set_polygon_mode(vk::PolygonMode::LINE)
/// }
/// ```
fn set_polygon_mode(self, mode: vk::PolygonMode) -> Result<Self> {
let funcs = self
.device
.dynamic_state3()
.ok_or_else::<anyhow::Error, _>(|| {
Error::ExtensionNotSupported(ExtensionID::ExtendedDynamicState3).into()
})?;
// SAFETY: Vulkan API call. This function pointer is not null because we just verified its availability.
unsafe {
funcs.cmd_set_polygon_mode(self.handle, mode);
}
Ok(self)
}
}