ShaderPrecache.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. else
  52. xmlFile_.CreateRoot("shaders");
  53. LOGDEBUG("Begin dumping shaders to " + fileName_);
  54. }
  55. ShaderPrecache::~ShaderPrecache()
  56. {
  57. LOGDEBUG("End dumping shaders");
  58. if (usedCombinations_.Empty())
  59. return;
  60. File dest(context_, fileName_, FILE_WRITE);
  61. xmlFile_.Save(dest);
  62. }
  63. void ShaderPrecache::StoreShaders(ShaderVariation* vs, ShaderVariation* ps)
  64. {
  65. if (!vs || !ps)
  66. return;
  67. String vsName = vs->GetName().Substring(0, vs->GetName().Find('_'));
  68. String psName = ps->GetName().Substring(0, ps->GetName().Find('_'));
  69. const String& vsDefines = vs->GetDefines();
  70. const String& psDefines = ps->GetDefines();
  71. // Check for duplicate
  72. String newCombination = vsName + " " + vsDefines + " " + psName + " " + psDefines;
  73. if (usedCombinations_.Contains(newCombination))
  74. return;
  75. XMLElement shaderElem = xmlFile_.GetRoot().CreateChild("shader");
  76. shaderElem.SetAttribute("vs", vsName);
  77. shaderElem.SetAttribute("vsdefines", vsDefines);
  78. shaderElem.SetAttribute("ps", psName);
  79. shaderElem.SetAttribute("psdefines", psDefines);
  80. usedCombinations_.Insert(newCombination);
  81. }
  82. void ShaderPrecache::LoadShaders(Graphics* graphics, Deserializer& source)
  83. {
  84. LOGDEBUG("Begin precaching shaders");
  85. XMLFile xmlFile(graphics->GetContext());
  86. xmlFile.Load(source);
  87. XMLElement shader = xmlFile.GetRoot().GetChild("shader");
  88. while (shader)
  89. {
  90. ShaderVariation* vs = graphics->GetShader(VS, shader.GetAttribute("vs"), shader.GetAttribute("vsdefines"));
  91. ShaderVariation* ps = graphics->GetShader(PS, shader.GetAttribute("ps"), shader.GetAttribute("psdefines"));
  92. // Set the shaders active to actually compile them
  93. graphics->SetShaders(vs, ps);
  94. shader = shader.GetNext("shader");
  95. }
  96. LOGDEBUG("End precaching shaders");
  97. }
  98. }