ShaderPrecache.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Copyright (c) 2008-2014 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 "File.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "Log.h"
  27. #include "ShaderPrecache.h"
  28. #include "ShaderVariation.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. ShaderPrecache::ShaderPrecache(Context* context, const String& fileName) :
  33. Object(context),
  34. fileName_(fileName),
  35. xmlFile_(context)
  36. {
  37. if (GetSubsystem<FileSystem>()->FileExists(fileName))
  38. {
  39. // If file exists, read the already listed combinations
  40. File source(context_, fileName);
  41. xmlFile_.Load(source);
  42. XMLElement shader = xmlFile_.GetRoot().GetChild("shader");
  43. while (shader)
  44. {
  45. String oldCombination = shader.GetAttribute("vs") + " " + shader.GetAttribute("vsdefines") + " " +
  46. shader.GetAttribute("ps") + " " + shader.GetAttribute("psdefines");
  47. usedCombinations_.Insert(oldCombination);
  48. shader = shader.GetNext("shader");
  49. }
  50. }
  51. // If no file yet or loading failed, create the root element now
  52. if (!xmlFile_.GetRoot())
  53. xmlFile_.CreateRoot("shaders");
  54. LOGINFO("Begin dumping shaders to " + fileName_);
  55. }
  56. ShaderPrecache::~ShaderPrecache()
  57. {
  58. LOGINFO("End dumping shaders");
  59. if (usedCombinations_.Empty())
  60. return;
  61. File dest(context_, fileName_, FILE_WRITE);
  62. xmlFile_.Save(dest);
  63. }
  64. void ShaderPrecache::StoreShaders(ShaderVariation* vs, ShaderVariation* ps)
  65. {
  66. if (!vs || !ps)
  67. return;
  68. // Check for duplicate using pointers first (fast)
  69. Pair<ShaderVariation*, ShaderVariation*> shaderPair = MakePair(vs, ps);
  70. if (usedPtrCombinations_.Contains(shaderPair))
  71. return;
  72. usedPtrCombinations_.Insert(shaderPair);
  73. String vsName = vs->GetName();
  74. String psName = ps->GetName();
  75. const String& vsDefines = vs->GetDefines();
  76. const String& psDefines = ps->GetDefines();
  77. // Check for duplicate using strings (needed for combinations loaded from existing file)
  78. String newCombination = vsName + " " + vsDefines + " " + psName + " " + psDefines;
  79. if (usedCombinations_.Contains(newCombination))
  80. return;
  81. usedCombinations_.Insert(newCombination);
  82. XMLElement shaderElem = xmlFile_.GetRoot().CreateChild("shader");
  83. shaderElem.SetAttribute("vs", vsName);
  84. shaderElem.SetAttribute("vsdefines", vsDefines);
  85. shaderElem.SetAttribute("ps", psName);
  86. shaderElem.SetAttribute("psdefines", psDefines);
  87. }
  88. void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source)
  89. {
  90. LOGDEBUG("Begin precaching shaders");
  91. XMLFile xmlFile(graphics->GetContext());
  92. xmlFile.Load(source);
  93. XMLElement shader = xmlFile.GetRoot().GetChild("shader");
  94. while (shader)
  95. {
  96. ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), shader.GetAttribute("vsdefines"));
  97. ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), shader.GetAttribute("psdefines"));
  98. // Set the shaders active to actually compile them
  99. graphics->SetShaders(vs, ps);
  100. shader = shader.GetNext("shader");
  101. }
  102. LOGDEBUG("End precaching shaders");
  103. }
  104. }