refactor: Refactor OpenGL backend to use OpenGLEnums

Use centralized OpenGLEnums.h for enum conversion:
- Remove local ToGL* functions from OpenGLCommandList
- Replace with ToOpenGL() and ToOpenGLClearBuffer() from OpenGLEnums
- Simplify OpenGLTexture, OpenGLBuffer, OpenGLSampler, etc.
This commit is contained in:
2026-03-25 19:01:36 +08:00
parent 773d1aa38a
commit 712e975610
13 changed files with 108 additions and 297 deletions

View File

@@ -1,4 +1,5 @@
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
#include "XCEngine/RHI/OpenGL/OpenGLEnums.h"
#include <glad/glad.h>
#include <stb_image.h>
#include <iostream>
@@ -6,47 +7,6 @@
namespace XCEngine {
namespace RHI {
static unsigned int ToGLTextureTarget(OpenGLTextureType type) {
switch (type) {
case OpenGLTextureType::Texture1D: return GL_TEXTURE_1D;
case OpenGLTextureType::Texture2D: return GL_TEXTURE_2D;
case OpenGLTextureType::Texture2DArray: return GL_TEXTURE_2D_ARRAY;
case OpenGLTextureType::Texture3D: return GL_TEXTURE_3D;
case OpenGLTextureType::TextureCube: return GL_TEXTURE_CUBE_MAP;
case OpenGLTextureType::TextureCubeArray: return GL_TEXTURE_CUBE_MAP_ARRAY;
default: return GL_TEXTURE_2D;
}
}
static void ToGLFormat(OpenGLFormat fmt, unsigned int& internalFormat, unsigned int& glFormat, unsigned int& glType) {
switch (fmt) {
case OpenGLFormat::R8:
internalFormat = GL_R8; glFormat = GL_RED; glType = GL_UNSIGNED_BYTE;
break;
case OpenGLFormat::RG8:
internalFormat = GL_RG8; glFormat = GL_RG; glType = GL_UNSIGNED_BYTE;
break;
case OpenGLFormat::RGBA8:
internalFormat = GL_RGBA8; glFormat = GL_RGBA; glType = GL_UNSIGNED_BYTE;
break;
case OpenGLFormat::RGBA16F:
internalFormat = GL_RGBA16F; glFormat = GL_RGBA; glType = GL_HALF_FLOAT;
break;
case OpenGLFormat::RGBA32F:
internalFormat = GL_RGBA32F; glFormat = GL_RGBA; glType = GL_FLOAT;
break;
case OpenGLFormat::Depth24Stencil8:
internalFormat = GL_DEPTH24_STENCIL8; glFormat = GL_DEPTH_STENCIL; glType = GL_UNSIGNED_INT_24_8;
break;
case OpenGLFormat::Depth32F:
internalFormat = GL_DEPTH_COMPONENT32F; glFormat = GL_DEPTH_COMPONENT; glType = GL_FLOAT;
break;
default:
internalFormat = GL_RGBA8; glFormat = GL_RGBA; glType = GL_UNSIGNED_BYTE;
break;
}
}
OpenGLTexture::OpenGLTexture()
: m_texture(0)
, m_type(OpenGLTextureType::Texture2D)
@@ -68,9 +28,10 @@ bool OpenGLTexture::Initialize(OpenGLTextureType type, int width, int height, in
m_depth = depth;
m_mipLevels = mipLevels;
unsigned int target = ToGLTextureTarget(type);
unsigned int internalFormat, glFormat, glType;
ToGLFormat(format, internalFormat, glFormat, glType);
unsigned int target = ToOpenGL(type);
GLint internalFormat;
GLenum glFormat, glType;
ToOpenGLFormat(format, internalFormat, glFormat, glType);
glGenTextures(1, &m_texture);
glBindTexture(target, m_texture);
@@ -136,8 +97,9 @@ bool OpenGLTexture::InitializeCubeMap(int size, int mipLevels, OpenGLFormat form
m_depth = 1;
m_mipLevels = mipLevels;
unsigned int internalFormat, glFormat, glType;
ToGLFormat(format, internalFormat, glFormat, glType);
GLint internalFormat;
GLenum glFormat, glType;
ToOpenGLFormat(format, internalFormat, glFormat, glType);
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
@@ -178,31 +140,31 @@ void OpenGLTexture::Shutdown() {
}
void OpenGLTexture::Bind(int slot) const {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(target, m_texture);
}
void OpenGLTexture::Unbind() const {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
glBindTexture(target, 0);
}
void OpenGLTexture::BindImage(int slot, bool read, bool write) const {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
GLenum access = (read && write) ? GL_READ_WRITE : (read ? GL_READ_ONLY : GL_WRITE_ONLY);
glBindImageTexture(slot, m_texture, 0, GL_FALSE, 0, access, GL_RGBA8);
}
void OpenGLTexture::GenerateMipmap() {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
glBindTexture(target, m_texture);
glGenerateMipmap(target);
glBindTexture(target, 0);
}
void OpenGLTexture::SetFiltering(int minFilter, int magFilter) {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
glBindTexture(target, m_texture);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
@@ -210,7 +172,7 @@ void OpenGLTexture::SetFiltering(int minFilter, int magFilter) {
}
void OpenGLTexture::SetWrapping(int wrapS, int wrapT, int wrapR) {
unsigned int target = ToGLTextureTarget(m_type);
unsigned int target = ToOpenGL(m_type);
glBindTexture(target, m_texture);
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapT);