From d0e8e945611c6097dd42f9169009d008c255736c Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 25 Mar 2022 18:35:22 +0100 Subject: [PATCH] Load vertex shader and pixel shader from raw --- .../AssetLoaders/AssetLoaderPixelShader.cpp | 40 +++++++++++++++++++ .../IW4/AssetLoaders/AssetLoaderPixelShader.h | 2 + .../AssetLoaders/AssetLoaderVertexShader.cpp | 40 +++++++++++++++++++ .../AssetLoaders/AssetLoaderVertexShader.h | 2 + 4 files changed, 84 insertions(+) diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.cpp b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.cpp index 2a7a1ced..02fcaa09 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.cpp +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.cpp @@ -1,6 +1,9 @@ #include "AssetLoaderPixelShader.h" +#include #include +#include +#include #include "ObjLoading.h" #include "Game/IW4/IW4.h" @@ -15,3 +18,40 @@ void* AssetLoaderPixelShader::CreateEmptyAsset(const std::string& assetName, Mem pixelShader->name = memory->Dup(assetName.c_str()); return pixelShader; } + +bool AssetLoaderPixelShader::CanLoadFromRaw() const +{ + return true; +} + +bool AssetLoaderPixelShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +{ + std::ostringstream ss; + ss << "shader_bin/ps_" << assetName << ".cso"; + + const auto file = searchPath->Open(assetName); + if (!file.IsOpen()) + return false; + + if(file.m_length % sizeof(uint32_t) != 0) + { + std::cerr << "Invalid pixel shader \"" << assetName << "\": Size must be dividable by " << sizeof(uint32_t) << "\n"; + return false; + } + + auto* pixelShader = memory->Create(); + pixelShader->name = memory->Dup(assetName.c_str()); + pixelShader->prog.loadDef.programSize = static_cast(static_cast(file.m_length) / sizeof(uint32_t)); + pixelShader->prog.loadDef.loadForRenderer = 0; + pixelShader->prog.ps = nullptr; + + auto* fileBuffer = static_cast(memory->Alloc(pixelShader->prog.loadDef.programSize * sizeof(uint32_t))); + file.m_stream->read(fileBuffer, static_cast(pixelShader->prog.loadDef.programSize) * sizeof(uint32_t)); + if (file.m_stream->gcount() != file.m_length) + return false; + + pixelShader->prog.loadDef.program = reinterpret_cast(fileBuffer); + manager->AddAsset(ASSET_TYPE_PIXELSHADER, assetName, pixelShader); + + return true; +} diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.h b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.h index f85a4c4a..4541dbb7 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.h +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderPixelShader.h @@ -10,5 +10,7 @@ namespace IW4 { public: _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; + _NODISCARD bool CanLoadFromRaw() const override; + bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; }; } diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.cpp b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.cpp index b977d35a..c747965d 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.cpp +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.cpp @@ -1,6 +1,9 @@ #include "AssetLoaderVertexShader.h" +#include #include +#include +#include #include "ObjLoading.h" #include "Game/IW4/IW4.h" @@ -15,3 +18,40 @@ void* AssetLoaderVertexShader::CreateEmptyAsset(const std::string& assetName, Me vertexShader->name = memory->Dup(assetName.c_str()); return vertexShader; } + +bool AssetLoaderVertexShader::CanLoadFromRaw() const +{ + return true; +} + +bool AssetLoaderVertexShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const +{ + std::ostringstream ss; + ss << "shader_bin/vs_" << assetName << ".cso"; + + const auto file = searchPath->Open(assetName); + if (!file.IsOpen()) + return false; + + if (file.m_length % sizeof(uint32_t) != 0) + { + std::cerr << "Invalid vertex shader \"" << assetName << "\": Size must be dividable by " << sizeof(uint32_t) << "\n"; + return false; + } + + auto* vertexShader = memory->Create(); + vertexShader->name = memory->Dup(assetName.c_str()); + vertexShader->prog.loadDef.programSize = static_cast(static_cast(file.m_length) / sizeof(uint32_t)); + vertexShader->prog.loadDef.loadForRenderer = 0; + vertexShader->prog.vs = nullptr; + + auto* fileBuffer = static_cast(memory->Alloc(vertexShader->prog.loadDef.programSize * sizeof(uint32_t))); + file.m_stream->read(fileBuffer, static_cast(vertexShader->prog.loadDef.programSize) * sizeof(uint32_t)); + if (file.m_stream->gcount() != file.m_length) + return false; + + vertexShader->prog.loadDef.program = reinterpret_cast(fileBuffer); + manager->AddAsset(ASSET_TYPE_VERTEXSHADER, assetName, vertexShader); + + return true; +} diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.h b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.h index 647543de..8084d2c3 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.h +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderVertexShader.h @@ -10,5 +10,7 @@ namespace IW4 { public: _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; + _NODISCARD bool CanLoadFromRaw() const override; + bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; }; }