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
;
39 struct pipe_context
*ctx
;
41 /* Offset from start of buffer. */
44 /* Detiled texture. */
45 struct r300_texture
*detiled_texture
;
47 /* Transfer and format flags. */
48 unsigned render_target_usage
;
51 /* Convenience cast wrapper. */
52 static INLINE
struct r300_transfer
*
53 r300_transfer(struct pipe_transfer
* transfer
)
55 return (struct r300_transfer
*)transfer
;
58 /* Copy from a tiled texture to a detiled one. */
59 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
60 struct r300_transfer
*r300transfer
)
62 struct pipe_screen
*screen
= ctx
->screen
;
63 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
64 struct pipe_resource
*tex
= transfer
->resource
;
65 struct pipe_surface
*src
, *dst
;
67 src
= screen
->get_tex_surface(screen
, tex
,
71 PIPE_BIND_BLIT_SOURCE
);
73 dst
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->b
.b
,
75 PIPE_BIND_BLIT_DESTINATION
);
77 ctx
->surface_copy(ctx
, dst
, 0, 0, src
,
78 transfer
->box
.x
, transfer
->box
.y
,
79 transfer
->box
.width
, transfer
->box
.height
);
81 pipe_surface_reference(&src
, NULL
);
82 pipe_surface_reference(&dst
, NULL
);
85 /* Copy a detiled texture to a tiled one. */
86 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
87 struct r300_transfer
*r300transfer
)
89 struct pipe_screen
*screen
= ctx
->screen
;
90 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
91 struct pipe_resource
*tex
= transfer
->resource
;
92 struct pipe_surface
*src
, *dst
;
94 src
= screen
->get_tex_surface(screen
, &r300transfer
->detiled_texture
->b
.b
,
96 PIPE_BIND_BLIT_SOURCE
);
98 dst
= screen
->get_tex_surface(screen
, tex
,
102 PIPE_BIND_BLIT_DESTINATION
);
104 /* XXX this flush prevents the following DRM error from occuring:
105 * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
106 * Reproducible with perf/copytex. */
107 ctx
->flush(ctx
, 0, NULL
);
109 ctx
->surface_copy(ctx
, dst
,
110 transfer
->box
.x
, transfer
->box
.y
,
112 transfer
->box
.width
, transfer
->box
.height
);
114 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
115 ctx
->flush(ctx
, 0, NULL
);
117 pipe_surface_reference(&src
, NULL
);
118 pipe_surface_reference(&dst
, NULL
);
121 struct pipe_transfer
*
122 r300_texture_get_transfer(struct pipe_context
*ctx
,
123 struct pipe_resource
*texture
,
124 struct pipe_subresource sr
,
126 const struct pipe_box
*box
)
128 struct r300_texture
*tex
= r300_texture(texture
);
129 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
130 struct r300_transfer
*trans
;
131 struct pipe_resource base
;
133 trans
= CALLOC_STRUCT(r300_transfer
);
135 /* Initialize the transfer object. */
136 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
137 trans
->transfer
.sr
= sr
;
138 trans
->transfer
.usage
= usage
;
139 trans
->transfer
.box
= *box
;
142 /* If the texture is tiled, we must create a temporary detiled texture
143 * for this transfer. */
144 if (tex
->microtile
|| tex
->macrotile
) {
145 trans
->render_target_usage
=
146 util_format_is_depth_or_stencil(texture
->format
) ?
147 PIPE_BIND_DEPTH_STENCIL
:
148 PIPE_BIND_RENDER_TARGET
;
150 base
.target
= PIPE_TEXTURE_2D
;
151 base
.format
= texture
->format
;
152 base
.width0
= box
->width
;
153 base
.height0
= box
->height
;
157 base
._usage
= PIPE_USAGE_DYNAMIC
;
159 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
161 /* For texture reading, the temporary (detiled) texture is used as
162 * a render target when blitting from a tiled texture. */
163 if (usage
& PIPE_TRANSFER_READ
) {
164 base
.bind
|= trans
->render_target_usage
;
166 /* For texture writing, the temporary texture is used as a sampler
167 * when blitting into a tiled texture. */
168 if (usage
& PIPE_TRANSFER_WRITE
) {
169 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
172 /* Create the temporary texture. */
173 trans
->detiled_texture
= r300_texture(
174 ctx
->screen
->resource_create(ctx
->screen
,
177 assert(!trans
->detiled_texture
->microtile
&&
178 !trans
->detiled_texture
->macrotile
);
182 * Even though we are using an internal texture for this,
183 * the transfer sr, box and usage parameters still reflect
184 * the arguments received to get_transfer. We just do the
185 * right thing internally.
187 trans
->transfer
.stride
=
188 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
190 if (usage
& PIPE_TRANSFER_READ
) {
191 /* We cannot map a tiled texture directly because the data is
192 * in a different order, therefore we do detiling using a blit. */
193 r300_copy_from_tiled_texture(ctx
, trans
);
196 trans
->transfer
.stride
=
197 r300_texture_get_stride(r300screen
, tex
, sr
.level
);
198 trans
->offset
= r300_texture_get_offset(tex
, sr
.level
, box
->z
, sr
.face
);
201 return &trans
->transfer
;
204 void r300_texture_transfer_destroy(struct pipe_context
*ctx
,
205 struct pipe_transfer
*trans
)
207 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
209 if (r300transfer
->detiled_texture
) {
210 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
211 r300_copy_into_tiled_texture(r300transfer
->ctx
, r300transfer
);
214 pipe_resource_reference(
215 (struct pipe_resource
**)&r300transfer
->detiled_texture
, NULL
);
217 pipe_resource_reference(&trans
->resource
, NULL
);
221 void* r300_texture_transfer_map(struct pipe_context
*ctx
,
222 struct pipe_transfer
*transfer
)
224 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
225 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
226 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
228 enum pipe_format format
= tex
->b
.b
.format
;
230 if (r300transfer
->detiled_texture
) {
231 /* The detiled texture is of the same size as the region being mapped
232 * (no offset needed). */
233 return rws
->buffer_map(rws
,
234 r300transfer
->detiled_texture
->buffer
,
237 /* Tiling is disabled. */
238 map
= rws
->buffer_map(rws
, tex
->buffer
,
245 return map
+ r300_transfer(transfer
)->offset
+
246 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
247 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
251 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
252 struct pipe_transfer
*transfer
)
254 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
255 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
256 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
258 if (r300transfer
->detiled_texture
) {
259 rws
->buffer_unmap(rws
, r300transfer
->detiled_texture
->buffer
);
261 rws
->buffer_unmap(rws
, tex
->buffer
);