1use alloc::borrow::ToOwned;
2use alloc::{
3 borrow::Cow::{self, Borrowed},
4 boxed::Box,
5 string::String,
6 sync::Arc,
7 vec::Vec,
8};
9use core::{
10 error::Error,
11 fmt,
12 future::ready,
13 ops::{Deref, Range},
14 pin::Pin,
15 ptr::NonNull,
16};
17use wgc::resource::{BufferMapping, ParentDevice as _};
18use wgt::error::WebGpuError;
19
20use arrayvec::ArrayVec;
21use smallvec::SmallVec;
22use wgc::resource::BlasPrepareCompactResult;
23use wgt::WasmNotSendSync;
24
25use crate::{
26 api,
27 dispatch::{self, BlasCompactCallback},
28 BindingResource, Blas, BufferBinding, BufferDescriptor, Features, LoadOp, Operations,
29 ShaderSource, SurfaceTargetUnsafe, TextureDescriptor, Tlas, WriteOnly,
30};
31use crate::{dispatch::DispatchAdapter, util::Mutex};
32
33use wgc::error::format_error;
34
35#[derive(Clone)]
36pub struct ContextWgpuCore(Arc<wgc::instance::Instance>);
37
38impl fmt::Debug for ContextWgpuCore {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 f.debug_struct("ContextWgpuCore")
41 .field("type", &"Native")
42 .finish()
43 }
44}
45
46#[track_caller]
47#[cold]
48fn handle_error_fatal(cause: impl Error + WasmNotSendSync + 'static, operation: &'static str) -> ! {
49 panic!("Error in {operation}: {f}", f = format_error(&cause));
50}
51
52impl ContextWgpuCore {
53 pub unsafe fn from_hal_instance<A: hal::Api>(hal_instance: A::Instance) -> Self {
54 Self(wgc::instance::Instance::from_hal_instance::<A>(
55 "wgpu".to_owned(),
56 hal_instance,
57 ))
58 }
59
60 pub unsafe fn instance_as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
64 unsafe { self.0.as_hal::<A>() }
65 }
66
67 pub fn from_core_instance(core_instance: Arc<wgc::instance::Instance>) -> Self {
68 Self(core_instance)
69 }
70
71 #[cfg(wgpu_core)]
72 pub fn enumerate_adapters(&self, backends: wgt::Backends) -> Vec<Arc<wgc::instance::Adapter>> {
73 self.0
74 .enumerate_adapters(backends, false )
75 }
76
77 pub unsafe fn create_adapter_from_hal<A: hal::Api>(
78 &self,
79 hal_adapter: hal::ExposedAdapter<A>,
80 ) -> Arc<wgc::instance::Adapter> {
81 unsafe { self.0.create_adapter_from_hal(hal_adapter.into()) }
82 }
83
84 pub(crate) fn as_core(&self) -> Arc<wgc::instance::Instance> {
85 self.0.clone()
86 }
87}
88
89fn map_buffer_copy_view(
90 view: crate::TexelCopyBufferInfo<'_>,
91) -> wgt::TexelCopyBufferInfo<Arc<wgc::resource::Buffer>> {
92 wgt::TexelCopyBufferInfo {
93 buffer: view.buffer.inner.as_core().wgpu_buffer.clone(),
94 layout: view.layout,
95 }
96}
97
98fn map_texture_copy_view(
99 view: crate::TexelCopyTextureInfo<'_>,
100) -> wgt::TexelCopyTextureInfo<Arc<wgc::resource::Texture>> {
101 wgt::TexelCopyTextureInfo {
102 texture: view.texture.inner.as_core().wgpu_texture.clone(),
103 mip_level: view.mip_level,
104 origin: view.origin,
105 aspect: view.aspect,
106 }
107}
108
109#[cfg_attr(not(webgl), expect(unused))]
110fn map_texture_tagged_copy_view(
111 view: crate::CopyExternalImageDestInfo<&api::Texture>,
112) -> wgt::CopyExternalImageDestInfo<Arc<wgc::resource::Texture>> {
113 wgt::CopyExternalImageDestInfo {
114 texture: view.texture.inner.as_core().wgpu_texture.clone(),
115 mip_level: view.mip_level,
116 origin: view.origin,
117 aspect: view.aspect,
118 color_space: view.color_space,
119 premultiplied_alpha: view.premultiplied_alpha,
120 }
121}
122
123fn map_load_op<V: Copy>(load: &LoadOp<V>) -> LoadOp<Option<V>> {
124 match *load {
125 LoadOp::Clear(clear_value) => LoadOp::Clear(Some(clear_value)),
126 LoadOp::DontCare(token) => LoadOp::DontCare(token),
127 LoadOp::Load => LoadOp::Load,
128 }
129}
130
131fn map_pass_channel<V: Copy>(ops: Option<&Operations<V>>) -> wgc::command::PassChannel<Option<V>> {
132 match ops {
133 Some(&Operations { load, store }) => wgc::command::PassChannel {
134 load_op: Some(map_load_op(&load)),
135 store_op: Some(store),
136 read_only: false,
137 },
138 None => wgc::command::PassChannel {
139 load_op: None,
140 store_op: None,
141 read_only: true,
142 },
143 }
144}
145
146#[derive(Clone)]
147pub struct CoreSurface {
148 pub(crate) wgpu_surface: Arc<wgc::instance::Surface>,
149 configured_device: Arc<Mutex<Option<Arc<wgc::device::Device>>>>,
152}
153
154impl fmt::Debug for CoreSurface {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 f.debug_struct("CoreSurface")
157 .field("wgpu_surface", &Arc::as_ptr(&self.wgpu_surface))
158 .field("configured_device", &self.configured_device)
159 .finish()
160 }
161}
162
163impl CoreSurface {
164 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::Surface>> {
165 unsafe { self.wgpu_surface.clone().as_hal::<A>() }
166 }
167}
168
169#[derive(Clone)]
170pub struct CoreAdapter {
171 pub(crate) wgpu_adapter: Arc<wgc::instance::Adapter>,
172}
173
174impl fmt::Debug for CoreAdapter {
175 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176 f.debug_struct("CoreAdapter")
177 .field("wgpu_adapter", &Arc::as_ptr(&self.wgpu_adapter))
178 .finish()
179 }
180}
181
182impl CoreAdapter {
183 pub unsafe fn as_hal<A: hal::Api>(
184 &self,
185 ) -> Option<impl Deref<Target = A::Adapter> + WasmNotSendSync> {
186 unsafe { self.wgpu_adapter.clone().as_hal::<A>() }
187 }
188
189 pub unsafe fn create_device_from_hal<A: hal::Api>(
190 &self,
191 hal_device: hal::OpenDevice<A>,
192 desc: &crate::DeviceDescriptor<'_>,
193 ) -> Result<(CoreDevice, CoreQueue), crate::RequestDeviceError> {
194 let (device, queue) = unsafe {
195 self.wgpu_adapter.create_device_and_queue_from_hal(
196 hal_device.into(),
197 &desc.map_label(|l| l.map(Borrowed)),
198 )
199 }?;
200 let device = CoreDevice {
201 wgpu_device: device.clone(),
202 };
203 let queue = CoreQueue { wgpu_queue: queue };
204 Ok((device, queue))
205 }
206
207 pub(crate) fn from_core(core_adapter: Arc<wgc::instance::Adapter>) -> Self {
208 Self {
209 wgpu_adapter: core_adapter,
210 }
211 }
212
213 pub(crate) fn as_core(&self) -> Arc<wgc::instance::Adapter> {
214 self.wgpu_adapter.clone()
215 }
216}
217
218#[derive(Debug, Clone)]
219pub struct CoreDevice {
220 pub(crate) wgpu_device: Arc<wgc::device::Device>,
221}
222
223impl CoreDevice {
224 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::Device>> {
225 unsafe { self.wgpu_device.clone().as_hal::<A>() }
226 }
227
228 pub unsafe fn create_texture_from_hal<A: hal::Api>(
229 &self,
230 hal_texture: A::Texture,
231 desc: &TextureDescriptor<'_>,
232 initial_state: wgt::TextureUses,
233 cleared: bool,
234 ) -> CoreTexture {
235 let descriptor = desc.map_label_and_view_formats(|l| l.map(Borrowed), |v| v.to_vec());
236 let (wgpu_texture, error) = unsafe {
237 self.wgpu_device.create_texture_from_hal(
238 Box::new(hal_texture),
239 &descriptor,
240 initial_state,
241 cleared,
242 )
243 };
244 if let Some(cause) = error {
245 self.wgpu_device
246 .handle_error(cause, desc.label, "Device::create_texture_from_hal");
247 }
248 CoreTexture { wgpu_texture }
249 }
250
251 pub unsafe fn create_buffer_from_hal<A: hal::Api>(
258 &self,
259 hal_buffer: A::Buffer,
260 desc: &BufferDescriptor<'_>,
261 ) -> CoreBuffer {
262 let (wgpu_buffer, error) = unsafe {
263 self.wgpu_device
264 .create_buffer_from_hal(Box::new(hal_buffer), &desc.map_label(|l| l.map(Borrowed)))
265 };
266 if let Some(cause) = error {
267 self.wgpu_device
268 .handle_error(cause, desc.label, "Device::create_buffer_from_hal");
269 }
270 CoreBuffer { wgpu_buffer }
271 }
272
273 #[cfg(webgl)]
275 pub fn texture_belongs_to_device(&self, texture: &CoreTexture) -> bool {
276 use wgc::resource::ParentDevice as _;
277 texture.wgpu_texture.same_device(&self.wgpu_device).is_ok()
278 }
279
280 pub(crate) fn from_core(core_device: Arc<wgc::device::Device>) -> Self {
281 Self {
282 wgpu_device: core_device,
283 }
284 }
285
286 pub(crate) fn as_core(&self) -> Arc<wgc::device::Device> {
287 self.wgpu_device.clone()
288 }
289}
290
291#[derive(Debug, Clone)]
292pub struct CoreBuffer {
293 pub(crate) wgpu_buffer: Arc<wgc::resource::Buffer>,
294}
295
296impl CoreBuffer {
297 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::Buffer>> {
298 unsafe { self.wgpu_buffer.clone().as_hal::<A>() }
299 }
300
301 pub(crate) fn from_core(core_buffer: Arc<wgc::resource::Buffer>) -> Self {
302 Self {
303 wgpu_buffer: core_buffer,
304 }
305 }
306
307 pub(crate) fn as_core(&self) -> Arc<wgc::resource::Buffer> {
308 self.wgpu_buffer.clone()
309 }
310}
311
312#[derive(Debug, Clone)]
313pub struct CoreShaderModule {
314 pub(crate) wgpu_shader_module: Arc<wgc::pipeline::ShaderModule>,
315}
316
317impl CoreShaderModule {
318 pub(crate) fn from_core(core_shader_module: Arc<wgc::pipeline::ShaderModule>) -> Self {
319 Self {
320 wgpu_shader_module: core_shader_module,
321 }
322 }
323
324 pub(crate) fn as_core(&self) -> Arc<wgc::pipeline::ShaderModule> {
325 self.wgpu_shader_module.clone()
326 }
327}
328
329#[derive(Debug, Clone)]
330pub struct CoreBindGroupLayout {
331 pub(crate) wgpu_bind_group_layout: Arc<wgc::binding_model::BindGroupLayout>,
332}
333
334impl CoreBindGroupLayout {
335 pub(crate) fn from_core(
336 core_bind_group_layout: Arc<wgc::binding_model::BindGroupLayout>,
337 ) -> Self {
338 Self {
339 wgpu_bind_group_layout: core_bind_group_layout,
340 }
341 }
342
343 pub(crate) fn as_core(&self) -> Arc<wgc::binding_model::BindGroupLayout> {
344 self.wgpu_bind_group_layout.clone()
345 }
346}
347
348#[derive(Debug, Clone)]
349pub struct CoreBindGroup {
350 pub(crate) wgpu_bind_group: Arc<wgc::binding_model::BindGroup>,
351}
352impl CoreBindGroup {
353 pub(crate) fn from_core(core_bind_group: Arc<wgc::binding_model::BindGroup>) -> Self {
354 Self {
355 wgpu_bind_group: core_bind_group,
356 }
357 }
358
359 pub(crate) fn as_core(&self) -> Arc<wgc::binding_model::BindGroup> {
360 self.wgpu_bind_group.clone()
361 }
362}
363
364#[derive(Debug, Clone)]
365pub struct CoreTexture {
366 pub(crate) wgpu_texture: Arc<wgc::resource::Texture>,
367}
368
369impl CoreTexture {
370 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::Texture>> {
371 unsafe { self.wgpu_texture.clone().as_hal::<A>() }
372 }
373
374 pub(crate) fn from_core(core_texture: Arc<wgc::resource::Texture>) -> Self {
375 Self {
376 wgpu_texture: core_texture,
377 }
378 }
379
380 pub(crate) fn as_core(&self) -> Arc<wgc::resource::Texture> {
381 self.wgpu_texture.clone()
382 }
383}
384
385#[derive(Debug, Clone)]
386pub struct CoreTextureView {
387 pub(crate) wgpu_texture_view: Arc<wgc::resource::TextureView>,
388}
389
390impl CoreTextureView {
391 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::TextureView>> {
392 unsafe { self.wgpu_texture_view.clone().as_hal::<A>() }
393 }
394
395 pub(crate) fn from_core(core_texture_view: Arc<wgc::resource::TextureView>) -> Self {
396 Self {
397 wgpu_texture_view: core_texture_view,
398 }
399 }
400
401 pub(crate) fn as_core(&self) -> Arc<wgc::resource::TextureView> {
402 self.wgpu_texture_view.clone()
403 }
404}
405
406#[derive(Debug, Clone)]
407pub struct CoreExternalTexture {
408 pub(crate) wgpu_external_texture: Arc<wgc::resource::ExternalTexture>,
409}
410
411impl CoreExternalTexture {
412 pub(crate) fn from_core(core_external_texture: Arc<wgc::resource::ExternalTexture>) -> Self {
413 Self {
414 wgpu_external_texture: core_external_texture,
415 }
416 }
417
418 pub(crate) fn as_core(&self) -> Arc<wgc::resource::ExternalTexture> {
419 self.wgpu_external_texture.clone()
420 }
421}
422
423#[derive(Debug, Clone)]
424pub struct CoreSampler {
425 pub(crate) wgpu_sampler: Arc<wgc::resource::Sampler>,
426}
427
428impl CoreSampler {
429 pub(crate) fn from_core(core_sampler: Arc<wgc::resource::Sampler>) -> Self {
430 Self {
431 wgpu_sampler: core_sampler,
432 }
433 }
434
435 pub(crate) fn as_core(&self) -> Arc<wgc::resource::Sampler> {
436 self.wgpu_sampler.clone()
437 }
438}
439
440#[derive(Debug, Clone)]
441pub struct CoreQuerySet {
442 pub(crate) wgpu_query_set: Arc<wgc::resource::QuerySet>,
443}
444
445impl CoreQuerySet {
446 pub(crate) fn from_core(core_query_set: Arc<wgc::resource::QuerySet>) -> Self {
447 Self {
448 wgpu_query_set: core_query_set,
449 }
450 }
451
452 pub(crate) fn as_core(&self) -> Arc<wgc::resource::QuerySet> {
453 self.wgpu_query_set.clone()
454 }
455}
456
457#[derive(Debug, Clone)]
458pub struct CorePipelineLayout {
459 pub(crate) wgpu_pipeline_layout: Arc<wgc::binding_model::PipelineLayout>,
460}
461
462impl CorePipelineLayout {
463 pub(crate) fn from_core(core_pipeline_layout: Arc<wgc::binding_model::PipelineLayout>) -> Self {
464 Self {
465 wgpu_pipeline_layout: core_pipeline_layout,
466 }
467 }
468
469 pub(crate) fn as_core(&self) -> Arc<wgc::binding_model::PipelineLayout> {
470 self.wgpu_pipeline_layout.clone()
471 }
472}
473
474#[derive(Debug, Clone)]
475pub struct CorePipelineCache {
476 pub(crate) wgpu_pipeline_cache: Arc<wgc::pipeline::PipelineCache>,
477}
478
479pub struct CoreCommandBuffer {
480 pub(crate) wgpu_command_buffer: Arc<wgc::command::CommandBuffer>,
481}
482impl CoreCommandBuffer {
483 pub(crate) fn from_core(core_buffer: Arc<wgc::command::CommandBuffer>) -> Self {
484 Self {
485 wgpu_command_buffer: core_buffer,
486 }
487 }
488
489 pub(crate) fn as_core(&self) -> Arc<wgc::command::CommandBuffer> {
490 self.wgpu_command_buffer.clone()
491 }
492}
493
494impl fmt::Debug for CoreCommandBuffer {
495 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
496 f.debug_struct("CoreCommandBuffer")
497 .field(
498 "wgpu_command_buffer",
499 &Arc::as_ptr(&self.wgpu_command_buffer),
500 )
501 .finish()
502 }
503}
504
505#[derive(Debug)]
506pub struct CoreRenderBundleEncoder {
507 encoder: Box<wgc::command::RenderBundleEncoder>,
508}
509
510#[derive(Debug, Clone)]
511pub struct CoreRenderBundle {
512 pub(crate) wgpu_render_bundle: Arc<wgc::command::RenderBundle>,
513}
514
515#[derive(Clone)]
516pub struct CoreQueue {
517 pub(crate) wgpu_queue: Arc<wgc::device::queue::Queue>,
518}
519
520impl fmt::Debug for CoreQueue {
521 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
522 f.debug_struct("CoreQueue")
523 .field("wgpu_queue", &Arc::as_ptr(&self.wgpu_queue))
524 .finish()
525 }
526}
527
528impl CoreQueue {
529 pub unsafe fn as_hal<A: hal::Api>(
530 &self,
531 ) -> Option<impl Deref<Target = A::Queue> + WasmNotSendSync> {
532 unsafe { self.wgpu_queue.clone().as_hal::<A>() }
533 }
534
535 pub(crate) fn from_core(core_queue: Arc<wgc::device::queue::Queue>) -> Self {
536 Self {
537 wgpu_queue: core_queue,
538 }
539 }
540
541 pub(crate) fn as_core(&self) -> Arc<wgc::device::queue::Queue> {
542 self.wgpu_queue.clone()
543 }
544}
545
546#[derive(Debug, Clone)]
547pub struct CoreComputePipeline {
548 pub(crate) wgpu_compute_pipeline: Arc<wgc::pipeline::ComputePipeline>,
549}
550
551impl CoreComputePipeline {
552 pub(crate) fn from_core(core_compute_pipeline: Arc<wgc::pipeline::ComputePipeline>) -> Self {
553 Self {
554 wgpu_compute_pipeline: core_compute_pipeline,
555 }
556 }
557
558 pub(crate) fn as_core(&self) -> Arc<wgc::pipeline::ComputePipeline> {
559 self.wgpu_compute_pipeline.clone()
560 }
561}
562
563#[derive(Debug, Clone)]
564pub struct CoreRenderPipeline {
565 pub(crate) wgpu_render_pipeline: Arc<wgc::pipeline::RenderPipeline>,
566}
567
568impl CoreRenderPipeline {
569 pub(crate) fn from_core(core_render_pipeline: Arc<wgc::pipeline::RenderPipeline>) -> Self {
570 Self {
571 wgpu_render_pipeline: core_render_pipeline,
572 }
573 }
574
575 pub(crate) fn as_core(&self) -> Arc<wgc::pipeline::RenderPipeline> {
576 self.wgpu_render_pipeline.clone()
577 }
578}
579
580#[derive(Debug, Clone)]
581pub struct CoreRayTracingPipeline {
582 pub(crate) wgpu_ray_tracing_pipeline: Arc<wgc::ray_tracing_pipeline::RayTracingPipeline>,
583}
584
585impl CoreRayTracingPipeline {
586 pub(crate) fn from_core(
587 core_ray_tracing_pipeline: Arc<wgc::ray_tracing_pipeline::RayTracingPipeline>,
588 ) -> Self {
589 Self {
590 wgpu_ray_tracing_pipeline: core_ray_tracing_pipeline,
591 }
592 }
593
594 pub(crate) fn as_core(&self) -> Arc<wgc::ray_tracing_pipeline::RayTracingPipeline> {
595 self.wgpu_ray_tracing_pipeline.clone()
596 }
597}
598
599#[derive(Debug)]
600pub struct CoreComputePass {
601 pass: wgc::command::ComputePass,
602
603 id: crate::cmp::Identifier,
604}
605
606#[derive(Debug)]
607pub struct CoreRenderPass {
608 pass: wgc::command::RenderPass,
609
610 id: crate::cmp::Identifier,
611}
612
613#[derive(Debug)]
614pub struct CoreRayTracingPass {
615 pass: wgc::command::RayTracingPass,
616
617 id: crate::cmp::Identifier,
618}
619
620pub struct CoreCommandEncoder {
621 pub(crate) wgpu_command_encoder: Arc<wgc::command::CommandEncoder>,
622}
623
624impl fmt::Debug for CoreCommandEncoder {
625 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
626 f.debug_struct("CoreCommandEncoder")
627 .field(
628 "wgpu_command_encoder",
629 &Arc::as_ptr(&self.wgpu_command_encoder),
630 )
631 .finish()
632 }
633}
634
635impl CoreCommandEncoder {
636 pub unsafe fn as_hal_mut<A: hal::Api, F: FnOnce(Option<&mut A::CommandEncoder>) -> R, R>(
638 &self,
639 hal_command_encoder_callback: F,
640 ) -> R {
641 unsafe {
642 self.wgpu_command_encoder
643 .as_hal_mut::<A, F, R>(hal_command_encoder_callback)
644 }
645 }
646
647 pub(crate) fn from_core(core_encoder: Arc<wgc::command::CommandEncoder>) -> Self {
648 Self {
649 wgpu_command_encoder: core_encoder,
650 }
651 }
652
653 pub(crate) fn as_core(&self) -> Arc<wgc::command::CommandEncoder> {
654 self.wgpu_command_encoder.clone()
655 }
656}
657
658#[derive(Debug, Clone)]
659pub struct CoreBlas {
660 pub(crate) wgpu_blas: Arc<wgc::resource::Blas>,
661}
662
663impl CoreBlas {
664 pub unsafe fn as_hal<A: hal::Api>(
665 &self,
666 ) -> Option<impl Deref<Target = A::AccelerationStructure>> {
667 unsafe { self.wgpu_blas.clone().as_hal::<A>() }
668 }
669}
670
671#[derive(Debug, Clone)]
672pub struct CoreTlas {
673 pub(crate) wgpu_tlas: Arc<wgc::resource::Tlas>,
674}
675
676impl CoreTlas {
677 pub unsafe fn as_hal<A: hal::Api>(
678 &self,
679 ) -> Option<impl Deref<Target = A::AccelerationStructure>> {
680 unsafe { self.wgpu_tlas.clone().as_hal::<A>() }
681 }
682}
683
684#[derive(Clone)]
685pub struct CoreSurfaceOutputDetail {
686 wgpu_surface: Arc<wgc::instance::Surface>,
687 error_sink: ErrorSink,
688}
689
690impl fmt::Debug for CoreSurfaceOutputDetail {
691 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
692 f.debug_struct("CoreSurfaceOutputDetail")
693 .field("wgpu_surface", &Arc::as_ptr(&self.wgpu_surface))
694 .finish()
695 }
696}
697
698#[derive(Debug)]
699pub struct CoreQueueWriteBuffer {
700 wgpu_staging_buffer: wgc::resource::StagingBuffer,
701}
702
703#[derive(Debug)]
704pub struct CoreBufferMappedRange(BufferMapping);
705
706#[cfg(send_sync)]
707unsafe impl Send for CoreBufferMappedRange {}
708#[cfg(send_sync)]
709unsafe impl Sync for CoreBufferMappedRange {}
710
711impl Drop for CoreBufferMappedRange {
712 fn drop(&mut self) {
713 }
716}
717
718crate::cmp::impl_eq_ord_hash_arc_address!(ContextWgpuCore => .0);
719crate::cmp::impl_eq_ord_hash_arc_address!(CoreAdapter => .wgpu_adapter);
720crate::cmp::impl_eq_ord_hash_arc_address!(CoreDevice => .wgpu_device);
721crate::cmp::impl_eq_ord_hash_arc_address!(CoreQueue => .wgpu_queue);
722crate::cmp::impl_eq_ord_hash_arc_address!(CoreShaderModule => .wgpu_shader_module);
723crate::cmp::impl_eq_ord_hash_arc_address!(CoreBindGroupLayout => .wgpu_bind_group_layout);
724crate::cmp::impl_eq_ord_hash_arc_address!(CoreBindGroup => .wgpu_bind_group);
725crate::cmp::impl_eq_ord_hash_arc_address!(CoreTextureView => .wgpu_texture_view);
726crate::cmp::impl_eq_ord_hash_arc_address!(CoreSampler => .wgpu_sampler);
727crate::cmp::impl_eq_ord_hash_arc_address!(CoreBuffer => .wgpu_buffer);
728crate::cmp::impl_eq_ord_hash_arc_address!(CoreTexture => .wgpu_texture);
729crate::cmp::impl_eq_ord_hash_arc_address!(CoreExternalTexture => .wgpu_external_texture);
730crate::cmp::impl_eq_ord_hash_arc_address!(CoreBlas => .wgpu_blas);
731crate::cmp::impl_eq_ord_hash_arc_address!(CoreTlas => .wgpu_tlas);
732crate::cmp::impl_eq_ord_hash_arc_address!(CoreQuerySet => .wgpu_query_set);
733crate::cmp::impl_eq_ord_hash_arc_address!(CorePipelineLayout => .wgpu_pipeline_layout);
734crate::cmp::impl_eq_ord_hash_arc_address!(CoreRenderPipeline => .wgpu_render_pipeline);
735crate::cmp::impl_eq_ord_hash_arc_address!(CoreComputePipeline => .wgpu_compute_pipeline);
736crate::cmp::impl_eq_ord_hash_arc_address!(CoreRayTracingPipeline => .wgpu_ray_tracing_pipeline);
737crate::cmp::impl_eq_ord_hash_arc_address!(CorePipelineCache => .wgpu_pipeline_cache);
738crate::cmp::impl_eq_ord_hash_arc_address!(CoreCommandEncoder => .wgpu_command_encoder);
739crate::cmp::impl_eq_ord_hash_proxy!(CoreComputePass => .id);
740crate::cmp::impl_eq_ord_hash_proxy!(CoreRenderPass => .id);
741crate::cmp::impl_eq_ord_hash_proxy!(CoreRayTracingPass => .id);
742crate::cmp::impl_eq_ord_hash_arc_address!(CoreCommandBuffer => .wgpu_command_buffer);
743crate::cmp::impl_eq_ord_hash_box_address!(CoreRenderBundleEncoder => .encoder);
744crate::cmp::impl_eq_ord_hash_arc_address!(CoreRenderBundle => .wgpu_render_bundle);
745crate::cmp::impl_eq_ord_hash_arc_address!(CoreSurface => .wgpu_surface);
746crate::cmp::impl_eq_ord_hash_arc_address!(CoreSurfaceOutputDetail => .wgpu_surface);
747impl PartialEq for CoreQueueWriteBuffer {
748 fn eq(&self, other: &Self) -> bool {
749 unsafe { self.wgpu_staging_buffer.ptr() == other.wgpu_staging_buffer.ptr() }
750 }
751}
752impl Eq for CoreQueueWriteBuffer {}
753impl PartialOrd for CoreQueueWriteBuffer {
754 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
755 Some(self.cmp(other))
756 }
757}
758impl Ord for CoreQueueWriteBuffer {
759 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
760 unsafe {
761 self.wgpu_staging_buffer
762 .ptr()
763 .cmp(&other.wgpu_staging_buffer.ptr())
764 }
765 }
766}
767impl core::hash::Hash for CoreQueueWriteBuffer {
768 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
769 unsafe { self.wgpu_staging_buffer.ptr().hash(state) }
770 }
771}
772crate::cmp::impl_eq_ord_hash_proxy!(CoreBufferMappedRange => .0.ptr());
773
774impl dispatch::InstanceInterface for ContextWgpuCore {
775 fn new(desc: wgt::InstanceDescriptor) -> Self
776 where
777 Self: Sized,
778 {
779 Self(wgc::instance::Instance::new("wgpu", desc, None))
780 }
781
782 unsafe fn create_surface(
783 &self,
784 target: crate::api::SurfaceTargetUnsafe,
785 ) -> Result<dispatch::DispatchSurface, crate::CreateSurfaceError> {
786 let wgpu_surface = match target {
787 SurfaceTargetUnsafe::RawHandle {
788 raw_display_handle,
789 raw_window_handle,
790 } => unsafe { self.0.create_surface(raw_display_handle, raw_window_handle) },
791
792 #[cfg(all(drm, not(target_os = "netbsd")))]
793 SurfaceTargetUnsafe::Drm {
794 fd,
795 plane,
796 connector_id,
797 width,
798 height,
799 refresh_rate,
800 } => unsafe {
801 self.0
802 .create_surface_from_drm(fd, plane, connector_id, width, height, refresh_rate)
803 },
804
805 #[cfg(metal)]
806 SurfaceTargetUnsafe::CoreAnimationLayer(layer) => unsafe {
807 self.0.create_surface_metal(layer)
808 },
809
810 #[cfg(all(drm, target_os = "netbsd"))]
811 SurfaceTargetUnsafe::Drm { .. } => Err(
812 wgc::instance::CreateSurfaceError::BackendNotEnabled(wgt::Backend::Vulkan),
813 ),
814
815 #[cfg(dx12)]
816 SurfaceTargetUnsafe::CompositionVisual(visual) => unsafe {
817 self.0.create_surface_from_visual(visual)
818 },
819
820 #[cfg(dx12)]
821 SurfaceTargetUnsafe::SurfaceHandle(surface_handle) => unsafe {
822 self.0.create_surface_from_surface_handle(surface_handle)
823 },
824
825 #[cfg(dx12)]
826 SurfaceTargetUnsafe::SwapChainPanel(swap_chain_panel) => unsafe {
827 self.0
828 .create_surface_from_swap_chain_panel(swap_chain_panel)
829 },
830 }?;
831
832 Ok(CoreSurface {
833 wgpu_surface,
834 configured_device: Arc::new(Mutex::default()),
835 }
836 .into())
837 }
838
839 fn request_adapter(
840 &self,
841 options: &crate::api::RequestAdapterOptions<'_, '_>,
842 ) -> Pin<Box<dyn dispatch::RequestAdapterFuture>> {
843 let adapter = self.0.request_adapter(
844 &wgt::RequestAdapterOptions {
845 power_preference: options.power_preference,
846 force_fallback_adapter: options.force_fallback_adapter,
847 compatible_surface: options
848 .compatible_surface
849 .map(|surface| &*surface.inner.as_core().wgpu_surface),
850 apply_limit_buckets: false,
851 },
852 wgt::Backends::all(),
853 );
854 let adapter = adapter.map(|wgpu_adapter| {
855 let core = CoreAdapter { wgpu_adapter };
856 let generic: dispatch::DispatchAdapter = core.into();
857 generic
858 });
859 Box::pin(ready(adapter))
860 }
861
862 fn poll_all_devices(&self, force_wait: bool) -> bool {
863 match self.0.poll_all_devices(force_wait) {
864 Ok(all_queue_empty) => all_queue_empty,
865 Err(err) => handle_error_fatal(err, "Instance::poll_all_devices"),
866 }
867 }
868
869 #[cfg(feature = "wgsl")]
870 fn wgsl_language_features(&self) -> crate::WgslLanguageFeatures {
871 use wgc::naga::front::wgsl::ImplementedLanguageExtension;
872 ImplementedLanguageExtension::all().iter().copied().fold(
873 crate::WgslLanguageFeatures::empty(),
874 |acc, wle| {
875 acc | match wle {
876 ImplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => {
877 crate::WgslLanguageFeatures::ReadOnlyAndReadWriteStorageTextures
878 }
879 ImplementedLanguageExtension::Packed4x8IntegerDotProduct => {
880 crate::WgslLanguageFeatures::Packed4x8IntegerDotProduct
881 }
882 ImplementedLanguageExtension::PointerCompositeAccess => {
883 crate::WgslLanguageFeatures::PointerCompositeAccess
884 }
885 ImplementedLanguageExtension::ImmediateAddressSpace => {
886 crate::WgslLanguageFeatures::ImmediateAddressSpace
887 }
888 }
889 },
890 )
891 }
892
893 fn enumerate_adapters(
894 &self,
895 backends: crate::Backends,
896 ) -> Pin<Box<dyn dispatch::EnumerateAdapterFuture>> {
897 let adapters: Vec<DispatchAdapter> = self
898 .enumerate_adapters(backends)
899 .into_iter()
900 .map(|adapter| {
901 let core = crate::backend::wgpu_core::CoreAdapter {
902 wgpu_adapter: adapter,
903 };
904 core.into()
905 })
906 .collect();
907 Box::pin(ready(adapters))
908 }
909}
910
911impl dispatch::AdapterInterface for CoreAdapter {
912 fn request_device(
913 &self,
914 desc: &crate::DeviceDescriptor<'_>,
915 ) -> Pin<Box<dyn dispatch::RequestDeviceFuture>> {
916 let res = self
917 .wgpu_adapter
918 .request_device(&desc.map_label(|l| l.map(Borrowed)));
919 let (device, queue) = match res {
920 Ok(ids) => ids,
921 Err(err) => {
922 return Box::pin(ready(Err(err.into())));
923 }
924 };
925 let device = CoreDevice {
926 wgpu_device: device,
927 };
928 let queue = CoreQueue { wgpu_queue: queue };
929 Box::pin(ready(Ok((device.into(), queue.into()))))
930 }
931
932 fn is_surface_supported(&self, surface: &dispatch::DispatchSurface) -> bool {
933 let surface = surface.as_core();
934
935 self.wgpu_adapter
936 .is_surface_supported(&surface.wgpu_surface)
937 }
938
939 fn features(&self) -> crate::Features {
940 self.wgpu_adapter.features()
941 }
942
943 fn limits(&self) -> crate::Limits {
944 self.wgpu_adapter.limits()
945 }
946
947 fn downlevel_capabilities(&self) -> crate::DownlevelCapabilities {
948 self.wgpu_adapter.downlevel_capabilities()
949 }
950
951 fn get_info(&self) -> crate::AdapterInfo {
952 self.wgpu_adapter.get_info()
953 }
954
955 fn get_texture_format_features(
956 &self,
957 format: crate::TextureFormat,
958 ) -> crate::TextureFormatFeatures {
959 self.wgpu_adapter.get_texture_format_features(format)
960 }
961
962 fn get_presentation_timestamp(&self) -> crate::PresentationTimestamp {
963 self.wgpu_adapter.get_presentation_timestamp()
964 }
965
966 fn cooperative_matrix_properties(&self) -> Vec<crate::wgt::CooperativeMatrixProperties> {
967 self.wgpu_adapter.cooperative_matrix_properties()
968 }
969
970 fn ray_tracing_invocation_reorder_mode(&self) -> crate::wgt::RayTracingInvocationReorderMode {
971 self.wgpu_adapter.ray_tracing_invocation_reorder_mode()
972 }
973}
974
975impl Drop for CoreAdapter {
976 fn drop(&mut self) {}
977}
978
979impl dispatch::DeviceInterface for CoreDevice {
980 fn features(&self) -> crate::Features {
981 *self.wgpu_device.features()
982 }
983
984 fn limits(&self) -> crate::Limits {
985 self.wgpu_device.limits().clone()
986 }
987
988 fn is_surface_supported(&self, surface: &dispatch::DispatchSurface) -> bool {
989 self.wgpu_device
990 .adapter()
991 .is_surface_supported(&surface.as_core().wgpu_surface)
992 }
993
994 fn adapter_info(&self) -> crate::AdapterInfo {
995 self.wgpu_device.adapter_info()
996 }
997
998 fn queue(&self) -> dispatch::DispatchQueue {
999 CoreQueue {
1000 wgpu_queue: self.wgpu_device.get_queue().expect("Queue should be set"),
1001 }
1002 .into()
1003 }
1004
1005 #[cfg_attr(
1007 not(any(
1008 feature = "spirv",
1009 feature = "glsl",
1010 feature = "wgsl",
1011 feature = "naga-ir"
1012 )),
1013 expect(unused)
1014 )]
1015 fn create_shader_module(
1016 &self,
1017 desc: crate::ShaderModuleDescriptor<'_>,
1018 shader_bound_checks: wgt::ShaderRuntimeChecks,
1019 ) -> dispatch::DispatchShaderModule {
1020 let descriptor = wgc::pipeline::ShaderModuleDescriptor {
1021 label: desc.label.map(Borrowed),
1022 runtime_checks: shader_bound_checks,
1023 };
1024 let source = match desc.source {
1025 #[cfg(feature = "spirv")]
1026 ShaderSource::SpirV(ref spv) => {
1027 let options = naga::front::spv::Options {
1029 adjust_coordinate_space: false, strict_capabilities: true,
1031 block_ctx_dump_prefix: None,
1032 };
1033 wgc::pipeline::ShaderModuleSource::SpirV(Borrowed(spv), options)
1034 }
1035 #[cfg(feature = "glsl")]
1036 ShaderSource::Glsl {
1037 ref shader,
1038 stage,
1039 defines,
1040 } => {
1041 let options = naga::front::glsl::Options {
1042 stage,
1043 defines: defines
1044 .iter()
1045 .map(|&(key, value)| (String::from(key), String::from(value)))
1046 .collect(),
1047 };
1048 wgc::pipeline::ShaderModuleSource::Glsl(Borrowed(shader), options)
1049 }
1050 #[cfg(feature = "wgsl")]
1051 ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
1052 #[cfg(feature = "naga-ir")]
1053 ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
1054 ShaderSource::Dummy(_) => panic!("found `ShaderSource::Dummy`"),
1055 };
1056 let wgpu_shader_module = self.wgpu_device.create_shader_module(&descriptor, source);
1057
1058 CoreShaderModule { wgpu_shader_module }.into()
1059 }
1060
1061 unsafe fn create_shader_module_passthrough(
1062 &self,
1063 desc: &crate::ShaderModuleDescriptorPassthrough<'_>,
1064 ) -> dispatch::DispatchShaderModule {
1065 let desc = desc.map_label(|l| l.map(Cow::from));
1066 let wgpu_shader_module =
1067 unsafe { self.wgpu_device.create_shader_module_passthrough(&desc) };
1068
1069 CoreShaderModule { wgpu_shader_module }.into()
1070 }
1071
1072 fn create_bind_group_layout(
1073 &self,
1074 desc: &crate::BindGroupLayoutDescriptor<'_>,
1075 ) -> dispatch::DispatchBindGroupLayout {
1076 let descriptor = wgc::binding_model::BindGroupLayoutDescriptor {
1077 label: desc.label.map(Borrowed),
1078 entries: Borrowed(desc.entries),
1079 };
1080 let wgpu_bind_group_layout = self.wgpu_device.create_bind_group_layout(&descriptor);
1081 CoreBindGroupLayout {
1082 wgpu_bind_group_layout,
1083 }
1084 .into()
1085 }
1086
1087 fn create_bind_group(
1088 &self,
1089 desc: &crate::BindGroupDescriptor<'_>,
1090 ) -> dispatch::DispatchBindGroup {
1091 use wgc::binding_model as bm;
1092
1093 let mut arrayed_texture_views = Vec::new();
1094 let mut arrayed_samplers = Vec::new();
1095 if self
1096 .wgpu_device
1097 .features()
1098 .contains(Features::TEXTURE_BINDING_ARRAY)
1099 {
1100 for entry in desc.entries.iter() {
1102 if let BindingResource::TextureViewArray(array) = entry.resource {
1103 arrayed_texture_views.extend(
1104 array
1105 .iter()
1106 .map(|view| view.inner.as_core().wgpu_texture_view.clone()),
1107 );
1108 }
1109 if let BindingResource::SamplerArray(array) = entry.resource {
1110 arrayed_samplers.extend(
1111 array
1112 .iter()
1113 .map(|sampler| sampler.inner.as_core().wgpu_sampler.clone()),
1114 );
1115 }
1116 }
1117 }
1118 let mut remaining_arrayed_texture_views = &arrayed_texture_views[..];
1119 let mut remaining_arrayed_samplers = &arrayed_samplers[..];
1120
1121 let mut arrayed_buffer_bindings = Vec::new();
1122 if self
1123 .wgpu_device
1124 .features()
1125 .contains(Features::BUFFER_BINDING_ARRAY)
1126 {
1127 for entry in desc.entries.iter() {
1129 if let BindingResource::BufferArray(array) = entry.resource {
1130 arrayed_buffer_bindings.extend(array.iter().map(|binding| bm::BufferBinding {
1131 buffer: binding.buffer.inner.as_core().wgpu_buffer.clone(),
1132 offset: binding.offset,
1133 size: binding.size.map(wgt::BufferSize::get),
1134 }));
1135 }
1136 }
1137 }
1138 let mut remaining_arrayed_buffer_bindings = &arrayed_buffer_bindings[..];
1139
1140 let mut arrayed_acceleration_structures = Vec::new();
1141 if self
1142 .wgpu_device
1143 .features()
1144 .contains(Features::ACCELERATION_STRUCTURE_BINDING_ARRAY)
1145 {
1146 for entry in desc.entries.iter() {
1148 if let BindingResource::AccelerationStructureArray(array) = entry.resource {
1149 arrayed_acceleration_structures.extend(
1150 array
1151 .iter()
1152 .map(|tlas| tlas.inner.as_core().wgpu_tlas.clone()),
1153 );
1154 }
1155 }
1156 }
1157 let mut remaining_arrayed_acceleration_structures = &arrayed_acceleration_structures[..];
1158
1159 let entries = desc
1160 .entries
1161 .iter()
1162 .map(|entry| bm::BindGroupEntry {
1163 binding: entry.binding,
1164 resource: match entry.resource {
1165 BindingResource::Buffer(BufferBinding {
1166 buffer,
1167 offset,
1168 size,
1169 }) => bm::BindingResource::Buffer(bm::BufferBinding {
1170 buffer: buffer.inner.as_core().wgpu_buffer.clone(),
1171 offset,
1172 size: size.map(wgt::BufferSize::get),
1173 }),
1174 BindingResource::BufferArray(array) => {
1175 let slice = &remaining_arrayed_buffer_bindings[..array.len()];
1176 remaining_arrayed_buffer_bindings =
1177 &remaining_arrayed_buffer_bindings[array.len()..];
1178 bm::BindingResource::BufferArray(Borrowed(slice))
1179 }
1180 BindingResource::Sampler(sampler) => {
1181 bm::BindingResource::Sampler(sampler.inner.as_core().wgpu_sampler.clone())
1182 }
1183 BindingResource::SamplerArray(array) => {
1184 let slice = &remaining_arrayed_samplers[..array.len()];
1185 remaining_arrayed_samplers = &remaining_arrayed_samplers[array.len()..];
1186 bm::BindingResource::SamplerArray(Borrowed(slice))
1187 }
1188 BindingResource::TextureView(texture_view) => bm::BindingResource::TextureView(
1189 texture_view.inner.as_core().wgpu_texture_view.clone(),
1190 ),
1191 BindingResource::TextureViewArray(array) => {
1192 let slice = &remaining_arrayed_texture_views[..array.len()];
1193 remaining_arrayed_texture_views =
1194 &remaining_arrayed_texture_views[array.len()..];
1195 bm::BindingResource::TextureViewArray(Borrowed(slice))
1196 }
1197 BindingResource::AccelerationStructure(acceleration_structure) => {
1198 bm::BindingResource::AccelerationStructure(
1199 acceleration_structure.inner.as_core().wgpu_tlas.clone(),
1200 )
1201 }
1202 BindingResource::AccelerationStructureArray(array) => {
1203 let slice = &remaining_arrayed_acceleration_structures[..array.len()];
1204 remaining_arrayed_acceleration_structures =
1205 &remaining_arrayed_acceleration_structures[array.len()..];
1206 bm::BindingResource::AccelerationStructureArray(Borrowed(slice))
1207 }
1208 BindingResource::ExternalTexture(external_texture) => {
1209 bm::BindingResource::ExternalTexture(
1210 external_texture
1211 .inner
1212 .as_core()
1213 .wgpu_external_texture
1214 .clone(),
1215 )
1216 }
1217 },
1218 })
1219 .collect::<Vec<_>>();
1220 let descriptor = bm::BindGroupDescriptor {
1221 label: desc.label.as_ref().map(|label| Borrowed(&label[..])),
1222 layout: desc.layout.inner.as_core().wgpu_bind_group_layout.clone(),
1223 entries: Borrowed(&entries),
1224 };
1225
1226 let wgpu_bind_group = self.wgpu_device.create_bind_group(&descriptor);
1227 CoreBindGroup { wgpu_bind_group }.into()
1228 }
1229
1230 fn create_pipeline_layout(
1231 &self,
1232 desc: &crate::PipelineLayoutDescriptor<'_>,
1233 ) -> dispatch::DispatchPipelineLayout {
1234 assert!(
1237 desc.bind_group_layouts.len() <= wgc::MAX_BIND_GROUPS,
1238 "Bind group layout count {} exceeds device bind group limit {}",
1239 desc.bind_group_layouts.len(),
1240 wgc::MAX_BIND_GROUPS
1241 );
1242
1243 let temp_layouts = desc
1244 .bind_group_layouts
1245 .iter()
1246 .map(|bgl| bgl.map(|bgl| bgl.inner.as_core().wgpu_bind_group_layout.clone()))
1247 .collect::<ArrayVec<_, { wgc::MAX_BIND_GROUPS }>>();
1248 let descriptor = wgc::binding_model::PipelineLayoutDescriptor {
1249 label: desc.label.map(Borrowed),
1250 bind_group_layouts: Borrowed(&temp_layouts),
1251 immediate_size: desc.immediate_size,
1252 };
1253
1254 let wgpu_pipeline_layout = self.wgpu_device.create_pipeline_layout(&descriptor);
1255
1256 CorePipelineLayout {
1257 wgpu_pipeline_layout,
1258 }
1259 .into()
1260 }
1261
1262 fn create_render_pipeline(
1263 &self,
1264 desc: &crate::RenderPipelineDescriptor<'_>,
1265 ) -> dispatch::DispatchRenderPipeline {
1266 use wgc::pipeline as pipe;
1267
1268 let vertex_buffers: ArrayVec<_, { wgc::MAX_VERTEX_BUFFERS }> = desc
1269 .vertex
1270 .buffers
1271 .iter()
1272 .map(|vbuf| {
1273 vbuf.as_ref().map(|vbuf| pipe::VertexBufferLayout {
1274 array_stride: vbuf.array_stride,
1275 step_mode: vbuf.step_mode,
1276 attributes: Borrowed(vbuf.attributes),
1277 })
1278 })
1279 .collect();
1280
1281 let vert_constants = desc
1282 .vertex
1283 .compilation_options
1284 .constants
1285 .iter()
1286 .map(|&(key, value)| (String::from(key), value))
1287 .collect();
1288
1289 let descriptor = pipe::ResolvedGeneralRenderPipelineDescriptor {
1290 label: desc.label.map(Borrowed),
1291 layout: desc
1292 .layout
1293 .map(|layout| layout.inner.as_core().wgpu_pipeline_layout.clone()),
1294 vertex: wgc::pipeline::RenderPipelineVertexProcessor::Vertex(pipe::VertexState {
1295 stage: pipe::ProgrammableStageDescriptor {
1296 module: desc
1297 .vertex
1298 .module
1299 .inner
1300 .as_core()
1301 .wgpu_shader_module
1302 .clone(),
1303 entry_point: desc.vertex.entry_point.map(Borrowed),
1304 constants: vert_constants,
1305 zero_initialize_workgroup_memory: desc
1306 .vertex
1307 .compilation_options
1308 .zero_initialize_workgroup_memory,
1309 },
1310 buffers: Borrowed(&vertex_buffers),
1311 }),
1312 primitive: desc.primitive,
1313 depth_stencil: desc.depth_stencil.clone(),
1314 multisample: desc.multisample,
1315 fragment: desc.fragment.as_ref().map(|frag| {
1316 let frag_constants = frag
1317 .compilation_options
1318 .constants
1319 .iter()
1320 .map(|&(key, value)| (String::from(key), value))
1321 .collect();
1322 pipe::FragmentState {
1323 stage: pipe::ProgrammableStageDescriptor {
1324 module: frag.module.inner.as_core().wgpu_shader_module.clone(),
1325 entry_point: frag.entry_point.map(Borrowed),
1326 constants: frag_constants,
1327 zero_initialize_workgroup_memory: frag
1328 .compilation_options
1329 .zero_initialize_workgroup_memory,
1330 },
1331 targets: Borrowed(frag.targets),
1332 }
1333 }),
1334 multiview_mask: desc.multiview_mask,
1335 cache: desc
1336 .cache
1337 .map(|cache| cache.inner.as_core().wgpu_pipeline_cache.clone()),
1338 };
1339
1340 let wgpu_render_pipeline = self.wgpu_device.create_render_pipeline(descriptor);
1341 CoreRenderPipeline {
1342 wgpu_render_pipeline,
1343 }
1344 .into()
1345 }
1346
1347 fn create_mesh_pipeline(
1348 &self,
1349 desc: &crate::MeshPipelineDescriptor<'_>,
1350 ) -> dispatch::DispatchRenderPipeline {
1351 use wgc::pipeline as pipe;
1352
1353 let mesh_constants = desc
1354 .mesh
1355 .compilation_options
1356 .constants
1357 .iter()
1358 .map(|&(key, value)| (String::from(key), value))
1359 .collect();
1360 let descriptor = pipe::MeshPipelineDescriptor {
1361 label: desc.label.map(Borrowed),
1362 task: desc.task.as_ref().map(|task| {
1363 let task_constants = task
1364 .compilation_options
1365 .constants
1366 .iter()
1367 .map(|&(key, value)| (String::from(key), value))
1368 .collect();
1369 pipe::TaskState {
1370 stage: pipe::ProgrammableStageDescriptor {
1371 module: task.module.inner.as_core().wgpu_shader_module.clone(),
1372 entry_point: task.entry_point.map(Borrowed),
1373 constants: task_constants,
1374 zero_initialize_workgroup_memory: desc
1375 .mesh
1376 .compilation_options
1377 .zero_initialize_workgroup_memory,
1378 },
1379 }
1380 }),
1381 mesh: pipe::MeshState {
1382 stage: pipe::ProgrammableStageDescriptor {
1383 module: desc.mesh.module.inner.as_core().wgpu_shader_module.clone(),
1384 entry_point: desc.mesh.entry_point.map(Borrowed),
1385 constants: mesh_constants,
1386 zero_initialize_workgroup_memory: desc
1387 .mesh
1388 .compilation_options
1389 .zero_initialize_workgroup_memory,
1390 },
1391 },
1392 layout: desc
1393 .layout
1394 .map(|layout| layout.inner.as_core().wgpu_pipeline_layout.clone()),
1395 primitive: desc.primitive,
1396 depth_stencil: desc.depth_stencil.clone(),
1397 multisample: desc.multisample,
1398 fragment: desc.fragment.as_ref().map(|frag| {
1399 let frag_constants = frag
1400 .compilation_options
1401 .constants
1402 .iter()
1403 .map(|&(key, value)| (String::from(key), value))
1404 .collect();
1405 pipe::FragmentState {
1406 stage: pipe::ProgrammableStageDescriptor {
1407 module: frag.module.inner.as_core().wgpu_shader_module.clone(),
1408 entry_point: frag.entry_point.map(Borrowed),
1409 constants: frag_constants,
1410 zero_initialize_workgroup_memory: frag
1411 .compilation_options
1412 .zero_initialize_workgroup_memory,
1413 },
1414 targets: Borrowed(frag.targets),
1415 }
1416 }),
1417 multiview: desc.multiview,
1418 cache: desc
1419 .cache
1420 .map(|cache| cache.inner.as_core().wgpu_pipeline_cache.clone()),
1421 };
1422
1423 let wgpu_render_pipeline = self.wgpu_device.create_render_pipeline(descriptor.into());
1424 CoreRenderPipeline {
1425 wgpu_render_pipeline,
1426 }
1427 .into()
1428 }
1429
1430 fn create_compute_pipeline(
1431 &self,
1432 desc: &crate::ComputePipelineDescriptor<'_>,
1433 ) -> dispatch::DispatchComputePipeline {
1434 use wgc::pipeline as pipe;
1435
1436 let constants = desc
1437 .compilation_options
1438 .constants
1439 .iter()
1440 .map(|&(key, value)| (String::from(key), value))
1441 .collect();
1442
1443 let descriptor = pipe::ComputePipelineDescriptor {
1444 label: desc.label.map(Borrowed),
1445 layout: desc
1446 .layout
1447 .map(|pll| pll.inner.as_core().wgpu_pipeline_layout.clone()),
1448 stage: pipe::ProgrammableStageDescriptor {
1449 module: desc.module.inner.as_core().wgpu_shader_module.clone(),
1450 entry_point: desc.entry_point.map(Borrowed),
1451 constants,
1452 zero_initialize_workgroup_memory: desc
1453 .compilation_options
1454 .zero_initialize_workgroup_memory,
1455 },
1456 cache: desc
1457 .cache
1458 .map(|cache| cache.inner.as_core().wgpu_pipeline_cache.clone()),
1459 };
1460
1461 let wgpu_compute_pipeline = self.wgpu_device.create_compute_pipeline(descriptor);
1462 CoreComputePipeline {
1463 wgpu_compute_pipeline,
1464 }
1465 .into()
1466 }
1467
1468 fn create_ray_tracing_pipeline(
1469 &self,
1470 desc: &crate::RayTracingPipelineDescriptor<'_>,
1471 ) -> dispatch::DispatchRayTracingPipeline {
1472 use wgc::pipeline as pipe;
1473 use wgc::ray_tracing_pipeline as rt_pipe;
1474
1475 fn downcast_rt_stage<'a>(
1476 stage: &'a crate::RayTracingStage<'a>,
1477 ) -> pipe::ProgrammableStageDescriptor<'a> {
1478 pipe::ProgrammableStageDescriptor {
1479 module: stage.module.inner.as_core().wgpu_shader_module.clone(),
1480 entry_point: stage.entry_point.map(Borrowed),
1481 constants: stage
1482 .compilation_options
1483 .constants
1484 .iter()
1485 .map(|&(key, value)| (String::from(key), value))
1486 .collect(),
1487 zero_initialize_workgroup_memory: stage
1488 .compilation_options
1489 .zero_initialize_workgroup_memory,
1490 }
1491 }
1492
1493 let descriptor = rt_pipe::RayTracingPipelineDescriptor {
1494 label: desc.label.map(Borrowed),
1495 layout: desc
1496 .layout
1497 .map(|pll| pll.inner.as_core().wgpu_pipeline_layout.clone()),
1498 ray_generation: downcast_rt_stage(&desc.ray_generation),
1499 miss: downcast_rt_stage(&desc.miss),
1500 intersections: desc
1501 .intersections
1502 .iter()
1503 .map(|intersection_desc| match intersection_desc {
1504 crate::RayTracingIntersectionDescriptor::Triangle {
1505 closest_hit,
1506 any_hit,
1507 } => rt_pipe::RayTracingIntersectionDescriptor::Triangle {
1508 closest_hit: downcast_rt_stage(closest_hit),
1509 any_hit: any_hit.as_ref().map(|stage| downcast_rt_stage(stage)),
1510 },
1511 })
1512 .collect::<Vec<_>>(),
1513 max_recursion_depth: desc.max_recursion_depth,
1514 cache: desc
1515 .cache
1516 .map(|cache| cache.inner.as_core().wgpu_pipeline_cache.clone()),
1517 };
1518
1519 let (wgpu_ray_tracing_pipeline, error) =
1520 self.wgpu_device.create_ray_tracing_pipeline(descriptor);
1521 if let Some(cause) = error {
1522 if let pipe::CreatePipelineError::Internal { stage, ref message } = cause {
1523 log::error!(
1524 "Shader translation error for stage {:?}: {}",
1525 stage,
1526 message
1527 );
1528 log::error!("Please report it to https://github.com/gfx-rs/wgpu");
1529 }
1530 self.wgpu_device
1531 .handle_error(cause, desc.label, "Device::create_ray_tracing_pipeline");
1532 }
1533 CoreRayTracingPipeline {
1534 wgpu_ray_tracing_pipeline,
1535 }
1536 .into()
1537 }
1538
1539 unsafe fn create_pipeline_cache(
1540 &self,
1541 desc: &crate::PipelineCacheDescriptor<'_>,
1542 ) -> dispatch::DispatchPipelineCache {
1543 use wgc::pipeline as pipe;
1544
1545 let descriptor = pipe::PipelineCacheDescriptor {
1546 label: desc.label.map(Borrowed),
1547 data: desc.data.map(Borrowed),
1548 fallback: desc.fallback,
1549 };
1550 let (wgpu_pipeline_cache, error) =
1551 unsafe { self.wgpu_device.create_pipeline_cache(&descriptor) };
1552 if let Some(cause) = error {
1553 self.wgpu_device.handle_error(
1554 cause,
1555 desc.label,
1556 "Device::device_create_pipeline_cache_init",
1557 );
1558 }
1559 CorePipelineCache {
1560 wgpu_pipeline_cache,
1561 }
1562 .into()
1563 }
1564
1565 fn create_buffer(&self, desc: &crate::BufferDescriptor<'_>) -> dispatch::DispatchBuffer {
1566 let wgpu_buffer = self
1567 .wgpu_device
1568 .create_buffer(&desc.map_label(|l| l.map(Borrowed)));
1569
1570 CoreBuffer { wgpu_buffer }.into()
1571 }
1572
1573 fn create_texture(&self, desc: &crate::TextureDescriptor<'_>) -> dispatch::DispatchTexture {
1574 let wgt_desc = desc.map_label_and_view_formats(|l| l.map(Borrowed), |v| v.to_vec());
1575 let wgpu_texture = self.wgpu_device.create_texture(&wgt_desc);
1576
1577 CoreTexture { wgpu_texture }.into()
1578 }
1579
1580 fn create_external_texture(
1581 &self,
1582 desc: &crate::ExternalTextureDescriptor<'_>,
1583 planes: &[&crate::TextureView],
1584 ) -> dispatch::DispatchExternalTexture {
1585 let wgt_desc = desc.map_label(|l| l.map(Borrowed));
1586 let planes = planes
1587 .iter()
1588 .map(|plane| plane.inner.as_core().wgpu_texture_view.clone())
1589 .collect::<Vec<_>>();
1590 let wgpu_external_texture = self.wgpu_device.create_external_texture(&wgt_desc, &planes);
1591
1592 CoreExternalTexture {
1593 wgpu_external_texture,
1594 }
1595 .into()
1596 }
1597
1598 fn create_blas(
1599 &self,
1600 desc: &crate::CreateBlasDescriptor<'_>,
1601 sizes: crate::BlasGeometrySizeDescriptors,
1602 ) -> (Option<u64>, dispatch::DispatchBlas) {
1603 let (wgpu_blas, error) = self
1604 .wgpu_device
1605 .create_blas(&desc.map_label(|l| l.map(Borrowed)), sizes);
1606 if let Some(cause) = error {
1607 self.wgpu_device
1608 .handle_error(cause, desc.label, "Device::create_blas");
1609 }
1610 (wgpu_blas.handle(), CoreBlas { wgpu_blas }.into())
1611 }
1612
1613 fn create_tlas(&self, desc: &crate::CreateTlasDescriptor<'_>) -> dispatch::DispatchTlas {
1614 let (wgpu_tlas, error) = self
1615 .wgpu_device
1616 .create_tlas(&desc.map_label(|l| l.map(Borrowed)));
1617 if let Some(cause) = error {
1618 self.wgpu_device
1619 .handle_error(cause, desc.label, "Device::create_tlas");
1620 }
1621 CoreTlas { wgpu_tlas }.into()
1622 }
1623
1624 fn create_sampler(&self, desc: &crate::SamplerDescriptor<'_>) -> dispatch::DispatchSampler {
1625 let descriptor = wgc::resource::SamplerDescriptor {
1626 label: desc.label.map(Borrowed),
1627 address_modes: [
1628 desc.address_mode_u,
1629 desc.address_mode_v,
1630 desc.address_mode_w,
1631 ],
1632 mag_filter: desc.mag_filter,
1633 min_filter: desc.min_filter,
1634 mipmap_filter: desc.mipmap_filter,
1635 lod_min_clamp: desc.lod_min_clamp,
1636 lod_max_clamp: desc.lod_max_clamp,
1637 compare: desc.compare,
1638 anisotropy_clamp: desc.anisotropy_clamp,
1639 border_color: desc.border_color,
1640 };
1641
1642 let wgpu_sampler = self.wgpu_device.create_sampler(&descriptor);
1643 CoreSampler { wgpu_sampler }.into()
1644 }
1645
1646 fn create_query_set(&self, desc: &crate::QuerySetDescriptor<'_>) -> dispatch::DispatchQuerySet {
1647 let wgpu_query_set = self
1648 .wgpu_device
1649 .create_query_set(&desc.map_label(|l| l.map(Borrowed)));
1650 CoreQuerySet { wgpu_query_set }.into()
1651 }
1652
1653 fn create_command_encoder(
1654 &self,
1655 desc: &crate::CommandEncoderDescriptor<'_>,
1656 ) -> dispatch::DispatchCommandEncoder {
1657 let wgpu_command_encoder = self
1658 .wgpu_device
1659 .create_command_encoder(&desc.map_label(|l| l.map(Borrowed)));
1660
1661 CoreCommandEncoder {
1662 wgpu_command_encoder,
1663 }
1664 .into()
1665 }
1666
1667 fn create_render_bundle_encoder(
1668 &self,
1669 desc: &crate::RenderBundleEncoderDescriptor<'_>,
1670 ) -> dispatch::DispatchRenderBundleEncoder {
1671 let descriptor = wgc::command::RenderBundleEncoderDescriptor {
1672 label: desc.label.map(Borrowed),
1673 color_formats: Borrowed(desc.color_formats),
1674 depth_stencil: desc.depth_stencil,
1675 sample_count: desc.sample_count,
1676 multiview: desc.multiview,
1677 };
1678 let encoder = self.wgpu_device.create_render_bundle_encoder(&descriptor);
1679
1680 CoreRenderBundleEncoder { encoder }.into()
1681 }
1682
1683 fn set_device_lost_callback(&self, device_lost_callback: dispatch::BoxDeviceLostCallback) {
1684 self.wgpu_device
1685 .set_device_lost_closure(device_lost_callback);
1686 }
1687
1688 fn on_uncaptured_error(&self, handler: Arc<dyn crate::UncapturedErrorHandler>) {
1689 self.wgpu_device.on_uncaptured_error(handler);
1690 }
1691
1692 fn push_error_scope(&self, filter: crate::ErrorFilter) -> u32 {
1693 self.wgpu_device.push_error_scope_with_index(filter)
1694 }
1695
1696 fn pop_error_scope(&self, index: u32) -> Pin<Box<dyn dispatch::PopErrorScopeFuture>> {
1697 Box::pin(ready(self.wgpu_device.pop_error_scope_checked(index)))
1698 }
1699
1700 unsafe fn start_graphics_debugger_capture(&self) {
1701 unsafe { self.wgpu_device.start_graphics_debugger_capture() };
1702 }
1703
1704 unsafe fn stop_graphics_debugger_capture(&self) {
1705 unsafe { self.wgpu_device.stop_graphics_debugger_capture() };
1706 }
1707
1708 fn poll(&self, poll_type: wgt::PollType<u64>) -> Result<crate::PollStatus, crate::PollError> {
1709 match self.wgpu_device.poll(poll_type) {
1710 Ok(status) => Ok(status),
1711 Err(err) => {
1712 if let Some(poll_error) = err.to_poll_error() {
1713 return Err(poll_error);
1714 }
1715
1716 handle_error_fatal(err, "Device::poll")
1717 }
1718 }
1719 }
1720
1721 fn get_internal_counters(&self) -> crate::InternalCounters {
1722 self.wgpu_device.get_internal_counters()
1723 }
1724
1725 fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {
1726 self.wgpu_device.generate_allocator_report()
1727 }
1728
1729 fn destroy(&self) {
1730 self.wgpu_device.destroy();
1731 }
1732}
1733
1734impl Drop for CoreDevice {
1735 fn drop(&mut self) {}
1736}
1737
1738impl dispatch::QueueInterface for CoreQueue {
1739 fn write_buffer(
1740 &self,
1741 buffer: &dispatch::DispatchBuffer,
1742 offset: crate::BufferAddress,
1743 data: &[u8],
1744 ) {
1745 let buffer = buffer.as_core();
1746
1747 self.wgpu_queue
1748 .write_buffer(buffer.wgpu_buffer.clone(), offset, data)
1749 }
1750
1751 fn create_staging_buffer(
1752 &self,
1753 size: crate::BufferSize,
1754 ) -> Option<dispatch::DispatchQueueWriteBuffer> {
1755 match self.wgpu_queue.create_staging_buffer(size) {
1756 Ok((wgpu_staging_buffer, _)) => Some(
1757 CoreQueueWriteBuffer {
1758 wgpu_staging_buffer,
1759 }
1760 .into(),
1761 ),
1762 Err(err) => {
1763 self.wgpu_queue
1764 .device()
1765 .handle_error_nolabel(err, "Queue::write_buffer_with");
1766 None
1767 }
1768 }
1769 }
1770
1771 fn validate_write_buffer(
1772 &self,
1773 buffer: &dispatch::DispatchBuffer,
1774 offset: wgt::BufferAddress,
1775 size: wgt::BufferSize,
1776 ) -> Option<()> {
1777 let buffer = buffer.as_core();
1778
1779 match self
1780 .wgpu_queue
1781 .validate_write_buffer(buffer.wgpu_buffer.clone(), offset, size)
1782 {
1783 Ok(()) => Some(()),
1784 Err(err) => {
1785 self.wgpu_queue
1786 .device()
1787 .handle_error_nolabel(err, "Queue::write_buffer_with");
1788 None
1789 }
1790 }
1791 }
1792
1793 fn write_staging_buffer(
1794 &self,
1795 buffer: &dispatch::DispatchBuffer,
1796 offset: crate::BufferAddress,
1797 staging_buffer: dispatch::DispatchQueueWriteBuffer,
1798 ) {
1799 let buffer = buffer.as_core();
1800 let staging_buffer = {
1801 #[allow(
1802 clippy::allow_attributes,
1803 unreachable_patterns,
1804 reason = "features may be disabled"
1805 )]
1806 match staging_buffer {
1807 dispatch::DispatchQueueWriteBuffer::Core(value) => value,
1808 _ => panic!(concat!(
1809 stringify!(DispatchQueueWriteBuffer),
1810 " is not core"
1811 )),
1812 }
1813 };
1814
1815 match self.wgpu_queue.write_staging_buffer(
1816 buffer.wgpu_buffer.clone(),
1817 offset,
1818 staging_buffer.wgpu_staging_buffer,
1819 ) {
1820 Ok(()) => (),
1821 Err(err) => {
1822 self.wgpu_queue
1823 .device()
1824 .handle_error_nolabel(err, "Queue::write_buffer_with");
1825 }
1826 }
1827 }
1828
1829 fn write_texture(
1830 &self,
1831 texture: crate::TexelCopyTextureInfo<'_>,
1832 data: &[u8],
1833 data_layout: crate::TexelCopyBufferLayout,
1834 size: crate::Extent3d,
1835 ) {
1836 self.wgpu_queue
1837 .write_texture(map_texture_copy_view(texture), data, &data_layout, &size);
1838 }
1839
1840 #[cfg(web)]
1843 #[cfg_attr(not(webgl), expect(unused_variables))]
1844 fn copy_external_image_to_texture(
1845 &self,
1846 source: &crate::CopyExternalImageSourceInfo,
1847 dest: crate::CopyExternalImageDestInfo<&crate::api::Texture>,
1848 size: crate::Extent3d,
1849 ) {
1850 #[cfg(webgl)]
1851 match self.wgpu_queue.copy_external_image_to_texture(
1852 source,
1853 map_texture_tagged_copy_view(dest),
1854 size,
1855 ) {
1856 Ok(()) => (),
1857 Err(err) => self
1858 .wgpu_queue
1859 .device()
1860 .handle_error_nolabel(err, "Queue::copy_external_image_to_texture"),
1861 }
1862 }
1863
1864 fn submit(
1865 &self,
1866 command_buffers: &mut dyn Iterator<Item = dispatch::DispatchCommandBuffer>,
1867 ) -> Option<u64> {
1868 let temp_command_buffers = command_buffers.collect::<SmallVec<[_; 4]>>();
1869 let command_buffers = temp_command_buffers
1870 .iter()
1871 .map(|cmdbuf| cmdbuf.as_core().wgpu_command_buffer.clone())
1872 .collect::<SmallVec<[_; 4]>>();
1873
1874 let index = self.wgpu_queue.submit(&command_buffers);
1875
1876 drop(temp_command_buffers);
1877
1878 index
1879 }
1880
1881 fn get_timestamp_period(&self) -> f32 {
1882 self.wgpu_queue.get_timestamp_period()
1883 }
1884
1885 fn on_submitted_work_done(&self, callback: dispatch::BoxSubmittedWorkDoneCallback) {
1886 self.wgpu_queue.on_submitted_work_done(callback);
1887 }
1888
1889 fn compact_blas(&self, blas: &dispatch::DispatchBlas) -> (Option<u64>, dispatch::DispatchBlas) {
1890 let (wgpu_blas, error) = self.wgpu_queue.compact_blas(&blas.as_core().wgpu_blas);
1891
1892 if let Some(cause) = error {
1893 self.wgpu_queue
1894 .device()
1895 .handle_error_nolabel(cause, "Queue::compact_blas");
1896 }
1897 (wgpu_blas.handle(), CoreBlas { wgpu_blas }.into())
1898 }
1899
1900 fn present(&self, detail: &dispatch::DispatchSurfaceOutputDetail) {
1901 self.present_with_damage(detail, &[]);
1902 }
1903
1904 fn present_with_damage(
1905 &self,
1906 detail: &dispatch::DispatchSurfaceOutputDetail,
1907 damage_rects: &[wgt::Rect<u32>],
1908 ) {
1909 let detail = detail.as_core();
1910 match detail.wgpu_surface.present_with_damage(damage_rects) {
1911 Ok(_status) => (),
1912 Err(err) => {
1913 self.wgpu_queue
1914 .device()
1915 .handle_error_nolabel(err, "Queue::present");
1916 }
1917 }
1918 }
1919}
1920
1921impl dispatch::ShaderModuleInterface for CoreShaderModule {
1922 fn get_compilation_info(&self) -> Pin<Box<dyn dispatch::ShaderCompilationInfoFuture>> {
1923 Box::pin(ready(self.wgpu_shader_module.compilation_info().clone()))
1924 }
1925}
1926
1927impl dispatch::BindGroupLayoutInterface for CoreBindGroupLayout {}
1928
1929impl dispatch::BindGroupInterface for CoreBindGroup {}
1930
1931impl dispatch::TextureViewInterface for CoreTextureView {}
1932
1933impl dispatch::ExternalTextureInterface for CoreExternalTexture {
1934 fn destroy(&self) {
1935 self.wgpu_external_texture.destroy();
1936 }
1937}
1938
1939impl dispatch::SamplerInterface for CoreSampler {}
1940
1941impl dispatch::BufferInterface for CoreBuffer {
1942 fn map_async(
1943 &self,
1944 mode: crate::MapMode,
1945 range: Range<crate::BufferAddress>,
1946 callback: dispatch::BufferMapCallback,
1947 ) {
1948 self.wgpu_buffer.map_async(
1949 mode,
1950 range.start,
1951 Some(range.end - range.start),
1952 Some(Box::new(|status| {
1953 let res = status.map(|_| ()).map_err(|_| crate::BufferAsyncError);
1954 callback(res);
1955 })),
1956 );
1957 }
1958
1959 fn get_mapped_range(
1960 &self,
1961 sub_range: Range<crate::BufferAddress>,
1962 ) -> Result<dispatch::DispatchBufferMappedRange, crate::MapRangeError> {
1963 let size = sub_range.end - sub_range.start;
1964 self.wgpu_buffer
1965 .get_mapped_range(sub_range.start, Some(size))
1966 .map(|mapping| CoreBufferMappedRange(mapping).into())
1967 .map_err(|err| crate::MapRangeError(format_error(&err)))
1968 }
1969
1970 fn unmap(&self) {
1971 self.wgpu_buffer.unmap();
1972 }
1973
1974 fn destroy(&self) {
1975 self.wgpu_buffer.destroy();
1976 }
1977
1978 fn size(&self) -> crate::BufferAddress {
1979 self.wgpu_buffer.size()
1980 }
1981
1982 fn usage(&self) -> crate::BufferUsages {
1983 self.wgpu_buffer.usage()
1984 }
1985}
1986
1987impl dispatch::TextureInterface for CoreTexture {
1988 fn create_view(
1989 &self,
1990 desc: &crate::TextureViewDescriptor<'_>,
1991 ) -> dispatch::DispatchTextureView {
1992 let descriptor = wgc::resource::TextureViewDescriptor {
1993 label: desc.label.map(Borrowed),
1994 format: desc.format,
1995 dimension: desc.dimension,
1996 usage: desc.usage,
1997 range: wgt::ImageSubresourceRange {
1998 aspect: desc.aspect,
1999 base_mip_level: desc.base_mip_level,
2000 mip_level_count: desc.mip_level_count,
2001 base_array_layer: desc.base_array_layer,
2002 array_layer_count: desc.array_layer_count,
2003 },
2004 swizzle: desc.swizzle,
2005 };
2006 let wgpu_texture_view = self.wgpu_texture.create_view(&descriptor);
2007 CoreTextureView { wgpu_texture_view }.into()
2008 }
2009
2010 fn destroy(&self) {
2011 self.wgpu_texture.destroy();
2012 }
2013
2014 fn size(&self) -> wgt::Extent3d {
2015 self.wgpu_texture.descriptor().size
2016 }
2017
2018 fn mip_level_count(&self) -> u32 {
2019 self.wgpu_texture.descriptor().mip_level_count
2020 }
2021
2022 fn sample_count(&self) -> u32 {
2023 self.wgpu_texture.descriptor().sample_count
2024 }
2025
2026 fn dimension(&self) -> wgt::TextureDimension {
2027 self.wgpu_texture.descriptor().dimension
2028 }
2029
2030 fn format(&self) -> wgt::TextureFormat {
2031 self.wgpu_texture.descriptor().format
2032 }
2033
2034 fn usage(&self) -> wgt::TextureUsages {
2035 self.wgpu_texture.descriptor().usage
2036 }
2037
2038 unsafe fn mark_externally_initialized(&self) {
2039 unsafe { self.wgpu_texture.mark_externally_initialized() }
2040 }
2041
2042 fn texture_binding_view_dimension(&self) -> Option<crate::TextureViewDimension> {
2043 self.wgpu_texture.texture_binding_view_dimension()
2044 }
2045}
2046
2047impl dispatch::BlasInterface for CoreBlas {
2048 fn prepare_compact_async(&self, callback: BlasCompactCallback) {
2049 let callback: Option<wgc::resource::BlasCompactCallback> =
2050 Some(Box::new(|status: BlasPrepareCompactResult| {
2051 let res = status.map_err(|_| crate::BlasAsyncError);
2052 callback(res);
2053 }));
2054
2055 match self.wgpu_blas.prepare_compact_async(callback) {
2056 Ok(_) => (),
2057 Err(cause) => self
2058 .wgpu_blas
2059 .device()
2060 .handle_error_nolabel(cause, "Blas::prepare_compact_async"),
2061 }
2062 }
2063
2064 fn ready_for_compaction(&self) -> bool {
2065 match self.wgpu_blas.ready_for_compaction() {
2066 Ok(ready) => ready,
2067 Err(cause) => {
2068 self.wgpu_blas
2069 .device()
2070 .handle_error_nolabel(cause, "Blas::ready_for_compaction");
2071 false
2073 }
2074 }
2075 }
2076}
2077
2078impl dispatch::TlasInterface for CoreTlas {}
2079
2080impl dispatch::QuerySetInterface for CoreQuerySet {
2081 fn destroy(&self) {
2082 self.wgpu_query_set.destroy();
2083 }
2084
2085 fn ty(&self) -> crate::QueryType {
2086 self.wgpu_query_set.descriptor().ty
2087 }
2088
2089 fn count(&self) -> u32 {
2090 self.wgpu_query_set.descriptor().count
2091 }
2092}
2093
2094impl dispatch::PipelineLayoutInterface for CorePipelineLayout {}
2095
2096impl dispatch::RenderPipelineInterface for CoreRenderPipeline {
2097 fn get_bind_group_layout(&self, index: u32) -> dispatch::DispatchBindGroupLayout {
2098 let wgpu_bind_group_layout = self.wgpu_render_pipeline.get_bind_group_layout(index);
2099 CoreBindGroupLayout {
2100 wgpu_bind_group_layout,
2101 }
2102 .into()
2103 }
2104}
2105
2106impl dispatch::RayTracingPipelineInterface for CoreRayTracingPipeline {
2107 fn get_bind_group_layout(&self, index: u32) -> dispatch::DispatchBindGroupLayout {
2108 let (wgpu_bind_group_layout, error) =
2109 self.wgpu_ray_tracing_pipeline.get_bind_group_layout(index);
2110 if let Some(err) = error {
2111 self.wgpu_ray_tracing_pipeline
2112 .device()
2113 .handle_error_nolabel(err, "RayTracingPipeline::get_bind_group_layout")
2114 }
2115 CoreBindGroupLayout {
2116 wgpu_bind_group_layout,
2117 }
2118 .into()
2119 }
2120}
2121
2122impl dispatch::ComputePipelineInterface for CoreComputePipeline {
2123 fn get_bind_group_layout(&self, index: u32) -> dispatch::DispatchBindGroupLayout {
2124 let wgpu_bind_group_layout = self.wgpu_compute_pipeline.get_bind_group_layout(index);
2125 CoreBindGroupLayout {
2126 wgpu_bind_group_layout,
2127 }
2128 .into()
2129 }
2130}
2131
2132impl dispatch::PipelineCacheInterface for CorePipelineCache {
2133 fn get_data(&self) -> Option<Vec<u8>> {
2134 self.wgpu_pipeline_cache.get_data()
2135 }
2136}
2137
2138impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
2139 fn copy_buffer_to_buffer(
2140 &self,
2141 source: &dispatch::DispatchBuffer,
2142 source_offset: crate::BufferAddress,
2143 destination: &dispatch::DispatchBuffer,
2144 destination_offset: crate::BufferAddress,
2145 copy_size: Option<crate::BufferAddress>,
2146 ) {
2147 let source = source.as_core();
2148 let destination = destination.as_core();
2149
2150 self.wgpu_command_encoder.copy_buffer_to_buffer(
2151 source.wgpu_buffer.clone(),
2152 source_offset,
2153 destination.wgpu_buffer.clone(),
2154 destination_offset,
2155 copy_size,
2156 )
2157 }
2158
2159 fn copy_buffer_to_texture(
2160 &self,
2161 source: crate::TexelCopyBufferInfo<'_>,
2162 destination: crate::TexelCopyTextureInfo<'_>,
2163 copy_size: crate::Extent3d,
2164 ) {
2165 self.wgpu_command_encoder.copy_buffer_to_texture(
2166 &map_buffer_copy_view(source),
2167 &map_texture_copy_view(destination),
2168 ©_size,
2169 )
2170 }
2171
2172 fn copy_texture_to_buffer(
2173 &self,
2174 source: crate::TexelCopyTextureInfo<'_>,
2175 destination: crate::TexelCopyBufferInfo<'_>,
2176 copy_size: crate::Extent3d,
2177 ) {
2178 self.wgpu_command_encoder.copy_texture_to_buffer(
2179 &map_texture_copy_view(source),
2180 &map_buffer_copy_view(destination),
2181 ©_size,
2182 );
2183 }
2184
2185 fn copy_texture_to_texture(
2186 &self,
2187 source: crate::TexelCopyTextureInfo<'_>,
2188 destination: crate::TexelCopyTextureInfo<'_>,
2189 copy_size: crate::Extent3d,
2190 ) {
2191 self.wgpu_command_encoder.copy_texture_to_texture(
2192 &map_texture_copy_view(source),
2193 &map_texture_copy_view(destination),
2194 ©_size,
2195 );
2196 }
2197
2198 fn begin_compute_pass(
2199 &self,
2200 desc: &crate::ComputePassDescriptor<'_>,
2201 ) -> dispatch::DispatchComputePass {
2202 let timestamp_writes =
2203 desc.timestamp_writes
2204 .as_ref()
2205 .map(|tw| wgc::command::PassTimestampWrites {
2206 query_set: tw.query_set.inner.as_core().wgpu_query_set.clone(),
2207 beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
2208 end_of_pass_write_index: tw.end_of_pass_write_index,
2209 });
2210
2211 let pass =
2212 self.wgpu_command_encoder
2213 .begin_compute_pass(&wgc::command::ComputePassDescriptor {
2214 label: desc.label.map(Borrowed),
2215 timestamp_writes,
2216 });
2217
2218 CoreComputePass {
2219 pass,
2220 id: crate::cmp::Identifier::create(),
2221 }
2222 .into()
2223 }
2224
2225 fn begin_render_pass(
2226 &self,
2227 desc: &crate::RenderPassDescriptor<'_>,
2228 ) -> dispatch::DispatchRenderPass {
2229 let colors = desc
2230 .color_attachments
2231 .iter()
2232 .map(|ca| {
2233 ca.as_ref()
2234 .map(|at| wgc::command::RenderPassColorAttachment {
2235 view: at.view.inner.as_core().wgpu_texture_view.clone(),
2236 depth_slice: at.depth_slice,
2237 resolve_target: at
2238 .resolve_target
2239 .map(|view| view.inner.as_core().wgpu_texture_view.clone()),
2240 load_op: at.ops.load,
2241 store_op: at.ops.store,
2242 })
2243 })
2244 .collect::<Vec<_>>();
2245
2246 let depth_stencil = desc.depth_stencil_attachment.as_ref().map(|dsa| {
2247 wgc::command::RenderPassDepthStencilAttachment {
2248 view: dsa.view.inner.as_core().wgpu_texture_view.clone(),
2249 depth: map_pass_channel(dsa.depth_ops.as_ref()),
2250 stencil: map_pass_channel(dsa.stencil_ops.as_ref()),
2251 }
2252 });
2253
2254 let timestamp_writes =
2255 desc.timestamp_writes
2256 .as_ref()
2257 .map(|tw| wgc::command::PassTimestampWrites {
2258 query_set: tw.query_set.inner.as_core().wgpu_query_set.clone(),
2259 beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
2260 end_of_pass_write_index: tw.end_of_pass_write_index,
2261 });
2262
2263 let pass = self.wgpu_command_encoder.begin_render_pass(
2264 wgc::command::ResolvedRenderPassDescriptor {
2265 label: desc.label.map(Borrowed),
2266 timestamp_writes,
2267 color_attachments: Borrowed(&colors),
2268 depth_stencil_attachment: depth_stencil,
2269 occlusion_query_set: desc
2270 .occlusion_query_set
2271 .map(|qs| qs.inner.as_core().wgpu_query_set.clone()),
2272 multiview_mask: desc.multiview_mask,
2273 },
2274 );
2275
2276 CoreRenderPass {
2277 pass,
2278 id: crate::cmp::Identifier::create(),
2279 }
2280 .into()
2281 }
2282
2283 fn begin_ray_tracing_pass(
2284 &self,
2285 desc: &crate::RayTracingPassDescriptor<'_>,
2286 ) -> dispatch::DispatchRayTracingPass {
2287 let pass = self.wgpu_command_encoder.begin_ray_tracing_pass(
2288 &wgc::command::RayTracingPassDescriptor {
2289 label: desc.label.map(Borrowed),
2290 },
2291 );
2292
2293 CoreRayTracingPass {
2294 pass,
2295 id: crate::cmp::Identifier::create(),
2296 }
2297 .into()
2298 }
2299
2300 fn finish(&mut self) -> dispatch::DispatchCommandBuffer {
2301 let descriptor = wgt::CommandBufferDescriptor::default();
2302 let wgpu_command_buffer = self.wgpu_command_encoder.finish(&descriptor);
2303 CoreCommandBuffer {
2304 wgpu_command_buffer,
2305 }
2306 .into()
2307 }
2308
2309 fn clear_texture(
2310 &self,
2311 texture: &dispatch::DispatchTexture,
2312 subresource_range: &crate::ImageSubresourceRange,
2313 ) {
2314 let texture = texture.as_core();
2315
2316 self.wgpu_command_encoder
2317 .clear_texture(texture.wgpu_texture.clone(), subresource_range)
2318 }
2319
2320 fn clear_buffer(
2321 &self,
2322 buffer: &dispatch::DispatchBuffer,
2323 offset: crate::BufferAddress,
2324 size: Option<crate::BufferAddress>,
2325 ) {
2326 let buffer = buffer.as_core();
2327
2328 self.wgpu_command_encoder
2329 .clear_buffer(buffer.wgpu_buffer.clone(), offset, size)
2330 }
2331
2332 fn insert_debug_marker(&self, label: &str) {
2333 self.wgpu_command_encoder.insert_debug_marker(label)
2334 }
2335
2336 fn push_debug_group(&self, label: &str) {
2337 self.wgpu_command_encoder.push_debug_group(label)
2338 }
2339
2340 fn pop_debug_group(&self) {
2341 self.wgpu_command_encoder.pop_debug_group()
2342 }
2343
2344 fn write_timestamp(&self, query_set: &dispatch::DispatchQuerySet, query_index: u32) {
2345 let query_set = query_set.as_core();
2346
2347 self.wgpu_command_encoder
2348 .write_timestamp(query_set.wgpu_query_set.clone(), query_index)
2349 }
2350
2351 fn resolve_query_set(
2352 &self,
2353 query_set: &dispatch::DispatchQuerySet,
2354 first_query: u32,
2355 query_count: u32,
2356 destination: &dispatch::DispatchBuffer,
2357 destination_offset: crate::BufferAddress,
2358 ) {
2359 let query_set = query_set.as_core();
2360 let destination = destination.as_core();
2361
2362 self.wgpu_command_encoder.resolve_query_set(
2363 query_set.wgpu_query_set.clone(),
2364 first_query,
2365 query_count,
2366 destination.wgpu_buffer.clone(),
2367 destination_offset,
2368 );
2369 }
2370
2371 fn mark_acceleration_structures_built<'a>(
2372 &self,
2373 blas: &mut dyn Iterator<Item = &'a Blas>,
2374 tlas: &mut dyn Iterator<Item = &'a Tlas>,
2375 ) {
2376 let blas = blas
2377 .map(|b| b.inner.as_core().wgpu_blas.clone())
2378 .collect::<SmallVec<[_; 4]>>();
2379 let tlas = tlas
2380 .map(|t| t.inner.as_core().wgpu_tlas.clone())
2381 .collect::<SmallVec<[_; 4]>>();
2382 self.wgpu_command_encoder
2383 .mark_acceleration_structures_built(&blas, &tlas)
2384 }
2385
2386 fn build_acceleration_structures<'a>(
2387 &self,
2388 blas: &mut dyn Iterator<Item = &'a crate::BlasBuildEntry<'a>>,
2389 tlas: &mut dyn Iterator<Item = &'a crate::Tlas>,
2390 ) {
2391 let blas = blas.map(|e: &crate::BlasBuildEntry<'_>| {
2392 let geometries = match e.geometry {
2393 crate::BlasGeometries::TriangleGeometries(ref triangle_geometries) => {
2394 let iter = triangle_geometries.iter().map(|tg| {
2395 wgc::ray_tracing::BlasTriangleGeometry {
2396 vertex_buffer: tg.vertex_buffer.inner.as_core().wgpu_buffer.clone(),
2397 index_buffer: tg
2398 .index_buffer
2399 .map(|buf| buf.inner.as_core().wgpu_buffer.clone()),
2400 transform_buffer: tg
2401 .transform_buffer
2402 .map(|buf| buf.inner.as_core().wgpu_buffer.clone()),
2403 size: tg.size,
2404 transform_buffer_offset: tg.transform_buffer_offset,
2405 first_vertex: tg.first_vertex,
2406 vertex_stride: tg.vertex_stride,
2407 first_index: tg.first_index,
2408 }
2409 });
2410 wgc::ray_tracing::BlasGeometries::TriangleGeometries(Box::new(iter))
2411 }
2412 crate::BlasGeometries::AabbGeometries(ref aabb_geometries) => {
2413 let iter =
2414 aabb_geometries
2415 .iter()
2416 .map(|ag| wgc::ray_tracing::BlasAabbGeometry {
2417 aabb_buffer: ag.aabb_buffer.inner.as_core().wgpu_buffer.clone(),
2418 stride: ag.stride,
2419 size: ag.size,
2420 primitive_offset: ag.primitive_offset,
2421 });
2422 wgc::ray_tracing::BlasGeometries::AabbGeometries(Box::new(iter))
2423 }
2424 };
2425 wgc::ray_tracing::BlasBuildEntry {
2426 blas: e.blas.inner.as_core().wgpu_blas.clone(),
2427 geometries,
2428 }
2429 });
2430
2431 let tlas = tlas.into_iter().map(|e| {
2432 let instances = e
2433 .instances
2434 .iter()
2435 .map(|instance: &Option<crate::TlasInstance>| {
2436 instance
2437 .as_ref()
2438 .map(|instance| wgc::ray_tracing::ArcTlasInstance {
2439 blas: instance.blas.as_core().wgpu_blas.clone(),
2440 transform: instance.transform,
2441 custom_data: instance.custom_data,
2442 mask: instance.mask,
2443 intersection_index: instance.intersection_index,
2444 })
2445 })
2446 .collect();
2447 wgc::ray_tracing::ArcTlasPackage {
2448 tlas: e.inner.as_core().wgpu_tlas.clone(),
2449 instances,
2450 lowest_unmodified: e.lowest_unmodified,
2451 }
2452 });
2453
2454 self.wgpu_command_encoder
2455 .build_acceleration_structures(blas, tlas.collect())
2456 }
2457
2458 fn transition_resources<'a>(
2459 &mut self,
2460 buffer_transitions: &mut dyn Iterator<
2461 Item = wgt::BufferTransition<&'a dispatch::DispatchBuffer>,
2462 >,
2463 texture_transitions: &mut dyn Iterator<
2464 Item = wgt::TextureTransition<&'a dispatch::DispatchTexture>,
2465 >,
2466 ) {
2467 self.wgpu_command_encoder.transition_resources(
2468 buffer_transitions.map(|t| wgt::BufferTransition {
2469 buffer: t.buffer.as_core().wgpu_buffer.clone(),
2470 state: t.state,
2471 }),
2472 texture_transitions.map(|t| wgt::TextureTransition {
2473 texture: t.texture.as_core().wgpu_texture.clone(),
2474 selector: t.selector.clone(),
2475 state: t.state,
2476 }),
2477 );
2478 }
2479}
2480
2481impl dispatch::CommandBufferInterface for CoreCommandBuffer {}
2482
2483impl dispatch::ComputePassInterface for CoreComputePass {
2484 fn set_pipeline(&mut self, pipeline: &dispatch::DispatchComputePipeline) {
2485 let pipeline = pipeline.as_core();
2486
2487 self.pass
2488 .set_pipeline(pipeline.wgpu_compute_pipeline.clone());
2489 }
2490
2491 fn set_bind_group(
2492 &mut self,
2493 index: u32,
2494 bind_group: Option<&dispatch::DispatchBindGroup>,
2495 offsets: &[crate::DynamicOffset],
2496 ) {
2497 let bg = bind_group.map(|bg| bg.as_core().wgpu_bind_group.clone());
2498
2499 self.pass.set_bind_group(index, bg, offsets);
2500 }
2501
2502 fn set_immediates(&mut self, offset: u32, data: &[u8]) {
2503 self.pass.set_immediates(offset, data);
2504 }
2505
2506 fn insert_debug_marker(&mut self, label: &str) {
2507 self.pass.insert_debug_marker(label, 0);
2508 }
2509
2510 fn push_debug_group(&mut self, group_label: &str) {
2511 self.pass.push_debug_group(group_label, 0);
2512 }
2513
2514 fn pop_debug_group(&mut self) {
2515 self.pass.pop_debug_group();
2516 }
2517
2518 fn write_timestamp(&mut self, query_set: &dispatch::DispatchQuerySet, query_index: u32) {
2519 let query_set = query_set.as_core();
2520
2521 self.pass
2522 .write_timestamp(query_set.wgpu_query_set.clone(), query_index);
2523 }
2524
2525 fn begin_pipeline_statistics_query(
2526 &mut self,
2527 query_set: &dispatch::DispatchQuerySet,
2528 query_index: u32,
2529 ) {
2530 let query_set = query_set.as_core();
2531
2532 self.pass
2533 .begin_pipeline_statistics_query(query_set.wgpu_query_set.clone(), query_index);
2534 }
2535
2536 fn end_pipeline_statistics_query(&mut self) {
2537 self.pass.end_pipeline_statistics_query();
2538 }
2539
2540 fn dispatch_workgroups(&mut self, x: u32, y: u32, z: u32) {
2541 self.pass.dispatch_workgroups(x, y, z);
2542 }
2543
2544 fn dispatch_workgroups_indirect(
2545 &mut self,
2546 indirect_buffer: &dispatch::DispatchBuffer,
2547 indirect_offset: crate::BufferAddress,
2548 ) {
2549 let indirect_buffer = indirect_buffer.as_core();
2550
2551 self.pass
2552 .dispatch_workgroups_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
2553 }
2554
2555 fn transition_resources<'a>(
2556 &mut self,
2557 buffer_transitions: &mut dyn Iterator<
2558 Item = wgt::BufferTransition<&'a dispatch::DispatchBuffer>,
2559 >,
2560 texture_transitions: &mut dyn Iterator<
2561 Item = wgt::TextureTransition<&'a dispatch::DispatchTextureView>,
2562 >,
2563 ) {
2564 self.pass.transition_resources(
2565 buffer_transitions.map(|t| wgt::BufferTransition {
2566 buffer: t.buffer.as_core().wgpu_buffer.clone(),
2567 state: t.state,
2568 }),
2569 texture_transitions.map(|t| wgt::TextureTransition {
2570 texture: t.texture.as_core().wgpu_texture_view.clone(),
2571 selector: t.selector.clone(),
2572 state: t.state,
2573 }),
2574 );
2575 }
2576}
2577
2578impl Drop for CoreComputePass {
2579 fn drop(&mut self) {
2580 self.pass.end();
2581 }
2582}
2583
2584impl dispatch::RenderPassInterface for CoreRenderPass {
2585 fn set_pipeline(&mut self, pipeline: &dispatch::DispatchRenderPipeline) {
2586 let pipeline = pipeline.as_core();
2587
2588 self.pass
2589 .set_pipeline(pipeline.wgpu_render_pipeline.clone());
2590 }
2591
2592 fn set_bind_group(
2593 &mut self,
2594 index: u32,
2595 bind_group: Option<&dispatch::DispatchBindGroup>,
2596 offsets: &[crate::DynamicOffset],
2597 ) {
2598 let bg = bind_group.map(|bg| bg.as_core().wgpu_bind_group.clone());
2599
2600 self.pass.set_bind_group(index, bg, offsets);
2601 }
2602
2603 fn set_index_buffer(
2604 &mut self,
2605 buffer: &dispatch::DispatchBuffer,
2606 index_format: crate::IndexFormat,
2607 offset: crate::BufferAddress,
2608 size: Option<crate::BufferAddress>,
2609 ) {
2610 let buffer = buffer.as_core();
2611
2612 self.pass
2613 .set_index_buffer(buffer.wgpu_buffer.clone(), index_format, offset, size)
2614 }
2615
2616 fn set_vertex_buffer(
2617 &mut self,
2618 slot: u32,
2619 buffer: Option<&dispatch::DispatchBuffer>,
2620 offset: crate::BufferAddress,
2621 size: Option<crate::BufferAddress>,
2622 ) {
2623 let buffer = buffer.map(|buffer| buffer.as_core().wgpu_buffer.clone());
2624
2625 self.pass.set_vertex_buffer(slot, buffer, offset, size);
2626 }
2627
2628 fn set_immediates(&mut self, offset: u32, data: &[u8]) {
2629 self.pass.set_immediates(offset, data);
2630 }
2631
2632 fn set_blend_constant(&mut self, color: crate::Color) {
2633 self.pass.set_blend_constant(color);
2634 }
2635
2636 fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) {
2637 self.pass.set_scissor_rect(x, y, width, height);
2638 }
2639
2640 fn set_viewport(
2641 &mut self,
2642 x: f32,
2643 y: f32,
2644 width: f32,
2645 height: f32,
2646 min_depth: f32,
2647 max_depth: f32,
2648 ) {
2649 self.pass
2650 .set_viewport(x, y, width, height, min_depth, max_depth);
2651 }
2652
2653 fn set_stencil_reference(&mut self, reference: u32) {
2654 self.pass.set_stencil_reference(reference);
2655 }
2656
2657 fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
2658 self.pass.draw(
2659 vertices.end - vertices.start,
2660 instances.end - instances.start,
2661 vertices.start,
2662 instances.start,
2663 );
2664 }
2665
2666 fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
2667 self.pass.draw_indexed(
2668 indices.end - indices.start,
2669 instances.end - instances.start,
2670 indices.start,
2671 base_vertex,
2672 instances.start,
2673 );
2674 }
2675
2676 fn draw_mesh_tasks(&mut self, group_count_x: u32, group_count_y: u32, group_count_z: u32) {
2677 self.pass
2678 .draw_mesh_tasks(group_count_x, group_count_y, group_count_z);
2679 }
2680
2681 fn draw_indirect(
2682 &mut self,
2683 indirect_buffer: &dispatch::DispatchBuffer,
2684 indirect_offset: crate::BufferAddress,
2685 ) {
2686 let indirect_buffer = indirect_buffer.as_core();
2687
2688 self.pass
2689 .draw_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
2690 }
2691
2692 fn draw_indexed_indirect(
2693 &mut self,
2694 indirect_buffer: &dispatch::DispatchBuffer,
2695 indirect_offset: crate::BufferAddress,
2696 ) {
2697 let indirect_buffer = indirect_buffer.as_core();
2698
2699 self.pass
2700 .draw_indexed_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
2701 }
2702
2703 fn draw_mesh_tasks_indirect(
2704 &mut self,
2705 indirect_buffer: &dispatch::DispatchBuffer,
2706 indirect_offset: crate::BufferAddress,
2707 ) {
2708 let indirect_buffer = indirect_buffer.as_core();
2709
2710 self.pass
2711 .draw_mesh_tasks_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
2712 }
2713
2714 fn multi_draw_indirect(
2715 &mut self,
2716 indirect_buffer: &dispatch::DispatchBuffer,
2717 indirect_offset: crate::BufferAddress,
2718 count: u32,
2719 ) {
2720 let indirect_buffer = indirect_buffer.as_core();
2721
2722 self.pass
2723 .multi_draw_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset, count);
2724 }
2725
2726 fn multi_draw_indexed_indirect(
2727 &mut self,
2728 indirect_buffer: &dispatch::DispatchBuffer,
2729 indirect_offset: crate::BufferAddress,
2730 count: u32,
2731 ) {
2732 let indirect_buffer = indirect_buffer.as_core();
2733
2734 self.pass.multi_draw_indexed_indirect(
2735 indirect_buffer.wgpu_buffer.clone(),
2736 indirect_offset,
2737 count,
2738 );
2739 }
2740
2741 fn multi_draw_mesh_tasks_indirect(
2742 &mut self,
2743 indirect_buffer: &dispatch::DispatchBuffer,
2744 indirect_offset: crate::BufferAddress,
2745 count: u32,
2746 ) {
2747 let indirect_buffer = indirect_buffer.as_core();
2748
2749 self.pass.multi_draw_mesh_tasks_indirect(
2750 indirect_buffer.wgpu_buffer.clone(),
2751 indirect_offset,
2752 count,
2753 );
2754 }
2755
2756 fn multi_draw_indirect_count(
2757 &mut self,
2758 indirect_buffer: &dispatch::DispatchBuffer,
2759 indirect_offset: crate::BufferAddress,
2760 count_buffer: &dispatch::DispatchBuffer,
2761 count_buffer_offset: crate::BufferAddress,
2762 max_count: u32,
2763 ) {
2764 let indirect_buffer = indirect_buffer.as_core();
2765 let count_buffer = count_buffer.as_core();
2766
2767 self.pass.multi_draw_indirect_count(
2768 indirect_buffer.wgpu_buffer.clone(),
2769 indirect_offset,
2770 count_buffer.wgpu_buffer.clone(),
2771 count_buffer_offset,
2772 max_count,
2773 );
2774 }
2775
2776 fn multi_draw_indexed_indirect_count(
2777 &mut self,
2778 indirect_buffer: &dispatch::DispatchBuffer,
2779 indirect_offset: crate::BufferAddress,
2780 count_buffer: &dispatch::DispatchBuffer,
2781 count_buffer_offset: crate::BufferAddress,
2782 max_count: u32,
2783 ) {
2784 let indirect_buffer = indirect_buffer.as_core();
2785 let count_buffer = count_buffer.as_core();
2786
2787 self.pass.multi_draw_indexed_indirect_count(
2788 indirect_buffer.wgpu_buffer.clone(),
2789 indirect_offset,
2790 count_buffer.wgpu_buffer.clone(),
2791 count_buffer_offset,
2792 max_count,
2793 );
2794 }
2795
2796 fn multi_draw_mesh_tasks_indirect_count(
2797 &mut self,
2798 indirect_buffer: &dispatch::DispatchBuffer,
2799 indirect_offset: crate::BufferAddress,
2800 count_buffer: &dispatch::DispatchBuffer,
2801 count_buffer_offset: crate::BufferAddress,
2802 max_count: u32,
2803 ) {
2804 let indirect_buffer = indirect_buffer.as_core();
2805 let count_buffer = count_buffer.as_core();
2806
2807 self.pass.multi_draw_mesh_tasks_indirect_count(
2808 indirect_buffer.wgpu_buffer.clone(),
2809 indirect_offset,
2810 count_buffer.wgpu_buffer.clone(),
2811 count_buffer_offset,
2812 max_count,
2813 );
2814 }
2815
2816 fn insert_debug_marker(&mut self, label: &str) {
2817 self.pass.insert_debug_marker(label, 0);
2818 }
2819
2820 fn push_debug_group(&mut self, group_label: &str) {
2821 self.pass.push_debug_group(group_label, 0);
2822 }
2823
2824 fn pop_debug_group(&mut self) {
2825 self.pass.pop_debug_group();
2826 }
2827
2828 fn write_timestamp(&mut self, query_set: &dispatch::DispatchQuerySet, query_index: u32) {
2829 let query_set = query_set.as_core();
2830
2831 self.pass
2832 .write_timestamp(query_set.wgpu_query_set.clone(), query_index);
2833 }
2834
2835 fn begin_occlusion_query(&mut self, query_index: u32) {
2836 self.pass.begin_occlusion_query(query_index);
2837 }
2838
2839 fn end_occlusion_query(&mut self) {
2840 self.pass.end_occlusion_query();
2841 }
2842
2843 fn begin_pipeline_statistics_query(
2844 &mut self,
2845 query_set: &dispatch::DispatchQuerySet,
2846 query_index: u32,
2847 ) {
2848 let query_set = query_set.as_core();
2849
2850 self.pass
2851 .begin_pipeline_statistics_query(query_set.wgpu_query_set.clone(), query_index);
2852 }
2853
2854 fn end_pipeline_statistics_query(&mut self) {
2855 self.pass.end_pipeline_statistics_query();
2856 }
2857
2858 fn execute_bundles(
2859 &mut self,
2860 render_bundles: &mut dyn Iterator<Item = &dispatch::DispatchRenderBundle>,
2861 ) {
2862 let temp_render_bundles = render_bundles
2863 .map(|rb| rb.as_core().wgpu_render_bundle.clone())
2864 .collect::<SmallVec<[_; 4]>>();
2865 self.pass.execute_bundles(&temp_render_bundles);
2866 }
2867}
2868
2869impl Drop for CoreRenderPass {
2870 fn drop(&mut self) {
2871 self.pass.end()
2872 }
2873}
2874
2875impl dispatch::RayTracingPassInterface for CoreRayTracingPass {
2876 fn set_pipeline(&mut self, pipeline: &dispatch::DispatchRayTracingPipeline) {
2877 let pipeline = pipeline.as_core();
2878
2879 self.pass
2880 .set_pipeline(pipeline.wgpu_ray_tracing_pipeline.clone())
2881 }
2882
2883 fn set_bind_group(
2884 &mut self,
2885 index: u32,
2886 bind_group: Option<&dispatch::DispatchBindGroup>,
2887 offsets: &[crate::DynamicOffset],
2888 ) {
2889 let bg = bind_group.map(|bg| bg.as_core().wgpu_bind_group.clone());
2890
2891 self.pass.set_bind_group(index, bg, offsets)
2892 }
2893
2894 fn set_immediates(&mut self, offset: u32, data: &[u8]) {
2895 self.pass.set_immediates(offset, data)
2896 }
2897
2898 fn insert_debug_marker(&mut self, label: &str) {
2899 self.pass.insert_debug_marker(label, 0)
2900 }
2901
2902 fn push_debug_group(&mut self, group_label: &str) {
2903 self.pass.push_debug_group(group_label, 0)
2904 }
2905
2906 fn pop_debug_group(&mut self) {
2907 self.pass.pop_debug_group()
2908 }
2909
2910 fn trace_rays(&mut self, x: u32, y: u32, z: u32) {
2911 self.pass.trace_rays(x, y, z)
2912 }
2913}
2914
2915impl Drop for CoreRayTracingPass {
2916 fn drop(&mut self) {
2917 self.pass.end()
2918 }
2919}
2920
2921impl dispatch::RenderBundleEncoderInterface for CoreRenderBundleEncoder {
2922 fn set_pipeline(&mut self, pipeline: &dispatch::DispatchRenderPipeline) {
2923 let pipeline = pipeline.as_core();
2924
2925 self.encoder
2926 .set_pipeline(pipeline.wgpu_render_pipeline.clone())
2927 }
2928
2929 fn set_bind_group(
2930 &mut self,
2931 index: u32,
2932 bind_group: Option<&dispatch::DispatchBindGroup>,
2933 offsets: &[crate::DynamicOffset],
2934 ) {
2935 let bg = bind_group.map(|bg| bg.as_core().wgpu_bind_group.clone());
2936
2937 self.encoder.set_bind_group(index, bg, offsets);
2938 }
2939
2940 fn set_index_buffer(
2941 &mut self,
2942 buffer: &dispatch::DispatchBuffer,
2943 index_format: crate::IndexFormat,
2944 offset: crate::BufferAddress,
2945 size: Option<crate::BufferAddress>,
2946 ) {
2947 let buffer = buffer.as_core();
2948
2949 self.encoder
2950 .set_index_buffer(buffer.wgpu_buffer.clone(), index_format, offset, size);
2951 }
2952
2953 fn set_vertex_buffer(
2954 &mut self,
2955 slot: u32,
2956 buffer: Option<&dispatch::DispatchBuffer>,
2957 offset: crate::BufferAddress,
2958 size: Option<crate::BufferAddress>,
2959 ) {
2960 let buffer = buffer.map(|buffer| buffer.as_core().wgpu_buffer.clone());
2961
2962 self.encoder.set_vertex_buffer(slot, buffer, offset, size);
2963 }
2964
2965 fn set_immediates(&mut self, offset: u32, data: &[u8]) {
2966 if !data
2967 .len()
2968 .is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT as usize)
2969 {
2970 self.encoder.device().handle_error(
2971 wgc::binding_model::ImmediateUploadError::SizeUnaligned(data.len()),
2972 self.encoder.label(),
2973 "RenderBundleEncoder::set_immediates",
2974 );
2975 return;
2976 }
2977
2978 self.encoder.set_immediates(offset, data);
2979 }
2980
2981 fn insert_debug_marker(&mut self, label: &str) {
2982 self.encoder.insert_debug_marker(label);
2983 }
2984
2985 fn push_debug_group(&mut self, group_label: &str) {
2986 self.encoder.push_debug_group(group_label);
2987 }
2988
2989 fn pop_debug_group(&mut self) {
2990 self.encoder.pop_debug_group();
2991 }
2992
2993 fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
2994 self.encoder.draw(
2995 vertices.end - vertices.start,
2996 instances.end - instances.start,
2997 vertices.start,
2998 instances.start,
2999 );
3000 }
3001
3002 fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
3003 self.encoder.draw_indexed(
3004 indices.end - indices.start,
3005 instances.end - instances.start,
3006 indices.start,
3007 base_vertex,
3008 instances.start,
3009 );
3010 }
3011
3012 fn draw_indirect(
3013 &mut self,
3014 indirect_buffer: &dispatch::DispatchBuffer,
3015 indirect_offset: crate::BufferAddress,
3016 ) {
3017 let indirect_buffer = indirect_buffer.as_core();
3018
3019 self.encoder
3020 .draw_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
3021 }
3022
3023 fn draw_indexed_indirect(
3024 &mut self,
3025 indirect_buffer: &dispatch::DispatchBuffer,
3026 indirect_offset: crate::BufferAddress,
3027 ) {
3028 let indirect_buffer = indirect_buffer.as_core();
3029
3030 self.encoder
3031 .draw_indexed_indirect(indirect_buffer.wgpu_buffer.clone(), indirect_offset);
3032 }
3033
3034 fn finish(mut self, desc: &crate::RenderBundleDescriptor<'_>) -> dispatch::DispatchRenderBundle
3035 where
3036 Self: Sized,
3037 {
3038 let wgpu_render_bundle = self.encoder.finish(&desc.map_label(|l| l.map(Borrowed)));
3039 CoreRenderBundle { wgpu_render_bundle }.into()
3040 }
3041
3042 #[cfg(custom)]
3043 fn finish_boxed(
3044 self: Box<Self>,
3045 desc: &crate::RenderBundleDescriptor<'_>,
3046 ) -> dispatch::DispatchRenderBundle {
3047 (*self).finish(desc)
3048 }
3049}
3050
3051impl dispatch::RenderBundleInterface for CoreRenderBundle {}
3052
3053#[derive(Clone)]
3054enum ErrorSink {
3055 Actual(Arc<wgc::device::Device>),
3056 Dummy(Arc<Mutex<wgc::error::ErrorSink>>),
3057}
3058
3059impl ErrorSink {
3060 fn new() -> Self {
3061 Self::Dummy(Arc::new(Mutex::new(wgc::error::ErrorSink::new())))
3062 }
3063
3064 fn handle_error_nolabel(
3065 &self,
3066 source: impl WebGpuError + WasmNotSendSync + 'static,
3067 fn_ident: &'static str,
3068 ) {
3069 match self {
3070 ErrorSink::Actual(device) => device.handle_error_nolabel(source, fn_ident),
3071 ErrorSink::Dummy(sink) => sink.lock().handle_error_nolabel(source, fn_ident),
3072 }
3073 }
3074}
3075
3076impl dispatch::SurfaceInterface for CoreSurface {
3077 fn get_capabilities(&self, adapter: &dispatch::DispatchAdapter) -> wgt::SurfaceCapabilities {
3078 let adapter = adapter.as_core();
3079
3080 self.wgpu_surface
3081 .get_capabilities(&adapter.wgpu_adapter)
3082 .unwrap_or_default()
3083 }
3084
3085 fn display_hdr_info(&self, adapter: &dispatch::DispatchAdapter) -> wgt::DisplayHdrInfo {
3086 let adapter = adapter.as_core();
3087
3088 self.wgpu_surface.display_hdr_info(&adapter.wgpu_adapter)
3089 }
3090
3091 fn configure(&self, device: &dispatch::DispatchDevice, config: &crate::SurfaceConfiguration) {
3092 let device = device.as_core();
3093
3094 let result = self.wgpu_surface.configure(&device.wgpu_device, config);
3095 if let Some(e) = result.err() {
3096 device
3097 .wgpu_device
3098 .handle_error_nolabel(e, "Surface::configure");
3099 } else {
3100 *self.configured_device.lock() = Some(device.wgpu_device.clone());
3101 }
3102 }
3103
3104 fn get_current_texture(
3105 &self,
3106 _desc: Option<crate::TextureDescriptor<'static>>,
3107 ) -> (
3108 Option<dispatch::DispatchTexture>,
3109 crate::SurfaceStatus,
3110 dispatch::DispatchSurfaceOutputDetail,
3111 ) {
3112 let error_sink = if let Some(error_sink) = self.configured_device.lock().as_ref() {
3113 ErrorSink::Actual(error_sink.clone())
3114 } else {
3115 ErrorSink::new()
3116 };
3117
3118 let output_detail = CoreSurfaceOutputDetail {
3119 wgpu_surface: self.wgpu_surface.clone(),
3120 error_sink,
3121 }
3122 .into();
3123
3124 match self.wgpu_surface.get_current_texture() {
3125 Ok(wgc::present::SurfaceOutput {
3126 status,
3127 texture: texture_id,
3128 }) => {
3129 let data = texture_id
3130 .map(|wgpu_texture| CoreTexture { wgpu_texture })
3131 .map(Into::into);
3132
3133 (data, status, output_detail)
3134 }
3135 Err(err) => {
3136 let error_sink = self.configured_device.lock();
3137 match error_sink.as_ref() {
3138 Some(error_sink) => {
3139 error_sink.handle_error_nolabel(err, "Surface::get_current_texture_view");
3140 (None, crate::SurfaceStatus::Validation, output_detail)
3141 }
3142 None => handle_error_fatal(err, "Surface::get_current_texture_view"),
3143 }
3144 }
3145 }
3146 }
3147}
3148
3149impl dispatch::SurfaceOutputDetailInterface for CoreSurfaceOutputDetail {
3150 fn texture_discard(&self) {
3151 match self.wgpu_surface.discard() {
3152 Ok(_status) => (),
3153 Err(err) => self
3154 .error_sink
3155 .handle_error_nolabel(err, "Surface::discard_texture"),
3156 }
3157 }
3158
3159 fn texture_release(&self) {
3160 match self.wgpu_surface.release() {
3161 Ok(_status) => (),
3162 Err(err) => self
3163 .error_sink
3164 .handle_error_nolabel(err, "Surface::release_texture"),
3165 }
3166 }
3167}
3168
3169impl dispatch::QueueWriteBufferInterface for CoreQueueWriteBuffer {
3170 #[inline]
3171 fn len(&self) -> usize {
3172 self.wgpu_staging_buffer.size().get() as usize
3173 }
3174
3175 #[inline]
3176 unsafe fn write_slice(&mut self) -> WriteOnly<'_, [u8]> {
3177 unsafe {
3178 WriteOnly::new(NonNull::slice_from_raw_parts(
3179 self.wgpu_staging_buffer.ptr(),
3180 self.len(),
3181 ))
3182 }
3183 }
3184}
3185
3186impl dispatch::BufferMappedRangeInterface for CoreBufferMappedRange {
3187 #[inline]
3188 unsafe fn read_slice(&self) -> &[u8] {
3189 self.0.read_slice()
3190 }
3191
3192 #[inline]
3193 unsafe fn write_slice(&mut self) -> WriteOnly<'_, [u8]> {
3194 self.0.write_slice()
3195 }
3196
3197 #[cfg(webgpu)]
3198 fn as_uint8array(&self) -> &js_sys::Uint8Array {
3199 panic!("Only available on WebGPU")
3200 }
3201}