Skip to content

Commit

Permalink
[rs] Merge gfx-rs#32
Browse files Browse the repository at this point in the history
32: Add conversion matrix from OpenGL to wgpu r=kvark a=dragly

The matrices in the examples are given in an OpenGL-like coordinate system,
while a Vulkan-like coordinate system is used by wgpu. This was previously
partially corrected in the shader and by flipping the up axis of the camera,
but left the x-axis mirrored in the final result.

This change adds a conversion matrix to framework.rs that can be used
to convert from OpenGL to wgpu. This also allows us to set the
winding-order to counter-clockwise, which matches the ordering in the data.

Co-authored-by: Svenn-Arne Dragly <dragly@cognite.com>
Co-authored-by: Dzmitry Malyshau <dmalyshau@mozilla.com>
  • Loading branch information
3 people committed Jul 8, 2019
2 parents a70eab2 + a4b500f commit 00f7dfa
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
7 changes: 4 additions & 3 deletions wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ impl Example {
let mx_view = cgmath::Matrix4::look_at(
cgmath::Point3::new(1.5f32, -5.0, 3.0),
cgmath::Point3::new(0f32, 0.0, 0.0),
-cgmath::Vector3::unit_z(),
cgmath::Vector3::unit_z(),
);
mx_projection * mx_view
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
mx_correction * mx_projection * mx_view
}
}

Expand Down Expand Up @@ -251,7 +252,7 @@ impl framework::Example for Example {
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
Expand Down
2 changes: 0 additions & 2 deletions wgpu/examples/cube/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ layout(set = 0, binding = 0) uniform Locals {
void main() {
v_TexCoord = a_TexCoord;
gl_Position = u_Transform * a_Pos;
// convert from -1,1 Z to 0,1
gl_Position.z = 0.5 * (gl_Position.z + gl_Position.w);
}
8 changes: 8 additions & 0 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use log::info;

#[cfg_attr(rustfmt, rustfmt_skip)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);

#[allow(dead_code)]
pub fn cast_slice<T>(data: &[T]) -> &[u8] {
use std::mem::size_of;
Expand Down
2 changes: 0 additions & 2 deletions wgpu/examples/mipmap/draw.vert
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ layout(set = 0, binding = 0) uniform Locals {
void main() {
v_TexCoord = a_Pos.xy / 20.0 + 0.5;
gl_Position = u_Transform * a_Pos;
// convert from -1,1 Z to 0,1
gl_Position.z = 0.5 * (gl_Position.z + gl_Position.w);
}
9 changes: 5 additions & 4 deletions wgpu/examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ impl Example {
let mx_view = cgmath::Matrix4::look_at(
cgmath::Point3::new(0f32, 0.0, 10.0),
cgmath::Point3::new(0f32, 50.0, 0.0),
-cgmath::Vector3::unit_z(),
cgmath::Vector3::unit_z(),
);
mx_projection * mx_view
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
mx_correction * mx_projection * mx_view
}

fn generate_mipmaps(
Expand Down Expand Up @@ -110,7 +111,7 @@ impl Example {
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
Expand Down Expand Up @@ -337,7 +338,7 @@ impl framework::Example for Example {
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
Expand Down
2 changes: 0 additions & 2 deletions wgpu/examples/shadow/bake.vert
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ layout(set = 1, binding = 0) uniform Entity {

void main() {
gl_Position = u_ViewProj * u_World * vec4(a_Pos);
// convert from -1,1 Z to 0,1
gl_Position.z = 0.5 * (gl_Position.z + gl_Position.w);
}
3 changes: 2 additions & 1 deletion wgpu/examples/shadow/forward.frag
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void main() {
// project into the light space
vec4 light_local = light.proj * v_Position;
// compute texture coordinates for shadow lookup
light_local.xyw = (light_local.xyz/light_local.w + 1.0) / 2.0;
light_local.xy = (light_local.xy/light_local.w + 1.0) / 2.0;
light_local.w = light_local.z / light_local.w;
light_local.z = i;
// do the lookup, using HW PCF and comparison
float shadow = texture(sampler2DArrayShadow(t_Shadow, s_Shadow), light_local);
Expand Down
2 changes: 0 additions & 2 deletions wgpu/examples/shadow/forward.vert
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ void main() {
v_Normal = mat3(u_World) * vec3(a_Normal.xyz);
v_Position = u_World * vec4(a_Pos);
gl_Position = u_ViewProj * v_Position;
// convert from -1,1 Z to 0,1
gl_Position.z = 0.5 * (gl_Position.z + gl_Position.w);
}
14 changes: 8 additions & 6 deletions wgpu/examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ impl Light {
fn to_raw(&self) -> LightRaw {
use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3};

let mx_view = Matrix4::look_at(self.pos, Point3::origin(), -Vector3::unit_z());
let mx_view = Matrix4::look_at(self.pos, Point3::origin(), Vector3::unit_z());
let projection = PerspectiveFov {
fovy: Deg(self.fov).into(),
aspect: 1.0,
near: self.depth.start,
far: self.depth.end,
};
let mx_view_proj = cgmath::Matrix4::from(projection.to_perspective()) * mx_view;
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
let mx_view_proj = mx_correction * cgmath::Matrix4::from(projection.to_perspective()) * mx_view;
LightRaw {
proj: *mx_view_proj.as_ref(),
pos: [self.pos.x, self.pos.y, self.pos.z, 1.0],
Expand Down Expand Up @@ -172,9 +173,10 @@ impl Example {
let mx_view = cgmath::Matrix4::look_at(
cgmath::Point3::new(3.0f32, -10.0, 6.0),
cgmath::Point3::new(0f32, 0.0, 0.0),
-cgmath::Vector3::unit_z(),
cgmath::Vector3::unit_z(),
);
mx_projection * mx_view
let mx_correction = framework::OPENGL_TO_WGPU_MATRIX;
mx_correction * mx_projection * mx_view
}
}

Expand Down Expand Up @@ -450,7 +452,7 @@ impl framework::Example for Example {
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 2, // corresponds to bilinear filtering
depth_bias_slope_scale: 2.0,
Expand Down Expand Up @@ -573,7 +575,7 @@ impl framework::Example for Example {
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Cw,
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
Expand Down

0 comments on commit 00f7dfa

Please sign in to comment.