Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes has_integer_attributes and macroquad #461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,11 +1052,12 @@ pub struct ContextInfo {
}

impl ContextInfo {
pub fn has_integer_attributes(&self) -> bool {
pub fn has_integer_attributes(&self, shader_version: u32) -> bool {
match self.backend {
Backend::Metal => true,
Backend::OpenGl => {
self.glsl_support.v150 | self.glsl_support.v300es | self.glsl_support.v330
(self.glsl_support.v150 | self.glsl_support.v300es | self.glsl_support.v330)
&& shader_version >= 150
}
}
}
Expand Down
39 changes: 31 additions & 8 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct ShaderUniform {

struct ShaderInternal {
program: GLuint,
version: u32,
images: Vec<ShaderImage>,
uniforms: Vec<ShaderUniform>,
}
Expand Down Expand Up @@ -509,8 +510,8 @@ fn load_shader_internal(
meta: ShaderMeta,
) -> Result<ShaderInternal, ShaderError> {
unsafe {
let vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader)?;
let fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader)?;
let (vertex_shader, vertex_version) = load_shader(GL_VERTEX_SHADER, vertex_shader)?;
let (fragment_shader, fragment_version) = load_shader(GL_FRAGMENT_SHADER, fragment_shader)?;

let program = glCreateProgram();
glAttachShader(program, vertex_shader);
Expand Down Expand Up @@ -559,15 +560,17 @@ fn load_shader_internal(
Some(res)
}).collect();

assert!(vertex_version == fragment_version); // Probably most shader usages use same version for both
Ok(ShaderInternal {
program,
version: vertex_version,
images,
uniforms,
})
}
}

pub fn load_shader(shader_type: GLenum, source: &str) -> Result<GLuint, ShaderError> {
pub fn load_shader(shader_type: GLenum, source: &str) -> Result<(GLuint, u32), ShaderError> {
unsafe {
let shader = glCreateShader(shader_type);
assert!(shader != 0);
Expand Down Expand Up @@ -611,7 +614,26 @@ pub fn load_shader(shader_type: GLenum, source: &str) -> Result<GLuint, ShaderEr
});
}

Ok(shader)
let mut version = 110; // About default version: https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Version

let mut pos = 0;
// To not depend on regexes but expect comments in every place since it is valid shader syntax
if let Some(pound_pos) = source.find('#') {
pos += pound_pos;
if let Some(version_pos) = source[pos..].find("version") {
pos += version_pos;
if let Some(number_pos) = source[pos..].find(|c: char| ('0'..'9').contains(&c)) {
pos += number_pos;
let version_str = source[pos..]
.chars()
.take_while(|c| ('0'..'9').contains(&c))
.collect::<String>();
version = version_str.parse().unwrap();
}
}
}

Ok((shader, version))
}
}

Expand Down Expand Up @@ -739,8 +761,8 @@ impl GlContext {
self.cache.color_write = color_write;
}

fn has_integer_attributes(&self) -> bool {
self.info.has_integer_attributes()
fn has_integer_attributes(&self, shader_version: u32) -> bool {
self.info.has_integer_attributes(shader_version)
}
}

Expand Down Expand Up @@ -1332,8 +1354,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(shader.version) =>
{
glVertexAttribIPointer(
attr_index as GLuint,
attribute.size,
Expand Down
Loading