Merge remote-tracking branch 'jekstrand/wip/i965-uniforms' into vulkan
[mesa.git] / src / mesa / drivers / common / meta_blit.c
index 496ce4588249653752ccfff35355b407c034abe0..b414dce88199e51747e7340b94e2c3fbf5da77f4 100644 (file)
@@ -325,7 +325,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
       }
       break;
    default:
-      _mesa_problem(ctx, "Unkown texture target %s\n",
+      _mesa_problem(ctx, "Unknown texture target %s\n",
                     _mesa_enum_to_string(target));
       shader_index = BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
    }
@@ -357,10 +357,16 @@ 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 *tex_coords = "texCoords";
 
       if (dst_is_msaa) {
          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 {
          /* From the GL 4.3 spec:
           *
@@ -392,17 +398,19 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                                   "#version 130\n"
                                   "#extension GL_ARB_texture_multisample : enable\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",
                                   sampler_array_suffix,
                                   texcoord_type,
                                   texcoord_type,
+                                  tex_coords,
                                   sample_index);
    } else {
       /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
@@ -415,7 +423,33 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                              dst_is_msaa ? "copy" : "resolve");
 
       if (dst_is_msaa) {
-         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;
@@ -486,6 +520,7 @@ setup_glsl_msaa_blit_shader(struct gl_context *ctx,
                                   "#version 130\n"
                                   "#extension GL_ARB_texture_multisample : enable\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"
@@ -530,7 +565,7 @@ setup_glsl_blit_framebuffer(struct gl_context *ctx,
 
    texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
 
-   _mesa_meta_setup_vertex_objects(&blit->VAO, &blit->VBO, true,
+   _mesa_meta_setup_vertex_objects(ctx, &blit->VAO, &blit->buf_obj, true,
                                    2, texcoord_size, 0);
 
    if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
@@ -656,8 +691,9 @@ blitframebuffer_texture(struct gl_context *ctx,
                                   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);
    }
 
@@ -667,8 +703,8 @@ blitframebuffer_texture(struct gl_context *ctx,
      printf("  srcTex %p  dstText %p\n", texObj, drawAtt->Texture);
    */
 
-   fb_tex_blit.sampler = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
-                                                  srcLevel);
+   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.
     *
@@ -689,13 +725,12 @@ blitframebuffer_texture(struct gl_context *ctx,
    if (ctx->Extensions.EXT_texture_sRGB_decode) {
       if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
           drawFb->Visual.sRGBCapable) {
-         _mesa_SamplerParameteri(fb_tex_blit.sampler,
-                                 GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
+         _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
+                                       GL_DECODE_EXT);
          _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
       } else {
-         _mesa_SamplerParameteri(fb_tex_blit.sampler,
-                                 GL_TEXTURE_SRGB_DECODE_EXT,
-                                 GL_SKIP_DECODE_EXT);
+         _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
+                                       GL_SKIP_DECODE_EXT);
          /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
       }
    }
@@ -754,7 +789,8 @@ blitframebuffer_texture(struct gl_context *ctx,
       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 */
@@ -771,12 +807,20 @@ 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)
 {
-   blit->samplerSave =
-      ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
-      ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
+   /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
+    * and both use stack variables.  If samp_obj_save is not NULL,
+    * _mesa_reference_sampler_object will try to dereference it.  Leaving
+    * random garbage in samp_obj_save can only lead to crashes.
+    *
+    * Since the state isn't persistent across calls, we won't catch ref
+    * counting problems.
+    */
+   blit->samp_obj_save = NULL;
+   _mesa_reference_sampler_object(ctx, &blit->samp_obj_save,
+                                  ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
    blit->tempTex = 0;
 }
 
@@ -802,8 +846,10 @@ _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
       }
    }
 
-   _mesa_BindSampler(ctx->Texture.CurrentUnit, blit->samplerSave);
-   _mesa_DeleteSamplers(1, &blit->sampler);
+   _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save);
+   _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, NULL);
+   _mesa_reference_sampler_object(ctx, &blit->samp_obj, NULL);
+
    if (blit->tempTex)
       _mesa_DeleteTextures(1, &blit->tempTex);
 }
@@ -847,31 +893,33 @@ _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
    return true;
 }
 
-GLuint
+struct gl_sampler_object *
 _mesa_meta_setup_sampler(struct gl_context *ctx,
                          const 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);
-   _mesa_BindSampler(ctx->Texture.CurrentUnit, 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);
+   _mesa_set_sampler_wrap(ctx, samp_obj, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
+                          samp_obj->WrapR);
 
    /* Prepare src texture state */
    _mesa_BindTexture(target, texObj->Name);
-   _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);
    }
-   _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-   _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
-   return sampler;
+   return samp_obj;
 }
 
 /**
@@ -969,13 +1017,12 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
 }
 
 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_with_depth);