2 * Copyright 2014, 2015 Red Hat.
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:
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
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.
23 #include "util/u_format.h"
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"
30 bool virgl_res_needs_flush(struct virgl_context
*vctx
,
31 struct virgl_transfer
*trans
)
33 struct virgl_screen
*vs
= virgl_screen(vctx
->base
.screen
);
34 struct virgl_resource
*res
= virgl_resource(trans
->base
.resource
);
36 if (trans
->base
.usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)
38 if (!vs
->vws
->res_is_referenced(vs
->vws
, vctx
->cbuf
, res
->hw_res
))
40 if (res
->clean_mask
& (1 << trans
->base
.level
)) {
41 if (vctx
->num_draws
== 0 && vctx
->num_compute
== 0)
43 if (!virgl_transfer_queue_is_queued(&vctx
->queue
, trans
))
50 bool virgl_res_needs_readback(struct virgl_context
*vctx
,
51 struct virgl_resource
*res
,
52 unsigned usage
, unsigned level
)
55 if (res
->clean_mask
& (1 << level
))
57 else if (usage
& PIPE_TRANSFER_DISCARD_RANGE
)
59 else if ((usage
& (PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_FLUSH_EXPLICIT
)) ==
60 (PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_FLUSH_EXPLICIT
))
65 static struct pipe_resource
*virgl_resource_create(struct pipe_screen
*screen
,
66 const struct pipe_resource
*templ
)
69 struct virgl_screen
*vs
= virgl_screen(screen
);
70 struct virgl_resource
*res
= CALLOC_STRUCT(virgl_resource
);
73 res
->u
.b
.screen
= &vs
->base
;
74 pipe_reference_init(&res
->u
.b
.reference
, 1);
75 vbind
= pipe_to_virgl_bind(vs
, templ
->bind
);
76 virgl_resource_layout(&res
->u
.b
, &res
->metadata
);
77 res
->hw_res
= vs
->vws
->resource_create(vs
->vws
, templ
->target
,
85 res
->metadata
.total_size
);
91 res
->clean_mask
= (1 << VR_MAX_TEXTURE_2D_LEVELS
) - 1;
93 if (templ
->target
== PIPE_BUFFER
)
94 virgl_buffer_init(res
);
96 virgl_texture_init(res
);
102 static struct pipe_resource
*virgl_resource_from_handle(struct pipe_screen
*screen
,
103 const struct pipe_resource
*templ
,
104 struct winsys_handle
*whandle
,
107 struct virgl_screen
*vs
= virgl_screen(screen
);
108 if (templ
->target
== PIPE_BUFFER
)
111 struct virgl_resource
*res
= CALLOC_STRUCT(virgl_resource
);
113 res
->u
.b
.screen
= &vs
->base
;
114 pipe_reference_init(&res
->u
.b
.reference
, 1);
116 res
->hw_res
= vs
->vws
->resource_create_from_handle(vs
->vws
, whandle
);
122 virgl_texture_init(res
);
127 void virgl_init_screen_resource_functions(struct pipe_screen
*screen
)
129 screen
->resource_create
= virgl_resource_create
;
130 screen
->resource_from_handle
= virgl_resource_from_handle
;
131 screen
->resource_get_handle
= u_resource_get_handle_vtbl
;
132 screen
->resource_destroy
= u_resource_destroy_vtbl
;
135 static bool virgl_buffer_transfer_extend(struct pipe_context
*ctx
,
136 struct pipe_resource
*resource
,
138 const struct pipe_box
*box
,
141 struct virgl_context
*vctx
= virgl_context(ctx
);
142 struct virgl_resource
*vbuf
= virgl_resource(resource
);
143 struct virgl_transfer dummy_trans
= { 0 };
145 struct virgl_transfer
*queued
;
148 * Attempts to short circuit the entire process of mapping and unmapping
149 * a resource if there is an existing transfer that can be extended.
150 * Pessimestically falls back if a flush is required.
152 dummy_trans
.base
.resource
= resource
;
153 dummy_trans
.base
.usage
= usage
;
154 dummy_trans
.base
.box
= *box
;
155 dummy_trans
.base
.stride
= vbuf
->metadata
.stride
[0];
156 dummy_trans
.base
.layer_stride
= vbuf
->metadata
.layer_stride
[0];
157 dummy_trans
.offset
= box
->x
;
159 flush
= virgl_res_needs_flush(vctx
, &dummy_trans
);
163 queued
= virgl_transfer_queue_extend(&vctx
->queue
, &dummy_trans
);
164 if (!queued
|| !queued
->hw_res_map
)
167 memcpy(queued
->hw_res_map
+ dummy_trans
.offset
, data
, box
->width
);
172 static void virgl_buffer_subdata(struct pipe_context
*pipe
,
173 struct pipe_resource
*resource
,
174 unsigned usage
, unsigned offset
,
175 unsigned size
, const void *data
)
179 assert(!(usage
& PIPE_TRANSFER_READ
));
181 /* the write flag is implicit by the nature of buffer_subdata */
182 usage
|= PIPE_TRANSFER_WRITE
;
184 if (offset
== 0 && size
== resource
->width0
)
185 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
187 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
189 u_box_1d(offset
, size
, &box
);
191 if (usage
& PIPE_TRANSFER_DISCARD_RANGE
&&
192 virgl_buffer_transfer_extend(pipe
, resource
, usage
, &box
, data
))
195 if (resource
->width0
>= getpagesize())
196 u_default_buffer_subdata(pipe
, resource
, usage
, offset
, size
, data
);
198 virgl_transfer_inline_write(pipe
, resource
, 0, usage
, &box
, data
, 0, 0);
201 void virgl_init_context_resource_functions(struct pipe_context
*ctx
)
203 ctx
->transfer_map
= u_transfer_map_vtbl
;
204 ctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
205 ctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
206 ctx
->buffer_subdata
= virgl_buffer_subdata
;
207 ctx
->texture_subdata
= u_default_texture_subdata
;
210 void virgl_resource_layout(struct pipe_resource
*pt
,
211 struct virgl_resource_metadata
*metadata
)
213 unsigned level
, nblocksy
;
214 unsigned width
= pt
->width0
;
215 unsigned height
= pt
->height0
;
216 unsigned depth
= pt
->depth0
;
217 unsigned buffer_size
= 0;
219 for (level
= 0; level
<= pt
->last_level
; level
++) {
222 if (pt
->target
== PIPE_TEXTURE_CUBE
)
224 else if (pt
->target
== PIPE_TEXTURE_3D
)
227 slices
= pt
->array_size
;
229 nblocksy
= util_format_get_nblocksy(pt
->format
, height
);
230 metadata
->stride
[level
] = util_format_get_stride(pt
->format
, width
);
231 metadata
->layer_stride
[level
] = nblocksy
* metadata
->stride
[level
];
232 metadata
->level_offset
[level
] = buffer_size
;
234 buffer_size
+= slices
* metadata
->layer_stride
[level
];
236 width
= u_minify(width
, 1);
237 height
= u_minify(height
, 1);
238 depth
= u_minify(depth
, 1);
241 if (pt
->nr_samples
<= 1)
242 metadata
->total_size
= buffer_size
;
243 else /* don't create guest backing store for MSAA */
244 metadata
->total_size
= 0;
247 struct virgl_transfer
*
248 virgl_resource_create_transfer(struct slab_child_pool
*pool
,
249 struct pipe_resource
*pres
,
250 const struct virgl_resource_metadata
*metadata
,
251 unsigned level
, unsigned usage
,
252 const struct pipe_box
*box
)
254 struct virgl_transfer
*trans
;
255 enum pipe_format format
= pres
->format
;
256 const unsigned blocksy
= box
->y
/ util_format_get_blockheight(format
);
257 const unsigned blocksx
= box
->x
/ util_format_get_blockwidth(format
);
259 unsigned offset
= metadata
->level_offset
[level
];
260 if (pres
->target
== PIPE_TEXTURE_CUBE
||
261 pres
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
262 pres
->target
== PIPE_TEXTURE_3D
||
263 pres
->target
== PIPE_TEXTURE_2D_ARRAY
) {
264 offset
+= box
->z
* metadata
->layer_stride
[level
];
266 else if (pres
->target
== PIPE_TEXTURE_1D_ARRAY
) {
267 offset
+= box
->z
* metadata
->stride
[level
];
269 } else if (pres
->target
== PIPE_BUFFER
) {
270 assert(box
->y
== 0 && box
->z
== 0);
275 offset
+= blocksy
* metadata
->stride
[level
];
276 offset
+= blocksx
* util_format_get_blocksize(format
);
278 trans
= slab_alloc(pool
);
282 trans
->base
.resource
= pres
;
283 trans
->base
.level
= level
;
284 trans
->base
.usage
= usage
;
285 trans
->base
.box
= *box
;
286 trans
->base
.stride
= metadata
->stride
[level
];
287 trans
->base
.layer_stride
= metadata
->layer_stride
[level
];
288 trans
->offset
= offset
;
289 util_range_init(&trans
->range
);
291 if (trans
->base
.resource
->target
!= PIPE_TEXTURE_3D
&&
292 trans
->base
.resource
->target
!= PIPE_TEXTURE_CUBE
&&
293 trans
->base
.resource
->target
!= PIPE_TEXTURE_1D_ARRAY
&&
294 trans
->base
.resource
->target
!= PIPE_TEXTURE_2D_ARRAY
&&
295 trans
->base
.resource
->target
!= PIPE_TEXTURE_CUBE_ARRAY
)
298 trans
->l_stride
= trans
->base
.layer_stride
;
303 void virgl_resource_destroy_transfer(struct slab_child_pool
*pool
,
304 struct virgl_transfer
*trans
)
306 util_range_destroy(&trans
->range
);
307 slab_free(pool
, trans
);
310 void virgl_resource_destroy(struct pipe_screen
*screen
,
311 struct pipe_resource
*resource
)
313 struct virgl_screen
*vs
= virgl_screen(screen
);
314 struct virgl_resource
*res
= virgl_resource(resource
);
315 vs
->vws
->resource_unref(vs
->vws
, res
->hw_res
);
319 boolean
virgl_resource_get_handle(struct pipe_screen
*screen
,
320 struct pipe_resource
*resource
,
321 struct winsys_handle
*whandle
)
323 struct virgl_screen
*vs
= virgl_screen(screen
);
324 struct virgl_resource
*res
= virgl_resource(resource
);
326 if (res
->u
.b
.target
== PIPE_BUFFER
)
329 return vs
->vws
->resource_get_handle(vs
->vws
, res
->hw_res
,
330 res
->metadata
.stride
[0],
334 void virgl_resource_dirty(struct virgl_resource
*res
, uint32_t level
)
337 if (res
->u
.b
.target
== PIPE_BUFFER
)
338 res
->clean_mask
&= ~1;
340 res
->clean_mask
&= ~(1 << level
);