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_format.h"
26 #include "util/u_memory.h"
27 #include "util/u_math.h"
28 #include "pipe/p_state.h"
29 #include "tgsi/tgsi_dump.h"
30 #include "tgsi/tgsi_parse.h"
32 #include "virgl_context.h"
33 #include "virgl_encode.h"
34 #include "virgl_protocol.h"
35 #include "virgl_resource.h"
36 #include "virgl_screen.h"
38 static int virgl_encoder_write_cmd_dword(struct virgl_context
*ctx
,
41 int len
= (dword
>> 16);
43 if ((ctx
->cbuf
->cdw
+ len
+ 1) > VIRGL_MAX_CMDBUF_DWORDS
)
44 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
46 virgl_encoder_write_dword(ctx
->cbuf
, dword
);
50 static void virgl_encoder_write_res(struct virgl_context
*ctx
,
51 struct virgl_resource
*res
)
53 struct virgl_winsys
*vws
= virgl_screen(ctx
->base
.screen
)->vws
;
55 if (res
&& res
->hw_res
)
56 vws
->emit_res(vws
, ctx
->cbuf
, res
->hw_res
, TRUE
);
58 virgl_encoder_write_dword(ctx
->cbuf
, 0);
62 int virgl_encode_bind_object(struct virgl_context
*ctx
,
63 uint32_t handle
, uint32_t object
)
65 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_OBJECT
, object
, 1));
66 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
70 int virgl_encode_delete_object(struct virgl_context
*ctx
,
71 uint32_t handle
, uint32_t object
)
73 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DESTROY_OBJECT
, object
, 1));
74 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
78 int virgl_encode_blend_state(struct virgl_context
*ctx
,
80 const struct pipe_blend_state
*blend_state
)
85 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_BLEND
, VIRGL_OBJ_BLEND_SIZE
));
86 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
89 VIRGL_OBJ_BLEND_S0_INDEPENDENT_BLEND_ENABLE(blend_state
->independent_blend_enable
) |
90 VIRGL_OBJ_BLEND_S0_LOGICOP_ENABLE(blend_state
->logicop_enable
) |
91 VIRGL_OBJ_BLEND_S0_DITHER(blend_state
->dither
) |
92 VIRGL_OBJ_BLEND_S0_ALPHA_TO_COVERAGE(blend_state
->alpha_to_coverage
) |
93 VIRGL_OBJ_BLEND_S0_ALPHA_TO_ONE(blend_state
->alpha_to_one
);
95 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
97 tmp
= VIRGL_OBJ_BLEND_S1_LOGICOP_FUNC(blend_state
->logicop_func
);
98 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
100 for (i
= 0; i
< VIRGL_MAX_COLOR_BUFS
; i
++) {
102 VIRGL_OBJ_BLEND_S2_RT_BLEND_ENABLE(blend_state
->rt
[i
].blend_enable
) |
103 VIRGL_OBJ_BLEND_S2_RT_RGB_FUNC(blend_state
->rt
[i
].rgb_func
) |
104 VIRGL_OBJ_BLEND_S2_RT_RGB_SRC_FACTOR(blend_state
->rt
[i
].rgb_src_factor
) |
105 VIRGL_OBJ_BLEND_S2_RT_RGB_DST_FACTOR(blend_state
->rt
[i
].rgb_dst_factor
)|
106 VIRGL_OBJ_BLEND_S2_RT_ALPHA_FUNC(blend_state
->rt
[i
].alpha_func
) |
107 VIRGL_OBJ_BLEND_S2_RT_ALPHA_SRC_FACTOR(blend_state
->rt
[i
].alpha_src_factor
) |
108 VIRGL_OBJ_BLEND_S2_RT_ALPHA_DST_FACTOR(blend_state
->rt
[i
].alpha_dst_factor
) |
109 VIRGL_OBJ_BLEND_S2_RT_COLORMASK(blend_state
->rt
[i
].colormask
);
110 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
115 int virgl_encode_dsa_state(struct virgl_context
*ctx
,
117 const struct pipe_depth_stencil_alpha_state
*dsa_state
)
121 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_DSA
, VIRGL_OBJ_DSA_SIZE
));
122 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
124 tmp
= VIRGL_OBJ_DSA_S0_DEPTH_ENABLE(dsa_state
->depth
.enabled
) |
125 VIRGL_OBJ_DSA_S0_DEPTH_WRITEMASK(dsa_state
->depth
.writemask
) |
126 VIRGL_OBJ_DSA_S0_DEPTH_FUNC(dsa_state
->depth
.func
) |
127 VIRGL_OBJ_DSA_S0_ALPHA_ENABLED(dsa_state
->alpha
.enabled
) |
128 VIRGL_OBJ_DSA_S0_ALPHA_FUNC(dsa_state
->alpha
.func
);
129 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
131 for (i
= 0; i
< 2; i
++) {
132 tmp
= VIRGL_OBJ_DSA_S1_STENCIL_ENABLED(dsa_state
->stencil
[i
].enabled
) |
133 VIRGL_OBJ_DSA_S1_STENCIL_FUNC(dsa_state
->stencil
[i
].func
) |
134 VIRGL_OBJ_DSA_S1_STENCIL_FAIL_OP(dsa_state
->stencil
[i
].fail_op
) |
135 VIRGL_OBJ_DSA_S1_STENCIL_ZPASS_OP(dsa_state
->stencil
[i
].zpass_op
) |
136 VIRGL_OBJ_DSA_S1_STENCIL_ZFAIL_OP(dsa_state
->stencil
[i
].zfail_op
) |
137 VIRGL_OBJ_DSA_S1_STENCIL_VALUEMASK(dsa_state
->stencil
[i
].valuemask
) |
138 VIRGL_OBJ_DSA_S1_STENCIL_WRITEMASK(dsa_state
->stencil
[i
].writemask
);
139 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
142 virgl_encoder_write_dword(ctx
->cbuf
, fui(dsa_state
->alpha
.ref_value
));
145 int virgl_encode_rasterizer_state(struct virgl_context
*ctx
,
147 const struct pipe_rasterizer_state
*state
)
151 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_RASTERIZER
, VIRGL_OBJ_RS_SIZE
));
152 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
154 tmp
= VIRGL_OBJ_RS_S0_FLATSHADE(state
->flatshade
) |
155 VIRGL_OBJ_RS_S0_DEPTH_CLIP(state
->depth_clip
) |
156 VIRGL_OBJ_RS_S0_CLIP_HALFZ(state
->clip_halfz
) |
157 VIRGL_OBJ_RS_S0_RASTERIZER_DISCARD(state
->rasterizer_discard
) |
158 VIRGL_OBJ_RS_S0_FLATSHADE_FIRST(state
->flatshade_first
) |
159 VIRGL_OBJ_RS_S0_LIGHT_TWOSIZE(state
->light_twoside
) |
160 VIRGL_OBJ_RS_S0_SPRITE_COORD_MODE(state
->sprite_coord_mode
) |
161 VIRGL_OBJ_RS_S0_POINT_QUAD_RASTERIZATION(state
->point_quad_rasterization
) |
162 VIRGL_OBJ_RS_S0_CULL_FACE(state
->cull_face
) |
163 VIRGL_OBJ_RS_S0_FILL_FRONT(state
->fill_front
) |
164 VIRGL_OBJ_RS_S0_FILL_BACK(state
->fill_back
) |
165 VIRGL_OBJ_RS_S0_SCISSOR(state
->scissor
) |
166 VIRGL_OBJ_RS_S0_FRONT_CCW(state
->front_ccw
) |
167 VIRGL_OBJ_RS_S0_CLAMP_VERTEX_COLOR(state
->clamp_vertex_color
) |
168 VIRGL_OBJ_RS_S0_CLAMP_FRAGMENT_COLOR(state
->clamp_fragment_color
) |
169 VIRGL_OBJ_RS_S0_OFFSET_LINE(state
->offset_line
) |
170 VIRGL_OBJ_RS_S0_OFFSET_POINT(state
->offset_point
) |
171 VIRGL_OBJ_RS_S0_OFFSET_TRI(state
->offset_tri
) |
172 VIRGL_OBJ_RS_S0_POLY_SMOOTH(state
->poly_smooth
) |
173 VIRGL_OBJ_RS_S0_POLY_STIPPLE_ENABLE(state
->poly_stipple_enable
) |
174 VIRGL_OBJ_RS_S0_POINT_SMOOTH(state
->point_smooth
) |
175 VIRGL_OBJ_RS_S0_POINT_SIZE_PER_VERTEX(state
->point_size_per_vertex
) |
176 VIRGL_OBJ_RS_S0_MULTISAMPLE(state
->multisample
) |
177 VIRGL_OBJ_RS_S0_LINE_SMOOTH(state
->line_smooth
) |
178 VIRGL_OBJ_RS_S0_LINE_STIPPLE_ENABLE(state
->line_stipple_enable
) |
179 VIRGL_OBJ_RS_S0_LINE_LAST_PIXEL(state
->line_last_pixel
) |
180 VIRGL_OBJ_RS_S0_HALF_PIXEL_CENTER(state
->half_pixel_center
) |
181 VIRGL_OBJ_RS_S0_BOTTOM_EDGE_RULE(state
->bottom_edge_rule
);
183 virgl_encoder_write_dword(ctx
->cbuf
, tmp
); /* S0 */
184 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->point_size
)); /* S1 */
185 virgl_encoder_write_dword(ctx
->cbuf
, state
->sprite_coord_enable
); /* S2 */
186 tmp
= VIRGL_OBJ_RS_S3_LINE_STIPPLE_PATTERN(state
->line_stipple_pattern
) |
187 VIRGL_OBJ_RS_S3_LINE_STIPPLE_FACTOR(state
->line_stipple_factor
) |
188 VIRGL_OBJ_RS_S3_CLIP_PLANE_ENABLE(state
->clip_plane_enable
);
189 virgl_encoder_write_dword(ctx
->cbuf
, tmp
); /* S3 */
190 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->line_width
)); /* S4 */
191 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_units
)); /* S5 */
192 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_scale
)); /* S6 */
193 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->offset_clamp
)); /* S7 */
197 static void virgl_emit_shader_header(struct virgl_context
*ctx
,
198 uint32_t handle
, uint32_t len
,
199 uint32_t type
, uint32_t offlen
,
202 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SHADER
, len
));
203 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
204 virgl_encoder_write_dword(ctx
->cbuf
, type
);
205 virgl_encoder_write_dword(ctx
->cbuf
, offlen
);
206 virgl_encoder_write_dword(ctx
->cbuf
, num_tokens
);
209 static void virgl_emit_shader_streamout(struct virgl_context
*ctx
,
210 const struct pipe_stream_output_info
*so_info
)
217 num_outputs
= so_info
->num_outputs
;
219 virgl_encoder_write_dword(ctx
->cbuf
, num_outputs
);
221 for (i
= 0; i
< 4; i
++)
222 virgl_encoder_write_dword(ctx
->cbuf
, so_info
->stride
[i
]);
224 for (i
= 0; i
< so_info
->num_outputs
; i
++) {
226 VIRGL_OBJ_SHADER_SO_OUTPUT_REGISTER_INDEX(so_info
->output
[i
].register_index
) |
227 VIRGL_OBJ_SHADER_SO_OUTPUT_START_COMPONENT(so_info
->output
[i
].start_component
) |
228 VIRGL_OBJ_SHADER_SO_OUTPUT_NUM_COMPONENTS(so_info
->output
[i
].num_components
) |
229 VIRGL_OBJ_SHADER_SO_OUTPUT_BUFFER(so_info
->output
[i
].output_buffer
) |
230 VIRGL_OBJ_SHADER_SO_OUTPUT_DST_OFFSET(so_info
->output
[i
].dst_offset
);
231 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
232 virgl_encoder_write_dword(ctx
->cbuf
, 0);
237 int virgl_encode_shader_state(struct virgl_context
*ctx
,
240 const struct pipe_stream_output_info
*so_info
,
241 const struct tgsi_token
*tokens
)
244 uint32_t shader_len
, len
;
246 int num_tokens
= tgsi_num_tokens(tokens
);
247 int str_total_size
= 65536;
249 uint32_t left_bytes
, base_hdr_size
, strm_hdr_size
, thispass
;
251 str
= CALLOC(1, str_total_size
);
258 bret
= tgsi_dump_str(tokens
, TGSI_DUMP_FLOAT_AS_HEX
, str
, str_total_size
);
260 fprintf(stderr
, "Failed to translate shader in available space - trying again\n");
261 old_size
= str_total_size
;
262 str_total_size
= 65536 * ++retry_size
;
263 str
= REALLOC(str
, old_size
, str_total_size
);
267 } while (bret
== false && retry_size
< 10);
272 shader_len
= strlen(str
) + 1;
274 left_bytes
= shader_len
;
277 strm_hdr_size
= so_info
->num_outputs
? so_info
->num_outputs
* 2 + 4 : 0;
281 uint32_t length
, offlen
;
282 int hdr_len
= base_hdr_size
+ (first_pass
? strm_hdr_size
: 0);
283 if (ctx
->cbuf
->cdw
+ hdr_len
+ 1 > VIRGL_MAX_CMDBUF_DWORDS
)
284 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
286 thispass
= (VIRGL_MAX_CMDBUF_DWORDS
- ctx
->cbuf
->cdw
- hdr_len
- 1) * 4;
288 length
= MIN2(thispass
, left_bytes
);
289 len
= ((length
+ 3) / 4) + hdr_len
;
292 offlen
= VIRGL_OBJ_SHADER_OFFSET_VAL(shader_len
);
294 offlen
= VIRGL_OBJ_SHADER_OFFSET_VAL((uintptr_t)sptr
- (uintptr_t)str
) | VIRGL_OBJ_SHADER_OFFSET_CONT
;
296 virgl_emit_shader_header(ctx
, handle
, len
, type
, offlen
, num_tokens
);
298 virgl_emit_shader_streamout(ctx
, first_pass
? so_info
: NULL
);
300 virgl_encoder_write_block(ctx
->cbuf
, (uint8_t *)sptr
, length
);
304 left_bytes
-= length
;
312 int virgl_encode_clear(struct virgl_context
*ctx
,
314 const union pipe_color_union
*color
,
315 double depth
, unsigned stencil
)
319 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CLEAR
, 0, VIRGL_OBJ_CLEAR_SIZE
));
320 virgl_encoder_write_dword(ctx
->cbuf
, buffers
);
321 for (i
= 0; i
< 4; i
++)
322 virgl_encoder_write_dword(ctx
->cbuf
, color
->ui
[i
]);
323 virgl_encoder_write_qword(ctx
->cbuf
, *(uint64_t *)&depth
);
324 virgl_encoder_write_dword(ctx
->cbuf
, stencil
);
328 int virgl_encoder_set_framebuffer_state(struct virgl_context
*ctx
,
329 const struct pipe_framebuffer_state
*state
)
331 struct virgl_surface
*zsurf
= virgl_surface(state
->zsbuf
);
334 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_FRAMEBUFFER_STATE
, 0, VIRGL_SET_FRAMEBUFFER_STATE_SIZE(state
->nr_cbufs
)));
335 virgl_encoder_write_dword(ctx
->cbuf
, state
->nr_cbufs
);
336 virgl_encoder_write_dword(ctx
->cbuf
, zsurf
? zsurf
->handle
: 0);
337 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
338 struct virgl_surface
*surf
= virgl_surface(state
->cbufs
[i
]);
339 virgl_encoder_write_dword(ctx
->cbuf
, surf
? surf
->handle
: 0);
345 int virgl_encoder_set_viewport_states(struct virgl_context
*ctx
,
348 const struct pipe_viewport_state
*states
)
351 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_VIEWPORT_STATE
, 0, VIRGL_SET_VIEWPORT_STATE_SIZE(num_viewports
)));
352 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
353 for (v
= 0; v
< num_viewports
; v
++) {
354 for (i
= 0; i
< 3; i
++)
355 virgl_encoder_write_dword(ctx
->cbuf
, fui(states
[v
].scale
[i
]));
356 for (i
= 0; i
< 3; i
++)
357 virgl_encoder_write_dword(ctx
->cbuf
, fui(states
[v
].translate
[i
]));
362 int virgl_encoder_create_vertex_elements(struct virgl_context
*ctx
,
364 unsigned num_elements
,
365 const struct pipe_vertex_element
*element
)
368 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_VERTEX_ELEMENTS
, VIRGL_OBJ_VERTEX_ELEMENTS_SIZE(num_elements
)));
369 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
370 for (i
= 0; i
< num_elements
; i
++) {
371 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].src_offset
);
372 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].instance_divisor
);
373 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].vertex_buffer_index
);
374 virgl_encoder_write_dword(ctx
->cbuf
, element
[i
].src_format
);
379 int virgl_encoder_set_vertex_buffers(struct virgl_context
*ctx
,
380 unsigned num_buffers
,
381 const struct pipe_vertex_buffer
*buffers
)
384 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_VERTEX_BUFFERS
, 0, VIRGL_SET_VERTEX_BUFFERS_SIZE(num_buffers
)));
385 for (i
= 0; i
< num_buffers
; i
++) {
386 struct virgl_resource
*res
= virgl_resource(buffers
[i
].buffer
);
387 virgl_encoder_write_dword(ctx
->cbuf
, buffers
[i
].stride
);
388 virgl_encoder_write_dword(ctx
->cbuf
, buffers
[i
].buffer_offset
);
389 virgl_encoder_write_res(ctx
, res
);
394 int virgl_encoder_set_index_buffer(struct virgl_context
*ctx
,
395 const struct pipe_index_buffer
*ib
)
397 int length
= VIRGL_SET_INDEX_BUFFER_SIZE(ib
);
398 struct virgl_resource
*res
= NULL
;
400 res
= virgl_resource(ib
->buffer
);
402 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_INDEX_BUFFER
, 0, length
));
403 virgl_encoder_write_res(ctx
, res
);
405 virgl_encoder_write_dword(ctx
->cbuf
, ib
->index_size
);
406 virgl_encoder_write_dword(ctx
->cbuf
, ib
->offset
);
411 int virgl_encoder_draw_vbo(struct virgl_context
*ctx
,
412 const struct pipe_draw_info
*info
)
414 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DRAW_VBO
, 0, VIRGL_DRAW_VBO_SIZE
));
415 virgl_encoder_write_dword(ctx
->cbuf
, info
->start
);
416 virgl_encoder_write_dword(ctx
->cbuf
, info
->count
);
417 virgl_encoder_write_dword(ctx
->cbuf
, info
->mode
);
418 virgl_encoder_write_dword(ctx
->cbuf
, info
->indexed
);
419 virgl_encoder_write_dword(ctx
->cbuf
, info
->instance_count
);
420 virgl_encoder_write_dword(ctx
->cbuf
, info
->index_bias
);
421 virgl_encoder_write_dword(ctx
->cbuf
, info
->start_instance
);
422 virgl_encoder_write_dword(ctx
->cbuf
, info
->primitive_restart
);
423 virgl_encoder_write_dword(ctx
->cbuf
, info
->restart_index
);
424 virgl_encoder_write_dword(ctx
->cbuf
, info
->min_index
);
425 virgl_encoder_write_dword(ctx
->cbuf
, info
->max_index
);
426 if (info
->count_from_stream_output
)
427 virgl_encoder_write_dword(ctx
->cbuf
, info
->count_from_stream_output
->buffer_size
);
429 virgl_encoder_write_dword(ctx
->cbuf
, 0);
433 int virgl_encoder_create_surface(struct virgl_context
*ctx
,
435 struct virgl_resource
*res
,
436 const struct pipe_surface
*templat
)
438 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SURFACE
, VIRGL_OBJ_SURFACE_SIZE
));
439 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
440 virgl_encoder_write_res(ctx
, res
);
441 virgl_encoder_write_dword(ctx
->cbuf
, templat
->format
);
442 if (templat
->texture
->target
== PIPE_BUFFER
) {
443 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.buf
.first_element
);
444 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.buf
.last_element
);
447 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.tex
.level
);
448 virgl_encoder_write_dword(ctx
->cbuf
, templat
->u
.tex
.first_layer
| (templat
->u
.tex
.last_layer
<< 16));
453 int virgl_encoder_create_so_target(struct virgl_context
*ctx
,
455 struct virgl_resource
*res
,
456 unsigned buffer_offset
,
457 unsigned buffer_size
)
459 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_STREAMOUT_TARGET
, VIRGL_OBJ_STREAMOUT_SIZE
));
460 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
461 virgl_encoder_write_res(ctx
, res
);
462 virgl_encoder_write_dword(ctx
->cbuf
, buffer_offset
);
463 virgl_encoder_write_dword(ctx
->cbuf
, buffer_size
);
467 static void virgl_encoder_iw_emit_header_1d(struct virgl_context
*ctx
,
468 struct virgl_resource
*res
,
469 unsigned level
, unsigned usage
,
470 const struct pipe_box
*box
,
471 unsigned stride
, unsigned layer_stride
)
473 virgl_encoder_write_res(ctx
, res
);
474 virgl_encoder_write_dword(ctx
->cbuf
, level
);
475 virgl_encoder_write_dword(ctx
->cbuf
, usage
);
476 virgl_encoder_write_dword(ctx
->cbuf
, stride
);
477 virgl_encoder_write_dword(ctx
->cbuf
, layer_stride
);
478 virgl_encoder_write_dword(ctx
->cbuf
, box
->x
);
479 virgl_encoder_write_dword(ctx
->cbuf
, box
->y
);
480 virgl_encoder_write_dword(ctx
->cbuf
, box
->z
);
481 virgl_encoder_write_dword(ctx
->cbuf
, box
->width
);
482 virgl_encoder_write_dword(ctx
->cbuf
, box
->height
);
483 virgl_encoder_write_dword(ctx
->cbuf
, box
->depth
);
486 int virgl_encoder_inline_write(struct virgl_context
*ctx
,
487 struct virgl_resource
*res
,
488 unsigned level
, unsigned usage
,
489 const struct pipe_box
*box
,
490 const void *data
, unsigned stride
,
491 unsigned layer_stride
)
493 uint32_t size
= (stride
? stride
: box
->width
) * box
->height
;
494 uint32_t length
, thispass
, left_bytes
;
495 struct pipe_box mybox
= *box
;
497 length
= 11 + (size
+ 3) / 4;
498 if ((ctx
->cbuf
->cdw
+ length
+ 1) > VIRGL_MAX_CMDBUF_DWORDS
) {
499 if (box
->height
> 1 || box
->depth
> 1) {
500 debug_printf("inline transfer failed due to multi dimensions and too large\n");
507 if (ctx
->cbuf
->cdw
+ 12 > VIRGL_MAX_CMDBUF_DWORDS
)
508 ctx
->base
.flush(&ctx
->base
, NULL
, 0);
510 thispass
= (VIRGL_MAX_CMDBUF_DWORDS
- ctx
->cbuf
->cdw
- 12) * 4;
512 length
= MIN2(thispass
, left_bytes
);
514 mybox
.width
= length
;
515 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_INLINE_WRITE
, 0, ((length
+ 3) / 4) + 11));
516 virgl_encoder_iw_emit_header_1d(ctx
, res
, level
, usage
, &mybox
, stride
, layer_stride
);
517 virgl_encoder_write_block(ctx
->cbuf
, data
, length
);
518 left_bytes
-= length
;
525 int virgl_encoder_flush_frontbuffer(struct virgl_context
*ctx
,
526 struct virgl_resource
*res
)
528 // virgl_encoder_write_dword(ctx->cbuf, VIRGL_CMD0(VIRGL_CCMD_FLUSH_FRONTUBFFER, 0, 1));
529 // virgl_encoder_write_dword(ctx->cbuf, res_handle);
533 int virgl_encode_sampler_state(struct virgl_context
*ctx
,
535 const struct pipe_sampler_state
*state
)
539 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SAMPLER_STATE
, VIRGL_OBJ_SAMPLER_STATE_SIZE
));
540 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
542 tmp
= VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_S(state
->wrap_s
) |
543 VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_T(state
->wrap_t
) |
544 VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_R(state
->wrap_r
) |
545 VIRGL_OBJ_SAMPLE_STATE_S0_MIN_IMG_FILTER(state
->min_img_filter
) |
546 VIRGL_OBJ_SAMPLE_STATE_S0_MIN_MIP_FILTER(state
->min_mip_filter
) |
547 VIRGL_OBJ_SAMPLE_STATE_S0_MAG_IMG_FILTER(state
->mag_img_filter
) |
548 VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_MODE(state
->compare_mode
) |
549 VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_FUNC(state
->compare_func
);
551 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
552 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->lod_bias
));
553 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->min_lod
));
554 virgl_encoder_write_dword(ctx
->cbuf
, fui(state
->max_lod
));
555 for (i
= 0; i
< 4; i
++)
556 virgl_encoder_write_dword(ctx
->cbuf
, state
->border_color
.ui
[i
]);
561 int virgl_encode_sampler_view(struct virgl_context
*ctx
,
563 struct virgl_resource
*res
,
564 const struct pipe_sampler_view
*state
)
566 unsigned elem_size
= util_format_get_blocksize(state
->format
);
569 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_SAMPLER_VIEW
, VIRGL_OBJ_SAMPLER_VIEW_SIZE
));
570 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
571 virgl_encoder_write_res(ctx
, res
);
572 virgl_encoder_write_dword(ctx
->cbuf
, state
->format
);
573 if (res
->u
.b
.target
== PIPE_BUFFER
) {
574 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.buf
.offset
/ elem_size
);
575 virgl_encoder_write_dword(ctx
->cbuf
, (state
->u
.buf
.offset
+ state
->u
.buf
.size
) / elem_size
- 1);
577 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.tex
.first_layer
| state
->u
.tex
.last_layer
<< 16);
578 virgl_encoder_write_dword(ctx
->cbuf
, state
->u
.tex
.first_level
| state
->u
.tex
.last_level
<< 8);
580 tmp
= VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_R(state
->swizzle_r
) |
581 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_G(state
->swizzle_g
) |
582 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_B(state
->swizzle_b
) |
583 VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_A(state
->swizzle_a
);
584 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
588 int virgl_encode_set_sampler_views(struct virgl_context
*ctx
,
589 uint32_t shader_type
,
592 struct virgl_sampler_view
**views
)
595 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLER_VIEWS
, 0, VIRGL_SET_SAMPLER_VIEWS_SIZE(num_views
)));
596 virgl_encoder_write_dword(ctx
->cbuf
, shader_type
);
597 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
598 for (i
= 0; i
< num_views
; i
++) {
599 uint32_t handle
= views
[i
] ? views
[i
]->handle
: 0;
600 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
605 int virgl_encode_bind_sampler_states(struct virgl_context
*ctx
,
606 uint32_t shader_type
,
608 uint32_t num_handles
,
612 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_SAMPLER_STATES
, 0, VIRGL_BIND_SAMPLER_STATES(num_handles
)));
613 virgl_encoder_write_dword(ctx
->cbuf
, shader_type
);
614 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
615 for (i
= 0; i
< num_handles
; i
++)
616 virgl_encoder_write_dword(ctx
->cbuf
, handles
[i
]);
620 int virgl_encoder_write_constant_buffer(struct virgl_context
*ctx
,
626 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_CONSTANT_BUFFER
, 0, size
+ 2));
627 virgl_encoder_write_dword(ctx
->cbuf
, shader
);
628 virgl_encoder_write_dword(ctx
->cbuf
, index
);
630 virgl_encoder_write_block(ctx
->cbuf
, data
, size
* 4);
634 int virgl_encoder_set_uniform_buffer(struct virgl_context
*ctx
,
639 struct virgl_resource
*res
)
641 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_UNIFORM_BUFFER
, 0, VIRGL_SET_UNIFORM_BUFFER_SIZE
));
642 virgl_encoder_write_dword(ctx
->cbuf
, shader
);
643 virgl_encoder_write_dword(ctx
->cbuf
, index
);
644 virgl_encoder_write_dword(ctx
->cbuf
, offset
);
645 virgl_encoder_write_dword(ctx
->cbuf
, length
);
646 virgl_encoder_write_res(ctx
, res
);
651 int virgl_encoder_set_stencil_ref(struct virgl_context
*ctx
,
652 const struct pipe_stencil_ref
*ref
)
654 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_STENCIL_REF
, 0, VIRGL_SET_STENCIL_REF_SIZE
));
655 virgl_encoder_write_dword(ctx
->cbuf
, VIRGL_STENCIL_REF_VAL(ref
->ref_value
[0] , (ref
->ref_value
[1])));
659 int virgl_encoder_set_blend_color(struct virgl_context
*ctx
,
660 const struct pipe_blend_color
*color
)
663 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_BLEND_COLOR
, 0, VIRGL_SET_BLEND_COLOR_SIZE
));
664 for (i
= 0; i
< 4; i
++)
665 virgl_encoder_write_dword(ctx
->cbuf
, fui(color
->color
[i
]));
669 int virgl_encoder_set_scissor_state(struct virgl_context
*ctx
,
672 const struct pipe_scissor_state
*ss
)
675 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SCISSOR_STATE
, 0, VIRGL_SET_SCISSOR_STATE_SIZE(num_scissors
)));
676 virgl_encoder_write_dword(ctx
->cbuf
, start_slot
);
677 for (i
= 0; i
< num_scissors
; i
++) {
678 virgl_encoder_write_dword(ctx
->cbuf
, (ss
[i
].minx
| ss
[i
].miny
<< 16));
679 virgl_encoder_write_dword(ctx
->cbuf
, (ss
[i
].maxx
| ss
[i
].maxy
<< 16));
684 void virgl_encoder_set_polygon_stipple(struct virgl_context
*ctx
,
685 const struct pipe_poly_stipple
*ps
)
688 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_POLYGON_STIPPLE
, 0, VIRGL_POLYGON_STIPPLE_SIZE
));
689 for (i
= 0; i
< VIRGL_POLYGON_STIPPLE_SIZE
; i
++) {
690 virgl_encoder_write_dword(ctx
->cbuf
, ps
->stipple
[i
]);
694 void virgl_encoder_set_sample_mask(struct virgl_context
*ctx
,
695 unsigned sample_mask
)
697 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLE_MASK
, 0, VIRGL_SET_SAMPLE_MASK_SIZE
));
698 virgl_encoder_write_dword(ctx
->cbuf
, sample_mask
);
701 void virgl_encoder_set_clip_state(struct virgl_context
*ctx
,
702 const struct pipe_clip_state
*clip
)
705 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_CLIP_STATE
, 0, VIRGL_SET_CLIP_STATE_SIZE
));
706 for (i
= 0; i
< VIRGL_MAX_CLIP_PLANES
; i
++) {
707 for (j
= 0; j
< 4; j
++) {
708 virgl_encoder_write_dword(ctx
->cbuf
, fui(clip
->ucp
[i
][j
]));
713 int virgl_encode_resource_copy_region(struct virgl_context
*ctx
,
714 struct virgl_resource
*dst_res
,
716 unsigned dstx
, unsigned dsty
, unsigned dstz
,
717 struct virgl_resource
*src_res
,
719 const struct pipe_box
*src_box
)
721 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_COPY_REGION
, 0, VIRGL_CMD_RESOURCE_COPY_REGION_SIZE
));
722 virgl_encoder_write_res(ctx
, dst_res
);
723 virgl_encoder_write_dword(ctx
->cbuf
, dst_level
);
724 virgl_encoder_write_dword(ctx
->cbuf
, dstx
);
725 virgl_encoder_write_dword(ctx
->cbuf
, dsty
);
726 virgl_encoder_write_dword(ctx
->cbuf
, dstz
);
727 virgl_encoder_write_res(ctx
, src_res
);
728 virgl_encoder_write_dword(ctx
->cbuf
, src_level
);
729 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->x
);
730 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->y
);
731 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->z
);
732 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->width
);
733 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->height
);
734 virgl_encoder_write_dword(ctx
->cbuf
, src_box
->depth
);
738 int virgl_encode_blit(struct virgl_context
*ctx
,
739 struct virgl_resource
*dst_res
,
740 struct virgl_resource
*src_res
,
741 const struct pipe_blit_info
*blit
)
744 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BLIT
, 0, VIRGL_CMD_BLIT_SIZE
));
745 tmp
= VIRGL_CMD_BLIT_S0_MASK(blit
->mask
) |
746 VIRGL_CMD_BLIT_S0_FILTER(blit
->filter
) |
747 VIRGL_CMD_BLIT_S0_SCISSOR_ENABLE(blit
->scissor_enable
) |
748 VIRGL_CMD_BLIT_S0_RENDER_CONDITION_ENABLE(blit
->render_condition_enable
) |
749 VIRGL_CMD_BLIT_S0_ALPHA_BLEND(blit
->alpha_blend
);
750 virgl_encoder_write_dword(ctx
->cbuf
, tmp
);
751 virgl_encoder_write_dword(ctx
->cbuf
, (blit
->scissor
.minx
| blit
->scissor
.miny
<< 16));
752 virgl_encoder_write_dword(ctx
->cbuf
, (blit
->scissor
.maxx
| blit
->scissor
.maxy
<< 16));
754 virgl_encoder_write_res(ctx
, dst_res
);
755 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.level
);
756 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.format
);
757 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.x
);
758 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.y
);
759 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.z
);
760 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.width
);
761 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.height
);
762 virgl_encoder_write_dword(ctx
->cbuf
, blit
->dst
.box
.depth
);
764 virgl_encoder_write_res(ctx
, src_res
);
765 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.level
);
766 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.format
);
767 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.x
);
768 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.y
);
769 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.z
);
770 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.width
);
771 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.height
);
772 virgl_encoder_write_dword(ctx
->cbuf
, blit
->src
.box
.depth
);
776 int virgl_encoder_create_query(struct virgl_context
*ctx
,
780 struct virgl_resource
*res
,
783 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT
, VIRGL_OBJECT_QUERY
, VIRGL_OBJ_QUERY_SIZE
));
784 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
785 virgl_encoder_write_dword(ctx
->cbuf
, ((query_type
& 0xffff) | (query_index
<< 16)));
786 virgl_encoder_write_dword(ctx
->cbuf
, offset
);
787 virgl_encoder_write_res(ctx
, res
);
791 int virgl_encoder_begin_query(struct virgl_context
*ctx
,
794 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BEGIN_QUERY
, 0, 1));
795 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
799 int virgl_encoder_end_query(struct virgl_context
*ctx
,
802 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_END_QUERY
, 0, 1));
803 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
807 int virgl_encoder_get_query_result(struct virgl_context
*ctx
,
808 uint32_t handle
, boolean wait
)
810 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_GET_QUERY_RESULT
, 0, 2));
811 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
812 virgl_encoder_write_dword(ctx
->cbuf
, wait
? 1 : 0);
816 int virgl_encoder_render_condition(struct virgl_context
*ctx
,
817 uint32_t handle
, boolean condition
,
820 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_RENDER_CONDITION
, 0, VIRGL_RENDER_CONDITION_SIZE
));
821 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
822 virgl_encoder_write_dword(ctx
->cbuf
, condition
);
823 virgl_encoder_write_dword(ctx
->cbuf
, mode
);
827 int virgl_encoder_set_so_targets(struct virgl_context
*ctx
,
828 unsigned num_targets
,
829 struct pipe_stream_output_target
**targets
,
830 unsigned append_bitmask
)
834 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_STREAMOUT_TARGETS
, 0, num_targets
+ 1));
835 virgl_encoder_write_dword(ctx
->cbuf
, append_bitmask
);
836 for (i
= 0; i
< num_targets
; i
++) {
837 struct virgl_so_target
*tg
= virgl_so_target(targets
[i
]);
838 virgl_encoder_write_dword(ctx
->cbuf
, tg
->handle
);
844 int virgl_encoder_set_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
846 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_SET_SUB_CTX
, 0, 1));
847 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
851 int virgl_encoder_create_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
853 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_CREATE_SUB_CTX
, 0, 1));
854 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
858 int virgl_encoder_destroy_sub_ctx(struct virgl_context
*ctx
, uint32_t sub_ctx_id
)
860 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_DESTROY_SUB_CTX
, 0, 1));
861 virgl_encoder_write_dword(ctx
->cbuf
, sub_ctx_id
);
865 int virgl_encode_bind_shader(struct virgl_context
*ctx
,
866 uint32_t handle
, uint32_t type
)
868 virgl_encoder_write_cmd_dword(ctx
, VIRGL_CMD0(VIRGL_CCMD_BIND_SHADER
, 0, 2));
869 virgl_encoder_write_dword(ctx
->cbuf
, handle
);
870 virgl_encoder_write_dword(ctx
->cbuf
, type
);