r200: add blit support
[mesa.git] / src / mesa / drivers / dri / r200 / r200_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 "r200_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 "r200_blit.h"
38 #include <main/debug.h>
39
40 // TODO:
41 // need to pass correct pitch for small dst textures!
42 static GLboolean
43 do_copy_texsubimage(GLcontext *ctx,
44 GLenum target, GLint level,
45 struct radeon_tex_obj *tobj,
46 radeon_texture_image *timg,
47 GLint dstx, GLint dsty,
48 GLint x, GLint y,
49 GLsizei width, GLsizei height)
50 {
51 struct r200_context *r200 = R200_CONTEXT(ctx);
52 struct radeon_renderbuffer *rrb;
53
54 if (_mesa_get_format_bits(timg->base.TexFormat, GL_DEPTH_BITS) > 0) {
55 rrb = radeon_get_depthbuffer(&r200->radeon);
56 } else {
57 rrb = radeon_get_colorbuffer(&r200->radeon);
58 }
59
60 if (!timg->mt) {
61 radeon_validate_texture_miptree(ctx, &tobj->base);
62 }
63
64 assert(rrb && rrb->bo);
65 assert(timg->mt->bo);
66 assert(timg->base.Width >= dstx + width);
67 assert(timg->base.Height >= dsty + height);
68
69 intptr_t src_offset = rrb->draw_offset;
70 intptr_t dst_offset = radeon_miptree_image_offset(timg->mt, _mesa_tex_target_to_face(target), level);
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\n",
81 x, y, rrb->base.Width, rrb->base.Height, (uint32_t) src_offset, 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 r200_blit(r200, rrb->bo, src_offset, rrb->base.Format, rrb->pitch,
88 rrb->base.Width, rrb->base.Height, x, y,
89 timg->mt->bo, dst_offset, timg->base.TexFormat,
90 timg->base.Width, timg->base.Width, timg->base.Height,
91 dstx, dsty, width, height, 1);
92 }
93
94 static void
95 r200CopyTexImage2D(GLcontext *ctx, GLenum target, GLint level,
96 GLenum internalFormat,
97 GLint x, GLint y, GLsizei width, GLsizei height,
98 GLint border)
99 {
100 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
101 struct gl_texture_object *texObj =
102 _mesa_select_tex_object(ctx, texUnit, target);
103 struct gl_texture_image *texImage =
104 _mesa_select_tex_image(ctx, texObj, target, level);
105 int srcx, srcy, dstx, dsty;
106
107 if (border)
108 goto fail;
109
110 /* Setup or redefine the texture object, mipmap tree and texture
111 * image. Don't populate yet.
112 */
113 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
114 width, height, border,
115 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
116 &ctx->DefaultPacking, texObj, texImage);
117
118 srcx = x;
119 srcy = y;
120 dstx = 0;
121 dsty = 0;
122 if (!_mesa_clip_copytexsubimage(ctx,
123 &dstx, &dsty,
124 &srcx, &srcy,
125 &width, &height)) {
126 return;
127 }
128
129 if (!do_copy_texsubimage(ctx, target, level,
130 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
131 0, 0, x, y, width, height)) {
132 goto fail;
133 }
134
135 return;
136
137 fail:
138 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
139 width, height, border);
140 }
141
142 static void
143 r200CopyTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
144 GLint xoffset, GLint yoffset,
145 GLint x, GLint y,
146 GLsizei width, GLsizei height)
147 {
148 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
149 struct gl_texture_object *texObj = _mesa_select_tex_object(ctx, texUnit, target);
150 struct gl_texture_image *texImage = _mesa_select_tex_image(ctx, texObj, target, level);
151
152 if (!do_copy_texsubimage(ctx, target, level,
153 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
154 xoffset, yoffset, x, y, width, height)) {
155
156 //DEBUG_FALLBACKS
157
158 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
159 xoffset, yoffset, x, y, width, height);
160 }
161 }
162
163
164 void r200_init_texcopy_functions(struct dd_function_table *table)
165 {
166 table->CopyTexImage2D = r200CopyTexImage2D;
167 table->CopyTexSubImage2D = r200CopyTexSubImage2D;
168 }