+/**
+ * Generate a generic shader to blit from a texture to a framebuffer
+ *
+ * \param ctx Current GL context
+ * \param texTarget Texture target that will be the source of the blit
+ *
+ * \returns a handle to a shader program on success or zero on failure.
+ */
+static void
+setup_blit_shader(struct gl_context *ctx,
+ GLenum target,
+ struct sampler_table *table)
+{
+ const char *vs_source;
+ char *fs_source;
+ GLuint vs, fs;
+ void *const mem_ctx = ralloc_context(NULL);
+ struct glsl_sampler *sampler =
+ setup_texture_sampler(target, table);
+
+ assert(sampler != NULL);
+
+ if (sampler->shader_prog != 0) {
+ _mesa_UseProgram(sampler->shader_prog);
+ return;
+ }
+
+ /* The version check is a little tricky. API is set to API_OPENGLES2 even
+ * for OpenGL ES 3.0 contexts, and GLSLVersion may be set to 140, for
+ * example, in an OpenGL ES 2.0 context.
+ */
+ if ((ctx->API == API_OPENGLES2 && ctx->Version < 30)
+ || ctx->Const.GLSLVersion < 130) {
+ vs_source =
+ "attribute vec2 position;\n"
+ "attribute vec3 textureCoords;\n"
+ "varying vec4 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ "}\n";
+
+ fs_source = ralloc_asprintf(mem_ctx,
+ "#extension GL_EXT_texture_array : enable\n"
+ "#extension GL_ARB_texture_cube_map_array: enable\n"
+ "#ifdef GL_ES\n"
+ "precision highp float;\n"
+ "#endif\n"
+ "uniform %s texSampler;\n"
+ "varying vec4 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " gl_FragColor = %s(texSampler, %s);\n"
+ " gl_FragDepth = gl_FragColor.x;\n"
+ "}\n",
+ sampler->type,
+ sampler->func, sampler->texcoords);
+ }
+ else {
+ vs_source = ralloc_asprintf(mem_ctx,
+ "#version %s\n"
+ "in vec2 position;\n"
+ "in vec4 textureCoords;\n"
+ "out vec4 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ "}\n",
+ _mesa_is_desktop_gl(ctx) ? "130" : "300 es");
+ fs_source = ralloc_asprintf(mem_ctx,
+ "#version %s\n"
+ "#extension GL_ARB_texture_cube_map_array: enable\n"
+ "#ifdef GL_ES\n"
+ "precision highp float;\n"
+ "#endif\n"
+ "uniform %s texSampler;\n"
+ "in vec4 texCoords;\n"
+ "out vec4 out_color;\n"
+ "\n"
+ "void main()\n"
+ "{\n"
+ " out_color = texture(texSampler, %s);\n"
+ " gl_FragDepth = out_color.x;\n"
+ "}\n",
+ _mesa_is_desktop_gl(ctx) ? "130" : "300 es",
+ sampler->type,
+ sampler->texcoords);
+ }
+
+ vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
+ fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
+
+ sampler->shader_prog = _mesa_CreateProgramObjectARB();
+ _mesa_AttachShader(sampler->shader_prog, fs);
+ _mesa_DeleteObjectARB(fs);
+ _mesa_AttachShader(sampler->shader_prog, vs);
+ _mesa_DeleteObjectARB(vs);
+ _mesa_BindAttribLocation(sampler->shader_prog, 0, "position");
+ _mesa_BindAttribLocation(sampler->shader_prog, 1, "texcoords");
+ link_program_with_debug(ctx, sampler->shader_prog);
+ ralloc_free(mem_ctx);
+
+ _mesa_UseProgram(sampler->shader_prog);
+}
+
+/**
+ * Configure vertex buffer and vertex array objects for tests
+ *
+ * Regardless of whether a new VAO and new VBO are created, the objects
+ * referenced by \c VAO and \c VBO will be bound into the GL state vector
+ * when this function terminates.
+ *
+ * \param VAO Storage for vertex array object handle. If 0, a new VAO
+ * will be created.
+ * \param VBO Storage for vertex buffer object handle. If 0, a new VBO
+ * will be created. The new VBO will have storage for 4
+ * \c vertex structures.
+ * \param use_generic_attributes Should generic attributes 0 and 1 be used,
+ * or should traditional, fixed-function color and texture
+ * coordinate be used?
+ * \param vertex_size Number of components for attribute 0 / vertex.
+ * \param texcoord_size Number of components for attribute 1 / texture
+ * coordinate. If this is 0, attribute 1 will not be set or
+ * enabled.
+ * \param color_size Number of components for attribute 1 / primary color.
+ * If this is 0, attribute 1 will not be set or enabled.
+ *
+ * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
+ * Use \c texcoord_size instead.
+ */
+static void
+setup_vertex_objects(GLuint *VAO, GLuint *VBO, bool use_generic_attributes,
+ unsigned vertex_size, unsigned texcoord_size,
+ unsigned color_size)
+{
+ if (*VAO == 0) {
+ assert(*VBO == 0);
+
+ /* create vertex array object */
+ _mesa_GenVertexArrays(1, VAO);
+ _mesa_BindVertexArray(*VAO);
+
+ /* create vertex array buffer */
+ _mesa_GenBuffers(1, VBO);
+ _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
+ _mesa_BufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vertex), NULL,
+ GL_DYNAMIC_DRAW);
+
+ /* setup vertex arrays */
+ if (use_generic_attributes) {
+ assert(color_size == 0);
+
+ _mesa_VertexAttribPointer(0, vertex_size, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(x));
+ _mesa_EnableVertexAttribArray(0);
+
+ if (texcoord_size > 0) {
+ _mesa_VertexAttribPointer(1, texcoord_size, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(tex));
+ _mesa_EnableVertexAttribArray(1);
+ }
+ } else {
+ _mesa_VertexPointer(vertex_size, GL_FLOAT, sizeof(struct vertex),
+ OFFSET(x));
+ _mesa_EnableClientState(GL_VERTEX_ARRAY);
+
+ if (texcoord_size > 0) {
+ _mesa_TexCoordPointer(texcoord_size, GL_FLOAT,
+ sizeof(struct vertex), OFFSET(tex));
+ _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
+ }
+
+ if (color_size > 0) {
+ _mesa_ColorPointer(color_size, GL_FLOAT,
+ sizeof(struct vertex), OFFSET(r));
+ _mesa_EnableClientState(GL_COLOR_ARRAY);
+ }
+ }
+ } else {
+ _mesa_BindVertexArray(*VAO);
+ _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
+ }
+}
+