From 4fc70a216d936bdd76834736664aec43f9124318 Mon Sep 17 00:00:00 2001 From: hakasapl Date: Thu, 18 Jul 2024 20:33:58 -0400 Subject: [PATCH] cleanup --- include/ParallaxGen/ParallaxGen.hpp | 2 +- include/ParallaxGenUtil/ParallaxGenUtil.hpp | 3 -- src/BethesdaDirectory/BethesdaDirectory.cpp | 4 +- src/ParallaxGen/ParallaxGen.cpp | 41 +++++++++---------- src/ParallaxGenD3D/ParallaxGenD3D.cpp | 9 +++- .../ParallaxGenDirectory.cpp | 2 + src/ParallaxGenUtil/ParallaxGenUtil.cpp | 6 --- src/main/main.cpp | 3 +- 8 files changed, 33 insertions(+), 37 deletions(-) diff --git a/include/ParallaxGen/ParallaxGen.hpp b/include/ParallaxGen/ParallaxGen.hpp index 6beb7902..cec24bf6 100644 --- a/include/ParallaxGen/ParallaxGen.hpp +++ b/include/ParallaxGen/ParallaxGen.hpp @@ -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 diff --git a/include/ParallaxGenUtil/ParallaxGenUtil.hpp b/include/ParallaxGenUtil/ParallaxGenUtil.hpp index 2aaa387e..dade7acc 100644 --- a/include/ParallaxGenUtil/ParallaxGenUtil.hpp +++ b/include/ParallaxGenUtil/ParallaxGenUtil.hpp @@ -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); diff --git a/src/BethesdaDirectory/BethesdaDirectory.cpp b/src/BethesdaDirectory/BethesdaDirectory.cpp index 18c931c7..2df8b353 100644 --- a/src/BethesdaDirectory/BethesdaDirectory.cpp +++ b/src/BethesdaDirectory/BethesdaDirectory.cpp @@ -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 }; @@ -552,7 +552,7 @@ BethesdaDirectory::BethesdaFile BethesdaDirectory::getFileFromMap(const fs::path void BethesdaDirectory::updateFileMap(const fs::path& file_path, shared_ptr 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 }; diff --git a/src/ParallaxGen/ParallaxGen.cpp b/src/ParallaxGen/ParallaxGen.cpp index 1a42c28e..40666711 100644 --- a/src/ParallaxGen/ParallaxGen.cpp +++ b/src/ParallaxGen/ParallaxGen.cpp @@ -14,14 +14,14 @@ 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); @@ -29,11 +29,15 @@ ParallaxGen::ParallaxGen(const fs::path output_dir, ParallaxGenDirectory* pgd, P // 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; @@ -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; @@ -283,11 +289,10 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector& 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; @@ -305,8 +310,8 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector& 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()); @@ -326,7 +331,7 @@ void ParallaxGen::processNIF(const fs::path& nif_file, vector& heightM continue; } - // enable parallax on mesh! + // enable parallax for this shape! nif_modified |= enableParallaxOnShape(nif, shape, shader, search_prefix); break; } @@ -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); @@ -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); } } diff --git a/src/ParallaxGenD3D/ParallaxGenD3D.cpp b/src/ParallaxGenD3D/ParallaxGenD3D.cpp index 86f99492..0ae18ea1 100644 --- a/src/ParallaxGenD3D/ParallaxGenD3D.cpp +++ b/src/ParallaxGenD3D/ParallaxGenD3D.cpp @@ -421,7 +421,7 @@ ComPtr ParallaxGenD3D::createBuffer(const void* data, D3D11_BUFFER initData.pSysMem = data; ComPtr 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); @@ -550,7 +550,12 @@ vector ParallaxGenD3D::readBack(const ComPtr& gpu_resource) con buffer_desc.StructureByteStride = 0; // Create the staging buffer - ComPtr staging_buffer = createBuffer(nullptr, buffer_desc); + ComPtr 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(); + } // copy resource pContext->CopyResource(staging_buffer.Get(), gpu_resource.Get()); diff --git a/src/ParallaxGenDirectory/ParallaxGenDirectory.cpp b/src/ParallaxGenDirectory/ParallaxGenDirectory.cpp index 7f014b94..a19dcdd3 100644 --- a/src/ParallaxGenDirectory/ParallaxGenDirectory.cpp +++ b/src/ParallaxGenDirectory/ParallaxGenDirectory.cpp @@ -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 diff --git a/src/ParallaxGenUtil/ParallaxGenUtil.cpp b/src/ParallaxGenUtil/ParallaxGenUtil.cpp index 85d578d2..1def427e 100644 --- a/src/ParallaxGenUtil/ParallaxGenUtil.cpp +++ b/src/ParallaxGenUtil/ParallaxGenUtil.cpp @@ -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 wbuffer(length); diff --git a/src/main/main.cpp b/src/main/main.cpp index 906d7a27..90b2ca9f 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -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) { @@ -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(); }