Dawn/src/dawn/asset/loader/ShaderLoader.cpp

60 lines
1.4 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ShaderLoader.hpp"
#include "assert/assert.hpp"
#include "asset/AssetManager.hpp"
#include "game/Game.hpp"
using namespace Dawn;
const std::string ShaderLoader::ASSET_TYPE = "shader";
ShaderLoader::ShaderLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
state(ShaderLoaderState::INITIAL),
shader(std::make_shared<ShaderProgram>())
{
}
void ShaderLoader::updateAsync() {
}
void ShaderLoader::updateSync() {
if(state != ShaderLoaderState::INITIAL) return;
this->state = ShaderLoaderState::LOADING;
assertFalse(loaded, "ShaderLoader already loaded.");
// Shorthand.
auto sm = this->getAssetManager()->getGame()->shaderManager;
// Load the shader string
Slang::ComPtr<IBlob> diagnostics;
auto module = sm->session->loadModule(
this->name.c_str(),
diagnostics.writeRef()
);
shader->init(module, sm->session);
// Finished loading.
this->state = ShaderLoaderState::LOADED;
this->loaded = true;
}
std::string ShaderLoader::getAssetType() const {
return ShaderLoader::ASSET_TYPE;
}
std::shared_ptr<ShaderProgram> ShaderLoader::getShader() {
assertNotNull(shader, "ShaderLoader shader is null.");
return shader;
}
ShaderLoader::~ShaderLoader() {
shader = nullptr;
}