Path: blob/21.2-virgl/src/gallium/drivers/virgl/virgl_encode.c
4570 views
/*1* Copyright 2014, 2015 Red Hat.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/22#include <stdint.h>23#include <assert.h>24#include <string.h>2526#include "util/format/u_format.h"27#include "util/u_memory.h"28#include "util/u_math.h"29#include "pipe/p_state.h"30#include "tgsi/tgsi_dump.h"31#include "tgsi/tgsi_parse.h"3233#include "virgl_context.h"34#include "virgl_encode.h"35#include "virtio-gpu/virgl_protocol.h"36#include "virgl_resource.h"37#include "virgl_screen.h"3839#define VIRGL_ENCODE_MAX_DWORDS MIN2(VIRGL_MAX_CMDBUF_DWORDS, VIRGL_CMD0_MAX_DWORDS)4041#define CONV_FORMAT(f) [PIPE_FORMAT_##f] = VIRGL_FORMAT_##f,4243static const enum virgl_formats virgl_formats_conv_table[PIPE_FORMAT_COUNT] = {44CONV_FORMAT(B8G8R8A8_UNORM)45CONV_FORMAT(B8G8R8X8_UNORM)46CONV_FORMAT(A8R8G8B8_UNORM)47CONV_FORMAT(X8R8G8B8_UNORM)48CONV_FORMAT(B5G5R5A1_UNORM)49CONV_FORMAT(B4G4R4A4_UNORM)50CONV_FORMAT(B5G6R5_UNORM)51CONV_FORMAT(R10G10B10A2_UNORM)52CONV_FORMAT(L8_UNORM)53CONV_FORMAT(A8_UNORM)54CONV_FORMAT(L8A8_UNORM)55CONV_FORMAT(L16_UNORM)56CONV_FORMAT(Z16_UNORM)57CONV_FORMAT(Z32_UNORM)58CONV_FORMAT(Z32_FLOAT)59CONV_FORMAT(Z24_UNORM_S8_UINT)60CONV_FORMAT(S8_UINT_Z24_UNORM)61CONV_FORMAT(Z24X8_UNORM)62CONV_FORMAT(X8Z24_UNORM)63CONV_FORMAT(S8_UINT)64CONV_FORMAT(R64_FLOAT)65CONV_FORMAT(R64G64_FLOAT)66CONV_FORMAT(R64G64B64_FLOAT)67CONV_FORMAT(R64G64B64A64_FLOAT)68CONV_FORMAT(R32_FLOAT)69CONV_FORMAT(R32G32_FLOAT)70CONV_FORMAT(R32G32B32_FLOAT)71CONV_FORMAT(R32G32B32A32_FLOAT)72CONV_FORMAT(R32_UNORM)73CONV_FORMAT(R32G32_UNORM)74CONV_FORMAT(R32G32B32_UNORM)75CONV_FORMAT(R32G32B32A32_UNORM)76CONV_FORMAT(R32_USCALED)77CONV_FORMAT(R32G32_USCALED)78CONV_FORMAT(R32G32B32_USCALED)79CONV_FORMAT(R32G32B32A32_USCALED)80CONV_FORMAT(R32_SNORM)81CONV_FORMAT(R32G32_SNORM)82CONV_FORMAT(R32G32B32_SNORM)83CONV_FORMAT(R32G32B32A32_SNORM)84CONV_FORMAT(R32_SSCALED)85CONV_FORMAT(R32G32_SSCALED)86CONV_FORMAT(R32G32B32_SSCALED)87CONV_FORMAT(R32G32B32A32_SSCALED)88CONV_FORMAT(R16_UNORM)89CONV_FORMAT(R16G16_UNORM)90CONV_FORMAT(R16G16B16_UNORM)91CONV_FORMAT(R16G16B16A16_UNORM)92CONV_FORMAT(R16_USCALED)93CONV_FORMAT(R16G16_USCALED)94CONV_FORMAT(R16G16B16_USCALED)95CONV_FORMAT(R16G16B16A16_USCALED)96CONV_FORMAT(R16_SNORM)97CONV_FORMAT(R16G16_SNORM)98CONV_FORMAT(R16G16B16_SNORM)99CONV_FORMAT(R16G16B16A16_SNORM)100CONV_FORMAT(R16_SSCALED)101CONV_FORMAT(R16G16_SSCALED)102CONV_FORMAT(R16G16B16_SSCALED)103CONV_FORMAT(R16G16B16A16_SSCALED)104CONV_FORMAT(R8_UNORM)105CONV_FORMAT(R8G8_UNORM)106CONV_FORMAT(R8G8B8_UNORM)107CONV_FORMAT(R8G8B8A8_UNORM)108CONV_FORMAT(R8_USCALED)109CONV_FORMAT(R8G8_USCALED)110CONV_FORMAT(R8G8B8_USCALED)111CONV_FORMAT(R8G8B8A8_USCALED)112CONV_FORMAT(R8_SNORM)113CONV_FORMAT(R8G8_SNORM)114CONV_FORMAT(R8G8B8_SNORM)115CONV_FORMAT(R8G8B8A8_SNORM)116CONV_FORMAT(R8_SSCALED)117CONV_FORMAT(R8G8_SSCALED)118CONV_FORMAT(R8G8B8_SSCALED)119CONV_FORMAT(R8G8B8A8_SSCALED)120CONV_FORMAT(R16_FLOAT)121CONV_FORMAT(R16G16_FLOAT)122CONV_FORMAT(R16G16B16_FLOAT)123CONV_FORMAT(R16G16B16A16_FLOAT)124CONV_FORMAT(L8_SRGB)125CONV_FORMAT(L8A8_SRGB)126CONV_FORMAT(R8G8B8_SRGB)127CONV_FORMAT(A8B8G8R8_SRGB)128CONV_FORMAT(X8B8G8R8_SRGB)129CONV_FORMAT(B8G8R8A8_SRGB)130CONV_FORMAT(B8G8R8X8_SRGB)131CONV_FORMAT(A8R8G8B8_SRGB)132CONV_FORMAT(X8R8G8B8_SRGB)133CONV_FORMAT(R8G8B8A8_SRGB)134CONV_FORMAT(DXT1_RGB)135CONV_FORMAT(DXT1_RGBA)136CONV_FORMAT(DXT3_RGBA)137CONV_FORMAT(DXT5_RGBA)138CONV_FORMAT(DXT1_SRGB)139CONV_FORMAT(DXT1_SRGBA)140CONV_FORMAT(DXT3_SRGBA)141CONV_FORMAT(DXT5_SRGBA)142CONV_FORMAT(RGTC1_UNORM)143CONV_FORMAT(RGTC1_SNORM)144CONV_FORMAT(RGTC2_UNORM)145CONV_FORMAT(RGTC2_SNORM)146CONV_FORMAT(A8B8G8R8_UNORM)147CONV_FORMAT(B5G5R5X1_UNORM)148CONV_FORMAT(R10G10B10A2_USCALED)149CONV_FORMAT(R11G11B10_FLOAT)150CONV_FORMAT(R9G9B9E5_FLOAT)151CONV_FORMAT(Z32_FLOAT_S8X24_UINT)152CONV_FORMAT(B10G10R10A2_UNORM)153CONV_FORMAT(R8G8B8X8_UNORM)154CONV_FORMAT(B4G4R4X4_UNORM)155CONV_FORMAT(X24S8_UINT)156CONV_FORMAT(S8X24_UINT)157CONV_FORMAT(X32_S8X24_UINT)158CONV_FORMAT(B2G3R3_UNORM)159CONV_FORMAT(L16A16_UNORM)160CONV_FORMAT(A16_UNORM)161CONV_FORMAT(I16_UNORM)162CONV_FORMAT(LATC1_UNORM)163CONV_FORMAT(LATC1_SNORM)164CONV_FORMAT(LATC2_UNORM)165CONV_FORMAT(LATC2_SNORM)166CONV_FORMAT(A8_SNORM)167CONV_FORMAT(L8_SNORM)168CONV_FORMAT(L8A8_SNORM)169CONV_FORMAT(A16_SNORM)170CONV_FORMAT(L16_SNORM)171CONV_FORMAT(L16A16_SNORM)172CONV_FORMAT(A16_FLOAT)173CONV_FORMAT(L16_FLOAT)174CONV_FORMAT(L16A16_FLOAT)175CONV_FORMAT(A32_FLOAT)176CONV_FORMAT(L32_FLOAT)177CONV_FORMAT(L32A32_FLOAT)178CONV_FORMAT(YV12)179CONV_FORMAT(YV16)180CONV_FORMAT(IYUV)181CONV_FORMAT(NV12)182CONV_FORMAT(NV21)183CONV_FORMAT(R8_UINT)184CONV_FORMAT(R8G8_UINT)185CONV_FORMAT(R8G8B8_UINT)186CONV_FORMAT(R8G8B8A8_UINT)187CONV_FORMAT(R8_SINT)188CONV_FORMAT(R8G8_SINT)189CONV_FORMAT(R8G8B8_SINT)190CONV_FORMAT(R8G8B8A8_SINT)191CONV_FORMAT(R16_UINT)192CONV_FORMAT(R16G16_UINT)193CONV_FORMAT(R16G16B16_UINT)194CONV_FORMAT(R16G16B16A16_UINT)195CONV_FORMAT(R16_SINT)196CONV_FORMAT(R16G16_SINT)197CONV_FORMAT(R16G16B16_SINT)198CONV_FORMAT(R16G16B16A16_SINT)199CONV_FORMAT(R32_UINT)200CONV_FORMAT(R32G32_UINT)201CONV_FORMAT(R32G32B32_UINT)202CONV_FORMAT(R32G32B32A32_UINT)203CONV_FORMAT(R32_SINT)204CONV_FORMAT(R32G32_SINT)205CONV_FORMAT(R32G32B32_SINT)206CONV_FORMAT(R32G32B32A32_SINT)207CONV_FORMAT(A8_UINT)208CONV_FORMAT(L8_UINT)209CONV_FORMAT(L8A8_UINT)210CONV_FORMAT(A8_SINT)211CONV_FORMAT(L8_SINT)212CONV_FORMAT(L8A8_SINT)213CONV_FORMAT(A16_UINT)214CONV_FORMAT(L16_UINT)215CONV_FORMAT(L16A16_UINT)216CONV_FORMAT(A16_SINT)217CONV_FORMAT(L16_SINT)218CONV_FORMAT(L16A16_SINT)219CONV_FORMAT(A32_UINT)220CONV_FORMAT(L32_UINT)221CONV_FORMAT(L32A32_UINT)222CONV_FORMAT(A32_SINT)223CONV_FORMAT(L32_SINT)224CONV_FORMAT(L32A32_SINT)225CONV_FORMAT(R10G10B10A2_SSCALED)226CONV_FORMAT(R10G10B10A2_SNORM)227CONV_FORMAT(B10G10R10A2_SNORM)228CONV_FORMAT(B10G10R10A2_UINT)229CONV_FORMAT(R8G8B8X8_SNORM)230CONV_FORMAT(R8G8B8X8_SRGB)231CONV_FORMAT(R8G8B8X8_UINT)232CONV_FORMAT(R8G8B8X8_SINT)233CONV_FORMAT(B10G10R10X2_UNORM)234CONV_FORMAT(R16G16B16X16_UNORM)235CONV_FORMAT(R16G16B16X16_SNORM)236CONV_FORMAT(R16G16B16X16_FLOAT)237CONV_FORMAT(R16G16B16X16_UINT)238CONV_FORMAT(R16G16B16X16_SINT)239CONV_FORMAT(R32G32B32X32_FLOAT)240CONV_FORMAT(R32G32B32X32_UINT)241CONV_FORMAT(R32G32B32X32_SINT)242CONV_FORMAT(R10G10B10A2_UINT)243CONV_FORMAT(BPTC_RGBA_UNORM)244CONV_FORMAT(BPTC_SRGBA)245CONV_FORMAT(BPTC_RGB_FLOAT)246CONV_FORMAT(BPTC_RGB_UFLOAT)247CONV_FORMAT(R10G10B10X2_UNORM)248CONV_FORMAT(A4B4G4R4_UNORM)249CONV_FORMAT(R8_SRGB)250CONV_FORMAT(R8G8_SRGB)251CONV_FORMAT(ETC1_RGB8)252CONV_FORMAT(ETC2_RGB8)253CONV_FORMAT(ETC2_SRGB8)254CONV_FORMAT(ETC2_RGB8A1)255CONV_FORMAT(ETC2_SRGB8A1)256CONV_FORMAT(ETC2_RGBA8)257CONV_FORMAT(ETC2_SRGBA8)258CONV_FORMAT(ETC2_R11_UNORM)259CONV_FORMAT(ETC2_R11_SNORM)260CONV_FORMAT(ETC2_RG11_UNORM)261CONV_FORMAT(ETC2_RG11_SNORM)262CONV_FORMAT(ASTC_4x4)263CONV_FORMAT(ASTC_5x4)264CONV_FORMAT(ASTC_5x5)265CONV_FORMAT(ASTC_6x5)266CONV_FORMAT(ASTC_6x6)267CONV_FORMAT(ASTC_8x5)268CONV_FORMAT(ASTC_8x6)269CONV_FORMAT(ASTC_8x8)270CONV_FORMAT(ASTC_10x5)271CONV_FORMAT(ASTC_10x6)272CONV_FORMAT(ASTC_10x8)273CONV_FORMAT(ASTC_10x10)274CONV_FORMAT(ASTC_12x10)275CONV_FORMAT(ASTC_12x12)276CONV_FORMAT(ASTC_4x4_SRGB)277CONV_FORMAT(ASTC_5x4_SRGB)278CONV_FORMAT(ASTC_5x5_SRGB)279CONV_FORMAT(ASTC_6x5_SRGB)280CONV_FORMAT(ASTC_6x6_SRGB)281CONV_FORMAT(ASTC_8x5_SRGB)282CONV_FORMAT(ASTC_8x6_SRGB)283CONV_FORMAT(ASTC_8x8_SRGB )284CONV_FORMAT(ASTC_10x5_SRGB)285CONV_FORMAT(ASTC_10x6_SRGB)286CONV_FORMAT(ASTC_10x8_SRGB)287CONV_FORMAT(ASTC_10x10_SRGB)288CONV_FORMAT(ASTC_12x10_SRGB)289CONV_FORMAT(ASTC_12x12_SRGB)290};291292enum virgl_formats pipe_to_virgl_format(enum pipe_format format)293{294enum virgl_formats vformat = virgl_formats_conv_table[format];295if (format != PIPE_FORMAT_NONE && !vformat)296debug_printf("VIRGL: pipe format %s not in the format table\n", util_format_name(format));297return vformat;298}299300static int virgl_encoder_write_cmd_dword(struct virgl_context *ctx,301uint32_t dword)302{303int len = (dword >> 16);304305if ((ctx->cbuf->cdw + len + 1) > VIRGL_MAX_CMDBUF_DWORDS)306ctx->base.flush(&ctx->base, NULL, 0);307308virgl_encoder_write_dword(ctx->cbuf, dword);309return 0;310}311312static void virgl_encoder_emit_resource(struct virgl_screen *vs,313struct virgl_cmd_buf *buf,314struct virgl_resource *res)315{316struct virgl_winsys *vws = vs->vws;317if (res && res->hw_res)318vws->emit_res(vws, buf, res->hw_res, TRUE);319else {320virgl_encoder_write_dword(buf, 0);321}322}323324static void virgl_encoder_write_res(struct virgl_context *ctx,325struct virgl_resource *res)326{327struct virgl_screen *vs = virgl_screen(ctx->base.screen);328virgl_encoder_emit_resource(vs, ctx->cbuf, res);329}330331int virgl_encode_bind_object(struct virgl_context *ctx,332uint32_t handle, uint32_t object)333{334virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_BIND_OBJECT, object, 1));335virgl_encoder_write_dword(ctx->cbuf, handle);336return 0;337}338339int virgl_encode_delete_object(struct virgl_context *ctx,340uint32_t handle, uint32_t object)341{342virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_DESTROY_OBJECT, object, 1));343virgl_encoder_write_dword(ctx->cbuf, handle);344return 0;345}346347int virgl_encode_blend_state(struct virgl_context *ctx,348uint32_t handle,349const struct pipe_blend_state *blend_state)350{351uint32_t tmp;352int i;353354virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_BLEND, VIRGL_OBJ_BLEND_SIZE));355virgl_encoder_write_dword(ctx->cbuf, handle);356357tmp =358VIRGL_OBJ_BLEND_S0_INDEPENDENT_BLEND_ENABLE(blend_state->independent_blend_enable) |359VIRGL_OBJ_BLEND_S0_LOGICOP_ENABLE(blend_state->logicop_enable) |360VIRGL_OBJ_BLEND_S0_DITHER(blend_state->dither) |361VIRGL_OBJ_BLEND_S0_ALPHA_TO_COVERAGE(blend_state->alpha_to_coverage) |362VIRGL_OBJ_BLEND_S0_ALPHA_TO_ONE(blend_state->alpha_to_one);363364virgl_encoder_write_dword(ctx->cbuf, tmp);365366tmp = VIRGL_OBJ_BLEND_S1_LOGICOP_FUNC(blend_state->logicop_func);367virgl_encoder_write_dword(ctx->cbuf, tmp);368369for (i = 0; i < VIRGL_MAX_COLOR_BUFS; i++) {370/* We use alpha src factor to pass the advanced blend equation value371* to the host. By doing so, we don't have to change the protocol.372*/373uint32_t alpha = (i == 0 && blend_state->advanced_blend_func)374? blend_state->advanced_blend_func375: blend_state->rt[i].alpha_src_factor;376tmp =377VIRGL_OBJ_BLEND_S2_RT_BLEND_ENABLE(blend_state->rt[i].blend_enable) |378VIRGL_OBJ_BLEND_S2_RT_RGB_FUNC(blend_state->rt[i].rgb_func) |379VIRGL_OBJ_BLEND_S2_RT_RGB_SRC_FACTOR(blend_state->rt[i].rgb_src_factor) |380VIRGL_OBJ_BLEND_S2_RT_RGB_DST_FACTOR(blend_state->rt[i].rgb_dst_factor)|381VIRGL_OBJ_BLEND_S2_RT_ALPHA_FUNC(blend_state->rt[i].alpha_func) |382VIRGL_OBJ_BLEND_S2_RT_ALPHA_SRC_FACTOR(alpha) |383VIRGL_OBJ_BLEND_S2_RT_ALPHA_DST_FACTOR(blend_state->rt[i].alpha_dst_factor) |384VIRGL_OBJ_BLEND_S2_RT_COLORMASK(blend_state->rt[i].colormask);385virgl_encoder_write_dword(ctx->cbuf, tmp);386}387return 0;388}389390int virgl_encode_dsa_state(struct virgl_context *ctx,391uint32_t handle,392const struct pipe_depth_stencil_alpha_state *dsa_state)393{394uint32_t tmp;395int i;396virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_DSA, VIRGL_OBJ_DSA_SIZE));397virgl_encoder_write_dword(ctx->cbuf, handle);398399tmp = VIRGL_OBJ_DSA_S0_DEPTH_ENABLE(dsa_state->depth_enabled) |400VIRGL_OBJ_DSA_S0_DEPTH_WRITEMASK(dsa_state->depth_writemask) |401VIRGL_OBJ_DSA_S0_DEPTH_FUNC(dsa_state->depth_func) |402VIRGL_OBJ_DSA_S0_ALPHA_ENABLED(dsa_state->alpha_enabled) |403VIRGL_OBJ_DSA_S0_ALPHA_FUNC(dsa_state->alpha_func);404virgl_encoder_write_dword(ctx->cbuf, tmp);405406for (i = 0; i < 2; i++) {407tmp = VIRGL_OBJ_DSA_S1_STENCIL_ENABLED(dsa_state->stencil[i].enabled) |408VIRGL_OBJ_DSA_S1_STENCIL_FUNC(dsa_state->stencil[i].func) |409VIRGL_OBJ_DSA_S1_STENCIL_FAIL_OP(dsa_state->stencil[i].fail_op) |410VIRGL_OBJ_DSA_S1_STENCIL_ZPASS_OP(dsa_state->stencil[i].zpass_op) |411VIRGL_OBJ_DSA_S1_STENCIL_ZFAIL_OP(dsa_state->stencil[i].zfail_op) |412VIRGL_OBJ_DSA_S1_STENCIL_VALUEMASK(dsa_state->stencil[i].valuemask) |413VIRGL_OBJ_DSA_S1_STENCIL_WRITEMASK(dsa_state->stencil[i].writemask);414virgl_encoder_write_dword(ctx->cbuf, tmp);415}416417virgl_encoder_write_dword(ctx->cbuf, fui(dsa_state->alpha_ref_value));418return 0;419}420int virgl_encode_rasterizer_state(struct virgl_context *ctx,421uint32_t handle,422const struct pipe_rasterizer_state *state)423{424uint32_t tmp;425426virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_RASTERIZER, VIRGL_OBJ_RS_SIZE));427virgl_encoder_write_dword(ctx->cbuf, handle);428429tmp = VIRGL_OBJ_RS_S0_FLATSHADE(state->flatshade) |430VIRGL_OBJ_RS_S0_DEPTH_CLIP(state->depth_clip_near) |431VIRGL_OBJ_RS_S0_CLIP_HALFZ(state->clip_halfz) |432VIRGL_OBJ_RS_S0_RASTERIZER_DISCARD(state->rasterizer_discard) |433VIRGL_OBJ_RS_S0_FLATSHADE_FIRST(state->flatshade_first) |434VIRGL_OBJ_RS_S0_LIGHT_TWOSIZE(state->light_twoside) |435VIRGL_OBJ_RS_S0_SPRITE_COORD_MODE(state->sprite_coord_mode) |436VIRGL_OBJ_RS_S0_POINT_QUAD_RASTERIZATION(state->point_quad_rasterization) |437VIRGL_OBJ_RS_S0_CULL_FACE(state->cull_face) |438VIRGL_OBJ_RS_S0_FILL_FRONT(state->fill_front) |439VIRGL_OBJ_RS_S0_FILL_BACK(state->fill_back) |440VIRGL_OBJ_RS_S0_SCISSOR(state->scissor) |441VIRGL_OBJ_RS_S0_FRONT_CCW(state->front_ccw) |442VIRGL_OBJ_RS_S0_CLAMP_VERTEX_COLOR(state->clamp_vertex_color) |443VIRGL_OBJ_RS_S0_CLAMP_FRAGMENT_COLOR(state->clamp_fragment_color) |444VIRGL_OBJ_RS_S0_OFFSET_LINE(state->offset_line) |445VIRGL_OBJ_RS_S0_OFFSET_POINT(state->offset_point) |446VIRGL_OBJ_RS_S0_OFFSET_TRI(state->offset_tri) |447VIRGL_OBJ_RS_S0_POLY_SMOOTH(state->poly_smooth) |448VIRGL_OBJ_RS_S0_POLY_STIPPLE_ENABLE(state->poly_stipple_enable) |449VIRGL_OBJ_RS_S0_POINT_SMOOTH(state->point_smooth) |450VIRGL_OBJ_RS_S0_POINT_SIZE_PER_VERTEX(state->point_size_per_vertex) |451VIRGL_OBJ_RS_S0_MULTISAMPLE(state->multisample) |452VIRGL_OBJ_RS_S0_LINE_SMOOTH(state->line_smooth) |453VIRGL_OBJ_RS_S0_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |454VIRGL_OBJ_RS_S0_LINE_LAST_PIXEL(state->line_last_pixel) |455VIRGL_OBJ_RS_S0_HALF_PIXEL_CENTER(state->half_pixel_center) |456VIRGL_OBJ_RS_S0_BOTTOM_EDGE_RULE(state->bottom_edge_rule) |457VIRGL_OBJ_RS_S0_FORCE_PERSAMPLE_INTERP(state->force_persample_interp);458459virgl_encoder_write_dword(ctx->cbuf, tmp); /* S0 */460virgl_encoder_write_dword(ctx->cbuf, fui(state->point_size)); /* S1 */461virgl_encoder_write_dword(ctx->cbuf, state->sprite_coord_enable); /* S2 */462tmp = VIRGL_OBJ_RS_S3_LINE_STIPPLE_PATTERN(state->line_stipple_pattern) |463VIRGL_OBJ_RS_S3_LINE_STIPPLE_FACTOR(state->line_stipple_factor) |464VIRGL_OBJ_RS_S3_CLIP_PLANE_ENABLE(state->clip_plane_enable);465virgl_encoder_write_dword(ctx->cbuf, tmp); /* S3 */466virgl_encoder_write_dword(ctx->cbuf, fui(state->line_width)); /* S4 */467virgl_encoder_write_dword(ctx->cbuf, fui(state->offset_units)); /* S5 */468virgl_encoder_write_dword(ctx->cbuf, fui(state->offset_scale)); /* S6 */469virgl_encoder_write_dword(ctx->cbuf, fui(state->offset_clamp)); /* S7 */470return 0;471}472473static void virgl_emit_shader_header(struct virgl_context *ctx,474uint32_t handle, uint32_t len,475uint32_t type, uint32_t offlen,476uint32_t num_tokens)477{478virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_SHADER, len));479virgl_encoder_write_dword(ctx->cbuf, handle);480virgl_encoder_write_dword(ctx->cbuf, type);481virgl_encoder_write_dword(ctx->cbuf, offlen);482virgl_encoder_write_dword(ctx->cbuf, num_tokens);483}484485static void virgl_emit_shader_streamout(struct virgl_context *ctx,486const struct pipe_stream_output_info *so_info)487{488int num_outputs = 0;489int i;490uint32_t tmp;491492if (so_info)493num_outputs = so_info->num_outputs;494495virgl_encoder_write_dword(ctx->cbuf, num_outputs);496if (num_outputs) {497for (i = 0; i < 4; i++)498virgl_encoder_write_dword(ctx->cbuf, so_info->stride[i]);499500for (i = 0; i < so_info->num_outputs; i++) {501tmp =502VIRGL_OBJ_SHADER_SO_OUTPUT_REGISTER_INDEX(so_info->output[i].register_index) |503VIRGL_OBJ_SHADER_SO_OUTPUT_START_COMPONENT(so_info->output[i].start_component) |504VIRGL_OBJ_SHADER_SO_OUTPUT_NUM_COMPONENTS(so_info->output[i].num_components) |505VIRGL_OBJ_SHADER_SO_OUTPUT_BUFFER(so_info->output[i].output_buffer) |506VIRGL_OBJ_SHADER_SO_OUTPUT_DST_OFFSET(so_info->output[i].dst_offset);507virgl_encoder_write_dword(ctx->cbuf, tmp);508virgl_encoder_write_dword(ctx->cbuf, so_info->output[i].stream);509}510}511}512513int virgl_encode_shader_state(struct virgl_context *ctx,514uint32_t handle,515uint32_t type,516const struct pipe_stream_output_info *so_info,517uint32_t cs_req_local_mem,518const struct tgsi_token *tokens)519{520char *str, *sptr;521uint32_t shader_len, len;522bool bret;523int num_tokens = tgsi_num_tokens(tokens);524int str_total_size = 65536;525int retry_size = 1;526uint32_t left_bytes, base_hdr_size, strm_hdr_size, thispass;527bool first_pass;528str = CALLOC(1, str_total_size);529if (!str)530return -1;531532do {533int old_size;534535bret = tgsi_dump_str(tokens, TGSI_DUMP_FLOAT_AS_HEX, str, str_total_size);536if (bret == false) {537if (virgl_debug & VIRGL_DEBUG_VERBOSE)538debug_printf("Failed to translate shader in available space - trying again\n");539old_size = str_total_size;540str_total_size = 65536 * retry_size;541retry_size *= 2;542str = REALLOC(str, old_size, str_total_size);543if (!str)544return -1;545}546} while (bret == false && retry_size < 1024);547548if (bret == false)549return -1;550551if (virgl_debug & VIRGL_DEBUG_TGSI)552debug_printf("TGSI:\n---8<---\n%s\n---8<---\n", str);553554shader_len = strlen(str) + 1;555556left_bytes = shader_len;557558base_hdr_size = 5;559strm_hdr_size = so_info->num_outputs ? so_info->num_outputs * 2 + 4 : 0;560first_pass = true;561sptr = str;562while (left_bytes) {563uint32_t length, offlen;564int hdr_len = base_hdr_size + (first_pass ? strm_hdr_size : 0);565if (ctx->cbuf->cdw + hdr_len + 1 >= VIRGL_ENCODE_MAX_DWORDS)566ctx->base.flush(&ctx->base, NULL, 0);567568thispass = (VIRGL_ENCODE_MAX_DWORDS - ctx->cbuf->cdw - hdr_len - 1) * 4;569570length = MIN2(thispass, left_bytes);571len = ((length + 3) / 4) + hdr_len;572573if (first_pass)574offlen = VIRGL_OBJ_SHADER_OFFSET_VAL(shader_len);575else576offlen = VIRGL_OBJ_SHADER_OFFSET_VAL((uintptr_t)sptr - (uintptr_t)str) | VIRGL_OBJ_SHADER_OFFSET_CONT;577578virgl_emit_shader_header(ctx, handle, len, type, offlen, num_tokens);579580if (type == PIPE_SHADER_COMPUTE)581virgl_encoder_write_dword(ctx->cbuf, cs_req_local_mem);582else583virgl_emit_shader_streamout(ctx, first_pass ? so_info : NULL);584585virgl_encoder_write_block(ctx->cbuf, (uint8_t *)sptr, length);586587sptr += length;588first_pass = false;589left_bytes -= length;590}591592FREE(str);593return 0;594}595596597int virgl_encode_clear(struct virgl_context *ctx,598unsigned buffers,599const union pipe_color_union *color,600double depth, unsigned stencil)601{602int i;603uint64_t qword;604605STATIC_ASSERT(sizeof(qword) == sizeof(depth));606memcpy(&qword, &depth, sizeof(qword));607608virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CLEAR, 0, VIRGL_OBJ_CLEAR_SIZE));609virgl_encoder_write_dword(ctx->cbuf, buffers);610for (i = 0; i < 4; i++)611virgl_encoder_write_dword(ctx->cbuf, color->ui[i]);612virgl_encoder_write_qword(ctx->cbuf, qword);613virgl_encoder_write_dword(ctx->cbuf, stencil);614return 0;615}616617int virgl_encode_clear_texture(struct virgl_context *ctx,618struct virgl_resource *res,619unsigned int level,620const struct pipe_box *box,621const void *data)622{623const struct util_format_description *desc = util_format_description(res->b.format);624unsigned block_bits = desc->block.bits;625uint32_t arr[4] = {0};626/* The spec describe <data> as a pointer to an array of between one627* and four components of texel data that will be used as the source628* for the constant fill value.629* Here, we are just copying the memory into <arr>. We do not try to630* re-create the data array. The host part will take care of interpreting631* the memory and applying the correct format to the clear call.632*/633memcpy(&arr, data, block_bits / 8);634635virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CLEAR_TEXTURE, 0, VIRGL_CLEAR_TEXTURE_SIZE));636virgl_encoder_write_res(ctx, res);637virgl_encoder_write_dword(ctx->cbuf, level);638virgl_encoder_write_dword(ctx->cbuf, box->x);639virgl_encoder_write_dword(ctx->cbuf, box->y);640virgl_encoder_write_dword(ctx->cbuf, box->z);641virgl_encoder_write_dword(ctx->cbuf, box->width);642virgl_encoder_write_dword(ctx->cbuf, box->height);643virgl_encoder_write_dword(ctx->cbuf, box->depth);644for (unsigned i = 0; i < 4; i++)645virgl_encoder_write_dword(ctx->cbuf, arr[i]);646return 0;647}648649int virgl_encoder_set_framebuffer_state(struct virgl_context *ctx,650const struct pipe_framebuffer_state *state)651{652struct virgl_surface *zsurf = virgl_surface(state->zsbuf);653int i;654655virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_FRAMEBUFFER_STATE, 0, VIRGL_SET_FRAMEBUFFER_STATE_SIZE(state->nr_cbufs)));656virgl_encoder_write_dword(ctx->cbuf, state->nr_cbufs);657virgl_encoder_write_dword(ctx->cbuf, zsurf ? zsurf->handle : 0);658for (i = 0; i < state->nr_cbufs; i++) {659struct virgl_surface *surf = virgl_surface(state->cbufs[i]);660virgl_encoder_write_dword(ctx->cbuf, surf ? surf->handle : 0);661}662663struct virgl_screen *rs = virgl_screen(ctx->base.screen);664if (rs->caps.caps.v2.capability_bits & VIRGL_CAP_FB_NO_ATTACH) {665virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_FRAMEBUFFER_STATE_NO_ATTACH, 0, VIRGL_SET_FRAMEBUFFER_STATE_NO_ATTACH_SIZE));666virgl_encoder_write_dword(ctx->cbuf, state->width | (state->height << 16));667virgl_encoder_write_dword(ctx->cbuf, state->layers | (state->samples << 16));668}669return 0;670}671672int virgl_encoder_set_viewport_states(struct virgl_context *ctx,673int start_slot,674int num_viewports,675const struct pipe_viewport_state *states)676{677int i,v;678virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_VIEWPORT_STATE, 0, VIRGL_SET_VIEWPORT_STATE_SIZE(num_viewports)));679virgl_encoder_write_dword(ctx->cbuf, start_slot);680for (v = 0; v < num_viewports; v++) {681for (i = 0; i < 3; i++)682virgl_encoder_write_dword(ctx->cbuf, fui(states[v].scale[i]));683for (i = 0; i < 3; i++)684virgl_encoder_write_dword(ctx->cbuf, fui(states[v].translate[i]));685}686return 0;687}688689int virgl_encoder_create_vertex_elements(struct virgl_context *ctx,690uint32_t handle,691unsigned num_elements,692const struct pipe_vertex_element *element)693{694int i;695virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_VERTEX_ELEMENTS, VIRGL_OBJ_VERTEX_ELEMENTS_SIZE(num_elements)));696virgl_encoder_write_dword(ctx->cbuf, handle);697for (i = 0; i < num_elements; i++) {698virgl_encoder_write_dword(ctx->cbuf, element[i].src_offset);699virgl_encoder_write_dword(ctx->cbuf, element[i].instance_divisor);700virgl_encoder_write_dword(ctx->cbuf, element[i].vertex_buffer_index);701virgl_encoder_write_dword(ctx->cbuf, pipe_to_virgl_format(element[i].src_format));702}703return 0;704}705706int virgl_encoder_set_vertex_buffers(struct virgl_context *ctx,707unsigned num_buffers,708const struct pipe_vertex_buffer *buffers)709{710int i;711virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_VERTEX_BUFFERS, 0, VIRGL_SET_VERTEX_BUFFERS_SIZE(num_buffers)));712for (i = 0; i < num_buffers; i++) {713struct virgl_resource *res = virgl_resource(buffers[i].buffer.resource);714virgl_encoder_write_dword(ctx->cbuf, buffers[i].stride);715virgl_encoder_write_dword(ctx->cbuf, buffers[i].buffer_offset);716virgl_encoder_write_res(ctx, res);717}718return 0;719}720721int virgl_encoder_set_index_buffer(struct virgl_context *ctx,722const struct virgl_indexbuf *ib)723{724int length = VIRGL_SET_INDEX_BUFFER_SIZE(ib);725struct virgl_resource *res = NULL;726if (ib)727res = virgl_resource(ib->buffer);728729virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_INDEX_BUFFER, 0, length));730virgl_encoder_write_res(ctx, res);731if (ib) {732virgl_encoder_write_dword(ctx->cbuf, ib->index_size);733virgl_encoder_write_dword(ctx->cbuf, ib->offset);734}735return 0;736}737738int virgl_encoder_draw_vbo(struct virgl_context *ctx,739const struct pipe_draw_info *info,740unsigned drawid_offset,741const struct pipe_draw_indirect_info *indirect,742const struct pipe_draw_start_count_bias *draw)743{744uint32_t length = VIRGL_DRAW_VBO_SIZE;745if (info->mode == PIPE_PRIM_PATCHES)746length = VIRGL_DRAW_VBO_SIZE_TESS;747if (indirect && indirect->buffer)748length = VIRGL_DRAW_VBO_SIZE_INDIRECT;749virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_DRAW_VBO, 0, length));750virgl_encoder_write_dword(ctx->cbuf, draw->start);751virgl_encoder_write_dword(ctx->cbuf, draw->count);752virgl_encoder_write_dword(ctx->cbuf, info->mode);753virgl_encoder_write_dword(ctx->cbuf, !!info->index_size);754virgl_encoder_write_dword(ctx->cbuf, info->instance_count);755virgl_encoder_write_dword(ctx->cbuf, info->index_size ? draw->index_bias : 0);756virgl_encoder_write_dword(ctx->cbuf, info->start_instance);757virgl_encoder_write_dword(ctx->cbuf, info->primitive_restart);758virgl_encoder_write_dword(ctx->cbuf, info->primitive_restart ? info->restart_index : 0);759virgl_encoder_write_dword(ctx->cbuf, info->index_bounds_valid ? info->min_index : 0);760virgl_encoder_write_dword(ctx->cbuf, info->index_bounds_valid ? info->max_index : ~0);761if (indirect && indirect->count_from_stream_output)762virgl_encoder_write_dword(ctx->cbuf, indirect->count_from_stream_output->buffer_size);763else764virgl_encoder_write_dword(ctx->cbuf, 0);765if (length >= VIRGL_DRAW_VBO_SIZE_TESS) {766virgl_encoder_write_dword(ctx->cbuf, info->vertices_per_patch); /* vertices per patch */767virgl_encoder_write_dword(ctx->cbuf, drawid_offset); /* drawid */768}769if (length == VIRGL_DRAW_VBO_SIZE_INDIRECT) {770virgl_encoder_write_res(ctx, virgl_resource(indirect->buffer));771virgl_encoder_write_dword(ctx->cbuf, indirect->offset);772virgl_encoder_write_dword(ctx->cbuf, indirect->stride); /* indirect stride */773virgl_encoder_write_dword(ctx->cbuf, indirect->draw_count); /* indirect draw count */774virgl_encoder_write_dword(ctx->cbuf, indirect->indirect_draw_count_offset); /* indirect draw count offset */775if (indirect->indirect_draw_count)776virgl_encoder_write_res(ctx, virgl_resource(indirect->indirect_draw_count));777else778virgl_encoder_write_dword(ctx->cbuf, 0); /* indirect draw count handle */779}780return 0;781}782783static int virgl_encoder_create_surface_common(struct virgl_context *ctx,784uint32_t handle,785struct virgl_resource *res,786const struct pipe_surface *templat)787{788virgl_encoder_write_dword(ctx->cbuf, handle);789virgl_encoder_write_res(ctx, res);790virgl_encoder_write_dword(ctx->cbuf, pipe_to_virgl_format(templat->format));791792assert(templat->texture->target != PIPE_BUFFER);793virgl_encoder_write_dword(ctx->cbuf, templat->u.tex.level);794virgl_encoder_write_dword(ctx->cbuf, templat->u.tex.first_layer | (templat->u.tex.last_layer << 16));795796return 0;797}798799int virgl_encoder_create_surface(struct virgl_context *ctx,800uint32_t handle,801struct virgl_resource *res,802const struct pipe_surface *templat)803{804if (templat->nr_samples > 0) {805ASSERTED struct virgl_screen *rs = virgl_screen(ctx->base.screen);806assert(rs->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_IMPLICIT_MSAA);807808virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_MSAA_SURFACE, VIRGL_OBJ_MSAA_SURFACE_SIZE));809virgl_encoder_create_surface_common(ctx, handle, res, templat);810virgl_encoder_write_dword(ctx->cbuf, templat->nr_samples);811} else {812virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_SURFACE, VIRGL_OBJ_SURFACE_SIZE));813virgl_encoder_create_surface_common(ctx, handle, res, templat);814}815816return 0;817}818819int virgl_encoder_create_so_target(struct virgl_context *ctx,820uint32_t handle,821struct virgl_resource *res,822unsigned buffer_offset,823unsigned buffer_size)824{825virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_STREAMOUT_TARGET, VIRGL_OBJ_STREAMOUT_SIZE));826virgl_encoder_write_dword(ctx->cbuf, handle);827virgl_encoder_write_res(ctx, res);828virgl_encoder_write_dword(ctx->cbuf, buffer_offset);829virgl_encoder_write_dword(ctx->cbuf, buffer_size);830return 0;831}832833enum virgl_transfer3d_encode_stride {834/* The stride and layer_stride are explicitly specified in the command. */835virgl_transfer3d_explicit_stride,836/* The stride and layer_stride are inferred by the host. In this case, the837* host will use the image stride and layer_stride for the specified level.838*/839virgl_transfer3d_host_inferred_stride,840};841842static void virgl_encoder_transfer3d_common(struct virgl_screen *vs,843struct virgl_cmd_buf *buf,844struct virgl_transfer *xfer,845enum virgl_transfer3d_encode_stride encode_stride)846847{848struct pipe_transfer *transfer = &xfer->base;849unsigned stride;850unsigned layer_stride;851852if (encode_stride == virgl_transfer3d_explicit_stride) {853stride = transfer->stride;854layer_stride = transfer->layer_stride;855} else if (encode_stride == virgl_transfer3d_host_inferred_stride) {856stride = 0;857layer_stride = 0;858} else {859assert(!"Invalid virgl_transfer3d_encode_stride value");860}861862/* We cannot use virgl_encoder_emit_resource with transfer->resource here863* because transfer->resource might have a different virgl_hw_res than what864* this transfer targets, which is saved in xfer->hw_res.865*/866vs->vws->emit_res(vs->vws, buf, xfer->hw_res, TRUE);867virgl_encoder_write_dword(buf, transfer->level);868virgl_encoder_write_dword(buf, transfer->usage);869virgl_encoder_write_dword(buf, stride);870virgl_encoder_write_dword(buf, layer_stride);871virgl_encoder_write_dword(buf, transfer->box.x);872virgl_encoder_write_dword(buf, transfer->box.y);873virgl_encoder_write_dword(buf, transfer->box.z);874virgl_encoder_write_dword(buf, transfer->box.width);875virgl_encoder_write_dword(buf, transfer->box.height);876virgl_encoder_write_dword(buf, transfer->box.depth);877}878879int virgl_encoder_inline_write(struct virgl_context *ctx,880struct virgl_resource *res,881unsigned level, unsigned usage,882const struct pipe_box *box,883const void *data, unsigned stride,884unsigned layer_stride)885{886uint32_t size = (stride ? stride : box->width) * box->height;887uint32_t length, thispass, left_bytes;888struct virgl_transfer transfer;889struct virgl_screen *vs = virgl_screen(ctx->base.screen);890891transfer.base.resource = &res->b;892transfer.hw_res = res->hw_res;893transfer.base.level = level;894transfer.base.usage = usage;895transfer.base.box = *box;896897length = 11 + (size + 3) / 4;898if ((ctx->cbuf->cdw + length + 1) > VIRGL_ENCODE_MAX_DWORDS) {899if (box->height > 1 || box->depth > 1) {900debug_printf("inline transfer failed due to multi dimensions and too large\n");901assert(0);902}903}904905left_bytes = size;906while (left_bytes) {907if (ctx->cbuf->cdw + 12 >= VIRGL_ENCODE_MAX_DWORDS)908ctx->base.flush(&ctx->base, NULL, 0);909910thispass = (VIRGL_ENCODE_MAX_DWORDS - ctx->cbuf->cdw - 12) * 4;911912length = MIN2(thispass, left_bytes);913914transfer.base.box.width = length;915virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_INLINE_WRITE, 0, ((length + 3) / 4) + 11));916virgl_encoder_transfer3d_common(vs, ctx->cbuf, &transfer,917virgl_transfer3d_host_inferred_stride);918virgl_encoder_write_block(ctx->cbuf, data, length);919left_bytes -= length;920transfer.base.box.x += length;921data += length;922}923return 0;924}925926int virgl_encoder_flush_frontbuffer(struct virgl_context *ctx,927struct virgl_resource *res)928{929// virgl_encoder_write_dword(ctx->cbuf, VIRGL_CMD0(VIRGL_CCMD_FLUSH_FRONTUBFFER, 0, 1));930// virgl_encoder_write_dword(ctx->cbuf, res_handle);931return 0;932}933934int virgl_encode_sampler_state(struct virgl_context *ctx,935uint32_t handle,936const struct pipe_sampler_state *state)937{938uint32_t tmp;939int i;940virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_SAMPLER_STATE, VIRGL_OBJ_SAMPLER_STATE_SIZE));941virgl_encoder_write_dword(ctx->cbuf, handle);942943tmp = VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_S(state->wrap_s) |944VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_T(state->wrap_t) |945VIRGL_OBJ_SAMPLE_STATE_S0_WRAP_R(state->wrap_r) |946VIRGL_OBJ_SAMPLE_STATE_S0_MIN_IMG_FILTER(state->min_img_filter) |947VIRGL_OBJ_SAMPLE_STATE_S0_MIN_MIP_FILTER(state->min_mip_filter) |948VIRGL_OBJ_SAMPLE_STATE_S0_MAG_IMG_FILTER(state->mag_img_filter) |949VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_MODE(state->compare_mode) |950VIRGL_OBJ_SAMPLE_STATE_S0_COMPARE_FUNC(state->compare_func) |951VIRGL_OBJ_SAMPLE_STATE_S0_SEAMLESS_CUBE_MAP(state->seamless_cube_map);952953virgl_encoder_write_dword(ctx->cbuf, tmp);954virgl_encoder_write_dword(ctx->cbuf, fui(state->lod_bias));955virgl_encoder_write_dword(ctx->cbuf, fui(state->min_lod));956virgl_encoder_write_dword(ctx->cbuf, fui(state->max_lod));957for (i = 0; i < 4; i++)958virgl_encoder_write_dword(ctx->cbuf, state->border_color.ui[i]);959return 0;960}961962963int virgl_encode_sampler_view(struct virgl_context *ctx,964uint32_t handle,965struct virgl_resource *res,966const struct pipe_sampler_view *state)967{968unsigned elem_size = util_format_get_blocksize(state->format);969struct virgl_screen *rs = virgl_screen(ctx->base.screen);970uint32_t tmp;971uint32_t dword_fmt_target = pipe_to_virgl_format(state->format);972virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_SAMPLER_VIEW, VIRGL_OBJ_SAMPLER_VIEW_SIZE));973virgl_encoder_write_dword(ctx->cbuf, handle);974virgl_encoder_write_res(ctx, res);975if (rs->caps.caps.v2.capability_bits & VIRGL_CAP_TEXTURE_VIEW)976dword_fmt_target |= (state->target << 24);977virgl_encoder_write_dword(ctx->cbuf, dword_fmt_target);978if (res->b.target == PIPE_BUFFER) {979virgl_encoder_write_dword(ctx->cbuf, state->u.buf.offset / elem_size);980virgl_encoder_write_dword(ctx->cbuf, (state->u.buf.offset + state->u.buf.size) / elem_size - 1);981} else {982if (res->metadata.plane) {983debug_assert(state->u.tex.first_layer == 0 && state->u.tex.last_layer == 0);984virgl_encoder_write_dword(ctx->cbuf, res->metadata.plane);985} else {986virgl_encoder_write_dword(ctx->cbuf, state->u.tex.first_layer | state->u.tex.last_layer << 16);987}988virgl_encoder_write_dword(ctx->cbuf, state->u.tex.first_level | state->u.tex.last_level << 8);989}990tmp = VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_R(state->swizzle_r) |991VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_G(state->swizzle_g) |992VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_B(state->swizzle_b) |993VIRGL_OBJ_SAMPLER_VIEW_SWIZZLE_A(state->swizzle_a);994virgl_encoder_write_dword(ctx->cbuf, tmp);995return 0;996}997998int virgl_encode_set_sampler_views(struct virgl_context *ctx,999uint32_t shader_type,1000uint32_t start_slot,1001uint32_t num_views,1002struct virgl_sampler_view **views)1003{1004int i;1005virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLER_VIEWS, 0, VIRGL_SET_SAMPLER_VIEWS_SIZE(num_views)));1006virgl_encoder_write_dword(ctx->cbuf, shader_type);1007virgl_encoder_write_dword(ctx->cbuf, start_slot);1008for (i = 0; i < num_views; i++) {1009uint32_t handle = views[i] ? views[i]->handle : 0;1010virgl_encoder_write_dword(ctx->cbuf, handle);1011}1012return 0;1013}10141015int virgl_encode_bind_sampler_states(struct virgl_context *ctx,1016uint32_t shader_type,1017uint32_t start_slot,1018uint32_t num_handles,1019uint32_t *handles)1020{1021int i;1022virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_BIND_SAMPLER_STATES, 0, VIRGL_BIND_SAMPLER_STATES(num_handles)));1023virgl_encoder_write_dword(ctx->cbuf, shader_type);1024virgl_encoder_write_dword(ctx->cbuf, start_slot);1025for (i = 0; i < num_handles; i++)1026virgl_encoder_write_dword(ctx->cbuf, handles[i]);1027return 0;1028}10291030int virgl_encoder_write_constant_buffer(struct virgl_context *ctx,1031uint32_t shader,1032uint32_t index,1033uint32_t size,1034const void *data)1035{1036virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_CONSTANT_BUFFER, 0, size + 2));1037virgl_encoder_write_dword(ctx->cbuf, shader);1038virgl_encoder_write_dword(ctx->cbuf, index);1039if (data)1040virgl_encoder_write_block(ctx->cbuf, data, size * 4);1041return 0;1042}10431044int virgl_encoder_set_uniform_buffer(struct virgl_context *ctx,1045uint32_t shader,1046uint32_t index,1047uint32_t offset,1048uint32_t length,1049struct virgl_resource *res)1050{1051virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_UNIFORM_BUFFER, 0, VIRGL_SET_UNIFORM_BUFFER_SIZE));1052virgl_encoder_write_dword(ctx->cbuf, shader);1053virgl_encoder_write_dword(ctx->cbuf, index);1054virgl_encoder_write_dword(ctx->cbuf, offset);1055virgl_encoder_write_dword(ctx->cbuf, length);1056virgl_encoder_write_res(ctx, res);1057return 0;1058}105910601061int virgl_encoder_set_stencil_ref(struct virgl_context *ctx,1062const struct pipe_stencil_ref *ref)1063{1064virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_STENCIL_REF, 0, VIRGL_SET_STENCIL_REF_SIZE));1065virgl_encoder_write_dword(ctx->cbuf, VIRGL_STENCIL_REF_VAL(ref->ref_value[0] , (ref->ref_value[1])));1066return 0;1067}10681069int virgl_encoder_set_blend_color(struct virgl_context *ctx,1070const struct pipe_blend_color *color)1071{1072int i;1073virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_BLEND_COLOR, 0, VIRGL_SET_BLEND_COLOR_SIZE));1074for (i = 0; i < 4; i++)1075virgl_encoder_write_dword(ctx->cbuf, fui(color->color[i]));1076return 0;1077}10781079int virgl_encoder_set_scissor_state(struct virgl_context *ctx,1080unsigned start_slot,1081int num_scissors,1082const struct pipe_scissor_state *ss)1083{1084int i;1085virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SCISSOR_STATE, 0, VIRGL_SET_SCISSOR_STATE_SIZE(num_scissors)));1086virgl_encoder_write_dword(ctx->cbuf, start_slot);1087for (i = 0; i < num_scissors; i++) {1088virgl_encoder_write_dword(ctx->cbuf, (ss[i].minx | ss[i].miny << 16));1089virgl_encoder_write_dword(ctx->cbuf, (ss[i].maxx | ss[i].maxy << 16));1090}1091return 0;1092}10931094void virgl_encoder_set_polygon_stipple(struct virgl_context *ctx,1095const struct pipe_poly_stipple *ps)1096{1097int i;1098virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_POLYGON_STIPPLE, 0, VIRGL_POLYGON_STIPPLE_SIZE));1099for (i = 0; i < VIRGL_POLYGON_STIPPLE_SIZE; i++) {1100virgl_encoder_write_dword(ctx->cbuf, ps->stipple[i]);1101}1102}11031104void virgl_encoder_set_sample_mask(struct virgl_context *ctx,1105unsigned sample_mask)1106{1107virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SAMPLE_MASK, 0, VIRGL_SET_SAMPLE_MASK_SIZE));1108virgl_encoder_write_dword(ctx->cbuf, sample_mask);1109}11101111void virgl_encoder_set_min_samples(struct virgl_context *ctx,1112unsigned min_samples)1113{1114virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_MIN_SAMPLES, 0, VIRGL_SET_MIN_SAMPLES_SIZE));1115virgl_encoder_write_dword(ctx->cbuf, min_samples);1116}11171118void virgl_encoder_set_clip_state(struct virgl_context *ctx,1119const struct pipe_clip_state *clip)1120{1121int i, j;1122virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_CLIP_STATE, 0, VIRGL_SET_CLIP_STATE_SIZE));1123for (i = 0; i < VIRGL_MAX_CLIP_PLANES; i++) {1124for (j = 0; j < 4; j++) {1125virgl_encoder_write_dword(ctx->cbuf, fui(clip->ucp[i][j]));1126}1127}1128}11291130int virgl_encode_resource_copy_region(struct virgl_context *ctx,1131struct virgl_resource *dst_res,1132unsigned dst_level,1133unsigned dstx, unsigned dsty, unsigned dstz,1134struct virgl_resource *src_res,1135unsigned src_level,1136const struct pipe_box *src_box)1137{1138virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_RESOURCE_COPY_REGION, 0, VIRGL_CMD_RESOURCE_COPY_REGION_SIZE));1139virgl_encoder_write_res(ctx, dst_res);1140virgl_encoder_write_dword(ctx->cbuf, dst_level);1141virgl_encoder_write_dword(ctx->cbuf, dstx);1142virgl_encoder_write_dword(ctx->cbuf, dsty);1143virgl_encoder_write_dword(ctx->cbuf, dstz);1144virgl_encoder_write_res(ctx, src_res);1145virgl_encoder_write_dword(ctx->cbuf, src_level);1146virgl_encoder_write_dword(ctx->cbuf, src_box->x);1147virgl_encoder_write_dword(ctx->cbuf, src_box->y);1148virgl_encoder_write_dword(ctx->cbuf, src_box->z);1149virgl_encoder_write_dword(ctx->cbuf, src_box->width);1150virgl_encoder_write_dword(ctx->cbuf, src_box->height);1151virgl_encoder_write_dword(ctx->cbuf, src_box->depth);1152return 0;1153}11541155int virgl_encode_blit(struct virgl_context *ctx,1156struct virgl_resource *dst_res,1157struct virgl_resource *src_res,1158const struct pipe_blit_info *blit)1159{1160uint32_t tmp;1161virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_BLIT, 0, VIRGL_CMD_BLIT_SIZE));1162tmp = VIRGL_CMD_BLIT_S0_MASK(blit->mask) |1163VIRGL_CMD_BLIT_S0_FILTER(blit->filter) |1164VIRGL_CMD_BLIT_S0_SCISSOR_ENABLE(blit->scissor_enable) |1165VIRGL_CMD_BLIT_S0_RENDER_CONDITION_ENABLE(blit->render_condition_enable) |1166VIRGL_CMD_BLIT_S0_ALPHA_BLEND(blit->alpha_blend);1167virgl_encoder_write_dword(ctx->cbuf, tmp);1168virgl_encoder_write_dword(ctx->cbuf, (blit->scissor.minx | blit->scissor.miny << 16));1169virgl_encoder_write_dword(ctx->cbuf, (blit->scissor.maxx | blit->scissor.maxy << 16));11701171virgl_encoder_write_res(ctx, dst_res);1172virgl_encoder_write_dword(ctx->cbuf, blit->dst.level);1173virgl_encoder_write_dword(ctx->cbuf, pipe_to_virgl_format(blit->dst.format));1174virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.x);1175virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.y);1176virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.z);1177virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.width);1178virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.height);1179virgl_encoder_write_dword(ctx->cbuf, blit->dst.box.depth);11801181virgl_encoder_write_res(ctx, src_res);1182virgl_encoder_write_dword(ctx->cbuf, blit->src.level);1183virgl_encoder_write_dword(ctx->cbuf, pipe_to_virgl_format(blit->src.format));1184virgl_encoder_write_dword(ctx->cbuf, blit->src.box.x);1185virgl_encoder_write_dword(ctx->cbuf, blit->src.box.y);1186virgl_encoder_write_dword(ctx->cbuf, blit->src.box.z);1187virgl_encoder_write_dword(ctx->cbuf, blit->src.box.width);1188virgl_encoder_write_dword(ctx->cbuf, blit->src.box.height);1189virgl_encoder_write_dword(ctx->cbuf, blit->src.box.depth);1190return 0;1191}11921193int virgl_encoder_create_query(struct virgl_context *ctx,1194uint32_t handle,1195uint query_type,1196uint query_index,1197struct virgl_resource *res,1198uint32_t offset)1199{1200virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_OBJECT, VIRGL_OBJECT_QUERY, VIRGL_OBJ_QUERY_SIZE));1201virgl_encoder_write_dword(ctx->cbuf, handle);1202virgl_encoder_write_dword(ctx->cbuf, ((query_type & 0xffff) | (query_index << 16)));1203virgl_encoder_write_dword(ctx->cbuf, offset);1204virgl_encoder_write_res(ctx, res);1205return 0;1206}12071208int virgl_encoder_begin_query(struct virgl_context *ctx,1209uint32_t handle)1210{1211virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_BEGIN_QUERY, 0, 1));1212virgl_encoder_write_dword(ctx->cbuf, handle);1213return 0;1214}12151216int virgl_encoder_end_query(struct virgl_context *ctx,1217uint32_t handle)1218{1219virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_END_QUERY, 0, 1));1220virgl_encoder_write_dword(ctx->cbuf, handle);1221return 0;1222}12231224int virgl_encoder_get_query_result(struct virgl_context *ctx,1225uint32_t handle, boolean wait)1226{1227virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_GET_QUERY_RESULT, 0, 2));1228virgl_encoder_write_dword(ctx->cbuf, handle);1229virgl_encoder_write_dword(ctx->cbuf, wait ? 1 : 0);1230return 0;1231}12321233int virgl_encoder_render_condition(struct virgl_context *ctx,1234uint32_t handle, boolean condition,1235enum pipe_render_cond_flag mode)1236{1237virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_RENDER_CONDITION, 0, VIRGL_RENDER_CONDITION_SIZE));1238virgl_encoder_write_dword(ctx->cbuf, handle);1239virgl_encoder_write_dword(ctx->cbuf, condition);1240virgl_encoder_write_dword(ctx->cbuf, mode);1241return 0;1242}12431244int virgl_encoder_set_so_targets(struct virgl_context *ctx,1245unsigned num_targets,1246struct pipe_stream_output_target **targets,1247unsigned append_bitmask)1248{1249int i;12501251virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_STREAMOUT_TARGETS, 0, num_targets + 1));1252virgl_encoder_write_dword(ctx->cbuf, append_bitmask);1253for (i = 0; i < num_targets; i++) {1254struct virgl_so_target *tg = virgl_so_target(targets[i]);1255virgl_encoder_write_dword(ctx->cbuf, tg ? tg->handle : 0);1256}1257return 0;1258}125912601261int virgl_encoder_set_sub_ctx(struct virgl_context *ctx, uint32_t sub_ctx_id)1262{1263virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SUB_CTX, 0, 1));1264virgl_encoder_write_dword(ctx->cbuf, sub_ctx_id);1265return 0;1266}12671268int virgl_encoder_create_sub_ctx(struct virgl_context *ctx, uint32_t sub_ctx_id)1269{1270virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_CREATE_SUB_CTX, 0, 1));1271virgl_encoder_write_dword(ctx->cbuf, sub_ctx_id);1272return 0;1273}12741275int virgl_encoder_destroy_sub_ctx(struct virgl_context *ctx, uint32_t sub_ctx_id)1276{1277virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_DESTROY_SUB_CTX, 0, 1));1278virgl_encoder_write_dword(ctx->cbuf, sub_ctx_id);1279return 0;1280}12811282int virgl_encode_bind_shader(struct virgl_context *ctx,1283uint32_t handle, uint32_t type)1284{1285virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_BIND_SHADER, 0, 2));1286virgl_encoder_write_dword(ctx->cbuf, handle);1287virgl_encoder_write_dword(ctx->cbuf, type);1288return 0;1289}12901291int virgl_encode_set_tess_state(struct virgl_context *ctx,1292const float outer[4],1293const float inner[2])1294{1295int i;1296virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_TESS_STATE, 0, 6));1297for (i = 0; i < 4; i++)1298virgl_encoder_write_dword(ctx->cbuf, fui(outer[i]));1299for (i = 0; i < 2; i++)1300virgl_encoder_write_dword(ctx->cbuf, fui(inner[i]));1301return 0;1302}13031304int virgl_encode_set_shader_buffers(struct virgl_context *ctx,1305enum pipe_shader_type shader,1306unsigned start_slot, unsigned count,1307const struct pipe_shader_buffer *buffers)1308{1309int i;1310virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SHADER_BUFFERS, 0, VIRGL_SET_SHADER_BUFFER_SIZE(count)));13111312virgl_encoder_write_dword(ctx->cbuf, shader);1313virgl_encoder_write_dword(ctx->cbuf, start_slot);1314for (i = 0; i < count; i++) {1315if (buffers && buffers[i].buffer) {1316struct virgl_resource *res = virgl_resource(buffers[i].buffer);1317virgl_encoder_write_dword(ctx->cbuf, buffers[i].buffer_offset);1318virgl_encoder_write_dword(ctx->cbuf, buffers[i].buffer_size);1319virgl_encoder_write_res(ctx, res);13201321util_range_add(&res->b, &res->valid_buffer_range, buffers[i].buffer_offset,1322buffers[i].buffer_offset + buffers[i].buffer_size);1323virgl_resource_dirty(res, 0);1324} else {1325virgl_encoder_write_dword(ctx->cbuf, 0);1326virgl_encoder_write_dword(ctx->cbuf, 0);1327virgl_encoder_write_dword(ctx->cbuf, 0);1328}1329}1330return 0;1331}13321333int virgl_encode_set_hw_atomic_buffers(struct virgl_context *ctx,1334unsigned start_slot, unsigned count,1335const struct pipe_shader_buffer *buffers)1336{1337int i;1338virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_ATOMIC_BUFFERS, 0, VIRGL_SET_ATOMIC_BUFFER_SIZE(count)));13391340virgl_encoder_write_dword(ctx->cbuf, start_slot);1341for (i = 0; i < count; i++) {1342if (buffers && buffers[i].buffer) {1343struct virgl_resource *res = virgl_resource(buffers[i].buffer);1344virgl_encoder_write_dword(ctx->cbuf, buffers[i].buffer_offset);1345virgl_encoder_write_dword(ctx->cbuf, buffers[i].buffer_size);1346virgl_encoder_write_res(ctx, res);13471348util_range_add(&res->b, &res->valid_buffer_range, buffers[i].buffer_offset,1349buffers[i].buffer_offset + buffers[i].buffer_size);1350virgl_resource_dirty(res, 0);1351} else {1352virgl_encoder_write_dword(ctx->cbuf, 0);1353virgl_encoder_write_dword(ctx->cbuf, 0);1354virgl_encoder_write_dword(ctx->cbuf, 0);1355}1356}1357return 0;1358}13591360int virgl_encode_set_shader_images(struct virgl_context *ctx,1361enum pipe_shader_type shader,1362unsigned start_slot, unsigned count,1363const struct pipe_image_view *images)1364{1365int i;1366virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_SHADER_IMAGES, 0, VIRGL_SET_SHADER_IMAGE_SIZE(count)));13671368virgl_encoder_write_dword(ctx->cbuf, shader);1369virgl_encoder_write_dword(ctx->cbuf, start_slot);1370for (i = 0; i < count; i++) {1371if (images && images[i].resource) {1372struct virgl_resource *res = virgl_resource(images[i].resource);1373virgl_encoder_write_dword(ctx->cbuf, pipe_to_virgl_format(images[i].format));1374virgl_encoder_write_dword(ctx->cbuf, images[i].access);1375virgl_encoder_write_dword(ctx->cbuf, images[i].u.buf.offset);1376virgl_encoder_write_dword(ctx->cbuf, images[i].u.buf.size);1377virgl_encoder_write_res(ctx, res);13781379if (res->b.target == PIPE_BUFFER) {1380util_range_add(&res->b, &res->valid_buffer_range, images[i].u.buf.offset,1381images[i].u.buf.offset + images[i].u.buf.size);1382}1383virgl_resource_dirty(res, images[i].u.tex.level);1384} else {1385virgl_encoder_write_dword(ctx->cbuf, 0);1386virgl_encoder_write_dword(ctx->cbuf, 0);1387virgl_encoder_write_dword(ctx->cbuf, 0);1388virgl_encoder_write_dword(ctx->cbuf, 0);1389virgl_encoder_write_dword(ctx->cbuf, 0);1390}1391}1392return 0;1393}13941395int virgl_encode_memory_barrier(struct virgl_context *ctx,1396unsigned flags)1397{1398virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_MEMORY_BARRIER, 0, 1));1399virgl_encoder_write_dword(ctx->cbuf, flags);1400return 0;1401}14021403int virgl_encode_launch_grid(struct virgl_context *ctx,1404const struct pipe_grid_info *grid_info)1405{1406virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_LAUNCH_GRID, 0, VIRGL_LAUNCH_GRID_SIZE));1407virgl_encoder_write_dword(ctx->cbuf, grid_info->block[0]);1408virgl_encoder_write_dword(ctx->cbuf, grid_info->block[1]);1409virgl_encoder_write_dword(ctx->cbuf, grid_info->block[2]);1410virgl_encoder_write_dword(ctx->cbuf, grid_info->grid[0]);1411virgl_encoder_write_dword(ctx->cbuf, grid_info->grid[1]);1412virgl_encoder_write_dword(ctx->cbuf, grid_info->grid[2]);1413if (grid_info->indirect) {1414struct virgl_resource *res = virgl_resource(grid_info->indirect);1415virgl_encoder_write_res(ctx, res);1416} else1417virgl_encoder_write_dword(ctx->cbuf, 0);1418virgl_encoder_write_dword(ctx->cbuf, grid_info->indirect_offset);1419return 0;1420}14211422int virgl_encode_texture_barrier(struct virgl_context *ctx,1423unsigned flags)1424{1425virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_TEXTURE_BARRIER, 0, 1));1426virgl_encoder_write_dword(ctx->cbuf, flags);1427return 0;1428}14291430int virgl_encode_host_debug_flagstring(struct virgl_context *ctx,1431const char *flagstring)1432{1433unsigned long slen = strlen(flagstring) + 1;1434uint32_t sslen;1435uint32_t string_length;14361437if (!slen)1438return 0;14391440if (slen > 4 * 0xffff) {1441debug_printf("VIRGL: host debug flag string too long, will be truncated\n");1442slen = 4 * 0xffff;1443}14441445sslen = (uint32_t )(slen + 3) / 4;1446string_length = (uint32_t)MIN2(sslen * 4, slen);14471448virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_DEBUG_FLAGS, 0, sslen));1449virgl_encoder_write_block(ctx->cbuf, (const uint8_t *)flagstring, string_length);1450return 0;1451}14521453int virgl_encode_tweak(struct virgl_context *ctx, enum vrend_tweak_type tweak, uint32_t value)1454{1455virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_SET_TWEAKS, 0, VIRGL_SET_TWEAKS_SIZE));1456virgl_encoder_write_dword(ctx->cbuf, tweak);1457virgl_encoder_write_dword(ctx->cbuf, value);1458return 0;1459}146014611462int virgl_encode_get_query_result_qbo(struct virgl_context *ctx,1463uint32_t handle,1464struct virgl_resource *res, boolean wait,1465uint32_t result_type,1466uint32_t offset,1467uint32_t index)1468{1469virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_GET_QUERY_RESULT_QBO, 0, VIRGL_QUERY_RESULT_QBO_SIZE));1470virgl_encoder_write_dword(ctx->cbuf, handle);1471virgl_encoder_write_res(ctx, res);1472virgl_encoder_write_dword(ctx->cbuf, wait ? 1 : 0);1473virgl_encoder_write_dword(ctx->cbuf, result_type);1474virgl_encoder_write_dword(ctx->cbuf, offset);1475virgl_encoder_write_dword(ctx->cbuf, index);1476return 0;1477}14781479void virgl_encode_transfer(struct virgl_screen *vs, struct virgl_cmd_buf *buf,1480struct virgl_transfer *trans, uint32_t direction)1481{1482uint32_t command;1483struct virgl_resource *vres = virgl_resource(trans->base.resource);1484enum virgl_transfer3d_encode_stride stride_type =1485virgl_transfer3d_host_inferred_stride;14861487if (trans->base.box.depth == 1 && trans->base.level == 0 &&1488trans->base.resource->target == PIPE_TEXTURE_2D &&1489vres->blob_mem == VIRGL_BLOB_MEM_HOST3D_GUEST)1490stride_type = virgl_transfer3d_explicit_stride;14911492command = VIRGL_CMD0(VIRGL_CCMD_TRANSFER3D, 0, VIRGL_TRANSFER3D_SIZE);1493virgl_encoder_write_dword(buf, command);1494virgl_encoder_transfer3d_common(vs, buf, trans, stride_type);1495virgl_encoder_write_dword(buf, trans->offset);1496virgl_encoder_write_dword(buf, direction);1497}14981499void virgl_encode_copy_transfer(struct virgl_context *ctx,1500struct virgl_transfer *trans)1501{1502uint32_t command;1503struct virgl_screen *vs = virgl_screen(ctx->base.screen);15041505assert(trans->copy_src_hw_res);15061507command = VIRGL_CMD0(VIRGL_CCMD_COPY_TRANSFER3D, 0, VIRGL_COPY_TRANSFER3D_SIZE);1508virgl_encoder_write_cmd_dword(ctx, command);1509/* Copy transfers need to explicitly specify the stride, since it may differ1510* from the image stride.1511*/1512virgl_encoder_transfer3d_common(vs, ctx->cbuf, trans, virgl_transfer3d_explicit_stride);1513vs->vws->emit_res(vs->vws, ctx->cbuf, trans->copy_src_hw_res, TRUE);1514virgl_encoder_write_dword(ctx->cbuf, trans->copy_src_offset);1515/* At the moment all copy transfers are synchronized. */1516virgl_encoder_write_dword(ctx->cbuf, 1);1517}15181519void virgl_encode_end_transfers(struct virgl_cmd_buf *buf)1520{1521uint32_t command, diff;1522diff = VIRGL_MAX_TBUF_DWORDS - buf->cdw;1523if (diff) {1524command = VIRGL_CMD0(VIRGL_CCMD_END_TRANSFERS, 0, diff - 1);1525virgl_encoder_write_dword(buf, command);1526}1527}15281529void virgl_encode_get_memory_info(struct virgl_context *ctx, struct virgl_resource *res)1530{1531virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_GET_MEMORY_INFO, 0, 1));1532virgl_encoder_write_res(ctx, res);1533}15341535void virgl_encode_emit_string_marker(struct virgl_context *ctx,1536const char *message, int len)1537{1538if (!len)1539return;15401541if (len > 4 * 0xffff) {1542debug_printf("VIRGL: host debug flag string too long, will be truncated\n");1543len = 4 * 0xffff;1544}15451546uint32_t buf_len = (uint32_t )(len + 3) / 4 + 1;1547virgl_encoder_write_cmd_dword(ctx, VIRGL_CMD0(VIRGL_CCMD_EMIT_STRING_MARKER, 0, buf_len));1548virgl_encoder_write_dword(ctx->cbuf, len);1549virgl_encoder_write_block(ctx->cbuf, (const uint8_t *)message, len);1550}155115521553