mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-08 04:08:32 +09:00
172 lines
4.6 KiB
C++
172 lines
4.6 KiB
C++
#include <gtest/gtest.h>
|
|
#include "Includes.h"
|
|
#include "MG_Impl/GLImpl/Program/GL_Program.h"
|
|
#include "MG_State/GLState/Core.h"
|
|
|
|
using namespace MobileGL;
|
|
using namespace MobileGL::MG_Impl::GLImpl;
|
|
|
|
class ProgramTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
MG_State::pGLContext = new MG_State::GLState::GLContext();
|
|
}
|
|
|
|
void TearDown() override {
|
|
delete MG_State::pGLContext;
|
|
}
|
|
};
|
|
|
|
TEST_F(ProgramTest, Sanity) {
|
|
ASSERT_TRUE(true);
|
|
}
|
|
|
|
const char* vsSrc = R"(#version 460
|
|
|
|
layout (location = 0) in vec4 Position;
|
|
in float fIn4;
|
|
in float fIn2;
|
|
in float fIn5;
|
|
in float fIn6;
|
|
in float fIn1;
|
|
in float fIn3;
|
|
|
|
layout(location = 0) uniform mat4 ProjMat;
|
|
uniform vec2 InSize;
|
|
uniform vec2 OutSize;
|
|
|
|
out vec2 texCoord;
|
|
out vec2 oneTexel;
|
|
|
|
void main(){
|
|
vec4 outPos = ProjMat * vec4(Position.xy, 0.0, 1.0);
|
|
gl_Position = vec4(outPos.xy, 0.2, 1.0);
|
|
|
|
oneTexel = (1.0 * (fIn1 * fIn2 * fIn3 * fIn4 * fIn5 * fIn6)) / InSize;
|
|
|
|
texCoord = Position.xy / OutSize;
|
|
})";
|
|
|
|
const char* fsSrc = R"(#version 460
|
|
|
|
uniform sampler2D InSampler;
|
|
|
|
in vec2 texCoord;
|
|
in vec2 oneTexel;
|
|
|
|
uniform vec2 InSize;
|
|
|
|
layout(location = 1) uniform vec3 Gray;
|
|
uniform vec3 RedMatrix;
|
|
uniform vec3 GreenMatrix0;
|
|
uniform vec3 BlueMatrix;
|
|
uniform vec3 Offset;
|
|
uniform vec3 ColorScale;
|
|
layout(location = 6) uniform float Saturation;
|
|
uniform int AQuickFoxJumpsOverALazyDog;
|
|
uniform int intVal;
|
|
|
|
out vec4 fragColor;
|
|
|
|
void main() {
|
|
vec4 InTexel = texture(InSampler, texCoord);
|
|
|
|
// Color Matrix
|
|
float RedValue = dot(InTexel.rgb, RedMatrix);
|
|
float GreenValue = dot(InTexel.rgb, GreenMatrix0);
|
|
float BlueValue = dot(InTexel.rgb, BlueMatrix);
|
|
vec3 OutColor = vec3(RedValue, GreenValue, BlueValue);
|
|
|
|
// Offset & Scale
|
|
OutColor = (OutColor * ColorScale) + Offset;
|
|
|
|
// Saturation
|
|
float Luma = dot(OutColor, Gray);
|
|
vec3 Chroma = OutColor - Luma;
|
|
OutColor = (Chroma * Saturation) + Luma;
|
|
|
|
fragColor = vec4(OutColor, float(intVal));
|
|
})";
|
|
|
|
|
|
TEST_F(ProgramTest, CompileVertex) {
|
|
GLuint vs = CreateShader(GL_VERTEX_SHADER);
|
|
ShaderSource(vs, 1, &vsSrc, NULL);
|
|
CompileShader(vs);
|
|
}
|
|
|
|
TEST_F(ProgramTest, CompileFragment) {
|
|
GLuint fs = CreateShader(GL_FRAGMENT_SHADER);
|
|
ShaderSource(fs, 1, &fsSrc, NULL);
|
|
CompileShader(fs);
|
|
}
|
|
|
|
TEST_F(ProgramTest, CompileAndLink) {
|
|
char infoLog[1024] = "";
|
|
|
|
GLuint vs = CreateShader(GL_VERTEX_SHADER);
|
|
ShaderSource(vs, 1, &vsSrc, NULL);
|
|
printf("Compiling vertex shader: %s\n", vsSrc);
|
|
CompileShader(vs);
|
|
GLint vsStatus = GL_FALSE;
|
|
GetShaderiv(vs, GL_COMPILE_STATUS, &vsStatus);
|
|
GetShaderInfoLog(vs, 1024, nullptr, infoLog);
|
|
ASSERT_EQ(vsStatus, GL_TRUE) << infoLog;
|
|
printf("Compiled vertex shader.\n");
|
|
|
|
GLuint fs = CreateShader(GL_FRAGMENT_SHADER);
|
|
ShaderSource(fs, 1, &fsSrc, NULL);
|
|
printf("Compiling fragment shader: %s\n", fsSrc);
|
|
CompileShader(fs);
|
|
GLint fsStatus = GL_FALSE;
|
|
GetShaderiv(fs, GL_COMPILE_STATUS, &fsStatus);
|
|
GetShaderInfoLog(fs, 1024, nullptr, infoLog);
|
|
ASSERT_EQ(fsStatus, GL_TRUE) << infoLog;
|
|
printf("Compiled fragment shader.\n");
|
|
|
|
GLuint program = CreateProgram();
|
|
AttachShader(program, vs);
|
|
AttachShader(program, fs);
|
|
|
|
BindAttribLocation(program, 1, "fIn1");
|
|
BindAttribLocation(program, 3, "fIn3");
|
|
BindAttribLocation(program, 5, "fIn5");
|
|
printf("Linking program...\n");
|
|
LinkProgram(program);
|
|
printf("Program linked.\n");
|
|
|
|
ASSERT_EQ(GetUniformLocation(program, "ProjMat"), 0);
|
|
ASSERT_EQ(GetUniformLocation(program, "Gray"), 1);
|
|
ASSERT_EQ(GetUniformLocation(program, "Saturation"), 6);
|
|
GLint uniformCount = 0;
|
|
GetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount);
|
|
ASSERT_EQ(uniformCount, 12);
|
|
GLint uniformNameMaxLength = 0;
|
|
GetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformNameMaxLength);
|
|
ASSERT_EQ(uniformNameMaxLength, 12);
|
|
|
|
ASSERT_EQ(GetAttribLocation(program, "Position"), 0);
|
|
ASSERT_EQ(GetAttribLocation(program, "fIn1"), 1);
|
|
ASSERT_EQ(GetAttribLocation(program, "fIn3"), 3);
|
|
ASSERT_EQ(GetAttribLocation(program, "fIn5"), 5);
|
|
|
|
UseProgram(program);
|
|
|
|
auto locRed = GetUniformLocation(program, "RedMatrix");
|
|
Uniform3f(locRed, 1.0, 3.0, 5.0);
|
|
float redVal[3];
|
|
GetUniformfv(program, locRed, redVal);
|
|
ASSERT_EQ(redVal[0], 1.0);
|
|
ASSERT_EQ(redVal[1], 3.0);
|
|
ASSERT_EQ(redVal[2], 5.0);
|
|
|
|
auto locAbc = GetUniformLocation(program, "AQuickFoxJumpsOverALazyDog");
|
|
ASSERT_EQ(locAbc, -1);
|
|
|
|
auto locInt = GetUniformLocation(program, "intVal");
|
|
Uniform1i(locInt, 114514);
|
|
int intVal;
|
|
GetUniformiv(program, locInt, &intVal);
|
|
EXPECT_EQ(intVal, 114514);
|
|
}
|