mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-04-21 10:58:44 +00:00
feat: add shader compiler for IW4
This commit is contained in:
@@ -4,6 +4,11 @@
|
||||
|
||||
namespace shader
|
||||
{
|
||||
std::string GetSourceFileNameForShaderAssetName(const std::string& assetName)
|
||||
{
|
||||
return std::format("shader/{}", assetName);
|
||||
}
|
||||
|
||||
std::string GetFileNameForPixelShaderAssetName(const std::string& assetName)
|
||||
{
|
||||
return std::format("shader_bin/ps_{}.cso", assetName);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
namespace shader
|
||||
{
|
||||
std::string GetSourceFileNameForShaderAssetName(const std::string& assetName);
|
||||
|
||||
std::string GetFileNameForPixelShaderAssetName(const std::string& assetName);
|
||||
std::string GetFileNameForVertexShaderAssetName(const std::string& assetName);
|
||||
} // namespace shader
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "Game/IW4/MaterialConstantsIW4.h"
|
||||
#include "Game/IW4/ObjConstantsIW4.h"
|
||||
#include "Game/IW4/Techset/CompilerTechsetIW4.h"
|
||||
#include "Game/IW4/Techset/TechsetConstantsIW4.h"
|
||||
#include "Gdt/AbstractGdtEntryReader.h"
|
||||
#include "Gdt/IGdtQueryable.h"
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "Game/IW4/Techset/VertexDeclCompilerIW4.h"
|
||||
#include "Image/ImageIwdPostProcessor.h"
|
||||
#include "Material/CompilerMaterialIW4.h"
|
||||
#include "Techset/PixelShaderCompilerIW4.h"
|
||||
#include "Techset/VertexShaderCompilerIW4.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
@@ -21,6 +23,8 @@ namespace
|
||||
collection.AddAssetCreator(material::CreateCompilerIW4(memory, searchPath, gdt));
|
||||
#endif
|
||||
collection.AddAssetCreator(techset::CreateVertexDeclCompilerIW4(memory));
|
||||
collection.AddAssetCreator(techset::CreateVertexShaderCompilerIW4(memory, searchPath));
|
||||
collection.AddAssetCreator(techset::CreatePixelShaderCompilerIW4(memory, searchPath));
|
||||
collection.AddAssetCreator(techset::CreateTechsetCompilerIW4(memory, searchPath));
|
||||
|
||||
collection.AddSubAssetCreator(techset::CreateTechniqueCompilerIW4(memory, zone, searchPath));
|
||||
|
||||
117
src/ObjCompiling/Game/IW4/Techset/PixelShaderCompilerIW4.cpp
Normal file
117
src/ObjCompiling/Game/IW4/Techset/PixelShaderCompilerIW4.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "PixelShaderCompilerIW4.h"
|
||||
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "Shader/ShaderCommon.h"
|
||||
#include "Techset/ShaderIncludeHandler.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <d3dcompiler.h>
|
||||
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
#endif
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
namespace
|
||||
{
|
||||
class PixelShaderCompilerIW4 final : public AssetCreator<AssetPixelShader>
|
||||
{
|
||||
public:
|
||||
PixelShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath)
|
||||
: m_memory(memory),
|
||||
m_search_path(searchPath)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const auto fileName = shader::GetSourceFileNameForShaderAssetName(assetName);
|
||||
auto file = m_search_path.Open(fileName);
|
||||
if (!file.IsOpen() || file.m_length <= 0)
|
||||
return AssetCreationResult::NoAction();
|
||||
|
||||
if (std::cmp_greater(file.m_length, techset::ShaderIncludeHandler::MAX_SHADER_SIZE))
|
||||
{
|
||||
con::error("Invalid shader source \"{}\": File too big: {}", assetName, file.m_length);
|
||||
return AssetCreationResult::Failure();
|
||||
}
|
||||
|
||||
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
|
||||
;
|
||||
|
||||
techset::ShaderIncludeHandler shaderIncluder(m_search_path);
|
||||
|
||||
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))
|
||||
{
|
||||
con::error("Invalid pixel shader \"{}\": Compilation error", assetName);
|
||||
|
||||
if (errorBlob)
|
||||
{
|
||||
std::cerr << " " << static_cast<char*>(errorBlob->GetBufferPointer()) << "\n";
|
||||
errorBlob->Release();
|
||||
}
|
||||
|
||||
if (shaderBlob)
|
||||
shaderBlob->Release();
|
||||
|
||||
return AssetCreationResult::Failure();
|
||||
}
|
||||
|
||||
con::info("Compiled pixel shader \"{}\"", assetName);
|
||||
|
||||
const auto shaderBlobSize = static_cast<size_t>(shaderBlob->GetBufferSize());
|
||||
assert(shaderBlobSize % sizeof(uint32_t) == 0);
|
||||
|
||||
auto* pixelShader = m_memory.Alloc<MaterialPixelShader>();
|
||||
pixelShader->name = m_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 = m_memory.Alloc<char>(shaderBlobSize);
|
||||
memcpy(assetShaderBuffer, shaderBlob->GetBufferPointer(), shaderBlobSize);
|
||||
pixelShader->prog.loadDef.program = reinterpret_cast<uint32_t*>(assetShaderBuffer);
|
||||
|
||||
shaderBlob->Release();
|
||||
|
||||
return AssetCreationResult::Success(context.AddAsset(AssetRegistration<AssetPixelShader>(assetName, pixelShader)));
|
||||
#else
|
||||
// Shader compilation is only support with Windows
|
||||
return AssetCreationResult::NoAction();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FinalizeZone(AssetCreationContext& context) override {}
|
||||
|
||||
private:
|
||||
MemoryManager& m_memory;
|
||||
ISearchPath& m_search_path;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CreatePixelShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath)
|
||||
{
|
||||
return std::make_unique<PixelShaderCompilerIW4>(memory, searchPath);
|
||||
}
|
||||
} // namespace techset
|
||||
11
src/ObjCompiling/Game/IW4/Techset/PixelShaderCompilerIW4.h
Normal file
11
src/ObjCompiling/Game/IW4/Techset/PixelShaderCompilerIW4.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CreatePixelShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath);
|
||||
}
|
||||
117
src/ObjCompiling/Game/IW4/Techset/VertexShaderCompilerIW4.cpp
Normal file
117
src/ObjCompiling/Game/IW4/Techset/VertexShaderCompilerIW4.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "VertexShaderCompilerIW4.h"
|
||||
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "Shader/ShaderCommon.h"
|
||||
#include "Techset/ShaderIncludeHandler.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <d3dcompiler.h>
|
||||
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
#endif
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
namespace
|
||||
{
|
||||
class VertexShaderCompilerIW4 final : public AssetCreator<AssetVertexShader>
|
||||
{
|
||||
public:
|
||||
VertexShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath)
|
||||
: m_memory(memory),
|
||||
m_search_path(searchPath)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
#ifdef _WIN32
|
||||
const auto fileName = shader::GetSourceFileNameForShaderAssetName(assetName);
|
||||
auto file = m_search_path.Open(fileName);
|
||||
if (!file.IsOpen() || file.m_length <= 0)
|
||||
return AssetCreationResult::NoAction();
|
||||
|
||||
if (std::cmp_greater(file.m_length, techset::ShaderIncludeHandler::MAX_SHADER_SIZE))
|
||||
{
|
||||
con::error("Invalid shader source \"{}\": File too big: {}", assetName, file.m_length);
|
||||
return AssetCreationResult::Failure();
|
||||
}
|
||||
|
||||
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
|
||||
;
|
||||
|
||||
techset::ShaderIncludeHandler shaderIncluder(m_search_path);
|
||||
|
||||
ID3DBlob* shaderBlob = nullptr;
|
||||
ID3DBlob* errorBlob = nullptr;
|
||||
const auto errorCode = D3DCompile(
|
||||
shaderData.get(), shaderSize, assetName.c_str(), nullptr, &shaderIncluder, "VSMain", "vs_3_0", shaderFlags, 0u, &shaderBlob, &errorBlob);
|
||||
|
||||
if (FAILED(errorCode))
|
||||
{
|
||||
con::error("Invalid vertex shader \"{}\": Compilation error", assetName);
|
||||
|
||||
if (errorBlob)
|
||||
{
|
||||
std::cerr << " " << static_cast<char*>(errorBlob->GetBufferPointer()) << "\n";
|
||||
errorBlob->Release();
|
||||
}
|
||||
|
||||
if (shaderBlob)
|
||||
shaderBlob->Release();
|
||||
|
||||
return AssetCreationResult::Failure();
|
||||
}
|
||||
|
||||
con::info("Compiled vertex shader \"{}\"", assetName);
|
||||
|
||||
const auto shaderBlobSize = static_cast<size_t>(shaderBlob->GetBufferSize());
|
||||
assert(shaderBlobSize % sizeof(uint32_t) == 0);
|
||||
|
||||
auto* vertexShader = m_memory.Alloc<MaterialVertexShader>();
|
||||
vertexShader->name = m_memory.Dup(assetName.c_str());
|
||||
vertexShader->prog.loadDef.programSize = static_cast<uint16_t>(shaderBlobSize / sizeof(uint32_t));
|
||||
vertexShader->prog.loadDef.loadForRenderer = 0;
|
||||
vertexShader->prog.vs = nullptr;
|
||||
|
||||
auto* assetShaderBuffer = m_memory.Alloc<char>(shaderBlobSize);
|
||||
memcpy(assetShaderBuffer, shaderBlob->GetBufferPointer(), shaderBlobSize);
|
||||
vertexShader->prog.loadDef.program = reinterpret_cast<uint32_t*>(assetShaderBuffer);
|
||||
|
||||
shaderBlob->Release();
|
||||
|
||||
return AssetCreationResult::Success(context.AddAsset(AssetRegistration<AssetVertexShader>(assetName, vertexShader)));
|
||||
#else
|
||||
// Shader compilation is only support with Windows
|
||||
return AssetCreationResult::NoAction();
|
||||
#endif
|
||||
}
|
||||
|
||||
void FinalizeZone(AssetCreationContext& context) override {}
|
||||
|
||||
private:
|
||||
MemoryManager& m_memory;
|
||||
ISearchPath& m_search_path;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CreateVertexShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath)
|
||||
{
|
||||
return std::make_unique<VertexShaderCompilerIW4>(memory, searchPath);
|
||||
}
|
||||
} // namespace techset
|
||||
11
src/ObjCompiling/Game/IW4/Techset/VertexShaderCompilerIW4.h
Normal file
11
src/ObjCompiling/Game/IW4/Techset/VertexShaderCompilerIW4.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CreateVertexShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath);
|
||||
}
|
||||
59
src/ObjCompiling/Techset/ShaderIncludeHandler.cpp
Normal file
59
src/ObjCompiling/Techset/ShaderIncludeHandler.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "ShaderIncludeHandler.h"
|
||||
|
||||
#include "Shader/ShaderCommon.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
namespace techset
|
||||
{
|
||||
ShaderIncludeHandler::ShaderIncludeHandler(ISearchPath& searchPath)
|
||||
: m_search_path(searchPath)
|
||||
{
|
||||
}
|
||||
|
||||
HRESULT ShaderIncludeHandler::Open(D3D_INCLUDE_TYPE includeType, const LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes)
|
||||
{
|
||||
const auto fileName = shader::GetSourceFileNameForShaderAssetName(pFileName);
|
||||
auto file = m_search_path.Open(fileName);
|
||||
if (!file.IsOpen() || file.m_length <= 0)
|
||||
return E_FAIL;
|
||||
|
||||
if (std::cmp_greater(file.m_length, MAX_SHADER_SIZE))
|
||||
{
|
||||
con::error("Invalid shader source \"{}\": File too big: {}\n", pFileName, file.m_length);
|
||||
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;
|
||||
}
|
||||
} // namespace techset
|
||||
|
||||
#endif
|
||||
27
src/ObjCompiling/Techset/ShaderIncludeHandler.h
Normal file
27
src/ObjCompiling/Techset/ShaderIncludeHandler.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
// Shader compilation only available on Windows
|
||||
#ifdef _WIN32
|
||||
#include <d3dcompiler.h>
|
||||
|
||||
namespace techset
|
||||
{
|
||||
class ShaderIncludeHandler : public ID3DInclude
|
||||
{
|
||||
public:
|
||||
static constexpr size_t MAX_SHADER_SIZE = 0x1900000u;
|
||||
|
||||
explicit ShaderIncludeHandler(ISearchPath& searchPath);
|
||||
virtual ~ShaderIncludeHandler() = default;
|
||||
|
||||
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;
|
||||
};
|
||||
} // namespace techset
|
||||
#endif
|
||||
@@ -5,6 +5,11 @@
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
// Thanks wingdi.h
|
||||
#ifdef ERROR
|
||||
#undef ERROR
|
||||
#endif
|
||||
|
||||
namespace con
|
||||
{
|
||||
enum class LogLevel : std::uint8_t
|
||||
|
||||
Reference in New Issue
Block a user