Initial work on texture arrays

This commit is contained in:
FakeShemp
2016-02-26 17:40:35 +01:00
parent f220d8088c
commit 22c391df54
9 changed files with 101 additions and 86 deletions
+14
View File
@@ -8,10 +8,12 @@ class BufferResource
{
public:
BufferResource(GLuint* resourceHandle, GLenum resourceType, GLenum attachment);
BufferResource(GLuint* resourceHandle, GLenum resourceType, GLenum attachment, GLuint layers);
GLuint* m_ResourceHandle;
GLenum m_ResourceType;
GLenum m_Attachment;
GLuint m_Layers;
private:
};
@@ -22,6 +24,9 @@ class ResourceType : public BufferResource
public:
ResourceType(GLuint* resourceHandle, GLenum attachment)
: BufferResource(resourceHandle, RESOURCETYPE, attachment) { }
ResourceType(GLuint* resourceHandle, GLenum attachment, GLuint layers)
: BufferResource(resourceHandle, RESOURCETYPE, attachment, layers) { }
};
class Texture2D : public ResourceType<GL_TEXTURE_2D>
@@ -43,6 +48,15 @@ public:
~RenderBuffer();
};
class Texture2DArray : public ResourceType<GL_TEXTURE_2D_ARRAY>
{
public:
Texture2DArray(GLuint* resourceHandle, GLenum attachment, GLuint layers)
: ResourceType(resourceHandle, attachment, layers) { };
~Texture2DArray();
};
class FrameBuffer
{
public:
+6 -3
View File
@@ -39,7 +39,8 @@ public:
void ClearBuffer();
void Draw(RenderScene& scene);
GLuint DepthMap(int level) const { return m_DepthMap[level]; }
//GLuint DepthMap(int level) const { return m_DepthMap[level]; }
GLuint DepthMap() const { return m_DepthMap; }
std::array<glm::mat4, MAX_SPLITS> LightP() const { return m_LightProjection; }
std::array<glm::mat4, MAX_SPLITS> LightV() const { return m_LightView; }
std::array<float, MAX_SPLITS> FarDistance() const { return { m_shadowFrusta[0].FarClip, m_shadowFrusta[1].FarClip, m_shadowFrusta[2].FarClip, m_shadowFrusta[3].FarClip }; }
@@ -55,8 +56,10 @@ private:
EventBroker* m_EventBroker;
const IRenderer* m_Renderer;
std::array<GLuint, MAX_SPLITS> m_DepthMap;
std::array<FrameBuffer, MAX_SPLITS> m_DepthBuffer;
//std::array<GLuint, MAX_SPLITS> m_DepthMap;
//std::array<FrameBuffer, MAX_SPLITS> m_DepthBuffer;
GLuint m_DepthMap;
FrameBuffer m_DepthBuffer;
std::array<glm::mat4, MAX_SPLITS> m_LightProjection;
std::array<glm::mat4, MAX_SPLITS> m_LightView;
+1 -1
View File
@@ -1,6 +1,6 @@
#version 430
#define MAX_SPLITS 3
#define MAX_SPLITS 4
uniform mat4 M;
uniform mat4 V;
+16 -47
View File
@@ -1,6 +1,6 @@
#version 430
#define MAX_SPLITS 3
#define MAX_SPLITS 4
uniform mat4 M;
uniform mat4 V;
@@ -17,9 +17,8 @@ layout (binding = 0) uniform sampler2D DiffuseTexture;
layout (binding = 1) uniform sampler2D NormalMapTexture;
layout (binding = 2) uniform sampler2D SpecularMapTexture;
layout (binding = 3) uniform sampler2D GlowMapTexture;
layout (binding = 4) uniform sampler2DShadow DepthMap0;
layout (binding = 5) uniform sampler2DShadow DepthMap1;
layout (binding = 6) uniform sampler2DShadow DepthMap2;
layout (binding = 4) uniform sampler2DShadow DepthMap[MAX_SPLITS];
#define TILE_SIZE 16
@@ -209,36 +208,8 @@ int getShadowIndex(float far_distance[MAX_SPLITS])
return index;
}
//sampler2DShadow whichDepthMap(int DepthMapIndex)
//{
// if( DepthMapIndex == 0 )
// {
// return DepthMap0;
// }
// else if( DepthMapIndex == 1 )
// {
// return DepthMap1;
// }
// else
// {
// return DepthMap2;
// }
//}
void main()
{
//sampler2DShadow DepthMaps[3] = { sampler2DShadow(DepthMap0), sampler2DShadow(DepthMap1), sampler2DShadow(DepthMap2) };
//sampler2DShadow DepthMaps[3] = { DepthMap0, DepthMap1, DepthMap2 };
//sampler2DShadow DepthMaps[3] = sampler2DShadow[]( DepthMap0, DepthMap1, DepthMap2 );
//sampler2DShadow DepthMaps[3] = sampler2DShadow[3]( DepthMap0, DepthMap1, DepthMap2 );
//sampler2DShadow DepthMaps[3];
//sampler2DShadow DepthMapOne = DepthMap0;
//DepthMaps[0] = DepthMap0;
//DepthMaps[1] = DepthMap1;
//DepthMaps[2] = DepthMap2;
vec4 diffuseTexel = texture2D(DiffuseTexture, Input.TextureCoordinate);
vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate);
vec4 specularTexel = texture2D(SpecularMapTexture, Input.TextureCoordinate);
@@ -271,24 +242,22 @@ 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);
//sampler2DShadow WhichDepthMap = whichDepthMap(DepthMapIndex);
//int DepthMapIndex = getShadowIndex(FarDistance);
light_result = CalcDirectionalLightSource(V * light.Direction, light.Color, light.Intensity, viewVec, normal);
//shadowFactor = CalcShadowValue(Input.PositionLightSpace[0], Input.Normal, vec3(light.Direction), DepthMap0);
if( DepthMapIndex == 0 )
{
shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap0);
}
else if( DepthMapIndex == 1 )
{
shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap1);
}
else
{
shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap2);
}
//if( DepthMapIndex == 0 )
//{
shadowFactor = CalcShadowValue(Input.PositionLightSpace[0], Input.Normal, vec3(light.Direction), DepthMap[0]);
//}
//else if( DepthMapIndex == 1 )
//{
// shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap1);
//}
//else
//{
// shadowFactor = CalcShadowValue(Input.PositionLightSpace[DepthMapIndex], Input.Normal, vec3(light.Direction), DepthMap2);
//}
}
totalLighting.Diffuse += light_result.Diffuse;
+1 -1
View File
@@ -1,6 +1,6 @@
#version 430
#define MAX_SPLITS 3
#define MAX_SPLITS 4
uniform mat4 M;
uniform mat4 V;
+7 -7
View File
@@ -320,15 +320,15 @@ void DrawFinalPass::BindModelTextures(std::shared_ptr<ModelJob>& job)
glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture);
}
for (int i = 0; i < m_ShadowPass->CurrentNrOfSplits(); i++)
{
glActiveTexture(GL_TEXTURE4 + i);
if (m_ShadowPass->DepthMap(i) != NULL) {
glBindTexture(GL_TEXTURE_2D, m_ShadowPass->DepthMap(i));
//for (int i = 0; i < m_ShadowPass->CurrentNrOfSplits(); i++)
//{
glActiveTexture(GL_TEXTURE4);
if (m_ShadowPass->DepthMap() != NULL) {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_ShadowPass->DepthMap());
} else {
glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_WhiteTexture->m_Texture);
}
}
//}
}
+21 -3
View File
@@ -9,6 +9,14 @@ BufferResource::BufferResource(GLuint* resourceHandle, GLenum resourceType, GLen
m_Attachment = attachment;
}
BufferResource::BufferResource(GLuint* resourceHandle, GLenum resourceType, GLenum attachment, GLuint layers)
{
m_ResourceHandle = resourceHandle;
m_ResourceType = resourceType;
m_Attachment = attachment;
m_Layers = layers;
}
Texture2D::~Texture2D()
{
if (m_ResourceHandle != 0) {
@@ -16,6 +24,12 @@ Texture2D::~Texture2D()
}
}
Texture2DArray::~Texture2DArray()
{
if (m_ResourceHandle != 0) {
glDeleteTextures(1, m_ResourceHandle);
}
}
RenderBuffer::~RenderBuffer()
{
@@ -48,18 +62,22 @@ void FrameBuffer::Generate()
switch ((*it)->m_ResourceType) {
case GL_TEXTURE_2D:
glFramebufferTexture2D(GL_FRAMEBUFFER, (*it)->m_Attachment, (*it)->m_ResourceType, *(*it)->m_ResourceHandle, 0);
attachments.push_back((*it)->m_Attachment);
GLERROR("FrameBuffer generate: glFramebufferTexture2D");
break;
case GL_RENDERBUFFER:
glFramebufferRenderbuffer(GL_FRAMEBUFFER, (*it)->m_Attachment, (*it)->m_ResourceType, *(*it)->m_ResourceHandle);
GLERROR("FrameBuffer generate: glFramebufferRenderbuffer");
break;
case GL_TEXTURE_2D_ARRAY:
glFramebufferTexture(GL_FRAMEBUFFER, (*it)->m_Attachment, *(*it)->m_ResourceHandle, 0);
attachments.push_back((*it)->m_Attachment);
GLERROR("FrameBuffer generate: GL_TEXTURE_2D_ARRAY");
break;
}
if ((*it)->m_ResourceType == GL_TEXTURE_2D) {
attachments.push_back((*it)->m_Attachment);
}
}
GLenum* bufferTextures = &attachments[0];
+3 -3
View File
@@ -136,9 +136,9 @@ void Renderer::Draw(RenderFrame& frame)
if (m_DebugTextureToDraw == 4) {
m_DrawScreenQuadPass->Draw(m_PickingPass->PickingTexture());
}
if (m_DebugTextureToDraw == 5) {
m_DrawScreenQuadPass->Draw(m_ShadowPass->DepthMap(0));
}
//if (m_DebugTextureToDraw == 5) {
// m_DrawScreenQuadPass->Draw(m_ShadowPass->DepthMap());
//}
m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window);
+32 -21
View File
@@ -106,26 +106,37 @@ float ShadowPass::FindRadius(Frustum& frustum)
void ShadowPass::InitializeFrameBuffers()
{
// Depth texture
glGenTextures(m_CurrentNrOfSplits, m_DepthMap.data());
GLERROR("depthMap failed PRE");
// Depth texture
glGenTextures(1, &m_DepthMap);
for (int i = 0; i < m_CurrentNrOfSplits; i++) {
glBindTexture(GL_TEXTURE_2D, m_DepthMap[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, m_ResolutionSizeWidth / (1 /*+ i*/), m_ResolutionSizeHeigth + (1 /*+ i*/), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, glm::vec4(1.f).data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
GLERROR("depthMap failed1");
glBindTexture(GL_TEXTURE_2D_ARRAY, m_DepthMap);
GLERROR("depthMap failed2");
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_DEPTH_COMPONENT16, m_ResolutionSizeWidth, m_ResolutionSizeHeigth, m_CurrentNrOfSplits);
GLERROR("depthMap failed3");
m_DepthBuffer[i].AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_DepthMap[i], GL_DEPTH_ATTACHMENT)));
m_DepthBuffer[i].Generate();
}
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, m_ResolutionSizeWidth, m_ResolutionSizeHeigth, m_CurrentNrOfSplits, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
GLERROR("depthMap failed4");
GLERROR("depthMap failed");
//for (int i = 0; i < m_CurrentNrOfSplits; i++) {
// glBindTexture(GL_TEXTURE_2D, m_DepthMap[i]);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, m_ResolutionSizeWidth / (1 /*+ i*/), m_ResolutionSizeHeigth + (1 /*+ i*/), 0, 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);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
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);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
GLERROR("depthMap failed5");
m_DepthBuffer.AddResource(std::shared_ptr<BufferResource>(new Texture2DArray(&m_DepthMap, GL_DEPTH_ATTACHMENT, m_CurrentNrOfSplits)));
m_DepthBuffer.Generate();
//}
GLERROR("depthMap failed END");
}
void ShadowPass::InitializeShaderPrograms()
@@ -141,10 +152,10 @@ void ShadowPass::InitializeShaderPrograms()
void ShadowPass::ClearBuffer()
{
for (int i = 0; i < m_CurrentNrOfSplits; i++) {
m_DepthBuffer[i].Bind();
m_DepthBuffer.Bind();
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_DepthBuffer[i].Unbind();
m_DepthBuffer.Unbind();
}
}
@@ -192,7 +203,7 @@ void ShadowPass::Draw(RenderScene & scene)
UpdateFrustumPoints(m_shadowFrusta[i], scene.Camera->Position(), scene.Camera->Forward(), scene.Camera->ProjectionMatrix(), scene.Camera->ViewMatrix());
//float test = FindRadius(m_shadFrusta[i]);
ShadowPassState* state = new ShadowPassState(m_DepthBuffer[i].GetHandle());
ShadowPassState* state = new ShadowPassState(m_DepthBuffer.GetHandle());
GLuint shaderHandle = m_ShadowProgram->GetHandle();
m_ShadowProgram->Bind();
@@ -230,7 +241,7 @@ void ShadowPass::Draw(RenderScene & scene)
GLERROR("Shadow Draw ERROR");
}
}
m_DepthBuffer[i].Unbind();
m_DepthBuffer.Unbind();
delete state;
}