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.
25 #include "util/u_memory.h"
26 #include "util/u_math.h"
27 #include "pipe/p_state.h"
28 #include "tgsi/tgsi_dump.h"
29 #include "tgsi/tgsi_parse.h"
31 #include "virgl_context.h"
32 #include "virgl_encode.h"
33 #include "virgl_protocol.h"
34 #include "virgl_resource.h"
35 #include "virgl_screen.h"
37 static int virgl_encoder_write_cmd_dword(struct virgl_context
*ctx
,
40 int len
= (dword
>> 16);
42 if ((ctx
->cbuf
->cdw
+ len
+ 1) > VIRGL_MAX_CMDBUF_DWORDS
)
43 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
45 virgl_encoder_write_dword(ctx
->cbuf
, dword
);
49 static void virgl_encoder_write_res(struct virgl_context
*ctx
,
50 struct virgl_resource
*res
)
52 struct virgl_winsys
*vws
= virgl_screen(ctx
->base
.screen
)->vws
;
54 if (res
&& res
->hw_res
)
55 vws
->emit_res(vws
, ctx
->cbuf
, res
->hw_res
, TRUE
);
57 virgl_encoder_write_dword(ctx
->cbuf
, 0);
61 int virgl_encode_bind_object(struct virgl_context
*ctx
,
62 uint32_t handle
, uint32_t object
)
64 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_OBJECT
, object
, 1));
65 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
69 int virgl_encode_delete_object(struct virgl_context
*ctx
,
70 uint32_t handle
, uint32_t object
)
72 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DESTROY_OBJECT
, object
, 1));
73 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
77 int virgl_encode_blend_state(struct virgl_context
*ctx
,
79 const struct pipe_blend_state
*blend_state
)
84 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_BLEND
, VIRGL_OBJ_BLEND_SIZE
));
85 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
88 VIRGL_OBJ_BLEND_S0_INDEPENDENT_BLEND_ENABLE(blend_state
->independent_blend_enable
) |
89 VIRGL_OBJ_BLEND_S0_LOGICOP_ENABLE(blend_state
->logicop_enable
) |
90 VIRGL_OBJ_BLEND_S0_DITHER(blend_state
->dither
) |
91 VIRGL_OBJ_BLEND_S0_ALPHA_TO_COVERAGE(blend_state
->alpha_to_coverage
) |
92 VIRGL_OBJ_BLEND_S0_ALPHA_TO_ONE(blend_state
->alpha_to_one
);
94 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
96 tmp
= VIRGL_OBJ_BLEND_S1_LOGICOP_FUNC(blend_state
->logicop_func
);
97 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
99 for (i
= 0; i
< VIRGL_MAX_COLOR_BUFS
; i
++) {
101 VIRGL_OBJ_BLEND_S2_RT_BLEND_ENABLE(blend_state
->rt
[i
].blend_enable
) |
102 VIRGL_OBJ_BLEND_S2_RT_RGB_FUNC(blend_state
->rt
[i
].rgb_func
) |
103 VIRGL_OBJ_BLEND_S2_RT_RGB_SRC_FACTOR(blend_state
->rt
[i
].rgb_src_factor
) |
104 VIRGL_OBJ_BLEND_S2_RT_RGB_DST_FACTOR(blend_state
->rt
[i
].rgb_dst_factor
)|
105 VIRGL_OBJ_BLEND_S2_RT_ALPHA_FUNC(blend_state
->rt
[i
].alpha_func
) |
106 VIRGL_OBJ_BLEND_S2_RT_ALPHA_SRC_FACTOR(blend_state
->rt
[i
].alpha_src_factor
) |
107 VIRGL_OBJ_BLEND_S2_RT_ALPHA_DST_FACTOR(blend_state
->rt
[i
].alpha_dst_factor
) |
108 VIRGL_OBJ_BLEND_S2_RT_COLORMASK(blend_state
->rt
[i
].colormask
);
109 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
114 int virgl_encode_dsa_state(struct virgl_context
*ctx
,
116 const struct pipe_depth_stencil_alpha_state
*dsa_state
)
120 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_DSA
, VIRGL_OBJ_DSA_SIZE
));
121 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
123 tmp
= VIRGL_OBJ_DSA_S0_DEPTH_ENABLE(dsa_state
->depth
.enabled
) |
124 VIRGL_OBJ_DSA_S0_DEPTH_WRITEMASK(dsa_state
->depth
.writemask
) |
125 VIRGL_OBJ_DSA_S0_DEPTH_FUNC(dsa_state
->depth
.func
) |
126 VIRGL_OBJ_DSA_S0_ALPHA_ENABLED(dsa_state
->alpha
.enabled
) |
127 VIRGL_OBJ_DSA_S0_ALPHA_FUNC(dsa_state
->alpha
.func
);
128 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
130 for (i
= 0; i
< 2; i
++) {
131 tmp
= VIRGL_OBJ_DSA_S1_STENCIL_ENABLED(dsa_state
->stencil
[i
].enabled
) |
132 VIRGL_OBJ_DSA_S1_STENCIL_FUNC(dsa_state
->stencil
[i
].func
) |
133 VIRGL_OBJ_DSA_S1_STENCIL_FAIL_OP(dsa_state
->stencil
[i
].fail_op
) |
134 VIRGL_OBJ_DSA_S1_STENCIL_ZPASS_OP(dsa_state
->stencil
[i
].zpass_op
) |
135 VIRGL_OBJ_DSA_S1_STENCIL_ZFAIL_OP(dsa_state
->stencil
[i
].zfail_op
) |
136 VIRGL_OBJ_DSA_S1_STENCIL_VALUEMASK(dsa_state
->stencil
[i
].valuemask
) |
137 VIRGL_OBJ_DSA_S1_STENCIL_WRITEMASK(dsa_state
->stencil
[i
].writemask
);
138 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
141 virgl_encoder_write_dword(ctx
->cbuf
, fui(dsa_state
->alpha
.ref_value
));
144 int virgl_encode_rasterizer_state(struct virgl_context
*ctx
,
146 const struct pipe_rasterizer_state
*state
)
150 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_RASTERIZER
, VIRGL_OBJ_RS_SIZE
));
151 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
153 tmp
= VIRGL_OBJ_RS_S0_FLATSHADE(state
->flatshade
) |
154 VIRGL_OBJ_RS_S0_DEPTH_CLIP(state
->depth_clip
) |
155 VIRGL_OBJ_RS_S0_CLIP_HALFZ(state
->clip_halfz
) |
156 VIRGL_OBJ_RS_S0_RASTERIZER_DISCARD(state
->rasterizer_discard
) |
157 VIRGL_OBJ_RS_S0_FLATSHADE_FIRST(state
->flatshade_first
) |
158 VIRGL_OBJ_RS_S0_LIGHT_TWOSIZE(state
->light_twoside
) |
159 VIRGL_OBJ_RS_S0_SPRITE_COORD_MODE(state
->sprite_coord_mode
) |
160 VIRGL_OBJ_RS_S0_POINT_QUAD_RASTERIZATION(state
->point_quad_rasterization
) |
161 VIRGL_OBJ_RS_S0_CULL_FACE(state
->cull_face
) |
162 VIRGL_OBJ_RS_S0_FILL_FRONT(state
->fill_front
) |
163 VIRGL_OBJ_RS_S0_FILL_BACK(state
->fill_back
) |
164 VIRGL_OBJ_RS_S0_SCISSOR(state
->scissor
) |
165 VIRGL_OBJ_RS_S0_FRONT_CCW(state
->front_ccw
) |
166 VIRGL_OBJ_RS_S0_CLAMP_VERTEX_COLOR(state
->clamp_vertex_color
) |
167 VIRGL_OBJ_RS_S0_CLAMP_FRAGMENT_COLOR(state
->clamp_fragment_color
) |
168 VIRGL_OBJ_RS_S0_OFFSET_LINE(state
->offset_line
) |
169 VIRGL_OBJ_RS_S0_OFFSET_POINT(state
->offset_point
) |
170 VIRGL_OBJ_RS_S0_OFFSET_TRI(state
->offset_tri
) |
171 VIRGL_OBJ_RS_S0_POLY_SMOOTH(state
->poly_smooth
) |
172 VIRGL_OBJ_RS_S0_POLY_STIPPLE_ENABLE(state
->poly_stipple_enable
) |
173 VIRGL_OBJ_RS_S0_POINT_SMOOTH(state
->point_smooth
) |
174 VIRGL_OBJ_RS_S0_POINT_SIZE_PER_VERTEX(state
->point_size_per_vertex
) |
175 VIRGL_OBJ_RS_S0_MULTISAMPLE(state
->multisample
) |
176 VIRGL_OBJ_RS_S0_LINE_SMOOTH(state
->line_smooth
) |
177 VIRGL_OBJ_RS_S0_LINE_STIPPLE_ENABLE(state
->line_stipple_enable
) |
178 VIRGL_OBJ_RS_S0_LINE_LAST_PIXEL(state
->line_last_pixel
) |
179 VIRGL_OBJ_RS_S0_HALF_PIXEL_CENTER(state
->half_pixel_center
) |
180 VIRGL_OBJ_RS_S0_BOTTOM_EDGE_RULE(state
->bottom_edge_rule
);
182 virgl_encoder_write_dword(ctx
->cbuf
, tmp
); /* S0 */
183 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->point_size
)); /* S1 */
184 virgl_encoder_write_dword(ctx
->cbuf
, state
->sprite_coord_enable
); /* S2 */
185 tmp
= VIRGL_OBJ_RS_S3_LINE_STIPPLE_PATTERN(state
->line_stipple_pattern
) |
186 VIRGL_OBJ_RS_S3_LINE_STIPPLE_FACTOR(state
->line_stipple_factor
) |
187 VIRGL_OBJ_RS_S3_CLIP_PLANE_ENABLE(state
->clip_plane_enable
);
188 virgl_encoder_write_dword(ctx
->cbuf
, tmp
); /* S3 */
189 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->line_width
)); /* S4 */
190 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_units
)); /* S5 */
191 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_scale
)); /* S6 */
192 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_clamp
)); /* S7 */
196 static void virgl_emit_shader_header(struct virgl_context
*ctx
,
197 uint32_t handle
, uint32_t len
,
198 uint32_t type
, uint32_t offlen
,
201 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SHADER
, len
));
202 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
203 virgl_encoder_write_dword(ctx
->cbuf
, type
);
204 virgl_encoder_write_dword(ctx
->cbuf
, offlen
);
205 virgl_encoder_write_dword(ctx
->cbuf
, num_tokens
);
208 static void virgl_emit_shader_streamout(struct virgl_context
*ctx
,
209 const struct pipe_stream_output_info
*so_info
)
216 num_outputs
= so_info
->num_outputs
;
218 virgl_encoder_write_dword(ctx
->cbuf
, num_outputs
);
220 for (i
= 0; i
< 4; i
++)
221 virgl_encoder_write_dword(ctx
->cbuf
, so_info
->stride
[i
]);
223 for (i
= 0; i
< so_info
->num_outputs
; i
++) {
225 VIRGL_OBJ_SHADER_SO_OUTPUT_REGISTER_INDEX(so_info
->output
[i
].register_index
) |
226 VIRGL_OBJ_SHADER_SO_OUTPUT_START_COMPONENT(so_info
->output
[i
].start_component
) |
227 VIRGL_OBJ_SHADER_SO_OUTPUT_NUM_COMPONENTS(so_info
->output
[i
].num_components
) |
228 VIRGL_OBJ_SHADER_SO_OUTPUT_BUFFER(so_info
->output
[i
].output_buffer
) |
229 VIRGL_OBJ_SHADER_SO_OUTPUT_DST_OFFSET(so_info
->output
[i
].dst_offset
);
230 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
231 virgl_encoder_write_dword(ctx
->cbuf
, 0);
236 int virgl_encode_shader_state(struct virgl_context
*ctx
,
239 const struct pipe_stream_output_info
*so_info
,
240 const struct tgsi_token
*tokens
)
243 uint32_t shader_len
, len
;
245 int num_tokens
= tgsi_num_tokens(tokens
);
246 int str_total_size
= 65536;
248 uint32_t left_bytes
, base_hdr_size
, strm_hdr_size
, thispass
;
250 str
= CALLOC(1, str_total_size
);
257 bret
= tgsi_dump_str(tokens
, TGSI_DUMP_FLOAT_AS_HEX
, str
, str_total_size
);
259 fprintf(stderr
, "Failed to translate shader in available space - trying again\n");
260 old_size
= str_total_size
;
261 str_total_size
= 65536 * ++retry_size
;
262 str
= REALLOC(str
, old_size
, str_total_size
);
266 } while (bret
== false && retry_size
< 10);
271 shader_len
= strlen(str
) + 1;
273 left_bytes
= shader_len
;
276 strm_hdr_size
= so_info
->num_outputs
? so_info
->num_outputs
* 2 + 4 : 0;
280 uint32_t length
, offlen
;
281 int hdr_len
= base_hdr_size
+ (first_pass
? strm_hdr_size
: 0);
282 if (ctx
->cbuf
->cdw
+ hdr_len
+ 1 > VIRGL_MAX_CMDBUF_DWORDS
)
283 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
285 thispass
= (VIRGL_MAX_CMDBUF_DWORDS
- ctx
->cbuf
->cdw
- hdr_len
- 1) * 4;
287 length
= MIN2(thispass
, left_bytes
);
288 len
= ((length
+ 3) / 4) + hdr_len
;
291 offlen
= VIRGL_OBJ_SHADER_OFFSET_VAL(shader_len
);
293 offlen
= VIRGL_OBJ_SHADER_OFFSET_VAL((uintptr_t)sptr
- (uintptr_t)str
) | VIRGL_OBJ_SHADER_OFFSET_CONT
;
295 virgl_emit_shader_header(ctx
, handle
, len
, type
, offlen
, num_tokens
);
297 virgl_emit_shader_streamout(ctx
, first_pass
? so_info
: NULL
);
299 virgl_encoder_write_block(ctx
->cbuf
, (uint8_t *)sptr
, length
);
303 left_bytes
-= length
;
311 int virgl_encode_clear(struct virgl_context
*ctx
,
313 const union pipe_color_union
*color
,
314 double depth
, unsigned stencil
)
318 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CLEAR
, 0, VIRGL_OBJ_CLEAR_SIZE
));
319 virgl_encoder_write_dword(ctx
->cbuf
, buffers
);
320 for (i
= 0; i
< 4; i
++)
321 virgl_encoder_write_dword(ctx
->cbuf
, color
->ui
[i
]);
322 virgl_encoder_write_qword(ctx
->cbuf
, *(uint64_t *)&depth
);
323 virgl_encoder_write_dword(ctx
->cbuf
, stencil
);
327 int virgl_encoder_set_framebuffer_state(struct virgl_context
*ctx
,
328 const struct pipe_framebuffer_state
*state
)
330 struct virgl_surface
*zsurf
= virgl_surface(state
->zsbuf
);
333 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_FRAMEBUFFER_STATE
, 0, VIRGL_SET_FRAMEBUFFER_STATE_SIZE(state
->nr_cbufs
)));
334 virgl_encoder_write_dword(ctx
->cbuf
, state
->nr_cbufs
);
335 virgl_encoder_write_dword(ctx
->cbuf
, zsurf
? zsurf
->handle
: 0);
336 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
337 struct virgl_surface
*surf
= virgl_surface(state
->cbufs
[i
]);
338 virgl_encoder_write_dword(ctx
->cbuf
, surf
? surf
->handle
: 0);
344 int virgl_encoder_set_viewport_states(struct virgl_context
*ctx
,
347 const struct pipe_viewport_state
*states
)
350 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_VIEWPORT_STATE
, 0, VIRGL_SET_VIEWPORT_STATE_SIZE(num_viewports
)));
351 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
352 for (v
= 0; v
< num_viewports
; v
++) {
353 for (i
= 0; i
< 3; i
++)
354 virgl_encoder_write_dword(ctx
->cbuf
, fui(states
[v
].scale
[i
]));
355 for (i
= 0; i
< 3; i
++)
356 virgl_encoder_write_dword(ctx
->cbuf
, fui(states
[v
].translate
[i
]));
361 int virgl_encoder_create_vertex_elements(struct virgl_context
*ctx
,
363 unsigned num_elements
,
364 const struct pipe_vertex_element
*element
)
367 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_VERTEX_ELEMENTS
, VIRGL_OBJ_VERTEX_ELEMENTS_SIZE(num_elements
)));
368 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
369 for (i
= 0; i
< num_elements
; i
++) {
370 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].src_offset
);
371 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].instance_divisor
);
372 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].vertex_buffer_index
);
373 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].src_format
);
378 int virgl_encoder_set_vertex_buffers(struct virgl_context
*ctx
,
379 unsigned num_buffers
,
380 const struct pipe_vertex_buffer
*buffers
)
383 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_VERTEX_BUFFERS
, 0, VIRGL_SET_VERTEX_BUFFERS_SIZE(num_buffers
)));
384 for (i
= 0; i
< num_buffers
; i
++) {
385 struct virgl_resource
*res
= virgl_resource(buffers
[i
].buffer
);
386 virgl_encoder_write_dword(ctx
->cbuf
, buffers
[i
].stride
);
387 virgl_encoder_write_dword(ctx
->cbuf
, buffers
[i
].buffer_offset
);
388 virgl_encoder_write_res(ctx
, res
);
393 int virgl_encoder_set_index_buffer(struct virgl_context
*ctx
,
394 const struct pipe_index_buffer
*ib
)
396 int length
= VIRGL_SET_INDEX_BUFFER_SIZE(ib
);
397 struct virgl_resource
*res
= NULL
;
399 res
= virgl_resource(ib
->buffer
);
401 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_INDEX_BUFFER
, 0, length
));
402 virgl_encoder_write_res(ctx
, res
);
404 virgl_encoder_write_dword(ctx
->cbuf
, ib
->index_size
);
405 virgl_encoder_write_dword(ctx
->cbuf
, ib
->offset
);
410 int virgl_encoder_draw_vbo(struct virgl_context
*ctx
,
411 const struct pipe_draw_info
*info
)
413 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DRAW_VBO
, 0, VIRGL_DRAW_VBO_SIZE
));
414 virgl_encoder_write_dword(ctx
->cbuf
, info
->start
);
415 virgl_encoder_write_dword(ctx
->cbuf
, info
->count
);
416 virgl_encoder_write_dword(ctx
->cbuf
, info
->mode
);
417 virgl_encoder_write_dword(ctx
->cbuf
, info
->indexed
);
418 virgl_encoder_write_dword(ctx
->cbuf
, info
->instance_count
);
419 virgl_encoder_write_dword(ctx
->cbuf
, info
->index_bias
);
420 virgl_encoder_write_dword(ctx
->cbuf
, info
->start_instance
);
421 virgl_encoder_write_dword(ctx
->cbuf
, info
->primitive_restart
);
422 virgl_encoder_write_dword(ctx
->cbuf
, info
->restart_index
);
423 virgl_encoder_write_dword(ctx
->cbuf
, info
->min_index
);
424 virgl_encoder_write_dword(ctx
->cbuf
, info
->max_index
);
425 if (info
->count_from_stream_output
)
426 virgl_encoder_write_dword(ctx
->cbuf
, info
->count_from_stream_output
->buffer_size
);
428 virgl_encoder_write_dword(ctx
->cbuf
, 0);
432 int virgl_encoder_create_surface(struct virgl_context
*ctx
,
434 struct virgl_resource
*res
,
435 const struct pipe_surface
*templat
)
437 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SURFACE
, VIRGL_OBJ_SURFACE_SIZE
));
438 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
439 virgl_encoder_write_res(ctx
, res
);
440 virgl_encoder_write_dword(ctx
->cbuf
, templat
->format
);
441 if (templat
->texture
->target
== PIPE_BUFFER
) {
442 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.buf
.first_element
);
443 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.buf
.last_element
);
446 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.tex
.level
);
447 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.tex
.first_layer
| (templat
->u
.tex
.last_layer
<< 16));
452 int virgl_encoder_create_so_target(struct virgl_context
*ctx
,
454 struct virgl_resource
*res
,
455 unsigned buffer_offset
,
456 unsigned buffer_size
)
458 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_STREAMOUT_TARGET
, VIRGL_OBJ_STREAMOUT_SIZE
));
459 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
460 virgl_encoder_write_res(ctx
, res
);
461 virgl_encoder_write_dword(ctx
->cbuf
, buffer_offset
);
462 virgl_encoder_write_dword(ctx
->cbuf
, buffer_size
);
466 static void virgl_encoder_iw_emit_header_1d(struct virgl_context
*ctx
,
467 struct virgl_resource
*res
,
468 unsigned level
, unsigned usage
,
469 const struct pipe_box
*box
,
470 unsigned stride
, unsigned layer_stride
)
472 virgl_encoder_write_res(ctx
, res
);
473 virgl_encoder_write_dword(ctx
->cbuf
, level
);
474 virgl_encoder_write_dword(ctx
->cbuf
, usage
);
475 virgl_encoder_write_dword(ctx
->cbuf
, stride
);
476 virgl_encoder_write_dword(ctx
->cbuf
, layer_stride
);
477 virgl_encoder_write_dword(ctx
->cbuf
, box
->x
);
478 virgl_encoder_write_dword(ctx
->cbuf
, box
->y
);
479 virgl_encoder_write_dword(ctx
->cbuf
, box
->z
);
480 virgl_encoder_write_dword(ctx
->cbuf
, box
->width
);
481 virgl_encoder_write_dword(ctx
->cbuf
, box
->height
);
482 virgl_encoder_write_dword(ctx
->cbuf
, box
->depth
);
485 int virgl_encoder_inline_write(struct virgl_context
*ctx
,
486 struct virgl_resource
*res
,
487 unsigned level
, unsigned usage
,
488 const struct pipe_box
*box
,
489 const void *data
, unsigned stride
,
490 unsigned layer_stride
)
492 uint32_t size
= (stride
? stride
: box
->width
) * box
->height
;
493 uint32_t length
, thispass
, left_bytes
;
494 struct pipe_box mybox
= *box
;
496 length
= 11 + (size
+ 3) / 4;
497 if ((ctx
->cbuf
->cdw
+ length
+ 1) > VIRGL_MAX_CMDBUF_DWORDS
) {
498 if (box
->height
> 1 || box
->depth
> 1) {
499 debug_printf("inline transfer failed due to multi dimensions and too large\n");
506 if (ctx
->cbuf
->cdw
+ 12 > VIRGL_MAX_CMDBUF_DWORDS
)
507 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
509 thispass
= (VIRGL_MAX_CMDBUF_DWORDS
- ctx
->cbuf
->cdw
- 12) * 4;
511 length
= MIN2(thispass
, left_bytes
);
513 mybox
.width
= length
;
514 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_INLINE_WRITE
, 0, ((length
+ 3) / 4) + 11));
515 virgl_encoder_iw_emit_header_1d(ctx
, res
, level
, usage
, &mybox
, stride
, layer_stride
);
516 virgl_encoder_write_block(ctx
->cbuf
, data
, length
);
517 left_bytes
-= length
;
524 int virgl_encoder_flush_frontbuffer(struct virgl_context
*ctx
,
525 struct virgl_resource
*res
)
527 // virgl_encoder_write_dword(ctx->cbuf, VIRGL_CMD0(VIRGL_CCMD_FLUSH_FRONTUBFFER, 0, 1));
528 // virgl_encoder_write_dword(ctx->cbuf, res_handle);
532 int virgl_encode_sampler_state(struct virgl_context
*ctx
,
534 const struct pipe_sampler_state
*state
)
538 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SAMPLER_STATE
, VIRGL_OBJ_SAMPLER_STATE_SIZE
));
539 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
541 tmp
= VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_S(state
->wrap_s
) |
542 VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_T(state
->wrap_t
) |
543 VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_R(state
->wrap_r
) |
544 VIRGL_OBJ_SAMPLE_STATE_S0_MIN_IMG_FILTER(state
->min_img_filter
) |
545 VIRGL_OBJ_SAMPLE_STATE_S0_MIN_MIP_FILTER(state
->min_mip_filter
) |
546 VIRGL_OBJ_SAMPLE_STATE_S0_MAG_IMG_FILTER(state
->mag_img_filter
) |
547 VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_MODE(state
->compare_mode
) |
548 VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_FUNC(state
->compare_func
);
550 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
551 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->lod_bias
));
552 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->min_lod
));
553 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->max_lod
));
554 for (i
= 0; i
< 4; i
++)
555 virgl_encoder_write_dword(ctx
->cbuf
, state
->border_color
.ui
[i
]);
560 int virgl_encode_sampler_view(struct virgl_context
*ctx
,
562 struct virgl_resource
*res
,
563 const struct pipe_sampler_view
*state
)
566 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SAMPLER_VIEW
, VIRGL_OBJ_SAMPLER_VIEW_SIZE
));
567 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
568 virgl_encoder_write_res(ctx
, res
);
569 virgl_encoder_write_dword(ctx
->cbuf
, state
->format
);
570 if (res
->u
.b
.target
== PIPE_BUFFER
) {
571 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.buf
.first_element
);
572 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.buf
.last_element
);
574 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.tex
.first_layer
| state
->u
.tex
.last_layer
<< 16);
575 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.tex
.first_level
| state
->u
.tex
.last_level
<< 8);
577 tmp
= VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_R(state
->swizzle_r
) |
578 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_G(state
->swizzle_g
) |
579 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_B(state
->swizzle_b
) |
580 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_A(state
->swizzle_a
);
581 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
585 int virgl_encode_set_sampler_views(struct virgl_context
*ctx
,
586 uint32_t shader_type
,
589 struct virgl_sampler_view
**views
)
592 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLER_VIEWS
, 0, VIRGL_SET_SAMPLER_VIEWS_SIZE(num_views
)));
593 virgl_encoder_write_dword(ctx
->cbuf
, shader_type
);
594 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
595 for (i
= 0; i
< num_views
; i
++) {
596 uint32_t handle
= views
[i
] ? views
[i
]->handle
: 0;
597 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
602 int virgl_encode_bind_sampler_states(struct virgl_context
*ctx
,
603 uint32_t shader_type
,
605 uint32_t num_handles
,
609 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_SAMPLER_STATES
, 0, VIRGL_BIND_SAMPLER_STATES(num_handles
)));
610 virgl_encoder_write_dword(ctx
->cbuf
, shader_type
);
611 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
612 for (i
= 0; i
< num_handles
; i
++)
613 virgl_encoder_write_dword(ctx
->cbuf
, handles
[i
]);
617 int virgl_encoder_write_constant_buffer(struct virgl_context
*ctx
,
623 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_CONSTANT_BUFFER
, 0, size
+ 2));
624 virgl_encoder_write_dword(ctx
->cbuf
, shader
);
625 virgl_encoder_write_dword(ctx
->cbuf
, index
);
627 virgl_encoder_write_block(ctx
->cbuf
, data
, size
* 4);
631 int virgl_encoder_set_uniform_buffer(struct virgl_context
*ctx
,
636 struct virgl_resource
*res
)
638 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_UNIFORM_BUFFER
, 0, VIRGL_SET_UNIFORM_BUFFER_SIZE
));
639 virgl_encoder_write_dword(ctx
->cbuf
, shader
);
640 virgl_encoder_write_dword(ctx
->cbuf
, index
);
641 virgl_encoder_write_dword(ctx
->cbuf
, offset
);
642 virgl_encoder_write_dword(ctx
->cbuf
, length
);
643 virgl_encoder_write_res(ctx
, res
);
648 int virgl_encoder_set_stencil_ref(struct virgl_context
*ctx
,
649 const struct pipe_stencil_ref
*ref
)
651 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_STENCIL_REF
, 0, VIRGL_SET_STENCIL_REF_SIZE
));
652 virgl_encoder_write_dword(ctx
->cbuf
, VIRGL_STENCIL_REF_VAL(ref
->ref_value
[0] , (ref
->ref_value
[1])));
656 int virgl_encoder_set_blend_color(struct virgl_context
*ctx
,
657 const struct pipe_blend_color
*color
)
660 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_BLEND_COLOR
, 0, VIRGL_SET_BLEND_COLOR_SIZE
));
661 for (i
= 0; i
< 4; i
++)
662 virgl_encoder_write_dword(ctx
->cbuf
, fui(color
->color
[i
]));
666 int virgl_encoder_set_scissor_state(struct virgl_context
*ctx
,
669 const struct pipe_scissor_state
*ss
)
672 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SCISSOR_STATE
, 0, VIRGL_SET_SCISSOR_STATE_SIZE(num_scissors
)));
673 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
674 for (i
= 0; i
< num_scissors
; i
++) {
675 virgl_encoder_write_dword(ctx
->cbuf
, (ss
[i
].minx
| ss
[i
].miny
<< 16));
676 virgl_encoder_write_dword(ctx
->cbuf
, (ss
[i
].maxx
| ss
[i
].maxy
<< 16));
681 void virgl_encoder_set_polygon_stipple(struct virgl_context
*ctx
,
682 const struct pipe_poly_stipple
*ps
)
685 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_POLYGON_STIPPLE
, 0, VIRGL_POLYGON_STIPPLE_SIZE
));
686 for (i
= 0; i
< VIRGL_POLYGON_STIPPLE_SIZE
; i
++) {
687 virgl_encoder_write_dword(ctx
->cbuf
, ps
->stipple
[i
]);
691 void virgl_encoder_set_sample_mask(struct virgl_context
*ctx
,
692 unsigned sample_mask
)
694 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLE_MASK
, 0, VIRGL_SET_SAMPLE_MASK_SIZE
));
695 virgl_encoder_write_dword(ctx
->cbuf
, sample_mask
);
698 void virgl_encoder_set_clip_state(struct virgl_context
*ctx
,
699 const struct pipe_clip_state
*clip
)
702 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_CLIP_STATE
, 0, VIRGL_SET_CLIP_STATE_SIZE
));
703 for (i
= 0; i
< VIRGL_MAX_CLIP_PLANES
; i
++) {
704 for (j
= 0; j
< 4; j
++) {
705 virgl_encoder_write_dword(ctx
->cbuf
, fui(clip
->ucp
[i
][j
]));
710 int virgl_encode_resource_copy_region(struct virgl_context
*ctx
,
711 struct virgl_resource
*dst_res
,
713 unsigned dstx
, unsigned dsty
, unsigned dstz
,
714 struct virgl_resource
*src_res
,
716 const struct pipe_box
*src_box
)
718 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_COPY_REGION
, 0, VIRGL_CMD_RESOURCE_COPY_REGION_SIZE
));
719 virgl_encoder_write_res(ctx
, dst_res
);
720 virgl_encoder_write_dword(ctx
->cbuf
, dst_level
);
721 virgl_encoder_write_dword(ctx
->cbuf
, dstx
);
722 virgl_encoder_write_dword(ctx
->cbuf
, dsty
);
723 virgl_encoder_write_dword(ctx
->cbuf
, dstz
);
724 virgl_encoder_write_res(ctx
, src_res
);
725 virgl_encoder_write_dword(ctx
->cbuf
, src_level
);
726 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->x
);
727 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->y
);
728 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->z
);
729 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->width
);
730 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->height
);
731 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->depth
);
735 int virgl_encode_blit(struct virgl_context
*ctx
,
736 struct virgl_resource
*dst_res
,
737 struct virgl_resource
*src_res
,
738 const struct pipe_blit_info
*blit
)
741 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BLIT
, 0, VIRGL_CMD_BLIT_SIZE
));
742 tmp
= VIRGL_CMD_BLIT_S0_MASK(blit
->mask
) |
743 VIRGL_CMD_BLIT_S0_FILTER(blit
->filter
) |
744 VIRGL_CMD_BLIT_S0_SCISSOR_ENABLE(blit
->scissor_enable
);
745 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
746 virgl_encoder_write_dword(ctx
->cbuf
, (blit
->scissor
.minx
| blit
->scissor
.miny
<< 16));
747 virgl_encoder_write_dword(ctx
->cbuf
, (blit
->scissor
.maxx
| blit
->scissor
.maxy
<< 16));
749 virgl_encoder_write_res(ctx
, dst_res
);
750 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.level
);
751 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.format
);
752 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.x
);
753 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.y
);
754 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.z
);
755 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.width
);
756 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.height
);
757 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.depth
);
759 virgl_encoder_write_res(ctx
, src_res
);
760 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.level
);
761 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.format
);
762 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.x
);
763 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.y
);
764 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.z
);
765 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.width
);
766 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.height
);
767 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.depth
);
771 int virgl_encoder_create_query(struct virgl_context
*ctx
,
775 struct virgl_resource
*res
,
778 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_QUERY
, VIRGL_OBJ_QUERY_SIZE
));
779 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
780 virgl_encoder_write_dword(ctx
->cbuf
, ((query_type
& 0xffff) | (query_index
<< 16)));
781 virgl_encoder_write_dword(ctx
->cbuf
, offset
);
782 virgl_encoder_write_res(ctx
, res
);
786 int virgl_encoder_begin_query(struct virgl_context
*ctx
,
789 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BEGIN_QUERY
, 0, 1));
790 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
794 int virgl_encoder_end_query(struct virgl_context
*ctx
,
797 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_END_QUERY
, 0, 1));
798 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
802 int virgl_encoder_get_query_result(struct virgl_context
*ctx
,
803 uint32_t handle
, boolean wait
)
805 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_GET_QUERY_RESULT
, 0, 2));
806 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
807 virgl_encoder_write_dword(ctx
->cbuf
, wait
? 1 : 0);
811 int virgl_encoder_render_condition(struct virgl_context
*ctx
,
812 uint32_t handle
, boolean condition
,
815 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_RENDER_CONDITION
, 0, VIRGL_RENDER_CONDITION_SIZE
));
816 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
817 virgl_encoder_write_dword(ctx
->cbuf
, condition
);
818 virgl_encoder_write_dword(ctx
->cbuf
, mode
);
822 int virgl_encoder_set_so_targets(struct virgl_context
*ctx
,
823 unsigned num_targets
,
824 struct pipe_stream_output_target
**targets
,
825 unsigned append_bitmask
)
829 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_STREAMOUT_TARGETS
, 0, num_targets
+ 1));
830 virgl_encoder_write_dword(ctx
->cbuf
, append_bitmask
);
831 for (i
= 0; i
< num_targets
; i
++) {
832 struct virgl_so_target
*tg
= virgl_so_target(targets
[i
]);
833 virgl_encoder_write_dword(ctx
->cbuf
, tg
->handle
);
839 int virgl_encoder_set_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
841 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SUB_CTX
, 0, 1));
842 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
846 int virgl_encoder_create_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
848 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_SUB_CTX
, 0, 1));
849 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
853 int virgl_encoder_destroy_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
855 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DESTROY_SUB_CTX
, 0, 1));
856 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
860 int virgl_encode_bind_shader(struct virgl_context
*ctx
,
861 uint32_t handle
, uint32_t type
)
863 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_SHADER
, 0, 2));
864 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
865 virgl_encoder_write_dword(ctx
->cbuf
, type
);