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_context.h"
25 #include "r300_transfer.h"
26 #include "r300_texture.h"
27 #include "r300_screen.h"
29 #include "r300_winsys.h"
31 #include "util/u_memory.h"
32 #include "util/u_format.h"
34 struct r300_transfer
{
36 struct pipe_transfer transfer
;
38 /* Offset from start of buffer. */
41 /* Detiled texture. */
42 struct r300_texture
*detiled_texture
;
44 /* Transfer and format flags. */
45 unsigned render_target_usage
;
48 /* Convenience cast wrapper. */
49 static INLINE
struct r300_transfer
*
50 r300_transfer(struct pipe_transfer
* transfer
)
52 return (struct r300_transfer
*)transfer
;
55 /* Copy from a tiled texture to a detiled one. */
56 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
57 struct r300_transfer
*r300transfer
)
59 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
60 struct pipe_resource
*tex
= transfer
->resource
;
61 struct pipe_subresource subdst
;
66 ctx
->resource_copy_region(ctx
, &r300transfer
->detiled_texture
->b
.b
, subdst
,
69 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
70 transfer
->box
.width
, transfer
->box
.height
);
73 /* Copy a detiled texture to a tiled one. */
74 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
75 struct r300_transfer
*r300transfer
)
77 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
78 struct pipe_resource
*tex
= transfer
->resource
;
79 struct pipe_subresource subsrc
;
84 /* XXX this flush prevents the following DRM error from occuring:
85 * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
86 * Reproducible with perf/copytex. */
87 ctx
->flush(ctx
, 0, NULL
);
89 ctx
->resource_copy_region(ctx
, tex
, transfer
->sr
,
90 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
91 &r300transfer
->detiled_texture
->b
.b
, subsrc
,
93 transfer
->box
.width
, transfer
->box
.height
);
95 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
96 ctx
->flush(ctx
, 0, NULL
);
100 r300_texture_get_transfer(struct pipe_context
*ctx
,
101 struct pipe_resource
*texture
,
102 struct pipe_subresource sr
,
104 const struct pipe_box
*box
)
106 struct r300_texture
*tex
= r300_texture(texture
);
107 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
108 struct r300_transfer
*trans
;
109 struct pipe_resource base
;
111 trans
= CALLOC_STRUCT(r300_transfer
);
113 /* Initialize the transfer object. */
114 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
115 trans
->transfer
.sr
= sr
;
116 trans
->transfer
.usage
= usage
;
117 trans
->transfer
.box
= *box
;
119 /* If the texture is tiled, we must create a temporary detiled texture
120 * for this transfer. */
121 if (tex
->microtile
|| tex
->macrotile
) {
122 trans
->render_target_usage
=
123 util_format_is_depth_or_stencil(texture
->format
) ?
124 PIPE_BIND_DEPTH_STENCIL
:
125 PIPE_BIND_RENDER_TARGET
;
127 base
.target
= PIPE_TEXTURE_2D
;
128 base
.format
= texture
->format
;
129 base
.width0
= box
->width
;
130 base
.height0
= box
->height
;
134 base
.usage
= PIPE_USAGE_DYNAMIC
;
136 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
138 /* For texture reading, the temporary (detiled) texture is used as
139 * a render target when blitting from a tiled texture. */
140 if (usage
& PIPE_TRANSFER_READ
) {
141 base
.bind
|= trans
->render_target_usage
;
143 /* For texture writing, the temporary texture is used as a sampler
144 * when blitting into a tiled texture. */
145 if (usage
& PIPE_TRANSFER_WRITE
) {
146 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
149 /* Create the temporary texture. */
150 trans
->detiled_texture
= r300_texture(
151 ctx
->screen
->resource_create(ctx
->screen
,
154 assert(!trans
->detiled_texture
->microtile
&&
155 !trans
->detiled_texture
->macrotile
);
159 * Even though we are using an internal texture for this,
160 * the transfer sr, box and usage parameters still reflect
161 * the arguments received to get_transfer. We just do the
162 * right thing internally.
164 trans
->transfer
.stride
=
165 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
167 if (usage
& PIPE_TRANSFER_READ
) {
168 /* We cannot map a tiled texture directly because the data is
169 * in a different order, therefore we do detiling using a blit. */
170 r300_copy_from_tiled_texture(ctx
, trans
);
173 trans
->transfer
.stride
=
174 r300_texture_get_stride(r300screen
, tex
, sr
.level
);
175 trans
->offset
= r300_texture_get_offset(tex
, sr
.level
, box
->z
, sr
.face
);
178 return &trans
->transfer
;
181 void r300_texture_transfer_destroy(struct pipe_context
*ctx
,
182 struct pipe_transfer
*trans
)
184 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
186 if (r300transfer
->detiled_texture
) {
187 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
188 r300_copy_into_tiled_texture(ctx
, r300transfer
);
191 pipe_resource_reference(
192 (struct pipe_resource
**)&r300transfer
->detiled_texture
, NULL
);
194 pipe_resource_reference(&trans
->resource
, NULL
);
198 void* r300_texture_transfer_map(struct pipe_context
*ctx
,
199 struct pipe_transfer
*transfer
)
201 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
202 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
203 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
205 enum pipe_format format
= tex
->b
.b
.format
;
207 if (r300transfer
->detiled_texture
) {
208 /* The detiled texture is of the same size as the region being mapped
209 * (no offset needed). */
210 return rws
->buffer_map(rws
,
211 r300transfer
->detiled_texture
->buffer
,
214 /* Tiling is disabled. */
215 map
= rws
->buffer_map(rws
, tex
->buffer
,
222 return map
+ r300_transfer(transfer
)->offset
+
223 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
224 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
228 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
229 struct pipe_transfer
*transfer
)
231 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
232 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
233 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
235 if (r300transfer
->detiled_texture
) {
236 rws
->buffer_unmap(rws
, r300transfer
->detiled_texture
->buffer
);
238 rws
->buffer_unmap(rws
, tex
->buffer
);