Skip to content

Commit

Permalink
fix: Write gl_PointSize = 1.0; in case of vertex shader and `allow_…
Browse files Browse the repository at this point in the history
…point_size` == true

According to https://docs.gl/el3/gl_PointSize
```
The variable gl_PointSize is intended for a vertex shader to write the size of the point to be rasterized. It is measured in pixels. If gl_PointSize is not written to, its value is undefined in subsequent pipeline stages.
```
  • Loading branch information
REASY committed Jan 26, 2023
1 parent f0edae8 commit 2118afb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ fn backends(c: &mut Criterion) {
shader_stage: ep.stage,
entry_point: ep.name.clone(),
multiview: None,
allow_point_size: true,
};

// might be `Err` if missing features
Expand Down
14 changes: 8 additions & 6 deletions cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,20 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
stage @ ("vert" | "frag" | "comp") => {
use naga::back::glsl;

let shader_stage = match stage {
"vert" => naga::ShaderStage::Vertex,
"frag" => naga::ShaderStage::Fragment,
"comp" => naga::ShaderStage::Compute,
_ => unreachable!(),
};
let pipeline_options = glsl::PipelineOptions {
entry_point: match params.entry_point {
Some(ref name) => name.clone(),
None => "main".to_string(),
},
shader_stage: match stage {
"vert" => naga::ShaderStage::Vertex,
"frag" => naga::ShaderStage::Fragment,
"comp" => naga::ShaderStage::Compute,
_ => unreachable!(),
},
shader_stage: shader_stage,
multiview: None,
allow_point_size: shader_stage == naga::ShaderStage::Vertex,
};

let mut buffer = String::new();
Expand Down
14 changes: 14 additions & 0 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ pub struct PipelineOptions {
pub entry_point: String,
/// How many views to render to, if doing multiview rendering.
pub multiview: Option<std::num::NonZeroU32>,
/// Allow `BuiltIn::PointSize` in the vertex shader.
pub allow_point_size: bool,
}

/// Reflection info for texture mappings and uniforms.
Expand Down Expand Up @@ -434,6 +436,7 @@ pub struct Writer<'a, W> {
need_bake_expressions: back::NeedBakeExpressions,
/// How many views to render to, if doing multiview rendering.
multiview: Option<std::num::NonZeroU32>,
pipeline_options: &'a PipelineOptions,
}

impl<'a, W: Write> Writer<'a, W> {
Expand Down Expand Up @@ -489,6 +492,7 @@ impl<'a, W: Write> Writer<'a, W> {
block_id: IdGenerator::default(),
named_expressions: Default::default(),
need_bake_expressions: Default::default(),
pipeline_options,
};

// Find all features required to print this module
Expand Down Expand Up @@ -2072,6 +2076,16 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, "{}", level)?;
}
}

if let back::FunctionType::EntryPoint(ep_index) = ctx.ty {
if self.module.entry_points[ep_index as usize].stage
== ShaderStage::Vertex
&& self.pipeline_options.allow_point_size
{
writeln!(self.out, "gl_PointSize = 1.0;",)?;
write!(self.out, "{}", level)?;
}
}
writeln!(self.out, "return;")?;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ fn write_output_glsl(
shader_stage: stage,
entry_point: ep_name.to_string(),
multiview,
allow_point_size: true,
};

let mut buffer = String::new();
Expand Down

0 comments on commit 2118afb

Please sign in to comment.