i965: Add hardware context support.
[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 bool
48 intel_copy_texsubimage(struct intel_context *intel,
49 struct intel_texture_image *intelImage,
50 GLint dstx, GLint dsty,
51 struct intel_renderbuffer *irb,
52 GLint x, GLint y, GLsizei width, GLsizei height)
53 {
54 struct gl_context *ctx = &intel->ctx;
55 struct intel_region *region;
56 const GLenum internalFormat = intelImage->base.Base.InternalFormat;
57 bool copy_supported = false;
58 bool copy_supported_with_alpha_override = false;
59
60 intel_prepare_render(intel);
61
62 if (!intelImage->mt || !irb || !irb->mt) {
63 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
64 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
65 __FUNCTION__, intelImage->mt, irb, internalFormat);
66 return false;
67 } else {
68 region = irb->mt->region;
69 assert(region);
70 }
71
72 copy_supported = intelImage->base.Base.TexFormat == intel_rb_format(irb);
73
74 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
75 if (intel_rb_format(irb) == MESA_FORMAT_ARGB8888 &&
76 intelImage->base.Base.TexFormat == MESA_FORMAT_XRGB8888) {
77 copy_supported = true;
78 }
79
80 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
81 if (intel_rb_format(irb) == MESA_FORMAT_XRGB8888 &&
82 intelImage->base.Base.TexFormat == MESA_FORMAT_ARGB8888) {
83 copy_supported_with_alpha_override = true;
84 }
85
86 if (!copy_supported && !copy_supported_with_alpha_override) {
87 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
88 fprintf(stderr, "%s mismatched formats %s, %s\n",
89 __FUNCTION__,
90 _mesa_get_format_name(intelImage->base.Base.TexFormat),
91 _mesa_get_format_name(intel_rb_format(irb)));
92 return false;
93 }
94
95 {
96 GLuint image_x, image_y;
97 GLshort src_pitch;
98
99 /* get dest x/y in destination texture */
100 intel_miptree_get_image_offset(intelImage->mt,
101 intelImage->base.Base.Level,
102 intelImage->base.Base.Face,
103 0,
104 &image_x, &image_y);
105
106 /* The blitter can't handle Y-tiled buffers. */
107 if (intelImage->mt->region->tiling == I915_TILING_Y) {
108 return false;
109 }
110
111 if (ctx->ReadBuffer->Name == 0) {
112 /* Flip vertical orientation for system framebuffers */
113 y = ctx->ReadBuffer->Height - (y + height);
114 src_pitch = -region->pitch;
115 } else {
116 /* reading from a FBO, y is already oriented the way we like */
117 src_pitch = region->pitch;
118 }
119
120 /* blit from src buffer to texture */
121 if (!intelEmitCopyBlit(intel,
122 intelImage->mt->cpp,
123 src_pitch,
124 region->bo,
125 0,
126 region->tiling,
127 intelImage->mt->region->pitch,
128 intelImage->mt->region->bo,
129 0,
130 intelImage->mt->region->tiling,
131 irb->draw_x + x, irb->draw_y + y,
132 image_x + dstx, image_y + dsty,
133 width, height,
134 GL_COPY)) {
135 return false;
136 }
137 }
138
139 if (copy_supported_with_alpha_override)
140 intel_set_teximage_alpha_to_one(ctx, intelImage);
141
142 return true;
143 }
144
145
146 static void
147 intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
148 struct gl_texture_image *texImage,
149 GLint xoffset, GLint yoffset, GLint zoffset,
150 struct gl_renderbuffer *rb,
151 GLint x, GLint y,
152 GLsizei width, GLsizei height)
153 {
154 if (dims == 3 || !intel_copy_texsubimage(intel_context(ctx),
155 intel_texture_image(texImage),
156 xoffset, yoffset,
157 intel_renderbuffer(rb), x, y, width, height)) {
158 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
159 _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
160 xoffset, yoffset, zoffset,
161 rb, x, y, width, height);
162 }
163 }
164
165
166 void
167 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
168 {
169 functions->CopyTexSubImage = intelCopyTexSubImage;
170 }