r300_transfer.c revision 6eb892cc12047af36f4eb42050f1d2e57b0f3a3c
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    /* Pipe context. */
39    struct pipe_context *ctx;
40
41    /* Parameters of get_tex_transfer. */
42    unsigned x, y, level, zslice, face;
43
44    /* Offset from start of buffer. */
45    unsigned offset;
46
47    /* Detiled texture. */
48    struct r300_texture *detiled_texture;
49
50    /* Transfer and format flags. */
51    unsigned buffer_usage, render_target_usage;
52};
53
54/* Convenience cast wrapper. */
55static INLINE struct r300_transfer*
56r300_transfer(struct pipe_transfer* transfer)
57{
58    return (struct r300_transfer*)transfer;
59}
60
61/* Copy from a tiled texture to a detiled one. */
62static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
63                                         struct r300_transfer *r300transfer)
64{
65    struct pipe_screen *screen = ctx->screen;
66    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
67    struct pipe_texture *tex = transfer->texture;
68    struct pipe_surface *src, *dst;
69
70    src = screen->get_tex_surface(screen, tex, r300transfer->face,
71                                  r300transfer->level, r300transfer->zslice,
72                                  PIPE_BUFFER_USAGE_GPU_READ |
73                                  PIPE_BUFFER_USAGE_PIXEL);
74
75    dst = screen->get_tex_surface(screen, &r300transfer->detiled_texture->tex,
76                                  0, 0, 0,
77                                  PIPE_BUFFER_USAGE_GPU_WRITE |
78                                  PIPE_BUFFER_USAGE_PIXEL |
79                                  r300transfer->buffer_usage);
80
81    ctx->surface_copy(ctx, dst, 0, 0, src, r300transfer->x, r300transfer->y,
82                      transfer->width, transfer->height);
83
84    pipe_surface_reference(&src, NULL);
85    pipe_surface_reference(&dst, NULL);
86}
87
88/* Copy a detiled texture to a tiled one. */
89static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
90                                         struct r300_transfer *r300transfer)
91{
92    struct pipe_screen *screen = ctx->screen;
93    struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
94    struct pipe_texture *tex = transfer->texture;
95    struct pipe_surface *src, *dst;
96
97    src = screen->get_tex_surface(screen, &r300transfer->detiled_texture->tex,
98                                  0, 0, 0,
99                                  PIPE_BUFFER_USAGE_GPU_READ |
100                                  PIPE_BUFFER_USAGE_PIXEL);
101
102    dst = screen->get_tex_surface(screen, tex, r300transfer->face,
103                                  r300transfer->level, r300transfer->zslice,
104                                  PIPE_BUFFER_USAGE_GPU_WRITE |
105                                  PIPE_BUFFER_USAGE_PIXEL);
106
107    /* XXX this flush prevents the following DRM error from occuring:
108     * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
109     * Reproducible with perf/copytex. */
110    ctx->flush(ctx, 0, NULL);
111
112    ctx->surface_copy(ctx, dst, r300transfer->x, r300transfer->y, src, 0, 0,
113                      transfer->width, transfer->height);
114
115    /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
116    ctx->flush(ctx, 0, NULL);
117
118    pipe_surface_reference(&src, NULL);
119    pipe_surface_reference(&dst, NULL);
120}
121
122static struct pipe_transfer*
123r300_get_tex_transfer(struct pipe_context *ctx,
124                      struct pipe_texture *texture,
125                      unsigned face, unsigned level, unsigned zslice,
126                      enum pipe_transfer_usage usage, unsigned x, unsigned y,
127                      unsigned w, unsigned h)
128{
129    struct r300_texture *tex = (struct r300_texture *)texture;
130    struct r300_screen *r300screen = r300_screen(ctx->screen);
131    struct r300_transfer *trans;
132    struct pipe_texture base;
133
134    trans = CALLOC_STRUCT(r300_transfer);
135    if (trans) {
136        /* Initialize the transfer object. */
137        pipe_texture_reference(&trans->transfer.texture, texture);
138        trans->transfer.usage = usage;
139        trans->transfer.width = w;
140        trans->transfer.height = h;
141        trans->ctx = ctx;
142        trans->x = x;
143        trans->y = y;
144        trans->level = level;
145        trans->zslice = zslice;
146        trans->face = face;
147
148        /* If the texture is tiled, we must create a temporary detiled texture
149         * for this transfer. */
150        if (tex->microtile || tex->macrotile) {
151            trans->buffer_usage = pipe_transfer_buffer_flags(&trans->transfer);
152            trans->render_target_usage =
153                util_format_is_depth_or_stencil(texture->format) ?
154                PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
155                PIPE_TEXTURE_USAGE_RENDER_TARGET;
156
157            base.target = PIPE_TEXTURE_2D;
158            base.format = texture->format;
159            base.width0 = w;
160            base.height0 = h;
161            base.depth0 = 0;
162            base.last_level = 0;
163            base.nr_samples = 0;
164            base.tex_usage = PIPE_TEXTURE_USAGE_DYNAMIC |
165                                 R300_TEXTURE_USAGE_TRANSFER;
166
167            /* For texture reading, the temporary (detiled) texture is used as
168             * a render target when blitting from a tiled texture. */
169            if (usage & PIPE_TRANSFER_READ) {
170                base.tex_usage |= trans->render_target_usage;
171            }
172            /* For texture writing, the temporary texture is used as a sampler
173             * when blitting into a tiled texture. */
174            if (usage & PIPE_TRANSFER_WRITE) {
175                base.tex_usage |= PIPE_TEXTURE_USAGE_SAMPLER;
176            }
177
178            /* Create the temporary texture. */
179            trans->detiled_texture = (struct r300_texture*)
180               ctx->screen->texture_create(ctx->screen,
181                                           &base);
182
183            assert(!trans->detiled_texture->microtile &&
184                   !trans->detiled_texture->macrotile);
185
186            /* Set the stride.
187             * Parameters x, y, level, zslice, and face remain zero. */
188            trans->transfer.stride =
189                r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
190
191            if (usage & PIPE_TRANSFER_READ) {
192                /* We cannot map a tiled texture directly because the data is
193                 * in a different order, therefore we do detiling using a blit. */
194                r300_copy_from_tiled_texture(ctx, trans);
195            }
196        } else {
197            trans->transfer.x = x;
198            trans->transfer.y = y;
199            trans->transfer.stride =
200                r300_texture_get_stride(r300screen, tex, level);
201            trans->transfer.level = level;
202            trans->transfer.zslice = zslice;
203            trans->transfer.face = face;
204            trans->offset = r300_texture_get_offset(tex, level, zslice, face);
205        }
206    }
207    return &trans->transfer;
208}
209
210static void r300_tex_transfer_destroy(struct pipe_context *ctx,
211                                      struct pipe_transfer *trans)
212{
213    struct r300_transfer *r300transfer = r300_transfer(trans);
214
215    if (r300transfer->detiled_texture) {
216        if (trans->usage & PIPE_TRANSFER_WRITE) {
217            r300_copy_into_tiled_texture(r300transfer->ctx, r300transfer);
218        }
219
220        pipe_texture_reference(
221            (struct pipe_texture**)&r300transfer->detiled_texture, NULL);
222    }
223    pipe_texture_reference(&trans->texture, NULL);
224    FREE(trans);
225}
226
227static void* r300_transfer_map(struct pipe_context *ctx,
228                               struct pipe_transfer *transfer)
229{
230    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
231    struct r300_transfer *r300transfer = r300_transfer(transfer);
232    struct r300_texture *tex = (struct r300_texture*)transfer->texture;
233    char *map;
234    enum pipe_format format = tex->tex.format;
235
236    if (r300transfer->detiled_texture) {
237        /* The detiled texture is of the same size as the region being mapped
238         * (no offset needed). */
239        return rws->buffer_map(rws,
240                               r300transfer->detiled_texture->buffer,
241                               pipe_transfer_buffer_flags(transfer));
242    } else {
243        /* Tiling is disabled. */
244        map = rws->buffer_map(rws, tex->buffer,
245                              pipe_transfer_buffer_flags(transfer));
246
247        if (!map) {
248            return NULL;
249        }
250
251        return map + r300_transfer(transfer)->offset +
252            transfer->y / util_format_get_blockheight(format) * transfer->stride +
253            transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
254    }
255}
256
257static void r300_transfer_unmap(struct pipe_context *ctx,
258                                struct pipe_transfer *transfer)
259{
260    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
261    struct r300_transfer *r300transfer = r300_transfer(transfer);
262    struct r300_texture *tex = (struct r300_texture*)transfer->texture;
263
264    if (r300transfer->detiled_texture) {
265	rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
266    } else {
267        rws->buffer_unmap(rws, tex->buffer);
268    }
269}
270
271
272void r300_init_transfer_functions( struct r300_context *r300ctx )
273{
274   struct pipe_context *ctx = &r300ctx->context;
275
276   ctx->get_tex_transfer = r300_get_tex_transfer;
277   ctx->tex_transfer_destroy = r300_tex_transfer_destroy;
278   ctx->transfer_map = r300_transfer_map;
279   ctx->transfer_unmap = r300_transfer_unmap;
280}
281