ShaderPrecache.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Graphics/Graphics.h"
  24. #include "../Graphics/GraphicsImpl.h"
  25. #include "../Graphics/ShaderPrecache.h"
  26. #include "../Graphics/ShaderVariation.h"
  27. #include "../IO/File.h"
  28. #include "../IO/FileSystem.h"
  29. #include "../IO/Log.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. ShaderPrecache::ShaderPrecache(Context* context, const String& fileName) :
  34. Object(context),
  35. fileName_(fileName),
  36. xmlFile_(context)
  37. {
  38. if (GetSubsystem<FileSystem>()->FileExists(fileName))
  39. {
  40. // If file exists, read the already listed combinations
  41. File source(context_, fileName);
  42. xmlFile_.Load(source);
  43. XMLElement shader = xmlFile_.GetRoot().GetChild("shader");
  44. while (shader)
  45. {
  46. String oldCombination = shader.GetAttribute("vs") + " " + shader.GetAttribute("vsdefines") + " " +
  47. shader.GetAttribute("ps") + " " + shader.GetAttribute("psdefines");
  48. usedCombinations_.Insert(oldCombination);
  49. shader = shader.GetNext("shader");
  50. }
  51. }
  52. // If no file yet or loading failed, create the root element now
  53. if (!xmlFile_.GetRoot())
  54. xmlFile_.CreateRoot("shaders");
  55. ATOMIC_LOGINFO("Begin dumping shaders to " + fileName_);
  56. }
  57. ShaderPrecache::~ShaderPrecache()
  58. {
  59. ATOMIC_LOGINFO("End dumping shaders");
  60. if (usedCombinations_.Empty())
  61. return;
  62. File dest(context_, fileName_, FILE_WRITE);
  63. xmlFile_.Save(dest);
  64. }
  65. void ShaderPrecache::StoreShaders(ShaderVariation* vs, ShaderVariation* ps)
  66. {
  67. if (!vs || !ps)
  68. return;
  69. // Check for duplicate using pointers first (fast)
  70. Pair<ShaderVariation*, ShaderVariation*> shaderPair = MakePair(vs, ps);
  71. if (usedPtrCombinations_.Contains(shaderPair))
  72. return;
  73. usedPtrCombinations_.Insert(shaderPair);
  74. String vsName = vs->GetName();
  75. String psName = ps->GetName();
  76. const String& vsDefines = vs->GetDefines();
  77. const String& psDefines = ps->GetDefines();
  78. // Check for duplicate using strings (needed for combinations loaded from existing file)
  79. String newCombination = vsName + " " + vsDefines + " " + psName + " " + psDefines;
  80. if (usedCombinations_.Contains(newCombination))
  81. return;
  82. usedCombinations_.Insert(newCombination);
  83. XMLElement shaderElem = xmlFile_.GetRoot().CreateChild("shader");
  84. shaderElem.SetAttribute("vs", vsName);
  85. shaderElem.SetAttribute("vsdefines", vsDefines);
  86. shaderElem.SetAttribute("ps", psName);
  87. shaderElem.SetAttribute("psdefines", psDefines);
  88. }
  89. void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source)
  90. {
  91. ATOMIC_LOGDEBUG("Begin precaching shaders");
  92. XMLFile xmlFile(graphics->GetContext());
  93. xmlFile.Load(source);
  94. XMLElement shader = xmlFile.GetRoot().GetChild("shader");
  95. while (shader)
  96. {
  97. String vsDefines = shader.GetAttribute("vsdefines");
  98. String psDefines = shader.GetAttribute("psdefines");
  99. // Check for illegal variations on OpenGL ES and skip them
  100. #ifdef GL_ES_VERSION_2_0
  101. if (
  102. #ifndef __EMSCRIPTEN__
  103. vsDefines.Contains("INSTANCED") ||
  104. #endif
  105. (psDefines.Contains("POINTLIGHT") && psDefines.Contains("SHADOW")))
  106. {
  107. shader = shader.GetNext("shader");
  108. continue;
  109. }
  110. #endif
  111. ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), vsDefines);
  112. ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), psDefines);
  113. // Set the shaders active to actually compile them
  114. graphics->SetShaders(vs, ps);
  115. shader = shader.GetNext("shader");
  116. }
  117. ATOMIC_LOGDEBUG("End precaching shaders");
  118. }
  119. }