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"
27 #include "virgl_context.h"
28 #include "virgl_resource.h"
29 #include "virgl_screen.h"
31 static void virgl_copy_region_with_blit(struct pipe_context
*pipe
,
32 struct pipe_resource
*dst
,
34 const struct pipe_box
*dst_box
,
35 struct pipe_resource
*src
,
37 const struct pipe_box
*src_box
)
39 struct pipe_blit_info blit
;
41 memset(&blit
, 0, sizeof(blit
));
42 blit
.src
.resource
= src
;
43 blit
.src
.format
= src
->format
;
44 blit
.src
.level
= src_level
;
45 blit
.src
.box
= *src_box
;
46 blit
.dst
.resource
= dst
;
47 blit
.dst
.format
= dst
->format
;
48 blit
.dst
.level
= dst_level
;
49 blit
.dst
.box
.x
= dst_box
->x
;
50 blit
.dst
.box
.y
= dst_box
->y
;
51 blit
.dst
.box
.z
= dst_box
->z
;
52 blit
.dst
.box
.width
= dst_box
->width
;
53 blit
.dst
.box
.height
= dst_box
->height
;
54 blit
.dst
.box
.depth
= dst_box
->depth
;
55 blit
.mask
= util_format_get_mask(src
->format
) &
56 util_format_get_mask(dst
->format
);
57 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
60 pipe
->blit(pipe
, &blit
);
64 static unsigned temp_bind(unsigned orig
)
66 unsigned warn
= ~(PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
|
67 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DISPLAY_TARGET
);
69 debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
72 return orig
& (PIPE_BIND_DEPTH_STENCIL
| PIPE_BIND_RENDER_TARGET
);
75 static void virgl_init_temp_resource_from_box(struct pipe_resource
*res
,
76 struct pipe_resource
*orig
,
77 const struct pipe_box
*box
,
78 unsigned level
, unsigned flags
,
81 memset(res
, 0, sizeof(*res
));
82 res
->bind
= temp_bind(orig
->bind
);
84 res
->width0
= box
->width
;
85 res
->height0
= box
->height
;
88 res
->usage
= PIPE_USAGE_STAGING
;
91 /* We must set the correct texture target and dimensions for a 3D box. */
92 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0)
93 res
->target
= orig
->target
;
95 res
->target
= PIPE_TEXTURE_2D
;
97 if (res
->target
!= PIPE_BUFFER
)
98 res
->bind
= PIPE_BIND_RENDER_TARGET
;
100 switch (res
->target
) {
101 case PIPE_TEXTURE_1D_ARRAY
:
102 case PIPE_TEXTURE_2D_ARRAY
:
103 case PIPE_TEXTURE_CUBE_ARRAY
:
104 res
->array_size
= box
->depth
;
106 case PIPE_TEXTURE_3D
:
107 res
->depth0
= box
->depth
;
114 static void *texture_transfer_map_plain(struct pipe_context
*ctx
,
115 struct pipe_resource
*resource
,
118 const struct pipe_box
*box
,
119 struct pipe_transfer
**transfer
)
121 struct virgl_context
*vctx
= virgl_context(ctx
);
122 struct virgl_winsys
*vws
= virgl_screen(ctx
->screen
)->vws
;
123 struct virgl_resource
*vtex
= virgl_resource(resource
);
124 struct virgl_transfer
*trans
;
125 enum virgl_transfer_map_type map_type
;
127 trans
= virgl_resource_create_transfer(&vctx
->transfer_pool
, resource
,
128 &vtex
->metadata
, level
, usage
, box
);
129 trans
->resolve_transfer
= NULL
;
131 assert(resource
->nr_samples
<= 1);
133 map_type
= virgl_resource_transfer_prepare(vctx
, trans
);
135 case VIRGL_TRANSFER_MAP_HW_RES
:
136 trans
->hw_res_map
= vws
->resource_map(vws
, vtex
->hw_res
);
138 case VIRGL_TRANSFER_MAP_ERROR
:
140 trans
->hw_res_map
= NULL
;
144 if (!trans
->hw_res_map
) {
145 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
149 *transfer
= &trans
->base
;
150 return trans
->hw_res_map
+ trans
->offset
;
153 static void *texture_transfer_map_resolve(struct pipe_context
*ctx
,
154 struct pipe_resource
*resource
,
157 const struct pipe_box
*box
,
158 struct pipe_transfer
**transfer
)
160 struct virgl_context
*vctx
= virgl_context(ctx
);
161 struct virgl_resource
*vtex
= virgl_resource(resource
);
162 struct pipe_resource templ
, *resolve_tmp
;
163 struct virgl_transfer
*trans
;
165 trans
= virgl_resource_create_transfer(&vctx
->transfer_pool
, resource
,
166 &vtex
->metadata
, level
, usage
, box
);
170 enum pipe_format fmt
= resource
->format
;
171 if (!virgl_has_readback_format(ctx
->screen
, fmt
)) {
172 if (util_format_fits_8unorm(util_format_description(fmt
)))
173 fmt
= PIPE_FORMAT_R8G8B8A8_UNORM
;
174 else if (util_format_is_pure_sint(fmt
))
175 fmt
= PIPE_FORMAT_R32G32B32A32_SINT
;
176 else if (util_format_is_pure_uint(fmt
))
177 fmt
= PIPE_FORMAT_R32G32B32A32_UINT
;
179 fmt
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
180 assert(virgl_has_readback_format(ctx
->screen
, fmt
));
183 struct pipe_box dst_box
= *box
;
184 dst_box
.x
= dst_box
.y
= dst_box
.z
= 0;
185 if (usage
& PIPE_TRANSFER_READ
) {
186 /* readback should scale to the block size */
187 dst_box
.width
= align(dst_box
.width
,
188 util_format_get_blockwidth(resource
->format
));
189 dst_box
.height
= align(dst_box
.height
,
190 util_format_get_blockheight(resource
->format
));
193 virgl_init_temp_resource_from_box(&templ
, resource
, &dst_box
, level
, 0, fmt
);
195 resolve_tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
199 if (usage
& PIPE_TRANSFER_READ
) {
200 virgl_copy_region_with_blit(ctx
, resolve_tmp
, 0, &dst_box
, resource
,
202 ctx
->flush(ctx
, NULL
, 0);
205 void *ptr
= texture_transfer_map_plain(ctx
, resolve_tmp
, 0, usage
, &dst_box
,
206 &trans
->resolve_transfer
);
210 *transfer
= &trans
->base
;
211 if (fmt
== resource
->format
) {
212 trans
->base
.stride
= trans
->resolve_transfer
->stride
;
213 trans
->base
.layer_stride
= trans
->resolve_transfer
->layer_stride
;
216 if (usage
& PIPE_TRANSFER_READ
) {
217 struct virgl_winsys
*vws
= virgl_screen(ctx
->screen
)->vws
;
219 ptr
= vws
->resource_map(vws
, vtex
->hw_res
);
223 if (!util_format_translate_3d(resource
->format
,
224 ptr
+ vtex
->metadata
.level_offset
[level
],
226 trans
->base
.layer_stride
,
227 box
->x
, box
->y
, box
->z
,
230 trans
->resolve_transfer
->stride
,
231 trans
->resolve_transfer
->layer_stride
,
236 debug_printf("failed to translate format %s to %s\n",
237 util_format_short_name(fmt
),
238 util_format_short_name(resource
->format
));
243 if ((usage
& PIPE_TRANSFER_WRITE
) == 0)
244 pipe_resource_reference(&trans
->resolve_transfer
->resource
, NULL
);
246 return ptr
+ trans
->offset
;
250 pipe_resource_reference(&resolve_tmp
, NULL
);
251 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
255 static bool needs_resolve(struct pipe_screen
*screen
,
256 struct pipe_resource
*resource
, unsigned usage
)
258 if (resource
->nr_samples
> 1)
261 if (usage
& PIPE_TRANSFER_READ
)
262 return !util_format_is_depth_or_stencil(resource
->format
) &&
263 !virgl_has_readback_format(screen
, resource
->format
);
268 static void *virgl_texture_transfer_map(struct pipe_context
*ctx
,
269 struct pipe_resource
*resource
,
272 const struct pipe_box
*box
,
273 struct pipe_transfer
**transfer
)
275 if (needs_resolve(ctx
->screen
, resource
, usage
))
276 return texture_transfer_map_resolve(ctx
, resource
, level
, usage
, box
,
279 return texture_transfer_map_plain(ctx
, resource
, level
, usage
, box
, transfer
);
282 static void flush_data(struct pipe_context
*ctx
,
283 struct virgl_transfer
*trans
,
284 const struct pipe_box
*box
)
286 struct virgl_winsys
*vws
= virgl_screen(ctx
->screen
)->vws
;
287 vws
->transfer_put(vws
, virgl_resource(trans
->base
.resource
)->hw_res
, box
,
288 trans
->base
.stride
, trans
->l_stride
, trans
->offset
,
292 static void virgl_texture_transfer_unmap(struct pipe_context
*ctx
,
293 struct pipe_transfer
*transfer
)
295 struct virgl_context
*vctx
= virgl_context(ctx
);
296 struct virgl_transfer
*trans
= virgl_transfer(transfer
);
297 bool queue_unmap
= false;
299 if (transfer
->usage
& PIPE_TRANSFER_WRITE
&&
300 (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) == 0) {
302 if (trans
->resolve_transfer
&& (trans
->base
.resource
->format
==
303 trans
->resolve_transfer
->resource
->format
)) {
304 flush_data(ctx
, virgl_transfer(trans
->resolve_transfer
),
305 &trans
->resolve_transfer
->box
);
307 /* FINISHME: In case the destination format isn't renderable here, the
308 * blit here will currently fail. This could for instance happen if the
309 * mapped resource is of a compressed format, and it's mapped with both
310 * read and write usage.
313 virgl_copy_region_with_blit(ctx
,
314 trans
->base
.resource
, trans
->base
.level
,
316 trans
->resolve_transfer
->resource
, 0,
317 &trans
->resolve_transfer
->box
);
318 ctx
->flush(ctx
, NULL
, 0);
323 if (trans
->resolve_transfer
) {
324 pipe_resource_reference(&trans
->resolve_transfer
->resource
, NULL
);
325 virgl_resource_destroy_transfer(&vctx
->transfer_pool
,
326 virgl_transfer(trans
->resolve_transfer
));
330 virgl_transfer_queue_unmap(&vctx
->queue
, trans
);
332 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
335 static const struct u_resource_vtbl virgl_texture_vtbl
=
337 virgl_resource_get_handle
, /* get_handle */
338 virgl_resource_destroy
, /* resource_destroy */
339 virgl_texture_transfer_map
, /* transfer_map */
340 NULL
, /* transfer_flush_region */
341 virgl_texture_transfer_unmap
, /* transfer_unmap */
344 void virgl_texture_init(struct virgl_resource
*res
)
346 res
->u
.vtbl
= &virgl_texture_vtbl
;