1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc. 3bf215546Sopenharmony_ci * All Rights Reserved. 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub 9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom 10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include "si_pipe.h" 26bf215546Sopenharmony_ci#include "util/u_memory.h" 27bf215546Sopenharmony_ci#include "util/u_transfer.h" 28bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include <inttypes.h> 31bf215546Sopenharmony_ci#include <stdio.h> 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_cibool si_cs_is_buffer_referenced(struct si_context *sctx, struct pb_buffer *buf, 34bf215546Sopenharmony_ci unsigned usage) 35bf215546Sopenharmony_ci{ 36bf215546Sopenharmony_ci return sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, buf, usage); 37bf215546Sopenharmony_ci} 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_civoid *si_buffer_map(struct si_context *sctx, struct si_resource *resource, 40bf215546Sopenharmony_ci unsigned usage) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci return sctx->ws->buffer_map(sctx->ws, resource->buf, &sctx->gfx_cs, usage); 43bf215546Sopenharmony_ci} 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_civoid si_init_resource_fields(struct si_screen *sscreen, struct si_resource *res, uint64_t size, 46bf215546Sopenharmony_ci unsigned alignment) 47bf215546Sopenharmony_ci{ 48bf215546Sopenharmony_ci struct si_texture *tex = (struct si_texture *)res; 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci res->bo_size = size; 51bf215546Sopenharmony_ci res->bo_alignment_log2 = util_logbase2(alignment); 52bf215546Sopenharmony_ci res->flags = 0; 53bf215546Sopenharmony_ci res->texture_handle_allocated = false; 54bf215546Sopenharmony_ci res->image_handle_allocated = false; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci switch (res->b.b.usage) { 57bf215546Sopenharmony_ci case PIPE_USAGE_STREAM: 58bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_GTT_WC; 59bf215546Sopenharmony_ci if (sscreen->info.smart_access_memory) 60bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_VRAM; 61bf215546Sopenharmony_ci else 62bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_GTT; 63bf215546Sopenharmony_ci break; 64bf215546Sopenharmony_ci case PIPE_USAGE_STAGING: 65bf215546Sopenharmony_ci /* Transfers are likely to occur more often with these 66bf215546Sopenharmony_ci * resources. */ 67bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_GTT; 68bf215546Sopenharmony_ci break; 69bf215546Sopenharmony_ci case PIPE_USAGE_DYNAMIC: 70bf215546Sopenharmony_ci case PIPE_USAGE_DEFAULT: 71bf215546Sopenharmony_ci case PIPE_USAGE_IMMUTABLE: 72bf215546Sopenharmony_ci default: 73bf215546Sopenharmony_ci /* Not listing GTT here improves performance in some 74bf215546Sopenharmony_ci * apps. */ 75bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_VRAM; 76bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_GTT_WC; 77bf215546Sopenharmony_ci break; 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci if (res->b.b.target == PIPE_BUFFER && res->b.b.flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) { 81bf215546Sopenharmony_ci /* Use GTT for all persistent mappings with older 82bf215546Sopenharmony_ci * kernels, because they didn't always flush the HDP 83bf215546Sopenharmony_ci * cache before CS execution. 84bf215546Sopenharmony_ci * 85bf215546Sopenharmony_ci * Write-combined CPU mappings are fine, the kernel 86bf215546Sopenharmony_ci * ensures all CPU writes finish before the GPU 87bf215546Sopenharmony_ci * executes a command stream. 88bf215546Sopenharmony_ci * 89bf215546Sopenharmony_ci * radeon doesn't have good BO move throttling, so put all 90bf215546Sopenharmony_ci * persistent buffers into GTT to prevent VRAM CPU page faults. 91bf215546Sopenharmony_ci */ 92bf215546Sopenharmony_ci if (!sscreen->info.is_amdgpu) 93bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_GTT; 94bf215546Sopenharmony_ci } 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci /* Tiled textures are unmappable. Always put them in VRAM. */ 97bf215546Sopenharmony_ci if ((res->b.b.target != PIPE_BUFFER && !tex->surface.is_linear) || 98bf215546Sopenharmony_ci res->b.b.flags & PIPE_RESOURCE_FLAG_UNMAPPABLE) { 99bf215546Sopenharmony_ci res->domains = RADEON_DOMAIN_VRAM; 100bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_GTT_WC; 101bf215546Sopenharmony_ci } 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci /* Displayable and shareable surfaces are not suballocated. */ 104bf215546Sopenharmony_ci if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT)) 105bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */ 106bf215546Sopenharmony_ci else 107bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci if (res->b.b.bind & PIPE_BIND_PROTECTED || 110bf215546Sopenharmony_ci /* Force scanout/depth/stencil buffer allocation to be encrypted */ 111bf215546Sopenharmony_ci (sscreen->debug_flags & DBG(TMZ) && 112bf215546Sopenharmony_ci res->b.b.bind & (PIPE_BIND_SCANOUT | PIPE_BIND_DEPTH_STENCIL))) 113bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_ENCRYPTED; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci if (res->b.b.flags & PIPE_RESOURCE_FLAG_ENCRYPTED) 116bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_ENCRYPTED; 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(NO_WC)) 119bf215546Sopenharmony_ci res->flags &= ~RADEON_FLAG_GTT_WC; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci if (res->b.b.flags & SI_RESOURCE_FLAG_READ_ONLY) 122bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_READ_ONLY; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci if (res->b.b.flags & SI_RESOURCE_FLAG_32BIT) 125bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_32BIT; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci if (res->b.b.flags & SI_RESOURCE_FLAG_DRIVER_INTERNAL) 128bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_DRIVER_INTERNAL; 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci if (res->b.b.flags & PIPE_RESOURCE_FLAG_SPARSE) 131bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_SPARSE; 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci /* For higher throughput and lower latency over PCIe assuming sequential access. 134bf215546Sopenharmony_ci * Only CP DMA and optimized compute benefit from this. 135bf215546Sopenharmony_ci * GFX8 and older don't support RADEON_FLAG_GL2_BYPASS. 136bf215546Sopenharmony_ci */ 137bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX9 && 138bf215546Sopenharmony_ci res->b.b.flags & SI_RESOURCE_FLAG_GL2_BYPASS) 139bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_GL2_BYPASS; 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci if (res->b.b.flags & SI_RESOURCE_FLAG_DISCARDABLE && 142bf215546Sopenharmony_ci sscreen->info.drm_major == 3 && sscreen->info.drm_minor >= 47) { 143bf215546Sopenharmony_ci /* Assume VRAM, so that we can use BIG_PAGE. */ 144bf215546Sopenharmony_ci assert(res->domains == RADEON_DOMAIN_VRAM); 145bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_DISCARDABLE; 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci if (res->domains == RADEON_DOMAIN_VRAM && 149bf215546Sopenharmony_ci sscreen->options.mall_noalloc) 150bf215546Sopenharmony_ci res->flags |= RADEON_FLAG_MALL_NOALLOC; 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci /* Set expected VRAM and GART usage for the buffer. */ 153bf215546Sopenharmony_ci res->memory_usage_kb = MAX2(1, size / 1024); 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci if (res->domains & RADEON_DOMAIN_VRAM) { 156bf215546Sopenharmony_ci /* We don't want to evict buffers from VRAM by mapping them for CPU access, 157bf215546Sopenharmony_ci * because they might never be moved back again. If a buffer is large enough, 158bf215546Sopenharmony_ci * upload data by copying from a temporary GTT buffer. 159bf215546Sopenharmony_ci */ 160bf215546Sopenharmony_ci if (!sscreen->info.smart_access_memory && 161bf215546Sopenharmony_ci sscreen->info.has_dedicated_vram && 162bf215546Sopenharmony_ci !res->b.cpu_storage && /* TODO: The CPU storage breaks this. */ 163bf215546Sopenharmony_ci size >= sscreen->options.max_vram_map_size) 164bf215546Sopenharmony_ci res->b.b.flags |= PIPE_RESOURCE_FLAG_DONT_MAP_DIRECTLY; 165bf215546Sopenharmony_ci } 166bf215546Sopenharmony_ci} 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_cibool si_alloc_resource(struct si_screen *sscreen, struct si_resource *res) 169bf215546Sopenharmony_ci{ 170bf215546Sopenharmony_ci struct pb_buffer *old_buf, *new_buf; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci /* Allocate a new resource. */ 173bf215546Sopenharmony_ci new_buf = sscreen->ws->buffer_create(sscreen->ws, res->bo_size, 1 << res->bo_alignment_log2, 174bf215546Sopenharmony_ci res->domains, res->flags); 175bf215546Sopenharmony_ci if (!new_buf) { 176bf215546Sopenharmony_ci return false; 177bf215546Sopenharmony_ci } 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci /* Replace the pointer such that if res->buf wasn't NULL, it won't be 180bf215546Sopenharmony_ci * NULL. This should prevent crashes with multiple contexts using 181bf215546Sopenharmony_ci * the same buffer where one of the contexts invalidates it while 182bf215546Sopenharmony_ci * the others are using it. */ 183bf215546Sopenharmony_ci old_buf = res->buf; 184bf215546Sopenharmony_ci res->buf = new_buf; /* should be atomic */ 185bf215546Sopenharmony_ci res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf); 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci if (res->flags & RADEON_FLAG_32BIT) { 188bf215546Sopenharmony_ci uint64_t start = res->gpu_address; 189bf215546Sopenharmony_ci uint64_t last = start + res->bo_size - 1; 190bf215546Sopenharmony_ci (void)start; 191bf215546Sopenharmony_ci (void)last; 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci assert((start >> 32) == sscreen->info.address32_hi); 194bf215546Sopenharmony_ci assert((last >> 32) == sscreen->info.address32_hi); 195bf215546Sopenharmony_ci } 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci radeon_bo_reference(sscreen->ws, &old_buf, NULL); 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci util_range_set_empty(&res->valid_buffer_range); 200bf215546Sopenharmony_ci res->TC_L2_dirty = false; 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci /* Print debug information. */ 203bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(VM) && res->b.b.target == PIPE_BUFFER) { 204bf215546Sopenharmony_ci fprintf(stderr, "VM start=0x%" PRIX64 " end=0x%" PRIX64 " | Buffer %" PRIu64 " bytes\n", 205bf215546Sopenharmony_ci res->gpu_address, res->gpu_address + res->buf->size, res->buf->size); 206bf215546Sopenharmony_ci } 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci if (res->b.b.flags & SI_RESOURCE_FLAG_CLEAR) 209bf215546Sopenharmony_ci si_screen_clear_buffer(sscreen, &res->b.b, 0, res->bo_size, 0, SI_OP_SYNC_AFTER); 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci return true; 212bf215546Sopenharmony_ci} 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_cistatic void si_resource_destroy(struct pipe_screen *screen, struct pipe_resource *buf) 215bf215546Sopenharmony_ci{ 216bf215546Sopenharmony_ci if (buf->target == PIPE_BUFFER) { 217bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 218bf215546Sopenharmony_ci struct si_resource *buffer = si_resource(buf); 219bf215546Sopenharmony_ci 220bf215546Sopenharmony_ci threaded_resource_deinit(buf); 221bf215546Sopenharmony_ci util_range_destroy(&buffer->valid_buffer_range); 222bf215546Sopenharmony_ci radeon_bo_reference(((struct si_screen*)screen)->ws, &buffer->buf, NULL); 223bf215546Sopenharmony_ci util_idalloc_mt_free(&sscreen->buffer_ids, buffer->b.buffer_id_unique); 224bf215546Sopenharmony_ci FREE_CL(buffer); 225bf215546Sopenharmony_ci } else if (buf->flags & SI_RESOURCE_AUX_PLANE) { 226bf215546Sopenharmony_ci struct si_auxiliary_texture *tex = (struct si_auxiliary_texture *)buf; 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci radeon_bo_reference(((struct si_screen*)screen)->ws, &tex->buffer, NULL); 229bf215546Sopenharmony_ci FREE_CL(tex); 230bf215546Sopenharmony_ci } else { 231bf215546Sopenharmony_ci struct si_texture *tex = (struct si_texture *)buf; 232bf215546Sopenharmony_ci struct si_resource *resource = &tex->buffer; 233bf215546Sopenharmony_ci 234bf215546Sopenharmony_ci si_texture_reference(&tex->flushed_depth_texture, NULL); 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ci if (tex->cmask_buffer != &tex->buffer) { 237bf215546Sopenharmony_ci si_resource_reference(&tex->cmask_buffer, NULL); 238bf215546Sopenharmony_ci } 239bf215546Sopenharmony_ci radeon_bo_reference(((struct si_screen*)screen)->ws, &resource->buf, NULL); 240bf215546Sopenharmony_ci FREE_CL(tex); 241bf215546Sopenharmony_ci } 242bf215546Sopenharmony_ci} 243bf215546Sopenharmony_ci 244bf215546Sopenharmony_ci/* Reallocate the buffer a update all resource bindings where the buffer is 245bf215546Sopenharmony_ci * bound. 246bf215546Sopenharmony_ci * 247bf215546Sopenharmony_ci * This is used to avoid CPU-GPU synchronizations, because it makes the buffer 248bf215546Sopenharmony_ci * idle by discarding its contents. 249bf215546Sopenharmony_ci */ 250bf215546Sopenharmony_cistatic bool si_invalidate_buffer(struct si_context *sctx, struct si_resource *buf) 251bf215546Sopenharmony_ci{ 252bf215546Sopenharmony_ci /* Shared buffers can't be reallocated. */ 253bf215546Sopenharmony_ci if (buf->b.is_shared) 254bf215546Sopenharmony_ci return false; 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci /* Sparse buffers can't be reallocated. */ 257bf215546Sopenharmony_ci if (buf->flags & RADEON_FLAG_SPARSE) 258bf215546Sopenharmony_ci return false; 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_ci /* In AMD_pinned_memory, the user pointer association only gets 261bf215546Sopenharmony_ci * broken when the buffer is explicitly re-allocated. 262bf215546Sopenharmony_ci */ 263bf215546Sopenharmony_ci if (buf->b.is_user_ptr) 264bf215546Sopenharmony_ci return false; 265bf215546Sopenharmony_ci 266bf215546Sopenharmony_ci /* Check if mapping this buffer would cause waiting for the GPU. */ 267bf215546Sopenharmony_ci if (si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) || 268bf215546Sopenharmony_ci !sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) { 269bf215546Sopenharmony_ci /* Reallocate the buffer in the same pipe_resource. */ 270bf215546Sopenharmony_ci si_alloc_resource(sctx->screen, buf); 271bf215546Sopenharmony_ci si_rebind_buffer(sctx, &buf->b.b); 272bf215546Sopenharmony_ci } else { 273bf215546Sopenharmony_ci util_range_set_empty(&buf->valid_buffer_range); 274bf215546Sopenharmony_ci } 275bf215546Sopenharmony_ci 276bf215546Sopenharmony_ci return true; 277bf215546Sopenharmony_ci} 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_ci/* Replace the storage of dst with src. */ 280bf215546Sopenharmony_civoid si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *dst, 281bf215546Sopenharmony_ci struct pipe_resource *src, unsigned num_rebinds, uint32_t rebind_mask, 282bf215546Sopenharmony_ci uint32_t delete_buffer_id) 283bf215546Sopenharmony_ci{ 284bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 285bf215546Sopenharmony_ci struct si_resource *sdst = si_resource(dst); 286bf215546Sopenharmony_ci struct si_resource *ssrc = si_resource(src); 287bf215546Sopenharmony_ci 288bf215546Sopenharmony_ci radeon_bo_reference(sctx->screen->ws, &sdst->buf, ssrc->buf); 289bf215546Sopenharmony_ci sdst->gpu_address = ssrc->gpu_address; 290bf215546Sopenharmony_ci sdst->b.b.bind = ssrc->b.b.bind; 291bf215546Sopenharmony_ci sdst->flags = ssrc->flags; 292bf215546Sopenharmony_ci 293bf215546Sopenharmony_ci assert(sdst->memory_usage_kb == ssrc->memory_usage_kb); 294bf215546Sopenharmony_ci assert(sdst->bo_size == ssrc->bo_size); 295bf215546Sopenharmony_ci assert(sdst->bo_alignment_log2 == ssrc->bo_alignment_log2); 296bf215546Sopenharmony_ci assert(sdst->domains == ssrc->domains); 297bf215546Sopenharmony_ci 298bf215546Sopenharmony_ci si_rebind_buffer(sctx, dst); 299bf215546Sopenharmony_ci 300bf215546Sopenharmony_ci util_idalloc_mt_free(&sctx->screen->buffer_ids, delete_buffer_id); 301bf215546Sopenharmony_ci} 302bf215546Sopenharmony_ci 303bf215546Sopenharmony_cistatic void si_invalidate_resource(struct pipe_context *ctx, struct pipe_resource *resource) 304bf215546Sopenharmony_ci{ 305bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 306bf215546Sopenharmony_ci struct si_resource *buf = si_resource(resource); 307bf215546Sopenharmony_ci 308bf215546Sopenharmony_ci /* We currently only do anyting here for buffers */ 309bf215546Sopenharmony_ci if (resource->target == PIPE_BUFFER) 310bf215546Sopenharmony_ci (void)si_invalidate_buffer(sctx, buf); 311bf215546Sopenharmony_ci} 312bf215546Sopenharmony_ci 313bf215546Sopenharmony_cistatic void *si_buffer_get_transfer(struct pipe_context *ctx, struct pipe_resource *resource, 314bf215546Sopenharmony_ci unsigned usage, const struct pipe_box *box, 315bf215546Sopenharmony_ci struct pipe_transfer **ptransfer, void *data, 316bf215546Sopenharmony_ci struct si_resource *staging, unsigned offset) 317bf215546Sopenharmony_ci{ 318bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 319bf215546Sopenharmony_ci struct si_transfer *transfer; 320bf215546Sopenharmony_ci 321bf215546Sopenharmony_ci if (usage & PIPE_MAP_THREAD_SAFE) 322bf215546Sopenharmony_ci transfer = calloc(1, sizeof(*transfer)); 323bf215546Sopenharmony_ci else if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC) 324bf215546Sopenharmony_ci transfer = slab_zalloc(&sctx->pool_transfers_unsync); 325bf215546Sopenharmony_ci else 326bf215546Sopenharmony_ci transfer = slab_zalloc(&sctx->pool_transfers); 327bf215546Sopenharmony_ci 328bf215546Sopenharmony_ci pipe_resource_reference(&transfer->b.b.resource, resource); 329bf215546Sopenharmony_ci transfer->b.b.usage = usage; 330bf215546Sopenharmony_ci transfer->b.b.box = *box; 331bf215546Sopenharmony_ci transfer->b.b.offset = offset; 332bf215546Sopenharmony_ci transfer->staging = staging; 333bf215546Sopenharmony_ci *ptransfer = &transfer->b.b; 334bf215546Sopenharmony_ci return data; 335bf215546Sopenharmony_ci} 336bf215546Sopenharmony_ci 337bf215546Sopenharmony_cistatic void *si_buffer_transfer_map(struct pipe_context *ctx, struct pipe_resource *resource, 338bf215546Sopenharmony_ci unsigned level, unsigned usage, const struct pipe_box *box, 339bf215546Sopenharmony_ci struct pipe_transfer **ptransfer) 340bf215546Sopenharmony_ci{ 341bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 342bf215546Sopenharmony_ci struct si_resource *buf = si_resource(resource); 343bf215546Sopenharmony_ci uint8_t *data; 344bf215546Sopenharmony_ci 345bf215546Sopenharmony_ci assert(resource->target == PIPE_BUFFER); 346bf215546Sopenharmony_ci assert(box->x + box->width <= resource->width0); 347bf215546Sopenharmony_ci 348bf215546Sopenharmony_ci /* From GL_AMD_pinned_memory issues: 349bf215546Sopenharmony_ci * 350bf215546Sopenharmony_ci * 4) Is glMapBuffer on a shared buffer guaranteed to return the 351bf215546Sopenharmony_ci * same system address which was specified at creation time? 352bf215546Sopenharmony_ci * 353bf215546Sopenharmony_ci * RESOLVED: NO. The GL implementation might return a different 354bf215546Sopenharmony_ci * virtual mapping of that memory, although the same physical 355bf215546Sopenharmony_ci * page will be used. 356bf215546Sopenharmony_ci * 357bf215546Sopenharmony_ci * So don't ever use staging buffers. 358bf215546Sopenharmony_ci */ 359bf215546Sopenharmony_ci if (buf->b.is_user_ptr) 360bf215546Sopenharmony_ci usage |= PIPE_MAP_PERSISTENT; 361bf215546Sopenharmony_ci if (usage & PIPE_MAP_ONCE) 362bf215546Sopenharmony_ci usage |= RADEON_MAP_TEMPORARY; 363bf215546Sopenharmony_ci 364bf215546Sopenharmony_ci /* See if the buffer range being mapped has never been initialized, 365bf215546Sopenharmony_ci * in which case it can be mapped unsynchronized. */ 366bf215546Sopenharmony_ci if (!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) && 367bf215546Sopenharmony_ci usage & PIPE_MAP_WRITE && !buf->b.is_shared && 368bf215546Sopenharmony_ci !util_ranges_intersect(&buf->valid_buffer_range, box->x, box->x + box->width)) { 369bf215546Sopenharmony_ci usage |= PIPE_MAP_UNSYNCHRONIZED; 370bf215546Sopenharmony_ci } 371bf215546Sopenharmony_ci 372bf215546Sopenharmony_ci /* If discarding the entire range, discard the whole resource instead. */ 373bf215546Sopenharmony_ci if (usage & PIPE_MAP_DISCARD_RANGE && box->x == 0 && box->width == resource->width0) { 374bf215546Sopenharmony_ci usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE; 375bf215546Sopenharmony_ci } 376bf215546Sopenharmony_ci 377bf215546Sopenharmony_ci /* If a buffer in VRAM is too large and the range is discarded, don't 378bf215546Sopenharmony_ci * map it directly. This makes sure that the buffer stays in VRAM. 379bf215546Sopenharmony_ci */ 380bf215546Sopenharmony_ci bool force_discard_range = false; 381bf215546Sopenharmony_ci if (usage & (PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_DISCARD_RANGE) && 382bf215546Sopenharmony_ci !(usage & PIPE_MAP_PERSISTENT) && 383bf215546Sopenharmony_ci buf->b.b.flags & PIPE_RESOURCE_FLAG_DONT_MAP_DIRECTLY) { 384bf215546Sopenharmony_ci usage &= ~(PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_UNSYNCHRONIZED); 385bf215546Sopenharmony_ci usage |= PIPE_MAP_DISCARD_RANGE; 386bf215546Sopenharmony_ci force_discard_range = true; 387bf215546Sopenharmony_ci } 388bf215546Sopenharmony_ci 389bf215546Sopenharmony_ci if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE && 390bf215546Sopenharmony_ci !(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INVALIDATE))) { 391bf215546Sopenharmony_ci assert(usage & PIPE_MAP_WRITE); 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ci if (si_invalidate_buffer(sctx, buf)) { 394bf215546Sopenharmony_ci /* At this point, the buffer is always idle. */ 395bf215546Sopenharmony_ci usage |= PIPE_MAP_UNSYNCHRONIZED; 396bf215546Sopenharmony_ci } else { 397bf215546Sopenharmony_ci /* Fall back to a temporary buffer. */ 398bf215546Sopenharmony_ci usage |= PIPE_MAP_DISCARD_RANGE; 399bf215546Sopenharmony_ci } 400bf215546Sopenharmony_ci } 401bf215546Sopenharmony_ci 402bf215546Sopenharmony_ci if (usage & PIPE_MAP_DISCARD_RANGE && 403bf215546Sopenharmony_ci ((!(usage & (PIPE_MAP_UNSYNCHRONIZED | PIPE_MAP_PERSISTENT))) || 404bf215546Sopenharmony_ci (buf->flags & RADEON_FLAG_SPARSE))) { 405bf215546Sopenharmony_ci assert(usage & PIPE_MAP_WRITE); 406bf215546Sopenharmony_ci 407bf215546Sopenharmony_ci /* Check if mapping this buffer would cause waiting for the GPU. 408bf215546Sopenharmony_ci */ 409bf215546Sopenharmony_ci if (buf->flags & (RADEON_FLAG_SPARSE | RADEON_FLAG_NO_CPU_ACCESS) || 410bf215546Sopenharmony_ci force_discard_range || 411bf215546Sopenharmony_ci si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) || 412bf215546Sopenharmony_ci !sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) { 413bf215546Sopenharmony_ci /* Do a wait-free write-only transfer using a temporary buffer. */ 414bf215546Sopenharmony_ci struct u_upload_mgr *uploader; 415bf215546Sopenharmony_ci struct si_resource *staging = NULL; 416bf215546Sopenharmony_ci unsigned offset; 417bf215546Sopenharmony_ci 418bf215546Sopenharmony_ci /* If we are not called from the driver thread, we have 419bf215546Sopenharmony_ci * to use the uploader from u_threaded_context, which is 420bf215546Sopenharmony_ci * local to the calling thread. 421bf215546Sopenharmony_ci */ 422bf215546Sopenharmony_ci if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC) 423bf215546Sopenharmony_ci uploader = sctx->tc->base.stream_uploader; 424bf215546Sopenharmony_ci else 425bf215546Sopenharmony_ci uploader = sctx->b.stream_uploader; 426bf215546Sopenharmony_ci 427bf215546Sopenharmony_ci u_upload_alloc(uploader, 0, box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT), 428bf215546Sopenharmony_ci sctx->screen->info.tcc_cache_line_size, &offset, 429bf215546Sopenharmony_ci (struct pipe_resource **)&staging, (void **)&data); 430bf215546Sopenharmony_ci 431bf215546Sopenharmony_ci if (staging) { 432bf215546Sopenharmony_ci data += box->x % SI_MAP_BUFFER_ALIGNMENT; 433bf215546Sopenharmony_ci return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging, 434bf215546Sopenharmony_ci offset); 435bf215546Sopenharmony_ci } else if (buf->flags & RADEON_FLAG_SPARSE) { 436bf215546Sopenharmony_ci return NULL; 437bf215546Sopenharmony_ci } 438bf215546Sopenharmony_ci } else { 439bf215546Sopenharmony_ci /* At this point, the buffer is always idle (we checked it above). */ 440bf215546Sopenharmony_ci usage |= PIPE_MAP_UNSYNCHRONIZED; 441bf215546Sopenharmony_ci } 442bf215546Sopenharmony_ci } 443bf215546Sopenharmony_ci /* Use a staging buffer in cached GTT for reads. */ 444bf215546Sopenharmony_ci else if (((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) && 445bf215546Sopenharmony_ci (buf->domains & RADEON_DOMAIN_VRAM || buf->flags & RADEON_FLAG_GTT_WC)) || 446bf215546Sopenharmony_ci (buf->flags & (RADEON_FLAG_SPARSE | RADEON_FLAG_NO_CPU_ACCESS))) { 447bf215546Sopenharmony_ci struct si_resource *staging; 448bf215546Sopenharmony_ci 449bf215546Sopenharmony_ci assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC | PIPE_MAP_THREAD_SAFE))); 450bf215546Sopenharmony_ci staging = si_aligned_buffer_create(ctx->screen, 451bf215546Sopenharmony_ci SI_RESOURCE_FLAG_GL2_BYPASS | SI_RESOURCE_FLAG_DRIVER_INTERNAL, 452bf215546Sopenharmony_ci PIPE_USAGE_STAGING, 453bf215546Sopenharmony_ci box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT), 256); 454bf215546Sopenharmony_ci if (staging) { 455bf215546Sopenharmony_ci /* Copy the VRAM buffer to the staging buffer. */ 456bf215546Sopenharmony_ci si_copy_buffer(sctx, &staging->b.b, resource, box->x % SI_MAP_BUFFER_ALIGNMENT, 457bf215546Sopenharmony_ci box->x, box->width, SI_OP_SYNC_BEFORE_AFTER); 458bf215546Sopenharmony_ci 459bf215546Sopenharmony_ci data = si_buffer_map(sctx, staging, usage & ~PIPE_MAP_UNSYNCHRONIZED); 460bf215546Sopenharmony_ci if (!data) { 461bf215546Sopenharmony_ci si_resource_reference(&staging, NULL); 462bf215546Sopenharmony_ci return NULL; 463bf215546Sopenharmony_ci } 464bf215546Sopenharmony_ci data += box->x % SI_MAP_BUFFER_ALIGNMENT; 465bf215546Sopenharmony_ci 466bf215546Sopenharmony_ci return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging, 0); 467bf215546Sopenharmony_ci } else if (buf->flags & RADEON_FLAG_SPARSE) { 468bf215546Sopenharmony_ci return NULL; 469bf215546Sopenharmony_ci } 470bf215546Sopenharmony_ci } 471bf215546Sopenharmony_ci 472bf215546Sopenharmony_ci data = si_buffer_map(sctx, buf, usage); 473bf215546Sopenharmony_ci if (!data) { 474bf215546Sopenharmony_ci return NULL; 475bf215546Sopenharmony_ci } 476bf215546Sopenharmony_ci data += box->x; 477bf215546Sopenharmony_ci 478bf215546Sopenharmony_ci return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, NULL, 0); 479bf215546Sopenharmony_ci} 480bf215546Sopenharmony_ci 481bf215546Sopenharmony_cistatic void si_buffer_do_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer, 482bf215546Sopenharmony_ci const struct pipe_box *box) 483bf215546Sopenharmony_ci{ 484bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 485bf215546Sopenharmony_ci struct si_transfer *stransfer = (struct si_transfer *)transfer; 486bf215546Sopenharmony_ci struct si_resource *buf = si_resource(transfer->resource); 487bf215546Sopenharmony_ci 488bf215546Sopenharmony_ci if (stransfer->staging) { 489bf215546Sopenharmony_ci unsigned src_offset = 490bf215546Sopenharmony_ci stransfer->b.b.offset + transfer->box.x % SI_MAP_BUFFER_ALIGNMENT + (box->x - transfer->box.x); 491bf215546Sopenharmony_ci 492bf215546Sopenharmony_ci /* Copy the staging buffer into the original one. */ 493bf215546Sopenharmony_ci si_copy_buffer(sctx, transfer->resource, &stransfer->staging->b.b, box->x, src_offset, 494bf215546Sopenharmony_ci box->width, SI_OP_SYNC_BEFORE_AFTER); 495bf215546Sopenharmony_ci } 496bf215546Sopenharmony_ci 497bf215546Sopenharmony_ci util_range_add(&buf->b.b, &buf->valid_buffer_range, box->x, box->x + box->width); 498bf215546Sopenharmony_ci} 499bf215546Sopenharmony_ci 500bf215546Sopenharmony_cistatic void si_buffer_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer, 501bf215546Sopenharmony_ci const struct pipe_box *rel_box) 502bf215546Sopenharmony_ci{ 503bf215546Sopenharmony_ci unsigned required_usage = PIPE_MAP_WRITE | PIPE_MAP_FLUSH_EXPLICIT; 504bf215546Sopenharmony_ci 505bf215546Sopenharmony_ci if ((transfer->usage & required_usage) == required_usage) { 506bf215546Sopenharmony_ci struct pipe_box box; 507bf215546Sopenharmony_ci 508bf215546Sopenharmony_ci u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box); 509bf215546Sopenharmony_ci si_buffer_do_flush_region(ctx, transfer, &box); 510bf215546Sopenharmony_ci } 511bf215546Sopenharmony_ci} 512bf215546Sopenharmony_ci 513bf215546Sopenharmony_cistatic void si_buffer_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *transfer) 514bf215546Sopenharmony_ci{ 515bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 516bf215546Sopenharmony_ci struct si_transfer *stransfer = (struct si_transfer *)transfer; 517bf215546Sopenharmony_ci 518bf215546Sopenharmony_ci if (transfer->usage & PIPE_MAP_WRITE && !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT)) 519bf215546Sopenharmony_ci si_buffer_do_flush_region(ctx, transfer, &transfer->box); 520bf215546Sopenharmony_ci 521bf215546Sopenharmony_ci if (transfer->usage & (PIPE_MAP_ONCE | RADEON_MAP_TEMPORARY) && 522bf215546Sopenharmony_ci !stransfer->staging) 523bf215546Sopenharmony_ci sctx->ws->buffer_unmap(sctx->ws, si_resource(stransfer->b.b.resource)->buf); 524bf215546Sopenharmony_ci 525bf215546Sopenharmony_ci si_resource_reference(&stransfer->staging, NULL); 526bf215546Sopenharmony_ci assert(stransfer->b.staging == NULL); /* for threaded context only */ 527bf215546Sopenharmony_ci pipe_resource_reference(&transfer->resource, NULL); 528bf215546Sopenharmony_ci 529bf215546Sopenharmony_ci if (transfer->usage & PIPE_MAP_THREAD_SAFE) { 530bf215546Sopenharmony_ci free(transfer); 531bf215546Sopenharmony_ci } else { 532bf215546Sopenharmony_ci /* Don't use pool_transfers_unsync. We are always in the driver 533bf215546Sopenharmony_ci * thread. Freeing an object into a different pool is allowed. 534bf215546Sopenharmony_ci */ 535bf215546Sopenharmony_ci slab_free(&sctx->pool_transfers, transfer); 536bf215546Sopenharmony_ci } 537bf215546Sopenharmony_ci} 538bf215546Sopenharmony_ci 539bf215546Sopenharmony_cistatic void si_buffer_subdata(struct pipe_context *ctx, struct pipe_resource *buffer, 540bf215546Sopenharmony_ci unsigned usage, unsigned offset, unsigned size, const void *data) 541bf215546Sopenharmony_ci{ 542bf215546Sopenharmony_ci struct pipe_transfer *transfer = NULL; 543bf215546Sopenharmony_ci struct pipe_box box; 544bf215546Sopenharmony_ci uint8_t *map = NULL; 545bf215546Sopenharmony_ci 546bf215546Sopenharmony_ci usage |= PIPE_MAP_WRITE; 547bf215546Sopenharmony_ci 548bf215546Sopenharmony_ci if (!(usage & PIPE_MAP_DIRECTLY)) 549bf215546Sopenharmony_ci usage |= PIPE_MAP_DISCARD_RANGE; 550bf215546Sopenharmony_ci 551bf215546Sopenharmony_ci u_box_1d(offset, size, &box); 552bf215546Sopenharmony_ci map = si_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer); 553bf215546Sopenharmony_ci if (!map) 554bf215546Sopenharmony_ci return; 555bf215546Sopenharmony_ci 556bf215546Sopenharmony_ci memcpy(map, data, size); 557bf215546Sopenharmony_ci si_buffer_transfer_unmap(ctx, transfer); 558bf215546Sopenharmony_ci} 559bf215546Sopenharmony_ci 560bf215546Sopenharmony_cistatic struct si_resource *si_alloc_buffer_struct(struct pipe_screen *screen, 561bf215546Sopenharmony_ci const struct pipe_resource *templ, 562bf215546Sopenharmony_ci bool allow_cpu_storage) 563bf215546Sopenharmony_ci{ 564bf215546Sopenharmony_ci struct si_resource *buf = MALLOC_STRUCT_CL(si_resource); 565bf215546Sopenharmony_ci 566bf215546Sopenharmony_ci buf->b.b = *templ; 567bf215546Sopenharmony_ci buf->b.b.next = NULL; 568bf215546Sopenharmony_ci pipe_reference_init(&buf->b.b.reference, 1); 569bf215546Sopenharmony_ci buf->b.b.screen = screen; 570bf215546Sopenharmony_ci 571bf215546Sopenharmony_ci threaded_resource_init(&buf->b.b, allow_cpu_storage); 572bf215546Sopenharmony_ci 573bf215546Sopenharmony_ci buf->buf = NULL; 574bf215546Sopenharmony_ci buf->bind_history = 0; 575bf215546Sopenharmony_ci buf->TC_L2_dirty = false; 576bf215546Sopenharmony_ci util_range_init(&buf->valid_buffer_range); 577bf215546Sopenharmony_ci return buf; 578bf215546Sopenharmony_ci} 579bf215546Sopenharmony_ci 580bf215546Sopenharmony_cistatic struct pipe_resource *si_buffer_create(struct pipe_screen *screen, 581bf215546Sopenharmony_ci const struct pipe_resource *templ, unsigned alignment) 582bf215546Sopenharmony_ci{ 583bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 584bf215546Sopenharmony_ci struct si_resource *buf = 585bf215546Sopenharmony_ci si_alloc_buffer_struct(screen, templ, 586bf215546Sopenharmony_ci templ->width0 <= sscreen->options.tc_max_cpu_storage_size); 587bf215546Sopenharmony_ci 588bf215546Sopenharmony_ci if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE) 589bf215546Sopenharmony_ci buf->b.b.flags |= PIPE_RESOURCE_FLAG_UNMAPPABLE; 590bf215546Sopenharmony_ci 591bf215546Sopenharmony_ci si_init_resource_fields(sscreen, buf, templ->width0, alignment); 592bf215546Sopenharmony_ci 593bf215546Sopenharmony_ci buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids); 594bf215546Sopenharmony_ci 595bf215546Sopenharmony_ci if (!si_alloc_resource(sscreen, buf)) { 596bf215546Sopenharmony_ci si_resource_destroy(screen, &buf->b.b); 597bf215546Sopenharmony_ci return NULL; 598bf215546Sopenharmony_ci } 599bf215546Sopenharmony_ci 600bf215546Sopenharmony_ci return &buf->b.b; 601bf215546Sopenharmony_ci} 602bf215546Sopenharmony_ci 603bf215546Sopenharmony_cistruct pipe_resource *pipe_aligned_buffer_create(struct pipe_screen *screen, unsigned flags, 604bf215546Sopenharmony_ci unsigned usage, unsigned size, unsigned alignment) 605bf215546Sopenharmony_ci{ 606bf215546Sopenharmony_ci struct pipe_resource buffer; 607bf215546Sopenharmony_ci 608bf215546Sopenharmony_ci memset(&buffer, 0, sizeof buffer); 609bf215546Sopenharmony_ci buffer.target = PIPE_BUFFER; 610bf215546Sopenharmony_ci buffer.format = PIPE_FORMAT_R8_UNORM; 611bf215546Sopenharmony_ci buffer.bind = 0; 612bf215546Sopenharmony_ci buffer.usage = usage; 613bf215546Sopenharmony_ci buffer.flags = flags; 614bf215546Sopenharmony_ci buffer.width0 = size; 615bf215546Sopenharmony_ci buffer.height0 = 1; 616bf215546Sopenharmony_ci buffer.depth0 = 1; 617bf215546Sopenharmony_ci buffer.array_size = 1; 618bf215546Sopenharmony_ci return si_buffer_create(screen, &buffer, alignment); 619bf215546Sopenharmony_ci} 620bf215546Sopenharmony_ci 621bf215546Sopenharmony_cistruct si_resource *si_aligned_buffer_create(struct pipe_screen *screen, unsigned flags, 622bf215546Sopenharmony_ci unsigned usage, unsigned size, unsigned alignment) 623bf215546Sopenharmony_ci{ 624bf215546Sopenharmony_ci return si_resource(pipe_aligned_buffer_create(screen, flags, usage, size, alignment)); 625bf215546Sopenharmony_ci} 626bf215546Sopenharmony_ci 627bf215546Sopenharmony_cistatic struct pipe_resource *si_buffer_from_user_memory(struct pipe_screen *screen, 628bf215546Sopenharmony_ci const struct pipe_resource *templ, 629bf215546Sopenharmony_ci void *user_memory) 630bf215546Sopenharmony_ci{ 631bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 632bf215546Sopenharmony_ci struct radeon_winsys *ws = sscreen->ws; 633bf215546Sopenharmony_ci struct si_resource *buf = si_alloc_buffer_struct(screen, templ, false); 634bf215546Sopenharmony_ci 635bf215546Sopenharmony_ci buf->domains = RADEON_DOMAIN_GTT; 636bf215546Sopenharmony_ci buf->flags = 0; 637bf215546Sopenharmony_ci buf->b.is_user_ptr = true; 638bf215546Sopenharmony_ci util_range_add(&buf->b.b, &buf->valid_buffer_range, 0, templ->width0); 639bf215546Sopenharmony_ci util_range_add(&buf->b.b, &buf->b.valid_buffer_range, 0, templ->width0); 640bf215546Sopenharmony_ci 641bf215546Sopenharmony_ci buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids); 642bf215546Sopenharmony_ci 643bf215546Sopenharmony_ci /* Convert a user pointer to a buffer. */ 644bf215546Sopenharmony_ci buf->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0, 0); 645bf215546Sopenharmony_ci if (!buf->buf) { 646bf215546Sopenharmony_ci si_resource_destroy(screen, &buf->b.b); 647bf215546Sopenharmony_ci return NULL; 648bf215546Sopenharmony_ci } 649bf215546Sopenharmony_ci 650bf215546Sopenharmony_ci buf->gpu_address = ws->buffer_get_virtual_address(buf->buf); 651bf215546Sopenharmony_ci buf->memory_usage_kb = templ->width0 / 1024; 652bf215546Sopenharmony_ci return &buf->b.b; 653bf215546Sopenharmony_ci} 654bf215546Sopenharmony_ci 655bf215546Sopenharmony_cistruct pipe_resource *si_buffer_from_winsys_buffer(struct pipe_screen *screen, 656bf215546Sopenharmony_ci const struct pipe_resource *templ, 657bf215546Sopenharmony_ci struct pb_buffer *imported_buf, 658bf215546Sopenharmony_ci uint64_t offset) 659bf215546Sopenharmony_ci{ 660bf215546Sopenharmony_ci if (offset + templ->width0 > imported_buf->size) 661bf215546Sopenharmony_ci return NULL; 662bf215546Sopenharmony_ci 663bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 664bf215546Sopenharmony_ci struct si_resource *res = si_alloc_buffer_struct(screen, templ, false); 665bf215546Sopenharmony_ci 666bf215546Sopenharmony_ci if (!res) 667bf215546Sopenharmony_ci return NULL; 668bf215546Sopenharmony_ci 669bf215546Sopenharmony_ci enum radeon_bo_domain domains = sscreen->ws->buffer_get_initial_domain(imported_buf); 670bf215546Sopenharmony_ci 671bf215546Sopenharmony_ci /* Get or guess the BO flags. */ 672bf215546Sopenharmony_ci unsigned flags = RADEON_FLAG_NO_SUBALLOC; 673bf215546Sopenharmony_ci 674bf215546Sopenharmony_ci if (sscreen->ws->buffer_get_flags) 675bf215546Sopenharmony_ci res->flags |= sscreen->ws->buffer_get_flags(imported_buf); 676bf215546Sopenharmony_ci else 677bf215546Sopenharmony_ci flags |= RADEON_FLAG_GTT_WC; /* unknown flags, guess them */ 678bf215546Sopenharmony_ci 679bf215546Sopenharmony_ci /* Deduce the usage. */ 680bf215546Sopenharmony_ci switch (domains) { 681bf215546Sopenharmony_ci case RADEON_DOMAIN_VRAM: 682bf215546Sopenharmony_ci case RADEON_DOMAIN_VRAM_GTT: 683bf215546Sopenharmony_ci res->b.b.usage = PIPE_USAGE_DEFAULT; 684bf215546Sopenharmony_ci break; 685bf215546Sopenharmony_ci 686bf215546Sopenharmony_ci default: 687bf215546Sopenharmony_ci /* Other values are interpreted as GTT. */ 688bf215546Sopenharmony_ci domains = RADEON_DOMAIN_GTT; 689bf215546Sopenharmony_ci 690bf215546Sopenharmony_ci if (flags & RADEON_FLAG_GTT_WC) 691bf215546Sopenharmony_ci res->b.b.usage = PIPE_USAGE_STREAM; 692bf215546Sopenharmony_ci else 693bf215546Sopenharmony_ci res->b.b.usage = PIPE_USAGE_STAGING; 694bf215546Sopenharmony_ci } 695bf215546Sopenharmony_ci 696bf215546Sopenharmony_ci si_init_resource_fields(sscreen, res, imported_buf->size, 697bf215546Sopenharmony_ci 1 << imported_buf->alignment_log2); 698bf215546Sopenharmony_ci 699bf215546Sopenharmony_ci res->b.is_shared = true; 700bf215546Sopenharmony_ci res->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids); 701bf215546Sopenharmony_ci res->buf = imported_buf; 702bf215546Sopenharmony_ci res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf) + offset; 703bf215546Sopenharmony_ci res->domains = domains; 704bf215546Sopenharmony_ci res->flags = flags; 705bf215546Sopenharmony_ci 706bf215546Sopenharmony_ci if (res->flags & RADEON_FLAG_NO_CPU_ACCESS) 707bf215546Sopenharmony_ci res->b.b.flags |= PIPE_RESOURCE_FLAG_UNMAPPABLE; 708bf215546Sopenharmony_ci 709bf215546Sopenharmony_ci util_range_add(&res->b.b, &res->valid_buffer_range, 0, templ->width0); 710bf215546Sopenharmony_ci util_range_add(&res->b.b, &res->b.valid_buffer_range, 0, templ->width0); 711bf215546Sopenharmony_ci 712bf215546Sopenharmony_ci return &res->b.b; 713bf215546Sopenharmony_ci} 714bf215546Sopenharmony_ci 715bf215546Sopenharmony_cistatic struct pipe_resource *si_resource_create(struct pipe_screen *screen, 716bf215546Sopenharmony_ci const struct pipe_resource *templ) 717bf215546Sopenharmony_ci{ 718bf215546Sopenharmony_ci if (templ->target == PIPE_BUFFER) { 719bf215546Sopenharmony_ci return si_buffer_create(screen, templ, 256); 720bf215546Sopenharmony_ci } else { 721bf215546Sopenharmony_ci return si_texture_create(screen, templ); 722bf215546Sopenharmony_ci } 723bf215546Sopenharmony_ci} 724bf215546Sopenharmony_ci 725bf215546Sopenharmony_cistatic bool si_buffer_commit(struct si_context *ctx, struct si_resource *res, 726bf215546Sopenharmony_ci struct pipe_box *box, bool commit) 727bf215546Sopenharmony_ci{ 728bf215546Sopenharmony_ci return ctx->ws->buffer_commit(ctx->ws, res->buf, box->x, box->width, commit); 729bf215546Sopenharmony_ci} 730bf215546Sopenharmony_ci 731bf215546Sopenharmony_cistatic bool si_resource_commit(struct pipe_context *pctx, struct pipe_resource *resource, 732bf215546Sopenharmony_ci unsigned level, struct pipe_box *box, bool commit) 733bf215546Sopenharmony_ci{ 734bf215546Sopenharmony_ci struct si_context *ctx = (struct si_context *)pctx; 735bf215546Sopenharmony_ci struct si_resource *res = si_resource(resource); 736bf215546Sopenharmony_ci 737bf215546Sopenharmony_ci /* 738bf215546Sopenharmony_ci * Since buffer commitment changes cannot be pipelined, we need to 739bf215546Sopenharmony_ci * (a) flush any pending commands that refer to the buffer we're about 740bf215546Sopenharmony_ci * to change, and 741bf215546Sopenharmony_ci * (b) wait for threaded submit to finish, including those that were 742bf215546Sopenharmony_ci * triggered by some other, earlier operation. 743bf215546Sopenharmony_ci */ 744bf215546Sopenharmony_ci if (radeon_emitted(&ctx->gfx_cs, ctx->initial_gfx_cs_size) && 745bf215546Sopenharmony_ci ctx->ws->cs_is_buffer_referenced(&ctx->gfx_cs, res->buf, RADEON_USAGE_READWRITE)) { 746bf215546Sopenharmony_ci si_flush_gfx_cs(ctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL); 747bf215546Sopenharmony_ci } 748bf215546Sopenharmony_ci ctx->ws->cs_sync_flush(&ctx->gfx_cs); 749bf215546Sopenharmony_ci 750bf215546Sopenharmony_ci if (resource->target == PIPE_BUFFER) 751bf215546Sopenharmony_ci return si_buffer_commit(ctx, res, box, commit); 752bf215546Sopenharmony_ci else 753bf215546Sopenharmony_ci return si_texture_commit(ctx, res, level, box, commit); 754bf215546Sopenharmony_ci} 755bf215546Sopenharmony_ci 756bf215546Sopenharmony_civoid si_init_screen_buffer_functions(struct si_screen *sscreen) 757bf215546Sopenharmony_ci{ 758bf215546Sopenharmony_ci sscreen->b.resource_create = si_resource_create; 759bf215546Sopenharmony_ci sscreen->b.resource_destroy = si_resource_destroy; 760bf215546Sopenharmony_ci sscreen->b.resource_from_user_memory = si_buffer_from_user_memory; 761bf215546Sopenharmony_ci} 762bf215546Sopenharmony_ci 763bf215546Sopenharmony_civoid si_init_buffer_functions(struct si_context *sctx) 764bf215546Sopenharmony_ci{ 765bf215546Sopenharmony_ci sctx->b.invalidate_resource = si_invalidate_resource; 766bf215546Sopenharmony_ci sctx->b.buffer_map = si_buffer_transfer_map; 767bf215546Sopenharmony_ci sctx->b.transfer_flush_region = si_buffer_flush_region; 768bf215546Sopenharmony_ci sctx->b.buffer_unmap = si_buffer_transfer_unmap; 769bf215546Sopenharmony_ci sctx->b.texture_subdata = u_default_texture_subdata; 770bf215546Sopenharmony_ci sctx->b.buffer_subdata = si_buffer_subdata; 771bf215546Sopenharmony_ci sctx->b.resource_commit = si_resource_commit; 772bf215546Sopenharmony_ci} 773