Good looking blur, need some fixes, but framerates takes a huge hit.

This commit is contained in:
Tleety
2016-02-09 16:19:17 +01:00
parent a33cd42ca3
commit 64e7bb1e94
9 changed files with 220 additions and 24 deletions
+105 -13
View File
@@ -6,6 +6,18 @@ DrawBloomPass::DrawBloomPass(IRenderer* renderer)
m_ScreenQuad = ResourceManager::Load<Model>("Models/Core/ScreenQuad.mesh");
m_ScreenSizes[0] = 1920.f;
m_ScreenSizes[1] = 1053.f;
m_ScreenSizes[2] = 959.f;
m_ScreenSizes[3] = 525.f;
m_ScreenSizes[4] = 479.f;
m_ScreenSizes[5] = 262.f;
m_ScreenSizes[6] = 239.f;
m_ScreenSizes[7] = 130.f;
m_ScreenSizes[8] = 119.f;
m_ScreenSizes[9] = 64.f;
InitializeTextures();
InitializeBuffers();
InitializeShaderPrograms();
@@ -18,31 +30,51 @@ void DrawBloomPass::InitializeTextures()
void DrawBloomPass::InitializeShaderPrograms()
{
m_GaussianProgram_horiz = ResourceManager::Load<ShaderProgram>("##GaussianProgramHoriz");
m_GaussianProgram_horiz = ResourceManager::Load<ShaderProgram>("#GaussianProgramHoriz");
m_GaussianProgram_horiz->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Gaussian_horiz.vert.glsl")));
m_GaussianProgram_horiz->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Gaussian_horiz.frag.glsl")));
m_GaussianProgram_horiz->Compile();
m_GaussianProgram_horiz->Link();
m_GaussianProgram_vert = ResourceManager::Load<ShaderProgram>("##GaussianProgramVert");
m_GaussianProgram_vert = ResourceManager::Load<ShaderProgram>("#GaussianProgramVert");
m_GaussianProgram_vert->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Gaussian_vert.vert.glsl")));
m_GaussianProgram_vert->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Gaussian_vert.frag.glsl")));
m_GaussianProgram_vert->Compile();
m_GaussianProgram_vert->Link();
m_GaussianProgram_both = ResourceManager::Load<ShaderProgram>("#GaussianProgramBoth");
m_GaussianProgram_both->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Gaussian_both.vert.glsl")));
m_GaussianProgram_both->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Gaussian_both.frag.glsl")));
m_GaussianProgram_both->Compile();
m_GaussianProgram_both->Link();
m_CombineProgram = ResourceManager::Load<ShaderProgram>("#CombineProgram");
m_CombineProgram->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/TextureCombine.vert.glsl")));
m_CombineProgram->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/TextureCombine.frag.glsl")));
m_CombineProgram->Compile();
m_CombineProgram->Link();
//Fixa nya combine shadern
}
void DrawBloomPass::InitializeBuffers()
{
GenerateTexture(&m_GaussianTexture_horiz, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height), GL_RGB16F, GL_RGB, GL_FLOAT);
GenerateTexture(&m_GaussianTexture_horiz, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height), GL_RGB16F, GL_RGB, GL_HALF_FLOAT);
m_GaussianFrameBuffer_horiz.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_GaussianTexture_horiz, GL_COLOR_ATTACHMENT0)));
m_GaussianFrameBuffer_horiz.Generate();
GenerateTexture(&m_GaussianTexture_vert, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height), GL_RGB16F, GL_RGB, GL_FLOAT);
GenerateTexture(&m_GaussianTexture_vert, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height), GL_RGB16F, GL_RGB, GL_HALF_FLOAT);
m_GaussianFrameBuffer_vert.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_GaussianTexture_vert, GL_COLOR_ATTACHMENT0)));
m_GaussianFrameBuffer_vert.Generate();
GenerateTexture(&m_FinalBlurTexture, GL_CLAMP_TO_EDGE, GL_LINEAR, glm::vec2(m_Renderer->GetViewportSize().Width, m_Renderer->GetViewportSize().Height), GL_RGB16F, GL_RGB, GL_FLOAT);
m_BlurredFinalTexture.AddResource(std::shared_ptr<BufferResource>(new Texture2D(&m_FinalBlurTexture, GL_COLOR_ATTACHMENT0)));
m_BlurredFinalTexture.Generate();
}
@@ -60,65 +92,123 @@ void DrawBloomPass::ClearBuffer()
void DrawBloomPass::Draw(GLuint texture)
{
GLERROR("DrawBloomPass::Draw: Pre");
ImGui::Combo("DebugBloom", &m_DebugTextureToDraw, "ALL\0First\0SEcond\0Third\0Fourth\0Fifth");
ImGui::SliderInt("Iterations", &m_Iterations, 0, 10);
if (m_DebugTextureToDraw == 1)
BlurOneMipMapLevel(&texture, m_Iterations, 0);
else if (m_DebugTextureToDraw == 2)
BlurOneMipMapLevel(&texture, m_Iterations, 1);
else if (m_DebugTextureToDraw == 3)
BlurOneMipMapLevel(&texture, m_Iterations, 2);
else if (m_DebugTextureToDraw == 4)
BlurOneMipMapLevel(&texture, m_Iterations, 3);
else if (m_DebugTextureToDraw == 0) {
BlurOneMipMapLevel(&texture, m_Iterations, 0);
BlurOneMipMapLevel(&texture, m_Iterations, 1);
BlurOneMipMapLevel(&texture, m_Iterations, 2);
BlurOneMipMapLevel(&texture, m_Iterations, 3);
}
}
void DrawBloomPass::BlurOneMipMapLevel(GLuint* texture, GLint iterations, GLint mipMapLevel)
{
GLERROR("PRE");
//new code
DrawBloomPassState state;
//Loop through and blur texture at mipmap level
GLuint shaderHandle_horiz = m_GaussianProgram_horiz->GetHandle();
GLuint shaderHandle_vert = m_GaussianProgram_vert->GetHandle();
//Horizontal pass, first use the given texture then save it to the horizontal framebuffer.
//First horiz pass
m_GaussianFrameBuffer_horiz.Bind();
m_GaussianProgram_horiz->Bind();
GLERROR("Bind");
glUniform1i(glGetUniformLocation(shaderHandle_horiz, "MipMapLevel"), mipMapLevel);
//glUniform2fv(glGetUniformLocation(m_GaussianProgram_both->GetHandle(), "ScreenSize"), 5, m_ScreenSizes);
//glUniform1i(glGetUniformLocation(m_GaussianProgram_both->GetHandle(), "Horizontal"), true);
GLERROR("1");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
glBindVertexArray(m_ScreenQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1
, GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex);
GLERROR("2");
//Iterate some times to make it more gaussian.
for (int i = 1; i < m_iterations; i++) {
//Loop through vert, then horiz and save to respective textures X number of times.
for (int i = 1; i < iterations; i++) {
//Vertical pass
m_GaussianFrameBuffer_vert.Bind();
m_GaussianProgram_vert->Bind();
//glUniform1i(glGetUniformLocation(m_GaussianProgram_both->GetHandle(), "Horizontal"), false);
glBindTexture(GL_TEXTURE_2D, m_GaussianTexture_horiz);
glBindVertexArray(m_ScreenQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1
, GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex);
GLERROR("3");
//horizontal pass
m_GaussianFrameBuffer_horiz.Bind();
m_GaussianProgram_horiz->Bind();
//glUniform1i(glGetUniformLocation(m_GaussianProgram_both->GetHandle(), "Horizontal"), true);
glBindTexture(GL_TEXTURE_2D, m_GaussianTexture_vert);
glBindVertexArray(m_ScreenQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1
, GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex);
GLERROR("4");
}
//final vertical gaussian after the iterations are done
//the final vertical iteration.
m_GaussianFrameBuffer_vert.Bind();
m_GaussianProgram_vert->Bind();
//glUniform1i(glGetUniformLocation(m_GaussianProgram_both->GetHandle(), "Horizontal"), false);
glBindTexture(GL_TEXTURE_2D, m_GaussianTexture_horiz);
glBindVertexArray(m_ScreenQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1
, GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex);
GLERROR("5");
GLERROR("DrawBloomPass::Draw: END");
//Combine the the new blurred texture into the final texture
m_BlurredFinalTexture.Bind();
m_CombineProgram->Bind();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_FinalBlurTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_GaussianTexture_vert);
glBindVertexArray(m_ScreenQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1
, GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex);
GLERROR("END");
}
void DrawBloomPass::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const
@@ -132,3 +222,5 @@ void DrawBloomPass::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum fil
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, dimensions.x, dimensions.y, 0, format, type, nullptr);//TODO: Renderer: Fix the precision and Resolution
GLERROR("Texture initialization failed");
}