1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2014, 2015 Red Hat.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include "util/u_memory.h"
24bf215546Sopenharmony_ci#include "util/format/u_format.h"
25bf215546Sopenharmony_ci#include "util/format/u_format_s3tc.h"
26bf215546Sopenharmony_ci#include "util/u_screen.h"
27bf215546Sopenharmony_ci#include "util/u_video.h"
28bf215546Sopenharmony_ci#include "util/u_math.h"
29bf215546Sopenharmony_ci#include "util/u_inlines.h"
30bf215546Sopenharmony_ci#include "util/os_time.h"
31bf215546Sopenharmony_ci#include "util/xmlconfig.h"
32bf215546Sopenharmony_ci#include "pipe/p_defines.h"
33bf215546Sopenharmony_ci#include "pipe/p_screen.h"
34bf215546Sopenharmony_ci#include "nir/nir_to_tgsi.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "tgsi/tgsi_exec.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "virgl_screen.h"
39bf215546Sopenharmony_ci#include "virgl_resource.h"
40bf215546Sopenharmony_ci#include "virgl_public.h"
41bf215546Sopenharmony_ci#include "virgl_context.h"
42bf215546Sopenharmony_ci#include "virtio-gpu/virgl_protocol.h"
43bf215546Sopenharmony_ci#include "virgl_encode.h"
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ciint virgl_debug = 0;
46bf215546Sopenharmony_cistatic const struct debug_named_value virgl_debug_options[] = {
47bf215546Sopenharmony_ci   { "verbose",   VIRGL_DEBUG_VERBOSE,             NULL },
48bf215546Sopenharmony_ci   { "tgsi",      VIRGL_DEBUG_TGSI,                NULL },
49bf215546Sopenharmony_ci   { "use_tgsi",  VIRGL_DEBUG_USE_TGSI,            NULL },
50bf215546Sopenharmony_ci   { "noemubgra", VIRGL_DEBUG_NO_EMULATE_BGRA,     "Disable tweak to emulate BGRA as RGBA on GLES hosts"},
51bf215546Sopenharmony_ci   { "nobgraswz", VIRGL_DEBUG_NO_BGRA_DEST_SWIZZLE,"Disable tweak to swizzle emulated BGRA on GLES hosts" },
52bf215546Sopenharmony_ci   { "sync",      VIRGL_DEBUG_SYNC,                "Sync after every flush" },
53bf215546Sopenharmony_ci   { "xfer",      VIRGL_DEBUG_XFER,                "Do not optimize for transfers" },
54bf215546Sopenharmony_ci   { "r8srgb-readback",   VIRGL_DEBUG_L8_SRGB_ENABLE_READBACK, "Enable redaback for L8 sRGB textures" },
55bf215546Sopenharmony_ci   { "nocoherent", VIRGL_DEBUG_NO_COHERENT,        "Disable coherent memory"},
56bf215546Sopenharmony_ci   DEBUG_NAMED_VALUE_END
57bf215546Sopenharmony_ci};
58bf215546Sopenharmony_ciDEBUG_GET_ONCE_FLAGS_OPTION(virgl_debug, "VIRGL_DEBUG", virgl_debug_options, 0)
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistatic const char *
61bf215546Sopenharmony_civirgl_get_vendor(struct pipe_screen *screen)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   return "Mesa/X.org";
64bf215546Sopenharmony_ci}
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic const char *
68bf215546Sopenharmony_civirgl_get_name(struct pipe_screen *screen)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
71bf215546Sopenharmony_ci   if (vscreen->caps.caps.v2.host_feature_check_version >= 5)
72bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.renderer;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   return "virgl";
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic int
78bf215546Sopenharmony_civirgl_get_param(struct pipe_screen *screen, enum pipe_cap param)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
81bf215546Sopenharmony_ci   switch (param) {
82bf215546Sopenharmony_ci   case PIPE_CAP_NPOT_TEXTURES:
83bf215546Sopenharmony_ci      return 1;
84bf215546Sopenharmony_ci   case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
85bf215546Sopenharmony_ci   case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
86bf215546Sopenharmony_ci      return 1;
87bf215546Sopenharmony_ci   case PIPE_CAP_ANISOTROPIC_FILTER:
88bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_anisotropy > 1.0;
89bf215546Sopenharmony_ci   case PIPE_CAP_POINT_SPRITE:
90bf215546Sopenharmony_ci      return 1;
91bf215546Sopenharmony_ci   case PIPE_CAP_MAX_RENDER_TARGETS:
92bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_render_targets;
93bf215546Sopenharmony_ci   case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
94bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_dual_source_render_targets;
95bf215546Sopenharmony_ci   case PIPE_CAP_OCCLUSION_QUERY:
96bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.occlusion_query;
97bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
98bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
99bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.mirror_clamp;
100bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_SWIZZLE:
101bf215546Sopenharmony_ci      return 1;
102bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
103bf215546Sopenharmony_ci      if (vscreen->caps.caps.v2.max_texture_2d_size)
104bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_texture_2d_size;
105bf215546Sopenharmony_ci      return 16384;
106bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
107bf215546Sopenharmony_ci      if (vscreen->caps.caps.v2.max_texture_3d_size)
108bf215546Sopenharmony_ci         return 1 + util_logbase2(vscreen->caps.caps.v2.max_texture_3d_size);
109bf215546Sopenharmony_ci      return 9; /* 256 x 256 x 256 */
110bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
111bf215546Sopenharmony_ci      if (vscreen->caps.caps.v2.max_texture_cube_size)
112bf215546Sopenharmony_ci         return 1 + util_logbase2(vscreen->caps.caps.v2.max_texture_cube_size);
113bf215546Sopenharmony_ci      return 13; /* 4K x 4K */
114bf215546Sopenharmony_ci   case PIPE_CAP_BLEND_EQUATION_SEPARATE:
115bf215546Sopenharmony_ci      return 1;
116bf215546Sopenharmony_ci   case PIPE_CAP_INDEP_BLEND_ENABLE:
117bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.indep_blend_enable;
118bf215546Sopenharmony_ci   case PIPE_CAP_INDEP_BLEND_FUNC:
119bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.indep_blend_func;
120bf215546Sopenharmony_ci   case PIPE_CAP_FS_COORD_ORIGIN_UPPER_LEFT:
121bf215546Sopenharmony_ci   case PIPE_CAP_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
122bf215546Sopenharmony_ci   case PIPE_CAP_FS_COORD_PIXEL_CENTER_INTEGER:
123bf215546Sopenharmony_ci      return 1;
124bf215546Sopenharmony_ci   case PIPE_CAP_FS_COORD_ORIGIN_LOWER_LEFT:
125bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.fragment_coord_conventions;
126bf215546Sopenharmony_ci   case PIPE_CAP_DEPTH_CLIP_DISABLE:
127bf215546Sopenharmony_ci      if (vscreen->caps.caps.v1.bset.depth_clip_disable)
128bf215546Sopenharmony_ci         return 1;
129bf215546Sopenharmony_ci      return 0;
130bf215546Sopenharmony_ci   case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
131bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_streamout_buffers;
132bf215546Sopenharmony_ci   case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
133bf215546Sopenharmony_ci   case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
134bf215546Sopenharmony_ci      return 16*4;
135bf215546Sopenharmony_ci   case PIPE_CAP_SUPPORTED_PRIM_MODES:
136bf215546Sopenharmony_ci      return BITFIELD_MASK(PIPE_PRIM_MAX) &
137bf215546Sopenharmony_ci            ~BITFIELD_BIT(PIPE_PRIM_QUADS) &
138bf215546Sopenharmony_ci            ~BITFIELD_BIT(PIPE_PRIM_QUAD_STRIP);
139bf215546Sopenharmony_ci   case PIPE_CAP_PRIMITIVE_RESTART:
140bf215546Sopenharmony_ci   case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
141bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.primitive_restart;
142bf215546Sopenharmony_ci   case PIPE_CAP_SHADER_STENCIL_EXPORT:
143bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.shader_stencil_export;
144bf215546Sopenharmony_ci   case PIPE_CAP_VS_INSTANCEID:
145bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
146bf215546Sopenharmony_ci      return 1;
147bf215546Sopenharmony_ci   case PIPE_CAP_SEAMLESS_CUBE_MAP:
148bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.seamless_cube_map;
149bf215546Sopenharmony_ci   case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
150bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.seamless_cube_map_per_texture;
151bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
152bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_texture_array_layers;
153bf215546Sopenharmony_ci   case PIPE_CAP_MIN_TEXEL_OFFSET:
154bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.min_texel_offset;
155bf215546Sopenharmony_ci   case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
156bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.min_texture_gather_offset;
157bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXEL_OFFSET:
158bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_texel_offset;
159bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
160bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_texture_gather_offset;
161bf215546Sopenharmony_ci   case PIPE_CAP_CONDITIONAL_RENDER:
162bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.conditional_render;
163bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_BARRIER:
164bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TEXTURE_BARRIER;
165bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
166bf215546Sopenharmony_ci      return 1;
167bf215546Sopenharmony_ci   case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
168bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_COLOR_CLAMPED:
169bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.color_clamping;
170bf215546Sopenharmony_ci   case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
171bf215546Sopenharmony_ci      return (vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FBO_MIXED_COLOR_FORMATS) ||
172bf215546Sopenharmony_ci            (vscreen->caps.caps.v2.host_feature_check_version < 1);
173bf215546Sopenharmony_ci   case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
174bf215546Sopenharmony_ci       if (vscreen->caps.caps.v2.host_feature_check_version < 6)
175bf215546Sopenharmony_ci           return MIN2(vscreen->caps.caps.v1.glsl_level, 140);
176bf215546Sopenharmony_ci       FALLTHROUGH;
177bf215546Sopenharmony_ci   case PIPE_CAP_GLSL_FEATURE_LEVEL:
178bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.glsl_level;
179bf215546Sopenharmony_ci   case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
180bf215546Sopenharmony_ci      return 1;
181bf215546Sopenharmony_ci   case PIPE_CAP_DEPTH_CLIP_DISABLE_SEPARATE:
182bf215546Sopenharmony_ci      return 0;
183bf215546Sopenharmony_ci   case PIPE_CAP_COMPUTE:
184bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_COMPUTE_SHADER;
185bf215546Sopenharmony_ci   case PIPE_CAP_USER_VERTEX_BUFFERS:
186bf215546Sopenharmony_ci      return 0;
187bf215546Sopenharmony_ci   case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
188bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.uniform_buffer_offset_alignment;
189bf215546Sopenharmony_ci   case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
190bf215546Sopenharmony_ci   case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
191bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.streamout_pause_resume;
192bf215546Sopenharmony_ci   case PIPE_CAP_START_INSTANCE:
193bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.start_instance;
194bf215546Sopenharmony_ci   case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS:
195bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
196bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
197bf215546Sopenharmony_ci   case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
198bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_TRANSFER_MODES:
199bf215546Sopenharmony_ci   case PIPE_CAP_NIR_IMAGES_AS_DEREF:
200bf215546Sopenharmony_ci      return 0;
201bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_TIMESTAMP:
202bf215546Sopenharmony_ci      return 1;
203bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_TIME_ELAPSED:
204bf215546Sopenharmony_ci      return 1;
205bf215546Sopenharmony_ci   case PIPE_CAP_TGSI_TEXCOORD:
206bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.host_feature_check_version >= 10;
207bf215546Sopenharmony_ci   case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
208bf215546Sopenharmony_ci      return VIRGL_MAP_BUFFER_ALIGNMENT;
209bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
210bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_tbo_size > 0;
211bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
212bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.texture_buffer_offset_alignment;
213bf215546Sopenharmony_ci   case PIPE_CAP_BUFFER_SAMPLER_VIEW_RGBA_ONLY:
214bf215546Sopenharmony_ci      return 0;
215bf215546Sopenharmony_ci   case PIPE_CAP_CUBE_MAP_ARRAY:
216bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.cube_map_array;
217bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_MULTISAMPLE:
218bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.texture_multisample;
219bf215546Sopenharmony_ci   case PIPE_CAP_MAX_VIEWPORTS:
220bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_viewports;
221bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXEL_BUFFER_ELEMENTS_UINT:
222bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_tbo_size;
223bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
224bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
225bf215546Sopenharmony_ci   case PIPE_CAP_ENDIANNESS:
226bf215546Sopenharmony_ci      return 0;
227bf215546Sopenharmony_ci   case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
228bf215546Sopenharmony_ci   case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
229bf215546Sopenharmony_ci      return 1;
230bf215546Sopenharmony_ci   case PIPE_CAP_VS_LAYER_VIEWPORT:
231bf215546Sopenharmony_ci      return 0;
232bf215546Sopenharmony_ci   case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
233bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_geom_output_vertices;
234bf215546Sopenharmony_ci   case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
235bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_geom_total_output_components;
236bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_QUERY_LOD:
237bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.texture_query_lod;
238bf215546Sopenharmony_ci   case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
239bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.max_texture_gather_components;
240bf215546Sopenharmony_ci   case PIPE_CAP_DRAW_INDIRECT:
241bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.has_indirect_draw;
242bf215546Sopenharmony_ci   case PIPE_CAP_SAMPLE_SHADING:
243bf215546Sopenharmony_ci   case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
244bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.has_sample_shading;
245bf215546Sopenharmony_ci   case PIPE_CAP_CULL_DISTANCE:
246bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.has_cull;
247bf215546Sopenharmony_ci   case PIPE_CAP_MAX_VERTEX_STREAMS:
248bf215546Sopenharmony_ci      return ((vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TRANSFORM_FEEDBACK3) ||
249bf215546Sopenharmony_ci              (vscreen->caps.caps.v2.host_feature_check_version < 2)) ? 4 : 1;
250bf215546Sopenharmony_ci   case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
251bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.conditional_render_inverted;
252bf215546Sopenharmony_ci   case PIPE_CAP_FS_FINE_DERIVATIVE:
253bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.derivative_control;
254bf215546Sopenharmony_ci   case PIPE_CAP_POLYGON_OFFSET_CLAMP:
255bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.polygon_offset_clamp;
256bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_SO_OVERFLOW:
257bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.transform_feedback_overflow_query;
258bf215546Sopenharmony_ci   case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
259bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.shader_buffer_offset_alignment;
260bf215546Sopenharmony_ci   case PIPE_CAP_DOUBLES:
261bf215546Sopenharmony_ci      return vscreen->caps.caps.v1.bset.has_fp64 ||
262bf215546Sopenharmony_ci            (vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FAKE_FP64);
263bf215546Sopenharmony_ci   case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
264bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_shader_patch_varyings;
265bf215546Sopenharmony_ci   case PIPE_CAP_SAMPLER_VIEW_TARGET:
266bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TEXTURE_VIEW;
267bf215546Sopenharmony_ci   case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
268bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_vertex_attrib_stride;
269bf215546Sopenharmony_ci   case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
270bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_COPY_IMAGE;
271bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_QUERY_SAMPLES:
272bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TXQS;
273bf215546Sopenharmony_ci   case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
274bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FB_NO_ATTACH;
275bf215546Sopenharmony_ci   case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
276bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_ROBUST_BUFFER_ACCESS;
277bf215546Sopenharmony_ci   case PIPE_CAP_FBFETCH:
278bf215546Sopenharmony_ci      return (vscreen->caps.caps.v2.capability_bits &
279bf215546Sopenharmony_ci              VIRGL_CAP_TGSI_FBFETCH) ? 1 : 0;
280bf215546Sopenharmony_ci   case PIPE_CAP_BLEND_EQUATION_ADVANCED:
281bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_BLEND_EQUATION;
282bf215546Sopenharmony_ci   case PIPE_CAP_SHADER_CLOCK:
283bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_SHADER_CLOCK;
284bf215546Sopenharmony_ci   case PIPE_CAP_SHADER_ARRAY_COMPONENTS:
285bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TGSI_COMPONENTS;
286bf215546Sopenharmony_ci   case PIPE_CAP_MAX_COMBINED_SHADER_BUFFERS:
287bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_combined_shader_buffers;
288bf215546Sopenharmony_ci   case PIPE_CAP_MAX_COMBINED_HW_ATOMIC_COUNTERS:
289bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_combined_atomic_counters;
290bf215546Sopenharmony_ci   case PIPE_CAP_MAX_COMBINED_HW_ATOMIC_COUNTER_BUFFERS:
291bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_combined_atomic_counter_buffers;
292bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
293bf215546Sopenharmony_ci   case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
294bf215546Sopenharmony_ci      return 1; /* TODO: need to introduce a hw-cap for this */
295bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_BUFFER_OBJECT:
296bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_QBO;
297bf215546Sopenharmony_ci   case PIPE_CAP_MAX_VARYINGS:
298bf215546Sopenharmony_ci      if (vscreen->caps.caps.v1.glsl_level < 150)
299bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_vertex_attribs;
300bf215546Sopenharmony_ci      return 32;
301bf215546Sopenharmony_ci   case PIPE_CAP_FAKE_SW_MSAA:
302bf215546Sopenharmony_ci      /* If the host supports only one sample (e.g., if it is using softpipe),
303bf215546Sopenharmony_ci       * fake multisampling to able to advertise higher GL versions. */
304bf215546Sopenharmony_ci      return (vscreen->caps.caps.v1.max_samples == 1) ? 1 : 0;
305bf215546Sopenharmony_ci   case PIPE_CAP_MULTI_DRAW_INDIRECT:
306bf215546Sopenharmony_ci      return !!(vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_MULTI_DRAW_INDIRECT);
307bf215546Sopenharmony_ci   case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
308bf215546Sopenharmony_ci      return !!(vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_INDIRECT_PARAMS);
309bf215546Sopenharmony_ci   case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
310bf215546Sopenharmony_ci      return (vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_ARB_BUFFER_STORAGE) &&
311bf215546Sopenharmony_ci             (vscreen->caps.caps.v2.host_feature_check_version >= 4) &&
312bf215546Sopenharmony_ci              vscreen->vws->supports_coherent && !vscreen->no_coherent;
313bf215546Sopenharmony_ci   case PIPE_CAP_PCI_GROUP:
314bf215546Sopenharmony_ci   case PIPE_CAP_PCI_BUS:
315bf215546Sopenharmony_ci   case PIPE_CAP_PCI_DEVICE:
316bf215546Sopenharmony_ci   case PIPE_CAP_PCI_FUNCTION:
317bf215546Sopenharmony_ci   case PIPE_CAP_ALLOW_MAPPED_BUFFERS_DURING_EXECUTION:
318bf215546Sopenharmony_ci      return 0;
319bf215546Sopenharmony_ci   case PIPE_CAP_CLEAR_TEXTURE:
320bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_CLEAR_TEXTURE;
321bf215546Sopenharmony_ci   case PIPE_CAP_CLIP_HALFZ:
322bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_CLIP_HALFZ;
323bf215546Sopenharmony_ci   case PIPE_CAP_MAX_GS_INVOCATIONS:
324bf215546Sopenharmony_ci      return 32;
325bf215546Sopenharmony_ci   case PIPE_CAP_MAX_SHADER_BUFFER_SIZE_UINT:
326bf215546Sopenharmony_ci      return 1 << 27;
327bf215546Sopenharmony_ci   case PIPE_CAP_VENDOR_ID:
328bf215546Sopenharmony_ci      return 0x1af4;
329bf215546Sopenharmony_ci   case PIPE_CAP_DEVICE_ID:
330bf215546Sopenharmony_ci      return 0x1010;
331bf215546Sopenharmony_ci   case PIPE_CAP_ACCELERATED:
332bf215546Sopenharmony_ci      return -1; /* -1 means unknown */
333bf215546Sopenharmony_ci   case PIPE_CAP_UMA:
334bf215546Sopenharmony_ci   case PIPE_CAP_VIDEO_MEMORY:
335bf215546Sopenharmony_ci      if (vscreen->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_VIDEO_MEMORY)
336bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_video_memory;
337bf215546Sopenharmony_ci      return 0;
338bf215546Sopenharmony_ci   case PIPE_CAP_NATIVE_FENCE_FD:
339bf215546Sopenharmony_ci      return vscreen->vws->supports_fences;
340bf215546Sopenharmony_ci   case PIPE_CAP_DEST_SURFACE_SRGB_CONTROL:
341bf215546Sopenharmony_ci      return (vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_SRGB_WRITE_CONTROL) ||
342bf215546Sopenharmony_ci            (vscreen->caps.caps.v2.host_feature_check_version < 1);
343bf215546Sopenharmony_ci   case PIPE_CAP_SHAREABLE_SHADERS:
344bf215546Sopenharmony_ci      /* Shader creation emits the shader through the context's command buffer
345bf215546Sopenharmony_ci       * in virgl_encode_shader_state().
346bf215546Sopenharmony_ci       */
347bf215546Sopenharmony_ci      return 0;
348bf215546Sopenharmony_ci   case PIPE_CAP_QUERY_MEMORY_INFO:
349bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_MEMINFO;
350bf215546Sopenharmony_ci   case PIPE_CAP_STRING_MARKER:
351bf215546Sopenharmony_ci       return vscreen->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_STRING_MARKER;
352bf215546Sopenharmony_ci   case PIPE_CAP_SURFACE_SAMPLE_COUNT:
353bf215546Sopenharmony_ci       return vscreen->caps.caps.v2.capability_bits_v2 & VIRGL_CAP_V2_IMPLICIT_MSAA;
354bf215546Sopenharmony_ci   case PIPE_CAP_IMAGE_STORE_FORMATTED:
355bf215546Sopenharmony_ci      return 1;
356bf215546Sopenharmony_ci   case PIPE_CAP_MAX_CONSTANT_BUFFER_SIZE_UINT:
357bf215546Sopenharmony_ci      if (vscreen->caps.caps.v2.host_feature_check_version >= 13)
358bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_uniform_block_size;
359bf215546Sopenharmony_ci      FALLTHROUGH;
360bf215546Sopenharmony_ci   default:
361bf215546Sopenharmony_ci      return u_pipe_screen_get_param_defaults(screen, param);
362bf215546Sopenharmony_ci   }
363bf215546Sopenharmony_ci}
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_cistatic int
366bf215546Sopenharmony_civirgl_get_shader_param(struct pipe_screen *screen,
367bf215546Sopenharmony_ci                       enum pipe_shader_type shader,
368bf215546Sopenharmony_ci                       enum pipe_shader_cap param)
369bf215546Sopenharmony_ci{
370bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   if ((shader == PIPE_SHADER_TESS_CTRL || shader == PIPE_SHADER_TESS_EVAL) &&
373bf215546Sopenharmony_ci       !vscreen->caps.caps.v1.bset.has_tessellation_shaders)
374bf215546Sopenharmony_ci      return 0;
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   if (shader == PIPE_SHADER_COMPUTE &&
377bf215546Sopenharmony_ci       !(vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_COMPUTE_SHADER))
378bf215546Sopenharmony_ci     return 0;
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   switch(shader)
381bf215546Sopenharmony_ci   {
382bf215546Sopenharmony_ci   case PIPE_SHADER_FRAGMENT:
383bf215546Sopenharmony_ci   case PIPE_SHADER_VERTEX:
384bf215546Sopenharmony_ci   case PIPE_SHADER_GEOMETRY:
385bf215546Sopenharmony_ci   case PIPE_SHADER_TESS_CTRL:
386bf215546Sopenharmony_ci   case PIPE_SHADER_TESS_EVAL:
387bf215546Sopenharmony_ci   case PIPE_SHADER_COMPUTE:
388bf215546Sopenharmony_ci      switch (param) {
389bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
390bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
391bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
392bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
393bf215546Sopenharmony_ci         return INT_MAX;
394bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
395bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
396bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
397bf215546Sopenharmony_ci         return 1;
398bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
399bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
400bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_INDIRECT_INPUT_ADDR;
401bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_INPUTS:
402bf215546Sopenharmony_ci         if (vscreen->caps.caps.v1.glsl_level < 150)
403bf215546Sopenharmony_ci            return vscreen->caps.caps.v2.max_vertex_attribs;
404bf215546Sopenharmony_ci         return (shader == PIPE_SHADER_VERTEX ||
405bf215546Sopenharmony_ci                 shader == PIPE_SHADER_GEOMETRY) ? vscreen->caps.caps.v2.max_vertex_attribs : 32;
406bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_OUTPUTS:
407bf215546Sopenharmony_ci         if (shader == PIPE_SHADER_FRAGMENT)
408bf215546Sopenharmony_ci            return vscreen->caps.caps.v1.max_render_targets;
409bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_vertex_outputs;
410bf215546Sopenharmony_ci     // case PIPE_SHADER_CAP_MAX_CONSTS:
411bf215546Sopenharmony_ci     //    return 4096;
412bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_TEMPS:
413bf215546Sopenharmony_ci         return 256;
414bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
415bf215546Sopenharmony_ci         return vscreen->caps.caps.v1.max_uniform_blocks;
416bf215546Sopenharmony_ci    //  case PIPE_SHADER_CAP_MAX_ADDRS:
417bf215546Sopenharmony_ci     //    return 1;
418bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_SUBROUTINES:
419bf215546Sopenharmony_ci         return 1;
420bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
421bf215546Sopenharmony_ci         return MIN2(vscreen->caps.caps.v2.max_shader_sampler_views,
422bf215546Sopenharmony_ci                     PIPE_MAX_SHADER_SAMPLER_VIEWS);
423bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INTEGERS:
424bf215546Sopenharmony_ci         return vscreen->caps.caps.v1.glsl_level >= 130;
425bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
426bf215546Sopenharmony_ci         return 32;
427bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE:
428bf215546Sopenharmony_ci         if (vscreen->caps.caps.v2.host_feature_check_version < 12)
429bf215546Sopenharmony_ci            return 4096 * sizeof(float[4]);
430bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_const_buffer_size[shader];
431bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
432bf215546Sopenharmony_ci         if (shader == PIPE_SHADER_FRAGMENT || shader == PIPE_SHADER_COMPUTE)
433bf215546Sopenharmony_ci            return vscreen->caps.caps.v2.max_shader_buffer_frag_compute;
434bf215546Sopenharmony_ci         else
435bf215546Sopenharmony_ci            return vscreen->caps.caps.v2.max_shader_buffer_other_stages;
436bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
437bf215546Sopenharmony_ci         if (shader == PIPE_SHADER_FRAGMENT || shader == PIPE_SHADER_COMPUTE)
438bf215546Sopenharmony_ci            return vscreen->caps.caps.v2.max_shader_image_frag_compute;
439bf215546Sopenharmony_ci         else
440bf215546Sopenharmony_ci            return vscreen->caps.caps.v2.max_shader_image_other_stages;
441bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_PREFERRED_IR:
442bf215546Sopenharmony_ci         return (virgl_debug & VIRGL_DEBUG_USE_TGSI) ? PIPE_SHADER_IR_TGSI : PIPE_SHADER_IR_NIR;
443bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_SUPPORTED_IRS:
444bf215546Sopenharmony_ci         return (1 << PIPE_SHADER_IR_TGSI) | ((virgl_debug & VIRGL_DEBUG_USE_TGSI) ? 0 : (1 << PIPE_SHADER_IR_NIR));
445bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
446bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_atomic_counters[shader];
447bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
448bf215546Sopenharmony_ci         return vscreen->caps.caps.v2.max_atomic_counter_buffers[shader];
449bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INT64_ATOMICS:
450bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_FP16:
451bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_FP16_DERIVATIVES:
452bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_FP16_CONST_BUFFERS:
453bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_INT16:
454bf215546Sopenharmony_ci      case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
455bf215546Sopenharmony_ci         return 0;
456bf215546Sopenharmony_ci      default:
457bf215546Sopenharmony_ci         return 0;
458bf215546Sopenharmony_ci      }
459bf215546Sopenharmony_ci   default:
460bf215546Sopenharmony_ci      return 0;
461bf215546Sopenharmony_ci   }
462bf215546Sopenharmony_ci}
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_cistatic float
465bf215546Sopenharmony_civirgl_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
466bf215546Sopenharmony_ci{
467bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
468bf215546Sopenharmony_ci   switch (param) {
469bf215546Sopenharmony_ci   case PIPE_CAPF_MIN_LINE_WIDTH:
470bf215546Sopenharmony_ci   case PIPE_CAPF_MIN_LINE_WIDTH_AA:
471bf215546Sopenharmony_ci   case PIPE_CAPF_MIN_POINT_SIZE:
472bf215546Sopenharmony_ci   case PIPE_CAPF_MIN_POINT_SIZE_AA:
473bf215546Sopenharmony_ci      return 1;
474bf215546Sopenharmony_ci   case PIPE_CAPF_POINT_SIZE_GRANULARITY:
475bf215546Sopenharmony_ci   case PIPE_CAPF_LINE_WIDTH_GRANULARITY:
476bf215546Sopenharmony_ci      return 0.1;
477bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_LINE_WIDTH:
478bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_aliased_line_width;
479bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_LINE_WIDTH_AA:
480bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_smooth_line_width;
481bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_POINT_SIZE:
482bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_aliased_point_size;
483bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_POINT_SIZE_AA:
484bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_smooth_point_size;
485bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
486bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_anisotropy;
487bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
488bf215546Sopenharmony_ci      return vscreen->caps.caps.v2.max_texture_lod_bias;
489bf215546Sopenharmony_ci   case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
490bf215546Sopenharmony_ci   case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
491bf215546Sopenharmony_ci   case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
492bf215546Sopenharmony_ci      return 0.0f;
493bf215546Sopenharmony_ci   }
494bf215546Sopenharmony_ci   /* should only get here on unhandled cases */
495bf215546Sopenharmony_ci   debug_printf("Unexpected PIPE_CAPF %d query\n", param);
496bf215546Sopenharmony_ci   return 0.0;
497bf215546Sopenharmony_ci}
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_cistatic int
500bf215546Sopenharmony_civirgl_get_compute_param(struct pipe_screen *screen,
501bf215546Sopenharmony_ci                        enum pipe_shader_ir ir_type,
502bf215546Sopenharmony_ci                        enum pipe_compute_cap param,
503bf215546Sopenharmony_ci                        void *ret)
504bf215546Sopenharmony_ci{
505bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
506bf215546Sopenharmony_ci   if (!(vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_COMPUTE_SHADER))
507bf215546Sopenharmony_ci      return 0;
508bf215546Sopenharmony_ci   switch (param) {
509bf215546Sopenharmony_ci   case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
510bf215546Sopenharmony_ci      if (ret) {
511bf215546Sopenharmony_ci         uint64_t *grid_size = ret;
512bf215546Sopenharmony_ci         grid_size[0] = vscreen->caps.caps.v2.max_compute_grid_size[0];
513bf215546Sopenharmony_ci         grid_size[1] = vscreen->caps.caps.v2.max_compute_grid_size[1];
514bf215546Sopenharmony_ci         grid_size[2] = vscreen->caps.caps.v2.max_compute_grid_size[2];
515bf215546Sopenharmony_ci      }
516bf215546Sopenharmony_ci      return 3 * sizeof(uint64_t) ;
517bf215546Sopenharmony_ci   case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
518bf215546Sopenharmony_ci      if (ret) {
519bf215546Sopenharmony_ci         uint64_t *block_size = ret;
520bf215546Sopenharmony_ci         block_size[0] = vscreen->caps.caps.v2.max_compute_block_size[0];
521bf215546Sopenharmony_ci         block_size[1] = vscreen->caps.caps.v2.max_compute_block_size[1];
522bf215546Sopenharmony_ci         block_size[2] = vscreen->caps.caps.v2.max_compute_block_size[2];
523bf215546Sopenharmony_ci      }
524bf215546Sopenharmony_ci      return 3 * sizeof(uint64_t);
525bf215546Sopenharmony_ci   case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
526bf215546Sopenharmony_ci      if (ret) {
527bf215546Sopenharmony_ci         uint64_t *max_threads_per_block = ret;
528bf215546Sopenharmony_ci         *max_threads_per_block = vscreen->caps.caps.v2.max_compute_work_group_invocations;
529bf215546Sopenharmony_ci      }
530bf215546Sopenharmony_ci      return sizeof(uint64_t);
531bf215546Sopenharmony_ci   case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
532bf215546Sopenharmony_ci      if (ret) {
533bf215546Sopenharmony_ci         uint64_t *max_local_size = ret;
534bf215546Sopenharmony_ci         /* Value reported by the closed source driver. */
535bf215546Sopenharmony_ci         *max_local_size = vscreen->caps.caps.v2.max_compute_shared_memory_size;
536bf215546Sopenharmony_ci      }
537bf215546Sopenharmony_ci      return sizeof(uint64_t);
538bf215546Sopenharmony_ci   default:
539bf215546Sopenharmony_ci      break;
540bf215546Sopenharmony_ci   }
541bf215546Sopenharmony_ci   return 0;
542bf215546Sopenharmony_ci}
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_cistatic bool
545bf215546Sopenharmony_cihas_format_bit(struct virgl_supported_format_mask *mask,
546bf215546Sopenharmony_ci               enum virgl_formats fmt)
547bf215546Sopenharmony_ci{
548bf215546Sopenharmony_ci   assert(fmt < VIRGL_FORMAT_MAX);
549bf215546Sopenharmony_ci   unsigned val = (unsigned)fmt;
550bf215546Sopenharmony_ci   unsigned idx = val / 32;
551bf215546Sopenharmony_ci   unsigned bit = val % 32;
552bf215546Sopenharmony_ci   assert(idx < ARRAY_SIZE(mask->bitmask));
553bf215546Sopenharmony_ci   return (mask->bitmask[idx] & (1u << bit)) != 0;
554bf215546Sopenharmony_ci}
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_cibool
557bf215546Sopenharmony_civirgl_has_readback_format(struct pipe_screen *screen,
558bf215546Sopenharmony_ci                          enum virgl_formats fmt, bool allow_tweak)
559bf215546Sopenharmony_ci{
560bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
561bf215546Sopenharmony_ci   if (has_format_bit(&vscreen->caps.caps.v2.supported_readback_formats,
562bf215546Sopenharmony_ci                         fmt))
563bf215546Sopenharmony_ci      return true;
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_ci   if (allow_tweak && fmt == VIRGL_FORMAT_L8_SRGB && vscreen->tweak_l8_srgb_readback) {
566bf215546Sopenharmony_ci      return true;
567bf215546Sopenharmony_ci   }
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_ci   return false;
570bf215546Sopenharmony_ci}
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_cistatic bool
573bf215546Sopenharmony_civirgl_is_vertex_format_supported(struct pipe_screen *screen,
574bf215546Sopenharmony_ci                                 enum pipe_format format)
575bf215546Sopenharmony_ci{
576bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
577bf215546Sopenharmony_ci   const struct util_format_description *format_desc;
578bf215546Sopenharmony_ci   int i;
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci   format_desc = util_format_description(format);
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
583bf215546Sopenharmony_ci      int vformat = VIRGL_FORMAT_R11G11B10_FLOAT;
584bf215546Sopenharmony_ci      int big = vformat / 32;
585bf215546Sopenharmony_ci      int small = vformat % 32;
586bf215546Sopenharmony_ci      if (!(vscreen->caps.caps.v1.vertexbuffer.bitmask[big] & (1 << small)))
587bf215546Sopenharmony_ci         return false;
588bf215546Sopenharmony_ci      return true;
589bf215546Sopenharmony_ci   }
590bf215546Sopenharmony_ci
591bf215546Sopenharmony_ci   /* Find the first non-VOID channel. */
592bf215546Sopenharmony_ci   for (i = 0; i < 4; i++) {
593bf215546Sopenharmony_ci      if (format_desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
594bf215546Sopenharmony_ci         break;
595bf215546Sopenharmony_ci      }
596bf215546Sopenharmony_ci   }
597bf215546Sopenharmony_ci
598bf215546Sopenharmony_ci   if (i == 4)
599bf215546Sopenharmony_ci      return false;
600bf215546Sopenharmony_ci
601bf215546Sopenharmony_ci   if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
602bf215546Sopenharmony_ci      return false;
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci   if (format_desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED)
605bf215546Sopenharmony_ci      return false;
606bf215546Sopenharmony_ci   return true;
607bf215546Sopenharmony_ci}
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_cistatic bool
610bf215546Sopenharmony_civirgl_format_check_bitmask(enum pipe_format format,
611bf215546Sopenharmony_ci                           uint32_t bitmask[16],
612bf215546Sopenharmony_ci                           bool may_emulate_bgra)
613bf215546Sopenharmony_ci{
614bf215546Sopenharmony_ci   enum virgl_formats vformat = pipe_to_virgl_format(format);
615bf215546Sopenharmony_ci   int big = vformat / 32;
616bf215546Sopenharmony_ci   int small = vformat % 32;
617bf215546Sopenharmony_ci   if ((bitmask[big] & (1 << small)))
618bf215546Sopenharmony_ci      return true;
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_ci   /* On GLES hosts we don't advertise BGRx_SRGB, but we may be able
621bf215546Sopenharmony_ci    * emulate it by using a swizzled RGBx */
622bf215546Sopenharmony_ci   if (may_emulate_bgra) {
623bf215546Sopenharmony_ci      if (format == PIPE_FORMAT_B8G8R8A8_SRGB)
624bf215546Sopenharmony_ci         format = PIPE_FORMAT_R8G8B8A8_SRGB;
625bf215546Sopenharmony_ci      else if (format == PIPE_FORMAT_B8G8R8X8_SRGB)
626bf215546Sopenharmony_ci         format = PIPE_FORMAT_R8G8B8X8_SRGB;
627bf215546Sopenharmony_ci      else {
628bf215546Sopenharmony_ci         return false;
629bf215546Sopenharmony_ci      }
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci      vformat = pipe_to_virgl_format(format);
632bf215546Sopenharmony_ci      big = vformat / 32;
633bf215546Sopenharmony_ci      small = vformat % 32;
634bf215546Sopenharmony_ci      if (bitmask[big] & (1 << small))
635bf215546Sopenharmony_ci         return true;
636bf215546Sopenharmony_ci   }
637bf215546Sopenharmony_ci   return false;
638bf215546Sopenharmony_ci}
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_cibool virgl_has_scanout_format(struct virgl_screen *vscreen,
641bf215546Sopenharmony_ci                              enum pipe_format format,
642bf215546Sopenharmony_ci                              bool may_emulate_bgra)
643bf215546Sopenharmony_ci{
644bf215546Sopenharmony_ci   return  virgl_format_check_bitmask(format,
645bf215546Sopenharmony_ci                                      vscreen->caps.caps.v2.scanout.bitmask,
646bf215546Sopenharmony_ci                                      may_emulate_bgra);
647bf215546Sopenharmony_ci}
648bf215546Sopenharmony_ci
649bf215546Sopenharmony_ci/**
650bf215546Sopenharmony_ci * Query format support for creating a texture, drawing surface, etc.
651bf215546Sopenharmony_ci * \param format  the format to test
652bf215546Sopenharmony_ci * \param type  one of PIPE_TEXTURE, PIPE_SURFACE
653bf215546Sopenharmony_ci */
654bf215546Sopenharmony_cistatic bool
655bf215546Sopenharmony_civirgl_is_format_supported( struct pipe_screen *screen,
656bf215546Sopenharmony_ci                                 enum pipe_format format,
657bf215546Sopenharmony_ci                                 enum pipe_texture_target target,
658bf215546Sopenharmony_ci                                 unsigned sample_count,
659bf215546Sopenharmony_ci                                 unsigned storage_sample_count,
660bf215546Sopenharmony_ci                                 unsigned bind)
661bf215546Sopenharmony_ci{
662bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
663bf215546Sopenharmony_ci   const struct util_format_description *format_desc;
664bf215546Sopenharmony_ci   int i;
665bf215546Sopenharmony_ci
666bf215546Sopenharmony_ci   union virgl_caps *caps = &vscreen->caps.caps;
667bf215546Sopenharmony_ci   boolean may_emulate_bgra = (caps->v2.capability_bits &
668bf215546Sopenharmony_ci                               VIRGL_CAP_APP_TWEAK_SUPPORT) &&
669bf215546Sopenharmony_ci                               vscreen->tweak_gles_emulate_bgra;
670bf215546Sopenharmony_ci
671bf215546Sopenharmony_ci   if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
672bf215546Sopenharmony_ci      return false;
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci   if (!util_is_power_of_two_or_zero(sample_count))
675bf215546Sopenharmony_ci      return false;
676bf215546Sopenharmony_ci
677bf215546Sopenharmony_ci   assert(target == PIPE_BUFFER ||
678bf215546Sopenharmony_ci          target == PIPE_TEXTURE_1D ||
679bf215546Sopenharmony_ci          target == PIPE_TEXTURE_1D_ARRAY ||
680bf215546Sopenharmony_ci          target == PIPE_TEXTURE_2D ||
681bf215546Sopenharmony_ci          target == PIPE_TEXTURE_2D_ARRAY ||
682bf215546Sopenharmony_ci          target == PIPE_TEXTURE_RECT ||
683bf215546Sopenharmony_ci          target == PIPE_TEXTURE_3D ||
684bf215546Sopenharmony_ci          target == PIPE_TEXTURE_CUBE ||
685bf215546Sopenharmony_ci          target == PIPE_TEXTURE_CUBE_ARRAY);
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci   format_desc = util_format_description(format);
688bf215546Sopenharmony_ci
689bf215546Sopenharmony_ci   if (util_format_is_intensity(format))
690bf215546Sopenharmony_ci      return false;
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_ci   if (sample_count > 1) {
693bf215546Sopenharmony_ci      if (!caps->v1.bset.texture_multisample)
694bf215546Sopenharmony_ci         return false;
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci      if (bind & PIPE_BIND_SHADER_IMAGE) {
697bf215546Sopenharmony_ci         if (sample_count > caps->v2.max_image_samples)
698bf215546Sopenharmony_ci            return false;
699bf215546Sopenharmony_ci      }
700bf215546Sopenharmony_ci
701bf215546Sopenharmony_ci      if (sample_count > caps->v1.max_samples)
702bf215546Sopenharmony_ci         return false;
703bf215546Sopenharmony_ci
704bf215546Sopenharmony_ci      if (caps->v2.host_feature_check_version >= 9 &&
705bf215546Sopenharmony_ci          !has_format_bit(&caps->v2.supported_multisample_formats,
706bf215546Sopenharmony_ci                          pipe_to_virgl_format(format)))
707bf215546Sopenharmony_ci         return false;
708bf215546Sopenharmony_ci   }
709bf215546Sopenharmony_ci
710bf215546Sopenharmony_ci   if (bind & PIPE_BIND_VERTEX_BUFFER) {
711bf215546Sopenharmony_ci      return virgl_is_vertex_format_supported(screen, format);
712bf215546Sopenharmony_ci   }
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci   if (util_format_is_compressed(format) && target == PIPE_BUFFER)
715bf215546Sopenharmony_ci      return false;
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_ci   /* Allow 3-comp 32 bit textures only for TBOs (needed for ARB_tbo_rgb32) */
718bf215546Sopenharmony_ci   if ((format == PIPE_FORMAT_R32G32B32_FLOAT ||
719bf215546Sopenharmony_ci       format == PIPE_FORMAT_R32G32B32_SINT ||
720bf215546Sopenharmony_ci       format == PIPE_FORMAT_R32G32B32_UINT) &&
721bf215546Sopenharmony_ci       target != PIPE_BUFFER)
722bf215546Sopenharmony_ci      return false;
723bf215546Sopenharmony_ci
724bf215546Sopenharmony_ci   if ((format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC ||
725bf215546Sopenharmony_ci        format_desc->layout == UTIL_FORMAT_LAYOUT_ETC ||
726bf215546Sopenharmony_ci        format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) &&
727bf215546Sopenharmony_ci       target == PIPE_TEXTURE_3D)
728bf215546Sopenharmony_ci      return false;
729bf215546Sopenharmony_ci
730bf215546Sopenharmony_ci
731bf215546Sopenharmony_ci   if (bind & PIPE_BIND_RENDER_TARGET) {
732bf215546Sopenharmony_ci      /* For ARB_framebuffer_no_attachments. */
733bf215546Sopenharmony_ci      if (format == PIPE_FORMAT_NONE)
734bf215546Sopenharmony_ci         return TRUE;
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci      if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
737bf215546Sopenharmony_ci         return false;
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci      /*
740bf215546Sopenharmony_ci       * Although possible, it is unnatural to render into compressed or YUV
741bf215546Sopenharmony_ci       * surfaces. So disable these here to avoid going into weird paths
742bf215546Sopenharmony_ci       * inside gallium frontends.
743bf215546Sopenharmony_ci       */
744bf215546Sopenharmony_ci      if (format_desc->block.width != 1 ||
745bf215546Sopenharmony_ci          format_desc->block.height != 1)
746bf215546Sopenharmony_ci         return false;
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_ci      if (!virgl_format_check_bitmask(format,
749bf215546Sopenharmony_ci                                      caps->v1.render.bitmask,
750bf215546Sopenharmony_ci                                      may_emulate_bgra))
751bf215546Sopenharmony_ci         return false;
752bf215546Sopenharmony_ci   }
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_ci   if (bind & PIPE_BIND_DEPTH_STENCIL) {
755bf215546Sopenharmony_ci      if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
756bf215546Sopenharmony_ci         return false;
757bf215546Sopenharmony_ci   }
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci   if (bind & PIPE_BIND_SCANOUT) {
760bf215546Sopenharmony_ci      if (!virgl_format_check_bitmask(format, caps->v2.scanout.bitmask, false))
761bf215546Sopenharmony_ci         return false;
762bf215546Sopenharmony_ci   }
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   /*
765bf215546Sopenharmony_ci    * All other operations (sampling, transfer, etc).
766bf215546Sopenharmony_ci    */
767bf215546Sopenharmony_ci
768bf215546Sopenharmony_ci   if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
769bf215546Sopenharmony_ci      goto out_lookup;
770bf215546Sopenharmony_ci   }
771bf215546Sopenharmony_ci   if (format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
772bf215546Sopenharmony_ci      goto out_lookup;
773bf215546Sopenharmony_ci   }
774bf215546Sopenharmony_ci   if (format_desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
775bf215546Sopenharmony_ci      goto out_lookup;
776bf215546Sopenharmony_ci   }
777bf215546Sopenharmony_ci   if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC) {
778bf215546Sopenharmony_ci      goto out_lookup;
779bf215546Sopenharmony_ci   }
780bf215546Sopenharmony_ci
781bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
782bf215546Sopenharmony_ci      goto out_lookup;
783bf215546Sopenharmony_ci   } else if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
784bf215546Sopenharmony_ci      goto out_lookup;
785bf215546Sopenharmony_ci   }
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci   if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
788bf215546Sopenharmony_ci     goto out_lookup;
789bf215546Sopenharmony_ci   }
790bf215546Sopenharmony_ci
791bf215546Sopenharmony_ci   /* Find the first non-VOID channel. */
792bf215546Sopenharmony_ci   for (i = 0; i < 4; i++) {
793bf215546Sopenharmony_ci      if (format_desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
794bf215546Sopenharmony_ci         break;
795bf215546Sopenharmony_ci      }
796bf215546Sopenharmony_ci   }
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci   if (i == 4)
799bf215546Sopenharmony_ci      return false;
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_ci   /* no L4A4 */
802bf215546Sopenharmony_ci   if (format_desc->nr_channels < 4 && format_desc->channel[i].size == 4)
803bf215546Sopenharmony_ci      return false;
804bf215546Sopenharmony_ci
805bf215546Sopenharmony_ci out_lookup:
806bf215546Sopenharmony_ci   return virgl_format_check_bitmask(format,
807bf215546Sopenharmony_ci                                     caps->v1.sampler.bitmask,
808bf215546Sopenharmony_ci                                     may_emulate_bgra);
809bf215546Sopenharmony_ci}
810bf215546Sopenharmony_ci
811bf215546Sopenharmony_cistatic void virgl_flush_frontbuffer(struct pipe_screen *screen,
812bf215546Sopenharmony_ci                                    struct pipe_context *ctx,
813bf215546Sopenharmony_ci                                      struct pipe_resource *res,
814bf215546Sopenharmony_ci                                      unsigned level, unsigned layer,
815bf215546Sopenharmony_ci                                    void *winsys_drawable_handle, struct pipe_box *sub_box)
816bf215546Sopenharmony_ci{
817bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
818bf215546Sopenharmony_ci   struct virgl_winsys *vws = vscreen->vws;
819bf215546Sopenharmony_ci   struct virgl_resource *vres = virgl_resource(res);
820bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_ci   if (vws->flush_frontbuffer) {
823bf215546Sopenharmony_ci      virgl_flush_eq(vctx, vctx, NULL);
824bf215546Sopenharmony_ci      vws->flush_frontbuffer(vws, vres->hw_res, level, layer, winsys_drawable_handle,
825bf215546Sopenharmony_ci                             sub_box);
826bf215546Sopenharmony_ci   }
827bf215546Sopenharmony_ci}
828bf215546Sopenharmony_ci
829bf215546Sopenharmony_cistatic void virgl_fence_reference(struct pipe_screen *screen,
830bf215546Sopenharmony_ci                                  struct pipe_fence_handle **ptr,
831bf215546Sopenharmony_ci                                  struct pipe_fence_handle *fence)
832bf215546Sopenharmony_ci{
833bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
834bf215546Sopenharmony_ci   struct virgl_winsys *vws = vscreen->vws;
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci   vws->fence_reference(vws, ptr, fence);
837bf215546Sopenharmony_ci}
838bf215546Sopenharmony_ci
839bf215546Sopenharmony_cistatic bool virgl_fence_finish(struct pipe_screen *screen,
840bf215546Sopenharmony_ci                               struct pipe_context *ctx,
841bf215546Sopenharmony_ci                               struct pipe_fence_handle *fence,
842bf215546Sopenharmony_ci                               uint64_t timeout)
843bf215546Sopenharmony_ci{
844bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
845bf215546Sopenharmony_ci   struct virgl_winsys *vws = vscreen->vws;
846bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
847bf215546Sopenharmony_ci
848bf215546Sopenharmony_ci   if (vctx && timeout)
849bf215546Sopenharmony_ci      virgl_flush_eq(vctx, NULL, NULL);
850bf215546Sopenharmony_ci
851bf215546Sopenharmony_ci   return vws->fence_wait(vws, fence, timeout);
852bf215546Sopenharmony_ci}
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_cistatic int virgl_fence_get_fd(struct pipe_screen *screen,
855bf215546Sopenharmony_ci            struct pipe_fence_handle *fence)
856bf215546Sopenharmony_ci{
857bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
858bf215546Sopenharmony_ci   struct virgl_winsys *vws = vscreen->vws;
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci   return vws->fence_get_fd(vws, fence);
861bf215546Sopenharmony_ci}
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_cistatic uint64_t
864bf215546Sopenharmony_civirgl_get_timestamp(struct pipe_screen *_screen)
865bf215546Sopenharmony_ci{
866bf215546Sopenharmony_ci   return os_time_get_nano();
867bf215546Sopenharmony_ci}
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_cistatic void
870bf215546Sopenharmony_civirgl_destroy_screen(struct pipe_screen *screen)
871bf215546Sopenharmony_ci{
872bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
873bf215546Sopenharmony_ci   struct virgl_winsys *vws = vscreen->vws;
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_ci   slab_destroy_parent(&vscreen->transfer_pool);
876bf215546Sopenharmony_ci
877bf215546Sopenharmony_ci   if (vws)
878bf215546Sopenharmony_ci      vws->destroy(vws);
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci   disk_cache_destroy(vscreen->disk_cache);
881bf215546Sopenharmony_ci
882bf215546Sopenharmony_ci   FREE(vscreen);
883bf215546Sopenharmony_ci}
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_cistatic void
886bf215546Sopenharmony_cifixup_formats(union virgl_caps *caps, struct virgl_supported_format_mask *mask)
887bf215546Sopenharmony_ci{
888bf215546Sopenharmony_ci   const size_t size = ARRAY_SIZE(mask->bitmask);
889bf215546Sopenharmony_ci   for (int i = 0; i < size; ++i) {
890bf215546Sopenharmony_ci      if (mask->bitmask[i] != 0)
891bf215546Sopenharmony_ci         return; /* we got some formats, we definitely have a new protocol */
892bf215546Sopenharmony_ci   }
893bf215546Sopenharmony_ci
894bf215546Sopenharmony_ci   /* old protocol used; fall back to considering all sampleable formats valid
895bf215546Sopenharmony_ci    * readback-formats
896bf215546Sopenharmony_ci    */
897bf215546Sopenharmony_ci   for (int i = 0; i < size; ++i)
898bf215546Sopenharmony_ci      mask->bitmask[i] = caps->v1.sampler.bitmask[i];
899bf215546Sopenharmony_ci}
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_cistatic void virgl_query_memory_info(struct pipe_screen *screen, struct pipe_memory_info *info)
902bf215546Sopenharmony_ci{
903bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(screen);
904bf215546Sopenharmony_ci   struct pipe_context *ctx = screen->context_create(screen, NULL, 0);
905bf215546Sopenharmony_ci   struct virgl_context *vctx = virgl_context(ctx);
906bf215546Sopenharmony_ci   struct virgl_resource *res;
907bf215546Sopenharmony_ci   struct virgl_memory_info virgl_info = {0};
908bf215546Sopenharmony_ci   const static struct pipe_resource templ = {
909bf215546Sopenharmony_ci      .target = PIPE_BUFFER,
910bf215546Sopenharmony_ci      .format = PIPE_FORMAT_R8_UNORM,
911bf215546Sopenharmony_ci      .bind = PIPE_BIND_CUSTOM,
912bf215546Sopenharmony_ci      .width0 = sizeof(struct virgl_memory_info),
913bf215546Sopenharmony_ci      .height0 = 1,
914bf215546Sopenharmony_ci      .depth0 = 1,
915bf215546Sopenharmony_ci      .array_size = 1,
916bf215546Sopenharmony_ci      .last_level = 0,
917bf215546Sopenharmony_ci      .nr_samples = 0,
918bf215546Sopenharmony_ci      .flags = 0
919bf215546Sopenharmony_ci   };
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ci   res = (struct virgl_resource*) screen->resource_create(screen, &templ);
922bf215546Sopenharmony_ci
923bf215546Sopenharmony_ci   virgl_encode_get_memory_info(vctx, res);
924bf215546Sopenharmony_ci   ctx->flush(ctx, NULL, 0);
925bf215546Sopenharmony_ci   vscreen->vws->resource_wait(vscreen->vws, res->hw_res);
926bf215546Sopenharmony_ci   pipe_buffer_read(ctx, &res->b, 0, sizeof(struct virgl_memory_info), &virgl_info);
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci   info->avail_device_memory = virgl_info.avail_device_memory;
929bf215546Sopenharmony_ci   info->avail_staging_memory = virgl_info.avail_staging_memory;
930bf215546Sopenharmony_ci   info->device_memory_evicted = virgl_info.device_memory_evicted;
931bf215546Sopenharmony_ci   info->nr_device_memory_evictions = virgl_info.nr_device_memory_evictions;
932bf215546Sopenharmony_ci   info->total_device_memory = virgl_info.total_device_memory;
933bf215546Sopenharmony_ci   info->total_staging_memory = virgl_info.total_staging_memory;
934bf215546Sopenharmony_ci
935bf215546Sopenharmony_ci   screen->resource_destroy(screen, &res->b);
936bf215546Sopenharmony_ci   ctx->destroy(ctx);
937bf215546Sopenharmony_ci}
938bf215546Sopenharmony_ci
939bf215546Sopenharmony_cistatic struct disk_cache *virgl_get_disk_shader_cache (struct pipe_screen *pscreen)
940bf215546Sopenharmony_ci{
941bf215546Sopenharmony_ci   struct virgl_screen *screen = virgl_screen(pscreen);
942bf215546Sopenharmony_ci
943bf215546Sopenharmony_ci   return screen->disk_cache;
944bf215546Sopenharmony_ci}
945bf215546Sopenharmony_ci
946bf215546Sopenharmony_cistatic void virgl_disk_cache_create(struct virgl_screen *screen)
947bf215546Sopenharmony_ci{
948bf215546Sopenharmony_ci   const struct build_id_note *note =
949bf215546Sopenharmony_ci      build_id_find_nhdr_for_addr(virgl_disk_cache_create);
950bf215546Sopenharmony_ci   unsigned build_id_len = build_id_length(note);
951bf215546Sopenharmony_ci   assert(note && build_id_len == 20); /* sha1 */
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci   const uint8_t *id_sha1 = build_id_data(note);
954bf215546Sopenharmony_ci   assert(id_sha1);
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci   struct mesa_sha1 sha1_ctx;
957bf215546Sopenharmony_ci   _mesa_sha1_init(&sha1_ctx);
958bf215546Sopenharmony_ci   _mesa_sha1_update(&sha1_ctx, id_sha1, build_id_len);
959bf215546Sopenharmony_ci
960bf215546Sopenharmony_ci   uint32_t shader_debug_flags = virgl_debug & VIRGL_DEBUG_USE_TGSI;
961bf215546Sopenharmony_ci   _mesa_sha1_update(&sha1_ctx, &shader_debug_flags, sizeof(shader_debug_flags));
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci   /* When we switch the host the caps might change and then we might have to
964bf215546Sopenharmony_ci    * apply different lowering. */
965bf215546Sopenharmony_ci   _mesa_sha1_update(&sha1_ctx, &screen->caps, sizeof(screen->caps));
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci   uint8_t sha1[20];
968bf215546Sopenharmony_ci   _mesa_sha1_final(&sha1_ctx, sha1);
969bf215546Sopenharmony_ci   char timestamp[41];
970bf215546Sopenharmony_ci   _mesa_sha1_format(timestamp, sha1);
971bf215546Sopenharmony_ci
972bf215546Sopenharmony_ci   screen->disk_cache = disk_cache_create("virgl", timestamp, 0);
973bf215546Sopenharmony_ci}
974bf215546Sopenharmony_ci
975bf215546Sopenharmony_cistatic bool
976bf215546Sopenharmony_civirgl_is_dmabuf_modifier_supported(UNUSED struct pipe_screen *pscreen,
977bf215546Sopenharmony_ci                                   UNUSED uint64_t modifier,
978bf215546Sopenharmony_ci                                   UNUSED enum pipe_format format,
979bf215546Sopenharmony_ci                                   UNUSED bool *external_only)
980bf215546Sopenharmony_ci{
981bf215546Sopenharmony_ci   /* Always advertise support until virgl starts checking against host
982bf215546Sopenharmony_ci    * virglrenderer or consuming valid non-linear modifiers here.
983bf215546Sopenharmony_ci    */
984bf215546Sopenharmony_ci   return true;
985bf215546Sopenharmony_ci}
986bf215546Sopenharmony_ci
987bf215546Sopenharmony_cistatic unsigned int
988bf215546Sopenharmony_civirgl_get_dmabuf_modifier_planes(UNUSED struct pipe_screen *pscreen,
989bf215546Sopenharmony_ci                                 UNUSED uint64_t modifier,
990bf215546Sopenharmony_ci                                 enum pipe_format format)
991bf215546Sopenharmony_ci{
992bf215546Sopenharmony_ci   /* Return the format plane count queried from pipe_format. For virgl,
993bf215546Sopenharmony_ci    * additional aux planes are entirely resolved on the host side.
994bf215546Sopenharmony_ci    */
995bf215546Sopenharmony_ci   return util_format_get_num_planes(format);
996bf215546Sopenharmony_ci}
997bf215546Sopenharmony_ci
998bf215546Sopenharmony_cistatic void
999bf215546Sopenharmony_cifixup_renderer(union virgl_caps *caps)
1000bf215546Sopenharmony_ci{
1001bf215546Sopenharmony_ci   if (caps->v2.host_feature_check_version < 5)
1002bf215546Sopenharmony_ci      return;
1003bf215546Sopenharmony_ci
1004bf215546Sopenharmony_ci   char renderer[64];
1005bf215546Sopenharmony_ci   int renderer_len = snprintf(renderer, sizeof(renderer), "virgl (%s)",
1006bf215546Sopenharmony_ci                               caps->v2.renderer);
1007bf215546Sopenharmony_ci   if (renderer_len >= 64) {
1008bf215546Sopenharmony_ci      memcpy(renderer + 59, "...)", 4);
1009bf215546Sopenharmony_ci      renderer_len = 63;
1010bf215546Sopenharmony_ci   }
1011bf215546Sopenharmony_ci   memcpy(caps->v2.renderer, renderer, renderer_len + 1);
1012bf215546Sopenharmony_ci}
1013bf215546Sopenharmony_ci
1014bf215546Sopenharmony_cistatic const void *
1015bf215546Sopenharmony_civirgl_get_compiler_options(struct pipe_screen *pscreen,
1016bf215546Sopenharmony_ci                           enum pipe_shader_ir ir,
1017bf215546Sopenharmony_ci                           unsigned shader)
1018bf215546Sopenharmony_ci{
1019bf215546Sopenharmony_ci   struct virgl_screen *vscreen = virgl_screen(pscreen);
1020bf215546Sopenharmony_ci
1021bf215546Sopenharmony_ci   return &vscreen->compiler_options;
1022bf215546Sopenharmony_ci}
1023bf215546Sopenharmony_ci
1024bf215546Sopenharmony_cistruct pipe_screen *
1025bf215546Sopenharmony_civirgl_create_screen(struct virgl_winsys *vws, const struct pipe_screen_config *config)
1026bf215546Sopenharmony_ci{
1027bf215546Sopenharmony_ci   struct virgl_screen *screen = CALLOC_STRUCT(virgl_screen);
1028bf215546Sopenharmony_ci
1029bf215546Sopenharmony_ci   const char *VIRGL_GLES_EMULATE_BGRA = "gles_emulate_bgra";
1030bf215546Sopenharmony_ci   const char *VIRGL_GLES_APPLY_BGRA_DEST_SWIZZLE = "gles_apply_bgra_dest_swizzle";
1031bf215546Sopenharmony_ci   const char *VIRGL_GLES_SAMPLES_PASSED_VALUE = "gles_samples_passed_value";
1032bf215546Sopenharmony_ci   const char *VIRGL_FORMAT_L8_SRGB_ENABLE_READBACK = "format_l8_srgb_enable_readback";
1033bf215546Sopenharmony_ci
1034bf215546Sopenharmony_ci   if (!screen)
1035bf215546Sopenharmony_ci      return NULL;
1036bf215546Sopenharmony_ci
1037bf215546Sopenharmony_ci   virgl_debug = debug_get_option_virgl_debug();
1038bf215546Sopenharmony_ci
1039bf215546Sopenharmony_ci   if (config && config->options) {
1040bf215546Sopenharmony_ci      driParseConfigFiles(config->options, config->options_info, 0, "virtio_gpu",
1041bf215546Sopenharmony_ci                          NULL, NULL, NULL, 0, NULL, 0);
1042bf215546Sopenharmony_ci
1043bf215546Sopenharmony_ci      screen->tweak_gles_emulate_bgra =
1044bf215546Sopenharmony_ci            driQueryOptionb(config->options, VIRGL_GLES_EMULATE_BGRA);
1045bf215546Sopenharmony_ci      screen->tweak_gles_apply_bgra_dest_swizzle =
1046bf215546Sopenharmony_ci            driQueryOptionb(config->options, VIRGL_GLES_APPLY_BGRA_DEST_SWIZZLE);
1047bf215546Sopenharmony_ci      screen->tweak_gles_tf3_value =
1048bf215546Sopenharmony_ci            driQueryOptioni(config->options, VIRGL_GLES_SAMPLES_PASSED_VALUE);
1049bf215546Sopenharmony_ci      screen->tweak_l8_srgb_readback =
1050bf215546Sopenharmony_ci            driQueryOptionb(config->options, VIRGL_FORMAT_L8_SRGB_ENABLE_READBACK);
1051bf215546Sopenharmony_ci   }
1052bf215546Sopenharmony_ci   screen->tweak_gles_emulate_bgra &= !(virgl_debug & VIRGL_DEBUG_NO_EMULATE_BGRA);
1053bf215546Sopenharmony_ci   screen->tweak_gles_apply_bgra_dest_swizzle &= !(virgl_debug & VIRGL_DEBUG_NO_BGRA_DEST_SWIZZLE);
1054bf215546Sopenharmony_ci   screen->no_coherent = virgl_debug & VIRGL_DEBUG_NO_COHERENT;
1055bf215546Sopenharmony_ci   screen->tweak_l8_srgb_readback |= !!(virgl_debug & VIRGL_DEBUG_L8_SRGB_ENABLE_READBACK);
1056bf215546Sopenharmony_ci
1057bf215546Sopenharmony_ci   screen->vws = vws;
1058bf215546Sopenharmony_ci   screen->base.get_name = virgl_get_name;
1059bf215546Sopenharmony_ci   screen->base.get_vendor = virgl_get_vendor;
1060bf215546Sopenharmony_ci   screen->base.get_param = virgl_get_param;
1061bf215546Sopenharmony_ci   screen->base.get_shader_param = virgl_get_shader_param;
1062bf215546Sopenharmony_ci   screen->base.get_compute_param = virgl_get_compute_param;
1063bf215546Sopenharmony_ci   screen->base.get_paramf = virgl_get_paramf;
1064bf215546Sopenharmony_ci   screen->base.get_compiler_options = virgl_get_compiler_options;
1065bf215546Sopenharmony_ci   screen->base.is_format_supported = virgl_is_format_supported;
1066bf215546Sopenharmony_ci   screen->base.destroy = virgl_destroy_screen;
1067bf215546Sopenharmony_ci   screen->base.context_create = virgl_context_create;
1068bf215546Sopenharmony_ci   screen->base.flush_frontbuffer = virgl_flush_frontbuffer;
1069bf215546Sopenharmony_ci   screen->base.get_timestamp = virgl_get_timestamp;
1070bf215546Sopenharmony_ci   screen->base.fence_reference = virgl_fence_reference;
1071bf215546Sopenharmony_ci   //screen->base.fence_signalled = virgl_fence_signalled;
1072bf215546Sopenharmony_ci   screen->base.fence_finish = virgl_fence_finish;
1073bf215546Sopenharmony_ci   screen->base.fence_get_fd = virgl_fence_get_fd;
1074bf215546Sopenharmony_ci   screen->base.query_memory_info = virgl_query_memory_info;
1075bf215546Sopenharmony_ci   screen->base.get_disk_shader_cache = virgl_get_disk_shader_cache;
1076bf215546Sopenharmony_ci   screen->base.is_dmabuf_modifier_supported = virgl_is_dmabuf_modifier_supported;
1077bf215546Sopenharmony_ci   screen->base.get_dmabuf_modifier_planes = virgl_get_dmabuf_modifier_planes;
1078bf215546Sopenharmony_ci
1079bf215546Sopenharmony_ci   virgl_init_screen_resource_functions(&screen->base);
1080bf215546Sopenharmony_ci
1081bf215546Sopenharmony_ci   vws->get_caps(vws, &screen->caps);
1082bf215546Sopenharmony_ci   fixup_formats(&screen->caps.caps,
1083bf215546Sopenharmony_ci                 &screen->caps.caps.v2.supported_readback_formats);
1084bf215546Sopenharmony_ci   fixup_formats(&screen->caps.caps, &screen->caps.caps.v2.scanout);
1085bf215546Sopenharmony_ci   fixup_renderer(&screen->caps.caps);
1086bf215546Sopenharmony_ci
1087bf215546Sopenharmony_ci   union virgl_caps *caps = &screen->caps.caps;
1088bf215546Sopenharmony_ci   screen->tweak_gles_emulate_bgra &= !virgl_format_check_bitmask(PIPE_FORMAT_B8G8R8A8_SRGB, caps->v1.render.bitmask, false);
1089bf215546Sopenharmony_ci   screen->refcnt = 1;
1090bf215546Sopenharmony_ci
1091bf215546Sopenharmony_ci   /* Set up the NIR shader compiler options now that we've figured out the caps. */
1092bf215546Sopenharmony_ci   screen->compiler_options = *(nir_shader_compiler_options *)
1093bf215546Sopenharmony_ci      nir_to_tgsi_get_compiler_options(&screen->base, PIPE_SHADER_IR_NIR, PIPE_SHADER_FRAGMENT);
1094bf215546Sopenharmony_ci   if (virgl_get_param(&screen->base, PIPE_CAP_DOUBLES)) {
1095bf215546Sopenharmony_ci      /* virglrenderer is missing DFLR support, so avoid turning 64-bit
1096bf215546Sopenharmony_ci       * ffract+fsub back into ffloor.
1097bf215546Sopenharmony_ci       */
1098bf215546Sopenharmony_ci      screen->compiler_options.lower_ffloor = true;
1099bf215546Sopenharmony_ci      screen->compiler_options.lower_fneg = true;
1100bf215546Sopenharmony_ci   }
1101bf215546Sopenharmony_ci
1102bf215546Sopenharmony_ci   slab_create_parent(&screen->transfer_pool, sizeof(struct virgl_transfer), 16);
1103bf215546Sopenharmony_ci
1104bf215546Sopenharmony_ci   virgl_disk_cache_create(screen);
1105bf215546Sopenharmony_ci   return &screen->base;
1106bf215546Sopenharmony_ci}
1107