1use crate::prelude::*;
2use crate::vk;
3use crate::RawPtr;
4use crate::{Device, Instance};
5use std::ffi::CStr;
6use std::mem;
7use std::ptr;
8
9#[derive(Clone)]
11pub struct ShaderObject {
12 handle: vk::Device,
13 fp: vk::ExtShaderObjectFn,
14}
15
16impl ShaderObject {
17 pub fn new(instance: &Instance, device: &Device) -> Self {
18 let handle = device.handle();
19 let fp = vk::ExtShaderObjectFn::load(|name| unsafe {
20 mem::transmute(instance.get_device_proc_addr(handle, name.as_ptr()))
21 });
22 Self { handle, fp }
23 }
24
25 #[inline]
27 pub unsafe fn create_shaders(
28 &self,
29 create_infos: &[vk::ShaderCreateInfoEXT],
30 allocator: Option<&vk::AllocationCallbacks>,
31 ) -> VkResult<Vec<vk::ShaderEXT>> {
32 let mut shaders = Vec::with_capacity(create_infos.len());
33 (self.fp.create_shaders_ext)(
34 self.handle,
35 create_infos.len() as u32,
36 create_infos.as_ptr(),
37 allocator.as_raw_ptr(),
38 shaders.as_mut_ptr(),
39 )
40 .result()?;
41 shaders.set_len(create_infos.len());
42 Ok(shaders)
43 }
44
45 #[inline]
47 pub unsafe fn destroy_shader(
48 &self,
49 shader: vk::ShaderEXT,
50 allocator: Option<&vk::AllocationCallbacks>,
51 ) {
52 (self.fp.destroy_shader_ext)(self.handle, shader, allocator.as_raw_ptr())
53 }
54
55 #[inline]
57 pub unsafe fn get_shader_binary_data(&self, shader: vk::ShaderEXT) -> VkResult<Vec<u8>> {
58 read_into_uninitialized_vector(|count, data: *mut u8| {
59 (self.fp.get_shader_binary_data_ext)(self.handle, shader, count, data.cast())
60 })
61 }
62
63 #[inline]
65 pub unsafe fn cmd_bind_shaders(
66 &self,
67 command_buffer: vk::CommandBuffer,
68 stages: &[vk::ShaderStageFlags],
69 shaders: &[vk::ShaderEXT],
70 ) {
71 assert_eq!(stages.len(), shaders.len());
72 (self.fp.cmd_bind_shaders_ext)(
73 command_buffer,
74 stages.len() as u32,
75 stages.as_ptr(),
76 shaders.as_ptr(),
77 )
78 }
79
80 #[inline]
82 pub unsafe fn cmd_set_vertex_input(
83 &self,
84 command_buffer: vk::CommandBuffer,
85 vertex_binding_descriptions: &[vk::VertexInputBindingDescription2EXT],
86 vertex_attribute_descriptions: &[vk::VertexInputAttributeDescription2EXT],
87 ) {
88 (self.fp.cmd_set_vertex_input_ext)(
89 command_buffer,
90 vertex_binding_descriptions.len() as u32,
91 vertex_binding_descriptions.as_ptr(),
92 vertex_attribute_descriptions.len() as u32,
93 vertex_attribute_descriptions.as_ptr(),
94 )
95 }
96
97 #[inline]
101 pub unsafe fn cmd_set_cull_mode(
102 &self,
103 command_buffer: vk::CommandBuffer,
104 cull_mode: vk::CullModeFlags,
105 ) {
106 (self.fp.cmd_set_cull_mode_ext)(command_buffer, cull_mode)
107 }
108
109 #[inline]
111 pub unsafe fn cmd_set_front_face(
112 &self,
113 command_buffer: vk::CommandBuffer,
114 front_face: vk::FrontFace,
115 ) {
116 (self.fp.cmd_set_front_face_ext)(command_buffer, front_face)
117 }
118
119 #[inline]
121 pub unsafe fn cmd_set_primitive_topology(
122 &self,
123 command_buffer: vk::CommandBuffer,
124 primitive_topology: vk::PrimitiveTopology,
125 ) {
126 (self.fp.cmd_set_primitive_topology_ext)(command_buffer, primitive_topology)
127 }
128
129 #[inline]
131 pub unsafe fn cmd_set_viewport_with_count(
132 &self,
133 command_buffer: vk::CommandBuffer,
134 viewports: &[vk::Viewport],
135 ) {
136 (self.fp.cmd_set_viewport_with_count_ext)(
137 command_buffer,
138 viewports.len() as u32,
139 viewports.as_ptr(),
140 )
141 }
142
143 #[inline]
145 pub unsafe fn cmd_set_scissor_with_count(
146 &self,
147 command_buffer: vk::CommandBuffer,
148 scissors: &[vk::Rect2D],
149 ) {
150 (self.fp.cmd_set_scissor_with_count_ext)(
151 command_buffer,
152 scissors.len() as u32,
153 scissors.as_ptr(),
154 )
155 }
156
157 #[inline]
159 pub unsafe fn cmd_bind_vertex_buffers2(
160 &self,
161 command_buffer: vk::CommandBuffer,
162 first_binding: u32,
163 buffers: &[vk::Buffer],
164 offsets: &[vk::DeviceSize],
165 sizes: Option<&[vk::DeviceSize]>,
166 strides: Option<&[vk::DeviceSize]>,
167 ) {
168 assert_eq!(offsets.len(), buffers.len());
169 let p_sizes = if let Some(sizes) = sizes {
170 assert_eq!(sizes.len(), buffers.len());
171 sizes.as_ptr()
172 } else {
173 ptr::null()
174 };
175 let p_strides = if let Some(strides) = strides {
176 assert_eq!(strides.len(), buffers.len());
177 strides.as_ptr()
178 } else {
179 ptr::null()
180 };
181 (self.fp.cmd_bind_vertex_buffers2_ext)(
182 command_buffer,
183 first_binding,
184 buffers.len() as u32,
185 buffers.as_ptr(),
186 offsets.as_ptr(),
187 p_sizes,
188 p_strides,
189 )
190 }
191
192 #[inline]
194 pub unsafe fn cmd_set_depth_test_enable(
195 &self,
196 command_buffer: vk::CommandBuffer,
197 depth_test_enable: bool,
198 ) {
199 (self.fp.cmd_set_depth_test_enable_ext)(command_buffer, depth_test_enable.into())
200 }
201
202 #[inline]
204 pub unsafe fn cmd_set_depth_write_enable(
205 &self,
206 command_buffer: vk::CommandBuffer,
207 depth_write_enable: bool,
208 ) {
209 (self.fp.cmd_set_depth_write_enable_ext)(command_buffer, depth_write_enable.into())
210 }
211
212 #[inline]
214 pub unsafe fn cmd_set_depth_compare_op(
215 &self,
216 command_buffer: vk::CommandBuffer,
217 depth_compare_op: vk::CompareOp,
218 ) {
219 (self.fp.cmd_set_depth_compare_op_ext)(command_buffer, depth_compare_op)
220 }
221
222 #[inline]
224 pub unsafe fn cmd_set_depth_bounds_test_enable(
225 &self,
226 command_buffer: vk::CommandBuffer,
227 depth_bounds_test_enable: bool,
228 ) {
229 (self.fp.cmd_set_depth_bounds_test_enable_ext)(
230 command_buffer,
231 depth_bounds_test_enable.into(),
232 )
233 }
234
235 #[inline]
237 pub unsafe fn cmd_set_stencil_test_enable(
238 &self,
239 command_buffer: vk::CommandBuffer,
240 stencil_test_enable: bool,
241 ) {
242 (self.fp.cmd_set_stencil_test_enable_ext)(command_buffer, stencil_test_enable.into())
243 }
244
245 #[inline]
247 pub unsafe fn cmd_set_stencil_op(
248 &self,
249 command_buffer: vk::CommandBuffer,
250 face_mask: vk::StencilFaceFlags,
251 fail_op: vk::StencilOp,
252 pass_op: vk::StencilOp,
253 depth_fail_op: vk::StencilOp,
254 compare_op: vk::CompareOp,
255 ) {
256 (self.fp.cmd_set_stencil_op_ext)(
257 command_buffer,
258 face_mask,
259 fail_op,
260 pass_op,
261 depth_fail_op,
262 compare_op,
263 )
264 }
265
266 #[inline]
270 pub unsafe fn cmd_set_patch_control_points(
271 &self,
272 command_buffer: vk::CommandBuffer,
273 patch_control_points: u32,
274 ) {
275 (self.fp.cmd_set_patch_control_points_ext)(command_buffer, patch_control_points)
276 }
277
278 #[inline]
280 pub unsafe fn cmd_set_rasterizer_discard_enable(
281 &self,
282 command_buffer: vk::CommandBuffer,
283 rasterizer_discard_enable: bool,
284 ) {
285 (self.fp.cmd_set_rasterizer_discard_enable_ext)(
286 command_buffer,
287 rasterizer_discard_enable.into(),
288 )
289 }
290
291 #[inline]
293 pub unsafe fn cmd_set_depth_bias_enable(
294 &self,
295 command_buffer: vk::CommandBuffer,
296 depth_bias_enable: bool,
297 ) {
298 (self.fp.cmd_set_depth_bias_enable_ext)(command_buffer, depth_bias_enable.into())
299 }
300
301 #[inline]
303 pub unsafe fn cmd_set_logic_op(
304 &self,
305 command_buffer: vk::CommandBuffer,
306 logic_op: vk::LogicOp,
307 ) {
308 (self.fp.cmd_set_logic_op_ext)(command_buffer, logic_op)
309 }
310
311 #[inline]
313 pub unsafe fn cmd_set_primitive_restart_enable(
314 &self,
315 command_buffer: vk::CommandBuffer,
316 primitive_restart_enable: bool,
317 ) {
318 (self.fp.cmd_set_primitive_restart_enable_ext)(
319 command_buffer,
320 primitive_restart_enable.into(),
321 )
322 }
323
324 #[inline]
328 pub unsafe fn cmd_set_tessellation_domain_origin(
329 &self,
330 command_buffer: vk::CommandBuffer,
331 domain_origin: vk::TessellationDomainOrigin,
332 ) {
333 (self.fp.cmd_set_tessellation_domain_origin_ext)(command_buffer, domain_origin)
334 }
335
336 #[inline]
338 pub unsafe fn cmd_set_depth_clamp_enable(
339 &self,
340 command_buffer: vk::CommandBuffer,
341 depth_clamp_enable: bool,
342 ) {
343 (self.fp.cmd_set_depth_clamp_enable_ext)(command_buffer, depth_clamp_enable.into())
344 }
345
346 #[inline]
348 pub unsafe fn cmd_set_polygon_mode(
349 &self,
350 command_buffer: vk::CommandBuffer,
351 polygon_mode: vk::PolygonMode,
352 ) {
353 (self.fp.cmd_set_polygon_mode_ext)(command_buffer, polygon_mode)
354 }
355
356 #[inline]
358 pub unsafe fn cmd_set_rasterization_samples(
359 &self,
360 command_buffer: vk::CommandBuffer,
361 rasterization_samples: vk::SampleCountFlags,
362 ) {
363 (self.fp.cmd_set_rasterization_samples_ext)(command_buffer, rasterization_samples)
364 }
365
366 #[inline]
368 pub unsafe fn cmd_set_sample_mask(
369 &self,
370 command_buffer: vk::CommandBuffer,
371 samples: vk::SampleCountFlags,
372 sample_mask: &[vk::SampleMask],
373 ) {
374 assert!(
375 samples.as_raw().is_power_of_two(),
376 "Only one SampleCount bit must be set"
377 );
378 assert_eq!(samples.as_raw() as usize / 32, sample_mask.len());
379 (self.fp.cmd_set_sample_mask_ext)(command_buffer, samples, sample_mask.as_ptr())
380 }
381
382 #[inline]
384 pub unsafe fn cmd_set_alpha_to_coverage_enable(
385 &self,
386 command_buffer: vk::CommandBuffer,
387 alpha_to_coverage_enable: bool,
388 ) {
389 (self.fp.cmd_set_alpha_to_coverage_enable_ext)(
390 command_buffer,
391 alpha_to_coverage_enable.into(),
392 )
393 }
394
395 #[inline]
397 pub unsafe fn cmd_set_alpha_to_one_enable(
398 &self,
399 command_buffer: vk::CommandBuffer,
400 alpha_to_one_enable: bool,
401 ) {
402 (self.fp.cmd_set_alpha_to_one_enable_ext)(command_buffer, alpha_to_one_enable.into())
403 }
404
405 #[inline]
407 pub unsafe fn cmd_set_logic_op_enable(
408 &self,
409 command_buffer: vk::CommandBuffer,
410 logic_op_enable: bool,
411 ) {
412 (self.fp.cmd_set_logic_op_enable_ext)(command_buffer, logic_op_enable.into())
413 }
414
415 #[inline]
417 pub unsafe fn cmd_set_color_blend_enable(
418 &self,
419 command_buffer: vk::CommandBuffer,
420 first_attachment: u32,
421 color_blend_enables: &[vk::Bool32],
422 ) {
423 (self.fp.cmd_set_color_blend_enable_ext)(
424 command_buffer,
425 first_attachment,
426 color_blend_enables.len() as u32,
427 color_blend_enables.as_ptr(),
428 )
429 }
430
431 #[inline]
433 pub unsafe fn cmd_set_color_blend_equation(
434 &self,
435 command_buffer: vk::CommandBuffer,
436 first_attachment: u32,
437 color_blend_equations: &[vk::ColorBlendEquationEXT],
438 ) {
439 (self.fp.cmd_set_color_blend_equation_ext)(
440 command_buffer,
441 first_attachment,
442 color_blend_equations.len() as u32,
443 color_blend_equations.as_ptr(),
444 )
445 }
446
447 #[inline]
449 pub unsafe fn cmd_set_color_write_mask(
450 &self,
451 command_buffer: vk::CommandBuffer,
452 first_attachment: u32,
453 color_write_masks: &[vk::ColorComponentFlags],
454 ) {
455 (self.fp.cmd_set_color_write_mask_ext)(
456 command_buffer,
457 first_attachment,
458 color_write_masks.len() as u32,
459 color_write_masks.as_ptr(),
460 )
461 }
462
463 #[inline]
465 pub unsafe fn cmd_set_rasterization_stream(
466 &self,
467 command_buffer: vk::CommandBuffer,
468 rasterization_stream: u32,
469 ) {
470 (self.fp.cmd_set_rasterization_stream_ext)(command_buffer, rasterization_stream)
471 }
472
473 #[inline]
475 pub unsafe fn cmd_set_conservative_rasterization_mode(
476 &self,
477 command_buffer: vk::CommandBuffer,
478 conservative_rasterization_mode: vk::ConservativeRasterizationModeEXT,
479 ) {
480 (self.fp.cmd_set_conservative_rasterization_mode_ext)(
481 command_buffer,
482 conservative_rasterization_mode,
483 )
484 }
485
486 #[inline]
488 pub unsafe fn cmd_set_extra_primitive_overestimation_size(
489 &self,
490 command_buffer: vk::CommandBuffer,
491 extra_primitive_overestimation_size: f32,
492 ) {
493 (self.fp.cmd_set_extra_primitive_overestimation_size_ext)(
494 command_buffer,
495 extra_primitive_overestimation_size,
496 )
497 }
498
499 #[inline]
501 pub unsafe fn cmd_set_depth_clip_enable(
502 &self,
503 command_buffer: vk::CommandBuffer,
504 depth_clip_enable: bool,
505 ) {
506 (self.fp.cmd_set_depth_clip_enable_ext)(command_buffer, depth_clip_enable.into())
507 }
508
509 #[inline]
511 pub unsafe fn cmd_set_sample_locations_enable(
512 &self,
513 command_buffer: vk::CommandBuffer,
514 sample_locations_enable: bool,
515 ) {
516 (self.fp.cmd_set_sample_locations_enable_ext)(
517 command_buffer,
518 sample_locations_enable.into(),
519 )
520 }
521
522 #[inline]
524 pub unsafe fn cmd_set_color_blend_advanced(
525 &self,
526 command_buffer: vk::CommandBuffer,
527 first_attachment: u32,
528 color_blend_advanced: &[vk::ColorBlendAdvancedEXT],
529 ) {
530 (self.fp.cmd_set_color_blend_advanced_ext)(
531 command_buffer,
532 first_attachment,
533 color_blend_advanced.len() as u32,
534 color_blend_advanced.as_ptr(),
535 )
536 }
537
538 #[inline]
540 pub unsafe fn cmd_set_provoking_vertex_mode(
541 &self,
542 command_buffer: vk::CommandBuffer,
543 provoking_vertex_mode: vk::ProvokingVertexModeEXT,
544 ) {
545 (self.fp.cmd_set_provoking_vertex_mode_ext)(command_buffer, provoking_vertex_mode)
546 }
547
548 #[inline]
550 pub unsafe fn cmd_set_line_rasterization_mode(
551 &self,
552 command_buffer: vk::CommandBuffer,
553 line_rasterization_mode: vk::LineRasterizationModeEXT,
554 ) {
555 (self.fp.cmd_set_line_rasterization_mode_ext)(command_buffer, line_rasterization_mode)
556 }
557
558 #[inline]
560 pub unsafe fn cmd_set_line_stipple_enable(
561 &self,
562 command_buffer: vk::CommandBuffer,
563 stippled_line_enable: bool,
564 ) {
565 (self.fp.cmd_set_line_stipple_enable_ext)(command_buffer, stippled_line_enable.into())
566 }
567
568 #[inline]
570 pub unsafe fn cmd_set_depth_clip_negative_one_to_one(
571 &self,
572 command_buffer: vk::CommandBuffer,
573 negative_one_to_one: bool,
574 ) {
575 (self.fp.cmd_set_depth_clip_negative_one_to_one_ext)(
576 command_buffer,
577 negative_one_to_one.into(),
578 )
579 }
580
581 #[inline]
583 pub unsafe fn cmd_set_viewport_w_scaling_enable_nv(
584 &self,
585 command_buffer: vk::CommandBuffer,
586 viewport_w_scaling_enable: bool,
587 ) {
588 (self.fp.cmd_set_viewport_w_scaling_enable_nv)(
589 command_buffer,
590 viewport_w_scaling_enable.into(),
591 )
592 }
593
594 #[inline]
596 pub unsafe fn cmd_set_viewport_swizzle_nv(
597 &self,
598 command_buffer: vk::CommandBuffer,
599 first_attachment: u32,
600 viewport_swizzles: &[vk::ViewportSwizzleNV],
601 ) {
602 (self.fp.cmd_set_viewport_swizzle_nv)(
603 command_buffer,
604 first_attachment,
605 viewport_swizzles.len() as u32,
606 viewport_swizzles.as_ptr(),
607 )
608 }
609
610 #[inline]
612 pub unsafe fn cmd_set_coverage_to_color_enable_nv(
613 &self,
614 command_buffer: vk::CommandBuffer,
615 coverage_to_color_enable: bool,
616 ) {
617 (self.fp.cmd_set_coverage_to_color_enable_nv)(
618 command_buffer,
619 coverage_to_color_enable.into(),
620 )
621 }
622
623 #[inline]
625 pub unsafe fn cmd_set_coverage_to_color_location_nv(
626 &self,
627 command_buffer: vk::CommandBuffer,
628 coverage_to_color_location: u32,
629 ) {
630 (self.fp.cmd_set_coverage_to_color_location_nv)(command_buffer, coverage_to_color_location)
631 }
632
633 #[inline]
635 pub unsafe fn cmd_set_coverage_modulation_mode_nv(
636 &self,
637 command_buffer: vk::CommandBuffer,
638 coverage_modulation_mode: vk::CoverageModulationModeNV,
639 ) {
640 (self.fp.cmd_set_coverage_modulation_mode_nv)(command_buffer, coverage_modulation_mode)
641 }
642
643 #[inline]
645 pub unsafe fn cmd_set_coverage_modulation_table_enable_nv(
646 &self,
647 command_buffer: vk::CommandBuffer,
648 coverage_modulation_table_enable: bool,
649 ) {
650 (self.fp.cmd_set_coverage_modulation_table_enable_nv)(
651 command_buffer,
652 coverage_modulation_table_enable.into(),
653 )
654 }
655
656 #[inline]
658 pub unsafe fn cmd_set_coverage_modulation_table_nv(
659 &self,
660 command_buffer: vk::CommandBuffer,
661 coverage_modulation_table: &[f32],
662 ) {
663 (self.fp.cmd_set_coverage_modulation_table_nv)(
664 command_buffer,
665 coverage_modulation_table.len() as u32,
666 coverage_modulation_table.as_ptr(),
667 )
668 }
669
670 #[inline]
672 pub unsafe fn cmd_set_shading_rate_image_enable_nv(
673 &self,
674 command_buffer: vk::CommandBuffer,
675 shading_rate_image_enable: bool,
676 ) {
677 (self.fp.cmd_set_shading_rate_image_enable_nv)(
678 command_buffer,
679 shading_rate_image_enable.into(),
680 )
681 }
682
683 #[inline]
685 pub unsafe fn cmd_set_representative_fragment_test_enable_nv(
686 &self,
687 command_buffer: vk::CommandBuffer,
688 representative_fragment_test_enable: bool,
689 ) {
690 (self.fp.cmd_set_representative_fragment_test_enable_nv)(
691 command_buffer,
692 representative_fragment_test_enable.into(),
693 )
694 }
695
696 #[inline]
698 pub unsafe fn cmd_set_coverage_reduction_mode_nv(
699 &self,
700 command_buffer: vk::CommandBuffer,
701 coverage_reduction_mode: vk::CoverageReductionModeNV,
702 ) {
703 (self.fp.cmd_set_coverage_reduction_mode_nv)(command_buffer, coverage_reduction_mode)
704 }
705
706 pub const fn name() -> &'static CStr {
707 vk::ExtShaderObjectFn::name()
708 }
709
710 #[inline]
711 pub fn fp(&self) -> &vk::ExtShaderObjectFn {
712 &self.fp
713 }
714
715 #[inline]
716 pub fn device(&self) -> vk::Device {
717 self.handle
718 }
719}