Skip to content

Commit

Permalink
Merge #514
Browse files Browse the repository at this point in the history
514: Move some types into shared wgpu-types crate r=kvark a=grovesNL

As we discussed a while ago, we need to be able to share some types between wgpu-core/wgpu-native/wgpu-remote/wgpu-rs.

The problem is that we want to avoid a dependency on wgpu-core and wgpu-native when building [wgpu-rs for the wasm32-unknown-unknown target](gfx-rs/wgpu-rs#101). We can avoid this by moving all shared types into a separate crate which is exposed on all targets.

Let me know if we should use some other approach or organize the types somehow. This isn't complete yet, but it might be easier to integrate this over several PRs instead of diverging my branch too far.

Co-authored-by: Joshua Groves <josh@joshgroves.com>
  • Loading branch information
bors[bot] and grovesNL authored Mar 10, 2020
2 parents 79e9ab7 + df3db1c commit f1d459c
Show file tree
Hide file tree
Showing 27 changed files with 660 additions and 561 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"wgpu-core",
"wgpu-native",
"wgpu-remote",
"wgpu-types",
]
5 changes: 5 additions & 0 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ serde = { version = "1.0", features = ["serde_derive"], optional = true }
smallvec = "1.0"
vec_map = "0.8"

[dependencies.wgt]
path = "../wgpu-types"
package = "wgpu-types"
version = "0.1"

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
gfx-backend-metal = { version = "0.4" }
gfx-backend-vulkan = { version = "0.4", optional = true }
Expand Down
22 changes: 4 additions & 18 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,21 @@

use crate::{
id::{BindGroupLayoutId, BufferId, DeviceId, SamplerId, TextureViewId},
resource::TextureViewDimension,
track::{DUMMY_SELECTOR, TrackerSet},
BufferAddress,
FastHashMap,
LifeGuard,
RefCount,
Stored,
};

use wgt::BufferAddress;
use arrayvec::ArrayVec;
use rendy_descriptor::{DescriptorRanges, DescriptorSet};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;

pub const MAX_BIND_GROUPS: usize = 4;

bitflags::bitflags! {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ShaderStage: u32 {
const NONE = 0;
const VERTEX = 1;
const FRAGMENT = 2;
const COMPUTE = 4;
}
}

#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -50,9 +36,9 @@ pub enum BindingType {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BindGroupLayoutBinding {
pub binding: u32,
pub visibility: ShaderStage,
pub visibility: wgt::ShaderStage,
pub ty: BindingType,
pub texture_dimension: TextureViewDimension,
pub texture_dimension: wgt::TextureViewDimension,
pub multisampled: bool,
pub dynamic: bool,
}
Expand Down Expand Up @@ -82,7 +68,7 @@ pub struct PipelineLayoutDescriptor {
#[derive(Debug)]
pub struct PipelineLayout<B: hal::Backend> {
pub(crate) raw: B::PipelineLayout,
pub(crate) bind_group_layout_ids: ArrayVec<[BindGroupLayoutId; MAX_BIND_GROUPS]>,
pub(crate) bind_group_layout_ids: ArrayVec<[BindGroupLayoutId; wgt::MAX_BIND_GROUPS]>,
}

#[repr(C)]
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::{
hub::{GfxBackend, Global, Token},
id,
resource::BufferUsage,
BufferAddress,
DynamicOffset,
};

use wgt::BufferAddress;
use hal::command::CommandBuffer as _;
use peek_poke::{Peek, PeekCopy, Poke};

Expand Down Expand Up @@ -222,10 +222,10 @@ pub mod compute_ffi {
};
use crate::{
id,
BufferAddress,
DynamicOffset,
RawString,
};
use wgt::BufferAddress;
use std::{convert::TryInto, slice};

/// # Safety
Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ use crate::{
},
hub::{GfxBackend, Global, Token},
id,
pipeline::{IndexFormat, InputStepMode, PipelineFlags},
pipeline::PipelineFlags,
resource::{BufferUsage, TextureUsage, TextureViewInner},
track::TrackerSet,
BufferAddress,
Color,
DynamicOffset,
Stored,
};

use wgt::{BufferAddress, IndexFormat, InputStepMode};
use arrayvec::ArrayVec;
use hal::command::CommandBuffer as _;
use peek_poke::{Peek, PeekCopy, Poke};
Expand Down Expand Up @@ -1166,11 +1166,11 @@ pub mod render_ffi {
};
use crate::{
id,
BufferAddress,
Color,
DynamicOffset,
RawString,
};
use wgt::BufferAddress;
use std::{convert::TryInto, slice};

/// # Safety
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use crate::{
hub::{GfxBackend, Global, Token},
id::{BufferId, CommandEncoderId, TextureId},
resource::{BufferUsage, TextureUsage},
BufferAddress,
Extent3d,
Origin3d,
};

use wgt::BufferAddress;
use hal::command::CommandBuffer as _;

use std::iter;
Expand Down
77 changes: 39 additions & 38 deletions wgpu-core/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{binding_model, command, pipeline, resource, Color, Extent3d, Features, Origin3d};
use crate::{binding_model, command, resource, Color, Extent3d, Features, Origin3d};
use wgt::{BlendDescriptor, BlendFactor, ColorStateDescriptor, ColorWrite, CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat, PrimitiveTopology, StencilOperation, StencilStateFaceDescriptor, TextureFormat, RasterizationStateDescriptor, VertexFormat};

pub fn map_buffer_usage(
usage: resource::BufferUsage,
Expand Down Expand Up @@ -104,9 +105,9 @@ pub fn map_binding_type(
}

pub fn map_shader_stage_flags(
shader_stage_flags: binding_model::ShaderStage,
shader_stage_flags: wgt::ShaderStage,
) -> hal::pso::ShaderStageFlags {
use crate::binding_model::ShaderStage as Ss;
use wgt::ShaderStage as Ss;
use hal::pso::ShaderStageFlags as H;

let mut value = H::empty();
Expand Down Expand Up @@ -139,9 +140,9 @@ pub fn map_extent(extent: Extent3d) -> hal::image::Extent {
}

pub fn map_primitive_topology(
primitive_topology: pipeline::PrimitiveTopology,
primitive_topology: PrimitiveTopology,
) -> hal::pso::Primitive {
use crate::pipeline::PrimitiveTopology as Pt;
use wgt::PrimitiveTopology as Pt;
use hal::pso::Primitive as H;
match primitive_topology {
Pt::PointList => H::PointList,
Expand All @@ -153,11 +154,11 @@ pub fn map_primitive_topology(
}

pub fn map_color_state_descriptor(
desc: &pipeline::ColorStateDescriptor,
desc: &ColorStateDescriptor,
) -> hal::pso::ColorBlendDesc {
let color_mask = desc.write_mask;
let blend_state = if desc.color_blend != pipeline::BlendDescriptor::REPLACE
|| desc.alpha_blend != pipeline::BlendDescriptor::REPLACE
let blend_state = if desc.color_blend != BlendDescriptor::REPLACE
|| desc.alpha_blend != BlendDescriptor::REPLACE
{
Some(hal::pso::BlendState {
color: map_blend_descriptor(&desc.color_blend),
Expand All @@ -172,8 +173,8 @@ pub fn map_color_state_descriptor(
}
}

fn map_color_write_flags(flags: pipeline::ColorWrite) -> hal::pso::ColorMask {
use crate::pipeline::ColorWrite as Cw;
fn map_color_write_flags(flags: ColorWrite) -> hal::pso::ColorMask {
use wgt::ColorWrite as Cw;
use hal::pso::ColorMask as H;

let mut value = H::empty();
Expand All @@ -192,8 +193,8 @@ fn map_color_write_flags(flags: pipeline::ColorWrite) -> hal::pso::ColorMask {
value
}

fn map_blend_descriptor(blend_desc: &pipeline::BlendDescriptor) -> hal::pso::BlendOp {
use crate::pipeline::BlendOperation as Bo;
fn map_blend_descriptor(blend_desc: &BlendDescriptor) -> hal::pso::BlendOp {
use wgt::BlendOperation as Bo;
use hal::pso::BlendOp as H;
match blend_desc.operation {
Bo::Add => H::Add {
Expand All @@ -213,8 +214,8 @@ fn map_blend_descriptor(blend_desc: &pipeline::BlendDescriptor) -> hal::pso::Ble
}
}

fn map_blend_factor(blend_factor: pipeline::BlendFactor) -> hal::pso::Factor {
use crate::pipeline::BlendFactor as Bf;
fn map_blend_factor(blend_factor: BlendFactor) -> hal::pso::Factor {
use wgt::BlendFactor as Bf;
use hal::pso::Factor as H;
match blend_factor {
Bf::Zero => H::Zero,
Expand All @@ -234,11 +235,11 @@ fn map_blend_factor(blend_factor: pipeline::BlendFactor) -> hal::pso::Factor {
}

pub fn map_depth_stencil_state_descriptor(
desc: &pipeline::DepthStencilStateDescriptor,
desc: &DepthStencilStateDescriptor,
) -> hal::pso::DepthStencilDesc {
hal::pso::DepthStencilDesc {
depth: if desc.depth_write_enabled
|| desc.depth_compare != resource::CompareFunction::Always
|| desc.depth_compare != CompareFunction::Always
{
Some(hal::pso::DepthTest {
fun: map_compare_function(desc.depth_compare),
Expand All @@ -250,8 +251,8 @@ pub fn map_depth_stencil_state_descriptor(
depth_bounds: false, // TODO
stencil: if desc.stencil_read_mask != !0
|| desc.stencil_write_mask != !0
|| desc.stencil_front != pipeline::StencilStateFaceDescriptor::IGNORE
|| desc.stencil_back != pipeline::StencilStateFaceDescriptor::IGNORE
|| desc.stencil_front != StencilStateFaceDescriptor::IGNORE
|| desc.stencil_back != StencilStateFaceDescriptor::IGNORE
{
Some(hal::pso::StencilTest {
faces: hal::pso::Sided {
Expand All @@ -273,7 +274,7 @@ pub fn map_depth_stencil_state_descriptor(
}

fn map_stencil_face(
stencil_state_face_desc: &pipeline::StencilStateFaceDescriptor,
stencil_state_face_desc: &StencilStateFaceDescriptor,
) -> hal::pso::StencilFace {
hal::pso::StencilFace {
fun: map_compare_function(stencil_state_face_desc.compare),
Expand All @@ -283,8 +284,8 @@ fn map_stencil_face(
}
}

pub fn map_compare_function(compare_function: resource::CompareFunction) -> hal::pso::Comparison {
use crate::resource::CompareFunction as Cf;
pub fn map_compare_function(compare_function: CompareFunction) -> hal::pso::Comparison {
use wgt::CompareFunction as Cf;
use hal::pso::Comparison as H;
match compare_function {
Cf::Never => H::Never,
Expand All @@ -298,8 +299,8 @@ pub fn map_compare_function(compare_function: resource::CompareFunction) -> hal:
}
}

fn map_stencil_operation(stencil_operation: pipeline::StencilOperation) -> hal::pso::StencilOp {
use crate::pipeline::StencilOperation as So;
fn map_stencil_operation(stencil_operation: StencilOperation) -> hal::pso::StencilOp {
use wgt::StencilOperation as So;
use hal::pso::StencilOp as H;
match stencil_operation {
So::Keep => H::Keep,
Expand All @@ -314,10 +315,10 @@ fn map_stencil_operation(stencil_operation: pipeline::StencilOperation) -> hal::
}

pub(crate) fn map_texture_format(
texture_format: resource::TextureFormat,
texture_format: TextureFormat,
features: Features,
) -> hal::format::Format {
use crate::resource::TextureFormat as Tf;
use wgt::TextureFormat as Tf;
use hal::format::Format as H;
match texture_format {
// Normal 8 bit formats
Expand Down Expand Up @@ -393,8 +394,8 @@ pub(crate) fn map_texture_format(
}
}

pub fn map_vertex_format(vertex_format: pipeline::VertexFormat) -> hal::format::Format {
use crate::pipeline::VertexFormat as Vf;
pub fn map_vertex_format(vertex_format: VertexFormat) -> hal::format::Format {
use wgt::VertexFormat as Vf;
use hal::format::Format as H;
match vertex_format {
Vf::Uchar2 => H::Rg8Uint,
Expand Down Expand Up @@ -482,9 +483,9 @@ pub fn map_texture_dimension_size(
}

pub fn map_texture_view_dimension(
dimension: resource::TextureViewDimension,
dimension: wgt::TextureViewDimension,
) -> hal::image::ViewKind {
use crate::resource::TextureViewDimension::*;
use wgt::TextureViewDimension::*;
use hal::image::ViewKind as H;
match dimension {
D1 => H::D1,
Expand Down Expand Up @@ -628,19 +629,19 @@ pub fn map_wrap(address: resource::AddressMode) -> hal::image::WrapMode {
}

pub fn map_rasterization_state_descriptor(
desc: &pipeline::RasterizationStateDescriptor,
desc: &RasterizationStateDescriptor,
) -> hal::pso::Rasterizer {
hal::pso::Rasterizer {
depth_clamping: false,
polygon_mode: hal::pso::PolygonMode::Fill,
cull_face: match desc.cull_mode {
pipeline::CullMode::None => hal::pso::Face::empty(),
pipeline::CullMode::Front => hal::pso::Face::FRONT,
pipeline::CullMode::Back => hal::pso::Face::BACK,
CullMode::None => hal::pso::Face::empty(),
CullMode::Front => hal::pso::Face::FRONT,
CullMode::Back => hal::pso::Face::BACK,
},
front_face: match desc.front_face {
pipeline::FrontFace::Ccw => hal::pso::FrontFace::CounterClockwise,
pipeline::FrontFace::Cw => hal::pso::FrontFace::Clockwise,
FrontFace::Ccw => hal::pso::FrontFace::CounterClockwise,
FrontFace::Cw => hal::pso::FrontFace::Clockwise,
},
depth_bias: if desc.depth_bias != 0
|| desc.depth_bias_slope_scale != 0.0
Expand All @@ -658,9 +659,9 @@ pub fn map_rasterization_state_descriptor(
}
}

pub fn map_index_format(index_format: pipeline::IndexFormat) -> hal::IndexType {
pub fn map_index_format(index_format: IndexFormat) -> hal::IndexType {
match index_format {
pipeline::IndexFormat::Uint16 => hal::IndexType::U16,
pipeline::IndexFormat::Uint32 => hal::IndexType::U32,
IndexFormat::Uint16 => hal::IndexType::U16,
IndexFormat::Uint32 => hal::IndexType::U32,
}
}
Loading

0 comments on commit f1d459c

Please sign in to comment.