Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/limitless/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <limitless/util/filesystem.hpp>
#include <limitless/loaders/texture_loader.hpp>

#include <cstdint>
#include <vector>

namespace Limitless::ms {
class Material;
}
Expand Down Expand Up @@ -129,6 +132,17 @@ namespace Limitless {
[[nodiscard]] const auto& getBaseDir() const noexcept { return base_dir; }
[[nodiscard]] const auto& getShaderDir() const noexcept { return shader_dir; }

/**
* Read file bytes from the given path. Default implementation uses the filesystem.
* Paths may be absolute or relative.
*/
[[nodiscard]] virtual std::vector<std::uint8_t> readFile(const fs::path& path) const;

/**
* Whether a regular file exists at the given path. Default implementation uses the filesystem.
*/
[[nodiscard]] virtual bool fileExists(const fs::path& path) const;

void reloadTextures(const TextureLoaderFlags& settings);
};
}
8 changes: 6 additions & 2 deletions include/limitless/core/shader/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Limitless {
using std::runtime_error::runtime_error;
};

class Assets;

/**
* Shader class describes shader that is used to compile shader program by ShaderCompiler
*
Expand Down Expand Up @@ -68,6 +70,8 @@ namespace Limitless {
*/
std::set<std::string> include_entries;

Assets* assets {nullptr};

/**
* Self-implemented "#include" directive for GLSL
*
Expand All @@ -92,7 +96,7 @@ namespace Limitless {
* @return - file source code
* @throw shader_file_not_found - if file at @param filepath does not exist
*/
static std::string getSource(const fs::path& filepath);
std::string getSource(const fs::path& filepath);

/**
* Swap function used by move-semantics
Expand All @@ -119,7 +123,7 @@ namespace Limitless {
* @throw shader_file_not_found - if file at @param path does not exist
* @throw shader_include_not_found - if file specified at include directive does not exist
*/
Shader(fs::path path, Type type, const ShaderAction& action = {});
Shader(fs::path path, Type type, const ShaderAction& action = {}, Assets* assets = nullptr);

/**
* Shader destructor
Expand Down
7 changes: 5 additions & 2 deletions include/limitless/core/shader/shader_compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Limitless {
class ShaderProgram;
class RendererSettings;
class Context;
class Assets;

class shader_linking_error : public std::runtime_error {
public:
Expand Down Expand Up @@ -46,6 +47,8 @@ namespace Limitless {
*/
std::optional<RendererSettings> render_settings;

Assets* assets {nullptr};

void replaceCommonDefines(Shader& shader);
public:
/**
Expand All @@ -58,8 +61,8 @@ namespace Limitless {
* @param ctx
* @param settings
*/
ShaderCompiler(Context& ctx, const RendererSettings& settings);
explicit ShaderCompiler(Context& ctx);
ShaderCompiler(Context& ctx, const RendererSettings& settings, Assets* assets = nullptr);
explicit ShaderCompiler(Context& ctx, Assets* assets = nullptr);
virtual ~ShaderCompiler() = default;

ShaderCompiler(const ShaderCompiler&) noexcept = delete;
Expand Down
3 changes: 2 additions & 1 deletion include/limitless/shader_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Limitless {
class RendererSettings;
class ShaderProgram;
class Context;
class Assets;

class shader_storage_error : public std::runtime_error {
public:
Expand All @@ -27,7 +28,7 @@ namespace Limitless {

std::mutex mutex;
public:
void initialize(Context& ctx, const RendererSettings& settings, const fs::path& shader_dir);
void initialize(Context& ctx, const RendererSettings& settings, const fs::path& shader_dir, Assets& assets);

ShaderProgram& get(const std::string& name) const;
ShaderProgram& get(ShaderType material_type, InstanceType model_type, uint64_t material_index) const;
Expand Down
10 changes: 10 additions & 0 deletions include/limitless/text/font_atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace Limitless {
explicit font_error(const std::string& error) : runtime_error{error} {}
};

class Assets;

class FontAtlas {
public:
static std::shared_ptr<FontAtlas> load(
Expand All @@ -64,6 +66,14 @@ namespace Limitless {
std::optional<CjkVariant> _cjk_variant = std::nullopt
);

static std::shared_ptr<FontAtlas> load(
Assets& assets,
const fs::path& path,
uint32_t pixel_size,
std::vector<std::pair<uint32_t, uint32_t>> codepoint_ranges = {},
std::optional<CjkVariant> _cjk_variant = std::nullopt
);

static std::shared_ptr<FontAtlas> make(
uint32_t font_size_in_pixels,
std::unordered_map<uint32_t, std::shared_ptr<Texture>> icons
Expand Down
24 changes: 23 additions & 1 deletion src/limitless/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,31 @@

#include <utility>
#include <iostream>
#include <fstream>

using namespace Limitless;

std::vector<std::uint8_t> Assets::readFile(const fs::path& path) const {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file) {
throw std::runtime_error("Failed to read file: " + path.string());
}
const auto size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::uint8_t> data(static_cast<size_t>(std::max<std::streamoff>(size, 0)));
if (size > 0) {
file.read(reinterpret_cast<char*>(data.data()), size);
if (file.gcount() != size) {
throw std::runtime_error("Truncated file: " + path.string());
}
}
return data;
}

bool Assets::fileExists(const fs::path& path) const {
return fs::is_regular_file(path);
}

Assets::Assets(const fs::path& _base_dir) noexcept
: base_dir {_base_dir}
, shader_dir {_base_dir / "../shaders"} {
Expand Down Expand Up @@ -105,7 +127,7 @@ void Assets::load([[maybe_unused]] Context& context) {
}

void Assets::initialize(Context& ctx, const RendererSettings& settings) {
shaders.initialize(ctx, settings, shader_dir);
shaders.initialize(ctx, settings, shader_dir, *this);
}

void Assets::add(const Assets& other) {
Expand Down
27 changes: 25 additions & 2 deletions src/limitless/core/shader/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@

using namespace Limitless;

Shader::Shader(fs::path _path, Type _type, const ShaderAction& action)
#include <limitless/core/shader/shader.hpp>
#include <limitless/assets.hpp>
#include <limitless/core/context_initializer.hpp>
#include <limitless/core/keyline_extensions.hpp>
#include <string>
#include <sstream>
#include <limitless/core/shader/shader_extensions.hpp>

using namespace Limitless;

Shader::Shader(fs::path _path, Type _type, const ShaderAction& action, Assets* _assets)
: path {std::move(_path)}
, type {_type} {
, type {_type}
, assets {_assets} {
source = getSource(path);

replaceIncludes(path.parent_path());
Expand Down Expand Up @@ -86,6 +97,14 @@ void Shader::replaceKey(const std::string& key, const std::string& value) noexce

std::string Shader::getSource(const fs::path& filepath) {
try {
if (assets) {
if (!assets->fileExists(filepath)) {
throw shader_file_not_found(filepath.string());
}
const auto bytes = assets->readFile(filepath);
return std::string(reinterpret_cast<const char*>(bytes.data()), bytes.size());
}

std::ifstream file(filepath);
std::string file_source;

Expand All @@ -99,6 +118,8 @@ std::string Shader::getSource(const fs::path& filepath) {
}

return file_source;
} catch (const shader_file_not_found&) {
throw;
} catch (...) {
throw shader_file_not_found(filepath.string());
}
Expand Down Expand Up @@ -157,6 +178,8 @@ void Limitless::swap(Shader &lhs, Shader &rhs) noexcept {
swap(lhs.path, rhs.path);
swap(lhs.type, rhs.type);
swap(lhs.id, rhs.id);
swap(lhs.assets, rhs.assets);
swap(lhs.include_entries, rhs.include_entries);
}

Shader::Shader(Shader&& rhs) noexcept : Shader() {
Expand Down
12 changes: 7 additions & 5 deletions src/limitless/core/shader/shader_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

using namespace Limitless;

ShaderCompiler::ShaderCompiler(Context& _context)
: context {_context} {
ShaderCompiler::ShaderCompiler(Context& _context, Assets* _assets)
: context {_context}
, assets {_assets} {
}

ShaderCompiler::ShaderCompiler(Context& _context, const RendererSettings& _settings)
ShaderCompiler::ShaderCompiler(Context& _context, const RendererSettings& _settings, Assets* _assets)
: context {_context}
, render_settings {_settings} {
, render_settings {_settings}
, assets {_assets} {
}

void ShaderCompiler::checkStatus(const GLuint program_id) {
Expand Down Expand Up @@ -77,7 +79,7 @@ std::shared_ptr<ShaderProgram> ShaderCompiler::compile(const fs::path& path, con
uint8_t shader_count {};
for (const auto& [extension, type] : shader_file_extensions) {
try {
Shader shader { path.string() + extension.data(), type, action };
Shader shader { path.string() + extension.data(), type, action, assets };

replaceCommonDefines(shader);

Expand Down
Loading