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 2a97901
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 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
6 changes: 5 additions & 1 deletion 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,7 +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 => {
| GL_UNSIGNED_BYTE | GL_BYTE
if !attribute.gl_pass_as_float =>
{
assert!(self.has_integer_attributes());
glVertexAttribIPointer(
attr_index as GLuint,
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
8 changes: 5 additions & 3 deletions src/native/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ pub fn define_glk_or_mtk_view_dlg(superclass: &Class) -> *const Class {
d.screen_width = screen_width;
d.screen_height = screen_height;
}
send_message(Message::Resize { width: screen_width, height: screen_height });
send_message(Message::Resize {
width: screen_width,
height: screen_height,
});
}

if let Some(ref mut event_handler) = payload.event_handler {
Expand Down Expand Up @@ -648,8 +651,7 @@ pub fn define_app_delegate() -> *const Class {
// I hope it will work the same on the real device.
if conf.platform.blocking_event_loop {
msg_send_![&*view, performSelectorOnMainThread:sel!(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
else {
} else {
msg_send_![&*view, performSelectorOnMainThread:sel!(display) withObject:nil waitUntilDone:YES];
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/native/linux_x11/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ pub unsafe fn translate_mouse_button(button: i32) -> MouseButton {
};
}

pub unsafe extern "C" fn keysym_to_unicode(libxkbcommon: &mut LibXkbCommon, keysym: super::libx11::KeySym) -> i32 {
return (libxkbcommon.xkb_keysym_to_utf32)(keysym as u32) as i32
pub unsafe extern "C" fn keysym_to_unicode(
libxkbcommon: &mut LibXkbCommon,
keysym: super::libx11::KeySym,
) -> i32 {
return (libxkbcommon.xkb_keysym_to_utf32)(keysym as u32) as i32;
}

0 comments on commit 2a97901

Please sign in to comment.