| 1 |
#![allow(unused_variables)] |
| 2 |
|
| 3 |
use alloc::{string::String, vec, vec::Vec}; |
| 4 |
use core::{ptr, sync::atomic::Ordering, time::Duration}; |
| 5 |
|
| 6 |
#[cfg(supports_64bit_atomics)] |
| 7 |
use core::sync::atomic::AtomicU64; |
| 8 |
#[cfg(not(supports_64bit_atomics))] |
| 9 |
use portable_atomic::AtomicU64; |
| 10 |
|
| 11 |
use crate::TlasInstance; |
| 12 |
|
| 13 |
mod buffer; |
| 14 |
pub use buffer::Buffer; |
| 15 |
mod command; |
| 16 |
pub use command::CommandBuffer; |
| 17 |
|
| 18 |
#[derive(Clone, Debug)] |
| 19 |
pub struct Api; |
| 20 |
pub struct Context; |
| 21 |
#[derive(Debug)] |
| 22 |
pub struct Encoder; |
| 23 |
#[derive(Debug)] |
| 24 |
pub struct Resource; |
| 25 |
|
| 26 |
#[derive(Debug)] |
| 27 |
pub struct Fence { |
| 28 |
value: AtomicU64, |
| 29 |
} |
| 30 |
|
| 31 |
type DeviceResult<T> = Result<T, crate::DeviceError>; |
| 32 |
|
| 33 |
impl crate::Api for Api { |
| 34 |
type Instance = Context; |
| 35 |
type Surface = Context; |
| 36 |
type Adapter = Context; |
| 37 |
type Device = Context; |
| 38 |
|
| 39 |
type Queue = Context; |
| 40 |
type CommandEncoder = CommandBuffer; |
| 41 |
type CommandBuffer = CommandBuffer; |
| 42 |
|
| 43 |
type Buffer = Buffer; |
| 44 |
type Texture = Resource; |
| 45 |
type SurfaceTexture = Resource; |
| 46 |
type TextureView = Resource; |
| 47 |
type Sampler = Resource; |
| 48 |
type QuerySet = Resource; |
| 49 |
type Fence = Fence; |
| 50 |
type AccelerationStructure = Resource; |
| 51 |
type PipelineCache = Resource; |
| 52 |
|
| 53 |
type BindGroupLayout = Resource; |
| 54 |
type BindGroup = Resource; |
| 55 |
type PipelineLayout = Resource; |
| 56 |
type ShaderModule = Resource; |
| 57 |
type RenderPipeline = Resource; |
| 58 |
type ComputePipeline = Resource; |
| 59 |
} |
| 60 |
|
| 61 |
crate::impl_dyn_resource!(Buffer, CommandBuffer, Context, Fence, Resource); |
| 62 |
|
| 63 |
impl crate::DynAccelerationStructure for Resource {} |
| 64 |
impl crate::DynBindGroup for Resource {} |
| 65 |
impl crate::DynBindGroupLayout for Resource {} |
| 66 |
impl crate::DynBuffer for Buffer {} |
| 67 |
impl crate::DynCommandBuffer for CommandBuffer {} |
| 68 |
impl crate::DynComputePipeline for Resource {} |
| 69 |
impl crate::DynFence for Fence {} |
| 70 |
impl crate::DynPipelineCache for Resource {} |
| 71 |
impl crate::DynPipelineLayout for Resource {} |
| 72 |
impl crate::DynQuerySet for Resource {} |
| 73 |
impl crate::DynRenderPipeline for Resource {} |
| 74 |
impl crate::DynSampler for Resource {} |
| 75 |
impl crate::DynShaderModule for Resource {} |
| 76 |
impl crate::DynSurfaceTexture for Resource {} |
| 77 |
impl crate::DynTexture for Resource {} |
| 78 |
impl crate::DynTextureView for Resource {} |
| 79 |
|
| 80 |
impl core::borrow::Borrow<dyn crate::DynTexture> for Resource { |
| 81 |
fn borrow(&self) -> &dyn crate::DynTexture { |
| 82 |
self |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl crate::Instance for Context { |
| 87 |
type A = Api; |
| 88 |
|
| 89 |
unsafe fn init(desc: &crate::InstanceDescriptor) -> Result<Self, crate::InstanceError> { |
| 90 |
let crate::InstanceDescriptor { |
| 91 |
backend_options: |
| 92 |
wgt::BackendOptions { |
| 93 |
noop: wgt::NoopBackendOptions { enable }, |
| 94 |
.. |
| 95 |
}, |
| 96 |
name: _, |
| 97 |
flags: _, |
| 98 |
memory_budget_thresholds: _, |
| 99 |
} = *desc; |
| 100 |
if enable { |
| 101 |
Ok(Context) |
| 102 |
} else { |
| 103 |
Err(crate::InstanceError::new(String::from( |
| 104 |
"noop backend disabled because NoopBackendOptions::enable is false", |
| 105 |
))) |
| 106 |
} |
| 107 |
} |
| 108 |
unsafe fn create_surface( |
| 109 |
&self, |
| 110 |
_display_handle: raw_window_handle::RawDisplayHandle, |
| 111 |
_window_handle: raw_window_handle::RawWindowHandle, |
| 112 |
) -> Result<Context, crate::InstanceError> { |
| 113 |
Ok(Context) |
| 114 |
} |
| 115 |
unsafe fn enumerate_adapters( |
| 116 |
&self, |
| 117 |
_surface_hint: Option<&Context>, |
| 118 |
) -> Vec<crate::ExposedAdapter<Api>> { |
| 119 |
vec![crate::ExposedAdapter { |
| 120 |
adapter: Context, |
| 121 |
info: wgt::AdapterInfo { |
| 122 |
name: String::from("noop wgpu backend"), |
| 123 |
vendor: 0, |
| 124 |
device: 0, |
| 125 |
device_type: wgt::DeviceType::Cpu, |
| 126 |
driver: String::from("wgpu"), |
| 127 |
driver_info: String::new(), |
| 128 |
backend: wgt::Backend::Noop, |
| 129 |
}, |
| 130 |
features: wgt::Features::all(), |
| 131 |
capabilities: CAPABILITIES, |
| 132 |
}] |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
const CAPABILITIES: crate::Capabilities = { |
| 137 |
/// Guaranteed to be no bigger than isize::MAX which is the maximum size of an allocation, |
| 138 |
/// except on 16-bit platforms which we certainly don’t fit in. |
| 139 |
const ALLOC_MAX_U32: u32 = i32::MAX as u32; |
| 140 |
|
| 141 |
crate::Capabilities { |
| 142 |
limits: wgt::Limits { |
| 143 |
// All maximally permissive |
| 144 |
max_texture_dimension_1d: ALLOC_MAX_U32, |
| 145 |
max_texture_dimension_2d: ALLOC_MAX_U32, |
| 146 |
max_texture_dimension_3d: ALLOC_MAX_U32, |
| 147 |
max_texture_array_layers: ALLOC_MAX_U32, |
| 148 |
max_bind_groups: ALLOC_MAX_U32, |
| 149 |
max_bindings_per_bind_group: ALLOC_MAX_U32, |
| 150 |
max_dynamic_uniform_buffers_per_pipeline_layout: ALLOC_MAX_U32, |
| 151 |
max_dynamic_storage_buffers_per_pipeline_layout: ALLOC_MAX_U32, |
| 152 |
max_sampled_textures_per_shader_stage: ALLOC_MAX_U32, |
| 153 |
max_samplers_per_shader_stage: ALLOC_MAX_U32, |
| 154 |
max_storage_buffers_per_shader_stage: ALLOC_MAX_U32, |
| 155 |
max_storage_textures_per_shader_stage: ALLOC_MAX_U32, |
| 156 |
max_uniform_buffers_per_shader_stage: ALLOC_MAX_U32, |
| 157 |
max_binding_array_elements_per_shader_stage: ALLOC_MAX_U32, |
| 158 |
max_binding_array_sampler_elements_per_shader_stage: ALLOC_MAX_U32, |
| 159 |
max_uniform_buffer_binding_size: ALLOC_MAX_U32, |
| 160 |
max_storage_buffer_binding_size: ALLOC_MAX_U32, |
| 161 |
max_vertex_buffers: ALLOC_MAX_U32, |
| 162 |
max_buffer_size: ALLOC_MAX_U32 as u64, |
| 163 |
max_vertex_attributes: ALLOC_MAX_U32, |
| 164 |
max_vertex_buffer_array_stride: ALLOC_MAX_U32, |
| 165 |
min_uniform_buffer_offset_alignment: 1, |
| 166 |
min_storage_buffer_offset_alignment: 1, |
| 167 |
max_inter_stage_shader_components: ALLOC_MAX_U32, |
| 168 |
max_color_attachments: ALLOC_MAX_U32, |
| 169 |
max_color_attachment_bytes_per_sample: ALLOC_MAX_U32, |
| 170 |
max_compute_workgroup_storage_size: ALLOC_MAX_U32, |
| 171 |
max_compute_invocations_per_workgroup: ALLOC_MAX_U32, |
| 172 |
max_compute_workgroup_size_x: ALLOC_MAX_U32, |
| 173 |
max_compute_workgroup_size_y: ALLOC_MAX_U32, |
| 174 |
max_compute_workgroup_size_z: ALLOC_MAX_U32, |
| 175 |
max_compute_workgroups_per_dimension: ALLOC_MAX_U32, |
| 176 |
min_subgroup_size: 1, |
| 177 |
max_subgroup_size: ALLOC_MAX_U32, |
| 178 |
max_push_constant_size: ALLOC_MAX_U32, |
| 179 |
max_non_sampler_bindings: ALLOC_MAX_U32, |
| 180 |
}, |
| 181 |
alignments: crate::Alignments { |
| 182 |
// All maximally permissive |
| 183 |
buffer_copy_offset: wgt::BufferSize::MIN, |
| 184 |
buffer_copy_pitch: wgt::BufferSize::MIN, |
| 185 |
uniform_bounds_check_alignment: wgt::BufferSize::MIN, |
| 186 |
raw_tlas_instance_size: 0, |
| 187 |
ray_tracing_scratch_buffer_alignment: 1, |
| 188 |
}, |
| 189 |
downlevel: wgt::DownlevelCapabilities { |
| 190 |
flags: wgt::DownlevelFlags::all(), |
| 191 |
limits: wgt::DownlevelLimits {}, |
| 192 |
shader_model: wgt::ShaderModel::Sm5, |
| 193 |
}, |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
impl crate::Surface for Context { |
| 198 |
type A = Api; |
| 199 |
|
| 200 |
unsafe fn configure( |
| 201 |
&self, |
| 202 |
device: &Context, |
| 203 |
config: &crate::SurfaceConfiguration, |
| 204 |
) -> Result<(), crate::SurfaceError> { |
| 205 |
Ok(()) |
| 206 |
} |
| 207 |
|
| 208 |
unsafe fn unconfigure(&self, device: &Context) {} |
| 209 |
|
| 210 |
unsafe fn acquire_texture( |
| 211 |
&self, |
| 212 |
timeout: Option<Duration>, |
| 213 |
fence: &Fence, |
| 214 |
) -> Result<Option<crate::AcquiredSurfaceTexture<Api>>, crate::SurfaceError> { |
| 215 |
Ok(None) |
| 216 |
} |
| 217 |
unsafe fn discard_texture(&self, texture: Resource) {} |
| 218 |
} |
| 219 |
|
| 220 |
impl crate::Adapter for Context { |
| 221 |
type A = Api; |
| 222 |
|
| 223 |
unsafe fn open( |
| 224 |
&self, |
| 225 |
features: wgt::Features, |
| 226 |
_limits: &wgt::Limits, |
| 227 |
_memory_hints: &wgt::MemoryHints, |
| 228 |
) -> DeviceResult<crate::OpenDevice<Api>> { |
| 229 |
Ok(crate::OpenDevice { |
| 230 |
device: Context, |
| 231 |
queue: Context, |
| 232 |
}) |
| 233 |
} |
| 234 |
unsafe fn texture_format_capabilities( |
| 235 |
&self, |
| 236 |
format: wgt::TextureFormat, |
| 237 |
) -> crate::TextureFormatCapabilities { |
| 238 |
crate::TextureFormatCapabilities::empty() |
| 239 |
} |
| 240 |
|
| 241 |
unsafe fn surface_capabilities(&self, surface: &Context) -> Option<crate::SurfaceCapabilities> { |
| 242 |
None |
| 243 |
} |
| 244 |
|
| 245 |
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { |
| 246 |
wgt::PresentationTimestamp::INVALID_TIMESTAMP |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
impl crate::Queue for Context { |
| 251 |
type A = Api; |
| 252 |
|
| 253 |
unsafe fn submit( |
| 254 |
&self, |
| 255 |
command_buffers: &[&CommandBuffer], |
| 256 |
surface_textures: &[&Resource], |
| 257 |
(fence, fence_value): (&mut Fence, crate::FenceValue), |
| 258 |
) -> DeviceResult<()> { |
| 259 |
// All commands are executed synchronously. |
| 260 |
for cb in command_buffers { |
| 261 |
// SAFETY: Caller is responsible for ensuring synchronization between commands and |
| 262 |
// other mutations. |
| 263 |
unsafe { |
| 264 |
cb.execute(); |
| 265 |
} |
| 266 |
} |
| 267 |
fence.value.store(fence_value, Ordering::Release); |
| 268 |
Ok(()) |
| 269 |
} |
| 270 |
unsafe fn present( |
| 271 |
&self, |
| 272 |
surface: &Context, |
| 273 |
texture: Resource, |
| 274 |
) -> Result<(), crate::SurfaceError> { |
| 275 |
Ok(()) |
| 276 |
} |
| 277 |
|
| 278 |
unsafe fn get_timestamp_period(&self) -> f32 { |
| 279 |
1.0 |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl crate::Device for Context { |
| 284 |
type A = Api; |
| 285 |
|
| 286 |
unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<Buffer> { |
| 287 |
Buffer::new(desc) |
| 288 |
} |
| 289 |
|
| 290 |
unsafe fn destroy_buffer(&self, buffer: Buffer) {} |
| 291 |
unsafe fn add_raw_buffer(&self, _buffer: &Buffer) {} |
| 292 |
|
| 293 |
unsafe fn map_buffer( |
| 294 |
&self, |
| 295 |
buffer: &Buffer, |
| 296 |
range: crate::MemoryRange, |
| 297 |
) -> DeviceResult<crate::BufferMapping> { |
| 298 |
// Safety: the `wgpu-core` validation layer will prevent any user-accessible aliasing |
| 299 |
// mappings from being created, so we don’t need to perform any checks here, except for |
| 300 |
// bounds checks on the range which are built into `get_slice_ptr()`. |
| 301 |
Ok(crate::BufferMapping { |
| 302 |
ptr: ptr::NonNull::new(buffer.get_slice_ptr(range).cast::<u8>()).unwrap(), |
| 303 |
is_coherent: true, |
| 304 |
}) |
| 305 |
} |
| 306 |
unsafe fn unmap_buffer(&self, buffer: &Buffer) {} |
| 307 |
unsafe fn flush_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {} |
| 308 |
unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {} |
| 309 |
|
| 310 |
unsafe fn create_texture(&self, desc: &crate::TextureDescriptor) -> DeviceResult<Resource> { |
| 311 |
Ok(Resource) |
| 312 |
} |
| 313 |
unsafe fn destroy_texture(&self, texture: Resource) {} |
| 314 |
unsafe fn add_raw_texture(&self, _texture: &Resource) {} |
| 315 |
|
| 316 |
unsafe fn create_texture_view( |
| 317 |
&self, |
| 318 |
texture: &Resource, |
| 319 |
desc: &crate::TextureViewDescriptor, |
| 320 |
) -> DeviceResult<Resource> { |
| 321 |
Ok(Resource) |
| 322 |
} |
| 323 |
unsafe fn destroy_texture_view(&self, view: Resource) {} |
| 324 |
unsafe fn create_sampler(&self, desc: &crate::SamplerDescriptor) -> DeviceResult<Resource> { |
| 325 |
Ok(Resource) |
| 326 |
} |
| 327 |
unsafe fn destroy_sampler(&self, sampler: Resource) {} |
| 328 |
|
| 329 |
unsafe fn create_command_encoder( |
| 330 |
&self, |
| 331 |
desc: &crate::CommandEncoderDescriptor<Context>, |
| 332 |
) -> DeviceResult<CommandBuffer> { |
| 333 |
Ok(CommandBuffer::new()) |
| 334 |
} |
| 335 |
|
| 336 |
unsafe fn create_bind_group_layout( |
| 337 |
&self, |
| 338 |
desc: &crate::BindGroupLayoutDescriptor, |
| 339 |
) -> DeviceResult<Resource> { |
| 340 |
Ok(Resource) |
| 341 |
} |
| 342 |
unsafe fn destroy_bind_group_layout(&self, bg_layout: Resource) {} |
| 343 |
unsafe fn create_pipeline_layout( |
| 344 |
&self, |
| 345 |
desc: &crate::PipelineLayoutDescriptor<Resource>, |
| 346 |
) -> DeviceResult<Resource> { |
| 347 |
Ok(Resource) |
| 348 |
} |
| 349 |
unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Resource) {} |
| 350 |
unsafe fn create_bind_group( |
| 351 |
&self, |
| 352 |
desc: &crate::BindGroupDescriptor<Resource, Buffer, Resource, Resource, Resource>, |
| 353 |
) -> DeviceResult<Resource> { |
| 354 |
Ok(Resource) |
| 355 |
} |
| 356 |
unsafe fn destroy_bind_group(&self, group: Resource) {} |
| 357 |
|
| 358 |
unsafe fn create_shader_module( |
| 359 |
&self, |
| 360 |
desc: &crate::ShaderModuleDescriptor, |
| 361 |
shader: crate::ShaderInput, |
| 362 |
) -> Result<Resource, crate::ShaderError> { |
| 363 |
Ok(Resource) |
| 364 |
} |
| 365 |
unsafe fn destroy_shader_module(&self, module: Resource) {} |
| 366 |
unsafe fn create_render_pipeline( |
| 367 |
&self, |
| 368 |
desc: &crate::RenderPipelineDescriptor<Resource, Resource, Resource>, |
| 369 |
) -> Result<Resource, crate::PipelineError> { |
| 370 |
Ok(Resource) |
| 371 |
} |
| 372 |
unsafe fn create_mesh_pipeline( |
| 373 |
&self, |
| 374 |
desc: &crate::MeshPipelineDescriptor< |
| 375 |
<Self::A as crate::Api>::PipelineLayout, |
| 376 |
<Self::A as crate::Api>::ShaderModule, |
| 377 |
<Self::A as crate::Api>::PipelineCache, |
| 378 |
>, |
| 379 |
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> { |
| 380 |
Ok(Resource) |
| 381 |
} |
| 382 |
unsafe fn destroy_render_pipeline(&self, pipeline: Resource) {} |
| 383 |
unsafe fn create_compute_pipeline( |
| 384 |
&self, |
| 385 |
desc: &crate::ComputePipelineDescriptor<Resource, Resource, Resource>, |
| 386 |
) -> Result<Resource, crate::PipelineError> { |
| 387 |
Ok(Resource) |
| 388 |
} |
| 389 |
unsafe fn destroy_compute_pipeline(&self, pipeline: Resource) {} |
| 390 |
unsafe fn create_pipeline_cache( |
| 391 |
&self, |
| 392 |
desc: &crate::PipelineCacheDescriptor<'_>, |
| 393 |
) -> Result<Resource, crate::PipelineCacheError> { |
| 394 |
Ok(Resource) |
| 395 |
} |
| 396 |
unsafe fn destroy_pipeline_cache(&self, cache: Resource) {} |
| 397 |
|
| 398 |
unsafe fn create_query_set( |
| 399 |
&self, |
| 400 |
desc: &wgt::QuerySetDescriptor<crate::Label>, |
| 401 |
) -> DeviceResult<Resource> { |
| 402 |
Ok(Resource) |
| 403 |
} |
| 404 |
unsafe fn destroy_query_set(&self, set: Resource) {} |
| 405 |
unsafe fn create_fence(&self) -> DeviceResult<Fence> { |
| 406 |
Ok(Fence { |
| 407 |
value: AtomicU64::new(0), |
| 408 |
}) |
| 409 |
} |
| 410 |
unsafe fn destroy_fence(&self, fence: Fence) {} |
| 411 |
unsafe fn get_fence_value(&self, fence: &Fence) -> DeviceResult<crate::FenceValue> { |
| 412 |
Ok(fence.value.load(Ordering::Acquire)) |
| 413 |
} |
| 414 |
unsafe fn wait( |
| 415 |
&self, |
| 416 |
fence: &Fence, |
| 417 |
value: crate::FenceValue, |
| 418 |
timeout_ms: u32, |
| 419 |
) -> DeviceResult<bool> { |
| 420 |
// The relevant commands must have already been submitted, and noop-backend commands are |
| 421 |
// executed synchronously, so there is no waiting — either it is already done, |
| 422 |
// or this method was called incorrectly. |
| 423 |
assert!( |
| 424 |
fence.value.load(Ordering::Acquire) >= value, |
| 425 |
"submission must have already been done" |
| 426 |
); |
| 427 |
Ok(true) |
| 428 |
} |
| 429 |
|
| 430 |
unsafe fn start_graphics_debugger_capture(&self) -> bool { |
| 431 |
false |
| 432 |
} |
| 433 |
unsafe fn stop_graphics_debugger_capture(&self) {} |
| 434 |
unsafe fn create_acceleration_structure( |
| 435 |
&self, |
| 436 |
desc: &crate::AccelerationStructureDescriptor, |
| 437 |
) -> DeviceResult<Resource> { |
| 438 |
Ok(Resource) |
| 439 |
} |
| 440 |
unsafe fn get_acceleration_structure_build_sizes<'a>( |
| 441 |
&self, |
| 442 |
_desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, Buffer>, |
| 443 |
) -> crate::AccelerationStructureBuildSizes { |
| 444 |
Default::default() |
| 445 |
} |
| 446 |
unsafe fn get_acceleration_structure_device_address( |
| 447 |
&self, |
| 448 |
_acceleration_structure: &Resource, |
| 449 |
) -> wgt::BufferAddress { |
| 450 |
Default::default() |
| 451 |
} |
| 452 |
unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: Resource) {} |
| 453 |
|
| 454 |
fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { |
| 455 |
vec![] |
| 456 |
} |
| 457 |
|
| 458 |
fn get_internal_counters(&self) -> wgt::HalCounters { |
| 459 |
Default::default() |
| 460 |
} |
| 461 |
|
| 462 |
fn check_if_oom(&self) -> DeviceResult<()> { |
| 463 |
Ok(()) |
| 464 |
} |
| 465 |
} |