2026-03-16 19:06:21 +08:00
|
|
|
#define GLFW_INCLUDE_NONE
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLDepthStencilView.h"
|
|
|
|
|
#include <glad/glad.h>
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace RHI {
|
|
|
|
|
|
2026-03-17 02:20:56 +08:00
|
|
|
OpenGLDepthStencilView::OpenGLDepthStencilView()
|
|
|
|
|
: m_texture(0)
|
|
|
|
|
, m_framebuffer(0)
|
|
|
|
|
, m_mipLevel(0)
|
|
|
|
|
, m_width(0)
|
|
|
|
|
, m_height(0)
|
|
|
|
|
, m_type(DepthStencilType::Texture2D)
|
|
|
|
|
, m_format(DepthStencilFormat::D24_UNORM_S8_UINT) {
|
2026-03-16 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenGLDepthStencilView::~OpenGLDepthStencilView() {
|
2026-03-17 02:20:56 +08:00
|
|
|
Shutdown();
|
2026-03-16 19:06:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 02:20:56 +08:00
|
|
|
bool OpenGLDepthStencilView::Initialize(unsigned int texture, const DepthStencilViewDesc& desc) {
|
2026-03-16 19:06:21 +08:00
|
|
|
m_texture = texture;
|
2026-03-17 02:20:56 +08:00
|
|
|
m_mipLevel = desc.mipLevel;
|
|
|
|
|
m_type = desc.type;
|
|
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &m_framebuffer);
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
|
|
|
|
|
|
|
|
|
GLenum depthAttachment = GL_DEPTH_ATTACHMENT;
|
|
|
|
|
GLenum stencilAttachment = GL_STENCIL_ATTACHMENT;
|
|
|
|
|
|
|
|
|
|
switch (desc.type) {
|
|
|
|
|
case DepthStencilType::Texture2D:
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture, desc.mipLevel);
|
|
|
|
|
break;
|
|
|
|
|
case DepthStencilType::Texture2DArray:
|
|
|
|
|
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texture, desc.mipLevel, desc.baseArraySlice);
|
|
|
|
|
break;
|
|
|
|
|
case DepthStencilType::TextureCube:
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + desc.baseArraySlice, texture, desc.mipLevel);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDepthStencilView::Initialize(unsigned int texture, int mipLevel) {
|
|
|
|
|
DepthStencilViewDesc desc;
|
|
|
|
|
desc.mipLevel = mipLevel;
|
|
|
|
|
return Initialize(texture, desc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDepthStencilView::InitializeCubemap(unsigned int cubemap, int face, int mipLevel) {
|
|
|
|
|
m_texture = cubemap;
|
2026-03-16 19:06:21 +08:00
|
|
|
m_mipLevel = mipLevel;
|
2026-03-17 02:20:56 +08:00
|
|
|
m_type = DepthStencilType::TextureCube;
|
2026-03-16 19:06:21 +08:00
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &m_framebuffer);
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
2026-03-17 02:20:56 +08:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap, mipLevel);
|
|
|
|
|
|
2026-03-16 19:06:21 +08:00
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
2026-03-17 02:20:56 +08:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2026-03-16 19:06:21 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-17 02:20:56 +08:00
|
|
|
|
2026-03-16 19:06:21 +08:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::Shutdown() {
|
|
|
|
|
if (m_framebuffer) {
|
|
|
|
|
glDeleteFramebuffers(1, &m_framebuffer);
|
|
|
|
|
m_framebuffer = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::Bind() {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::Unbind() {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 02:20:56 +08:00
|
|
|
void OpenGLDepthStencilView::ClearDepth(float depth) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
|
|
|
|
glClearDepth(depth);
|
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::ClearStencil(uint8_t stencil) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
|
|
|
|
glClearStencil(stencil);
|
|
|
|
|
glClear(GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::ClearDepthStencil(float depth, uint8_t stencil) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
|
|
|
|
|
glClearDepth(depth);
|
|
|
|
|
glClearStencil(stencil);
|
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::BindFramebuffer(unsigned int framebuffer) {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDepthStencilView::UnbindFramebuffer() {
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 19:06:21 +08:00
|
|
|
} // namespace RHI
|
|
|
|
|
} // namespace XCEngine
|