forked from gfx-rs/gfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
27 changed files
with
1,250 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#version 450 | ||
|
||
void main() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.