Some slang progress

This commit is contained in:
2024-12-17 12:32:44 -06:00
parent b3c2e0114f
commit b5958189cf
33 changed files with 617 additions and 255 deletions

View File

@@ -5,6 +5,7 @@
#include "AssetLoader.hpp"
#include "assert/assert.hpp"
#include "asset/AssetManager.hpp"
using namespace Dawn;
@@ -27,6 +28,12 @@ std::shared_ptr<AssetManager> AssetLoader::getAssetManager() {
return am;
}
void AssetLoader::loadImmediately() {
while(!this->loaded) {
this->getAssetManager()->update();
}
}
AssetLoader::~AssetLoader() {
this->loaded = false;
}

View File

@@ -57,6 +57,11 @@ namespace Dawn {
* @return The asset manager.
*/
std::shared_ptr<AssetManager> getAssetManager();
/**
* Load the asset immediately, this is blocking on the main thread.
*/
void loadImmediately();
/**
* Dispose the asset item.

View File

@@ -9,6 +9,8 @@ target_sources(${DAWN_TARGET_NAME}
TextureLoader.cpp
JSONLoader.cpp
TrueTypeLoader.cpp
ShaderLoader.cpp
StringLoader.cpp
)
# Subdirs

View File

@@ -0,0 +1,137 @@
// 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"
#include <fstream>
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)
{
}
void ShaderLoader::updateAsync() {
}
void ShaderLoader::updateSync() {
if(state != ShaderLoaderState::INITIAL) return;
this->state = ShaderLoaderState::LOADING;
assertFalse(loaded, "ShaderLoader already loaded.");
auto sm = this->getAssetManager()->getGame()->shaderManager;
// Load the shader string
Slang::ComPtr<IBlob> diagnostics;
module = sm->session->loadModule(
this->name.c_str(),
diagnostics.writeRef()
);
// Get list of entry points and create components
int32_t definedEntryPointCount = module->getDefinedEntryPointCount();
IComponentType** components = new IComponentType*[definedEntryPointCount + 1];
int32_t j = 0;
components[j++] = module;
for(auto i = 0; i < definedEntryPointCount; i++) {
Slang::ComPtr<IEntryPoint> ep;
auto result = module->getDefinedEntryPoint(i, ep.writeRef());
if(result != SLANG_OK) {
assertUnreachable("Failed to get entry point.");
return;
}
auto name = ep->getFunctionReflection()->getName();
std::cout << "Found entry point: " << name << std::endl;
entryPoints.push_back(std::string(name));
components[j++] = ep;
}
// Create the composite component type
sm->session->createCompositeComponentType(
components,
sizeof(components) / sizeof(components[0]),
program.writeRef()
);
// Link the program.
auto result = program->link(linkedProgram.writeRef(), diagnostics.writeRef());
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
return;
}
// result
Slang::ComPtr<IBlob> blob;
auto result2 = linkedProgram->getEntryPointCode(
0,
0,
blob.writeRef(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
}
this->state = ShaderLoaderState::LOADED;
this->loaded = true;
}
std::string ShaderLoader::getAssetType() const {
return ShaderLoader::ASSET_TYPE;
}
std::string ShaderLoader::getEntryPointCode(const std::string &entryPoint) {
assertTrue(loaded, "ShaderLoader not loaded.");
assertNotNull(linkedProgram, "ShaderLoader linkedProgram is null.");
// Get the entry point index
int32_t entryIndex = -1;
for(auto i = 0; i < entryPoints.size(); i++) {
if(entryPoints[i] != entryPoint) continue;
entryIndex = i;
break;
}
assertTrue(entryIndex != -1, "EntryPoint not found.");
// Find the entry point code
Slang::ComPtr<IBlob> blob;
Slang::ComPtr<IBlob> diagnostics;
auto result = linkedProgram->getEntryPointCode(
entryIndex,
0,
blob.writeRef(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
return "";
}
return std::string((const char*)blob->getBufferPointer());
}
ShaderLoader::~ShaderLoader() {
if(linkedProgram) {
linkedProgram->release();
linkedProgram = nullptr;
}
}

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "asset/AssetLoader.hpp"
#include "asset/AssetDataLoader.hpp"
#include "display/shader/ShaderManager.hpp"
namespace Dawn {
enum class ShaderLoaderState {
INITIAL,
LOADING,
LOADED
};
class ShaderLoader : public AssetLoader {
protected:
enum ShaderLoaderState state;
public:
const static std::string ASSET_TYPE;
std::vector<std::string> entryPoints;
Slang::ComPtr<IComponentType> linkedProgram;
Slang::ComPtr<IComponentType> program;
IModule* module;
ShaderLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
void updateSync() override;
void updateAsync() override;
std::string getAssetType() const override;
std::string getEntryPointCode(const std::string &entryPoint);
~ShaderLoader();
};
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "StringLoader.hpp"
using namespace Dawn;
const std::string StringLoader::ASSET_TYPE = "string";
StringLoader::StringLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
) :
AssetLoader(assetManager, name),
loader(name),
state(StringLoaderState::INITIAL)
{
}
void StringLoader::updateSync() {
}
void StringLoader::updateAsync() {
if(this->state != StringLoaderState::INITIAL) return;
this->state = StringLoaderState::LOADING_STRING;
this->data = this->loader.getEntireContentsAsString();
this->state = StringLoaderState::DONE;
this->loaded = true;
}
std::string StringLoader::getAssetType() const {
return StringLoader::ASSET_TYPE;
}
StringLoader::~StringLoader() {
}

View File

@@ -0,0 +1,36 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "asset/AssetLoader.hpp"
#include "asset/AssetDataLoader.hpp"
namespace Dawn {
enum class StringLoaderState {
INITIAL,
LOADING_STRING,
DONE
};
class StringLoader : public AssetLoader {
protected:
AssetDataLoader loader;
enum StringLoaderState state;
public:
const static std::string ASSET_TYPE;
std::string data;
StringLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name
);
void updateSync() override;
void updateAsync() override;
std::string getAssetType() const override;
~StringLoader();
};
}