1/*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include "main/mtypes.h"
27#include "main/enums.h"
28#include "main/image.h"
29#include "main/teximage.h"
30#include "main/texobj.h"
31#include "main/texstate.h"
32#include "main/fbobject.h"
33
34#include "drivers/common/meta.h"
35
36#include "intel_screen.h"
37#include "intel_mipmap_tree.h"
38#include "intel_fbo.h"
39#include "intel_tex.h"
40#include "intel_blit.h"
41#include "brw_context.h"
42
43#define FILE_DEBUG_FLAG DEBUG_TEXTURE
44
45
46static bool
47intel_copy_texsubimage(struct brw_context *brw,
48                       struct intel_texture_image *intelImage,
49                       GLint dstx, GLint dsty, GLint slice,
50                       struct intel_renderbuffer *irb,
51                       GLint x, GLint y, GLsizei width, GLsizei height)
52{
53   const GLenum internalFormat = intelImage->base.Base.InternalFormat;
54   bool ret;
55
56   /* No pixel transfer operations (zoom, bias, mapping), just a blit */
57   if (brw->ctx._ImageTransferState)
58      return false;
59
60   intel_prepare_render(brw);
61
62   /* glCopyTexSubImage() can be called on a multisampled renderbuffer (if
63    * that renderbuffer is associated with the window system framebuffer),
64    * however the hardware blitter can't handle this case, so fall back to
65    * meta (which can, since it uses ReadPixels).
66    */
67   if (irb->Base.Base.NumSamples != 0)
68      return false;
69
70   /* glCopyTexSubImage() can't be called on a multisampled texture. */
71   assert(intelImage->base.Base.NumSamples == 0);
72
73   if (!intelImage->mt || !irb || !irb->mt) {
74      if (unlikely(INTEL_DEBUG & DEBUG_PERF))
75	 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
76		 __func__, intelImage->mt, irb, internalFormat);
77      return false;
78   }
79
80   /* account for view parameters and face index */
81   int dst_level = intelImage->base.Base.Level +
82                   intelImage->base.Base.TexObject->MinLevel;
83   int dst_slice = slice + intelImage->base.Base.Face +
84                   intelImage->base.Base.TexObject->MinLayer;
85
86   _mesa_unlock_texture(&brw->ctx, intelImage->base.Base.TexObject);
87
88   /* blit from src buffer to texture */
89   ret = intel_miptree_blit(brw,
90                            irb->mt, irb->mt_level, irb->mt_layer,
91                            x, y, irb->Base.Base.Name == 0,
92                            intelImage->mt, dst_level, dst_slice,
93                            dstx, dsty, false,
94                            width, height, GL_COPY);
95
96   _mesa_lock_texture(&brw->ctx, intelImage->base.Base.TexObject);
97
98   return ret;
99}
100
101
102static void
103intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
104                     struct gl_texture_image *texImage,
105                     GLint xoffset, GLint yoffset, GLint slice,
106                     struct gl_renderbuffer *rb,
107                     GLint x, GLint y,
108                     GLsizei width, GLsizei height)
109{
110   struct brw_context *brw = brw_context(ctx);
111
112   /* Try BLORP first.  It can handle almost everything. */
113   if (brw_blorp_copytexsubimage(brw, rb, texImage, slice, x, y,
114                                 xoffset, yoffset, width, height))
115      return;
116
117   /* Next, try the BLT engine. */
118   if (intel_copy_texsubimage(brw,
119                              intel_texture_image(texImage),
120                              xoffset, yoffset, slice,
121                              intel_renderbuffer(rb), x, y, width, height)) {
122      return;
123   }
124
125   /* Finally, fall back to meta.  This will likely be slow. */
126   perf_debug("%s - fallback to swrast\n", __func__);
127   _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
128                              xoffset, yoffset, slice,
129                              rb, x, y, width, height);
130}
131
132
133void
134intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
135{
136   functions->CopyTexSubImage = intelCopyTexSubImage;
137}
138