xref: /third_party/mesa3d/src/vulkan/util/vk_util.c (revision bf215546)
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/debug.h"
31
32#include "compiler/spirv/nir_spirv.h"
33
34uint32_t vk_get_driver_version(void)
35{
36   const char *minor_string = strchr(PACKAGE_VERSION, '.');
37   const char *patch_string = minor_string ? strchr(minor_string + 1, '.') : NULL;
38   int major = atoi(PACKAGE_VERSION);
39   int minor = minor_string ? atoi(minor_string + 1) : 0;
40   int patch = patch_string ? atoi(patch_string + 1) : 0;
41   if (strstr(PACKAGE_VERSION, "devel")) {
42      if (patch == 0) {
43         patch = 99;
44         if (minor == 0) {
45            minor = 99;
46            --major;
47         } else
48            --minor;
49      } else
50         --patch;
51   }
52   return VK_MAKE_VERSION(major, minor, patch);
53}
54
55uint32_t vk_get_version_override(void)
56{
57   const char *str = getenv("MESA_VK_VERSION_OVERRIDE");
58   if (str == NULL)
59      return 0;
60
61   const char *minor_str = strchr(str, '.');
62   const char *patch_str = minor_str ? strchr(minor_str + 1, '.') : NULL;
63
64   int major = atoi(str);
65   int minor = minor_str ? atoi(minor_str + 1) : 0;
66   int patch = patch_str ? atoi(patch_str + 1) : 0;
67
68   /* Do some basic version sanity checking */
69   if (major < 1 || minor < 0 || patch < 0 || minor > 1023 || patch > 4095)
70      return 0;
71
72   return VK_MAKE_VERSION(major, minor, patch);
73}
74
75void
76vk_warn_non_conformant_implementation(const char *driver_name)
77{
78   if (env_var_as_boolean("MESA_VK_IGNORE_CONFORMANCE_WARNING", false))
79      return;
80
81   fprintf(stderr, "WARNING: %s is not a conformant Vulkan implementation, "
82                   "testing use only.\n", driver_name);
83}
84
85struct nir_spirv_specialization*
86vk_spec_info_to_nir_spirv(const VkSpecializationInfo *spec_info,
87                          uint32_t *out_num_spec_entries)
88{
89   if (spec_info == NULL || spec_info->mapEntryCount == 0)
90      return NULL;
91
92   uint32_t num_spec_entries = spec_info->mapEntryCount;
93   struct nir_spirv_specialization *spec_entries =
94      calloc(num_spec_entries, sizeof(*spec_entries));
95
96   for (uint32_t i = 0; i < num_spec_entries; i++) {
97      VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
98      const void *data = (uint8_t *)spec_info->pData + entry.offset;
99      assert((uint8_t *)data + entry.size <=
100             (uint8_t *)spec_info->pData + spec_info->dataSize);
101
102      spec_entries[i].id = spec_info->pMapEntries[i].constantID;
103      switch (entry.size) {
104      case 8:
105         spec_entries[i].value.u64 = *(const uint64_t *)data;
106         break;
107      case 4:
108         spec_entries[i].value.u32 = *(const uint32_t *)data;
109         break;
110      case 2:
111         spec_entries[i].value.u16 = *(const uint16_t *)data;
112         break;
113      case 1:
114         spec_entries[i].value.u8 = *(const uint8_t *)data;
115         break;
116      case 0:
117      default:
118         /* The Vulkan spec says:
119          *
120          *    "For a constantID specialization constant declared in a
121          *    shader, size must match the byte size of the constantID. If
122          *    the specialization constant is of type boolean, size must be
123          *    the byte size of VkBool32."
124          *
125          * Therefore, since only scalars can be decorated as
126          * specialization constants, we can assume that if it doesn't have
127          * a size of 1, 2, 4, or 8, any use in a shader would be invalid
128          * usage.  The spec further says:
129          *
130          *    "If a constantID value is not a specialization constant ID
131          *    used in the shader, that map entry does not affect the
132          *    behavior of the pipeline."
133          *
134          * so we should ignore any invalid specialization constants rather
135          * than crash or error out when we see one.
136          */
137         break;
138      }
139   }
140
141   *out_num_spec_entries = num_spec_entries;
142   return spec_entries;
143}
144