Berserk
UtilsGLSL.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_GLSLUTILS_HPP
29 #define BERSERK_GLSLUTILS_HPP
30 
31 #include <render/shader/Shader.hpp>
33 
34 #include <sstream>
35 
37 
47 class UtilsGLSL {
48 public:
49  static void GenerateDefines(const ShaderCompileOptions &options, std::stringstream &stream) {
50  stream << "//<-- inserted generated options -->//\n";
51  for (const auto &option : options.Get())
52  stream << "#define " << option << "\n";
53  stream << "//<-- inserted generated options -->//\n";
54  }
55 
56  static void GenerateStruct(const char *name, const char *layout, const ShaderParams &params, std::stringstream &stream) {
57  stream << "//<-- inserted generated params code -->//\n";
58  stream << "layout (" << layout << ") uniform " << name << " {\n";
59 
60  for (const auto &p : params.GetParams()) {
61  if (p.type == ShaderParamType::Data) {
62  auto array = p.arraySize > 1;
63  auto type = p.typeData;
64 
65  stream << " " << RHIGetShaderDataIdGLSL(type) << " " << p.name;
66 
67  if (array)
68  stream << "[" << p.arraySize << "];\n";
69  else
70  stream << ";\n";
71  }
72  }
73 
74  stream << "};\n";
75  stream << "//<-- inserted generated params code -->//\n";
76  }
77 
78  static void GenerateUniformParams(const ShaderParams &params, std::stringstream &stream) {
79  stream << "//<-- inserted generated uniform params -->//\n";
80 
81  for (const auto &p : params.GetParams()) {
82  if (p.type == ShaderParamType::Texture) {
83  auto array = p.arraySize > 1;
84  auto type = p.typeParam;
85 
86  stream << "uniform " << RHIGetShaderParamIdGLSL(type) << " " << p.name;
87 
88  if (array)
89  stream << "[" << p.arraySize << "];\n";
90  else
91  stream << ";\n";
92  }
93  }
94 
95  stream << "//<-- inserted generated uniform params -->//\n";
96  }
97 
98  static void GenerateUserCode(const String &code, std::stringstream &stream) {
99  stream << "//<-- inserted user code -->//\n";
100  stream << code << "\n";
101  stream << "//<-- inserted user code -->//\n";
102  }
103 };
104 
110 
111 #endif//BERSERK_GLSLUTILS_HPP
#define BRK_NS_END
Definition: Config.hpp:48
Options passed to compile shader.
Definition: Shader.hpp:67
BRK_API const std::unordered_set< StringName > & Get() const
Definition: Shader.hpp:76
Describes layout of shader params for packing by material.
Definition: ShaderParams.hpp:88
Utf-8 encoded std based default string class.
GLSL source code processing utils.
Definition: UtilsGLSL.hpp:47
static void GenerateUserCode(const String &code, std::stringstream &stream)
Definition: UtilsGLSL.hpp:98
static void GenerateDefines(const ShaderCompileOptions &options, std::stringstream &stream)
Definition: UtilsGLSL.hpp:49
static void GenerateStruct(const char *name, const char *layout, const ShaderParams &params, std::stringstream &stream)
Definition: UtilsGLSL.hpp:56
static void GenerateUniformParams(const ShaderParams &params, std::stringstream &stream)
Definition: UtilsGLSL.hpp:78
BRK_API const std::vector< ShaderParam > & GetParams() const
Definition: ShaderParams.hpp:128
const char * RHIGetShaderParamIdGLSL(RHIShaderParamType type)
Definition: RHIDefs.hpp:456
const char * RHIGetShaderDataIdGLSL(RHIShaderDataType type)
Definition: RHIDefs.hpp:411
Definition: GLDevice.cpp:46