Skip to content

Commit

Permalink
graphics: Fix backwards compatibility with integer attributes
Browse files Browse the repository at this point in the history
Since a359a34, glVertexAttribIPointer was used for VertexAttribute:Byte*/Short*/etc. While it is a correct behavior, it broke backwards compatibility with glsl100 shaders receiving Byte4 vectors as vec4. This commit makes this feature opt-in: it is possible to pass Byte4 as uvec4, but only if its explicitly required.
  • Loading branch information
not-fl3 committed Jun 25, 2024
1 parent 0457af0 commit 6104676
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion examples/triangle_color4b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl Stage {
&[BufferLayout::default()],
&[
VertexAttribute::new("in_pos", VertexFormat::Float2),
VertexAttribute::new("in_color", VertexFormat::Byte4),
VertexAttribute {
gl_pass_as_float: false,
..VertexAttribute::new("in_color", VertexFormat::Byte4)
},
],
shader,
PipelineParams::default(),
Expand Down
10 changes: 10 additions & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ pub struct VertexAttribute {
pub name: &'static str,
pub format: VertexFormat,
pub buffer_index: usize,
/// This flag affects integer VertexFormats, Byte*, Short*, Int*
/// Taking Byte4 as an example:
/// On Metal, it might be received as either `float4` or `uint4`
/// On OpenGl and `gl_pass_as_float = true` shaders should receive it as `vec4`
/// With `gl_pass_as_float = false`, as `uvec4`
///
/// Note that `uvec4` requires at least `150` glsl version
/// Before setting `gl_pass_as_float` to false, better check `context.info().has_integer_attributes()` and double check that shaders are at least `150`
pub gl_pass_as_float: bool,
}

impl VertexAttribute {
Expand All @@ -252,6 +261,7 @@ impl VertexAttribute {
name,
format,
buffer_index,
gl_pass_as_float: true,
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ impl RenderingBackend for GlContext {
name,
format,
buffer_index,
gl_pass_as_float,
} in attributes
{
let buffer_data = &mut buffer_cache
Expand Down Expand Up @@ -1105,6 +1106,7 @@ impl RenderingBackend for GlContext {
stride: buffer_data.stride,
buffer_index: *buffer_index,
divisor,
gl_pass_as_float: *gl_pass_as_float,
};

assert!(
Expand Down Expand Up @@ -1332,8 +1334,9 @@ impl RenderingBackend for GlContext {
unsafe {
match attribute.type_ {
GL_INT | GL_UNSIGNED_INT | GL_SHORT | GL_UNSIGNED_SHORT
| GL_UNSIGNED_BYTE | GL_BYTE => {
assert!(self.has_integer_attributes());
| GL_UNSIGNED_BYTE | GL_BYTE
if self.has_integer_attributes() =>
{
glVertexAttribIPointer(
attr_index as GLuint,
attribute.size,
Expand Down
1 change: 1 addition & 0 deletions src/graphics/gl/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct VertexAttributeInternal {
pub stride: i32,
pub buffer_index: usize,
pub divisor: i32,
pub gl_pass_as_float: bool,
}

#[derive(Default, Copy, Clone)]
Expand Down

0 comments on commit 6104676

Please sign in to comment.