From 26e7389e385f0742b1757ccf15688c5985486aba Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Fri, 20 May 2022 19:04:48 +0200 Subject: [PATCH 1/2] Make animate_shader example work on WASM Previously, it would draw nothing and complain with the following console warnings: Firefox: WebGL warning: drawElementsInstanced: Buffer for uniform block is smaller than UNIFORM_BLOCK_DATA_SIZE Chrome: [.WebGL-00003B081B72A300] GL_INVALID_OPERATION: It is undefined behaviour to use a uniform buffer that is too small. Issue: #4813 --- examples/shader/animate_shader.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/shader/animate_shader.rs b/examples/shader/animate_shader.rs index dd46d6293d985..8dcf2a7596b0a 100644 --- a/examples/shader/animate_shader.rs +++ b/examples/shader/animate_shader.rs @@ -16,7 +16,10 @@ use bevy::{ AddRenderCommand, DrawFunctions, EntityRenderCommand, RenderCommandResult, RenderPhase, SetItemPipeline, TrackedRenderPass, }, - render_resource::*, + render_resource::{ + std140::{AsStd140, Std140}, + *, + }, renderer::{RenderDevice, RenderQueue}, view::{ComputedVisibility, ExtractedView, Msaa, Visibility}, RenderApp, RenderStage, @@ -59,7 +62,7 @@ impl Plugin for CustomMaterialPlugin { let render_device = app.world.resource::(); let buffer = render_device.create_buffer(&BufferDescriptor { label: Some("time uniform buffer"), - size: std::mem::size_of::() as u64, + size: TimeUniform::std140_size_static() as u64, usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST, mapped_at_creation: false, }); @@ -133,14 +136,14 @@ fn queue_custom( } } -#[derive(Default)] -struct ExtractedTime { +#[derive(Default, Clone, AsStd140)] +struct TimeUniform { seconds_since_startup: f32, } // extract the passed time into a resource in the render world fn extract_time(mut commands: Commands, time: Res