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.h"
26 #include "r300_screen_buffer.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
31 struct r300_transfer
{
33 struct pipe_transfer transfer
;
35 /* Offset from start of buffer. */
38 /* Detiled texture. */
39 struct r300_texture
*detiled_texture
;
42 /* Convenience cast wrapper. */
43 static INLINE
struct r300_transfer
*
44 r300_transfer(struct pipe_transfer
* transfer
)
46 return (struct r300_transfer
*)transfer
;
49 /* Copy from a tiled texture to a detiled one. */
50 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
51 struct r300_transfer
*r300transfer
)
53 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
54 struct pipe_resource
*tex
= transfer
->resource
;
55 struct pipe_subresource subdst
;
60 ctx
->resource_copy_region(ctx
, &r300transfer
->detiled_texture
->b
.b
, subdst
,
63 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
64 transfer
->box
.width
, transfer
->box
.height
);
67 /* Copy a detiled texture to a tiled one. */
68 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
69 struct r300_transfer
*r300transfer
)
71 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
72 struct pipe_resource
*tex
= transfer
->resource
;
73 struct pipe_subresource subsrc
;
78 ctx
->resource_copy_region(ctx
, tex
, transfer
->sr
,
79 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
80 &r300transfer
->detiled_texture
->b
.b
, subsrc
,
82 transfer
->box
.width
, transfer
->box
.height
);
84 ctx
->flush(ctx
, 0, NULL
);
88 r300_texture_get_transfer(struct pipe_context
*ctx
,
89 struct pipe_resource
*texture
,
90 struct pipe_subresource sr
,
92 const struct pipe_box
*box
)
94 struct r300_context
*r300
= r300_context(ctx
);
95 struct r300_texture
*tex
= r300_texture(texture
);
96 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
97 struct r300_transfer
*trans
;
98 struct pipe_resource base
;
99 boolean referenced_cs
, referenced_hw
, blittable
;
102 r300
->rws
->cs_is_buffer_referenced(r300
->cs
,
103 tex
->buffer
, R300_REF_CS
);
105 referenced_hw
= TRUE
;
108 r300
->rws
->cs_is_buffer_referenced(r300
->cs
,
109 tex
->buffer
, R300_REF_HW
);
112 blittable
= ctx
->screen
->is_format_supported(
113 ctx
->screen
, texture
->format
, texture
->target
, 0,
114 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
, 0);
116 trans
= CALLOC_STRUCT(r300_transfer
);
118 /* Initialize the transfer object. */
119 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
120 trans
->transfer
.sr
= sr
;
121 trans
->transfer
.usage
= usage
;
122 trans
->transfer
.box
= *box
;
124 /* If the texture is tiled, we must create a temporary detiled texture
126 * Also make write transfers pipelined. */
127 if (tex
->microtile
|| tex
->macrotile
||
128 ((referenced_hw
& !(usage
& PIPE_TRANSFER_READ
)) && blittable
)) {
129 base
.target
= PIPE_TEXTURE_2D
;
130 base
.format
= texture
->format
;
131 base
.width0
= box
->width
;
132 base
.height0
= box
->height
;
136 base
.usage
= PIPE_USAGE_DYNAMIC
;
138 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
140 /* For texture reading, the temporary (detiled) texture is used as
141 * a render target when blitting from a tiled texture. */
142 if (usage
& PIPE_TRANSFER_READ
) {
143 base
.bind
|= PIPE_BIND_RENDER_TARGET
;
145 /* For texture writing, the temporary texture is used as a sampler
146 * when blitting into a tiled texture. */
147 if (usage
& PIPE_TRANSFER_WRITE
) {
148 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
151 /* Create the temporary texture. */
152 trans
->detiled_texture
= r300_texture(
153 ctx
->screen
->resource_create(ctx
->screen
,
156 if (!trans
->detiled_texture
) {
157 /* Oh crap, the thing can't create the texture.
158 * Let's flush and try again. */
159 ctx
->flush(ctx
, 0, NULL
);
161 trans
->detiled_texture
= r300_texture(
162 ctx
->screen
->resource_create(ctx
->screen
,
165 if (!trans
->detiled_texture
) {
166 /* For linear textures, it's safe to fallback to
167 * an unpipelined transfer. */
168 if (!tex
->microtile
&& !tex
->macrotile
) {
172 /* Otherwise, go to hell. */
174 "r300: Failed to create a transfer object, praise.\n");
180 assert(!trans
->detiled_texture
->microtile
&&
181 !trans
->detiled_texture
->macrotile
);
185 * Even though we are using an internal texture for this,
186 * the transfer sr, box and usage parameters still reflect
187 * the arguments received to get_transfer. We just do the
188 * right thing internally.
190 trans
->transfer
.stride
=
191 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
193 if (usage
& PIPE_TRANSFER_READ
) {
194 /* We cannot map a tiled texture directly because the data is
195 * in a different order, therefore we do detiling using a blit. */
196 r300_copy_from_tiled_texture(ctx
, trans
);
198 /* Always referenced in the blit. */
199 ctx
->flush(ctx
, 0, NULL
);
201 return &trans
->transfer
;
205 /* Unpipelined transfer. */
206 trans
->transfer
.stride
=
207 r300_texture_get_stride(r300screen
, tex
, sr
.level
);
208 trans
->offset
= r300_texture_get_offset(tex
, sr
.level
, box
->z
, sr
.face
);
211 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
212 return &trans
->transfer
;
217 void r300_texture_transfer_destroy(struct pipe_context
*ctx
,
218 struct pipe_transfer
*trans
)
220 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
222 if (r300transfer
->detiled_texture
) {
223 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
224 r300_copy_into_tiled_texture(ctx
, r300transfer
);
227 pipe_resource_reference(
228 (struct pipe_resource
**)&r300transfer
->detiled_texture
, NULL
);
230 pipe_resource_reference(&trans
->resource
, NULL
);
234 void* r300_texture_transfer_map(struct pipe_context
*ctx
,
235 struct pipe_transfer
*transfer
)
237 struct r300_context
*r300
= r300_context(ctx
);
238 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
239 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
240 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
242 enum pipe_format format
= tex
->b
.b
.format
;
244 if (r300transfer
->detiled_texture
) {
245 /* The detiled texture is of the same size as the region being mapped
246 * (no offset needed). */
247 return rws
->buffer_map(rws
,
248 r300transfer
->detiled_texture
->buffer
,
252 /* Tiling is disabled. */
253 map
= rws
->buffer_map(rws
, tex
->buffer
, r300
->cs
,
260 return map
+ r300_transfer(transfer
)->offset
+
261 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
262 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
266 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
267 struct pipe_transfer
*transfer
)
269 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
270 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
271 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
273 if (r300transfer
->detiled_texture
) {
274 rws
->buffer_unmap(rws
, r300transfer
->detiled_texture
->buffer
);
276 rws
->buffer_unmap(rws
, tex
->buffer
);