r300g: inline FLUSH_CS
[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_transfer.h"
25 #include "r300_texture.h"
26 #include "r300_screen_buffer.h"
27
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30
31 struct r300_transfer {
32 /* Parent class */
33 struct pipe_transfer transfer;
34
35 /* Offset from start of buffer. */
36 unsigned offset;
37
38 /* Detiled texture. */
39 struct r300_texture *detiled_texture;
40 };
41
42 /* Convenience cast wrapper. */
43 static INLINE struct r300_transfer*
44 r300_transfer(struct pipe_transfer* transfer)
45 {
46 return (struct r300_transfer*)transfer;
47 }
48
49 /* Copy from a tiled texture to a detiled one. */
50 static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
51 struct r300_transfer *r300transfer)
52 {
53 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
54 struct pipe_resource *tex = transfer->resource;
55 struct pipe_subresource subdst;
56
57 subdst.face = 0;
58 subdst.level = 0;
59
60 /* XXX if we don't flush before copying the texture and mapping it,
61 * we get wrong pixels, i.e. it's like latest draw calls didn't happen,
62 * including this blit. Tests: e.g. piglit/provoking-vertex
63 *
64 * Since the flush immediately before mapping is implicit (the buffer is
65 * always referenced in resource_copy_region), every read transfer costs
66 * 2 flushes. That sucks. */
67 ctx->flush(ctx, 0, NULL);
68
69 ctx->resource_copy_region(ctx, &r300transfer->detiled_texture->b.b, subdst,
70 0, 0, 0,
71 tex, transfer->sr,
72 transfer->box.x, transfer->box.y, transfer->box.z,
73 transfer->box.width, transfer->box.height);
74
75 /* Flushing after the copy is implicit, issued by winsys. */
76 }
77
78 /* Copy a detiled texture to a tiled one. */
79 static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
80 struct r300_transfer *r300transfer)
81 {
82 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
83 struct pipe_resource *tex = transfer->resource;
84 struct pipe_subresource subsrc;
85
86 subsrc.face = 0;
87 subsrc.level = 0;
88
89 ctx->resource_copy_region(ctx, tex, transfer->sr,
90 transfer->box.x, transfer->box.y, transfer->box.z,
91 &r300transfer->detiled_texture->b.b, subsrc,
92 0, 0, 0,
93 transfer->box.width, transfer->box.height);
94
95 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
96 ctx->flush(ctx, 0, NULL);
97 }
98
99 struct pipe_transfer*
100 r300_texture_get_transfer(struct pipe_context *ctx,
101 struct pipe_resource *texture,
102 struct pipe_subresource sr,
103 unsigned usage,
104 const struct pipe_box *box)
105 {
106 struct r300_texture *tex = r300_texture(texture);
107 struct r300_screen *r300screen = r300_screen(ctx->screen);
108 struct r300_transfer *trans;
109 struct pipe_resource base;
110 boolean referenced_cs, referenced_hw, blittable;
111
112 referenced_cs = r300screen->rws->is_buffer_referenced(
113 r300screen->rws, tex->buffer, R300_REF_CS);
114 if (referenced_cs) {
115 referenced_hw = TRUE;
116 } else {
117 referenced_hw = r300screen->rws->is_buffer_referenced(
118 r300screen->rws, tex->buffer, R300_REF_HW);
119 }
120
121 blittable = ctx->screen->is_format_supported(
122 ctx->screen, texture->format, texture->target, 0,
123 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET, 0);
124
125 trans = CALLOC_STRUCT(r300_transfer);
126 if (trans) {
127 /* Initialize the transfer object. */
128 pipe_resource_reference(&trans->transfer.resource, texture);
129 trans->transfer.sr = sr;
130 trans->transfer.usage = usage;
131 trans->transfer.box = *box;
132
133 /* If the texture is tiled, we must create a temporary detiled texture
134 * for this transfer.
135 * Also make write transfers pipelined. */
136 if (tex->microtile || tex->macrotile ||
137 ((referenced_hw & !(usage & PIPE_TRANSFER_READ)) && blittable)) {
138 base.target = PIPE_TEXTURE_2D;
139 base.format = texture->format;
140 base.width0 = box->width;
141 base.height0 = box->height;
142 base.depth0 = 0;
143 base.last_level = 0;
144 base.nr_samples = 0;
145 base.usage = PIPE_USAGE_DYNAMIC;
146 base.bind = 0;
147 base.flags = R300_RESOURCE_FLAG_TRANSFER;
148
149 /* For texture reading, the temporary (detiled) texture is used as
150 * a render target when blitting from a tiled texture. */
151 if (usage & PIPE_TRANSFER_READ) {
152 base.bind |= PIPE_BIND_RENDER_TARGET;
153 }
154 /* For texture writing, the temporary texture is used as a sampler
155 * when blitting into a tiled texture. */
156 if (usage & PIPE_TRANSFER_WRITE) {
157 base.bind |= PIPE_BIND_SAMPLER_VIEW;
158 }
159
160 /* Create the temporary texture. */
161 trans->detiled_texture = r300_texture(
162 ctx->screen->resource_create(ctx->screen,
163 &base));
164
165 if (!trans->detiled_texture) {
166 /* Oh crap, the thing can't create the texture.
167 * Let's flush and try again. */
168 ctx->flush(ctx, 0, NULL);
169
170 trans->detiled_texture = r300_texture(
171 ctx->screen->resource_create(ctx->screen,
172 &base));
173
174 if (!trans->detiled_texture) {
175 /* For linear textures, it's safe to fallback to
176 * an unpipelined transfer. */
177 if (!tex->microtile && !tex->macrotile) {
178 goto unpipelined;
179 }
180
181 /* Otherwise, go to hell. */
182 fprintf(stderr,
183 "r300: Failed to create a transfer object, praise.\n");
184 FREE(trans);
185 return NULL;
186 }
187 }
188
189 assert(!trans->detiled_texture->microtile &&
190 !trans->detiled_texture->macrotile);
191
192 /* Set the stride.
193 *
194 * Even though we are using an internal texture for this,
195 * the transfer sr, box and usage parameters still reflect
196 * the arguments received to get_transfer. We just do the
197 * right thing internally.
198 */
199 trans->transfer.stride =
200 r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
201
202 if (usage & PIPE_TRANSFER_READ) {
203 /* We cannot map a tiled texture directly because the data is
204 * in a different order, therefore we do detiling using a blit. */
205 r300_copy_from_tiled_texture(ctx, trans);
206
207 /* Always referenced in the blit. */
208 ctx->flush(ctx, 0, NULL);
209 }
210 return &trans->transfer;
211 }
212
213 unpipelined:
214 /* Unpipelined transfer. */
215 trans->transfer.stride =
216 r300_texture_get_stride(r300screen, tex, sr.level);
217 trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
218
219 if (referenced_cs && (usage & PIPE_TRANSFER_READ))
220 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
221 return &trans->transfer;
222 }
223 return NULL;
224 }
225
226 void r300_texture_transfer_destroy(struct pipe_context *ctx,
227 struct pipe_transfer *trans)
228 {
229 struct r300_transfer *r300transfer = r300_transfer(trans);
230
231 if (r300transfer->detiled_texture) {
232 if (trans->usage & PIPE_TRANSFER_WRITE) {
233 r300_copy_into_tiled_texture(ctx, r300transfer);
234 }
235
236 pipe_resource_reference(
237 (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
238 }
239 pipe_resource_reference(&trans->resource, NULL);
240 FREE(trans);
241 }
242
243 void* r300_texture_transfer_map(struct pipe_context *ctx,
244 struct pipe_transfer *transfer)
245 {
246 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
247 struct r300_transfer *r300transfer = r300_transfer(transfer);
248 struct r300_texture *tex = r300_texture(transfer->resource);
249 char *map;
250 enum pipe_format format = tex->b.b.format;
251
252 if (r300transfer->detiled_texture) {
253 /* The detiled texture is of the same size as the region being mapped
254 * (no offset needed). */
255 return rws->buffer_map(rws,
256 r300transfer->detiled_texture->buffer,
257 transfer->usage);
258 } else {
259 /* Tiling is disabled. */
260 map = rws->buffer_map(rws, tex->buffer,
261 transfer->usage);
262
263 if (!map) {
264 return NULL;
265 }
266
267 return map + r300_transfer(transfer)->offset +
268 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
269 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
270 }
271 }
272
273 void r300_texture_transfer_unmap(struct pipe_context *ctx,
274 struct pipe_transfer *transfer)
275 {
276 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
277 struct r300_transfer *r300transfer = r300_transfer(transfer);
278 struct r300_texture *tex = r300_texture(transfer->resource);
279
280 if (r300transfer->detiled_texture) {
281 rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
282 } else {
283 rws->buffer_unmap(rws, tex->buffer);
284 }
285 }