-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rendering): add conditional shader compilation
- Loading branch information
Showing
7 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
engine/include/cubos/engine/render/shader/shader_builder.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/// @file | ||
/// @brief Class @ref cubos::engine::ShaderBuilder. | ||
/// @ingroup render-shader-plugin | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include <cubos/core/gl/render_device.hpp> | ||
|
||
#include <cubos/engine/api.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
class Shader; | ||
|
||
/// @brief Configures compile-time shader parameters using #define macros. | ||
/// @ingroup render-shader-plugin | ||
class CUBOS_ENGINE_API ShaderBuilder final | ||
{ | ||
public: | ||
/// @brief Creates a shader builder. | ||
/// @param renderDevice Render device used to create the shader. | ||
/// @param stage Shader stage to create. | ||
/// @param contents Shader source code. | ||
ShaderBuilder(cubos::core::gl::RenderDevice& renderDevice, const cubos::core::gl::Stage stage, | ||
const std::string& contents) | ||
: mRenderDevice(renderDevice) | ||
, mStage(stage) | ||
, mContents(contents){}; | ||
|
||
/// @brief Creates a shader builder, using the same render device and shader stage as another builder. | ||
/// @param shaderBuilder Shader builder to copy parameters from. | ||
/// @param contents Shader source code. | ||
ShaderBuilder(const ShaderBuilder& shaderBuilder, const std::string& contents) | ||
: mRenderDevice(shaderBuilder.mRenderDevice) | ||
, mStage(shaderBuilder.mStage) | ||
, mContents(contents){}; | ||
|
||
/// @brief Defines a parameter with no value. | ||
/// @param defineName Parameter name to define. | ||
/// @return New shader builder with the parameter defined. | ||
ShaderBuilder with(const std::string& defineName) const; | ||
|
||
/// @brief Defines a parameter with a value. | ||
/// @param defineName Parameter name to define. | ||
/// @param defineValue Parameter value. | ||
/// @return New shader builder with the parameter defined. | ||
ShaderBuilder with(const std::string& defineName, const std::string& defineValue) const; | ||
|
||
/// @brief Compiles a shader from the source code stored in the builder. | ||
/// @return Shader asset. | ||
std::shared_ptr<Shader> build() const; | ||
|
||
private: | ||
cubos::core::gl::RenderDevice& mRenderDevice; ///< Render device used to create the shader. | ||
const cubos::core::gl::Stage mStage; ///< Shader stage type to create. | ||
const std::string mContents; ///< Shader source code. | ||
}; | ||
} // namespace cubos::engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <cubos/core/reflection/external/glm.hpp> | ||
#include <cubos/core/reflection/external/primitives.hpp> | ||
#include <cubos/core/reflection/traits/constructible.hpp> | ||
#include <cubos/core/reflection/type.hpp> | ||
|
||
#include <cubos/engine/render/shader/shader.hpp> | ||
#include <cubos/engine/render/shader/shader_builder.hpp> | ||
|
||
using namespace cubos::engine; | ||
|
||
ShaderBuilder ShaderBuilder::with(const std::string& defineName) const | ||
{ | ||
return ShaderBuilder(*this, "#define " + defineName + "\n" + mContents); | ||
} | ||
|
||
ShaderBuilder ShaderBuilder::with(const std::string& defineName, const std::string& defineValue) const | ||
{ | ||
return ShaderBuilder(*this, "#define " + defineName + " " + defineValue + "\n" + mContents); | ||
} | ||
|
||
std::shared_ptr<Shader> ShaderBuilder::build() const | ||
{ | ||
cubos::core::gl::ShaderStage shaderStage = mRenderDevice.createShaderStage(mStage, mContents.c_str()); | ||
if (shaderStage == nullptr) | ||
{ | ||
CUBOS_ERROR("Shader asset stage creation failed"); | ||
return nullptr; | ||
} | ||
return std::make_shared<Shader>(shaderStage, mRenderDevice, mStage, mContents); | ||
} |