Skip to main content

OpenGLShaderCompileManager Class Template

Manager that consumes shader compile commands and performs OpenGL compilation/linking. More...

Declaration

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>> class helios::opengl::OpenGLShaderCompileManager<THandle, TUniformCacheStrategy> { ... }

Public Member Typedefs Index

template < ... >
usingEngineRoleTag = ManagerRole

Engine role marker used by runtime registries. More...

Public Constructors Index

template < ... >
OpenGLShaderCompileManager (RenderResourceWorld &renderResourceWorld, TUniformCacheStrategy &&uniformCacheStrategy)

Constructs the manager with access to render-resource world storage. More...

Public Member Functions Index

template < ... >
voidflush (UpdateContext &updateContext) noexcept

Compiles all queued shaders and clears processed command data. More...

template < ... >
boolsubmit (ShaderBatchCompileCommand< THandle > &&command) noexcept

Enqueues a batch of shader handles for compilation. More...

template < ... >
boolsubmit (ShaderCompileCommand< THandle > &&command) noexcept

Enqueues a single shader handle for compilation. More...

template < ... >
voidinit (helios::engine::runtime::messaging::command::CommandHandlerRegistry &commandHandlerRegistry) noexcept

Registers compile command handlers in the command registry. More...

Private Member Functions Index

template < ... >
boolload (const std::string &vertexShaderPath, const std::string &fragmentShaderPath, std::string &vertexShaderSource, std::string &fragmentShaderSource) noexcept

Loads the specified vertex and fragment shader. More...

template < ... >
boolcompile (ShaderEntity shader) noexcept

Compiles the vertex and fragment shader represented by this instance. More...

Private Member Attributes Index

template < ... >
BasicStringFileReaderstringFileReader_

File reader used to load shader source text from disk. More...

template < ... >
RenderResourceWorld &renderResourceWorld_

Render-resource world used to resolve shader entities by handle. More...

template < ... >
std::vector< ShaderHandle >shaderHandles_

Pending shader handles queued for compilation during flush(...). More...

template < ... >
std::stringvertexShaderSource_

Reused storage for loaded vertex shader source. More...

template < ... >
std::stringfragmentShaderSource_

Reused storage for loaded fragment shader source. More...

template < ... >
TUniformCacheStrategyuniformCacheStrategy_

Uniform caching strategy executed after successful shader compilation. More...

Private Static Attributes Index

template < ... >
static const Logger &logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE)

Scoped logger for shader compile and link diagnostics. More...

Description

Manager that consumes shader compile commands and performs OpenGL compilation/linking.

Template Parameters
THandle

Shader handle type.

TUniformCacheStrategy

Uniform cache strategy used after successful program linking.

Definition at line 67 of file OpenGLShaderCompileManager.ixx.

Public Member Typedefs

EngineRoleTag

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
using helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::EngineRoleTag = ManagerRole

Engine role marker used by runtime registries.

Definition at line 242 of file OpenGLShaderCompileManager.ixx.

242 using EngineRoleTag = ManagerRole;

Public Constructors

OpenGLShaderCompileManager()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::OpenGLShaderCompileManager (RenderResourceWorld & renderResourceWorld, TUniformCacheStrategy && uniformCacheStrategy)
inline explicit

Constructs the manager with access to render-resource world storage.

Parameters
renderResourceWorld

Render-resource world used to resolve shader entities.

uniformCacheStrategy

Strategy object used to cache pass/draw uniforms.

Definition at line 230 of file OpenGLShaderCompileManager.ixx.

231 RenderResourceWorld& renderResourceWorld,
232 TUniformCacheStrategy&& uniformCacheStrategy
233 )
234 :
235 renderResourceWorld_(renderResourceWorld),
236 uniformCacheStrategy_(std::move(uniformCacheStrategy))
237 { }

Public Member Functions

flush()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
void helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::flush (UpdateContext & updateContext)
inline noexcept

Compiles all queued shaders and clears processed command data.

Handles are queued through consuming submit(...) overloads and processed in FIFO order for the current frame.

Parameters
updateContext

Frame-local update context.

Definition at line 253 of file OpenGLShaderCompileManager.ixx.

253 void flush(UpdateContext& updateContext) noexcept {
254
255 if (shaderHandles_.empty()) {
256 return;
257 }
258
259 for (const auto& sourceHandle : shaderHandles_) {
260 auto shaderEntity = renderResourceWorld_.findEntity<THandle>(sourceHandle);
261
262 if (!shaderEntity) {
263 logger_.error("Could not find shader source");
264 assert(false && "Could not find shader source");
265 continue;
266 }
267
268 if (!compile(*shaderEntity)) {
269 logger_.error("Could not compile shader");
270 } else {
271 shaderEntity->template remove<ShaderSourceComponent<THandle>>();
272 std::ignore = uniformCacheStrategy_.template cacheUniforms<UniformScope::Pass>(shaderEntity->handle(), renderResourceWorld_, updateContext);
273 std::ignore = uniformCacheStrategy_.template cacheUniforms<UniformScope::Material>(shaderEntity->handle(), renderResourceWorld_, updateContext);
274 std::ignore = uniformCacheStrategy_.template cacheUniforms<UniformScope::Draw>(shaderEntity->handle(), renderResourceWorld_, updateContext);
275
276 }
277 }
278
279 shaderHandles_.clear();
280 }

init()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
void helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::init (helios::engine::runtime::messaging::command::CommandHandlerRegistry & commandHandlerRegistry)
inline noexcept

Registers compile command handlers in the command registry.

Parameters
commandHandlerRegistry

Registry used for command-handler registration.

Definition at line 313 of file OpenGLShaderCompileManager.ixx.

313 void init(helios::engine::runtime::messaging::command::CommandHandlerRegistry& commandHandlerRegistry) noexcept {
314 commandHandlerRegistry.registerHandler<ShaderCompileCommand<THandle>>(*this);
315 commandHandlerRegistry.registerHandler<ShaderBatchCompileCommand<THandle>>(*this);
316 }

submit()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
bool helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::submit (ShaderBatchCompileCommand< THandle > && command)
inline noexcept

Enqueues a batch of shader handles for compilation.

Parameters
command

Compile command that is consumed by move.

Returns

true if the command was accepted.

Definition at line 288 of file OpenGLShaderCompileManager.ixx.

288 bool submit(ShaderBatchCompileCommand<THandle>&& command) noexcept {
289 shaderHandles_.reserve(shaderHandles_.size() + command.shaderHandles.size());
290
291 for (auto& shaderHandle : command.shaderHandles) {
292 shaderHandles_.push_back(std::move(shaderHandle));
293 }
294 return true;
295 }

submit()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
bool helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::submit (ShaderCompileCommand< THandle > && command)
inline noexcept

Enqueues a single shader handle for compilation.

Parameters
command

Compile command that is consumed by move.

Returns

true if the command was accepted.

Definition at line 303 of file OpenGLShaderCompileManager.ixx.

303 bool submit(ShaderCompileCommand<THandle>&& command) noexcept {
304 shaderHandles_.push_back(std::move(command.shaderHandle));
305 return true;
306 }

Private Member Functions

compile()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
bool helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::compile (ShaderEntity shader)
inline noexcept

Compiles the vertex and fragment shader represented by this instance.

Parameters
shader

Shader entity providing source and receiving OpenGL shader state.

Returns

true if compilation succeeded, otherwise false.

info

On compile/link errors, diagnostics are asserted/logged and false is returned.

Definition at line 140 of file OpenGLShaderCompileManager.ixx.

140 bool compile(ShaderEntity shader) noexcept {
141
142 using Handle = typename ShaderEntity::Handle_type;
143
144 logger_.info("Compiling shader...");
145
146 if (shader.template get<OpenGLShaderComponent<Handle>>()) {
147 logger_.error("Shader already has a ShaderComponent");
148 assert(false && "Shader already has a ShaderComponent");
149 return false;
150 }
151
152 auto* shaderSourceComponent = shader.template get<ShaderSourceComponent<Handle>>();
153
154
155 const bool loaded = load(
156 shaderSourceComponent->vertexShaderPath,
157 shaderSourceComponent->fragmentShaderPath,
158 vertexShaderSource_, fragmentShaderSource_);
159
160 if (!loaded) {
161 logger_.error("Could not load shader files");
162 assert(false && "Could not load shader files");
163 return false;
164 }
165
166 const GLchar* vertexSrc = vertexShaderSource_.c_str();
167 const GLchar* fragmentSrc = fragmentShaderSource_.c_str();
168
169 const unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
170 glShaderSource(vertexShader, 1, &vertexSrc, nullptr);
171 glCompileShader(vertexShader);
172
173 const unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
174 glShaderSource(fragmentShader, 1, &fragmentSrc, nullptr);
175 glCompileShader(fragmentShader);
176
177 int success;
178 char infoLog[512];
179
180 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
181 if (!success) {
182 glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
183 logger_.error("Vertex Shader Compilation failed.");
184 assert(false && "Vertex Shader Compilation failed.");
185 return false;
186 }
187
188 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
189 if (!success) {
190 glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
191 logger_.error("Fragment Shader Compilation failed.");
192 assert(false && "Fragment Shader Compilation failed.");
193 return false;
194 }
195
196 auto progId = glCreateProgram();
197
198 glAttachShader(progId, vertexShader);
199 glAttachShader(progId, fragmentShader);
200 glLinkProgram(progId);
201
202 glGetProgramiv(progId, GL_LINK_STATUS, &success);
203 if (!success) {
204 glGetProgramInfoLog(progId, 512, nullptr, infoLog);
205 logger_.error("Program linking failed.");
206 assert(false && "Program linking failed.");
207 return false;
208 }
209
210 auto& shaderComponent = shader.add<OpenGLShaderComponent<THandle>>();
211 shaderComponent.programId = progId;
212
213 glDeleteShader(vertexShader);
214 glDeleteShader(fragmentShader);
215
216 vertexShaderSource_.clear();
217 fragmentShaderSource_.clear();
218
219 return true;
220 }

load()

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
bool helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::load (const std::string & vertexShaderPath, const std::string & fragmentShaderPath, std::string & vertexShaderSource, std::string & fragmentShaderSource)
inline noexcept

Loads the specified vertex and fragment shader.

Parameters
vertexShaderPath

Path to the vertex shader source file.

fragmentShaderPath

Path to the fragment shader source file.

vertexShaderSource

Output buffer receiving vertex shader source text.

fragmentShaderSource

Output buffer receiving fragment shader source text.

Returns

true if loading succeeded, otherwise false.

info

On failure, diagnostics are asserted/logged and false is returned.

Definition at line 116 of file OpenGLShaderCompileManager.ixx.

116 bool load(
117 const std::string& vertexShaderPath,
118 const std::string& fragmentShaderPath,
119 std::string& vertexShaderSource,
120 std::string& fragmentShaderSource
121 ) noexcept {
122
123 const bool frag = stringFileReader_.readInto(fragmentShaderPath, fragmentShaderSource);
124 const bool vert = stringFileReader_.readInto(vertexShaderPath, vertexShaderSource);
125
126 assert(frag && vert && "Could not load shader files");
127
128 return frag && vert;
129 }

Private Member Attributes

fragmentShaderSource_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
std::string helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::fragmentShaderSource_

Reused storage for loaded fragment shader source.

Definition at line 92 of file OpenGLShaderCompileManager.ixx.

92 std::string fragmentShaderSource_;

renderResourceWorld_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
RenderResourceWorld& helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::renderResourceWorld_

Render-resource world used to resolve shader entities by handle.

Definition at line 77 of file OpenGLShaderCompileManager.ixx.

77 RenderResourceWorld& renderResourceWorld_;

shaderHandles_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
std::vector<ShaderHandle> helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::shaderHandles_

Pending shader handles queued for compilation during flush(...).

Definition at line 82 of file OpenGLShaderCompileManager.ixx.

82 std::vector<ShaderHandle> shaderHandles_;

stringFileReader_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
BasicStringFileReader helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::stringFileReader_

File reader used to load shader source text from disk.

Definition at line 72 of file OpenGLShaderCompileManager.ixx.

72 BasicStringFileReader stringFileReader_;

uniformCacheStrategy_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
TUniformCacheStrategy helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::uniformCacheStrategy_

Uniform caching strategy executed after successful shader compilation.

Definition at line 103 of file OpenGLShaderCompileManager.ixx.

103 TUniformCacheStrategy uniformCacheStrategy_;

vertexShaderSource_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
std::string helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::vertexShaderSource_

Reused storage for loaded vertex shader source.

Definition at line 87 of file OpenGLShaderCompileManager.ixx.

87 std::string vertexShaderSource_;

Private Static Attributes

logger_

template <typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
const Logger& helios::opengl::OpenGLShaderCompileManager< THandle, TUniformCacheStrategy >::logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE)
static

Scoped logger for shader compile and link diagnostics.

Definition at line 98 of file OpenGLShaderCompileManager.ixx.

98 inline static const Logger& logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE);

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.