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