Skip to content

Commit 76f2348

Browse files
dominickmlotucHeavenVolkoff
committed
Added macos comptible shaders from unlimitedbacon#14
Co-authored-by: lotuc <[email protected]> Co-authored-by: Vitor Vasconcellos <[email protected]>
1 parent 6f7615a commit 76f2348

File tree

6 files changed

+231
-4
lines changed

6 files changed

+231
-4
lines changed

src/fxaa.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,36 @@ struct SpriteVertex {
2929

3030
implement_vertex!(SpriteVertex, position, i_tex_coords);
3131

32+
#[cfg(target_os = "macos")]
33+
fn get_shader() -> (&'static str, &'static str) {
34+
return (
35+
include_str!("shaders/fxaa.macos.vert"),
36+
include_str!("shaders/fxaa.macos.frag"),
37+
);
38+
}
39+
40+
#[cfg(target_os = "windows")]
41+
fn get_shader() -> (&'static str, &'static str) {
42+
return (
43+
include_str!("shaders/fxaa.vert"),
44+
include_str!("shaders/fxaa.frag"),
45+
);
46+
}
47+
48+
#[cfg(target_os = "linux")]
49+
fn get_shader() -> (&'static str, &'static str) {
50+
return (
51+
include_str!("shaders/fxaa.vert"),
52+
include_str!("shaders/fxaa.frag"),
53+
);
54+
}
55+
3256
impl FxaaSystem {
3357
pub fn new<F>(facade: &F) -> FxaaSystem
3458
where
3559
F: Facade + ?Sized,
3660
{
61+
let (vertex, fragment) = get_shader();
3762
FxaaSystem {
3863
context: facade.get_context().clone(),
3964

@@ -69,8 +94,8 @@ impl FxaaSystem {
6994

7095
program: program!(facade,
7196
100 => {
72-
vertex: include_str!("shaders/fxaa.vert"),
73-
fragment: include_str!("shaders/fxaa.frag"),
97+
vertex: vertex,
98+
fragment: fragment,
7499
}
75100
)
76101
.unwrap(),

src/lib.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, B
9090
Ok(display)
9191
}
9292

93+
#[cfg(target_os = "macos")]
94+
fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, Box<dyn Error>> {
95+
let size = PhysicalSize::new(config.width, config.height);
96+
let event_loop: EventLoop<()> = EventLoopBuilder::new().build();
97+
let cb = glutin::ContextBuilder::new();
98+
let context = cb.build_headless(&event_loop, size)?;
99+
100+
let context = unsafe { context.treat_as_current() };
101+
let display = glium::backend::glutin::headless::Headless::new(context)?;
102+
print_context_info(&display);
103+
Ok(display)
104+
}
105+
93106
#[cfg(target_os = "linux")]
94107
fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, Box<dyn Error>> {
95108
use glium::glutin::platform::unix::{EventLoopBuilderExtUnix, HeadlessContextExt};
@@ -142,6 +155,24 @@ fn create_headless_display(config: &Config) -> Result<glium::HeadlessRenderer, B
142155
Ok(display)
143156
}
144157

158+
#[cfg(target_os = "macos")]
159+
fn get_shader() -> (&'static str, &'static str) {
160+
return (include_str!("shaders/model.macos.vert"),
161+
include_str!("shaders/model.macos.frag"))
162+
}
163+
164+
#[cfg(target_os = "windows")]
165+
fn get_shader() -> (&'static str, &'static str) {
166+
return (include_str!("shaders/model.vert"),
167+
include_str!("shaders/model.frag"))
168+
}
169+
170+
#[cfg(target_os = "linux")]
171+
fn get_shader() -> (&'static str, &'static str) {
172+
return (include_str!("shaders/model.vert"),
173+
include_str!("shaders/model.frag"))
174+
}
175+
145176
fn render_pipeline<F>(
146177
display: &F,
147178
config: &Config,
@@ -168,8 +199,7 @@ where
168199
// Load and compile shaders
169200
// ------------------------
170201

171-
let vertex_shader_src = include_str!("shaders/model.vert");
172-
let pixel_shader_src = include_str!("shaders/model.frag");
202+
let (vertex_shader_src, pixel_shader_src) = get_shader();
173203

174204
// TODO: Cache program binary
175205
let program = glium::Program::from_source(display, vertex_shader_src, pixel_shader_src, None);

src/shaders/fxaa.macos.frag

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#version 150
2+
3+
//precision mediump float;
4+
5+
uniform vec2 resolution;
6+
uniform sampler2D tex;
7+
uniform int enabled;
8+
9+
in vec2 v_tex_coords;
10+
11+
out vec4 color;
12+
13+
#define FXAA_REDUCE_MIN (1.0/ 128.0)
14+
#define FXAA_REDUCE_MUL (1.0 / 8.0)
15+
#define FXAA_SPAN_MAX 8.0
16+
17+
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
18+
vec2 v_rgbNW, vec2 v_rgbNE,
19+
vec2 v_rgbSW, vec2 v_rgbSE,
20+
vec2 v_rgbM) {
21+
vec4 color;
22+
23+
// Sample adjascent pixels
24+
vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
25+
vec3 rgbNW = texture(tex, v_rgbNW).xyz;
26+
vec3 rgbNE = texture(tex, v_rgbNE).xyz;
27+
vec3 rgbSW = texture(tex, v_rgbSW).xyz;
28+
vec3 rgbSE = texture(tex, v_rgbSE).xyz;
29+
vec4 texColor = texture(tex, v_rgbM);
30+
vec3 rgbM = texColor.xyz;
31+
32+
// Calculate luminance
33+
vec3 luma = vec3(0.299, 0.587, 0.114);
34+
vec4 luma4 = vec4(0.299, 0.587, 0.114, 0.0);
35+
36+
float lumaNW = dot(rgbNW, luma);
37+
float lumaNE = dot(rgbNE, luma);
38+
float lumaSW = dot(rgbSW, luma);
39+
float lumaSE = dot(rgbSE, luma);
40+
float lumaM = dot(rgbM, luma);
41+
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
42+
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
43+
44+
// Determining blend direction
45+
vec2 dir;
46+
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
47+
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
48+
49+
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
50+
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
51+
52+
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
53+
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
54+
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
55+
dir * rcpDirMin)) * inverseVP;
56+
57+
// Blending
58+
// A lot of stuff here is my attempt to incorporate the alpha channel into the blending. I don't think I did it right. ¯\_(ツ)_/¯
59+
// A bit of the background color still bleeds through around the edges of the image. It's hardly noticable, though.
60+
vec2 coordAA = fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5);
61+
vec2 coordAB = fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5);
62+
float alphaAA = texture(tex, coordAA).a;
63+
float alphaAB = texture(tex, coordAB).a;
64+
float alphaATotal = alphaAA + alphaAB;
65+
float weightAA = alphaAA / alphaATotal;
66+
float weightAB = alphaAB / alphaATotal;
67+
vec4 rgbA = vec4( // Multiply by 2
68+
texture(tex, coordAA).rgb*weightAA + texture(tex, coordAB).rgb*weightAB,
69+
0.5 * (alphaAA + alphaAB)
70+
);
71+
72+
vec2 coordBC = fragCoord * inverseVP + dir * -0.5;
73+
vec2 coordBD = fragCoord * inverseVP + dir * 0.5;
74+
float alphaBC = texture(tex, coordBC).a;
75+
float alphaBD = texture(tex, coordBD).a;
76+
float alphaBTotal = alphaAA + alphaAB + alphaBC + alphaBD;
77+
float weightBA = alphaAA / alphaBTotal;
78+
float weightBB = alphaAB / alphaBTotal;
79+
float weightBC = alphaBC / alphaBTotal;
80+
float weightBD = alphaBD / alphaBTotal;
81+
vec4 rgbB = vec4(
82+
texture(tex, coordAA).rgb*weightBA + texture(tex, coordAB).rgb*weightBB + texture(tex, coordBC).rgb*weightBC + texture(tex, coordBD).rgb*weightBD,
83+
0.25 * (alphaAA + alphaAB + alphaBC + alphaBD)
84+
);
85+
86+
float lumaB = dot(rgbB, luma4);
87+
if ((lumaB < lumaMin) || (lumaB > lumaMax))
88+
color = rgbA;
89+
else
90+
color = rgbB;
91+
return color;
92+
}
93+
94+
void main() {
95+
vec2 fragCoord = v_tex_coords * resolution;
96+
if (enabled != 0) {
97+
vec2 inverseVP = 1.0 / resolution.xy;
98+
vec2 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
99+
vec2 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
100+
vec2 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
101+
vec2 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
102+
vec2 v_rgbM = vec2(fragCoord * inverseVP);
103+
color = fxaa(tex, fragCoord, resolution, v_rgbNW, v_rgbNE, v_rgbSW,
104+
v_rgbSE, v_rgbM);
105+
} else {
106+
color = texture(tex, v_tex_coords);
107+
}
108+
}

src/shaders/fxaa.macos.vert

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 150
2+
3+
in vec2 position;
4+
in vec2 i_tex_coords;
5+
6+
out vec2 v_tex_coords;
7+
8+
void main() {
9+
gl_Position = vec4(position, 0.0, 1.0);
10+
v_tex_coords = i_tex_coords;
11+
}

src/shaders/model.macos.frag

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#version 150
2+
3+
in vec3 v_normal;
4+
in vec3 v_position;
5+
6+
out vec4 color;
7+
8+
uniform vec3 u_light;
9+
10+
uniform vec3 ambient_color;
11+
uniform vec3 diffuse_color;
12+
uniform vec3 specular_color;
13+
14+
void main() {
15+
float diffuse = max(dot(normalize(v_normal), normalize(u_light)), 0.0);
16+
17+
vec3 camera_dir = normalize(-v_position);
18+
vec3 half_direction = normalize(normalize(u_light) + camera_dir);
19+
float specular = pow(max(dot(half_direction, normalize(v_normal)), 0.0), 16.0);
20+
21+
// Alternative specular method
22+
// vec3 R = reflect( normalize(-u_light), normalize(v_normal) );
23+
// float cosAlpha = clamp( dot(camera_dir,R), 0, 1 );
24+
// float specular = pow( cosAlpha, 4.0 );
25+
26+
color = vec4(ambient_color + diffuse * diffuse_color + specular * specular_color, 1.0);
27+
}
28+

src/shaders/model.macos.vert

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#version 150
2+
3+
in vec3 position;
4+
in vec3 normal;
5+
6+
out vec3 v_normal;
7+
out vec3 v_position;
8+
9+
uniform mat4 perspective;
10+
//uniform mat4 view;
11+
//uniform mat4 model;
12+
uniform mat4 modelview;
13+
14+
void main() {
15+
// These never change, so they can be computed CPU side.
16+
//mat4 modelview = view * model;
17+
18+
gl_Position = perspective * modelview * vec4(position, 1.0);
19+
20+
vec4 p = modelview * vec4(position, 1.0);
21+
v_position = p.xyz / p.w;
22+
23+
v_normal = mat3(modelview) * normal;
24+
}
25+

0 commit comments

Comments
 (0)