r300g: better describe another flush and move it closer to the real problem
[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
45 /* Convenience cast wrapper. */
46 static INLINE struct r300_transfer*
47 r300_transfer(struct pipe_transfer* transfer)
48 {
49 return (struct r300_transfer*)transfer;
50 }
51
52 /* Copy from a tiled texture to a detiled one. */
53 static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
54 struct r300_transfer *r300transfer)
55 {
56 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
57 struct pipe_resource *tex = transfer->resource;
58 struct pipe_subresource subdst;
59
60 subdst.face = 0;
61 subdst.level = 0;
62
63 /* XXX if we don't flush before copying the texture and mapping it,
64 * we get wrong pixels, i.e. it's like latest draw calls didn't happen,
65 * including this blit. Tests: e.g. piglit/provoking-vertex
66 *
67 * Since the flush immediately before mapping is implicit (the buffer is
68 * always referenced in resource_copy_region), every read transfer costs
69 * 2 flushes. That sucks. */
70 ctx->flush(ctx, 0, NULL);
71
72 ctx->resource_copy_region(ctx, &r300transfer->detiled_texture->b.b, subdst,
73 0, 0, 0,
74 tex, transfer->sr,
75 transfer->box.x, transfer->box.y, transfer->box.z,
76 transfer->box.width, transfer->box.height);
77
78 /* Flushing after the copy is implicit, issued by winsys. */
79 }
80
81 /* Copy a detiled texture to a tiled one. */
82 static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
83 struct r300_transfer *r300transfer)
84 {
85 struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
86 struct pipe_resource *tex = transfer->resource;
87 struct pipe_subresource subsrc;
88
89 subsrc.face = 0;
90 subsrc.level = 0;
91
92 ctx->resource_copy_region(ctx, tex, transfer->sr,
93 transfer->box.x, transfer->box.y, transfer->box.z,
94 &r300transfer->detiled_texture->b.b, subsrc,
95 0, 0, 0,
96 transfer->box.width, transfer->box.height);
97
98 /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
99 ctx->flush(ctx, 0, NULL);
100 }
101
102 struct pipe_transfer*
103 r300_texture_get_transfer(struct pipe_context *ctx,
104 struct pipe_resource *texture,
105 struct pipe_subresource sr,
106 unsigned usage,
107 const struct pipe_box *box)
108 {
109 struct r300_texture *tex = r300_texture(texture);
110 struct r300_screen *r300screen = r300_screen(ctx->screen);
111 struct r300_transfer *trans;
112 struct pipe_resource base;
113
114 trans = CALLOC_STRUCT(r300_transfer);
115 if (trans) {
116 /* Initialize the transfer object. */
117 pipe_resource_reference(&trans->transfer.resource, texture);
118 trans->transfer.sr = sr;
119 trans->transfer.usage = usage;
120 trans->transfer.box = *box;
121
122 /* If the texture is tiled, we must create a temporary detiled texture
123 * for this transfer. */
124 if (tex->microtile || tex->macrotile) {
125 base.target = PIPE_TEXTURE_2D;
126 base.format = texture->format;
127 base.width0 = box->width;
128 base.height0 = box->height;
129 base.depth0 = 0;
130 base.last_level = 0;
131 base.nr_samples = 0;
132 base.usage = PIPE_USAGE_DYNAMIC;
133 base.bind = 0;
134 base.flags = R300_RESOURCE_FLAG_TRANSFER;
135
136 /* For texture reading, the temporary (detiled) texture is used as
137 * a render target when blitting from a tiled texture. */
138 if (usage & PIPE_TRANSFER_READ) {
139 base.bind |= PIPE_BIND_RENDER_TARGET;
140 }
141 /* For texture writing, the temporary texture is used as a sampler
142 * when blitting into a tiled texture. */
143 if (usage & PIPE_TRANSFER_WRITE) {
144 base.bind |= PIPE_BIND_SAMPLER_VIEW;
145 }
146
147 /* Create the temporary texture. */
148 trans->detiled_texture = r300_texture(
149 ctx->screen->resource_create(ctx->screen,
150 &base));
151
152 assert(!trans->detiled_texture->microtile &&
153 !trans->detiled_texture->macrotile);
154
155 /* Set the stride.
156 *
157 * Even though we are using an internal texture for this,
158 * the transfer sr, box and usage parameters still reflect
159 * the arguments received to get_transfer. We just do the
160 * right thing internally.
161 */
162 trans->transfer.stride =
163 r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
164
165 if (usage & PIPE_TRANSFER_READ) {
166 /* We cannot map a tiled texture directly because the data is
167 * in a different order, therefore we do detiling using a blit. */
168 r300_copy_from_tiled_texture(ctx, trans);
169 }
170 } else {
171 trans->transfer.stride =
172 r300_texture_get_stride(r300screen, tex, sr.level);
173 trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
174 }
175 }
176 return &trans->transfer;
177 }
178
179 void r300_texture_transfer_destroy(struct pipe_context *ctx,
180 struct pipe_transfer *trans)
181 {
182 struct r300_transfer *r300transfer = r300_transfer(trans);
183
184 if (r300transfer->detiled_texture) {
185 if (trans->usage & PIPE_TRANSFER_WRITE) {
186 r300_copy_into_tiled_texture(ctx, r300transfer);
187 }
188
189 pipe_resource_reference(
190 (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
191 }
192 pipe_resource_reference(&trans->resource, NULL);
193 FREE(trans);
194 }
195
196 void* r300_texture_transfer_map(struct pipe_context *ctx,
197 struct pipe_transfer *transfer)
198 {
199 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
200 struct r300_transfer *r300transfer = r300_transfer(transfer);
201 struct r300_texture *tex = r300_texture(transfer->resource);
202 char *map;
203 enum pipe_format format = tex->b.b.format;
204
205 if (r300transfer->detiled_texture) {
206 /* The detiled texture is of the same size as the region being mapped
207 * (no offset needed). */
208 return rws->buffer_map(rws,
209 r300transfer->detiled_texture->buffer,
210 transfer->usage);
211 } else {
212 /* Tiling is disabled. */
213 map = rws->buffer_map(rws, tex->buffer,
214 transfer->usage);
215
216 if (!map) {
217 return NULL;
218 }
219
220 return map + r300_transfer(transfer)->offset +
221 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
222 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
223 }
224 }
225
226 void r300_texture_transfer_unmap(struct pipe_context *ctx,
227 struct pipe_transfer *transfer)
228 {
229 struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
230 struct r300_transfer *r300transfer = r300_transfer(transfer);
231 struct r300_texture *tex = r300_texture(transfer->resource);
232
233 if (r300transfer->detiled_texture) {
234 rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
235 } else {
236 rws->buffer_unmap(rws, tex->buffer);
237 }
238 }