radeon: texture/renderbuffer overhaul.
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_tex_copy.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 "radeon_texture.h"
30
31 #include "main/enums.h"
32 #include "main/image.h"
33 #include "main/teximage.h"
34 #include "main/texstate.h"
35 #include "drivers/common/meta.h"
36
37 #include "radeon_mipmap_tree.h"
38
39 static GLboolean
40 do_copy_texsubimage(struct gl_context *ctx,
41 GLenum target, GLint level,
42 struct radeon_tex_obj *tobj,
43 radeon_texture_image *timg,
44 GLint dstx, GLint dsty,
45 GLint x, GLint y,
46 GLsizei width, GLsizei height)
47 {
48 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
49 struct radeon_renderbuffer *rrb;
50 unsigned src_bpp;
51 unsigned dst_bpp;
52 gl_format src_mesaformat;
53 gl_format dst_mesaformat;
54 unsigned flip_y;
55
56 if (!radeon->vtbl.blit) {
57 return GL_FALSE;
58 }
59
60 if (_mesa_get_format_bits(timg->base.Base.TexFormat, GL_DEPTH_BITS) > 0) {
61 if (ctx->ReadBuffer->_DepthBuffer && ctx->ReadBuffer->_DepthBuffer->Wrapped) {
62 rrb = radeon_renderbuffer(ctx->ReadBuffer->_DepthBuffer->Wrapped);
63 } else {
64 rrb = radeon_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
65 }
66 flip_y = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Type == GL_NONE;
67 } else {
68 rrb = radeon_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
69 flip_y = ctx->ReadBuffer->Attachment[BUFFER_COLOR0].Type == GL_NONE;
70 }
71
72 // This is software renderbuffer, fallback to swrast
73 if (!rrb) {
74 return GL_FALSE;
75 }
76
77 if (!timg->mt) {
78 radeon_validate_texture_miptree(ctx, &tobj->base);
79 }
80
81 assert(rrb->bo);
82 assert(timg->mt);
83 assert(timg->mt->bo);
84 assert(timg->base.Base.Width >= dstx + width);
85 assert(timg->base.Base.Height >= dsty + height);
86
87 intptr_t src_offset = rrb->draw_offset;
88 intptr_t dst_offset = radeon_miptree_image_offset(timg->mt, _mesa_tex_target_to_face(target), level);
89
90 if (0) {
91 fprintf(stderr, "%s: copying to face %d, level %d\n",
92 __FUNCTION__, _mesa_tex_target_to_face(target), level);
93 fprintf(stderr, "to: x %d, y %d, offset %d\n", dstx, dsty, (uint32_t) dst_offset);
94 fprintf(stderr, "from (%dx%d) width %d, height %d, offset %d, pitch %d\n",
95 x, y, rrb->base.Width, rrb->base.Height, (uint32_t) src_offset, rrb->pitch/rrb->cpp);
96 fprintf(stderr, "src size %d, dst size %d\n", rrb->bo->size, timg->mt->bo->size);
97
98 }
99
100 src_mesaformat = rrb->base.Format;
101 dst_mesaformat = timg->base.Base.TexFormat;
102 src_bpp = _mesa_get_format_bytes(src_mesaformat);
103 dst_bpp = _mesa_get_format_bytes(dst_mesaformat);
104 if (!radeon->vtbl.check_blit(dst_mesaformat, rrb->pitch / rrb->cpp)) {
105 /* depth formats tend to be special */
106 if (_mesa_get_format_bits(dst_mesaformat, GL_DEPTH_BITS) > 0)
107 return GL_FALSE;
108
109 if (src_bpp != dst_bpp)
110 return GL_FALSE;
111
112 switch (dst_bpp) {
113 case 2:
114 src_mesaformat = MESA_FORMAT_RGB565;
115 dst_mesaformat = MESA_FORMAT_RGB565;
116 break;
117 case 4:
118 src_mesaformat = MESA_FORMAT_ARGB8888;
119 dst_mesaformat = MESA_FORMAT_ARGB8888;
120 break;
121 case 1:
122 src_mesaformat = MESA_FORMAT_A8;
123 dst_mesaformat = MESA_FORMAT_A8;
124 break;
125 default:
126 return GL_FALSE;
127 }
128 }
129
130 /* blit from src buffer to texture */
131 return radeon->vtbl.blit(ctx, rrb->bo, src_offset, src_mesaformat, rrb->pitch/rrb->cpp,
132 rrb->base.Width, rrb->base.Height, x, y,
133 timg->mt->bo, dst_offset, dst_mesaformat,
134 timg->mt->levels[level].rowstride / dst_bpp,
135 timg->base.Base.Width, timg->base.Base.Height,
136 dstx, dsty, width, height, flip_y);
137 }
138
139 void
140 radeonCopyTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
141 GLint xoffset, GLint yoffset,
142 GLint x, GLint y,
143 GLsizei width, GLsizei height)
144 {
145 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
146 struct gl_texture_object *texObj = _mesa_select_tex_object(ctx, texUnit, target);
147 struct gl_texture_image *texImage = _mesa_select_tex_image(ctx, texObj, target, level);
148
149 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
150 radeon_prepare_render(radeon);
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 radeon_print(RADEON_FALLBACKS, RADEON_NORMAL,
157 "Falling back to sw for glCopyTexSubImage2D\n");
158
159 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
160 xoffset, yoffset, x, y, width, height);
161 }
162 }