Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(gfx-backend): WGPU Renderer #9

Merged
merged 21 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions fast3d-glium-renderer/src/glium_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,53 @@ impl TextureData {
#[repr(C)]
#[derive(Copy, Clone)]
struct VertexUniforms {
screen_size: [f32; 2],
_padding: [f32; 2],
projection: [[f32; 4]; 4],
}

implement_uniform_block!(VertexUniforms, projection);
impl VertexUniforms {
pub fn new(screen_size: [f32; 2], projection: [[f32; 4]; 4]) -> Self {
Self {
screen_size,
_padding: [0.0; 2],
projection,
}
}
}

implement_uniform_block!(VertexUniforms, screen_size, projection);

#[repr(C)]
#[derive(Copy, Clone)]
struct VertexWithFogUniforms {
screen_size: [f32; 2],
_padding: [f32; 2],
projection: [[f32; 4]; 4],
fog_multiplier: f32,
fog_offset: f32,
}

impl VertexWithFogUniforms {
pub fn new(
screen_size: [f32; 2],
projection: [[f32; 4]; 4],
fog_multiplier: f32,
fog_offset: f32,
) -> Self {
Self {
screen_size,
_padding: [0.0; 2],
projection,
fog_multiplier,
fog_offset,
}
}
}

implement_uniform_block!(
VertexWithFogUniforms,
screen_size,
projection,
fog_multiplier,
fog_offset
Expand Down Expand Up @@ -159,6 +191,7 @@ pub struct GliumGraphicsDevice<'draw> {

frame_count: i32,
current_height: i32,
screen_size: [u32; 2],

draw_params: DrawParameters<'draw>,
}
Expand Down Expand Up @@ -212,14 +245,8 @@ fn clamp_to_glium(clamp: u32) -> SamplerWrapFunction {
SamplerWrapFunction::Repeat
}

impl<'draw> Default for GliumGraphicsDevice<'draw> {
fn default() -> Self {
Self::new()
}
}

impl<'draw> GliumGraphicsDevice<'draw> {
pub fn new() -> Self {
pub fn new(screen_size: [u32; 2]) -> Self {
Self {
shader_cache: rustc_hash::FxHashMap::default(),
current_shader: None,
Expand All @@ -230,6 +257,7 @@ impl<'draw> GliumGraphicsDevice<'draw> {

frame_count: 0,
current_height: 0,
screen_size,

draw_params: DrawParameters {
..Default::default()
Expand All @@ -247,7 +275,9 @@ impl<'draw> GliumGraphicsDevice<'draw> {
};
}

pub fn end_frame(&self) {}
pub fn resize(&mut self, screen_size: [u32; 2]) {
self.screen_size = screen_size;
}

pub fn set_cull_mode(&mut self, cull_mode: Option<Face>) {
self.draw_params.backface_culling = match cull_mode {
Expand Down Expand Up @@ -481,11 +511,12 @@ impl<'draw> GliumGraphicsDevice<'draw> {
// Setup uniforms

let vtx_uniform_buf = if program.get_define_bool("USE_FOG") {
let data = VertexWithFogUniforms {
projection: projection_matrix.to_cols_array_2d(),
fog_multiplier: fog.multiplier as f32,
fog_offset: fog.offset as f32,
};
let data = VertexWithFogUniforms::new(
[self.screen_size[0] as f32, self.screen_size[1] as f32],
projection_matrix.to_cols_array_2d(),
fog.multiplier as f32,
fog.offset as f32,
);

let buffer = Buffer::new(
display,
Expand All @@ -496,9 +527,10 @@ impl<'draw> GliumGraphicsDevice<'draw> {
.unwrap();
BufferAny::from(buffer)
} else {
let data = VertexUniforms {
projection: projection_matrix.to_cols_array_2d(),
};
let data = VertexUniforms::new(
[self.screen_size[0] as f32, self.screen_size[1] as f32],
projection_matrix.to_cols_array_2d(),
);

let buffer = Buffer::new(
display,
Expand Down
33 changes: 14 additions & 19 deletions fast3d-glium-renderer/src/opengl_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl<T> OpenGLProgram<T> {
#endif

layout(std140, set = 0, binding = 0) uniform Uniforms {
vec2 screenSize;
mat4 uProjection;
#ifdef USE_FOG
float uFogMultiplier;
Expand All @@ -257,6 +258,7 @@ impl<T> OpenGLProgram<T> {
gl_Position = vec4(aVtxPos.xyz, 1.0);
} else {
gl_Position = aVtxPos * uProjection;
gl_Position.x = gl_Position.x * (4.0 / 3.0) / (screenSize.x / screenSize.y);
}

#ifdef USE_FOG
Expand Down Expand Up @@ -300,9 +302,7 @@ impl<T> OpenGLProgram<T> {
} else {
match input {
CCMUX::CENTER__SCALE__ONE => "tOne.rgb", // matching against ONE
CCMUX::COMBINED_ALPHA__NOISE__K4 => {
"vec3(randomNoise, randomNoise, randomNoise)"
} // matching against NOISE
CCMUX::COMBINED_ALPHA__NOISE__K4 => "vec3(RAND_NOISE, RAND_NOISE, RAND_NOISE)", // matching against NOISE
_ => "tZero.rgb",
}
}
Expand Down Expand Up @@ -434,12 +434,12 @@ impl<T> OpenGLProgram<T> {
vec4 Texture2D_N64_Point(in sampler2D tex, in vec2 texCoord) {{
return texture(tex, texCoord);
}}

vec4 Texture2D_N64_Average(in sampler2D tex, in vec2 texCoord) {{
// Unimplemented.
return texture(tex, texCoord);
}}

// Implements N64-style "triangle bilienar filtering" with three taps.
// Based on ArthurCarvalho's implementation, modified for use here.
vec4 Texture2D_N64_Bilerp(in sampler2D tex, in vec2 texCoord) {{
Expand All @@ -451,27 +451,22 @@ impl<T> OpenGLProgram<T> {
vec4 s2 = TEX_OFFSET(vec2(offset.x, offset.y - sign(offset.y)));
return s0 + abs(offset.x) * (s1 - s0) + abs(offset.y) * (s2 - s0);
}}

#define Texture2D_N64 Texture2D_N64_{}
#define RAND_NOISE floor(random(vec3(floor(gl_FragCoord.xy * (240.0 / float(uFrameHeight))), float(uFrameCount))) + 0.5)

vec3 CombineColorCycle0(vec4 tCombColor, vec4 texVal0, vec4 texVal1) {{
#if defined(USE_ALPHA) && defined(ALPHA_COMPARE_DITHER)
float randomNoise = random(vec3(floor(gl_FragCoord.xy * (240.0 / float(uFrameHeight)), float(uFrameCount))) + 1.0) / 2.0;
#endif
return ({} - {}) * {} + {};
}}
}}

float CombineAlphaCycle0(vec4 tCombColor, vec4 texVal0, vec4 texVal1) {{
return ({} - {}) * {} + {};
}}

vec3 CombineColorCycle1(vec4 tCombColor, vec4 texVal0, vec4 texVal1) {{
#if defined(USE_ALPHA) && defined(ALPHA_COMPARE_DITHER)
float randomNoise = random(vec3(floor(gl_FragCoord.xy * (240.0 / float(uFrameHeight)), float(uFrameCount))) + 1.0) / 2.0;
#endif
return ({} - {}) * {} + {};
}}

float CombineAlphaCycle1(vec4 tCombColor, vec4 texVal0, vec4 texVal1) {{
return ({} - {}) * {} + {};
}}
Expand All @@ -493,7 +488,7 @@ impl<T> OpenGLProgram<T> {
CombineColorCycle0(tHalf, texVal0, texVal1),
CombineAlphaCycle0(tHalf, texVal0, texVal1)
);

#ifdef TWO_CYCLE
// Note that in the second cycle, Tex0 and Tex1 are swapped
texel = vec4(
Expand All @@ -505,9 +500,9 @@ impl<T> OpenGLProgram<T> {

#if defined(USE_ALPHA)
#if defined(ALPHA_COMPARE_DITHER)
if (texel.a < floor(random(vec3(floor(gl_FragCoord.xy * (240.0 / float(uFrameHeight))), float(uFrameCount))) + 0.5)) discard;
if (texel.a < RAND_NOISE) discard;
#endif

#if defined(ALPHA_COMPARE_THRESHOLD)
if (texel.a < uBlendColor.a) discard;
#endif
Expand Down
Loading