r300_transfer.c revision c9e9cfbf60001c1c35de33b31716c9ffe78fde53
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 32struct 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. */ 44static INLINE struct r300_transfer* 45r300_transfer(struct pipe_transfer* transfer) 46{ 47 return (struct r300_transfer*)transfer; 48} 49 50/* Copy from a tiled texture to a detiled one. */ 51static 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.b, 0, 58 0, 0, 0, 59 tex, transfer->level, &transfer->box); 60} 61 62/* Copy a detiled texture to a tiled one. */ 63static 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.b, 0, &src_box); 74 75 /* XXX remove this. */ 76 r300_flush(ctx, 0, NULL); 77} 78 79struct pipe_transfer* 80r300_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 if (usage & (PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_MAP_PERMANENTLY)) { 93 return NULL; 94 } 95 96 referenced_cs = 97 r300->rws->cs_is_buffer_referenced(r300->cs, tex->cs_buf); 98 if (referenced_cs) { 99 referenced_hw = TRUE; 100 } else { 101 referenced_hw = 102 r300->rws->buffer_is_busy(tex->buf, RADEON_USAGE_READWRITE); 103 } 104 105 trans = CALLOC_STRUCT(r300_transfer); 106 if (trans) { 107 /* Initialize the transfer object. */ 108 pipe_resource_reference(&trans->transfer.resource, texture); 109 trans->transfer.level = level; 110 trans->transfer.usage = usage; 111 trans->transfer.box = *box; 112 113 /* If the texture is tiled, we must create a temporary detiled texture 114 * for this transfer. 115 * Also make write transfers pipelined. */ 116 if (tex->tex.microtile || tex->tex.macrotile[level] || 117 (referenced_hw && !(usage & PIPE_TRANSFER_READ) && 118 r300_is_blit_supported(texture->format))) { 119 if (r300->blitter->running) { 120 fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n"); 121 os_break(); 122 } 123 124 base.target = PIPE_TEXTURE_2D; 125 base.format = texture->format; 126 base.width0 = box->width; 127 base.height0 = box->height; 128 base.depth0 = 1; 129 base.array_size = 1; 130 base.last_level = 0; 131 base.nr_samples = 0; 132 base.usage = PIPE_USAGE_STAGING; 133 base.bind = 0; 134 if (usage & PIPE_TRANSFER_READ) { 135 base.bind |= PIPE_BIND_SAMPLER_VIEW; 136 } 137 if (usage & PIPE_TRANSFER_WRITE) { 138 base.bind |= PIPE_BIND_RENDER_TARGET; 139 } 140 base.flags = R300_RESOURCE_FLAG_TRANSFER; 141 142 /* For texture reading, the temporary (detiled) texture is used as 143 * a render target when blitting from a tiled texture. */ 144 if (usage & PIPE_TRANSFER_READ) { 145 base.bind |= PIPE_BIND_RENDER_TARGET; 146 } 147 /* For texture writing, the temporary texture is used as a sampler 148 * when blitting into a tiled texture. */ 149 if (usage & PIPE_TRANSFER_WRITE) { 150 base.bind |= PIPE_BIND_SAMPLER_VIEW; 151 } 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 /* For linear textures, it's safe to fallback to 169 * an unpipelined transfer. */ 170 if (!tex->tex.microtile && !tex->tex.macrotile[level]) { 171 goto unpipelined; 172 } 173 174 /* Otherwise, go to hell. */ 175 fprintf(stderr, 176 "r300: Failed to create a transfer object, praise.\n"); 177 FREE(trans); 178 return NULL; 179 } 180 } 181 182 assert(!trans->linear_texture->tex.microtile && 183 !trans->linear_texture->tex.macrotile[0]); 184 185 /* Set the stride. */ 186 trans->transfer.stride = 187 trans->linear_texture->tex.stride_in_bytes[0]; 188 189 if (usage & PIPE_TRANSFER_READ) { 190 /* We cannot map a tiled texture directly because the data is 191 * in a different order, therefore we do detiling using a blit. */ 192 r300_copy_from_tiled_texture(ctx, trans); 193 194 /* Always referenced in the blit. */ 195 r300_flush(ctx, 0, NULL); 196 } 197 return &trans->transfer; 198 } 199 200 unpipelined: 201 /* Unpipelined transfer. */ 202 trans->transfer.stride = tex->tex.stride_in_bytes[level]; 203 trans->offset = r300_texture_get_offset(tex, level, box->z); 204 205 if (referenced_cs && 206 !(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) 207 r300_flush(ctx, 0, NULL); 208 return &trans->transfer; 209 } 210 return NULL; 211} 212 213void r300_texture_transfer_destroy(struct pipe_context *ctx, 214 struct pipe_transfer *trans) 215{ 216 struct r300_transfer *r300transfer = r300_transfer(trans); 217 218 if (r300transfer->linear_texture) { 219 if (trans->usage & PIPE_TRANSFER_WRITE) { 220 r300_copy_into_tiled_texture(ctx, r300transfer); 221 } 222 223 pipe_resource_reference( 224 (struct pipe_resource**)&r300transfer->linear_texture, NULL); 225 } 226 pipe_resource_reference(&trans->resource, NULL); 227 FREE(trans); 228} 229 230void* r300_texture_transfer_map(struct pipe_context *ctx, 231 struct pipe_transfer *transfer) 232{ 233 struct r300_context *r300 = r300_context(ctx); 234 struct r300_transfer *r300transfer = r300_transfer(transfer); 235 struct r300_resource *tex = r300_resource(transfer->resource); 236 char *map; 237 enum pipe_format format = tex->b.b.b.format; 238 239 if (r300transfer->linear_texture) { 240 /* The detiled texture is of the same size as the region being mapped 241 * (no offset needed). */ 242 return r300->rws->buffer_map(r300transfer->linear_texture->buf, 243 r300->cs, transfer->usage); 244 } else { 245 /* Tiling is disabled. */ 246 map = r300->rws->buffer_map(tex->buf, r300->cs, transfer->usage); 247 248 if (!map) { 249 return NULL; 250 } 251 252 return map + r300_transfer(transfer)->offset + 253 transfer->box.y / util_format_get_blockheight(format) * transfer->stride + 254 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format); 255 } 256} 257 258void r300_texture_transfer_unmap(struct pipe_context *ctx, 259 struct pipe_transfer *transfer) 260{ 261 struct radeon_winsys *rws = r300_context(ctx)->rws; 262 struct r300_transfer *r300transfer = r300_transfer(transfer); 263 struct r300_resource *tex = r300_resource(transfer->resource); 264 265 if (r300transfer->linear_texture) { 266 rws->buffer_unmap(r300transfer->linear_texture->buf); 267 } else { 268 rws->buffer_unmap(tex->buf); 269 } 270} 271