Skip to content

Commit 9559e5c

Browse files
committed
Merge branch 'feature/animation' into dev
2 parents 8642289 + e3822d4 commit 9559e5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+53645
-7064
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#version 460 core
2+
layout(location = 0) in vec3 aPos;
3+
layout(location = 1) in vec3 aNormal;
4+
layout(location = 2) in vec2 aTexCoords;
5+
layout(location = 3) in vec3 aTangent;
6+
layout(location = 4) in vec2 aBitangent;
7+
layout(location = 5) in ivec4 boneIds;
8+
layout(location = 6) in vec4 weights;
9+
10+
const int MAX_BONES = 128;
11+
const int MAX_BONE_INFLUENCE = 4;
12+
13+
out vec3 FragPos;
14+
out vec3 viewPos;
15+
16+
out vec2 TexCoords;
17+
18+
out vec3 Normal;
19+
flat out int drawId;
20+
21+
layout(std140, binding = 1) uniform MeshData
22+
{
23+
mat4 view;
24+
mat4 projection;
25+
};
26+
27+
layout(std430, binding = 6) buffer AnimationMatrices
28+
{
29+
mat4 modelAnimationMatrices[];
30+
};
31+
32+
struct SSBOAnimation {
33+
mat4 animations[MAX_BONES];
34+
};
35+
36+
layout(std430, binding = 8) buffer BoneMatrices
37+
{
38+
SSBOAnimation boneMatrices[];
39+
};
40+
41+
void main()
42+
{
43+
vec4 totalPosition = vec4(0.0f);
44+
for (int i = 0; i < MAX_BONE_INFLUENCE; i++)
45+
{
46+
if (boneIds[i] == -1)
47+
continue;
48+
if (boneIds[i] >= MAX_BONES)
49+
{
50+
totalPosition = vec4(aPos, 1.0f);
51+
break;
52+
}
53+
vec4 localPosition = boneMatrices[gl_DrawID].animations[boneIds[i]] * vec4(aPos, 1.0f);
54+
totalPosition += localPosition * weights[i];
55+
}
56+
drawId = gl_DrawID;
57+
FragPos = vec3(modelAnimationMatrices[gl_DrawID] * vec4(aPos, 1.0));
58+
TexCoords = aTexCoords;
59+
mat3 normalMatrix = mat3(transpose(inverse(mat3(modelAnimationMatrices[gl_DrawID]))));
60+
Normal = normalMatrix * aNormal;
61+
62+
viewPos = vec3(inverse(view)[3]);
63+
64+
gl_Position = projection * view * modelAnimationMatrices[gl_DrawID] * totalPosition;
65+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#version 460 core
2+
layout(location = 0) in vec3 aPos;
3+
layout(location = 5) in ivec4 boneIds;
4+
layout(location = 6) in vec4 weights;
5+
6+
const int MAX_BONES = 128;
7+
const int MAX_BONE_INFLUENCE = 4;
8+
9+
layout(std430, binding = 6) buffer AnimationMatrices
10+
{
11+
mat4 modelAnimationMatrices[];
12+
};
13+
14+
struct SSBOAnimation {
15+
mat4 animations[MAX_BONES];
16+
};
17+
18+
layout(std430, binding = 8) buffer BoneMatrices
19+
{
20+
SSBOAnimation boneMatrices[];
21+
};
22+
23+
void main()
24+
{
25+
vec4 totalPosition = vec4(0.0f);
26+
for (int i = 0; i < MAX_BONE_INFLUENCE; i++)
27+
{
28+
if (boneIds[i] == -1)
29+
continue;
30+
if (boneIds[i] >= MAX_BONES)
31+
{
32+
totalPosition = vec4(aPos, 1.0f);
33+
break;
34+
}
35+
vec4 localPosition = boneMatrices[gl_DrawID].animations[boneIds[i]] * vec4(aPos, 1.0f);
36+
totalPosition += localPosition * weights[i];
37+
}
38+
39+
gl_Position = modelAnimationMatrices[gl_DrawID] * totalPosition;
40+
}

Engine/Shaders/DeferredPipeline/fragment.glsl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#version 460 core
2-
#extension GL_ARB_bindless_texture : enable
1+
32

43
in vec3 FragPos;
54
in vec2 TexCoords;
@@ -18,14 +17,23 @@ struct MaterialData {
1817
vec2 padding;
1918
};
2019

20+
MaterialData currentMaterial;
21+
22+
#if defined(ANIMATE)
23+
layout(std430, binding = 7) buffer MaterialAnimation
24+
{
25+
MaterialData materialDataAnimation[];
26+
};
27+
#else
2128
layout(std430, binding = 0) buffer Material
2229
{
2330
MaterialData materialData[];
2431
};
32+
#endif
2533

2634
vec3 getNormalFromMap()
2735
{
28-
vec3 tangentNormal = texture(materialData[drawId].normal, TexCoords).xyz * 2.0 - 1.0;
36+
vec3 tangentNormal = texture(currentMaterial.normal, TexCoords).xyz * 2.0 - 1.0;
2937

3038
vec3 Q1 = dFdx(FragPos);
3139
vec3 Q2 = dFdy(FragPos);
@@ -42,14 +50,19 @@ vec3 getNormalFromMap()
4250

4351
void main()
4452
{
53+
#if defined(ANIMATE)
54+
currentMaterial = materialDataAnimation[drawId];
55+
#else
56+
currentMaterial = materialData[drawId];
57+
#endif
4558
// store the fragment position vector in the first gbuffer texture
4659
gPosition.rgb = FragPos;
4760
// also store the per-fragment normals into the gbuffer
4861
gNormal.rgb = getNormalFromMap();
4962
// and the diffuse per-fragment color
50-
gAlbedoSpec.rgb = texture(materialData[drawId].diffuse, TexCoords).rgb;
63+
gAlbedoSpec.rgb = texture(currentMaterial.diffuse, TexCoords).rgb;
5164

52-
vec4 roughnessMetalnessTexture = texture(materialData[drawId].roughness_metalness, TexCoords);
65+
vec4 roughnessMetalnessTexture = texture(currentMaterial.roughness_metalness, TexCoords);
5366

5467
gAlbedoSpec.a= roughnessMetalnessTexture.b;
5568
gNormal.a = roughnessMetalnessTexture.g;

Engine/Shaders/DeferredPipeline/fragment_d.glsl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ layout(std430, binding = 5) restrict buffer clusterSSBO
3030
Cluster clusters[];
3131
};
3232

33-
uniform vec3 viewPos;
34-
3533
struct ShadowData {
3634
mat4 shadow;
3735
};
@@ -79,9 +77,14 @@ layout(std430, binding = 3) buffer Omni
7977
OmniData omniData[];
8078
};
8179

82-
layout(bindless_sampler) uniform samplerCube irradianceMap;
83-
layout(bindless_sampler) uniform samplerCube prefilterMap;
84-
layout(bindless_sampler) uniform sampler2D brdfLUT;
80+
layout(std140, binding = 3) uniform FragmentData
81+
{
82+
vec4 viewPos;
83+
samplerCube irradianceMap;
84+
samplerCube prefilterMap;
85+
sampler2D brdfLUT;
86+
vec2 paddingFragment;
87+
};
8588

8689
const float PI = 3.14159265359;
8790

@@ -149,7 +152,7 @@ float ShadowCalculation(vec3 fragPos,vec3 lightPos,int depthMapId)
149152
float shadow = 0.0;
150153
float bias = 0.15;
151154
int samples = 20;
152-
float viewDistance = length(viewPos - fragPos);
155+
float viewDistance = length(viewPos.xyz - fragPos);
153156
float diskRadius = (1.0 + (viewDistance / omniData[depthMapId].far_plane.r)) / 25.0;
154157
for(int i = 0; i < samples; ++i)
155158
{
@@ -213,7 +216,7 @@ void main()
213216
float roughness = texture(gNormal, TexCoords).a;
214217
float metallic = texture(gAlbedo, TexCoords).a;
215218

216-
vec3 V = normalize(viewPos - FragPos);
219+
vec3 V = normalize(viewPos.xyz - FragPos);
217220

218221
vec3 F0 = vec3(0.04);
219222
F0 = mix(F0, albedo, metallic);

Engine/Shaders/ForwardPipeline/fragment.glsl

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#version 460 core
2-
#extension GL_ARB_bindless_texture : enable
1+
32
out vec4 FragColor;
43

54
in vec3 FragPos;
65
in vec2 TexCoords;
7-
uniform vec3 viewPos;
86
in vec3 Normal;
97
flat in int drawId;
108
in vec4 shadowDirData[16];
@@ -47,6 +45,15 @@ layout(std140, binding = 1) uniform MeshData
4745
mat4 projection;
4846
};
4947

48+
layout(std140, binding = 3) uniform FragmentData
49+
{
50+
vec4 viewPos;
51+
samplerCube irradianceMap;
52+
samplerCube prefilterMap;
53+
sampler2D brdfLUT;
54+
vec2 paddingFragment;
55+
};
56+
5057
struct OmniData {
5158
vec4 position;
5259
vec4 diffuse;
@@ -64,14 +71,19 @@ struct MaterialData {
6471
vec2 padding;
6572
};
6673

67-
layout(bindless_sampler) uniform samplerCube irradianceMap;
68-
layout(bindless_sampler) uniform samplerCube prefilterMap;
69-
layout(bindless_sampler) uniform sampler2D brdfLUT;
74+
MaterialData currentMaterial;
7075

76+
#if defined(ANIMATE)
77+
layout(std430, binding = 7) buffer MaterialAnimation
78+
{
79+
MaterialData materialDataAnimation[];
80+
};
81+
#else
7182
layout(std430, binding = 0) buffer Material
7283
{
7384
MaterialData materialData[];
7485
};
86+
#endif
7587

7688
layout(std430, binding = 2) buffer Directional
7789
{
@@ -99,7 +111,7 @@ const float PI = 3.14159265359;
99111

100112
vec3 getNormalFromMap()
101113
{
102-
vec3 tangentNormal = texture(materialData[drawId].normal, TexCoords).xyz * 2.0 - 1.0;
114+
vec3 tangentNormal = texture(currentMaterial.normal, TexCoords).xyz * 2.0 - 1.0;
103115

104116
vec3 Q1 = dFdx(FragPos);
105117
vec3 Q2 = dFdy(FragPos);
@@ -178,7 +190,7 @@ float ShadowCalculation(vec3 fragPos,vec3 lightPos,int depthMapId)
178190
float shadow = 0.0;
179191
float bias = 0.15;
180192
int samples = 20;
181-
float viewDistance = length(viewPos - fragPos);
193+
float viewDistance = length(viewPos.xyz - fragPos);
182194
float diskRadius = (1.0 + (viewDistance / omniData[depthMapId].far_plane.r)) / 25.0;
183195
for(int i = 0; i < samples; ++i)
184196
{
@@ -233,14 +245,19 @@ float ShadowCalculationDirectional(vec3 fragPosWorldSpace,vec3 lightPos,vec3 N,u
233245

234246
void main()
235247
{
236-
vec3 albedo = texture(materialData[drawId].diffuse, TexCoords).rgb;
237-
vec4 roughnessMetalnessTexture = texture(materialData[drawId].roughness_metalness, TexCoords);
248+
#if defined(ANIMATE)
249+
currentMaterial = materialDataAnimation[drawId];
250+
#else
251+
currentMaterial = materialData[drawId];
252+
#endif
253+
vec3 albedo = texture(currentMaterial.diffuse, TexCoords).rgb;
254+
vec4 roughnessMetalnessTexture = texture(currentMaterial.roughness_metalness, TexCoords);
238255
float metallic = roughnessMetalnessTexture.b;
239256
float roughness = roughnessMetalnessTexture.g;
240257

241258
vec3 N = getNormalFromMap();
242259

243-
vec3 V = normalize(viewPos - FragPos);
260+
vec3 V = normalize(viewPos.xyz - FragPos);
244261

245262
vec3 F0 = vec3(0.04);
246263
F0 = mix(F0, albedo, metallic);

Engine/include/Handlers/AnimationHandler.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@
88
namespace Prisma {
99
class AnimationHandler {
1010
public:
11+
struct SSBOAnimation {
12+
glm::mat4 animations[MAX_BONES];
13+
};
1114
AnimationHandler(const AnimationHandler&) = delete;
1215
AnimationHandler& operator=(const AnimationHandler&) = delete;
1316

1417
static AnimationHandler& getInstance();
1518

1619
void updateAnimations();
1720

21+
std::vector<SSBOAnimation>& animations();
22+
1823
AnimationHandler();
1924

2025
private:
2126
std::shared_ptr<SSBO> m_ssboAnimation;
22-
struct SSBOAnimation {
23-
glm::mat4 animations[MAX_BONES];
24-
};
2527
void copyMatrices(SSBOAnimation& animation, std::vector<glm::mat4>& animationsData);
28+
std::vector<SSBOAnimation> m_animations;
29+
2630
static std::shared_ptr<AnimationHandler> instance;
2731
};
2832
}

Engine/include/Handlers/MeshHandler.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ namespace Prisma {
2121
float padding[2];
2222
};
2323

24+
struct alignas(16) UBOFragment {
25+
glm::vec4 viewPos;
26+
uint64_t irradiancePos;
27+
uint64_t prefilterPos;
28+
uint64_t lutPos;
29+
glm::vec2 padding;
30+
};
31+
2432
static const unsigned int VIEW_OFFSET = 0;
2533
static const unsigned int PROJECTION_OFFSET = sizeof(glm::mat4);
2634

@@ -30,6 +38,7 @@ namespace Prisma {
3038
MeshHandler& operator=(const MeshHandler&) = delete;
3139
void updateCamera();
3240
void updateCluster();
41+
void updateFragment();
3342

3443
static MeshHandler& getInstance();
3544

@@ -39,7 +48,9 @@ namespace Prisma {
3948
std::shared_ptr<UBOData> m_uboData;
4049
std::shared_ptr<Ubo> m_ubo;
4150
std::shared_ptr<Ubo> m_uboCluster;
51+
std::shared_ptr<Ubo> m_uboFragment;
4252
UBOCluster m_uboClusterData;
53+
UBOFragment m_fragment;
4354
Prisma::Settings m_settings;
4455
static std::shared_ptr<MeshHandler> instance;
4556
};

0 commit comments

Comments
 (0)