MCPcopy Create free account
hub / github.com/Illation/ETEngine / ReplaceInclude

Method ReplaceInclude

Engine/source/EtRendering/GraphicsTypes/Shader.cpp:387–447  ·  view source on GitHub ↗

--------------------------------- ShaderAsset::ReplaceInclude Include another shader asset inline

Source from the content-addressed store, hash-verified

385// Include another shader asset inline
386//
387bool ShaderAsset::ReplaceInclude(std::string &line)
388{
389 // Get the asset ID
390 uint32 firstQ = (uint32)line.find("\"");
391 uint32 lastQ = (uint32)line.rfind("\"");
392 if ((firstQ == std::string::npos) ||
393 (lastQ == std::string::npos) ||
394 lastQ <= firstQ)
395 {
396 LOG(std::string("ShaderAsset::ReplaceInclude > Replacing include line '") + line + "' failed", core::LogLevel::Warning);
397 return false;
398 }
399 firstQ++;
400 std::string path = line.substr(firstQ, lastQ - firstQ);
401 core::HashString const assetId(core::FileUtil::ExtractName(path).c_str());
402
403 // Get the stub asset data
404 auto const foundRefIt = std::find_if(GetReferences().cbegin(), GetReferences().cend(), [assetId](Reference const& reference)
405 {
406 ET_ASSERT(reference.GetAsset() != nullptr);
407 return reference.GetAsset()->GetAsset()->GetId() == assetId;
408 });
409
410 if (foundRefIt == GetReferences().cend())
411 {
412 LOG(std::string("ShaderAsset::ReplaceInclude > Asset at path '") + path + "' not found in references!", core::LogLevel::Warning);
413 return false;
414 }
415 I_AssetPtr const* const rawAssetPtr = foundRefIt->GetAsset();
416 ET_ASSERT(rawAssetPtr->GetType() == rttr::type::get<core::StubData>(), "Asset reference found at path %s is not of type StubData", path);
417 AssetPtr<core::StubData> stubPtr = *static_cast<AssetPtr<core::StubData> const*>(rawAssetPtr);
418
419 // extract the shader string
420 std::string shaderContent(stubPtr->GetText(), stubPtr->GetLength());
421 if (shaderContent.size() == 0)
422 {
423 LOG(std::string("ShaderAsset::ReplaceInclude > Shader string extracted from stub data at'") + path + "' was empty!", core::LogLevel::Warning);
424 return false;
425 }
426
427 // replace the original line with the included shader
428 line = "";
429 std::string extractedLine;
430 while (core::FileUtil::ParseLine(shaderContent, extractedLine))
431 {
432 //Includes
433 if (extractedLine.find("#include") != std::string::npos)
434 {
435 if (!(ReplaceInclude(extractedLine)))
436 {
437 LOG(std::string("ShaderAsset::ReplaceInclude > Replacing include at '") + extractedLine + "' failed!", core::LogLevel::Warning);
438 return false;
439 }
440 }
441
442 line += extractedLine + "\n";
443 }
444

Callers

nothing calls this directly

Calls 5

GetTextMethod · 0.80
GetLengthMethod · 0.80
GetAssetMethod · 0.45
GetIdMethod · 0.45
GetTypeMethod · 0.45

Tested by

no test coverage detected