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_screen
*screen
= ctx
->screen
;
60 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
61 struct pipe_resource
*tex
= transfer
->resource
;
62 struct pipe_surface
*src
, *dst
;
64 src
= screen
->get_tex_surface(screen
, tex
,
68 PIPE_BIND_BLIT_SOURCE
);
70 dst
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->b
.b
,
72 PIPE_BIND_BLIT_DESTINATION
);
74 ctx
->surface_copy(ctx
, dst
, 0, 0, src
,
75 transfer
->box
.x
, transfer
->box
.y
,
76 transfer
->box
.width
, transfer
->box
.height
);
78 pipe_surface_reference(&src
, NULL
);
79 pipe_surface_reference(&dst
, NULL
);
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_screen
*screen
= ctx
->screen
;
87 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
88 struct pipe_resource
*tex
= transfer
->resource
;
89 struct pipe_surface
*src
, *dst
;
91 src
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->b
.b
,
93 PIPE_BIND_BLIT_SOURCE
);
95 dst
= screen
->get_tex_surface(screen
, tex
,
99 PIPE_BIND_BLIT_DESTINATION
);
101 /* XXX this flush prevents the following DRM error from occuring:
102 * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
103 * Reproducible with perf/copytex. */
104 ctx
->flush(ctx
, 0, NULL
);
106 ctx
->surface_copy(ctx
, dst
,
107 transfer
->box
.x
, transfer
->box
.y
,
109 transfer
->box
.width
, transfer
->box
.height
);
111 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
112 ctx
->flush(ctx
, 0, NULL
);
114 pipe_surface_reference(&src
, NULL
);
115 pipe_surface_reference(&dst
, NULL
);
118 struct pipe_transfer
*
119 r300_texture_get_transfer(struct pipe_context
*ctx
,
120 struct pipe_resource
*texture
,
121 struct pipe_subresource sr
,
123 const struct pipe_box
*box
)
125 struct r300_texture
*tex
= r300_texture(texture
);
126 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
127 struct r300_transfer
*trans
;
128 struct pipe_resource base
;
130 trans
= CALLOC_STRUCT(r300_transfer
);
132 /* Initialize the transfer object. */
133 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
134 trans
->transfer
.sr
= sr
;
135 trans
->transfer
.usage
= usage
;
136 trans
->transfer
.box
= *box
;
138 /* If the texture is tiled, we must create a temporary detiled texture
139 * for this transfer. */
140 if (tex
->microtile
|| tex
->macrotile
) {
141 trans
->render_target_usage
=
142 util_format_is_depth_or_stencil(texture
->format
) ?
143 PIPE_BIND_DEPTH_STENCIL
:
144 PIPE_BIND_RENDER_TARGET
;
146 base
.target
= PIPE_TEXTURE_2D
;
147 base
.format
= texture
->format
;
148 base
.width0
= box
->width
;
149 base
.height0
= box
->height
;
153 base
._usage
= PIPE_USAGE_DYNAMIC
;
155 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
157 /* For texture reading, the temporary (detiled) texture is used as
158 * a render target when blitting from a tiled texture. */
159 if (usage
& PIPE_TRANSFER_READ
) {
160 base
.bind
|= trans
->render_target_usage
;
162 /* For texture writing, the temporary texture is used as a sampler
163 * when blitting into a tiled texture. */
164 if (usage
& PIPE_TRANSFER_WRITE
) {
165 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
168 /* Create the temporary texture. */
169 trans
->detiled_texture
= r300_texture(
170 ctx
->screen
->resource_create(ctx
->screen
,
173 assert(!trans
->detiled_texture
->microtile
&&
174 !trans
->detiled_texture
->macrotile
);
178 * Even though we are using an internal texture for this,
179 * the transfer sr, box and usage parameters still reflect
180 * the arguments received to get_transfer. We just do the
181 * right thing internally.
183 trans
->transfer
.stride
=
184 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
186 if (usage
& PIPE_TRANSFER_READ
) {
187 /* We cannot map a tiled texture directly because the data is
188 * in a different order, therefore we do detiling using a blit. */
189 r300_copy_from_tiled_texture(ctx
, trans
);
192 trans
->transfer
.stride
=
193 r300_texture_get_stride(r300screen
, tex
, sr
.level
);
194 trans
->offset
= r300_texture_get_offset(tex
, sr
.level
, box
->z
, sr
.face
);
197 return &trans
->transfer
;
200 void r300_texture_transfer_destroy(struct pipe_context
*ctx
,
201 struct pipe_transfer
*trans
)
203 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
205 if (r300transfer
->detiled_texture
) {
206 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
207 r300_copy_into_tiled_texture(ctx
, r300transfer
);
210 pipe_resource_reference(
211 (struct pipe_resource
**)&r300transfer
->detiled_texture
, NULL
);
213 pipe_resource_reference(&trans
->resource
, NULL
);
217 void* r300_texture_transfer_map(struct pipe_context
*ctx
,
218 struct pipe_transfer
*transfer
)
220 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
221 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
222 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
224 enum pipe_format format
= tex
->b
.b
.format
;
226 if (r300transfer
->detiled_texture
) {
227 /* The detiled texture is of the same size as the region being mapped
228 * (no offset needed). */
229 return rws
->buffer_map(rws
,
230 r300transfer
->detiled_texture
->buffer
,
233 /* Tiling is disabled. */
234 map
= rws
->buffer_map(rws
, tex
->buffer
,
241 return map
+ r300_transfer(transfer
)->offset
+
242 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
243 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
247 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
248 struct pipe_transfer
*transfer
)
250 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
251 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
252 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
254 if (r300transfer
->detiled_texture
) {
255 rws
->buffer_unmap(rws
, r300transfer
->detiled_texture
->buffer
);
257 rws
->buffer_unmap(rws
, tex
->buffer
);