Bruce Merry | 239be83 | 2007-12-21 15:23:39 +0200 | [diff] [blame^] | 1 | /* Tests to validate fixes to various bugs in src/mesa/shader/shader_api.c |
| 2 | * |
| 3 | * Written by Bruce Merry |
| 4 | */ |
| 5 | #include <string.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #define GL_GLEXT_PROTOTYPES |
| 9 | #include <GL/glut.h> |
| 10 | |
| 11 | static void assert_test(const char *file, int line, int cond, const char *msg) |
| 12 | { |
| 13 | if (!cond) |
| 14 | fprintf(stderr, "%s:%d assertion \"%s\" failed\n", file, line, msg); |
| 15 | } |
| 16 | |
| 17 | #undef assert |
| 18 | #define assert(x) assert_test(__FILE__, __LINE__, (x), #x) |
| 19 | |
| 20 | static void assert_no_error_test(const char *file, int line) |
| 21 | { |
| 22 | GLenum err; |
| 23 | |
| 24 | err = glGetError(); |
| 25 | if (err != GL_NO_ERROR) |
| 26 | fprintf(stderr, "%s:%d received error %s\n", |
| 27 | file, line, gluErrorString(err)); |
| 28 | } |
| 29 | |
| 30 | #define assert_no_error() assert_no_error_test(__FILE__, __LINE__) |
| 31 | |
| 32 | static void assert_error_test(const char *file, int line, GLenum expect) |
| 33 | { |
| 34 | GLenum err; |
| 35 | |
| 36 | err = glGetError(); |
| 37 | if (err != expect) |
| 38 | fprintf(stderr, "%s:%d expected %s but received %s\n", |
| 39 | file, line, gluErrorString(expect), gluErrorString(err)); |
| 40 | while (glGetError()); /* consume any following errors */ |
| 41 | } |
| 42 | |
| 43 | #define assert_error(err) assert_error_test(__FILE__, __LINE__, (err)) |
| 44 | |
| 45 | static void check_status(GLuint id, GLenum pname, void (*query)(GLuint, GLenum, GLint *)) |
| 46 | { |
| 47 | GLint status; |
| 48 | |
| 49 | query(id, pname, &status); |
| 50 | if (!status) |
| 51 | { |
| 52 | char info[1024]; |
| 53 | |
| 54 | fprintf(stderr, "Compilation/link failure:\n"); |
| 55 | glGetInfoLogARB(id, sizeof(info), NULL, info); |
| 56 | fprintf(stderr, "%s\n", info); |
| 57 | exit(1); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void check_compile_status(GLuint id) |
| 62 | { |
| 63 | check_status(id, GL_COMPILE_STATUS, glGetShaderiv); |
| 64 | } |
| 65 | |
| 66 | static void check_link_status(GLuint id) |
| 67 | { |
| 68 | check_status(id, GL_LINK_STATUS, glGetProgramiv); |
| 69 | } |
| 70 | |
| 71 | static GLuint make_shader(GLenum type, const char *src) |
| 72 | { |
| 73 | GLuint id; |
| 74 | |
| 75 | assert_no_error(); |
| 76 | id = glCreateShader(type); |
| 77 | glShaderSource(id, 1, &src, NULL); |
| 78 | glCompileShader(id); |
| 79 | check_compile_status(id); |
| 80 | assert_no_error(); |
| 81 | return id; |
| 82 | } |
| 83 | |
| 84 | static GLuint make_program(const char *vs_src, const char *fs_src) |
| 85 | { |
| 86 | GLuint id, vs, fs; |
| 87 | |
| 88 | assert_no_error(); |
| 89 | id = glCreateProgram(); |
| 90 | if (vs_src) { |
| 91 | vs = make_shader(GL_VERTEX_SHADER, vs_src); |
| 92 | glAttachShader(id, vs); |
| 93 | glDeleteShader(vs); |
| 94 | } |
| 95 | if (fs_src) { |
| 96 | fs = make_shader(GL_FRAGMENT_SHADER, fs_src); |
| 97 | glAttachShader(id, fs); |
| 98 | glDeleteShader(fs); |
| 99 | } |
| 100 | glLinkProgram(id); |
| 101 | check_link_status(id); |
| 102 | glUseProgram(id); |
| 103 | glDeleteProgram(id); |
| 104 | assert_no_error(); |
| 105 | return id; |
| 106 | } |
| 107 | |
| 108 | static void test_uniform_size_type1(const char *glslType, GLenum glType, const char *el) |
| 109 | { |
| 110 | char buffer[1024]; |
| 111 | GLuint program; |
| 112 | GLint active, i; |
| 113 | GLenum type; |
| 114 | GLint size; |
| 115 | |
| 116 | printf(" Running subtest %s\n", glslType); fflush(stdout); |
| 117 | sprintf(buffer, "#version 120\nuniform %s m[60];\nvoid main() { gl_Position[0] = m[59]%s; }\n", |
| 118 | glslType, el); |
| 119 | |
| 120 | program = make_program(buffer, NULL); |
| 121 | glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active); |
| 122 | assert_no_error(); |
| 123 | for (i = 0; i < active; i++) { |
| 124 | size = -1; |
| 125 | type = 0; |
| 126 | glGetActiveUniform(program, i, sizeof(buffer), NULL, &size, &type, buffer); |
| 127 | assert_no_error(); |
| 128 | if (strncmp(buffer, "m", 1) == 0) |
| 129 | break; |
| 130 | } |
| 131 | assert(i < active); /* Otherwise the compiler optimised it out */ |
| 132 | assert(type == glType); |
| 133 | assert(size == 60); |
| 134 | } |
| 135 | |
| 136 | static void test_uniform_size_type(void) |
| 137 | { |
| 138 | test_uniform_size_type1("float", GL_FLOAT, ""); |
| 139 | test_uniform_size_type1("vec2", GL_FLOAT_VEC2, "[0]"); |
| 140 | test_uniform_size_type1("vec3", GL_FLOAT_VEC3, "[0]"); |
| 141 | test_uniform_size_type1("vec4", GL_FLOAT_VEC4, "[0]"); |
| 142 | |
| 143 | test_uniform_size_type1("bool", GL_BOOL, " ? 1.0 : 0.0"); |
| 144 | test_uniform_size_type1("bvec2", GL_BOOL_VEC2, "[0] ? 1.0 : 0.0"); |
| 145 | test_uniform_size_type1("bvec3", GL_BOOL_VEC3, "[0] ? 1.0 : 0.0"); |
| 146 | test_uniform_size_type1("bvec4", GL_BOOL_VEC4, "[0] ? 1.0 : 0.0"); |
| 147 | |
| 148 | test_uniform_size_type1("int", GL_INT, ""); |
| 149 | test_uniform_size_type1("ivec2", GL_INT_VEC2, "[0]"); |
| 150 | test_uniform_size_type1("ivec3", GL_INT_VEC3, "[0]"); |
| 151 | test_uniform_size_type1("ivec4", GL_INT_VEC4, "[0]"); |
| 152 | |
| 153 | test_uniform_size_type1("mat2", GL_FLOAT_MAT2, "[0][0]"); |
| 154 | test_uniform_size_type1("mat3", GL_FLOAT_MAT3, "[0][0]"); |
| 155 | test_uniform_size_type1("mat4", GL_FLOAT_MAT4, "[0][0]"); |
| 156 | test_uniform_size_type1("mat2x3", GL_FLOAT_MAT2x3, "[0][0]"); |
| 157 | test_uniform_size_type1("mat2x4", GL_FLOAT_MAT2x4, "[0][0]"); |
| 158 | test_uniform_size_type1("mat3x2", GL_FLOAT_MAT3x2, "[0][0]"); |
| 159 | test_uniform_size_type1("mat3x4", GL_FLOAT_MAT3x4, "[0][0]"); |
| 160 | test_uniform_size_type1("mat4x2", GL_FLOAT_MAT4x2, "[0][0]"); |
| 161 | test_uniform_size_type1("mat4x3", GL_FLOAT_MAT4x3, "[0][0]"); |
| 162 | } |
| 163 | |
| 164 | static void test_attrib_size_type1(const char *glslType, GLenum glType, const char *el) |
| 165 | { |
| 166 | char buffer[1024]; |
| 167 | GLuint program; |
| 168 | GLint active, i; |
| 169 | GLenum type; |
| 170 | GLint size; |
| 171 | |
| 172 | printf(" Running subtest %s\n", glslType); fflush(stdout); |
| 173 | sprintf(buffer, "#version 120\nattribute %s m;\nvoid main() { gl_Position[0] = m%s; }\n", |
| 174 | glslType, el); |
| 175 | |
| 176 | program = make_program(buffer, NULL); |
| 177 | glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active); |
| 178 | assert_no_error(); |
| 179 | for (i = 0; i < active; i++) { |
| 180 | size = -1; |
| 181 | type = -1; |
| 182 | glGetActiveAttrib(program, i, sizeof(buffer), NULL, &size, &type, buffer); |
| 183 | assert_no_error(); |
| 184 | if (strncmp(buffer, "m", 1) == 0) |
| 185 | break; |
| 186 | } |
| 187 | assert(i < active); /* Otherwise the compiler optimised it out */ |
| 188 | assert(type == glType); |
| 189 | assert(size == 1); |
| 190 | } |
| 191 | |
| 192 | static void test_attrib_size_type(void) |
| 193 | { |
| 194 | test_attrib_size_type1("float", GL_FLOAT, ""); |
| 195 | test_attrib_size_type1("vec2", GL_FLOAT_VEC2, "[0]"); |
| 196 | test_attrib_size_type1("vec3", GL_FLOAT_VEC3, "[0]"); |
| 197 | test_attrib_size_type1("vec4", GL_FLOAT_VEC4, "[0]"); |
| 198 | |
| 199 | test_attrib_size_type1("mat2", GL_FLOAT_MAT2, "[0][0]"); |
| 200 | test_attrib_size_type1("mat3", GL_FLOAT_MAT3, "[0][0]"); |
| 201 | test_attrib_size_type1("mat4", GL_FLOAT_MAT4, "[0][0]"); |
| 202 | test_attrib_size_type1("mat2x3", GL_FLOAT_MAT2x3, "[0][0]"); |
| 203 | test_attrib_size_type1("mat2x4", GL_FLOAT_MAT2x4, "[0][0]"); |
| 204 | test_attrib_size_type1("mat3x2", GL_FLOAT_MAT3x2, "[0][0]"); |
| 205 | test_attrib_size_type1("mat3x4", GL_FLOAT_MAT3x4, "[0][0]"); |
| 206 | test_attrib_size_type1("mat4x2", GL_FLOAT_MAT4x2, "[0][0]"); |
| 207 | test_attrib_size_type1("mat4x3", GL_FLOAT_MAT4x3, "[0][0]"); |
| 208 | } |
| 209 | |
| 210 | static void test_uniform_array_overflow(void) |
| 211 | { |
| 212 | GLuint program; |
| 213 | GLint location; |
| 214 | GLfloat data[128]; |
| 215 | |
| 216 | program = make_program("#version 120\nuniform vec2 x[10];\nvoid main() { gl_Position.xy = x[9]; }\n", NULL); |
| 217 | location = glGetUniformLocation(program, "x"); |
| 218 | assert_no_error(); |
| 219 | glUniform2fv(location, 64, data); |
| 220 | assert_no_error(); |
| 221 | } |
| 222 | |
| 223 | static void test_uniform_scalar_count(void) |
| 224 | { |
| 225 | GLuint program; |
| 226 | GLint location; |
| 227 | GLfloat data[128]; |
| 228 | |
| 229 | program = make_program("#version 110\nuniform vec2 x;\nvoid main() { gl_Position.xy = x; }\n", NULL); |
| 230 | location = glGetUniformLocation(program, "x"); |
| 231 | assert_no_error(); |
| 232 | glUniform2fv(location, 64, data); |
| 233 | assert_error(GL_INVALID_OPERATION); |
| 234 | } |
| 235 | |
| 236 | static void test_uniform_query_matrix(void) |
| 237 | { |
| 238 | GLuint program; |
| 239 | GLfloat data[18]; |
| 240 | GLint i, r, c; |
| 241 | GLint location; |
| 242 | |
| 243 | program = make_program("#version 110\nuniform mat3 m[2];\nvoid main() { gl_Position.xyz = m[1][2]; }\n", NULL); |
| 244 | location = glGetUniformLocation(program, "m"); |
| 245 | for (i = 0; i < 9; i++) |
| 246 | data[i] = i; |
| 247 | for (i = 9; i < 18; i++) |
| 248 | data[i] = 321.0; |
| 249 | glUniformMatrix3fv(location, 1, GL_TRUE, data); |
| 250 | |
| 251 | for (i = 0; i < 18; i++) |
| 252 | data[i] = 123.0; |
| 253 | glGetUniformfv(program, location, data); |
| 254 | for (c = 0; c < 3; c++) |
| 255 | for (r = 0; r < 3; r++) |
| 256 | assert(data[c * 3 + r] == r * 3 + c); |
| 257 | for (i = 9; i < 18; i++) |
| 258 | assert(data[i] == 123.0); |
| 259 | } |
| 260 | |
| 261 | static void test_uniform_neg_location(void) |
| 262 | { |
| 263 | GLuint program; |
| 264 | GLfloat data[4]; |
| 265 | |
| 266 | program = make_program("#version 110\nvoid main() { gl_Position = vec4(1.0, 1.0, 1.0, 1.0); }\n", NULL); |
| 267 | assert_no_error(); |
| 268 | glUniform1i(-1, 1); |
| 269 | assert_no_error(); |
| 270 | glUniform1i(-200, 1); |
| 271 | assert_error(GL_INVALID_OPERATION); |
| 272 | glUniformMatrix2fv(-1, 1, GL_FALSE, data); |
| 273 | assert_no_error(); |
| 274 | glUniformMatrix2fv(-200, 1, GL_FALSE, data); |
| 275 | } |
| 276 | |
| 277 | static void run_test(const char *name, void (*callback)(void)) |
| 278 | { |
| 279 | printf("Running %s\n", name); |
| 280 | fflush(stdout); |
| 281 | callback(); |
| 282 | } |
| 283 | |
| 284 | #define RUN_TEST(name) run_test(#name, (name)) |
| 285 | |
| 286 | int main(int argc, char **argv) |
| 287 | { |
| 288 | glutInit(&argc, argv); |
| 289 | glutCreateWindow("Mesa bug demo"); |
| 290 | |
| 291 | RUN_TEST(test_uniform_size_type); |
| 292 | RUN_TEST(test_attrib_size_type); |
| 293 | RUN_TEST(test_uniform_array_overflow); |
| 294 | RUN_TEST(test_uniform_scalar_count); |
| 295 | RUN_TEST(test_uniform_query_matrix); |
| 296 | RUN_TEST(test_uniform_neg_location); |
| 297 | return 0; |
| 298 | } |