Skip to content

Commit

Permalink
Merge gfx-rs#71
Browse files Browse the repository at this point in the history
71: Shadow example r=grovesNL a=kvark

~~I believe all the nuts and bolts are in place, just figuring out some details of making the shadow match between the rendering and sampling (hence the WIP).~~

Most importantly, this PR includes crucial fixes and improvements of our resource tracking, plus a few fixes in the bind group and texture creation code.

Co-authored-by: Dzmitry Malyshau <[email protected]>
  • Loading branch information
bors[bot] and kvark committed Feb 23, 2019
2 parents 022087b + 616a3dd commit b2f58d0
Show file tree
Hide file tree
Showing 27 changed files with 1,250 additions and 237 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ else
endif


.PHONY: all check test doc clear lib-native lib-rust examples-native examples-rust gfx-cube
.PHONY: all check test doc clear lib-native lib-rust examples-native examples-rust gfx

all: examples-native examples-rust examples-gfx

Expand Down Expand Up @@ -65,5 +65,5 @@ examples-rust: lib-rust examples/Cargo.toml $(wildcard wgpu-native/**/*.rs)
examples-gfx: lib-rust gfx-examples/Cargo.toml $(wildcard gfx-examples/*.rs)
cargo build --manifest-path gfx-examples/Cargo.toml --features $(FEATURE_RUST)

gfx-cube:
cargo run --manifest-path gfx-examples/Cargo.toml --bin cube --features $(FEATURE_RUST)
gfx:
cargo run --manifest-path gfx-examples/Cargo.toml --bin $(name) --features $(FEATURE_RUST)
4 changes: 2 additions & 2 deletions examples/hello_compute_rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ fn main() {
});

let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_tobuffer(&staging_buffer, 0, &storage_buffer, 0, size);
encoder.copy_buffer_to_buffer(&staging_buffer, 0, &storage_buffer, 0, size);
{
let mut cpass = encoder.begin_compute_pass();
cpass.set_pipeline(&compute_pipeline);
cpass.set_bind_group(0, &bind_group);
cpass.dispatch(numbers.len() as u32, 1, 1);
}
encoder.copy_buffer_tobuffer(&storage_buffer, 0, &staging_buffer, 0, size);
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);

// TODO: read the results back out of the staging buffer

Expand Down
5 changes: 5 additions & 0 deletions gfx-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ publish = false
name = "cube"
path = "src/cube.rs"

[[bin]]
name = "shadow"
path = "src/shadow.rs"

[features]
default = []
metal-auto-capture = ["wgpu/metal-auto-capture"]
metal = ["wgpu/metal"]
dx11 = ["wgpu/dx11"]
dx12 = ["wgpu/dx12"]
Expand Down
2 changes: 2 additions & 0 deletions gfx-examples/data/cube.vert
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ 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);
}
4 changes: 4 additions & 0 deletions gfx-examples/data/shadow-bake.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#version 450

void main() {
}
18 changes: 18 additions & 0 deletions gfx-examples/data/shadow-bake.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 450

layout(location = 0) in ivec4 a_Pos;

layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
};

layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
};

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);
}
54 changes: 54 additions & 0 deletions gfx-examples/data/shadow-forward.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#version 450

const int MAX_LIGHTS = 10;

layout(location = 0) in vec3 v_Normal;
layout(location = 1) in vec4 v_Position;

layout(location = 0) out vec4 o_Target;

struct Light {
mat4 proj;
vec4 pos;
vec4 color;
};

layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
};
layout(set = 0, binding = 1) uniform Lights {
Light u_Lights[MAX_LIGHTS];
};
layout(set = 0, binding = 2) uniform texture2DArray t_Shadow;
layout(set = 0, binding = 3) uniform samplerShadow s_Shadow;

layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
};


void main() {
vec3 normal = normalize(v_Normal);
vec3 ambient = vec3(0.05, 0.05, 0.05);
// accumulate color
vec3 color = ambient;
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = u_Lights[i];
// 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.z = i;
// do the lookup, using HW PCF and comparison
float shadow = texture(sampler2DArrayShadow(t_Shadow, s_Shadow), light_local);
// compute Lambertian diffuse term
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
float diffuse = max(0.0, dot(normal, light_dir));
// add light contribution
color += shadow * diffuse * light.color.xyz;
}
// multiply the light by material color
o_Target = vec4(color, 1.0) * u_Color;
}
24 changes: 24 additions & 0 deletions gfx-examples/data/shadow-forward.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#version 450

layout(location = 0) in ivec4 a_Pos;
layout(location = 1) in ivec4 a_Normal;

layout(location = 0) out vec3 v_Normal;
layout(location = 1) out vec4 v_Position;

layout(set = 0, binding = 0) uniform Globals {
mat4 u_ViewProj;
uvec4 u_NumLights;
};
layout(set = 1, binding = 0) uniform Entity {
mat4 u_World;
vec4 u_Color;
};

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);
}
28 changes: 15 additions & 13 deletions gfx-examples/src/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn create_texels(size: usize) -> Vec<u8> {
.collect()
}

struct Cube {
struct Example {
vertex_buf: wgpu::Buffer,
index_buf: wgpu::Buffer,
index_count: usize,
Expand All @@ -92,20 +92,20 @@ struct Cube {
pipeline: wgpu::RenderPipeline,
}

impl Cube {
impl Example {
fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0);
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
}
}

impl framework::Example for Cube {
fn init(device: &mut wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Self {
impl framework::Example for Example {
fn init(sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) -> Self {
use std::mem;

let mut init_encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
Expand Down Expand Up @@ -293,7 +293,7 @@ impl framework::Example for Cube {
// Done
let init_command_buf = init_encoder.finish();
device.get_queue().submit(&[init_command_buf]);
Cube {
Example {
vertex_buf,
index_buf,
index_count: index_data.len(),
Expand All @@ -303,12 +303,14 @@ impl framework::Example for Cube {
}
}

fn update(&mut self, event: wgpu::winit::WindowEvent) {
if let wgpu::winit::WindowEvent::Resized(size) = event {
let mx_total = Self::generate_matrix(size.width as f32 / size.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
self.uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..]));
}
fn update(&mut self, _event: wgpu::winit::WindowEvent) {
//empty
}

fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, _device: &mut wgpu::Device) {
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
self.uniform_buf.set_sub_data(0, framework::cast_slice(&mx_ref[..]));
}

fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
Expand Down Expand Up @@ -337,5 +339,5 @@ impl framework::Example for Cube {
}

fn main() {
framework::run::<Cube>("cube");
framework::run::<Example>("cube");
}
14 changes: 10 additions & 4 deletions gfx-examples/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ pub fn load_glsl(name: &str, stage: ShaderStage) -> Vec<u8> {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("data")
.join(name);
let code = read_to_string(path).unwrap();
let code = match read_to_string(&path) {
Ok(code) => code,
Err(e) => panic!("Unable to read {:?}: {:?}", path, e),
};

let mut output = glsl_to_spirv::compile(&code, ty).unwrap();
let mut spv = Vec::new();
output.read_to_end(&mut spv).unwrap();
spv
}

pub trait Example {
fn init(device: &mut wgpu::Device, sc_desc: &wgpu::SwapChainDescriptor) -> Self;
fn init(sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) -> Self;
fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device);
fn update(&mut self, event: wgpu::winit::WindowEvent);
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device);
}
Expand Down Expand Up @@ -79,7 +84,7 @@ pub fn run<E: Example>(title: &str) {
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);

info!("Initializing the example...");
let mut example = E::init(&mut device, &sc_desc);
let mut example = E::init(&sc_desc, &mut device);

info!("Entering render loop...");
let mut running = true;
Expand All @@ -95,7 +100,7 @@ pub fn run<E: Example>(title: &str) {
sc_desc.width = physical.width as u32;
sc_desc.height = physical.height as u32;
swap_chain = device.create_swap_chain(&surface, &sc_desc);
example.update(WindowEvent::Resized(size));
example.resize(&sc_desc, &mut device);
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
Expand All @@ -119,5 +124,6 @@ pub fn run<E: Example>(title: &str) {

let frame = swap_chain.get_next_texture();
example.render(&frame, &mut device);
running &= !cfg!(feature = "metal-auto-capture");
}
}
Loading

0 comments on commit b2f58d0

Please sign in to comment.