Skip to content

Commit

Permalink
bevy_pbr: Pass all bindings as function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Feb 15, 2022
1 parent ffb3acf commit ca183a6
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 105 deletions.
11 changes: 7 additions & 4 deletions assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#import bevy_pbr::mesh_view_types
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_bindings
// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_bindings

[[group(1), binding(0)]]
var my_array_texture: texture_2d_array<f32>;
Expand All @@ -24,7 +23,11 @@ struct VertexOutput {
[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh_model_position_to_clip(vec4<f32>(vertex.position, 1.0));
out.clip_position = mesh_model_position_to_clip(
mesh.model,
view.view_proj,
vec4<f32>(vertex.position, 1.0)
);
out.position = out.clip_position;
return out;
}
Expand Down
12 changes: 7 additions & 5 deletions assets/shaders/shader_defs.wgsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#import bevy_pbr::mesh_view_types
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_functions
#import bevy_pbr::mesh_view_bindings

[[group(1), binding(0)]]
var<uniform> mesh: Mesh;

// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions

struct Vertex {
[[location(0)]] position: vec3<f32>;
[[location(1)]] normal: vec3<f32>;
Expand All @@ -21,7 +19,11 @@ struct VertexOutput {
[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh_model_position_to_clip(vec4<f32>(vertex.position, 1.0));
out.clip_position = mesh_model_position_to_clip(
mesh.model,
view.view_proj,
vec4<f32>(vertex.position, 1.0)
);
return out;
}

Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_pbr/src/render/depth.wgsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#import bevy_pbr::mesh_view_types
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_functions

[[group(0), binding(0)]]
var<uniform> view: View;

[[group(1), binding(0)]]
var<uniform> mesh: Mesh;

// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions

struct Vertex {
[[location(0)]] position: vec3<f32>;
};
Expand All @@ -21,6 +19,10 @@ struct VertexOutput {
[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.clip_position = mesh_model_position_to_clip(vec4<f32>(vertex.position, 1.0));
out.clip_position = mesh_model_position_to_clip(
mesh.model,
view.view_proj,
vec4<f32>(vertex.position, 1.0)
);
return out;
}
20 changes: 13 additions & 7 deletions crates/bevy_pbr/src/render/mesh.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#import bevy_pbr::mesh_view_types
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_bindings
// NOTE: Bindings must come before functions that use them!
#import bevy_pbr::mesh_functions
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_bindings

struct Vertex {
[[location(0)]] position: vec3<f32>;
Expand All @@ -26,15 +25,22 @@ struct VertexOutput {

[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
let world_position = mesh_model_position_to_world(vec4<f32>(vertex.position, 1.0));
let world_position = mesh_model_position_to_world(mesh.model, vec4<f32>(vertex.position, 1.0));

var out: VertexOutput;
out.uv = vertex.uv;
out.world_position = world_position;
out.clip_position = mesh_world_position_to_clip(world_position);
out.world_normal = mesh_model_normal_to_world(vertex.normal);
out.clip_position = mesh_world_position_to_clip(view.view_proj, world_position);
out.world_normal = mesh_model_normal_to_world(
mat3x3<f32>(
mesh.inverse_transpose_model[0].xyz,
mesh.inverse_transpose_model[1].xyz,
mesh.inverse_transpose_model[2].xyz
),
vertex.normal
);
#ifdef VERTEX_TANGENTS
out.world_tangent = mesh_model_tangent_to_world(vertex.tangent);
out.world_tangent = mesh_model_tangent_to_world(mesh.model, vertex.tangent);
#endif
return out;
}
Expand Down
46 changes: 29 additions & 17 deletions crates/bevy_pbr/src/render/mesh_functions.wgsl
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
fn mesh_model_position_to_world(vertex_position: vec4<f32>) -> vec4<f32> {
return mesh.model * vertex_position;
fn mesh_model_position_to_world(
model: mat4x4<f32>,
vertex_position: vec4<f32>
) -> vec4<f32> {
return model * vertex_position;
}

fn mesh_world_position_to_clip(world_position: vec4<f32>) -> vec4<f32> {
return view.view_proj * world_position;
fn mesh_world_position_to_clip(
view_proj: mat4x4<f32>,
world_position: vec4<f32>
) -> vec4<f32> {
return view_proj * world_position;
}

// NOTE: The intermediate world_position assignment is important
// for precision purposes when using the 'equals' depth comparison
// function.
fn mesh_model_position_to_clip(vertex_position: vec4<f32>) -> vec4<f32> {
let world_position = mesh_model_position_to_world(vertex_position);
return mesh_world_position_to_clip(world_position);
fn mesh_model_position_to_clip(
model: mat4x4<f32>,
view_proj: mat4x4<f32>,
vertex_position: vec4<f32>
) -> vec4<f32> {
let world_position = mesh_model_position_to_world(model, vertex_position);
return mesh_world_position_to_clip(view_proj, world_position);
}

fn mesh_model_normal_to_world(vertex_normal: vec3<f32>) -> vec3<f32> {
return mat3x3<f32>(
mesh.inverse_transpose_model[0].xyz,
mesh.inverse_transpose_model[1].xyz,
mesh.inverse_transpose_model[2].xyz
) * vertex_normal;
fn mesh_model_normal_to_world(
inverse_tranpose_model_3x3: mat3x3<f32>,
vertex_normal: vec3<f32>
) -> vec3<f32> {
return inverse_tranpose_model_3x3 * vertex_normal;
}

fn mesh_model_tangent_to_world(vertex_tangent: vec4<f32>) -> vec4<f32> {
fn mesh_model_tangent_to_world(
model: mat4x4<f32>,
vertex_tangent: vec4<f32>
) -> vec4<f32> {
return vec4<f32>(
mat3x3<f32>(
mesh.model[0].xyz,
mesh.model[1].xyz,
mesh.model[2].xyz
model[0].xyz,
model[1].xyz,
model[2].xyz
) * vertex_tangent.xyz,
vertex_tangent.w
);
Expand Down
38 changes: 32 additions & 6 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
// The above integration needs to be approximated.

#import bevy_pbr::mesh_view_types
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_bindings
#import bevy_pbr::pbr_types
#import bevy_pbr::pbr_bindings
#import bevy_pbr::pbr_functions
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::mesh_bindings
#import bevy_pbr::pbr_bindings

struct FragmentInput {
[[builtin(front_facing)]] is_front: bool;
Expand Down Expand Up @@ -97,13 +97,21 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
normal_map_texture,
normal_map_sampler,
in.uv,
#endif
#endif
in.uv,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
);

let V = calculate_view(in.world_position);
let V = calculate_view(
view.projection,
view.view_proj,
view.world_position,
in.world_position.xyz
);

pbr_material.material.reflectance = material.reflectance;

Expand All @@ -112,7 +120,25 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
pbr_in.world_position = in.world_position;
pbr_in.world_normal = in.world_normal;

output_color = pbr(pbr_in, pbr_material, N, V);
var pbr_lights: PbrLights;
pbr_lights.lights = lights;
pbr_lights.point_lights = point_lights;
pbr_lights.cluster_light_index_lists = cluster_light_index_lists;
pbr_lights.cluster_offsets_and_counts = cluster_offsets_and_counts;

output_color = pbr(
view,
pbr_in,
pbr_lights,
pbr_material,
point_shadow_textures,
point_shadow_textures_sampler,
directional_shadow_textures,
directional_shadow_textures_sampler,
N,
V,
(mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
);
}

return output_color;
Expand Down
Loading

0 comments on commit ca183a6

Please sign in to comment.