Set colors on CPU

Indexing colors on the vertex shader added complexity and after
profiling suggests perf is basically the same. This commit will still be
here in case it makes sense to try this again at some point.
This commit is contained in:
Joe Wilm 2016-10-28 08:36:26 -07:00
parent cb2fa27f15
commit f8cb6d42cc
2 changed files with 43 additions and 94 deletions

View File

@ -24,16 +24,14 @@ layout (location = 2) in vec4 glyph;
layout (location = 3) in vec4 uv;
// text fg color
layout (location = 4) in uvec4 textColor;
layout (location = 4) in vec3 textColor;
// Background color
layout (location = 5) in uvec4 backgroundColor;
layout (location = 5) in vec3 backgroundColor;
out vec2 TexCoords;
out vec3 fg;
out vec3 bg;
uniform vec3 colors[18];
// Terminal properties
uniform vec2 termDim;
uniform vec2 cellDim;
@ -75,33 +73,6 @@ void main()
}
background = backgroundPass;
switch (textColor.x) {
case 0u:
// cell::Color::Rgb
fg = vec3(textColor.yzw) / vec3(255.0, 255.0, 255.0);
break;
case 1u:
// cell::Color::Ansi
fg = vec3(colors[textColor.y]);
break;
default:
// Should never happen; let's make it red
fg = vec3(1.0, 0.0, 0.0);
break;
}
switch (backgroundColor.x) {
case 0u:
// cell::Color::Rgb
bg = vec3(backgroundColor.yzw) / vec3(255.0, 255.0, 255.0);
break;
case 1u:
// cell::Color::Ansi
bg = vec3(colors[backgroundColor.y]);
break;
default:
// Should never happen; let's make it blue
bg = vec3(0.0, 0.0, 1.0);
break;
}
bg = backgroundColor / vec3(255.0, 255.0, 255.0);
fg = textColor / vec3(255.0, 255.0, 255.0);
}

View File

@ -66,11 +66,6 @@ pub struct ShaderProgram {
///
/// Rendering is split into two passes; 1 for backgrounds, and one for text
u_background: GLint,
/// Color uniforms
///
/// Ansi colors are uploaded to the GPU and indexed in the vertex shader
u_colors: [GLint; 18],
}
@ -219,15 +214,13 @@ struct InstanceData {
uv_width: f32,
uv_height: f32,
// color
disc: u32,
r: u32,
g: u32,
b: u32,
r: f32,
g: f32,
b: f32,
// background color
bg_disc: u32,
bg_r: u32,
bg_g: u32,
bg_b: u32,
bg_r: f32,
bg_g: f32,
bg_b: f32,
}
#[derive(Debug)]
@ -269,14 +262,16 @@ pub struct PackedVertex {
pub struct Batch {
tex: GLuint,
instances: Vec<InstanceData>,
colors: [Rgb; 18],
}
impl Batch {
#[inline]
pub fn new() -> Batch {
pub fn new(colors: [Rgb; 18]) -> Batch {
Batch {
tex: 0,
instances: Vec::with_capacity(BATCH_MAX),
colors: colors,
}
}
@ -285,8 +280,15 @@ impl Batch {
self.tex = glyph.tex_id;
}
let fg: &[u8; 4] = unsafe { ::std::mem::transmute(&cell.fg) };
let bg: &[u8; 4] = unsafe { ::std::mem::transmute(&cell.bg) };
let fg = match cell.fg {
::term::cell::Color::Rgb(rgb) => rgb,
::term::cell::Color::Ansi(ansi) => self.colors[ansi as usize],
};
let bg = match cell.bg {
::term::cell::Color::Rgb(rgb) => rgb,
::term::cell::Color::Ansi(ansi) => self.colors[ansi as usize],
};
let mut instance = InstanceData {
col: col,
@ -302,27 +304,23 @@ impl Batch {
uv_width: glyph.uv_width,
uv_height: glyph.uv_height,
disc: fg[0] as u32,
r: fg[1] as u32,
g: fg[2] as u32,
b: fg[3] as u32,
r: fg.r as f32,
g: fg.g as f32,
b: fg.b as f32,
bg_disc: bg[0] as u32,
bg_r: bg[1] as u32,
bg_g: bg[2] as u32,
bg_b: bg[3] as u32,
bg_r: bg.r as f32,
bg_g: bg.g as f32,
bg_b: bg.b as f32,
};
if cell.flags.contains(cell::INVERSE) {
instance.disc = bg[0] as u32;
instance.r = bg[1] as u32;
instance.g = bg[2] as u32;
instance.b = bg[3] as u32;
instance.r = bg.r as f32;
instance.g = bg.g as f32;
instance.b = bg.b as f32;
instance.bg_disc = fg[0] as u32;
instance.bg_r = fg[1] as u32;
instance.bg_g = fg[2] as u32;
instance.bg_b = fg[3] as u32;
instance.bg_r = fg.r as f32;
instance.bg_g = fg.g as f32;
instance.bg_b = fg.b as f32;
}
self.instances.push(instance);
@ -366,8 +364,7 @@ const ATLAS_SIZE: i32 = 1024;
impl QuadRenderer {
// TODO should probably hand this a transform instead of width/height
pub fn new(config: &Config, width: u32, height: u32) -> QuadRenderer {
let colors = config.color_list();
let program = ShaderProgram::new(&colors, width, height).unwrap();
let program = ShaderProgram::new(width, height).unwrap();
let mut vao: GLuint = 0;
let mut vbo: GLuint = 0;
@ -448,17 +445,17 @@ impl QuadRenderer {
gl::EnableVertexAttribArray(3);
gl::VertexAttribDivisor(3, 1);
// color
gl::VertexAttribIPointer(4, 4,
gl::UNSIGNED_INT,
gl::VertexAttribPointer(4, 3,
gl::FLOAT, gl::FALSE,
size_of::<InstanceData>() as i32,
(10 * size_of::<f32>()) as *const _);
gl::EnableVertexAttribArray(4);
gl::VertexAttribDivisor(4, 1);
// color
gl::VertexAttribIPointer(5, 4,
gl::UNSIGNED_INT,
gl::VertexAttribPointer(5, 3,
gl::FLOAT, gl::FALSE,
size_of::<InstanceData>() as i32,
(14 * size_of::<f32>()) as *const _);
(13 * size_of::<f32>()) as *const _);
gl::EnableVertexAttribArray(5);
gl::VertexAttribDivisor(5, 1);
@ -505,7 +502,7 @@ impl QuadRenderer {
vbo_instance: vbo_instance,
atlas: Vec::new(),
active_tex: 0,
batch: Batch::new(),
batch: Batch::new(config.color_list()),
colors: config.color_list(),
rx: msg_rx,
};
@ -518,9 +515,7 @@ impl QuadRenderer {
pub fn update_config(&mut self, config: &Config) {
self.colors = config.color_list();
self.program.activate();
self.program.set_color_uniforms(&self.colors);
self.program.deactivate();
self.batch.colors = config.color_list();
}
pub fn with_api<F, T>(&mut self, props: &term::SizeInfo, func: F) -> T
@ -577,7 +572,7 @@ impl QuadRenderer {
}
pub fn reload_shaders(&mut self, width: u32, height: u32) {
let program = match ShaderProgram::new(&self.colors, width, height) {
let program = match ShaderProgram::new(width, height) {
Ok(program) => program,
Err(err) => {
match err {
@ -797,7 +792,6 @@ impl ShaderProgram {
}
pub fn new(
colors: &[Rgb; 18],
width: u32,
height: u32
) -> Result<ShaderProgram, ShaderCreationError> {
@ -850,7 +844,6 @@ impl ShaderProgram {
let shader = ShaderProgram {
id: program,
u_colors: color_uniforms,
u_projection: projection,
u_term_dim: term_dim,
u_cell_dim: cell_dim,
@ -858,7 +851,6 @@ impl ShaderProgram {
};
shader.update_projection(width as f32, height as f32);
shader.set_color_uniforms(colors);
shader.deactivate();
@ -879,20 +871,6 @@ impl ShaderProgram {
}
fn set_color_uniforms(&self, colors: &[Rgb; 18]) {
println!("setting colors: {:#?}", colors);
for (i, rgb) in colors.iter().enumerate() {
unsafe {
gl::Uniform3f(
self.u_colors[i],
rgb.r as f32 / 255.0,
rgb.g as f32 / 255.0,
rgb.b as f32 / 255.0,
);
}
}
}
fn set_term_uniforms(&self, props: &term::SizeInfo) {
unsafe {
gl::Uniform2f(self.u_term_dim, props.width, props.height);