6d90db318d2224381f5de1ac2a185bac279557a3
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_copy.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/mtypes.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/teximage.h"
32 #include "main/texstate.h"
33 #include "main/fbobject.h"
34
35 #include "drivers/common/meta.h"
36
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_mipmap_tree.h"
40 #include "intel_regions.h"
41 #include "intel_fbo.h"
42 #include "intel_tex.h"
43 #include "intel_blit.h"
44 #ifndef I915
45 #include "brw_context.h"
46 #endif
47
48 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
49
50
51 bool
52 intel_copy_texsubimage(struct intel_context *intel,
53 struct intel_texture_image *intelImage,
54 GLint dstx, GLint dsty,
55 struct intel_renderbuffer *irb,
56 GLint x, GLint y, GLsizei width, GLsizei height)
57 {
58 struct gl_context *ctx = &intel->ctx;
59 struct intel_region *region;
60 const GLenum internalFormat = intelImage->base.Base.InternalFormat;
61 bool copy_supported = false;
62 bool copy_supported_with_alpha_override = false;
63
64 intel_prepare_render(intel);
65
66 if (!intelImage->mt || !irb || !irb->mt) {
67 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
68 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
69 __FUNCTION__, intelImage->mt, irb, internalFormat);
70 return false;
71 } else {
72 region = irb->mt->region;
73 assert(region);
74 }
75
76 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
77 * Data Size Limitations):
78 *
79 * The BLT engine is capable of transferring very large quantities of
80 * graphics data. Any graphics data read from and written to the
81 * destination is permitted to represent a number of pixels that
82 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
83 * at the destination. The maximum number of pixels that may be
84 * represented per scan line’s worth of graphics data depends on the
85 * color depth.
86 *
87 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
88 * 16-bit integer to represent buffer pitch, so it can only handle buffer
89 * pitches < 32k.
90 *
91 * As a result of these two limitations, we can only use the blitter to do
92 * this copy when the region's pitch is less than 32k.
93 */
94 if (region->pitch >= 32768)
95 return false;
96
97 if (intelImage->base.Base.TexObject->Target == GL_TEXTURE_1D_ARRAY ||
98 intelImage->base.Base.TexObject->Target == GL_TEXTURE_2D_ARRAY) {
99 perf_debug("no support for array textures\n");
100 }
101
102 copy_supported = intelImage->base.Base.TexFormat == intel_rb_format(irb);
103
104 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
105 if (intel_rb_format(irb) == MESA_FORMAT_ARGB8888 &&
106 intelImage->base.Base.TexFormat == MESA_FORMAT_XRGB8888) {
107 copy_supported = true;
108 }
109
110 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
111 if (intel_rb_format(irb) == MESA_FORMAT_XRGB8888 &&
112 intelImage->base.Base.TexFormat == MESA_FORMAT_ARGB8888) {
113 copy_supported_with_alpha_override = true;
114 }
115
116 if (!copy_supported && !copy_supported_with_alpha_override) {
117 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
118 fprintf(stderr, "%s mismatched formats %s, %s\n",
119 __FUNCTION__,
120 _mesa_get_format_name(intelImage->base.Base.TexFormat),
121 _mesa_get_format_name(intel_rb_format(irb)));
122 return false;
123 }
124
125 {
126 GLuint image_x, image_y;
127 GLshort src_pitch;
128
129 /* get dest x/y in destination texture */
130 intel_miptree_get_image_offset(intelImage->mt,
131 intelImage->base.Base.Level,
132 intelImage->base.Base.Face,
133 &image_x, &image_y);
134
135 /* The blitter can't handle Y-tiled buffers. */
136 if (intelImage->mt->region->tiling == I915_TILING_Y) {
137 return false;
138 }
139
140 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
141 /* Flip vertical orientation for system framebuffers */
142 y = ctx->ReadBuffer->Height - (y + height);
143 src_pitch = -region->pitch;
144 } else {
145 /* reading from a FBO, y is already oriented the way we like */
146 src_pitch = region->pitch;
147 }
148
149 /* blit from src buffer to texture */
150 if (!intelEmitCopyBlit(intel,
151 intelImage->mt->cpp,
152 src_pitch,
153 region->bo,
154 0,
155 region->tiling,
156 intelImage->mt->region->pitch,
157 intelImage->mt->region->bo,
158 0,
159 intelImage->mt->region->tiling,
160 irb->draw_x + x, irb->draw_y + y,
161 image_x + dstx, image_y + dsty,
162 width, height,
163 GL_COPY)) {
164 return false;
165 }
166 }
167
168 if (copy_supported_with_alpha_override)
169 intel_set_teximage_alpha_to_one(ctx, intelImage);
170
171 return true;
172 }
173
174
175 static void
176 intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
177 struct gl_texture_image *texImage,
178 GLint xoffset, GLint yoffset, GLint zoffset,
179 struct gl_renderbuffer *rb,
180 GLint x, GLint y,
181 GLsizei width, GLsizei height)
182 {
183 struct intel_context *intel = intel_context(ctx);
184 if (dims != 3) {
185 #ifndef I915
186 /* Try BLORP first. It can handle almost everything. */
187 if (brw_blorp_copytexsubimage(intel, rb, texImage, x, y,
188 xoffset, yoffset, width, height))
189 return;
190 #endif
191
192 /* Next, try the BLT engine. */
193 if (intel_copy_texsubimage(intel,
194 intel_texture_image(texImage),
195 xoffset, yoffset,
196 intel_renderbuffer(rb), x, y, width, height))
197 return;
198 }
199
200 /* Finally, fall back to meta. This will likely be slow. */
201 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
202 _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
203 xoffset, yoffset, zoffset,
204 rb, x, y, width, height);
205 }
206
207
208 void
209 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
210 {
211 functions->CopyTexSubImage = intelCopyTexSubImage;
212 }