• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Collabora Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "zink_screen.h"
25 
26 #include "zink_kopper.h"
27 #include "zink_compiler.h"
28 #include "zink_context.h"
29 #include "zink_device_info.h"
30 #include "zink_descriptors.h"
31 #include "zink_fence.h"
32 #include "zink_format.h"
33 #include "zink_framebuffer.h"
34 #include "zink_instance.h"
35 #include "zink_program.h"
36 #include "zink_public.h"
37 #include "zink_query.h"
38 #include "zink_resource.h"
39 #include "nir_to_spirv/nir_to_spirv.h" // for SPIRV_VERSION
40 
41 #include "os/os_process.h"
42 #include "util/u_debug.h"
43 #include "util/u_dl.h"
44 #include "util/format/u_format.h"
45 #include "util/hash_table.h"
46 #include "util/os_file.h"
47 #include "util/u_math.h"
48 #include "util/u_memory.h"
49 #include "util/u_screen.h"
50 #include "util/u_string.h"
51 #include "util/u_transfer_helper.h"
52 #include "util/xmlconfig.h"
53 
54 #include "util/u_cpu_detect.h"
55 
56 #if DETECT_OS_WINDOWS
57 #include <io.h>
58 #define VK_LIBNAME "vulkan-1.dll"
59 #else
60 #include <unistd.h>
61 #if DETECT_OS_APPLE
62 #define VK_LIBNAME "libvulkan.1.dylib"
63 #else
64 #define VK_LIBNAME "libvulkan.so.1"
65 #endif
66 #endif
67 
68 #if defined(__APPLE__)
69 // Source of MVK_VERSION
70 #include "MoltenVK/vk_mvk_moltenvk.h"
71 #endif
72 
73 static const struct debug_named_value
74 zink_debug_options[] = {
75    { "nir", ZINK_DEBUG_NIR, "Dump NIR during program compile" },
76    { "spirv", ZINK_DEBUG_SPIRV, "Dump SPIR-V during program compile" },
77    { "tgsi", ZINK_DEBUG_TGSI, "Dump TGSI during program compile" },
78    { "validation", ZINK_DEBUG_VALIDATION, "Dump Validation layer output" },
79    { "sync", ZINK_DEBUG_SYNC, "Force synchronization before draws/dispatches" },
80    { "compact", ZINK_DEBUG_COMPACT, "Use only 4 descriptor sets" },
81    { "noreorder", ZINK_DEBUG_NOREORDER, "Do not reorder command streams" },
82    DEBUG_NAMED_VALUE_END
83 };
84 
85 DEBUG_GET_ONCE_FLAGS_OPTION(zink_debug, "ZINK_DEBUG", zink_debug_options, 0)
86 
87 uint32_t
88 zink_debug;
89 
90 
91 static const struct debug_named_value
92 zink_descriptor_options[] = {
93    { "auto", ZINK_DESCRIPTOR_MODE_AUTO, "Automatically detect best mode" },
94    { "lazy", ZINK_DESCRIPTOR_MODE_LAZY, "Don't cache, do least amount of updates" },
95    { "cached", ZINK_DESCRIPTOR_MODE_CACHED, "Cache, reuse sets" },
96    { "notemplates", ZINK_DESCRIPTOR_MODE_NOTEMPLATES, "Cache, but disable templated updates" },
97    DEBUG_NAMED_VALUE_END
98 };
99 
100 DEBUG_GET_ONCE_FLAGS_OPTION(zink_descriptor_mode, "ZINK_DESCRIPTORS", zink_descriptor_options, ZINK_DESCRIPTOR_MODE_AUTO)
101 
102 enum zink_descriptor_mode zink_descriptor_mode;
103 
104 static const char *
zink_get_vendor(struct pipe_screen * pscreen)105 zink_get_vendor(struct pipe_screen *pscreen)
106 {
107    return "Collabora Ltd";
108 }
109 
110 static const char *
zink_get_device_vendor(struct pipe_screen * pscreen)111 zink_get_device_vendor(struct pipe_screen *pscreen)
112 {
113    struct zink_screen *screen = zink_screen(pscreen);
114    static char buf[1000];
115    snprintf(buf, sizeof(buf), "Unknown (vendor-id: 0x%04x)", screen->info.props.vendorID);
116    return buf;
117 }
118 
119 static const char *
zink_get_name(struct pipe_screen * pscreen)120 zink_get_name(struct pipe_screen *pscreen)
121 {
122    struct zink_screen *screen = zink_screen(pscreen);
123    static char buf[1000];
124    snprintf(buf, sizeof(buf), "zink (%s)", screen->info.props.deviceName);
125    return buf;
126 }
127 
128 static void
zink_get_driver_uuid(struct pipe_screen * pscreen,char * uuid)129 zink_get_driver_uuid(struct pipe_screen *pscreen, char *uuid)
130 {
131    struct zink_screen *screen = zink_screen(pscreen);
132    if (screen->vk_version >= VK_MAKE_VERSION(1,2,0)) {
133       memcpy(uuid, screen->info.props11.driverUUID, VK_UUID_SIZE);
134    } else {
135       memcpy(uuid, screen->info.deviceid_props.driverUUID, VK_UUID_SIZE);
136    }
137 }
138 
139 static void
zink_get_device_uuid(struct pipe_screen * pscreen,char * uuid)140 zink_get_device_uuid(struct pipe_screen *pscreen, char *uuid)
141 {
142    struct zink_screen *screen = zink_screen(pscreen);
143    if (screen->vk_version >= VK_MAKE_VERSION(1,2,0)) {
144       memcpy(uuid, screen->info.props11.deviceUUID, VK_UUID_SIZE);
145    } else {
146       memcpy(uuid, screen->info.deviceid_props.deviceUUID, VK_UUID_SIZE);
147    }
148 }
149 
150 static void
zink_get_device_luid(struct pipe_screen * pscreen,char * luid)151 zink_get_device_luid(struct pipe_screen *pscreen, char *luid)
152 {
153    struct zink_screen *screen = zink_screen(pscreen);
154    if (screen->info.have_vulkan12) {
155       memcpy(luid, screen->info.props11.deviceLUID, VK_LUID_SIZE);
156    } else {
157       memcpy(luid, screen->info.deviceid_props.deviceLUID, VK_LUID_SIZE);
158    }
159 }
160 
161 static uint32_t
zink_get_device_node_mask(struct pipe_screen * pscreen)162 zink_get_device_node_mask(struct pipe_screen *pscreen)
163 {
164    struct zink_screen *screen = zink_screen(pscreen);
165    if (screen->info.have_vulkan12) {
166       return screen->info.props11.deviceNodeMask;
167    } else {
168       return screen->info.deviceid_props.deviceNodeMask;
169    }
170 }
171 
172 static VkDeviceSize
get_video_mem(struct zink_screen * screen)173 get_video_mem(struct zink_screen *screen)
174 {
175    VkDeviceSize size = 0;
176    for (uint32_t i = 0; i < screen->info.mem_props.memoryHeapCount; ++i) {
177       if (screen->info.mem_props.memoryHeaps[i].flags &
178           VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
179          size += screen->info.mem_props.memoryHeaps[i].size;
180    }
181    return size;
182 }
183 
184 static bool
disk_cache_init(struct zink_screen * screen)185 disk_cache_init(struct zink_screen *screen)
186 {
187 #ifdef ENABLE_SHADER_CACHE
188    static char buf[1000];
189    snprintf(buf, sizeof(buf), "zink_%x04x", screen->info.props.vendorID);
190 
191    screen->disk_cache = disk_cache_create(buf, screen->info.props.deviceName, 0);
192    if (!screen->disk_cache)
193       return true;
194 
195    if (!util_queue_init(&screen->cache_put_thread, "zcq", 8, 1, UTIL_QUEUE_INIT_RESIZE_IF_FULL, screen) ||
196       !util_queue_init(&screen->cache_get_thread, "zcfq", 8, 4,
197          UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SCALE_THREADS, screen)) {
198       mesa_loge("zink: Failed to create disk cache queue\n");
199 
200       disk_cache_destroy(screen->disk_cache);
201       screen->disk_cache = NULL;
202 
203       util_queue_destroy(&screen->cache_put_thread);
204       util_queue_destroy(&screen->cache_get_thread);
205 
206       return false;
207    }
208 #endif
209 
210    return true;
211 }
212 
213 
214 static void
cache_put_job(void * data,void * gdata,int thread_index)215 cache_put_job(void *data, void *gdata, int thread_index)
216 {
217    struct zink_program *pg = data;
218    struct zink_screen *screen = gdata;
219    size_t size = 0;
220    VkResult result = VKSCR(GetPipelineCacheData)(screen->dev, pg->pipeline_cache, &size, NULL);
221    if (result != VK_SUCCESS) {
222       mesa_loge("ZINK: vkGetPipelineCacheData failed (%s)", vk_Result_to_str(result));
223       return;
224    }
225    if (pg->pipeline_cache_size == size)
226       return;
227    void *pipeline_data = malloc(size);
228    if (!pipeline_data)
229       return;
230    result = VKSCR(GetPipelineCacheData)(screen->dev, pg->pipeline_cache, &size, pipeline_data);
231    if (result == VK_SUCCESS) {
232       pg->pipeline_cache_size = size;
233 
234       cache_key key;
235       disk_cache_compute_key(screen->disk_cache, pg->sha1, sizeof(pg->sha1), key);
236       disk_cache_put_nocopy(screen->disk_cache, key, pipeline_data, size, NULL);
237    } else {
238       mesa_loge("ZINK: vkGetPipelineCacheData failed (%s)", vk_Result_to_str(result));
239    }
240 }
241 
242 void
zink_screen_update_pipeline_cache(struct zink_screen * screen,struct zink_program * pg)243 zink_screen_update_pipeline_cache(struct zink_screen *screen, struct zink_program *pg)
244 {
245    if (!screen->disk_cache)
246       return;
247 
248    util_queue_add_job(&screen->cache_put_thread, pg, &pg->cache_fence, cache_put_job, NULL, 0);
249 }
250 
251 static void
cache_get_job(void * data,void * gdata,int thread_index)252 cache_get_job(void *data, void *gdata, int thread_index)
253 {
254    struct zink_program *pg = data;
255    struct zink_screen *screen = gdata;
256 
257    VkPipelineCacheCreateInfo pcci;
258    pcci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
259    pcci.pNext = NULL;
260    pcci.flags = screen->info.have_EXT_pipeline_creation_cache_control ? VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT : 0;
261    pcci.initialDataSize = 0;
262    pcci.pInitialData = NULL;
263 
264    cache_key key;
265    disk_cache_compute_key(screen->disk_cache, pg->sha1, sizeof(pg->sha1), key);
266    pcci.pInitialData = disk_cache_get(screen->disk_cache, key, &pg->pipeline_cache_size);
267    pcci.initialDataSize = pg->pipeline_cache_size;
268 
269    VkResult res = VKSCR(CreatePipelineCache)(screen->dev, &pcci, NULL, &pg->pipeline_cache);
270    if (res != VK_SUCCESS) {
271       mesa_loge("ZINK: vkCreatePipelineCache failed (%s)", vk_Result_to_str(res));
272    }
273    free((void*)pcci.pInitialData);
274 }
275 
276 void
zink_screen_get_pipeline_cache(struct zink_screen * screen,struct zink_program * pg)277 zink_screen_get_pipeline_cache(struct zink_screen *screen, struct zink_program *pg)
278 {
279    if (!screen->disk_cache)
280       return;
281 
282    util_queue_add_job(&screen->cache_get_thread, pg, &pg->cache_fence, cache_get_job, NULL, 0);
283 }
284 
285 static int
zink_get_compute_param(struct pipe_screen * pscreen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)286 zink_get_compute_param(struct pipe_screen *pscreen, enum pipe_shader_ir ir_type,
287                        enum pipe_compute_cap param, void *ret)
288 {
289    struct zink_screen *screen = zink_screen(pscreen);
290 #define RET(x) do {                  \
291    if (ret)                          \
292       memcpy(ret, x, sizeof(x));     \
293    return sizeof(x);                 \
294 } while (0)
295 
296    switch (param) {
297    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
298       RET((uint32_t []){ 32 });
299 
300    case PIPE_COMPUTE_CAP_IR_TARGET:
301       if (ret)
302          strcpy(ret, "nir");
303       return 4;
304 
305    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
306       RET((uint64_t []) { 3 });
307 
308    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
309       RET(((uint64_t []) { screen->info.props.limits.maxComputeWorkGroupCount[0],
310                            screen->info.props.limits.maxComputeWorkGroupCount[1],
311                            screen->info.props.limits.maxComputeWorkGroupCount[2] }));
312 
313    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
314       /* MaxComputeWorkGroupSize[0..2] */
315       RET(((uint64_t []) {screen->info.props.limits.maxComputeWorkGroupSize[0],
316                           screen->info.props.limits.maxComputeWorkGroupSize[1],
317                           screen->info.props.limits.maxComputeWorkGroupSize[2]}));
318 
319    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
320    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
321       RET((uint64_t []) { screen->info.props.limits.maxComputeWorkGroupInvocations });
322 
323    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
324       RET((uint64_t []) { screen->info.props.limits.maxComputeSharedMemorySize });
325 
326    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
327       RET((uint32_t []) { 1 });
328 
329    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
330       RET((uint32_t []) { screen->info.props11.subgroupSize });
331 
332    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
333    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
334    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
335    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
336    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
337    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
338       // XXX: I think these are for Clover...
339       return 0;
340 
341    default:
342       unreachable("unknown compute param");
343    }
344 }
345 
346 static uint32_t
get_smallest_buffer_heap(struct zink_screen * screen)347 get_smallest_buffer_heap(struct zink_screen *screen)
348 {
349    enum zink_heap heaps[] = {
350       ZINK_HEAP_DEVICE_LOCAL,
351       ZINK_HEAP_DEVICE_LOCAL_VISIBLE,
352       ZINK_HEAP_HOST_VISIBLE_COHERENT,
353       ZINK_HEAP_HOST_VISIBLE_COHERENT
354    };
355    unsigned size = UINT32_MAX;
356    for (unsigned i = 0; i < ARRAY_SIZE(heaps); i++) {
357       unsigned heap_idx = screen->info.mem_props.memoryTypes[screen->heap_map[i]].heapIndex;
358       size = MIN2(screen->info.mem_props.memoryHeaps[heap_idx].size, size);
359    }
360    return size;
361 }
362 
363 static int
zink_get_param(struct pipe_screen * pscreen,enum pipe_cap param)364 zink_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
365 {
366    struct zink_screen *screen = zink_screen(pscreen);
367 
368    switch (param) {
369    case PIPE_CAP_TEXRECT:
370    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARTIAL_STRIDE:
371       return 0;
372    case PIPE_CAP_ANISOTROPIC_FILTER:
373       return screen->info.feats.features.samplerAnisotropy;
374    case PIPE_CAP_EMULATE_NONFIXED_PRIMITIVE_RESTART:
375       return 1;
376    case PIPE_CAP_SUPPORTED_PRIM_MODES_WITH_RESTART: {
377       uint32_t modes = BITFIELD_BIT(PIPE_PRIM_LINE_STRIP) |
378                        BITFIELD_BIT(PIPE_PRIM_TRIANGLE_STRIP) |
379                        BITFIELD_BIT(PIPE_PRIM_LINE_STRIP_ADJACENCY) |
380                        BITFIELD_BIT(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
381       if (screen->have_triangle_fans)
382          modes |= BITFIELD_BIT(PIPE_PRIM_TRIANGLE_FAN);
383       if (screen->info.have_EXT_primitive_topology_list_restart) {
384          modes |= BITFIELD_BIT(PIPE_PRIM_POINTS) |
385                   BITFIELD_BIT(PIPE_PRIM_LINES) |
386                   BITFIELD_BIT(PIPE_PRIM_LINES_ADJACENCY) |
387                   BITFIELD_BIT(PIPE_PRIM_TRIANGLES) |
388                   BITFIELD_BIT(PIPE_PRIM_TRIANGLES_ADJACENCY);
389          if (screen->info.list_restart_feats.primitiveTopologyPatchListRestart)
390             modes |= BITFIELD_BIT(PIPE_PRIM_PATCHES);
391       }
392       return modes;
393    }
394    case PIPE_CAP_SUPPORTED_PRIM_MODES: {
395       uint32_t modes = BITFIELD_MASK(PIPE_PRIM_MAX);
396       modes &= ~BITFIELD_BIT(PIPE_PRIM_QUADS);
397       modes &= ~BITFIELD_BIT(PIPE_PRIM_QUAD_STRIP);
398       modes &= ~BITFIELD_BIT(PIPE_PRIM_POLYGON);
399       modes &= ~BITFIELD_BIT(PIPE_PRIM_LINE_LOOP);
400       if (!screen->have_triangle_fans)
401          modes &= ~BITFIELD_BIT(PIPE_PRIM_TRIANGLE_FAN);
402       return modes;
403    }
404 
405    case PIPE_CAP_FBFETCH:
406       return 1;
407 
408    case PIPE_CAP_MEMOBJ:
409       return screen->instance_info.have_KHR_external_memory_capabilities && (screen->info.have_KHR_external_memory_fd || screen->info.have_KHR_external_memory_win32);
410    case PIPE_CAP_FENCE_SIGNAL:
411       return screen->info.have_KHR_external_semaphore_fd || screen->info.have_KHR_external_semaphore_win32;
412 
413    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
414    case PIPE_CAP_QUERY_MEMORY_INFO:
415    case PIPE_CAP_NPOT_TEXTURES:
416    case PIPE_CAP_TGSI_TEXCOORD:
417    case PIPE_CAP_DRAW_INDIRECT:
418    case PIPE_CAP_TEXTURE_QUERY_LOD:
419    case PIPE_CAP_GLSL_TESS_LEVELS_AS_INPUTS:
420    case PIPE_CAP_CLEAR_TEXTURE:
421    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
422    case PIPE_CAP_FORCE_PERSAMPLE_INTERP:
423    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
424    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
425    case PIPE_CAP_SHADER_ARRAY_COMPONENTS:
426    case PIPE_CAP_QUERY_BUFFER_OBJECT:
427    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
428    case PIPE_CAP_CLIP_HALFZ:
429    case PIPE_CAP_TEXTURE_QUERY_SAMPLES:
430    case PIPE_CAP_TEXTURE_BARRIER:
431    case PIPE_CAP_QUERY_SO_OVERFLOW:
432    case PIPE_CAP_GL_SPIRV:
433    case PIPE_CAP_CLEAR_SCISSORED:
434    case PIPE_CAP_INVALIDATE_BUFFER:
435    case PIPE_CAP_PREFER_REAL_BUFFER_IN_CONSTBUF0:
436    case PIPE_CAP_PACKED_UNIFORMS:
437    case PIPE_CAP_SHADER_PACK_HALF_FLOAT:
438    case PIPE_CAP_CULL_DISTANCE_NOCOMBINE:
439    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
440    case PIPE_CAP_LOAD_CONSTBUF:
441       return 1;
442 
443    case PIPE_CAP_DRAW_VERTEX_STATE:
444       return screen->info.have_EXT_vertex_input_dynamic_state;
445 
446    case PIPE_CAP_SURFACE_SAMPLE_COUNT:
447       return screen->vk_version >= VK_MAKE_VERSION(1,2,0);
448 
449    case PIPE_CAP_DRAW_PARAMETERS:
450       return screen->info.feats11.shaderDrawParameters || screen->info.have_KHR_shader_draw_parameters;
451 
452    case PIPE_CAP_SHADER_GROUP_VOTE:
453       if (screen->info.have_vulkan11 &&
454              (screen->info.subgroup.supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT) &&
455           (screen->info.subgroup.supportedStages & VK_SHADER_STAGE_COMPUTE_BIT))
456          return true;
457       if (screen->info.have_EXT_shader_subgroup_vote)
458          return true;
459       return false;
460    case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
461       return screen->info.have_EXT_provoking_vertex;
462 
463    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
464       return screen->info.have_KHR_sampler_mirror_clamp_to_edge;
465 
466    case PIPE_CAP_POLYGON_OFFSET_UNITS_UNSCALED:
467       return 1;
468 
469    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
470       return screen->info.feats.features.depthBiasClamp;
471 
472    case PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE:
473       return screen->info.feats.features.pipelineStatisticsQuery;
474 
475    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
476       return screen->info.feats.features.robustBufferAccess;
477 
478    case PIPE_CAP_MULTI_DRAW_INDIRECT:
479       return screen->info.feats.features.multiDrawIndirect;
480 
481    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
482       return screen->info.have_KHR_draw_indirect_count;
483 
484    case PIPE_CAP_START_INSTANCE:
485       return (screen->info.have_vulkan12 && screen->info.feats11.shaderDrawParameters) ||
486               screen->info.have_KHR_shader_draw_parameters;
487 
488    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
489       return screen->info.have_EXT_vertex_attribute_divisor;
490 
491    case PIPE_CAP_MAX_VERTEX_STREAMS:
492       return screen->info.tf_props.maxTransformFeedbackStreams;
493 
494    case PIPE_CAP_INT64:
495    case PIPE_CAP_INT64_DIVMOD:
496    case PIPE_CAP_DOUBLES:
497       return 1;
498 
499    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
500       if (!screen->info.feats.features.dualSrcBlend)
501          return 0;
502       return screen->info.props.limits.maxFragmentDualSrcAttachments;
503 
504    case PIPE_CAP_MAX_RENDER_TARGETS:
505       return screen->info.props.limits.maxColorAttachments;
506 
507    case PIPE_CAP_OCCLUSION_QUERY:
508       return screen->info.feats.features.occlusionQueryPrecise;
509 
510    case PIPE_CAP_PROGRAMMABLE_SAMPLE_LOCATIONS:
511       return screen->info.have_EXT_sample_locations && screen->info.have_EXT_extended_dynamic_state;
512 
513    case PIPE_CAP_QUERY_TIME_ELAPSED:
514       return screen->timestamp_valid_bits > 0;
515 
516    case PIPE_CAP_TEXTURE_MULTISAMPLE:
517       return 1;
518 
519    case PIPE_CAP_FRAGMENT_SHADER_INTERLOCK:
520       return screen->info.have_EXT_fragment_shader_interlock;
521 
522    case PIPE_CAP_SHADER_CLOCK:
523       return screen->info.have_KHR_shader_clock;
524 
525    case PIPE_CAP_POINT_SPRITE:
526       return 1;
527 
528    case PIPE_CAP_SHADER_BALLOT:
529       if (screen->info.props11.subgroupSize > 64)
530          return false;
531       if (screen->info.have_vulkan11 &&
532           screen->info.subgroup.supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT)
533          return true;
534       if (screen->info.have_EXT_shader_subgroup_ballot)
535          return true;
536       return false;
537    case PIPE_CAP_SAMPLE_SHADING:
538       return screen->info.feats.features.sampleRateShading;
539 
540    case PIPE_CAP_TEXTURE_SWIZZLE:
541       return 1;
542 
543    case PIPE_CAP_VERTEX_ATTRIB_ELEMENT_ALIGNED_ONLY:
544       return 1;
545 
546    case PIPE_CAP_GL_CLAMP:
547       return 0;
548 
549    case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
550       if (!screen->info.border_color_feats.customBorderColorWithoutFormat)
551          return PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_FREEDRENO;
552       /* assume that if drivers don't implement this extension they either:
553        * - don't support custom border colors
554        * - handle things correctly
555        * - hate border color accuracy
556        */
557       if (screen->info.have_EXT_border_color_swizzle &&
558           !screen->info.border_swizzle_feats.borderColorSwizzleFromImage)
559          return PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50;
560       return 0;
561 
562    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
563       return screen->info.props.limits.maxImageDimension2D;
564    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
565       return 1 + util_logbase2(screen->info.props.limits.maxImageDimension3D);
566    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
567       return 1 + util_logbase2(screen->info.props.limits.maxImageDimensionCube);
568 
569    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
570    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
571       return 1;
572 
573    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
574    case PIPE_CAP_INDEP_BLEND_ENABLE:
575    case PIPE_CAP_INDEP_BLEND_FUNC:
576       return screen->info.feats.features.independentBlend;
577 
578    case PIPE_CAP_DITHERING:
579       return 0;
580 
581    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
582       return screen->info.have_EXT_transform_feedback ? screen->info.tf_props.maxTransformFeedbackBuffers : 0;
583    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
584    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
585       return screen->info.have_EXT_transform_feedback;
586 
587    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
588       return screen->info.props.limits.maxImageArrayLayers;
589 
590    case PIPE_CAP_DEPTH_CLIP_DISABLE:
591       return !screen->driver_workarounds.depth_clip_control_missing;
592 
593    case PIPE_CAP_SHADER_STENCIL_EXPORT:
594       return screen->info.have_EXT_shader_stencil_export;
595 
596    case PIPE_CAP_VS_INSTANCEID:
597    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
598    case PIPE_CAP_SEAMLESS_CUBE_MAP:
599       return 1;
600 
601    case PIPE_CAP_MIN_TEXEL_OFFSET:
602       return screen->info.props.limits.minTexelOffset;
603    case PIPE_CAP_MAX_TEXEL_OFFSET:
604       return screen->info.props.limits.maxTexelOffset;
605 
606    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
607       return 1;
608 
609    case PIPE_CAP_CONDITIONAL_RENDER:
610      return 1;
611 
612    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
613    case PIPE_CAP_GLSL_FEATURE_LEVEL:
614       return 460;
615 
616    case PIPE_CAP_COMPUTE:
617       return 1;
618 
619    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
620       return screen->info.props.limits.minUniformBufferOffsetAlignment;
621 
622    case PIPE_CAP_QUERY_TIMESTAMP:
623       return screen->timestamp_valid_bits > 0;
624 
625    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
626       return 1 << MIN_SLAB_ORDER;
627 
628    case PIPE_CAP_CUBE_MAP_ARRAY:
629       return screen->info.feats.features.imageCubeArray;
630 
631    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
632    case PIPE_CAP_PRIMITIVE_RESTART:
633       return 1;
634 
635    case PIPE_CAP_BINDLESS_TEXTURE:
636       return screen->info.have_EXT_descriptor_indexing;
637 
638    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
639       return screen->info.props.limits.minTexelBufferOffsetAlignment;
640 
641    case PIPE_CAP_TEXTURE_TRANSFER_MODES: {
642       enum pipe_texture_transfer_mode mode = PIPE_TEXTURE_TRANSFER_BLIT;
643       if (!screen->is_cpu &&
644           /* this needs substantial perf tuning */
645           screen->info.driver_props.driverID != VK_DRIVER_ID_MESA_TURNIP &&
646           screen->info.have_KHR_8bit_storage &&
647           screen->info.have_KHR_16bit_storage &&
648           screen->info.have_KHR_shader_float16_int8)
649          mode |= PIPE_TEXTURE_TRANSFER_COMPUTE;
650       return mode;
651    }
652 
653    case PIPE_CAP_MAX_TEXEL_BUFFER_ELEMENTS_UINT:
654       return MIN2(get_smallest_buffer_heap(screen),
655                   screen->info.props.limits.maxTexelBufferElements);
656 
657    case PIPE_CAP_ENDIANNESS:
658       return PIPE_ENDIAN_NATIVE; /* unsure */
659 
660    case PIPE_CAP_MAX_VIEWPORTS:
661       return MIN2(screen->info.props.limits.maxViewports, PIPE_MAX_VIEWPORTS);
662 
663    case PIPE_CAP_IMAGE_LOAD_FORMATTED:
664       return screen->info.feats.features.shaderStorageImageReadWithoutFormat;
665 
666    case PIPE_CAP_IMAGE_STORE_FORMATTED:
667       return screen->info.feats.features.shaderStorageImageWriteWithoutFormat;
668 
669    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
670       return 1;
671 
672    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
673       return screen->info.props.limits.maxGeometryOutputVertices;
674    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
675       return screen->info.props.limits.maxGeometryTotalOutputComponents;
676 
677    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
678       return 4;
679 
680    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
681       return screen->info.props.limits.minTexelGatherOffset;
682    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
683       return screen->info.props.limits.maxTexelGatherOffset;
684 
685    case PIPE_CAP_SAMPLER_REDUCTION_MINMAX_ARB:
686       return screen->info.feats12.samplerFilterMinmax || screen->info.have_EXT_sampler_filter_minmax;
687 
688    case PIPE_CAP_FS_FINE_DERIVATIVE:
689       return 1;
690 
691    case PIPE_CAP_VENDOR_ID:
692       return screen->info.props.vendorID;
693    case PIPE_CAP_DEVICE_ID:
694       return screen->info.props.deviceID;
695 
696    case PIPE_CAP_ACCELERATED:
697       return !screen->is_cpu;
698    case PIPE_CAP_VIDEO_MEMORY:
699       return get_video_mem(screen) >> 20;
700    case PIPE_CAP_UMA:
701       return screen->info.props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
702 
703    case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
704       return screen->info.props.limits.maxVertexInputBindingStride;
705 
706    case PIPE_CAP_SAMPLER_VIEW_TARGET:
707       return 1;
708 
709    case PIPE_CAP_VS_LAYER_VIEWPORT:
710    case PIPE_CAP_TES_LAYER_VIEWPORT:
711       return screen->info.have_EXT_shader_viewport_index_layer ||
712              (screen->spirv_version >= SPIRV_VERSION(1, 5) &&
713               screen->info.feats12.shaderOutputLayer &&
714               screen->info.feats12.shaderOutputViewportIndex);
715 
716    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
717    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
718       return 1;
719 
720    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
721       return screen->info.props.limits.minStorageBufferOffsetAlignment;
722 
723    case PIPE_CAP_PCI_GROUP:
724    case PIPE_CAP_PCI_BUS:
725    case PIPE_CAP_PCI_DEVICE:
726    case PIPE_CAP_PCI_FUNCTION:
727       return 0; /* TODO: figure these out */
728 
729    case PIPE_CAP_CULL_DISTANCE:
730       return screen->info.feats.features.shaderCullDistance;
731 
732    case PIPE_CAP_SPARSE_BUFFER_PAGE_SIZE:
733       return screen->info.feats.features.sparseBinding ? ZINK_SPARSE_BUFFER_PAGE_SIZE : 0;
734 
735    /* Sparse texture */
736    case PIPE_CAP_MAX_SPARSE_TEXTURE_SIZE:
737       return screen->info.feats.features.sparseResidencyImage2D ?
738          zink_get_param(pscreen, PIPE_CAP_MAX_TEXTURE_2D_SIZE) : 0;
739    case PIPE_CAP_MAX_SPARSE_3D_TEXTURE_SIZE:
740       return screen->info.feats.features.sparseResidencyImage3D ?
741          (1 << (zink_get_param(pscreen, PIPE_CAP_MAX_TEXTURE_3D_LEVELS) - 1)) : 0;
742    case PIPE_CAP_MAX_SPARSE_ARRAY_TEXTURE_LAYERS:
743       return screen->info.feats.features.sparseResidencyImage2D ?
744          zink_get_param(pscreen, PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) : 0;
745    case PIPE_CAP_SPARSE_TEXTURE_FULL_ARRAY_CUBE_MIPMAPS:
746       return screen->info.feats.features.sparseResidencyImage2D ? 1 : 0;
747    case PIPE_CAP_QUERY_SPARSE_TEXTURE_RESIDENCY:
748    case PIPE_CAP_CLAMP_SPARSE_TEXTURE_LOD:
749       return screen->info.feats.features.sparseResidency2Samples ? 1 : 0;
750 
751    case PIPE_CAP_VIEWPORT_SUBPIXEL_BITS:
752       return screen->info.props.limits.viewportSubPixelBits;
753 
754    case PIPE_CAP_MAX_GS_INVOCATIONS:
755       return screen->info.props.limits.maxGeometryShaderInvocations;
756 
757    case PIPE_CAP_MAX_COMBINED_SHADER_BUFFERS:
758       /* gallium handles this automatically */
759       return 0;
760 
761    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE_UINT:
762       /* 1<<27 is required by VK spec */
763       assert(screen->info.props.limits.maxStorageBufferRange >= 1 << 27);
764       /* clamp to VK spec minimum */
765       return MIN2(get_smallest_buffer_heap(screen), screen->info.props.limits.maxStorageBufferRange);
766 
767    case PIPE_CAP_FS_COORD_ORIGIN_UPPER_LEFT:
768    case PIPE_CAP_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
769       return 1;
770 
771    case PIPE_CAP_FS_COORD_ORIGIN_LOWER_LEFT:
772    case PIPE_CAP_FS_COORD_PIXEL_CENTER_INTEGER:
773       return 0;
774 
775    case PIPE_CAP_NIR_COMPACT_ARRAYS:
776       return 1;
777 
778    case PIPE_CAP_FS_FACE_IS_INTEGER_SYSVAL:
779       return 1;
780 
781    case PIPE_CAP_VIEWPORT_TRANSFORM_LOWERED:
782       return 1;
783 
784    case PIPE_CAP_FLATSHADE:
785    case PIPE_CAP_ALPHA_TEST:
786    case PIPE_CAP_CLIP_PLANES:
787    case PIPE_CAP_POINT_SIZE_FIXED:
788    case PIPE_CAP_TWO_SIDED_COLOR:
789       return 0;
790 
791    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
792       return screen->info.props.limits.maxTessellationControlPerVertexOutputComponents / 4;
793    case PIPE_CAP_MAX_VARYINGS:
794       /* need to reserve up to 60 of our varying components and 16 slots for streamout */
795       return MIN2(screen->info.props.limits.maxVertexOutputComponents / 4 / 2, 16);
796 
797    case PIPE_CAP_DMABUF:
798       return screen->info.have_KHR_external_memory_fd &&
799              screen->info.have_EXT_external_memory_dma_buf &&
800              screen->info.have_EXT_queue_family_foreign;
801 
802    case PIPE_CAP_DEPTH_BOUNDS_TEST:
803       return screen->info.feats.features.depthBounds;
804 
805    case PIPE_CAP_POST_DEPTH_COVERAGE:
806       return screen->info.have_EXT_post_depth_coverage;
807 
808    case PIPE_CAP_STRING_MARKER:
809       return screen->instance_info.have_EXT_debug_utils;
810 
811    default:
812       return u_pipe_screen_get_param_defaults(pscreen, param);
813    }
814 }
815 
816 static float
zink_get_paramf(struct pipe_screen * pscreen,enum pipe_capf param)817 zink_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
818 {
819    struct zink_screen *screen = zink_screen(pscreen);
820 
821    switch (param) {
822    case PIPE_CAPF_MIN_LINE_WIDTH:
823    case PIPE_CAPF_MIN_LINE_WIDTH_AA:
824       if (!screen->info.feats.features.wideLines)
825          return 1.0f;
826       return MAX2(screen->info.props.limits.lineWidthRange[0], 0.01);
827 
828    case PIPE_CAPF_MIN_POINT_SIZE:
829    case PIPE_CAPF_MIN_POINT_SIZE_AA:
830       if (!screen->info.feats.features.largePoints)
831          return 1.0f;
832       return MAX2(screen->info.props.limits.pointSizeRange[0], 0.01);
833 
834 
835    case PIPE_CAPF_LINE_WIDTH_GRANULARITY:
836       if (!screen->info.feats.features.wideLines)
837          return 0.1f;
838       return screen->info.props.limits.lineWidthGranularity;
839 
840    case PIPE_CAPF_POINT_SIZE_GRANULARITY:
841       if (!screen->info.feats.features.largePoints)
842          return 0.1f;
843       return screen->info.props.limits.pointSizeGranularity;
844 
845 
846    case PIPE_CAPF_MAX_LINE_WIDTH:
847    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
848       if (!screen->info.feats.features.wideLines)
849          return 1.0f;
850       return screen->info.props.limits.lineWidthRange[1];
851 
852    case PIPE_CAPF_MAX_POINT_SIZE:
853    case PIPE_CAPF_MAX_POINT_SIZE_AA:
854       if (!screen->info.feats.features.largePoints)
855          return 1.0f;
856       return screen->info.props.limits.pointSizeRange[1];
857 
858    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
859       if (!screen->info.feats.features.samplerAnisotropy)
860          return 1.0f;
861       return screen->info.props.limits.maxSamplerAnisotropy;
862 
863    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
864       return screen->info.props.limits.maxSamplerLodBias;
865 
866    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
867    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
868    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
869       return 0.0f; /* not implemented */
870    }
871 
872    /* should only get here on unhandled cases */
873    return 0.0f;
874 }
875 
876 static int
zink_get_shader_param(struct pipe_screen * pscreen,enum pipe_shader_type shader,enum pipe_shader_cap param)877 zink_get_shader_param(struct pipe_screen *pscreen,
878                        enum pipe_shader_type shader,
879                        enum pipe_shader_cap param)
880 {
881    struct zink_screen *screen = zink_screen(pscreen);
882 
883    switch (param) {
884    case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
885       switch (shader) {
886       case PIPE_SHADER_FRAGMENT:
887       case PIPE_SHADER_VERTEX:
888          return INT_MAX;
889       case PIPE_SHADER_TESS_CTRL:
890       case PIPE_SHADER_TESS_EVAL:
891          if (screen->info.feats.features.tessellationShader &&
892              screen->info.have_KHR_maintenance2)
893             return INT_MAX;
894          break;
895 
896       case PIPE_SHADER_GEOMETRY:
897          if (screen->info.feats.features.geometryShader)
898             return INT_MAX;
899          break;
900 
901       case PIPE_SHADER_COMPUTE:
902          return INT_MAX;
903       default:
904          break;
905       }
906       return 0;
907    case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
908    case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
909    case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
910    case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
911       return INT_MAX;
912 
913    case PIPE_SHADER_CAP_MAX_INPUTS: {
914       uint32_t max = 0;
915       switch (shader) {
916       case PIPE_SHADER_VERTEX:
917          max = MIN2(screen->info.props.limits.maxVertexInputAttributes, PIPE_MAX_ATTRIBS);
918          break;
919       case PIPE_SHADER_TESS_CTRL:
920          max = screen->info.props.limits.maxTessellationControlPerVertexInputComponents / 4;
921          break;
922       case PIPE_SHADER_TESS_EVAL:
923          max = screen->info.props.limits.maxTessellationEvaluationInputComponents / 4;
924          break;
925       case PIPE_SHADER_GEOMETRY:
926          max = screen->info.props.limits.maxGeometryInputComponents / 4;
927          break;
928       case PIPE_SHADER_FRAGMENT:
929          /* intel drivers report fewer components, but it's a value that's compatible
930           * with what we need for GL, so we can still force a conformant value here
931           */
932          if (screen->info.driver_props.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA ||
933              screen->info.driver_props.driverID == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS)
934             return 32;
935          max = screen->info.props.limits.maxFragmentInputComponents / 4;
936          break;
937       default:
938          return 0; /* unsupported stage */
939       }
940       switch (shader) {
941       case PIPE_SHADER_VERTEX:
942       case PIPE_SHADER_TESS_EVAL:
943       case PIPE_SHADER_GEOMETRY:
944          /* last vertex stage must support streamout, and this is capped in glsl compiler */
945          return MIN2(max, MAX_VARYING);
946       default: break;
947       }
948       return MIN2(max, 64); // prevent overflowing struct shader_info::inputs_read
949    }
950 
951    case PIPE_SHADER_CAP_MAX_OUTPUTS: {
952       uint32_t max = 0;
953       switch (shader) {
954       case PIPE_SHADER_VERTEX:
955          max = screen->info.props.limits.maxVertexOutputComponents / 4;
956          break;
957       case PIPE_SHADER_TESS_CTRL:
958          max = screen->info.props.limits.maxTessellationControlPerVertexOutputComponents / 4;
959          break;
960       case PIPE_SHADER_TESS_EVAL:
961          max = screen->info.props.limits.maxTessellationEvaluationOutputComponents / 4;
962          break;
963       case PIPE_SHADER_GEOMETRY:
964          max = screen->info.props.limits.maxGeometryOutputComponents / 4;
965          break;
966       case PIPE_SHADER_FRAGMENT:
967          max = screen->info.props.limits.maxColorAttachments;
968          break;
969       default:
970          return 0; /* unsupported stage */
971       }
972       return MIN2(max, 64); // prevent overflowing struct shader_info::outputs_read/written
973    }
974 
975    case PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE:
976       /* At least 16384 is guaranteed by VK spec */
977       assert(screen->info.props.limits.maxUniformBufferRange >= 16384);
978       /* but Gallium can't handle values that are too big */
979       return MIN3(get_smallest_buffer_heap(screen),
980                   screen->info.props.limits.maxUniformBufferRange, BITFIELD_BIT(31));
981 
982    case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
983       return  MIN2(screen->info.props.limits.maxPerStageDescriptorUniformBuffers,
984                    PIPE_MAX_CONSTANT_BUFFERS);
985 
986    case PIPE_SHADER_CAP_MAX_TEMPS:
987       return INT_MAX;
988 
989    case PIPE_SHADER_CAP_INTEGERS:
990       return 1;
991 
992    case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
993    case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
994    case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
995    case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
996       return 1;
997 
998    case PIPE_SHADER_CAP_SUBROUTINES:
999    case PIPE_SHADER_CAP_INT64_ATOMICS:
1000    case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
1001       return 0; /* not implemented */
1002 
1003    case PIPE_SHADER_CAP_FP16_CONST_BUFFERS:
1004       //enabling this breaks GTF-GL46.gtf21.GL2Tests.glGetUniform.glGetUniform
1005       //return screen->info.feats11.uniformAndStorageBuffer16BitAccess ||
1006              //(screen->info.have_KHR_16bit_storage && screen->info.storage_16bit_feats.uniformAndStorageBuffer16BitAccess);
1007       return 0;
1008    case PIPE_SHADER_CAP_FP16_DERIVATIVES:
1009       return 0; //spirv requires 32bit derivative srcs and dests
1010    case PIPE_SHADER_CAP_FP16:
1011       return screen->info.feats12.shaderFloat16 ||
1012              (screen->info.have_KHR_shader_float16_int8 &&
1013               screen->info.shader_float16_int8_feats.shaderFloat16);
1014 
1015    case PIPE_SHADER_CAP_INT16:
1016       return screen->info.feats.features.shaderInt16;
1017 
1018    case PIPE_SHADER_CAP_PREFERRED_IR:
1019       return PIPE_SHADER_IR_NIR;
1020 
1021    case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
1022       return 0; /* not implemented */
1023 
1024    case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
1025    case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
1026       return MIN2(MIN2(screen->info.props.limits.maxPerStageDescriptorSamplers,
1027                        screen->info.props.limits.maxPerStageDescriptorSampledImages),
1028                   PIPE_MAX_SAMPLERS);
1029 
1030    case PIPE_SHADER_CAP_DROUND_SUPPORTED:
1031    case PIPE_SHADER_CAP_DFRACEXP_DLDEXP_SUPPORTED:
1032       return 0; /* not implemented */
1033 
1034    case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
1035       return 0; /* no idea */
1036 
1037    case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
1038       switch (shader) {
1039       case PIPE_SHADER_VERTEX:
1040       case PIPE_SHADER_TESS_CTRL:
1041       case PIPE_SHADER_TESS_EVAL:
1042       case PIPE_SHADER_GEOMETRY:
1043          if (!screen->info.feats.features.vertexPipelineStoresAndAtomics)
1044             return 0;
1045          break;
1046 
1047       case PIPE_SHADER_FRAGMENT:
1048          if (!screen->info.feats.features.fragmentStoresAndAtomics)
1049             return 0;
1050          break;
1051 
1052       default:
1053          break;
1054       }
1055 
1056       /* TODO: this limitation is dumb, and will need some fixes in mesa */
1057       return MIN2(screen->info.props.limits.maxPerStageDescriptorStorageBuffers, PIPE_MAX_SHADER_BUFFERS);
1058 
1059    case PIPE_SHADER_CAP_SUPPORTED_IRS:
1060       return (1 << PIPE_SHADER_IR_NIR) | (1 << PIPE_SHADER_IR_TGSI);
1061 
1062    case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
1063       if (screen->info.feats.features.shaderStorageImageExtendedFormats &&
1064           screen->info.feats.features.shaderStorageImageWriteWithoutFormat)
1065          return MIN2(screen->info.props.limits.maxPerStageDescriptorStorageImages,
1066                      ZINK_MAX_SHADER_IMAGES);
1067       return 0;
1068 
1069    case PIPE_SHADER_CAP_LDEXP_SUPPORTED:
1070    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
1071    case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
1072       return 0; /* not implemented */
1073    case PIPE_SHADER_CAP_CONT_SUPPORTED:
1074       return 1;
1075    }
1076 
1077    /* should only get here on unhandled cases */
1078    return 0;
1079 }
1080 
1081 static VkSampleCountFlagBits
vk_sample_count_flags(uint32_t sample_count)1082 vk_sample_count_flags(uint32_t sample_count)
1083 {
1084    switch (sample_count) {
1085    case 1: return VK_SAMPLE_COUNT_1_BIT;
1086    case 2: return VK_SAMPLE_COUNT_2_BIT;
1087    case 4: return VK_SAMPLE_COUNT_4_BIT;
1088    case 8: return VK_SAMPLE_COUNT_8_BIT;
1089    case 16: return VK_SAMPLE_COUNT_16_BIT;
1090    case 32: return VK_SAMPLE_COUNT_32_BIT;
1091    case 64: return VK_SAMPLE_COUNT_64_BIT;
1092    default:
1093       return 0;
1094    }
1095 }
1096 
1097 static bool
zink_is_compute_copy_faster(struct pipe_screen * pscreen,enum pipe_format src_format,enum pipe_format dst_format,unsigned width,unsigned height,unsigned depth,bool cpu)1098 zink_is_compute_copy_faster(struct pipe_screen *pscreen,
1099                             enum pipe_format src_format,
1100                             enum pipe_format dst_format,
1101                             unsigned width,
1102                             unsigned height,
1103                             unsigned depth,
1104                             bool cpu)
1105 {
1106    if (cpu)
1107       /* very basic for now, probably even worse for some cases,
1108        * but fixes lots of others
1109        */
1110       return width * height * depth > 64 * 64;
1111    return false;
1112 }
1113 
1114 static bool
zink_is_format_supported(struct pipe_screen * pscreen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned bind)1115 zink_is_format_supported(struct pipe_screen *pscreen,
1116                          enum pipe_format format,
1117                          enum pipe_texture_target target,
1118                          unsigned sample_count,
1119                          unsigned storage_sample_count,
1120                          unsigned bind)
1121 {
1122    struct zink_screen *screen = zink_screen(pscreen);
1123 
1124    if (storage_sample_count && !screen->info.feats.features.shaderStorageImageMultisample && bind & PIPE_BIND_SHADER_IMAGE)
1125       return false;
1126 
1127    if (format == PIPE_FORMAT_NONE)
1128       return screen->info.props.limits.framebufferNoAttachmentsSampleCounts &
1129              vk_sample_count_flags(sample_count);
1130 
1131    if (bind & PIPE_BIND_INDEX_BUFFER) {
1132       if (format == PIPE_FORMAT_R8_UINT &&
1133           !screen->info.have_EXT_index_type_uint8)
1134          return false;
1135       if (format != PIPE_FORMAT_R8_UINT &&
1136           format != PIPE_FORMAT_R16_UINT &&
1137           format != PIPE_FORMAT_R32_UINT)
1138          return false;
1139    }
1140 
1141    VkFormat vkformat = zink_get_format(screen, format);
1142    if (vkformat == VK_FORMAT_UNDEFINED)
1143       return false;
1144 
1145    if (sample_count >= 1) {
1146       VkSampleCountFlagBits sample_mask = vk_sample_count_flags(sample_count);
1147       if (!sample_mask)
1148          return false;
1149       const struct util_format_description *desc = util_format_description(format);
1150       if (util_format_is_depth_or_stencil(format)) {
1151          if (util_format_has_depth(desc)) {
1152             if (bind & PIPE_BIND_DEPTH_STENCIL &&
1153                 (screen->info.props.limits.framebufferDepthSampleCounts & sample_mask) != sample_mask)
1154                return false;
1155             if (bind & PIPE_BIND_SAMPLER_VIEW &&
1156                 (screen->info.props.limits.sampledImageDepthSampleCounts & sample_mask) != sample_mask)
1157                return false;
1158          }
1159          if (util_format_has_stencil(desc)) {
1160             if (bind & PIPE_BIND_DEPTH_STENCIL &&
1161                 (screen->info.props.limits.framebufferStencilSampleCounts & sample_mask) != sample_mask)
1162                return false;
1163             if (bind & PIPE_BIND_SAMPLER_VIEW &&
1164                 (screen->info.props.limits.sampledImageStencilSampleCounts & sample_mask) != sample_mask)
1165                return false;
1166          }
1167       } else if (util_format_is_pure_integer(format)) {
1168          if (bind & PIPE_BIND_RENDER_TARGET &&
1169              !(screen->info.props.limits.framebufferColorSampleCounts & sample_mask))
1170             return false;
1171          if (bind & PIPE_BIND_SAMPLER_VIEW &&
1172              !(screen->info.props.limits.sampledImageIntegerSampleCounts & sample_mask))
1173             return false;
1174       } else {
1175          if (bind & PIPE_BIND_RENDER_TARGET &&
1176              !(screen->info.props.limits.framebufferColorSampleCounts & sample_mask))
1177             return false;
1178          if (bind & PIPE_BIND_SAMPLER_VIEW &&
1179              !(screen->info.props.limits.sampledImageColorSampleCounts & sample_mask))
1180             return false;
1181       }
1182       if (bind & PIPE_BIND_SHADER_IMAGE) {
1183           if (!(screen->info.props.limits.storageImageSampleCounts & sample_mask))
1184              return false;
1185       }
1186    }
1187 
1188    VkFormatProperties props = screen->format_props[format];
1189 
1190    if (target == PIPE_BUFFER) {
1191       if (bind & PIPE_BIND_VERTEX_BUFFER) {
1192          if (!(props.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT)) {
1193             enum pipe_format new_format = zink_decompose_vertex_format(format);
1194             if (!new_format)
1195                return false;
1196             if (!(screen->format_props[new_format].bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT))
1197                return false;
1198          }
1199       }
1200 
1201       if (bind & PIPE_BIND_SAMPLER_VIEW &&
1202          !(props.bufferFeatures & VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT))
1203             return false;
1204 
1205       if (bind & PIPE_BIND_SHADER_IMAGE &&
1206           !(props.bufferFeatures & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT))
1207          return false;
1208    } else {
1209       /* all other targets are texture-targets */
1210       if (bind & PIPE_BIND_RENDER_TARGET &&
1211           !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
1212          return false;
1213 
1214       if (bind & PIPE_BIND_BLENDABLE &&
1215          !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT))
1216         return false;
1217 
1218       if (bind & PIPE_BIND_SAMPLER_VIEW &&
1219          !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
1220             return false;
1221 
1222       if (bind & PIPE_BIND_SAMPLER_REDUCTION_MINMAX &&
1223           !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT))
1224          return false;
1225 
1226       if ((bind & PIPE_BIND_SAMPLER_VIEW) || (bind & PIPE_BIND_RENDER_TARGET)) {
1227          /* if this is a 3-component texture, force gallium to give us 4 components by rejecting this one */
1228          const struct util_format_description *desc = util_format_description(format);
1229          if (desc->nr_channels == 3 &&
1230              (desc->block.bits == 24 || desc->block.bits == 48 || desc->block.bits == 96))
1231             return false;
1232       }
1233 
1234       if (bind & PIPE_BIND_DEPTH_STENCIL &&
1235           !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
1236          return false;
1237 
1238       if (bind & PIPE_BIND_SHADER_IMAGE &&
1239           !(props.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT))
1240          return false;
1241    }
1242 
1243    if (util_format_is_compressed(format)) {
1244       const struct util_format_description *desc = util_format_description(format);
1245       if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC &&
1246           !screen->info.feats.features.textureCompressionBC)
1247          return false;
1248    }
1249 
1250    return true;
1251 }
1252 
1253 static void
zink_destroy_screen(struct pipe_screen * pscreen)1254 zink_destroy_screen(struct pipe_screen *pscreen)
1255 {
1256    struct zink_screen *screen = zink_screen(pscreen);
1257 
1258    hash_table_foreach(&screen->dts, entry)
1259       zink_kopper_deinit_displaytarget(screen, entry->data);
1260    simple_mtx_destroy(&screen->dt_lock);
1261 
1262    if (screen->copy_context)
1263       screen->copy_context->base.destroy(&screen->copy_context->base);
1264 
1265    if (VK_NULL_HANDLE != screen->debugUtilsCallbackHandle) {
1266       VKSCR(DestroyDebugUtilsMessengerEXT)(screen->instance, screen->debugUtilsCallbackHandle, NULL);
1267    }
1268 
1269    util_vertex_state_cache_deinit(&screen->vertex_state_cache);
1270 
1271    u_transfer_helper_destroy(pscreen->transfer_helper);
1272 #ifdef ENABLE_SHADER_CACHE
1273    if (screen->disk_cache) {
1274       util_queue_finish(&screen->cache_put_thread);
1275       util_queue_finish(&screen->cache_get_thread);
1276       disk_cache_wait_for_idle(screen->disk_cache);
1277       util_queue_destroy(&screen->cache_put_thread);
1278       util_queue_destroy(&screen->cache_get_thread);
1279    }
1280 #endif
1281    disk_cache_destroy(screen->disk_cache);
1282    zink_bo_deinit(screen);
1283    util_live_shader_cache_deinit(&screen->shaders);
1284 
1285    if (screen->sem)
1286       VKSCR(DestroySemaphore)(screen->dev, screen->sem, NULL);
1287 
1288    if (screen->fence)
1289       VKSCR(DestroyFence)(screen->dev, screen->fence, NULL);
1290 
1291    if (screen->threaded)
1292       util_queue_destroy(&screen->flush_queue);
1293 
1294    simple_mtx_destroy(&screen->queue_lock);
1295    VKSCR(DestroyDevice)(screen->dev, NULL);
1296    VKSCR(DestroyInstance)(screen->instance, NULL);
1297    util_idalloc_mt_fini(&screen->buffer_ids);
1298 
1299    util_dl_close(screen->loader_lib);
1300    if (screen->drm_fd != -1)
1301       close(screen->drm_fd);
1302 
1303    slab_destroy_parent(&screen->transfer_pool);
1304    ralloc_free(screen);
1305    glsl_type_singleton_decref();
1306 }
1307 
1308 static bool
choose_pdev(struct zink_screen * screen)1309 choose_pdev(struct zink_screen *screen)
1310 {
1311    uint32_t i, pdev_count;
1312    VkPhysicalDevice *pdevs;
1313    bool is_cpu = false;
1314    VkResult result = VKSCR(EnumeratePhysicalDevices)(screen->instance, &pdev_count, NULL);
1315    if (result != VK_SUCCESS) {
1316       mesa_loge("ZINK: vkEnumeratePhysicalDevices failed (%s)", vk_Result_to_str(result));
1317       return is_cpu;
1318    }
1319 
1320    assert(pdev_count > 0);
1321 
1322    pdevs = malloc(sizeof(*pdevs) * pdev_count);
1323    result = VKSCR(EnumeratePhysicalDevices)(screen->instance, &pdev_count, pdevs);
1324    assert(result == VK_SUCCESS);
1325    assert(pdev_count > 0);
1326 
1327    VkPhysicalDeviceProperties props;
1328    bool cpu = debug_get_bool_option("LIBGL_ALWAYS_SOFTWARE", false);
1329    /* priority when multiple drivers are available (highest to lowest):
1330       VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU
1331       VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
1332       VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
1333       VK_PHYSICAL_DEVICE_TYPE_CPU
1334       VK_PHYSICAL_DEVICE_TYPE_OTHER
1335 
1336     * users should specify VK_ICD_FILENAMES since this is a standardized variable
1337     * used by all vulkan applications
1338     */
1339    unsigned prio_map[] = {
1340       [VK_PHYSICAL_DEVICE_TYPE_OTHER] = 0,
1341       [VK_PHYSICAL_DEVICE_TYPE_CPU] = 1,
1342       [VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU] = 2,
1343       [VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU] = 3,
1344       [VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU] = 4,
1345    };
1346    unsigned idx = 0;
1347    int cur_prio = 0;
1348    for (i = 0; i < pdev_count; ++i) {
1349       VKSCR(GetPhysicalDeviceProperties)(pdevs[i], &props);
1350 
1351       if (cpu) {
1352          /* if user wants cpu, only give them cpu */
1353          if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1354             idx = i;
1355             cur_prio = prio_map[props.deviceType];
1356             break;
1357          }
1358       } else {
1359          assert(props.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU);
1360          if (prio_map[props.deviceType] > cur_prio) {
1361             idx = i;
1362             cur_prio = prio_map[props.deviceType];
1363          }
1364       }
1365    }
1366    is_cpu = cur_prio == prio_map[VK_PHYSICAL_DEVICE_TYPE_CPU];
1367    if (cpu != is_cpu)
1368       goto out;
1369 
1370    screen->pdev = pdevs[idx];
1371    VKSCR(GetPhysicalDeviceProperties)(screen->pdev, &screen->info.props);
1372    screen->info.device_version = screen->info.props.apiVersion;
1373 
1374    /* runtime version is the lesser of the instance version and device version */
1375    screen->vk_version = MIN2(screen->info.device_version, screen->instance_info.loader_version);
1376 
1377    /* calculate SPIR-V version based on VK version */
1378    if (screen->vk_version >= VK_MAKE_VERSION(1, 2, 0))
1379       screen->spirv_version = SPIRV_VERSION(1, 5);
1380    else if (screen->vk_version >= VK_MAKE_VERSION(1, 1, 0))
1381       screen->spirv_version = SPIRV_VERSION(1, 3);
1382    else
1383       screen->spirv_version = SPIRV_VERSION(1, 0);
1384 out:
1385    free(pdevs);
1386    return is_cpu;
1387 }
1388 
1389 static void
update_queue_props(struct zink_screen * screen)1390 update_queue_props(struct zink_screen *screen)
1391 {
1392    uint32_t num_queues;
1393    VKSCR(GetPhysicalDeviceQueueFamilyProperties)(screen->pdev, &num_queues, NULL);
1394    assert(num_queues > 0);
1395 
1396    VkQueueFamilyProperties *props = malloc(sizeof(*props) * num_queues);
1397    VKSCR(GetPhysicalDeviceQueueFamilyProperties)(screen->pdev, &num_queues, props);
1398 
1399    bool found_gfx = false;
1400    uint32_t sparse_only = UINT32_MAX;
1401    screen->sparse_queue = UINT32_MAX;
1402    for (uint32_t i = 0; i < num_queues; i++) {
1403       if (!found_gfx && (props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
1404          screen->gfx_queue = i;
1405          screen->max_queues = props[i].queueCount;
1406          screen->timestamp_valid_bits = props[i].timestampValidBits;
1407          found_gfx = true;
1408          if (props[i].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT)
1409             screen->sparse_queue = i;
1410       } else if (props[i].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT)
1411          sparse_only = i;
1412    }
1413    if (screen->sparse_queue == UINT32_MAX)
1414       screen->sparse_queue = sparse_only;
1415    free(props);
1416 }
1417 
1418 static void
init_queue(struct zink_screen * screen)1419 init_queue(struct zink_screen *screen)
1420 {
1421    simple_mtx_init(&screen->queue_lock, mtx_plain);
1422    VKSCR(GetDeviceQueue)(screen->dev, screen->gfx_queue, 0, &screen->queue);
1423    if (screen->sparse_queue != UINT32_MAX) {
1424       if (screen->sparse_queue != screen->gfx_queue)
1425          VKSCR(GetDeviceQueue)(screen->dev, screen->sparse_queue, 0, &screen->queue_sparse);
1426       else
1427          screen->queue_sparse = screen->queue;
1428    }
1429 }
1430 
1431 static void
zink_flush_frontbuffer(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * pres,unsigned level,unsigned layer,void * winsys_drawable_handle,struct pipe_box * sub_box)1432 zink_flush_frontbuffer(struct pipe_screen *pscreen,
1433                        struct pipe_context *pctx,
1434                        struct pipe_resource *pres,
1435                        unsigned level, unsigned layer,
1436                        void *winsys_drawable_handle,
1437                        struct pipe_box *sub_box)
1438 {
1439    struct zink_screen *screen = zink_screen(pscreen);
1440    struct zink_resource *res = zink_resource(pres);
1441    struct zink_context *ctx = zink_context(pctx);
1442 
1443    /* if the surface has never been acquired, there's nothing to present,
1444     * so this is a no-op */
1445    if (!zink_is_swapchain(res) || (!zink_kopper_acquired(res->obj->dt, res->obj->dt_idx) && res->obj->last_dt_idx == UINT32_MAX))
1446       return;
1447 
1448    ctx = zink_tc_context_unwrap(pctx);
1449    if (ctx->batch.swapchain || ctx->needs_present) {
1450       ctx->batch.has_work = true;
1451       pctx->flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
1452       if (ctx->last_fence && screen->threaded) {
1453          struct zink_batch_state *bs = zink_batch_state(ctx->last_fence);
1454          util_queue_fence_wait(&bs->flush_completed);
1455       }
1456    }
1457 
1458    if (zink_kopper_acquired(res->obj->dt, res->obj->dt_idx))
1459       zink_kopper_present_queue(screen, res);
1460    else {
1461       assert(res->obj->last_dt_idx != UINT32_MAX);
1462       if (!zink_kopper_last_present_eq(res->obj->dt, res->obj->last_dt_idx)) {
1463          zink_kopper_acquire_readback(ctx, res);
1464          zink_kopper_present_readback(ctx, res);
1465       }
1466    }
1467 }
1468 
1469 bool
zink_is_depth_format_supported(struct zink_screen * screen,VkFormat format)1470 zink_is_depth_format_supported(struct zink_screen *screen, VkFormat format)
1471 {
1472    VkFormatProperties props;
1473    VKSCR(GetPhysicalDeviceFormatProperties)(screen->pdev, format, &props);
1474    return (props.linearTilingFeatures | props.optimalTilingFeatures) &
1475           VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
1476 }
1477 
1478 static enum pipe_format
emulate_x8(enum pipe_format format)1479 emulate_x8(enum pipe_format format)
1480 {
1481    /* convert missing X8 variants to A8 */
1482    switch (format) {
1483    case PIPE_FORMAT_B8G8R8X8_UNORM:
1484       return PIPE_FORMAT_B8G8R8A8_UNORM;
1485 
1486    case PIPE_FORMAT_B8G8R8X8_SRGB:
1487       return PIPE_FORMAT_B8G8R8A8_SRGB;
1488    case PIPE_FORMAT_R8G8B8X8_SRGB:
1489       return PIPE_FORMAT_R8G8B8A8_SRGB;
1490 
1491    case PIPE_FORMAT_R8G8B8X8_SINT:
1492       return PIPE_FORMAT_R8G8B8A8_SINT;
1493    case PIPE_FORMAT_R8G8B8X8_SNORM:
1494       return PIPE_FORMAT_R8G8B8A8_SNORM;
1495    case PIPE_FORMAT_R8G8B8X8_UNORM:
1496       return PIPE_FORMAT_R8G8B8A8_UNORM;
1497 
1498    case PIPE_FORMAT_R16G16B16X16_FLOAT:
1499       return PIPE_FORMAT_R16G16B16A16_FLOAT;
1500    case PIPE_FORMAT_R16G16B16X16_SINT:
1501       return PIPE_FORMAT_R16G16B16A16_SINT;
1502    case PIPE_FORMAT_R16G16B16X16_SNORM:
1503       return PIPE_FORMAT_R16G16B16A16_SNORM;
1504    case PIPE_FORMAT_R16G16B16X16_UNORM:
1505       return PIPE_FORMAT_R16G16B16A16_UNORM;
1506 
1507    default:
1508       return format;
1509    }
1510 }
1511 
1512 VkFormat
zink_get_format(struct zink_screen * screen,enum pipe_format format)1513 zink_get_format(struct zink_screen *screen, enum pipe_format format)
1514 {
1515    VkFormat ret = zink_pipe_format_to_vk_format(emulate_x8(format));
1516 
1517    if (format == PIPE_FORMAT_X32_S8X24_UINT &&
1518        screen->have_D32_SFLOAT_S8_UINT)
1519       return VK_FORMAT_D32_SFLOAT_S8_UINT;
1520 
1521    if (format == PIPE_FORMAT_X24S8_UINT)
1522       /* valid when using aspects to extract stencil,
1523        * fails format test because it's emulated */
1524       ret = VK_FORMAT_D24_UNORM_S8_UINT;
1525 
1526    if (ret == VK_FORMAT_X8_D24_UNORM_PACK32 &&
1527        !screen->have_X8_D24_UNORM_PACK32) {
1528       assert(zink_is_depth_format_supported(screen, VK_FORMAT_D32_SFLOAT));
1529       return VK_FORMAT_D32_SFLOAT;
1530    }
1531 
1532    if (ret == VK_FORMAT_D24_UNORM_S8_UINT &&
1533        !screen->have_D24_UNORM_S8_UINT) {
1534       assert(screen->have_D32_SFLOAT_S8_UINT);
1535       return VK_FORMAT_D32_SFLOAT_S8_UINT;
1536    }
1537 
1538    if ((ret == VK_FORMAT_A4B4G4R4_UNORM_PACK16 &&
1539         !screen->info.format_4444_feats.formatA4B4G4R4) ||
1540        (ret == VK_FORMAT_A4R4G4B4_UNORM_PACK16 &&
1541         !screen->info.format_4444_feats.formatA4R4G4B4))
1542       return VK_FORMAT_UNDEFINED;
1543 
1544    return ret;
1545 }
1546 
1547 void
zink_screen_init_descriptor_funcs(struct zink_screen * screen,bool fallback)1548 zink_screen_init_descriptor_funcs(struct zink_screen *screen, bool fallback)
1549 {
1550    if (!fallback &&
1551        zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_LAZY) {
1552 #define LAZY(FUNC) screen->FUNC = zink_##FUNC##_lazy
1553       LAZY(descriptor_program_init);
1554       LAZY(descriptor_program_deinit);
1555       LAZY(context_invalidate_descriptor_state);
1556       LAZY(batch_descriptor_init);
1557       LAZY(batch_descriptor_reset);
1558       LAZY(batch_descriptor_deinit);
1559       LAZY(descriptors_init);
1560       LAZY(descriptors_deinit);
1561       LAZY(descriptors_update);
1562 #undef LAZY
1563    } else {
1564 #define DEFAULT(FUNC) screen->FUNC = zink_##FUNC
1565       DEFAULT(descriptor_program_init);
1566       DEFAULT(descriptor_program_deinit);
1567       DEFAULT(context_invalidate_descriptor_state);
1568       DEFAULT(batch_descriptor_init);
1569       DEFAULT(batch_descriptor_reset);
1570       DEFAULT(batch_descriptor_deinit);
1571       DEFAULT(descriptors_init);
1572       DEFAULT(descriptors_deinit);
1573       DEFAULT(descriptors_update);
1574 #undef DEFAULT
1575    }
1576 }
1577 
1578 static bool
check_have_device_time(struct zink_screen * screen)1579 check_have_device_time(struct zink_screen *screen)
1580 {
1581    uint32_t num_domains = 0;
1582    VkTimeDomainEXT domains[8]; //current max is 4
1583    VkResult result = VKSCR(GetPhysicalDeviceCalibrateableTimeDomainsEXT)(screen->pdev, &num_domains, NULL);
1584    if (result != VK_SUCCESS) {
1585       mesa_loge("ZINK: vkGetPhysicalDeviceCalibrateableTimeDomainsEXT failed (%s)", vk_Result_to_str(result));
1586    }
1587    assert(num_domains > 0);
1588    assert(num_domains < ARRAY_SIZE(domains));
1589 
1590    result = VKSCR(GetPhysicalDeviceCalibrateableTimeDomainsEXT)(screen->pdev, &num_domains, domains);
1591    if (result != VK_SUCCESS) {
1592       mesa_loge("ZINK: vkGetPhysicalDeviceCalibrateableTimeDomainsEXT failed (%s)", vk_Result_to_str(result));
1593    }
1594 
1595    /* VK_TIME_DOMAIN_DEVICE_EXT is used for the ctx->get_timestamp hook and is the only one we really need */
1596    for (unsigned i = 0; i < num_domains; i++) {
1597       if (domains[i] == VK_TIME_DOMAIN_DEVICE_EXT) {
1598          return true;
1599       }
1600    }
1601 
1602    return false;
1603 }
1604 
1605 static void
zink_error(const char * msg)1606 zink_error(const char *msg)
1607 {
1608 }
1609 
1610 static void
zink_warn(const char * msg)1611 zink_warn(const char *msg)
1612 {
1613 }
1614 
1615 static void
zink_info(const char * msg)1616 zink_info(const char *msg)
1617 {
1618 }
1619 
1620 static void
zink_msg(const char * msg)1621 zink_msg(const char *msg)
1622 {
1623 }
1624 
1625 static VKAPI_ATTR VkBool32 VKAPI_CALL
zink_debug_util_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,VkDebugUtilsMessageTypeFlagsEXT messageType,const VkDebugUtilsMessengerCallbackDataEXT * pCallbackData,void * pUserData)1626 zink_debug_util_callback(
1627     VkDebugUtilsMessageSeverityFlagBitsEXT           messageSeverity,
1628     VkDebugUtilsMessageTypeFlagsEXT                  messageType,
1629     const VkDebugUtilsMessengerCallbackDataEXT      *pCallbackData,
1630     void                                            *pUserData)
1631 {
1632    // Pick message prefix and color to use.
1633    // Only MacOS and Linux have been tested for color support
1634    if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
1635       zink_error(pCallbackData->pMessage);
1636    } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
1637       zink_warn(pCallbackData->pMessage);
1638    } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
1639       zink_info(pCallbackData->pMessage);
1640    } else
1641       zink_msg(pCallbackData->pMessage);
1642 
1643    return VK_FALSE;
1644 }
1645 
1646 static bool
create_debug(struct zink_screen * screen)1647 create_debug(struct zink_screen *screen)
1648 {
1649    VkDebugUtilsMessengerCreateInfoEXT vkDebugUtilsMessengerCreateInfoEXT = {
1650        VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
1651        NULL,
1652        0,  // flags
1653        VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
1654        VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
1655        VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
1656        VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
1657        VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
1658        VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
1659        VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
1660        zink_debug_util_callback,
1661        NULL
1662    };
1663 
1664    VkDebugUtilsMessengerEXT vkDebugUtilsCallbackEXT = VK_NULL_HANDLE;
1665 
1666    VkResult result = VKSCR(CreateDebugUtilsMessengerEXT)(
1667            screen->instance,
1668            &vkDebugUtilsMessengerCreateInfoEXT,
1669            NULL,
1670            &vkDebugUtilsCallbackEXT);
1671    if (result != VK_SUCCESS) {
1672       mesa_loge("ZINK: vkCreateDebugUtilsMessengerEXT failed (%s)", vk_Result_to_str(result));
1673    }
1674 
1675    screen->debugUtilsCallbackHandle = vkDebugUtilsCallbackEXT;
1676 
1677    return true;
1678 }
1679 
1680 static bool
zink_internal_setup_moltenvk(struct zink_screen * screen)1681 zink_internal_setup_moltenvk(struct zink_screen *screen)
1682 {
1683 #if defined(MVK_VERSION)
1684    if (!screen->instance_info.have_MVK_moltenvk)
1685       return true;
1686 
1687    GET_PROC_ADDR_INSTANCE_LOCAL(screen, screen->instance, GetMoltenVKConfigurationMVK);
1688    GET_PROC_ADDR_INSTANCE_LOCAL(screen, screen->instance, SetMoltenVKConfigurationMVK);
1689    GET_PROC_ADDR_INSTANCE_LOCAL(screen, screen->instance, GetVersionStringsMVK);
1690 
1691    if (vk_GetVersionStringsMVK) {
1692       char molten_version[64] = {0};
1693       char vulkan_version[64] = {0};
1694 
1695       vk_GetVersionStringsMVK(molten_version, sizeof(molten_version) - 1, vulkan_version, sizeof(vulkan_version) - 1);
1696 
1697       printf("zink: MoltenVK %s Vulkan %s \n", molten_version, vulkan_version);
1698    }
1699 
1700    if (vk_GetMoltenVKConfigurationMVK && vk_SetMoltenVKConfigurationMVK) {
1701       MVKConfiguration molten_config = {0};
1702       size_t molten_config_size = sizeof(molten_config);
1703 
1704       VkResult res = vk_GetMoltenVKConfigurationMVK(screen->instance, &molten_config, &molten_config_size);
1705       if (res == VK_SUCCESS || res == VK_INCOMPLETE) {
1706          // Needed to allow MoltenVK to accept VkImageView swizzles.
1707          // Encountered when using VK_FORMAT_R8G8_UNORM
1708          molten_config.fullImageViewSwizzle = VK_TRUE;
1709          vk_SetMoltenVKConfigurationMVK(screen->instance, &molten_config, &molten_config_size);
1710       }
1711    }
1712 #endif // MVK_VERSION
1713 
1714    return true;
1715 }
1716 
1717 static void
populate_format_props(struct zink_screen * screen)1718 populate_format_props(struct zink_screen *screen)
1719 {
1720    for (unsigned i = 0; i < PIPE_FORMAT_COUNT; i++) {
1721       VkFormat format = zink_get_format(screen, i);
1722       if (!format)
1723          continue;
1724       if (VKSCR(GetPhysicalDeviceFormatProperties2)) {
1725          VkFormatProperties2 props = {0};
1726          props.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;
1727 
1728          VkDrmFormatModifierPropertiesListEXT mod_props;
1729          VkDrmFormatModifierPropertiesEXT mods[128];
1730          if (screen->info.have_EXT_image_drm_format_modifier) {
1731             mod_props.sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT;
1732             mod_props.pNext = NULL;
1733             mod_props.drmFormatModifierCount = ARRAY_SIZE(mods);
1734             mod_props.pDrmFormatModifierProperties = mods;
1735             props.pNext = &mod_props;
1736          }
1737          VkFormatProperties3 props3 = {0};
1738          props3.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3;
1739          props3.pNext = props.pNext;
1740          props.pNext = &props3;
1741          VKSCR(GetPhysicalDeviceFormatProperties2)(screen->pdev, format, &props);
1742          screen->format_props[i] = props.formatProperties;
1743          if (props3.linearTilingFeatures & VK_FORMAT_FEATURE_2_LINEAR_COLOR_ATTACHMENT_BIT_NV)
1744             screen->format_props[i].linearTilingFeatures |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
1745          if (screen->info.have_EXT_image_drm_format_modifier && mod_props.drmFormatModifierCount) {
1746             screen->modifier_props[i].drmFormatModifierCount = mod_props.drmFormatModifierCount;
1747             screen->modifier_props[i].pDrmFormatModifierProperties = ralloc_array(screen, VkDrmFormatModifierPropertiesEXT, mod_props.drmFormatModifierCount);
1748             if (mod_props.pDrmFormatModifierProperties) {
1749                for (unsigned j = 0; j < mod_props.drmFormatModifierCount; j++)
1750                   screen->modifier_props[i].pDrmFormatModifierProperties[j] = mod_props.pDrmFormatModifierProperties[j];
1751             }
1752          }
1753       } else
1754          VKSCR(GetPhysicalDeviceFormatProperties)(screen->pdev, format, &screen->format_props[i]);
1755    }
1756    VkImageFormatProperties image_props;
1757    VkResult ret = VKSCR(GetPhysicalDeviceImageFormatProperties)(screen->pdev, VK_FORMAT_D32_SFLOAT,
1758                                                                 VK_IMAGE_TYPE_1D,
1759                                                                 VK_IMAGE_TILING_OPTIMAL,
1760                                                                 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1761                                                                 0, &image_props);
1762    if (ret != VK_SUCCESS && ret != VK_ERROR_FORMAT_NOT_SUPPORTED) {
1763       mesa_loge("ZINK: vkGetPhysicalDeviceImageFormatProperties failed (%s)", vk_Result_to_str(ret));
1764    }
1765    screen->need_2D_zs = ret != VK_SUCCESS;
1766 
1767    if (screen->info.feats.features.sparseResidencyImage2D)
1768       screen->need_2D_sparse = !screen->base.get_sparse_texture_virtual_page_size(&screen->base, PIPE_TEXTURE_1D, false, PIPE_FORMAT_R32_FLOAT, 0, 16, NULL, NULL, NULL);
1769 }
1770 
1771 bool
zink_screen_init_semaphore(struct zink_screen * screen)1772 zink_screen_init_semaphore(struct zink_screen *screen)
1773 {
1774    VkSemaphoreCreateInfo sci = {0};
1775    VkSemaphoreTypeCreateInfo tci = {0};
1776    sci.pNext = &tci;
1777    sci.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1778    tci.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO;
1779    tci.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE;
1780 
1781    return VKSCR(CreateSemaphore)(screen->dev, &sci, NULL, &screen->sem) == VK_SUCCESS;
1782 }
1783 
1784 bool
zink_screen_timeline_wait(struct zink_screen * screen,uint64_t batch_id,uint64_t timeout)1785 zink_screen_timeline_wait(struct zink_screen *screen, uint64_t batch_id, uint64_t timeout)
1786 {
1787    VkSemaphoreWaitInfo wi = {0};
1788 
1789    if (zink_screen_check_last_finished(screen, batch_id))
1790       return true;
1791 
1792    wi.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO;
1793    wi.semaphoreCount = 1;
1794    wi.pSemaphores = &screen->sem;
1795    wi.pValues = &batch_id;
1796    bool success = false;
1797    if (screen->device_lost)
1798       return true;
1799    VkResult ret = VKSCR(WaitSemaphores)(screen->dev, &wi, timeout);
1800    success = zink_screen_handle_vkresult(screen, ret);
1801 
1802    if (success)
1803       zink_screen_update_last_finished(screen, batch_id);
1804 
1805    return success;
1806 }
1807 
1808 static uint32_t
zink_get_loader_version(struct zink_screen * screen)1809 zink_get_loader_version(struct zink_screen *screen)
1810 {
1811 
1812    uint32_t loader_version = VK_API_VERSION_1_0;
1813 
1814    // Get the Loader version
1815    GET_PROC_ADDR_INSTANCE_LOCAL(screen, NULL, EnumerateInstanceVersion);
1816    if (vk_EnumerateInstanceVersion) {
1817       uint32_t loader_version_temp = VK_API_VERSION_1_0;
1818       VkResult result = (*vk_EnumerateInstanceVersion)(&loader_version_temp);
1819       if (VK_SUCCESS == result) {
1820          loader_version = loader_version_temp;
1821       } else {
1822          mesa_loge("ZINK: vkEnumerateInstanceVersion failed (%s)", vk_Result_to_str(result));
1823       }
1824    }
1825 
1826    return loader_version;
1827 }
1828 
1829 static void
zink_query_memory_info(struct pipe_screen * pscreen,struct pipe_memory_info * info)1830 zink_query_memory_info(struct pipe_screen *pscreen, struct pipe_memory_info *info)
1831 {
1832    struct zink_screen *screen = zink_screen(pscreen);
1833    memset(info, 0, sizeof(struct pipe_memory_info));
1834    if (screen->info.have_EXT_memory_budget && VKSCR(GetPhysicalDeviceMemoryProperties2)) {
1835       VkPhysicalDeviceMemoryProperties2 mem = {0};
1836       mem.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
1837 
1838       VkPhysicalDeviceMemoryBudgetPropertiesEXT budget = {0};
1839       budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
1840       mem.pNext = &budget;
1841       VKSCR(GetPhysicalDeviceMemoryProperties2)(screen->pdev, &mem);
1842 
1843       for (unsigned i = 0; i < mem.memoryProperties.memoryHeapCount; i++) {
1844          if (mem.memoryProperties.memoryHeaps[i].flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
1845             /* VRAM */
1846             info->total_device_memory += mem.memoryProperties.memoryHeaps[i].size / 1024;
1847             info->avail_device_memory += (mem.memoryProperties.memoryHeaps[i].size - budget.heapUsage[i]) / 1024;
1848          } else {
1849             /* GART */
1850             info->total_staging_memory += mem.memoryProperties.memoryHeaps[i].size / 1024;
1851             info->avail_staging_memory += (mem.memoryProperties.memoryHeaps[i].size - budget.heapUsage[i]) / 1024;
1852          }
1853       }
1854       /* evictions not yet supported in vulkan */
1855    } else {
1856       for (unsigned i = 0; i < screen->info.mem_props.memoryHeapCount; i++) {
1857          if (screen->info.mem_props.memoryHeaps[i].flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
1858             /* VRAM */
1859             info->total_device_memory += screen->info.mem_props.memoryHeaps[i].size / 1024;
1860             /* free real estate! */
1861             info->avail_device_memory += info->total_device_memory;
1862          } else {
1863             /* GART */
1864             info->total_staging_memory += screen->info.mem_props.memoryHeaps[i].size / 1024;
1865             /* free real estate! */
1866             info->avail_staging_memory += info->total_staging_memory;
1867          }
1868       }
1869    }
1870 }
1871 
1872 static void
zink_query_dmabuf_modifiers(struct pipe_screen * pscreen,enum pipe_format format,int max,uint64_t * modifiers,unsigned int * external_only,int * count)1873 zink_query_dmabuf_modifiers(struct pipe_screen *pscreen, enum pipe_format format, int max, uint64_t *modifiers, unsigned int *external_only, int *count)
1874 {
1875    struct zink_screen *screen = zink_screen(pscreen);
1876    *count = screen->modifier_props[format].drmFormatModifierCount;
1877    for (int i = 0; i < MIN2(max, *count); i++)
1878       modifiers[i] = screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifier;
1879 }
1880 
1881 static bool
zink_is_dmabuf_modifier_supported(struct pipe_screen * pscreen,uint64_t modifier,enum pipe_format format,bool * external_only)1882 zink_is_dmabuf_modifier_supported(struct pipe_screen *pscreen, uint64_t modifier, enum pipe_format format, bool *external_only)
1883 {
1884    struct zink_screen *screen = zink_screen(pscreen);
1885    for (unsigned i = 0; i < screen->modifier_props[format].drmFormatModifierCount; i++)
1886       if (screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
1887          return true;
1888    return false;
1889 }
1890 
1891 static unsigned
zink_get_dmabuf_modifier_planes(struct pipe_screen * pscreen,uint64_t modifier,enum pipe_format format)1892 zink_get_dmabuf_modifier_planes(struct pipe_screen *pscreen, uint64_t modifier, enum pipe_format format)
1893 {
1894    struct zink_screen *screen = zink_screen(pscreen);
1895    for (unsigned i = 0; i < screen->modifier_props[format].drmFormatModifierCount; i++)
1896       if (screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
1897          return screen->modifier_props[format].pDrmFormatModifierProperties[i].drmFormatModifierPlaneCount;
1898    return 0;
1899 }
1900 
1901 static int
zink_get_sparse_texture_virtual_page_size(struct pipe_screen * pscreen,enum pipe_texture_target target,bool multi_sample,enum pipe_format pformat,unsigned offset,unsigned size,int * x,int * y,int * z)1902 zink_get_sparse_texture_virtual_page_size(struct pipe_screen *pscreen,
1903                                           enum pipe_texture_target target,
1904                                           bool multi_sample,
1905                                           enum pipe_format pformat,
1906                                           unsigned offset, unsigned size,
1907                                           int *x, int *y, int *z)
1908 {
1909    struct zink_screen *screen = zink_screen(pscreen);
1910    static const int page_size_2d[][3] = {
1911       { 256, 256, 1 }, /* 8bpp   */
1912       { 256, 128, 1 }, /* 16bpp  */
1913       { 128, 128, 1 }, /* 32bpp  */
1914       { 128, 64,  1 }, /* 64bpp  */
1915       { 64,  64,  1 }, /* 128bpp */
1916    };
1917    static const int page_size_3d[][3] = {
1918       { 64,  32,  32 }, /* 8bpp   */
1919       { 32,  32,  32 }, /* 16bpp  */
1920       { 32,  32,  16 }, /* 32bpp  */
1921       { 32,  16,  16 }, /* 64bpp  */
1922       { 16,  16,  16 }, /* 128bpp */
1923    };
1924    /* Only support one type of page size. */
1925    if (offset != 0)
1926       return 0;
1927 
1928    /* reject multisample if 2x isn't supported; assume none are */
1929    if (multi_sample && !screen->info.feats.features.sparseResidency2Samples)
1930       return 0;
1931 
1932    VkFormat format = zink_get_format(screen, pformat);
1933    bool is_zs = util_format_is_depth_or_stencil(pformat);
1934    VkImageType type;
1935    switch (target) {
1936    case PIPE_TEXTURE_1D:
1937    case PIPE_TEXTURE_1D_ARRAY:
1938       type = (screen->need_2D_sparse || (screen->need_2D_zs && is_zs)) ? VK_IMAGE_TYPE_2D : VK_IMAGE_TYPE_1D;
1939       break;
1940 
1941    case PIPE_TEXTURE_2D:
1942    case PIPE_TEXTURE_CUBE:
1943    case PIPE_TEXTURE_RECT:
1944    case PIPE_TEXTURE_2D_ARRAY:
1945    case PIPE_TEXTURE_CUBE_ARRAY:
1946       type = VK_IMAGE_TYPE_2D;
1947       break;
1948 
1949    case PIPE_TEXTURE_3D:
1950       type = VK_IMAGE_TYPE_3D;
1951       break;
1952 
1953    case PIPE_BUFFER:
1954       goto hack_it_up;
1955 
1956    default:
1957       return 0;
1958    }
1959    VkImageUsageFlags flags = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1960                              VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1961    flags |= is_zs ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1962    VkSparseImageFormatProperties props[4]; //planar?
1963    unsigned prop_count = ARRAY_SIZE(props);
1964    VKSCR(GetPhysicalDeviceSparseImageFormatProperties)(screen->pdev, format, type,
1965                                                        multi_sample ? VK_SAMPLE_COUNT_2_BIT : VK_SAMPLE_COUNT_1_BIT,
1966                                                        flags,
1967                                                        VK_IMAGE_TILING_OPTIMAL,
1968                                                        &prop_count, props);
1969    if (!prop_count) {
1970       if (pformat == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1971          screen->faked_e5sparse = true;
1972          goto hack_it_up;
1973       }
1974       return 0;
1975    }
1976 
1977    if (size) {
1978       if (x)
1979          *x = props[0].imageGranularity.width;
1980       if (y)
1981          *y = props[0].imageGranularity.height;
1982       if (z)
1983          *z = props[0].imageGranularity.depth;
1984    }
1985 
1986    return 1;
1987 hack_it_up:
1988    {
1989       const int (*page_sizes)[3] = target == PIPE_TEXTURE_3D ? page_size_3d : page_size_2d;
1990       int blk_size = util_format_get_blocksize(pformat);
1991 
1992       if (size) {
1993          unsigned index = util_logbase2(blk_size);
1994          if (x) *x = page_sizes[index][0];
1995          if (y) *y = page_sizes[index][1];
1996          if (z) *z = page_sizes[index][2];
1997       }
1998    }
1999    return 1;
2000 }
2001 
2002 static VkDevice
zink_create_logical_device(struct zink_screen * screen)2003 zink_create_logical_device(struct zink_screen *screen)
2004 {
2005    VkDevice dev = VK_NULL_HANDLE;
2006 
2007    VkDeviceQueueCreateInfo qci = {0};
2008    float dummy = 0.0f;
2009    qci.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
2010    qci.queueFamilyIndex = screen->gfx_queue;
2011    qci.queueCount = screen->threaded && screen->max_queues > 1 ? 2 : 1;
2012    qci.pQueuePriorities = &dummy;
2013 
2014    VkDeviceCreateInfo dci = {0};
2015    dci.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
2016    dci.queueCreateInfoCount = 1;
2017    dci.pQueueCreateInfos = &qci;
2018    /* extensions don't have bool members in pEnabledFeatures.
2019     * this requires us to pass the whole VkPhysicalDeviceFeatures2 struct
2020     */
2021    if (screen->info.feats.sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
2022       dci.pNext = &screen->info.feats;
2023    } else {
2024       dci.pEnabledFeatures = &screen->info.feats.features;
2025    }
2026 
2027    dci.ppEnabledExtensionNames = screen->info.extensions;
2028    dci.enabledExtensionCount = screen->info.num_extensions;
2029 
2030    VkResult result = VKSCR(CreateDevice)(screen->pdev, &dci, NULL, &dev);
2031    if (result != VK_SUCCESS)
2032       mesa_loge("ZINK: vkCreateDevice failed (%s)", vk_Result_to_str(result));
2033 
2034    return dev;
2035 }
2036 
2037 static void
pre_hash_descriptor_states(struct zink_screen * screen)2038 pre_hash_descriptor_states(struct zink_screen *screen)
2039 {
2040    VkImageViewCreateInfo null_info = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
2041    VkBufferViewCreateInfo null_binfo = {.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO};
2042    screen->null_descriptor_hashes.image_view = _mesa_hash_data(&null_info, sizeof(VkImageViewCreateInfo));
2043    screen->null_descriptor_hashes.buffer_view = _mesa_hash_data(&null_binfo, sizeof(VkBufferViewCreateInfo));
2044 }
2045 
2046 static void
check_base_requirements(struct zink_screen * screen)2047 check_base_requirements(struct zink_screen *screen)
2048 {
2049    if (!screen->info.feats.features.logicOp ||
2050        !screen->info.feats.features.fillModeNonSolid ||
2051        !screen->info.feats.features.shaderClipDistance ||
2052        !(screen->info.feats12.scalarBlockLayout ||
2053          screen->info.have_EXT_scalar_block_layout) ||
2054        !screen->info.have_KHR_maintenance1 ||
2055        !screen->info.have_EXT_custom_border_color ||
2056        !screen->info.have_EXT_line_rasterization) {
2057       fprintf(stderr, "WARNING: Some incorrect rendering "
2058               "might occur because the selected Vulkan device (%s) doesn't support "
2059               "base Zink requirements: ", screen->info.props.deviceName);
2060 #define CHECK_OR_PRINT(X) \
2061       if (!screen->info.X) \
2062          fprintf(stderr, "%s ", #X)
2063       CHECK_OR_PRINT(feats.features.logicOp);
2064       CHECK_OR_PRINT(feats.features.fillModeNonSolid);
2065       CHECK_OR_PRINT(feats.features.shaderClipDistance);
2066       if (!screen->info.feats12.scalarBlockLayout && !screen->info.have_EXT_scalar_block_layout)
2067          printf("scalarBlockLayout OR EXT_scalar_block_layout ");
2068       CHECK_OR_PRINT(have_KHR_maintenance1);
2069       CHECK_OR_PRINT(have_EXT_custom_border_color);
2070       CHECK_OR_PRINT(have_EXT_line_rasterization);
2071       fprintf(stderr, "\n");
2072    }
2073 }
2074 
2075 static void
zink_get_sample_pixel_grid(struct pipe_screen * pscreen,unsigned sample_count,unsigned * width,unsigned * height)2076 zink_get_sample_pixel_grid(struct pipe_screen *pscreen, unsigned sample_count,
2077                            unsigned *width, unsigned *height)
2078 {
2079    struct zink_screen *screen = zink_screen(pscreen);
2080    unsigned idx = util_logbase2_ceil(MAX2(sample_count, 1));
2081    assert(idx < ARRAY_SIZE(screen->maxSampleLocationGridSize));
2082    *width = screen->maxSampleLocationGridSize[idx].width;
2083    *height = screen->maxSampleLocationGridSize[idx].height;
2084 }
2085 
2086 static void
init_driver_workarounds(struct zink_screen * screen)2087 init_driver_workarounds(struct zink_screen *screen)
2088 {
2089    /* enable implicit sync for all non-mesa drivers */
2090    screen->driver_workarounds.implicit_sync = true;
2091    switch (screen->info.driver_props.driverID) {
2092    case VK_DRIVER_ID_MESA_RADV:
2093    case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
2094    case VK_DRIVER_ID_MESA_LLVMPIPE:
2095    case VK_DRIVER_ID_MESA_TURNIP:
2096    case VK_DRIVER_ID_MESA_V3DV:
2097    case VK_DRIVER_ID_MESA_PANVK:
2098    case VK_DRIVER_ID_MESA_VENUS:
2099       screen->driver_workarounds.implicit_sync = false;
2100       break;
2101    default:
2102       break;
2103    }
2104 
2105    screen->driver_workarounds.color_write_missing =
2106       !screen->info.have_EXT_color_write_enable ||
2107       !screen->info.cwrite_feats.colorWriteEnable;
2108 
2109    screen->driver_workarounds.depth_clip_control_missing = !screen->info.have_EXT_depth_clip_control;
2110    if (screen->info.driver_props.driverID == VK_DRIVER_ID_AMD_PROPRIETARY)
2111       /* this completely breaks xfb somehow */
2112       screen->info.have_EXT_extended_dynamic_state2 = false;
2113    if (screen->info.driver_props.driverID == VK_DRIVER_ID_MESA_TURNIP) {
2114       /* performance */
2115       screen->info.border_color_feats.customBorderColorWithoutFormat = VK_FALSE;
2116    }
2117    if (screen->info.driver_props.driverID == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
2118        screen->info.driver_props.driverID == VK_DRIVER_ID_AMD_PROPRIETARY ||
2119        screen->info.driver_props.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY ||
2120        screen->info.driver_props.driverID == VK_DRIVER_ID_MESA_RADV)
2121       screen->driver_workarounds.z24_unscaled_bias = 1<<23;
2122    else
2123       screen->driver_workarounds.z24_unscaled_bias = 1<<24;
2124    if (screen->info.driver_props.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY)
2125       screen->driver_workarounds.z16_unscaled_bias = 1<<15;
2126    else
2127       screen->driver_workarounds.z16_unscaled_bias = 1<<16;
2128 }
2129 
2130 static struct zink_screen *
zink_internal_create_screen(const struct pipe_screen_config * config)2131 zink_internal_create_screen(const struct pipe_screen_config *config)
2132 {
2133    if (getenv("ZINK_USE_LAVAPIPE")) {
2134       mesa_loge("ZINK_USE_LAVAPIPE is obsolete. Use LIBGL_ALWAYS_SOFTWARE\n");
2135       return NULL;
2136    }
2137 
2138    struct zink_screen *screen = rzalloc(NULL, struct zink_screen);
2139    if (!screen)
2140       return NULL;
2141 
2142    screen->threaded = util_get_cpu_caps()->nr_cpus > 1 && debug_get_bool_option("GALLIUM_THREAD", util_get_cpu_caps()->nr_cpus > 1);
2143    screen->abort_on_hang = debug_get_bool_option("ZINK_HANG_ABORT", false);
2144 
2145    zink_debug = debug_get_option_zink_debug();
2146    zink_descriptor_mode = debug_get_option_zink_descriptor_mode();
2147    if (zink_descriptor_mode > ZINK_DESCRIPTOR_MODE_NOTEMPLATES) {
2148       printf("Specify exactly one descriptor mode.\n");
2149       abort();
2150    }
2151 
2152    screen->loader_lib = util_dl_open(VK_LIBNAME);
2153    if (!screen->loader_lib)
2154       goto fail;
2155 
2156    screen->vk_GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)util_dl_get_proc_address(screen->loader_lib, "vkGetInstanceProcAddr");
2157    screen->vk_GetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)util_dl_get_proc_address(screen->loader_lib, "vkGetDeviceProcAddr");
2158    if (!screen->vk_GetInstanceProcAddr ||
2159        !screen->vk_GetDeviceProcAddr)
2160       goto fail;
2161 
2162    screen->instance_info.loader_version = zink_get_loader_version(screen);
2163 #if WITH_XMLCONFIG
2164    if (config) {
2165       driParseConfigFiles(config->options, config->options_info, 0, "zink",
2166                           NULL, NULL, NULL, 0, NULL, 0);
2167       screen->driconf.dual_color_blend_by_location = driQueryOptionb(config->options, "dual_color_blend_by_location");
2168       //screen->driconf.inline_uniforms = driQueryOptionb(config->options, "radeonsi_inline_uniforms");
2169       screen->instance_info.disable_xcb_surface = driQueryOptionb(config->options, "disable_xcb_surface");
2170    }
2171 #endif
2172 
2173    if (!zink_create_instance(screen))
2174       goto fail;
2175 
2176    vk_instance_dispatch_table_load(&screen->vk.instance,
2177                                    screen->vk_GetInstanceProcAddr,
2178                                    screen->instance);
2179    vk_physical_device_dispatch_table_load(&screen->vk.physical_device,
2180                                           screen->vk_GetInstanceProcAddr,
2181                                           screen->instance);
2182 
2183    zink_verify_instance_extensions(screen);
2184 
2185    if (screen->instance_info.have_EXT_debug_utils &&
2186       (zink_debug & ZINK_DEBUG_VALIDATION) && !create_debug(screen))
2187       debug_printf("ZINK: failed to setup debug utils\n");
2188 
2189    screen->is_cpu = choose_pdev(screen);
2190    if (screen->pdev == VK_NULL_HANDLE)
2191       goto fail;
2192 
2193    update_queue_props(screen);
2194 
2195    screen->have_X8_D24_UNORM_PACK32 = zink_is_depth_format_supported(screen,
2196                                               VK_FORMAT_X8_D24_UNORM_PACK32);
2197    screen->have_D24_UNORM_S8_UINT = zink_is_depth_format_supported(screen,
2198                                               VK_FORMAT_D24_UNORM_S8_UINT);
2199    screen->have_D32_SFLOAT_S8_UINT = zink_is_depth_format_supported(screen,
2200                                               VK_FORMAT_D32_SFLOAT_S8_UINT);
2201 
2202    if (!zink_get_physical_device_info(screen)) {
2203       debug_printf("ZINK: failed to detect features\n");
2204       goto fail;
2205    }
2206 
2207    if (screen->threaded && !util_queue_init(&screen->flush_queue, "zfq", 8, 1, UTIL_QUEUE_INIT_RESIZE_IF_FULL, screen)) {
2208       mesa_loge("zink: Failed to create flush queue.\n");
2209       goto fail;
2210    }
2211 
2212    zink_internal_setup_moltenvk(screen);
2213    if (!screen->info.have_KHR_timeline_semaphore) {
2214       mesa_loge("zink: KHR_timeline_semaphore is required");
2215       goto fail;
2216    }
2217 
2218    init_driver_workarounds(screen);
2219 
2220    screen->dev = zink_create_logical_device(screen);
2221    if (!screen->dev)
2222       goto fail;
2223 
2224    vk_device_dispatch_table_load(&screen->vk.device,
2225                                  screen->vk_GetDeviceProcAddr,
2226                                  screen->dev);
2227 
2228    init_queue(screen);
2229    if (screen->info.driver_props.driverID == VK_DRIVER_ID_MESA_RADV ||
2230        screen->info.driver_props.driverID == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
2231        screen->info.driver_props.driverID == VK_DRIVER_ID_AMD_PROPRIETARY)
2232       /* this has bad perf on AMD */
2233       screen->info.have_KHR_push_descriptor = false;
2234 
2235    zink_verify_device_extensions(screen);
2236 
2237    if ((zink_debug & ZINK_DEBUG_COMPACT) ||
2238        screen->info.props.limits.maxBoundDescriptorSets < ZINK_MAX_DESCRIPTOR_SETS) {
2239       screen->desc_set_id[ZINK_DESCRIPTOR_TYPES] = 0;
2240       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_UBO] = 1;
2241       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_SSBO] = 1;
2242       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW] = 2;
2243       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_IMAGE] = 2;
2244       screen->desc_set_id[ZINK_DESCRIPTOR_BINDLESS] = 3;
2245       screen->compact_descriptors = true;
2246    } else {
2247       screen->desc_set_id[ZINK_DESCRIPTOR_TYPES] = 0;
2248       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_UBO] = 1;
2249       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_SAMPLER_VIEW] = 2;
2250       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_SSBO] = 3;
2251       screen->desc_set_id[ZINK_DESCRIPTOR_TYPE_IMAGE] = 4;
2252       screen->desc_set_id[ZINK_DESCRIPTOR_BINDLESS] = 5;
2253    }
2254    if (zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_AUTO) {
2255       if (screen->info.have_KHR_descriptor_update_template)
2256          zink_descriptor_mode = ZINK_DESCRIPTOR_MODE_LAZY;
2257       else
2258          zink_descriptor_mode = ZINK_DESCRIPTOR_MODE_CACHED;
2259    }
2260 
2261    if (screen->info.have_EXT_calibrated_timestamps && !check_have_device_time(screen))
2262       goto fail;
2263 
2264    screen->have_triangle_fans = true;
2265 #if defined(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME)
2266    if (screen->info.have_KHR_portability_subset) {
2267       screen->have_triangle_fans = (VK_TRUE == screen->info.portability_subset_feats.triangleFans);
2268    }
2269 #endif // VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME
2270 
2271    check_base_requirements(screen);
2272    util_live_shader_cache_init(&screen->shaders, zink_create_gfx_shader_state, zink_delete_shader_state);
2273 
2274    screen->base.get_name = zink_get_name;
2275    if (screen->instance_info.have_KHR_external_memory_capabilities) {
2276       screen->base.get_device_uuid = zink_get_device_uuid;
2277       screen->base.get_driver_uuid = zink_get_driver_uuid;
2278    }
2279    if (screen->info.have_KHR_external_memory_win32) {
2280       screen->base.get_device_luid = zink_get_device_luid;
2281       screen->base.get_device_node_mask = zink_get_device_node_mask;
2282    }
2283    screen->base.get_vendor = zink_get_vendor;
2284    screen->base.get_device_vendor = zink_get_device_vendor;
2285    screen->base.get_compute_param = zink_get_compute_param;
2286    screen->base.get_timestamp = zink_get_timestamp;
2287    screen->base.query_memory_info = zink_query_memory_info;
2288    screen->base.get_param = zink_get_param;
2289    screen->base.get_paramf = zink_get_paramf;
2290    screen->base.get_shader_param = zink_get_shader_param;
2291    screen->base.get_compiler_options = zink_get_compiler_options;
2292    screen->base.get_sample_pixel_grid = zink_get_sample_pixel_grid;
2293    screen->base.is_compute_copy_faster = zink_is_compute_copy_faster;
2294    screen->base.is_format_supported = zink_is_format_supported;
2295    if (screen->info.have_EXT_image_drm_format_modifier && screen->info.have_EXT_external_memory_dma_buf) {
2296       screen->base.query_dmabuf_modifiers = zink_query_dmabuf_modifiers;
2297       screen->base.is_dmabuf_modifier_supported = zink_is_dmabuf_modifier_supported;
2298       screen->base.get_dmabuf_modifier_planes = zink_get_dmabuf_modifier_planes;
2299    }
2300 #if defined(_WIN32)
2301    if (screen->info.have_KHR_external_memory_win32)
2302       screen->base.create_fence_win32 = zink_create_fence_win32;
2303 #endif
2304    screen->base.context_create = zink_context_create;
2305    screen->base.flush_frontbuffer = zink_flush_frontbuffer;
2306    screen->base.destroy = zink_destroy_screen;
2307    screen->base.finalize_nir = zink_shader_finalize;
2308    screen->base.get_sparse_texture_virtual_page_size = zink_get_sparse_texture_virtual_page_size;
2309 
2310    if (screen->info.have_EXT_sample_locations) {
2311       VkMultisamplePropertiesEXT prop;
2312       prop.sType = VK_STRUCTURE_TYPE_MULTISAMPLE_PROPERTIES_EXT;
2313       prop.pNext = NULL;
2314       for (unsigned i = 0; i < ARRAY_SIZE(screen->maxSampleLocationGridSize); i++) {
2315          if (screen->info.sample_locations_props.sampleLocationSampleCounts & (1 << i)) {
2316             VKSCR(GetPhysicalDeviceMultisamplePropertiesEXT)(screen->pdev, 1 << i, &prop);
2317             screen->maxSampleLocationGridSize[i] = prop.maxSampleLocationGridSize;
2318          }
2319       }
2320    }
2321 
2322    if (!zink_screen_resource_init(&screen->base))
2323       goto fail;
2324    zink_bo_init(screen);
2325    zink_screen_fence_init(&screen->base);
2326 
2327    zink_screen_init_compiler(screen);
2328    if (!disk_cache_init(screen))
2329       goto fail;
2330    populate_format_props(screen);
2331    pre_hash_descriptor_states(screen);
2332 
2333    slab_create_parent(&screen->transfer_pool, sizeof(struct zink_transfer), 16);
2334 
2335    screen->driconf.inline_uniforms = debug_get_bool_option("ZINK_INLINE_UNIFORMS", screen->is_cpu);
2336 
2337    screen->total_video_mem = get_video_mem(screen);
2338    screen->clamp_video_mem = screen->total_video_mem * 0.8;
2339    if (!os_get_total_physical_memory(&screen->total_mem))
2340       goto fail;
2341 
2342    if (!zink_screen_init_semaphore(screen)) {
2343       mesa_loge("zink: failed to create timeline semaphore");
2344       goto fail;
2345    }
2346 
2347    memset(&screen->heap_map, UINT8_MAX, sizeof(screen->heap_map));
2348    for (enum zink_heap i = 0; i < ZINK_HEAP_MAX; i++) {
2349       for (unsigned j = 0; j < screen->info.mem_props.memoryTypeCount; j++) {
2350          VkMemoryPropertyFlags domains = vk_domain_from_heap(i);
2351          if ((screen->info.mem_props.memoryTypes[j].propertyFlags & domains) == domains) {
2352             assert(screen->heap_map[i] == UINT8_MAX);
2353             screen->heap_map[i] = j;
2354             break;
2355          }
2356       }
2357 
2358       /* not found: use compatible heap */
2359       if (screen->heap_map[i] == UINT8_MAX) {
2360          /* only cached mem has a failure case for now */
2361          assert(i == ZINK_HEAP_HOST_VISIBLE_CACHED || i == ZINK_HEAP_DEVICE_LOCAL_LAZY ||
2362                 i == ZINK_HEAP_DEVICE_LOCAL_VISIBLE);
2363          if (i == ZINK_HEAP_HOST_VISIBLE_CACHED)
2364             screen->heap_map[i] = screen->heap_map[ZINK_HEAP_HOST_VISIBLE_COHERENT];
2365          else
2366             screen->heap_map[i] = screen->heap_map[ZINK_HEAP_DEVICE_LOCAL];
2367       }
2368       screen->heap_flags[i] = screen->info.mem_props.memoryTypes[screen->heap_map[i]].propertyFlags;
2369    }
2370    {
2371       unsigned vis_vram = screen->heap_map[ZINK_HEAP_DEVICE_LOCAL_VISIBLE];
2372       unsigned vram = screen->heap_map[ZINK_HEAP_DEVICE_LOCAL];
2373       /* determine if vis vram is roughly equal to total vram */
2374       if (screen->info.mem_props.memoryHeaps[screen->info.mem_props.memoryTypes[vis_vram].heapIndex].size >
2375           screen->info.mem_props.memoryHeaps[screen->info.mem_props.memoryTypes[vram].heapIndex].size * 0.9)
2376          screen->resizable_bar = true;
2377    }
2378 
2379    simple_mtx_init(&screen->dt_lock, mtx_plain);
2380 
2381    zink_screen_init_descriptor_funcs(screen, false);
2382    util_idalloc_mt_init_tc(&screen->buffer_ids);
2383 
2384    util_vertex_state_cache_init(&screen->vertex_state_cache,
2385                                 zink_create_vertex_state, zink_vertex_state_destroy);
2386    screen->base.create_vertex_state = zink_cache_create_vertex_state;
2387    screen->base.vertex_state_destroy = zink_cache_vertex_state_destroy;
2388    glsl_type_singleton_init_or_ref();
2389 
2390    screen->copy_context = zink_context(screen->base.context_create(&screen->base, NULL, ZINK_CONTEXT_COPY_ONLY));
2391    if (!screen->copy_context) {
2392       mesa_loge("zink: failed to create copy context");
2393       goto fail;
2394    }
2395 
2396    return screen;
2397 
2398 fail:
2399    if (screen->loader_lib)
2400       util_dl_close(screen->loader_lib);
2401    if (screen->threaded)
2402       util_queue_destroy(&screen->flush_queue);
2403 
2404    ralloc_free(screen);
2405    return NULL;
2406 }
2407 
2408 struct pipe_screen *
zink_create_screen(struct sw_winsys * winsys,const struct pipe_screen_config * config)2409 zink_create_screen(struct sw_winsys *winsys, const struct pipe_screen_config *config)
2410 {
2411    struct zink_screen *ret = zink_internal_create_screen(config);
2412    if (ret) {
2413       ret->drm_fd = -1;
2414    }
2415 
2416    return &ret->base;
2417 }
2418 
2419 struct pipe_screen *
zink_drm_create_screen(int fd,const struct pipe_screen_config * config)2420 zink_drm_create_screen(int fd, const struct pipe_screen_config *config)
2421 {
2422    struct zink_screen *ret = zink_internal_create_screen(config);
2423 
2424    if (ret)
2425       ret->drm_fd = os_dupfd_cloexec(fd);
2426    if (ret && !ret->info.have_KHR_external_memory_fd) {
2427       debug_printf("ZINK: KHR_external_memory_fd required!\n");
2428       zink_destroy_screen(&ret->base);
2429       return NULL;
2430    }
2431 
2432    return &ret->base;
2433 }
2434 
zink_stub_function_not_loaded()2435 void zink_stub_function_not_loaded()
2436 {
2437    /* this will be used by the zink_verify_*_extensions() functions on a
2438     * release build
2439     */
2440    mesa_loge("ZINK: a Vulkan function was called without being loaded");
2441    abort();
2442 }
2443