Handle unused uniforms and attributes better

This commit is contained in:
Thinkofname 2016-04-09 11:08:17 +01:00
parent d5a6eb36db
commit a644f3ac8f
6 changed files with 121 additions and 91 deletions

View File

@ -484,16 +484,26 @@ impl Program {
}
}
pub fn uniform_location(&self, name: &str) -> Uniform {
Uniform(unsafe {
pub fn uniform_location(&self, name: &str) -> Option<Uniform> {
let u = unsafe {
gl::GetUniformLocation(self.0, ffi::CString::new(name).unwrap().as_ptr())
})
};
if u != -1 {
Some(Uniform(u))
} else {
None
}
}
pub fn attribute_location(&self, name: &str) -> Attribute {
Attribute(unsafe {
pub fn attribute_location(&self, name: &str) -> Option<Attribute> {
let a = unsafe {
gl::GetAttribLocation(self.0, ffi::CString::new(name).unwrap().as_ptr())
})
};
if a != -1 {
Some(Attribute(a))
} else {
None
}
}
}
@ -547,6 +557,7 @@ impl Shader {
}
}
#[derive(Clone, Copy)]
pub struct Uniform(i32);
impl Uniform {
@ -607,6 +618,7 @@ impl Uniform {
}
}
#[derive(Clone, Copy)]
pub struct Attribute(i32);
impl Attribute {

View File

@ -93,17 +93,17 @@ impl Clouds {
program.link();
program.use_program();
let a_position = program.attribute_location("aPosition");
let u_perspective_matrix = program.uniform_location("perspectiveMatrix");
let u_camera_matrix = program.uniform_location("cameraMatrix");
let u_light_level = program.uniform_location("lightLevel");
let u_sky_offset = program.uniform_location("skyOffset");
let u_offset = program.uniform_location("offset");
let u_texture_info = program.uniform_location("textureInfo");
let u_atlas = program.uniform_location("atlas");
let u_textures = program.uniform_location("textures");
let u_cloud_map = program.uniform_location("cloudMap");
let u_cloud_offset = program.uniform_location("cloudOffset");
let a_position = program.attribute_location("aPosition").unwrap();
let u_perspective_matrix = program.uniform_location("perspectiveMatrix").unwrap();
let u_camera_matrix = program.uniform_location("cameraMatrix").unwrap();
let u_light_level = program.uniform_location("lightLevel").unwrap();
let u_sky_offset = program.uniform_location("skyOffset").unwrap();
let u_offset = program.uniform_location("offset").unwrap();
let u_texture_info = program.uniform_location("textureInfo").unwrap();
let u_atlas = program.uniform_location("atlas").unwrap();
let u_textures = program.uniform_location("textures").unwrap();
let u_cloud_map = program.uniform_location("cloudMap").unwrap();
let u_cloud_offset = program.uniform_location("cloudOffset").unwrap();
let array = gl::VertexArray::new();
array.bind();

View File

@ -115,19 +115,19 @@ init_shader! {
vert = "chunk_vertex",
frag = "chunk_frag",
attribute = {
position => "aPosition",
texture_info => "aTextureInfo",
texture_offset => "aTextureOffset",
color => "aColor",
lighting => "aLighting",
required position => "aPosition",
required texture_info => "aTextureInfo",
required texture_offset => "aTextureOffset",
required color => "aColor",
required lighting => "aLighting",
},
uniform = {
perspective_matrix => "perspectiveMatrix",
camera_matrix => "cameraMatrix",
offset => "offset",
texture => "textures",
light_level => "lightLevel",
sky_offset => "skyOffset",
required perspective_matrix => "perspectiveMatrix",
required camera_matrix => "cameraMatrix",
required offset => "offset",
required texture => "textures",
required light_level => "lightLevel",
required sky_offset => "skyOffset",
},
}
}
@ -137,19 +137,19 @@ init_shader! {
vert = "chunk_vertex",
frag = "chunk_frag", #alpha
attribute = {
position => "aPosition",
texture_info => "aTextureInfo",
texture_offset => "aTextureOffset",
color => "aColor",
lighting => "aLighting",
required position => "aPosition",
required texture_info => "aTextureInfo",
required texture_offset => "aTextureOffset",
required color => "aColor",
required lighting => "aLighting",
},
uniform = {
perspective_matrix => "perspectiveMatrix",
camera_matrix => "cameraMatrix",
offset => "offset",
texture => "textures",
light_level => "lightLevel",
sky_offset => "skyOffset",
required perspective_matrix => "perspectiveMatrix",
required camera_matrix => "cameraMatrix",
required offset => "offset",
required texture => "textures",
required light_level => "lightLevel",
required sky_offset => "skyOffset",
},
}
}
@ -669,13 +669,13 @@ init_shader! {
vert = "trans_vertex",
frag = "trans_frag",
attribute = {
position => "aPosition",
required position => "aPosition",
},
uniform = {
accum => "taccum",
revealage => "trevealage",
color => "tcolor",
samples => "samples",
required accum => "taccum",
required revealage => "trevealage",
required color => "tcolor",
required samples => "samples",
},
}
}

View File

@ -79,16 +79,16 @@ impl Manager {
let mut model = {
let collection = &mut self.collections[ckey.0];
collection.shader.program.use_program();
collection.shader.position.enable();
collection.shader.texture_info.enable();
collection.shader.texture_offset.enable();
collection.shader.color.enable();
collection.shader.id.enable();
collection.shader.position.vertex_pointer(3, gl::FLOAT, false, 36, 0);
collection.shader.texture_info.vertex_pointer(4, gl::UNSIGNED_SHORT, false, 36, 12);
collection.shader.texture_offset.vertex_pointer_int(3, gl::SHORT, 36, 20);
collection.shader.color.vertex_pointer(4, gl::UNSIGNED_BYTE, true, 36, 28);
collection.shader.id.vertex_pointer_int(1, gl::UNSIGNED_BYTE, 36, 32);
collection.shader.position.map(|v| v.enable());
collection.shader.texture_info.map(|v| v.enable());
collection.shader.texture_offset.map(|v| v.enable());
collection.shader.color.map(|v| v.enable());
collection.shader.id.map(|v| v.enable());
collection.shader.position.map(|v| v.vertex_pointer(3, gl::FLOAT, false, 36, 0));
collection.shader.texture_info.map(|v| v.vertex_pointer(4, gl::UNSIGNED_SHORT, false, 36, 12));
collection.shader.texture_offset.map(|v| v.vertex_pointer_int(3, gl::SHORT, 36, 20));
collection.shader.color.map(|v| v.vertex_pointer(4, gl::UNSIGNED_BYTE, true, 36, 28));
collection.shader.id.map(|v| v.vertex_pointer_int(1, gl::UNSIGNED_BYTE, 36, 32));
let mut model = Model {
// For culling only
@ -215,11 +215,11 @@ impl Manager {
gl::enable(gl::BLEND);
for collection in &self.collections {
collection.shader.program.use_program();
collection.shader.perspective_matrix.set_matrix4(perspective_matrix);
collection.shader.camera_matrix.set_matrix4(camera_matrix);
collection.shader.texture.set_int(0);
collection.shader.sky_offset.set_float(sky_offset);
collection.shader.light_level.set_float(light_level);
collection.shader.perspective_matrix.map(|v| v.set_matrix4(perspective_matrix));
collection.shader.camera_matrix.map(|v| v.set_matrix4(camera_matrix));
collection.shader.texture.map(|v| v.set_int(0));
collection.shader.sky_offset.map(|v| v.set_float(sky_offset));
collection.shader.light_level.map(|v| v.set_float(light_level));
gl::blend_func(collection.blend_s, collection.blend_d);
for model in collection.models.values() {
@ -230,18 +230,18 @@ impl Manager {
continue;
}
model.array.bind();
collection.shader.lighting.set_float2(model.block_light, model.sky_light);
collection.shader.lighting.map(|v| v.set_float2(model.block_light, model.sky_light));
if model.counts.len() > 1 {
let mut offsets = model.offsets.clone();
for offset in &mut offsets {
*offset *= m;
}
collection.shader.model_matrix.set_matrix4_multi(&model.matrix);
collection.shader.color_mul.set_float_mutli_raw(model.colors.as_ptr() as *const _, model.colors.len());
collection.shader.model_matrix.map(|v| v.set_matrix4_multi(&model.matrix));
collection.shader.color_mul.map(|v| v.set_float_mutli_raw(model.colors.as_ptr() as *const _, model.colors.len()));
gl::multi_draw_elements(gl::TRIANGLES, &model.counts, self.index_type, &offsets);
} else {
collection.shader.model_matrix.set_matrix4_multi(&model.matrix);
collection.shader.color_mul.set_float_mutli_raw(model.colors.as_ptr() as *const _, model.colors.len());
collection.shader.model_matrix.map(|v| v.set_matrix4_multi(&model.matrix));
collection.shader.color_mul.map(|v| v.set_float_mutli_raw(model.colors.as_ptr() as *const _, model.colors.len()));
gl::draw_elements(gl::TRIANGLES, model.counts[0], self.index_type, model.offsets[0] * m);
}
}
@ -303,21 +303,21 @@ init_shader! {
vert = "model_vertex",
frag = "model_frag",
attribute = {
position => "aPosition",
texture_info => "aTextureInfo",
texture_offset => "aTextureOffset",
color => "aColor",
id => "id",
optional position => "aPosition",
optional texture_info => "aTextureInfo",
optional texture_offset => "aTextureOffset",
optional color => "aColor",
optional id => "id",
},
uniform = {
perspective_matrix => "perspectiveMatrix",
camera_matrix => "cameraMatrix",
model_matrix => "modelMatrix[]",
texture => "textures",
light_level => "lightLevel",
sky_offset => "skyOffset",
lighting => "lighting",
color_mul => "colorMul[]",
optional perspective_matrix => "perspectiveMatrix",
optional camera_matrix => "cameraMatrix",
optional model_matrix => "modelMatrix[]",
optional texture => "textures",
optional light_level => "lightLevel",
optional sky_offset => "skyOffset",
optional lighting => "lighting",
optional color_mul => "colorMul[]",
},
}
}

View File

@ -56,12 +56,18 @@ macro_rules! init_shader {
frag = $frag:expr, $(#$fdef:ident)*
attribute = {
$(
$field:ident => $glname:expr,
required $field:ident => $glname:expr,
)*
$(
optional $ofield:ident => $oglname:expr,
)*
},
uniform = {
$(
$ufield:ident => $uglname:expr,
required $ufield:ident => $uglname:expr,
)*
$(
optional $oufield:ident => $ouglname:expr,
)*
},
}
@ -71,10 +77,16 @@ macro_rules! init_shader {
program: gl::Program,
$(
$field: gl::Attribute,
)+
)*
$(
$ofield: Option<gl::Attribute>,
)*
$(
$ufield: gl::Uniform,
)+
)*
$(
$oufield: Option<gl::Uniform>,
)*
}
impl $name {
@ -90,11 +102,17 @@ macro_rules! init_shader {
let shader = shaders::create_program(&v, &f);
$name {
$(
$field: shader.attribute_location($glname),
)+
$field: shader.attribute_location($glname).unwrap(),
)*
$(
$ufield: shader.uniform_location($uglname),
)+
$ofield: shader.attribute_location($oglname),
)*
$(
$ufield: shader.uniform_location($uglname).unwrap(),
)*
$(
$oufield: shader.uniform_location($ouglname),
)*
program: shader,
}
}

View File

@ -56,14 +56,14 @@ init_shader! {
vert = "ui_vertex",
frag = "ui_frag",
attribute = {
position => "aPosition",
texture_info => "aTextureInfo",
texture_offset => "aTextureOffset",
color => "aColor",
required position => "aPosition",
required texture_info => "aTextureInfo",
required texture_offset => "aTextureOffset",
required color => "aColor",
},
uniform = {
texture => "textures",
screensize => "screenSize",
required texture => "textures",
required screensize => "screenSize",
},
}
}