bool init_fb(const struct grid_info grid) { bool ret = true; if (grid.stages & GL_COMPUTE_SHADER_BIT) { const struct image_info img = image_info_for_grid(grid); const unsigned n = product(grid.size) * image_num_components(grid.format); uint32_t *pixels = malloc(n * sizeof(*pixels)); ret = init_pixels(img, pixels, 0.5, 0.5, 0.5, 0.5) && upload_image(img, max_image_units(), pixels); free(pixels); } else { ret = generate_fb(grid, 0); glClearColor(0.5, 0.5, 0.5, 0.5); glClear(GL_COLOR_BUFFER_BIT); glClearDepth(0.5); glClear(GL_DEPTH_BUFFER_BIT); } return ret; }
static bool run_test(GLbitfield shaders) { const struct grid_info grid = { shaders, get_image_format(GL_R32UI), { W, H, 1, 1 } }; const struct image_info img = image_info_for_grid(grid); GLuint prog = generate_program( grid, GL_VERTEX_SHADER, generate_source(grid, img, GL_VERTEX_SHADER), GL_TESS_CONTROL_SHADER, generate_source(grid, img, GL_TESS_CONTROL_SHADER), GL_TESS_EVALUATION_SHADER, generate_source(grid, img, GL_TESS_EVALUATION_SHADER), GL_GEOMETRY_SHADER, generate_source(grid, img, GL_GEOMETRY_SHADER), GL_FRAGMENT_SHADER, generate_source(grid, img, GL_FRAGMENT_SHADER), GL_COMPUTE_SHADER, generate_source(grid, img, GL_COMPUTE_SHADER)); bool ret = prog && init_fb(grid) && init_images(img) && bind_images(grid, prog) && draw_grid(grid, prog) && check(grid, img); glDeleteProgram(prog); return ret; }
static bool run_test(const struct image_op_info *op, unsigned w, unsigned h, bool (*check)(const struct grid_info grid, const struct image_info img, unsigned w, unsigned h), const char *body) { const struct grid_info grid = grid_info(GL_FRAGMENT_SHADER, GL_R32UI, W, H); const struct image_info img = image_info_for_grid(grid); GLuint prog = generate_program( grid, GL_FRAGMENT_SHADER, concat(image_hunk(img, ""), hunk("uniform IMAGE_T img;\n"), hunk(op->hunk), hunk(body), NULL)); bool ret = prog && init_fb(grid) && init_image(img) && set_uniform_int(prog, "img", 0) && draw_grid(set_grid_size(grid, w, h), prog) && check(grid, img, w, h); glDeleteProgram(prog); return ret; }
static bool run_test(const struct image_qualifier_info *qual, const struct image_stage_info *stage_w, const struct image_stage_info *stage_r, unsigned l) { const struct grid_info grid = { stage_w->bit | stage_r->bit, get_image_format(GL_RGBA32UI), { l, l, 1, 1 } }; const struct image_info img = image_info_for_grid(grid); GLuint prog = generate_program( grid, /* * Write (11, 22, 33, 44) to some location on the * image from the write stage. */ stage_w->stage, concat(qualifier_hunk(qual), image_hunk(img, ""), hunk("IMAGE_Q uniform IMAGE_T img;\n" "\n" "GRID_T op(ivec2 idx, GRID_T x) {\n" " imageStore(img, idx, DATA_T(11, 22, 33, 44));" " return x;" "}\n"), NULL), /* * The same location will read back the expected value * if image access is coherent, as the shader inputs * of the read stage are dependent on the outputs of * the write stage and consequently they are * guaranteed to be executed sequentially. */ stage_r->stage, concat(qualifier_hunk(qual), image_hunk(img, ""), hunk("IMAGE_Q uniform IMAGE_T img;\n" "\n" "GRID_T op(ivec2 idx, GRID_T x) {\n" " DATA_T v = imageLoad(img, idx);" " if (v == DATA_T(11, 22, 33, 44))" " return GRID_T(33, 33, 33, 33);" " else" " return GRID_T(77, 77, 77, 77);" "}\n"), NULL)); bool ret = prog && init_fb(grid) && init_image(img) && set_uniform_int(prog, "img", 0) && draw_grid(grid, prog) && (check(grid, img) || qual->control_test); glDeleteProgram(prog); return ret; }
static bool check(const struct grid_info grid, const struct image_info img, unsigned l) { const unsigned layer_sz = 4 * product(grid.size); uint32_t pixels_fb[4 * N], expect_fb[4 * N]; uint32_t pixels_img[4 * N], expect_img[4 * N]; unsigned i; if (!download_result(grid, pixels_fb) || !download_image(img, 0, pixels_img)) return false; for (i = 0; i < layer_sz; ++i) { /* * The framebuffer contents should reflect layer l of * the image which is bound to the image unit. */ expect_fb[i] = encode(grid.format, layer_sz * l + i); } for (i = 0; i < 4 * N; ++i) { if (i / layer_sz == l) { /* * Layer l should have been modified by the * shader. */ expect_img[i] = encode(img.format, 33); } else { /* * Other layers should have remained * unchanged. */ expect_img[i] = encode(img.format, i); } } if (!check_pixels_v(image_info_for_grid(grid), pixels_fb, expect_fb)) { printf(" Source: framebuffer\n"); return false; } if (!check_pixels_v(img, pixels_img, expect_img)) { printf(" Source: image\n"); return false; } return true; }
bool download_result(const struct grid_info grid, uint32_t *r_pixels) { if (grid.stages & GL_COMPUTE_SHADER_BIT) { /* No actual framebuffer. Results are returned into * an image. */ return download_image(image_info_for_grid(grid), max_image_units(), r_pixels); } else { glReadPixels(0, 0, grid.size.x, grid.size.y, grid.format->pixel_format, image_base_type(grid.format), r_pixels); return piglit_check_gl_error(GL_NO_ERROR); } }
/** * Test skeleton: Init image to \a init_value, run the provided shader * \a op and check that the resulting image pixels equal \a * check_value. */ static bool run_test(uint32_t init_value, uint32_t check_value, const char *op) { const struct grid_info grid = grid_info(GL_FRAGMENT_SHADER, GL_R32UI, W, H); const struct image_info img = image_info_for_grid(grid); GLuint prog = generate_program( grid, GL_FRAGMENT_SHADER, concat(image_hunk(img, ""), hunk("uniform IMAGE_T img;\n"), hunk(op), NULL)); bool ret = prog && init_fb(grid) && init_image(img, init_value) && set_uniform_int(prog, "img", 0) && draw_grid(grid, prog) && check(img, check_value); glDeleteProgram(prog); return ret; }
/** * Test binding image uniforms to image units for a simple shader * program. */ static bool run_test_uniform(void) { const struct grid_info grid = grid_info(GL_FRAGMENT_SHADER, GL_RGBA32F, W, H); GLuint prog = generate_program( grid, GL_FRAGMENT_SHADER, concat(image_hunk(image_info_for_grid(grid), ""), hunk("uniform IMAGE_T imgs[2];\n" "\n" "GRID_T op(ivec2 idx, GRID_T x) {\n" " imageStore(imgs[0], IMAGE_ADDR(idx), x);\n" " imageStore(imgs[1], IMAGE_ADDR(idx), x);\n" " return x;\n" "}\n"), NULL)); const int loc = glGetUniformLocation(prog, "imgs"); bool ret = prog && check_uniform_int(prog, loc, 0) && check_uniform_int(prog, loc + 1, 0); int v[2]; glUseProgram(prog); /* * Image uniforms are bound to image units using * glUniform1i{v}. */ glUniform1i(loc, 3); ret &= check_uniform_int(prog, loc, 3) && check_uniform_int(prog, loc + 1, 0); glUniform1i(loc + 1, 3); ret &= check_uniform_int(prog, loc, 3) && check_uniform_int(prog, loc + 1, 3); v[0] = 4; v[1] = 5; glUniform1iv(loc, 2, v); ret &= check_uniform_int(prog, loc, 4) && check_uniform_int(prog, loc + 1, 5); /* * GL_INVALID_VALUE is generated if the value specified is * greater than or equal to the value of GL_MAX_IMAGE_UNITS. */ glUniform1i(loc, max_image_units()); ret &= piglit_check_gl_error(GL_INVALID_VALUE); v[0] = 3; v[1] = max_image_units() + 1; glUniform1iv(loc, 2, v); ret &= piglit_check_gl_error(GL_INVALID_VALUE); /* * GL_INVALID_VALUE is generated if the value specified is * less than zero. */ glUniform1i(loc, -1); ret &= piglit_check_gl_error(GL_INVALID_VALUE); v[0] = 3; v[1] = -4; glUniform1iv(loc, 2, v); ret &= piglit_check_gl_error(GL_INVALID_VALUE); /* * GL_INVALID_OPERATION is generated by Uniform* functions * other than Uniform1i{v}. */ CHECK_INVAL_2(glUniform, 1f, 1ui, (loc, 0), ret); CHECK_INVAL_3(glUniform, 2i, 2f, 2ui, (loc, 0, 0), ret); CHECK_INVAL_3(glUniform, 3i, 3f, 3ui, (loc, 0, 0, 0), ret); CHECK_INVAL_3(glUniform, 4i, 4f, 4ui, (loc, 0, 0, 0, 0), ret); CHECK_INVAL_2(glUniform, 1fv, 1uiv, (loc, 1, (void *)v), ret); CHECK_INVAL_3(glUniform, 2iv, 2fv, 2uiv, (loc, 1, (void *)v), ret); CHECK_INVAL_3(glUniform, 3iv, 3fv, 3uiv, (loc, 1, (void *)v), ret); CHECK_INVAL_3(glUniform, 4iv, 4fv, 4uiv, (loc, 1, (void *)v), ret); CHECK_INVAL_3(glUniformMatrix, 2fv, 3fv, 4fv, (loc, 1, GL_FALSE, (float *)v), ret); CHECK_INVAL_3(glUniformMatrix, 2x3fv, 3x2fv, 2x4fv, (loc, 1, GL_FALSE, (float *)v), ret); CHECK_INVAL_3(glUniformMatrix, 4x2fv, 3x4fv, 4x3fv, (loc, 1, GL_FALSE, (float *)v), ret); if (piglit_is_extension_supported("GL_ARB_gpu_shader_fp64")) { CHECK_INVAL_1(glUniform, 1d, (loc, 0), ret); CHECK_INVAL_1(glUniform, 2d, (loc, 0, 0), ret); CHECK_INVAL_1(glUniform, 3d, (loc, 0, 0, 0), ret); CHECK_INVAL_1(glUniform, 4d, (loc, 0, 0, 0, 0), ret); CHECK_INVAL_2(glUniform, 1dv, 2dv, (loc, 1, (double *)v), ret); CHECK_INVAL_2(glUniform, 3dv, 4dv, (loc, 1, (double *)v), ret); CHECK_INVAL_3(glUniformMatrix, 2dv, 3dv, 4dv, (loc, 1, GL_FALSE, (double *)v), ret); CHECK_INVAL_3(glUniformMatrix, 2x3dv, 3x2dv, 2x4dv, (loc, 1, GL_FALSE, (double *)v), ret); CHECK_INVAL_3(glUniformMatrix, 4x2dv, 3x4dv, 4x3dv, (loc, 1, GL_FALSE, (double *)v), ret); } glDeleteProgram(prog); return ret; }
bool download_image_levels(const struct image_info img, unsigned num_levels, unsigned unit, uint32_t *r_pixels) { const unsigned m = image_num_components(img.format); int i, l; glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT | GL_PIXEL_BUFFER_BARRIER_BIT | GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glBindTexture(img.target->target, textures[unit]); switch (img.target->target) { case GL_TEXTURE_1D: case GL_TEXTURE_2D: case GL_TEXTURE_3D: case GL_TEXTURE_RECTANGLE: case GL_TEXTURE_1D_ARRAY: case GL_TEXTURE_2D_ARRAY: case GL_TEXTURE_CUBE_MAP_ARRAY: assert(img.target->target != GL_TEXTURE_RECTANGLE || num_levels == 1); for (l = 0; l < num_levels; ++l) glGetTexImage(img.target->target, l, img.format->pixel_format, image_base_type(img.format), &r_pixels[m * image_level_offset(img, l)]); break; case GL_TEXTURE_CUBE_MAP: for (l = 0; l < num_levels; ++l) { const unsigned offset = m * image_level_offset(img, l); const unsigned face_sz = m * product(image_level_size(img, l)) / 6; for (i = 0; i < 6; ++i) glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, l, img.format->pixel_format, image_base_type(img.format), &r_pixels[offset + face_sz * i]); } break; case GL_TEXTURE_BUFFER: { /* * glGetTexImage() isn't supposed to work with buffer * textures. We copy the packed pixels to a texture * with the same internal format as the image to let * the GL unpack it for us. */ const struct image_extent grid = image_optimal_extent(img.size); GLuint packed_tex; assert(num_levels == 1); glGenTextures(1, &packed_tex); glBindTexture(GL_TEXTURE_2D, packed_tex); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffers[unit]); glTexImage2D(GL_TEXTURE_2D, 0, img.format->format, grid.x, grid.y, 0, img.format->pixel_format, img.format->pixel_type, NULL); glGetTexImage(GL_TEXTURE_2D, 0, img.format->pixel_format, image_base_type(img.format), r_pixels); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glDeleteTextures(1, &packed_tex); break; } case GL_TEXTURE_2D_MULTISAMPLE: case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: { /* * GL doesn't seem to provide any direct way to read * back a multisample texture, so we use imageLoad() * to copy its contents to a larger single-sample 2D * texture from the fragment shader. */ const struct grid_info grid = { get_image_stage(GL_FRAGMENT_SHADER)->bit, img.format, image_optimal_extent(img.size) }; GLuint prog = generate_program( grid, GL_FRAGMENT_SHADER, concat(image_hunk(img, "SRC_"), image_hunk(image_info_for_grid(grid), "DST_"), hunk("readonly SRC_IMAGE_UNIFORM_T src_img;\n" "writeonly DST_IMAGE_UNIFORM_T dst_img;\n" "\n" "GRID_T op(ivec2 idx, GRID_T x) {\n" " imageStore(dst_img, DST_IMAGE_ADDR(idx),\n" " imageLoad(src_img, SRC_IMAGE_ADDR(idx)));\n" " return x;\n" "}\n"), NULL)); bool ret = prog && generate_fb(grid, 1); GLuint tmp_tex; assert(num_levels == 1); glGenTextures(1, &tmp_tex); glBindTexture(GL_TEXTURE_2D, tmp_tex); glTexImage2D(GL_TEXTURE_2D, 0, img.format->format, grid.size.x, grid.size.y, 0, img.format->pixel_format, image_base_type(img.format), NULL); glBindImageTexture(unit, textures[unit], 0, GL_TRUE, 0, GL_READ_ONLY, img.format->format); glBindImageTexture(6, tmp_tex, 0, GL_TRUE, 0, GL_WRITE_ONLY, img.format->format); ret &= set_uniform_int(prog, "src_img", unit) && set_uniform_int(prog, "dst_img", 6) && draw_grid(grid, prog); glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT); glGetTexImage(GL_TEXTURE_2D, 0, img.format->pixel_format, image_base_type(img.format), r_pixels); glDeleteProgram(prog); glDeleteTextures(1, &tmp_tex); glBindFramebuffer(GL_FRAMEBUFFER, fb[0]); glViewportIndexedfv(0, vp[0]); if (!ret) return false; break; } default: abort(); } return piglit_check_gl_error(GL_NO_ERROR); }
bool upload_image_levels(const struct image_info img, unsigned num_levels, unsigned level, unsigned unit, const uint32_t *pixels) { const unsigned m = image_num_components(img.format); int i, l; if (get_texture(unit)) { glDeleteTextures(1, &textures[unit]); textures[unit] = 0; } if (get_buffer(unit)) { glDeleteBuffers(1, &buffers[unit]); buffers[unit] = 0; } glGenTextures(1, &textures[unit]); glBindTexture(img.target->target, textures[unit]); switch (img.target->target) { case GL_TEXTURE_1D: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage1D(GL_TEXTURE_1D, l, img.format->format, size.x, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_2D: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage2D(GL_TEXTURE_2D, l, img.format->format, size.x, size.y, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_3D: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage3D(GL_TEXTURE_3D, l, img.format->format, size.x, size.y, size.z, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_RECTANGLE: assert(num_levels == 1); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, img.format->format, img.size.x, img.size.y, 0, img.format->pixel_format, image_base_type(img.format), pixels); break; case GL_TEXTURE_CUBE_MAP: for (l = 0; l < num_levels; ++l) { const unsigned offset = m * image_level_offset(img, l); const struct image_extent size = image_level_size(img, l); const unsigned face_sz = m * product(size) / 6; for (i = 0; i < 6; ++i) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, l, img.format->format, size.x, size.y, 0, img.format->pixel_format, image_base_type(img.format), &pixels[offset + face_sz * i]); } break; case GL_TEXTURE_BUFFER: { /* * glTexImage*() isn't supposed to work with buffer * textures. We copy the unpacked pixels to a texture * with the desired internal format to let the GL pack * them for us. */ const struct image_extent grid = image_optimal_extent(img.size); GLuint packed_tex; assert(num_levels == 1); glGenBuffers(1, &buffers[unit]); glBindBuffer(GL_PIXEL_PACK_BUFFER, buffers[unit]); glBufferData(GL_PIXEL_PACK_BUFFER, img.size.x * image_pixel_size(img.format) / 8, NULL, GL_STATIC_DRAW); glGenTextures(1, &packed_tex); glBindTexture(GL_TEXTURE_2D, packed_tex); glTexImage2D(GL_TEXTURE_2D, 0, img.format->format, grid.x, grid.y, 0, img.format->pixel_format, image_base_type(img.format), pixels); glGetTexImage(GL_TEXTURE_2D, 0, img.format->pixel_format, img.format->pixel_type, NULL); glDeleteTextures(1, &packed_tex); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glTexBuffer(GL_TEXTURE_BUFFER, image_compat_format(img.format), buffers[unit]); break; } case GL_TEXTURE_1D_ARRAY: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage2D(GL_TEXTURE_1D_ARRAY, l, img.format->format, size.x, size.y, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_2D_ARRAY: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage3D(GL_TEXTURE_2D_ARRAY, l, img.format->format, size.x, size.y, size.z, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_CUBE_MAP_ARRAY: for (l = 0; l < num_levels; ++l) { const struct image_extent size = image_level_size(img, l); glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, l, img.format->format, size.x, size.y, size.z, 0, img.format->pixel_format, image_base_type(img.format), &pixels[m * image_level_offset(img, l)]); } break; case GL_TEXTURE_2D_MULTISAMPLE: case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: { /* * GL doesn't seem to provide any direct way to * initialize a multisample texture, so we use * imageStore() to render to it from the fragment * shader copying the contents of a larger * single-sample 2D texture. */ const struct grid_info grid = { get_image_stage(GL_FRAGMENT_SHADER)->bit, img.format, image_optimal_extent(img.size) }; GLuint prog = generate_program( grid, GL_FRAGMENT_SHADER, concat(image_hunk(image_info_for_grid(grid), "SRC_"), image_hunk(img, "DST_"), hunk("readonly SRC_IMAGE_UNIFORM_T src_img;\n" "writeonly DST_IMAGE_UNIFORM_T dst_img;\n" "\n" "GRID_T op(ivec2 idx, GRID_T x) {\n" " imageStore(dst_img, DST_IMAGE_ADDR(idx),\n" " imageLoad(src_img, SRC_IMAGE_ADDR(idx)));\n" " return x;\n" "}\n"), NULL)); bool ret = prog && generate_fb(grid, 1); GLuint tmp_tex; assert(num_levels == 1); glGenTextures(1, &tmp_tex); glBindTexture(GL_TEXTURE_2D, tmp_tex); if (img.target->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) { glTexImage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, img.size.x, img.format->format, img.size.y, img.size.z, img.size.w, GL_FALSE); } else { glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, img.size.x, img.format->format, img.size.y, img.size.z, GL_FALSE); } glTexImage2D(GL_TEXTURE_2D, 0, img.format->format, grid.size.x, grid.size.y, 0, img.format->pixel_format, image_base_type(img.format), pixels); glBindImageTexture(unit, textures[unit], 0, GL_TRUE, 0, GL_WRITE_ONLY, img.format->format); glBindImageTexture(6, tmp_tex, 0, GL_TRUE, 0, GL_READ_ONLY, img.format->format); ret &= set_uniform_int(prog, "src_img", 6) && set_uniform_int(prog, "dst_img", unit) && draw_grid(grid, prog); glDeleteProgram(prog); glDeleteTextures(1, &tmp_tex); glBindFramebuffer(GL_FRAMEBUFFER, fb[0]); glViewportIndexedfv(0, vp[0]); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); if (!ret) return false; break; } default: abort(); } glBindImageTexture(unit, textures[unit], level, GL_TRUE, 0, GL_READ_WRITE, img.format->format); return piglit_check_gl_error(GL_NO_ERROR); }