From a92d5e7665776bf9eb60e0e7f00f952ef0647c7a Mon Sep 17 00:00:00 2001 From: turanszkij Date: Wed, 15 Apr 2020 19:10:45 +0100 Subject: [PATCH] bvh fix --- WickedEngine/wiGPUBVH.cpp | 147 +++++++++++++++++------------------- WickedEngine/wiGPUBVH.h | 7 +- WickedEngine/wiRenderer.cpp | 3 + WickedEngine/wiScene.cpp | 6 ++ WickedEngine/wiVersion.cpp | 2 +- 5 files changed, 84 insertions(+), 81 deletions(-) diff --git a/WickedEngine/wiGPUBVH.cpp b/WickedEngine/wiGPUBVH.cpp index 3ee44d16b..b3cf54f50 100644 --- a/WickedEngine/wiGPUBVH.cpp +++ b/WickedEngine/wiGPUBVH.cpp @@ -35,12 +35,6 @@ void wiGPUBVH::UpdateGlobalMaterialResources(const Scene& scene, CommandList cmd using namespace wiRectPacker; - if (sceneTextures.empty()) - { - sceneTextures.insert(wiTextureHelper::getWhite()); - sceneTextures.insert(wiTextureHelper::getNormalMapDefault()); - } - for (size_t i = 0; i < scene.objects.GetCount(); ++i) { const ObjectComponent& object = scene.objects[i]; @@ -53,10 +47,22 @@ void wiGPUBVH::UpdateGlobalMaterialResources(const Scene& scene, CommandList cmd { const MaterialComponent& material = *scene.materials.GetComponent(subset.materialID); - sceneTextures.insert(material.GetBaseColorMap()); - sceneTextures.insert(material.GetSurfaceMap()); - sceneTextures.insert(material.GetEmissiveMap()); - sceneTextures.insert(material.GetNormalMap()); + if (material.baseColorMap != nullptr) + { + sceneTextures.insert(material.baseColorMap); + } + if (material.surfaceMap != nullptr) + { + sceneTextures.insert(material.surfaceMap); + } + if (material.emissiveMap != nullptr) + { + sceneTextures.insert(material.emissiveMap); + } + if (material.normalMap != nullptr) + { + sceneTextures.insert(material.normalMap); + } } } @@ -64,18 +70,18 @@ void wiGPUBVH::UpdateGlobalMaterialResources(const Scene& scene, CommandList cmd bool repackAtlas = false; const int atlasWrapBorder = 1; - for (const Texture* tex : sceneTextures) + for (auto res : sceneTextures) { - if (tex == nullptr) + if (res == nullptr) { continue; } - if (storedTextures.find(tex) == storedTextures.end()) + if (storedTextures.find(res) == storedTextures.end()) { // we need to pack this texture into the atlas - rect_xywh newRect = rect_xywh(0, 0, tex->GetDesc().Width + atlasWrapBorder * 2, tex->GetDesc().Height + atlasWrapBorder * 2); - storedTextures[tex] = newRect; + rect_xywh newRect = rect_xywh(0, 0, res->texture->GetDesc().Width + atlasWrapBorder * 2, res->texture->GetDesc().Height + atlasWrapBorder * 2); + storedTextures[res] = newRect; repackAtlas = true; } @@ -114,7 +120,7 @@ void wiGPUBVH::UpdateGlobalMaterialResources(const Scene& scene, CommandList cmd for (auto& it : storedTextures) { - wiRenderer::CopyTexture2D(globalMaterialAtlas, 0, it.second.x + atlasWrapBorder, it.second.y + atlasWrapBorder, *it.first, 0, cmd, wiRenderer::BORDEREXPAND_WRAP); + wiRenderer::CopyTexture2D(globalMaterialAtlas, 0, it.second.x + atlasWrapBorder, it.second.y + atlasWrapBorder, *it.first->texture, 0, cmd, wiRenderer::BORDEREXPAND_WRAP); } } else @@ -141,78 +147,54 @@ void wiGPUBVH::UpdateGlobalMaterialResources(const Scene& scene, CommandList cmd // Add extended properties: const TextureDesc& desc = globalMaterialAtlas.GetDesc(); - rect_xywh rect; - - if (material.GetBaseColorMap() != nullptr) + if (material.baseColorMap != nullptr) { - rect = storedTextures[material.GetBaseColorMap()]; + rect_xywh rect = storedTextures[material.baseColorMap]; + // eliminate border expansion: + rect.x += atlasWrapBorder; + rect.y += atlasWrapBorder; + rect.w -= atlasWrapBorder * 2; + rect.h -= atlasWrapBorder * 2; + global_material.baseColorAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, + (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); } - else + + if (material.surfaceMap != nullptr) { - rect = storedTextures[wiTextureHelper::getWhite()]; + rect_xywh rect = storedTextures[material.surfaceMap]; + // eliminate border expansion: + rect.x += atlasWrapBorder; + rect.y += atlasWrapBorder; + rect.w -= atlasWrapBorder * 2; + rect.h -= atlasWrapBorder * 2; + global_material.surfaceMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, + (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); } - // eliminate border expansion: - rect.x += atlasWrapBorder; - rect.y += atlasWrapBorder; - rect.w -= atlasWrapBorder * 2; - rect.h -= atlasWrapBorder * 2; - global_material.baseColorAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, - (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); - - - if (material.GetSurfaceMap() != nullptr) + if (material.emissiveMap != nullptr) { - rect = storedTextures[material.GetSurfaceMap()]; + rect_xywh rect = storedTextures[material.emissiveMap]; + // eliminate border expansion: + rect.x += atlasWrapBorder; + rect.y += atlasWrapBorder; + rect.w -= atlasWrapBorder * 2; + rect.h -= atlasWrapBorder * 2; + global_material.emissiveMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, + (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); } - else + + if (material.normalMap != nullptr) { - rect = storedTextures[wiTextureHelper::getWhite()]; + rect_xywh rect = storedTextures[material.normalMap]; + // eliminate border expansion: + rect.x += atlasWrapBorder; + rect.y += atlasWrapBorder; + rect.w -= atlasWrapBorder * 2; + rect.h -= atlasWrapBorder * 2; + global_material.normalMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, + (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); } - // eliminate border expansion: - rect.x += atlasWrapBorder; - rect.y += atlasWrapBorder; - rect.w -= atlasWrapBorder * 2; - rect.h -= atlasWrapBorder * 2; - global_material.surfaceMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, - (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); - - - - if (material.GetEmissiveMap() != nullptr) - { - rect = storedTextures[material.GetEmissiveMap()]; - } - else - { - rect = storedTextures[wiTextureHelper::getWhite()]; - } - // eliminate border expansion: - rect.x += atlasWrapBorder; - rect.y += atlasWrapBorder; - rect.w -= atlasWrapBorder * 2; - rect.h -= atlasWrapBorder * 2; - global_material.emissiveMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, - (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); - - - - if (material.GetNormalMap() != nullptr) - { - rect = storedTextures[material.GetNormalMap()]; - } - else - { - rect = storedTextures[wiTextureHelper::getNormalMapDefault()]; - } - // eliminate border expansion: - rect.x += atlasWrapBorder; - rect.y += atlasWrapBorder; - rect.w -= atlasWrapBorder * 2; - rect.h -= atlasWrapBorder * 2; - global_material.normalMapAtlasMulAdd = XMFLOAT4((float)rect.w / (float)desc.Width, (float)rect.h / (float)desc.Height, - (float)rect.x / (float)desc.Width, (float)rect.y / (float)desc.Height); materialArray.push_back(global_material); } @@ -587,6 +569,15 @@ void wiGPUBVH::Bind(SHADERSTAGE stage, CommandList cmd) const device->BindResources(stage, res, TEXSLOT_ONDEMAND0, arraysize(res), cmd); } +void wiGPUBVH::Clear() +{ + primitiveCapacity = 0; + primitiveCount = 0; + materialArray.clear(); + storedTextures.clear(); + sceneTextures.clear(); +} + void wiGPUBVH::LoadShaders() { string SHADERPATH = wiRenderer::GetShaderPath(); diff --git a/WickedEngine/wiGPUBVH.h b/WickedEngine/wiGPUBVH.h index 2b6377e24..27e5af679 100644 --- a/WickedEngine/wiGPUBVH.h +++ b/WickedEngine/wiGPUBVH.h @@ -4,6 +4,7 @@ #include "wiScene_Decl.h" #include "wiRectPacker.h" #include "ShaderInterop_Renderer.h" +#include "wiResourceManager.h" #include #include @@ -28,14 +29,16 @@ private: wiGraphics::GPUBuffer globalMaterialBuffer; wiGraphics::Texture globalMaterialAtlas; std::vector materialArray; - std::unordered_map storedTextures; - std::unordered_set sceneTextures; + std::unordered_map, wiRectPacker::rect_xywh> storedTextures; + std::unordered_set> sceneTextures; void UpdateGlobalMaterialResources(const wiScene::Scene& scene, wiGraphics::CommandList cmd); public: void Build(const wiScene::Scene& scene, wiGraphics::CommandList cmd); void Bind(wiGraphics::SHADERSTAGE stage, wiGraphics::CommandList cmd) const; + void Clear(); + static void LoadShaders(); }; diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 526ed4393..19efa6d7f 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -2803,6 +2803,9 @@ void ClearWorld() GetScene().Clear(); + sceneBVH.Clear(); + scene_bvh_invalid = true; + deferredMIPGenLock.lock(); deferredMIPGens.clear(); deferredMIPGenLock.unlock(); diff --git a/WickedEngine/wiScene.cpp b/WickedEngine/wiScene.cpp index a4d59bebd..cafdeb552 100644 --- a/WickedEngine/wiScene.cpp +++ b/WickedEngine/wiScene.cpp @@ -304,6 +304,12 @@ namespace wiScene { retVal.options |= SHADERMATERIAL_OPTION_BIT_USE_WIND; } + + retVal.baseColorAtlasMulAdd = XMFLOAT4(0, 0, 0, 0); + retVal.surfaceMapAtlasMulAdd = XMFLOAT4(0, 0, 0, 0); + retVal.emissiveMapAtlasMulAdd = XMFLOAT4(0, 0, 0, 0); + retVal.normalMapAtlasMulAdd = XMFLOAT4(0, 0, 0, 0); + return retVal; } diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 8601bbd7a..99a0faa25 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 39; // minor bug fixes, alterations, refactors, updates - const int revision = 64; + const int revision = 65; long GetVersion()