@@ -29,6 +29,10 @@ using namespace video;
29
29
#include < chrono>
30
30
#define BENCHMARK_TILL_FIRST_FRAME
31
31
32
+ // Shader cache tests. Only define one of these, or none for no use of the Cache
33
+ // #define SHADER_CACHE_TEST_COMPILATION_CACHE_STORE
34
+ #define SHADER_CACHE_TEST_CACHE_RETRIEVE
35
+
32
36
static constexpr bool DebugMode = false ;
33
37
static constexpr bool DebugRotatingViewProj = false ;
34
38
static constexpr bool FragmentShaderPixelInterlock = true ;
@@ -650,30 +654,124 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
650
654
constexpr auto fragmentShaderPath = " ../fragment_shader.hlsl" ;
651
655
constexpr auto debugfragmentShaderPath = " ../fragment_shader_debug.hlsl" ;
652
656
constexpr auto resolveAlphasShaderPath = " ../resolve_alphas.hlsl" ;
657
+ #if defined(SHADER_CACHE_TEST_COMPILATION_CACHE_STORE)
653
658
659
+
660
+ auto cache = core::make_smart_refctd_ptr<IShaderCompiler::CCache>();
661
+
654
662
// Load Custom Shader
655
663
auto loadCompileAndCreateShader = [&](const std::string& relPath, IShader::E_SHADER_STAGE stage) -> smart_refctd_ptr<IGPUShader>
664
+ {
665
+ IAssetLoader::SAssetLoadParams lp = {};
666
+ lp.logger = m_logger.get ();
667
+ lp.workingDirectory = " " ; // virtual root
668
+ auto assetBundle = m_assetMgr->getAsset (relPath, lp);
669
+ const auto assets = assetBundle.getContents ();
670
+ if (assets.empty ())
671
+ return nullptr ;
672
+
673
+ // lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
674
+ auto cpuShader = IAsset::castDown<ICPUShader>(assets[0 ]);
675
+ cpuShader->setShaderStage (stage);
676
+ if (!cpuShader)
677
+ return nullptr ;
678
+
679
+ return m_device->createShader ({ cpuShader.get (), nullptr , cache.get () });
680
+ };
681
+ shaders[0 ] = loadCompileAndCreateShader (vertexShaderPath, IShader::ESS_VERTEX);
682
+ shaders[1 ] = loadCompileAndCreateShader (fragmentShaderPath, IShader::ESS_FRAGMENT);
683
+ shaders[2 ] = loadCompileAndCreateShader (debugfragmentShaderPath, IShader::ESS_FRAGMENT);
684
+ shaders[3 ] = loadCompileAndCreateShader (resolveAlphasShaderPath, IShader::ESS_FRAGMENT);
685
+
686
+ auto serializedCache = cache->serialize ();
687
+ auto savePath = localOutputCWD / " cache.bin" ;
688
+ core::smart_refctd_ptr<system::IFile> f;
656
689
{
657
- IAssetLoader::SAssetLoadParams lp = {};
658
- lp.logger = m_logger.get ();
659
- lp.workingDirectory = " " ; // virtual root
660
- auto assetBundle = m_assetMgr->getAsset (relPath,lp);
661
- const auto assets = assetBundle.getContents ();
662
- if (assets.empty ())
663
- return nullptr ;
664
-
665
- // lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
666
- auto cpuShader = IAsset::castDown<ICPUShader>(assets[0 ]);
667
- cpuShader->setShaderStage (stage);
668
- if (!cpuShader)
669
- return nullptr ;
670
-
671
- return m_device->createShader (cpuShader.get ());
672
- };
690
+ system::ISystem::future_t <core::smart_refctd_ptr<system::IFile>> future;
691
+ m_system->createFile (future, savePath.c_str (), system::IFile::ECF_WRITE);
692
+ if (!future.wait ())
693
+ return {};
694
+ future.acquire ().move_into (f);
695
+ }
696
+ if (!f)
697
+ return {};
698
+ system::IFile::success_t succ;
699
+ f->write (succ, serializedCache->getPointer (), 0 , serializedCache->getSize ());
700
+ const bool success = bool (succ);
701
+ assert (success);
702
+ #elif defined(SHADER_CACHE_TEST_CACHE_RETRIEVE)
703
+
704
+ auto savePath = localOutputCWD / " cache.bin" ;
705
+
706
+ core::smart_refctd_ptr<system::IFile> f;
707
+ {
708
+ system::ISystem::future_t <core::smart_refctd_ptr<system::IFile>> future;
709
+ m_system->createFile (future, savePath.c_str (), system::IFile::ECF_READ);
710
+ if (!future.wait ())
711
+ return {};
712
+ future.acquire ().move_into (f);
713
+ }
714
+ if (!f)
715
+ return {};
716
+ const size_t size = f->getSize ();
717
+
718
+ std::vector<uint8_t > contents (size);
719
+ system::IFile::success_t succ;
720
+ f->read (succ, contents.data (), 0 , size);
721
+ const bool success = bool (succ);
722
+ assert (success);
723
+
724
+ auto cache = IShaderCompiler::CCache::deserialize (contents);
725
+
726
+ // Load Custom Shader
727
+ auto loadCompileAndCreateShader = [&](const std::string& relPath, IShader::E_SHADER_STAGE stage) -> smart_refctd_ptr<IGPUShader>
728
+ {
729
+ IAssetLoader::SAssetLoadParams lp = {};
730
+ lp.logger = m_logger.get ();
731
+ lp.workingDirectory = " " ; // virtual root
732
+ auto assetBundle = m_assetMgr->getAsset (relPath, lp);
733
+ const auto assets = assetBundle.getContents ();
734
+ if (assets.empty ())
735
+ return nullptr ;
736
+
737
+ // lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
738
+ auto cpuShader = IAsset::castDown<ICPUShader>(assets[0 ]);
739
+ cpuShader->setShaderStage (stage);
740
+ if (!cpuShader)
741
+ return nullptr ;
742
+
743
+ return m_device->createShader ({ cpuShader.get (), nullptr , cache.get () });
744
+ };
745
+ shaders[0 ] = loadCompileAndCreateShader (vertexShaderPath, IShader::ESS_VERTEX);
746
+ shaders[1 ] = loadCompileAndCreateShader (fragmentShaderPath, IShader::ESS_FRAGMENT);
747
+ shaders[2 ] = loadCompileAndCreateShader (debugfragmentShaderPath, IShader::ESS_FRAGMENT);
748
+ shaders[3 ] = loadCompileAndCreateShader (resolveAlphasShaderPath, IShader::ESS_FRAGMENT);
749
+ #else
750
+
751
+ // Load Custom Shader
752
+ auto loadCompileAndCreateShader = [&](const std::string& relPath, IShader::E_SHADER_STAGE stage) -> smart_refctd_ptr<IGPUShader>
753
+ {
754
+ IAssetLoader::SAssetLoadParams lp = {};
755
+ lp.logger = m_logger.get ();
756
+ lp.workingDirectory = " " ; // virtual root
757
+ auto assetBundle = m_assetMgr->getAsset (relPath, lp);
758
+ const auto assets = assetBundle.getContents ();
759
+ if (assets.empty ())
760
+ return nullptr ;
761
+
762
+ // lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
763
+ auto cpuShader = IAsset::castDown<ICPUShader>(assets[0 ]);
764
+ cpuShader->setShaderStage (stage);
765
+ if (!cpuShader)
766
+ return nullptr ;
767
+
768
+ return m_device->createShader (cpuShader.get ());
769
+ };
673
770
shaders[0 ] = loadCompileAndCreateShader (vertexShaderPath, IShader::ESS_VERTEX);
674
771
shaders[1 ] = loadCompileAndCreateShader (fragmentShaderPath, IShader::ESS_FRAGMENT);
675
772
shaders[2 ] = loadCompileAndCreateShader (debugfragmentShaderPath, IShader::ESS_FRAGMENT);
676
773
shaders[3 ] = loadCompileAndCreateShader (resolveAlphasShaderPath, IShader::ESS_FRAGMENT);
774
+ #endif
677
775
}
678
776
679
777
// Shared Blend Params between pipelines
0 commit comments