-
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 shader asset and bridge
- Loading branch information
Showing
8 changed files
with
217 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// @dir | ||
/// @brief @ref render-plugins module. | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @defgroup render-plugins Render | ||
/// @ingroup engine | ||
/// @brief Provides plugins for graphics rendering. | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/// @file | ||
/// @brief Class @ref cubos::engine::ShaderBridge. | ||
/// @ingroup render-shader-plugin | ||
|
||
#pragma once | ||
|
||
#include <cubos/core/memory/stream.hpp> | ||
#include <cubos/core/reflection/reflect.hpp> | ||
|
||
#include <cubos/engine/assets/bridges/file.hpp> | ||
#include <cubos/engine/render/shader/shader.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @brief Bridge for loading shader assets. | ||
/// @ingroup render-shader-plugin | ||
class ShaderBridge : public FileBridge | ||
{ | ||
public: | ||
/// @brief Constructs a bridge. | ||
/// @param renderDevice Render device used to create the shader. | ||
ShaderBridge(core::gl::RenderDevice& renderDevice) | ||
: FileBridge(cubos::core::reflection::reflect<cubos::engine::Shader>()) | ||
, mRenderDevice(renderDevice) | ||
{ | ||
} | ||
|
||
protected: | ||
bool loadFromFile(Assets& assets, const AnyAsset& handle, cubos::core::memory::Stream& stream) override; | ||
bool saveToFile(const Assets& assets, const AnyAsset& handle, cubos::core::memory::Stream& stream) override; | ||
|
||
private: | ||
core::gl::RenderDevice& mRenderDevice; ///< Render device used to create the shader. | ||
}; | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/// @dir | ||
/// @brief @ref render-shader-plugin plugin directory. | ||
|
||
/// @file | ||
/// @brief Plugin entry point. | ||
/// @ingroup render-shader-plugin | ||
|
||
#pragma once | ||
|
||
#include <cubos/engine/prelude.hpp> | ||
#include <cubos/engine/render/shader/shader.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @defgroup render-shader-plugin Shader | ||
/// @ingroup render-plugins | ||
/// @brief Adds shader assets to @b CUBOS. | ||
/// | ||
/// ## Bridges | ||
/// - @ref ShaderBridge - registered with the `.glsl` extension, loads @ref Shader assets. | ||
/// | ||
/// ## Dependencies | ||
/// - @ref assets-plugin | ||
/// - @ref window-plugin | ||
|
||
/// @brief Plugin entry function. | ||
/// @param cubos @b CUBOS. main class. | ||
/// @ingroup render-shader-plugin | ||
void shaderPlugin(Cubos& cubos); | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/// @file | ||
/// @brief Class @ref cubos::engine::Shader. | ||
/// @ingroup render-shader-plugin | ||
|
||
#pragma once | ||
|
||
#include <cubos/core/gl/render_device.hpp> | ||
#include <cubos/core/reflection/reflect.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @brief Contains a shader stage created from GLSL code. | ||
/// @ingroup render-shader-plugin | ||
class Shader final | ||
{ | ||
public: | ||
CUBOS_REFLECT; | ||
|
||
~Shader() = default; | ||
|
||
/// @brief Constructs a shader from code. | ||
/// @param shaderStage Shader stage created from GLSL code. | ||
Shader(cubos::core::gl::ShaderStage shaderStage) | ||
: mShaderStage(std::move(shaderStage)){}; | ||
|
||
cubos::core::gl::ShaderStage getShaderStage() const; | ||
|
||
private: | ||
cubos::core::gl::ShaderStage mShaderStage; | ||
}; | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <cubos/core/log.hpp> | ||
|
||
#include <cubos/engine/render/shader/bridge.hpp> | ||
|
||
using namespace cubos::engine; | ||
|
||
using cubos::core::memory::Stream; | ||
|
||
bool ShaderBridge::loadFromFile(Assets& assets, const AnyAsset& handle, Stream& stream) | ||
{ | ||
// Dump the shader code into a string. | ||
std::string contents; | ||
stream.readUntil(contents, nullptr); | ||
|
||
// Read shader stage type | ||
auto stageTypeString = assets.readMeta(handle)->get("stage"); | ||
cubos::core::gl::Stage stageType; | ||
if (stageTypeString == "Vertex") | ||
{ | ||
stageType = cubos::core::gl::Stage::Vertex; | ||
} | ||
else if (stageTypeString == "Geometry") | ||
{ | ||
stageType = cubos::core::gl::Stage::Geometry; | ||
} | ||
else if (stageTypeString == "Pixel") | ||
{ | ||
stageType = cubos::core::gl::Stage::Pixel; | ||
} | ||
else if (stageTypeString == "Compute") | ||
{ | ||
stageType = cubos::core::gl::Stage::Compute; | ||
} | ||
else | ||
{ | ||
CUBOS_ERROR("Shader assets must specify a stage type"); | ||
return false; | ||
} | ||
|
||
cubos::core::gl::ShaderStage shaderStage = mRenderDevice.createShaderStage(stageType, contents.c_str()); | ||
if (shaderStage == nullptr) | ||
{ | ||
return false; | ||
} | ||
|
||
Shader shader(shaderStage); | ||
|
||
// Store the asset's data. | ||
assets.store(handle, shader); | ||
return true; | ||
} | ||
|
||
bool ShaderBridge::saveToFile(const Assets& assets, const AnyAsset& handle, cubos::core::memory::Stream& stream) | ||
{ | ||
// Ignore unused argument warnings | ||
(void)assets; | ||
(void)handle; | ||
(void)stream; | ||
|
||
CUBOS_ERROR("Shader assets are not saveable"); | ||
return false; | ||
} |
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,25 @@ | ||
#include <cubos/core/io/window.hpp> | ||
|
||
#include <cubos/engine/assets/bridges/binary.hpp> | ||
#include <cubos/engine/assets/plugin.hpp> | ||
#include <cubos/engine/prelude.hpp> | ||
#include <cubos/engine/render/shader/bridge.hpp> | ||
#include <cubos/engine/render/shader/plugin.hpp> | ||
#include <cubos/engine/window/plugin.hpp> | ||
|
||
using cubos::core::io::Window; | ||
using cubos::engine::ShaderBridge; | ||
|
||
void cubos::engine::shaderPlugin(Cubos& cubos) | ||
{ | ||
cubos.addPlugin(assetsPlugin); | ||
cubos.addPlugin(windowPlugin); | ||
|
||
cubos.startupSystem("setup Shader asset bridge") | ||
.tagged("cubos.assets.bridge") | ||
.after("cubos.window.init") | ||
.call([](Assets& assets, Window& window) { | ||
// Add the bridge to load .glsl files. | ||
assets.registerBridge(".glsl", std::make_unique<ShaderBridge>(window->renderDevice())); | ||
}); | ||
} |
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,21 @@ | ||
#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> | ||
|
||
using namespace cubos::engine; | ||
|
||
CUBOS_REFLECT_IMPL(Shader) | ||
{ | ||
using namespace cubos::core::reflection; | ||
|
||
return Type::create("cubos::engine::Shader") | ||
.with(ConstructibleTrait::typed<Shader>().withMoveConstructor().build()); | ||
} | ||
|
||
cubos::core::gl::ShaderStage Shader::getShaderStage() const | ||
{ | ||
return mShaderStage; | ||
} |