+/**
+ * Given a user-specified texture base format, the actual gallium texture
+ * format and the current GL_DEPTH_MODE, return a texture swizzle.
+ *
+ * Consider the case where the user requests a GL_RGB internal texture
+ * format the driver actually uses an RGBA format. The A component should
+ * be ignored and sampling from the texture should always return (r,g,b,1).
+ * But if we rendered to the texture we might have written A values != 1.
+ * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
+ * This function computes the texture swizzle needed to get the expected
+ * values.
+ *
+ * In the case of depth textures, the GL_DEPTH_MODE state determines the
+ * texture swizzle.
+ *
+ * This result must be composed with the user-specified swizzle to get
+ * the final swizzle.
+ */
+static unsigned
+compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
+ enum pipe_format actualFormat,
+ unsigned glsl_version)
+{
+ switch (baseFormat) {
+ case GL_RGBA:
+ return SWIZZLE_XYZW;
+ case GL_RGB:
+ if (util_format_has_alpha(actualFormat))
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
+ else
+ return SWIZZLE_XYZW;
+ case GL_RG:
+ if (util_format_get_nr_components(actualFormat) > 2)
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
+ else
+ return SWIZZLE_XYZW;
+ case GL_RED:
+ if (util_format_get_nr_components(actualFormat) > 1)
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
+ SWIZZLE_ZERO, SWIZZLE_ONE);
+ else
+ return SWIZZLE_XYZW;
+ case GL_ALPHA:
+ if (util_format_get_nr_components(actualFormat) > 1)
+ return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
+ SWIZZLE_ZERO, SWIZZLE_W);
+ else
+ return SWIZZLE_XYZW;
+ case GL_LUMINANCE:
+ if (util_format_get_nr_components(actualFormat) > 1)
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
+ else
+ return SWIZZLE_XYZW;
+ case GL_LUMINANCE_ALPHA:
+ if (util_format_get_nr_components(actualFormat) > 2)
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
+ else
+ return SWIZZLE_XYZW;
+ case GL_INTENSITY:
+ if (util_format_get_nr_components(actualFormat) > 1)
+ return SWIZZLE_XXXX;
+ else
+ return SWIZZLE_XYZW;
+ case GL_STENCIL_INDEX:
+ return SWIZZLE_XYZW;
+ case GL_DEPTH_STENCIL:
+ /* fall-through */
+ case GL_DEPTH_COMPONENT:
+ /* Now examine the depth mode */
+ switch (depthMode) {
+ case GL_LUMINANCE:
+ return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);