r300_transfer.c revision 60a053510155c119a6927bf7114e597066f8c50a
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
34struct 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. */
49static INLINE struct r300_transfer*
50r300_transfer(struct pipe_transfer* transfer)
51{
52    return (struct r300_transfer*)transfer;
53}
54
55/* Copy from a tiled texture to a detiled one. */
56static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
57                                         struct r300_transfer *r300transfer)
58{
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;
63
64    src = screen->get_tex_surface(screen, tex,
65				  transfer->sr.face,
66                                  transfer->sr.level,
67				  transfer->box.z,
68				  PIPE_BIND_BLIT_SOURCE);
69
70    dst = screen->get_tex_surface(screen, &r300transfer->detiled_texture->b.b,
71                                  0, 0, 0,
72                                  PIPE_BIND_BLIT_DESTINATION);
73
74    ctx->surface_copy(ctx, dst, 0, 0, src,
75		      transfer->box.x, transfer->box.y,
76                      transfer->box.width, transfer->box.height);
77
78    pipe_surface_reference(&src, NULL);
79    pipe_surface_reference(&dst, NULL);
80}
81
82/* Copy a detiled texture to a tiled one. */
83static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
84                                         struct r300_transfer *r300transfer)
85{
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;
90
91    src = screen->get_tex_surface(screen, &r300transfer->detiled_texture->b.b,
92                                  0, 0, 0,
93                                  PIPE_BIND_BLIT_SOURCE);
94
95    dst = screen->get_tex_surface(screen, tex,
96				  transfer->sr.face,
97                                  transfer->sr.level,
98				  transfer->box.z,
99                                  PIPE_BIND_BLIT_DESTINATION);
100
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);
105
106    ctx->surface_copy(ctx, dst,
107		      transfer->box.x, transfer->box.y,
108		      src, 0, 0,
109                      transfer->box.width, transfer->box.height);
110
111    /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
112    ctx->flush(ctx, 0, NULL);
113
114    pipe_surface_reference(&src, NULL);
115    pipe_surface_reference(&dst, NULL);
116}
117
118struct pipe_transfer*
119r300_texture_get_transfer(struct pipe_context *ctx,
120			  struct pipe_resource *texture,
121			  struct pipe_subresource sr,
122			  unsigned usage,
123			  const struct pipe_box *box)
124{
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;
129
130    /* XXX Why aren't flushes taken care of by winsys automatically?
131     * Winsys seems to sometimes return a cached buffer instead of
132     * a mapped hardware buffer if this flush is commented out. */
133    if (ctx->is_resource_referenced(ctx, texture, sr.face, sr.level))
134        ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
135
136    trans = CALLOC_STRUCT(r300_transfer);
137    if (trans) {
138        /* Initialize the transfer object. */
139        pipe_resource_reference(&trans->transfer.resource, texture);
140        trans->transfer.sr = sr;
141        trans->transfer.usage = usage;
142        trans->transfer.box = *box;
143
144        /* If the texture is tiled, we must create a temporary detiled texture
145         * for this transfer. */
146        if (tex->microtile || tex->macrotile) {
147            trans->render_target_usage =
148                util_format_is_depth_or_stencil(texture->format) ?
149                PIPE_BIND_DEPTH_STENCIL :
150                PIPE_BIND_RENDER_TARGET;
151
152            base.target = PIPE_TEXTURE_2D;
153            base.format = texture->format;
154            base.width0 = box->width;
155            base.height0 = box->height;
156            base.depth0 = 0;
157            base.last_level = 0;
158            base.nr_samples = 0;
159            base.usage = PIPE_USAGE_DYNAMIC;
160            base.bind = 0;
161	    base.flags = R300_RESOURCE_FLAG_TRANSFER;
162
163            /* For texture reading, the temporary (detiled) texture is used as
164             * a render target when blitting from a tiled texture. */
165            if (usage & PIPE_TRANSFER_READ) {
166                base.bind |= trans->render_target_usage;
167            }
168            /* For texture writing, the temporary texture is used as a sampler
169             * when blitting into a tiled texture. */
170            if (usage & PIPE_TRANSFER_WRITE) {
171                base.bind |= PIPE_BIND_SAMPLER_VIEW;
172            }
173
174            /* Create the temporary texture. */
175            trans->detiled_texture = r300_texture(
176               ctx->screen->resource_create(ctx->screen,
177                                            &base));
178
179            assert(!trans->detiled_texture->microtile &&
180                   !trans->detiled_texture->macrotile);
181
182            /* Set the stride.
183	     *
184	     * Even though we are using an internal texture for this,
185	     * the transfer sr, box and usage parameters still reflect
186	     * the arguments received to get_transfer.  We just do the
187	     * right thing internally.
188	     */
189            trans->transfer.stride =
190                r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
191
192            if (usage & PIPE_TRANSFER_READ) {
193                /* We cannot map a tiled texture directly because the data is
194                 * in a different order, therefore we do detiling using a blit. */
195                r300_copy_from_tiled_texture(ctx, trans);
196            }
197        } else {
198            trans->transfer.stride =
199                r300_texture_get_stride(r300screen, tex, sr.level);
200            trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
201        }
202    }
203    return &trans->transfer;
204}
205
206void r300_texture_transfer_destroy(struct pipe_context *ctx,
207				   struct pipe_transfer *trans)
208{
209    struct r300_transfer *r300transfer = r300_transfer(trans);
210
211    if (r300transfer->detiled_texture) {
212        if (trans->usage & PIPE_TRANSFER_WRITE) {
213            r300_copy_into_tiled_texture(ctx, r300transfer);
214        }
215
216        pipe_resource_reference(
217            (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
218    }
219    pipe_resource_reference(&trans->resource, NULL);
220    FREE(trans);
221}
222
223void* r300_texture_transfer_map(struct pipe_context *ctx,
224				struct pipe_transfer *transfer)
225{
226    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
227    struct r300_transfer *r300transfer = r300_transfer(transfer);
228    struct r300_texture *tex = r300_texture(transfer->resource);
229    char *map;
230    enum pipe_format format = tex->b.b.format;
231
232    if (r300transfer->detiled_texture) {
233        /* The detiled texture is of the same size as the region being mapped
234         * (no offset needed). */
235        return rws->buffer_map(rws,
236                               r300transfer->detiled_texture->buffer,
237                               transfer->usage);
238    } else {
239        /* Tiling is disabled. */
240        map = rws->buffer_map(rws, tex->buffer,
241                              transfer->usage);
242
243        if (!map) {
244            return NULL;
245        }
246
247        return map + r300_transfer(transfer)->offset +
248            transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
249            transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
250    }
251}
252
253void r300_texture_transfer_unmap(struct pipe_context *ctx,
254				 struct pipe_transfer *transfer)
255{
256    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
257    struct r300_transfer *r300transfer = r300_transfer(transfer);
258    struct r300_texture *tex = r300_texture(transfer->resource);
259
260    if (r300transfer->detiled_texture) {
261	rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
262    } else {
263        rws->buffer_unmap(rws, tex->buffer);
264    }
265}
266
267