436b30445b273269c75c96d5dae4fc3d7a6e9502
[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_resource *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 *src = transfer->resource;
56 struct pipe_resource *dst = &r300transfer->linear_texture->b.b;
57
58 if (src->nr_samples <= 1) {
59 ctx->resource_copy_region(ctx, dst, 0, 0, 0, 0,
60 src, transfer->level, &transfer->box);
61 } else {
62 /* Resolve the resource. */
63 struct pipe_blit_info blit;
64
65 memset(&blit, 0, sizeof(blit));
66 blit.src.resource = src;
67 blit.src.format = src->format;
68 blit.src.level = transfer->level;
69 blit.src.box = transfer->box;
70 blit.dst.resource = dst;
71 blit.dst.format = dst->format;
72 blit.dst.box.width = transfer->box.width;
73 blit.dst.box.height = transfer->box.height;
74 blit.dst.box.depth = transfer->box.depth;
75 blit.mask = PIPE_MASK_RGBA;
76 blit.filter = PIPE_TEX_FILTER_NEAREST;
77
78 ctx->blit(ctx, &blit);
79 }
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_transfer *transfer = (struct pipe_transfer*)r300transfer;
87 struct pipe_resource *tex = transfer->resource;
88 struct pipe_box src_box;
89 u_box_origin_2d(transfer->box.width, transfer->box.height, &src_box);
90
91 ctx->resource_copy_region(ctx, tex, transfer->level,
92 transfer->box.x, transfer->box.y, transfer->box.z,
93 &r300transfer->linear_texture->b.b, 0, &src_box);
94
95 /* XXX remove this. */
96 r300_flush(ctx, 0, NULL);
97 }
98
99 void *
100 r300_texture_transfer_map(struct pipe_context *ctx,
101 struct pipe_resource *texture,
102 unsigned level,
103 unsigned usage,
104 const struct pipe_box *box,
105 struct pipe_transfer **transfer)
106 {
107 struct r300_context *r300 = r300_context(ctx);
108 struct r300_resource *tex = r300_resource(texture);
109 struct r300_transfer *trans;
110 struct pipe_resource base;
111 boolean referenced_cs, referenced_hw;
112 enum pipe_format format = tex->b.b.format;
113 char *map;
114
115 referenced_cs =
116 r300->rws->cs_is_buffer_referenced(r300->cs, tex->cs_buf, RADEON_USAGE_READWRITE);
117 if (referenced_cs) {
118 referenced_hw = TRUE;
119 } else {
120 referenced_hw =
121 r300->rws->buffer_is_busy(tex->buf, RADEON_USAGE_READWRITE);
122 }
123
124 trans = CALLOC_STRUCT(r300_transfer);
125 if (trans) {
126 /* Initialize the transfer object. */
127 trans->transfer.resource = texture;
128 trans->transfer.level = level;
129 trans->transfer.usage = usage;
130 trans->transfer.box = *box;
131
132 /* If the texture is tiled, we must create a temporary detiled texture
133 * for this transfer.
134 * Also make write transfers pipelined. */
135 if (tex->tex.microtile || tex->tex.macrotile[level] ||
136 (referenced_hw && !(usage & PIPE_TRANSFER_READ) &&
137 r300_is_blit_supported(texture->format))) {
138 if (r300->blitter->running) {
139 fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
140 os_break();
141 }
142
143 memset(&base, 0, sizeof(base));
144 base.target = PIPE_TEXTURE_2D;
145 base.format = texture->format;
146 base.width0 = box->width;
147 base.height0 = box->height;
148 base.depth0 = 1;
149 base.array_size = 1;
150 base.usage = PIPE_USAGE_STAGING;
151 base.flags = R300_RESOURCE_FLAG_TRANSFER;
152
153 /* Create the temporary texture. */
154 trans->linear_texture = r300_resource(
155 ctx->screen->resource_create(ctx->screen,
156 &base));
157
158 if (!trans->linear_texture) {
159 /* Oh crap, the thing can't create the texture.
160 * Let's flush and try again. */
161 r300_flush(ctx, 0, NULL);
162
163 trans->linear_texture = r300_resource(
164 ctx->screen->resource_create(ctx->screen,
165 &base));
166
167 if (!trans->linear_texture) {
168 fprintf(stderr,
169 "r300: Failed to create a transfer object.\n");
170 FREE(trans);
171 return NULL;
172 }
173 }
174
175 assert(!trans->linear_texture->tex.microtile &&
176 !trans->linear_texture->tex.macrotile[0]);
177
178 /* Set the stride. */
179 trans->transfer.stride =
180 trans->linear_texture->tex.stride_in_bytes[0];
181
182 if (usage & PIPE_TRANSFER_READ) {
183 /* We cannot map a tiled texture directly because the data is
184 * in a different order, therefore we do detiling using a blit. */
185 r300_copy_from_tiled_texture(ctx, trans);
186
187 /* Always referenced in the blit. */
188 r300_flush(ctx, 0, NULL);
189 }
190 } else {
191 /* Unpipelined transfer. */
192 trans->transfer.stride = tex->tex.stride_in_bytes[level];
193 trans->offset = r300_texture_get_offset(tex, level, box->z);
194
195 if (referenced_cs &&
196 !(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
197 r300_flush(ctx, 0, NULL);
198 }
199 }
200 }
201
202 if (trans->linear_texture) {
203 /* The detiled texture is of the same size as the region being mapped
204 * (no offset needed). */
205 map = r300->rws->buffer_map(trans->linear_texture->cs_buf,
206 r300->cs, usage);
207 if (!map) {
208 pipe_resource_reference(
209 (struct pipe_resource**)&trans->linear_texture, NULL);
210 FREE(trans);
211 return NULL;
212 }
213 *transfer = &trans->transfer;
214 return map;
215 } else {
216 /* Tiling is disabled. */
217 map = r300->rws->buffer_map(tex->cs_buf, r300->cs, usage);
218 if (!map) {
219 FREE(trans);
220 return NULL;
221 }
222
223 *transfer = &trans->transfer;
224 return map + trans->offset +
225 box->y / util_format_get_blockheight(format) * trans->transfer.stride +
226 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
227 }
228 }
229
230 void r300_texture_transfer_unmap(struct pipe_context *ctx,
231 struct pipe_transfer *transfer)
232 {
233 struct radeon_winsys *rws = r300_context(ctx)->rws;
234 struct r300_transfer *trans = r300_transfer(transfer);
235 struct r300_resource *tex = r300_resource(transfer->resource);
236
237 if (trans->linear_texture) {
238 rws->buffer_unmap(trans->linear_texture->cs_buf);
239
240 if (transfer->usage & PIPE_TRANSFER_WRITE) {
241 r300_copy_into_tiled_texture(ctx, trans);
242 }
243
244 pipe_resource_reference(
245 (struct pipe_resource**)&trans->linear_texture, NULL);
246 } else {
247 rws->buffer_unmap(tex->cs_buf);
248 }
249 FREE(transfer);
250 }