Compile IW4 pixel shaders as part of the zone building process

This commit is contained in:
Jan 2022-09-17 16:07:38 +02:00
parent dd359d8f6a
commit 08278c451a
8 changed files with 191 additions and 75 deletions

View File

@ -7,7 +7,7 @@ struct VSInput
struct VSOutput struct VSOutput
{ {
float4 position : POSITION; float4 position : SV_POSITION;
half4 color : COLOR0; half4 color : COLOR0;
half2 texcoord : TEXCOORD0; half2 texcoord : TEXCOORD0;
}; };

View File

@ -8,6 +8,13 @@
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Game/IW4/IW4.h" #include "Game/IW4/IW4.h"
#include "Pool/GlobalAssetPool.h" #include "Pool/GlobalAssetPool.h"
#include "Shader/ShaderIncludeHandler.h"
#ifdef OS_TARGET_WINDOWS
#include <d3dcompiler.h>
#pragma comment(lib,"d3dcompiler.lib")
#endif
using namespace IW4; using namespace IW4;
@ -24,16 +31,95 @@ bool AssetLoaderPixelShader::CanLoadFromRaw() const
return true; return true;
} }
std::string AssetLoaderPixelShader::GetFileNameForAsset(const std::string& assetName) std::string AssetLoaderPixelShader::GetFileNameForSourceShader(const std::string& assetName)
{
std::ostringstream ss;
ss << "shader/" << assetName;
return ss.str();
}
bool AssetLoaderPixelShader::LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{
#ifdef OS_TARGET_WINDOWS
const auto fileName = GetFileNameForSourceShader(assetName);
auto file = searchPath->Open(fileName);
if (!file.IsOpen() || file.m_length <= 0)
return false;
if (file.m_length > ShaderIncludeHandler::MAX_SHADER_SIZE)
{
std::cerr << "Invalid pixel shader source \"" << assetName << "\": File too big: " << file.m_length << "\n";
return false;
}
const auto shaderSize = static_cast<size_t>(file.m_length);
const auto shaderData = std::make_unique<char[]>(shaderSize);
file.m_stream->read(shaderData.get(), shaderSize);
file.m_stream.reset();
constexpr unsigned shaderFlags = D3DCOMPILE_OPTIMIZATION_LEVEL1
#ifdef _DEBUG
| D3DCOMPILE_DEBUG
#endif
;
ShaderIncludeHandler shaderIncluder(searchPath);
ID3DBlob* shaderBlob = nullptr;
ID3DBlob* errorBlob = nullptr;
const auto errorCode = D3DCompile(shaderData.get(), shaderSize, assetName.c_str(), nullptr, &shaderIncluder, "PSMain", "ps_3_0", shaderFlags, 0u, &shaderBlob, &errorBlob);
if (FAILED(errorCode))
{
std::cerr << "Invalid pixel shader \"" << assetName << "\": Compilation error\n";
if (errorBlob)
{
std::cerr << " " << static_cast<char*>(errorBlob->GetBufferPointer()) << "\n";
errorBlob->Release();
}
if (shaderBlob)
shaderBlob->Release();
return false;
}
std::cout << "Compiled pixel shader \"" << assetName << "\"\n";
const auto shaderBlobSize = static_cast<size_t>(shaderBlob->GetBufferSize());
assert(shaderBlobSize % sizeof(uint32_t) == 0);
auto* pixelShader = memory->Create<MaterialPixelShader>();
pixelShader->name = memory->Dup(assetName.c_str());
pixelShader->prog.loadDef.programSize = static_cast<uint16_t>(shaderBlobSize / sizeof(uint32_t));
pixelShader->prog.loadDef.loadForRenderer = 0;
pixelShader->prog.ps = nullptr;
auto* assetShaderBuffer = static_cast<char*>(memory->Alloc(shaderBlobSize));
memcpy(assetShaderBuffer, shaderBlob->GetBufferPointer(), shaderBlobSize);
pixelShader->prog.loadDef.program = reinterpret_cast<uint32_t*>(assetShaderBuffer);
manager->AddAsset(ASSET_TYPE_PIXELSHADER, assetName, pixelShader);
shaderBlob->Release();
return true;
#else
// Shader compilation is only support with Windows
return false;
#endif
}
std::string AssetLoaderPixelShader::GetFileNameForCompiledShader(const std::string& assetName)
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "shader_bin/ps_" << assetName << ".cso"; ss << "shader_bin/ps_" << assetName << ".cso";
return ss.str(); return ss.str();
} }
bool AssetLoaderPixelShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const bool AssetLoaderPixelShader::LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{ {
const auto fileName = GetFileNameForAsset(assetName); const auto fileName = GetFileNameForCompiledShader(assetName);
const auto file = searchPath->Open(fileName); const auto file = searchPath->Open(fileName);
if (!file.IsOpen()) if (!file.IsOpen())
return false; return false;
@ -60,3 +146,8 @@ bool AssetLoaderPixelShader::LoadFromRaw(const std::string& assetName, ISearchPa
return true; return true;
} }
bool AssetLoaderPixelShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
return LoadFromSource(assetName, searchPath, memory, manager) || LoadCompiled(assetName, searchPath, memory, manager);
}

View File

@ -11,7 +11,11 @@ namespace IW4
class AssetLoaderPixelShader final : public BasicAssetLoader<ASSET_TYPE_PIXELSHADER, MaterialPixelShader> class AssetLoaderPixelShader final : public BasicAssetLoader<ASSET_TYPE_PIXELSHADER, MaterialPixelShader>
{ {
public: public:
_NODISCARD static std::string GetFileNameForAsset(const std::string& assetName); _NODISCARD static std::string GetFileNameForSourceShader(const std::string& assetName);
static bool LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
_NODISCARD static std::string GetFileNameForCompiledShader(const std::string& assetName);
static bool LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override; _NODISCARD bool CanLoadFromRaw() const override;

View File

@ -503,7 +503,7 @@ namespace IW4
if (pass.m_pixel_shader->Asset()->name && pass.m_pixel_shader->Asset()->name[0] == ',') if (pass.m_pixel_shader->Asset()->name && pass.m_pixel_shader->Asset()->name[0] == ',')
{ {
pass.m_pixel_shader_info = m_shader_info_cache->LoadShaderInfoFromDisk(m_search_path, AssetLoaderPixelShader::GetFileNameForAsset(pixelShaderName)); pass.m_pixel_shader_info = m_shader_info_cache->LoadShaderInfoFromDisk(m_search_path, AssetLoaderPixelShader::GetFileNameForCompiledShader(pixelShaderName));
} }
else else
{ {

View File

@ -4,11 +4,11 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector>
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Game/IW4/IW4.h" #include "Game/IW4/IW4.h"
#include "Pool/GlobalAssetPool.h" #include "Pool/GlobalAssetPool.h"
#include "Shader/ShaderIncludeHandler.h"
#ifdef OS_TARGET_WINDOWS #ifdef OS_TARGET_WINDOWS
#include <d3dcompiler.h> #include <d3dcompiler.h>
@ -18,65 +18,6 @@
using namespace IW4; using namespace IW4;
class ShaderIncluder : public ID3DInclude
{
static constexpr auto MAX_SHADER_SIZE = 0x1900000u;
public:
ShaderIncluder(ISearchPath* searchPath)
: m_search_path(searchPath)
{
}
virtual ~ShaderIncluder() = default;
HRESULT __stdcall Open(D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes) override
{
std::ostringstream ss;
ss << "shaders/" << pFileName;
auto file = m_search_path->Open(ss.str());
if (!file.IsOpen() || file.m_length <= 0)
return E_FAIL;
if (file.m_length > MAX_SHADER_SIZE)
{
std::cerr << "Invalid shader source \"" << pFileName << "\": File too big: " << file.m_length << "\n";
return false;
}
const auto shaderSize = static_cast<size_t>(file.m_length);
auto shaderData = std::make_unique<char[]>(shaderSize);
file.m_stream->read(shaderData.get(), shaderSize);
file.m_stream.reset();
*ppData = shaderData.get();
*pBytes = shaderSize;
m_file_buffers_in_use.push_back(std::move(shaderData));
return S_OK;
}
HRESULT __stdcall Close(LPCVOID pData) override
{
for (auto i = m_file_buffers_in_use.begin(); i != m_file_buffers_in_use.end(); ++i)
{
if (i->get() == pData)
{
m_file_buffers_in_use.erase(i);
return S_OK;
}
}
return E_FAIL;
}
private:
ISearchPath* m_search_path;
std::vector<std::unique_ptr<char[]>> m_file_buffers_in_use;
};
void* AssetLoaderVertexShader::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) void* AssetLoaderVertexShader::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{ {
auto* vertexShader = memory->Create<MaterialVertexShader>(); auto* vertexShader = memory->Create<MaterialVertexShader>();
@ -97,7 +38,7 @@ std::string AssetLoaderVertexShader::GetFileNameForSourceShader(const std::strin
return ss.str(); return ss.str();
} }
bool AssetLoaderVertexShader::LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) bool AssetLoaderVertexShader::LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{ {
#ifdef OS_TARGET_WINDOWS #ifdef OS_TARGET_WINDOWS
const auto fileName = GetFileNameForSourceShader(assetName); const auto fileName = GetFileNameForSourceShader(assetName);
@ -105,7 +46,7 @@ bool AssetLoaderVertexShader::LoadFromSource(const std::string& assetName, ISear
if (!file.IsOpen() || file.m_length <= 0) if (!file.IsOpen() || file.m_length <= 0)
return false; return false;
if (file.m_length > MAX_SHADER_SIZE) if (file.m_length > ShaderIncludeHandler::MAX_SHADER_SIZE)
{ {
std::cerr << "Invalid vertex shader source \"" << assetName << "\": File too big: " << file.m_length << "\n"; std::cerr << "Invalid vertex shader source \"" << assetName << "\": File too big: " << file.m_length << "\n";
return false; return false;
@ -122,7 +63,7 @@ bool AssetLoaderVertexShader::LoadFromSource(const std::string& assetName, ISear
#endif #endif
; ;
ShaderIncluder shaderIncluder(searchPath); ShaderIncludeHandler shaderIncluder(searchPath);
ID3DBlob* shaderBlob = nullptr; ID3DBlob* shaderBlob = nullptr;
ID3DBlob* errorBlob = nullptr; ID3DBlob* errorBlob = nullptr;
@ -176,7 +117,7 @@ std::string AssetLoaderVertexShader::GetFileNameForCompiledShader(const std::str
return ss.str(); return ss.str();
} }
bool AssetLoaderVertexShader::LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) bool AssetLoaderVertexShader::LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{ {
const auto fileName = GetFileNameForCompiledShader(assetName); const auto fileName = GetFileNameForCompiledShader(assetName);
const auto file = searchPath->Open(fileName); const auto file = searchPath->Open(fileName);
@ -208,5 +149,5 @@ bool AssetLoaderVertexShader::LoadCompiled(const std::string& assetName, ISearch
bool AssetLoaderVertexShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const bool AssetLoaderVertexShader::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{ {
return LoadFromSource(assetName, searchPath, memory, manager, zone) || LoadCompiled(assetName, searchPath, memory, manager, zone); return LoadFromSource(assetName, searchPath, memory, manager) || LoadCompiled(assetName, searchPath, memory, manager);
} }

View File

@ -8,14 +8,12 @@ namespace IW4
{ {
class AssetLoaderVertexShader final : public BasicAssetLoader<ASSET_TYPE_VERTEXSHADER, MaterialVertexShader> class AssetLoaderVertexShader final : public BasicAssetLoader<ASSET_TYPE_VERTEXSHADER, MaterialVertexShader>
{ {
static constexpr auto MAX_SHADER_SIZE = 0x1900000u;
public: public:
_NODISCARD static std::string GetFileNameForSourceShader(const std::string& assetName); _NODISCARD static std::string GetFileNameForSourceShader(const std::string& assetName);
static bool LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone); static bool LoadFromSource(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
_NODISCARD static std::string GetFileNameForCompiledShader(const std::string& assetName); _NODISCARD static std::string GetFileNameForCompiledShader(const std::string& assetName);
static bool LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone); static bool LoadCompiled(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override; _NODISCARD bool CanLoadFromRaw() const override;

View File

@ -0,0 +1,57 @@
#include "ShaderIncludeHandler.h"
#include <iostream>
#include <sstream>
#ifdef OS_TARGET_WINDOWS
ShaderIncludeHandler::ShaderIncludeHandler(ISearchPath* searchPath)
: m_search_path(searchPath)
{
}
ShaderIncludeHandler::~ShaderIncludeHandler() = default;
HRESULT ShaderIncludeHandler::Open(D3D_INCLUDE_TYPE includeType, const LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes)
{
std::ostringstream ss;
ss << "shaders/" << pFileName;
auto file = m_search_path->Open(ss.str());
if (!file.IsOpen() || file.m_length <= 0)
return E_FAIL;
if (file.m_length > MAX_SHADER_SIZE)
{
std::cerr << "Invalid shader source \"" << pFileName << "\": File too big: " << file.m_length << "\n";
return E_FAIL;
}
const auto shaderSize = static_cast<size_t>(file.m_length);
auto shaderData = std::make_unique<char[]>(shaderSize);
file.m_stream->read(shaderData.get(), shaderSize);
file.m_stream.reset();
*ppData = shaderData.get();
*pBytes = shaderSize;
m_file_buffers_in_use.push_back(std::move(shaderData));
return S_OK;
}
HRESULT ShaderIncludeHandler::Close(const LPCVOID pData)
{
for (auto i = m_file_buffers_in_use.begin(); i != m_file_buffers_in_use.end(); ++i)
{
if (i->get() == pData)
{
m_file_buffers_in_use.erase(i);
return S_OK;
}
}
return E_FAIL;
}
#endif

View File

@ -0,0 +1,25 @@
#pragma once
#include "SearchPath/ISearchPath.h"
// Shader compilation only available on Windows
#ifdef OS_TARGET_WINDOWS
#include <d3dcompiler.h>
class ShaderIncludeHandler : public ID3DInclude
{
public:
static constexpr auto MAX_SHADER_SIZE = 0x1900000u;
ShaderIncludeHandler(ISearchPath* searchPath);
virtual ~ShaderIncludeHandler();
HRESULT __declspec(nothrow) __stdcall Open(D3D_INCLUDE_TYPE includeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes) override;
HRESULT __declspec(nothrow) __stdcall Close(LPCVOID pData) override;
private:
ISearchPath* m_search_path;
std::vector<std::unique_ptr<char[]>> m_file_buffers_in_use;
};
#endif