Skip to content

Commit

Permalink
fix: Set FORCE_POINT_SIZE if it is vertex shader with mesh consist …
Browse files Browse the repository at this point in the history
…of point list
  • Loading branch information
REASY committed Feb 3, 2023
1 parent 6399dd4 commit 6787e6d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
43 changes: 38 additions & 5 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ impl super::Device {
naga_stage: naga::ShaderStage,
stage: &crate::ProgrammableStage<super::Api>,
context: CompilationContext,
primitive_topology: Option<wgt::PrimitiveTopology>,
) -> Result<glow::Shader, crate::PipelineError> {
use naga::back::glsl;

let pipeline_options = glsl::PipelineOptions {
shader_stage: naga_stage,
entry_point: stage.entry_point.to_string(),
Expand Down Expand Up @@ -237,12 +239,31 @@ impl super::Device {
binding_array: BoundsCheckPolicy::Unchecked,
};

let update_naga_options = primitive_topology == Some(wgt::PrimitiveTopology::PointList)
&& naga_stage == naga::ShaderStage::Vertex;
// Update naga options if mesh consist of point list and it is vertex shader
let naga_options = if update_naga_options {
let mut wrt_flags = context.layout.naga_options.writer_flags;
wrt_flags.set(glsl::WriterFlags::FORCE_POINT_SIZE, true);
glsl::Options {
version: context.layout.naga_options.version,
writer_flags: wrt_flags,
binding_map: context.layout.naga_options.binding_map.clone(),
zero_initialize_workgroup_memory: context
.layout
.naga_options
.zero_initialize_workgroup_memory,
}
} else {
context.layout.naga_options.clone()
};

let mut output = String::new();
let mut writer = glsl::Writer::new(
&mut output,
&shader.module,
&shader.info,
&context.layout.naga_options,
&naga_options,
&pipeline_options,
policies,
)
Expand Down Expand Up @@ -274,6 +295,7 @@ impl super::Device {
layout: &super::PipelineLayout,
#[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,
multiview: Option<std::num::NonZeroU32>,
primitive_topology: Option<wgt::PrimitiveTopology>,
) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {
let mut program_stages = ArrayVec::new();
let mut group_to_binding_to_slot = Vec::with_capacity(layout.group_infos.len());
Expand Down Expand Up @@ -312,6 +334,7 @@ impl super::Device {
multiview,
glsl_version,
self.shared.private_caps,
primitive_topology,
)
})
.to_owned()?;
Expand All @@ -328,6 +351,7 @@ impl super::Device {
multiview: Option<std::num::NonZeroU32>,
glsl_version: u16,
private_caps: super::PrivateCapabilities,
primitive_topology: Option<wgt::PrimitiveTopology>,
) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {
let program = unsafe { gl.create_program() }.unwrap();
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -352,7 +376,7 @@ impl super::Device {
multiview,
};

let shader = Self::create_shader(gl, naga_stage, stage, context)?;
let shader = Self::create_shader(gl, naga_stage, stage, context, primitive_topology)?;
shaders_to_delete.push(shader);
}

Expand Down Expand Up @@ -1128,8 +1152,16 @@ impl crate::Device<super::Api> for super::Device {
if let Some(ref fs) = desc.fragment_stage {
shaders.push((naga::ShaderStage::Fragment, fs));
}
let inner =
unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, desc.multiview) }?;
let inner = unsafe {
self.create_pipeline(
gl,
shaders,
desc.layout,
desc.label,
desc.multiview,
Some(desc.primitive.topology),
)
}?;

let (vertex_buffers, vertex_attributes) = {
let mut buffers = Vec::new();
Expand Down Expand Up @@ -1210,7 +1242,8 @@ impl crate::Device<super::Api> for super::Device {
let gl = &self.shared.context.lock();
let mut shaders = ArrayVec::new();
shaders.push((naga::ShaderStage::Compute, &desc.stage));
let inner = unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None) }?;
let inner =
unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None, None) }?;

Ok(super::ComputePipeline { inner })
}
Expand Down
9 changes: 6 additions & 3 deletions wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
entry_point: "fs_main",
targets: &[Some(swapchain_format.into())],
}),
primitive: wgpu::PrimitiveState::default(),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::PointList,
..wgpu::PrimitiveState::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
Expand Down Expand Up @@ -117,14 +120,14 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
load: wgpu::LoadOp::Clear(wgpu::Color::WHITE),
store: true,
},
})],
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
rpass.draw(0..64, 0..1);
}

queue.submit(Some(encoder.finish()));
Expand Down
70 changes: 67 additions & 3 deletions wgpu/examples/hello-triangle/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
var pos = array<vec2<f32>,64>(
vec2<f32>(0.5, 0.7),
vec2<f32>(0.51, 0.7),
vec2<f32>(0.52, 0.7),
vec2<f32>(0.53, 0.7),
vec2<f32>(0.54, 0.7),
vec2<f32>(0.55, 0.7),
vec2<f32>(0.56, 0.7),
vec2<f32>(0.57, 0.7),
vec2<f32>(0.6, 0.7),
vec2<f32>(0.61, 0.7),
vec2<f32>(0.62, 0.7),
vec2<f32>(0.63, 0.7),
vec2<f32>(0.64, 0.7),
vec2<f32>(0.65, 0.7),
vec2<f32>(0.66, 0.7),
vec2<f32>(0.67, 0.7),
vec2<f32>(0.7, 0.7),
vec2<f32>(0.71, 0.7),
vec2<f32>(0.72, 0.7),
vec2<f32>(0.73, 0.7),
vec2<f32>(0.74, 0.7),
vec2<f32>(0.75, 0.7),
vec2<f32>(0.76, 0.7),
vec2<f32>(0.77, 0.7),
vec2<f32>(0.8, 0.7),
vec2<f32>(0.81, 0.7),
vec2<f32>(0.82, 0.7),
vec2<f32>(0.83, 0.7),
vec2<f32>(0.84, 0.7),
vec2<f32>(0.85, 0.7),
vec2<f32>(0.86, 0.7),
vec2<f32>(0.87, 0.7),
vec2<f32>(0.5, 0.72),
vec2<f32>(0.51, 0.72),
vec2<f32>(0.52, 0.72),
vec2<f32>(0.53, 0.72),
vec2<f32>(0.54, 0.72),
vec2<f32>(0.55, 0.72),
vec2<f32>(0.56, 0.72),
vec2<f32>(0.57, 0.72),
vec2<f32>(0.6, 0.74),
vec2<f32>(0.61, 0.74),
vec2<f32>(0.62, 0.74),
vec2<f32>(0.63, 0.74),
vec2<f32>(0.64, 0.74),
vec2<f32>(0.65, 0.74),
vec2<f32>(0.66, 0.74),
vec2<f32>(0.67, 0.74),
vec2<f32>(0.7, 0.76),
vec2<f32>(0.71, 0.76),
vec2<f32>(0.72, 0.76),
vec2<f32>(0.73, 0.76),
vec2<f32>(0.74, 0.76),
vec2<f32>(0.75, 0.76),
vec2<f32>(0.76, 0.76),
vec2<f32>(0.77, 0.76),
vec2<f32>(0.8, 0.78),
vec2<f32>(0.81, 0.78),
vec2<f32>(0.82, 0.78),
vec2<f32>(0.83, 0.78),
vec2<f32>(0.84, 0.78),
vec2<f32>(0.85, 0.78),
vec2<f32>(0.86, 0.78),
vec2<f32>(0.87, 0.78),
);
return vec4<f32>(pos[in_vertex_index], 0.0, 1.0);
}

@fragment
Expand Down

0 comments on commit 6787e6d

Please sign in to comment.