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 "util/u_memory.h"
30 #include "util/u_format.h"
32 struct r300_transfer
{
34 struct pipe_transfer transfer
;
37 struct pipe_context
*ctx
;
39 /* Parameters of get_tex_transfer. */
40 unsigned x
, y
, level
, zslice
, face
;
42 /* Offset from start of buffer. */
45 /* Detiled texture. */
46 struct r300_texture
*detiled_texture
;
48 /* Transfer and format flags. */
49 unsigned buffer_usage
, render_target_usage
;
52 /* Convenience cast wrapper. */
53 static INLINE
struct r300_transfer
*
54 r300_transfer(struct pipe_transfer
* transfer
)
56 return (struct r300_transfer
*)transfer
;
59 /* Copy from a tiled texture to a detiled one. */
60 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
61 struct r300_transfer
*r300transfer
)
63 struct pipe_screen
*screen
= ctx
->screen
;
64 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
65 struct pipe_texture
*tex
= transfer
->texture
;
66 struct pipe_surface
*src
, *dst
;
68 src
= screen
->get_tex_surface(screen
, tex
, r300transfer
->face
,
69 r300transfer
->level
, r300transfer
->zslice
,
70 PIPE_BUFFER_USAGE_GPU_READ
|
71 PIPE_BUFFER_USAGE_PIXEL
);
73 dst
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->tex
,
75 PIPE_BUFFER_USAGE_GPU_WRITE
|
76 PIPE_BUFFER_USAGE_PIXEL
|
77 r300transfer
->buffer_usage
);
79 ctx
->surface_copy(ctx
, dst
, 0, 0, src
, r300transfer
->x
, r300transfer
->y
,
80 transfer
->width
, transfer
->height
);
82 pipe_surface_reference(&src
, NULL
);
83 pipe_surface_reference(&dst
, NULL
);
86 /* Copy a detiled texture to a tiled one. */
87 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
88 struct r300_transfer
*r300transfer
)
90 struct pipe_screen
*screen
= ctx
->screen
;
91 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
92 struct pipe_texture
*tex
= transfer
->texture
;
93 struct pipe_surface
*src
, *dst
;
95 src
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->tex
,
97 PIPE_BUFFER_USAGE_GPU_READ
|
98 PIPE_BUFFER_USAGE_PIXEL
);
100 dst
= screen
->get_tex_surface(screen
, tex
, r300transfer
->face
,
101 r300transfer
->level
, r300transfer
->zslice
,
102 PIPE_BUFFER_USAGE_GPU_WRITE
|
103 PIPE_BUFFER_USAGE_PIXEL
);
105 /* XXX this flush prevents the following DRM error from occuring:
106 * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
107 * Reproducible with perf/copytex. */
108 ctx
->flush(ctx
, 0, NULL
);
110 ctx
->surface_copy(ctx
, dst
, r300transfer
->x
, r300transfer
->y
, src
, 0, 0,
111 transfer
->width
, transfer
->height
);
113 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
114 ctx
->flush(ctx
, 0, NULL
);
116 pipe_surface_reference(&src
, NULL
);
117 pipe_surface_reference(&dst
, NULL
);
120 static struct pipe_transfer
*
121 r300_get_tex_transfer(struct pipe_context
*ctx
,
122 struct pipe_texture
*texture
,
123 unsigned face
, unsigned level
, unsigned zslice
,
124 enum pipe_transfer_usage usage
, unsigned x
, unsigned y
,
125 unsigned w
, unsigned h
)
127 struct r300_texture
*tex
= (struct r300_texture
*)texture
;
128 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
129 struct r300_transfer
*trans
;
130 struct pipe_texture
template;
132 trans
= CALLOC_STRUCT(r300_transfer
);
134 /* Initialize the transfer object. */
135 pipe_texture_reference(&trans
->transfer
.texture
, texture
);
136 trans
->transfer
.usage
= usage
;
137 trans
->transfer
.width
= w
;
138 trans
->transfer
.height
= h
;
142 trans
->level
= level
;
143 trans
->zslice
= zslice
;
146 /* If the texture is tiled, we must create a temporary detiled texture
147 * for this transfer. */
148 if (tex
->microtile
|| tex
->macrotile
) {
149 trans
->buffer_usage
= pipe_transfer_buffer_flags(&trans
->transfer
);
150 trans
->render_target_usage
=
151 util_format_is_depth_or_stencil(texture
->format
) ?
152 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
:
153 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
155 template.target
= PIPE_TEXTURE_2D
;
156 template.format
= texture
->format
;
158 template.height0
= h
;
160 template.last_level
= 0;
161 template.nr_samples
= 0;
162 template.tex_usage
= PIPE_TEXTURE_USAGE_DYNAMIC
|
163 R300_TEXTURE_USAGE_TRANSFER
;
165 /* For texture reading, the temporary (detiled) texture is used as
166 * a render target when blitting from a tiled texture. */
167 if (usage
& PIPE_TRANSFER_READ
) {
168 template.tex_usage
|= trans
->render_target_usage
;
170 /* For texture writing, the temporary texture is used as a sampler
171 * when blitting into a tiled texture. */
172 if (usage
& PIPE_TRANSFER_WRITE
) {
173 template.tex_usage
|= PIPE_TEXTURE_USAGE_SAMPLER
;
176 /* Create the temporary texture. */
177 trans
->detiled_texture
= (struct r300_texture
*)
178 ctx
->screen
->texture_create(ctx
->screen
,
181 assert(!trans
->detiled_texture
->microtile
&&
182 !trans
->detiled_texture
->macrotile
);
185 * Parameters x, y, level, zslice, and face remain zero. */
186 trans
->transfer
.stride
=
187 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
189 if (usage
& PIPE_TRANSFER_READ
) {
190 /* We cannot map a tiled texture directly because the data is
191 * in a different order, therefore we do detiling using a blit. */
192 r300_copy_from_tiled_texture(ctx
, trans
);
195 trans
->transfer
.x
= x
;
196 trans
->transfer
.y
= y
;
197 trans
->transfer
.stride
=
198 r300_texture_get_stride(r300screen
, tex
, level
);
199 trans
->transfer
.level
= level
;
200 trans
->transfer
.zslice
= zslice
;
201 trans
->transfer
.face
= face
;
202 trans
->offset
= r300_texture_get_offset(tex
, level
, zslice
, face
);
205 return &trans
->transfer
;
208 static void r300_tex_transfer_destroy(struct pipe_context
*ctx
,
209 struct pipe_transfer
*trans
)
211 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
213 if (r300transfer
->detiled_texture
) {
214 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
215 r300_copy_into_tiled_texture(r300transfer
->ctx
, r300transfer
);
218 pipe_texture_reference(
219 (struct pipe_texture
**)&r300transfer
->detiled_texture
, NULL
);
221 pipe_texture_reference(&trans
->texture
, NULL
);
225 static void* r300_transfer_map(struct pipe_context
*ctx
,
226 struct pipe_transfer
*transfer
)
228 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
229 struct r300_texture
*tex
= (struct r300_texture
*)transfer
->texture
;
231 enum pipe_format format
= tex
->tex
.format
;
233 if (r300transfer
->detiled_texture
) {
234 /* The detiled texture is of the same size as the region being mapped
235 * (no offset needed). */
236 return pipe_buffer_map(ctx
->screen
,
237 r300transfer
->detiled_texture
->buffer
,
238 pipe_transfer_buffer_flags(transfer
));
240 /* Tiling is disabled. */
241 map
= pipe_buffer_map(ctx
->screen
, tex
->buffer
,
242 pipe_transfer_buffer_flags(transfer
));
248 return map
+ r300_transfer(transfer
)->offset
+
249 transfer
->y
/ util_format_get_blockheight(format
) * transfer
->stride
+
250 transfer
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
254 static void r300_transfer_unmap(struct pipe_context
*ctx
,
255 struct pipe_transfer
*transfer
)
257 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
258 struct r300_texture
*tex
= (struct r300_texture
*)transfer
->texture
;
260 if (r300transfer
->detiled_texture
) {
261 pipe_buffer_unmap(ctx
->screen
, r300transfer
->detiled_texture
->buffer
);
263 pipe_buffer_unmap(ctx
->screen
, tex
->buffer
);
268 void r300_init_transfer_functions( struct r300_context
*r300ctx
)
270 struct pipe_context
*ctx
= &r300ctx
->context
;
272 ctx
->get_tex_transfer
= r300_get_tex_transfer
;
273 ctx
->tex_transfer_destroy
= r300_tex_transfer_destroy
;
274 ctx
->transfer_map
= r300_transfer_map
;
275 ctx
->transfer_unmap
= r300_transfer_unmap
;