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