intel/blorp: make upload_shader() return a bool indicating success or failure
[mesa.git] / src / mesa / drivers / common / meta_blit.c
index 07bd7e5016a265346b47198f13e68fa7c6aa5bd0..991d52f21e9e1e12e73fe161447f5a94600aae33 100644 (file)
@@ -105,12 +105,12 @@ setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
    }
 
    if (blit->msaa_shaders[shader_index]) {
-      _mesa_UseProgram(blit->msaa_shaders[shader_index]);
+      _mesa_meta_use_program(ctx, blit->msaa_shaders[shader_index]);
       /* Update the uniform values. */
       loc_src_width =
-         _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_width");
+         _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_width");
       loc_src_height =
-         _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_height");
+         _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_height");
       _mesa_Uniform1f(loc_src_width, src_rb->Width);
       _mesa_Uniform1f(loc_src_height, src_rb->Height);
       return;
@@ -168,8 +168,9 @@ setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
 
    static const char vs_source[] =
                                "#version 130\n"
-                               "in vec2 position;\n"
-                               "in vec3 textureCoords;\n"
+                               "#extension GL_ARB_explicit_attrib_location: enable\n"
+                               "layout(location = 0) in vec2 position;\n"
+                               "layout(location = 1) in vec3 textureCoords;\n"
                                "out vec2 texCoords;\n"
                                "flat out int layer;\n"
                                "void main()\n"
@@ -222,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,
@@ -236,9 +237,9 @@ setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
    _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");
+      _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_width");
    loc_src_height =
-      _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_height");
+      _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_height");
    _mesa_Uniform1f(loc_src_width, src_rb->Width);
    _mesa_Uniform1f(loc_src_height, src_rb->Height);
 
@@ -346,7 +347,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
    }
 
    if (blit->msaa_shaders[shader_index]) {
-      _mesa_UseProgram(blit->msaa_shaders[shader_index]);
+      _mesa_meta_use_program(ctx, blit->msaa_shaders[shader_index]);
       return;
    }
 
@@ -384,8 +385,9 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
 
       vs_source = ralloc_asprintf(mem_ctx,
                                   "#version 130\n"
-                                  "in vec2 position;\n"
-                                  "in %s textureCoords;\n"
+                                  "#extension GL_ARB_explicit_attrib_location: enable\n"
+                                  "layout(location = 0) in vec2 position;\n"
+                                  "layout(location = 1) in %s textureCoords;\n"
                                   "out %s texCoords;\n"
                                   "void main()\n"
                                   "{\n"
@@ -456,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 =
@@ -506,8 +517,9 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
 
       vs_source = ralloc_asprintf(mem_ctx,
                                   "#version 130\n"
-                                  "in vec2 position;\n"
-                                  "in %s textureCoords;\n"
+                                  "#extension GL_ARB_explicit_attrib_location: enable\n"
+                                  "layout(location = 0) in vec2 position;\n"
+                                  "layout(location = 1) in %s textureCoords;\n"
                                   "out %s texCoords;\n"
                                   "void main()\n"
                                   "{\n"
@@ -706,33 +718,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) {
@@ -807,7 +818,7 @@ blitframebuffer_texture(struct gl_context *ctx,
 }
 
 void
-_mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
+_mesa_meta_fb_tex_blit_begin(struct gl_context *ctx,
                              struct fb_tex_blit_state *blit)
 {
    /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
@@ -828,28 +839,34 @@ void
 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
                            struct fb_tex_blit_state *blit)
 {
+   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_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
-      _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
+      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
+                                &blit->baseLevelSave, false);
+      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
+                                &blit->maxLevelSave, false);
+   }
 
-      if (ctx->Extensions.ARB_stencil_texturing) {
-         const struct gl_texture_object *texObj =
-            _mesa_get_current_tex_object(ctx, target);
+   /* 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;
 
-         if (texObj->StencilSampling != blit->stencilSamplingSave)
-            _mesa_TexParameteri(target, GL_DEPTH_STENCIL_TEXTURE_MODE,
-                                blit->stencilSamplingSave ?
-                                   GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
-      }
+      _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE,
+                                &param, false);
    }
 
    _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save);
    _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, NULL);
-   _mesa_DeleteSamplers(1, &blit->samp_obj->Name);
-   blit->samp_obj = NULL;
+   _mesa_reference_sampler_object(ctx, &blit->samp_obj, NULL);
 
    if (blit->tempTex)
       _mesa_DeleteTextures(1, &blit->tempTex);
@@ -896,19 +913,17 @@ _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
 
 struct gl_sampler_object *
 _mesa_meta_setup_sampler(struct gl_context *ctx,
-                         const struct gl_texture_object *texObj,
+                         struct gl_texture_object *texObj,
                          GLenum target, GLenum filter, GLuint srcLevel)
 {
-   GLuint sampler;
    struct gl_sampler_object *samp_obj;
    GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
                         filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
                        GL_NEAREST : filter;
 
-   _mesa_GenSamplers(1, &sampler);
-
-   samp_obj = _mesa_lookup_samplerobj(ctx, sampler);
-   assert(samp_obj != NULL && samp_obj->Name == sampler);
+   samp_obj =  ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
+   if (samp_obj == NULL)
+      return NULL;
 
    _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj);
    _mesa_set_sampler_filters(ctx, samp_obj, tex_filter, tex_filter);
@@ -918,8 +933,10 @@ _mesa_meta_setup_sampler(struct gl_context *ctx,
    /* Prepare src texture state */
    _mesa_BindTexture(target, texObj->Name);
    if (target != GL_TEXTURE_RECTANGLE_ARB) {
-      _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
-      _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
+      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
+                                (GLint *) &srcLevel, false);
+      _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
+                                (GLint *) &srcLevel, false);
    }
 
    return samp_obj;
@@ -969,10 +986,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);
@@ -1028,8 +1047,8 @@ _mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
       _mesa_reference_buffer_object(ctx, &blit->buf_obj, NULL);
    }
 
-   _mesa_meta_blit_shader_table_cleanup(&blit->shaders_with_depth);
-   _mesa_meta_blit_shader_table_cleanup(&blit->shaders_without_depth);
+   _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_with_depth);
+   _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_without_depth);
 
    _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
    blit->depthTex.TexObj = 0;