Skip to content

Commit

Permalink
feat(rendering): add shader asset and bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Mar 16, 2024
1 parent 745426d commit fc5560d
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 0 deletions.
4 changes: 4 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ set(CUBOS_ENGINE_SOURCE
"src/screen_picker/screen_picker.cpp"

"src/fixed_step/plugin.cpp"

"src/render/shader/plugin.cpp"
"src/render/shader/shader.cpp"
"src/render/shader/bridge.cpp"
)

# Create cubos engine
Expand Down
9 changes: 9 additions & 0 deletions engine/include/cubos/engine/render/module.dox
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
35 changes: 35 additions & 0 deletions engine/include/cubos/engine/render/shader/bridge.hpp
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)

Check warning on line 24 in engine/include/cubos/engine/render/shader/bridge.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/render/shader/bridge.hpp#L22-L24

Added lines #L22 - L24 were not covered by tests
{
}

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
30 changes: 30 additions & 0 deletions engine/include/cubos/engine/render/shader/plugin.hpp
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
31 changes: 31 additions & 0 deletions engine/include/cubos/engine/render/shader/shader.hpp
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;

Check warning on line 19 in engine/include/cubos/engine/render/shader/shader.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/render/shader/shader.hpp#L19

Added line #L19 was not covered by tests

/// @brief Constructs a shader from code.
/// @param shaderStage Shader stage created from GLSL code.
Shader(cubos::core::gl::ShaderStage shaderStage)
: mShaderStage(std::move(shaderStage)){};

Check warning on line 24 in engine/include/cubos/engine/render/shader/shader.hpp

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/render/shader/shader.hpp#L23-L24

Added lines #L23 - L24 were not covered by tests

cubos::core::gl::ShaderStage getShaderStage() const;

private:
cubos::core::gl::ShaderStage mShaderStage;
};
} // namespace cubos::engine
62 changes: 62 additions & 0 deletions engine/src/render/shader/bridge.cpp
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)

Check warning on line 9 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L9

Added line #L9 was not covered by tests
{
// Dump the shader code into a string.
std::string contents;
stream.readUntil(contents, nullptr);

Check warning on line 13 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L12-L13

Added lines #L12 - L13 were not covered by tests

// Read shader stage type
auto stageTypeString = assets.readMeta(handle)->get("stage");

Check warning on line 16 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L16

Added line #L16 was not covered by tests
cubos::core::gl::Stage stageType;
if (stageTypeString == "Vertex")

Check warning on line 18 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L18

Added line #L18 was not covered by tests
{
stageType = cubos::core::gl::Stage::Vertex;

Check warning on line 20 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L20

Added line #L20 was not covered by tests
}
else if (stageTypeString == "Geometry")

Check warning on line 22 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L22

Added line #L22 was not covered by tests
{
stageType = cubos::core::gl::Stage::Geometry;

Check warning on line 24 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L24

Added line #L24 was not covered by tests
}
else if (stageTypeString == "Pixel")

Check warning on line 26 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L26

Added line #L26 was not covered by tests
{
stageType = cubos::core::gl::Stage::Pixel;

Check warning on line 28 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L28

Added line #L28 was not covered by tests
}
else if (stageTypeString == "Compute")

Check warning on line 30 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L30

Added line #L30 was not covered by tests
{
stageType = cubos::core::gl::Stage::Compute;

Check warning on line 32 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L32

Added line #L32 was not covered by tests
}
else
{
CUBOS_ERROR("Shader assets must specify a stage type");
return false;

Check warning on line 37 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L36-L37

Added lines #L36 - L37 were not covered by tests
}

cubos::core::gl::ShaderStage shaderStage = mRenderDevice.createShaderStage(stageType, contents.c_str());
if (shaderStage == nullptr)

Check warning on line 41 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L40-L41

Added lines #L40 - L41 were not covered by tests
{
return false;

Check warning on line 43 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L43

Added line #L43 was not covered by tests
}

Shader shader(shaderStage);

Check warning on line 46 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L46

Added line #L46 was not covered by tests

// Store the asset's data.
assets.store(handle, shader);
return true;

Check warning on line 50 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L49-L50

Added lines #L49 - L50 were not covered by tests
}

bool ShaderBridge::saveToFile(const Assets& assets, const AnyAsset& handle, cubos::core::memory::Stream& stream)

Check warning on line 53 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L53

Added line #L53 was not covered by tests
{
// Ignore unused argument warnings
(void)assets;
(void)handle;
(void)stream;

CUBOS_ERROR("Shader assets are not saveable");
return false;

Check warning on line 61 in engine/src/render/shader/bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/bridge.cpp#L60-L61

Added lines #L60 - L61 were not covered by tests
}
25 changes: 25 additions & 0 deletions engine/src/render/shader/plugin.cpp
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)

Check warning on line 13 in engine/src/render/shader/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/plugin.cpp#L13

Added line #L13 was not covered by tests
{
cubos.addPlugin(assetsPlugin);
cubos.addPlugin(windowPlugin);

Check warning on line 16 in engine/src/render/shader/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/plugin.cpp#L15-L16

Added lines #L15 - L16 were not covered by tests

cubos.startupSystem("setup Shader asset bridge")
.tagged("cubos.assets.bridge")
.after("cubos.window.init")
.call([](Assets& assets, Window& window) {

Check warning on line 21 in engine/src/render/shader/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/plugin.cpp#L18-L21

Added lines #L18 - L21 were not covered by tests
// Add the bridge to load .glsl files.
assets.registerBridge(".glsl", std::make_unique<ShaderBridge>(window->renderDevice()));
});

Check warning on line 24 in engine/src/render/shader/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/plugin.cpp#L23-L24

Added lines #L23 - L24 were not covered by tests
}
21 changes: 21 additions & 0 deletions engine/src/render/shader/shader.cpp
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)

Check warning on line 10 in engine/src/render/shader/shader.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader.cpp#L10

Added line #L10 was not covered by tests
{
using namespace cubos::core::reflection;

return Type::create("cubos::engine::Shader")
.with(ConstructibleTrait::typed<Shader>().withMoveConstructor().build());

Check warning on line 15 in engine/src/render/shader/shader.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader.cpp#L14-L15

Added lines #L14 - L15 were not covered by tests
}

cubos::core::gl::ShaderStage Shader::getShaderStage() const

Check warning on line 18 in engine/src/render/shader/shader.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader.cpp#L18

Added line #L18 was not covered by tests
{
return mShaderStage;

Check warning on line 20 in engine/src/render/shader/shader.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader.cpp#L20

Added line #L20 was not covered by tests
}

0 comments on commit fc5560d

Please sign in to comment.