Berserk
RHIShader.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_RHISHADER_HPP
29 #define BERSERK_RHISHADER_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Data.hpp>
33 #include <core/Typedefs.hpp>
34 #include <core/io/Logger.hpp>
36 #include <rhi/RHIResource.hpp>
37 
38 #include <functional>
39 #include <unordered_map>
40 #include <vector>
41 
43 
53 class RHIShaderMeta : public RefCnt {
54 public:
56  struct InputAttribute {
60  };
61 
63  struct ObjectParam {
68  };
69 
71  struct DataParam {
80  };
81 
83  struct DataParamBlock {
87  };
88 
89  inline uint32 GetSamplerLocation(const StringName &p) const {
90  auto query = samplers.find(p);
91  if (query == samplers.end()) {
92  BRK_ERROR("Failed to find p=\"" << p << "\" in shader name=\"" << name << "\'");
93  return 0xffffffff;
94  }
95  return query->second.location;
96  }
97 
98  inline uint32 GetParamBlockSlot(const StringName &p) const {
99  auto query = paramBlocks.find(p);
100  if (query == paramBlocks.end()) {
101  BRK_ERROR("Failed to find p=\"" << p << "\" in shader name=\"" << name << "\'");
102  return 0xffffffff;
103  }
104  return query->second.slot;
105  }
106 
108  std::unordered_map<StringName, InputAttribute> inputs;
109  std::unordered_map<StringName, DataParam> params;
110  std::unordered_map<StringName, DataParamBlock> paramBlocks;
111  std::unordered_map<StringName, ObjectParam> samplers;
112 };
113 
121 };
122 
131  std::vector<RHIShaderStageDesc> stages;
132  std::function<void(Ref<class RHIShader>)> callback;
133 };
134 
139 class RHIShader : public RHIResource {
140 public:
142  enum class Status : uint32 {
144  PendingCompilation = 0,
146  Compiled = 1,
148  FailedCompile = 2
149  };
150 
151  BRK_API ~RHIShader() override = default;
152 
161  BRK_API virtual Status GetCompilationStatus() const = 0;
162 
171  BRK_API virtual String GetCompilerMessage() const = 0;
172 
182 
184  const StringName &GetShaderName() const { return mName; }
185 
188 
190  const std::vector<RHIShaderStageDesc> &GetStages() const { return mStages; }
191 
192 protected:
195 
198 
200  std::vector<RHIShaderStageDesc> mStages;
201 };
202 
208 
209 #endif//BERSERK_RHISHADER_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
std::uint32_t uint32
Definition: Typedefs.hpp:44
std::uint16_t uint16
Definition: Typedefs.hpp:41
Base class for RHI resource.
Definition: RHIResource.hpp:55
Reflection info about compiled RHI program.
Definition: RHIShader.hpp:53
StringName name
Definition: RHIShader.hpp:107
uint32 GetParamBlockSlot(const StringName &p) const
Definition: RHIShader.hpp:98
std::unordered_map< StringName, DataParam > params
Definition: RHIShader.hpp:109
std::unordered_map< StringName, ObjectParam > samplers
Definition: RHIShader.hpp:111
uint32 GetSamplerLocation(const StringName &p) const
Definition: RHIShader.hpp:89
std::unordered_map< StringName, InputAttribute > inputs
Definition: RHIShader.hpp:108
std::unordered_map< StringName, DataParamBlock > paramBlocks
Definition: RHIShader.hpp:110
Compiled and linked native shader.
Definition: RHIShader.hpp:139
const StringName & GetShaderName() const
Definition: RHIShader.hpp:184
virtual BRK_API Status GetCompilationStatus() const =0
StringName mName
Definition: RHIShader.hpp:194
const std::vector< RHIShaderStageDesc > & GetStages() const
Definition: RHIShader.hpp:190
virtual BRK_API Ref< const RHIShaderMeta > GetShaderMeta() const =0
virtual BRK_API String GetCompilerMessage() const =0
std::vector< RHIShaderStageDesc > mStages
Definition: RHIShader.hpp:200
RHIShaderLanguage GetLanguage() const
Definition: RHIShader.hpp:187
RHIShaderLanguage mLanguage
Definition: RHIShader.hpp:197
Status
Program may have different status (incorrect shaders mush not crash the application)
Definition: RHIShader.hpp:142
@ PendingCompilation
Program pending to be compiled.
@ Compiled
Program successfully compiled.
@ FailedCompile
Failed to compile and link program.
BRK_API ~RHIShader() override=default
Reference counted base object.
Definition: RefCnt.hpp:52
Cached shared utf-8 string id.
Definition: StringName.hpp:61
Utf-8 encoded std based default string class.
#define BRK_ERROR(message)
Definition: Logger.hpp:148
RHIShaderParamType
Definition: RHIDefs.hpp:106
RHIShaderDataType
Definition: RHIDefs.hpp:83
RHIShaderType
Definition: RHIDefs.hpp:122
RHIVertexElementType
Definition: RHIDefs.hpp:71
RHIShaderLanguage
Definition: RHIDefs.hpp:127
Definition: GLDevice.cpp:46
Shader creation descriptor. Allows create from byteCode.
Definition: RHIShader.hpp:127
Ref< Data > byteCode
Definition: RHIShader.hpp:128
RHIShaderLanguage language
Definition: RHIShader.hpp:130
std::vector< RHIShaderStageDesc > stages
Definition: RHIShader.hpp:131
StringName name
Definition: RHIShader.hpp:129
std::function< void(Ref< class RHIShader >)> callback
Definition: RHIShader.hpp:132
Uniform block info.
Definition: RHIShader.hpp:83
uint32 size
Definition: RHIShader.hpp:86
uint32 slot
Definition: RHIShader.hpp:85
StringName name
Definition: RHIShader.hpp:84
Data param within uniform block.
Definition: RHIShader.hpp:71
RHIShaderDataType type
Definition: RHIShader.hpp:79
uint16 blockSlot
Definition: RHIShader.hpp:77
uint16 arraySize
Definition: RHIShader.hpp:74
uint16 arrayStride
Definition: RHIShader.hpp:75
uint16 elementSize
Definition: RHIShader.hpp:73
uint32 blockOffset
Definition: RHIShader.hpp:78
StringName name
Definition: RHIShader.hpp:72
uint16 matrixStride
Definition: RHIShader.hpp:76
Vertex shader inputs.
Definition: RHIShader.hpp:56
uint16 location
Definition: RHIShader.hpp:58
RHIVertexElementType type
Definition: RHIShader.hpp:59
StringName name
Definition: RHIShader.hpp:57
Object params like samplers, textures.
Definition: RHIShader.hpp:63
uint16 arraySize
Definition: RHIShader.hpp:66
StringName name
Definition: RHIShader.hpp:64
RHIShaderParamType type
Definition: RHIShader.hpp:67
uint16 location
Definition: RHIShader.hpp:65
Single shader stage.
Definition: RHIShader.hpp:118
RHIShaderType type
Definition: RHIShader.hpp:119
String sourceCode
Definition: RHIShader.hpp:120