Skip to content

Commit 1ddf2b5

Browse files
committed
Adding tests for caching on 62_CAD
1 parent dac9956 commit 1ddf2b5

File tree

1 file changed

+114
-16
lines changed

1 file changed

+114
-16
lines changed

62_CAD/main.cpp

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ using namespace video;
2929
#include <chrono>
3030
#define BENCHMARK_TILL_FIRST_FRAME
3131

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+
3236
static constexpr bool DebugMode = false;
3337
static constexpr bool DebugRotatingViewProj = false;
3438
static constexpr bool FragmentShaderPixelInterlock = true;
@@ -650,30 +654,124 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
650654
constexpr auto fragmentShaderPath = "../fragment_shader.hlsl";
651655
constexpr auto debugfragmentShaderPath = "../fragment_shader_debug.hlsl";
652656
constexpr auto resolveAlphasShaderPath = "../resolve_alphas.hlsl";
657+
#if defined(SHADER_CACHE_TEST_COMPILATION_CACHE_STORE)
653658

659+
660+
auto cache = core::make_smart_refctd_ptr<IShaderCompiler::CCache>();
661+
654662
// Load Custom Shader
655663
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;
656689
{
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+
};
673770
shaders[0] = loadCompileAndCreateShader(vertexShaderPath, IShader::ESS_VERTEX);
674771
shaders[1] = loadCompileAndCreateShader(fragmentShaderPath, IShader::ESS_FRAGMENT);
675772
shaders[2] = loadCompileAndCreateShader(debugfragmentShaderPath, IShader::ESS_FRAGMENT);
676773
shaders[3] = loadCompileAndCreateShader(resolveAlphasShaderPath, IShader::ESS_FRAGMENT);
774+
#endif
677775
}
678776

679777
// Shared Blend Params between pipelines

0 commit comments

Comments
 (0)