Replace string lookups for uniforms with const int lookups

main
_ 2020-02-17 02:57:30 +00:00
parent 3c37cd030b
commit a1c21b92c9
2 changed files with 75 additions and 44 deletions

View File

@ -2,6 +2,8 @@ use glam::{Mat4, Vec3, Vec4};
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::{Keycode, Scancode}; use sdl2::keyboard::{Keycode, Scancode};
use std::collections::*;
use std::iter::FromIterator;
use std::time::{Duration}; use std::time::{Duration};
use opengl_rust::*; use opengl_rust::*;
@ -94,6 +96,18 @@ impl WorldState {
} }
} }
mod uniforms {
use iota::iota;
iota! {
pub const MVP: u32 = iota;
, OBJECT_SPACE_LIGHT
, ALBEDO
, MIN_ALBEDO
, MIN_BRIGHT
, TEXTURE
}
}
fn main () { fn main () {
let sdl_context = sdl2::init ().unwrap (); let sdl_context = sdl2::init ().unwrap ();
let video_subsystem = sdl_context.video ().unwrap (); let video_subsystem = sdl_context.video ().unwrap ();
@ -127,14 +141,24 @@ fn main () {
let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap (); let shader_program = ShaderProgram::new (&vert_shader, &frag_shader).unwrap ();
let unis = shader_program.get_uniform_locations (vec! [ let uni_lookup: HashMap <_, &str> = HashMap::from_iter ({
"mvp", use uniforms::*;
"object_space_light", vec! [
"albedo", (MVP, "mvp"),
"min_albedo", (OBJECT_SPACE_LIGHT, "object_space_light"),
"min_bright", (ALBEDO, "albedo"),
"texture", (MIN_ALBEDO, "min_albedo"),
].into_iter ()); (MIN_BRIGHT, "min_bright"),
(TEXTURE, "texture"),
].into_iter ()
});
let unis = shader_program.get_uniform_locations (uni_lookup.values ().map (|s| *s));
let unis: HashMap <u32, i32> = HashMap::from_iter (
uni_lookup.iter ()
.map (|(key, name)| (*key, *unis.get (*name).unwrap ()))
);
let attrs = shader_program.get_attribute_locations (vec! [ let attrs = shader_program.get_attribute_locations (vec! [
"pos", "pos",
@ -224,28 +248,31 @@ fn main () {
glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT); glezz::clear (gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
glezz::disable (gl::CULL_FACE); glezz::disable (gl::CULL_FACE);
glezz::uniform_3fv (unis ["min_bright"], &black); {
glezz::uniform_3fv (unis ["min_albedo"], &white); use uniforms::*;
glezz::uniform_3fv (unis ["albedo"], &orange); glezz::uniform_3fv (unis [&MIN_BRIGHT], &black);
glezz::uniform_3fv (unis [&MIN_ALBEDO], &white);
glezz::uniform_3fv (unis [&ALBEDO], &orange);
glezz::uniform_matrix_4fv (unis ["mvp"], &mvp_mat); glezz::uniform_matrix_4fv (unis [&MVP], &mvp_mat);
glezz::uniform_3fv (unis ["object_space_light"], &object_space_light); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
renderable_model.draw (&attrs, 0); renderable_model.draw (&attrs, 0);
glezz::uniform_3fv (unis ["albedo"], &green); glezz::uniform_3fv (unis [&ALBEDO], &green);
renderable_model.draw (&attrs, 1); renderable_model.draw (&attrs, 1);
let draw_sky = true; let draw_sky = true;
if draw_sky { if draw_sky {
glezz::uniform_matrix_4fv (unis ["mvp"], &sky_mvp_mat); glezz::uniform_matrix_4fv (unis [&MVP], &sky_mvp_mat);
glezz::uniform_3fv (unis ["albedo"], &white); glezz::uniform_3fv (unis [&ALBEDO], &white);
glezz::uniform_3fv (unis ["min_bright"], &white); glezz::uniform_3fv (unis [&MIN_BRIGHT], &white);
glezz::uniform_3fv (unis ["min_albedo"], &black); glezz::uniform_3fv (unis [&MIN_ALBEDO], &black);
glezz::uniform_1i (unis ["texture"], 0); glezz::uniform_1i (unis [&TEXTURE], 0);
renderable_sky.draw (&attrs, 0); renderable_sky.draw (&attrs, 0);
} }
}
window.gl_swap_window (); window.gl_swap_window ();

View File

@ -40,26 +40,30 @@ iota! {
} }
pub mod types { pub mod types {
pub const POSITION: usize = 0; iota! {
pub const TEXCOORD: usize = 1; pub const POSITION: usize = iota;
pub const NORMAL: usize = 2; , TEXCOORD
pub const TANGENT: usize = 3; , NORMAL
pub const BLENDINDEXES: usize = 4; , TANGENT
pub const BLENDWEIGHTS: usize = 5; , BLENDINDEXES
pub const COLOR: usize = 6; , BLENDWEIGHTS
, COLOR
}
pub const CUSTOM: usize = 0x10; pub const CUSTOM: usize = 0x10;
} }
pub mod formats { pub mod formats {
pub const BYTE: u32 = 0; iota! {
pub const UBYTE: u32 = 1; pub const BYTE: u32 = iota;
pub const SHORT: u32 = 2; , UBYTE
pub const USHORT: u32 = 3; , SHORT
pub const INT: u32 = 4; , USHORT
pub const UINT: u32 = 5; , INT
pub const HALF: u32 = 6; , UINT
pub const FLOAT: u32 = 7; , HALF
pub const DOUBLE: u32 = 8; , FLOAT
, DOUBLE
}
} }
#[derive (Debug)] #[derive (Debug)]