st/mesa: set compressed_data to NULL when freed
[mesa.git] / src / mesa / state_tracker / st_texture.c
index 9f57cfb3d964713204715cb79589b8ef32c53333..a2a310daab4cfe640209edb8ac57441c09bbe58c 100644 (file)
@@ -1,8 +1,8 @@
 /**************************************************************************
- * 
+ *
  * Copyright 2007 VMware, Inc.
  * All Rights Reserved.
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the
  * "Software"), to deal in the Software without restriction, including
  * distribute, sub license, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice (including the
  * next paragraph) shall be included in all copies or substantial portions
  * of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
@@ -22,7 +22,7 @@
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * 
+ *
  **************************************************************************/
 
 #include <stdio.h>
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "util/u_inlines.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_rect.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
+#include "tgsi/tgsi_from_mesa.h"
 
 
 #define DBG if(0) printf
 struct pipe_resource *
 st_texture_create(struct st_context *st,
                   enum pipe_texture_target target,
-                 enum pipe_format format,
-                 GLuint last_level,
-                 GLuint width0,
-                 GLuint height0,
-                 GLuint depth0,
+                  enum pipe_format format,
+                  GLuint last_level,
+                  GLuint width0,
+                  GLuint height0,
+                  GLuint depth0,
                   GLuint layers,
                   GLuint nr_samples,
-                  GLuint bind )
+                  GLuint bind)
 {
    struct pipe_resource pt, *newtex;
    struct pipe_screen *screen = st->pipe->screen;
@@ -74,11 +75,11 @@ st_texture_create(struct st_context *st,
    if (target == PIPE_TEXTURE_CUBE)
       assert(layers == 6);
 
-   DBG("%s target %d format %s last_level %d\n", __FUNCTION__,
+   DBG("%s target %d format %s last_level %d\n", __func__,
        (int) target, util_format_name(format), last_level);
 
    assert(format);
-   assert(screen->is_format_supported(screen, format, target, 0,
+   assert(screen->is_format_supported(screen, format, target, 0, 0,
                                       PIPE_BIND_SAMPLER_VIEW));
 
    memset(&pt, 0, sizeof(pt));
@@ -88,11 +89,13 @@ st_texture_create(struct st_context *st,
    pt.width0 = width0;
    pt.height0 = height0;
    pt.depth0 = depth0;
-   pt.array_size = (target == PIPE_TEXTURE_CUBE ? 6 : layers);
+   pt.array_size = layers;
    pt.usage = PIPE_USAGE_DEFAULT;
    pt.bind = bind;
-   pt.flags = 0;
+   /* only set this for OpenGL textures, not renderbuffers */
+   pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
    pt.nr_samples = nr_samples;
+   pt.nr_storage_samples = nr_samples;
 
    newtex = screen->resource_create(screen, &pt);
 
@@ -110,13 +113,13 @@ st_texture_create(struct st_context *st,
  */
 void
 st_gl_texture_dims_to_pipe_dims(GLenum texture,
-                                GLuint widthIn,
-                                GLuint heightIn,
-                                GLuint depthIn,
-                                GLuint *widthOut,
-                                GLuint *heightOut,
-                                GLuint *depthOut,
-                                GLuint *layersOut)
+                                unsigned widthIn,
+                                uint16_t heightIn,
+                                uint16_t depthIn,
+                                unsigned *widthOut,
+                                uint16_t *heightOut,
+                                uint16_t *depthOut,
+                                uint16_t *layersOut)
 {
    switch (texture) {
    case GL_TEXTURE_1D:
@@ -177,7 +180,7 @@ st_gl_texture_dims_to_pipe_dims(GLenum texture,
       *widthOut = widthIn;
       *heightOut = heightIn;
       *depthOut = 1;
-      *layersOut = depthIn;
+      *layersOut = util_align_npot(depthIn, 6);
       break;
    default:
       assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
@@ -197,19 +200,21 @@ st_gl_texture_dims_to_pipe_dims(GLenum texture,
  * Check if a texture image can be pulled into a unified mipmap texture.
  */
 GLboolean
-st_texture_match_image(const struct pipe_resource *pt,
+st_texture_match_image(struct st_context *st,
+                       const struct pipe_resource *pt,
                        const struct gl_texture_image *image)
 {
-   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
+   unsigned ptWidth;
+   uint16_t ptHeight, ptDepth, ptLayers;
 
-   /* Images with borders are never pulled into mipmap textures. 
+   /* Images with borders are never pulled into mipmap textures.
     */
-   if (image->Border) 
+   if (image->Border)
       return GL_FALSE;
 
    /* Check if this image's format matches the established texture's format.
     */
-   if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
+   if (st_mesa_format_to_pipe_format(st, image->TexFormat) != pt->format)
       return GL_FALSE;
 
    st_gl_texture_dims_to_pipe_dims(image->TexObject->Target,
@@ -225,6 +230,9 @@ st_texture_match_image(const struct pipe_resource *pt,
        ptLayers != pt->array_size)
       return GL_FALSE;
 
+   if (image->Level > pt->last_level)
+      return GL_FALSE;
+
    return GL_TRUE;
 }
 
@@ -249,7 +257,7 @@ st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
    GLuint level;
    void *map;
 
-   DBG("%s \n", __FUNCTION__);
+   DBG("%s \n", __func__);
 
    if (!stImage->pt)
       return NULL;
@@ -259,6 +267,13 @@ st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
    else
       level = stImage->base.Level;
 
+   if (stObj->base.Immutable) {
+      level += stObj->base.MinLevel;
+      z += stObj->base.MinLayer;
+      if (stObj->pt->array_size > 1)
+         d = MIN2(d, stObj->base.NumLayers);
+   }
+
    z += stImage->base.Face;
 
    map = pipe_transfer_map_3d(st->pipe, stImage->pt, level, usage,
@@ -288,55 +303,21 @@ st_texture_image_unmap(struct st_context *st,
                        struct st_texture_image *stImage, unsigned slice)
 {
    struct pipe_context *pipe = st->pipe;
-   struct pipe_transfer **transfer =
-      &stImage->transfer[slice + stImage->base.Face].transfer;
+   struct st_texture_object *stObj =
+      st_texture_object(stImage->base.TexObject);
+   struct pipe_transfer **transfer;
 
-   DBG("%s\n", __FUNCTION__);
+   if (stObj->base.Immutable)
+      slice += stObj->base.MinLayer;
+   transfer = &stImage->transfer[slice + stImage->base.Face].transfer;
+
+   DBG("%s\n", __func__);
 
    pipe_transfer_unmap(pipe, *transfer);
    *transfer = NULL;
 }
 
 
-/* Upload data for a particular image.
- */
-void
-st_texture_image_data(struct st_context *st,
-                      struct pipe_resource *dst,
-                      GLuint face,
-                      GLuint level,
-                      void *src,
-                      GLuint src_row_stride, GLuint src_image_stride)
-{
-   struct pipe_context *pipe = st->pipe;
-   GLuint i;
-   const GLubyte *srcUB = src;
-   GLuint layers;
-
-   if (dst->target == PIPE_TEXTURE_1D_ARRAY ||
-       dst->target == PIPE_TEXTURE_2D_ARRAY ||
-       dst->target == PIPE_TEXTURE_CUBE_ARRAY)
-      layers = dst->array_size;
-   else
-      layers = u_minify(dst->depth0, level);
-
-   DBG("%s\n", __FUNCTION__);
-
-   for (i = 0; i < layers; i++) {
-      struct pipe_box box;
-      u_box_2d_zslice(0, 0, face + i,
-                      u_minify(dst->width0, level),
-                      u_minify(dst->height0, level),
-                      &box);
-
-      pipe->transfer_inline_write(pipe, dst, level, PIPE_TRANSFER_WRITE,
-                                  &box, srcUB, src_row_stride, 0);
-
-      srcUB += src_image_stride;
-   }
-}
-
-
 /**
  * For debug only: get/print center pixel in the src resource.
  */
@@ -434,8 +415,8 @@ st_create_color_map_texture(struct gl_context *ctx)
 
    /* find an RGBA texture format */
    format = st_choose_format(st, GL_RGBA, GL_NONE, GL_NONE,
-                             PIPE_TEXTURE_2D, 0, PIPE_BIND_SAMPLER_VIEW,
-                             FALSE);
+                             PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW,
+                             false, false);
 
    /* create texture for color map/table */
    pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
@@ -443,85 +424,217 @@ st_create_color_map_texture(struct gl_context *ctx)
    return pt;
 }
 
+
 /**
- * Try to find a matching sampler view for the given context.
- * If none is found an empty slot is initialized with a
- * template and returned instead.
+ * Destroy bound texture handles for the given stage.
  */
-struct pipe_sampler_view **
-st_texture_get_sampler_view(struct st_context *st,
-                            struct st_texture_object *stObj)
+static void
+st_destroy_bound_texture_handles_per_stage(struct st_context *st,
+                                           enum pipe_shader_type shader)
 {
-   struct pipe_sampler_view *used = NULL, **free = NULL;
-   GLuint i;
+   struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
+   struct pipe_context *pipe = st->pipe;
+   unsigned i;
 
-   for (i = 0; i < stObj->num_sampler_views; ++i) {
-      struct pipe_sampler_view **sv = &stObj->sampler_views[i];
-      /* Is the array entry used ? */
-      if (*sv) {
-         /* Yes, check if it's the right one */
-         if ((*sv)->context == st->pipe)
-            return sv;
-
-         /* Wasn't the right one, but remember it as template */
-         used = *sv;
-      } else {
-         /* Found a free slot, remember that */
-         free = sv;
-      }
+   if (likely(!bound_handles->num_handles))
+      return;
+
+   for (i = 0; i < bound_handles->num_handles; i++) {
+      uint64_t handle = bound_handles->handles[i];
+
+      pipe->make_texture_handle_resident(pipe, handle, false);
+      pipe->delete_texture_handle(pipe, handle);
    }
+   free(bound_handles->handles);
+   bound_handles->handles = NULL;
+   bound_handles->num_handles = 0;
+}
 
-   /* Couldn't find a slot for our context, create a new one */
 
-   if (!free) {
-      /* Haven't even found a free one, resize the array */
-      GLuint old_size = stObj->num_sampler_views * sizeof(void *);
-      GLuint new_size = old_size + sizeof(void *);
-      stObj->sampler_views = REALLOC(stObj->sampler_views, old_size, new_size);
-      free = &stObj->sampler_views[stObj->num_sampler_views++];
-      *free = NULL;
+/**
+ * Destroy all bound texture handles in the context.
+ */
+void
+st_destroy_bound_texture_handles(struct st_context *st)
+{
+   unsigned i;
+
+   for (i = 0; i < PIPE_SHADER_TYPES; i++) {
+      st_destroy_bound_texture_handles_per_stage(st, i);
    }
+}
 
-   /* Add just any sampler view to be used as a template */
-   if (used)
-      pipe_sampler_view_reference(free, used);
 
-   return free;
+/**
+ * Destroy bound image handles for the given stage.
+ */
+static void
+st_destroy_bound_image_handles_per_stage(struct st_context *st,
+                                         enum pipe_shader_type shader)
+{
+   struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
+   struct pipe_context *pipe = st->pipe;
+   unsigned i;
+
+   if (likely(!bound_handles->num_handles))
+      return;
+
+   for (i = 0; i < bound_handles->num_handles; i++) {
+      uint64_t handle = bound_handles->handles[i];
+
+      pipe->make_image_handle_resident(pipe, handle, GL_READ_WRITE, false);
+      pipe->delete_image_handle(pipe, handle);
+   }
+   free(bound_handles->handles);
+   bound_handles->handles = NULL;
+   bound_handles->num_handles = 0;
 }
 
+
+/**
+ * Destroy all bound image handles in the context.
+ */
 void
-st_texture_release_sampler_view(struct st_context *st,
-                                struct st_texture_object *stObj)
+st_destroy_bound_image_handles(struct st_context *st)
 {
-   GLuint i;
-
-   for (i = 0; i < stObj->num_sampler_views; ++i) {
-      struct pipe_sampler_view **sv = &stObj->sampler_views[i];
+   unsigned i;
 
-      if (*sv && (*sv)->context == st->pipe) {
-         pipe_sampler_view_reference(sv, NULL);
-         break;
-      }
+   for (i = 0; i < PIPE_SHADER_TYPES; i++) {
+      st_destroy_bound_image_handles_per_stage(st, i);
    }
 }
 
+
+/**
+ * Create a texture handle from a texture unit.
+ */
+static GLuint64
+st_create_texture_handle_from_unit(struct st_context *st,
+                                   struct gl_program *prog, GLuint texUnit)
+{
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_sampler_view *view;
+   struct pipe_sampler_state sampler = {0};
+
+   /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
+   st_update_single_texture(st, &view, texUnit, prog->sh.data->Version >= 130, true);
+   if (!view)
+      return 0;
+
+   if (view->target != PIPE_BUFFER)
+      st_convert_sampler_from_unit(st, &sampler, texUnit);
+
+   assert(st->ctx->Texture.Unit[texUnit]._Current);
+
+   return pipe->create_texture_handle(pipe, view, &sampler);
+}
+
+
+/**
+ * Create an image handle from an image unit.
+ */
+static GLuint64
+st_create_image_handle_from_unit(struct st_context *st,
+                                 struct gl_program *prog, GLuint imgUnit)
+{
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_image_view img;
+
+   st_convert_image_from_unit(st, &img, imgUnit, GL_READ_WRITE);
+
+   return pipe->create_image_handle(pipe, &img);
+}
+
+
+/**
+ * Make all bindless samplers bound to texture units resident in the context.
+ */
 void
-st_texture_release_all_sampler_views(struct st_texture_object *stObj)
+st_make_bound_samplers_resident(struct st_context *st,
+                                struct gl_program *prog)
 {
-   GLuint i;
+   enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
+   struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
+   struct pipe_context *pipe = st->pipe;
+   GLuint64 handle;
+   int i;
+
+   /* Remove previous bound texture handles for this stage. */
+   st_destroy_bound_texture_handles_per_stage(st, shader);
+
+   if (likely(!prog->sh.HasBoundBindlessSampler))
+      return;
+
+   for (i = 0; i < prog->sh.NumBindlessSamplers; i++) {
+      struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i];
+
+      if (!sampler->bound)
+         continue;
 
-   for (i = 0; i < stObj->num_sampler_views; ++i)
-      pipe_sampler_view_reference(&stObj->sampler_views[i], NULL);
+      /* Request a new texture handle from the driver and make it resident. */
+      handle = st_create_texture_handle_from_unit(st, prog, sampler->unit);
+      if (!handle)
+         continue;
+
+      pipe->make_texture_handle_resident(st->pipe, handle, true);
+
+      /* Overwrite the texture unit value by the resident handle before
+       * uploading the constant buffer.
+       */
+      *(uint64_t *)sampler->data = handle;
+
+      /* Store the handle in the context. */
+      bound_handles->handles = (uint64_t *)
+         realloc(bound_handles->handles,
+                 (bound_handles->num_handles + 1) * sizeof(uint64_t));
+      bound_handles->handles[bound_handles->num_handles] = handle;
+      bound_handles->num_handles++;
+   }
 }
 
 
+/**
+ * Make all bindless images bound to image units resident in the context.
+ */
 void
-st_texture_free_sampler_views(struct st_texture_object *stObj)
+st_make_bound_images_resident(struct st_context *st,
+                              struct gl_program *prog)
 {
-   /* NOTE:
-    * We use FREE() here to match REALLOC() above.  Both come from
-    * u_memory.h, not imports.h.  If we mis-match MALLOC/FREE from
-    * those two headers we can trash the heap.
-    */
-   FREE(stObj->sampler_views);
+   enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
+   struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
+   struct pipe_context *pipe = st->pipe;
+   GLuint64 handle;
+   int i;
+
+   /* Remove previous bound image handles for this stage. */
+   st_destroy_bound_image_handles_per_stage(st, shader);
+
+   if (likely(!prog->sh.HasBoundBindlessImage))
+      return;
+
+   for (i = 0; i < prog->sh.NumBindlessImages; i++) {
+      struct gl_bindless_image *image = &prog->sh.BindlessImages[i];
+
+      if (!image->bound)
+         continue;
+
+      /* Request a new image handle from the driver and make it resident. */
+      handle = st_create_image_handle_from_unit(st, prog, image->unit);
+      if (!handle)
+         continue;
+
+      pipe->make_image_handle_resident(st->pipe, handle, GL_READ_WRITE, true);
+
+      /* Overwrite the image unit value by the resident handle before uploading
+       * the constant buffer.
+       */
+      *(uint64_t *)image->data = handle;
+
+      /* Store the handle in the context. */
+      bound_handles->handles = (uint64_t *)
+         realloc(bound_handles->handles,
+                 (bound_handles->num_handles + 1) * sizeof(uint64_t));
+      bound_handles->handles[bound_handles->num_handles] = handle;
+      bound_handles->num_handles++;
+   }
 }