From 787ad260594a466ef86e84de3df97c0f08dd9ac4 Mon Sep 17 00:00:00 2001 From: Gustav Sterbrant Date: Wed, 20 Nov 2024 15:57:54 +0100 Subject: [PATCH] Various fixes, related to using too many spot light shadow casters, textures not being released. --- code/foundation/core/types.h | 27 -------- code/foundation/memory/memory.cc | 7 ++ code/foundation/memory/memory.h | 37 +++++++++- code/render/coregraphics/textureloader.cc | 8 +-- code/render/coregraphics/vk/vktexture.cc | 14 ++-- code/render/lighting/lightcontext.cc | 68 +++++++++---------- code/render/materials/gltf.json | 2 +- .../import/gltf/ngltfmaterialexporter.cc | 2 +- .../texutil/directxtexconversionjob.cc | 17 ++++- 9 files changed, 104 insertions(+), 78 deletions(-) create mode 100644 code/foundation/memory/memory.cc diff --git a/code/foundation/core/types.h b/code/foundation/core/types.h index 5c2644b47..e8f6c83ab 100644 --- a/code/foundation/core/types.h +++ b/code/foundation/core/types.h @@ -53,33 +53,6 @@ typedef uintptr PtrT; // the ptr type typedef ptrdiff PtrDiff; static const int InvalidIndex = -1; -//------------------------------------------------------------------------------ -/** -*/ -constexpr uint64 -operator"" _KB(const unsigned long long val) -{ - return val * 1024; -} - -//------------------------------------------------------------------------------ -/** -*/ -constexpr uint64 -operator"" _MB(const unsigned long long val) -{ - return val * 1024 * 1024; -} - -//------------------------------------------------------------------------------ -/** -*/ -constexpr uint64 -operator"" _GB(const unsigned long long val) -{ - return val * 1024 * 1024 * 1024; -} - //------------------------------------------------------------------------------ /** */ diff --git a/code/foundation/memory/memory.cc b/code/foundation/memory/memory.cc new file mode 100644 index 000000000..1b8ebbc11 --- /dev/null +++ b/code/foundation/memory/memory.cc @@ -0,0 +1,7 @@ +//------------------------------------------------------------------------------ +// memory.cc +// (C) 2024 Individual contributors, see AUTHORS file +//------------------------------------------------------------------------------ + +thread_local char ThreadLocalMiniHeap[10_MB]; +thread_local size_t ThreadLocalMiniHeapIterator = 0; diff --git a/code/foundation/memory/memory.h b/code/foundation/memory/memory.h index 009f29ec8..32cb4c040 100644 --- a/code/foundation/memory/memory.h +++ b/code/foundation/memory/memory.h @@ -11,6 +11,33 @@ */ #include "core/config.h" +//------------------------------------------------------------------------------ +/** +*/ +constexpr uint64_t +operator"" _KB(const unsigned long long val) +{ + return val * 1024; +} + +//------------------------------------------------------------------------------ +/** +*/ +constexpr uint64_t +operator"" _MB(const unsigned long long val) +{ + return val * 1024 * 1024; +} + +//------------------------------------------------------------------------------ +/** +*/ +constexpr uint64_t +operator"" _GB(const unsigned long long val) +{ + return val * 1024 * 1024 * 1024; +} + #if (__WIN32__) #include "memory/win32/win32memory.h" #elif ( __OSX__ || __APPLE__ || __linux__ ) @@ -19,6 +46,9 @@ #error "UNKNOWN PLATFORM" #endif +extern thread_local char ThreadLocalMiniHeap[]; +extern thread_local size_t ThreadLocalMiniHeapIterator; + //------------------------------------------------------------------------------ /** */ @@ -44,7 +74,8 @@ template TYPE* ArrayAllocStack(size_t size) { - TYPE* buffer = (TYPE*)StackAlloc(size * sizeof(TYPE)); + TYPE* buffer = (TYPE*)(ThreadLocalMiniHeap + ThreadLocalMiniHeapIterator); + ThreadLocalMiniHeapIterator += size * sizeof(TYPE); if constexpr (!std::is_trivially_constructible::value) { for (size_t i = 0; i < size; ++i) @@ -79,6 +110,8 @@ template void ArrayFreeStack(size_t size, TYPE* buffer) { + char* topPtr = (ThreadLocalMiniHeap + ThreadLocalMiniHeapIterator - size * sizeof(TYPE)); + n_assert(buffer == (TYPE*)topPtr); if constexpr (!std::is_trivially_destructible::value) { for (size_t i = 0; i < size; ++i) @@ -86,5 +119,5 @@ ArrayFreeStack(size_t size, TYPE* buffer) buffer[i].~TYPE(); } } - StackFree((void*)buffer); + ThreadLocalMiniHeapIterator -= size * sizeof(TYPE); } diff --git a/code/render/coregraphics/textureloader.cc b/code/render/coregraphics/textureloader.cc index d12f518f1..0ef8928b8 100644 --- a/code/render/coregraphics/textureloader.cc +++ b/code/render/coregraphics/textureloader.cc @@ -120,8 +120,8 @@ LoadMips(CoreGraphics::CmdBufferId cmdBuf, TextureStreamData* streamData, uint b void FinishMips(CoreGraphics::CmdBufferId transferCommands, CoreGraphics::CmdBufferId handoverCommands, TextureStreamData* streamData, uint mipBits, const CoreGraphics::TextureId texture, const char* name) { - // Finish the mips by handing them over - Util::FixedArray barriers(Util::PopCnt(mipBits) * streamData->numLayers); + // Finish the mips by handing them over + Util::FixedArray barriers(Util::PopCnt(mipBits) * streamData->numLayers); uint mipIndexToLoad = Util::FirstOne(mipBits); uint barrierCounter = 0; @@ -261,10 +261,8 @@ TextureLoader::InitializeResource(const ResourceLoadJob& job, const Ptr(id.id).stencilExtension; @@ -55,7 +55,7 @@ TextureGetVkStencilImageView(const CoreGraphics::TextureId id) //------------------------------------------------------------------------------ /** */ -const VkDevice +const VkDevice TextureGetVkDevice(const CoreGraphics::TextureId id) { return textureAllocator.ConstGet(id.id).dev; @@ -116,7 +116,7 @@ SetupSparse(VkDevice dev, VkImage img, Ids::Id32 sparseExtension, const VkTextur table.bindCounts[i].Resize(sparseMemoryRequirement.imageMipTailFirstLod); } - // create sparse bindings, + // create sparse bindings, for (uint32_t layer = 0; layer < info.layers; layer++) { for (SizeT mip = 0; mip < (SizeT)sparseMemoryRequirement.imageMipTailFirstLod; mip++) @@ -666,6 +666,8 @@ CreateTexture(const TextureCreateInfo& info) ObjectSetName(ret, loadInfo.name.Value()); #endif + TextureIdRelease(ret); + return ret; } @@ -1006,7 +1008,7 @@ TextureSparseEvict(const CoreGraphics::TextureId id, IndexT layer, IndexT mip, I const TextureSparsePageTable& table = textureSparseExtensionAllocator.ConstGet(sparseExtension); Util::Array& pageBinds = textureSparseExtensionAllocator.Get(sparseExtension); - + // get page and allocate memory CoreGraphics::TextureSparsePage& page = table.pages[layer][mip][pageIndex]; n_assert(page.alloc.mem != VK_NULL_HANDLE); @@ -1173,7 +1175,7 @@ TextureSparseCommitChanges(const CoreGraphics::TextureId id) if (opaqueBinds.IsEmpty() && pageBinds.IsEmpty()) return; - /* unused? + /* unused? // setup bind structs VkSparseImageMemoryBindInfo imageMemoryBindInfo = { @@ -1197,7 +1199,7 @@ TextureSparseCommitChanges(const CoreGraphics::TextureId id) pageBinds.IsEmpty() ? 0u : 1u, &imageMemoryBindInfo, 0, nullptr }; -*/ +*/ // execute sparse bind, the bind call Vulkan::SparseTextureBind(img, opaqueBinds, pageBinds); diff --git a/code/render/lighting/lightcontext.cc b/code/render/lighting/lightcontext.cc index 246b45274..3f9e646d2 100644 --- a/code/render/lighting/lightcontext.cc +++ b/code/render/lighting/lightcontext.cc @@ -119,7 +119,7 @@ LightContext::~LightContext() //------------------------------------------------------------------------------ /** */ -void +void LightContext::Create() { __CreateContext(); @@ -291,7 +291,7 @@ LightContext::Discard() //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetupGlobalLight( const Graphics::GraphicsEntityId id, const Math::vec3& color, @@ -317,13 +317,13 @@ LightContext::SetupGlobalLight( Math::point sunPosition(Math::cos(azimuth) * Math::sin(zenith), Math::cos(zenith), Math::sin(azimuth) * Math::sin(zenith)); Math::mat4 mat = lookatrh(Math::point(0.0f), sunPosition, Math::vector::upvec()); - + SetGlobalLightTransform(cid, mat, Math::xyz(sunPosition)); directionalLightAllocator.Get(lid) = backlight; directionalLightAllocator.Get(lid) = ambient; directionalLightAllocator.Get(lid) = backlightFactor; - if (castShadows) + if (castShadows && shadowCasterAllocator.Size() < 16) { // create new graphics entity for each view for (IndexT i = 0; i < Shared::NUM_CASCADES; i++) @@ -361,7 +361,7 @@ LightContext::GetAmbient(const Graphics::GraphicsEntityId id) //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3& ambient) { const Graphics::ContextEntityId cid = GetContextId(id); @@ -374,12 +374,12 @@ LightContext::SetAmbient(const Graphics::GraphicsEntityId id, Math::vec3& ambien //------------------------------------------------------------------------------ /** */ -void -LightContext::SetupPointLight(const Graphics::GraphicsEntityId id, - const Math::vec3& color, - const float intensity, - const float range, - bool castShadows, +void +LightContext::SetupPointLight(const Graphics::GraphicsEntityId id, + const Math::vec3& color, + const float intensity, + const float range, + bool castShadows, const CoreGraphics::TextureId projection) { n_assert(id != Graphics::GraphicsEntityId::Invalid()); @@ -416,15 +416,15 @@ LightContext::SetupPointLight(const Graphics::GraphicsEntityId id, //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetupSpotLight( - const Graphics::GraphicsEntityId id, + const Graphics::GraphicsEntityId id, const Math::vec3& color, - const float intensity, + const float intensity, const float innerConeAngle, const float outerConeAngle, const float range, - bool castShadows, + bool castShadows, const CoreGraphics::TextureId projection) { n_assert(id != Graphics::GraphicsEntityId::Invalid()); @@ -459,7 +459,7 @@ LightContext::SetupSpotLight( spotLightAllocator.Set(sli, id); spotLightAllocator.Set(sli, proj); - if (castShadows) + if (castShadows && shadowCasterAllocator.Size() < 16) { // allocate shadow caster slice Ids::Id32 casterId = shadowCasterAllocator.Alloc(); @@ -565,7 +565,7 @@ LightContext::SetupAreaLight( //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetColor(const Graphics::GraphicsEntityId id, const Math::vec3& color) { const Graphics::ContextEntityId cid = GetContextId(id); @@ -595,7 +595,7 @@ LightContext::SetRange(const Graphics::GraphicsEntityId id, const float range) //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetIntensity(const Graphics::GraphicsEntityId id, const float intensity) { const Graphics::ContextEntityId cid = GetContextId(id); @@ -849,7 +849,7 @@ LightContext::GetType(const Graphics::GraphicsEntityId id) //------------------------------------------------------------------------------ /** */ -void +void LightContext::GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float& inner, float& outer) { const Graphics::ContextEntityId cid = GetContextId(id); @@ -863,7 +863,7 @@ LightContext::GetInnerOuterAngle(const Graphics::GraphicsEntityId id, float& inn //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inner, float outer) { const Graphics::ContextEntityId cid = GetContextId(id); @@ -882,7 +882,7 @@ LightContext::SetInnerOuterAngle(const Graphics::GraphicsEntityId id, float inne //------------------------------------------------------------------------------ /** */ -void +void LightContext::OnPrepareView(const Ptr& view, const Graphics::FrameContext& ctx) { const Graphics::ContextEntityId cid = GetContextId(lightServerState.globalLightEntity); @@ -902,7 +902,7 @@ LightContext::OnPrepareView(const Ptr& view, const Graphics::Fra // do reverse lookup to find shadow caster Ids::Id32 ctxId = shadowCasterSliceMap[observers[i]]; Math::mat4 cascadeProj = lightServerState.csmUtil.GetCascadeViewProjection(i); - + shadowCasterAllocator.Get(ctxId) = cascadeProj; cascadeProj.store(lightServerState.shadowMatrixUniforms.LightViewMatrix[ctxId]); } @@ -997,7 +997,7 @@ LightContext::SetupTerrainShadows(const CoreGraphics::TextureId terrainShadowMap //------------------------------------------------------------------------------ /** */ -const CoreGraphics::BufferId +const CoreGraphics::BufferId LightContext::GetLightIndexBuffer() { return clusterState.clusterLightIndexLists; @@ -1015,7 +1015,7 @@ LightContext::GetLightsBuffer() //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetGlobalLightTransform(const Graphics::ContextEntityId id, const Math::mat4& transform, const Math::vector& direction) { auto lid = genericLightAllocator.Get(id.id); @@ -1026,7 +1026,7 @@ LightContext::SetGlobalLightTransform(const Graphics::ContextEntityId id, const //------------------------------------------------------------------------------ /** */ -void +void LightContext::SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id, const Math::mat4& transform) { auto lid = genericLightAllocator.Get(id.id); @@ -1036,7 +1036,7 @@ LightContext::SetGlobalLightViewProjTransform(const Graphics::ContextEntityId id //------------------------------------------------------------------------------ /** */ -void +void LightContext::UpdateViewDependentResources(const Ptr& view, const Graphics::FrameContext& ctx) { N_SCOPE(UpdateLightResources, Lighting); @@ -1074,7 +1074,7 @@ LightContext::UpdateViewDependentResources(const Ptr& view, cons flags |= LightsCluster::USE_SHADOW_BITFLAG; } params.GlobalLightFlags = flags; - params.GlobalLightShadowBias = 0.0001f; + params.GlobalLightShadowBias = 0.0001f; params.GlobalLightShadowIntensity = 1.0f; auto shadowDims = CoreGraphics::TextureGetDimensions(lightServerState.globalLightShadowMap); params.GlobalLightShadowMapSize[0] = 1.0f / shadowDims.width; @@ -1181,7 +1181,7 @@ LightContext::UpdateViewDependentResources(const Ptr& view, cons trans.position.store3(spotLight.position); forward.store3(spotLight.forward); (color[i] * intensity[i]).store(spotLight.color); - + // calculate sine and cosine spotLight.angleSinCos[0] = Math::sin(angles[1]); spotLight.angleSinCos[1] = Math::cos(angles[1]); @@ -1197,7 +1197,7 @@ LightContext::UpdateViewDependentResources(const Ptr& view, cons Math::transform44 trans = areaLightAllocator.Get(typeIds[i]); bool twoSided = areaLightAllocator.Get(typeIds[i]); auto shape = areaLightAllocator.Get(typeIds[i]); - + auto& areaLight = clusterState.lightList.AreaLights[numAreaLights]; areaLight.shadowExtension = -1; @@ -1219,7 +1219,7 @@ LightContext::UpdateViewDependentResources(const Ptr& view, cons trans.setposition(trans.getposition() + Math::vector(0, 0, twoSided ? 0 : -range[i] / 2)); Math::mat4 viewSpace = viewTransform * trans.getmatrix(); Math::bbox bbox(viewSpace); - + bbox.pmin.store3(areaLight.bboxMin); areaLight.range = range[i]; bbox.pmax.store3(areaLight.bboxMax); @@ -1374,7 +1374,7 @@ void LightContext::OnRenderDebug(uint32_t flags) { using namespace CoreGraphics; - auto const& types = genericLightAllocator.GetArray(); + auto const& types = genericLightAllocator.GetArray(); auto const& colors = genericLightAllocator.GetArray(); auto const& ranges = genericLightAllocator.GetArray(); auto const& ids = genericLightAllocator.GetArray(); @@ -1426,13 +1426,13 @@ LightContext::OnRenderDebug(uint32_t flags) // we want the points to first get projected, then transformed v * Projection * Transform; Math::mat4 frustum = unscaledTransform * proj; - + Math::vec4 col = Math::vec4(colors[i], 1.0f); RenderShape shape; shape.SetupSimpleShape( - RenderShape::Box, - RenderShape::RenderFlag(RenderShape::CheckDepth | RenderShape::Wireframe), + RenderShape::Box, + RenderShape::RenderFlag(RenderShape::CheckDepth | RenderShape::Wireframe), col, frustum); shapeRenderer->AddShape(shape); diff --git a/code/render/materials/gltf.json b/code/render/materials/gltf.json index 277c9a53a..5b109931a 100644 --- a/code/render/materials/gltf.json +++ b/code/render/materials/gltf.json @@ -73,7 +73,7 @@ }, { "name": "emissiveTexture", - "defaultValue": "tex:system/white.dds" + "defaultValue": "tex:system/black.dds" }, { "name": "occlusionTexture", diff --git a/toolkit/toolkitutil/model/import/gltf/ngltfmaterialexporter.cc b/toolkit/toolkitutil/model/import/gltf/ngltfmaterialexporter.cc index d27cc80ef..95a52cc61 100644 --- a/toolkit/toolkitutil/model/import/gltf/ngltfmaterialexporter.cc +++ b/toolkit/toolkitutil/model/import/gltf/ngltfmaterialexporter.cc @@ -174,7 +174,7 @@ NglTFMaterialExtractor::ExtractMaterial(SurfaceBuilder& builder, Gltf::Material } else { - builder.AddParam("emissiveTexture", "systex:white"); + builder.AddParam("emissiveTexture", "systex:black"); } if (material.occlusionTexture.index != -1) diff --git a/toolkit/toolkitutil/texutil/directxtexconversionjob.cc b/toolkit/toolkitutil/texutil/directxtexconversionjob.cc index ce3e4ef02..610927ffb 100644 --- a/toolkit/toolkitutil/texutil/directxtexconversionjob.cc +++ b/toolkit/toolkitutil/texutil/directxtexconversionjob.cc @@ -79,7 +79,9 @@ DirectXTexConversionJob::Convert() { None, NormalMap, - HeightMap + MaterialMap, + HeightMap, + }; TextureDataType type = TextureDataType::None; @@ -96,7 +98,15 @@ DirectXTexConversionJob::Convert() this->logger->Print("%s ", "Normal Map (BC5 UNORM)"_text.Color(TextColor::Yellow).AsCharPtr()); type = TextureDataType::NormalMap; } - + else if (String::MatchPattern(this->srcPath, "*material.*") || + String::MatchPattern(this->srcPath, "*orm.*") || + String::MatchPattern(this->srcPath, "*ORM.*") || + String::MatchPattern(this->srcPath, "*MetallicRoughness.*") || + String::MatchPattern(this->srcPath, "*Occlusion.*")) + { + this->logger->Print("%s ", "Material/Occlusion Data Map (BC7 UNORM)"_text.Color(TextColor::Yellow).AsCharPtr()); + type = TextureDataType::MaterialMap; + } else if (String::MatchPattern(this->srcPath, "*height.*")) { this->logger->Print("%s ", "Height Map (R16 UNORM)"_text.Color(TextColor::Yellow).AsCharPtr()); @@ -145,6 +155,9 @@ DirectXTexConversionJob::Convert() case HeightMap: args.Append(" -f R16_UNORM "); break; + case MaterialMap: + args.Append(" -f BC7_UNORM "); + break; } args.Append(" \"");