summaryrefslogtreecommitdiff
path: root/tests/glean/tshaderapi.cpp
blob: 623562ae45f8d3de03ef5a3b72b5c2b6bc55da20 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 2009  VMware, Inc. All Rights Reserved.
// 
// 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 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 VMWARE 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.
// 
// END_COPYRIGHT


// Test GLSL-related API functions for correct behaviour
// Based on the "shader_api.c" test from Mesa, written by Bruce Merry.


#include <cstring>
#include "tshaderapi.h"
#include "rand.h"
#include "image.h"
#include "piglit-util-gl.h"


namespace GLEAN {

ShaderAPIResult::ShaderAPIResult()
{
	pass = false;
}

void
ShaderAPITest::assert_test(const char *file, int line, int cond, const char *msg)
{
	if (!cond) {
		error = true;
		fprintf(stderr, "%s:%d assertion \"%s\" failed\n", file, line, msg);
	}
}

#undef assert
#define assert(x) assert_test(__FILE__, __LINE__, (x), #x)


void
ShaderAPITest::assert_no_error_test(const char *file, int line)
{
	GLenum err;

	err = glGetError();
	if (err != GL_NO_ERROR) {
		error = true;
		fprintf(stderr, "%s:%d received error %s\n",
				file, line, piglit_get_gl_error_name(err));
	}
}

#define assert_no_error() assert_no_error_test(__FILE__, __LINE__)


void
ShaderAPITest::assert_error_test(const char *file, int line, GLenum expect)
{
	GLenum err;

	err = glGetError();
	if (err != expect) {
		fprintf(stderr, "%s:%d expected %s but received %s\n",
				file, line, piglit_get_gl_error_name(expect), piglit_get_gl_error_name(err));
		error = true;
	}

	while (glGetError())
		; /* consume any following errors */
}

#define assert_error(err) assert_error_test(__FILE__, __LINE__, (err))


void
ShaderAPITest::check_status(GLuint id, GLenum pname,
			    void (APIENTRY *query)(GLuint, GLenum, GLint *),
			    void (APIENTRY *get_log)(GLuint, GLsizei, GLsizei *, GLchar *))
{
	GLint status;

	query(id, pname, &status);
	if (!status) {
		char info[65536];

		fprintf(stderr, "Compilation/link failure:\n");
		get_log(id, sizeof(info), NULL, info);
		fprintf(stderr, "%s\n", info);

		error = true;
	}
}


void
ShaderAPITest::check_compile_status(GLuint id)
{
	check_status(id, GL_COMPILE_STATUS, glGetShaderiv,
		     glGetShaderInfoLog);
}


void
ShaderAPITest::check_link_status(GLuint id)
{
	check_status(id, GL_LINK_STATUS, glGetProgramiv,
		     glGetProgramInfoLog);
}


GLuint
ShaderAPITest::make_shader(GLenum type, const char *src)
{
	GLuint id;

	assert_no_error();
	id = glCreateShader(type);
	glShaderSource(id, 1, &src, NULL);
	glCompileShader(id);
	check_compile_status(id);
	assert_no_error();
	return id;
}


GLuint
ShaderAPITest::make_program(const char *vs_src, const char *fs_src)
{
	GLuint id, vs, fs;

	assert_no_error();
	id = glCreateProgram();
	if (vs_src) {
		vs = make_shader(GL_VERTEX_SHADER, vs_src);
		glAttachShader(id, vs);
		glDeleteShader(vs);
	}
	if (fs_src) {
		fs = make_shader(GL_FRAGMENT_SHADER, fs_src);
		glAttachShader(id, fs);
		glDeleteShader(fs);
	}
	glLinkProgram(id);
	check_link_status(id);
	glUseProgram(id);
	glDeleteProgram(id);
	assert_no_error();
	return id;
}


void
ShaderAPITest::test_uniform_size_type1(const char *glslType, GLenum glType, const char *el)
{
	char buffer[1024];
	GLuint program;
	GLint active, i;

	//printf("  Running subtest %s\n", glslType);
	//fflush(stdout);
	sprintf(buffer, "#version 120\nuniform %s m[60];\nvoid main() { gl_Position[0] = m[59]%s; }\n",
			glslType, el);
	
	program = make_program(buffer, NULL);
	glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active);
	assert_no_error();
	for (i = 0; i < active; i++) {
		GLint size = -1;
		GLenum type = 0;
		glGetActiveUniform(program, i, sizeof(buffer), NULL,
								&size, &type, buffer);
		assert_no_error();
		assert(type == glType);
		assert(size == 60);
		if (strncmp(buffer, "m", 1) == 0)
			break;
	}
}


void
ShaderAPITest::test_uniform_size_type(void)
{
	test_uniform_size_type1("float", GL_FLOAT, "");
	test_uniform_size_type1("vec2", GL_FLOAT_VEC2, "[0]");
	test_uniform_size_type1("vec3", GL_FLOAT_VEC3, "[0]");
	test_uniform_size_type1("vec4", GL_FLOAT_VEC4, "[0]");

	test_uniform_size_type1("bool", GL_BOOL, " ? 1.0 : 0.0");
	test_uniform_size_type1("bvec2", GL_BOOL_VEC2, "[0] ? 1.0 : 0.0");
	test_uniform_size_type1("bvec3", GL_BOOL_VEC3, "[0] ? 1.0 : 0.0");
	test_uniform_size_type1("bvec4", GL_BOOL_VEC4, "[0] ? 1.0 : 0.0");

	test_uniform_size_type1("int", GL_INT, "");
	test_uniform_size_type1("ivec2", GL_INT_VEC2, "[0]");
	test_uniform_size_type1("ivec3", GL_INT_VEC3, "[0]");
	test_uniform_size_type1("ivec4", GL_INT_VEC4, "[0]");

	test_uniform_size_type1("mat2", GL_FLOAT_MAT2, "[0][0]");
	test_uniform_size_type1("mat3", GL_FLOAT_MAT3, "[0][0]");
	test_uniform_size_type1("mat4", GL_FLOAT_MAT4, "[0][0]");
	test_uniform_size_type1("mat2x3", GL_FLOAT_MAT2x3, "[0][0]");
	test_uniform_size_type1("mat2x4", GL_FLOAT_MAT2x4, "[0][0]");
	test_uniform_size_type1("mat3x2", GL_FLOAT_MAT3x2, "[0][0]");
	test_uniform_size_type1("mat3x4", GL_FLOAT_MAT3x4, "[0][0]");
	test_uniform_size_type1("mat4x2", GL_FLOAT_MAT4x2, "[0][0]");
	test_uniform_size_type1("mat4x3", GL_FLOAT_MAT4x3, "[0][0]");
}


void
ShaderAPITest::test_attrib_size_type1(const char *glslType, GLenum glType, const char *el)
{
	char buffer[1024];
	GLuint program;
	GLint active, i;

	//printf("  Running subtest %s\n", glslType);
	//fflush(stdout);
	sprintf(buffer, "#version 120\nattribute %s m;\nvoid main() { gl_Position[0] = m%s; }\n",
			glslType, el);

	program = make_program(buffer, NULL);
	glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active);
	assert_no_error();
	for (i = 0; i < active; i++) {
		GLint size = -1;
		GLenum type = 0;
		glGetActiveAttrib(program, i, sizeof(buffer), NULL,
							   &size, &type, buffer);
		assert_no_error();
		assert(type == glType);
		assert(size == 1);
		if (strncmp(buffer, "m", 1) == 0)
			break;
	}
	assert(i < active); /* Otherwise the compiler optimised it out */
}


void
ShaderAPITest::test_attrib_size_type(void)
{
	test_attrib_size_type1("float", GL_FLOAT, "");
	test_attrib_size_type1("vec2", GL_FLOAT_VEC2, "[0]");
	test_attrib_size_type1("vec3", GL_FLOAT_VEC3, "[0]");
	test_attrib_size_type1("vec4", GL_FLOAT_VEC4, "[0]");
	test_attrib_size_type1("mat2", GL_FLOAT_MAT2, "[0][0]");
	test_attrib_size_type1("mat3", GL_FLOAT_MAT3, "[0][0]");
	test_attrib_size_type1("mat4", GL_FLOAT_MAT4, "[0][0]");
	test_attrib_size_type1("mat2x3", GL_FLOAT_MAT2x3, "[0][0]");
	test_attrib_size_type1("mat2x4", GL_FLOAT_MAT2x4, "[0][0]");
	test_attrib_size_type1("mat3x2", GL_FLOAT_MAT3x2, "[0][0]");
	test_attrib_size_type1("mat3x4", GL_FLOAT_MAT3x4, "[0][0]");
	test_attrib_size_type1("mat4x2", GL_FLOAT_MAT4x2, "[0][0]");
	test_attrib_size_type1("mat4x3", GL_FLOAT_MAT4x3, "[0][0]");
}


void
ShaderAPITest::test_uniform_array_overflow(void)
{
	GLuint program;
	GLint location;
	GLfloat data[128];

	program = make_program("#version 120\nuniform vec2 x[10];\nvoid main() { gl_Position.xy = x[9]; }\n", NULL);
	location = glGetUniformLocation(program, "x");
	assert_no_error();
	glUniform2fv(location, 64, data);
	assert_no_error();
}


void
ShaderAPITest::test_uniform_scalar_count(void)
{
	GLuint program;
	GLint location;
	GLfloat data[128];

	program = make_program("#version 110\nuniform vec2 x;\nvoid main() { gl_Position.xy = x; }\n", NULL);
	location = glGetUniformLocation(program, "x");
	assert_no_error();
	glUniform2fv(location, 64, data);
	assert_error(GL_INVALID_OPERATION);
}


void
ShaderAPITest::test_uniform_query_matrix(void)
{
	GLuint program;
	GLfloat data[18];
	GLint i, r, c;
	GLint location;

	program = make_program("#version 110\nuniform mat3 m[2];\nvoid main() { gl_Position.xyz = m[1][2]; }\n", NULL);
	location = glGetUniformLocation(program, "m");
	for (i = 0; i < 9; i++)
		data[i] = i;
	for (i = 9; i < 18; i++)
		data[i] = 321.0;
	glUniformMatrix3fv(location, 1, GL_TRUE, data);

	for (i = 0; i < 18; i++)
		data[i] = 123.0;
	glGetUniformfv(program, location, data);
	for (c = 0; c < 3; c++)
		for (r = 0; r < 3; r++)
			assert(data[c * 3 + r] == r * 3 + c);
	for (i = 9; i < 18; i++)
		assert(data[i] == 123.0);
}


void
ShaderAPITest::test_uniform_neg_location(void)
{
	GLuint program;
	GLfloat data[4];

	program = make_program("#version 110\nvoid main() { gl_Position = vec4(1.0, 1.0, 1.0, 1.0); }\n", NULL);
	(void) program;
	assert_no_error();
	glUniform1i(-1, 1);
	assert_no_error();
	glUniform1i(-200, 1);
	assert_error(GL_INVALID_OPERATION);
	glUniformMatrix2fv(-1, 1, GL_FALSE, data);
	assert_no_error();
	glUniformMatrix2fv(-200, 1, GL_FALSE, data);
	assert_error(GL_INVALID_OPERATION);
}


void
ShaderAPITest::test_uniform_bool_conversion(void)
{
	GLuint program;
	GLint location;
	GLint value[16];  /* in case glGetUniformiv goes nuts on the stack */

	assert_no_error();
	program = make_program("uniform bool b;\nvoid main() { gl_Position.x = b ? 1.5 : 0.5; }\n", NULL);
	location = glGetUniformLocation(program, "b");
	assert(location != -1);
	assert_no_error();
	glUniform1i(location, 5);
	assert_no_error();
	glGetUniformiv(program, location, &value[0]);
	assert_no_error();
	assert(value[0] == 1);
}


void
ShaderAPITest::test_uniform_multiple_samplers(void)
{
   GLuint program;
   GLint location;
   GLint values[2] = {0, 1};

   assert_no_error();
   program = make_program(NULL, "uniform sampler2D s[2];\nvoid main() { gl_FragColor = texture2D(s[1], vec2(0.0, 0.0)); }\n");
   location = glGetUniformLocation(program, "s[0]");
   assert(location != -1);
   assert_no_error();
   glUniform1iv(location, 2, values);
   assert_no_error();
}


void
ShaderAPITest::run_tests(void)
{
	test_uniform_size_type();
	test_attrib_size_type();
	test_uniform_array_overflow();
	test_uniform_scalar_count();
	test_uniform_query_matrix();
	test_uniform_neg_location();
	test_uniform_bool_conversion();
	test_uniform_multiple_samplers();
}


void
ShaderAPITest::runOne(ShaderAPIResult &r, Window &w)
{
	(void) w;  // silence warning

	// error will be set to true if any of the assert functions below fail.
	error = false;

	run_tests();

	r.pass = !error;
}


void
ShaderAPITest::logOne(ShaderAPIResult &r)
{
	if (r.pass) {
		logPassFail(r);
		logConcise(r);
	}
	else {
		env->log << name << "FAIL\n";
	}
}


void
ShaderAPIResult::putresults(ostream &s) const
{
	if (pass) {
		s << "PASS\n";
	}
	else {
		s << "FAIL\n";
	}
}


bool
ShaderAPIResult::getresults(istream &s)
{
	char result[1000];
	s >> result;

	if (strcmp(result, "FAIL") == 0) {
		pass = false;
	}
	else {
		pass = true;
	}
	return s.good();
}


// We need OpenGL 2.0, 2.1 or 3.0
bool
ShaderAPITest::isApplicable() const
{
	if (GLUtils::getVersion() >= 2.0) {
		return true;
	}
	else {
		env->log << name
				 << ":  skipped.  Requires GL >= 2.0.\n";
		return false;
	}
}


// The test object itself:
ShaderAPITest shaderAPITest("shaderAPI", "window, rgb",
                            "",  // no extensions, but see isApplicable()
                            "Test GLSL shader-related API features.\n");



} // namespace GLEAN