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
*tex
= transfer
->resource
;
57 ctx
->resource_copy_region(ctx
, &r300transfer
->linear_texture
->b
.b
, 0,
59 tex
, transfer
->level
, &transfer
->box
);
62 /* Copy a detiled texture to a tiled one. */
63 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
64 struct r300_transfer
*r300transfer
)
66 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
67 struct pipe_resource
*tex
= transfer
->resource
;
68 struct pipe_box src_box
;
69 u_box_origin_2d(transfer
->box
.width
, transfer
->box
.height
, &src_box
);
71 ctx
->resource_copy_region(ctx
, tex
, transfer
->level
,
72 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
73 &r300transfer
->linear_texture
->b
.b
, 0, &src_box
);
75 /* XXX remove this. */
76 r300_flush(ctx
, 0, NULL
);
80 r300_texture_transfer_map(struct pipe_context
*ctx
,
81 struct pipe_resource
*texture
,
84 const struct pipe_box
*box
,
85 struct pipe_transfer
**transfer
)
87 struct r300_context
*r300
= r300_context(ctx
);
88 struct r300_resource
*tex
= r300_resource(texture
);
89 struct r300_transfer
*trans
;
90 struct pipe_resource base
;
91 boolean referenced_cs
, referenced_hw
;
92 enum pipe_format format
= tex
->b
.b
.format
;
96 r300
->rws
->cs_is_buffer_referenced(r300
->cs
, tex
->cs_buf
, RADEON_USAGE_READWRITE
);
101 r300
->rws
->buffer_is_busy(tex
->buf
, RADEON_USAGE_READWRITE
);
104 trans
= CALLOC_STRUCT(r300_transfer
);
106 /* Initialize the transfer object. */
107 trans
->transfer
.resource
= texture
;
108 trans
->transfer
.level
= level
;
109 trans
->transfer
.usage
= usage
;
110 trans
->transfer
.box
= *box
;
112 /* If the texture is tiled, we must create a temporary detiled texture
114 * Also make write transfers pipelined. */
115 if (tex
->tex
.microtile
|| tex
->tex
.macrotile
[level
] ||
116 (referenced_hw
&& !(usage
& PIPE_TRANSFER_READ
) &&
117 r300_is_blit_supported(texture
->format
))) {
118 if (r300
->blitter
->running
) {
119 fprintf(stderr
, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
123 base
.target
= PIPE_TEXTURE_2D
;
124 base
.format
= texture
->format
;
125 base
.width0
= box
->width
;
126 base
.height0
= box
->height
;
131 base
.usage
= PIPE_USAGE_STAGING
;
133 if (usage
& PIPE_TRANSFER_READ
) {
134 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
136 if (usage
& PIPE_TRANSFER_WRITE
) {
137 base
.bind
|= PIPE_BIND_RENDER_TARGET
;
139 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
141 /* For texture reading, the temporary (detiled) texture is used as
142 * a render target when blitting from a tiled texture. */
143 if (usage
& PIPE_TRANSFER_READ
) {
144 base
.bind
|= PIPE_BIND_RENDER_TARGET
;
146 /* For texture writing, the temporary texture is used as a sampler
147 * when blitting into a tiled texture. */
148 if (usage
& PIPE_TRANSFER_WRITE
) {
149 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
152 /* Create the temporary texture. */
153 trans
->linear_texture
= r300_resource(
154 ctx
->screen
->resource_create(ctx
->screen
,
157 if (!trans
->linear_texture
) {
158 /* Oh crap, the thing can't create the texture.
159 * Let's flush and try again. */
160 r300_flush(ctx
, 0, NULL
);
162 trans
->linear_texture
= r300_resource(
163 ctx
->screen
->resource_create(ctx
->screen
,
166 if (!trans
->linear_texture
) {
168 "r300: Failed to create a transfer object.\n");
174 assert(!trans
->linear_texture
->tex
.microtile
&&
175 !trans
->linear_texture
->tex
.macrotile
[0]);
177 /* Set the stride. */
178 trans
->transfer
.stride
=
179 trans
->linear_texture
->tex
.stride_in_bytes
[0];
181 if (usage
& PIPE_TRANSFER_READ
) {
182 /* We cannot map a tiled texture directly because the data is
183 * in a different order, therefore we do detiling using a blit. */
184 r300_copy_from_tiled_texture(ctx
, trans
);
186 /* Always referenced in the blit. */
187 r300_flush(ctx
, 0, NULL
);
190 /* Unpipelined transfer. */
191 trans
->transfer
.stride
= tex
->tex
.stride_in_bytes
[level
];
192 trans
->offset
= r300_texture_get_offset(tex
, level
, box
->z
);
195 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
196 r300_flush(ctx
, 0, NULL
);
201 if (trans
->linear_texture
) {
202 /* The detiled texture is of the same size as the region being mapped
203 * (no offset needed). */
204 map
= r300
->rws
->buffer_map(trans
->linear_texture
->cs_buf
,
207 pipe_resource_reference(
208 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);
212 *transfer
= &trans
->transfer
;
215 /* Tiling is disabled. */
216 map
= r300
->rws
->buffer_map(tex
->cs_buf
, r300
->cs
, usage
);
222 *transfer
= &trans
->transfer
;
223 return map
+ trans
->offset
+
224 box
->y
/ util_format_get_blockheight(format
) * trans
->transfer
.stride
+
225 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
229 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
230 struct pipe_transfer
*transfer
)
232 struct radeon_winsys
*rws
= r300_context(ctx
)->rws
;
233 struct r300_transfer
*trans
= r300_transfer(transfer
);
234 struct r300_resource
*tex
= r300_resource(transfer
->resource
);
236 if (trans
->linear_texture
) {
237 rws
->buffer_unmap(trans
->linear_texture
->cs_buf
);
239 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
240 r300_copy_into_tiled_texture(ctx
, trans
);
243 pipe_resource_reference(
244 (struct pipe_resource
**)&trans
->linear_texture
, NULL
);
246 rws
->buffer_unmap(tex
->cs_buf
);