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