Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
expose user textures, add repeat to sampling for wgpu backend
Browse files Browse the repository at this point in the history
  • Loading branch information
coderedart committed Aug 17, 2023
1 parent a4d4f8c commit 4665b76
Show file tree
Hide file tree
Showing 18 changed files with 754 additions and 671 deletions.
1 change: 0 additions & 1 deletion crates/egui_render_glow/egui.vert

This file was deleted.

27 changes: 27 additions & 0 deletions crates/egui_render_glow/egui.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 300 es


// vertex input
layout(location = 0) in vec2 vin_pos; // vertex in position
layout(location = 1) in vec2 vin_tc; // vertex in texture coordinates
layout(location = 2) in vec4 vin_sc; // vertex in normalized srgba color

// vertex output
out vec2 vout_tc; // vertex out texture coordinates
out vec4 vout_sc; // srgb color

// vertex uniform
uniform vec2 u_screen_size; // in physical pixels


void main() {
gl_Position = vec4(
2.0 * vin_pos.x / u_screen_size.x - 1.0,
1.0 - 2.0 * vin_pos.y / u_screen_size.y,
0.0,
1.0);
vout_tc = vin_tc;
// egui does everything in srgb space
vout_sc = vin_sc;

}
1 change: 0 additions & 1 deletion crates/egui_render_glow/egui_linear_output.frag

This file was deleted.

38 changes: 38 additions & 0 deletions crates/egui_render_glow/egui_linear_output.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#version 300 es

precision highp float;
// fragment shader uniforms. texture and sampler
uniform sampler2D u_sampler;

// fragment inputs
in vec2 vout_tc;
in vec4 vout_sc; // srgb color

out vec4 fout_color;

// Converts a color from linear light gamma to sRGB gamma
vec4 from_linear(vec4 linearRGB)
{
bvec3 cutoff = lessThan(linearRGB.rgb, vec3(0.0031308));
vec3 higher = vec3(1.055)*pow(linearRGB.rgb, vec3(1.0/2.4)) - vec3(0.055);
vec3 lower = linearRGB.rgb * vec3(12.92);

return vec4(mix(higher, lower, cutoff), linearRGB.a);
}

// Converts a color from sRGB gamma to linear light gamma
vec4 to_linear(vec4 sRGB)
{
bvec3 cutoff = lessThan(sRGB.rgb, vec3(0.04045));
vec3 higher = pow((sRGB.rgb + vec3(0.055))/vec3(1.055), vec3(2.4));
vec3 lower = sRGB.rgb/vec3(12.92);

return vec4(mix(higher, lower, cutoff), sRGB.a);
}

void main() {
// we output in linear. so that gpu can convert it into srgb for srgb framebuffers
fout_color = to_linear(vout_sc * from_linear(texture(u_sampler, vout_tc)));
}


1 change: 0 additions & 1 deletion crates/egui_render_glow/egui_srgb_output.frag

This file was deleted.

27 changes: 27 additions & 0 deletions crates/egui_render_glow/egui_srgb_output.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#version 300 es

precision highp float;
// fragment shader uniforms. texture and sampler
uniform sampler2D u_sampler;

// fragment inputs
in vec2 vout_tc;
in vec4 vout_sc; // srgb color

out vec4 fout_color;


// Converts a color from linear light gamma to sRGB gamma
vec4 from_linear(vec4 linearRGB)
{
bvec3 cutoff = lessThan(linearRGB.rgb, vec3(0.0031308));
vec3 higher = vec3(1.055)*pow(linearRGB.rgb, vec3(1.0/2.4)) - vec3(0.055);
vec3 lower = linearRGB.rgb * vec3(12.92);

return vec4(mix(higher, lower, cutoff), linearRGB.a);
}

void main() {
// we output in srgb space to write to linear framebuffers without gpu doing any conversion
fout_color = vout_sc * from_linear(texture(u_sampler, vout_tc));
}
46 changes: 44 additions & 2 deletions crates/egui_render_glow/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub unsafe fn create_egui_vao_buffers(
(vao, vbo, ebo)
}

pub unsafe fn create_samplers(glow_context: &glow::Context) -> (Sampler, Sampler) {
pub unsafe fn create_samplers(glow_context: &glow::Context) -> (Sampler, Sampler, Sampler) {
let nearest_sampler = glow_context
.create_sampler()
.expect("failed to create nearest sampler");
Expand All @@ -186,6 +186,42 @@ pub unsafe fn create_samplers(glow_context: &glow::Context) -> (Sampler, Sampler
);
glow_error!(glow_context);

let font_sampler = glow_context
.create_sampler()
.expect("failed to create linear sampler");
glow_context.bind_sampler(0, Some(font_sampler));
glow_error!(glow_context);

glow_context.sampler_parameter_i32(
font_sampler,
glow::TEXTURE_MAG_FILTER,
glow::LINEAR
.try_into()
.expect("failed to fit LINEAR MIPMAP NEAREST in i32"),
);
glow_error!(glow_context);

glow_context.sampler_parameter_i32(
font_sampler,
glow::TEXTURE_MIN_FILTER,
glow::LINEAR
.try_into()
.expect("failed to fit LINEAR MIPMAP NEAREST in i32"),
);
glow_error!(glow_context);
glow_context.sampler_parameter_i32(
font_sampler,
glow::TEXTURE_WRAP_S,
glow::CLAMP_TO_EDGE as i32,
);
glow_error!(glow_context);

glow_context.sampler_parameter_i32(
font_sampler,
glow::TEXTURE_WRAP_T,
glow::CLAMP_TO_EDGE as i32,
);
glow_error!(glow_context);
let linear_sampler = glow_context
.create_sampler()
.expect("failed to create linear sampler");
Expand All @@ -209,7 +245,13 @@ pub unsafe fn create_samplers(glow_context: &glow::Context) -> (Sampler, Sampler
.expect("failed to fit LINEAR MIPMAP NEAREST in i32"),
);
glow_error!(glow_context);
(linear_sampler, nearest_sampler)
glow_context.sampler_parameter_i32(linear_sampler, glow::TEXTURE_WRAP_S, glow::REPEAT as i32);
glow_error!(glow_context);

glow_context.sampler_parameter_i32(linear_sampler, glow::TEXTURE_WRAP_T, glow::REPEAT as i32);
glow_error!(glow_context);

(linear_sampler, nearest_sampler, font_sampler)
}

#[allow(unused)]
Expand Down
13 changes: 10 additions & 3 deletions crates/egui_render_glow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub struct Painter {
/// Most of these objects are created at startup
pub linear_sampler: Sampler,
pub nearest_sampler: Sampler,
pub font_sampler: Sampler,
pub managed_textures: IntMap<GpuTexture>,
pub egui_program: Program,
pub vao: VertexArray,
Expand Down Expand Up @@ -233,7 +234,7 @@ impl Painter {
gl.use_program(Some(egui_program));
let (vao, vbo, ebo) = create_egui_vao_buffers(gl, egui_program);
debug!("created egui vao, vbo, ebo");
let (linear_sampler, nearest_sampler) = create_samplers(gl);
let (linear_sampler, nearest_sampler, font_sampler) = create_samplers(gl);
debug!("created linear and nearest samplers");
Self {
managed_textures: Default::default(),
Expand All @@ -243,6 +244,7 @@ impl Painter {
ebo,
linear_sampler,
nearest_sampler,
font_sampler,
u_screen_size,
u_sampler,
clipped_primitives: Vec::new(),
Expand Down Expand Up @@ -270,6 +272,10 @@ impl Painter {

// update textures
for (texture_id, delta) in textures_delta.set {
let sampler = match delta.options.minification {
egui::TextureFilter::Nearest => self.nearest_sampler,
egui::TextureFilter::Linear => self.linear_sampler,
};
match texture_id {
TextureId::Managed(managed) => {
glow_context.bind_texture(
Expand All @@ -285,9 +291,10 @@ impl Painter {
width: 0,
height: 0,
sampler: if managed == 0 {
self.nearest_sampler
// special sampler for font that would clamp to edge
self.font_sampler
} else {
self.linear_sampler
sampler
},
})
.handle
Expand Down
14 changes: 0 additions & 14 deletions crates/egui_render_rend3/Cargo.toml

This file was deleted.

145 changes: 0 additions & 145 deletions crates/egui_render_rend3/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/egui_render_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "egui_render_wgpu"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
description = "egui rendering backend using wgpu"
license = "MIT"
Expand Down

0 comments on commit 4665b76

Please sign in to comment.