Files
XCEngine/tests/RHI/OpenGL/unit/test_command_list.cpp
ssdfasd 1797e7fe17 fix: encapsulate OpenGL types in VertexAttribute to eliminate raw GL API usage in tests
- Add VertexAttributeType and VertexAttributeNormalized enums in OpenGLVertexArray.h
- Add ToGLAttributeType() converter in OpenGLVertexArray.cpp
- Remove glActiveTexture() call from quad test (already handled by texture.Bind())
- Remove #include <glad/glad.h> from triangle test
- Update unit tests to use encapsulated enums

All three OpenGL integration tests (minimal, triangle, quad) pass with 0% pixel difference.
2026-03-22 14:33:57 +08:00

193 lines
5.2 KiB
C++

#include "fixtures/OpenGLTestFixture.h"
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
#include "XCEngine/RHI/OpenGL/OpenGLVertexArray.h"
using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, CommandList_Clear_ColorBuffer) {
OpenGLCommandList cmdList;
cmdList.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GLfloat color[4] = {};
glGetFloatv(GL_COLOR_CLEAR_VALUE, color);
EXPECT_FLOAT_EQ(color[0], 1.0f);
EXPECT_FLOAT_EQ(color[1], 0.0f);
EXPECT_FLOAT_EQ(color[2], 0.0f);
EXPECT_FLOAT_EQ(color[3], 1.0f);
}
TEST_F(OpenGLTestFixture, CommandList_Clear_DepthStencil) {
OpenGLCommandList cmdList;
cmdList.ClearDepth(0.5f);
cmdList.ClearStencil(2);
GLfloat depth = 0.0f;
glGetFloatv(GL_DEPTH_CLEAR_VALUE, &depth);
EXPECT_FLOAT_EQ(depth, 0.5f);
}
TEST_F(OpenGLTestFixture, CommandList_SetIndexBuffer) {
OpenGLCommandList cmdList;
OpenGLBuffer buffer;
buffer.InitializeIndexBuffer(nullptr, 12);
cmdList.SetIndexBuffer(buffer.GetID(), GL_UNSIGNED_INT, 0);
GLenum error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
buffer.Shutdown();
}
TEST_F(OpenGLTestFixture, CommandList_Draw_VAO) {
OpenGLCommandList cmdList;
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer vbo;
float vertices[] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f };
vbo.InitializeVertexBuffer(vertices, sizeof(vertices));
VertexAttribute attr;
attr.index = 0;
attr.count = 3;
attr.type = VertexAttributeType::Float;
attr.normalized = VertexAttributeNormalized::False;
attr.stride = sizeof(float) * 3;
attr.offset = 0;
vao.AddVertexBuffer(vbo.GetID(), attr);
OpenGLBuffer ibo;
unsigned int indices[] = { 0, 1, 2 };
ibo.InitializeIndexBuffer(indices, sizeof(indices));
vao.SetIndexBuffer(ibo.GetID(), GL_UNSIGNED_INT);
cmdList.BindVertexArray(vao.GetID());
cmdList.SetPrimitiveType(PrimitiveType::Triangles);
cmdList.Draw(PrimitiveType::Triangles, 3, 0);
GLenum error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
vbo.Shutdown();
ibo.Shutdown();
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, CommandList_SetViewport) {
OpenGLCommandList cmdList;
cmdList.SetViewport(0, 0, 800, 600);
GLint viewport[4] = {};
glGetIntegerv(GL_VIEWPORT, viewport);
EXPECT_EQ(viewport[0], 0);
EXPECT_EQ(viewport[1], 0);
EXPECT_EQ(viewport[2], 800);
EXPECT_EQ(viewport[3], 600);
}
TEST_F(OpenGLTestFixture, CommandList_SetScissor) {
OpenGLCommandList cmdList;
cmdList.EnableScissorTest(true);
cmdList.SetScissor(0, 0, 800, 600);
GLint scissor[4] = {};
glGetIntegerv(GL_SCISSOR_BOX, scissor);
EXPECT_EQ(scissor[0], 0);
EXPECT_EQ(scissor[1], 0);
EXPECT_EQ(scissor[2], 800);
EXPECT_EQ(scissor[3], 600);
}
TEST_F(OpenGLTestFixture, CommandList_EnableDisable_DepthTest) {
OpenGLCommandList cmdList;
cmdList.EnableDepthTest(true);
GLint depthTest = 0;
glGetIntegerv(GL_DEPTH_TEST, &depthTest);
EXPECT_EQ(depthTest, 1);
cmdList.EnableDepthTest(false);
glGetIntegerv(GL_DEPTH_TEST, &depthTest);
EXPECT_EQ(depthTest, 0);
}
TEST_F(OpenGLTestFixture, CommandList_EnableDisable_Blending) {
OpenGLCommandList cmdList;
cmdList.EnableBlending(true);
GLint blend = 0;
glGetIntegerv(GL_BLEND, &blend);
EXPECT_EQ(blend, 1);
cmdList.EnableBlending(false);
glGetIntegerv(GL_BLEND, &blend);
EXPECT_EQ(blend, 0);
}
TEST_F(OpenGLTestFixture, CommandList_SetPrimitiveTopology) {
OpenGLCommandList cmdList;
cmdList.SetPrimitiveTopology(PrimitiveTopology::TriangleList);
GLenum error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
cmdList.SetPrimitiveTopology(PrimitiveTopology::LineList);
error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
cmdList.SetPrimitiveTopology(PrimitiveTopology::PointList);
error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
}
TEST_F(OpenGLTestFixture, CommandList_SetDepthStencilState) {
OpenGLCommandList cmdList;
DepthStencilState state = {};
state.depthEnable = true;
state.depthWriteMask = true;
state.depthFunc = ComparisonFunc::Less;
cmdList.SetDepthStencilState(state);
GLint depthTest = 0;
glGetIntegerv(GL_DEPTH_TEST, &depthTest);
EXPECT_EQ(depthTest, 1);
}
TEST_F(OpenGLTestFixture, CommandList_SetBlendState) {
OpenGLCommandList cmdList;
BlendState state = {};
state.renderTargets[0].blendEnable = true;
state.renderTargets[0].srcBlend = BlendFactor::SrcAlpha;
state.renderTargets[0].dstBlend = BlendFactor::InvSrcAlpha;
cmdList.SetBlendState(state);
GLint blend = 0;
glGetIntegerv(GL_BLEND, &blend);
EXPECT_EQ(blend, 1);
}
TEST_F(OpenGLTestFixture, CommandList_SetBlendFactor) {
OpenGLCommandList cmdList;
float factor[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
cmdList.SetBlendFactor(factor);
GLfloat color[4] = {};
glGetFloatv(GL_BLEND_COLOR, color);
EXPECT_FLOAT_EQ(color[0], 0.5f);
EXPECT_FLOAT_EQ(color[1], 0.5f);
EXPECT_FLOAT_EQ(color[2], 0.5f);
EXPECT_FLOAT_EQ(color[3], 1.0f);
}