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.
24 #include "pipe/p_shader_tokens.h"
26 #include "pipe/p_context.h"
27 #include "pipe/p_defines.h"
28 #include "pipe/p_screen.h"
29 #include "pipe/p_state.h"
30 #include "util/u_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_format.h"
33 #include "util/u_prim.h"
34 #include "util/u_transfer.h"
35 #include "util/u_helpers.h"
36 #include "util/slab.h"
37 #include "util/u_upload_mgr.h"
38 #include "util/u_blitter.h"
39 #include "tgsi/tgsi_text.h"
40 #include "indices/u_primconvert.h"
42 #include "pipebuffer/pb_buffer.h"
44 #include "virgl_encode.h"
45 #include "virgl_context.h"
46 #include "virgl_protocol.h"
47 #include "virgl_resource.h"
48 #include "virgl_screen.h"
50 static uint32_t next_handle
;
51 uint32_t virgl_object_assign_handle(void)
56 static void virgl_buffer_flush(struct virgl_context
*vctx
,
57 struct virgl_buffer
*vbuf
)
59 struct virgl_screen
*rs
= virgl_screen(vctx
->base
.screen
);
62 assert(vbuf
->on_list
);
69 box
.x
= vbuf
->valid_buffer_range
.start
;
70 box
.width
= MIN2(vbuf
->valid_buffer_range
.end
- vbuf
->valid_buffer_range
.start
, vbuf
->base
.u
.b
.width0
);
72 vctx
->num_transfers
++;
73 rs
->vws
->transfer_put(rs
->vws
, vbuf
->base
.hw_res
,
74 &box
, 0, 0, box
.x
, 0);
76 util_range_set_empty(&vbuf
->valid_buffer_range
);
79 static void virgl_attach_res_framebuffer(struct virgl_context
*vctx
)
81 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
82 struct pipe_surface
*surf
;
83 struct virgl_resource
*res
;
86 surf
= vctx
->framebuffer
.zsbuf
;
88 res
= virgl_resource(surf
->texture
);
90 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
92 for (i
= 0; i
< vctx
->framebuffer
.nr_cbufs
; i
++) {
93 surf
= vctx
->framebuffer
.cbufs
[i
];
95 res
= virgl_resource(surf
->texture
);
97 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
102 static void virgl_attach_res_sampler_views(struct virgl_context
*vctx
,
103 enum pipe_shader_type shader_type
)
105 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
106 struct virgl_textures_info
*tinfo
= &vctx
->samplers
[shader_type
];
107 struct virgl_resource
*res
;
108 uint32_t remaining_mask
= tinfo
->enabled_mask
;
110 while (remaining_mask
) {
111 i
= u_bit_scan(&remaining_mask
);
112 assert(tinfo
->views
[i
]);
114 res
= virgl_resource(tinfo
->views
[i
]->base
.texture
);
116 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
120 static void virgl_attach_res_vertex_buffers(struct virgl_context
*vctx
)
122 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
123 struct virgl_resource
*res
;
126 for (i
= 0; i
< vctx
->num_vertex_buffers
; i
++) {
127 res
= virgl_resource(vctx
->vertex_buffer
[i
].buffer
.resource
);
129 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
133 static void virgl_attach_res_index_buffer(struct virgl_context
*vctx
,
134 struct virgl_indexbuf
*ib
)
136 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
137 struct virgl_resource
*res
;
139 res
= virgl_resource(ib
->buffer
);
141 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
144 static void virgl_attach_res_so_targets(struct virgl_context
*vctx
)
146 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
147 struct virgl_resource
*res
;
150 for (i
= 0; i
< vctx
->num_so_targets
; i
++) {
151 res
= virgl_resource(vctx
->so_targets
[i
].base
.buffer
);
153 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
157 static void virgl_attach_res_uniform_buffers(struct virgl_context
*vctx
,
158 enum pipe_shader_type shader_type
)
160 struct virgl_winsys
*vws
= virgl_screen(vctx
->base
.screen
)->vws
;
161 struct virgl_resource
*res
;
163 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
164 res
= virgl_resource(vctx
->ubos
[shader_type
][i
]);
166 vws
->emit_res(vws
, vctx
->cbuf
, res
->hw_res
, FALSE
);
172 * after flushing, the hw context still has a bunch of
173 * resources bound, so we need to rebind those here.
175 static void virgl_reemit_res(struct virgl_context
*vctx
)
177 enum pipe_shader_type shader_type
;
179 /* reattach any flushed resources */
180 /* framebuffer, sampler views, vertex/index/uniform/stream buffers */
181 virgl_attach_res_framebuffer(vctx
);
183 for (shader_type
= 0; shader_type
< PIPE_SHADER_TYPES
; shader_type
++) {
184 virgl_attach_res_sampler_views(vctx
, shader_type
);
185 virgl_attach_res_uniform_buffers(vctx
, shader_type
);
187 virgl_attach_res_vertex_buffers(vctx
);
188 virgl_attach_res_so_targets(vctx
);
191 static struct pipe_surface
*virgl_create_surface(struct pipe_context
*ctx
,
192 struct pipe_resource
*resource
,
193 const struct pipe_surface
*templ
)
195 struct virgl_context
*vctx
= virgl_context(ctx
);
196 struct virgl_surface
*surf
;
197 struct virgl_resource
*res
= virgl_resource(resource
);
200 surf
= CALLOC_STRUCT(virgl_surface
);
205 handle
= virgl_object_assign_handle();
206 pipe_reference_init(&surf
->base
.reference
, 1);
207 pipe_resource_reference(&surf
->base
.texture
, resource
);
208 surf
->base
.context
= ctx
;
209 surf
->base
.format
= templ
->format
;
210 if (resource
->target
!= PIPE_BUFFER
) {
211 surf
->base
.width
= u_minify(resource
->width0
, templ
->u
.tex
.level
);
212 surf
->base
.height
= u_minify(resource
->height0
, templ
->u
.tex
.level
);
213 surf
->base
.u
.tex
.level
= templ
->u
.tex
.level
;
214 surf
->base
.u
.tex
.first_layer
= templ
->u
.tex
.first_layer
;
215 surf
->base
.u
.tex
.last_layer
= templ
->u
.tex
.last_layer
;
217 surf
->base
.width
= templ
->u
.buf
.last_element
- templ
->u
.buf
.first_element
+ 1;
218 surf
->base
.height
= resource
->height0
;
219 surf
->base
.u
.buf
.first_element
= templ
->u
.buf
.first_element
;
220 surf
->base
.u
.buf
.last_element
= templ
->u
.buf
.last_element
;
222 virgl_encoder_create_surface(vctx
, handle
, res
, &surf
->base
);
223 surf
->handle
= handle
;
227 static void virgl_surface_destroy(struct pipe_context
*ctx
,
228 struct pipe_surface
*psurf
)
230 struct virgl_context
*vctx
= virgl_context(ctx
);
231 struct virgl_surface
*surf
= virgl_surface(psurf
);
233 pipe_resource_reference(&surf
->base
.texture
, NULL
);
234 virgl_encode_delete_object(vctx
, surf
->handle
, VIRGL_OBJECT_SURFACE
);
238 static void *virgl_create_blend_state(struct pipe_context
*ctx
,
239 const struct pipe_blend_state
*blend_state
)
241 struct virgl_context
*vctx
= virgl_context(ctx
);
243 handle
= virgl_object_assign_handle();
245 virgl_encode_blend_state(vctx
, handle
, blend_state
);
246 return (void *)(unsigned long)handle
;
250 static void virgl_bind_blend_state(struct pipe_context
*ctx
,
253 struct virgl_context
*vctx
= virgl_context(ctx
);
254 uint32_t handle
= (unsigned long)blend_state
;
255 virgl_encode_bind_object(vctx
, handle
, VIRGL_OBJECT_BLEND
);
258 static void virgl_delete_blend_state(struct pipe_context
*ctx
,
261 struct virgl_context
*vctx
= virgl_context(ctx
);
262 uint32_t handle
= (unsigned long)blend_state
;
263 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_BLEND
);
266 static void *virgl_create_depth_stencil_alpha_state(struct pipe_context
*ctx
,
267 const struct pipe_depth_stencil_alpha_state
*blend_state
)
269 struct virgl_context
*vctx
= virgl_context(ctx
);
271 handle
= virgl_object_assign_handle();
273 virgl_encode_dsa_state(vctx
, handle
, blend_state
);
274 return (void *)(unsigned long)handle
;
277 static void virgl_bind_depth_stencil_alpha_state(struct pipe_context
*ctx
,
280 struct virgl_context
*vctx
= virgl_context(ctx
);
281 uint32_t handle
= (unsigned long)blend_state
;
282 virgl_encode_bind_object(vctx
, handle
, VIRGL_OBJECT_DSA
);
285 static void virgl_delete_depth_stencil_alpha_state(struct pipe_context
*ctx
,
288 struct virgl_context
*vctx
= virgl_context(ctx
);
289 uint32_t handle
= (unsigned long)dsa_state
;
290 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_DSA
);
293 static void *virgl_create_rasterizer_state(struct pipe_context
*ctx
,
294 const struct pipe_rasterizer_state
*rs_state
)
296 struct virgl_context
*vctx
= virgl_context(ctx
);
298 handle
= virgl_object_assign_handle();
300 virgl_encode_rasterizer_state(vctx
, handle
, rs_state
);
301 return (void *)(unsigned long)handle
;
304 static void virgl_bind_rasterizer_state(struct pipe_context
*ctx
,
307 struct virgl_context
*vctx
= virgl_context(ctx
);
308 uint32_t handle
= (unsigned long)rs_state
;
310 virgl_encode_bind_object(vctx
, handle
, VIRGL_OBJECT_RASTERIZER
);
313 static void virgl_delete_rasterizer_state(struct pipe_context
*ctx
,
316 struct virgl_context
*vctx
= virgl_context(ctx
);
317 uint32_t handle
= (unsigned long)rs_state
;
318 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_RASTERIZER
);
321 static void virgl_set_framebuffer_state(struct pipe_context
*ctx
,
322 const struct pipe_framebuffer_state
*state
)
324 struct virgl_context
*vctx
= virgl_context(ctx
);
326 vctx
->framebuffer
= *state
;
327 virgl_encoder_set_framebuffer_state(vctx
, state
);
328 virgl_attach_res_framebuffer(vctx
);
331 static void virgl_set_viewport_states(struct pipe_context
*ctx
,
333 unsigned num_viewports
,
334 const struct pipe_viewport_state
*state
)
336 struct virgl_context
*vctx
= virgl_context(ctx
);
337 virgl_encoder_set_viewport_states(vctx
, start_slot
, num_viewports
, state
);
340 static void *virgl_create_vertex_elements_state(struct pipe_context
*ctx
,
341 unsigned num_elements
,
342 const struct pipe_vertex_element
*elements
)
344 struct virgl_context
*vctx
= virgl_context(ctx
);
345 uint32_t handle
= virgl_object_assign_handle();
346 virgl_encoder_create_vertex_elements(vctx
, handle
,
347 num_elements
, elements
);
348 return (void*)(unsigned long)handle
;
352 static void virgl_delete_vertex_elements_state(struct pipe_context
*ctx
,
355 struct virgl_context
*vctx
= virgl_context(ctx
);
356 uint32_t handle
= (unsigned long)ve
;
358 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_VERTEX_ELEMENTS
);
361 static void virgl_bind_vertex_elements_state(struct pipe_context
*ctx
,
364 struct virgl_context
*vctx
= virgl_context(ctx
);
365 uint32_t handle
= (unsigned long)ve
;
366 virgl_encode_bind_object(vctx
, handle
, VIRGL_OBJECT_VERTEX_ELEMENTS
);
369 static void virgl_set_vertex_buffers(struct pipe_context
*ctx
,
371 unsigned num_buffers
,
372 const struct pipe_vertex_buffer
*buffers
)
374 struct virgl_context
*vctx
= virgl_context(ctx
);
376 util_set_vertex_buffers_count(vctx
->vertex_buffer
,
377 &vctx
->num_vertex_buffers
,
378 buffers
, start_slot
, num_buffers
);
380 vctx
->vertex_array_dirty
= TRUE
;
383 static void virgl_hw_set_vertex_buffers(struct pipe_context
*ctx
)
385 struct virgl_context
*vctx
= virgl_context(ctx
);
387 if (vctx
->vertex_array_dirty
) {
388 virgl_encoder_set_vertex_buffers(vctx
, vctx
->num_vertex_buffers
, vctx
->vertex_buffer
);
389 virgl_attach_res_vertex_buffers(vctx
);
393 static void virgl_set_stencil_ref(struct pipe_context
*ctx
,
394 const struct pipe_stencil_ref
*ref
)
396 struct virgl_context
*vctx
= virgl_context(ctx
);
397 virgl_encoder_set_stencil_ref(vctx
, ref
);
400 static void virgl_set_blend_color(struct pipe_context
*ctx
,
401 const struct pipe_blend_color
*color
)
403 struct virgl_context
*vctx
= virgl_context(ctx
);
404 virgl_encoder_set_blend_color(vctx
, color
);
407 static void virgl_hw_set_index_buffer(struct pipe_context
*ctx
,
408 struct virgl_indexbuf
*ib
)
410 struct virgl_context
*vctx
= virgl_context(ctx
);
411 virgl_encoder_set_index_buffer(vctx
, ib
);
412 virgl_attach_res_index_buffer(vctx
, ib
);
415 static void virgl_set_constant_buffer(struct pipe_context
*ctx
,
416 enum pipe_shader_type shader
, uint index
,
417 const struct pipe_constant_buffer
*buf
)
419 struct virgl_context
*vctx
= virgl_context(ctx
);
422 if (!buf
->user_buffer
){
423 struct virgl_resource
*res
= virgl_resource(buf
->buffer
);
424 virgl_encoder_set_uniform_buffer(vctx
, shader
, index
, buf
->buffer_offset
,
425 buf
->buffer_size
, res
);
426 pipe_resource_reference(&vctx
->ubos
[shader
][index
], buf
->buffer
);
429 pipe_resource_reference(&vctx
->ubos
[shader
][index
], NULL
);
430 virgl_encoder_write_constant_buffer(vctx
, shader
, index
, buf
->buffer_size
/ 4, buf
->user_buffer
);
432 virgl_encoder_write_constant_buffer(vctx
, shader
, index
, 0, NULL
);
433 pipe_resource_reference(&vctx
->ubos
[shader
][index
], NULL
);
437 void virgl_transfer_inline_write(struct pipe_context
*ctx
,
438 struct pipe_resource
*res
,
441 const struct pipe_box
*box
,
444 unsigned layer_stride
)
446 struct virgl_context
*vctx
= virgl_context(ctx
);
447 struct virgl_screen
*vs
= virgl_screen(ctx
->screen
);
448 struct virgl_resource
*grres
= virgl_resource(res
);
449 struct virgl_buffer
*vbuf
= virgl_buffer(res
);
451 grres
->clean
= FALSE
;
453 if (virgl_res_needs_flush_wait(vctx
, &vbuf
->base
, usage
)) {
454 ctx
->flush(ctx
, NULL
, 0);
456 vs
->vws
->resource_wait(vs
->vws
, vbuf
->base
.hw_res
);
459 virgl_encoder_inline_write(vctx
, grres
, level
, usage
,
460 box
, data
, stride
, layer_stride
);
463 static void *virgl_shader_encoder(struct pipe_context
*ctx
,
464 const struct pipe_shader_state
*shader
,
467 struct virgl_context
*vctx
= virgl_context(ctx
);
469 struct tgsi_token
*new_tokens
;
472 new_tokens
= virgl_tgsi_transform(shader
->tokens
);
476 handle
= virgl_object_assign_handle();
477 /* encode VS state */
478 ret
= virgl_encode_shader_state(vctx
, handle
, type
,
479 &shader
->stream_output
,
486 return (void *)(unsigned long)handle
;
489 static void *virgl_create_vs_state(struct pipe_context
*ctx
,
490 const struct pipe_shader_state
*shader
)
492 return virgl_shader_encoder(ctx
, shader
, PIPE_SHADER_VERTEX
);
495 static void *virgl_create_gs_state(struct pipe_context
*ctx
,
496 const struct pipe_shader_state
*shader
)
498 return virgl_shader_encoder(ctx
, shader
, PIPE_SHADER_GEOMETRY
);
501 static void *virgl_create_fs_state(struct pipe_context
*ctx
,
502 const struct pipe_shader_state
*shader
)
504 return virgl_shader_encoder(ctx
, shader
, PIPE_SHADER_FRAGMENT
);
508 virgl_delete_fs_state(struct pipe_context
*ctx
,
511 uint32_t handle
= (unsigned long)fs
;
512 struct virgl_context
*vctx
= virgl_context(ctx
);
514 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_SHADER
);
518 virgl_delete_gs_state(struct pipe_context
*ctx
,
521 uint32_t handle
= (unsigned long)gs
;
522 struct virgl_context
*vctx
= virgl_context(ctx
);
524 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_SHADER
);
528 virgl_delete_vs_state(struct pipe_context
*ctx
,
531 uint32_t handle
= (unsigned long)vs
;
532 struct virgl_context
*vctx
= virgl_context(ctx
);
534 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_SHADER
);
537 static void virgl_bind_vs_state(struct pipe_context
*ctx
,
540 uint32_t handle
= (unsigned long)vss
;
541 struct virgl_context
*vctx
= virgl_context(ctx
);
543 virgl_encode_bind_shader(vctx
, handle
, PIPE_SHADER_VERTEX
);
546 static void virgl_bind_gs_state(struct pipe_context
*ctx
,
549 uint32_t handle
= (unsigned long)vss
;
550 struct virgl_context
*vctx
= virgl_context(ctx
);
552 virgl_encode_bind_shader(vctx
, handle
, PIPE_SHADER_GEOMETRY
);
556 static void virgl_bind_fs_state(struct pipe_context
*ctx
,
559 uint32_t handle
= (unsigned long)vss
;
560 struct virgl_context
*vctx
= virgl_context(ctx
);
562 virgl_encode_bind_shader(vctx
, handle
, PIPE_SHADER_FRAGMENT
);
565 static void virgl_clear(struct pipe_context
*ctx
,
567 const union pipe_color_union
*color
,
568 double depth
, unsigned stencil
)
570 struct virgl_context
*vctx
= virgl_context(ctx
);
572 virgl_encode_clear(vctx
, buffers
, color
, depth
, stencil
);
575 static void virgl_draw_vbo(struct pipe_context
*ctx
,
576 const struct pipe_draw_info
*dinfo
)
578 struct virgl_context
*vctx
= virgl_context(ctx
);
579 struct virgl_screen
*rs
= virgl_screen(ctx
->screen
);
580 struct virgl_indexbuf ib
= {};
581 struct pipe_draw_info info
= *dinfo
;
583 if (!dinfo
->count_from_stream_output
&& !dinfo
->indirect
&&
584 !dinfo
->primitive_restart
&&
585 !u_trim_pipe_prim(dinfo
->mode
, (unsigned*)&dinfo
->count
))
588 if (!(rs
->caps
.caps
.v1
.prim_mask
& (1 << dinfo
->mode
))) {
589 util_primconvert_draw_vbo(vctx
->primconvert
, dinfo
);
592 if (info
.index_size
) {
593 pipe_resource_reference(&ib
.buffer
, info
.has_user_indices
? NULL
: info
.index
.resource
);
594 ib
.user_buffer
= info
.has_user_indices
? info
.index
.user
: NULL
;
595 ib
.index_size
= dinfo
->index_size
;
596 ib
.offset
= info
.start
* ib
.index_size
;
598 if (ib
.user_buffer
) {
599 u_upload_data(vctx
->uploader
, 0, info
.count
* ib
.index_size
, 256,
600 ib
.user_buffer
, &ib
.offset
, &ib
.buffer
);
601 ib
.user_buffer
= NULL
;
605 u_upload_unmap(vctx
->uploader
);
608 virgl_hw_set_vertex_buffers(ctx
);
610 virgl_hw_set_index_buffer(ctx
, &ib
);
612 virgl_encoder_draw_vbo(vctx
, &info
);
614 pipe_resource_reference(&ib
.buffer
, NULL
);
618 static void virgl_flush_eq(struct virgl_context
*ctx
, void *closure
)
620 struct virgl_screen
*rs
= virgl_screen(ctx
->base
.screen
);
622 /* send the buffer to the remote side for decoding */
623 ctx
->num_transfers
= ctx
->num_draws
= 0;
624 rs
->vws
->submit_cmd(rs
->vws
, ctx
->cbuf
);
626 virgl_encoder_set_sub_ctx(ctx
, ctx
->hw_sub_ctx_id
);
628 /* add back current framebuffer resources to reference list? */
629 virgl_reemit_res(ctx
);
632 static void virgl_flush_from_st(struct pipe_context
*ctx
,
633 struct pipe_fence_handle
**fence
,
634 enum pipe_flush_flags flags
)
636 struct virgl_context
*vctx
= virgl_context(ctx
);
637 struct virgl_screen
*rs
= virgl_screen(ctx
->screen
);
638 struct virgl_buffer
*buf
, *tmp
;
641 *fence
= rs
->vws
->cs_create_fence(rs
->vws
);
643 LIST_FOR_EACH_ENTRY_SAFE(buf
, tmp
, &vctx
->to_flush_bufs
, flush_list
) {
644 struct pipe_resource
*res
= &buf
->base
.u
.b
;
645 virgl_buffer_flush(vctx
, buf
);
646 list_del(&buf
->flush_list
);
647 buf
->on_list
= FALSE
;
648 pipe_resource_reference(&res
, NULL
);
651 virgl_flush_eq(vctx
, vctx
);
654 static struct pipe_sampler_view
*virgl_create_sampler_view(struct pipe_context
*ctx
,
655 struct pipe_resource
*texture
,
656 const struct pipe_sampler_view
*state
)
658 struct virgl_context
*vctx
= virgl_context(ctx
);
659 struct virgl_sampler_view
*grview
;
661 struct virgl_resource
*res
;
666 grview
= CALLOC_STRUCT(virgl_sampler_view
);
670 res
= virgl_resource(texture
);
671 handle
= virgl_object_assign_handle();
672 virgl_encode_sampler_view(vctx
, handle
, res
, state
);
674 grview
->base
= *state
;
675 grview
->base
.reference
.count
= 1;
677 grview
->base
.texture
= NULL
;
678 grview
->base
.context
= ctx
;
679 pipe_resource_reference(&grview
->base
.texture
, texture
);
680 grview
->handle
= handle
;
681 return &grview
->base
;
684 static void virgl_set_sampler_views(struct pipe_context
*ctx
,
685 enum pipe_shader_type shader_type
,
688 struct pipe_sampler_view
**views
)
690 struct virgl_context
*vctx
= virgl_context(ctx
);
692 uint32_t disable_mask
= ~((1ull << num_views
) - 1);
693 struct virgl_textures_info
*tinfo
= &vctx
->samplers
[shader_type
];
694 uint32_t new_mask
= 0;
695 uint32_t remaining_mask
;
697 remaining_mask
= tinfo
->enabled_mask
& disable_mask
;
699 while (remaining_mask
) {
700 i
= u_bit_scan(&remaining_mask
);
701 assert(tinfo
->views
[i
]);
703 pipe_sampler_view_reference((struct pipe_sampler_view
**)&tinfo
->views
[i
], NULL
);
706 for (i
= 0; i
< num_views
; i
++) {
707 struct virgl_sampler_view
*grview
= virgl_sampler_view(views
[i
]);
709 if (views
[i
] == (struct pipe_sampler_view
*)tinfo
->views
[i
])
714 pipe_sampler_view_reference((struct pipe_sampler_view
**)&tinfo
->views
[i
], views
[i
]);
716 pipe_sampler_view_reference((struct pipe_sampler_view
**)&tinfo
->views
[i
], NULL
);
717 disable_mask
|= 1 << i
;
721 tinfo
->enabled_mask
&= ~disable_mask
;
722 tinfo
->enabled_mask
|= new_mask
;
723 virgl_encode_set_sampler_views(vctx
, shader_type
, start_slot
, num_views
, tinfo
->views
);
724 virgl_attach_res_sampler_views(vctx
, shader_type
);
727 static void virgl_destroy_sampler_view(struct pipe_context
*ctx
,
728 struct pipe_sampler_view
*view
)
730 struct virgl_context
*vctx
= virgl_context(ctx
);
731 struct virgl_sampler_view
*grview
= virgl_sampler_view(view
);
733 virgl_encode_delete_object(vctx
, grview
->handle
, VIRGL_OBJECT_SAMPLER_VIEW
);
734 pipe_resource_reference(&view
->texture
, NULL
);
738 static void *virgl_create_sampler_state(struct pipe_context
*ctx
,
739 const struct pipe_sampler_state
*state
)
741 struct virgl_context
*vctx
= virgl_context(ctx
);
744 handle
= virgl_object_assign_handle();
746 virgl_encode_sampler_state(vctx
, handle
, state
);
747 return (void *)(unsigned long)handle
;
750 static void virgl_delete_sampler_state(struct pipe_context
*ctx
,
753 struct virgl_context
*vctx
= virgl_context(ctx
);
754 uint32_t handle
= (unsigned long)ss
;
756 virgl_encode_delete_object(vctx
, handle
, VIRGL_OBJECT_SAMPLER_STATE
);
759 static void virgl_bind_sampler_states(struct pipe_context
*ctx
,
760 enum pipe_shader_type shader
,
762 unsigned num_samplers
,
765 struct virgl_context
*vctx
= virgl_context(ctx
);
766 uint32_t handles
[32];
768 for (i
= 0; i
< num_samplers
; i
++) {
769 handles
[i
] = (unsigned long)(samplers
[i
]);
771 virgl_encode_bind_sampler_states(vctx
, shader
, start_slot
, num_samplers
, handles
);
774 static void virgl_set_polygon_stipple(struct pipe_context
*ctx
,
775 const struct pipe_poly_stipple
*ps
)
777 struct virgl_context
*vctx
= virgl_context(ctx
);
778 virgl_encoder_set_polygon_stipple(vctx
, ps
);
781 static void virgl_set_scissor_states(struct pipe_context
*ctx
,
783 unsigned num_scissor
,
784 const struct pipe_scissor_state
*ss
)
786 struct virgl_context
*vctx
= virgl_context(ctx
);
787 virgl_encoder_set_scissor_state(vctx
, start_slot
, num_scissor
, ss
);
790 static void virgl_set_sample_mask(struct pipe_context
*ctx
,
791 unsigned sample_mask
)
793 struct virgl_context
*vctx
= virgl_context(ctx
);
794 virgl_encoder_set_sample_mask(vctx
, sample_mask
);
797 static void virgl_set_clip_state(struct pipe_context
*ctx
,
798 const struct pipe_clip_state
*clip
)
800 struct virgl_context
*vctx
= virgl_context(ctx
);
801 virgl_encoder_set_clip_state(vctx
, clip
);
804 static void virgl_resource_copy_region(struct pipe_context
*ctx
,
805 struct pipe_resource
*dst
,
807 unsigned dstx
, unsigned dsty
, unsigned dstz
,
808 struct pipe_resource
*src
,
810 const struct pipe_box
*src_box
)
812 struct virgl_context
*vctx
= virgl_context(ctx
);
813 struct virgl_resource
*dres
= virgl_resource(dst
);
814 struct virgl_resource
*sres
= virgl_resource(src
);
817 virgl_encode_resource_copy_region(vctx
, dres
,
818 dst_level
, dstx
, dsty
, dstz
,
824 virgl_flush_resource(struct pipe_context
*pipe
,
825 struct pipe_resource
*resource
)
829 static void virgl_blit(struct pipe_context
*ctx
,
830 const struct pipe_blit_info
*blit
)
832 struct virgl_context
*vctx
= virgl_context(ctx
);
833 struct virgl_resource
*dres
= virgl_resource(blit
->dst
.resource
);
834 struct virgl_resource
*sres
= virgl_resource(blit
->src
.resource
);
837 virgl_encode_blit(vctx
, dres
, sres
,
842 virgl_context_destroy( struct pipe_context
*ctx
)
844 struct virgl_context
*vctx
= virgl_context(ctx
);
845 struct virgl_screen
*rs
= virgl_screen(ctx
->screen
);
847 vctx
->framebuffer
.zsbuf
= NULL
;
848 vctx
->framebuffer
.nr_cbufs
= 0;
849 virgl_encoder_destroy_sub_ctx(vctx
, vctx
->hw_sub_ctx_id
);
850 virgl_flush_eq(vctx
, vctx
);
852 rs
->vws
->cmd_buf_destroy(vctx
->cbuf
);
854 u_upload_destroy(vctx
->uploader
);
855 util_primconvert_destroy(vctx
->primconvert
);
857 slab_destroy_child(&vctx
->texture_transfer_pool
);
861 struct pipe_context
*virgl_context_create(struct pipe_screen
*pscreen
,
865 struct virgl_context
*vctx
;
866 struct virgl_screen
*rs
= virgl_screen(pscreen
);
867 vctx
= CALLOC_STRUCT(virgl_context
);
869 vctx
->cbuf
= rs
->vws
->cmd_buf_create(rs
->vws
);
875 vctx
->base
.destroy
= virgl_context_destroy
;
876 vctx
->base
.create_surface
= virgl_create_surface
;
877 vctx
->base
.surface_destroy
= virgl_surface_destroy
;
878 vctx
->base
.set_framebuffer_state
= virgl_set_framebuffer_state
;
879 vctx
->base
.create_blend_state
= virgl_create_blend_state
;
880 vctx
->base
.bind_blend_state
= virgl_bind_blend_state
;
881 vctx
->base
.delete_blend_state
= virgl_delete_blend_state
;
882 vctx
->base
.create_depth_stencil_alpha_state
= virgl_create_depth_stencil_alpha_state
;
883 vctx
->base
.bind_depth_stencil_alpha_state
= virgl_bind_depth_stencil_alpha_state
;
884 vctx
->base
.delete_depth_stencil_alpha_state
= virgl_delete_depth_stencil_alpha_state
;
885 vctx
->base
.create_rasterizer_state
= virgl_create_rasterizer_state
;
886 vctx
->base
.bind_rasterizer_state
= virgl_bind_rasterizer_state
;
887 vctx
->base
.delete_rasterizer_state
= virgl_delete_rasterizer_state
;
889 vctx
->base
.set_viewport_states
= virgl_set_viewport_states
;
890 vctx
->base
.create_vertex_elements_state
= virgl_create_vertex_elements_state
;
891 vctx
->base
.bind_vertex_elements_state
= virgl_bind_vertex_elements_state
;
892 vctx
->base
.delete_vertex_elements_state
= virgl_delete_vertex_elements_state
;
893 vctx
->base
.set_vertex_buffers
= virgl_set_vertex_buffers
;
894 vctx
->base
.set_constant_buffer
= virgl_set_constant_buffer
;
896 vctx
->base
.create_vs_state
= virgl_create_vs_state
;
897 vctx
->base
.create_gs_state
= virgl_create_gs_state
;
898 vctx
->base
.create_fs_state
= virgl_create_fs_state
;
900 vctx
->base
.bind_vs_state
= virgl_bind_vs_state
;
901 vctx
->base
.bind_gs_state
= virgl_bind_gs_state
;
902 vctx
->base
.bind_fs_state
= virgl_bind_fs_state
;
904 vctx
->base
.delete_vs_state
= virgl_delete_vs_state
;
905 vctx
->base
.delete_gs_state
= virgl_delete_gs_state
;
906 vctx
->base
.delete_fs_state
= virgl_delete_fs_state
;
908 vctx
->base
.clear
= virgl_clear
;
909 vctx
->base
.draw_vbo
= virgl_draw_vbo
;
910 vctx
->base
.flush
= virgl_flush_from_st
;
911 vctx
->base
.screen
= pscreen
;
912 vctx
->base
.create_sampler_view
= virgl_create_sampler_view
;
913 vctx
->base
.sampler_view_destroy
= virgl_destroy_sampler_view
;
914 vctx
->base
.set_sampler_views
= virgl_set_sampler_views
;
916 vctx
->base
.create_sampler_state
= virgl_create_sampler_state
;
917 vctx
->base
.delete_sampler_state
= virgl_delete_sampler_state
;
918 vctx
->base
.bind_sampler_states
= virgl_bind_sampler_states
;
920 vctx
->base
.set_polygon_stipple
= virgl_set_polygon_stipple
;
921 vctx
->base
.set_scissor_states
= virgl_set_scissor_states
;
922 vctx
->base
.set_sample_mask
= virgl_set_sample_mask
;
923 vctx
->base
.set_stencil_ref
= virgl_set_stencil_ref
;
924 vctx
->base
.set_clip_state
= virgl_set_clip_state
;
926 vctx
->base
.set_blend_color
= virgl_set_blend_color
;
928 vctx
->base
.resource_copy_region
= virgl_resource_copy_region
;
929 vctx
->base
.flush_resource
= virgl_flush_resource
;
930 vctx
->base
.blit
= virgl_blit
;
932 virgl_init_context_resource_functions(&vctx
->base
);
933 virgl_init_query_functions(vctx
);
934 virgl_init_so_functions(vctx
);
936 list_inithead(&vctx
->to_flush_bufs
);
937 slab_create_child(&vctx
->texture_transfer_pool
, &rs
->texture_transfer_pool
);
939 vctx
->primconvert
= util_primconvert_create(&vctx
->base
, rs
->caps
.caps
.v1
.prim_mask
);
940 vctx
->uploader
= u_upload_create(&vctx
->base
, 1024 * 1024,
941 PIPE_BIND_INDEX_BUFFER
, PIPE_USAGE_STREAM
);
944 vctx
->base
.stream_uploader
= vctx
->uploader
;
945 vctx
->base
.const_uploader
= vctx
->uploader
;
947 vctx
->hw_sub_ctx_id
= rs
->sub_ctx_id
++;
948 virgl_encoder_create_sub_ctx(vctx
, vctx
->hw_sub_ctx_id
);
950 virgl_encoder_set_sub_ctx(vctx
, vctx
->hw_sub_ctx_id
);