2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "r300_transfer.h"
25 #include "r300_texture_desc.h"
26 #include "r300_screen_buffer.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30 #include "util/u_box.h"
32 struct r300_transfer
{
34 struct pipe_transfer transfer
;
36 /* Offset from start of buffer. */
40 struct r300_resource
*linear_texture
;
43 /* Convenience cast wrapper. */
44 static INLINE
struct r300_transfer
*
45 r300_transfer(struct pipe_transfer
* transfer
)
47 return (struct r300_transfer
*)transfer
;
50 /* Copy from a tiled texture to a detiled one. */
51 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
52 struct r300_transfer
*r300transfer
)
54 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
55 struct pipe_resource
*src
= transfer
->resource
;
56 struct pipe_resource
*dst
= &r300transfer
->linear_texture
->b
.b
;
58 if (src
->nr_samples
<= 1) {
59 ctx
->resource_copy_region(ctx
, dst
, 0, 0, 0, 0,
60 src
, transfer
->level
, &transfer
->box
);
62 /* Resolve the resource. */
63 struct pipe_blit_info blit
;
65 memset(&blit
, 0, sizeof(blit
));
66 blit
.src
.resource
= src
;
67 blit
.src
.format
= src
->format
;
68 blit
.src
.level
= transfer
->level
;
69 blit
.src
.box
= transfer
->box
;
70 blit
.dst
.resource
= dst
;
71 blit
.dst
.format
= dst
->format
;
72 blit
.dst
.box
.width
= transfer
->box
.width
;
73 blit
.dst
.box
.height
= transfer
->box
.height
;
74 blit
.dst
.box
.depth
= transfer
->box
.depth
;
75 blit
.mask
= PIPE_MASK_RGBA
;
76 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
78 ctx
->blit(ctx
, &blit
);
82 /* Copy a detiled texture to a tiled one. */
83 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
84 struct r300_transfer
*r300transfer
)
86 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
87 struct pipe_resource
*tex
= transfer
->resource
;
88 struct pipe_box src_box
;
89 u_box_origin_2d(transfer
->box
.width
, transfer
->box
.height
, &src_box
);
91 ctx
->resource_copy_region(ctx
, tex
, transfer
->level
,
92 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
93 &r300transfer
->linear_texture
->b
.b
, 0, &src_box
);
95 /* XXX remove this. */
96 r300_flush(ctx
, 0, NULL
);
100 r300_texture_transfer_map(struct pipe_context
*ctx
,
101 struct pipe_resource
*texture
,
104 const struct pipe_box
*box
,
105 struct pipe_transfer
**transfer
)
107 struct r300_context
*r300
= r300_context(ctx
);
108 struct r300_resource
*tex
= r300_resource(texture
);
109 struct r300_transfer
*trans
;
110 struct pipe_resource base
;
111 boolean referenced_cs
, referenced_hw
;
112 enum pipe_format format
= tex
->b
.b
.format
;
116 r300
->rws
->cs_is_buffer_referenced(r300
->cs
, tex
->cs_buf
, RADEON_USAGE_READWRITE
);
118 referenced_hw
= TRUE
;
121 r300
->rws
->buffer_is_busy(tex
->buf
, RADEON_USAGE_READWRITE
);
124 trans
= CALLOC_STRUCT(r300_transfer
);
126 /* Initialize the transfer object. */
127 trans
->transfer
.resource
= texture
;
128 trans
->transfer
.level
= level
;
129 trans
->transfer
.usage
= usage
;
130 trans
->transfer
.box
= *box
;
132 /* If the texture is tiled, we must create a temporary detiled texture
134 * Also make write transfers pipelined. */
135 if (tex
->tex
.microtile
|| tex
->tex
.macrotile
[level
] ||
136 (referenced_hw
&& !(usage
& PIPE_TRANSFER_READ
) &&
137 r300_is_blit_supported(texture
->format
))) {
138 if (r300
->blitter
->running
) {
139 fprintf(stderr
, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
143 memset(&base
, 0, sizeof(base
));
144 base
.target
= PIPE_TEXTURE_2D
;
145 base
.format
= texture
->format
;
146 base
.width0
= box
->width
;
147 base
.height0
= box
->height
;
150 base
.usage
= PIPE_USAGE_STAGING
;
151 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
153 /* Create the temporary texture. */
154 trans
->linear_texture
= r300_resource(
155 ctx
->screen
->resource_create(ctx
->screen
,
158 if (!trans
->linear_texture
) {
159 /* Oh crap, the thing can't create the texture.
160 * Let's flush and try again. */
161 r300_flush(ctx
, 0, NULL
);
163 trans
->linear_texture
= r300_resource(
164 ctx
->screen
->resource_create(ctx
->screen
,
167 if (!trans
->linear_texture
) {
169 "r300: Failed to create a transfer object.\n");
175 assert(!trans
->linear_texture
->tex
.microtile
&&
176 !trans
->linear_texture
->tex
.macrotile
[0]);
178 /* Set the stride. */
179 trans
->transfer
.stride
=
180 trans
->linear_texture
->tex
.stride_in_bytes
[0];
182 if (usage
& PIPE_TRANSFER_READ
) {
183 /* We cannot map a tiled texture directly because the data is
184 * in a different order, therefore we do detiling using a blit. */
185 r300_copy_from_tiled_texture(ctx
, trans
);
187 /* Always referenced in the blit. */
188 r300_flush(ctx
, 0, NULL
);
191 /* Unpipelined transfer. */
192 trans
->transfer
.stride
= tex
->tex
.stride_in_bytes
[level
];
193 trans
->offset
= r300_texture_get_offset(tex
, level
, box
->z
);
196 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
197 r300_flush(ctx
, 0, NULL
);
202 if (trans
->linear_texture
) {
203 /* The detiled texture is of the same size as the region being mapped
204 * (no offset needed). */
205 map
= r300
->rws
->buffer_map(trans
->linear_texture
->cs_buf
,
208 pipe_resource_reference(
209 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);
213 *transfer
= &trans
->transfer
;
216 /* Tiling is disabled. */
217 map
= r300
->rws
->buffer_map(tex
->cs_buf
, r300
->cs
, usage
);
223 *transfer
= &trans
->transfer
;
224 return map
+ trans
->offset
+
225 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
226 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
230 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
231 struct pipe_transfer
*transfer
)
233 struct radeon_winsys
*rws
= r300_context(ctx
)->rws
;
234 struct r300_transfer
*trans
= r300_transfer(transfer
);
235 struct r300_resource
*tex
= r300_resource(transfer
->resource
);
237 if (trans
->linear_texture
) {
238 rws
->buffer_unmap(trans
->linear_texture
->cs_buf
);
240 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
241 r300_copy_into_tiled_texture(ctx
, trans
);
244 pipe_resource_reference(
245 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);
247 rws
->buffer_unmap(tex
->cs_buf
);