pub mod encoders;
pub mod error;
pub mod ids;
pub mod messages;
use std::ops::Range;
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use servo_base::generic_channel::{GenericOneshotSender, GenericSender, GenericSharedMemory};
use webrender_api::euclid::default::Size2D;
use webrender_api::{ImageDescriptor, ImageDescriptorFlags, ImageFormat};
pub mod markers {
pub use wgpu_core::id::markers::{
Adapter, BindGroup, BindGroupLayout, Buffer, CommandBuffer, CommandEncoder,
ComputePassEncoder, ComputePipeline, Device, ExternalTexture, PipelineLayout, QuerySet,
Queue, RenderBundle, RenderBundleEncoder, RenderPassEncoder, RenderPipeline, Sampler,
ShaderModule, Texture, TextureView,
};
}
pub mod id {
pub use wgpu_core::id::{
AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, CommandEncoderId,
ComputePassEncoderId, ComputePipelineId, DeviceId, ExternalTextureId, PipelineLayoutId,
QuerySetId, QueueId, RenderBundleEncoderId, RenderBundleId, RenderPassEncoderId,
RenderPipelineId, SamplerId, ShaderModuleId, TextureId, TextureViewId,
};
}
pub use wgpu_core::Label;
pub use wgpu_core::binding_model::{
BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, BindingResource, BufferBinding,
PipelineLayoutDescriptor,
};
pub use wgpu_core::command::{
ComputePassDescriptor, LoadOp, PassChannel, PassTimestampWrites, RenderBundleDescriptor,
RenderBundleEncoderDescriptor, RenderPassColorAttachment, RenderPassDepthStencilAttachment,
RenderPassDescriptor, StoreOp, TexelCopyBufferInfo, TexelCopyTextureInfo,
};
pub use wgpu_core::device::HostMap;
use wgpu_core::id::{ComputePipelineId, DeviceId, QueueId, RenderPipelineId};
pub use wgpu_core::identity::IdentityManager;
pub use wgpu_core::instance::RequestAdapterOptions;
use wgpu_core::limits::FailedLimit;
pub use wgpu_core::naga::front::wgsl::ImplementedLanguageExtension;
use wgpu_core::pipeline::CreateShaderModuleError;
pub use wgpu_core::pipeline::{
ComputePipelineDescriptor, FragmentState, ProgrammableStageDescriptor,
RenderPipelineDescriptor, ShaderModuleDescriptor, VertexBufferLayout, VertexState,
};
pub use wgpu_core::resource::{
BufferAccessError, BufferDescriptor, QuerySetDescriptor, SamplerDescriptor, TextureDescriptor,
TextureViewDescriptor,
};
use wgpu_types::COPY_BYTES_PER_ROW_ALIGNMENT;
pub use wgpu_types::{
AdapterInfo, AddressMode, AstcBlock, AstcChannel, BindGroupLayoutEntry, BindingType,
BlendComponent, BlendFactor, BlendOperation, BlendState, BufferAddress, BufferBindingType,
BufferSize, BufferUsages, COPY_BUFFER_ALIGNMENT, Color, ColorTargetState, ColorWrites,
CommandBufferDescriptor, CommandEncoderDescriptor, CompareFunction, DepthBiasState,
DepthStencilState, DeviceDescriptor, DeviceType, ExperimentalFeatures, Extent3d, Face,
Features, FilterMode, FrontFace, ImageSubresourceRange, IndexFormat, Limits, MAP_ALIGNMENT,
MemoryHints, MipmapFilterMode, MultisampleState, Origin2d, Origin3d, PowerPreference,
PredefinedColorSpace, PrimitiveState, PrimitiveTopology, QueryType, RenderBundleDepthStencil,
SamplerBindingType, ShaderStages, StencilFaceState, StencilOperation, StencilState,
StorageTextureAccess, TexelCopyBufferLayout, TextureAspect, TextureDimension, TextureFormat,
TextureSampleType, TextureUsages, TextureViewDimension, Trace, VertexAttribute, VertexFormat,
VertexStepMode,
};
pub use crate::encoders::*;
pub use crate::error::*;
pub use crate::ids::*;
pub use crate::messages::*;
pub const PRESENTATION_BUFFER_COUNT: usize = 10;
pub type WebGPUAdapterResponse = Option<Result<Adapter, String>>;
pub type WebGPUComputePipelineResponse = Result<Pipeline<ComputePipelineId>, Error>;
pub type WebGPUPoppedErrorScopeResponse = Result<Option<Error>, PopError>;
pub type WebGPURenderPipelineResponse = Result<Pipeline<RenderPipelineId>, Error>;
#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
pub struct WebGPU(pub GenericSender<WebGPURequest>);
impl WebGPU {
pub fn exit(&self, sender: GenericOneshotSender<()>) -> Result<(), &'static str> {
self.0
.send(WebGPURequest::Exit(sender))
.map_err(|_| "Failed to send Exit message")
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Adapter {
pub adapter_info: AdapterInfo,
pub adapter_id: WebGPUAdapter,
pub features: Features,
pub limits: Limits,
pub channel: WebGPU,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct ContextConfiguration {
pub device_id: DeviceId,
pub queue_id: QueueId,
pub format: ImageFormat,
pub is_opaque: bool,
pub size: Size2D<u32>,
}
impl ContextConfiguration {
pub fn stride(&self) -> u32 {
(self.size.width * self.format.bytes_per_pixel() as u32)
.next_multiple_of(COPY_BYTES_PER_ROW_ALIGNMENT)
}
pub fn buffer_size(&self) -> u64 {
self.stride() as u64 * self.size.height as u64
}
}
impl From<ContextConfiguration> for ImageDescriptor {
fn from(config: ContextConfiguration) -> Self {
ImageDescriptor {
format: config.format,
size: config.size.cast().cast_unit(),
stride: Some(config.stride() as i32),
offset: 0,
flags: if config.is_opaque {
ImageDescriptorFlags::IS_OPAQUE
} else {
ImageDescriptorFlags::empty()
},
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum DeviceLostReason {
Unknown,
Destroyed,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShaderCompilationInfo {
pub line_number: u64,
pub line_pos: u64,
pub offset: u64,
pub length: u64,
pub message: String,
}
impl ShaderCompilationInfo {
pub fn from(error: &CreateShaderModuleError, source: &str) -> Self {
let location = match error {
CreateShaderModuleError::Parsing(e) => e.inner.location(source),
CreateShaderModuleError::Validation(e) => e.inner.location(source),
_ => None,
};
if let Some(location) = location {
fn len_utf16(s: &str) -> u64 {
s.chars().map(|c| c.len_utf16() as u64).sum()
}
let start = location.offset as usize;
let end = start + location.length as usize;
let line_start = source[0..start].rfind('\n').map(|pos| pos + 1).unwrap_or(0);
Self {
line_number: location.line_number as u64,
line_pos: len_utf16(&source[line_start..start]) + 1,
offset: len_utf16(&source[0..start]),
length: len_utf16(&source[start..end]),
message: error.to_string(),
}
} else {
Self {
message: error.to_string(),
..Default::default()
}
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Pipeline<T: std::fmt::Debug + Serialize> {
pub id: T,
pub label: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Mapping {
pub data: GenericSharedMemory,
pub mode: HostMap,
pub range: Range<u64>,
}
pub type WebGPUDeviceResponse = (
WebGPUDevice,
WebGPUQueue,
Result<DeviceDescriptor<Option<String>>, RequestDeviceError>,
);
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum RequestDeviceError {
LimitsExceeded(FailedLimit),
UnsupportedFeature(Features),
Other(String),
}
impl From<wgpu_core::instance::RequestDeviceError> for RequestDeviceError {
fn from(value: wgpu_core::instance::RequestDeviceError) -> Self {
match value {
wgpu_core::instance::RequestDeviceError::LimitsExceeded(failed_limit) => {
RequestDeviceError::LimitsExceeded(failed_limit)
},
wgpu_core::instance::RequestDeviceError::UnsupportedFeature(features) => {
RequestDeviceError::UnsupportedFeature(features)
},
e => RequestDeviceError::Other(e.to_string()),
}
}
}