r300g: adapt to interface changes
[profile/ivi/mesa.git] / src / gallium / drivers / r300 / r300_transfer.c
1 /*
2  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3  * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4  *
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:
11  *
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
14  * Software.
15  *
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. */
23
24 #include "r300_context.h"
25 #include "r300_transfer.h"
26 #include "r300_texture.h"
27 #include "r300_screen.h"
28
29 #include "r300_winsys.h"
30
31 #include "util/u_memory.h"
32 #include "util/u_format.h"
33
34 struct r300_transfer {
35     /* Parent class */
36     struct pipe_transfer transfer;
37
38     /* Offset from start of buffer. */
39     unsigned offset;
40
41     /* Detiled texture. */
42     struct r300_texture *detiled_texture;
43
44     /* Transfer and format flags. */
45     unsigned render_target_usage;
46 };
47
48 /* Convenience cast wrapper. */
49 static INLINE struct r300_transfer*
50 r300_transfer(struct pipe_transfer* transfer)
51 {
52     return (struct r300_transfer*)transfer;
53 }
54
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)
58 {
59     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
60     struct pipe_resource *tex = transfer->resource;
61     struct pipe_subresource subdst;
62
63     subdst.face = 0;
64     subdst.level = 0;
65
66     ctx->resource_copy_region(ctx, &r300transfer->detiled_texture->b.b, subdst,
67                               0, 0, 0,
68                               tex, transfer->sr,
69                               transfer->box.x, transfer->box.y, transfer->box.z,
70                               transfer->box.width, transfer->box.height);
71 }
72
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)
76 {
77     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
78     struct pipe_resource *tex = transfer->resource;
79     struct pipe_subresource subsrc;
80
81     subsrc.face = 0;
82     subsrc.level = 0;
83
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);
88
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,
92                               0, 0, 0,
93                               transfer->box.width, transfer->box.height);
94
95     /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
96     ctx->flush(ctx, 0, NULL);
97 }
98
99 struct pipe_transfer*
100 r300_texture_get_transfer(struct pipe_context *ctx,
101                           struct pipe_resource *texture,
102                           struct pipe_subresource sr,
103                           unsigned usage,
104                           const struct pipe_box *box)
105 {
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;
110
111     trans = CALLOC_STRUCT(r300_transfer);
112     if (trans) {
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;
118
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;
126
127             base.target = PIPE_TEXTURE_2D;
128             base.format = texture->format;
129             base.width0 = box->width;
130             base.height0 = box->height;
131             base.depth0 = 0;
132             base.last_level = 0;
133             base.nr_samples = 0;
134             base.usage = PIPE_USAGE_DYNAMIC;
135             base.bind = 0;
136             base.flags = R300_RESOURCE_FLAG_TRANSFER;
137
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;
142             }
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;
147             }
148
149             /* Create the temporary texture. */
150             trans->detiled_texture = r300_texture(
151                ctx->screen->resource_create(ctx->screen,
152                                             &base));
153
154             assert(!trans->detiled_texture->microtile &&
155                    !trans->detiled_texture->macrotile);
156
157             /* Set the stride.
158              *
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.
163              */
164             trans->transfer.stride =
165                 r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
166
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);
171             }
172         } else {
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);
176         }
177     }
178     return &trans->transfer;
179 }
180
181 void r300_texture_transfer_destroy(struct pipe_context *ctx,
182                                    struct pipe_transfer *trans)
183 {
184     struct r300_transfer *r300transfer = r300_transfer(trans);
185
186     if (r300transfer->detiled_texture) {
187         if (trans->usage & PIPE_TRANSFER_WRITE) {
188             r300_copy_into_tiled_texture(ctx, r300transfer);
189         }
190
191         pipe_resource_reference(
192             (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
193     }
194     pipe_resource_reference(&trans->resource, NULL);
195     FREE(trans);
196 }
197
198 void* r300_texture_transfer_map(struct pipe_context *ctx,
199                                 struct pipe_transfer *transfer)
200 {
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);
204     char *map;
205     enum pipe_format format = tex->b.b.format;
206
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,
212                                transfer->usage);
213     } else {
214         /* Tiling is disabled. */
215         map = rws->buffer_map(rws, tex->buffer,
216                               transfer->usage);
217
218         if (!map) {
219             return NULL;
220         }
221
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);
225     }
226 }
227
228 void r300_texture_transfer_unmap(struct pipe_context *ctx,
229                                  struct pipe_transfer *transfer)
230 {
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);
234
235     if (r300transfer->detiled_texture) {
236         rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
237     } else {
238         rws->buffer_unmap(rws, tex->buffer);
239     }
240 }
241