Squashed figure vertices for better performance

This commit is contained in:
Joshua Barretto 2020-04-25 15:07:58 +01:00
parent 37301706d4
commit 7208b37dc5
3 changed files with 39 additions and 21 deletions

View File

@ -2,11 +2,10 @@
#include <globals.glsl> #include <globals.glsl>
in vec3 v_pos; in uint v_pos_norm;
in vec3 v_norm; in vec3 v_norm;
in vec3 v_col; in uint v_col;
in float v_ao; in uint v_ao_bone;
in uint v_bone_idx;
layout (std140) layout (std140)
uniform u_locals { uniform u_locals {
@ -33,20 +32,27 @@ flat out vec3 f_norm;
void main() { void main() {
// Pre-calculate bone matrix // Pre-calculate bone matrix
mat4 combined_mat = model_mat * bones[v_bone_idx].bone_mat; uint bone_idx = (v_ao_bone >> 2) & 0x3Fu;
mat4 combined_mat = model_mat * bones[bone_idx].bone_mat;
vec3 pos = vec3((uvec3(v_pos_norm) >> uvec3(0, 8, 16)) & uvec3(0xFFu)) - 128.0;
f_pos = ( f_pos = (
combined_mat * combined_mat *
vec4(v_pos, 1)).xyz; vec4(pos, 1)).xyz;
f_col = v_col; f_col = vec3((uvec3(v_col) >> uvec3(0, 8, 16)) & uvec3(0xFFu)) / 255.0;
f_ao = v_ao; f_ao = float(v_ao_bone & 0x3u) / 4.0;
// First 3 normals are negative, next 3 are positive
vec3 normals[6] = vec3[](vec3(-1,0,0), vec3(1,0,0), vec3(0,-1,0), vec3(0,1,0), vec3(0,0,-1), vec3(0,0,1));
vec3 norm = normals[(v_pos_norm >> 24) & 0x7u];
// Calculate normal here rather than for each pixel in the fragment shader // Calculate normal here rather than for each pixel in the fragment shader
f_norm = normalize(( f_norm = normalize((
combined_mat * combined_mat *
vec4(v_norm, 0.0) vec4(norm, 0.0)
).xyz); ).xyz);
gl_Position = all_mat * vec4(f_pos, 1); gl_Position = all_mat * vec4(f_pos, 1);

View File

@ -11,11 +11,12 @@ use vek::*;
gfx_defines! { gfx_defines! {
vertex Vertex { vertex Vertex {
pos: [f32; 3] = "v_pos", pos_norm: u32 = "v_pos_norm",
norm: [f32; 3] = "v_norm", col: u32 = "v_col",
col: [f32; 3] = "v_col", // BBBBBBAA
ao: f32 = "v_ao", // B = Bone
bone_idx: u8 = "v_bone_idx", // A = AO
ao_bone: u8 = "v_ao_bone",
} }
constant Locals { constant Locals {
@ -46,17 +47,26 @@ gfx_defines! {
impl Vertex { impl Vertex {
pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>, ao: f32, bone_idx: u8) -> Self { pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>, ao: f32, bone_idx: u8) -> Self {
let norm_bits = if norm.x != 0.0 {
if norm.x < 0.0 { 0 } else { 1 }
} else if norm.y != 0.0 {
if norm.y < 0.0 { 2 } else { 3 }
} else {
if norm.z < 0.0 { 4 } else { 5 }
};
Self { Self {
pos: pos.into_array(), pos_norm: pos
col: col.into_array(), .map2(Vec3::new(0, 8, 16), |e, shift| ((e + 128.0) as u32) << shift)
norm: norm.into_array(), .reduce_bitor() | (norm_bits << 24),
ao, col: col
bone_idx, .map2(Rgb::new(0, 8, 16), |e, shift| ((e * 255.0) as u32) << shift)
.reduce_bitor(),
ao_bone: (bone_idx << 2) | ((ao * 3.9999) as u8),
} }
} }
pub fn with_bone_idx(mut self, bone_idx: u8) -> Self { pub fn with_bone_idx(mut self, bone_idx: u8) -> Self {
self.bone_idx = bone_idx; self.ao_bone = (self.ao_bone & 0b11) | (bone_idx << 2);
self self
} }
} }

View File

@ -56,7 +56,9 @@ impl Vertex {
Self { Self {
pos: pos.into_array(), pos: pos.into_array(),
col: col.map2(Rgb::new(0, 8, 16), |e, shift| ((e * 255.0) as u32) << shift).reduce_bitor(), col: col
.map2(Rgb::new(0, 8, 16), |e, shift| ((e * 255.0) as u32) << shift)
.reduce_bitor(),
norm_ao: norm_bits | (((ao * 3.9999) as u32) << 3), norm_ao: norm_bits | (((ao * 3.9999) as u32) << 3),
} }
} }