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
;
45 /* Convenience cast wrapper. */
46 static INLINE
struct r300_transfer
*
47 r300_transfer(struct pipe_transfer
* transfer
)
49 return (struct r300_transfer
*)transfer
;
52 /* Copy from a tiled texture to a detiled one. */
53 static void r300_copy_from_tiled_texture(struct pipe_context
*ctx
,
54 struct r300_transfer
*r300transfer
)
56 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
57 struct pipe_resource
*tex
= transfer
->resource
;
58 struct pipe_subresource subdst
;
63 /* XXX if we don't flush before copying the texture and mapping it,
64 * we get wrong pixels, i.e. it's like latest draw calls didn't happen,
65 * including this blit. Tests: e.g. piglit/provoking-vertex
67 * Since the flush immediately before mapping is implicit (the buffer is
68 * always referenced in resource_copy_region), every read transfer costs
69 * 2 flushes. That sucks. */
70 ctx
->flush(ctx
, 0, NULL
);
72 ctx
->resource_copy_region(ctx
, &r300transfer
->detiled_texture
->b
.b
, subdst
,
75 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
76 transfer
->box
.width
, transfer
->box
.height
);
78 /* Flushing after the copy is implicit, issued by winsys. */
81 /* Copy a detiled texture to a tiled one. */
82 static void r300_copy_into_tiled_texture(struct pipe_context
*ctx
,
83 struct r300_transfer
*r300transfer
)
85 struct pipe_transfer
*transfer
= (struct pipe_transfer
*)r300transfer
;
86 struct pipe_resource
*tex
= transfer
->resource
;
87 struct pipe_subresource subsrc
;
92 ctx
->resource_copy_region(ctx
, tex
, transfer
->sr
,
93 transfer
->box
.x
, transfer
->box
.y
, transfer
->box
.z
,
94 &r300transfer
->detiled_texture
->b
.b
, subsrc
,
96 transfer
->box
.width
, transfer
->box
.height
);
98 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
99 ctx
->flush(ctx
, 0, NULL
);
102 struct pipe_transfer
*
103 r300_texture_get_transfer(struct pipe_context
*ctx
,
104 struct pipe_resource
*texture
,
105 struct pipe_subresource sr
,
107 const struct pipe_box
*box
)
109 struct r300_texture
*tex
= r300_texture(texture
);
110 struct r300_screen
*r300screen
= r300_screen(ctx
->screen
);
111 struct r300_transfer
*trans
;
112 struct pipe_resource base
;
114 trans
= CALLOC_STRUCT(r300_transfer
);
116 /* Initialize the transfer object. */
117 pipe_resource_reference(&trans
->transfer
.resource
, texture
);
118 trans
->transfer
.sr
= sr
;
119 trans
->transfer
.usage
= usage
;
120 trans
->transfer
.box
= *box
;
122 /* If the texture is tiled, we must create a temporary detiled texture
123 * for this transfer. */
124 if (tex
->microtile
|| tex
->macrotile
) {
125 base
.target
= PIPE_TEXTURE_2D
;
126 base
.format
= texture
->format
;
127 base
.width0
= box
->width
;
128 base
.height0
= box
->height
;
132 base
.usage
= PIPE_USAGE_DYNAMIC
;
134 base
.flags
= R300_RESOURCE_FLAG_TRANSFER
;
136 /* For texture reading, the temporary (detiled) texture is used as
137 * a render target when blitting from a tiled texture. */
138 if (usage
& PIPE_TRANSFER_READ
) {
139 base
.bind
|= PIPE_BIND_RENDER_TARGET
;
141 /* For texture writing, the temporary texture is used as a sampler
142 * when blitting into a tiled texture. */
143 if (usage
& PIPE_TRANSFER_WRITE
) {
144 base
.bind
|= PIPE_BIND_SAMPLER_VIEW
;
147 /* Create the temporary texture. */
148 trans
->detiled_texture
= r300_texture(
149 ctx
->screen
->resource_create(ctx
->screen
,
152 assert(!trans
->detiled_texture
->microtile
&&
153 !trans
->detiled_texture
->macrotile
);
157 * Even though we are using an internal texture for this,
158 * the transfer sr, box and usage parameters still reflect
159 * the arguments received to get_transfer. We just do the
160 * right thing internally.
162 trans
->transfer
.stride
=
163 r300_texture_get_stride(r300screen
, trans
->detiled_texture
, 0);
165 if (usage
& PIPE_TRANSFER_READ
) {
166 /* We cannot map a tiled texture directly because the data is
167 * in a different order, therefore we do detiling using a blit. */
168 r300_copy_from_tiled_texture(ctx
, trans
);
171 trans
->transfer
.stride
=
172 r300_texture_get_stride(r300screen
, tex
, sr
.level
);
173 trans
->offset
= r300_texture_get_offset(tex
, sr
.level
, box
->z
, sr
.face
);
176 return &trans
->transfer
;
179 void r300_texture_transfer_destroy(struct pipe_context
*ctx
,
180 struct pipe_transfer
*trans
)
182 struct r300_transfer
*r300transfer
= r300_transfer(trans
);
184 if (r300transfer
->detiled_texture
) {
185 if (trans
->usage
& PIPE_TRANSFER_WRITE
) {
186 r300_copy_into_tiled_texture(ctx
, r300transfer
);
189 pipe_resource_reference(
190 (struct pipe_resource
**)&r300transfer
->detiled_texture
, NULL
);
192 pipe_resource_reference(&trans
->resource
, NULL
);
196 void* r300_texture_transfer_map(struct pipe_context
*ctx
,
197 struct pipe_transfer
*transfer
)
199 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
200 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
201 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
203 enum pipe_format format
= tex
->b
.b
.format
;
205 if (r300transfer
->detiled_texture
) {
206 /* The detiled texture is of the same size as the region being mapped
207 * (no offset needed). */
208 return rws
->buffer_map(rws
,
209 r300transfer
->detiled_texture
->buffer
,
212 /* Tiling is disabled. */
213 map
= rws
->buffer_map(rws
, tex
->buffer
,
220 return map
+ r300_transfer(transfer
)->offset
+
221 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
222 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
226 void r300_texture_transfer_unmap(struct pipe_context
*ctx
,
227 struct pipe_transfer
*transfer
)
229 struct r300_winsys_screen
*rws
= (struct r300_winsys_screen
*)ctx
->winsys
;
230 struct r300_transfer
*r300transfer
= r300_transfer(transfer
);
231 struct r300_texture
*tex
= r300_texture(transfer
->resource
);
233 if (r300transfer
->detiled_texture
) {
234 rws
->buffer_unmap(rws
, r300transfer
->detiled_texture
->buffer
);
236 rws
->buffer_unmap(rws
, tex
->buffer
);