Merge commit 'origin/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 "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 /* Pipe context. */
39 struct pipe_context *ctx;
40
41 /* Offset from start of buffer. */
42 unsigned offset;
43
44 /* Detiled texture. */
45 struct r300_texture *detiled_texture;
46
47 /* Transfer and format flags. */
48 unsigned render_target_usage;
49 };
50
51 /* Convenience cast wrapper. */
52 static INLINE struct r300_transfer*
53 r300_transfer(struct pipe_transfer* transfer)
54 {
55 return (struct r300_transfer*)transfer;
56 }
57
58 /* Copy from a tiled texture to a detiled one. */
59 static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
60 struct r300_transfer *r300transfer)
61 {
62 struct pipe_screen *screen = ctx->screen;
63 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
64 struct pipe_resource *tex = transfer->resource;
65 struct pipe_surface *src, *dst;
66
67 src = screen->get_tex_surface(screen, tex,
68 transfer->sr.face,
69 transfer->sr.level,
70 transfer->box.z,
71 PIPE_BIND_BLIT_SOURCE);
72
73 dst = screen->get_tex_surface(screen, &r300transfer->detiled_texture->b.b,
74 0, 0, 0,
75 PIPE_BIND_BLIT_DESTINATION);
76
77 ctx->surface_copy(ctx, dst, 0, 0, src,
78 transfer->box.x, transfer->box.y,
79 transfer->box.width, transfer->box.height);
80
81 pipe_surface_reference(&src, NULL);
82 pipe_surface_reference(&dst, NULL);
83 }
84
85 /* Copy a detiled texture to a tiled one. */
86 static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
87 struct r300_transfer *r300transfer)
88 {
89 struct pipe_screen *screen = ctx->screen;
90 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
91 struct pipe_resource *tex = transfer->resource;
92 struct pipe_surface *src, *dst;
93
94 src = screen->get_tex_surface(screen, &r300transfer->detiled_texture->b.b,
95 0, 0, 0,
96 PIPE_BIND_BLIT_SOURCE);
97
98 dst = screen->get_tex_surface(screen, tex,
99 transfer->sr.face,
100 transfer->sr.level,
101 transfer->box.z,
102 PIPE_BIND_BLIT_DESTINATION);
103
104 /* XXX this flush prevents the following DRM error from occuring:
105 * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
106 * Reproducible with perf/copytex. */
107 ctx->flush(ctx, 0, NULL);
108
109 ctx->surface_copy(ctx, dst,
110 transfer->box.x, transfer->box.y,
111 src, 0, 0,
112 transfer->box.width, transfer->box.height);
113
114 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
115 ctx->flush(ctx, 0, NULL);
116
117 pipe_surface_reference(&src, NULL);
118 pipe_surface_reference(&dst, NULL);
119 }
120
121 struct pipe_transfer*
122 r300_texture_get_transfer(struct pipe_context *ctx,
123 struct pipe_resource *texture,
124 struct pipe_subresource sr,
125 unsigned usage,
126 const struct pipe_box *box)
127 {
128 struct r300_texture *tex = r300_texture(texture);
129 struct r300_screen *r300screen = r300_screen(ctx->screen);
130 struct r300_transfer *trans;
131 struct pipe_resource base;
132
133 trans = CALLOC_STRUCT(r300_transfer);
134 if (trans) {
135 /* Initialize the transfer object. */
136 pipe_resource_reference(&trans->transfer.resource, texture);
137 trans->transfer.sr = sr;
138 trans->transfer.usage = usage;
139 trans->transfer.box = *box;
140 trans->ctx = ctx;
141
142 /* If the texture is tiled, we must create a temporary detiled texture
143 * for this transfer. */
144 if (tex->microtile || tex->macrotile) {
145 trans->render_target_usage =
146 util_format_is_depth_or_stencil(texture->format) ?
147 PIPE_BIND_DEPTH_STENCIL :
148 PIPE_BIND_RENDER_TARGET;
149
150 base.target = PIPE_TEXTURE_2D;
151 base.format = texture->format;
152 base.width0 = box->width;
153 base.height0 = box->height;
154 base.depth0 = 0;
155 base.last_level = 0;
156 base.nr_samples = 0;
157 base._usage = PIPE_USAGE_DYNAMIC;
158 base.flags = R300_RESOURCE_FLAG_TRANSFER;
159
160 /* For texture reading, the temporary (detiled) texture is used as
161 * a render target when blitting from a tiled texture. */
162 if (usage & PIPE_TRANSFER_READ) {
163 base.bind |= trans->render_target_usage;
164 }
165 /* For texture writing, the temporary texture is used as a sampler
166 * when blitting into a tiled texture. */
167 if (usage & PIPE_TRANSFER_WRITE) {
168 base.bind |= PIPE_BIND_SAMPLER_VIEW;
169 }
170
171 /* Create the temporary texture. */
172 trans->detiled_texture = r300_texture(
173 ctx->screen->resource_create(ctx->screen,
174 &base));
175
176 assert(!trans->detiled_texture->microtile &&
177 !trans->detiled_texture->macrotile);
178
179 /* Set the stride.
180 *
181 * Even though we are using an internal texture for this,
182 * the transfer sr, box and usage parameters still reflect
183 * the arguments received to get_transfer. We just do the
184 * right thing internally.
185 */
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.stride =
196 r300_texture_get_stride(r300screen, tex, sr.level);
197 trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
198 }
199 }
200 return &trans->transfer;
201 }
202
203 void r300_texture_transfer_destroy(struct pipe_context *ctx,
204 struct pipe_transfer *trans)
205 {
206 struct r300_transfer *r300transfer = r300_transfer(trans);
207
208 if (r300transfer->detiled_texture) {
209 if (trans->usage & PIPE_TRANSFER_WRITE) {
210 r300_copy_into_tiled_texture(r300transfer->ctx, r300transfer);
211 }
212
213 pipe_resource_reference(
214 (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
215 }
216 pipe_resource_reference(&trans->resource, NULL);
217 FREE(trans);
218 }
219
220 void* r300_texture_transfer_map(struct pipe_context *ctx,
221 struct pipe_transfer *transfer)
222 {
223 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
224 struct r300_transfer *r300transfer = r300_transfer(transfer);
225 struct r300_texture *tex = r300_texture(transfer->resource);
226 char *map;
227 enum pipe_format format = tex->b.b.format;
228
229 if (r300transfer->detiled_texture) {
230 /* The detiled texture is of the same size as the region being mapped
231 * (no offset needed). */
232 return rws->buffer_map(rws,
233 r300transfer->detiled_texture->buffer,
234 transfer->usage);
235 } else {
236 /* Tiling is disabled. */
237 map = rws->buffer_map(rws, tex->buffer,
238 transfer->usage);
239
240 if (!map) {
241 return NULL;
242 }
243
244 return map + r300_transfer(transfer)->offset +
245 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
246 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
247 }
248 }
249
250 void r300_texture_transfer_unmap(struct pipe_context *ctx,
251 struct pipe_transfer *transfer)
252 {
253 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
254 struct r300_transfer *r300transfer = r300_transfer(transfer);
255 struct r300_texture *tex = r300_texture(transfer->resource);
256
257 if (r300transfer->detiled_texture) {
258 rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
259 } else {
260 rws->buffer_unmap(rws, tex->buffer);
261 }
262 }
263