Shader.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "Context.h"
  24. #include "Deserializer.h"
  25. #include "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "Log.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "Shader.h"
  31. #include "ShaderVariation.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. Shader::Shader(Context* context) :
  37. Resource(context)
  38. {
  39. }
  40. Shader::~Shader()
  41. {
  42. ResourceCache* cache = GetSubsystem<ResourceCache>();
  43. if (cache)
  44. cache->ResetDependencies(this);
  45. }
  46. void Shader::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Shader>();
  49. }
  50. bool Shader::Load(Deserializer& source)
  51. {
  52. PROFILE(LoadShader);
  53. Graphics* graphics = GetSubsystem<Graphics>();
  54. if (!graphics)
  55. return false;
  56. // Load the shader source code and resolve any includes
  57. String shaderCode;
  58. if (!ProcessSource(shaderCode, source))
  59. return false;
  60. // Customize the vertex & pixel shader source code to not include the unnecessary shader,
  61. // and on OpenGL, rename either VS() or PS() to main()
  62. #ifdef USE_OPENGL
  63. vsSourceCode_ = shaderCode;
  64. vsSourceCode_.Replace("void VS(", "void main(");
  65. vsSourceCode_.Replace("void PS(", "/* void PS(");
  66. vsSourceCode_ += "*/\n";
  67. psSourceCode_ = shaderCode;
  68. psSourceCode_.Replace("attribute ", "// attribute ");
  69. psSourceCode_.Replace("void VS(", "/* void VS(");
  70. psSourceCode_.Replace("void PS(", "*/\nvoid main(");
  71. #else
  72. vsSourceCode_ = shaderCode;
  73. vsSourceCode_.Replace("void PS(", "/* void PS(");
  74. vsSourceCode_ += "*/\n";
  75. psSourceCode_ = shaderCode;
  76. psSourceCode_.Replace("void VS(", "/* void VS(");
  77. psSourceCode_.Replace("void PS(", "*/\nvoid PS(");
  78. #endif
  79. // If variations had already been created, release them and require recompile
  80. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  81. i->second_->Release();
  82. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  83. i->second_->Release();
  84. SetMemoryUse(sizeof(Shader) + vsSourceCode_.Length() + psSourceCode_.Length() + (vsVariations_.Size() + psVariations_.Size()) *
  85. sizeof(ShaderVariation));
  86. return true;
  87. }
  88. ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
  89. {
  90. StringHash definesHash(defines);
  91. if (type == VS)
  92. {
  93. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(definesHash);
  94. // Create the shader variation now if not created yet
  95. if (i == vsVariations_.End())
  96. {
  97. i = vsVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  98. String path, fileName, extension;
  99. SplitPath(GetName(), path, fileName, extension);
  100. String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
  101. if (fullName.EndsWith("_"))
  102. fullName.Resize(fullName.Length() - 1);
  103. i->second_->SetName(fullName);
  104. i->second_->SetDefines(defines);
  105. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  106. }
  107. return i->second_;
  108. }
  109. else
  110. {
  111. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(definesHash);
  112. // Create the shader variation now if not created yet
  113. if (i == psVariations_.End())
  114. {
  115. i = psVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  116. String path, fileName, extension;
  117. SplitPath(GetName(), path, fileName, extension);
  118. String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
  119. if (fullName.EndsWith("_"))
  120. fullName.Resize(fullName.Length() - 1);
  121. i->second_->SetName(fullName);
  122. i->second_->SetDefines(defines);
  123. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  124. }
  125. return i->second_;
  126. }
  127. }
  128. String Shader::SanitateDefines(const String& definesIn)
  129. {
  130. String ret;
  131. ret.Reserve(definesIn.Length());
  132. unsigned numSpaces = 0;
  133. for (unsigned i = 0; i < definesIn.Length(); ++i)
  134. {
  135. // Ensure only one space in a row
  136. if (definesIn[i] == ' ')
  137. ++numSpaces;
  138. else
  139. numSpaces = 0;
  140. if (numSpaces <= 1)
  141. ret += definesIn[i];
  142. }
  143. return ret.Trimmed();
  144. }
  145. bool Shader::ProcessSource(String& code, Deserializer& source)
  146. {
  147. ResourceCache* cache = GetSubsystem<ResourceCache>();
  148. if (!cache)
  149. return false;
  150. // Store resource dependencies for includes so that we know to reload if any of them changes
  151. if (source.GetName() != GetName())
  152. cache->StoreResourceDependency(this, source.GetName());
  153. while (!source.IsEof())
  154. {
  155. String line = source.ReadLine();
  156. if (line.StartsWith("#include"))
  157. {
  158. String includeFileName = GetPath(source.GetName()) + line.Substring(9).Replaced("\"", "").Trimmed();
  159. SharedPtr<File> includeFile = cache->GetFile(includeFileName);
  160. if (!includeFile)
  161. return false;
  162. // Add the include file into the current code recursively
  163. if (!ProcessSource(code, *includeFile))
  164. return false;
  165. }
  166. else
  167. {
  168. code += line;
  169. code += "\n";
  170. }
  171. }
  172. // Finally insert an empty line to mark the space between files
  173. code += "\n";
  174. return true;
  175. }
  176. }