virgl: Clear the valid buffer range when possible
[mesa.git] / src / gallium / drivers / virgl / virgl_buffer.c
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_encode.h"
28 #include "virgl_resource.h"
29 #include "virgl_screen.h"
30
31 static void *virgl_buffer_transfer_map(struct pipe_context *ctx,
32 struct pipe_resource *resource,
33 unsigned level,
34 unsigned usage,
35 const struct pipe_box *box,
36 struct pipe_transfer **transfer)
37 {
38 struct virgl_context *vctx = virgl_context(ctx);
39 struct virgl_screen *vs = virgl_screen(ctx->screen);
40 struct virgl_resource *vbuf = virgl_resource(resource);
41 struct virgl_transfer *trans;
42 enum virgl_transfer_map_type map_type;
43 void *map_addr;
44
45 trans = virgl_resource_create_transfer(vctx, resource,
46 &vbuf->metadata, level, usage, box);
47
48 map_type = virgl_resource_transfer_prepare(vctx, trans);
49 switch (map_type) {
50 case VIRGL_TRANSFER_MAP_REALLOC:
51 if (!virgl_resource_realloc(vctx, vbuf)) {
52 map_addr = NULL;
53 break;
54 }
55 vs->vws->resource_reference(vs->vws, &trans->hw_res, vbuf->hw_res);
56 /* fall through */
57 case VIRGL_TRANSFER_MAP_HW_RES:
58 trans->hw_res_map = vs->vws->resource_map(vs->vws, vbuf->hw_res);
59 if (trans->hw_res_map)
60 map_addr = trans->hw_res_map + trans->offset;
61 else
62 map_addr = NULL;
63 break;
64 case VIRGL_TRANSFER_MAP_STAGING:
65 map_addr = virgl_staging_map(vctx, trans);
66 /* Copy transfers don't make use of hw_res_map at the moment. */
67 trans->hw_res_map = NULL;
68 break;
69 case VIRGL_TRANSFER_MAP_ERROR:
70 default:
71 trans->hw_res_map = NULL;
72 map_addr = NULL;
73 break;
74 }
75
76 if (!map_addr) {
77 virgl_resource_destroy_transfer(vctx, trans);
78 return NULL;
79 }
80
81 /* For the checks below to be able to use 'usage', we assume that
82 * transfer preparation doesn't affect the usage.
83 */
84 assert(usage == trans->base.usage);
85
86 /* If we are doing a whole resource discard with a hw_res map, the buffer
87 * storage can now be considered unused and we don't care about previous
88 * contents. We can thus mark the storage as uninitialized, but only if the
89 * buffer is not host writable (in which case we can't clear the valid
90 * range, since that would result in missed readbacks in future transfers).
91 * We only do this for VIRGL_TRANSFER_MAP_HW_RES, since for
92 * VIRGL_TRANSFER_MAP_REALLOC we already take care of the buffer range when
93 * reallocating and rebinding, and VIRGL_TRANSFER_MAP_STAGING is not
94 * currently used for whole resource discards.
95 */
96 if (map_type == VIRGL_TRANSFER_MAP_HW_RES &&
97 (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) &&
98 (vbuf->clean_mask & 1)) {
99 util_range_set_empty(&vbuf->valid_buffer_range);
100 }
101
102 if (usage & PIPE_TRANSFER_WRITE)
103 util_range_add(&vbuf->valid_buffer_range, box->x, box->x + box->width);
104
105 *transfer = &trans->base;
106 return map_addr;
107 }
108
109 static void virgl_buffer_transfer_unmap(struct pipe_context *ctx,
110 struct pipe_transfer *transfer)
111 {
112 struct virgl_context *vctx = virgl_context(ctx);
113 struct virgl_transfer *trans = virgl_transfer(transfer);
114
115 if (trans->base.usage & PIPE_TRANSFER_WRITE) {
116 if (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) {
117 if (trans->range.end <= trans->range.start) {
118 virgl_resource_destroy_transfer(vctx, trans);
119 return;
120 }
121
122 transfer->box.x += trans->range.start;
123 transfer->box.width = trans->range.end - trans->range.start;
124 trans->offset = transfer->box.x;
125 }
126
127 if (trans->copy_src_hw_res) {
128 virgl_encode_copy_transfer(vctx, trans);
129 virgl_resource_destroy_transfer(vctx, trans);
130 } else {
131 virgl_transfer_queue_unmap(&vctx->queue, trans);
132 }
133 } else
134 virgl_resource_destroy_transfer(vctx, trans);
135 }
136
137 static void virgl_buffer_transfer_flush_region(struct pipe_context *ctx,
138 struct pipe_transfer *transfer,
139 const struct pipe_box *box)
140 {
141 struct virgl_transfer *trans = virgl_transfer(transfer);
142
143 /*
144 * FIXME: This is not optimal. For example,
145 *
146 * glMapBufferRange(.., 0, 100, GL_MAP_FLUSH_EXPLICIT_BIT)
147 * glFlushMappedBufferRange(.., 25, 30)
148 * glFlushMappedBufferRange(.., 65, 70)
149 *
150 * We'll end up flushing 25 --> 70.
151 */
152 util_range_add(&trans->range, box->x, box->x + box->width);
153 }
154
155 static const struct u_resource_vtbl virgl_buffer_vtbl =
156 {
157 u_default_resource_get_handle, /* get_handle */
158 virgl_resource_destroy, /* resource_destroy */
159 virgl_buffer_transfer_map, /* transfer_map */
160 virgl_buffer_transfer_flush_region, /* transfer_flush_region */
161 virgl_buffer_transfer_unmap, /* transfer_unmap */
162 };
163
164 void virgl_buffer_init(struct virgl_resource *res)
165 {
166 res->u.vtbl = &virgl_buffer_vtbl;
167 }