r300: enable accelerated support for glCopyTexImage only under KMS
[mesa.git] / src / mesa / drivers / dri / r300 / r300_texcopy.c
1 /*
2 * Copyright (C) 2009 Maciej Cencora <m.cencora@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_common.h"
29 #include "r300_context.h"
30
31 #include "main/image.h"
32 #include "main/teximage.h"
33 #include "main/texstate.h"
34 #include "drivers/common/meta.h"
35
36 #include "radeon_mipmap_tree.h"
37 #include "r300_blit.h"
38 #include <main/debug.h>
39
40 static GLboolean
41 do_copy_texsubimage(GLcontext *ctx,
42 GLenum target, GLint level,
43 struct radeon_tex_obj *tobj,
44 radeon_texture_image *timg,
45 GLint dstx, GLint dsty,
46 GLint x, GLint y,
47 GLsizei width, GLsizei height)
48 {
49 struct r300_context *r300 = R300_CONTEXT(ctx);
50 struct radeon_renderbuffer *rrb;
51
52 if (_mesa_get_format_bits(timg->base.TexFormat, GL_DEPTH_BITS) > 0) {
53 rrb = radeon_get_depthbuffer(&r300->radeon);
54 } else {
55 rrb = radeon_get_colorbuffer(&r300->radeon);
56 }
57
58 if (!timg->mt) {
59 radeon_validate_texture_miptree(ctx, &tobj->base);
60 }
61
62 assert(rrb && rrb->bo);
63 assert(timg->mt->bo);
64 assert(timg->base.Width >= dstx + width);
65 assert(timg->base.Height >= dsty + height);
66
67 intptr_t src_offset = rrb->draw_offset + x * rrb->cpp + y * rrb->pitch;
68 intptr_t dst_offset = radeon_miptree_image_offset(timg->mt, _mesa_tex_target_to_face(target), level);
69 dst_offset += dstx * _mesa_get_format_bytes(timg->base.TexFormat) +
70 dsty * _mesa_format_row_stride(timg->base.TexFormat, timg->base.Width);
71
72 if (src_offset % 32 || dst_offset % 32) {
73 return GL_FALSE;
74 }
75
76 if (0) {
77 fprintf(stderr, "%s: copying to face %d, level %d\n",
78 __FUNCTION__, _mesa_tex_target_to_face(target), level);
79 fprintf(stderr, "to: x %d, y %d, offset %d\n", dstx, dsty, (uint32_t) dst_offset);
80 fprintf(stderr, "from (%dx%d) width %d, height %d, offset %d, pitch %d, width %d\n",
81 x, y, width, height, (uint32_t) src_offset, rrb->pitch, rrb->pitch/rrb->cpp);
82 fprintf(stderr, "src size %d, dst size %d\n", rrb->bo->size, timg->mt->bo->size);
83
84 }
85
86 /* blit from src buffer to texture */
87 return r300_blit(r300, rrb->bo, src_offset, rrb->base.Format, rrb->pitch,
88 rrb->base.Width, rrb->base.Height, timg->mt->bo ? timg->mt->bo : timg->bo, dst_offset,
89 timg->base.TexFormat, timg->base.Width, width, height);
90 }
91
92 static void
93 r300CopyTexImage2D(GLcontext *ctx, GLenum target, GLint level,
94 GLenum internalFormat,
95 GLint x, GLint y, GLsizei width, GLsizei height,
96 GLint border)
97 {
98 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
99 struct gl_texture_object *texObj =
100 _mesa_select_tex_object(ctx, texUnit, target);
101 struct gl_texture_image *texImage =
102 _mesa_select_tex_image(ctx, texObj, target, level);
103 int srcx, srcy, dstx, dsty;
104
105 if (border)
106 goto fail;
107
108 /* Setup or redefine the texture object, mipmap tree and texture
109 * image. Don't populate yet.
110 */
111 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
112 width, height, border,
113 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
114 &ctx->DefaultPacking, texObj, texImage);
115
116 srcx = x;
117 srcy = y;
118 dstx = 0;
119 dsty = 0;
120 if (!_mesa_clip_copytexsubimage(ctx,
121 &dstx, &dsty,
122 &srcx, &srcy,
123 &width, &height)) {
124 return;
125 }
126
127 if (!do_copy_texsubimage(ctx, target, level,
128 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
129 0, 0, x, y, width, height)) {
130 goto fail;
131 }
132
133 return;
134
135 fail:
136 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
137 width, height, border);
138 }
139
140 static void
141 r300CopyTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
142 GLint xoffset, GLint yoffset,
143 GLint x, GLint y,
144 GLsizei width, GLsizei height)
145 {
146 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
147 struct gl_texture_object *texObj = _mesa_select_tex_object(ctx, texUnit, target);
148 struct gl_texture_image *texImage = _mesa_select_tex_image(ctx, texObj, target, level);
149
150 if (!do_copy_texsubimage(ctx, target, level,
151 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
152 xoffset, yoffset, x, y, width, height)) {
153
154 //DEBUG_FALLBACKS
155
156 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
157 xoffset, yoffset, x, y, width, height);
158 }
159 }
160
161
162 void r300_init_texcopy_functions(struct dd_function_table *table)
163 {
164 table->CopyTexImage2D = r300CopyTexImage2D;
165 table->CopyTexSubImage2D = r300CopyTexSubImage2D;
166 }