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