mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-27 11:02:33 +01:00
gpui_wgpu: Fall back to textures on GL without shader storage (#67323)
## Summary GPUI binds instance storage buffers in both the vertex and fragment shader stages. On native OpenGL adapters that lack storage support in either stage, wgpu rejects the bind group layout and the window opens blank. Use GPUI’s existing texture-backed instance data path when a native OpenGL adapter lacks `VERTEX_STORAGE` or `FRAGMENT_STORAGE`. Closes #65765 ## Testing - All 42 `gpui_wgpu` unit tests passed on macOS. Formatting and Clippy checks passed. - In a hardware smoke test, the same renderer change presented 1080p frames and redrew after input on a device with a Mali-G52 running OpenGL ES 3.2. To reproduce, open a GPUI window on a native OpenGL adapter that reports either storage capability as missing and verify that the scene renders without pipeline validation errors. Release Notes: - [GPUI] Fixed blank windows on native OpenGL adapters that lack vertex or fragment shader storage support. --------- Co-authored-by: John Tur <john-tur@outlook.com>
This commit is contained in:
committed by
GitHub
parent
c671f77ffd
commit
f00a94318c
@@ -1,43 +1,14 @@
|
||||
// --- subpixel sprites --- //
|
||||
|
||||
struct SubpixelSprite {
|
||||
order: u32,
|
||||
pad: u32,
|
||||
tile: AtlasTile,
|
||||
bounds: Bounds,
|
||||
content_mask: Bounds,
|
||||
color: Hsla,
|
||||
transformation: TransformationMatrix,
|
||||
}
|
||||
@group(1) @binding(0) var<storage, read> b_subpixel_sprites: array<SubpixelSprite>;
|
||||
|
||||
struct SubpixelSpriteOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(0) tile_position: vec2<f32>,
|
||||
@location(1) @interpolate(flat) color: vec4<f32>,
|
||||
@location(3) clip_distances: vec4<f32>,
|
||||
}
|
||||
// Subpixel sprites share the monochrome sprite layout and vertex shader.
|
||||
|
||||
struct SubpixelSpriteFragmentOutput {
|
||||
@location(0) @blend_src(0) foreground: vec4<f32>,
|
||||
@location(0) @blend_src(1) alpha: vec4<f32>,
|
||||
}
|
||||
|
||||
@vertex
|
||||
fn vs_subpixel_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> SubpixelSpriteOutput {
|
||||
let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
|
||||
let sprite = b_subpixel_sprites[instance_id];
|
||||
|
||||
var out = SubpixelSpriteOutput();
|
||||
out.position = to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation);
|
||||
out.tile_position = to_tile_position(unit_vertex, sprite.tile);
|
||||
out.color = hsla_to_rgba(sprite.color);
|
||||
out.clip_distances = distance_from_clip_rect_transformed(unit_vertex, sprite.bounds, sprite.content_mask, sprite.transformation);
|
||||
return out;
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_subpixel_sprite(input: SubpixelSpriteOutput) -> SubpixelSpriteFragmentOutput {
|
||||
fn fs_subpixel_sprite(input: MonoSpriteVarying) -> SubpixelSpriteFragmentOutput {
|
||||
var sample = textureSample(t_sprite, s_sprite, input.tile_position).rgb;
|
||||
if (gamma_params.is_bgr != 0u) {
|
||||
sample = sample.bgr;
|
||||
|
||||
@@ -452,18 +452,12 @@ impl WgpuContext {
|
||||
}
|
||||
|
||||
let color_atlas_texture_format = Self::select_color_texture_format(adapter)?;
|
||||
#[cfg(target_family = "wasm")]
|
||||
let required_limits = if adapter.get_info().backend == wgpu::Backend::Gl {
|
||||
let base_limits = if Self::requires_texture_instance_data(adapter) {
|
||||
wgpu::Limits::downlevel_webgl2_defaults()
|
||||
.using_resolution(adapter.limits())
|
||||
.using_alignment(adapter.limits())
|
||||
} else {
|
||||
wgpu::Limits::downlevel_defaults()
|
||||
.using_resolution(adapter.limits())
|
||||
.using_alignment(adapter.limits())
|
||||
};
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
let required_limits = wgpu::Limits::downlevel_defaults()
|
||||
let required_limits = base_limits
|
||||
.using_resolution(adapter.limits())
|
||||
.using_alignment(adapter.limits());
|
||||
|
||||
@@ -738,8 +732,18 @@ impl WgpuContext {
|
||||
self.backend
|
||||
}
|
||||
|
||||
pub fn uses_webgl_instance_data(&self) -> bool {
|
||||
matches!(self.backend, WgpuBackend::Gl) && cfg!(target_family = "wasm")
|
||||
pub fn uses_texture_instance_data(&self) -> bool {
|
||||
Self::requires_texture_instance_data(&self.adapter)
|
||||
}
|
||||
|
||||
fn requires_texture_instance_data(adapter: &wgpu::Adapter) -> bool {
|
||||
if adapter.get_info().backend != wgpu::Backend::Gl {
|
||||
return false;
|
||||
}
|
||||
!adapter
|
||||
.get_downlevel_capabilities()
|
||||
.flags
|
||||
.contains(wgpu::DownlevelFlags::VERTEX_STORAGE | wgpu::DownlevelFlags::FRAGMENT_STORAGE)
|
||||
}
|
||||
|
||||
pub fn supports_dual_source_blending(&self) -> bool {
|
||||
|
||||
@@ -20,30 +20,33 @@ const MAX_INSTANCE_BUFFER_SIZE: u64 = 256 * 1024 * 1024;
|
||||
|
||||
const INSTANCE_TEXTURE_TEXEL_SIZE: u64 = 16;
|
||||
|
||||
/// Shader variant for backends with storage buffer support: the shared shader
|
||||
/// logic plus the storage-buffer instance transport.
|
||||
/// Shader variant for backends with storage buffer support.
|
||||
const STORAGE_BUFFER_SHADERS: &str = concat!(
|
||||
include_str!("shaders.wgsl"),
|
||||
include_str!("shaders_storage.wgsl"),
|
||||
);
|
||||
|
||||
/// Shader variant for WebGL2, which has no storage buffers: the shared shader
|
||||
/// logic plus the texture-based instance transport.
|
||||
/// Shader variant for adapters without storage buffers (WebGL2 and some OpenGL ES drivers).
|
||||
const WEBGL_SHADERS: &str = concat!(
|
||||
include_str!("shaders.wgsl"),
|
||||
include_str!("shaders_webgl.wgsl"),
|
||||
);
|
||||
|
||||
/// Subpixel text rendering requires dual-source blending, which WebGL2 lacks, so
|
||||
/// this variant only ever runs with the storage-buffer transport. The `enable`
|
||||
/// directive must precede all declarations.
|
||||
const SUBPIXEL_SHADERS: &str = concat!(
|
||||
/// Subpixel text rendering requires dual-source blending, so it gets its own variants.
|
||||
const SUBPIXEL_STORAGE_BUFFER_SHADERS: &str = concat!(
|
||||
"enable dual_source_blending;\n",
|
||||
include_str!("shaders.wgsl"),
|
||||
include_str!("shaders_storage.wgsl"),
|
||||
include_str!("shaders_subpixel.wgsl"),
|
||||
);
|
||||
|
||||
const SUBPIXEL_WEBGL_SHADERS: &str = concat!(
|
||||
"enable dual_source_blending;\n",
|
||||
include_str!("shaders.wgsl"),
|
||||
include_str!("shaders_webgl.wgsl"),
|
||||
include_str!("shaders_subpixel.wgsl"),
|
||||
);
|
||||
|
||||
fn least_common_multiple(left: u64, right: u64) -> u64 {
|
||||
let mut first = left;
|
||||
let mut second = right;
|
||||
@@ -138,7 +141,7 @@ struct InstanceBinding {
|
||||
bind_group: wgpu::BindGroup,
|
||||
/// Index of the allocation's first instance within the bound data. Always
|
||||
/// zero on the storage-buffer path, where the binding offset already
|
||||
/// positions the array; on the WebGL texture path the shader indexes the
|
||||
/// positions the array; on the texture path the shader indexes the
|
||||
/// shared instance texture absolutely, so draws must offset their
|
||||
/// instance (or vertex) ranges by this value.
|
||||
first_instance: u32,
|
||||
@@ -180,8 +183,8 @@ pub type GpuContext = Rc<RefCell<Option<WgpuContext>>>;
|
||||
|
||||
enum InstanceData {
|
||||
Storage(wgpu::Buffer),
|
||||
// WebGL2 has no storage buffers. A uint texture keeps the records available to both shader
|
||||
// stages while preserving integer and floating-point bit patterns exactly.
|
||||
// Used when storage buffers are unavailable. A uint texture keeps the records available to
|
||||
// both shader stages while preserving integer and floating-point bit patterns exactly.
|
||||
Texture {
|
||||
texture: wgpu::Texture,
|
||||
view: wgpu::TextureView,
|
||||
@@ -215,7 +218,7 @@ struct WgpuRendererCore {
|
||||
instance_data_capacity: u64,
|
||||
max_instance_data_size: u64,
|
||||
instance_data_alignment: u64,
|
||||
uses_webgl_instance_data: bool,
|
||||
uses_texture_instance_data: bool,
|
||||
rendering_params: RenderingParameters,
|
||||
is_bgr: bool,
|
||||
dual_source_blending: bool,
|
||||
@@ -526,7 +529,7 @@ impl WgpuRenderer {
|
||||
impl WgpuRendererCore {
|
||||
fn create_bind_group_layouts(
|
||||
device: &wgpu::Device,
|
||||
uses_webgl_instance_data: bool,
|
||||
uses_texture_instance_data: bool,
|
||||
) -> WgpuBindGroupLayouts {
|
||||
let globals =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
@@ -562,7 +565,7 @@ impl WgpuRendererCore {
|
||||
let instance_data_entry = wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
|
||||
ty: if uses_webgl_instance_data {
|
||||
ty: if uses_texture_instance_data {
|
||||
wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Uint,
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
@@ -585,7 +588,7 @@ impl WgpuRendererCore {
|
||||
|
||||
// Path tiles read their curves and paints too. The texture transport
|
||||
// reads all three from the one instance texture.
|
||||
let path_tiles = if uses_webgl_instance_data {
|
||||
let path_tiles = if uses_texture_instance_data {
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("path_tiles_layout"),
|
||||
entries: &[instance_data_entry],
|
||||
@@ -726,7 +729,7 @@ impl WgpuRendererCore {
|
||||
surface_format: wgpu::TextureFormat,
|
||||
alpha_mode: wgpu::CompositeAlphaMode,
|
||||
dual_source_blending: bool,
|
||||
uses_webgl_instance_data: bool,
|
||||
uses_texture_instance_data: bool,
|
||||
) -> WgpuPipelines {
|
||||
// Diagnostic guard: verify the device actually has
|
||||
// DUAL_SOURCE_BLENDING. We have a crash report (ZED-5G1) where a
|
||||
@@ -748,13 +751,12 @@ impl WgpuRendererCore {
|
||||
device.features(),
|
||||
);
|
||||
}
|
||||
let dual_source_blending =
|
||||
dual_source_blending && device_has_feature && !uses_webgl_instance_data;
|
||||
let dual_source_blending = dual_source_blending && device_has_feature;
|
||||
|
||||
let shader_source = if uses_webgl_instance_data {
|
||||
WEBGL_SHADERS
|
||||
let (shader_source, subpixel_shader_source) = if uses_texture_instance_data {
|
||||
(WEBGL_SHADERS, SUBPIXEL_WEBGL_SHADERS)
|
||||
} else {
|
||||
STORAGE_BUFFER_SHADERS
|
||||
(STORAGE_BUFFER_SHADERS, SUBPIXEL_STORAGE_BUFFER_SHADERS)
|
||||
};
|
||||
let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("gpui_shaders"),
|
||||
@@ -764,7 +766,7 @@ impl WgpuRendererCore {
|
||||
let subpixel_shader_module = if dual_source_blending {
|
||||
Some(device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("gpui_subpixel_shaders"),
|
||||
source: wgpu::ShaderSource::Wgsl(SUBPIXEL_SHADERS.into()),
|
||||
source: wgpu::ShaderSource::Wgsl(subpixel_shader_source.into()),
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
@@ -936,7 +938,7 @@ impl WgpuRendererCore {
|
||||
|
||||
Some(create_pipeline(
|
||||
"subpixel_sprites",
|
||||
"vs_subpixel_sprite",
|
||||
"vs_mono_sprite",
|
||||
"fs_subpixel_sprite",
|
||||
&layouts.globals,
|
||||
&layouts.instances,
|
||||
@@ -1177,17 +1179,17 @@ impl WgpuRendererCore {
|
||||
let device = Arc::clone(&context.device);
|
||||
let queue = Arc::clone(&context.queue);
|
||||
let rendering_params = RenderingParameters::new();
|
||||
let uses_webgl_instance_data = context.uses_webgl_instance_data();
|
||||
let dual_source_blending =
|
||||
context.supports_dual_source_blending() && !uses_webgl_instance_data;
|
||||
let bind_group_layouts = Self::create_bind_group_layouts(&device, uses_webgl_instance_data);
|
||||
let uses_texture_instance_data = context.uses_texture_instance_data();
|
||||
let dual_source_blending = context.supports_dual_source_blending();
|
||||
let bind_group_layouts =
|
||||
Self::create_bind_group_layouts(&device, uses_texture_instance_data);
|
||||
let pipelines = Self::create_pipelines(
|
||||
&device,
|
||||
&bind_group_layouts,
|
||||
target_format,
|
||||
alpha_mode,
|
||||
dual_source_blending,
|
||||
uses_webgl_instance_data,
|
||||
uses_texture_instance_data,
|
||||
);
|
||||
let atlas_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: Some("atlas_sampler"),
|
||||
@@ -1210,7 +1212,7 @@ impl WgpuRendererCore {
|
||||
instance_data_capacity,
|
||||
max_instance_data_size,
|
||||
instance_data_alignment,
|
||||
) = if uses_webgl_instance_data {
|
||||
) = if uses_texture_instance_data {
|
||||
let max_texture_dimension = device.limits().max_texture_dimension_2d;
|
||||
let max_instance_data_size = (u64::from(max_texture_dimension).pow(2)
|
||||
* INSTANCE_TEXTURE_TEXEL_SIZE)
|
||||
@@ -1285,7 +1287,7 @@ impl WgpuRendererCore {
|
||||
instance_data_capacity,
|
||||
max_instance_data_size,
|
||||
instance_data_alignment,
|
||||
uses_webgl_instance_data,
|
||||
uses_texture_instance_data,
|
||||
rendering_params,
|
||||
is_bgr: false,
|
||||
dual_source_blending,
|
||||
@@ -1316,7 +1318,7 @@ impl WgpuRendererCore {
|
||||
surface_format,
|
||||
alpha_mode,
|
||||
self.dual_source_blending,
|
||||
self.uses_webgl_instance_data,
|
||||
self.uses_texture_instance_data,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1762,7 +1764,7 @@ impl WgpuRendererCore {
|
||||
// wgpu rejects zero-sized bindings, so empty primitive arrays still
|
||||
// reserve the 16-byte minimum.
|
||||
let size = byte_len.max(16);
|
||||
if self.uses_webgl_instance_data {
|
||||
if self.uses_texture_instance_data {
|
||||
// The texture transport has no binding offset: the shader indexes
|
||||
// the instance texture absolutely, so each allocation must start on
|
||||
// a whole instance (a stride multiple) and a whole texel, and must
|
||||
@@ -1811,7 +1813,7 @@ impl WgpuRendererCore {
|
||||
}
|
||||
*instance_offset = offset + allocation_size;
|
||||
|
||||
let first_instance = if self.uses_webgl_instance_data {
|
||||
let first_instance = if self.uses_texture_instance_data {
|
||||
u32::try_from(offset / stride).context("instance index exceeds u32 range")?
|
||||
} else {
|
||||
0
|
||||
@@ -1895,7 +1897,7 @@ impl WgpuRendererCore {
|
||||
let tiles = self.write_instance_data(instance_offset, &tile_data)?;
|
||||
|
||||
let resources = self.resources();
|
||||
let bind_group = if self.uses_webgl_instance_data {
|
||||
let bind_group = if self.uses_texture_instance_data {
|
||||
resources
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
@@ -2044,9 +2046,9 @@ impl WgpuRendererCore {
|
||||
// Bind groups created earlier in the frame keep the previous buffer or
|
||||
// texture alive, so allocations written before the grow remain valid;
|
||||
// only subsequent writes land in the new allocation.
|
||||
let uses_webgl_instance_data = self.uses_webgl_instance_data;
|
||||
let uses_texture_instance_data = self.uses_texture_instance_data;
|
||||
let resources = self.resources_mut();
|
||||
if uses_webgl_instance_data {
|
||||
if uses_texture_instance_data {
|
||||
let max_texture_dimension = resources.device.limits().max_texture_dimension_2d;
|
||||
let (instance_data, actual_capacity) =
|
||||
Self::create_instance_texture(&resources.device, capacity, max_texture_dimension);
|
||||
@@ -2742,9 +2744,18 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subpixel_shader_is_valid_wgsl() {
|
||||
fn subpixel_storage_buffer_shader_is_valid_wgsl() {
|
||||
validate_wgsl(
|
||||
SUBPIXEL_SHADERS,
|
||||
SUBPIXEL_STORAGE_BUFFER_SHADERS,
|
||||
naga::valid::Capabilities::DUAL_SOURCE_BLENDING,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subpixel_webgl_shader_is_valid_wgsl_without_storage_buffers() {
|
||||
assert!(!SUBPIXEL_WEBGL_SHADERS.contains("var<storage"));
|
||||
validate_wgsl(
|
||||
SUBPIXEL_WEBGL_SHADERS,
|
||||
naga::valid::Capabilities::DUAL_SOURCE_BLENDING,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user