Shader.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2008-2013 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& definesIn)
  89. {
  90. String defines = SanitateDefines(definesIn);
  91. StringHash definesHash(defines);
  92. if (type == VS)
  93. {
  94. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(definesHash);
  95. // Create the shader variation now if not created yet
  96. if (i == vsVariations_.End())
  97. {
  98. i = vsVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  99. String path, fileName, extension;
  100. SplitPath(GetName(), path, fileName, extension);
  101. String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
  102. if (fullName.EndsWith("_"))
  103. fullName.Resize(fullName.Length() - 1);
  104. i->second_->SetName(fullName);
  105. i->second_->SetDefines(defines);
  106. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  107. }
  108. return i->second_;
  109. }
  110. else
  111. {
  112. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(definesHash);
  113. // Create the shader variation now if not created yet
  114. if (i == psVariations_.End())
  115. {
  116. i = psVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  117. String path, fileName, extension;
  118. SplitPath(GetName(), path, fileName, extension);
  119. String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
  120. if (fullName.EndsWith("_"))
  121. fullName.Resize(fullName.Length() - 1);
  122. i->second_->SetName(fullName);
  123. i->second_->SetDefines(defines);
  124. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  125. }
  126. return i->second_;
  127. }
  128. }
  129. bool Shader::ProcessSource(String& code, Deserializer& source)
  130. {
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. if (!cache)
  133. return false;
  134. // Store resource dependencies for includes so that we know to reload if any of them changes
  135. if (source.GetName() != GetName())
  136. cache->StoreResourceDependency(this, source.GetName());
  137. while (!source.IsEof())
  138. {
  139. String line = source.ReadLine();
  140. if (line.StartsWith("#include"))
  141. {
  142. String includeFileName = GetPath(source.GetName()) + line.Substring(9).Replaced("\"", "").Trimmed();
  143. SharedPtr<File> includeFile = cache->GetFile(includeFileName);
  144. if (!includeFile)
  145. return false;
  146. // Add the include file into the current code recursively
  147. if (!ProcessSource(code, *includeFile))
  148. return false;
  149. }
  150. else
  151. {
  152. code += line;
  153. code += "\n";
  154. }
  155. }
  156. // Finally insert an empty line to mark the space between files
  157. code += "\n";
  158. return true;
  159. }
  160. String Shader::SanitateDefines(const String& definesIn)
  161. {
  162. String ret;
  163. ret.Reserve(definesIn.Length());
  164. unsigned numSpaces = 0;
  165. unsigned start = 0, end = definesIn.Length();
  166. // Trim spaces from start & begin. Do not use String::Trimmed() as we also need to trim spaces from the middle
  167. for (unsigned i = 0; i < definesIn.Length(); ++i)
  168. {
  169. if (definesIn[i] != ' ')
  170. {
  171. start = i;
  172. break;
  173. }
  174. }
  175. for (unsigned i = definesIn.Length() - 1; i < definesIn.Length(); --i)
  176. {
  177. if (definesIn[i] != ' ')
  178. {
  179. end = i + 1;
  180. break;
  181. }
  182. }
  183. for (unsigned i = start; i < end; ++i)
  184. {
  185. // Ensure only one space in a row
  186. if (definesIn[i] == ' ')
  187. ++numSpaces;
  188. else
  189. numSpaces = 0;
  190. if (numSpaces <= 1)
  191. ret += definesIn[i];
  192. }
  193. return ret;
  194. }
  195. }