Skip to content

Commit

Permalink
[NOX-76] add GLProgram unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rafal Maziejuk <[email protected]>
  • Loading branch information
rafalmaziejuk committed May 25, 2024
1 parent 0700a1e commit 4ff9cb0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
branches: [ "dev" ]

env:
DISPLAY: ":99"
Expand Down
4 changes: 2 additions & 2 deletions src/format_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct VertexAttributeFormatDescriptor {
bool isNormalized{false};
};

NOX_EXPORT [[nodiscard]] ImageFormatDescriptor getImageFormatDescriptor(ImageFormat format);
NOX_EXPORT [[nodiscard]] VertexAttributeFormatDescriptor getVertexAttributeFormatDescriptor(VertexAttributeFormat format);
[[nodiscard]] ImageFormatDescriptor NOX_EXPORT getImageFormatDescriptor(ImageFormat format);
[[nodiscard]] VertexAttributeFormatDescriptor NOX_EXPORT getVertexAttributeFormatDescriptor(VertexAttributeFormat format);

} // namespace nox
1 change: 1 addition & 0 deletions tests/unit_tests/opengl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ endif()
set(NOX_UNIT_TESTS_OPENGL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/gl_context_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gl_program_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gl_swapchain_tests.cpp
)

Expand Down
105 changes: 105 additions & 0 deletions tests/unit_tests/opengl/gl_program_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "src/opengl/gl_context.h"
#include "src/opengl/gl_program.h"
#include "src/opengl/gl_shader.h"

#include "tests/fixtures/window_fixture.h"

#include <glad/gl.h>
#include <gtest/gtest.h>

using namespace nox;

struct GLProgramFixture : public tests::WindowFixture,
public ::testing::Test {
void SetUp() override {
tests::WindowFixture::setUp();

SurfaceDescriptor surfaceDescriptor{};
surfaceDescriptor.surfaceBackendDescriptor = surfaceBackendDescriptor;
surfaceDescriptor.surfaceAttributesDescriptor = OpenGLSurfaceAttributesDescriptor{};

context = GLContext::create(surfaceDescriptor);
ASSERT_NE(nullptr, context);
}

void TearDown() override {
tests::WindowFixture::tearDown();
}

std::unique_ptr<GLContext> context{nullptr};
};

TEST_F(GLProgramFixture, WhenCreatingProgramThenProgramIsSuccessfullyCreated) {
GLProgram program{};

EXPECT_NE(0u, program.getHandle());
}

TEST_F(GLProgramFixture, GivenCorrectShadersWhenLinkingProgramThenTrueIsReturned) {
constexpr auto vertexShaderSource = R"(
#version 460 core
out gl_PerVertex {
vec4 gl_Position;
};
void main() {
gl_Position = vec4(1.0);
}
)";
constexpr auto fragmentShaderSource = R"(
#version 460 core
out vec4 fragmentColor;
void main() {
fragmentColor = vec4(1.0);
}
)";

GLProgram program{};
const GLShader vertexShader{{ShaderType::VERTEX}};
const GLShader fragmentShader{{ShaderType::FRAGMENT}};

vertexShader.compile(vertexShaderSource);
fragmentShader.compile(fragmentShaderSource);
program.attachShader(vertexShader.getHandle());
program.attachShader(fragmentShader.getHandle());

EXPECT_TRUE(program.link());
}

TEST_F(GLProgramFixture, GivenIncorrectShadersWhenLinkingProgramThenFalseIsReturned) {
constexpr auto vertexShaderSource = R"(
#version 460 core
out gl_PerVertex {
vec4 gl_Position;
};
void main() {
gl_Position = vec4(1.0);
}
)";
constexpr auto fragmentShaderSource = R"(
#version 460 core
in vec2 textureCoordinates;
out vec4 fragmentColor;
void main() {
fragmentColor = texture(fullscreenTexture, textureCoordinates);
}
)";

GLProgram program{};
const GLShader vertexShader{{ShaderType::VERTEX}};
const GLShader fragmentShader{{ShaderType::FRAGMENT}};

vertexShader.compile(vertexShaderSource);
fragmentShader.compile(fragmentShaderSource);
program.attachShader(vertexShader.getHandle());
program.attachShader(fragmentShader.getHandle());

EXPECT_FALSE(program.link());
}

0 comments on commit 4ff9cb0

Please sign in to comment.