mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -04:00
Use Rgba (4xf32) instead of Color32 in all interfaces
This simplifies a few things, but some benchmarks gets worse, probably due to the increased memory use (and thus more cache misses). I don't plan to merge this, but leave it here as an experiment
This commit is contained in:
@@ -2,30 +2,16 @@ precision mediump float;
|
||||
uniform vec2 u_screen_size;
|
||||
attribute vec2 a_pos;
|
||||
attribute vec2 a_tc;
|
||||
attribute vec4 a_srgba;
|
||||
varying vec4 v_rgba;
|
||||
attribute vec4 a_rgba; // 0-1
|
||||
varying vec4 v_rgba; // 0-1
|
||||
varying vec2 v_tc;
|
||||
|
||||
// 0-1 linear from 0-255 sRGB
|
||||
vec3 linear_from_srgb(vec3 srgb) {
|
||||
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
|
||||
vec3 lower = srgb / vec3(3294.6);
|
||||
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
// 0-1 linear from 0-255 sRGBA
|
||||
vec4 linear_from_srgba(vec4 srgba) {
|
||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(
|
||||
2.0 * a_pos.x / u_screen_size.x - 1.0,
|
||||
1.0 - 2.0 * a_pos.y / u_screen_size.y,
|
||||
0.0,
|
||||
1.0);
|
||||
// egui encodes vertex colors in gamma spaces, so we must decode the colors here:
|
||||
v_rgba = linear_from_srgba(a_srgba);
|
||||
v_rgba = a_rgba;
|
||||
v_tc = a_tc;
|
||||
}
|
||||
|
||||
@@ -2,30 +2,16 @@ precision mediump float;
|
||||
uniform vec2 u_screen_size;
|
||||
attribute vec2 a_pos;
|
||||
attribute vec2 a_tc;
|
||||
attribute vec4 a_srgba;
|
||||
varying vec4 v_rgba;
|
||||
attribute vec4 a_rgba; // 0-1
|
||||
varying vec4 v_rgba; // 0-1
|
||||
varying vec2 v_tc;
|
||||
|
||||
// 0-1 linear from 0-255 sRGB
|
||||
vec3 linear_from_srgb(vec3 srgb) {
|
||||
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
|
||||
vec3 lower = srgb / vec3(3294.6);
|
||||
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
// 0-1 linear from 0-255 sRGBA
|
||||
vec4 linear_from_srgba(vec4 srgba) {
|
||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(
|
||||
2.0 * a_pos.x / u_screen_size.x - 1.0,
|
||||
1.0 - 2.0 * a_pos.y / u_screen_size.y,
|
||||
0.0,
|
||||
1.0);
|
||||
// egui encodes vertex colors in gamma spaces, so we must decode the colors here:
|
||||
v_rgba = linear_from_srgba(a_srgba);
|
||||
v_rgba = a_rgba;
|
||||
v_tc = a_tc;
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ impl WebGlPainter {
|
||||
|
||||
let mut positions: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
||||
let mut tex_coords: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
||||
let mut colors: Vec<u8> = Vec::with_capacity(4 * mesh.vertices.len());
|
||||
let mut colors: Vec<f32> = Vec::with_capacity(4 * mesh.vertices.len());
|
||||
for v in &mesh.vertices {
|
||||
positions.push(v.pos.x);
|
||||
positions.push(v.pos.y);
|
||||
@@ -289,29 +289,22 @@ impl WebGlPainter {
|
||||
let colors_memory_buffer = wasm_bindgen::memory()
|
||||
.dyn_into::<WebAssembly::Memory>()?
|
||||
.buffer();
|
||||
let colors_ptr = colors.as_ptr() as u32;
|
||||
let colors_ptr = colors.as_ptr() as u32 / 4;
|
||||
let colors_array = js_sys::Uint8Array::new(&colors_memory_buffer)
|
||||
.subarray(colors_ptr, colors_ptr + colors.len() as u32);
|
||||
|
||||
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.color_buffer));
|
||||
gl.buffer_data_with_array_buffer_view(Gl::ARRAY_BUFFER, &colors_array, Gl::STREAM_DRAW);
|
||||
|
||||
let a_srgba_loc = gl.get_attrib_location(&self.program, "a_srgba");
|
||||
assert!(a_srgba_loc >= 0);
|
||||
let a_srgba_loc = a_srgba_loc as u32;
|
||||
let a_rgba_loc = gl.get_attrib_location(&self.program, "a_rgba");
|
||||
assert!(a_rgba_loc >= 0);
|
||||
let a_rgba_loc = a_rgba_loc as u32;
|
||||
|
||||
let normalize = false;
|
||||
let stride = 0;
|
||||
let offset = 0;
|
||||
gl.vertex_attrib_pointer_with_i32(
|
||||
a_srgba_loc,
|
||||
4,
|
||||
Gl::UNSIGNED_BYTE,
|
||||
normalize,
|
||||
stride,
|
||||
offset,
|
||||
);
|
||||
gl.enable_vertex_attrib_array(a_srgba_loc);
|
||||
gl.vertex_attrib_pointer_with_i32(a_rgba_loc, 4, Gl::FLOAT, normalize, stride, offset);
|
||||
gl.enable_vertex_attrib_array(a_rgba_loc);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ impl WebGl2Painter {
|
||||
|
||||
let mut positions: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
||||
let mut tex_coords: Vec<f32> = Vec::with_capacity(2 * mesh.vertices.len());
|
||||
let mut colors: Vec<u8> = Vec::with_capacity(4 * mesh.vertices.len());
|
||||
let mut colors: Vec<f32> = Vec::with_capacity(4 * mesh.vertices.len());
|
||||
for v in &mesh.vertices {
|
||||
positions.push(v.pos.x);
|
||||
positions.push(v.pos.y);
|
||||
@@ -290,29 +290,22 @@ impl WebGl2Painter {
|
||||
let colors_memory_buffer = wasm_bindgen::memory()
|
||||
.dyn_into::<WebAssembly::Memory>()?
|
||||
.buffer();
|
||||
let colors_ptr = colors.as_ptr() as u32;
|
||||
let colors_ptr = colors.as_ptr() as u32 / 4;
|
||||
let colors_array = js_sys::Uint8Array::new(&colors_memory_buffer)
|
||||
.subarray(colors_ptr, colors_ptr + colors.len() as u32);
|
||||
|
||||
gl.bind_buffer(Gl::ARRAY_BUFFER, Some(&self.color_buffer));
|
||||
gl.buffer_data_with_array_buffer_view(Gl::ARRAY_BUFFER, &colors_array, Gl::STREAM_DRAW);
|
||||
|
||||
let a_srgba_loc = gl.get_attrib_location(&self.program, "a_srgba");
|
||||
assert!(a_srgba_loc >= 0);
|
||||
let a_srgba_loc = a_srgba_loc as u32;
|
||||
let a_rgba_loc = gl.get_attrib_location(&self.program, "a_rgba");
|
||||
assert!(a_rgba_loc >= 0);
|
||||
let a_rgba_loc = a_rgba_loc as u32;
|
||||
|
||||
let normalize = false;
|
||||
let stride = 0;
|
||||
let offset = 0;
|
||||
gl.vertex_attrib_pointer_with_i32(
|
||||
a_srgba_loc,
|
||||
4,
|
||||
Gl::UNSIGNED_BYTE,
|
||||
normalize,
|
||||
stride,
|
||||
offset,
|
||||
);
|
||||
gl.enable_vertex_attrib_array(a_srgba_loc);
|
||||
gl.vertex_attrib_pointer_with_i32(a_rgba_loc, 4, Gl::FLOAT, normalize, stride, offset);
|
||||
gl.enable_vertex_attrib_array(a_rgba_loc);
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user