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