Add Phase 3: Shader, PipelineState, VertexArray tests (11 tests)

- Add Shader tests: VertexFragment, Geometry, InvalidSource, SetUniforms
- Add PipelineState tests: DepthStencilState, BlendState, Viewport/Scissor
- Add VertexArray tests: Initialize, AddVertexBuffer, SetIndexBuffer, Bind/Unbind
This commit is contained in:
2026-03-17 12:31:05 +08:00
parent 1d181f1109
commit 276a9c476a
4 changed files with 260 additions and 0 deletions

View File

@@ -25,6 +25,9 @@ set(TEST_SOURCES
test_fence.cpp
test_texture.cpp
test_sampler.cpp
test_shader.cpp
test_pipeline_state.cpp
test_vertex_array.cpp
)
add_executable(opengl_engine_tests ${TEST_SOURCES})

View File

@@ -0,0 +1,68 @@
#include "fixtures/OpenGLTestFixture.h"
#include "XCEngine/RHI/OpenGL/OpenGLPipelineState.h"
using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) {
OpenGLPipelineState pipeline;
DepthStencilState state;
state.depthTestEnable = true;
state.depthWriteEnable = true;
state.depthFunc = ComparisonFunc::Less;
pipeline.SetDepthStencilState(state);
pipeline.ApplyDepthStencil();
GLint depthTest = 0;
glGetIntegerv(GL_DEPTH_TEST, &depthTest);
EXPECT_EQ(depthTest, 1);
GLint depthMask = 0;
glGetIntegerv(GL_DEPTH_WRITEMASK, &depthMask);
EXPECT_EQ(depthMask, 1);
}
TEST_F(OpenGLTestFixture, PipelineState_SetBlendState) {
OpenGLPipelineState pipeline;
BlendState state;
state.blendEnable = true;
state.srcBlend = BlendFactor::SrcAlpha;
state.dstBlend = BlendFactor::OneMinusSrcAlpha;
pipeline.SetBlendState(state);
pipeline.ApplyBlend();
GLint blend = 0;
glGetIntegerv(GL_BLEND, &blend);
EXPECT_EQ(blend, 1);
}
TEST_F(OpenGLTestFixture, PipelineState_SetViewport_SetScissor) {
OpenGLPipelineState pipeline;
ViewportState viewport;
viewport.x = 0;
viewport.y = 0;
viewport.width = 800;
viewport.height = 600;
ScissorState scissor;
scissor.enable = true;
scissor.x = 0;
scissor.y = 0;
scissor.width = 800;
scissor.height = 600;
pipeline.SetViewport(viewport);
pipeline.SetScissor(scissor);
pipeline.ApplyViewport();
GLint viewportArr[4] = {};
glGetIntegerv(GL_VIEWPORT, viewportArr);
EXPECT_EQ(viewportArr[0], 0);
EXPECT_EQ(viewportArr[1], 0);
EXPECT_EQ(viewportArr[2], 800);
EXPECT_EQ(viewportArr[3], 600);
}

View File

@@ -0,0 +1,119 @@
#include "fixtures/OpenGLTestFixture.h"
#include "XCEngine/RHI/OpenGL/OpenGLShader.h"
using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, Shader_Compile_VertexFragment) {
const char* vertexSource = R"(
#version 330 core
in vec3 aPosition;
void main() {
gl_Position = vec4(aPosition, 1.0);
}
)";
const char* fragmentSource = R"(
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
)";
OpenGLShader shader;
bool result = shader.Compile(vertexSource, fragmentSource);
ASSERT_TRUE(result);
EXPECT_TRUE(shader.IsValid());
EXPECT_NE(shader.GetID(), 0u);
shader.Shutdown();
}
TEST_F(OpenGLTestFixture, Shader_Compile_WithGeometry) {
const char* vertexSource = R"(
#version 330 core
void main() {
gl_Position = vec4(0.0);
}
)";
const char* fragmentSource = R"(
#version 330 core
void main() {
}
)";
const char* geometrySource = R"(
#version 330 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
void main() {
for(int i = 0; i < 3; i++) {
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
)";
OpenGLShader shader;
bool result = shader.Compile(vertexSource, fragmentSource, geometrySource);
ASSERT_TRUE(result);
EXPECT_TRUE(shader.IsValid());
shader.Shutdown();
}
TEST_F(OpenGLTestFixture, Shader_Compile_InvalidSource) {
const char* invalidSource = R"(
#version 330 core
void main() {
undefined_symbol;
}
)";
OpenGLShader shader;
bool result = shader.Compile(invalidSource, "void main() { }");
EXPECT_FALSE(result);
EXPECT_FALSE(shader.IsValid());
shader.Shutdown();
}
TEST_F(OpenGLTestFixture, Shader_SetUniforms) {
const char* vs = R"(
#version 330 core
void main() { gl_Position = vec4(0.0); }
)";
const char* fs = R"(
#version 330 core
uniform int uIntValue;
uniform float uFloatValue;
uniform vec3 uVec3Value;
uniform mat4 uMat4Value;
out vec4 FragColor;
void main() { FragColor = vec4(1.0); }
)";
OpenGLShader shader;
shader.Compile(vs, fs);
ASSERT_TRUE(shader.IsValid());
shader.Use();
shader.SetInt("uIntValue", 42);
shader.SetFloat("uFloatValue", 3.14f);
shader.SetVec3("uVec3Value", 1.0f, 2.0f, 3.0f);
float mat[16] = {};
mat[0] = 1.0f; mat[5] = 1.0f; mat[10] = 1.0f; mat[15] = 1.0f;
shader.SetMat4("uMat4Value", mat);
GLenum error = glGetError();
EXPECT_EQ(error, GL_NO_ERROR);
shader.Shutdown();
}

View File

@@ -0,0 +1,70 @@
#include "fixtures/OpenGLTestFixture.h"
#include "XCEngine/RHI/OpenGL/OpenGLVertexArray.h"
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, VertexArray_Initialize_CreatesVAO) {
OpenGLVertexArray vao;
bool result = vao.Initialize();
ASSERT_TRUE(result);
EXPECT_NE(vao.GetID(), 0u);
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_AddVertexBuffer) {
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer buffer;
buffer.InitializeVertexBuffer(nullptr, 64);
VertexAttribute attr;
attr.index = 0;
attr.count = 3;
attr.type = GL_FLOAT;
attr.normalized = false;
attr.stride = sizeof(float) * 3;
attr.offset = 0;
vao.AddVertexBuffer(buffer.GetID(), attr);
EXPECT_EQ(vao.GetID(), 1u);
buffer.Shutdown();
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_SetIndexBuffer) {
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer buffer;
buffer.InitializeIndexBuffer(nullptr, 12);
vao.SetIndexBuffer(buffer.GetID(), GL_UNSIGNED_INT);
EXPECT_NE(vao.GetIndexBuffer(), 0u);
buffer.Shutdown();
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_Bind_Unbind) {
OpenGLVertexArray vao;
vao.Initialize();
vao.Bind();
GLint boundVAO = 0;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, static_cast<GLint>(vao.GetID()));
vao.Unbind();
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, 0);
vao.Shutdown();
}