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*
r300_transfer(struct pipe_transfer * 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. */
r300_copy_from_tiled_texture(struct pipe_context * ctx,struct r300_transfer * r300transfer)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->b.b, 0,
58                               0, 0, 0,
59                               tex, transfer->level, &transfer->box);
60 }
61 
62 /* Copy a detiled texture to a tiled one. */
r300_copy_into_tiled_texture(struct pipe_context * ctx,struct r300_transfer * r300transfer)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->b.b, 0, &src_box);
74 
75     /* XXX remove this. */
76     r300_flush(ctx, 0, NULL);
77 }
78 
79 struct pipe_transfer*
r300_texture_get_transfer(struct pipe_context * ctx,struct pipe_resource * texture,unsigned level,unsigned usage,const struct pipe_box * box)80 r300_texture_get_transfer(struct pipe_context *ctx,
81                           struct pipe_resource *texture,
82                           unsigned level,
83                           unsigned usage,
84                           const struct pipe_box *box)
85 {
86     struct r300_context *r300 = r300_context(ctx);
87     struct r300_resource *tex = r300_resource(texture);
88     struct r300_transfer *trans;
89     struct pipe_resource base;
90     boolean referenced_cs, referenced_hw;
91 
92     referenced_cs =
93         r300->rws->cs_is_buffer_referenced(r300->cs, tex->cs_buf, RADEON_USAGE_READWRITE);
94     if (referenced_cs) {
95         referenced_hw = TRUE;
96     } else {
97         referenced_hw =
98             r300->rws->buffer_is_busy(tex->buf, RADEON_USAGE_READWRITE);
99     }
100 
101     trans = CALLOC_STRUCT(r300_transfer);
102     if (trans) {
103         /* Initialize the transfer object. */
104         pipe_resource_reference(&trans->transfer.resource, texture);
105         trans->transfer.level = level;
106         trans->transfer.usage = usage;
107         trans->transfer.box = *box;
108 
109         /* If the texture is tiled, we must create a temporary detiled texture
110          * for this transfer.
111          * Also make write transfers pipelined. */
112         if (tex->tex.microtile || tex->tex.macrotile[level] ||
113             (referenced_hw && !(usage & PIPE_TRANSFER_READ) &&
114              r300_is_blit_supported(texture->format))) {
115             if (r300->blitter->running) {
116                 fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
117                 os_break();
118             }
119 
120             base.target = PIPE_TEXTURE_2D;
121             base.format = texture->format;
122             base.width0 = box->width;
123             base.height0 = box->height;
124             base.depth0 = 1;
125             base.array_size = 1;
126             base.last_level = 0;
127             base.nr_samples = 0;
128             base.usage = PIPE_USAGE_STAGING;
129             base.bind = 0;
130             if (usage & PIPE_TRANSFER_READ) {
131                 base.bind |= PIPE_BIND_SAMPLER_VIEW;
132             }
133             if (usage & PIPE_TRANSFER_WRITE) {
134                 base.bind |= PIPE_BIND_RENDER_TARGET;
135             }
136             base.flags = R300_RESOURCE_FLAG_TRANSFER;
137 
138             /* For texture reading, the temporary (detiled) texture is used as
139              * a render target when blitting from a tiled texture. */
140             if (usage & PIPE_TRANSFER_READ) {
141                 base.bind |= PIPE_BIND_RENDER_TARGET;
142             }
143             /* For texture writing, the temporary texture is used as a sampler
144              * when blitting into a tiled texture. */
145             if (usage & PIPE_TRANSFER_WRITE) {
146                 base.bind |= PIPE_BIND_SAMPLER_VIEW;
147             }
148 
149             /* Create the temporary texture. */
150             trans->linear_texture = r300_resource(
151                ctx->screen->resource_create(ctx->screen,
152                                             &base));
153 
154             if (!trans->linear_texture) {
155                 /* Oh crap, the thing can't create the texture.
156                  * Let's flush and try again. */
157                 r300_flush(ctx, 0, NULL);
158 
159                 trans->linear_texture = r300_resource(
160                    ctx->screen->resource_create(ctx->screen,
161                                                 &base));
162 
163                 if (!trans->linear_texture) {
164                     /* For linear textures, it's safe to fallback to
165                      * an unpipelined transfer. */
166                     if (!tex->tex.microtile && !tex->tex.macrotile[level]) {
167                         goto unpipelined;
168                     }
169 
170                     /* Otherwise, go to hell. */
171                     fprintf(stderr,
172                         "r300: Failed to create a transfer object, praise.\n");
173                     FREE(trans);
174                     return NULL;
175                 }
176             }
177 
178             assert(!trans->linear_texture->tex.microtile &&
179                    !trans->linear_texture->tex.macrotile[0]);
180 
181             /* Set the stride. */
182             trans->transfer.stride =
183                     trans->linear_texture->tex.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                 r300_flush(ctx, 0, NULL);
192             }
193             return &trans->transfer;
194         }
195 
196     unpipelined:
197         /* Unpipelined transfer. */
198         trans->transfer.stride = tex->tex.stride_in_bytes[level];
199         trans->offset = r300_texture_get_offset(tex, level, box->z);
200 
201         if (referenced_cs &&
202             !(usage & PIPE_TRANSFER_UNSYNCHRONIZED))
203             r300_flush(ctx, 0, NULL);
204         return &trans->transfer;
205     }
206     return NULL;
207 }
208 
r300_texture_transfer_destroy(struct pipe_context * ctx,struct pipe_transfer * trans)209 void r300_texture_transfer_destroy(struct pipe_context *ctx,
210 				   struct pipe_transfer *trans)
211 {
212     struct r300_transfer *r300transfer = r300_transfer(trans);
213 
214     if (r300transfer->linear_texture) {
215         if (trans->usage & PIPE_TRANSFER_WRITE) {
216             r300_copy_into_tiled_texture(ctx, r300transfer);
217         }
218 
219         pipe_resource_reference(
220             (struct pipe_resource**)&r300transfer->linear_texture, NULL);
221     }
222     pipe_resource_reference(&trans->resource, NULL);
223     FREE(trans);
224 }
225 
r300_texture_transfer_map(struct pipe_context * ctx,struct pipe_transfer * transfer)226 void* r300_texture_transfer_map(struct pipe_context *ctx,
227 				struct pipe_transfer *transfer)
228 {
229     struct r300_context *r300 = r300_context(ctx);
230     struct r300_transfer *r300transfer = r300_transfer(transfer);
231     struct r300_resource *tex = r300_resource(transfer->resource);
232     char *map;
233     enum pipe_format format = tex->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 r300->rws->buffer_map(r300transfer->linear_texture->cs_buf,
239 				     r300->cs, transfer->usage);
240     } else {
241         /* Tiling is disabled. */
242         map = r300->rws->buffer_map(tex->cs_buf, r300->cs, transfer->usage);
243 
244         if (!map) {
245             return NULL;
246         }
247 
248         return map + r300_transfer(transfer)->offset +
249             transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
250             transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
251     }
252 }
253 
r300_texture_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)254 void r300_texture_transfer_unmap(struct pipe_context *ctx,
255 				 struct pipe_transfer *transfer)
256 {
257     struct radeon_winsys *rws = r300_context(ctx)->rws;
258     struct r300_transfer *r300transfer = r300_transfer(transfer);
259     struct r300_resource *tex = r300_resource(transfer->resource);
260 
261     if (r300transfer->linear_texture) {
262         rws->buffer_unmap(r300transfer->linear_texture->cs_buf);
263     } else {
264         rws->buffer_unmap(tex->cs_buf);
265     }
266 }
267