1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef GLES_SPIRV_CROSS_HELPERS_H
16 #define GLES_SPIRV_CROSS_HELPERS_H
17 
18 #include <spirv_cross.hpp>
19 #include <spirv_glsl.hpp>
20 #include <spirv_parser.hpp>
21 #include <string>
22 #include <string_view>
23 
24 #include "spirv_cross_helper_structs_gles.h"
25 
26 namespace Gles {
27 // inherit from CompilerGLSL to have better access
28 class CoreCompiler final : public spirv_cross::CompilerGLSL {
29 public:
30     CoreCompiler(const uint32_t* ir, size_t wordCount);
31     ~CoreCompiler() = default;
32     const std::vector<spirv_cross::SPIRConstant> GetConstants() const;
33     const spirv_cross::ParsedIR& GetIr() const;
34 };
35 
36 void ReflectPushConstants(spirv_cross::Compiler& compiler, const spirv_cross::ShaderResources& resources,
37     std::vector<PushConstantReflection>& reflections, ShaderStageFlags stage);
38 
39 // Converts specialization constant to normal constant, (to reduce unnecessary clutter in glsl)
40 void ConvertSpecConstToConstant(spirv_cross::CompilerGLSL& compiler, const char* name);
41 
42 // Converts constant declaration to uniform. (actually only works on spec constants)
43 void ConvertConstantToUniform(const spirv_cross::CompilerGLSL& compiler, std::string& source, const char* name);
44 
45 void SetSpecMacro(spirv_cross::CompilerGLSL& compiler, const char* name, uint32_t value);
46 
47 void ProcessStruct(std::string_view baseName, size_t baseOffset, const spirv_cross::Compiler& compiler,
48     uint32_t structTypeId, std::vector<PushConstantReflection>& reflections, ShaderStageFlags stage);
49 
50 #ifdef PLUGIN_UNUSED_SPRIV_CROSS_HELPERS
51 bool DefineForSpec(
52     const std::vector<SpecConstantInfo>& reflectionInfo, uint32_t spcid, uintptr_t offset, std::string& result);
53 
54 std::string InsertDefines(std::string_view shaderIn, std::string_view Defines);
55 
56 std::string Specialize(ShaderStageFlags mask, std::string_view shaderTemplate,
57     const std::vector<SpecConstantInfo>& info, const ShaderSpecializationConstantDataView& data);
58 
59 void CreateSpecInfos(const spirv_cross::Compiler& compiler, std::vector<SpecConstantInfo>& outSpecInfo);
60 
61 uint32_t ConstId(spirv_cross::CompilerGLSL& compiler, const char* name);
62 
63 uint32_t SpecConstId(spirv_cross::CompilerGLSL& compiler, const char* name);
64 #endif
65 } // namespace Gles
66 
67 #endif // GLES_SPIRV_CROSS_HELPERS_H
68