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