xref: /aosp_15_r20/external/mesa3d/src/vulkan/util/vk_util.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2016 Red Hat.
3*61046927SAndroid Build Coastguard Worker  * Copyright © 2016 Bas Nieuwenhuizen
4*61046927SAndroid Build Coastguard Worker  * Copyright © 2017 Intel Corporation
5*61046927SAndroid Build Coastguard Worker  *
6*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
8*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
9*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
11*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
12*61046927SAndroid Build Coastguard Worker  *
13*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
14*61046927SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
15*61046927SAndroid Build Coastguard Worker  * Software.
16*61046927SAndroid Build Coastguard Worker  *
17*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21*61046927SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22*61046927SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23*61046927SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
24*61046927SAndroid Build Coastguard Worker  */
25*61046927SAndroid Build Coastguard Worker 
26*61046927SAndroid Build Coastguard Worker #include <stdio.h>
27*61046927SAndroid Build Coastguard Worker #include <stdlib.h>
28*61046927SAndroid Build Coastguard Worker #include <string.h>
29*61046927SAndroid Build Coastguard Worker #include "vk_util.h"
30*61046927SAndroid Build Coastguard Worker #include "util/u_debug.h"
31*61046927SAndroid Build Coastguard Worker 
32*61046927SAndroid Build Coastguard Worker #include "compiler/spirv/nir_spirv.h"
33*61046927SAndroid Build Coastguard Worker 
vk_get_driver_version(void)34*61046927SAndroid Build Coastguard Worker uint32_t vk_get_driver_version(void)
35*61046927SAndroid Build Coastguard Worker {
36*61046927SAndroid Build Coastguard Worker    const char *minor_string = strchr(PACKAGE_VERSION, '.');
37*61046927SAndroid Build Coastguard Worker    const char *patch_string = minor_string ? strchr(minor_string + 1, '.') : NULL;
38*61046927SAndroid Build Coastguard Worker    int major = atoi(PACKAGE_VERSION);
39*61046927SAndroid Build Coastguard Worker    int minor = minor_string ? atoi(minor_string + 1) : 0;
40*61046927SAndroid Build Coastguard Worker    int patch = patch_string ? atoi(patch_string + 1) : 0;
41*61046927SAndroid Build Coastguard Worker    if (strstr(PACKAGE_VERSION, "devel")) {
42*61046927SAndroid Build Coastguard Worker       if (patch == 0) {
43*61046927SAndroid Build Coastguard Worker          patch = 99;
44*61046927SAndroid Build Coastguard Worker          if (minor == 0) {
45*61046927SAndroid Build Coastguard Worker             minor = 99;
46*61046927SAndroid Build Coastguard Worker             --major;
47*61046927SAndroid Build Coastguard Worker          } else
48*61046927SAndroid Build Coastguard Worker             --minor;
49*61046927SAndroid Build Coastguard Worker       } else
50*61046927SAndroid Build Coastguard Worker          --patch;
51*61046927SAndroid Build Coastguard Worker    }
52*61046927SAndroid Build Coastguard Worker    return VK_MAKE_VERSION(major, minor, patch);
53*61046927SAndroid Build Coastguard Worker }
54*61046927SAndroid Build Coastguard Worker 
vk_get_version_override(void)55*61046927SAndroid Build Coastguard Worker uint32_t vk_get_version_override(void)
56*61046927SAndroid Build Coastguard Worker {
57*61046927SAndroid Build Coastguard Worker    const char *str = getenv("MESA_VK_VERSION_OVERRIDE");
58*61046927SAndroid Build Coastguard Worker    if (str == NULL)
59*61046927SAndroid Build Coastguard Worker       return 0;
60*61046927SAndroid Build Coastguard Worker 
61*61046927SAndroid Build Coastguard Worker    const char *minor_str = strchr(str, '.');
62*61046927SAndroid Build Coastguard Worker    const char *patch_str = minor_str ? strchr(minor_str + 1, '.') : NULL;
63*61046927SAndroid Build Coastguard Worker 
64*61046927SAndroid Build Coastguard Worker    int major = atoi(str);
65*61046927SAndroid Build Coastguard Worker    int minor = minor_str ? atoi(minor_str + 1) : 0;
66*61046927SAndroid Build Coastguard Worker    int patch = patch_str ? atoi(patch_str + 1) : VK_HEADER_VERSION;
67*61046927SAndroid Build Coastguard Worker 
68*61046927SAndroid Build Coastguard Worker    /* Do some basic version sanity checking */
69*61046927SAndroid Build Coastguard Worker    if (major < 1 || minor < 0 || patch < 0 || minor > 1023 || patch > 4095)
70*61046927SAndroid Build Coastguard Worker       return 0;
71*61046927SAndroid Build Coastguard Worker 
72*61046927SAndroid Build Coastguard Worker    return VK_MAKE_VERSION(major, minor, patch);
73*61046927SAndroid Build Coastguard Worker }
74*61046927SAndroid Build Coastguard Worker 
75*61046927SAndroid Build Coastguard Worker void
vk_warn_non_conformant_implementation(const char * driver_name)76*61046927SAndroid Build Coastguard Worker vk_warn_non_conformant_implementation(const char *driver_name)
77*61046927SAndroid Build Coastguard Worker {
78*61046927SAndroid Build Coastguard Worker    if (debug_get_bool_option("MESA_VK_IGNORE_CONFORMANCE_WARNING", false))
79*61046927SAndroid Build Coastguard Worker       return;
80*61046927SAndroid Build Coastguard Worker 
81*61046927SAndroid Build Coastguard Worker    fprintf(stderr, "WARNING: %s is not a conformant Vulkan implementation, "
82*61046927SAndroid Build Coastguard Worker                    "testing use only.\n", driver_name);
83*61046927SAndroid Build Coastguard Worker }
84*61046927SAndroid Build Coastguard Worker 
85*61046927SAndroid Build Coastguard Worker struct nir_spirv_specialization*
vk_spec_info_to_nir_spirv(const VkSpecializationInfo * spec_info,uint32_t * out_num_spec_entries)86*61046927SAndroid Build Coastguard Worker vk_spec_info_to_nir_spirv(const VkSpecializationInfo *spec_info,
87*61046927SAndroid Build Coastguard Worker                           uint32_t *out_num_spec_entries)
88*61046927SAndroid Build Coastguard Worker {
89*61046927SAndroid Build Coastguard Worker    if (spec_info == NULL || spec_info->mapEntryCount == 0)
90*61046927SAndroid Build Coastguard Worker       return NULL;
91*61046927SAndroid Build Coastguard Worker 
92*61046927SAndroid Build Coastguard Worker    uint32_t num_spec_entries = spec_info->mapEntryCount;
93*61046927SAndroid Build Coastguard Worker    struct nir_spirv_specialization *spec_entries =
94*61046927SAndroid Build Coastguard Worker       calloc(num_spec_entries, sizeof(*spec_entries));
95*61046927SAndroid Build Coastguard Worker 
96*61046927SAndroid Build Coastguard Worker    for (uint32_t i = 0; i < num_spec_entries; i++) {
97*61046927SAndroid Build Coastguard Worker       VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
98*61046927SAndroid Build Coastguard Worker       const void *data = (uint8_t *)spec_info->pData + entry.offset;
99*61046927SAndroid Build Coastguard Worker       assert((uint8_t *)data + entry.size <=
100*61046927SAndroid Build Coastguard Worker              (uint8_t *)spec_info->pData + spec_info->dataSize);
101*61046927SAndroid Build Coastguard Worker 
102*61046927SAndroid Build Coastguard Worker       spec_entries[i].id = spec_info->pMapEntries[i].constantID;
103*61046927SAndroid Build Coastguard Worker       switch (entry.size) {
104*61046927SAndroid Build Coastguard Worker       case 8:
105*61046927SAndroid Build Coastguard Worker          spec_entries[i].value.u64 = *(const uint64_t *)data;
106*61046927SAndroid Build Coastguard Worker          break;
107*61046927SAndroid Build Coastguard Worker       case 4:
108*61046927SAndroid Build Coastguard Worker          spec_entries[i].value.u32 = *(const uint32_t *)data;
109*61046927SAndroid Build Coastguard Worker          break;
110*61046927SAndroid Build Coastguard Worker       case 2:
111*61046927SAndroid Build Coastguard Worker          spec_entries[i].value.u16 = *(const uint16_t *)data;
112*61046927SAndroid Build Coastguard Worker          break;
113*61046927SAndroid Build Coastguard Worker       case 1:
114*61046927SAndroid Build Coastguard Worker          spec_entries[i].value.u8 = *(const uint8_t *)data;
115*61046927SAndroid Build Coastguard Worker          break;
116*61046927SAndroid Build Coastguard Worker       case 0:
117*61046927SAndroid Build Coastguard Worker       default:
118*61046927SAndroid Build Coastguard Worker          /* The Vulkan spec says:
119*61046927SAndroid Build Coastguard Worker           *
120*61046927SAndroid Build Coastguard Worker           *    "For a constantID specialization constant declared in a
121*61046927SAndroid Build Coastguard Worker           *    shader, size must match the byte size of the constantID. If
122*61046927SAndroid Build Coastguard Worker           *    the specialization constant is of type boolean, size must be
123*61046927SAndroid Build Coastguard Worker           *    the byte size of VkBool32."
124*61046927SAndroid Build Coastguard Worker           *
125*61046927SAndroid Build Coastguard Worker           * Therefore, since only scalars can be decorated as
126*61046927SAndroid Build Coastguard Worker           * specialization constants, we can assume that if it doesn't have
127*61046927SAndroid Build Coastguard Worker           * a size of 1, 2, 4, or 8, any use in a shader would be invalid
128*61046927SAndroid Build Coastguard Worker           * usage.  The spec further says:
129*61046927SAndroid Build Coastguard Worker           *
130*61046927SAndroid Build Coastguard Worker           *    "If a constantID value is not a specialization constant ID
131*61046927SAndroid Build Coastguard Worker           *    used in the shader, that map entry does not affect the
132*61046927SAndroid Build Coastguard Worker           *    behavior of the pipeline."
133*61046927SAndroid Build Coastguard Worker           *
134*61046927SAndroid Build Coastguard Worker           * so we should ignore any invalid specialization constants rather
135*61046927SAndroid Build Coastguard Worker           * than crash or error out when we see one.
136*61046927SAndroid Build Coastguard Worker           */
137*61046927SAndroid Build Coastguard Worker          break;
138*61046927SAndroid Build Coastguard Worker       }
139*61046927SAndroid Build Coastguard Worker    }
140*61046927SAndroid Build Coastguard Worker 
141*61046927SAndroid Build Coastguard Worker    *out_num_spec_entries = num_spec_entries;
142*61046927SAndroid Build Coastguard Worker    return spec_entries;
143*61046927SAndroid Build Coastguard Worker }
144