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
|
/*
* Copyright © 2015 Intel Corporation
*
* 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 maxblocks.c
*
* From the GL_ARB_shader_storage_buffer_object spec:
*
* "If the number of active shader storage blocks referenced by the shaders in
* a program exceeds implementation-dependent limits, the program will fail
* to link. The limits for vertex, tessellation control, tessellation
* evaluation, geometry, fragment, and compute shaders can be obtained by
* calling GetIntegerv with pname values of MAX_VERTEX_SHADER_STORAGE_BLOCKS,
* MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS,
* MAX_TESS_EVALUATION_SHADER_STORAGE_BLOCKS,
* MAX_GEOMETRY_SHADER_STORAGE_BLOCKS, MAX_FRAGMENT_SHADER_STORAGE_BLOCKS,
* and MAX_COMPUTE_SHADER_STORAGE_BLOCKS, respectively. Additionally, a
* program will fail to link if the sum of the number of active shader
* storage blocks referenced by each shader stage in a program exceeds the
* value of the implementation-dependent limit
* MAX_COMBINED_SHADER_STORAGE_BLOCKS. If a shader storage block in a
* program is referenced by multiple shaders, each such reference counts
* separately against this combined limit.
*/
#include "piglit-util-gl.h"
PIGLIT_GL_TEST_CONFIG_BEGIN
config.window_width = 800;
config.window_height = 200;
config.supports_gl_compat_version = 32;
config.supports_gl_core_version = 32;
config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
config.khr_no_error_support = PIGLIT_NO_ERRORS;
PIGLIT_GL_TEST_CONFIG_END
static char *
get_shader(GLenum target, const char *block_prefix, int blocks)
{
char *shader = NULL;
const char *vs_source =
"#version 130\n"
"#extension GL_ARB_shader_storage_buffer_object : enable\n"
"#extension GL_ARB_uniform_buffer_object : enable\n"
"\n"
"varying vec4 v;\n"
"in vec4 piglit_vertex;\n"
"\n"
"%s"
"\n"
"void main() {\n"
" gl_Position = piglit_vertex;\n"
" v = vec4(0)%s;\n"
"}\n";
const char *fs_source =
"#version 130\n"
"#extension GL_ARB_shader_storage_buffer_object : enable\n"
"#extension GL_ARB_uniform_buffer_object : enable\n"
"\n"
"varying vec4 v;"
"\n"
"%s"
"\n"
"void main() {\n"
" gl_FragColor = v%s;\n"
"}\n";
const char *prefix_template =
"layout(std140) buffer %s_block%d {\n"
" vec4 %s_var%d;\n"
"};\n";
const char *body_template =
" + %s_var%d";
char *prefix, *prefix_tail, *body, *body_tail;
int i;
prefix = calloc(1, (strlen(block_prefix) * 2 + strlen(prefix_template) +
20) * blocks + 1);
body = calloc(1, (strlen(block_prefix) + strlen(body_template) +
20) * blocks + 1);
prefix_tail = prefix;
prefix[0] = 0;
body_tail = body;
body[0] = 0;
for (i = 0; i < blocks; i++) {
prefix_tail += sprintf(prefix_tail, prefix_template,
block_prefix, i,
block_prefix, i);
body_tail += sprintf(body_tail, body_template,
block_prefix, i);
}
switch (target) {
case GL_VERTEX_SHADER:
(void)!asprintf(&shader, vs_source, prefix, body);
break;
case GL_FRAGMENT_SHADER:
(void)!asprintf(&shader, fs_source, prefix, body);
break;
default:
piglit_report_result(PIGLIT_FAIL);
}
free(prefix);
free(body);
return shader;
}
static GLuint
build_shaders(const char *vs_prefix, int vs_blocks,
const char *fs_prefix, int fs_blocks)
{
char *vs_source, *fs_source;
GLuint vs, fs, prog;
vs_source = get_shader(GL_VERTEX_SHADER, vs_prefix, vs_blocks);
fs_source = get_shader(GL_FRAGMENT_SHADER, fs_prefix, fs_blocks);
vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_source);
fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_source);
prog = glCreateProgram();
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
if (!piglit_link_check_status_quiet(prog)) {
glDeleteProgram(prog);
prog = 0;
}
glDeleteShader(vs);
glDeleteShader(fs);
return prog;
}
static bool
fail_link_test(const char *vs_prefix, int vs_blocks,
const char *fs_prefix, int fs_blocks)
{
GLuint prog;
prog = build_shaders(vs_prefix, vs_blocks,
fs_prefix, fs_blocks);
if (prog) {
printf("linked with (%d, %d) blocks, should have failed\n",
vs_blocks, fs_blocks);
return false;
}
glDeleteProgram(prog);
return true;
}
static bool
test_draw(int y_index, GLuint prog,
GLuint *bos, int test_block, int active_blocks)
{
float black[4] = {0, 0, 0, 0};
/* Color values have to be 0 or 1, since in the case of a
* shared block between VS and FS, they'll be added twice.
*/
float other_colors[][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{1, 1, 1, 1},
};
float *color;
int screen_w = 10;
int screen_h = 10;
int screen_x = screen_w * (1 + 2 * test_block);
int screen_y = screen_h * (1 + 2 * y_index);
float x = -1.0 + 2.0 * screen_x / piglit_width;
float y = -1.0 + 2.0 * screen_y / piglit_height;
float w = 2.0 * screen_w / piglit_width;
float h = 2.0 * screen_h / piglit_height;
int i;
float *expected_color;
assert(test_block < active_blocks);
glViewport(0, 0, piglit_width, piglit_height);
expected_color = other_colors[test_block % ARRAY_SIZE(other_colors)];
for (i = 0; i < active_blocks; i++) {
if (i == test_block)
color = expected_color;
else
color = black;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bos[i]);
glBufferData(GL_SHADER_STORAGE_BUFFER,
4 * sizeof(float), color, GL_DYNAMIC_DRAW);
}
piglit_draw_rect(x, y, w, h);
if (screen_x + screen_w >= piglit_width ||
screen_y + screen_h >= piglit_height) {
printf("warning: window too small to display test rect.\n");
return true;
}
return piglit_probe_rect_rgba(screen_x, screen_y,
screen_w, screen_h, expected_color);
}
static bool
pass_link_test(int y_index,
const char *vs_prefix, int vs_blocks,
const char *fs_prefix, int fs_blocks)
{
bool pass = true;
GLuint prog;
GLuint *bos = (GLuint *) calloc(vs_blocks + fs_blocks, sizeof(GLuint));
GLint active_blocks;
int i;
prog = build_shaders(vs_prefix, vs_blocks,
fs_prefix, fs_blocks);
if (!prog) {
printf("shader with (%d, %d) blocks failed to link\n",
vs_blocks, fs_blocks);
free(bos);
return false;
}
glUseProgram(prog);
glGetProgramInterfaceiv(prog, GL_SHADER_STORAGE_BLOCK,
GL_ACTIVE_RESOURCES, &active_blocks);
glGenBuffers(active_blocks, bos);
for (i = 0; i < active_blocks; i++) {
glShaderStorageBlockBinding(prog, i, i);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, i, bos[i]);
}
for (i = 0; i < active_blocks; i++) {
if (!test_draw(y_index, prog, bos, i, active_blocks)) {
pass = false;
}
}
glDeleteBuffers(active_blocks, bos);
glDeleteProgram(prog);
free(bos);
return pass;
}
enum piglit_result
piglit_display(void)
{
bool pass = true;
GLint max_vs, max_fs, max_combined, max_combined_out;
piglit_require_extension("GL_ARB_shader_storage_buffer_object");
piglit_require_extension("GL_ARB_program_interface_query");
glGetIntegerv(GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS, &max_vs);
glGetIntegerv(GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS, &max_fs);
glGetIntegerv(GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS, &max_combined);
glGetIntegerv(GL_MAX_COMBINED_SHADER_OUTPUT_RESOURCES, &max_combined_out);
if (!piglit_check_gl_error(GL_NO_ERROR))
return PIGLIT_FAIL;
printf("Max VS shader storage blocks: %d\n", max_vs);
printf("Max FS shader storage blocks: %d\n", max_fs);
printf("Max combined shader storage blocks: %d\n", max_combined);
printf("Max combined shader output resources: %d\n", max_combined_out);
glClearColor(0.5, 0.5, 0.5, 0.5);
glClear(GL_COLOR_BUFFER_BIT);
pass = fail_link_test("vs", max_vs + 1,
"vs", 0) && pass;
pass = fail_link_test("fs", 0,
"fs", max_fs + 1) && pass;
if (max_vs + max_fs > max_combined) {
pass = fail_link_test("vs",
max_vs,
"fs",
max_combined + 1 - max_vs) && pass;
pass = fail_link_test("shared",
max_vs,
"shared",
max_combined + 1 - max_vs) && pass;
}
if (max_combined_out) {
pass = fail_link_test("vs",
max_vs,
"fs",
max_combined_out + 1 - max_vs) && pass;
pass = fail_link_test("shared",
max_vs,
"shared",
max_combined_out + 1 - max_vs) && pass;
}
pass = pass_link_test(0,
"vs", max_vs,
"vs", 0) && pass;
pass = pass_link_test(1,
"fs", 0,
"fs", max_fs) && pass;
pass = pass_link_test(2,
"vs", max_vs,
"fs", MIN2(max_fs,
max_combined - max_vs)) && pass;
pass = pass_link_test(3,
"shared", max_vs,
"shared", MIN2(max_fs,
max_combined - max_vs)) && pass;
if (max_combined_out) {
pass = pass_link_test(4,
"shared", max_vs,
"shared",
MIN2(max_fs, max_combined - max_vs)) && pass;
}
piglit_present_results();
return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}
void
piglit_init(int argc, char **argv)
{
}
|