Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hakasapl committed Jul 19, 2024
1 parent 44f45e0 commit 4fc70a2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 37 deletions.
2 changes: 1 addition & 1 deletion include/ParallaxGen/ParallaxGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ParallaxGen
static inline const std::string parallax_state_file = "PARALLAXGEN_DONTDELETE";

// constructor
ParallaxGen(const std::filesystem::path output_dir, ParallaxGenDirectory* pgd, ParallaxGenD3D* pgd3d, bool optimize_meshes = false);
ParallaxGen(const std::filesystem::path output_dir, ParallaxGenDirectory* pgd, ParallaxGenD3D* pgd3d, bool optimize_meshes = false, bool ignore_parallax = false, bool ignore_complex_material = false);
// upgrades textures whenever possible
void upgradeShaders();
// enables parallax on relevant meshes
Expand Down
3 changes: 0 additions & 3 deletions include/ParallaxGenUtil/ParallaxGenUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ namespace ParallaxGenUtil
// terminals usually auto exit when program ends, this function waits for user input before exiting
void exitWithUserInput(const int exit_code);

// converts fs::path to a lowercase variant (for case-insensitive comparison)
void pathLower(std::filesystem::path& path);

// converts a string to a wstring
std::wstring convertToWstring(const std::string str);

Expand Down
4 changes: 2 additions & 2 deletions src/BethesdaDirectory/BethesdaDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ bool BethesdaDirectory::file_allowed(const fs::path file_path) const
// helpers
BethesdaDirectory::BethesdaFile BethesdaDirectory::getFileFromMap(const fs::path& file_path) const
{
fs::path lower_path = boost::to_lower_copy(file_path.wstring());
fs::path lower_path = getPathLower(file_path);

if (fileMap.find(lower_path) == fileMap.end()) {
return BethesdaFile { fs::path(), nullptr };
Expand All @@ -552,7 +552,7 @@ BethesdaDirectory::BethesdaFile BethesdaDirectory::getFileFromMap(const fs::path

void BethesdaDirectory::updateFileMap(const fs::path& file_path, shared_ptr<BethesdaDirectory::BSAFile> bsa_file)
{
fs::path lower_path = boost::to_lower_copy(file_path.wstring());
fs::path lower_path = getPathLower(file_path);

BethesdaFile new_bfile = { file_path, bsa_file };

Expand Down
41 changes: 20 additions & 21 deletions src/ParallaxGen/ParallaxGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,30 @@ using namespace std;
namespace fs = filesystem;
using namespace nifly;

ParallaxGen::ParallaxGen(const fs::path output_dir, ParallaxGenDirectory* pgd, ParallaxGenD3D* pgd3d, bool optimize_meshes)
ParallaxGen::ParallaxGen(const fs::path output_dir, ParallaxGenDirectory* pgd, ParallaxGenD3D* pgd3d, bool optimize_meshes, bool ignore_parallax, bool ignore_complex_material)
{
// constructor
this->output_dir = output_dir;
this->pgd = pgd;
this->pgd3d = pgd3d;

// ! TODO normalize these paths before string comparing
// TODO normalize these paths before string comparing
if (boost::iequals(this->output_dir.wstring(), this->pgd->getDataPath().wstring())) {
spdlog::critical("Output directory cannot be your data folder, as meshes can be overwritten this way. Exiting.");
ParallaxGenUtil::exitWithUserInput(1);
}

// set optimize meshes flag
nif_save_options.optimize = optimize_meshes;

// set ignore flags
this->ignore_parallax = ignore_parallax;
this->ignore_complex_material = ignore_complex_material;
}

void ParallaxGen::upgradeShaders()
{
spdlog::info("Attempting to upgrade shaders where possible...");
spdlog::info("Starting shader upgrade process...");

//loop through height maps
size_t finished_task = 0;
Expand Down Expand Up @@ -79,6 +83,8 @@ void ParallaxGen::upgradeShaders()

void ParallaxGen::patchMeshes()
{
spdlog::info("Starting mesh patching process...");

// patch meshes
// loop through each mesh nif file
size_t finished_task = 0;
Expand Down Expand Up @@ -283,11 +289,10 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector<fs::path>& heightM
for (string& search_prefix : search_prefixes) {
// check if complex material file exists
fs::path search_path;
string search_prefix_lower = boost::algorithm::to_lower_copy(search_prefix);

// processing for complex material
search_path = search_prefix_lower + "_m.dds";
if (find(complexMaterialMaps.begin(), complexMaterialMaps.end(), search_path) != complexMaterialMaps.end()) {
search_path = search_prefix + "_m.dds";
if (!ignore_complex_material && pgd->isComplexMaterialMap(search_path)) {
if (shader_type != BSLSP::BSLSP_DEFAULT && shader_type != BSLSP::BSLSP_ENVMAP && shader_type != BSLSP::BSLSP_PARALLAX) {
spdlog::trace(L"Rejecting shape {} in NIF file {}: Incorrect shader type", block_id, nif_file.wstring());
continue;
Expand All @@ -305,8 +310,8 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector<fs::path>& heightM
}

// processing for parallax
search_path = search_prefix_lower + "_p.dds";
if (find(heightMaps.begin(), heightMaps.end(), search_path) != heightMaps.end()) {
search_path = search_prefix + "_p.dds";
if (!ignore_parallax && pgd->isHeightMap(search_path)) {
// decals don't work with regular parallax
if (cur_bslsp->shaderFlags1 & SSPF1::SLSF1_DECAL || cur_bslsp->shaderFlags1 & SSPF1::SLSF1_DYNAMIC_DECAL) {
spdlog::trace(L"Rejecting shape {} in NIF file {}: Decal shape", block_id, nif_file.wstring());
Expand All @@ -326,7 +331,7 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector<fs::path>& heightM
continue;
}

// enable parallax on mesh!
// enable parallax for this shape!
nif_modified |= enableParallaxOnShape(nif, shape, shader, search_prefix);
break;
}
Expand Down Expand Up @@ -366,16 +371,7 @@ bool ParallaxGen::enableComplexMaterialOnShape(NifFile& nif, NiShape* shape, NiS
cur_bslsp->shaderFlags1 |= SSPF1::SLSF1_ENVIRONMENT_MAPPING;
changed = true;
}
// 3. set vertex colors for shape
if (!shape->HasVertexColors()) {
shape->SetVertexColors(true);
changed = true;
}
// 4. set vertex colors for shader
if (!shader->HasVertexColors()) {
shader->SetVertexColors(true);
changed = true;
}

// 5. set complex material texture
string height_map;
uint32_t height_result = nif.GetTextureSlot(shape, height_map, 3);
Expand Down Expand Up @@ -447,13 +443,16 @@ bool ParallaxGen::enableParallaxOnShape(NifFile& nif, NiShape* shape, NiShader*
bool ParallaxGen::hasSameAspectRatio(const fs::path& dds_path_1, const fs::path& dds_path_2)
{
// verify that maps match each other
auto check_tuple = make_tuple(fs::path(boost::algorithm::to_lower_copy(dds_path_1.wstring())), fs::path(boost::algorithm::to_lower_copy(dds_path_2.wstring())));
fs::path dds_path_1_lower = pgd->getPathLower(dds_path_1);
fs::path dds_path_2_lower = pgd->getPathLower(dds_path_2);

auto check_tuple = make_tuple(dds_path_1_lower, dds_path_2_lower);
if (height_map_checks.find(check_tuple) != height_map_checks.end()) {
// key already exists
return height_map_checks[check_tuple];
} else {
// Need to perform computation
return pgd3d->checkIfAspectRatioMatches(dds_path_1, dds_path_2);
return pgd3d->checkIfAspectRatioMatches(dds_path_1_lower, dds_path_2_lower);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/ParallaxGenD3D/ParallaxGenD3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ ComPtr<ID3D11Buffer> ParallaxGenD3D::createBuffer(const void* data, D3D11_BUFFER
initData.pSysMem = data;

ComPtr<ID3D11Buffer> output_buffer;
HRESULT hr = pDevice->CreateBuffer(&desc, &initData, &output_buffer);
HRESULT hr = pDevice->CreateBuffer(&desc, &initData, output_buffer.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
spdlog::debug("Failed to create ID3D11Buffer on GPU: {}", hr);
Expand Down Expand Up @@ -550,7 +550,12 @@ vector<T> ParallaxGenD3D::readBack(const ComPtr<ID3D11Buffer>& gpu_resource) con
buffer_desc.StructureByteStride = 0;

// Create the staging buffer
ComPtr<ID3D11Buffer> staging_buffer = createBuffer(nullptr, buffer_desc);
ComPtr<ID3D11Buffer> staging_buffer;
hr = pDevice->CreateBuffer(&buffer_desc, nullptr, staging_buffer.ReleaseAndGetAddressOf());
if (FAILED(hr)) {
spdlog::debug("[GPU] Failed to create staging buffer: {}", hr);
return std::vector<T>();
}

// copy resource
pContext->CopyResource(staging_buffer.Get(), gpu_resource.Get());
Expand Down
2 changes: 2 additions & 0 deletions src/ParallaxGenDirectory/ParallaxGenDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void ParallaxGenDirectory::findHeightMaps()

void ParallaxGenDirectory::findComplexMaterialMaps()
{
// TODO GPU acceleration for this part

spdlog::info("Finding complex material maps");

// find complex material maps
Expand Down
6 changes: 0 additions & 6 deletions src/ParallaxGenUtil/ParallaxGenUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ namespace ParallaxGenUtil {
exit(exit_code);
}

void pathLower(fs::path& path) {
wstring path_str = path.wstring();
boost::algorithm::to_lower(path_str);
path = fs::path(path_str);
}

wstring convertToWstring(const string str) {
size_t length = str.length() + 1; // Including null terminator
std::vector<wchar_t> wbuffer(length);
Expand Down
3 changes: 1 addition & 2 deletions src/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ int main(int argc, char** argv) {
BethesdaGame bg = BethesdaGame(bg_type, game_dir, true);
ParallaxGenDirectory pgd = ParallaxGenDirectory(bg);
ParallaxGenD3D pgd3d = ParallaxGenD3D(&pgd, output_dir);
ParallaxGen pg = ParallaxGen(output_dir, &pgd, &pgd3d, optimize_meshes);
ParallaxGen pg = ParallaxGen(output_dir, &pgd, &pgd3d, optimize_meshes, ignore_parallax, ignore_complex_material);

// Check if GPU needs to be initialized
if (upgrade_shaders) {
Expand Down Expand Up @@ -227,7 +227,6 @@ int main(int argc, char** argv) {
pgd.populateFileMap();

// Build file vectors
// TODO make these instance vars
if (!ignore_parallax || (ignore_parallax && upgrade_shaders)) {
pgd.findHeightMaps();
}
Expand Down

0 comments on commit 4fc70a2

Please sign in to comment.