Skip to content

Commit

Permalink
Added macos comptible shaders from unlimitedbacon#14
Browse files Browse the repository at this point in the history
Co-authored-by: Vitor Vasconcellos <[email protected]>
  • Loading branch information
dominickm and HeavenVolkoff committed Oct 25, 2024
1 parent 6bc9057 commit 3b42bde
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/fxaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,36 @@ struct SpriteVertex {

implement_vertex!(SpriteVertex, position, i_tex_coords);

#[cfg(target_os = "macos")]
fn get_shader() -> (&'static str, &'static str) {
return (
include_str!("shaders/fxaa.macos.vert"),
include_str!("shaders/fxaa.macos.frag"),
);
}

#[cfg(target_os = "windows")]
fn get_shader() -> (&'static str, &'static str) {
return (
include_str!("shaders/fxaa.vert"),
include_str!("shaders/fxaa.frag"),
);
}

#[cfg(target_os = "linux")]
fn get_shader() -> (&'static str, &'static str) {
return (
include_str!("shaders/fxaa.vert"),
include_str!("shaders/fxaa.frag"),
);
}

impl FxaaSystem {
pub fn new<F>(facade: &F) -> FxaaSystem
where
F: Facade + ?Sized,
{
let (vertex, fragment) = get_shader();
FxaaSystem {
context: facade.get_context().clone(),

Expand Down Expand Up @@ -69,8 +94,8 @@ impl FxaaSystem {

program: program!(facade,
100 => {
vertex: include_str!("shaders/fxaa.vert"),
fragment: include_str!("shaders/fxaa.frag"),
vertex: vertex,
fragment: fragment,
}
)
.unwrap(),
Expand Down
34 changes: 32 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, B
Ok(display)
}

#[cfg(target_os = "macos")]
fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, Box<dyn Error>> {
let size = PhysicalSize::new(config.width, config.height);
let event_loop: EventLoop<()> = EventLoopBuilder::new().build();
let cb = glutin::ContextBuilder::new();
let context = cb.build_headless(&event_loop, size)?;

let context = unsafe { context.treat_as_current() };
let display = glium::backend::glutin::headless::Headless::new(context)?;
print_context_info(&display);
Ok(display)
}

#[cfg(target_os = "linux")]
fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, Box<dyn Error>> {
use glium::glutin::platform::unix::{EventLoopBuilderExtUnix, HeadlessContextExt};
Expand Down Expand Up @@ -142,6 +155,24 @@ fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, B
Ok(display)
}

#[cfg(target_os = "macos")]
fn get_shader() -> (&'static str, &'static str) {
return (include_str!("shaders/model.macos.vert"),
include_str!("shaders/model.macos.frag"))
}

#[cfg(target_os = "windows")]
fn get_shader() -> (&'static str, &'static str) {
return (include_str!("shaders/model.vert"),
include_str!("shaders/model.frag"))
}

#[cfg(target_os = "linux")]
fn get_shader() -> (&'static str, &'static str) {
return (include_str!("shaders/model.vert"),
include_str!("shaders/model.frag"))
}

fn render_pipeline<F>(
display: &F,
config: &Config,
Expand All @@ -168,8 +199,7 @@ where
// Load and compile shaders
// ------------------------

let vertex_shader_src = include_str!("shaders/model.vert");
let pixel_shader_src = include_str!("shaders/model.frag");
let (vertex_shader_src, pixel_shader_src) = get_shader();

// TODO: Cache program binary
let program = glium::Program::from_source(display, vertex_shader_src, pixel_shader_src, None);
Expand Down
108 changes: 108 additions & 0 deletions src/shaders/fxaa.macos.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#version 150

//precision mediump float;

uniform vec2 resolution;
uniform sampler2D tex;
uniform int enabled;

in vec2 v_tex_coords;

out vec4 color;

#define FXAA_REDUCE_MIN (1.0/ 128.0)
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#define FXAA_SPAN_MAX 8.0

vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;

// Sample adjascent pixels
vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture(tex, v_rgbNW).xyz;
vec3 rgbNE = texture(tex, v_rgbNE).xyz;
vec3 rgbSW = texture(tex, v_rgbSW).xyz;
vec3 rgbSE = texture(tex, v_rgbSE).xyz;
vec4 texColor = texture(tex, v_rgbM);
vec3 rgbM = texColor.xyz;

// Calculate luminance
vec3 luma = vec3(0.299, 0.587, 0.114);
vec4 luma4 = vec4(0.299, 0.587, 0.114, 0.0);

float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));

// Determining blend direction
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));

float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);

float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;

// Blending
// A lot of stuff here is my attempt to incorporate the alpha channel into the blending. I don't think I did it right. ¯\_(ツ)_/¯
// A bit of the background color still bleeds through around the edges of the image. It's hardly noticable, though.
vec2 coordAA = fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5);
vec2 coordAB = fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5);
float alphaAA = texture(tex, coordAA).a;
float alphaAB = texture(tex, coordAB).a;
float alphaATotal = alphaAA + alphaAB;
float weightAA = alphaAA / alphaATotal;
float weightAB = alphaAB / alphaATotal;
vec4 rgbA = vec4( // Multiply by 2
texture(tex, coordAA).rgb*weightAA + texture(tex, coordAB).rgb*weightAB,
0.5 * (alphaAA + alphaAB)
);

vec2 coordBC = fragCoord * inverseVP + dir * -0.5;
vec2 coordBD = fragCoord * inverseVP + dir * 0.5;
float alphaBC = texture(tex, coordBC).a;
float alphaBD = texture(tex, coordBD).a;
float alphaBTotal = alphaAA + alphaAB + alphaBC + alphaBD;
float weightBA = alphaAA / alphaBTotal;
float weightBB = alphaAB / alphaBTotal;
float weightBC = alphaBC / alphaBTotal;
float weightBD = alphaBD / alphaBTotal;
vec4 rgbB = vec4(
texture(tex, coordAA).rgb*weightBA + texture(tex, coordAB).rgb*weightBB + texture(tex, coordBC).rgb*weightBC + texture(tex, coordBD).rgb*weightBD,
0.25 * (alphaAA + alphaAB + alphaBC + alphaBD)
);

float lumaB = dot(rgbB, luma4);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = rgbA;
else
color = rgbB;
return color;
}

void main() {
vec2 fragCoord = v_tex_coords * resolution;
if (enabled != 0) {
vec2 inverseVP = 1.0 / resolution.xy;
vec2 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
vec2 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
vec2 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
vec2 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
vec2 v_rgbM = vec2(fragCoord * inverseVP);
color = fxaa(tex, fragCoord, resolution, v_rgbNW, v_rgbNE, v_rgbSW,
v_rgbSE, v_rgbM);
} else {
color = texture(tex, v_tex_coords);
}
}
11 changes: 11 additions & 0 deletions src/shaders/fxaa.macos.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 150

in vec2 position;
in vec2 i_tex_coords;

out vec2 v_tex_coords;

void main() {
gl_Position = vec4(position, 0.0, 1.0);
v_tex_coords = i_tex_coords;
}
28 changes: 28 additions & 0 deletions src/shaders/model.macos.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 150

in vec3 v_normal;
in vec3 v_position;

out vec4 color;

uniform vec3 u_light;

uniform vec3 ambient_color;
uniform vec3 diffuse_color;
uniform vec3 specular_color;

void main() {
float diffuse = max(dot(normalize(v_normal), normalize(u_light)), 0.0);

vec3 camera_dir = normalize(-v_position);
vec3 half_direction = normalize(normalize(u_light) + camera_dir);
float specular = pow(max(dot(half_direction, normalize(v_normal)), 0.0), 16.0);

// Alternative specular method
// vec3 R = reflect( normalize(-u_light), normalize(v_normal) );
// float cosAlpha = clamp( dot(camera_dir,R), 0, 1 );
// float specular = pow( cosAlpha, 4.0 );

color = vec4(ambient_color + diffuse * diffuse_color + specular * specular_color, 1.0);
}

25 changes: 25 additions & 0 deletions src/shaders/model.macos.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#version 150

in vec3 position;
in vec3 normal;

out vec3 v_normal;
out vec3 v_position;

uniform mat4 perspective;
//uniform mat4 view;
//uniform mat4 model;
uniform mat4 modelview;

void main() {
// These never change, so they can be computed CPU side.
//mat4 modelview = view * model;

gl_Position = perspective * modelview * vec4(position, 1.0);

vec4 p = modelview * vec4(position, 1.0);
v_position = p.xyz / p.w;

v_normal = mat3(modelview) * normal;
}

0 comments on commit 3b42bde

Please sign in to comment.