-
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
7 changed files
with
199 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,30 @@ | ||
/// @dir | ||
/// @brief @ref shader-plugin plugin directory. | ||
|
||
/// @file | ||
/// @brief Plugin entry point. | ||
/// @ingroup shader-plugin | ||
|
||
#pragma once | ||
|
||
#include <cubos/engine/prelude.hpp> | ||
#include <cubos/engine/render/shader/shader.hpp> | ||
|
||
namespace cubos::engine | ||
{ | ||
/// @defgroup shader-plugin Shader | ||
/// @ingroup engine | ||
/// @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 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,32 @@ | ||
/// @file | ||
/// @brief Class @ref cubos::engine::Shader. | ||
/// @ingroup 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 shader-plugin | ||
class Shader final | ||
{ | ||
public: | ||
CUBOS_REFLECT; | ||
|
||
~Shader() = default; | ||
|
||
/// @brief Constructs an empty shader. | ||
Shader() = default; | ||
|
||
/// @brief Constructs a shader from code. | ||
/// @param shaderStage Shader stage created from GLSL code. | ||
Shader(cubos::core::gl::ShaderStage shaderStage) | ||
: mShaderStage(shaderStage){}; | ||
|
||
private: | ||
cubos::core::gl::ShaderStage mShaderStage; | ||
}; | ||
} // namespace cubos::engine |
35 changes: 35 additions & 0 deletions
35
engine/include/cubos/engine/render/shader/shader_bridge.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,35 @@ | ||
/// @file | ||
/// @brief Class @ref cubos::engine::ShaderBridge. | ||
/// @ingroup 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 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,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/plugin.hpp> | ||
#include <cubos/engine/render/shader/shader_bridge.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,16 @@ | ||
#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>().withDefaultConstructor().withMoveConstructor().build()); | ||
} |
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,57 @@ | ||
#include <cubos/core/log.hpp> | ||
|
||
#include <cubos/engine/render/shader/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 meta = assets.readMeta(handle); | ||
auto stageTypeString = meta->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; | ||
} | ||
|
||
Shader shader(mRenderDevice.createShaderStage(stageType, contents.c_str())); | ||
|
||
// Store the asset's data. | ||
assets.store(handle, std::move(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; | ||
} |