c9cbcf480d52ff2fb64756d3e1eb51b26cbddd70
[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
45 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
46
47
48 bool
49 intel_copy_texsubimage(struct intel_context *intel,
50 struct intel_texture_image *intelImage,
51 GLint dstx, GLint dsty,
52 struct intel_renderbuffer *irb,
53 GLint x, GLint y, GLsizei width, GLsizei height)
54 {
55 struct gl_context *ctx = &intel->ctx;
56 struct intel_region *region;
57 const GLenum internalFormat = intelImage->base.Base.InternalFormat;
58 bool copy_supported = false;
59 bool copy_supported_with_alpha_override = false;
60
61 intel_prepare_render(intel);
62
63 if (!intelImage->mt || !irb || !irb->mt) {
64 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
65 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
66 __FUNCTION__, intelImage->mt, irb, internalFormat);
67 return false;
68 } else {
69 region = irb->mt->region;
70 assert(region);
71 }
72
73 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
74 * Data Size Limitations):
75 *
76 * The BLT engine is capable of transferring very large quantities of
77 * graphics data. Any graphics data read from and written to the
78 * destination is permitted to represent a number of pixels that
79 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
80 * at the destination. The maximum number of pixels that may be
81 * represented per scan line’s worth of graphics data depends on the
82 * color depth.
83 *
84 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
85 * 16-bit integer to represent buffer pitch, so it can only handle buffer
86 * pitches < 32k.
87 *
88 * As a result of these two limitations, we can only use the blitter to do
89 * this copy when the region's pitch is less than 32k.
90 */
91 if (region->pitch >= 32768)
92 return false;
93
94 if (intelImage->base.Base.TexObject->Target == GL_TEXTURE_1D_ARRAY ||
95 intelImage->base.Base.TexObject->Target == GL_TEXTURE_2D_ARRAY) {
96 perf_debug("no support for array textures\n");
97 }
98
99 copy_supported = intelImage->base.Base.TexFormat == intel_rb_format(irb);
100
101 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
102 if (intel_rb_format(irb) == MESA_FORMAT_ARGB8888 &&
103 intelImage->base.Base.TexFormat == MESA_FORMAT_XRGB8888) {
104 copy_supported = true;
105 }
106
107 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
108 if (intel_rb_format(irb) == MESA_FORMAT_XRGB8888 &&
109 intelImage->base.Base.TexFormat == MESA_FORMAT_ARGB8888) {
110 copy_supported_with_alpha_override = true;
111 }
112
113 if (!copy_supported && !copy_supported_with_alpha_override) {
114 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
115 fprintf(stderr, "%s mismatched formats %s, %s\n",
116 __FUNCTION__,
117 _mesa_get_format_name(intelImage->base.Base.TexFormat),
118 _mesa_get_format_name(intel_rb_format(irb)));
119 return false;
120 }
121
122 {
123 GLuint image_x, image_y;
124 GLshort src_pitch;
125
126 /* get dest x/y in destination texture */
127 intel_miptree_get_image_offset(intelImage->mt,
128 intelImage->base.Base.Level,
129 intelImage->base.Base.Face,
130 &image_x, &image_y);
131
132 /* The blitter can't handle Y-tiled buffers. */
133 if (intelImage->mt->region->tiling == I915_TILING_Y) {
134 return false;
135 }
136
137 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
138 /* Flip vertical orientation for system framebuffers */
139 y = ctx->ReadBuffer->Height - (y + height);
140 src_pitch = -region->pitch;
141 } else {
142 /* reading from a FBO, y is already oriented the way we like */
143 src_pitch = region->pitch;
144 }
145
146 /* blit from src buffer to texture */
147 if (!intelEmitCopyBlit(intel,
148 intelImage->mt->cpp,
149 src_pitch,
150 region->bo,
151 0,
152 region->tiling,
153 intelImage->mt->region->pitch,
154 intelImage->mt->region->bo,
155 0,
156 intelImage->mt->region->tiling,
157 irb->draw_x + x, irb->draw_y + y,
158 image_x + dstx, image_y + dsty,
159 width, height,
160 GL_COPY)) {
161 return false;
162 }
163 }
164
165 if (copy_supported_with_alpha_override)
166 intel_set_teximage_alpha_to_one(ctx, intelImage);
167
168 return true;
169 }
170
171
172 static void
173 intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
174 struct gl_texture_image *texImage,
175 GLint xoffset, GLint yoffset, GLint zoffset,
176 struct gl_renderbuffer *rb,
177 GLint x, GLint y,
178 GLsizei width, GLsizei height)
179 {
180 if (dims == 3 || !intel_copy_texsubimage(intel_context(ctx),
181 intel_texture_image(texImage),
182 xoffset, yoffset,
183 intel_renderbuffer(rb), x, y, width, height)) {
184 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
185 _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
186 xoffset, yoffset, zoffset,
187 rb, x, y, width, height);
188 }
189 }
190
191
192 void
193 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
194 {
195 functions->CopyTexSubImage = intelCopyTexSubImage;
196 }