meta/blit: Don't return the texture handle from _mesa_meta_bind_rb_as_tex_image
[mesa.git] / src / mesa / drivers / common / meta_blit.c
index 0066f7f9184e86faeff90e47ad4e7218a71238b0..4e660d2a57140e7868c514761c88e3cadb2c1806 100644 (file)
@@ -59,7 +59,7 @@ 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)
+                                   GLenum target)
 {
    GLint loc_src_width, loc_src_height;
    int i, samples;
@@ -223,8 +223,8 @@ setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
                                "#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"
+                               "   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,
@@ -458,8 +458,17 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
          int step;
 
          if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
-            merge_function =
-               "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
+            /* From the OpenGL ES 3.2 spec section 16.2.1:
+             *
+             *    "If the source formats are integer types or stencil values,
+             *    a single sample's value is selected for each pixel."
+             *
+             * The OpenGL 4.4 spec contains exactly the same language.
+             *
+             * We can accomplish this by making the merge function return just
+             * one of the two samples.  The compiler should do the rest.
+             */
+            merge_function = "gvec4 merge(gvec4 a, gvec4 b) { return a; }\n";
          } else {
             /* The divide will happen at the end for floats. */
             merge_function =
@@ -572,7 +581,7 @@ setup_glsl_blit_framebuffer(struct gl_context *ctx,
                                    2, texcoord_size, 0);
 
    if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
-      setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target, filter);
+      setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target);
    } else if (is_target_multisample) {
       setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
    } else {
@@ -630,12 +639,12 @@ blitframebuffer_texture(struct gl_context *ctx,
        */
       srcLevel = readAtt->TextureLevel;
       texObj = readAtt->Texture;
-      target = texObj->Target;
    } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
-      if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &fb_tex_blit.tempTex,
-                                           &texObj, &target))
+      if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &texObj))
          return false;
 
+      fb_tex_blit.tempTex = texObj->Name;
+
       srcLevel = 0;
       if (_mesa_is_winsys_fbo(readFb)) {
          GLint temp = srcY0;
@@ -664,7 +673,6 @@ 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;
@@ -676,6 +684,7 @@ blitframebuffer_texture(struct gl_context *ctx,
                                        tex_base_format,
                                        filter);
 
+      assert(texObj->Target == meta_temp_texture->Target);
 
       srcX0 = 0;
       srcY0 = 0;
@@ -683,6 +692,7 @@ blitframebuffer_texture(struct gl_context *ctx,
       srcY1 = srcH;
    }
 
+   target = texObj->Target;
    fb_tex_blit.baseLevelSave = texObj->BaseLevel;
    fb_tex_blit.maxLevelSave = texObj->MaxLevel;
    fb_tex_blit.stencilSamplingSave = texObj->StencilSampling;
@@ -709,33 +719,32 @@ blitframebuffer_texture(struct gl_context *ctx,
    fb_tex_blit.samp_obj = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
                                                    srcLevel);
 
-   /* Always do our blits with no net sRGB decode or encode.
-    *
-    * However, if both the src and dst can be srgb decode/encoded, enable them
-    * so that we do any blending (from scaling or from MSAA resolves) in the
-    * right colorspace.
-    *
-    * Our choice of not doing any net encode/decode is from the GL 3.0
-    * specification:
-    *
-    *     "Blit operations bypass the fragment pipeline. The only fragment
-    *      operations which affect a blit are the pixel ownership test and the
-    *      scissor test."
-    *
-    * The GL 4.4 specification disagrees and says that the sRGB part of the
-    * fragment pipeline applies, but this was found to break applications.
-    */
    if (ctx->Extensions.EXT_texture_sRGB_decode) {
-      if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
-          drawFb->Visual.sRGBCapable) {
-         _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
-                                       GL_DECODE_EXT);
-         _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
-      } else {
-         _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
-                                       GL_SKIP_DECODE_EXT);
-         /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
-      }
+      /* The GL 4.4 spec, section 18.3.1 ("Blitting Pixel Rectangles") says:
+       *
+       *    "When values are taken from the read buffer, if FRAMEBUFFER_SRGB
+       *     is enabled and the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
+       *     for the framebuffer attachment corresponding to the read buffer
+       *     is SRGB (see section 9.2.3), the red, green, and blue components
+       *     are converted from the non-linear sRGB color space according to
+       *     equation 3.24.
+       *
+       *     When values are written to the draw buffers, blit operations
+       *     bypass most of the fragment pipeline.  The only fragment
+       *     operations which affect a blit are the pixel ownership test,
+       *     the scissor test, and sRGB conversion (see section 17.3.9)."
+       *
+       * ES 3.0 contains nearly the exact same text, but omits the part
+       * about GL_FRAMEBUFFER_SRGB as that doesn't exist in ES.  Mesa
+       * defaults it to on for ES contexts, so we can safely check it.
+       */
+      const bool decode =
+         ctx->Color.sRGBEnabled &&
+         _mesa_get_format_color_encoding(rb->Format) == GL_SRGB;
+
+      _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
+                                    decode ? GL_DECODE_EXT
+                                           : GL_SKIP_DECODE_EXT);
    }
 
    if (!glsl_version) {
@@ -792,8 +801,7 @@ blitframebuffer_texture(struct gl_context *ctx,
       verts[3].tex[1] = t1;
       verts[3].tex[2] = readAtt->Zoffset;
 
-      _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts,
-                            __func__);
+      _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts);
    }
 
    /* setup viewport */
@@ -834,26 +842,36 @@ _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
    struct gl_texture_object *const texObj =
       _mesa_get_current_tex_object(ctx, target);
 
-   /* Restore texture object state, the texture binding will
-    * be restored by _mesa_meta_end().
-    */
-   if (target != GL_TEXTURE_RECTANGLE_ARB) {
-      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
-                                &blit->baseLevelSave, false);
-      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
-                                &blit->maxLevelSave, false);
-   }
+   /* Either there is no temporary texture or the temporary texture is bound. */
+   assert(blit->tempTex == 0 || texObj->Name == blit->tempTex);
 
-   /* If ARB_stencil_texturing is not supported, the mode won't have changed. */
-   if (texObj->StencilSampling != blit->stencilSamplingSave) {
-      /* GLint so the compiler won't complain about type signedness mismatch
-       * in the call to _mesa_texture_parameteriv below.
+   /* Restore texture object state, the texture binding will be restored by
+    * _mesa_meta_end().  If the texture is the temporary texture that is about
+    * to be destroyed, don't bother restoring its state.
+    */
+   if (blit->tempTex == 0) {
+      /* If the target restricts values for base level or max level, we assume
+       * that the original values were valid.
        */
-      const GLint param = blit->stencilSamplingSave ?
-         GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
+      if (blit->baseLevelSave != texObj->BaseLevel)
+         _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
+                                   &blit->baseLevelSave, false);
+
+      if (blit->maxLevelSave != texObj->MaxLevel)
+         _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
+                                   &blit->maxLevelSave, false);
+
+      /* If ARB_stencil_texturing is not supported, the mode won't have changed. */
+      if (texObj->StencilSampling != blit->stencilSamplingSave) {
+         /* GLint so the compiler won't complain about type signedness mismatch
+          * in the call to _mesa_texture_parameteriv below.
+          */
+         const GLint param = blit->stencilSamplingSave ?
+            GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
 
-      _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE,
-                                &param, false);
+         _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE,
+                                   &param, false);
+      }
    }
 
    _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save);
@@ -867,31 +885,24 @@ _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
 GLboolean
 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
                                 struct gl_renderbuffer *rb,
-                                GLuint *tex,
-                                struct gl_texture_object **texObj,
-                                GLenum *target)
+                                struct gl_texture_object **texObj)
 {
    struct gl_texture_image *texImage;
    GLuint tempTex;
-
-   if (rb->NumSamples > 1)
-      *target = GL_TEXTURE_2D_MULTISAMPLE;
-   else
-      *target = GL_TEXTURE_2D;
+   const GLenum target = rb->NumSamples > 1
+      ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
 
    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);
+   _mesa_BindTexture(target, tempTex);
+   *texObj = _mesa_lookup_texture(ctx, tempTex);
+   texImage = _mesa_get_tex_image(ctx, *texObj, target, 0);
 
    if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
-      _mesa_DeleteTextures(1, tex);
+      _mesa_DeleteTextures(1, &tempTex);
       return false;
    }
 
@@ -900,6 +911,8 @@ _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
       ctx->Driver.FinishRenderTexture(ctx, rb);
    }
 
+   assert(target == (*texObj)->Target);
+   assert(tempTex == (*texObj)->Name);
    return true;
 }
 
@@ -978,10 +991,12 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
       return 0;
    }
 
-   /* Only scissor affects blit, but we're doing to set a custom scissor if
-    * necessary anyway, so save/clear state.
+   /* Only scissor and FRAMEBUFFER_SRGB affect blit.  Leave sRGB alone, but
+    * save restore scissor as we'll set a custom scissor if necessary.
     */
-   _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
+   _mesa_meta_begin(ctx, MESA_META_ALL &
+                         ~(MESA_META_DRAW_BUFFERS |
+                           MESA_META_FRAMEBUFFER_SRGB));
 
    /* Dithering shouldn't be performed for glBlitFramebuffer */
    _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);