ShaderPrecache.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. String vsName = vs->GetName().Substring(0, vs->GetName().Find('_'));
  69. String psName = ps->GetName().Substring(0, ps->GetName().Find('_'));
  70. const String& vsDefines = vs->GetDefines();
  71. const String& psDefines = ps->GetDefines();
  72. // Check for duplicate
  73. String newCombination = vsName + " " + vsDefines + " " + psName + " " + psDefines;
  74. if (usedCombinations_.Contains(newCombination))
  75. return;
  76. XMLElement shaderElem = xmlFile_.GetRoot().CreateChild("shader");
  77. shaderElem.SetAttribute("vs", vsName);
  78. shaderElem.SetAttribute("vsdefines", vsDefines);
  79. shaderElem.SetAttribute("ps", psName);
  80. shaderElem.SetAttribute("psdefines", psDefines);
  81. usedCombinations_.Insert(newCombination);
  82. }
  83. void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source)
  84. {
  85. LOGDEBUG("Begin precaching shaders");
  86. XMLFile xmlFile(graphics->GetContext());
  87. xmlFile.Load(source);
  88. XMLElement shader = xmlFile.GetRoot().GetChild("shader");
  89. while (shader)
  90. {
  91. ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), shader.GetAttribute("vsdefines"));
  92. ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), shader.GetAttribute("psdefines"));
  93. // Set the shaders active to actually compile them
  94. graphics->SetShaders(vs, ps);
  95. shader = shader.GetNext("shader");
  96. }
  97. LOGDEBUG("End precaching shaders");
  98. }
  99. }