1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright © 2017 Fabian Bieler
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/**
* @file built-in-matrix-state.c: Access uniform matrix derived state in GLSL
*
* Set coordiante transformation matrices with the OpenGL API and access them
* and their derived uniforms in a GLSL shader.
*/
#include "piglit-util-gl.h"
#include "piglit-matrix.h"
PIGLIT_GL_TEST_CONFIG_BEGIN
config.supports_gl_compat_version = 20;
config.window_visual = PIGLIT_GL_VISUAL_RGB;
config.khr_no_error_support = PIGLIT_NO_ERRORS;
PIGLIT_GL_TEST_CONFIG_END
#ifdef _WIN32
#define SRAND(x) srand(x)
#define DRAND() ((float)rand() / RAND_MAX)
#else
#define SRAND(x) srand48(x)
#define DRAND() drand48()
#endif
/**
* Compute the transpose inverse of the 4x4 matrix \m and return the upper
* left 3x3 block matrix in \out.
*/
static void
compute_normal_matrix(float out[9], const float m[16])
{
float m_inv[16], m_inv_T[16];
piglit_matrix_inverse(m_inv, m);
piglit_matrix_transpose(m_inv_T, m_inv);
out[0] = m_inv_T[0];
out[1] = m_inv_T[1];
out[2] = m_inv_T[2];
out[3] = m_inv_T[4];
out[4] = m_inv_T[5];
out[5] = m_inv_T[6];
out[6] = m_inv_T[8];
out[7] = m_inv_T[9];
out[8] = m_inv_T[10];
}
static const char *vs_text =
"void main() {\n"
" gl_Position = gl_Vertex;\n"
"}\n";
static const char *fs_mat4 =
"void main() {\n"
" vec4 epsilon = vec4(1.0 / 256.0);\n"
" vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
" vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
" mat4 a = %s;\n"
" mat4 b = mat4(%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, "
"%f, %f, %f, %f);\n"
" bool pass = true;\n"
" pass = pass && all(lessThan(abs(a[0] - b[0]), epsilon));\n"
" pass = pass && all(lessThan(abs(a[1] - b[1]), epsilon));\n"
" pass = pass && all(lessThan(abs(a[2] - b[2]), epsilon));\n"
" pass = pass && all(lessThan(abs(a[3] - b[3]), epsilon));\n"
" gl_FragColor = pass ? green : red;\n"
"}\n";
static const char *fs_mat3 =
"void main() {\n"
" vec3 epsilon = vec3(1.0 / 256.0);\n"
" vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
" vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
" mat3 a = %s;\n"
" mat3 b = mat3(%f, %f, %f, %f, %f, %f, %f, %f, %f);\n"
" bool pass = true;\n"
" pass = pass && all(lessThan(abs(a[0] - b[0]), epsilon));\n"
" pass = pass && all(lessThan(abs(a[1] - b[1]), epsilon));\n"
" pass = pass && all(lessThan(abs(a[2] - b[2]), epsilon));\n"
" gl_FragColor = pass ? green : red;\n"
"}\n";
static const char *fs_float =
"void main() {\n"
" float epsilon = (1.0 / 256.0);\n"
" vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
" vec4 red = vec4(1.0, 0.0, 0.0, 1.0);\n"
" gl_FragColor = abs(%s - %f) < epsilon ? green : red;\n"
"}\n";
/**
* Check that the built-in shader uniform \name of type \type is equal to \m.
*
* Since we also test for derived state involving floating point computation
* don't test for strict equality but rather only check if the uniform's
* components are within and espilon of their expected values.
*/
static bool
check_shader_builtin(const GLenum type, const float *m, const char *name)
{
char *fs_text;
const float green[3] = {0.0, 1.0, 0.0};
switch(type) {
case GL_FLOAT:
asprintf(&fs_text, fs_float, name, m[0]);
break;
case GL_FLOAT_MAT4:
asprintf(&fs_text, fs_mat4, name, m[0], m[1], m[2], m[3],
m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11],
m[12], m[13], m[14], m[15]);
break;
case GL_FLOAT_MAT3:
asprintf(&fs_text, fs_mat3, name, m[0], m[1], m[2], m[3],
m[4], m[5], m[6], m[7], m[8], m[9]);
break;
default:
assert(0);
}
const GLuint program = piglit_build_simple_program(vs_text, fs_text);
free(fs_text);
glUseProgram(program);
glDeleteProgram(program);
glClear(GL_COLOR_BUFFER_BIT);
piglit_draw_rect(-1, -1, 2, 2);
if (piglit_probe_pixel_rgb_silent(piglit_width / 2, piglit_height / 2,
green, NULL))
return true;
printf("Failed uniform: '%s'.\n", name);
return false;
}
/**
* Load random 16 floats between 0 and 1 int matrix \pname
* and return them in \m.
*/
static void
load_matrix(float m[16], const GLenum pname)
{
glMatrixMode(pname);
for (int i = 0; i < 16; ++i)
m[i] = DRAND();
glLoadMatrixf(m);
}
/**
* Check that matrix \pname contains the values \m.
* if \idx is zero or positive add it as and index to the matrix array.
* Also check the matrix' transpose, inverse and transpose inverse.
*/
static bool
check_matrix_variants(const char *prefix, const float m[16], const int idx)
{
bool pass = true;
char *name, *name_T, *name_inv, *name_inv_T;
float m_T[16], m_inv[16], m_inv_T[16];
if (idx >= 0) {
asprintf(&name, "%s[%d]", prefix, idx);
asprintf(&name_T, "%sTranspose[%d]", prefix, idx);
asprintf(&name_inv, "%sInverse[%d]", prefix, idx);
asprintf(&name_inv_T, "%sInverseTranspose[%d]", prefix, idx);
} else {
asprintf(&name, "%s", prefix);
asprintf(&name_T, "%sTranspose", prefix);
asprintf(&name_inv, "%sInverse", prefix);
asprintf(&name_inv_T, "%sInverseTranspose", prefix);
}
piglit_matrix_transpose(m_T, m);
piglit_matrix_inverse(m_inv, m);
piglit_matrix_transpose(m_inv_T, m_inv);
pass = check_shader_builtin(GL_FLOAT_MAT4, m, name) && pass;
pass = check_shader_builtin(GL_FLOAT_MAT4, m_T, name_T) && pass;
pass = check_shader_builtin(GL_FLOAT_MAT4, m_inv, name_inv) && pass;
pass = check_shader_builtin(GL_FLOAT_MAT4, m_inv_T, name_inv_T) &&
pass;
free(name);
free(name_T);
free(name_inv);
free(name_inv_T);
return pass;
}
/**
* Load random data in matrix \pname and check it by it's shader name \name
* with (optional) index \idx.
*/
static bool
load_and_test_matrix(const char *name, const GLenum pname, const int idx)
{
float mat[16];
load_matrix(mat, pname);
return check_matrix_variants(name, mat, idx);
}
enum piglit_result
piglit_display(void)
{
bool pass = true;
/* Test modelview and projection matrices. */
pass = load_and_test_matrix("gl_ModelViewMatrix", GL_MODELVIEW, -1) &&
pass;
pass = load_and_test_matrix("gl_ProjectionMatrix", GL_PROJECTION, -1)
&& pass;
/* Test modelview-projection matrix. */
float mvp[16], proj[16], mview[16];
load_matrix(mview, GL_MODELVIEW);
load_matrix(proj, GL_PROJECTION);
piglit_matrix_mul_matrix(mvp, proj, mview);
pass = check_matrix_variants("gl_ModelViewProjectionMatrix", mvp, -1)
&& pass;
/* Test texture matrices. */
int max_texture_coords;
glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
for (int t = 0; t < max_texture_coords; ++t) {
glActiveTexture(GL_TEXTURE0 + t);
pass = load_and_test_matrix("gl_TextureMatrix",
GL_TEXTURE, t) && pass;
}
/* Test normal matrix. */
float norm[9] = {};
load_matrix(mview, GL_MODELVIEW);
compute_normal_matrix(norm, mview);
pass = check_shader_builtin(GL_FLOAT_MAT3, norm, "gl_NormalMatrix")
&& pass;
/* Test normal scale factor.
* Page 49 (63 of the PDF) of the OpenGL 2.0 spec says:
*
* "Rescale multiplies the transformed normals by a scale factor
* [f] [...] If rescaling is disabled, then f = 1."
*
* I'm unsure if this affacts the shader's built-in uniform, but
* enable normal rescaling just in case.
*/
glEnable(GL_RESCALE_NORMAL);
float ns = norm[6] * norm[6] + norm[7] * norm[7] + norm[8] * norm[8];
ns = 1 / sqrt(ns);
pass = check_shader_builtin(GL_FLOAT, &ns, "gl_NormalScale") && pass;
return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}
void
piglit_init(int argc, char **argv)
{
SRAND(17);
}
|