i965: Add support for 16-bit unorm L, A, and I textures.
[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
34 #include "drivers/common/meta.h"
35
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_mipmap_tree.h"
39 #include "intel_regions.h"
40 #include "intel_fbo.h"
41 #include "intel_tex.h"
42 #include "intel_blit.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
45
46 /**
47 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
48 *
49 * Do the best we can using the blitter. A future project is to use
50 * the texture engine and fragment programs for these copies.
51 */
52 static struct intel_renderbuffer *
53 get_teximage_readbuffer(struct intel_context *intel, GLenum internalFormat)
54 {
55 DBG("%s %s\n", __FUNCTION__,
56 _mesa_lookup_enum_by_nr(internalFormat));
57
58 if (_mesa_is_depth_format(internalFormat) ||
59 _mesa_is_depthstencil_format(internalFormat))
60 return intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
61
62 return intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
63 }
64
65
66 bool
67 intel_copy_texsubimage(struct intel_context *intel,
68 struct intel_texture_image *intelImage,
69 GLint dstx, GLint dsty,
70 GLint x, GLint y, GLsizei width, GLsizei height)
71 {
72 struct gl_context *ctx = &intel->ctx;
73 struct intel_renderbuffer *irb;
74 const GLenum internalFormat = intelImage->base.Base.InternalFormat;
75 bool copy_supported = false;
76 bool copy_supported_with_alpha_override = false;
77
78 intel_prepare_render(intel);
79
80 irb = get_teximage_readbuffer(intel, internalFormat);
81 if (!intelImage->mt || !irb || !irb->region) {
82 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
83 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
84 __FUNCTION__, intelImage->mt, irb, internalFormat);
85 return false;
86 }
87
88 copy_supported = intelImage->base.Base.TexFormat == irb->Base.Format;
89
90 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
91 if (irb->Base.Format == MESA_FORMAT_ARGB8888 &&
92 intelImage->base.Base.TexFormat == MESA_FORMAT_XRGB8888) {
93 copy_supported = true;
94 }
95
96 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
97 if (irb->Base.Format == MESA_FORMAT_XRGB8888 &&
98 intelImage->base.Base.TexFormat == MESA_FORMAT_ARGB8888) {
99 copy_supported_with_alpha_override = true;
100 }
101
102 if (!copy_supported && !copy_supported_with_alpha_override) {
103 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
104 fprintf(stderr, "%s mismatched formats %s, %s\n",
105 __FUNCTION__,
106 _mesa_get_format_name(intelImage->base.Base.TexFormat),
107 _mesa_get_format_name(irb->Base.Format));
108 return false;
109 }
110
111 {
112 GLuint image_x, image_y;
113 GLshort src_pitch;
114
115 /* get dest x/y in destination texture */
116 intel_miptree_get_image_offset(intelImage->mt,
117 intelImage->base.Base.Level,
118 intelImage->base.Base.Face,
119 0,
120 &image_x, &image_y);
121
122 /* The blitter can't handle Y-tiled buffers. */
123 if (intelImage->mt->region->tiling == I915_TILING_Y) {
124 return false;
125 }
126
127 if (ctx->ReadBuffer->Name == 0) {
128 /* Flip vertical orientation for system framebuffers */
129 y = ctx->ReadBuffer->Height - (y + height);
130 src_pitch = -irb->region->pitch;
131 } else {
132 /* reading from a FBO, y is already oriented the way we like */
133 src_pitch = irb->region->pitch;
134 }
135
136 /* blit from src buffer to texture */
137 if (!intelEmitCopyBlit(intel,
138 intelImage->mt->cpp,
139 src_pitch,
140 irb->region->bo,
141 0,
142 irb->region->tiling,
143 intelImage->mt->region->pitch,
144 intelImage->mt->region->bo,
145 0,
146 intelImage->mt->region->tiling,
147 irb->draw_x + x, irb->draw_y + y,
148 image_x + dstx, image_y + dsty,
149 width, height,
150 GL_COPY)) {
151 return false;
152 }
153 }
154
155 if (copy_supported_with_alpha_override)
156 intel_set_teximage_alpha_to_one(ctx, intelImage);
157
158 return true;
159 }
160
161
162 static void
163 intelCopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
164 GLint xoffset, GLint x, GLint y, GLsizei width)
165 {
166 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
167 struct gl_texture_object *texObj =
168 _mesa_select_tex_object(ctx, texUnit, target);
169 struct gl_texture_image *texImage =
170 _mesa_select_tex_image(ctx, texObj, target, level);
171
172 /* XXX need to check <border> as in above function? */
173
174 /* Need to check texture is compatible with source format.
175 */
176
177 if (!intel_copy_texsubimage(intel_context(ctx),
178 intel_texture_image(texImage),
179 xoffset, 0, x, y, width, 1)) {
180 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
181 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
182 }
183 }
184
185
186 static void
187 intelCopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
188 GLint xoffset, GLint yoffset,
189 GLint x, GLint y, GLsizei width, GLsizei height)
190 {
191 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
192 struct gl_texture_object *texObj =
193 _mesa_select_tex_object(ctx, texUnit, target);
194 struct gl_texture_image *texImage =
195 _mesa_select_tex_image(ctx, texObj, target, level);
196
197 /* Need to check texture is compatible with source format.
198 */
199
200 if (!intel_copy_texsubimage(intel_context(ctx),
201 intel_texture_image(texImage),
202 xoffset, yoffset, x, y, width, height)) {
203 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
204 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
205 xoffset, yoffset, x, y, width, height);
206 }
207 }
208
209
210 void
211 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
212 {
213 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
214 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
215 }