From 80078bf78eb4624c957f7be3bdc1e74431796676 Mon Sep 17 00:00:00 2001 From: Roderick Kennedy Date: Fri, 27 Sep 2024 16:49:26 +0100 Subject: [PATCH 1/6] Sampler error check. --- CrossPlatform/RenderPlatform.cpp | 5 +++-- CrossPlatform/Shaders/mip.sfx | 2 +- CrossPlatform/Shaders/random.sl | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CrossPlatform/RenderPlatform.cpp b/CrossPlatform/RenderPlatform.cpp index 4fcc11df..137c2280 100644 --- a/CrossPlatform/RenderPlatform.cpp +++ b/CrossPlatform/RenderPlatform.cpp @@ -535,7 +535,8 @@ void RenderPlatform::InvalidateDeviceObjects() shaders.clear();*/ for(auto s:sharedSamplerStates) { - s.second->InvalidateDeviceObjects(); + if(s.second) + s.second->InvalidateDeviceObjects(); delete s.second; } sharedSamplerStates.clear(); @@ -1710,7 +1711,7 @@ int2 RenderPlatform::DrawTexture(GraphicsDeviceContext &deviceContext, int x1, i tech = debugEffect->GetTechniqueByName("show_texture_array"); debugEffect->SetTexture(deviceContext, "imageTextureArray", tex, {TextureAspectFlags::COLOUR, (uint8_t)displayMip, 1, 0, (uint8_t)-1}); debugConstants.displayLayer = UpdateDisplayLayer(tex->arraySize); - } + } else if(tex) { tech = textured; diff --git a/CrossPlatform/Shaders/mip.sfx b/CrossPlatform/Shaders/mip.sfx index b7ee56ad..f669e86d 100644 --- a/CrossPlatform/Shaders/mip.sfx +++ b/CrossPlatform/Shaders/mip.sfx @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2021 Simul Software Ltd. All rights reserved. +// Copyright (c) 2015-2024 Simul Software Ltd. All rights reserved. #include "shader_platform.sl" #include "common.sl" #include "render_states.sl" diff --git a/CrossPlatform/Shaders/random.sl b/CrossPlatform/Shaders/random.sl index 67f6d462..527095e6 100644 --- a/CrossPlatform/Shaders/random.sl +++ b/CrossPlatform/Shaders/random.sl @@ -39,7 +39,6 @@ vec3 PcgRand3(uint seed) return r; } - vec3 PcgSphericalRandom(uint seed) { vec3 pcgrand=PcgRand3(seed); From 014dc733ff3d6c169aefee0f24ace03ef916d743 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Fri, 1 Nov 2024 11:25:44 +0000 Subject: [PATCH 2/6] Removed RenderPlatform::Destroy. --- CrossPlatform/HdrRenderer.cpp | 9 +++---- CrossPlatform/RenderPlatform.cpp | 37 ++++++---------------------- CrossPlatform/RenderPlatform.h | 3 --- CrossPlatform/SphericalHarmonics.cpp | 12 ++++----- 4 files changed, 17 insertions(+), 44 deletions(-) diff --git a/CrossPlatform/HdrRenderer.cpp b/CrossPlatform/HdrRenderer.cpp index a0309768..0ed2dd73 100644 --- a/CrossPlatform/HdrRenderer.cpp +++ b/CrossPlatform/HdrRenderer.cpp @@ -88,14 +88,14 @@ void HdrRenderer::RecompileShaders() void HdrRenderer::NotifyEffectsRecompiled() { - renderPlatform->Destroy(hdr_effect); + SAFE_DELETE(hdr_effect); } void HdrRenderer::LoadShaders() { if(!renderPlatform) return; - renderPlatform->Destroy(hdr_effect); + SAFE_DELETE(hdr_effect); hdr_effect =renderPlatform->CreateEffect("hdr"); exposureGammaTechnique =hdr_effect->GetTechniqueByName("exposure_gamma"); @@ -110,10 +110,7 @@ void HdrRenderer::InvalidateDeviceObjects() { hdrConstants.InvalidateDeviceObjects(); imageConstants.InvalidateDeviceObjects(); - if (renderPlatform) - { - renderPlatform->Destroy(hdr_effect); - } + SAFE_DELETE(hdr_effect); renderPlatform=NULL; } void HdrRenderer::Render(GraphicsDeviceContext &deviceContext,crossplatform::Texture *texture,float Exposure,float Gamma) diff --git a/CrossPlatform/RenderPlatform.cpp b/CrossPlatform/RenderPlatform.cpp index 3ad1ee5f..ac177223 100644 --- a/CrossPlatform/RenderPlatform.cpp +++ b/CrossPlatform/RenderPlatform.cpp @@ -465,16 +465,16 @@ void RenderPlatform::RestoreDeviceObjects(void*) mat->SetEffect(solidEffect); } - Destroy(debugEffect); + SAFE_DELETE(debugEffect); debugEffect=CreateEffect("debug"); - Destroy(solidEffect); + SAFE_DELETE(solidEffect); solidEffect=CreateEffect("solid"); - Destroy(copyEffect); + SAFE_DELETE(copyEffect); copyEffect=CreateEffect("copy"); - Destroy(mipEffect); + SAFE_DELETE(mipEffect); mipEffect=CreateEffect("mip"); if(debugEffect) @@ -493,11 +493,6 @@ void RenderPlatform::InvalidateDeviceObjects() { if(gpuProfiler) gpuProfiler->InvalidateDeviceObjects(); - for (auto e : destroyEffects) - { - SAFE_DELETE(e); - } - destroyEffects.clear(); if(textRenderer) textRenderer->InvalidateDeviceObjects(); for(std::map::iterator i=standardRenderStates.begin();i!=standardRenderStates.end();i++) @@ -560,23 +555,16 @@ void RenderPlatform::RecompileShaders() void RenderPlatform::LoadShaders() { -/* for (auto s : shaders) - { - s.second->Release(); - delete s.second; - } - shaders.clear();*/ - - Destroy(debugEffect); + SAFE_DELETE(debugEffect); debugEffect=CreateEffect("debug"); - Destroy(solidEffect); + SAFE_DELETE(solidEffect); solidEffect=CreateEffect("solid"); - Destroy(copyEffect); + SAFE_DELETE(copyEffect); copyEffect=CreateEffect("copy"); - Destroy(mipEffect); + SAFE_DELETE(mipEffect); mipEffect=CreateEffect("mip"); if(debugEffect) @@ -2014,15 +2002,6 @@ SamplerState *RenderPlatform::GetOrCreateSamplerStateByName (const char *name_ut return ss; } -void RenderPlatform::Destroy(Effect *&e) -{ - if (e) - { - destroyEffects.insert(e); - e = nullptr; - } -} - Effect *RenderPlatform::CreateEffect(const char *filename_utf8, bool checkRecompileShaders) { std::string fn(filename_utf8); diff --git a/CrossPlatform/RenderPlatform.h b/CrossPlatform/RenderPlatform.h index 895d615f..fdb18bda 100644 --- a/CrossPlatform/RenderPlatform.h +++ b/CrossPlatform/RenderPlatform.h @@ -363,8 +363,6 @@ namespace platform /// This is for states that will be shared by multiple shaders. There will be a warning if a description is passed that conflicts with the current definition, /// as the Effects system assumes that SamplerState names are unique. SamplerState *GetOrCreateSamplerStateByName (const char *name_utf8,platform::crossplatform::SamplerStateDesc *desc=0); - /// Destroy the effect when it is safe to do so. The pointer can now be reassigned or nulled. - void Destroy(Effect *&e); /// Create a platform-specific effect instance. virtual Effect *CreateEffect ()=0; /// Create a platform-specific effect instance. @@ -597,7 +595,6 @@ namespace platform std::set fencedTextures; virtual void ResetImmediateCommandList() {} public: - std::set< Effect*> destroyEffects; std::map effects; // all shaders are stored here and referenced by techniques. //std::map shaders; diff --git a/CrossPlatform/SphericalHarmonics.cpp b/CrossPlatform/SphericalHarmonics.cpp index 2a9761e4..29972da7 100644 --- a/CrossPlatform/SphericalHarmonics.cpp +++ b/CrossPlatform/SphericalHarmonics.cpp @@ -47,16 +47,16 @@ void SphericalHarmonics::RecompileShaders() void SphericalHarmonics::NotifyEffectsRecompiled() { - SAFE_DESTROY(renderPlatform, sphericalHarmonicsEffect); - SAFE_DESTROY(renderPlatform, lightProbesEffect); + SAFE_DELETE(sphericalHarmonicsEffect); + SAFE_DELETE(lightProbesEffect); } void SphericalHarmonics::LoadShaders() { if (!renderPlatform) return; - SAFE_DESTROY(renderPlatform, sphericalHarmonicsEffect); - SAFE_DESTROY(renderPlatform, lightProbesEffect); + SAFE_DELETE(sphericalHarmonicsEffect); + SAFE_DELETE(lightProbesEffect); sphericalHarmonicsEffect =renderPlatform->CreateEffect("spherical_harmonics"); jitter =sphericalHarmonicsEffect->GetTechniqueByName("jitter"); encode =sphericalHarmonicsEffect->GetTechniqueByName("encode"); @@ -77,8 +77,8 @@ void SphericalHarmonics::InvalidateDeviceObjects() ResetBuffers(); lightProbeConstants.InvalidateDeviceObjects(); sphericalHarmonicsConstants.InvalidateDeviceObjects(); - SAFE_DESTROY(renderPlatform,sphericalHarmonicsEffect); - SAFE_DESTROY(renderPlatform, lightProbesEffect); + SAFE_DELETE(sphericalHarmonicsEffect); + SAFE_DELETE(lightProbesEffect); } void SphericalHarmonics::ResetBuffers() From d22f9282f80160510e95eb98b088da8849ed9a8d Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 7 Nov 2024 08:41:35 +0000 Subject: [PATCH 3/6] Updated FileLoader::GetFileDate() to return a unix timastamp. Macros for C++17 and C++20 added too. Fixed some old DX11 files as well. --- Core/DefaultFileLoader.cpp | 45 ++++++++++++++------------------- Core/DefaultFileLoader.h | 2 +- Core/FileLoader.cpp | 14 ++-------- Core/FileLoader.h | 6 ++--- Core/RuntimeError.h | 9 +++++++ CrossPlatform/Effect.cpp | 11 ++++++++ DirectX11/CompileShaderDX1x.cpp | 14 +++++----- DirectX11/CompileShaderDX1x.h | 8 +++--- DirectX11/CreateEffectDX1x.cpp | 8 +++--- 9 files changed, 59 insertions(+), 58 deletions(-) diff --git a/Core/DefaultFileLoader.cpp b/Core/DefaultFileLoader.cpp index cf31f508..7747ec47 100644 --- a/Core/DefaultFileLoader.cpp +++ b/Core/DefaultFileLoader.cpp @@ -153,10 +153,10 @@ void DefaultFileLoader::AcquireFileContents(void *&pointer, unsigned int &bytes, filesLoaded.insert(filename_utf8); } -double DefaultFileLoader::GetFileDate(const char *filename_utf8) const +uint64_t DefaultFileLoader::GetFileDate(const char *filename_utf8) const { if (!FileExists(filename_utf8)) - return 0.0; + return 0; std::wstring wstr = platform::core::Utf8ToWString(filename_utf8); FILE *fp = nullptr; @@ -168,44 +168,37 @@ double DefaultFileLoader::GetFileDate(const char *filename_utf8) const if (!fp) { // std::cerr<<"Failed to find file "<(fileTime); + const time_t time = std::chrono::system_clock::to_time_t(systemTime); +#else + std::chrono::system_clock::time_point systemTime = std::chrono::time_point_cast(fileTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()); + const time_t time = std::chrono::system_clock::to_time_t(systemTime); +#endif + return time; +#endif + #if defined(_MSC_VER) && defined(_WIN32) struct _stat buf; _wstat(wstr.c_str(), &buf); - buf.st_mtime; time_t t = buf.st_mtime; - struct tm lt; - gmtime_s(<, &t); - // Note: bizarrely, the tm structure has MONTHS starting at ZERO, but DAYS start at 1. - double daynum = GetDayNumberFromDateTime(1900 + lt.tm_year, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec); - return daynum; -#elif PLATFORM_STD_FILESYSTEM -#ifdef CPP20 - auto write_time = fs::last_write_time(filename_utf8); - const auto systemTime = std::chrono::clock_cast(fileTime); - const auto time = std::chrono::system_clock::to_time_t(systemTime); - return ((double)ns) / (3600.0 * 24.0 * 1000000.0); + return t; #else std::wstring filenamew = StringToWString(filename_utf8); struct stat buf; stat(filename_utf8, &buf); - buf.st_mtime; time_t t = buf.st_mtime; - struct tm lt; -#if __COMMODORE__ - gmtime_s(&t, <); -#else - gmtime_r(&t, <); -#endif - double datetime = GetDayNumberFromDateTime(1900 + lt.tm_year, lt.tm_mon, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec); - return datetime; + return t; #endif -#else return 0; -#endif } bool DefaultFileLoader::Save(const void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text) diff --git a/Core/DefaultFileLoader.h b/Core/DefaultFileLoader.h index 001fd0a9..837192f7 100644 --- a/Core/DefaultFileLoader.h +++ b/Core/DefaultFileLoader.h @@ -16,7 +16,7 @@ namespace platform ~DefaultFileLoader() = default; bool FileExists(const char *filename_utf8) const override; void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text) override; - double GetFileDate(const char* filename_utf8) const override; + uint64_t GetFileDate(const char* filename_utf8) const override; void ReleaseFileContents(void* pointer) override; bool Save(const void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text) override; }; diff --git a/Core/FileLoader.cpp b/Core/FileLoader.cpp index 70686bc9..b8235de1 100644 --- a/Core/FileLoader.cpp +++ b/Core/FileLoader.cpp @@ -91,16 +91,6 @@ int FileLoader::NextSlash(const std::string &str, int pos) return slash; } -double FileLoader::GetDayNumberFromDateTime(int year, int month, int day, int hour, int min, int sec) -{ - int D = 367 * year - (7 * (year + ((month + 9) / 12))) / 4 + (275 * month) / 9 + day - 730531; // was +2451545 - double d = (double)D; - d += (double)hour / 24.0; - d += (double)min / 24.0 / 60.0; - d += (double)sec / 24.0 / 3600.0; - return d; -} - #if defined(_MSC_VER) && !defined(_GAMING_XBOX) && !defined(__SWITCH__) && PLATFORM_STD_FILESYSTEM > 0 std::string FileLoader::FindParentFolder(const char *folder_utf8) { @@ -252,8 +242,8 @@ int FileLoader::FindIndexInPathStack(const char* filename_utf8, const char* cons break; } } - double newest_date = 0.0; + uint64_t newest_date = 0; int index = 0; for (; i >= 0; i--) { @@ -268,7 +258,7 @@ int FileLoader::FindIndexInPathStack(const char* filename_utf8, const char* cons f += filename_utf8; if (FileExists(f.c_str())) { - double filedate = GetFileDate(f.c_str()); + uint64_t filedate = GetFileDate(f.c_str()); if (filedate >= newest_date) { fn = f; diff --git a/Core/FileLoader.h b/Core/FileLoader.h index b2609b79..94ea6c9c 100644 --- a/Core/FileLoader.h +++ b/Core/FileLoader.h @@ -31,8 +31,6 @@ namespace platform //! Get the position of the next slash in the string from the current position. static int NextSlash(const std::string &str, int pos = 0); - //! Get the Day Number (decimal Julian) from DateTime. Useful for file time stamps. - static double GetDayNumberFromDateTime(int year, int month, int day, int hour, int min, int sec); //! Get the parent folder of the current directory. static std::string FindParentFolder(const char *folder_utf8); @@ -43,8 +41,8 @@ namespace platform //! The memory should later be freed by a call to ReleaseFileContents. //! The filename should be unicode UTF8-encoded. virtual void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text)=0; - //! Get the file date as a julian day number. Return zero if the file doesn't exist. - virtual double GetFileDate(const char* filename_utf8) const=0; + //! Get the unix timestamp. Return zero if the file doesn't exist. + virtual uint64_t GetFileDate(const char* filename_utf8) const=0; //! Free the memory allocated by AcquireFileContents. virtual void ReleaseFileContents(void* pointer)=0; //! Save the chunk of memory to storage. diff --git a/Core/RuntimeError.h b/Core/RuntimeError.h index bdff70f4..2eb7fa45 100644 --- a/Core/RuntimeError.h +++ b/Core/RuntimeError.h @@ -35,6 +35,15 @@ #endif #include // for runtime_error +// C++ Versions +#if defined(_WIN64) +#define PLATFORM_CXX20 _HAS_CXX20 +#define PLATFORM_CXX17 _HAS_CXX17 +#elif defined(__linux__) +#define PLATFORM_CXX20 (__cplusplus == 202002L) +#define PLATFORM_CXX17 (__cplusplus == 201703L) +#endif + #define SIMUL_COUT \ std::cout << __FILE__ << "(" << std::dec << __LINE__ << "): info: " diff --git a/CrossPlatform/Effect.cpp b/CrossPlatform/Effect.cpp index 206b48d0..68a5a181 100644 --- a/CrossPlatform/Effect.cpp +++ b/CrossPlatform/Effect.cpp @@ -1044,6 +1044,17 @@ bool Effect::Load(crossplatform::RenderPlatform *r, const char *filename_utf8) std::string sfxbFilenameUtf8 = binFilenameUtf8; platform::core::find_and_replace(sfxbFilenameUtf8, ".sfxo", ".sfxb"); + uint64_t timestamp = platform::core::FileLoader::GetFileLoader()->GetFileDate(sfxbFilenameUtf8.c_str()); + struct tm timeDate; + gmtime_s(&timeDate, (time_t *)×tamp); + std::cout << sfxbFilenameUtf8 << " write time " << + (timeDate.tm_year + 1900) << "-" << + (timeDate.tm_mon + 1) << "-" << + (timeDate.tm_mday) << " " << + (timeDate.tm_hour) << ":" << + (timeDate.tm_min) << ":" << + (timeDate.tm_sec) << std::endl; + platform::core::FileLoader::GetFileLoader()->AcquireFileContents(ptr,num_bytes, binFilenameUtf8.c_str(),true); filenameInUseUtf8=binFilenameUtf8; void *bin_ptr=nullptr; diff --git a/DirectX11/CompileShaderDX1x.cpp b/DirectX11/CompileShaderDX1x.cpp index f835b4be..eab81da6 100644 --- a/DirectX11/CompileShaderDX1x.cpp +++ b/DirectX11/CompileShaderDX1x.cpp @@ -78,8 +78,8 @@ HRESULT __stdcall ShaderIncludeHandler::Close(LPCVOID pData) return S_OK; } -DetectChangesIncludeHandler::DetectChangesIncludeHandler(const char* shaderDirUtf8,const std::vector &shaderPathsUtf8, double binaryTime ) - : m_ShaderDirUtf8(shaderDirUtf8), lastCompileTime(binaryTime), newest(0.0) +DetectChangesIncludeHandler::DetectChangesIncludeHandler(const char* shaderDirUtf8,const std::vector &shaderPathsUtf8, uint64_t binaryTime ) + : m_ShaderDirUtf8(shaderDirUtf8), lastCompileTime(binaryTime), newest(0) { m_pathsUtf8 = shaderPathsUtf8; m_pathsUtf8.push_back(shaderDirUtf8); @@ -110,16 +110,16 @@ ERRNO_CHECK } if(finalPathUtf8.length()==0) { - newest=0.0; + newest=0; SIMUL_CERR<<"Can't find include file "<GetFileDate(finalPathUtf8.c_str()); - if(dateTimeJdn>newest) - newest=dateTimeJdn; - if(dateTimeJdn>lastCompileTime) + uint64_t timestamp= platform::core::FileLoader::GetFileLoader()->GetFileDate(finalPathUtf8.c_str()); + if(timestamp>newest) + newest=timestamp; + if(timestamp>lastCompileTime) { // Early-out if we're testing against a specific compile time. return E_FAIL; diff --git a/DirectX11/CompileShaderDX1x.h b/DirectX11/CompileShaderDX1x.h index 285b5c6e..2c846513 100644 --- a/DirectX11/CompileShaderDX1x.h +++ b/DirectX11/CompileShaderDX1x.h @@ -34,12 +34,12 @@ namespace platform class DetectChangesIncludeHandler : public ID3DInclude { public: - DetectChangesIncludeHandler(const char* shaderDirUtf8,const std::vector &shaderPathsUtf8, double binaryTime = 0.0); + DetectChangesIncludeHandler(const char* shaderDirUtf8,const std::vector &shaderPathsUtf8, uint64_t binaryTime = 0); HRESULT __stdcall Open(D3D_INCLUDE_TYPE IncludeType,LPCSTR pFileNameUtf8, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes); HRESULT __stdcall Close(LPCVOID pData); /// The newest file. If the d3d process fails and this is 0, it means we had a missing file. - double GetNewestIncludeDateJDN() const + uint64_t GetNewestIncludeTimestamp() const { return newest; } @@ -47,8 +47,8 @@ namespace platform std::vector m_pathsUtf8; std::string m_ShaderDirUtf8; std::string m_SystemDirUtf8; - double lastCompileTime; - double newest; + uint64_t lastCompileTime; + uint64_t newest; }; } } diff --git a/DirectX11/CreateEffectDX1x.cpp b/DirectX11/CreateEffectDX1x.cpp index b387d7d6..242f1390 100644 --- a/DirectX11/CreateEffectDX1x.cpp +++ b/DirectX11/CreateEffectDX1x.cpp @@ -208,7 +208,7 @@ struct d3dMacro }; static double GetNewestIncludeFileDate(std::string text_filename_utf8,const std::vector &shaderPathsUtf8 - ,void *textData,size_t textSize,D3D_SHADER_MACRO *macros,double binary_date_jdn,bool &missing) + ,void *textData,size_t textSize,D3D_SHADER_MACRO *macros,uint64_t binary_date,bool &missing) { ID3DBlob *binaryBlob=NULL; ID3DBlob *errorMsgs=NULL; @@ -217,7 +217,7 @@ static double GetNewestIncludeFileDate(std::string text_filename_utf8,const std: if(pos<0||bpos>pos) pos=bpos; std::string path_utf8=text_filename_utf8.substr(0,pos); - DetectChangesIncludeHandler detectChangesIncludeHandler(path_utf8.c_str(),shaderPathsUtf8,binary_date_jdn); + DetectChangesIncludeHandler detectChangesIncludeHandler(path_utf8.c_str(),shaderPathsUtf8,binary_date); HRESULT hr=D3DPreprocess( textData ,textSize ,text_filename_utf8.c_str() //in LPCSTR pSourceName, @@ -227,12 +227,12 @@ static double GetNewestIncludeFileDate(std::string text_filename_utf8,const std: ,&errorMsgs //ID3DBlob **ppErrorMsgs ); // Don't V_CHECK here as we'll often expect a S_FAIL result for early-out. - double newestFileTime=detectChangesIncludeHandler.GetNewestIncludeDateJDN(); + uint64_t newestFileTime=detectChangesIncludeHandler.GetNewestIncludeTimestamp(); if(binaryBlob) binaryBlob->Release(); if(errorMsgs) errorMsgs->Release(); - missing=(newestFileTime==0.0&&hr!=S_OK); + missing=(newestFileTime==0&&hr!=S_OK); return newestFileTime; } From 1e907377af554e6ed0e9946511a4f1cdf1d848f1 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 7 Nov 2024 08:49:39 +0000 Subject: [PATCH 4/6] Removed Timestamp logging in Effect::Load(). --- CrossPlatform/Effect.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CrossPlatform/Effect.cpp b/CrossPlatform/Effect.cpp index 68a5a181..248ef4ea 100644 --- a/CrossPlatform/Effect.cpp +++ b/CrossPlatform/Effect.cpp @@ -1044,6 +1044,7 @@ bool Effect::Load(crossplatform::RenderPlatform *r, const char *filename_utf8) std::string sfxbFilenameUtf8 = binFilenameUtf8; platform::core::find_and_replace(sfxbFilenameUtf8, ".sfxo", ".sfxb"); + #if 0 uint64_t timestamp = platform::core::FileLoader::GetFileLoader()->GetFileDate(sfxbFilenameUtf8.c_str()); struct tm timeDate; gmtime_s(&timeDate, (time_t *)×tamp); @@ -1054,6 +1055,7 @@ bool Effect::Load(crossplatform::RenderPlatform *r, const char *filename_utf8) (timeDate.tm_hour) << ":" << (timeDate.tm_min) << ":" << (timeDate.tm_sec) << std::endl; + #endif platform::core::FileLoader::GetFileLoader()->AcquireFileContents(ptr,num_bytes, binFilenameUtf8.c_str(),true); filenameInUseUtf8=binFilenameUtf8; From 7faf289709e8504ed87a686841214d7bb9475971 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 7 Nov 2024 09:03:19 +0000 Subject: [PATCH 5/6] Updated Effect creation to use std::shared_ptr. GetOrCreateEffect() replaces CreateEffect(). Set std::shared_ptr to reset it and decrement the count. Cleaned up DX11 trying to delete debugEffect. --- Applications/Test/Test.cpp | 16 ++-- CrossPlatform/HdrRenderer.cpp | 8 +- CrossPlatform/HdrRenderer.h | 2 +- CrossPlatform/Material.cpp | 4 +- CrossPlatform/Material.h | 6 +- CrossPlatform/MeshRenderer.cpp | 2 +- CrossPlatform/MeshRenderer.h | 2 +- CrossPlatform/RenderPlatform.cpp | 117 +++++++++------------------ CrossPlatform/RenderPlatform.h | 21 +++-- CrossPlatform/SphereRenderer.cpp | 4 +- CrossPlatform/SphereRenderer.h | 2 +- CrossPlatform/SphericalHarmonics.cpp | 16 ++-- CrossPlatform/SphericalHarmonics.h | 4 +- CrossPlatform/Text3DRenderer.cpp | 5 +- CrossPlatform/Text3DRenderer.h | 2 +- CrossPlatform/TextRenderer.cpp | 9 +-- CrossPlatform/TextRenderer.h | 2 +- DirectX11/RenderPlatform.cpp | 1 - ImGui/imgui_impl_platform.cpp | 12 +-- 19 files changed, 90 insertions(+), 145 deletions(-) diff --git a/Applications/Test/Test.cpp b/Applications/Test/Test.cpp index 4b39daf3..c5571570 100644 --- a/Applications/Test/Test.cpp +++ b/Applications/Test/Test.cpp @@ -121,8 +121,8 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface crossplatform::Texture* depthTexture = nullptr; crossplatform::HdrRenderer* hdrRenderer = nullptr; crossplatform::Framebuffer* hdrFramebuffer = nullptr; - crossplatform::Effect* effect = nullptr; - crossplatform::Effect* test = nullptr; + std::shared_ptr effect = nullptr; + std::shared_ptr test = nullptr; crossplatform::Texture* texture = nullptr; crossplatform::ConstantBuffer sceneConstants; crossplatform::ConstantBuffer cameraConstants; @@ -272,8 +272,8 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface rwSB.~StructuredBuffer(); roSB.~StructuredBuffer(); delete texture; - delete test; - delete effect; + test = nullptr; + effect = nullptr; delete hdrFramebuffer; delete hdrRenderer; delete depthTexture; @@ -288,11 +288,10 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface hdrRenderer->RestoreDeviceObjects(renderPlatform); hdrFramebuffer->RestoreDeviceObjects(renderPlatform); - effect = renderPlatform->CreateEffect(); - effect->Load(renderPlatform, "solid"); + effect = renderPlatform->GetOrCreateEffect("solid"); sceneConstants.RestoreDeviceObjects(renderPlatform); cameraConstants.RestoreDeviceObjects(renderPlatform); - test = renderPlatform->CreateEffect("Test"); + test = renderPlatform->GetOrCreateEffect("Test"); texture = renderPlatform->CreateTexture(); } @@ -301,7 +300,6 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface if (effect) { effect->InvalidateDeviceObjects(); - delete effect; effect = nullptr; } sceneConstants.InvalidateDeviceObjects(); @@ -423,7 +421,7 @@ class PlatformRenderer : public crossplatform::RenderDelegatorInterface hdrFramebuffer->Clear(deviceContext, 0.00f, 0.31f, 0.57f, 1.00f, reverseDepth ? 0.0f : 1.0f); renderPlatform->GetDebugConstantBuffer().multiplier = vec4(0.0f, 0.33f, 1.0f, 1.0f); renderPlatform->SetConstantBuffer(deviceContext, &(renderPlatform->GetDebugConstantBuffer())); - renderPlatform->DrawQuad(deviceContext, w / 4, h / 4, w / 2, h / 2, renderPlatform->GetDebugEffect(), renderPlatform->GetDebugEffect()->GetTechniqueByName("untextured"), "noblend"); + renderPlatform->DrawQuad(deviceContext, w / 4, h / 4, w / 2, h / 2, renderPlatform->GetDebugEffect().get(), renderPlatform->GetDebugEffect()->GetTechniqueByName("untextured"), "noblend"); } void Test_Text(crossplatform::GraphicsDeviceContext& deviceContext, int w, int h) diff --git a/CrossPlatform/HdrRenderer.cpp b/CrossPlatform/HdrRenderer.cpp index 0ed2dd73..5b731750 100644 --- a/CrossPlatform/HdrRenderer.cpp +++ b/CrossPlatform/HdrRenderer.cpp @@ -88,15 +88,15 @@ void HdrRenderer::RecompileShaders() void HdrRenderer::NotifyEffectsRecompiled() { - SAFE_DELETE(hdr_effect); + hdr_effect=nullptr; } void HdrRenderer::LoadShaders() { if(!renderPlatform) return; - SAFE_DELETE(hdr_effect); - hdr_effect =renderPlatform->CreateEffect("hdr"); + hdr_effect = nullptr; + hdr_effect =renderPlatform->GetOrCreateEffect("hdr"); exposureGammaTechnique =hdr_effect->GetTechniqueByName("exposure_gamma"); @@ -110,7 +110,7 @@ void HdrRenderer::InvalidateDeviceObjects() { hdrConstants.InvalidateDeviceObjects(); imageConstants.InvalidateDeviceObjects(); - SAFE_DELETE(hdr_effect); + hdr_effect = nullptr; renderPlatform=NULL; } void HdrRenderer::Render(GraphicsDeviceContext &deviceContext,crossplatform::Texture *texture,float Exposure,float Gamma) diff --git a/CrossPlatform/HdrRenderer.h b/CrossPlatform/HdrRenderer.h index 0c341e32..0f84a8b4 100644 --- a/CrossPlatform/HdrRenderer.h +++ b/CrossPlatform/HdrRenderer.h @@ -40,7 +40,7 @@ namespace platform crossplatform::RenderPlatform *renderPlatform; int Width,Height; //! The HDR tonemapping hlsl effect used to render the hdr buffer to an ldr screen. - crossplatform::Effect* hdr_effect; + std::shared_ptr hdr_effect; crossplatform::EffectTechnique* exposureGammaTechnique; crossplatform::EffectPass* exposureGammaMainPass; crossplatform::EffectPass* exposureGammaMSAAPass; diff --git a/CrossPlatform/Material.cpp b/CrossPlatform/Material.cpp index 73fc385e..b519a420 100644 --- a/CrossPlatform/Material.cpp +++ b/CrossPlatform/Material.cpp @@ -25,11 +25,11 @@ void Material::InvalidateDeviceObjects() effect= nullptr; } -void Material::SetEffect(crossplatform::Effect *e) +void Material::SetEffect(const std::shared_ptr &e) { effect=e; } -crossplatform::Effect *Material::GetEffect() +std::shared_ptr Material::GetEffect() { return effect; } diff --git a/CrossPlatform/Material.h b/CrossPlatform/Material.h index e28b0e72..6907d611 100644 --- a/CrossPlatform/Material.h +++ b/CrossPlatform/Material.h @@ -44,8 +44,8 @@ namespace platform Material(); Material(const char *name); virtual ~Material(); - void SetEffect(crossplatform::Effect *e); - crossplatform::Effect *GetEffect(); + void SetEffect(const std::shared_ptr& e); + std::shared_ptr GetEffect(); void InvalidateDeviceObjects(); void Apply(crossplatform::DeviceContext &,PhysicalLightRenderData &); template struct Channel @@ -65,7 +65,7 @@ namespace platform Channel ambientOcclusion; Channel transparencyAlpha; protected: - crossplatform::Effect *effect=nullptr; + std::shared_ptreffect=nullptr; std::string name; }; } diff --git a/CrossPlatform/MeshRenderer.cpp b/CrossPlatform/MeshRenderer.cpp index 670fd889..8e98ac57 100644 --- a/CrossPlatform/MeshRenderer.cpp +++ b/CrossPlatform/MeshRenderer.cpp @@ -22,7 +22,7 @@ void MeshRenderer::RestoreDeviceObjects(RenderPlatform *r) cameraConstants.RestoreDeviceObjects(r); solidConstants.RestoreDeviceObjects(r); perObjectConstants.RestoreDeviceObjects(r); - effect = r->GetEffect("solid"); + effect = r->GetOrCreateEffect("solid"); } void MeshRenderer::InvalidateDeviceObjects() diff --git a/CrossPlatform/MeshRenderer.h b/CrossPlatform/MeshRenderer.h index ae01c179..3923370a 100644 --- a/CrossPlatform/MeshRenderer.h +++ b/CrossPlatform/MeshRenderer.h @@ -32,7 +32,7 @@ namespace platform void DrawSubMesh(GraphicsDeviceContext& deviceContext, Mesh* mesh, int); void DrawSubNode(GraphicsDeviceContext& deviceContext, Mesh* mesh, const Mesh::SubNode& subNode); RenderPlatform *renderPlatform; - Effect *effect; + std::shared_ptr effect; public: ConstantBuffer cameraConstants; diff --git a/CrossPlatform/RenderPlatform.cpp b/CrossPlatform/RenderPlatform.cpp index ac177223..a2057660 100644 --- a/CrossPlatform/RenderPlatform.cpp +++ b/CrossPlatform/RenderPlatform.cpp @@ -108,13 +108,6 @@ RenderPlatform::~RenderPlatform() allocator.Shutdown(); InvalidateDeviceObjects(); delete gpuProfiler; - - for (auto i = materials.begin(); i != materials.end(); i++) - { - Material *mat = i->second; - delete mat; - } - materials.clear(); } static bool RewriteOutput(std::string str) @@ -184,7 +177,7 @@ void RenderPlatform::recompileAsync() { if(RecompileEffect(effectRecompile.effect_name)) { - effectRecompile.newEffect = CreateEffect(effectRecompile.effect_name.c_str(), false); + GetOrCreateEffect(effectRecompile.effect_name.c_str(), true); } } return effectRecompile; @@ -465,17 +458,10 @@ void RenderPlatform::RestoreDeviceObjects(void*) mat->SetEffect(solidEffect); } - SAFE_DELETE(debugEffect); - debugEffect=CreateEffect("debug"); - - SAFE_DELETE(solidEffect); - solidEffect=CreateEffect("solid"); - - SAFE_DELETE(copyEffect); - copyEffect=CreateEffect("copy"); - - SAFE_DELETE(mipEffect); - mipEffect=CreateEffect("mip"); + debugEffect=GetOrCreateEffect("debug"); + solidEffect=GetOrCreateEffect("solid"); + copyEffect=GetOrCreateEffect("copy"); + mipEffect=GetOrCreateEffect("mip"); if(debugEffect) { @@ -500,12 +486,11 @@ void RenderPlatform::InvalidateDeviceObjects() standardRenderStates.clear(); SAFE_DELETE(textRenderer); debugConstants.InvalidateDeviceObjects(); - SAFE_DELETE(debugEffect); - - SAFE_DELETE(solidEffect); - - SAFE_DELETE(copyEffect); - SAFE_DELETE(mipEffect); + + debugEffect = nullptr; + solidEffect = nullptr; + copyEffect = nullptr; + mipEffect = nullptr; textured=nullptr; untextured=nullptr; @@ -516,18 +501,14 @@ void RenderPlatform::InvalidateDeviceObjects() { Material *mat = i->second; mat->InvalidateDeviceObjects(); + delete mat; } + materials.clear(); for (auto &debugVertexBuffer : debugVertexBuffers) { debugVertexBuffer->InvalidateDeviceObjects(); } debugVertexBuffers.clear(); - /*for(auto s:shaders) - { - s.second->Release(); - delete s.second; - } - shaders.clear();*/ for(auto s:sharedSamplerStates) { s.second->InvalidateDeviceObjects(); @@ -539,6 +520,11 @@ void RenderPlatform::InvalidateDeviceObjects() SAFE_DELETE(t.second); } textures.clear(); + for (auto &effect : effects) + { + effect.second = nullptr; + } + effects.clear(); last_begin_frame_number=0; } @@ -555,17 +541,10 @@ void RenderPlatform::RecompileShaders() void RenderPlatform::LoadShaders() { - SAFE_DELETE(debugEffect); - debugEffect=CreateEffect("debug"); - - SAFE_DELETE(solidEffect); - solidEffect=CreateEffect("solid"); - - SAFE_DELETE(copyEffect); - copyEffect=CreateEffect("copy"); - - SAFE_DELETE(mipEffect); - mipEffect=CreateEffect("mip"); + debugEffect=GetOrCreateEffect("debug"); + solidEffect=GetOrCreateEffect("solid"); + copyEffect=GetOrCreateEffect("copy"); + mipEffect=GetOrCreateEffect("mip"); if(debugEffect) { @@ -966,12 +945,12 @@ void RenderPlatform::SetMemoryInterface(platform::core::MemoryInterface *m) allocator.SetExternalAllocator(m); } -crossplatform::Effect *RenderPlatform::GetDebugEffect() +std::shared_ptr RenderPlatform::GetDebugEffect() { return debugEffect; } -crossplatform::Effect* RenderPlatform::GetCopyEffect() +std::shared_ptr RenderPlatform::GetCopyEffect() { return copyEffect; } @@ -1738,7 +1717,7 @@ int2 RenderPlatform::DrawTexture(GraphicsDeviceContext &deviceContext, int x1, i tech=untextured; } - DrawQuad(deviceContext,x1,y1,dx,dy,debugEffect,tech,blend?"blend":"noblend"); + DrawQuad(deviceContext,x1,y1,dx,dy,debugEffect.get(),tech,blend?"blend":"noblend"); debugEffect->UnbindTextures(deviceContext); if(debug) @@ -2002,35 +1981,39 @@ SamplerState *RenderPlatform::GetOrCreateSamplerStateByName (const char *name_ut return ss; } -Effect *RenderPlatform::CreateEffect(const char *filename_utf8, bool checkRecompileShaders) +std::shared_ptr RenderPlatform::GetOrCreateEffect(const char *filename_utf8, bool createOnly) { std::string fn(filename_utf8); - //Check if the effect in being recompiled - if (checkRecompileShaders) + if (!createOnly) { + //Check if the effect in being recompiled std::lock_guard recompileEffectFutureGuard(recompileEffectFutureMutex); const auto &it = effectsToCompileFutures.find(fn); if (it != effectsToCompileFutures.end()) { if (it->second.valid()) { - Effect *e = it->second.get().newEffect; effectsToCompileFutures.erase(it); - effects[fn] = e; - return e; + return effects[fn]; } else { effectsToCompileFutures.erase(it); } } + + //Else, check if the effect is already loaded. + auto i = effects.find(filename_utf8); + if (i != effects.end()) + return i->second; } //Else, load as normal - crossplatform::Effect *e=CreateEffect(); - effects[fn] = e; + std::shared_ptr e; + e.reset(CreateEffect()); e->SetName(filename_utf8); + effects[fn] = e; bool success = e->Load(this,filename_utf8); if (!success) { @@ -2040,34 +2023,6 @@ Effect *RenderPlatform::CreateEffect(const char *filename_utf8, bool checkRecomp return e; } -Effect* RenderPlatform::GetEffect(const char* filename_utf8) -{ - std::string fn(filename_utf8); - - // Check if the effect in being recompiled - std::lock_guard recompileEffectFutureGuard(recompileEffectFutureMutex); - const auto &it = effectsToCompileFutures.find(fn); - if (it != effectsToCompileFutures.end()) - { - if (it->second.valid()) - { - Effect *e = it->second.get().newEffect; - effectsToCompileFutures.erase(it); - effects[fn] = e; - return e; - } - else - { - effectsToCompileFutures.erase(it); - } - } - - auto i = effects.find(filename_utf8); - if (i == effects.end()) - return nullptr; - return i->second; -} - crossplatform::Layout *RenderPlatform::CreateLayout(int num_elements,const LayoutDesc *layoutDesc,bool interleaved) { auto l= new Layout(); diff --git a/CrossPlatform/RenderPlatform.h b/CrossPlatform/RenderPlatform.h index fdb18bda..ffe71b41 100644 --- a/CrossPlatform/RenderPlatform.h +++ b/CrossPlatform/RenderPlatform.h @@ -365,10 +365,8 @@ namespace platform SamplerState *GetOrCreateSamplerStateByName (const char *name_utf8,platform::crossplatform::SamplerStateDesc *desc=0); /// Create a platform-specific effect instance. virtual Effect *CreateEffect ()=0; - /// Create a platform-specific effect instance. - virtual Effect *CreateEffect (const char *filename_utf8, bool checkRecompileShaders = true); - /// Get the effect named, or return null if it's not been created. - Effect *GetEffect (const char *name_utf8); + /// Get or create a platform-specific effect instance. + std::shared_ptr GetOrCreateEffect(const char *filename_utf8, bool createOnly = false); /// Create a platform-specific constant buffer instance. This is not usually used directly, instead, create a /// platform::crossplatform::ConstantBuffer, and pass this RenderPlatform's pointer to it in RestoreDeviceObjects(). virtual PlatformConstantBuffer *CreatePlatformConstantBuffer (ResourceUsageFrequency F) =0; @@ -504,15 +502,15 @@ namespace platform } //! This was introduced because Unity's deferred renderer flips the image vertically sometime after we render. bool mirrorY, mirrorY2, mirrorYText; - crossplatform::Effect *solidEffect = nullptr; - crossplatform::Effect *copyEffect = nullptr; + std::shared_ptr solidEffect = nullptr; + std::shared_ptr copyEffect = nullptr; std::map materials; std::map textures; std::vector GetTexturePathsUtf8(); platform::core::MemoryInterface *GetMemoryInterface(); void SetMemoryInterface(platform::core::MemoryInterface *m); - crossplatform::Effect *GetDebugEffect(); - crossplatform::Effect *GetCopyEffect(); + std::shared_ptr GetDebugEffect(); + std::shared_ptr GetCopyEffect(); ConstantBuffer &GetDebugConstantBuffer(); // Does the format use stencil? static PixelFormat ToColourFormat(PixelFormat f); @@ -533,7 +531,6 @@ namespace platform { std::string effect_name; std::function callback; - Effect* newEffect = nullptr; }; std::thread effectCompileThread; std::vector effectsToCompile; @@ -571,8 +568,8 @@ namespace platform GraphicsDeviceContext immediateContext; ComputeDeviceContext computeContext; // All for debug Effect - crossplatform::Effect *debugEffect=nullptr; - crossplatform::Effect *mipEffect=nullptr; + std::shared_ptr debugEffect=nullptr; + std::shared_ptr mipEffect=nullptr; crossplatform::EffectTechnique *textured=nullptr; crossplatform::EffectTechnique *untextured=nullptr; crossplatform::EffectTechnique *showVolume=nullptr; @@ -595,7 +592,7 @@ namespace platform std::set fencedTextures; virtual void ResetImmediateCommandList() {} public: - std::map effects; + std::map> effects; // all shaders are stored here and referenced by techniques. //std::map shaders; phmap::flat_hash_map contextState; diff --git a/CrossPlatform/SphereRenderer.cpp b/CrossPlatform/SphereRenderer.cpp index ff33aabb..fab71601 100644 --- a/CrossPlatform/SphereRenderer.cpp +++ b/CrossPlatform/SphereRenderer.cpp @@ -28,7 +28,6 @@ void SphereRenderer::InvalidateDeviceObjects() { sphereConstants.InvalidateDeviceObjects(); renderPlatform=nullptr; - delete effect; effect=nullptr; } @@ -45,8 +44,7 @@ void SphereRenderer::RecompileShaders() void SphereRenderer::LoadShaders() { reload_shaders = false; - delete effect; - effect=renderPlatform->CreateEffect("sphere"); + effect=renderPlatform->GetOrCreateEffect("sphere"); } void SphereRenderer::DrawCrossSection(GraphicsDeviceContext &deviceContext,crossplatform::Effect *effect, crossplatform::Texture *t, vec3 texcOffset, vec3 origin, vec4 orient_quat, float qsize, float sph_rad, vec4 colour, int pass) diff --git a/CrossPlatform/SphereRenderer.h b/CrossPlatform/SphereRenderer.h index cf4b1190..56715018 100644 --- a/CrossPlatform/SphereRenderer.h +++ b/CrossPlatform/SphereRenderer.h @@ -41,7 +41,7 @@ namespace platform protected: crossplatform::ConstantBuffer sphereConstants; - crossplatform::Effect *effect = nullptr; + std::shared_ptr effect = nullptr; bool reload_shaders = true; }; } diff --git a/CrossPlatform/SphericalHarmonics.cpp b/CrossPlatform/SphericalHarmonics.cpp index 29972da7..89845c7c 100644 --- a/CrossPlatform/SphericalHarmonics.cpp +++ b/CrossPlatform/SphericalHarmonics.cpp @@ -47,17 +47,17 @@ void SphericalHarmonics::RecompileShaders() void SphericalHarmonics::NotifyEffectsRecompiled() { - SAFE_DELETE(sphericalHarmonicsEffect); - SAFE_DELETE(lightProbesEffect); + sphericalHarmonicsEffect=nullptr; + lightProbesEffect=nullptr; } void SphericalHarmonics::LoadShaders() { if (!renderPlatform) return; - SAFE_DELETE(sphericalHarmonicsEffect); - SAFE_DELETE(lightProbesEffect); - sphericalHarmonicsEffect =renderPlatform->CreateEffect("spherical_harmonics"); + sphericalHarmonicsEffect=nullptr; + lightProbesEffect=nullptr; + sphericalHarmonicsEffect =renderPlatform->GetOrCreateEffect("spherical_harmonics"); jitter =sphericalHarmonicsEffect->GetTechniqueByName("jitter"); encode =sphericalHarmonicsEffect->GetTechniqueByName("encode"); _samplesBuffer =sphericalHarmonicsEffect->GetShaderResource("samplesBuffer"); @@ -65,7 +65,7 @@ void SphericalHarmonics::LoadShaders() _samplesBufferRW =sphericalHarmonicsEffect->GetShaderResource("samplesBufferRW"); _cubemapTexture =sphericalHarmonicsEffect->GetShaderResource("cubemapTexture"); - lightProbesEffect =renderPlatform->CreateEffect("light_probes"); + lightProbesEffect =renderPlatform->GetOrCreateEffect("light_probes"); auto group =lightProbesEffect->GetTechniqueGroupByName("mip_from_roughness"); mip_from_roughness_blend =group->GetTechniqueByName("mip_from_roughness_blend"); mip_from_roughness_no_blend =group->GetTechniqueByName("mip_from_roughness_no_blend"); @@ -77,8 +77,8 @@ void SphericalHarmonics::InvalidateDeviceObjects() ResetBuffers(); lightProbeConstants.InvalidateDeviceObjects(); sphericalHarmonicsConstants.InvalidateDeviceObjects(); - SAFE_DELETE(sphericalHarmonicsEffect); - SAFE_DELETE(lightProbesEffect); + sphericalHarmonicsEffect = nullptr; + lightProbesEffect = nullptr; } void SphericalHarmonics::ResetBuffers() diff --git a/CrossPlatform/SphericalHarmonics.h b/CrossPlatform/SphericalHarmonics.h index f90987ef..567ef4de 100644 --- a/CrossPlatform/SphericalHarmonics.h +++ b/CrossPlatform/SphericalHarmonics.h @@ -69,9 +69,9 @@ namespace platform StructuredBuffer sphericalSamples; ConstantBuffer sphericalHarmonicsConstants; StructuredBuffer sphericalHarmonics; - Effect *sphericalHarmonicsEffect; + std::shared_ptr sphericalHarmonicsEffect; int shSeed; - crossplatform::Effect *lightProbesEffect; + std::shared_ptr lightProbesEffect; crossplatform::EffectTechnique *mip_from_roughness_blend; crossplatform::EffectTechnique *mip_from_roughness_no_blend; crossplatform::EffectTechnique *jitter; diff --git a/CrossPlatform/Text3DRenderer.cpp b/CrossPlatform/Text3DRenderer.cpp index 64a6b114..f5b001d6 100644 --- a/CrossPlatform/Text3DRenderer.cpp +++ b/CrossPlatform/Text3DRenderer.cpp @@ -223,7 +223,7 @@ void Text3DRenderer::InvalidateDeviceObjects() } fontChars.clear(); constantBuffer.InvalidateDeviceObjects(); - SAFE_DELETE(effect); + effect = nullptr; SAFE_DELETE(font_texture); renderPlatform=NULL; if (fontWidth) @@ -242,9 +242,8 @@ void Text3DRenderer::RecompileShaders() void Text3DRenderer::LoadShaders() { recompile = false; - SAFE_DELETE(effect); ERRNO_CHECK - effect = renderPlatform->CreateEffect("font"); + effect = renderPlatform->GetOrCreateEffect("font"); ERRNO_CHECK backgTech =effect->GetTechniqueByName("backg"); textTech =effect->GetTechniqueByName("text"); diff --git a/CrossPlatform/Text3DRenderer.h b/CrossPlatform/Text3DRenderer.h index 72e41965..027611e7 100644 --- a/CrossPlatform/Text3DRenderer.h +++ b/CrossPlatform/Text3DRenderer.h @@ -43,7 +43,7 @@ namespace platform }; private: std::vector fontPaths; - crossplatform::Effect *effect=nullptr; + std::shared_ptr effect=nullptr; crossplatform::EffectTechnique *backgTech=nullptr; crossplatform::EffectTechnique *textTech=nullptr; crossplatform::ShaderResource textureResource; diff --git a/CrossPlatform/TextRenderer.cpp b/CrossPlatform/TextRenderer.cpp index 95abee63..4636ba81 100644 --- a/CrossPlatform/TextRenderer.cpp +++ b/CrossPlatform/TextRenderer.cpp @@ -164,7 +164,7 @@ void TextRenderer::InvalidateDeviceObjects() } fontChars.clear(); constantBuffer.InvalidateDeviceObjects(); - SAFE_DELETE(effect); + effect=nullptr; SAFE_DELETE(font_texture); renderPlatform=NULL; if (fontWidth) @@ -175,10 +175,10 @@ void TextRenderer::InvalidateDeviceObjects() void TextRenderer::LoadShaders() { - SAFE_DELETE(effect); + effect = nullptr; if (!renderPlatform) return; - effect=renderPlatform->CreateEffect("font"); + effect=renderPlatform->GetOrCreateEffect("font"); backgTech =effect->GetTechniqueByName("backg"); textTech =effect->GetTechniqueByName("text"); textureResource =effect->GetShaderResource("fontTexture"); @@ -189,8 +189,7 @@ void TextRenderer::LoadShaders() void TextRenderer::RecompileShaders() { if(renderPlatform) - renderPlatform->ScheduleRecompileEffects({"font"}, [this] - { recompiled=true; }); + renderPlatform->ScheduleRecompileEffects({"font"}, [this]{ recompiled=true; }); } int TextRenderer::GetDefaultTextHeight() const diff --git a/CrossPlatform/TextRenderer.h b/CrossPlatform/TextRenderer.h index 2c61bb71..fdf108dd 100644 --- a/CrossPlatform/TextRenderer.h +++ b/CrossPlatform/TextRenderer.h @@ -42,7 +42,7 @@ namespace platform private: void NotifyEffectRecompiled(Effect *e); void Recompile(); - crossplatform::Effect *effect; + std::shared_ptr effect; crossplatform::EffectTechnique *backgTech; crossplatform::EffectTechnique *textTech; crossplatform::ShaderResource textureResource; diff --git a/DirectX11/RenderPlatform.cpp b/DirectX11/RenderPlatform.cpp index d2544365..ed3f047f 100644 --- a/DirectX11/RenderPlatform.cpp +++ b/DirectX11/RenderPlatform.cpp @@ -228,7 +228,6 @@ void RenderPlatform::InvalidateDeviceObjects() SAFE_RELEASE(pUserDefinedAnnotation); #endif crossplatform::RenderPlatform::InvalidateDeviceObjects(); - SAFE_DELETE(debugEffect); ID3D11DeviceContext* c=immediateContext.asD3D11DeviceContext(); SAFE_RELEASE(c); immediateContext.platform_context=NULL; diff --git a/ImGui/imgui_impl_platform.cpp b/ImGui/imgui_impl_platform.cpp index 20cd7976..a46a43e3 100644 --- a/ImGui/imgui_impl_platform.cpp +++ b/ImGui/imgui_impl_platform.cpp @@ -43,7 +43,7 @@ struct ImGui_ImplPlatform_Data Buffer* pVB = nullptr; Buffer* pIB = nullptr; bool reload_shaders=false; - Effect* effect = nullptr; + std::shared_ptr effect = nullptr; EffectPass* effectPass_testDepth=nullptr; EffectPass* effectPass_noDepth=nullptr; EffectPass* effectPass_placeIn3D_testDepth=nullptr; @@ -499,7 +499,7 @@ bool ImGui_ImplPlatform_CreateDeviceObjects() bd->pInputLayout=bd->renderPlatform->CreateLayout(3,local_layout,true); if (!bd->effect) - bd->effect = bd->renderPlatform->CreateEffect("imgui"); + bd->effect = bd->renderPlatform->GetOrCreateEffect("imgui"); bd->effectPass_testDepth = bd->effect->GetTechniqueByName("layout_in_2d")->GetPass("test_depth"); bd->effectPass_noDepth = bd->effect->GetTechniqueByName("layout_in_2d")->GetPass("no_depth"); @@ -536,7 +536,7 @@ void ImGui_ImplPlatform_InvalidateDeviceObjects() bd->constantBuffer.InvalidateDeviceObjects(); SAFE_DELETE(bd->pInputLayout); - SAFE_DELETE(bd->effect); + bd->effect = nullptr; SAFE_DELETE(bd->framebufferTexture); } void ImGui_ImplPlatform_RecompileShaders() @@ -549,17 +549,17 @@ void ImGui_ImplPlatform_RecompileShaders() bd->renderPlatform->ScheduleRecompileEffects({"imgui"},[bd](){bd->reload_shaders=true;}); } -void ImGui_ImplPlatform_LoadShaders() +void ImGui_ImplPlatform_LoadShaders() { ImGui_ImplPlatform_Data* bd = ImGui_ImplPlatform_GetBackendData(); if (!bd) return; - delete bd->effect; + bd->effect =nullptr; if(!bd->renderPlatform) return; - bd->effect = bd->renderPlatform->CreateEffect("imgui"); + bd->effect = bd->renderPlatform->GetOrCreateEffect("imgui"); bd->effectPass_testDepth = bd->effect->GetTechniqueByName("layout_in_2d")->GetPass("test_depth"); bd->effectPass_noDepth = bd->effect->GetTechniqueByName("layout_in_2d")->GetPass("no_depth"); bd->effectPass_placeIn3D_testDepth = bd->effect->GetTechniqueByName("place_in_3d")->GetPass("test_depth"); From 06a7fbdfcc575ba2697fcbce6408de6a3fdf2e37 Mon Sep 17 00:00:00 2001 From: Andrew Richards Date: Thu, 7 Nov 2024 11:14:05 +0000 Subject: [PATCH 6/6] Updated Sfx's FileLoader::GetFileDate() to use unix timestamps. --- Applications/Sfx/FileLoader.cpp | 20 ++------------ Applications/Sfx/FileLoader.h | 2 +- Applications/Sfx/Preprocessor.h | 4 +-- Applications/Sfx/Preprocessor.lpp | 4 +-- Applications/Sfx/Preprocessor.ypp | 2 +- Applications/Sfx/Sfx.cpp | 45 +++++-------------------------- 6 files changed, 15 insertions(+), 62 deletions(-) diff --git a/Applications/Sfx/FileLoader.cpp b/Applications/Sfx/FileLoader.cpp index 9154f65b..cf47fec4 100644 --- a/Applications/Sfx/FileLoader.cpp +++ b/Applications/Sfx/FileLoader.cpp @@ -196,17 +196,8 @@ void FileLoader::AcquireFileContents(void*& pointer, unsigned int& bytes, const fclose(fp); } -static double GetDayNumberFromDateTime(int year,int month,int day,int hour,int min,int sec) -{ - int D = 367*year - (7*(year + ((month+9)/12)))/4 + (275*month)/9 + day - 730531;//was +2451545 - double d=(double)D; - d+=(double)hour/24.0; - d+=(double)min/24.0/60.0; - d+=(double)sec/24.0/3600.0; - return d; -} -double FileLoader::GetFileDate(const char* filename_utf8) +uint64_t FileLoader::GetFileDate(const char *filename_utf8) { std::wstring filenamew=StringToWString(filename_utf8); #ifdef _MSC_VER @@ -218,14 +209,7 @@ double FileLoader::GetFileDate(const char* filename_utf8) #endif buf.st_mtime; time_t t = buf.st_mtime; - struct tm lt; - #ifdef _MSC_VER - gmtime_s(<,&t); - #else - gmtime_r(&t,<); - #endif - double datetime=GetDayNumberFromDateTime(1900+lt.tm_year,lt.tm_mon,lt.tm_mday,lt.tm_hour,lt.tm_min,lt.tm_sec); - return datetime; + return static_cast(t); } void FileLoader::ReleaseFileContents(void* pointer) diff --git a/Applications/Sfx/FileLoader.h b/Applications/Sfx/FileLoader.h index bab958db..e22de120 100644 --- a/Applications/Sfx/FileLoader.h +++ b/Applications/Sfx/FileLoader.h @@ -16,7 +16,7 @@ class FileLoader bool FileExists(const char *filename_utf8) const; const char *FindFileInPathStack(const char *filename_utf8,const std::vector &path_stack_utf8) const; void AcquireFileContents(void*& pointer, unsigned int& bytes, const char* filename_utf8,bool open_as_text); - double GetFileDate(const char* filename_utf8); + static uint64_t GetFileDate(const char* filename_utf8); void ReleaseFileContents(void* pointer); void Save(void* pointer, unsigned int bytes, const char* filename_utf8,bool save_as_text); }; diff --git a/Applications/Sfx/Preprocessor.h b/Applications/Sfx/Preprocessor.h index d1f31ca8..cd3ad161 100644 --- a/Applications/Sfx/Preprocessor.h +++ b/Applications/Sfx/Preprocessor.h @@ -28,14 +28,14 @@ struct MacroDefinition }; // prevent echoing of unprocessed chars: #define ECHO -extern double latest_datetime; +extern uint64_t latest_datetime; extern std::string latest_file; #undef PREPRO_STYPE #define PREPRO_STYPE PreprocessorType #undef PREPRO_DEBUG #define PREPRO_DEBUG 1 // These are the callback functions for file handling that we will send to the preprocessor. -extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,double &datetime); +extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,uint64_t &datetime); extern void (*prepro_close)(FILE *f); extern void Unput(int c); extern std::ostringstream preproOutput; diff --git a/Applications/Sfx/Preprocessor.lpp b/Applications/Sfx/Preprocessor.lpp index e9771570..03930af7 100644 --- a/Applications/Sfx/Preprocessor.lpp +++ b/Applications/Sfx/Preprocessor.lpp @@ -49,7 +49,7 @@ extern void prepro_warning(const char *s); extern void prepro_warning(const char *s,const char *file,int line); extern int prepro_get_lineno (); - FILE* (*prepro_open)(const char *filename_utf8,string &fullPathName,double &filedate)=NULL; + FILE* (*prepro_open)(const char *filename_utf8,string &fullPathName,uint64_t &filedate)=NULL; void (*prepro_close)(FILE *f)=NULL; std::ostringstream preproOutput; @@ -621,7 +621,7 @@ void Preprocess(int argc, char **argv) int newfile(const char *fn) { std::string fullPathName; - double datetime=0.0; + uint64_t datetime=0; FILE *f = prepro_open(fn,fullPathName,datetime); /* die if no file or no room */ diff --git a/Applications/Sfx/Preprocessor.ypp b/Applications/Sfx/Preprocessor.ypp index 3d04bdeb..e0ad5cb5 100644 --- a/Applications/Sfx/Preprocessor.ypp +++ b/Applications/Sfx/Preprocessor.ypp @@ -37,7 +37,7 @@ vector activeBlockStack; vector blockTestStack; extern int preproBracket; - double latest_datetime = 0.0; + uint64_t latest_datetime = 0; std::string latest_file; void ClearParserState() { diff --git a/Applications/Sfx/Sfx.cpp b/Applications/Sfx/Sfx.cpp index 832ca952..f80c50b1 100644 --- a/Applications/Sfx/Sfx.cpp +++ b/Applications/Sfx/Sfx.cpp @@ -79,45 +79,14 @@ typedef int errno_t; using namespace std; // These are the callback functions for file handling that we will send to the preprocessor. -//extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,double &time); +//extern FILE* (*prepro_open)(const char *filename_utf8,std::string &fullPathName,uint64_t &time); //extern void (*prepro_close)(FILE *f); + vector shaderPathsUtf8; FileLoader fileLoader; extern std::ostringstream preproOutput; -static double GetDayNumberFromDateTime(int year,int month,int day,int hour,int min,int sec) -{ - int D = 367*year - (7*(year + ((month+9)/12)))/4 + (275*month)/9 + day - 730531;//was +2451545 - double d=(double)D; - d+=(double)hour/24.0; - d+=(double)min/24.0/60.0; - d+=(double)sec/24.0/3600.0; - return d; -} - -double GetFileDate(const std::string &fullPathNameUtf8) -{ - wstring filenamew=StringToWString(fullPathNameUtf8); - #ifdef _MSC_VER - struct _stat64i32 buf; - _wstat(filenamew.c_str(), &buf); - #else - struct stat buf; - stat(fullPathNameUtf8.c_str(), &buf); - #endif - buf.st_mtime; - time_t t = buf.st_mtime; - struct tm lt; - #ifdef _MSC_VER - gmtime_s(<,&t); - #else - gmtime_r(&t,<); - #endif - double datetime=GetDayNumberFromDateTime(1900+lt.tm_year,lt.tm_mon,lt.tm_mday,lt.tm_hour,lt.tm_min,lt.tm_sec); - return datetime; -} - -FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,double &datetime) +FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,uint64_t &datetime) { fullPathNameUtf8 =fileLoader.FindFileInPathStack(filename_utf8,shaderPathsUtf8); if(!fullPathNameUtf8.length()) @@ -137,7 +106,7 @@ FILE* OpenFile(const char *filename_utf8,std::string &fullPathNameUtf8,double &d if(last_slash>0) path=path.substr(0,last_slash); shaderPathsUtf8.push_back(path); - datetime=GetFileDate(fullPathNameUtf8); + datetime=FileLoader::GetFileDate(fullPathNameUtf8.c_str()); return f; } @@ -626,13 +595,13 @@ bool sfxParseEffectFromFile(int effect, const char *file, const std::vectorplatformFilename); + uint64_t exe_datetime=FileLoader::GetFileDate(exeNameUtf8); + uint64_t platformfile_datetime=FileLoader::GetFileDate(config->platformFilename.c_str()); latest_datetime= std::max(exe_datetime,platformfile_datetime); latest_file=file; if (!preprocess(file, config->define, sfxOptions->disableLineWrites)) return false; - double output_filedatetime=GetFileDate(sfxoFilename); + uint64_t output_filedatetime = FileLoader::GetFileDate(sfxoFilename.c_str()); bool recompile=false; if(sfxOptions->force) {