virgl_buffer.c revision 01e04c3f
1/* 2 * Copyright 2014, 2015 Red Hat. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 * USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24#include "util/u_inlines.h" 25#include "util/u_memory.h" 26#include "virgl_context.h" 27#include "virgl_resource.h" 28#include "virgl_screen.h" 29 30static void virgl_buffer_destroy(struct pipe_screen *screen, 31 struct pipe_resource *buf) 32{ 33 struct virgl_screen *vs = virgl_screen(screen); 34 struct virgl_buffer *vbuf = virgl_buffer(buf); 35 36 util_range_destroy(&vbuf->valid_buffer_range); 37 vs->vws->resource_unref(vs->vws, vbuf->base.hw_res); 38 FREE(vbuf); 39} 40 41static void *virgl_buffer_transfer_map(struct pipe_context *ctx, 42 struct pipe_resource *resource, 43 unsigned level, 44 unsigned usage, 45 const struct pipe_box *box, 46 struct pipe_transfer **transfer) 47{ 48 struct virgl_context *vctx = virgl_context(ctx); 49 struct virgl_screen *vs = virgl_screen(ctx->screen); 50 struct virgl_buffer *vbuf = virgl_buffer(resource); 51 struct virgl_transfer *trans; 52 void *ptr; 53 bool readback; 54 uint32_t offset; 55 bool doflushwait = false; 56 57 if ((usage & PIPE_TRANSFER_READ) && (vbuf->on_list == TRUE)) 58 doflushwait = true; 59 else 60 doflushwait = virgl_res_needs_flush_wait(vctx, &vbuf->base, usage); 61 62 if (doflushwait) 63 ctx->flush(ctx, NULL, 0); 64 65 trans = slab_alloc(&vctx->texture_transfer_pool); 66 if (!trans) 67 return NULL; 68 69 trans->base.resource = resource; 70 trans->base.level = level; 71 trans->base.usage = usage; 72 trans->base.box = *box; 73 trans->base.stride = 0; 74 trans->base.layer_stride = 0; 75 76 offset = box->x; 77 78 readback = virgl_res_needs_readback(vctx, &vbuf->base, usage); 79 if (readback) 80 vs->vws->transfer_get(vs->vws, vbuf->base.hw_res, box, trans->base.stride, trans->base.layer_stride, offset, level); 81 82 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) 83 doflushwait = true; 84 85 if (doflushwait || readback) 86 vs->vws->resource_wait(vs->vws, vbuf->base.hw_res); 87 88 ptr = vs->vws->resource_map(vs->vws, vbuf->base.hw_res); 89 if (!ptr) { 90 return NULL; 91 } 92 93 trans->offset = offset; 94 *transfer = &trans->base; 95 96 return ptr + trans->offset; 97} 98 99static void virgl_buffer_transfer_unmap(struct pipe_context *ctx, 100 struct pipe_transfer *transfer) 101{ 102 struct virgl_context *vctx = virgl_context(ctx); 103 struct virgl_transfer *trans = virgl_transfer(transfer); 104 struct virgl_buffer *vbuf = virgl_buffer(transfer->resource); 105 106 if (trans->base.usage & PIPE_TRANSFER_WRITE) { 107 if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) { 108 struct virgl_screen *vs = virgl_screen(ctx->screen); 109 vctx->num_transfers++; 110 vs->vws->transfer_put(vs->vws, vbuf->base.hw_res, 111 &transfer->box, trans->base.stride, trans->base.layer_stride, trans->offset, transfer->level); 112 113 } 114 } 115 116 slab_free(&vctx->texture_transfer_pool, trans); 117} 118 119static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx, 120 struct pipe_transfer *transfer, 121 const struct pipe_box *box) 122{ 123 struct virgl_context *vctx = virgl_context(ctx); 124 struct virgl_buffer *vbuf = virgl_buffer(transfer->resource); 125 126 if (!vbuf->on_list) { 127 struct pipe_resource *res = NULL; 128 129 list_addtail(&vbuf->flush_list, &vctx->to_flush_bufs); 130 vbuf->on_list = TRUE; 131 pipe_resource_reference(&res, &vbuf->base.u.b); 132 } 133 134 util_range_add(&vbuf->valid_buffer_range, transfer->box.x + box->x, 135 transfer->box.x + box->x + box->width); 136 137 vbuf->base.clean = FALSE; 138} 139 140static const struct u_resource_vtbl virgl_buffer_vtbl = 141{ 142 u_default_resource_get_handle, /* get_handle */ 143 virgl_buffer_destroy, /* resource_destroy */ 144 virgl_buffer_transfer_map, /* transfer_map */ 145 virgl_buffer_transfer_flush_region, /* transfer_flush_region */ 146 virgl_buffer_transfer_unmap, /* transfer_unmap */ 147}; 148 149struct pipe_resource *virgl_buffer_create(struct virgl_screen *vs, 150 const struct pipe_resource *template) 151{ 152 struct virgl_buffer *buf; 153 uint32_t size; 154 uint32_t vbind; 155 buf = CALLOC_STRUCT(virgl_buffer); 156 buf->base.clean = TRUE; 157 buf->base.u.b = *template; 158 buf->base.u.b.screen = &vs->base; 159 buf->base.u.vtbl = &virgl_buffer_vtbl; 160 pipe_reference_init(&buf->base.u.b.reference, 1); 161 util_range_init(&buf->valid_buffer_range); 162 163 vbind = pipe_to_virgl_bind(template->bind); 164 size = template->width0; 165 166 /* SSBOs and texture buffers can written to by host compute shaders. */ 167 if (vbind == VIRGL_BIND_SHADER_BUFFER || vbind == VIRGL_BIND_SAMPLER_VIEW) 168 buf->base.clean = FALSE; 169 buf->base.hw_res = vs->vws->resource_create(vs->vws, template->target, template->format, vbind, template->width0, 1, 1, 1, 0, 0, size); 170 171 util_range_set_empty(&buf->valid_buffer_range); 172 return &buf->base.u.b; 173} 174