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 "rogue/rogue_shader.h"
35 #include "spirv/nir_spirv.h"
36 #include "vk_format.h"
37 #include "vk_shader_module.h"
38 #include "vk_util.h"
39 
40 /**
41  * \file pvr_shader.c
42  *
43  * \brief Contains top-level functions to compile SPIR-V -> NIR -> Rogue, and
44  * interfaces with the compiler.
45  */
46 
47 /**
48  * \brief Converts a SPIR-V shader to NIR.
49  *
50  * \param[in] ctx Shared multi-stage build context.
51  * \param[in] stage Shader stage.
52  * \param[in] create_info Shader creation info from Vulkan pipeline.
53  * \return A nir_shader* if successful, or NULL if unsuccessful.
54  */
pvr_spirv_to_nir(struct rogue_build_ctx *ctx, gl_shader_stage stage, const VkPipelineShaderStageCreateInfo *create_info)55 nir_shader *pvr_spirv_to_nir(struct rogue_build_ctx *ctx,
56                              gl_shader_stage stage,
57                              const VkPipelineShaderStageCreateInfo *create_info)
58 {
59    VK_FROM_HANDLE(vk_shader_module, module, create_info->module);
60    struct nir_spirv_specialization *spec;
61    unsigned num_spec = 0;
62    nir_shader *nir;
63 
64    spec =
65       vk_spec_info_to_nir_spirv(create_info->pSpecializationInfo, &num_spec);
66 
67    nir = rogue_spirv_to_nir(ctx,
68                             stage,
69                             create_info->pName,
70                             module->size / sizeof(uint32_t),
71                             (uint32_t *)module->data,
72                             num_spec,
73                             spec);
74 
75    free(spec);
76 
77    return nir;
78 }
79 
80 /**
81  * \brief Converts a NIR shader to Rogue.
82  *
83  * \param[in] ctx Shared multi-stage build context.
84  * \param[in] nir NIR shader.
85  * \return A rogue_shader* if successful, or NULL if unsuccessful.
86  */
pvr_nir_to_rogue(struct rogue_build_ctx *ctx, nir_shader *nir)87 struct rogue_shader *pvr_nir_to_rogue(struct rogue_build_ctx *ctx,
88                                       nir_shader *nir)
89 {
90    return rogue_nir_to_rogue(ctx, nir);
91 }
92 
93 /**
94  * \brief Converts a Rogue shader to binary.
95  *
96  * \param[in] ctx Shared multi-stage build context.
97  * \param[in] shader Rogue shader.
98  * \return A rogue_shader_binary* if successful, or NULL if unsuccessful.
99  */
pvr_rogue_to_binary(struct rogue_build_ctx *ctx, struct rogue_shader *shader)100 struct rogue_shader_binary *pvr_rogue_to_binary(struct rogue_build_ctx *ctx,
101                                                 struct rogue_shader *shader)
102 {
103    return rogue_to_binary(ctx, shader);
104 }
105