glsl: add ir_emit_vertex and ir_end_primitive instruction types
[mesa.git] / src / mesa / state_tracker / st_cb_texture.c
index 606bae010d3cd0a9ab6e680a3ab968a7625ea5bc..faa9ee3f6c7686a48d9e31675226f2d52c03a219 100644 (file)
@@ -25,7 +25,6 @@
  * 
  **************************************************************************/
 
-#include "main/mfeatures.h"
 #include "main/bufferobj.h"
 #include "main/enums.h"
 #include "main/fbobject.h"
@@ -59,7 +58,6 @@
 #include "util/u_inlines.h"
 #include "pipe/p_shader_tokens.h"
 #include "util/u_tile.h"
-#include "util/u_blit.h"
 #include "util/u_format.h"
 #include "util/u_surface.h"
 #include "util/u_sampler.h"
@@ -69,7 +67,7 @@
 #define DBG if (0) printf
 
 
-static enum pipe_texture_target
+enum pipe_texture_target
 gl_target_to_pipe(GLenum target)
 {
    switch (target) {
@@ -79,6 +77,8 @@ gl_target_to_pipe(GLenum target)
    case GL_TEXTURE_2D:
    case GL_PROXY_TEXTURE_2D:
    case GL_TEXTURE_EXTERNAL_OES:
+   case GL_TEXTURE_2D_MULTISAMPLE:
+   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
       return PIPE_TEXTURE_2D;
    case GL_TEXTURE_RECTANGLE_NV:
    case GL_PROXY_TEXTURE_RECTANGLE_NV:
@@ -100,6 +100,8 @@ gl_target_to_pipe(GLenum target)
       return PIPE_TEXTURE_1D_ARRAY;
    case GL_TEXTURE_2D_ARRAY_EXT:
    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
+   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
+   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
       return PIPE_TEXTURE_2D_ARRAY;
    case GL_TEXTURE_BUFFER:
       return PIPE_BUFFER;
@@ -139,7 +141,7 @@ st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
    struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
 
    DBG("%s\n", __FUNCTION__);
-   _mesa_initialize_texture_object(&obj->base, name, target);
+   _mesa_initialize_texture_object(ctx, &obj->base, name, target);
 
    return &obj->base;
 }
@@ -409,7 +411,7 @@ guess_and_alloc_texture(struct st_context *st,
                                  ptWidth,
                                  ptHeight,
                                  ptDepth,
-                                 ptLayers,
+                                 ptLayers, 0,
                                  bindings);
 
    stObj->lastLevel = lastLevel;
@@ -497,7 +499,7 @@ st_AllocTextureImageBuffer(struct gl_context *ctx,
                                       ptWidth,
                                       ptHeight,
                                       ptDepth,
-                                      ptLayers,
+                                      ptLayers, 0,
                                       bindings);
       return stImage->pt != NULL;
    }
@@ -543,8 +545,8 @@ prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
  * Return a writemask for the gallium blit. The parameters can be base
  * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
  */
-static unsigned
-get_blit_mask(GLenum srcFormat, GLenum dstFormat)
+unsigned
+st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
 {
    switch (dstFormat) {
    case GL_DEPTH_STENCIL:
@@ -609,6 +611,10 @@ st_TexSubImage(struct gl_context *ctx, GLuint dims,
    unsigned bind;
    GLubyte *map;
 
+   if (!st->prefer_blit_based_texture_transfer) {
+      goto fallback;
+   }
+
    if (!dst) {
       goto fallback;
    }
@@ -728,7 +734,7 @@ st_TexSubImage(struct gl_context *ctx, GLuint dims,
       const uint bytesPerRow = width * util_format_get_blocksize(src_format);
       GLuint row, slice;
 
-      for (slice = 0; slice < depth; slice++) {
+      for (slice = 0; slice < (unsigned) depth; slice++) {
          if (gl_target == GL_TEXTURE_1D_ARRAY) {
             /* 1D array textures.
              * We need to convert gallium coords to GL coords.
@@ -741,7 +747,7 @@ st_TexSubImage(struct gl_context *ctx, GLuint dims,
          else {
             ubyte *slice_map = map;
 
-            for (row = 0; row < height; row++) {
+            for (row = 0; row < (unsigned) height; row++) {
                GLvoid *src = _mesa_image_address3d(unpack, pixels,
                                                    width, height, format,
                                                    type, slice, row, 0);
@@ -770,7 +776,7 @@ st_TexSubImage(struct gl_context *ctx, GLuint dims,
    blit.src.box.width = blit.dst.box.width = width;
    blit.src.box.height = blit.dst.box.height = height;
    blit.src.box.depth = blit.dst.box.depth = depth;
-   blit.mask = get_blit_mask(format, texImage->_BaseFormat);
+   blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
    blit.filter = PIPE_TEX_FILTER_NEAREST;
    blit.scissor_enable = FALSE;
 
@@ -861,7 +867,11 @@ st_GetTexImage(struct gl_context * ctx,
    ubyte *map = NULL;
    boolean done = FALSE;
 
-   if (!stImage->pt) {
+   if (!st->prefer_blit_based_texture_transfer) {
+      goto fallback;
+   }
+
+   if (!stImage->pt || !src) {
       goto fallback;
    }
 
@@ -997,7 +1007,7 @@ st_GetTexImage(struct gl_context * ctx,
    blit.src.box.width = blit.dst.box.width = width;
    blit.src.box.height = blit.dst.box.height = height;
    blit.src.box.depth = blit.dst.box.depth = depth;
-   blit.mask = get_blit_mask(texImage->_BaseFormat, format);
+   blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
    blit.filter = PIPE_TEX_FILTER_NEAREST;
    blit.scissor_enable = FALSE;
 
@@ -1123,7 +1133,7 @@ fallback_copy_texsubimage(struct gl_context *ctx,
                           struct st_renderbuffer *strb,
                           struct st_texture_image *stImage,
                           GLenum baseFormat,
-                          GLint destX, GLint destY, GLint destZ,
+                          GLint destX, GLint destY, GLint slice,
                           GLint srcX, GLint srcY,
                           GLsizei width, GLsizei height)
 {
@@ -1144,14 +1154,6 @@ fallback_copy_texsubimage(struct gl_context *ctx,
       srcY = strb->Base.Height - srcY - height;
    }
 
-   if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
-      /* Move y/height to z/depth for 1D array textures.  */
-      destZ = destY;
-      destY = 0;
-      dst_depth = dst_height;
-      dst_height = 1;
-   }
-
    map = pipe_transfer_map(pipe,
                            strb->texture,
                            strb->rtt_level,
@@ -1168,7 +1170,7 @@ fallback_copy_texsubimage(struct gl_context *ctx,
       transfer_usage = PIPE_TRANSFER_WRITE;
 
    texDest = st_texture_image_map(st, stImage, transfer_usage,
-                                  destX, destY, destZ,
+                                  destX, destY, slice,
                                   dst_width, dst_height, dst_depth);
 
    if (baseFormat == GL_DEPTH_COMPONENT ||
@@ -1282,7 +1284,7 @@ fallback_copy_texsubimage(struct gl_context *ctx,
 static void
 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
                    struct gl_texture_image *texImage,
-                   GLint destX, GLint destY, GLint destZ,
+                   GLint destX, GLint destY, GLint slice,
                    struct gl_renderbuffer *rb,
                    GLint srcX, GLint srcY, GLsizei width, GLsizei height)
 {
@@ -1296,7 +1298,7 @@ st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
    enum pipe_format dst_format;
    GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
    unsigned bind;
-   GLint srcY0, srcY1, yStep;
+   GLint srcY0, srcY1;
 
    if (!strb || !strb->surface || !stImage->pt) {
       debug_printf("%s: null strb or stImage\n", __FUNCTION__);
@@ -1341,12 +1343,10 @@ st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
    if (do_flip) {
       srcY1 = strb->Base.Height - srcY - height;
       srcY0 = srcY1 + height;
-      yStep = -1;
    }
    else {
       srcY0 = srcY;
       srcY1 = srcY0 + height;
-      yStep = 1;
    }
 
    /* Blit the texture.
@@ -1367,39 +1367,20 @@ st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
    blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level;
    blit.dst.box.x = destX;
    blit.dst.box.y = destY;
-   blit.dst.box.z = stImage->base.Face + destZ;
+   blit.dst.box.z = stImage->base.Face + slice;
    blit.dst.box.width = width;
    blit.dst.box.height = height;
    blit.dst.box.depth = 1;
-   blit.mask = get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
+   blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
    blit.filter = PIPE_TEX_FILTER_NEAREST;
-
-   /* 1D array textures need special treatment.
-    * Blit rows from the source to layers in the destination. */
-   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
-      int y, layer;
-
-      for (y = srcY0, layer = 0; layer < height; y += yStep, layer++) {
-         blit.src.box.y = y;
-         blit.src.box.height = 1;
-         blit.dst.box.y = 0;
-         blit.dst.box.height = 1;
-         blit.dst.box.z = destY + layer;
-
-         pipe->blit(pipe, &blit);
-      }
-   }
-   else {
-      /* All the other texture targets. */
-      pipe->blit(pipe, &blit);
-   }
+   pipe->blit(pipe, &blit);
    return;
 
 fallback:
    /* software fallback */
    fallback_copy_texsubimage(ctx,
                              strb, stImage, texImage->_BaseFormat,
-                             destX, destY, destZ,
+                             destX, destY, slice,
                              srcX, srcY, width, height);
 }
 
@@ -1485,7 +1466,7 @@ st_finalize_texture(struct gl_context *ctx,
    GLuint face;
    struct st_texture_image *firstImage;
    enum pipe_format firstImageFormat;
-   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
+   GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
 
    if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
       /* The texture is complete and we know exactly how many mipmap levels
@@ -1530,6 +1511,11 @@ st_finalize_texture(struct gl_context *ctx,
       pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
    }
 
+   /* If this texture comes from a window system, there is nothing else to do. */
+   if (stObj->surface_based) {
+      return GL_TRUE;
+   }
+
    /* Find gallium format for the Mesa texture */
    firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
 
@@ -1549,6 +1535,7 @@ st_finalize_texture(struct gl_context *ctx,
       /* convert GL dims to Gallium dims */
       st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
                                       &ptWidth, &ptHeight, &ptDepth, &ptLayers);
+      ptNumSamples = firstImage->base.NumSamples;
    }
 
    /* If we already have a gallium texture, check that it matches the texture
@@ -1556,11 +1543,12 @@ st_finalize_texture(struct gl_context *ctx,
     */
    if (stObj->pt) {
       if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
-          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
+          stObj->pt->format != firstImageFormat ||
           stObj->pt->last_level < stObj->lastLevel ||
           stObj->pt->width0 != ptWidth ||
           stObj->pt->height0 != ptHeight ||
           stObj->pt->depth0 != ptDepth ||
+          stObj->pt->nr_samples != ptNumSamples ||
           stObj->pt->array_size != ptLayers)
       {
          /* The gallium texture does not match the Mesa texture so delete the
@@ -1584,7 +1572,7 @@ st_finalize_texture(struct gl_context *ctx,
                                     ptWidth,
                                     ptHeight,
                                     ptDepth,
-                                    ptLayers,
+                                    ptLayers, ptNumSamples,
                                     bindings);
 
       if (!stObj->pt) {
@@ -1630,11 +1618,14 @@ st_AllocTextureStorage(struct gl_context *ctx,
                        GLsizei height, GLsizei depth)
 {
    const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
+   struct gl_texture_image *texImage = texObj->Image[0][0];
    struct st_context *st = st_context(ctx);
    struct st_texture_object *stObj = st_texture_object(texObj);
+   struct pipe_screen *screen = st->pipe->screen;
    GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
    enum pipe_format fmt;
    GLint level;
+   GLuint num_samples = texImage->NumSamples;
 
    assert(levels > 0);
 
@@ -1644,10 +1635,30 @@ st_AllocTextureStorage(struct gl_context *ctx,
    stObj->depth0 = depth;
    stObj->lastLevel = levels - 1;
 
-   fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
+   fmt = st_mesa_format_to_pipe_format(texImage->TexFormat);
 
    bindings = default_bindings(st, fmt);
 
+   /* Raise the sample count if the requested one is unsupported. */
+   if (num_samples > 1) {
+      boolean found = FALSE;
+
+      for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
+         if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
+                                         num_samples,
+                                         PIPE_BIND_SAMPLER_VIEW)) {
+            /* Update the sample count in gl_texture_image as well. */
+            texImage->NumSamples = num_samples;
+            found = TRUE;
+            break;
+         }
+      }
+
+      if (!found) {
+         return GL_FALSE;
+      }
+   }
+
    st_gl_texture_dims_to_pipe_dims(texObj->Target,
                                    width, height, depth,
                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
@@ -1655,11 +1666,11 @@ st_AllocTextureStorage(struct gl_context *ctx,
    stObj->pt = st_texture_create(st,
                                  gl_target_to_pipe(texObj->Target),
                                  fmt,
-                                 levels,
+                                 levels - 1,
                                  ptWidth,
                                  ptHeight,
                                  ptDepth,
-                                 ptLayers,
+                                 ptLayers, num_samples,
                                  bindings);
    if (!stObj->pt)
       return GL_FALSE;