Merge pull request #1524 from bungoboingo/fix/gradients_wasm

Additional cfg for wasm target for gradients
This commit is contained in:
Héctor Ramón 2022-11-15 21:32:18 +01:00 committed by GitHub
commit 5b0dfcd0b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 3 deletions

View file

@ -141,6 +141,7 @@ impl Pipeline {
triangle::Style::Solid(color) => { triangle::Style::Solid(color) => {
self.programs.solid.use_program(gl, color, &transform); self.programs.solid.use_program(gl, color, &transform);
} }
#[cfg(not(target_arch = "wasm32"))]
triangle::Style::Gradient(gradient) => { triangle::Style::Gradient(gradient) => {
self.programs self.programs
.gradient .gradient

View file

@ -1,5 +1,7 @@
//! Draw geometry using meshes of triangles. //! Draw geometry using meshes of triangles.
use crate::{Color, Gradient}; use crate::Color;
#[cfg(not(target_arch = "wasm32"))]
use crate::Gradient;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
@ -27,6 +29,7 @@ pub struct Vertex2D {
pub enum Style { pub enum Style {
/// Fill a primitive with a solid color. /// Fill a primitive with a solid color.
Solid(Color), Solid(Color),
#[cfg(not(target_arch = "wasm32"))]
/// Fill a primitive with an interpolated color. /// Fill a primitive with an interpolated color.
Gradient(Gradient), Gradient(Gradient),
} }
@ -37,6 +40,7 @@ impl From<Color> for Style {
} }
} }
#[cfg(not(target_arch = "wasm32"))]
impl From<Gradient> for Style { impl From<Gradient> for Style {
fn from(gradient: Gradient) -> Self { fn from(gradient: Gradient) -> Self {
Self::Gradient(gradient) Self::Gradient(gradient)

View file

@ -76,6 +76,7 @@ impl Transform {
fn transform_style(&self, style: triangle::Style) -> triangle::Style { fn transform_style(&self, style: triangle::Style) -> triangle::Style {
match style { match style {
triangle::Style::Solid(color) => triangle::Style::Solid(color), triangle::Style::Solid(color) => triangle::Style::Solid(color),
#[cfg(not(target_arch = "wasm32"))]
triangle::Style::Gradient(gradient) => { triangle::Style::Gradient(gradient) => {
triangle::Style::Gradient(self.transform_gradient(gradient)) triangle::Style::Gradient(self.transform_gradient(gradient))
} }

View file

@ -24,6 +24,7 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
) )
} }
#[cfg(not(target_arch = "wasm32"))]
/// Creates a new dynamic storage buffer. /// Creates a new dynamic storage buffer.
pub fn storage(device: &wgpu::Device, label: &'static str) -> Self { pub fn storage(device: &wgpu::Device, label: &'static str) -> Self {
Buffer::new( Buffer::new(
@ -91,6 +92,7 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
Internal::Uniform(_) => { Internal::Uniform(_) => {
wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST
} }
#[cfg(not(target_arch = "wasm32"))]
Internal::Storage(_) => { Internal::Storage(_) => {
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
} }
@ -154,6 +156,8 @@ impl<T: ShaderType + WriteInto> Buffer<T> {
// Currently supported dynamic buffers. // Currently supported dynamic buffers.
enum Internal { enum Internal {
Uniform(encase::DynamicUniformBuffer<Vec<u8>>), Uniform(encase::DynamicUniformBuffer<Vec<u8>>),
#[cfg(not(target_arch = "wasm32"))]
//storage buffers are not supported on wgpu wasm target (yet)
Storage(encase::DynamicStorageBuffer<Vec<u8>>), Storage(encase::DynamicStorageBuffer<Vec<u8>>),
} }
@ -168,6 +172,7 @@ impl Internal {
.write(value) .write(value)
.expect("Error when writing to dynamic uniform buffer.") .expect("Error when writing to dynamic uniform buffer.")
as u32, as u32,
#[cfg(not(target_arch = "wasm32"))]
Internal::Storage(buf) => buf Internal::Storage(buf) => buf
.write(value) .write(value)
.expect("Error when writing to dynamic storage buffer.") .expect("Error when writing to dynamic storage buffer.")
@ -179,6 +184,7 @@ impl Internal {
pub(super) fn get_ref(&self) -> &Vec<u8> { pub(super) fn get_ref(&self) -> &Vec<u8> {
match self { match self {
Internal::Uniform(buf) => buf.as_ref(), Internal::Uniform(buf) => buf.as_ref(),
#[cfg(not(target_arch = "wasm32"))]
Internal::Storage(buf) => buf.as_ref(), Internal::Storage(buf) => buf.as_ref(),
} }
} }
@ -190,6 +196,7 @@ impl Internal {
buf.as_mut().clear(); buf.as_mut().clear();
buf.set_offset(0); buf.set_offset(0);
} }
#[cfg(not(target_arch = "wasm32"))]
Internal::Storage(buf) => { Internal::Storage(buf) => {
buf.as_mut().clear(); buf.as_mut().clear();
buf.set_offset(0); buf.set_offset(0);

View file

@ -1,4 +1,5 @@
//! Draw meshes of triangles. //! Draw meshes of triangles.
#[cfg(not(target_arch = "wasm32"))]
mod gradient; mod gradient;
mod msaa; mod msaa;
mod solid; mod solid;
@ -27,6 +28,8 @@ pub(crate) struct Pipeline {
/// Supported triangle pipelines for different fills. /// Supported triangle pipelines for different fills.
pub(crate) struct PipelineList { pub(crate) struct PipelineList {
solid: solid::Pipeline, solid: solid::Pipeline,
/// Gradients are currently not supported on WASM targets due to their need of storage buffers.
#[cfg(not(target_arch = "wasm32"))]
gradient: gradient::Pipeline, gradient: gradient::Pipeline,
} }
@ -40,8 +43,11 @@ impl PipelineList {
/// Resets each pipeline's buffers. /// Resets each pipeline's buffers.
fn clear(&mut self) { fn clear(&mut self) {
self.solid.buffer.clear(); self.solid.buffer.clear();
self.gradient.uniform_buffer.clear(); #[cfg(not(target_arch = "wasm32"))]
self.gradient.storage_buffer.clear(); {
self.gradient.uniform_buffer.clear();
self.gradient.storage_buffer.clear();
}
} }
/// Writes the contents of each pipeline's CPU buffer to the GPU, resizing the GPU buffer /// Writes the contents of each pipeline's CPU buffer to the GPU, resizing the GPU buffer
@ -53,6 +59,7 @@ impl PipelineList {
encoder: &mut wgpu::CommandEncoder, encoder: &mut wgpu::CommandEncoder,
) { ) {
self.solid.write(device, staging_belt, encoder); self.solid.write(device, staging_belt, encoder);
#[cfg(not(target_arch = "wasm32"))]
self.gradient.write(device, staging_belt, encoder); self.gradient.write(device, staging_belt, encoder);
} }
} }
@ -79,6 +86,7 @@ impl Pipeline {
index_strides: Vec::new(), index_strides: Vec::new(),
pipelines: PipelineList { pipelines: PipelineList {
solid: solid::Pipeline::new(device, format, antialiasing), solid: solid::Pipeline::new(device, format, antialiasing),
#[cfg(not(target_arch = "wasm32"))]
gradient: gradient::Pipeline::new(device, format, antialiasing), gradient: gradient::Pipeline::new(device, format, antialiasing),
}, },
} }
@ -145,6 +153,7 @@ impl Pipeline {
triangle::Style::Solid(color) => { triangle::Style::Solid(color) => {
self.pipelines.solid.push(transform, color); self.pipelines.solid.push(transform, color);
} }
#[cfg(not(target_arch = "wasm32"))]
triangle::Style::Gradient(gradient) => { triangle::Style::Gradient(gradient) => {
self.pipelines.gradient.push(transform, gradient); self.pipelines.gradient.push(transform, gradient);
} }
@ -186,6 +195,7 @@ impl Pipeline {
}); });
let mut num_solids = 0; let mut num_solids = 0;
#[cfg(not(target_arch = "wasm32"))]
let mut num_gradients = 0; let mut num_gradients = 0;
let mut last_is_solid = None; let mut last_is_solid = None;
@ -216,6 +226,7 @@ impl Pipeline {
num_solids += 1; num_solids += 1;
} }
#[cfg(not(target_arch = "wasm32"))]
triangle::Style::Gradient(_) => { triangle::Style::Gradient(_) => {
if last_is_solid.unwrap_or(true) { if last_is_solid.unwrap_or(true) {
self.pipelines self.pipelines