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