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 assert(src_box
->width
== dst_box
->width
);
42 assert(src_box
->height
== dst_box
->height
);
43 assert(src_box
->depth
== dst_box
->depth
);
45 memset(&blit
, 0, sizeof(blit
));
46 blit
.src
.resource
= src
;
47 blit
.src
.format
= src
->format
;
48 blit
.src
.level
= src_level
;
49 blit
.src
.box
= *src_box
;
50 blit
.dst
.resource
= dst
;
51 blit
.dst
.format
= dst
->format
;
52 blit
.dst
.level
= dst_level
;
53 blit
.dst
.box
.x
= dst_box
->x
;
54 blit
.dst
.box
.y
= dst_box
->y
;
55 blit
.dst
.box
.z
= dst_box
->z
;
56 blit
.dst
.box
.width
= src_box
->width
;
57 blit
.dst
.box
.height
= src_box
->height
;
58 blit
.dst
.box
.depth
= src_box
->depth
;
59 blit
.mask
= util_format_get_mask(src
->format
) &
60 util_format_get_mask(dst
->format
);
61 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
64 pipe
->blit(pipe
, &blit
);
68 static unsigned temp_bind(unsigned orig
)
70 unsigned warn
= ~(PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
|
71 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DISPLAY_TARGET
);
73 debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
76 return orig
& (PIPE_BIND_DEPTH_STENCIL
| PIPE_BIND_RENDER_TARGET
);
79 static void virgl_init_temp_resource_from_box(struct pipe_resource
*res
,
80 struct pipe_resource
*orig
,
81 const struct pipe_box
*box
,
82 unsigned level
, unsigned flags
)
84 memset(res
, 0, sizeof(*res
));
85 res
->bind
= temp_bind(orig
->bind
);
86 res
->format
= orig
->format
;
87 res
->width0
= box
->width
;
88 res
->height0
= box
->height
;
91 res
->usage
= PIPE_USAGE_STAGING
;
94 /* We must set the correct texture target and dimensions for a 3D box. */
95 if (box
->depth
> 1 && util_max_layer(orig
, level
) > 0)
96 res
->target
= orig
->target
;
98 res
->target
= PIPE_TEXTURE_2D
;
100 if (res
->target
!= PIPE_BUFFER
)
101 res
->bind
= PIPE_BIND_RENDER_TARGET
;
103 switch (res
->target
) {
104 case PIPE_TEXTURE_1D_ARRAY
:
105 case PIPE_TEXTURE_2D_ARRAY
:
106 case PIPE_TEXTURE_CUBE_ARRAY
:
107 res
->array_size
= box
->depth
;
109 case PIPE_TEXTURE_3D
:
110 res
->depth0
= box
->depth
;
117 static void *texture_transfer_map_plain(struct pipe_context
*ctx
,
118 struct pipe_resource
*resource
,
121 const struct pipe_box
*box
,
122 struct pipe_transfer
**transfer
)
124 struct virgl_context
*vctx
= virgl_context(ctx
);
125 struct virgl_winsys
*vws
= virgl_screen(ctx
->screen
)->vws
;
126 struct virgl_resource
*vtex
= virgl_resource(resource
);
127 struct virgl_transfer
*trans
;
129 trans
= virgl_resource_create_transfer(&vctx
->transfer_pool
, resource
,
130 &vtex
->metadata
, level
, usage
, box
);
131 trans
->resolve_transfer
= NULL
;
133 assert(resource
->nr_samples
<= 1);
135 if (virgl_res_needs_flush(vctx
, trans
))
136 ctx
->flush(ctx
, NULL
, 0);
138 if (virgl_res_needs_readback(vctx
, vtex
, usage
, level
)) {
139 vws
->transfer_get(vws
, vtex
->hw_res
, box
, trans
->base
.stride
,
140 trans
->l_stride
, trans
->offset
, level
);
142 vws
->resource_wait(vws
, vtex
->hw_res
);
145 void *ptr
= vws
->resource_map(vws
, vtex
->hw_res
);
147 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
151 *transfer
= &trans
->base
;
152 return ptr
+ trans
->offset
;
155 static void *texture_transfer_map_resolve(struct pipe_context
*ctx
,
156 struct pipe_resource
*resource
,
159 const struct pipe_box
*box
,
160 struct pipe_transfer
**transfer
)
162 struct virgl_context
*vctx
= virgl_context(ctx
);
163 struct pipe_resource templ
, *resolve_tmp
;
164 struct virgl_transfer
*trans
;
166 trans
= virgl_resource_create_transfer(&vctx
->transfer_pool
, resource
,
167 &virgl_resource(resource
)->metadata
,
172 virgl_init_temp_resource_from_box(&templ
, resource
, box
, level
, 0);
174 resolve_tmp
= ctx
->screen
->resource_create(ctx
->screen
, &templ
);
178 struct pipe_box dst_box
= *box
;
179 dst_box
.x
= dst_box
.y
= dst_box
.z
= 0;
181 virgl_copy_region_with_blit(ctx
, resolve_tmp
, 0, &dst_box
, resource
, level
, box
);
182 ctx
->flush(ctx
, NULL
, 0);
184 void *ptr
= texture_transfer_map_plain(ctx
, resolve_tmp
, 0, usage
, &dst_box
,
185 &trans
->resolve_transfer
);
189 *transfer
= &trans
->base
;
190 trans
->base
.stride
= trans
->resolve_transfer
->stride
;
191 trans
->base
.layer_stride
= trans
->resolve_transfer
->layer_stride
;
195 pipe_resource_reference(&resolve_tmp
, NULL
);
196 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
200 static void *virgl_texture_transfer_map(struct pipe_context
*ctx
,
201 struct pipe_resource
*resource
,
204 const struct pipe_box
*box
,
205 struct pipe_transfer
**transfer
)
207 if (resource
->nr_samples
> 1)
208 return texture_transfer_map_resolve(ctx
, resource
, level
, usage
, box
,
211 return texture_transfer_map_plain(ctx
, resource
, level
, usage
, box
, transfer
);
214 static void flush_data(struct pipe_context
*ctx
,
215 struct virgl_transfer
*trans
,
216 const struct pipe_box
*box
)
218 struct virgl_winsys
*vws
= virgl_screen(ctx
->screen
)->vws
;
219 vws
->transfer_put(vws
, virgl_resource(trans
->base
.resource
)->hw_res
, box
,
220 trans
->base
.stride
, trans
->l_stride
, trans
->offset
,
224 static void virgl_texture_transfer_unmap(struct pipe_context
*ctx
,
225 struct pipe_transfer
*transfer
)
227 struct virgl_context
*vctx
= virgl_context(ctx
);
228 struct virgl_transfer
*trans
= virgl_transfer(transfer
);
229 bool queue_unmap
= false;
231 if (transfer
->usage
& PIPE_TRANSFER_WRITE
&&
232 (transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
) == 0) {
234 if (trans
->resolve_transfer
) {
235 flush_data(ctx
, virgl_transfer(trans
->resolve_transfer
),
236 &trans
->resolve_transfer
->box
);
238 virgl_copy_region_with_blit(ctx
,
239 trans
->base
.resource
, trans
->base
.level
,
241 trans
->resolve_transfer
->resource
, 0,
242 &trans
->resolve_transfer
->box
);
243 ctx
->flush(ctx
, NULL
, 0);
248 if (trans
->resolve_transfer
) {
249 pipe_resource_reference(&trans
->resolve_transfer
->resource
, NULL
);
250 virgl_resource_destroy_transfer(&vctx
->transfer_pool
,
251 virgl_transfer(trans
->resolve_transfer
));
255 virgl_transfer_queue_unmap(&vctx
->queue
, trans
);
257 virgl_resource_destroy_transfer(&vctx
->transfer_pool
, trans
);
260 static const struct u_resource_vtbl virgl_texture_vtbl
=
262 virgl_resource_get_handle
, /* get_handle */
263 virgl_resource_destroy
, /* resource_destroy */
264 virgl_texture_transfer_map
, /* transfer_map */
265 NULL
, /* transfer_flush_region */
266 virgl_texture_transfer_unmap
, /* transfer_unmap */
269 void virgl_texture_init(struct virgl_resource
*res
)
271 res
->u
.vtbl
= &virgl_texture_vtbl
;