OpenGL: Restructure tests similar to D3D12 layout
- Move old test files to new unit/integration structure - Add OpenGL Test Fixture - Update CMakeLists.txt for new layout - Add OpenGL_Test_Restructuring_Plan.md
This commit is contained in:
124
tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.cpp
Normal file
124
tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include "OpenGLTestFixture.h"
|
||||
|
||||
using namespace XCEngine::RHI;
|
||||
|
||||
static bool s_glfwInitialized = false;
|
||||
static bool s_gladInitialized = false;
|
||||
|
||||
void OpenGLTestFixture::SetUp() {
|
||||
if (!s_glfwInitialized) {
|
||||
if (!glfwInit()) {
|
||||
GTEST_SKIP() << "Failed to initialize GLFW";
|
||||
return;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
s_glfwInitialized = true;
|
||||
}
|
||||
|
||||
m_window = glfwCreateWindow(640, 480, "OpenGL Tests", nullptr, nullptr);
|
||||
if (!m_window) {
|
||||
GTEST_SKIP() << "Failed to create GLFW window";
|
||||
return;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(m_window);
|
||||
|
||||
if (!s_gladInitialized) {
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
GTEST_SKIP() << "Failed to initialize GLAD";
|
||||
glfwDestroyWindow(m_window);
|
||||
m_window = nullptr;
|
||||
return;
|
||||
}
|
||||
s_gladInitialized = true;
|
||||
}
|
||||
|
||||
m_device = new OpenGLDevice();
|
||||
m_device->CreateRenderWindow(640, 480, "Test Window", false);
|
||||
|
||||
ClearGLErrors();
|
||||
}
|
||||
|
||||
void OpenGLTestFixture::TearDown() {
|
||||
ResetGLState();
|
||||
|
||||
if (m_device) {
|
||||
delete m_device;
|
||||
m_device = nullptr;
|
||||
}
|
||||
|
||||
if (m_window) {
|
||||
glfwDestroyWindow(m_window);
|
||||
m_window = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTestFixture::MakeContextCurrent() {
|
||||
if (m_window) {
|
||||
glfwMakeContextCurrent(m_window);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTestFixture::DoneContextCurrent() {
|
||||
glfwMakeContextCurrent(nullptr);
|
||||
}
|
||||
|
||||
void OpenGLTestFixture::ClearGLErrors() {
|
||||
while (glGetError() != GL_NO_ERROR);
|
||||
}
|
||||
|
||||
bool OpenGLTestFixture::CheckGLError(const char* file, int line) {
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR) {
|
||||
ADD_FAILURE() << "OpenGL Error: " << error
|
||||
<< " (" << GetGLErrorString(error) << ")"
|
||||
<< " at " << file << ":" << line;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* OpenGLTestFixture::GetGLErrorString(GLenum error) {
|
||||
switch (error) {
|
||||
case GL_NO_ERROR: return "GL_NO_ERROR";
|
||||
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
|
||||
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
|
||||
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
|
||||
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
|
||||
default: return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTestFixture::ResetGLState() {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
|
||||
glUseProgram(0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
glDisable(GL_BLEND);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
|
||||
glDepthFunc(GL_LESS);
|
||||
glStencilFunc(GL_ALWAYS, 0, 0xFF);
|
||||
glBlendFunc(GL_ONE, GL_ZERO);
|
||||
}
|
||||
Reference in New Issue
Block a user