1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <vulkan/vulkan.h>
27
28 #include "compiler/shader_enums.h"
29 #include "nir/nir.h"
30 #include "nir/nir_builder.h"
31 #include "pvr_private.h"
32 #include "pvr_shader.h"
33 #include "rogue/rogue.h"
34 #include "spirv/nir_spirv.h"
35 #include "vk_format.h"
36 #include "vk_shader_module.h"
37 #include "vk_util.h"
38
39 /**
40 * \file pvr_shader.c
41 *
42 * \brief Contains top-level functions to compile SPIR-V -> NIR -> Rogue, and
43 * interfaces with the compiler.
44 */
45
46 /**
47 * \brief Converts a SPIR-V shader to NIR.
48 *
49 * \param[in] ctx Shared multi-stage build context.
50 * \param[in] stage Shader stage.
51 * \param[in] create_info Shader creation info from Vulkan pipeline.
52 * \return A nir_shader* if successful, or NULL if unsuccessful.
53 */
pvr_spirv_to_nir(rogue_build_ctx * ctx,gl_shader_stage stage,const VkPipelineShaderStageCreateInfo * create_info)54 nir_shader *pvr_spirv_to_nir(rogue_build_ctx *ctx,
55 gl_shader_stage stage,
56 const VkPipelineShaderStageCreateInfo *create_info)
57 {
58 VK_FROM_HANDLE(vk_shader_module, module, create_info->module);
59 struct nir_spirv_specialization *spec;
60 unsigned num_spec = 0;
61 nir_shader *nir;
62
63 spec =
64 vk_spec_info_to_nir_spirv(create_info->pSpecializationInfo, &num_spec);
65
66 nir = rogue_spirv_to_nir(ctx,
67 stage,
68 create_info->pName,
69 module->size / sizeof(uint32_t),
70 (uint32_t *)module->data,
71 num_spec,
72 spec);
73
74 free(spec);
75
76 return nir;
77 }
78
79 /**
80 * \brief Converts a NIR shader to Rogue.
81 *
82 * \param[in] ctx Shared multi-stage build context.
83 * \param[in] nir NIR shader.
84 * \return A rogue_shader* if successful, or NULL if unsuccessful.
85 */
pvr_nir_to_rogue(rogue_build_ctx * ctx,nir_shader * nir)86 rogue_shader *pvr_nir_to_rogue(rogue_build_ctx *ctx, nir_shader *nir)
87 {
88 return rogue_nir_to_rogue(ctx, nir);
89 }
90
91 /**
92 * \brief Converts a Rogue shader to binary.
93 *
94 * \param[in] ctx Shared multi-stage build context.
95 * \param[in] shader Rogue shader.
96 * \param[out] binary Array containing shader binary.
97 */
pvr_rogue_to_binary(rogue_build_ctx * ctx,rogue_shader * shader,struct util_dynarray * binary)98 void pvr_rogue_to_binary(rogue_build_ctx *ctx,
99 rogue_shader *shader,
100 struct util_dynarray *binary)
101 {
102 rogue_encode_shader(ctx, shader, binary);
103 }
104