Skip to content

Commit

Permalink
Working on getting framebuffer textures working for OpenGL
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Dec 24, 2022
1 parent 75ef065 commit 9862762
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
46 changes: 32 additions & 14 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLDescriptorSet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "OpenGLDescriptorSet.h"
#include "OpenGLTexture.h"
#include "OpenGLFramebuffer.h"

#include<glad/glad.h>

Expand All @@ -8,7 +9,12 @@ namespace Arcane
OpenGLDescriptorSet::OpenGLDescriptorSet(DescriptorSetSpecs& setSpecs, std::initializer_list<DescriptorLayoutSpecs> layoutSpecs)
{
for (DescriptorLayoutSpecs spec : layoutSpecs) {
m_Textures[{spec.Binding, spec.Name}] = nullptr;
DescriptorInfo newInfo;
newInfo.binding = spec.Binding;
newInfo.set = setSpecs.SetNumber;
newInfo.name = spec.Name;

m_TextureInfo.push_back(newInfo);
}
}

Expand All @@ -18,40 +24,52 @@ namespace Arcane

void OpenGLDescriptorSet::AddImageSampler(Texture* texture, uint32_t setNum, uint32_t bindingNum)
{
std::map<std::pair<int, std::string>, Texture*>::iterator it;
for (it = m_Textures.begin(); it != m_Textures.end(); it++) {
if (it->first.first == bindingNum) {
it->second = texture;
}
for (int i = 0; i < m_TextureInfo.size(); i++) {
DescriptorInfo& currentInfo = m_TextureInfo[i];
currentInfo.type = TextureSourceType::TEXTURE;
currentInfo.texture = texture;
}
}

void OpenGLDescriptorSet::AddImageSampler(Framebuffer* framebuffer, uint32_t setNum, uint32_t bindingNum)
{
for (int i = 0; i < m_TextureInfo.size(); i++) {
DescriptorInfo& currentInfo = m_TextureInfo[i];
currentInfo.type = TextureSourceType::FRAMEBUFFER;
currentInfo.framebuffer = framebuffer;
}
}

void OpenGLDescriptorSet::AddUniformBuffer(UniformBuffer* buffer, uint32_t setNum, uint32_t bindingNum)
{

}

void OpenGLDescriptorSet::BindTextures(uint32_t shaderID)
{
int textureCount = 0;

std::map<std::pair<int, std::string>, Texture*>::iterator it;
for (it = m_Textures.begin(); it != m_Textures.end(); it++) {
// Cast Texture to opengl texture
OpenGLTexture* openglTexture = static_cast<OpenGLTexture*>(it->second);
for (int i = 0; i < m_TextureInfo.size(); i++) {
DescriptorInfo currentInfo = m_TextureInfo[i];

// Get Texture Uniform Name
std::string uniformName = it->first.second;
std::string uniformName = currentInfo.name;

// Set Uniform Value
glUniform1i(glGetUniformLocation(shaderID, uniformName.c_str()), textureCount);

// Set Active Texture

uint32_t textureID = -1;

if (currentInfo.type == TextureSourceType::TEXTURE) {
textureID = static_cast<OpenGLTexture*>(currentInfo.texture)->GetTextureID();
}
else {
textureID = static_cast<OpenGLFramebuffer*>(currentInfo.framebuffer)->GetFramebufferID();
}

glActiveTexture(GL_TEXTURE + textureCount);
glBindTexture(GL_TEXTURE_2D, openglTexture->GetTextureID());
glBindTexture(GL_TEXTURE_2D, textureID);
textureCount++;
}
}
}
17 changes: 17 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLDescriptorSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@ namespace Arcane

// OpenGL Specific Functions
void BindTextures(uint32_t shaderID);
private:
enum class TextureSourceType
{
TEXTURE,
FRAMEBUFFER
};

struct DescriptorInfo
{
int set;
int binding;
std::string name;
TextureSourceType type;
Texture* texture = nullptr;
Framebuffer* framebuffer = nullptr;
};
private:
std::map<std::pair<int, std::string>, Texture*> m_Textures;
std::vector<DescriptorInfo> m_TextureInfo;
};
}
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLFramebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Arcane

void Bind();
void UnBind();

uint32_t GetFramebufferID() { return m_FramebufferTexture; }
private:
uint32_t m_FBO;
uint32_t m_FramebufferTexture;
Expand Down
3 changes: 1 addition & 2 deletions Arcane/src/Arcane/Platform/OpenGL/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ namespace Arcane
}
else {
OpenGLFramebuffer* framebuffer = static_cast<OpenGLFramebuffer*>(renderPass->GetRenderPassSpecs().TargetFramebuffer);
framebuffer->Bind();

glClear(GL_COLOR_BUFFER_BIT);
glClearColor(
framebuffer->GetSpecs().ClearColor.x,
framebuffer->GetSpecs().ClearColor.y,
framebuffer->GetSpecs().ClearColor.z,
framebuffer->GetSpecs().ClearColor.w
);
framebuffer->Bind();
}
}

Expand Down
50 changes: 49 additions & 1 deletion EnchantingTable/src/OpenGLTestLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ OpenGLTestLayer::OpenGLTestLayer()

void OpenGLTestLayer::OnAttach()
{
////////////////////////////////////////////////////////////
//// Geo Pass
////////////////////////////////////////////////////////////


// Generate Texture
m_Texture = Arcane::Texture::Create(".\\src\\Assets\\Textures\\shield.png");

Expand All @@ -25,7 +30,7 @@ void OpenGLTestLayer::OnAttach()

// Renderpass Setup
Arcane::RenderPassSpecs renderPassSpecs;
renderPassSpecs.SwapchainFramebuffer = true;
renderPassSpecs.SwapchainFramebuffer = false;
renderPassSpecs.TargetFramebuffer = m_Framebuffer;
m_RenderPass = Arcane::RenderPass::Create(renderPassSpecs);

Expand Down Expand Up @@ -70,6 +75,41 @@ void OpenGLTestLayer::OnAttach()
pipelineSpecs.renderPass = m_RenderPass;
pipelineSpecs.DescriptorSets = { m_DescriptorSet };
m_Pipeline = Arcane::Pipeline::Create(pipelineSpecs);

////////////////////////////////////////////////////////////
//// Composite Pass
////////////////////////////////////////////////////////////
Arcane::FramebufferSpecifications compSpecs;
specs.Width = 800;
specs.Height = 600;
specs.ClearColor = { 1.0f, 0.0f, 0.0f, 1.0f };
m_CompFramebuffer = Arcane::Framebuffer::Create(compSpecs);

Arcane::RenderPassSpecs compRenderPassSpecs;
compRenderPassSpecs.SwapchainFramebuffer = true;
compRenderPassSpecs.TargetFramebuffer = m_CompFramebuffer;
m_CompRenderPass = Arcane::RenderPass::Create(compRenderPassSpecs);

m_CompVertexBuffer = Arcane::VertexBuffer::Create(vertices, sizeof(float) * 20);
m_CompIndexBuffer = Arcane::IndexBuffer::Create(indices, 6);
m_CompVertexBuffer->AddIndexBuffer(m_CompIndexBuffer);

Arcane::DescriptorSetSpecs compDescriptorSetSpecs;
compDescriptorSetSpecs.SetNumber = 0;
m_CompDescriptorSet = Arcane::DescriptorSet::Create(
compDescriptorSetSpecs, {
{0, 1, Arcane::DescriptorType::SAMPLER, "texSampler", Arcane::DescriptorLocation::FRAGMENT}
}
);

m_CompDescriptorSet->AddImageSampler(m_CompFramebuffer, 0, 0);

Arcane::PipelineSpecification compPipelineSpecs;
compPipelineSpecs.descriptor = m_VertexDescriptor;
compPipelineSpecs.shader = m_Shader;
compPipelineSpecs.renderPass = m_CompRenderPass;
compPipelineSpecs.DescriptorSets = { m_CompDescriptorSet };
m_CompPipeline = Arcane::Pipeline::Create(compPipelineSpecs);
}

void OpenGLTestLayer::OnDetach()
Expand All @@ -78,11 +118,19 @@ void OpenGLTestLayer::OnDetach()

void OpenGLTestLayer::OnUpdate(float deltaTime)
{
// Geo Pass
Arcane::Renderer::BeginRenderPass(m_RenderPass);
{
Arcane::Renderer::RenderQuad(m_VertexBuffer, m_Pipeline, { m_DescriptorSet });
}
Arcane::Renderer::EndRenderPass(m_RenderPass);

// Comp RenderPass
Arcane::Renderer::BeginRenderPass(m_CompRenderPass);
{
Arcane::Renderer::RenderQuad(m_CompVertexBuffer, m_CompPipeline, { m_CompDescriptorSet });
}
Arcane::Renderer::EndRenderPass(m_CompRenderPass);
}

void OpenGLTestLayer::OnImGuiRender()
Expand Down
11 changes: 11 additions & 0 deletions EnchantingTable/src/OpenGLTestLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OpenGLTestLayer : public Arcane::Layer
void OnUpdate(float deltaTime) override;
void OnImGuiRender() override;
private:
// Geo Pass
Arcane::Framebuffer* m_Framebuffer;
Arcane::RenderPass* m_RenderPass;
Arcane::VertexBuffer* m_VertexBuffer;
Expand All @@ -21,4 +22,14 @@ class OpenGLTestLayer : public Arcane::Layer
Arcane::Pipeline* m_Pipeline;
Arcane::Texture* m_Texture;
Arcane::DescriptorSet* m_DescriptorSet;

// Composite Pass
Arcane::Framebuffer* m_CompFramebuffer;
Arcane::RenderPass* m_CompRenderPass;
Arcane::VertexBuffer* m_CompVertexBuffer;
Arcane::IndexBuffer* m_CompIndexBuffer;
Arcane::Shader* m_CompShader;
Arcane::VertexDescriptor* m_CompVertexDescriptor;
Arcane::Pipeline* m_CompPipeline;
Arcane::DescriptorSet* m_CompDescriptorSet;
};

0 comments on commit 9862762

Please sign in to comment.