i965: Only call brw_upload_tcs/tes_prog when using tessellation.
[mesa.git] / src / mesa / drivers / common / meta_blit.c
index beb1ea5a8cdc2c734638511a6c531320656ac106..4dbf0a763087da644192cf7d4f644a5064360da3 100644 (file)
 #include "main/texenv.h"
 #include "main/teximage.h"
 #include "main/texparam.h"
+#include "main/uniforms.h"
 #include "main/varray.h"
 #include "main/viewport.h"
 #include "swrast/swrast.h"
 #include "drivers/common/meta.h"
-#include "../glsl/ralloc.h"
+#include "util/ralloc.h"
 
 /** Return offset in bytes of the field within a vertex struct */
 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
 
+static void
+setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
+                                   struct blit_state *blit,
+                                   struct gl_renderbuffer *src_rb,
+                                   GLenum target, GLenum filter)
+{
+   GLint loc_src_width, loc_src_height;
+   int i, samples;
+   int shader_offset = 0;
+   void *mem_ctx = ralloc_context(NULL);
+   char *fs_source;
+   char *name, *sample_number;
+   const uint8_t *sample_map;
+   char *sample_map_str = rzalloc_size(mem_ctx, 1);
+   char *sample_map_expr = rzalloc_size(mem_ctx, 1);
+   char *texel_fetch_macro = rzalloc_size(mem_ctx, 1);
+   const char *sampler_array_suffix = "";
+   float x_scale, y_scale;
+   enum blit_msaa_shader shader_index;
+
+   assert(src_rb);
+   samples = MAX2(src_rb->NumSamples, 1);
+
+   if (samples == 16)
+      x_scale = 4.0;
+   else
+      x_scale = 2.0;
+   y_scale = samples / x_scale;
+
+   /* We expect only power of 2 samples in source multisample buffer. */
+   assert(samples > 0 && _mesa_is_pow_two(samples));
+   while (samples >> (shader_offset + 1)) {
+      shader_offset++;
+   }
+   /* Update the assert if we plan to support more than 16X MSAA. */
+   assert(shader_offset > 0 && shader_offset <= 4);
+
+   assert(target == GL_TEXTURE_2D_MULTISAMPLE ||
+          target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
+
+   shader_index = BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE +
+                  shader_offset - 1;
+
+   if (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
+      shader_index += BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_SCALED_RESOLVE -
+                      BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE;
+      sampler_array_suffix = "Array";
+   }
+
+   if (blit->msaa_shaders[shader_index]) {
+      _mesa_UseProgram(blit->msaa_shaders[shader_index]);
+      /* Update the uniform values. */
+      loc_src_width =
+         _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_width");
+      loc_src_height =
+         _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_height");
+      _mesa_Uniform1f(loc_src_width, src_rb->Width);
+      _mesa_Uniform1f(loc_src_height, src_rb->Height);
+      return;
+   }
+
+   name = ralloc_asprintf(mem_ctx, "vec4 MSAA scaled resolve");
+
+   /* Below switch is used to setup the shader expression, which computes
+    * sample index and map it to to a sample number on hardware.
+    */
+   switch(samples) {
+   case 2:
+      sample_number =  "sample_map[int(2 * fract(coord.x))]";
+      sample_map = ctx->Const.SampleMap2x;
+      break;
+   case 4:
+      sample_number =  "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
+      sample_map = ctx->Const.SampleMap4x;
+      break;
+   case 8:
+      sample_number =  "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
+      sample_map = ctx->Const.SampleMap8x;
+      break;
+   case 16:
+      sample_number =  "sample_map[int(4 * fract(coord.x) + 16 * fract(coord.y))]";
+      sample_map = ctx->Const.SampleMap16x;
+      break;
+   default:
+      sample_number = NULL;
+      sample_map = NULL;
+      _mesa_problem(ctx, "Unsupported sample count %d\n", samples);
+      unreachable("Unsupported sample count");
+   }
+
+   /* Create sample map string. */
+   for (i = 0 ; i < samples - 1; i++) {
+      ralloc_asprintf_append(&sample_map_str, "%d, ", sample_map[i]);
+   }
+   ralloc_asprintf_append(&sample_map_str, "%d", sample_map[samples - 1]);
+
+   /* Create sample map expression using above string. */
+   ralloc_asprintf_append(&sample_map_expr,
+                          "   const int sample_map[%d] = int[%d](%s);\n",
+                          samples, samples, sample_map_str);
+
+   if (target == GL_TEXTURE_2D_MULTISAMPLE) {
+      ralloc_asprintf_append(&texel_fetch_macro,
+                             "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec2(coord), %s);\n",
+                             sample_number);
+   } else {
+      ralloc_asprintf_append(&texel_fetch_macro,
+                             "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec3(coord, layer), %s);\n",
+                             sample_number);
+   }
+
+   static const char vs_source[] =
+                               "#version 130\n"
+                               "in vec2 position;\n"
+                               "in vec3 textureCoords;\n"
+                               "out vec2 texCoords;\n"
+                               "flat out int layer;\n"
+                               "void main()\n"
+                               "{\n"
+                               "   texCoords = textureCoords.xy;\n"
+                               "   layer = int(textureCoords.z);\n"
+                               "   gl_Position = vec4(position, 0.0, 1.0);\n"
+                               "}\n"
+      ;
+
+   fs_source = ralloc_asprintf(mem_ctx,
+                               "#version 130\n"
+                               "#extension GL_ARB_texture_multisample : enable\n"
+                               "uniform sampler2DMS%s texSampler;\n"
+                               "uniform float src_width, src_height;\n"
+                               "in vec2 texCoords;\n"
+                               "flat in int layer;\n"
+                               "out vec4 out_color;\n"
+                               "\n"
+                               "void main()\n"
+                               "{\n"
+                               "%s"
+                               "   vec2 interp;\n"
+                               "   const vec2 scale = vec2(%ff, %ff);\n"
+                               "   const vec2 scale_inv = vec2(%ff, %ff);\n"
+                               "   const vec2 s_0_offset = vec2(%ff, %ff);\n"
+                               "   vec2 s_0_coord, s_1_coord, s_2_coord, s_3_coord;\n"
+                               "   vec4 s_0_color, s_1_color, s_2_color, s_3_color;\n"
+                               "   vec4 x_0_color, x_1_color;\n"
+                               "   vec2 tex_coord = texCoords - s_0_offset;\n"
+                               "\n"
+                               "   tex_coord *= scale;\n"
+                               "   tex_coord.x = clamp(tex_coord.x, 0.0f, scale.x * src_width - 1.0f);\n"
+                               "   tex_coord.y = clamp(tex_coord.y, 0.0f, scale.y * src_height - 1.0f);\n"
+                               "   interp = fract(tex_coord);\n"
+                               "   tex_coord = ivec2(tex_coord) * scale_inv;\n"
+                               "\n"
+                               "   /* Compute the sample coordinates used for filtering. */\n"
+                               "   s_0_coord = tex_coord;\n"
+                               "   s_1_coord = tex_coord + vec2(scale_inv.x, 0.0f);\n"
+                               "   s_2_coord = tex_coord + vec2(0.0f, scale_inv.y);\n"
+                               "   s_3_coord = tex_coord + vec2(scale_inv.x, scale_inv.y);\n"
+                               "\n"
+                               "   /* Fetch sample color values. */\n"
+                               "%s"
+                               "   s_0_color = TEXEL_FETCH(s_0_coord)\n"
+                               "   s_1_color = TEXEL_FETCH(s_1_coord)\n"
+                               "   s_2_color = TEXEL_FETCH(s_2_coord)\n"
+                               "   s_3_color = TEXEL_FETCH(s_3_coord)\n"
+                               "#undef TEXEL_FETCH\n"
+                               "\n"
+                               "   /* Do bilinear filtering on sample colors. */\n"
+                               "   x_0_color =  mix(s_0_color, s_1_color, interp.x);\n"
+                               "   x_1_color =  mix(s_2_color, s_3_color, interp.x);\n"
+                               "   out_color = mix(x_0_color, x_1_color, interp.y);\n"
+                               "}\n",
+                               sampler_array_suffix,
+                               sample_map_expr,
+                               x_scale, y_scale,
+                               1.0f / x_scale, 1.0f / y_scale,
+                               0.5f / x_scale, 0.5f / y_scale,
+                               texel_fetch_macro);
+
+   _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
+                                       &blit->msaa_shaders[shader_index]);
+   loc_src_width =
+      _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_width");
+   loc_src_height =
+      _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_height");
+   _mesa_Uniform1f(loc_src_width, src_rb->Width);
+   _mesa_Uniform1f(loc_src_height, src_rb->Height);
+
+   ralloc_free(mem_ctx);
+}
+
 static void
 setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                             struct blit_state *blit,
+                            const struct gl_framebuffer *drawFb,
                             struct gl_renderbuffer *src_rb,
                             GLenum target)
 {
@@ -70,17 +262,29 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
    const char *sampler_array_suffix = "";
    char *name;
    const char *texcoord_type = "vec2";
+   int samples;
+   int shader_offset = 0;
 
    if (src_rb) {
+      samples = MAX2(src_rb->NumSamples, 1);
       src_datatype = _mesa_get_format_datatype(src_rb->Format);
    } else {
       /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
        * doesn't handle integer.
        */
+      samples = 1;
       src_datatype = GL_UNSIGNED_NORMALIZED;
    }
 
-   if (ctx->DrawBuffer->Visual.samples > 1) {
+   /* We expect only power of 2 samples in source multisample buffer. */
+   assert(samples > 0 && _mesa_is_pow_two(samples));
+   while (samples >> (shader_offset + 1)) {
+      shader_offset++;
+   }
+   /* Update the assert if we plan to support more than 16X MSAA. */
+   assert(shader_offset >= 0 && shader_offset <= 4);
+
+   if (drawFb->Visual.samples > 1) {
       /* If you're calling meta_BlitFramebuffer with the destination
        * multisampled, this is the only path that will work -- swrast and
        * CopyTexImage won't work on it either.
@@ -98,8 +302,8 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
    switch (target) {
    case GL_TEXTURE_2D_MULTISAMPLE:
    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
-      if (src_rb->_BaseFormat == GL_DEPTH_COMPONENT ||
-          src_rb->_BaseFormat == GL_DEPTH_STENCIL) {
+      if (src_rb && (src_rb->_BaseFormat == GL_DEPTH_COMPONENT ||
+          src_rb->_BaseFormat == GL_DEPTH_STENCIL)) {
          if (dst_is_msaa)
             shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY;
          else
@@ -107,33 +311,35 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
       } else {
          if (dst_is_msaa)
             shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY;
-         else
-            shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
+         else {
+            shader_index = BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE +
+                           shader_offset;
+         }
       }
 
       if (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
-         shader_index += (BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE -
-                          BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE);
+         shader_index += (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE -
+                          BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE);
          sampler_array_suffix = "Array";
          texcoord_type = "vec3";
       }
       break;
    default:
-      _mesa_problem(ctx, "Unkown texture target %s\n",
-                    _mesa_lookup_enum_by_nr(target));
-      shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
+      _mesa_problem(ctx, "Unknown texture target %s\n",
+                    _mesa_enum_to_string(target));
+      shader_index = BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
    }
 
    /* We rely on the enum being sorted this way. */
-   STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT ==
-                 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 1);
-   STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT ==
-                 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 2);
+   STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT ==
+                 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 5);
+   STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT ==
+                 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 10);
    if (src_datatype == GL_INT) {
-      shader_index++;
+      shader_index += 5;
       vec4_prefix = "i";
    } else if (src_datatype == GL_UNSIGNED_INT) {
-      shader_index += 2;
+      shader_index += 10;
       vec4_prefix = "u";
    } else {
       vec4_prefix = "";
@@ -151,17 +357,17 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
        shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY ||
        shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY) {
       char *sample_index;
-      const char *arb_sample_shading_extension_string;
+      const char *tex_coords = "texCoords";
 
       if (dst_is_msaa) {
-         arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
          sample_index = "gl_SampleID";
          name = "depth MSAA copy";
+
+         if (ctx->Extensions.ARB_gpu_shader5 && samples >= 16) {
+            /* See comment below for the color copy */
+            tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
+         }
       } else {
-         /* Don't need that extension, since we're drawing to a single-sampled
-          * destination.
-          */
-         arb_sample_shading_extension_string = "";
          /* From the GL 4.3 spec:
           *
           *     "If there is a multisample buffer (the value of SAMPLE_BUFFERS
@@ -191,37 +397,59 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
       fs_source = ralloc_asprintf(mem_ctx,
                                   "#version 130\n"
                                   "#extension GL_ARB_texture_multisample : enable\n"
-                                  "%s\n"
+                                  "#extension GL_ARB_sample_shading : enable\n"
+                                  "#extension GL_ARB_gpu_shader5 : enable\n"
                                   "uniform sampler2DMS%s texSampler;\n"
                                   "in %s texCoords;\n"
                                   "out vec4 out_color;\n"
                                   "\n"
                                   "void main()\n"
                                   "{\n"
-                                  "   gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
+                                  "   gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
                                   "}\n",
-                                  arb_sample_shading_extension_string,
                                   sampler_array_suffix,
                                   texcoord_type,
                                   texcoord_type,
+                                  tex_coords,
                                   sample_index);
    } else {
       /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
        * sample).  Yes, this is ridiculous.
        */
-      int samples;
       char *sample_resolve;
-      const char *arb_sample_shading_extension_string;
       const char *merge_function;
       name = ralloc_asprintf(mem_ctx, "%svec4 MSAA %s",
                              vec4_prefix,
                              dst_is_msaa ? "copy" : "resolve");
 
-      samples = MAX2(src_rb->NumSamples, 1);
-
       if (dst_is_msaa) {
-         arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
-         sample_resolve = ralloc_asprintf(mem_ctx, "   out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type);
+         const char *tex_coords;
+
+         if (ctx->Extensions.ARB_gpu_shader5 && samples >= 16) {
+            /* If interpolateAtOffset is available then it will be used to
+             * force the interpolation to the center. This is required at
+             * least on Intel hardware because it is possible to have a sample
+             * position on the 0 x or y axis which means it will lie exactly
+             * on the pixel boundary. If we let the hardware interpolate the
+             * coordinates at one of these positions then it is possible for
+             * it to jump to a neighboring texel when converting to ints due
+             * to rounding errors. This is only done for >= 16x MSAA because
+             * it probably has some overhead. It is more likely that some
+             * hardware will use one of these problematic positions at 16x
+             * MSAA because in that case in D3D they are defined to be at
+             * these positions.
+             */
+            tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
+         } else {
+            tex_coords = "texCoords";
+         }
+
+         sample_resolve =
+            ralloc_asprintf(mem_ctx,
+                            "   out_color = texelFetch(texSampler, "
+                            "i%s(%s), gl_SampleID);",
+                            texcoord_type, tex_coords);
+
          merge_function = "";
       } else {
          int i;
@@ -236,8 +464,6 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
          }
 
-         arb_sample_shading_extension_string = "";
-
          /* We're assuming power of two samples for this resolution procedure.
           *
           * To avoid losing any floating point precision if the samples all
@@ -245,7 +471,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
           * (so the floating point exponent just gets increased), rather than
           * doing a naive sum and dividing.
           */
-         assert((samples & (samples - 1)) == 0);
+         assert(_mesa_is_pow_two(samples));
          /* Fetch each individual sample. */
          sample_resolve = rzalloc_size(mem_ctx, 1);
          for (i = 0; i < samples; i++) {
@@ -273,7 +499,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                                    samples);
          } else {
             ralloc_asprintf_append(&sample_resolve,
-                                   "   out_color = sample_%d_0 / %f;\n",
+                                   "   gl_FragColor = sample_%d_0 / %f;\n",
                                    samples, (float)samples);
          }
       }
@@ -293,7 +519,8 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
       fs_source = ralloc_asprintf(mem_ctx,
                                   "#version 130\n"
                                   "#extension GL_ARB_texture_multisample : enable\n"
-                                  "%s\n"
+                                  "#extension GL_ARB_sample_shading : enable\n"
+                                  "#extension GL_ARB_gpu_shader5 : enable\n"
                                   "#define gvec4 %svec4\n"
                                   "uniform %ssampler2DMS%s texSampler;\n"
                                   "in %s texCoords;\n"
@@ -304,7 +531,6 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                                   "{\n"
                                   "%s\n" /* sample_resolve */
                                   "}\n",
-                                  arb_sample_shading_extension_string,
                                   vec4_prefix,
                                   vec4_prefix,
                                   sampler_array_suffix,
@@ -322,19 +548,34 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
 static void
 setup_glsl_blit_framebuffer(struct gl_context *ctx,
                             struct blit_state *blit,
+                            const struct gl_framebuffer *drawFb,
                             struct gl_renderbuffer *src_rb,
-                            GLenum target)
+                            GLenum target, GLenum filter,
+                            bool is_scaled_blit,
+                            bool do_depth)
 {
+   unsigned texcoord_size;
+   bool is_target_multisample = target == GL_TEXTURE_2D_MULTISAMPLE ||
+                                target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
+   bool is_filter_scaled_resolve = filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
+                                   filter == GL_SCALED_RESOLVE_NICEST_EXT;
+
    /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
    assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
 
-   _mesa_meta_setup_vertex_objects(&blit->VAO, &blit->VBO, true, 2, 2, 0);
+   texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
+
+   _mesa_meta_setup_vertex_objects(ctx, &blit->VAO, &blit->buf_obj, true,
+                                   2, texcoord_size, 0);
 
-   if (target == GL_TEXTURE_2D_MULTISAMPLE ||
-       target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
-      setup_glsl_msaa_blit_shader(ctx, blit, src_rb, target);
+   if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
+      setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target, filter);
+   } else if (is_target_multisample) {
+      setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
    } else {
-      _mesa_meta_setup_blit_shader(ctx, target, &blit->shaders);
+      _mesa_meta_setup_blit_shader(ctx, target, do_depth,
+                                   do_depth ? &blit->shaders_with_depth
+                                            : &blit->shaders_without_depth);
    }
 }
 
@@ -346,12 +587,13 @@ setup_glsl_blit_framebuffer(struct gl_context *ctx,
  */
 static bool
 blitframebuffer_texture(struct gl_context *ctx,
+                        const struct gl_framebuffer *readFb,
+                        const struct gl_framebuffer *drawFb,
                         GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                         GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                         GLenum filter, GLint flipX, GLint flipY,
                         GLboolean glsl_version, GLboolean do_depth)
 {
-   const struct gl_framebuffer *readFb = ctx->ReadBuffer;
    int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
    const struct gl_renderbuffer_attachment *readAtt =
       &readFb->Attachment[att_index];
@@ -361,6 +603,9 @@ blitframebuffer_texture(struct gl_context *ctx,
    const GLint dstY = MIN2(dstY0, dstY1);
    const GLint dstW = abs(dstX1 - dstX0);
    const GLint dstH = abs(dstY1 - dstY0);
+   const int srcW = abs(srcX1 - srcX0);
+   const int srcH = abs(srcY1 - srcY0);
+   bool scaled_blit = false;
    struct gl_texture_object *texObj;
    GLuint srcLevel;
    GLenum target;
@@ -370,11 +615,6 @@ blitframebuffer_texture(struct gl_context *ctx,
    if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
       return false;
 
-   if (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
-       filter == GL_SCALED_RESOLVE_NICEST_EXT) {
-      filter = GL_LINEAR;
-   }
-
    _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
 
    if (readAtt->Texture &&
@@ -402,8 +642,6 @@ blitframebuffer_texture(struct gl_context *ctx,
       }
    } else {
       GLenum tex_base_format;
-      int srcW = abs(srcY1 - srcY0);
-      int srcH = abs(srcY1 - srcY0);
       /* Fall back to doing a CopyTexSubImage to get the destination
        * renderbuffer into a texture.
        */
@@ -425,6 +663,9 @@ blitframebuffer_texture(struct gl_context *ctx,
       srcLevel = 0;
       target = meta_temp_texture->Target;
       texObj = _mesa_lookup_texture(ctx, meta_temp_texture->TexObj);
+      if (texObj == NULL) {
+         return false;
+      }
 
       _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
                                        srcX0, srcY0,
@@ -441,13 +682,18 @@ blitframebuffer_texture(struct gl_context *ctx,
 
    fb_tex_blit.baseLevelSave = texObj->BaseLevel;
    fb_tex_blit.maxLevelSave = texObj->MaxLevel;
+   fb_tex_blit.stencilSamplingSave = texObj->StencilSampling;
+
+   scaled_blit = dstW != srcW || dstH != srcH;
 
    if (glsl_version) {
-      setup_glsl_blit_framebuffer(ctx, blit, rb, target);
+      setup_glsl_blit_framebuffer(ctx, blit, drawFb, rb, target, filter, scaled_blit,
+                                  do_depth);
    }
    else {
-      _mesa_meta_setup_ff_tnl_for_blit(&ctx->Meta->Blit.VAO,
-                                       &ctx->Meta->Blit.VBO,
+      _mesa_meta_setup_ff_tnl_for_blit(ctx,
+                                       &ctx->Meta->Blit.VAO,
+                                       &ctx->Meta->Blit.buf_obj,
                                        2);
    }
 
@@ -478,7 +724,7 @@ blitframebuffer_texture(struct gl_context *ctx,
     */
    if (ctx->Extensions.EXT_texture_sRGB_decode) {
       if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
-          ctx->DrawBuffer->Visual.sRGBCapable) {
+          drawFb->Visual.sRGBCapable) {
          _mesa_SamplerParameteri(fb_tex_blit.sampler,
                                  GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
          _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
@@ -502,7 +748,7 @@ blitframebuffer_texture(struct gl_context *ctx,
 
       if (target == GL_TEXTURE_2D) {
          const struct gl_texture_image *texImage
-            = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
+            = _mesa_select_tex_image(texObj, target, srcLevel);
          s0 = srcX0 / (float) texImage->Width;
          s1 = srcX1 / (float) texImage->Width;
          t0 = srcY0 / (float) texImage->Height;
@@ -533,14 +779,19 @@ blitframebuffer_texture(struct gl_context *ctx,
 
       verts[0].tex[0] = s0;
       verts[0].tex[1] = t0;
+      verts[0].tex[2] = readAtt->Zoffset;
       verts[1].tex[0] = s1;
       verts[1].tex[1] = t0;
+      verts[1].tex[2] = readAtt->Zoffset;
       verts[2].tex[0] = s1;
       verts[2].tex[1] = t1;
+      verts[2].tex[2] = readAtt->Zoffset;
       verts[3].tex[0] = s0;
       verts[3].tex[1] = t1;
+      verts[3].tex[2] = readAtt->Zoffset;
 
-      _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+      _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts,
+                            __func__);
    }
 
    /* setup viewport */
@@ -567,7 +818,7 @@ _mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
 }
 
 void
-_mesa_meta_fb_tex_blit_end(const struct gl_context *ctx, GLenum target,
+_mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
                            struct fb_tex_blit_state *blit)
 {
    /* Restore texture object state, the texture binding will
@@ -576,6 +827,16 @@ _mesa_meta_fb_tex_blit_end(const struct gl_context *ctx, GLenum target,
    if (target != GL_TEXTURE_RECTANGLE_ARB) {
       _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
       _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
+
+      if (ctx->Extensions.ARB_stencil_texturing) {
+         const struct gl_texture_object *texObj =
+            _mesa_get_current_tex_object(ctx, target);
+
+         if (texObj->StencilSampling != blit->stencilSamplingSave)
+            _mesa_TexParameteri(target, GL_DEPTH_STENCIL_TEXTURE_MODE,
+                                blit->stencilSamplingSave ?
+                                   GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
+      }
    }
 
    _mesa_BindSampler(ctx->Texture.CurrentUnit, blit->samplerSave);
@@ -592,13 +853,20 @@ _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
                                 GLenum *target)
 {
    struct gl_texture_image *texImage;
+   GLuint tempTex;
 
    if (rb->NumSamples > 1)
       *target = GL_TEXTURE_2D_MULTISAMPLE;
    else
       *target = GL_TEXTURE_2D;
 
-   _mesa_GenTextures(1, tex);
+   tempTex = 0;
+   _mesa_GenTextures(1, &tempTex);
+   if (tempTex == 0)
+      return false;
+
+   *tex = tempTex;
+
    _mesa_BindTexture(*target, *tex);
    *texObj = _mesa_lookup_texture(ctx, *tex);
    texImage = _mesa_get_tex_image(ctx, *texObj, *target, 0);
@@ -622,14 +890,17 @@ _mesa_meta_setup_sampler(struct gl_context *ctx,
                          GLenum target, GLenum filter, GLuint srcLevel)
 {
    GLuint sampler;
+   GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
+                        filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
+                       GL_NEAREST : filter;
 
    _mesa_GenSamplers(1, &sampler);
    _mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
 
    /* Prepare src texture state */
    _mesa_BindTexture(target, texObj->Name);
-   _mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, filter);
-   _mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, filter);
+   _mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, tex_filter);
+   _mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, tex_filter);
    if (target != GL_TEXTURE_RECTANGLE_ARB) {
       _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
       _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
@@ -644,8 +915,10 @@ _mesa_meta_setup_sampler(struct gl_context *ctx,
  * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
  * of texture mapping and polygon rendering.
  */
-void
+GLbitfield
 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
+                           const struct gl_framebuffer *readFb,
+                           const struct gl_framebuffer *drawFb,
                            GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                            GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                            GLbitfield mask, GLenum filter)
@@ -667,18 +940,19 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
                                       ctx->Extensions.ARB_fragment_shader;
 
    /* Multisample texture blit support requires texture multisample. */
-   if (ctx->ReadBuffer->Visual.samples > 0 &&
+   if (readFb->Visual.samples > 0 &&
        !ctx->Extensions.ARB_texture_multisample) {
-      goto fallback;
+      return mask;
    }
 
    /* Clip a copy of the blit coordinates. If these differ from the input
     * coordinates, then we'll set the scissor.
     */
-   if (!_mesa_clip_blit(ctx, &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
+   if (!_mesa_clip_blit(ctx, readFb, drawFb,
+                        &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
                         &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
       /* clipped/scissored everything away */
-      return;
+      return 0;
    }
 
    /* Only scissor affects blit, but we're doing to set a custom scissor if
@@ -686,6 +960,9 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
     */
    _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
 
+   /* Dithering shouldn't be performed for glBlitFramebuffer */
+   _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
+
    /* If the clipping earlier changed the destination rect at all, then
     * enable the scissor to clip to it.
     */
@@ -700,28 +977,22 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
 
    /* Try faster, direct texture approach first */
    if (mask & GL_COLOR_BUFFER_BIT) {
-      if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
+      if (blitframebuffer_texture(ctx, readFb, drawFb,
+                                  srcX0, srcY0, srcX1, srcY1,
                                   dstX0, dstY0, dstX1, dstY1,
                                   filter, dstFlipX, dstFlipY,
                                   use_glsl_version, false)) {
          mask &= ~GL_COLOR_BUFFER_BIT;
-         if (mask == 0x0) {
-            _mesa_meta_end(ctx);
-            return;
-         }
       }
    }
 
    if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
-      if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
+      if (blitframebuffer_texture(ctx, readFb, drawFb,
+                                  srcX0, srcY0, srcX1, srcY1,
                                   dstX0, dstY0, dstX1, dstY1,
                                   filter, dstFlipX, dstFlipY,
                                   use_glsl_version, true)) {
          mask &= ~GL_DEPTH_BUFFER_BIT;
-         if (mask == 0x0) {
-            _mesa_meta_end(ctx);
-            return;
-         }
       }
    }
 
@@ -731,25 +1002,44 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
 
    _mesa_meta_end(ctx);
 
-fallback:
-   if (mask) {
-      _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
-                              dstX0, dstY0, dstX1, dstY1, mask, filter);
-   }
+   return mask;
 }
 
 void
-_mesa_meta_glsl_blit_cleanup(struct blit_state *blit)
+_mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
 {
    if (blit->VAO) {
       _mesa_DeleteVertexArrays(1, &blit->VAO);
       blit->VAO = 0;
-      _mesa_DeleteBuffers(1, &blit->VBO);
-      blit->VBO = 0;
+      _mesa_reference_buffer_object(ctx, &blit->buf_obj, NULL);
    }
 
-   _mesa_meta_blit_shader_table_cleanup(&blit->shaders);
+   _mesa_meta_blit_shader_table_cleanup(&blit->shaders_with_depth);
+   _mesa_meta_blit_shader_table_cleanup(&blit->shaders_without_depth);
 
    _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
    blit->depthTex.TexObj = 0;
 }
+
+void
+_mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
+                                      struct gl_framebuffer *readFb,
+                                      struct gl_framebuffer *drawFb,
+                                      GLint srcX0, GLint srcY0,
+                                      GLint srcX1, GLint srcY1,
+                                      GLint dstX0, GLint dstY0,
+                                      GLint dstX1, GLint dstY1,
+                                      GLbitfield mask, GLenum filter)
+{
+   mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
+                                     srcX0, srcY0, srcX1, srcY1,
+                                     dstX0, dstY0, dstX1, dstY1,
+                                     mask, filter);
+   if (mask == 0x0)
+      return;
+
+   _swrast_BlitFramebuffer(ctx, readFb, drawFb,
+                           srcX0, srcY0, srcX1, srcY1,
+                           dstX0, dstY0, dstX1, dstY1,
+                           mask, filter);
+}