Improved GPU memory usage by ~5x by compressing vertex data

Former-commit-id: b657ae88e44c6ecbede8073ffe17991ba3a99886
This commit is contained in:
Joshua Barretto 2019-05-22 19:30:18 +01:00
parent 5636a5ac73
commit 1684013138
2 changed files with 34 additions and 16 deletions

View File

@ -2,9 +2,8 @@
#include <globals.glsl> #include <globals.glsl>
in vec3 v_pos; in uint v_pos;
in vec3 v_norm; in uint v_col_norm;
in vec3 v_col;
layout (std140) layout (std140)
uniform u_locals { uniform u_locals {
@ -16,12 +15,26 @@ out vec3 f_norm;
out vec3 f_col; out vec3 f_col;
void main() { void main() {
f_pos = v_pos; f_pos = vec3(
f_norm = v_norm; float((v_pos >> 0) & 0x00FFu),
f_col = v_col; float((v_pos >> 8) & 0x00FFu),
float((v_pos >> 16) & 0xFFFFu)
);
f_norm = vec3(
float((v_col_norm >> 0) & 0x3u),
float((v_col_norm >> 2) & 0x3u),
float((v_col_norm >> 4) & 0x3u)
) - 1.0;
f_col = vec3(
float((v_col_norm >> 8) & 0xFFu),
float((v_col_norm >> 16) & 0xFFu),
float((v_col_norm >> 24) & 0xFFu)
) / 255.0;
gl_Position = gl_Position =
proj_mat * proj_mat *
view_mat * view_mat *
vec4(v_pos + model_offs, 1); vec4(f_pos + model_offs, 1);
} }

View File

@ -1,4 +1,4 @@
// Library use std::ops::{Add, Mul, Div};
use gfx::{ use gfx::{
self, self,
gfx_constant_struct_meta, gfx_constant_struct_meta,
@ -10,8 +10,6 @@ use gfx::{
gfx_vertex_struct_meta, gfx_vertex_struct_meta,
}; };
use vek::*; use vek::*;
// Local
use super::{ use super::{
super::{Pipeline, TgtColorFmt, TgtDepthFmt}, super::{Pipeline, TgtColorFmt, TgtDepthFmt},
Globals, Globals,
@ -19,9 +17,8 @@ use super::{
gfx_defines! { gfx_defines! {
vertex Vertex { vertex Vertex {
pos: [f32; 3] = "v_pos", pos: u32 = "v_pos",
norm: [f32; 3] = "v_norm", col_norm: u32 = "v_col_norm",
col: [f32; 3] = "v_col",
} }
constant Locals { constant Locals {
@ -42,9 +39,17 @@ gfx_defines! {
impl Vertex { impl Vertex {
pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>) -> Self { pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>) -> Self {
Self { Self {
pos: pos.into_array(), pos: 0
col: col.into_array(), | ((pos.x as u32) & 0x00FF) << 0
norm: norm.into_array(), | ((pos.y as u32) & 0x00FF) << 8
| ((pos.z as u32) & 0xFFFF) << 16,
col_norm: 0
| ((col.r.mul(255.0) as u32) & 0xFF) << 8
| ((col.g.mul(255.0) as u32) & 0xFF) << 16
| ((col.b.mul(255.0) as u32) & 0xFF) << 24
| ((norm.x.add(1.0) as u32) & 0x3) << 0
| ((norm.y.add(1.0) as u32) & 0x3) << 2
| ((norm.z.add(1.0) as u32) & 0x3) << 4,
} }
} }
} }