1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Based on blending-in-shader.c, by Marek Olšák
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 */
26
27 /** @file blending-in-shader-arb.c
28 *
29 * Test programmable blending with GL_ARB_texture_barrier.
30 *
31 * This test is equivalent to blending-in-shader.c, but:
32 *
33 * * Tests GL_ARB_texture_barrier instead of GL_NV_texture_barrier,
34 * that are totally equivalent.
35 *
36 * * Uses GL_ARB_framebuffer_object instead of
37 * GL_EXT_framebuffer_object. Those are slightly different, and
38 * several drivers doesn't support the EXT one.
39 *
40 * * This tests switches to use an integer texture, because that would
41 * allow us to do actual vs reference comparisons without the need
42 * of a tolerance.
43 *
44 * * Allows to parametrize several aspects: resolution, number of
45 * blending passes, number of drawing passes, square granularity
46 * (triangle count) and number of textures.
47 */
48
49 #include "piglit-util-gl.h"
50
51 PIGLIT_GL_TEST_CONFIG_BEGIN
52
53 config.supports_gl_compat_version = 31;
54 config.supports_gl_core_version = 31;
55 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
56 config.khr_no_error_support = PIGLIT_NO_ERRORS;
57
58 PIGLIT_GL_TEST_CONFIG_END
59
60 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
61
62 /* Texture stuff */
63 static GLuint fbo, prog;
64 static GLuint *tex;
65 static GLuint *texloc;
66
67 static GLuint **tex_data;
68 static GLuint **reference_data;
69
70 /* vertex data */
71 typedef struct {
72 float data[4];
73 }Vertex;
74
75 unsigned int total_indices;
76 GLuint buf = 0;
77 GLuint vao = 0;
78 GLuint buf_index = 0;
79
80 #define MAX_NUM_TEXTURES 8
81 /* Command line parameters */
82 static int width = 0;
83 static int height = 0;
84 static int num_textures = 0;
85 static int blend_passes = 0;
86 static int granularity = 0;
87 static int draw_passes = 0;
88
89 static const char *vs_text = {
90 "#version 130\n"
91 "in vec4 piglit_vertex;"
92 "void main() {\n"
93 " gl_Position = piglit_vertex;\n"
94 "}\n"
95 };
96
97 static const char *texel_fetch_template = {
98 " color[%u] = texelFetch(fb[%u], ivec2(gl_FragCoord.xy), 0);\n"
99 };
100
101 static const char *fs_text_template = {
102 "#version 130\n"
103 "#define NUM_TEXTURES %i\n"
104 "uniform usampler2D fb[NUM_TEXTURES];\n"
105 "out uvec4 color[NUM_TEXTURES];\n"
106 "void main() {\n"
107 "%s"
108 " for (int t = 0; t < NUM_TEXTURES; t++){\n"
109 " color[t]++;\n"
110 " }\n"
111 "}\n"
112 };
113
114 static void
allocate_data()115 allocate_data()
116 {
117 unsigned int t;
118
119 tex_data = malloc(sizeof(GLuint *) * num_textures);
120 reference_data = malloc(sizeof(GLuint *) * num_textures);
121
122 for (t = 0; t < num_textures; t++) {
123 tex_data[t] = malloc(sizeof(GLuint) * width * height);
124 reference_data[t] = malloc(sizeof(GLuint) * width * height);
125 }
126 }
127
128 static bool
compare_with_reference()129 compare_with_reference()
130 {
131 bool outcome = true;
132 unsigned int t;
133 GLuint *actual;
134
135 actual = malloc(sizeof(GLuint) * width * height);
136
137 for (t = 0; t < num_textures; t++) {
138 unsigned int count = 0;
139 unsigned int i;
140
141 glActiveTexture(GL_TEXTURE0 + t);
142 glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, actual);
143
144 for (i = 0; i < width * height; i++) {
145 if (reference_data[t][i] != actual[i]) {
146 count++;
147 outcome = false;
148 }
149 }
150
151 if (count > 0) {
152 fprintf(stderr, "Error on texture %u: %u texels out of "
153 "%u are different.\n", t, count, width * height);
154 }
155 }
156
157 free(actual);
158
159 return outcome;
160 }
161
162 static void
initialize_data()163 initialize_data()
164 {
165 unsigned int i,j;
166 unsigned int t;
167
168 allocate_data();
169
170 for (t = 0; t < num_textures; t++) {
171 for (i = 0; i < width * height; i++) {
172 tex_data[t][i] = reference_data[t][i] = rand();
173 for (j = 0; j < blend_passes; j++)
174 reference_data[t][i] = reference_data[t][i] + 1;
175 }
176
177 glActiveTexture(GL_TEXTURE0 + t);
178 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED_INTEGER,
179 GL_UNSIGNED_INT, tex_data[t]);
180 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + t, tex[t], 0);
181 }
182 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
183 }
184
185 static void
interpolate_square(const Vertex square[4],unsigned int granularity,Vertex * result)186 interpolate_square(const Vertex square[4],
187 unsigned int granularity,
188 Vertex *result)
189 {
190 unsigned int x;
191 unsigned int y;
192 unsigned int b = granularity - 1;
193 unsigned int k = 0;
194
195 for (y = 0; y < granularity; y++) {
196 for (x = 0; x < granularity; x++) {
197 result[k].data[0] = (square[0].data[0] * (b - x) + square[2].data[0] * x)/b;
198 result[k].data[1] = (square[0].data[1] * (b - y) + square[1].data[1] * y)/b;
199 result[k].data[2] = 0;
200 result[k].data[3] = 1;
201
202 k++;
203 }
204 }
205 }
206
207 /*
208 * Tesselates @square_pos, returning a set of vertex information and
209 * indices compatible to draw with glDrawElements at (@tesselated_pos,
210 * @indices). @granularity is the number of vertexes per side. So the
211 * minimum should be 2.
212 *
213 * Is assumed that the vertex at @square_pos are in the order
214 * (bottom-left, top-left, top-right, bottom-right)
215 *
216 * Probably not the most optimal code to do this, but it works
217 *
218 * Returns: newly allocated memory at @tesselated_pos, and @indices.
219 */
220 static void
util_tesselate_square(const Vertex square_pos[4],unsigned int granularity,Vertex ** tesselated_pos,unsigned int ** indices)221 util_tesselate_square(const Vertex square_pos[4],
222 unsigned int granularity,
223 Vertex **tesselated_pos,
224 unsigned int **indices)
225 {
226 unsigned int x;
227 unsigned int y;
228 unsigned int k;
229 Vertex *result_pos;
230 unsigned int *result_indices;
231
232 if (granularity < 2) {
233 fprintf(stderr, "Granularity should be equal or greater to 2"
234 " in order to tesselate a square. Setting it to 2.\n");
235 granularity = 2;
236 }
237
238 result_pos = (Vertex*) malloc(sizeof(Vertex) * granularity * granularity);
239 total_indices = (granularity - 1) * (granularity - 1) * 6;
240 result_indices = (unsigned int*) malloc(sizeof(unsigned int) * total_indices);
241
242 interpolate_square(square_pos, granularity, result_pos);
243
244 k = 0;
245 for (y = 0; y < granularity - 1; y++) {
246 for (x = 0; x < granularity - 1; x++) {
247 result_indices[k++] = y * granularity + x;
248 result_indices[k++] = y * granularity + x + 1;
249 result_indices[k++] = (y + 1) * granularity + x;
250
251 result_indices[k++] = y * granularity + x + 1;
252 result_indices[k++] = (y + 1) * granularity + x +1;
253 result_indices[k++] = (y + 1) * granularity + x;
254 }
255 }
256
257 *tesselated_pos = result_pos;
258 *indices = result_indices;
259 }
260
261 static GLvoid
initialize_vertex_data(float x,float y,float w,float h)262 initialize_vertex_data(float x, float y, float w, float h)
263 {
264 Vertex verts[4];
265 Vertex *tesselated_pos;
266 unsigned int *indices;
267
268 verts[0].data[0] = x;
269 verts[0].data[1] = y;
270 verts[1].data[0] = x;
271 verts[1].data[1] = y + h;
272 verts[2].data[0] = x + w;
273 verts[2].data[1] = y + h;
274 verts[3].data[0] = x + w;
275 verts[3].data[1] = y;
276
277 util_tesselate_square(verts, granularity,
278 &tesselated_pos,
279 &indices);
280
281 glGenVertexArrays(1, &vao);
282 glBindVertexArray(vao);
283
284 glGenBuffers(1, &buf);
285 glBindBuffer(GL_ARRAY_BUFFER, buf);
286
287 glBufferData(GL_ARRAY_BUFFER,
288 (sizeof(GLfloat) * 4 * granularity * granularity),
289 tesselated_pos,
290 GL_STATIC_DRAW);
291
292 glVertexAttribPointer(PIGLIT_ATTRIB_POS, 4, GL_FLOAT,
293 GL_FALSE, 0,
294 0);
295 glEnableVertexAttribArray(PIGLIT_ATTRIB_POS);
296
297 glGenBuffers(1, &buf_index);
298 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf_index);
299 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * total_indices,
300 indices, GL_STATIC_DRAW);
301
302 free(tesselated_pos);
303 free(indices);
304 }
305
306 static void
clean_resources()307 clean_resources()
308 {
309 unsigned int t;
310
311 glDeleteTextures(num_textures, tex);
312 glDeleteProgram(prog);
313 glDeleteFramebuffers(1, &fbo);
314 glDeleteVertexArrays(1, &vao);
315 glDeleteBuffers(1, &buf);
316 glDeleteBuffers(1, &buf_index);
317
318 for (t = 0; t < num_textures; t++) {
319 free(tex_data[t]);
320 free(reference_data[t]);
321 }
322
323 free(tex_data);
324 free(reference_data);
325 free(texloc);
326 free(tex);
327 }
328
329 static GLvoid
draw_rect_tex()330 draw_rect_tex()
331 {
332 /* This multiply and divide by three is a trick to ensure that
333 * basic_count is a multiple of three */
334 unsigned int basic_count = 3 * (total_indices / (3 * draw_passes));
335 unsigned int first = 0;
336
337 while (first < total_indices) {
338 unsigned int count = MIN2(total_indices - first, basic_count);
339
340 glDrawRangeElements(GL_TRIANGLES, first, first + count, count,
341 GL_UNSIGNED_INT, BUFFER_OFFSET(first * sizeof(GLuint)));
342 first += count;
343 }
344 }
345
346 enum piglit_result
piglit_display(void)347 piglit_display(void)
348 {
349 unsigned int i;
350 bool outcome = false;
351
352 glViewport(0, 0, width, height);
353
354 for (i = 0; i < blend_passes; i++) {
355 if (i != 0)
356 glTextureBarrier();
357 draw_rect_tex();
358 }
359
360 outcome = compare_with_reference() ? PIGLIT_PASS : PIGLIT_FAIL;
361
362 clean_resources();
363
364 return outcome;
365 }
366
367 static void
initialize_textures()368 initialize_textures()
369 {
370 unsigned int t;
371
372 tex = malloc(sizeof(GLuint) * num_textures);
373 glGenTextures(num_textures, tex);
374 for (t = 0; t < num_textures; t++) {
375 glActiveTexture(GL_TEXTURE0 + t);
376 glBindTexture(GL_TEXTURE_2D, tex[t]);
377 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, width, height);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
382 }
383 }
384
385 static void
initialize_fbo()386 initialize_fbo()
387 {
388 unsigned int t;
389 GLenum draw_buffers[MAX_NUM_TEXTURES];
390
391 glGenFramebuffers(1, &fbo);
392 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
393 for (t = 0; t < num_textures; t++) {
394 draw_buffers[t] = GL_COLOR_ATTACHMENT0 + t;
395 }
396 glDrawBuffers(num_textures, draw_buffers);
397 }
398
399 static void
initialize_program()400 initialize_program()
401 {
402 char *whole_fetch_string = NULL;
403 char *fs_text;
404 int result;
405 unsigned int t;
406
407 for (t = 0; t < num_textures; t++) {
408 char *one_fetch_string;
409
410 result = asprintf(&one_fetch_string, texel_fetch_template, t, t);
411 if (result == -1) {
412 fprintf(stderr, "Error creating fetch string from template.\n");
413 piglit_report_result(PIGLIT_FAIL);
414 }
415 if (whole_fetch_string != NULL) {
416 char *tmp;
417 result = asprintf(&tmp, "%s%s", whole_fetch_string,
418 one_fetch_string);
419 if (result == -1) {
420 fprintf(stderr, "Error concatenating fetch string "
421 "from template.\n");
422 piglit_report_result(PIGLIT_FAIL);
423 }
424 free(whole_fetch_string);
425 free(one_fetch_string);
426 whole_fetch_string = tmp;
427 } else {
428 whole_fetch_string = one_fetch_string;
429 }
430 }
431
432 result = asprintf(&fs_text, fs_text_template, num_textures,
433 whole_fetch_string);
434 if (result == -1) {
435 fprintf(stderr, "Error creating shader from template.\n");
436 piglit_report_result(PIGLIT_FAIL);
437 }
438 prog = piglit_build_simple_program(vs_text, fs_text);
439 piglit_check_gl_error(GL_NO_ERROR);
440
441 texloc = malloc(sizeof(GLuint) * num_textures);
442 glUseProgram(prog);
443 for (t = 0; t < num_textures; t++) {
444 const char name_template[] = "fb[%u]";
445 char *name;
446
447 result = asprintf(&name, name_template, t);
448 if (result == -1) {
449 fprintf(stderr, "Error creating uniform name from template.\n");
450 piglit_report_result(PIGLIT_FAIL);
451 }
452 texloc[t] = glGetUniformLocation(prog, name);
453 if (texloc[t] == -1) {
454 fprintf(stderr, "Error getting uniform %s.\n", name);
455 piglit_report_result(PIGLIT_FAIL);
456 }
457 glUniform1i(texloc[t], t);
458 free(name);
459 }
460
461 free(fs_text);
462 free(whole_fetch_string);
463 }
464
465 static void
print_usage()466 print_usage()
467 {
468 printf("Usage: arb_texture_barrier-blending-in-shader ");
469 printf("<resolution> <blend_passes> <num_textures> <granularity> <common piglit args>\n");
470 printf("\tresolution valid range is [1, 1024]\n");
471 printf("\tblend_passes valid range is [1,42]\n");
472 printf("\tnum_textures valid range is [1, %i]\n", MAX_NUM_TEXTURES);
473 printf("\tgranularity (the number of vertices per side) valid range is [2, 256]\n");
474 printf("\tdraw_passes valid range is [1, 10]\n");
475 }
476
477 static void
parse_args(int argc,char ** argv)478 parse_args(int argc, char **argv)
479 {
480 if (argc != 6) {
481 print_usage();
482 piglit_report_result(PIGLIT_FAIL);
483 }
484
485 width = height = atoi(argv[1]);
486 if (width < 1 || width > 1024) {
487 fprintf(stderr, "Wrong value for resolution: %s\n", argv[1]);
488 print_usage();
489 piglit_report_result(PIGLIT_FAIL);
490 }
491
492 blend_passes = atoi(argv[2]);
493 if (blend_passes < 1 || blend_passes > 42) {
494 fprintf(stderr, "Wrong value for blend_passes: %s\n", argv[2]);
495 print_usage();
496 piglit_report_result(PIGLIT_FAIL);
497 }
498
499 num_textures = atoi(argv[3]);
500 if (num_textures < 1 || num_textures > MAX_NUM_TEXTURES) {
501 fprintf(stderr, "Wrong value for num_textures: %s\n", argv[3]);
502 print_usage();
503 piglit_report_result(PIGLIT_FAIL);
504 }
505
506 granularity = atoi(argv[4]);
507 if (granularity < 2 || granularity > 256) {
508 fprintf(stderr, "Wrong value for granularity: %s\n", argv[4]);
509 print_usage();
510 piglit_report_result(PIGLIT_FAIL);
511 }
512
513 draw_passes = atoi(argv[5]);
514 if (draw_passes < 1 || draw_passes > 10) {
515 fprintf(stderr, "Wrong value for draw_passes: %s\n", argv[5]);
516 print_usage();
517 piglit_report_result(PIGLIT_FAIL);
518 }
519
520 fprintf(stdout, "Executing test with the following parameters:\n"
521 "resolution = %i\n"
522 "blend_passes = %i\n"
523 "num_textures = %i\n"
524 "granularity = %i\n"
525 "draw_passes = %i\n",
526 width, blend_passes, num_textures, granularity, draw_passes);
527 }
528
529 void
piglit_init(int argc,char ** argv)530 piglit_init(int argc, char **argv)
531 {
532 piglit_require_extension("GL_ARB_framebuffer_object");
533 piglit_require_extension("GL_ARB_texture_barrier");
534 piglit_require_GLSL_version(130);
535
536 parse_args(argc, argv);
537
538 srand(0);
539
540 initialize_program();
541 initialize_textures();
542 initialize_fbo();
543 initialize_data();
544 initialize_vertex_data(-1, -1, 2, 2);
545 }
546