OpenGLShaderCompileManager.ixx File
OpenGL manager that compiles shader sources into native OpenGL programs. More...
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <cassert>
#include <iostream>
#include <memory>
#include <ostream>
#include <ranges>
#include <unordered_map>
#include <vector>
#include <helios.engine.runtime.concepts>
#include <helios.engine.runtime.messaging.command.CommandHandlerRegistry>
#include <helios.engine.runtime.messaging.command.NullCommandBuffer>
#include <helios.engine.runtime.messaging.command.concepts>
#include <helios.engine.rendering.shader.commands>
#include <helios.engine.rendering.shader.types>
#include <helios.engine.runtime.world.tags>
#include <helios.engine.util.log>
#include <helios.engine.runtime.world>
#include <helios.engine.rendering.shader.components>
#include <helios.engine.rendering.shader.ShaderEntity>
#include <helios.engine.util.io>
#include <helios.engine.rendering.shader.concepts>
#include <helios.engine.rendering.shader.NullUniformCacheStrategy>
#include <
helios.opengl.components.OpenGLShaderComponent>
#include <helios.engine.runtime.world.EngineWorld>
Namespaces Index
| namespace | helios |
|
|
|
| namespace | opengl |
|
|
|
| namespace | commands |
|
|
|
| namespace | components |
|
|
|
Classes Index
Macro Definitions Index
Description
OpenGL manager that compiles shader sources into native OpenGL programs.
Macro Definitions
HELIOS_LOG_SCOPE
| #define HELIOS_LOG_SCOPE "helios::opengl::OpenGLShaderCompileManager" |
|
File Listing
The file content with the documentation metadata removed is:
14#include <unordered_map>
18export module helios.opengl.OpenGLShaderCompileManager;
20import helios.engine.util.log;
21import helios.engine.util.io;
22import helios.engine.runtime.world.tags;
23import helios.engine.runtime.world;
24import helios.engine.rendering.shader.commands;
25import helios.engine.rendering.shader.components;
26import helios.engine.rendering.shader.ShaderEntity;
27import helios.engine.rendering.shader.types;
28import helios.engine.rendering.shader.concepts;
29import helios.engine.rendering.shader.commands;
30import helios.engine.rendering.shader.NullUniformCacheStrategy;
32import helios.opengl.components.OpenGLShaderComponent;
34import helios.engine.runtime.world.EngineWorld;
35import helios.engine.runtime.messaging.command.concepts;
36import helios.engine.runtime.messaging.command.NullCommandBuffer;
37import helios.engine.runtime.messaging.command.CommandHandlerRegistry;
38import helios.engine.runtime.concepts;
40using namespace helios::engine::runtime::world;
41using namespace helios::engine::util::log;
42using namespace helios::engine::util::io;
43using namespace helios::engine::runtime::world::tags;
44using namespace helios::engine::rendering::shader::commands;
45using namespace helios::engine::rendering::shader::components;
46using namespace helios::engine::rendering::shader;
47using namespace helios::engine::rendering::shader::types;
48using namespace helios::engine::rendering::shader::concepts;
49using namespace helios::engine::rendering::shader::commands;
51using namespace helios::engine::runtime::messaging::command::concepts;
52using namespace helios::engine::runtime::messaging::command;
53#define HELIOS_LOG_SCOPE "helios::opengl::OpenGLShaderCompileManager"
62 template<typename THandle, typename TUniformCacheStrategy = NullUniformCacheStrategy<THandle>>
63 requires IsShaderHandle<THandle> && IsUniformCacheStrategyLike<TUniformCacheStrategy, THandle, UniformScope::Pass, UniformScope::Draw>
66 BasicStringFileReader stringFileReader_;
68 RenderResourceWorld& renderResourceWorld_;
70 std::vector<ShaderHandle> shaderHandles_;
72 std::string vertexShaderSource_;
74 std::string fragmentShaderSource_;
77 inline static const Logger& logger_ = LogManager::loggerForScope(HELIOS_LOG_SCOPE);
79 TUniformCacheStrategy uniformCacheStrategy_;
89 const std::string& vertexShaderPath,
90 const std::string& fragmentShaderPath,
91 std::string& vertexShaderSource,
92 std::string& fragmentShaderSource
95 const bool frag = stringFileReader_.readInto(fragmentShaderPath, fragmentShaderSource);
96 const bool vert = stringFileReader_.readInto(vertexShaderPath, vertexShaderSource);
98 assert(frag && vert && "Could not load shader files");
111 bool compile(ShaderEntity shader) noexcept {
113 using Handle = typename ShaderEntity::Handle_type;
115 logger_.info("Compiling shader...");
118 logger_.error("Shader already has a ShaderComponent");
119 assert(false && "Shader already has a ShaderComponent");
123 auto* shaderSourceComponent = shader.template get<ShaderSourceComponent<Handle>>();
126 const bool loaded = load(
127 shaderSourceComponent->vertexShaderPath,
128 shaderSourceComponent->fragmentShaderPath,
129 vertexShaderSource_, fragmentShaderSource_);
132 logger_.error("Could not load shader files");
133 assert(false && "Could not load shader files");
137 const GLchar* vertexSrc = vertexShaderSource_.c_str();
138 const GLchar* fragmentSrc = fragmentShaderSource_.c_str();
140 const unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
141 glShaderSource(vertexShader, 1, &vertexSrc, nullptr);
142 glCompileShader(vertexShader);
144 const unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
145 glShaderSource(fragmentShader, 1, &fragmentSrc, nullptr);
146 glCompileShader(fragmentShader);
151 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
153 glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
154 logger_.error("Vertex Shader Compilation failed.");
155 assert(false && "Vertex Shader Compilation failed.");
159 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
161 glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
162 logger_.error("Fragment Shader Compilation failed.");
163 assert(false && "Fragment Shader Compilation failed.");
167 auto progId = glCreateProgram();
169 glAttachShader(progId, vertexShader);
170 glAttachShader(progId, fragmentShader);
171 glLinkProgram(progId);
173 glGetProgramiv(progId, GL_LINK_STATUS, &success);
175 glGetProgramInfoLog(progId, 512, nullptr, infoLog);
176 logger_.error("Program linking failed.");
177 assert(false && "Program linking failed.");
184 glDeleteShader(vertexShader);
185 glDeleteShader(fragmentShader);
187 vertexShaderSource_.clear();
188 fragmentShaderSource_.clear();
201 RenderResourceWorld& renderResourceWorld,
202 TUniformCacheStrategy&& uniformCacheStrategy
205 renderResourceWorld_(renderResourceWorld),
206 uniformCacheStrategy_(std::move(uniformCacheStrategy))
220 void flush(UpdateContext& updateContext) noexcept {
222 if (shaderHandles_.empty()) {
226 for (const auto& sourceHandle : shaderHandles_) {
227 auto shaderEntity = renderResourceWorld_.findEntity<THandle>(sourceHandle);
230 logger_.error("Could not find shader source");
231 assert(false && "Could not find shader source");
235 if (!compile(*shaderEntity)) {
236 logger_.error("Could not compile shader");
238 shaderEntity->template remove<ShaderSourceComponent<THandle>>();
239 uniformCacheStrategy_.template cacheUniforms<UniformScope::Pass>(shaderEntity->handle(), renderResourceWorld_, updateContext);
240 uniformCacheStrategy_.template cacheUniforms<UniformScope::Draw>(shaderEntity->handle(), renderResourceWorld_, updateContext);
244 shaderHandles_.clear();
247 bool submit(const ShaderBatchCompileCommand<THandle>& command) noexcept {
248 for (const auto& shaderHandle : command.shaderHandles) {
249 shaderHandles_.push_back(shaderHandle);
254 bool submit(const ShaderCompileCommand<THandle>& command) noexcept {
255 shaderHandles_.push_back(command.shaderHandle);
264 void init(helios::engine::runtime::messaging::command::CommandHandlerRegistry& commandHandlerRegistry) noexcept {
265 commandHandlerRegistry.registerHandler<ShaderCompileCommand<THandle>>(*this);
266 commandHandlerRegistry.registerHandler<ShaderBatchCompileCommand<THandle>>(*this);
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.