Shadows now working on NVIDIA cards too

This commit is contained in:
Teejoon
2016-03-07 14:00:08 +01:00
parent 315eac1af9
commit b38ebbe676
13 changed files with 227 additions and 17 deletions
@@ -15,8 +15,8 @@
struct ExplosionEffectJob : ModelJob
{
ExplosionEffectJob(ComponentWrapper explosionEffectComponent, ::Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialProperties matGroup, ComponentWrapper modelComponent, ::World* world, glm::vec4 fillColor, float fillPercentage, bool isShielded)
: ModelJob(model, camera, matrix, matGroup, modelComponent, world, fillColor, fillPercentage, isShielded)
ExplosionEffectJob(ComponentWrapper explosionEffectComponent, ::Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialProperties matGroup, ComponentWrapper modelComponent, ::World* world, glm::vec4 fillColor, float fillPercentage, bool isShielded, bool shadow)
: ModelJob(model, camera, matrix, matGroup, modelComponent, world, fillColor, fillPercentage, isShielded, shadow)
{
ExplosionOrigin = (glm::vec3)explosionEffectComponent["ExplosionOrigin"];
TimeSinceDeath = (double)explosionEffectComponent["TimeSinceDeath"];
+10
View File
@@ -43,6 +43,16 @@ public:
~RenderBuffer();
};
class Texture2DArray : public ResourceType<GL_TEXTURE_2D_ARRAY>
{
public:
Texture2DArray(GLuint* resourceHandle, GLenum attachment)
: ResourceType(resourceHandle, attachment)
{ };
~Texture2DArray();
};
class FrameBuffer
{
public:
+4 -1
View File
@@ -18,7 +18,7 @@
struct ModelJob : RenderJob
{
ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialProperties matProp, ComponentWrapper modelComponent, World* world, glm::vec4 fillColor, float fillPercentage, bool isShielded)
ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialProperties matProp, ComponentWrapper modelComponent, World* world, glm::vec4 fillColor, float fillPercentage, bool isShielded, bool shadow)
: RenderJob()
{
Model = model;
@@ -114,6 +114,7 @@ struct ModelJob : RenderJob
glm::vec3 worldpos = glm::vec3(camera->ViewMatrix() * glm::vec4(abspos, 1));
Depth = worldpos.z;
World = world;
Shadow = shadow;
FillColor = fillColor;
FillPercentage = fillPercentage;
@@ -182,6 +183,8 @@ struct ModelJob : RenderJob
glm::vec4 FillColor = glm::vec4(0);
float FillPercentage = 0.0;
bool IsShielded;
bool Shadow;
void CalculateHash() override
{
Hash = ShaderID << 20 + ModelID << 10 + TextureID;
+1
View File
@@ -8,5 +8,6 @@
<NormalMap>true</NormalMap>
<SpecularMap>true</SpecularMap>
<GlowMap>true</GlowMap>
<Shadow>true</Shadow>
<GlowIntensity>3.0</GlowIntensity>
</Model>
+3
View File
@@ -36,6 +36,9 @@
<xs:element name="GlowIntensity" type="t:double" minOccurs="0">
<xs:annotation><xs:documentation>Intensity of the glow map</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Shadow" type="t:bool" minOccurs="0">
<xs:annotation><xs:documentation>Whether the object cast/recieve shadows or not</xs:documentation></xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
+4 -4
View File
@@ -149,9 +149,9 @@ void main()
Output.TextureCoordinate = Input[i].TextureCoordinate;
Output.Tangent = Input[i].Tangent;
Output.BiTangent = Input[i].BiTangent;
for (int i = 0; i < MAX_SPLITS; i++)
for (int j = 0; j < MAX_SPLITS; j++)
{
Output.PositionLightSpace[i] = Input[i].PositionLightSpace[i];
Output.PositionLightSpace[j] = Input[i].PositionLightSpace[j];
}
// convert to model space for the gravity to always be in -y
@@ -194,9 +194,9 @@ void main()
Output.TextureCoordinate = Input[i].TextureCoordinate;
Output.Tangent = Input[i].Tangent;
Output.BiTangent = Input[i].BiTangent;
for (int i = 0; i < MAX_SPLITS; i++)
for (int j = 0; j < MAX_SPLITS; j++)
{
Output.PositionLightSpace[i] = Input[i].PositionLightSpace[i];
Output.PositionLightSpace[j] = Input[i].PositionLightSpace[j];
}
// no change in position, pass through vertex
+1 -1
View File
@@ -27,7 +27,7 @@ layout (binding = 2) uniform sampler2D NormalMapTexture;
layout (binding = 3) uniform sampler2D SpecularMapTexture;
layout (binding = 4) uniform sampler2D GlowMapTexture;
layout (binding = 5) uniform samplerCube CubeMap;
layout (binding = 13) uniform sampler2DArrayShadow DepthMap;
layout (binding = 30) uniform sampler2DArrayShadow DepthMap;
#define TILE_SIZE 16
@@ -13,6 +13,10 @@ uniform vec4 FillColor;
uniform vec4 Color;
uniform vec4 AmbientColor;
uniform int SSAOQuality;
uniform float FarDistance[MAX_SPLITS];
layout (binding = 0) uniform sampler2D AOTexture;
layout (binding = 30) uniform sampler2DArrayShadow DepthMap;
//Get bineded at the same time as the textures
uniform vec2 DiffuseUVRepeat1;
@@ -27,7 +31,6 @@ uniform vec2 SpecularUVRepeat3;
uniform vec2 GlowUVRepeat1;
uniform vec2 GlowUVRepeat2;
uniform vec2 GlowUVRepeat3;
layout (binding = 0) uniform sampler2D AOTexture;
layout (binding = 1) uniform sampler2D SplatMapTexture;
layout (binding = 2) uniform sampler2D DiffuseTexture1;
layout (binding = 3) uniform sampler2D DiffuseTexture2;
@@ -95,6 +98,25 @@ struct LightResult {
vec4 Specular;
};
vec2 poissonDisk[16] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( -0.91588581, 0.45771432 ),
vec2( -0.81544232, -0.87912464 ),
vec2( -0.38277543, 0.27676845 ),
vec2( 0.97484398, 0.75648379 ),
vec2( 0.44323325, -0.97511554 ),
vec2( 0.53742981, -0.47373420 ),
vec2( -0.26496911, -0.41893023 ),
vec2( 0.79197514, 0.19090188 ),
vec2( -0.24188840, 0.99706507 ),
vec2( -0.81409955, 0.91437590 ),
vec2( 0.19984126, 0.78641367 ),
vec2( 0.14383161, -0.14100790 )
);
float CalcAttenuation(float radius, float dist, float falloff) {
return 1.0 - smoothstep(radius * 0.3, radius, dist);
}
@@ -178,6 +200,154 @@ vec4 CalcBlendedNormal(vec4 blendValue, sampler2D R, sampler2D G, sampler2D B,
return vec4(TBN * normalize(Normal_result), 0.0);
}
// Returns a "random" value.
float Random(vec3 seed, int i)
{
vec4 seed4 = vec4(seed, i);
float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673));
return fract(sin(dot_product) * 43758.5453);
}
int getShadowIndex(float far_distance[1])
{
return 0;
}
int getShadowIndex(float far_distance[2])
{
float depth = gl_FragCoord.z / gl_FragCoord.w;
int index = 1;
if ( depth < far_distance[0] )
{
index = 0;
}
return index;
}
int getShadowIndex(float far_distance[3])
{
float depth = gl_FragCoord.z / gl_FragCoord.w;
int index = 2;
if ( depth < far_distance[0] )
{
index = 0;
}
else if ( depth < far_distance[1] && depth > far_distance[0] )
{
index = 1;
}
return index;
}
int getShadowIndex(float far_distance[4])
{
float depth = gl_FragCoord.z / gl_FragCoord.w;
int index = 3;
if ( depth < far_distance[0] )
{
index = 0;
}
else if ( depth < far_distance[1] && depth > far_distance[0] )
{
index = 1;
}
else if ( depth < far_distance[2] && depth > far_distance[1] )
{
index = 2;
}
return index;
}
// Standard hardware-calculated PCF method
float PCFShadow(sampler2DArrayShadow depth_texture_array, vec3 projection_coords, int layer_index)
{
return texture(depth_texture_array, vec4(projection_coords.xy, layer_index, projection_coords.z));
}
// PCF + Poisson model method
float PoissonShadow(sampler2DArrayShadow depth_texture_array, vec3 projection_coords, int layer_index, int taps, float spread)
{
int loop;
float multiplier = 1.0 / float(taps);
float shadowMapDepth;
for (int i = 0; i < taps; i++)
{
loop = i;
vec3 newProjCoords = projection_coords + vec3(poissonDisk[loop], 0.0) / (spread * (1.0 + layer_index));
shadowMapDepth += multiplier * texture(depth_texture_array, vec4(newProjCoords.xy, layer_index, newProjCoords.z));
}
return shadowMapDepth;
}
// PCF + Poisson + RandomSample model method
float PoissonDotShadow(sampler2DArrayShadow depth_texture_array, vec3 projection_coords, int layer_index, int taps, float spread)
{
int loop;
float multiplier = 1.0 / float(taps);
float shadowMapDepth;
for (int i = 0; i < taps; i++)
{
loop = int(16.0 * Random(gl_FragCoord.xyy, i)) % 16;
vec3 newProjCoords = projection_coords + vec3(poissonDisk[loop], 0.0) / (spread * (1.0 + layer_index));
shadowMapDepth += multiplier * texture(depth_texture_array, vec4(newProjCoords.xy, layer_index, newProjCoords.z));
}
return shadowMapDepth;
}
// Hardware PCF + Additional software PCF method
float SoftwarePCF(sampler2DArrayShadow depth_texture_array, vec3 projection_coords, int layer_index, float bias)
{
float shadow = 0.0;
vec3 texelSize = 1.0 / textureSize(depth_texture_array, 0);
for(int x = -1; x <= 1; x++)
{
for(int y = -1; y <= 1; y++)
{
shadow += texture(depth_texture_array, vec4(projection_coords.xy + vec2(x, y) * texelSize.xy / (1.0 + layer_index), layer_index, projection_coords.z));
}
}
return shadow / 9.0;
}
float CalcShadowValue(vec4 light_space_pos, vec3 normal, vec3 light_dir, sampler2DArrayShadow depth_texture_array, int layer_index)
{
float shadowMapDepth;
float bias = 0.005;
// Various bias methods.
//bias = max(0.05 * (1.0 - dot(normal, light_dir)), bias);
//bias = bias * tan(acos(clamp(dot(normal, -light_dir), 0.0, 1.0)));
bias = bias + bias * tan(acos(clamp(dot(normal, -light_dir), 0.0, 1.0)));
// Calculate coordinates in projection space.
vec3 projCoords = vec3(light_space_pos.xy, light_space_pos.z + bias) / light_space_pos.w;
projCoords = projCoords * 0.5 + 0.5;
//projCoords = (floor(projCoords * 255.0)) / 255.0;
// Various methods for shadow calculation in fastest to slowest order.
//shadowMapDepth = PCFShadow(depth_texture_array, projCoords, layer_index);
//shadowMapDepth = PoissonShadow(depth_texture_array, projCoords, layer_index, 4, 25.0 * FarDistance[MAX_SPLITS - 1]);
//shadowMapDepth = PoissonDotShadow(depth_texture_array, projCoords, layer_index, 4, 25.0 * FarDistance[MAX_SPLITS - 1]);
shadowMapDepth = SoftwarePCF(depth_texture_array, projCoords, layer_index, bias);
return shadowMapDepth;
}
void main()
{
float ao = texelFetch(AOTexture, ivec2(gl_FragCoord.xy) >> int(SSAOQuality), 0).r;
@@ -210,6 +380,8 @@ void main()
int start = int(LightGrids.Data[currentTile].Start);
int amount = int(LightGrids.Data[currentTile].Amount);
float shadowFactor = 0.0;
for(int i = start; i < start + amount; i++) {
int l = int(LightIndex[i]);
@@ -220,12 +392,17 @@ void main()
if(light.Type == 1) { // point
light_result = CalcPointLightSource(V * light.Position, light.Radius, light.Color, light.Intensity, viewVec, position, normal, light.Falloff);
} else if (light.Type == 2) { //Directional
int DepthMapIndex = getShadowIndex(FarDistance);
light_result = CalcDirectionalLightSource(V * light.Direction, light.Color, light.Intensity, viewVec, normal);
shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap, DepthMapIndex);
}
totalLighting.Diffuse += vec4(light_result.Diffuse.rgb * ao, light_result.Diffuse.a);
totalLighting.Specular += vec4(light_result.Specular.rgb * ao, light_result.Specular.a);
}
totalLighting.Diffuse *= vec4(min(vec3(shadowFactor) + AmbientColor.xyz, vec3(1.0)), 1.0);
totalLighting.Specular *= vec4(min(vec3(shadowFactor) + AmbientColor.xyz, vec3(1.0)), 1.0);
vec4 color_result = mix((Color * diffuseTexel * DiffuseColor), Input.ExplosionColor, Input.ExplosionPercentageElapsed);
color_result = color_result * (totalLighting.Diffuse + (totalLighting.Specular * specularTexel));
//vec4 color_result = (DiffuseColor + Input.ExplosionColor) * (totalLighting.Diffuse + (totalLighting.Specular * specularTexel)) * diffuseTexel * Color;
+1 -1
View File
@@ -54,7 +54,7 @@ void EditorRenderSystem::Update(double dt)
EntityWrapper entity(m_World, cModel.EntityID);
glm::mat4 modelMatrix = Transform::ModelMatrix(entity.ID, entity.World);
for (auto matGroup : model->MaterialGroups()) {
std::shared_ptr<ModelJob> modelJob = std::make_shared<ModelJob>(model, scene.Camera, modelMatrix, matGroup, cModel, entity.World, glm::vec4(0), 0.f, false);
std::shared_ptr<ModelJob> modelJob = std::make_shared<ModelJob>(model, scene.Camera, modelMatrix, matGroup, cModel, entity.World, glm::vec4(0), 0.f, false, false);
if (cModel["Transparent"]) {
scene.Jobs.TransparentObjects.push_back(modelJob);
} else {
+2 -3
View File
@@ -344,11 +344,10 @@ void DrawFinalPass::DrawModelRenderQueues(std::list<std::shared_ptr<RenderJob>>&
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_SSAOPass->SSAOTexture());
glActiveTexture(GL_TEXTURE13);
glActiveTexture(GL_TEXTURE30);
if (m_ShadowPass->DepthMap() != NULL) {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_ShadowPass->DepthMap());
}
else {
} else {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_WhiteTexture->m_Texture);
}
+7
View File
@@ -24,6 +24,13 @@ RenderBuffer::~RenderBuffer()
}
}
Texture2DArray::~Texture2DArray()
{
if (m_ResourceHandle != 0) {
glDeleteTextures(1, m_ResourceHandle);
}
}
FrameBuffer::~FrameBuffer()
{
+4 -2
View File
@@ -258,7 +258,8 @@ void RenderSystem::fillModels(RenderScene::Queues &Jobs)
m_World,
fillColor,
fillPercentage,
isShielded
isShielded,
false
));
if (m_World->HasComponent(cModel.EntityID, "Shield")){
explosionEffectJob->CalculateHash();
@@ -296,7 +297,8 @@ void RenderSystem::fillModels(RenderScene::Queues &Jobs)
m_World,
fillColor,
fillPercentage,
isShielded
isShielded,
(bool)cModel["Shadow"]
));
if (m_World->HasComponent(cModel.EntityID, "Shield")) {
modelJob->CalculateHash();
+10 -2
View File
@@ -131,7 +131,7 @@ void ShadowPass::InitializeFrameBuffers()
glBindTexture(GL_TEXTURE_2D_ARRAY, m_DepthMap);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_DEPTH_COMPONENT16, m_ResolutionSizeWidth, m_ResolutionSizeHeight, m_CurrentNrOfSplits);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, m_ResolutionSizeWidth, m_ResolutionSizeHeight, m_CurrentNrOfSplits, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
//glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, m_ResolutionSizeWidth, m_ResolutionSizeHeight, m_CurrentNrOfSplits, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -141,7 +141,7 @@ void ShadowPass::InitializeFrameBuffers()
glTexParameterfv(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, glm::vec4(1.f).data);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap, GL_DEPTH_ATTACHMENT)));
m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2DArray(&m_DepthMap, GL_DEPTH_ATTACHMENT)));
m_DepthBuffer.Generate();
GLERROR("depthMap failed END");
@@ -241,6 +241,10 @@ void ShadowPass::Draw(RenderScene & scene)
if (!std::dynamic_pointer_cast<ExplosionEffectJob>(objectJob)) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
if (!modelJob->Shadow) {
continue;
}
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniform1f(glGetUniformLocation(shaderHandle, "Alpha"), 1.f);
@@ -257,6 +261,10 @@ void ShadowPass::Draw(RenderScene & scene)
if (!std::dynamic_pointer_cast<ExplosionEffectJob>(objectJob)) {
auto modelJob = std::dynamic_pointer_cast<ModelJob>(objectJob);
if (!modelJob->Shadow) {
continue;
}
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
glUniform1f(glGetUniformLocation(shaderHandle, "Alpha"), modelJob->Color.a);