Skip to content

Commit

Permalink
ManyCubes example scene, Instance rate vertex attribute support (#…
Browse files Browse the repository at this point in the history
…148)

* Add ManyCubes scene.
* Support disabling depth prepass.
* Use instance rate vertex attribute for model matrix in mesh / depth shaders.
  • Loading branch information
DavidVonDerau authored Jun 1, 2021
1 parent 89fb401 commit 2afe8fb
Show file tree
Hide file tree
Showing 58 changed files with 741 additions and 1,105 deletions.
40 changes: 40 additions & 0 deletions demo/assets/materials/mesh.material
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
),
],
),
(
name: Some("mesh textured z"),
fixed_function_state: (
depth_testing: EnabledReverseZ,
cull_mode: Some(Back),
front_face: Some(CounterClockwise),
),
shaders: [
(
stage: Vertex,
shader_module: "../shaders/mesh_textured.vert.cookedshaderpackage",
entry_name: "main"
),
(
stage: Fragment,
shader_module: "../shaders/mesh_textured.frag.cookedshaderpackage",
entry_name: "main"
),
],
),
(
name: Some("mesh untextured"),
fixed_function_state: (
Expand All @@ -41,6 +61,26 @@
),
],
),
(
name: Some("mesh untextured z"),
fixed_function_state: (
depth_testing: EnabledReverseZ,
cull_mode: Some(Back),
front_face: Some(CounterClockwise),
),
shaders: [
(
stage: Vertex,
shader_module: "../shaders/mesh_untextured.vert.cookedshaderpackage",
entry_name: "main"
),
(
stage: Fragment,
shader_module: "../shaders/mesh_untextured.frag.cookedshaderpackage",
entry_name: "main"
),
],
),
(
name: Some("mesh wireframe"),
phase: Some("Wireframe"),
Expand Down
Binary file modified demo/assets/shaders/depth.vert.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_textured.frag.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_textured.vert.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_untextured.frag.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_untextured.vert.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_wireframe.frag.cookedshaderpackage
Binary file not shown.
Binary file modified demo/assets/shaders/mesh_wireframe.vert.cookedshaderpackage
Binary file not shown.
23 changes: 11 additions & 12 deletions demo/shaders/generated_msl/depth.vert.metal
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ struct PerViewData
float4x4 view_proj;
};

struct PerObjectData
{
float4x4 model;
};

struct spvDescriptorSetBuffer0
{
constant PerViewData* per_view_data [[id(0)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float4 gl_Position [[position]];
Expand All @@ -32,12 +22,21 @@ struct main0_out
struct main0_in
{
float3 in_pos [[attribute(0)]];
float4 in_model_matrix_0 [[attribute(1)]];
float4 in_model_matrix_1 [[attribute(2)]];
float4 in_model_matrix_2 [[attribute(3)]];
float4 in_model_matrix_3 [[attribute(4)]];
};

vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]])
{
main0_out out = {};
float4x4 model_view_proj = (*spvDescriptorSet0.per_view_data).view_proj * (*spvDescriptorSet2.per_object_data).model;
float4x4 in_model_matrix = {};
in_model_matrix[0] = in.in_model_matrix_0;
in_model_matrix[1] = in.in_model_matrix_1;
in_model_matrix[2] = in.in_model_matrix_2;
in_model_matrix[3] = in.in_model_matrix_3;
float4x4 model_view_proj = (*spvDescriptorSet0.per_view_data).view_proj * in_model_matrix;
out.gl_Position = model_view_proj * float4(in.in_pos, 1.0);
return out;
}
Expand Down
12 changes: 1 addition & 11 deletions demo/shaders/generated_msl/mesh_textured.frag.metal
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ struct MaterialDataUbo
MaterialData data;
};

struct PerObjectData
{
float4x4 model;
};

struct spvDescriptorSetBuffer0
{
constant PerViewData* per_view_data [[id(0)]];
Expand All @@ -142,11 +137,6 @@ struct spvDescriptorSetBuffer1
texture2d<float> emissive_texture [[id(5)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float4 out_color [[color(0)]];
Expand Down Expand Up @@ -519,7 +509,7 @@ float4 pbr_main(thread texture2d<float> normal_texture, thread sampler smp, cons
return out_color;
}

fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
constexpr sampler smp(filter::linear, mip_filter::linear, address::repeat, compare_func::never, max_anisotropy(16));
constexpr sampler smp_depth(filter::linear, mip_filter::linear, compare_func::greater, max_anisotropy(16));
Expand Down
31 changes: 15 additions & 16 deletions demo/shaders/generated_msl/mesh_textured.vert.metal
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ struct PerViewData
ShadowMapCubeData shadow_map_cube_data[16];
};

struct PerObjectData
{
float4x4 model;
};

struct MaterialData
{
float4 base_color_factor;
Expand Down Expand Up @@ -110,11 +105,6 @@ struct spvDescriptorSetBuffer1
texture2d<float> emissive_texture [[id(5)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float3 out_position_vs [[user(locn0)]];
Expand All @@ -135,31 +125,40 @@ struct main0_in
float3 in_normal [[attribute(1)]];
float4 in_tangent [[attribute(2)]];
float2 in_uv [[attribute(3)]];
float4 in_model_matrix_0 [[attribute(4)]];
float4 in_model_matrix_1 [[attribute(5)]];
float4 in_model_matrix_2 [[attribute(6)]];
float4 in_model_matrix_3 [[attribute(7)]];
};

static inline __attribute__((always_inline))
void pbr_main(constant PerViewData& per_view_data, constant PerObjectData& per_object_data, thread float4& gl_Position, thread float3& in_pos, thread float3& out_position_vs, thread float3& out_normal_vs, thread float3& in_normal, thread float3& out_tangent_vs, thread float4& in_tangent, thread float3& out_binormal_vs, thread float2& out_uv, thread float2& in_uv, thread float4& out_position_ws, thread float3x3& out_model_view)
void pbr_main(constant PerViewData& per_view_data, thread float4x4& in_model_matrix, thread float4& gl_Position, thread float3& in_pos, thread float3& out_position_vs, thread float3& out_normal_vs, thread float3& in_normal, thread float3& out_tangent_vs, thread float4& in_tangent, thread float3& out_binormal_vs, thread float2& out_uv, thread float2& in_uv, thread float4& out_position_ws, thread float3x3& out_model_view)
{
float4x4 model_view_proj = per_view_data.view_proj * per_object_data.model;
float4x4 model_view = per_view_data.view * per_object_data.model;
float4x4 model_view_proj = per_view_data.view_proj * in_model_matrix;
float4x4 model_view = per_view_data.view * in_model_matrix;
gl_Position = model_view_proj * float4(in_pos, 1.0);
out_position_vs = (model_view * float4(in_pos, 1.0)).xyz;
out_normal_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * in_normal;
out_tangent_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * in_tangent.xyz;
float3 binormal = cross(in_normal, in_tangent.xyz) * in_tangent.w;
out_binormal_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * binormal;
out_uv = in_uv;
out_position_ws = per_object_data.model * float4(in_pos, 1.0);
out_position_ws = in_model_matrix * float4(in_pos, 1.0);
out_model_view = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz);
}

vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
constexpr sampler smp(filter::linear, mip_filter::linear, address::repeat, compare_func::never, max_anisotropy(16));
constexpr sampler smp_depth(filter::linear, mip_filter::linear, compare_func::greater, max_anisotropy(16));
main0_out out = {};
float3x3 out_model_view = {};
pbr_main((*spvDescriptorSet0.per_view_data), (*spvDescriptorSet2.per_object_data), out.gl_Position, in.in_pos, out.out_position_vs, out.out_normal_vs, in.in_normal, out.out_tangent_vs, in.in_tangent, out.out_binormal_vs, out.out_uv, in.in_uv, out.out_position_ws, out_model_view);
float4x4 in_model_matrix = {};
in_model_matrix[0] = in.in_model_matrix_0;
in_model_matrix[1] = in.in_model_matrix_1;
in_model_matrix[2] = in.in_model_matrix_2;
in_model_matrix[3] = in.in_model_matrix_3;
pbr_main((*spvDescriptorSet0.per_view_data), in_model_matrix, out.gl_Position, in.in_pos, out.out_position_vs, out.out_normal_vs, in.in_normal, out.out_tangent_vs, in.in_tangent, out.out_binormal_vs, out.out_uv, in.in_uv, out.out_position_ws, out_model_view);
out.out_model_view_0 = out_model_view[0];
out.out_model_view_1 = out_model_view[1];
out.out_model_view_2 = out_model_view[2];
Expand Down
12 changes: 1 addition & 11 deletions demo/shaders/generated_msl/mesh_untextured.frag.metal
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ struct MaterialDataUbo
MaterialData data;
};

struct PerObjectData
{
float4x4 model;
};

struct spvDescriptorSetBuffer0
{
constant PerViewData* per_view_data [[id(0)]];
Expand All @@ -137,11 +132,6 @@ struct spvDescriptorSetBuffer1
constant MaterialDataUbo* per_material_data [[id(0)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float4 out_color [[color(0)]];
Expand Down Expand Up @@ -476,7 +466,7 @@ float4 pbr_main(constant PerViewData& per_view_data, thread float4& in_position_
return out_color;
}

fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
fragment main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
constexpr sampler smp_depth(filter::linear, mip_filter::linear, compare_func::greater, max_anisotropy(16));
constexpr sampler smp(filter::linear, mip_filter::linear, address::repeat, compare_func::never, max_anisotropy(16));
Expand Down
31 changes: 15 additions & 16 deletions demo/shaders/generated_msl/mesh_untextured.vert.metal
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ struct PerViewData
ShadowMapCubeData shadow_map_cube_data[16];
};

struct PerObjectData
{
float4x4 model;
};

struct MaterialData
{
float4 base_color_factor;
Expand Down Expand Up @@ -105,11 +100,6 @@ struct spvDescriptorSetBuffer1
constant MaterialDataUbo* per_material_data [[id(0)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float3 out_position_vs [[user(locn0)]];
Expand All @@ -130,31 +120,40 @@ struct main0_in
float3 in_normal [[attribute(1)]];
float4 in_tangent [[attribute(2)]];
float2 in_uv [[attribute(3)]];
float4 in_model_matrix_0 [[attribute(4)]];
float4 in_model_matrix_1 [[attribute(5)]];
float4 in_model_matrix_2 [[attribute(6)]];
float4 in_model_matrix_3 [[attribute(7)]];
};

static inline __attribute__((always_inline))
void pbr_main(constant PerViewData& per_view_data, constant PerObjectData& per_object_data, thread float4& gl_Position, thread float3& in_pos, thread float3& out_position_vs, thread float3& out_normal_vs, thread float3& in_normal, thread float3& out_tangent_vs, thread float4& in_tangent, thread float3& out_binormal_vs, thread float2& out_uv, thread float2& in_uv, thread float4& out_position_ws, thread float3x3& out_model_view)
void pbr_main(constant PerViewData& per_view_data, thread float4x4& in_model_matrix, thread float4& gl_Position, thread float3& in_pos, thread float3& out_position_vs, thread float3& out_normal_vs, thread float3& in_normal, thread float3& out_tangent_vs, thread float4& in_tangent, thread float3& out_binormal_vs, thread float2& out_uv, thread float2& in_uv, thread float4& out_position_ws, thread float3x3& out_model_view)
{
float4x4 model_view_proj = per_view_data.view_proj * per_object_data.model;
float4x4 model_view = per_view_data.view * per_object_data.model;
float4x4 model_view_proj = per_view_data.view_proj * in_model_matrix;
float4x4 model_view = per_view_data.view * in_model_matrix;
gl_Position = model_view_proj * float4(in_pos, 1.0);
out_position_vs = (model_view * float4(in_pos, 1.0)).xyz;
out_normal_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * in_normal;
out_tangent_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * in_tangent.xyz;
float3 binormal = cross(in_normal, in_tangent.xyz) * in_tangent.w;
out_binormal_vs = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz) * binormal;
out_uv = in_uv;
out_position_ws = per_object_data.model * float4(in_pos, 1.0);
out_position_ws = in_model_matrix * float4(in_pos, 1.0);
out_model_view = float3x3(model_view[0].xyz, model_view[1].xyz, model_view[2].xyz);
}

vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
constexpr sampler smp(filter::linear, mip_filter::linear, address::repeat, compare_func::never, max_anisotropy(16));
constexpr sampler smp_depth(filter::linear, mip_filter::linear, compare_func::greater, max_anisotropy(16));
main0_out out = {};
float3x3 out_model_view = {};
pbr_main((*spvDescriptorSet0.per_view_data), (*spvDescriptorSet2.per_object_data), out.gl_Position, in.in_pos, out.out_position_vs, out.out_normal_vs, in.in_normal, out.out_tangent_vs, in.in_tangent, out.out_binormal_vs, out.out_uv, in.in_uv, out.out_position_ws, out_model_view);
float4x4 in_model_matrix = {};
in_model_matrix[0] = in.in_model_matrix_0;
in_model_matrix[1] = in.in_model_matrix_1;
in_model_matrix[2] = in.in_model_matrix_2;
in_model_matrix[3] = in.in_model_matrix_3;
pbr_main((*spvDescriptorSet0.per_view_data), in_model_matrix, out.gl_Position, in.in_pos, out.out_position_vs, out.out_normal_vs, in.in_normal, out.out_tangent_vs, in.in_tangent, out.out_binormal_vs, out.out_uv, in.in_uv, out.out_position_ws, out_model_view);
out.out_model_view_0 = out_model_view[0];
out.out_model_view_1 = out_model_view[1];
out.out_model_view_2 = out_model_view[2];
Expand Down
12 changes: 1 addition & 11 deletions demo/shaders/generated_msl/mesh_wireframe.frag.metal
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,17 @@ struct PerViewData
float4x4 view_proj;
};

struct PerObjectData
{
float4x4 model;
};

struct spvDescriptorSetBuffer0
{
constant PerViewData* per_view_data [[id(0)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float4 out_color [[color(0)]];
};

fragment main0_out main0(constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
fragment main0_out main0(constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]])
{
main0_out out = {};
out.out_color = float4(1.0);
Expand Down
23 changes: 11 additions & 12 deletions demo/shaders/generated_msl/mesh_wireframe.vert.metal
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ struct PerViewData
float4x4 view_proj;
};

struct PerObjectData
{
float4x4 model;
};

struct spvDescriptorSetBuffer0
{
constant PerViewData* per_view_data [[id(0)]];
};

struct spvDescriptorSetBuffer2
{
constant PerObjectData* per_object_data [[id(0)]];
};

struct main0_out
{
float4 gl_Position [[position]];
Expand All @@ -32,12 +22,21 @@ struct main0_out
struct main0_in
{
float3 in_pos [[attribute(0)]];
float4 in_model_matrix_0 [[attribute(1)]];
float4 in_model_matrix_1 [[attribute(2)]];
float4 in_model_matrix_2 [[attribute(3)]];
float4 in_model_matrix_3 [[attribute(4)]];
};

vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]], constant spvDescriptorSetBuffer2& spvDescriptorSet2 [[buffer(2)]])
vertex main0_out main0(main0_in in [[stage_in]], constant spvDescriptorSetBuffer0& spvDescriptorSet0 [[buffer(0)]])
{
main0_out out = {};
float4x4 model_view_proj = (*spvDescriptorSet0.per_view_data).view_proj * (*spvDescriptorSet2.per_object_data).model;
float4x4 in_model_matrix = {};
in_model_matrix[0] = in.in_model_matrix_0;
in_model_matrix[1] = in.in_model_matrix_1;
in_model_matrix[2] = in.in_model_matrix_2;
in_model_matrix[3] = in.in_model_matrix_3;
float4x4 model_view_proj = (*spvDescriptorSet0.per_view_data).view_proj * in_model_matrix;
out.gl_Position = model_view_proj * float4(in.in_pos, 1.0);
return out;
}
Expand Down
8 changes: 1 addition & 7 deletions demo/shaders/glsl/depth.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@
layout (set = 0, binding = 0) uniform PerViewData {
mat4 view;
mat4 view_proj;
} per_view_data;

// @[export]
// @[internal_buffer]
layout(set = 2, binding = 0) uniform PerObjectData {
mat4 model;
} per_object_data;
} per_view_data;
Loading

0 comments on commit 2afe8fb

Please sign in to comment.