Merge remote branch '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 /* 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_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. */
83 static 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
118 struct pipe_transfer*
119 r300_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 trans = CALLOC_STRUCT(r300_transfer);
131 if (trans) {
132 /* Initialize the transfer object. */
133 pipe_resource_reference(&trans->transfer.resource, texture);
134 trans->transfer.sr = sr;
135 trans->transfer.usage = usage;
136 trans->transfer.box = *box;
137
138 /* If the texture is tiled, we must create a temporary detiled texture
139 * for this transfer. */
140 if (tex->microtile || tex->macrotile) {
141 trans->render_target_usage =
142 util_format_is_depth_or_stencil(texture->format) ?
143 PIPE_BIND_DEPTH_STENCIL :
144 PIPE_BIND_RENDER_TARGET;
145
146 base.target = PIPE_TEXTURE_2D;
147 base.format = texture->format;
148 base.width0 = box->width;
149 base.height0 = box->height;
150 base.depth0 = 0;
151 base.last_level = 0;
152 base.nr_samples = 0;
153 base.usage = PIPE_USAGE_DYNAMIC;
154 base.bind = 0;
155 base.flags = R300_RESOURCE_FLAG_TRANSFER;
156
157 /* For texture reading, the temporary (detiled) texture is used as
158 * a render target when blitting from a tiled texture. */
159 if (usage & PIPE_TRANSFER_READ) {
160 base.bind |= trans->render_target_usage;
161 }
162 /* For texture writing, the temporary texture is used as a sampler
163 * when blitting into a tiled texture. */
164 if (usage & PIPE_TRANSFER_WRITE) {
165 base.bind |= PIPE_BIND_SAMPLER_VIEW;
166 }
167
168 /* Create the temporary texture. */
169 trans->detiled_texture = r300_texture(
170 ctx->screen->resource_create(ctx->screen,
171 &base));
172
173 assert(!trans->detiled_texture->microtile &&
174 !trans->detiled_texture->macrotile);
175
176 /* Set the stride.
177 *
178 * Even though we are using an internal texture for this,
179 * the transfer sr, box and usage parameters still reflect
180 * the arguments received to get_transfer. We just do the
181 * right thing internally.
182 */
183 trans->transfer.stride =
184 r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
185
186 if (usage & PIPE_TRANSFER_READ) {
187 /* We cannot map a tiled texture directly because the data is
188 * in a different order, therefore we do detiling using a blit. */
189 r300_copy_from_tiled_texture(ctx, trans);
190 }
191 } else {
192 trans->transfer.stride =
193 r300_texture_get_stride(r300screen, tex, sr.level);
194 trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
195 }
196 }
197 return &trans->transfer;
198 }
199
200 void r300_texture_transfer_destroy(struct pipe_context *ctx,
201 struct pipe_transfer *trans)
202 {
203 struct r300_transfer *r300transfer = r300_transfer(trans);
204
205 if (r300transfer->detiled_texture) {
206 if (trans->usage & PIPE_TRANSFER_WRITE) {
207 r300_copy_into_tiled_texture(ctx, r300transfer);
208 }
209
210 pipe_resource_reference(
211 (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
212 }
213 pipe_resource_reference(&trans->resource, NULL);
214 FREE(trans);
215 }
216
217 void* r300_texture_transfer_map(struct pipe_context *ctx,
218 struct pipe_transfer *transfer)
219 {
220 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
221 struct r300_transfer *r300transfer = r300_transfer(transfer);
222 struct r300_texture *tex = r300_texture(transfer->resource);
223 char *map;
224 enum pipe_format format = tex->b.b.format;
225
226 if (r300transfer->detiled_texture) {
227 /* The detiled texture is of the same size as the region being mapped
228 * (no offset needed). */
229 return rws->buffer_map(rws,
230 r300transfer->detiled_texture->buffer,
231 transfer->usage);
232 } else {
233 /* Tiling is disabled. */
234 map = rws->buffer_map(rws, tex->buffer,
235 transfer->usage);
236
237 if (!map) {
238 return NULL;
239 }
240
241 return map + r300_transfer(transfer)->offset +
242 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
243 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
244 }
245 }
246
247 void r300_texture_transfer_unmap(struct pipe_context *ctx,
248 struct pipe_transfer *transfer)
249 {
250 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
251 struct r300_transfer *r300transfer = r300_transfer(transfer);
252 struct r300_texture *tex = r300_texture(transfer->resource);
253
254 if (r300transfer->detiled_texture) {
255 rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
256 } else {
257 rws->buffer_unmap(rws, tex->buffer);
258 }
259 }
260