1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci/**
25bf215546Sopenharmony_ci * \file
26bf215546Sopenharmony_ci * \brief SPIRV-V extension handling. See ARB_spirv_extensions
27bf215546Sopenharmony_ci */
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include <stdbool.h>
30bf215546Sopenharmony_ci#include "spirv_extensions.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ciGLuint
33bf215546Sopenharmony_ci_mesa_get_spirv_extension_count(struct gl_context *ctx)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   if (ctx->Const.SpirVExtensions == NULL)
36bf215546Sopenharmony_ci      return 0;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   return ctx->Const.SpirVExtensions->count;
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ciconst GLubyte *
42bf215546Sopenharmony_ci_mesa_get_enabled_spirv_extension(struct gl_context *ctx,
43bf215546Sopenharmony_ci                                  GLuint index)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   unsigned int n = 0;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   if (ctx->Const.SpirVExtensions == NULL)
48bf215546Sopenharmony_ci      return (const GLubyte *) 0;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   for (unsigned int i = 0; i < SPV_EXTENSIONS_COUNT; i++) {
51bf215546Sopenharmony_ci      if (ctx->Const.SpirVExtensions->supported[i]) {
52bf215546Sopenharmony_ci         if (n == index)
53bf215546Sopenharmony_ci            return (const GLubyte *) _mesa_spirv_extensions_to_string(i);
54bf215546Sopenharmony_ci         else
55bf215546Sopenharmony_ci            n++;
56bf215546Sopenharmony_ci      }
57bf215546Sopenharmony_ci   }
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   return (const GLubyte *) 0;
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ciconst char *
63bf215546Sopenharmony_ci_mesa_spirv_extensions_to_string(enum SpvExtension ext)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci#define STR(x) case x: return #x;
66bf215546Sopenharmony_ci   switch (ext) {
67bf215546Sopenharmony_ci   STR(SPV_KHR_16bit_storage);
68bf215546Sopenharmony_ci   STR(SPV_KHR_device_group);
69bf215546Sopenharmony_ci   STR(SPV_KHR_multiview);
70bf215546Sopenharmony_ci   STR(SPV_KHR_shader_ballot);
71bf215546Sopenharmony_ci   STR(SPV_KHR_shader_draw_parameters);
72bf215546Sopenharmony_ci   STR(SPV_KHR_storage_buffer_storage_class);
73bf215546Sopenharmony_ci   STR(SPV_KHR_subgroup_vote);
74bf215546Sopenharmony_ci   STR(SPV_KHR_variable_pointers);
75bf215546Sopenharmony_ci   STR(SPV_AMD_gcn_shader);
76bf215546Sopenharmony_ci   case SPV_EXTENSIONS_COUNT:
77bf215546Sopenharmony_ci      unreachable("Unknown SPIR-V extension");
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci#undef STR
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   return "unknown";
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci/**
85bf215546Sopenharmony_ci * Sets the supported flags for known SPIR-V extensions based on the
86bf215546Sopenharmony_ci * capabilites supported (spirv capabilities based on the spirv to nir
87bf215546Sopenharmony_ci * support).
88bf215546Sopenharmony_ci *
89bf215546Sopenharmony_ci * One could argue that makes more sense in the other way around, as from the
90bf215546Sopenharmony_ci * spec pov capabilities are enable for a given extension. But from our pov,
91bf215546Sopenharmony_ci * we support or not (depending on the driver) some given capability, and
92bf215546Sopenharmony_ci * spirv_to_nir check for capabilities not extensions. Also we usually fill
93bf215546Sopenharmony_ci * first the supported capabilities, that are not always related to an
94bf215546Sopenharmony_ci * extension.
95bf215546Sopenharmony_ci */
96bf215546Sopenharmony_civoid
97bf215546Sopenharmony_ci_mesa_fill_supported_spirv_extensions(struct spirv_supported_extensions *ext,
98bf215546Sopenharmony_ci                                      const struct spirv_supported_capabilities *cap)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   memset(ext->supported, 0, sizeof(ext->supported));
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   ext->count = 0;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   ext->supported[SPV_KHR_shader_draw_parameters] = cap->draw_parameters;
105bf215546Sopenharmony_ci   ext->supported[SPV_KHR_multiview] = cap->multiview;
106bf215546Sopenharmony_ci   ext->supported[SPV_KHR_storage_buffer_storage_class] = true;
107bf215546Sopenharmony_ci   ext->supported[SPV_KHR_variable_pointers] = cap->variable_pointers;
108bf215546Sopenharmony_ci   ext->supported[SPV_AMD_gcn_shader] = cap->amd_gcn_shader;
109bf215546Sopenharmony_ci   ext->supported[SPV_KHR_shader_ballot] = cap->subgroup_ballot;
110bf215546Sopenharmony_ci   ext->supported[SPV_KHR_subgroup_vote] = cap->subgroup_vote;
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci   for (unsigned i = 0; i < SPV_EXTENSIONS_COUNT; i++) {
113bf215546Sopenharmony_ci      if (ext->supported[i])
114bf215546Sopenharmony_ci         ext->count++;
115bf215546Sopenharmony_ci   }
116bf215546Sopenharmony_ci}
117