2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-04-21 19:08:41 +00:00

chore: use templating for iw4 shader compilation

This commit is contained in:
Jan Laupetin
2026-04-08 10:13:30 +01:00
parent 4801b59b2c
commit 1d1b9cc4d1
9 changed files with 308 additions and 158 deletions

View File

@@ -1,13 +1,13 @@
#include "ObjCompilerIW4.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/Techset/PixelShaderCompilerIW4.h"
#include "Game/IW4/Techset/TechniqueCompilerIW4.h"
#include "Game/IW4/Techset/TechsetCompilerIW4.h"
#include "Game/IW4/Techset/VertexDeclCompilerIW4.h"
#include "Game/IW4/Techset/VertexShaderCompilerIW4.h"
#include "Image/ImageIwdPostProcessor.h"
#include "Material/CompilerMaterialIW4.h"
#include "Techset/PixelShaderCompilerIW4.h"
#include "Techset/VertexShaderCompilerIW4.h"
#include <memory>

View File

@@ -1,67 +0,0 @@
#include "PixelShaderCompilerIW4.h"
#include "Game/IW4/IW4.h"
#include "Shader/D3DShaderCompiler.h"
#include "Utils/Logging/Log.h"
#include <cassert>
#include <iostream>
#include <utility>
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
{
auto result = shader::CompileShader(assetName, "PSMain", "ps_3_0", false, m_search_path, m_memory);
if (result.has_value())
{
const std::optional maybeShader(*std::move(result));
if (!maybeShader.has_value())
return AssetCreationResult::NoAction();
con::info("Compiled pixel shader \"{}\"", assetName);
assert(maybeShader->m_shader_size % sizeof(uint32_t) == 0);
auto* pixelShader = m_memory.Alloc<MaterialPixelShader>();
pixelShader->name = m_memory.Dup(assetName.c_str());
pixelShader->prog.loadDef.program = static_cast<unsigned int*>(maybeShader->m_shader_bin);
pixelShader->prog.loadDef.programSize = static_cast<uint16_t>(maybeShader->m_shader_size / sizeof(uint32_t));
return AssetCreationResult::Success(context.AddAsset(AssetRegistration<AssetPixelShader>(assetName, pixelShader)));
}
con::error("Failed to compile pixel shader \"{}\": {}", assetName, result.error());
return AssetCreationResult::Failure();
}
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)
{
if (!shader::ShaderCompilationAvailable())
return nullptr;
return std::make_unique<PixelShaderCompilerIW4>(memory, searchPath);
}
} // namespace techset

View File

@@ -1,11 +0,0 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace techset
{
std::unique_ptr<IAssetCreator> CreatePixelShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath);
}

View File

@@ -1,67 +0,0 @@
#include "VertexShaderCompilerIW4.h"
#include "Game/IW4/IW4.h"
#include "Shader/D3DShaderCompiler.h"
#include "Utils/Logging/Log.h"
#include <cassert>
#include <iostream>
#include <utility>
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
{
auto result = shader::CompileShader(assetName, "VSMain", "vs_3_0", false, m_search_path, m_memory);
if (result.has_value())
{
const std::optional maybeShader(*std::move(result));
if (!maybeShader.has_value())
return AssetCreationResult::NoAction();
con::info("Compiled vertex shader \"{}\"", assetName);
assert(maybeShader->m_shader_size % sizeof(uint32_t) == 0);
auto* vertexShader = m_memory.Alloc<MaterialVertexShader>();
vertexShader->name = m_memory.Dup(assetName.c_str());
vertexShader->prog.loadDef.program = static_cast<unsigned int*>(maybeShader->m_shader_bin);
vertexShader->prog.loadDef.programSize = static_cast<uint16_t>(maybeShader->m_shader_size / sizeof(uint32_t));
return AssetCreationResult::Success(context.AddAsset(AssetRegistration<AssetVertexShader>(assetName, vertexShader)));
}
con::error("Failed to compile vertex shader \"{}\": {}", assetName, result.error());
return AssetCreationResult::Failure();
}
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)
{
if (!shader::ShaderCompilationAvailable())
return nullptr;
return std::make_unique<VertexShaderCompilerIW4>(memory, searchPath);
}
} // namespace techset

View File

@@ -1,11 +0,0 @@
#pragma once
#include "Asset/IAssetCreator.h"
#include "Utils/MemoryManager.h"
#include <memory>
namespace techset
{
std::unique_ptr<IAssetCreator> CreateVertexShaderCompilerIW4(MemoryManager& memory, ISearchPath& searchPath);
}

View File

@@ -0,0 +1,112 @@
#options GAME(IW4)
#filename "Game/" + GAME + "/Techset/PixelShaderCompiler" + GAME + ".cpp"
#set COMPILER_HEADER "\"PixelShaderCompiler" + GAME + ".h\""
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
#if GAME == "IW3"
#define FEATURE_IW3
#define IS_DX9
#define IS_SUB_ASSET
#elif GAME == "IW4"
#define FEATURE_IW4
#define IS_DX9
#elif GAME == "IW5"
#define FEATURE_IW5
#define IS_DX9
#elif GAME == "T5"
#define FEATURE_T5
#define IS_DX9
#define IS_SUB_ASSET
#elif GAME == "T6"
#define FEATURE_T6
#define IS_DX11
#define IS_SUB_ASSET
#endif
#include COMPILER_HEADER
#include GAME_HEADER
#include "Shader/D3DShaderCompiler.h"
#include "Utils/Logging/Log.h"
#include <cassert>
#include <iostream>
#include <utility>
using namespace GAME;
#set COMPILER_CLASS_NAME "PixelShaderCompiler" + GAME
#if defined(IS_SUB_ASSET)
#define ABSTRACT_CREATOR_NAME SubAssetCreator
#define OVERRIDDEN_CREATOR_METHOD CreateSubAsset
#define ADD_ASSET_METHOD AddSubAsset
#define ASSET_NAME SubAssetPixelShader
#define INTERFACE_NAME ISubAssetCreator
#else
#define ABSTRACT_CREATOR_NAME AssetCreator
#define OVERRIDDEN_CREATOR_METHOD CreateAsset
#define ADD_ASSET_METHOD AddAsset
#define ASSET_NAME AssetPixelShader
#define INTERFACE_NAME IAssetCreator
#endif
namespace
{
class COMPILER_CLASS_NAME final : public ABSTRACT_CREATOR_NAME<ASSET_NAME>
{
public:
COMPILER_CLASS_NAME(MemoryManager& memory, ISearchPath& searchPath)
: m_memory(memory),
m_search_path(searchPath)
{
}
AssetCreationResult OVERRIDDEN_CREATOR_METHOD(const std::string& assetName, AssetCreationContext& context) override
{
auto result = shader::CompileShader(assetName, "PSMain", "ps_3_0", false, m_search_path, m_memory);
if (result.has_value())
{
const std::optional maybeShader(*std::move(result));
if (!maybeShader.has_value())
return AssetCreationResult::NoAction();
con::info("Compiled pixel shader \"{}\"", assetName);
assert(maybeShader->m_shader_size % sizeof(uint32_t) == 0);
auto* pixelShader = m_memory.Alloc<MaterialPixelShader>();
pixelShader->name = m_memory.Dup(assetName.c_str());
pixelShader->prog.loadDef.program = static_cast<unsigned int*>(maybeShader->m_shader_bin);
pixelShader->prog.loadDef.programSize = static_cast<uint16_t>(maybeShader->m_shader_size / sizeof(uint32_t));
return AssetCreationResult::Success(context.ADD_ASSET_METHOD(AssetRegistration<ASSET_NAME>(assetName, pixelShader)));
}
con::error("Failed to compile pixel shader \"{}\": {}", assetName, result.error());
return AssetCreationResult::Failure();
}
void FinalizeZone(AssetCreationContext& context) override {}
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
};
} // namespace
#set CREATE_COMPILER_METHOD "CreatePixelShaderCompiler" + GAME
namespace techset
{
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath)
{
if (!shader::ShaderCompilationAvailable())
return nullptr;
return std::make_unique<COMPILER_CLASS_NAME>(memory, searchPath);
}
} // namespace techset

View File

@@ -0,0 +1,41 @@
#options GAME(IW4)
#filename "Game/" + GAME + "/Techset/PixelShaderCompiler" + GAME + ".h"
#if GAME == "IW3"
#define FEATURE_IW3
#define IS_SUB_ASSET
#elif GAME == "IW4"
#define FEATURE_IW4
#elif GAME == "IW5"
#define FEATURE_IW5
#elif GAME == "T5"
#define FEATURE_T5
#define IS_SUB_ASSET
#elif GAME == "T6"
#define FEATURE_T6
#define IS_SUB_ASSET
#endif
// This file was templated.
// See PixelShaderCompiler.h.template.
// Do not modify, changes will be lost.
#pragma once
#include "Asset/IAssetCreator.h"
#include "Utils/MemoryManager.h"
#include <memory>
#set CREATE_COMPILER_METHOD "CreatePixelShaderCompiler" + GAME
#if defined(IS_SUB_ASSET)
#define INTERFACE_NAME ISubAssetCreator
#else
#define INTERFACE_NAME IAssetCreator
#endif
namespace techset
{
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath);
}

View File

@@ -0,0 +1,112 @@
#options GAME(IW4)
#filename "Game/" + GAME + "/Techset/VertexShaderCompiler" + GAME + ".cpp"
#set COMPILER_HEADER "\"VertexShaderCompiler" + GAME + ".h\""
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
#if GAME == "IW3"
#define FEATURE_IW3
#define IS_DX9
#define IS_SUB_ASSET
#elif GAME == "IW4"
#define FEATURE_IW4
#define IS_DX9
#elif GAME == "IW5"
#define FEATURE_IW5
#define IS_DX9
#elif GAME == "T5"
#define FEATURE_T5
#define IS_DX9
#define IS_SUB_ASSET
#elif GAME == "T6"
#define FEATURE_T6
#define IS_DX11
#define IS_SUB_ASSET
#endif
#include COMPILER_HEADER
#include GAME_HEADER
#include "Shader/D3DShaderCompiler.h"
#include "Utils/Logging/Log.h"
#include <cassert>
#include <iostream>
#include <utility>
using namespace GAME;
#set COMPILER_CLASS_NAME "VertexShaderCompiler" + GAME
#if defined(IS_SUB_ASSET)
#define ABSTRACT_CREATOR_NAME SubAssetCreator
#define OVERRIDDEN_CREATOR_METHOD CreateSubAsset
#define ADD_ASSET_METHOD AddSubAsset
#define ASSET_NAME SubAssetVertexShader
#define INTERFACE_NAME ISubAssetCreator
#else
#define ABSTRACT_CREATOR_NAME AssetCreator
#define OVERRIDDEN_CREATOR_METHOD CreateAsset
#define ADD_ASSET_METHOD AddAsset
#define ASSET_NAME AssetVertexShader
#define INTERFACE_NAME IAssetCreator
#endif
namespace
{
class COMPILER_CLASS_NAME final : public ABSTRACT_CREATOR_NAME<ASSET_NAME>
{
public:
COMPILER_CLASS_NAME(MemoryManager& memory, ISearchPath& searchPath)
: m_memory(memory),
m_search_path(searchPath)
{
}
AssetCreationResult OVERRIDDEN_CREATOR_METHOD(const std::string& assetName, AssetCreationContext& context) override
{
auto result = shader::CompileShader(assetName, "VSMain", "vs_3_0", false, m_search_path, m_memory);
if (result.has_value())
{
const std::optional maybeShader(*std::move(result));
if (!maybeShader.has_value())
return AssetCreationResult::NoAction();
con::info("Compiled vertex shader \"{}\"", assetName);
assert(maybeShader->m_shader_size % sizeof(uint32_t) == 0);
auto* vertexShader = m_memory.Alloc<MaterialVertexShader>();
vertexShader->name = m_memory.Dup(assetName.c_str());
vertexShader->prog.loadDef.program = static_cast<unsigned int*>(maybeShader->m_shader_bin);
vertexShader->prog.loadDef.programSize = static_cast<uint16_t>(maybeShader->m_shader_size / sizeof(uint32_t));
return AssetCreationResult::Success(context.ADD_ASSET_METHOD(AssetRegistration<ASSET_NAME>(assetName, vertexShader)));
}
con::error("Failed to compile vertex shader \"{}\": {}", assetName, result.error());
return AssetCreationResult::Failure();
}
void FinalizeZone(AssetCreationContext& context) override {}
private:
MemoryManager& m_memory;
ISearchPath& m_search_path;
};
} // namespace
#set CREATE_COMPILER_METHOD "CreateVertexShaderCompiler" + GAME
namespace techset
{
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath)
{
if (!shader::ShaderCompilationAvailable())
return nullptr;
return std::make_unique<COMPILER_CLASS_NAME>(memory, searchPath);
}
} // namespace techset

View File

@@ -0,0 +1,41 @@
#options GAME(IW4)
#filename "Game/" + GAME + "/Techset/VertexShaderCompiler" + GAME + ".h"
#if GAME == "IW3"
#define FEATURE_IW3
#define IS_SUB_ASSET
#elif GAME == "IW4"
#define FEATURE_IW4
#elif GAME == "IW5"
#define FEATURE_IW5
#elif GAME == "T5"
#define FEATURE_T5
#define IS_SUB_ASSET
#elif GAME == "T6"
#define FEATURE_T6
#define IS_SUB_ASSET
#endif
// This file was templated.
// See VertexShaderCompiler.h.template.
// Do not modify, changes will be lost.
#pragma once
#include "Asset/IAssetCreator.h"
#include "Utils/MemoryManager.h"
#include <memory>
#set CREATE_COMPILER_METHOD "CreateVertexShaderCompiler" + GAME
#if defined(IS_SUB_ASSET)
#define INTERFACE_NAME ISubAssetCreator
#else
#define INTERFACE_NAME IAssetCreator
#endif
namespace techset
{
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath);
}