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 13, 2024
1 parent 745426d commit 670e42d
Show file tree
Hide file tree
Showing 7 changed files with 199 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/shader_bridge.cpp"
)

# Create cubos engine
Expand Down
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 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
32 changes: 32 additions & 0 deletions engine/include/cubos/engine/render/shader/shader.hpp
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;

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 an empty shader.
Shader() = default;

Check warning on line 22 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#L22

Added line #L22 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(shaderStage){};

Check warning on line 27 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#L26-L27

Added lines #L26 - L27 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

engine/include/cubos/engine/render/shader/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
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/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)

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
}
16 changes: 16 additions & 0 deletions engine/src/render/shader/shader.cpp
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)

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>().withDefaultConstructor().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
}
57 changes: 57 additions & 0 deletions engine/src/render/shader/shader_bridge.cpp
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)

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/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/shader_bridge.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L12 - L13 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L16-L17

Added lines #L16 - L17 were not covered by tests
cubos::core::gl::Stage stageType;
if (stageTypeString == "Vertex")

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L19

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L21

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L23

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L25

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L27

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L29

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L31

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L33

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L37-L38

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

Shader shader(mRenderDevice.createShaderStage(stageType, contents.c_str()));

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L41

Added line #L41 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L44-L45

Added lines #L44 - L45 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L48

Added line #L48 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 56 in engine/src/render/shader/shader_bridge.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/shader/shader_bridge.cpp#L55-L56

Added lines #L55 - L56 were not covered by tests
}

0 comments on commit 670e42d

Please sign in to comment.