I gues Shader2 starting to come together.

This commit is contained in:
2024-12-23 23:21:47 -06:00
parent 698aeb2afc
commit 2b36a12335
9 changed files with 179 additions and 56 deletions

View File

@@ -17,7 +17,8 @@ ShaderLoader::ShaderLoader(
const std::string name
) :
AssetLoader(assetManager, name),
state(ShaderLoaderState::INITIAL)
state(ShaderLoaderState::INITIAL),
shader(std::make_shared<ShaderProgram2>())
{
}
@@ -26,10 +27,10 @@ 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
@@ -43,21 +44,13 @@ void ShaderLoader::updateSync() {
int32_t definedEntryPointCount = module->getDefinedEntryPointCount();
IComponentType** components = new IComponentType*[definedEntryPointCount + 1];
int32_t componentCount = 0;
components[componentCount++] = module;// First component is module.
components[componentCount++] = module;
// Get the entry point info and append to components list.
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));
if(result != SLANG_OK) assertUnreachable("Failed to get entry point.");
components[componentCount++] = ep;
}
@@ -80,6 +73,42 @@ void ShaderLoader::updateSync() {
return;
}
// Create the shader program.
Slang::ComPtr<IBlob> blob;
slang::ProgramLayout* layout = program->getLayout();
std::vector<std::shared_ptr<ShaderEntry>> shaderEntries;
for(auto i = 0; i < definedEntryPointCount; i++) {
// Get the code
auto result = linkedProgram->getEntryPointCode(
i,
0,
blob.writeRef(),
diagnostics.writeRef()
);
if(diagnostics) {
assertUnreachable("%s\n", (const char*) diagnostics->getBufferPointer());
}
// Get the stage information
auto entryPointReflection = layout->getEntryPointByIndex(0);
auto stage = entryPointReflection->getStage();
// Create the shader entry
auto shaderEntry = std::make_shared<ShaderEntry>();
shaderEntry->init(
stage,
std::string((const char*)blob->getBufferPointer())
);
// Add to the list
shaderEntries.push_back(shaderEntry);
}
// Create the shader program.
shader->init(shaderEntries);
// Finished loading.
delete [] components;
this->state = ShaderLoaderState::LOADED;
this->loaded = true;
@@ -89,38 +118,14 @@ 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());
std::shared_ptr<IShaderProgram2> ShaderLoader::getShader() {
assertNotNull(shader, "ShaderLoader shader is null.");
return shader;
}
ShaderLoader::~ShaderLoader() {
shader = nullptr;
if(linkedProgram) {
linkedProgram->release();
linkedProgram = nullptr;

View File

@@ -7,6 +7,7 @@
#include "asset/AssetLoader.hpp"
#include "asset/AssetDataLoader.hpp"
#include "display/shader/ShaderManager.hpp"
#include "display/shader/ShaderProgram2.hpp"
namespace Dawn {
enum class ShaderLoaderState {
@@ -18,13 +19,14 @@ namespace Dawn {
class ShaderLoader : public AssetLoader {
protected:
enum ShaderLoaderState state;
std::shared_ptr<ShaderProgram2> shader;
public:
const static std::string ASSET_TYPE;
std::vector<std::string> entryPoints;
Slang::ComPtr<IComponentType> linkedProgram;
Slang::ComPtr<IComponentType> program;
IModule* module;
ShaderLoader(
@@ -34,7 +36,14 @@ namespace Dawn {
void updateSync() override;
void updateAsync() override;
std::string getAssetType() const override;
std::string getEntryPointCode(const std::string &entryPoint);
/**
* Retreives the shader program for this loader.
*
* @return std::shared_ptr<IShaderProgram2>
*/
std::shared_ptr<IShaderProgram2> getShader();
~ShaderLoader();
};
}