intel: Clean up confusion between logical and physical surface dimensions.
[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 if (intelImage->base.Base.TexObject->Target == GL_TEXTURE_1D_ARRAY ||
74 intelImage->base.Base.TexObject->Target == GL_TEXTURE_2D_ARRAY) {
75 perf_debug("no support for array textures\n");
76 }
77
78 copy_supported = intelImage->base.Base.TexFormat == intel_rb_format(irb);
79
80 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
81 if (intel_rb_format(irb) == MESA_FORMAT_ARGB8888 &&
82 intelImage->base.Base.TexFormat == MESA_FORMAT_XRGB8888) {
83 copy_supported = true;
84 }
85
86 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
87 if (intel_rb_format(irb) == MESA_FORMAT_XRGB8888 &&
88 intelImage->base.Base.TexFormat == MESA_FORMAT_ARGB8888) {
89 copy_supported_with_alpha_override = true;
90 }
91
92 if (!copy_supported && !copy_supported_with_alpha_override) {
93 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
94 fprintf(stderr, "%s mismatched formats %s, %s\n",
95 __FUNCTION__,
96 _mesa_get_format_name(intelImage->base.Base.TexFormat),
97 _mesa_get_format_name(intel_rb_format(irb)));
98 return false;
99 }
100
101 {
102 GLuint image_x, image_y;
103 GLshort src_pitch;
104
105 /* get dest x/y in destination texture */
106 intel_miptree_get_image_offset(intelImage->mt,
107 intelImage->base.Base.Level,
108 intelImage->base.Base.Face,
109 &image_x, &image_y);
110
111 /* The blitter can't handle Y-tiled buffers. */
112 if (intelImage->mt->region->tiling == I915_TILING_Y) {
113 return false;
114 }
115
116 if (_mesa_is_winsys_fbo(ctx->ReadBuffer)) {
117 /* Flip vertical orientation for system framebuffers */
118 y = ctx->ReadBuffer->Height - (y + height);
119 src_pitch = -region->pitch;
120 } else {
121 /* reading from a FBO, y is already oriented the way we like */
122 src_pitch = region->pitch;
123 }
124
125 /* blit from src buffer to texture */
126 if (!intelEmitCopyBlit(intel,
127 intelImage->mt->cpp,
128 src_pitch,
129 region->bo,
130 0,
131 region->tiling,
132 intelImage->mt->region->pitch,
133 intelImage->mt->region->bo,
134 0,
135 intelImage->mt->region->tiling,
136 irb->draw_x + x, irb->draw_y + y,
137 image_x + dstx, image_y + dsty,
138 width, height,
139 GL_COPY)) {
140 return false;
141 }
142 }
143
144 if (copy_supported_with_alpha_override)
145 intel_set_teximage_alpha_to_one(ctx, intelImage);
146
147 return true;
148 }
149
150
151 static void
152 intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
153 struct gl_texture_image *texImage,
154 GLint xoffset, GLint yoffset, GLint zoffset,
155 struct gl_renderbuffer *rb,
156 GLint x, GLint y,
157 GLsizei width, GLsizei height)
158 {
159 if (dims == 3 || !intel_copy_texsubimage(intel_context(ctx),
160 intel_texture_image(texImage),
161 xoffset, yoffset,
162 intel_renderbuffer(rb), x, y, width, height)) {
163 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
164 _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
165 xoffset, yoffset, zoffset,
166 rb, x, y, width, height);
167 }
168 }
169
170
171 void
172 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
173 {
174 functions->CopyTexSubImage = intelCopyTexSubImage;
175 }