Skip to content

Commit 6813b2e

Browse files
committed
WebGL2 hacks
1 parent 6ef9b65 commit 6813b2e

File tree

12 files changed

+124
-17
lines changed

12 files changed

+124
-17
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ subpixel_glyph_atlas = ["bevy_internal/subpixel_glyph_atlas"]
9797
# enable systems that allow for automated testing on CI
9898
bevy_ci_testing = ["bevy_internal/bevy_ci_testing"]
9999

100+
[patch.crates-io]
101+
wgpu = { path = "../wgpu/wgpu" }
102+
100103
[dependencies]
101104
bevy_dylib = { path = "crates/bevy_dylib", version = "0.5.0", default-features = false, optional = true }
102105
bevy_internal = { path = "crates/bevy_internal", version = "0.5.0", default-features = false }

crates/bevy_wgpu/src/wgpu_type_converter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ impl WgpuFrom<WgpuLimits> for wgpu::Limits {
721721
max_vertex_buffer_array_stride: val.max_vertex_buffer_array_stride,
722722
min_storage_buffer_offset_alignment: val.min_storage_buffer_offset_alignment,
723723
min_uniform_buffer_offset_alignment: val.min_uniform_buffer_offset_alignment,
724+
..Default::default()
724725
}
725726
}
726727
}

examples/3d/3d_scene_pipelined.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ use bevy::{
1313
camera::{OrthographicProjection, PerspectiveCameraBundle},
1414
color::Color,
1515
mesh::{shape, Mesh},
16+
view::Msaa,
1617
},
18+
window::WindowDescriptor,
1719
PipelinedDefaultPlugins,
1820
};
1921

2022
fn main() {
2123
App::new()
24+
.insert_resource(Msaa { samples: 1 })
25+
.insert_resource(WindowDescriptor {
26+
scale_factor_override: Some(1.0),
27+
..Default::default()
28+
})
2229
.add_plugins(PipelinedDefaultPlugins)
2330
.add_plugin(FrameTimeDiagnosticsPlugin::default())
2431
.add_plugin(LogDiagnosticsPlugin::default())

examples/3d/load_gltf_pipelined.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ use bevy::{
77
render2::{
88
camera::{OrthographicProjection, PerspectiveCameraBundle},
99
color::Color,
10+
view::Msaa,
1011
},
12+
window::WindowDescriptor,
1113
PipelinedDefaultPlugins,
1214
};
1315

1416
fn main() {
1517
App::new()
18+
.insert_resource(Msaa { samples: 1 })
19+
.insert_resource(WindowDescriptor {
20+
scale_factor_override: Some(1.0),
21+
..Default::default()
22+
})
1623
.insert_resource(AmbientLight {
1724
color: Color::WHITE,
1825
brightness: 1.0 / 5.0f32,

index.html.template

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
</head>
6+
<body>
7+
<script type="module">
8+
import init from "./${EXAMPLE_NAME}.js";
9+
window.addEventListener("load", () => {
10+
init();
11+
});
12+
</script>
13+
</body>
14+
</html>
15+

pipelined/bevy_pbr2/src/light.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub struct DirectionalLightShadowMap {
148148

149149
impl Default for DirectionalLightShadowMap {
150150
fn default() -> Self {
151-
Self { size: 4096 }
151+
Self { size: 1024 }
152152
}
153153
}
154154

pipelined/bevy_pbr2/src/render/light.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,9 @@ pub struct GpuLights {
137137

138138
// NOTE: this must be kept in sync with the same constants in pbr.frag
139139
pub const MAX_POINT_LIGHTS: usize = 256;
140+
pub const MAX_POINT_LIGHT_SHADOW_MAPS: usize = 1;
140141
pub const MAX_DIRECTIONAL_LIGHTS: usize = 1;
141-
// FIXME: How should we handle shadows for clustered forward? Limiting to maximum 10
142-
// point light shadow maps for now
143-
pub const POINT_SHADOW_LAYERS: u32 = (6 * 10) as u32;
142+
pub const POINT_SHADOW_LAYERS: u32 = (6 * MAX_POINT_LIGHT_SHADOW_MAPS) as u32;
144143
pub const DIRECTIONAL_SHADOW_LAYERS: u32 = MAX_DIRECTIONAL_LIGHTS as u32;
145144
pub const SHADOW_FORMAT: TextureFormat = TextureFormat::Depth32Float;
146145

@@ -189,6 +188,8 @@ impl FromWorld for ShadowPipeline {
189188
min_filter: FilterMode::Linear,
190189
mipmap_filter: FilterMode::Nearest,
191190
compare: Some(CompareFunction::GreaterEqual),
191+
// compare: Some(CompareFunction::LessEqual),
192+
// compare: None,
192193
..Default::default()
193194
}),
194195
directional_light_sampler: render_device.create_sampler(&SamplerDescriptor {
@@ -199,6 +200,7 @@ impl FromWorld for ShadowPipeline {
199200
min_filter: FilterMode::Linear,
200201
mipmap_filter: FilterMode::Nearest,
201202
compare: Some(CompareFunction::GreaterEqual),
203+
// compare: None,
202204
..Default::default()
203205
}),
204206
}
@@ -646,8 +648,8 @@ pub fn prepare_lights(
646648
};
647649

648650
// TODO: this should select lights based on relevance to the view instead of the first ones that show up in a query
649-
for (light_entity, light) in point_lights.iter() {
650-
if !light.shadows_enabled {
651+
for (i, (light_entity, light)) in point_lights.iter().enumerate().take(MAX_POINT_LIGHTS) {
652+
if !light.shadows_enabled || i >= MAX_POINT_LIGHT_SHADOW_MAPS {
651653
continue;
652654
}
653655
let light_index = *global_light_meta
@@ -787,9 +789,11 @@ pub fn prepare_lights(
787789
point_light_depth_texture
788790
.texture
789791
.create_view(&TextureViewDescriptor {
790-
label: Some("point_light_shadow_map_array_texture_view"),
792+
// label: Some("point_light_shadow_map_array_texture_view"),
793+
label: Some("point_light_shadow_map_texture_view"),
791794
format: None,
792-
dimension: Some(TextureViewDimension::CubeArray),
795+
// dimension: Some(TextureViewDimension::CubeArray),
796+
dimension: Some(TextureViewDimension::Cube),
793797
aspect: TextureAspect::All,
794798
base_mip_level: 0,
795799
mip_level_count: None,
@@ -799,9 +803,11 @@ pub fn prepare_lights(
799803
let directional_light_depth_texture_view = directional_light_depth_texture
800804
.texture
801805
.create_view(&TextureViewDescriptor {
802-
label: Some("directional_light_shadow_map_array_texture_view"),
806+
// label: Some("directional_light_shadow_map_array_texture_view"),
807+
label: Some("directional_light_shadow_map_texture_view"),
803808
format: None,
804-
dimension: Some(TextureViewDimension::D2Array),
809+
// dimension: Some(TextureViewDimension::D2Array),
810+
dimension: Some(TextureViewDimension::D2),
805811
aspect: TextureAspect::All,
806812
base_mip_level: 0,
807813
mip_level_count: None,

pipelined/bevy_pbr2/src/render/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,24 @@ impl FromWorld for PbrPipeline {
204204
},
205205
count: None,
206206
},
207-
// Point Shadow Texture Cube Array
207+
// Point Shadow Texture Cube [Array]
208208
BindGroupLayoutEntry {
209209
binding: 2,
210210
visibility: ShaderStages::FRAGMENT,
211211
ty: BindingType::Texture {
212212
multisampled: false,
213213
sample_type: TextureSampleType::Depth,
214-
view_dimension: TextureViewDimension::CubeArray,
214+
// view_dimension: TextureViewDimension::CubeArray,
215+
view_dimension: TextureViewDimension::Cube,
215216
},
216217
count: None,
217218
},
218-
// Point Shadow Texture Array Sampler
219+
// Point Shadow Texture [Array] Sampler
219220
BindGroupLayoutEntry {
220221
binding: 3,
221222
visibility: ShaderStages::FRAGMENT,
222223
ty: BindingType::Sampler {
224+
// comparison: false,
223225
comparison: true,
224226
filtering: true,
225227
},
@@ -232,7 +234,8 @@ impl FromWorld for PbrPipeline {
232234
ty: BindingType::Texture {
233235
multisampled: false,
234236
sample_type: TextureSampleType::Depth,
235-
view_dimension: TextureViewDimension::D2Array,
237+
// view_dimension: TextureViewDimension::D2Array,
238+
view_dimension: TextureViewDimension::D2,
236239
},
237240
count: None,
238241
},
@@ -242,6 +245,7 @@ impl FromWorld for PbrPipeline {
242245
visibility: ShaderStages::FRAGMENT,
243246
ty: BindingType::Sampler {
244247
comparison: true,
248+
// comparison: false,
245249
filtering: true,
246250
},
247251
count: None,
@@ -590,6 +594,7 @@ impl SpecializedPipeline for PbrPipeline {
590594
// depth buffer
591595
depth_write_enabled = true;
592596
}
597+
shader_defs.push(String::from("NO_CUBE_ARRAY_TEXTURES_SUPPORT"));
593598
RenderPipelineDescriptor {
594599
vertex: VertexState {
595600
shader: PBR_SHADER_HANDLE.typed::<Shader>(),

pipelined/bevy_pbr2/src/render/pbr.wgsl

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,25 @@ struct ClusterOffsetsAndCounts {
184184

185185
[[group(0), binding(1)]]
186186
var<uniform> lights: Lights;
187+
#ifdef CUBE_ARRAY_TEXTURES_SUPPORT
187188
[[group(0), binding(2)]]
188189
var point_shadow_textures: texture_depth_cube_array;
189190
[[group(0), binding(3)]]
190191
var point_shadow_textures_sampler: sampler_comparison;
192+
#endif
193+
#ifdef NO_CUBE_ARRAY_TEXTURES_SUPPORT
194+
[[group(0), binding(2)]]
195+
var point_shadow_textures: texture_depth_cube;
196+
[[group(0), binding(3)]]
197+
var point_shadow_textures_sampler: sampler_comparison;
198+
// var point_shadow_textures_sampler: sampler;
199+
#endif
191200
[[group(0), binding(4)]]
192-
var directional_shadow_textures: texture_depth_2d_array;
201+
// var directional_shadow_textures: texture_depth_2d_array;
202+
var directional_shadow_textures: texture_depth_2d;
193203
[[group(0), binding(5)]]
194204
var directional_shadow_textures_sampler: sampler_comparison;
205+
// var directional_shadow_textures_sampler: sampler;
195206
[[group(0), binding(6)]]
196207
var<uniform> point_lights: PointLights;
197208
[[group(0), binding(7)]]
@@ -516,7 +527,18 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
516527
// a quad (2x2 fragments) being processed not being sampled, and this messing with
517528
// mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
518529
// from LOD 0.
530+
#ifdef CUBE_ARRAY_TEXTURES_SUPPORT
519531
return textureSampleCompareLevel(point_shadow_textures, point_shadow_textures_sampler, frag_ls, i32(light_id), depth);
532+
#endif
533+
#ifdef NO_CUBE_ARRAY_TEXTURES_SUPPORT
534+
return textureSampleCompare(point_shadow_textures, point_shadow_textures_sampler, frag_ls, depth);
535+
// let sampled_depth = textureSample(point_shadow_textures, point_shadow_textures_sampler, frag_ls);
536+
// if (sampled_depth >= depth) {
537+
// return 0.0;
538+
// } else {
539+
// return 1.0;
540+
// }
541+
#endif
520542
}
521543

522544
fn fetch_directional_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 {
@@ -547,7 +569,24 @@ fn fetch_directional_shadow(light_id: u32, frag_position: vec4<f32>, surface_nor
547569
// do the lookup, using HW PCF and comparison
548570
// NOTE: Due to non-uniform control flow above, we must use the level variant of the texture
549571
// sampler to avoid use of implicit derivatives causing possible undefined behavior.
550-
return textureSampleCompareLevel(directional_shadow_textures, directional_shadow_textures_sampler, light_local, i32(light_id), depth);
572+
// return textureSampleCompareLevel(directional_shadow_textures, directional_shadow_textures_sampler, light_local, i32(light_id), depth);
573+
return textureSampleCompareLevel(directional_shadow_textures, directional_shadow_textures_sampler, light_local, depth);
574+
// let sampled_depth = textureSample(directional_shadow_textures, directional_shadow_textures_sampler, light_local, i32(light_id));
575+
// // No shadow outside the orthographic projection volume
576+
577+
// if (offset_position_clip.w <= 0.0) {
578+
// return 1.0;
579+
// }
580+
// if (any(offset_position_ndc.xy < vec2<f32>(-1.0)) || offset_position_ndc.z < 0.0
581+
// || any(offset_position_ndc > vec3<f32>(1.0))) {
582+
// return 1.0;
583+
// }
584+
585+
// if (sampled_depth >= depth) {
586+
// return 0.0;
587+
// } else {
588+
// return 1.0;
589+
// }
551590
}
552591

553592
fn hsv2rgb(hue: f32, saturation: f32, value: f32) -> vec3<f32> {
@@ -683,8 +722,15 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
683722
let light_id = get_light_id(i);
684723
let light = point_lights.data[light_id];
685724
var shadow: f32 = 1.0;
725+
#ifdef CUBE_ARRAY_TEXTURES_SUPPORT
686726
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
687727
|| (light.flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
728+
#endif
729+
#ifdef NO_CUBE_ARRAY_TEXTURES_SUPPORT
730+
if (light_id < 1u
731+
&& ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
732+
|| (light.flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u)) {
733+
#endif
688734
shadow = fetch_point_shadow(light_id, in.world_position, in.world_normal);
689735
}
690736
let light_contrib = point_light(in.world_position.xyz, light, roughness, NdotV, N, V, R, F0, diffuse_color);

pipelined/bevy_render2/src/render_resource/pipeline_cache.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
use bevy_app::EventReader;
1212
use bevy_asset::{AssetEvent, Handle};
1313
use bevy_ecs::system::{Res, ResMut};
14-
use bevy_utils::{HashMap, HashSet};
14+
use bevy_utils::{tracing::error, HashMap, HashSet};
1515
use std::{collections::hash_map::Entry, hash::Hash, ops::Deref, sync::Arc};
1616
use thiserror::Error;
1717
use wgpu::{PipelineLayoutDescriptor, ShaderModule, VertexBufferLayout};
@@ -54,6 +54,7 @@ impl ShaderCache {
5454
Entry::Occupied(entry) => entry.into_mut(),
5555
Entry::Vacant(entry) => {
5656
let processed = self.processor.process_shader(shader, shader_defs)?;
57+
error!("Shader code:\n{:?}", processed);
5758
let module_descriptor = processed.get_module_descriptor()?;
5859
entry.insert(Arc::new(
5960
render_device.create_shader_module(&module_descriptor),
@@ -249,6 +250,7 @@ impl RenderPipelineCache {
249250
continue;
250251
}
251252
};
253+
error!("Fragment shader module:\n{:?}", fragment_module);
252254
Some((
253255
fragment_module,
254256
fragment.entry_point.deref(),

0 commit comments

Comments
 (0)