2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2022-06-22 11:36:06 -04:00
|
|
|
#include "IMaterialTechniqueSet.hpp"
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2023-01-03 07:16:44 -05:00
|
|
|
#include <Utils/Json.hpp>
|
|
|
|
|
2022-12-14 03:40:15 -05:00
|
|
|
#define IW4X_TECHSET_VERSION 1
|
2019-02-01 16:16:42 -05:00
|
|
|
|
2016-01-22 05:59:43 -05:00
|
|
|
namespace Assets
|
|
|
|
{
|
2022-06-22 11:36:06 -04:00
|
|
|
void IMaterialTechniqueSet::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
|
|
|
|
{
|
2022-12-14 03:40:15 -05:00
|
|
|
if (!header->data) this->loadFromDisk(header, name, builder); // Check if we need to import a new one into the game
|
2022-06-22 11:36:06 -04:00
|
|
|
if (!header->data) this->loadNative(header, name, builder); // Check if there is a native one
|
2023-02-15 06:58:31 -05:00
|
|
|
|
|
|
|
assert(header->data);
|
2022-06-22 11:36:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void IMaterialTechniqueSet::loadNative(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/)
|
|
|
|
{
|
|
|
|
header->techniqueSet = Components::AssetHandler::FindOriginalAsset(this->getType(), name.data()).techniqueSet;
|
|
|
|
}
|
|
|
|
|
2022-12-14 03:40:15 -05:00
|
|
|
void IMaterialTechniqueSet::loadFromDisk(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
|
2022-06-22 11:36:06 -04:00
|
|
|
{
|
2023-02-17 06:26:40 -05:00
|
|
|
header->techniqueSet = builder->getIW4OfApi()->read<Game::MaterialTechniqueSet>(Game::ASSET_TYPE_TECHNIQUE_SET, name);
|
|
|
|
|
|
|
|
auto ptr = header->techniqueSet;
|
2023-02-17 07:43:52 -05:00
|
|
|
if (ptr)
|
2023-02-17 06:26:40 -05:00
|
|
|
{
|
2023-02-17 07:43:52 -05:00
|
|
|
while (ptr->remappedTechniqueSet && ptr->remappedTechniqueSet != ptr)
|
2023-02-17 06:26:40 -05:00
|
|
|
{
|
2023-02-17 07:43:52 -05:00
|
|
|
ptr = ptr->remappedTechniqueSet;
|
|
|
|
builder->loadAsset(Game::ASSET_TYPE_TECHNIQUE_SET, ptr, false);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < Game::TECHNIQUE_COUNT; i++)
|
2023-02-17 06:26:40 -05:00
|
|
|
{
|
2023-02-17 07:43:52 -05:00
|
|
|
const auto technique = ptr->techniques[i];
|
|
|
|
if (technique)
|
2023-02-17 06:26:40 -05:00
|
|
|
{
|
2023-02-17 07:43:52 -05:00
|
|
|
for (size_t j = 0; j < technique->passCount; j++)
|
|
|
|
{
|
|
|
|
const auto pass = &technique->passArray[j];
|
|
|
|
builder->loadAsset(Game::ASSET_TYPE_VERTEXDECL, pass->vertexDecl, true);
|
|
|
|
builder->loadAsset(Game::ASSET_TYPE_PIXELSHADER, pass->pixelShader, true);
|
|
|
|
builder->loadAsset(Game::ASSET_TYPE_VERTEXSHADER, pass->vertexShader, true);
|
|
|
|
}
|
2023-02-17 06:26:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 11:36:06 -04:00
|
|
|
}
|
2019-02-01 16:16:42 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
void IMaterialTechniqueSet::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
2022-12-14 03:40:15 -05:00
|
|
|
{
|
2016-12-21 11:26:16 -05:00
|
|
|
Game::MaterialTechniqueSet* asset = header.techniqueSet;
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-09-16 18:14:59 -04:00
|
|
|
for (int i = 0; i < ARRAYSIZE(Game::MaterialTechniqueSet::techniques); ++i)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
|
|
|
Game::MaterialTechnique* technique = asset->techniques[i];
|
|
|
|
|
|
|
|
if (!technique) continue;
|
|
|
|
|
2017-04-22 15:47:04 -04:00
|
|
|
for (short j = 0; j < technique->passCount; ++j)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
2017-04-22 15:47:04 -04:00
|
|
|
Game::MaterialPass* pass = &technique->passArray[j];
|
2016-01-22 05:59:43 -05:00
|
|
|
|
|
|
|
if (pass->vertexDecl)
|
|
|
|
{
|
2016-12-23 15:01:56 -05:00
|
|
|
builder->loadAsset(Game::XAssetType::ASSET_TYPE_VERTEXDECL, pass->vertexDecl);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pass->vertexShader)
|
|
|
|
{
|
2016-12-23 15:01:56 -05:00
|
|
|
builder->loadAsset(Game::XAssetType::ASSET_TYPE_VERTEXSHADER, pass->vertexShader);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pass->pixelShader)
|
|
|
|
{
|
2016-12-23 15:01:56 -05:00
|
|
|
builder->loadAsset(Game::XAssetType::ASSET_TYPE_PIXELSHADER, pass->pixelShader);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
void IMaterialTechniqueSet::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
AssertSize(Game::MaterialTechniqueSet, 204);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
Utils::Stream* buffer = builder->getBuffer();
|
2022-12-14 03:40:15 -05:00
|
|
|
|
2016-12-21 11:26:16 -05:00
|
|
|
Game::MaterialTechniqueSet* asset = header.techniqueSet;
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::MaterialTechniqueSet* dest = buffer->dest<Game::MaterialTechniqueSet>();
|
2022-12-14 03:40:15 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->save(asset);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
|
|
|
if (asset->name)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->saveString(builder->getAssetName(this->getType(), asset->name));
|
2016-06-10 07:52:55 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->name);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save_MaterialTechniquePtrArray
|
2016-09-16 18:14:59 -04:00
|
|
|
static_assert(ARRAYSIZE(Game::MaterialTechniqueSet::techniques) == 48, "Techniques array invalid!");
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-09-16 18:14:59 -04:00
|
|
|
for (int i = 0; i < ARRAYSIZE(Game::MaterialTechniqueSet::techniques); ++i)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
|
|
|
Game::MaterialTechnique* technique = asset->techniques[i];
|
|
|
|
|
|
|
|
if (technique)
|
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
if (builder->hasPointer(technique))
|
2016-01-22 07:18:26 -05:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
dest->techniques[i] = builder->getPointer(technique);
|
2016-01-22 07:18:26 -05:00
|
|
|
}
|
|
|
|
else
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
2016-01-22 07:18:26 -05:00
|
|
|
// Size-check is obsolete, as the structure is dynamic
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->align(Utils::Stream::ALIGN_4);
|
|
|
|
builder->storePointer(technique);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::MaterialTechnique* destTechnique = buffer->dest<Game::MaterialTechnique>();
|
|
|
|
buffer->save(technique, 8);
|
2016-01-22 05:59:43 -05:00
|
|
|
|
2016-01-22 07:18:26 -05:00
|
|
|
// Save_MaterialPassArray
|
2016-11-20 08:09:07 -05:00
|
|
|
Game::MaterialPass* destPasses = buffer->dest<Game::MaterialPass>();
|
2017-04-22 15:47:04 -04:00
|
|
|
buffer->saveArray(technique->passArray, technique->passCount);
|
2016-10-09 06:02:17 -04:00
|
|
|
|
2017-04-22 15:47:04 -04:00
|
|
|
for (short j = 0; j < technique->passCount; ++j)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
AssertSize(Game::MaterialPass, 20);
|
2016-01-22 07:18:26 -05:00
|
|
|
|
2016-10-09 06:02:17 -04:00
|
|
|
Game::MaterialPass* destPass = &destPasses[j];
|
2017-04-22 15:47:04 -04:00
|
|
|
Game::MaterialPass* pass = &technique->passArray[j];
|
2016-01-22 07:18:26 -05:00
|
|
|
|
|
|
|
if (pass->vertexDecl)
|
|
|
|
{
|
2016-12-23 01:42:56 -05:00
|
|
|
destPass->vertexDecl = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_VERTEXDECL, pass->vertexDecl).vertexDecl;
|
2016-01-22 07:18:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pass->vertexShader)
|
|
|
|
{
|
2016-12-23 01:42:56 -05:00
|
|
|
destPass->vertexShader = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_VERTEXSHADER, pass->vertexShader).vertexShader;
|
2016-01-22 07:18:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pass->pixelShader)
|
|
|
|
{
|
2016-12-23 01:42:56 -05:00
|
|
|
destPass->pixelShader = builder->saveSubAsset(Game::XAssetType::ASSET_TYPE_PIXELSHADER, pass->pixelShader).pixelShader;
|
2016-01-22 07:18:26 -05:00
|
|
|
}
|
|
|
|
|
2017-04-22 15:47:04 -04:00
|
|
|
if (pass->args)
|
2016-01-22 07:18:26 -05:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->align(Utils::Stream::ALIGN_4);
|
2017-05-29 04:54:20 -04:00
|
|
|
Game::MaterialShaderArgument* destArgs = buffer->dest<Game::MaterialShaderArgument>();
|
2017-04-22 15:47:04 -04:00
|
|
|
buffer->saveArray(pass->args, pass->perPrimArgCount + pass->perObjArgCount + pass->stableArgCount);
|
2017-05-29 04:54:20 -04:00
|
|
|
|
2017-06-14 06:06:04 -04:00
|
|
|
for (int k = 0; k < pass->perPrimArgCount + pass->perObjArgCount + pass->stableArgCount; ++k)
|
2017-05-29 04:54:20 -04:00
|
|
|
{
|
|
|
|
Game::MaterialShaderArgument* arg = &pass->args[k];
|
|
|
|
Game::MaterialShaderArgument* destArg = &destArgs[k];
|
|
|
|
|
2017-06-14 06:06:04 -04:00
|
|
|
if (arg->type == 1 || arg->type == 7)
|
2017-05-29 04:54:20 -04:00
|
|
|
{
|
|
|
|
if (builder->hasPointer(arg->u.literalConst))
|
|
|
|
{
|
|
|
|
destArg->u.literalConst = builder->getPointer(arg->u.literalConst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer->align(Utils::Stream::ALIGN_4);
|
|
|
|
builder->storePointer(arg->u.literalConst);
|
|
|
|
|
|
|
|
buffer->saveArray(arg->u.literalConst, 4);
|
|
|
|
Utils::Stream::ClearPointer(&destArg->u.literalConst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 15:47:04 -04:00
|
|
|
Utils::Stream::ClearPointer(&destPass->args);
|
2016-01-22 07:18:26 -05:00
|
|
|
}
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
|
2016-01-22 07:18:26 -05:00
|
|
|
if (technique->name)
|
2016-01-22 05:59:43 -05:00
|
|
|
{
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->saveString(technique->name);
|
2016-06-10 07:52:55 -04:00
|
|
|
Utils::Stream::ClearPointer(&destTechnique->name);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
|
2016-06-10 07:52:55 -04:00
|
|
|
Utils::Stream::ClearPointer(&dest->techniques[i]);
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 08:09:07 -05:00
|
|
|
buffer->popBlock();
|
2016-01-22 05:59:43 -05:00
|
|
|
}
|
|
|
|
}
|