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/format/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
;
91 transfer
->box
.width
, transfer
->box
.height
, transfer
->box
.depth
,
94 ctx
->resource_copy_region(ctx
, tex
, transfer
->level
,
95 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
96 &r300transfer
->linear_texture
->b
.b
, 0, &src_box
);
98 /* XXX remove this. */
99 r300_flush(ctx
, 0, NULL
);
103 r300_texture_transfer_map(struct pipe_context
*ctx
,
104 struct pipe_resource
*texture
,
107 const struct pipe_box
*box
,
108 struct pipe_transfer
**transfer
)
110 struct r300_context
*r300
= r300_context(ctx
);
111 struct r300_resource
*tex
= r300_resource(texture
);
112 struct r300_transfer
*trans
;
113 boolean referenced_cs
, referenced_hw
;
114 enum pipe_format format
= tex
->b
.b
.format
;
118 r300
->rws
->cs_is_buffer_referenced(r300
->cs
, tex
->buf
, RADEON_USAGE_READWRITE
);
120 referenced_hw
= TRUE
;
123 !r300
->rws
->buffer_wait(tex
->buf
, 0, RADEON_USAGE_READWRITE
);
126 trans
= CALLOC_STRUCT(r300_transfer
);
128 /* Initialize the transfer object. */
129 trans
->transfer
.resource
= texture
;
130 trans
->transfer
.level
= level
;
131 trans
->transfer
.usage
= usage
;
132 trans
->transfer
.box
= *box
;
134 /* If the texture is tiled, we must create a temporary detiled texture
136 * Also make write transfers pipelined. */
137 if (tex
->tex
.microtile
|| tex
->tex
.macrotile
[level
] ||
138 (referenced_hw
&& !(usage
& PIPE_TRANSFER_READ
) &&
139 r300_is_blit_supported(texture
->format
))) {
140 struct pipe_resource base
;
142 if (r300
->blitter
->running
) {
143 fprintf(stderr
, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
147 memset(&base
, 0, sizeof(base
));
148 base
.target
= PIPE_TEXTURE_2D
;
149 base
.format
= texture
->format
;
150 base
.width0
= box
->width
;
151 base
.height0
= box
->height
;
154 base
.usage
= PIPE_USAGE_STAGING
;
155 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
157 /* We must set the correct texture target and dimensions if needed for a 3D transfer. */
158 if (box
->depth
> 1 && util_max_layer(texture
, level
) > 0) {
159 base
.target
= texture
->target
;
161 if (base
.target
== PIPE_TEXTURE_3D
) {
162 base
.depth0
= util_next_power_of_two(box
->depth
);
166 /* Create the temporary texture. */
167 trans
->linear_texture
= r300_resource(
168 ctx
->screen
->resource_create(ctx
->screen
,
171 if (!trans
->linear_texture
) {
172 /* Oh crap, the thing can't create the texture.
173 * Let's flush and try again. */
174 r300_flush(ctx
, 0, NULL
);
176 trans
->linear_texture
= r300_resource(
177 ctx
->screen
->resource_create(ctx
->screen
,
180 if (!trans
->linear_texture
) {
182 "r300: Failed to create a transfer object.\n");
188 assert(!trans
->linear_texture
->tex
.microtile
&&
189 !trans
->linear_texture
->tex
.macrotile
[0]);
191 /* Set the stride. */
192 trans
->transfer
.stride
=
193 trans
->linear_texture
->tex
.stride_in_bytes
[0];
194 trans
->transfer
.layer_stride
=
195 trans
->linear_texture
->tex
.layer_size_in_bytes
[0];
197 if (usage
& PIPE_TRANSFER_READ
) {
198 /* We cannot map a tiled texture directly because the data is
199 * in a different order, therefore we do detiling using a blit. */
200 r300_copy_from_tiled_texture(ctx
, trans
);
202 /* Always referenced in the blit. */
203 r300_flush(ctx
, 0, NULL
);
206 /* Unpipelined transfer. */
207 trans
->transfer
.stride
= tex
->tex
.stride_in_bytes
[level
];
208 trans
->transfer
.layer_stride
= tex
->tex
.layer_size_in_bytes
[level
];
209 trans
->offset
= r300_texture_get_offset(tex
, level
, box
->z
);
212 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
213 r300_flush(ctx
, 0, NULL
);
218 if (trans
->linear_texture
) {
219 /* The detiled texture is of the same size as the region being mapped
220 * (no offset needed). */
221 map
= r300
->rws
->buffer_map(trans
->linear_texture
->buf
,
224 pipe_resource_reference(
225 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);
229 *transfer
= &trans
->transfer
;
232 /* Tiling is disabled. */
233 map
= r300
->rws
->buffer_map(tex
->buf
, r300
->cs
, usage
);
239 *transfer
= &trans
->transfer
;
240 return map
+ trans
->offset
+
241 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
242 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
246 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
247 struct pipe_transfer
*transfer
)
249 struct r300_transfer
*trans
= r300_transfer(transfer
);
251 if (trans
->linear_texture
) {
252 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
253 r300_copy_into_tiled_texture(ctx
, trans
);
256 pipe_resource_reference(
257 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);