| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Resource/ShaderProgramResource.h>
- #include <AnKi/Resource/ResourceManager.h>
- #include <AnKi/Resource/ShaderProgramResourceSystem.h>
- #include <AnKi/Gr/ShaderProgram.h>
- #include <AnKi/Gr/GrManager.h>
- #include <AnKi/Util/Filesystem.h>
- #include <AnKi/Util/Functions.h>
- #include <AnKi/Util/CVarSet.h>
- namespace anki {
- ShaderProgramResourceVariant::ShaderProgramResourceVariant()
- {
- }
- ShaderProgramResourceVariant::~ShaderProgramResourceVariant()
- {
- }
- ShaderProgramResource::ShaderProgramResource()
- {
- }
- ShaderProgramResource::~ShaderProgramResource()
- {
- for(auto it : m_variants)
- {
- ShaderProgramResourceVariant* variant = &(*it);
- deleteInstance(ResourceMemoryPool::getSingleton(), variant);
- }
- ResourceMemoryPool::getSingleton().free(m_binary);
- }
- Error ShaderProgramResource::load(const ResourceFilename& filename, [[maybe_unused]] Bool async)
- {
- // Load the binary
- ResourceFilePtr file;
- ANKI_CHECK(openFile(filename, file));
- ANKI_CHECK(deserializeShaderBinaryFromAnyFile(*file, m_binary, ResourceMemoryPool::getSingleton()));
- return Error::kNone;
- }
- void ShaderProgramResource::getOrCreateVariant(const ShaderProgramResourceVariantInitInfo& info_, const ShaderProgramResourceVariant*& variant) const
- {
- ShaderProgramResourceVariantInitInfo info = info_;
- // Sanity checks
- ANKI_ASSERT(info.m_setMutators.getSetBitCount() == m_binary->m_mutators.getSize());
- // User didn't provided Try to guess some defaults
- if(!info.m_shaderTypes)
- {
- U32 techniqueIdx = kMaxU32;
- for(U32 i = 0; i < m_binary->m_techniques.getSize(); ++i)
- {
- if(CString("Unnamed") == m_binary->m_techniques[i].m_name.getBegin())
- {
- techniqueIdx = i;
- break;
- }
- }
- ANKI_ASSERT(techniqueIdx != kMaxU32);
- const ShaderTypeBit techniqueShaderTypes = m_binary->m_techniques[techniqueIdx].m_shaderTypes;
- if(techniqueShaderTypes == (ShaderTypeBit::kPixel | ShaderTypeBit::kVertex))
- {
- info.requestTechniqueAndTypes(techniqueShaderTypes, "Unnamed");
- }
- else if(techniqueShaderTypes == ShaderTypeBit::kCompute)
- {
- info.requestTechniqueAndTypes(techniqueShaderTypes, "Unnamed");
- }
- else
- {
- ANKI_ASSERT(!"Can't figure out a sensible default");
- }
- }
- // Compute variant hash
- U64 hash = computeHash(&info.m_shaderTypes, sizeof(info.m_shaderTypes));
- for(ShaderType stype : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
- {
- const PtrSize len = strlen(info.m_techniqueNames[stype].getBegin());
- ANKI_ASSERT(len > 0);
- hash = appendHash(info.m_techniqueNames[stype].getBegin(), len, hash);
- }
- if(m_binary->m_mutators.getSize())
- {
- hash = appendHash(info.m_mutation.getBegin(), m_binary->m_mutators.getSize() * sizeof(info.m_mutation[0]), hash);
- }
- // Check if the variant is in the cache
- {
- RLockGuard<RWMutex> lock(m_mtx);
- auto it = m_variants.find(hash);
- if(it != m_variants.getEnd())
- {
- // Done
- variant = *it;
- if(!!(info.m_shaderTypes & ShaderTypeBit::kAllGraphics))
- {
- ANKI_ASSERT(variant->m_prog->getShaderTypes() == info.m_shaderTypes);
- }
- return;
- }
- }
- // Create the variant
- WLockGuard<RWMutex> lock(m_mtx);
- // Check again
- auto it = m_variants.find(hash);
- if(it != m_variants.getEnd())
- {
- // Done
- variant = *it;
- return;
- }
- // Create
- ShaderProgramResourceVariant* v = createNewVariant(info);
- if(v)
- {
- m_variants.emplace(hash, v);
- }
- variant = v;
- if(!!(info.m_shaderTypes & ShaderTypeBit::kAllGraphics))
- {
- ANKI_ASSERT(variant->m_prog->getShaderTypes() == info.m_shaderTypes);
- }
- }
- U32 ShaderProgramResource::findTechnique(CString name) const
- {
- U32 techniqueIdx = kMaxU32;
- for(U32 i = 0; i < m_binary->m_techniques.getSize(); ++i)
- {
- if(m_binary->m_techniques[i].m_name.getBegin() == name)
- {
- techniqueIdx = i;
- break;
- }
- }
- ANKI_ASSERT(techniqueIdx != kMaxU32 && "Technique not found");
- return techniqueIdx;
- }
- ShaderProgramResourceVariant* ShaderProgramResource::createNewVariant(const ShaderProgramResourceVariantInitInfo& info) const
- {
- // Get the binary program variant
- const ShaderBinaryVariant* binaryVariant = nullptr;
- U64 mutationHash = 0;
- if(m_binary->m_mutators.getSize())
- {
- // Create the mutation hash
- mutationHash = computeHash(info.m_mutation.getBegin(), m_binary->m_mutators.getSize() * sizeof(info.m_mutation[0]));
- // Search for the mutation in the binary
- // TODO optimize the search
- for(const ShaderBinaryMutation& mutation : m_binary->m_mutations)
- {
- if(mutation.m_hash == mutationHash)
- {
- if(mutation.m_variantIndex == kMaxU32)
- {
- // Skipped mutation, nothing to create
- return nullptr;
- }
- binaryVariant = &m_binary->m_variants[mutation.m_variantIndex];
- break;
- }
- }
- }
- else
- {
- ANKI_ASSERT(m_binary->m_variants.getSize() == 1);
- binaryVariant = &m_binary->m_variants[0];
- }
- ANKI_ASSERT(binaryVariant);
- ShaderProgramResourceVariant* variant = newInstance<ShaderProgramResourceVariant>(ResourceMemoryPool::getSingleton());
- // Time to init the shaders
- if(!!(info.m_shaderTypes & (ShaderTypeBit::kAllGraphics | ShaderTypeBit::kCompute)))
- {
- // Create the program name
- String progName;
- getFilepathFilename(getFilename(), progName);
- ShaderProgramInitInfo progInf(progName);
- Array<ShaderPtr, U32(ShaderType::kCount)> shaderRefs; // Just for refcounting
- for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
- {
- const ShaderTypeBit shaderBit = ShaderTypeBit(1 << shaderType);
- const U32 techniqueIdx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
- ANKI_ASSERT(!!(m_binary->m_techniques[techniqueIdx].m_shaderTypes & shaderBit));
- const ResourceString shaderName = (progName + "_" + m_binary->m_techniques[techniqueIdx].m_name.getBegin()).cstr();
- ShaderInitInfo inf(shaderName);
- inf.m_shaderType = shaderType;
- const ShaderBinaryCodeBlock& binBlock =
- m_binary->m_codeBlocks[binaryVariant->m_techniqueCodeBlocks[techniqueIdx].m_codeBlockIndices[shaderType]];
- inf.m_binary = binBlock.m_binary;
- inf.m_reflection = binBlock.m_reflection;
- ShaderPtr shader = GrManager::getSingleton().newShader(inf);
- shaderRefs[shaderType] = shader;
- if(!!(shaderBit & ShaderTypeBit::kAllGraphics))
- {
- progInf.m_graphicsShaders[shaderType] = shader.get();
- }
- else if(shaderType == ShaderType::kCompute)
- {
- progInf.m_computeShader = shader.get();
- }
- else
- {
- ANKI_ASSERT(0);
- }
- }
- // Create the program
- variant->m_prog = GrManager::getSingleton().newShaderProgram(progInf);
- }
- else
- {
- ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllRayTracing));
- U32 techniqueIdx = kMaxU32;
- for(ShaderType shaderType : EnumBitsIterable<ShaderType, ShaderTypeBit>(info.m_shaderTypes))
- {
- const U32 idx = findTechnique(info.m_techniqueNames[shaderType].getBegin());
- if(techniqueIdx == kMaxU32)
- {
- techniqueIdx = idx;
- }
- else
- {
- ANKI_ASSERT(idx == techniqueIdx && "Can't mix and match different techniques in ray tracing");
- }
- }
- ANKI_ASSERT(techniqueIdx != kMaxU32);
- // Find the library
- const CString libName = m_binary->m_techniques[techniqueIdx].m_name.getBegin();
- ANKI_ASSERT(libName.getLength() > 0);
- const ShaderProgramResourceSystem& progSystem = ResourceManager::getSingleton().getShaderProgramResourceSystem();
- const ShaderProgramRaytracingLibrary* foundLib = nullptr;
- for(const ShaderProgramRaytracingLibrary& lib : progSystem.getRayTracingLibraries())
- {
- if(lib.getLibraryName() == libName)
- {
- foundLib = &lib;
- break;
- }
- }
- ANKI_ASSERT(foundLib);
- variant->m_prog = foundLib->getShaderProgram();
- RayTracingShaderGroupType groupType;
- if(!!(info.m_shaderTypes & ShaderTypeBit::kRayGen))
- {
- groupType = RayTracingShaderGroupType::kRayGen;
- }
- else if(!!(info.m_shaderTypes & ShaderTypeBit::kMiss))
- {
- groupType = RayTracingShaderGroupType::kMiss;
- }
- else
- {
- ANKI_ASSERT(!!(info.m_shaderTypes & ShaderTypeBit::kAllHit));
- groupType = RayTracingShaderGroupType::kHit;
- }
- // Set the group handle index
- variant->m_shaderGroupHandleIndex = foundLib->getShaderGroupHandleIndex(getFilename(), mutationHash, groupType);
- }
- return variant;
- }
- } // end namespace anki
|