+/**
+ * Compute the texture coordinates for the four vertices of a quad for
+ * drawing a 2D texture image or slice of a cube/3D texture.
+ * \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
+ * \param slice slice of a 1D/2D array texture or 3D texture
+ * \param width width of the texture image
+ * \param height height of the texture image
+ * \param coords0/1/2/3 returns the computed texcoords
+ */
+static void
+setup_texture_coords(GLenum faceTarget,
+ GLint slice,
+ GLint width,
+ GLint height,
+ GLfloat coords0[3],
+ GLfloat coords1[3],
+ GLfloat coords2[3],
+ GLfloat coords3[3])
+{
+ static const GLfloat st[4][2] = {
+ {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
+ };
+ GLuint i;
+ GLfloat r;
+
+ switch (faceTarget) {
+ case GL_TEXTURE_1D:
+ case GL_TEXTURE_2D:
+ case GL_TEXTURE_3D:
+ case GL_TEXTURE_2D_ARRAY:
+ if (faceTarget == GL_TEXTURE_3D)
+ r = 1.0F / slice;
+ else if (faceTarget == GL_TEXTURE_2D_ARRAY)
+ r = slice;
+ else
+ r = 0.0F;
+ coords0[0] = 0.0F; /* s */
+ coords0[1] = 0.0F; /* t */
+ coords0[2] = r; /* r */
+ coords1[0] = 1.0F;
+ coords1[1] = 0.0F;
+ coords1[2] = r;
+ coords2[0] = 1.0F;
+ coords2[1] = 1.0F;
+ coords2[2] = r;
+ coords3[0] = 0.0F;
+ coords3[1] = 1.0F;
+ coords3[2] = r;
+ break;
+ case GL_TEXTURE_RECTANGLE_ARB:
+ coords0[0] = 0.0F; /* s */
+ coords0[1] = 0.0F; /* t */
+ coords0[2] = 0.0F; /* r */
+ coords1[0] = width;
+ coords1[1] = 0.0F;
+ coords1[2] = 0.0F;
+ coords2[0] = width;
+ coords2[1] = height;
+ coords2[2] = 0.0F;
+ coords3[0] = 0.0F;
+ coords3[1] = height;
+ coords3[2] = 0.0F;
+ break;
+ case GL_TEXTURE_1D_ARRAY:
+ coords0[0] = 0.0F; /* s */
+ coords0[1] = slice; /* t */
+ coords0[2] = 0.0F; /* r */
+ coords1[0] = 1.0f;
+ coords1[1] = slice;
+ coords1[2] = 0.0F;
+ coords2[0] = 1.0F;
+ coords2[1] = slice;
+ coords2[2] = 0.0F;
+ coords3[0] = 0.0F;
+ coords3[1] = slice;
+ coords3[2] = 0.0F;
+ break;
+
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
+ /* loop over quad verts */
+ for (i = 0; i < 4; i++) {
+ /* Compute sc = +/-scale and tc = +/-scale.
+ * Not +/-1 to avoid cube face selection ambiguity near the edges,
+ * though that can still sometimes happen with this scale factor...
+ */
+ const GLfloat scale = 0.9999f;
+ const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
+ const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
+ GLfloat *coord;
+
+ switch (i) {
+ case 0:
+ coord = coords0;
+ break;
+ case 1:
+ coord = coords1;
+ break;
+ case 2:
+ coord = coords2;
+ break;
+ case 3:
+ coord = coords3;
+ break;
+ default:
+ assert(0);
+ }
+
+ switch (faceTarget) {
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+ coord[0] = 1.0f;
+ coord[1] = -tc;
+ coord[2] = -sc;
+ break;
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
+ coord[0] = -1.0f;
+ coord[1] = -tc;
+ coord[2] = sc;
+ break;
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
+ coord[0] = sc;
+ coord[1] = 1.0f;
+ coord[2] = tc;
+ break;
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
+ coord[0] = sc;
+ coord[1] = -1.0f;
+ coord[2] = -tc;
+ break;
+ case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
+ coord[0] = sc;
+ coord[1] = -tc;
+ coord[2] = 1.0f;
+ break;
+ case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
+ coord[0] = -sc;
+ coord[1] = -tc;
+ coord[2] = -1.0f;
+ break;
+ default:
+ assert(0);
+ }
+ }
+ break;
+ default:
+ assert(0 && "unexpected target in meta setup_texture_coords()");
+ }
+}
+
+
+static void
+setup_ff_generate_mipmap(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap)
+{
+ struct vertex {
+ GLfloat x, y, tex[3];
+ };
+
+ if (mipmap->ArrayObj == 0) {
+ /* one-time setup */
+ /* create vertex array object */
+ _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
+ _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
+
+ /* create vertex array buffer */
+ _mesa_GenBuffersARB(1, &mipmap->VBO);
+ _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+ /* setup vertex arrays */
+ _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
+ _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
+ _mesa_EnableClientState(GL_VERTEX_ARRAY);
+ _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
+ }
+
+ /* setup projection matrix */
+ _mesa_MatrixMode(GL_PROJECTION);
+ _mesa_LoadIdentity();
+ _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
+}
+
+
+static void
+setup_glsl_generate_mipmap(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap)
+{
+ struct vertex {
+ GLfloat x, y, tex[3];
+ };
+
+ static const char *vs_source =
+ "attribute vec2 position;\n"
+ "attribute vec3 textureCoords;\n"
+ "varying vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ "}\n";
+ static const char *fs_source =
+ "uniform sampler2D tex2d;\n"
+ "varying vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " gl_FragColor = texture2D(tex2d, texCoords.xy);\n"
+ "}\n";
+
+ static const char *vs_int_source =
+ "#version 130\n"
+ "in vec2 position;\n"
+ "in vec3 textureCoords;\n"
+ "out vec3 texCoords;\n"
+ "void main()\n"
+ "{\n"
+ " texCoords = textureCoords;\n"
+ " gl_Position = gl_Vertex;\n"
+ "}\n";
+ static const char *fs_int_source =
+ "#version 130\n"
+ "uniform isampler2D tex2d;\n"
+ "in vec3 texCoords;\n"
+ "out ivec4 out_color;\n"
+ "\n"
+ "void main()\n"
+ "{\n"
+ " out_color = texture(tex2d, texCoords.xy);\n"
+ "}\n";
+ GLuint vs, fs;
+
+ /* Check if already initialized */
+ if (mipmap->ArrayObj != 0)
+ return;
+ /* create vertex array object */
+ _mesa_GenVertexArrays(1, &mipmap->ArrayObj);
+ _mesa_BindVertexArray(mipmap->ArrayObj);
+
+ /* create vertex array buffer */
+ _mesa_GenBuffersARB(1, &mipmap->VBO);
+ _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+
+ /* setup vertex arrays */
+ _mesa_VertexAttribPointerARB(0, 2, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(x));
+ _mesa_VertexAttribPointerARB(1, 3, GL_FLOAT, GL_FALSE,
+ sizeof(struct vertex), OFFSET(tex));
+
+ vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
+ fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
+
+ mipmap->ShaderProg = _mesa_CreateProgramObjectARB();
+ _mesa_AttachShader(mipmap->ShaderProg, fs);
+ _mesa_DeleteObjectARB(fs);
+ _mesa_AttachShader(mipmap->ShaderProg, vs);
+ _mesa_DeleteObjectARB(vs);
+ _mesa_BindAttribLocationARB(mipmap->ShaderProg, 0, "position");
+ _mesa_BindAttribLocationARB(mipmap->ShaderProg, 1, "texcoords");
+ _mesa_EnableVertexAttribArrayARB(0);
+ _mesa_EnableVertexAttribArrayARB(1);
+ link_program_with_debug(ctx, mipmap->ShaderProg);
+
+ if ((_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130) ||
+ _mesa_is_gles3(ctx)){
+ vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_int_source);
+ fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_int_source);
+
+ mipmap->IntegerShaderProg = _mesa_CreateProgramObjectARB();
+ _mesa_AttachShader(mipmap->IntegerShaderProg, fs);
+ _mesa_DeleteObjectARB(fs);
+ _mesa_AttachShader(mipmap->IntegerShaderProg, vs);
+ _mesa_DeleteObjectARB(vs);
+ _mesa_BindAttribLocationARB(mipmap->IntegerShaderProg, 0, "position");
+ _mesa_BindAttribLocationARB(mipmap->IntegerShaderProg, 1, "texcoords");
+
+ /* Note that user-defined out attributes get automatically assigned
+ * locations starting from 0, so we don't need to explicitly
+ * BindFragDataLocation to 0.
+ */
+ link_program_with_debug(ctx, mipmap->IntegerShaderProg);
+ }
+}
+
+
+static void
+meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
+ struct gen_mipmap_state *mipmap)
+{
+ if (mipmap->ArrayObj == 0)
+ return;
+ _mesa_DeleteVertexArraysAPPLE(1, &mipmap->ArrayObj);
+ mipmap->ArrayObj = 0;
+ _mesa_DeleteBuffersARB(1, &mipmap->VBO);
+ mipmap->VBO = 0;
+ _mesa_DeleteObjectARB(mipmap->ShaderProg);
+ mipmap->ShaderProg = 0;
+
+ if (mipmap->IntegerShaderProg) {
+ _mesa_DeleteObjectARB(mipmap->IntegerShaderProg);
+ mipmap->IntegerShaderProg = 0;
+ }
+}
+
+