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>
in vec3 v_pos;
in vec3 v_norm;
in vec3 v_col;
in uint v_pos;
in uint v_col_norm;
layout (std140)
uniform u_locals {
@ -16,12 +15,26 @@ out vec3 f_norm;
out vec3 f_col;
void main() {
f_pos = v_pos;
f_norm = v_norm;
f_col = v_col;
f_pos = vec3(
float((v_pos >> 0) & 0x00FFu),
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 =
proj_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::{
self,
gfx_constant_struct_meta,
@ -10,8 +10,6 @@ use gfx::{
gfx_vertex_struct_meta,
};
use vek::*;
// Local
use super::{
super::{Pipeline, TgtColorFmt, TgtDepthFmt},
Globals,
@ -19,9 +17,8 @@ use super::{
gfx_defines! {
vertex Vertex {
pos: [f32; 3] = "v_pos",
norm: [f32; 3] = "v_norm",
col: [f32; 3] = "v_col",
pos: u32 = "v_pos",
col_norm: u32 = "v_col_norm",
}
constant Locals {
@ -42,9 +39,17 @@ gfx_defines! {
impl Vertex {
pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>) -> Self {
Self {
pos: pos.into_array(),
col: col.into_array(),
norm: norm.into_array(),
pos: 0
| ((pos.x as u32) & 0x00FF) << 0
| ((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,
}
}
}