Shader.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. void CommentOutFunction(String& code, const String& signature)
  37. {
  38. unsigned startPos = code.Find(signature);
  39. unsigned braceLevel = 0;
  40. if (startPos == String::NPOS)
  41. return;
  42. code.Insert(startPos, "/*");
  43. for (unsigned i = startPos + 2 + signature.Length(); i < code.Length(); ++i)
  44. {
  45. if (code[i] == '{')
  46. ++braceLevel;
  47. else if (code[i] == '}')
  48. {
  49. --braceLevel;
  50. if (braceLevel == 0)
  51. {
  52. code.Insert(i + 1, "*/");
  53. return;
  54. }
  55. }
  56. }
  57. }
  58. Shader::Shader(Context* context) :
  59. Resource(context),
  60. timeStamp_(0)
  61. {
  62. }
  63. Shader::~Shader()
  64. {
  65. ResourceCache* cache = GetSubsystem<ResourceCache>();
  66. if (cache)
  67. cache->ResetDependencies(this);
  68. }
  69. void Shader::RegisterObject(Context* context)
  70. {
  71. context->RegisterFactory<Shader>();
  72. }
  73. bool Shader::Load(Deserializer& source)
  74. {
  75. PROFILE(LoadShader);
  76. Graphics* graphics = GetSubsystem<Graphics>();
  77. if (!graphics)
  78. return false;
  79. // Load the shader source code and resolve any includes
  80. timeStamp_ = 0;
  81. String shaderCode;
  82. if (!ProcessSource(shaderCode, source))
  83. return false;
  84. // Comment out the unneeded shader function
  85. vsSourceCode_ = shaderCode;
  86. psSourceCode_ = shaderCode;
  87. CommentOutFunction(vsSourceCode_, "void PS(");
  88. CommentOutFunction(psSourceCode_, "void VS(");
  89. // OpenGL: rename either VS() or PS() to main()
  90. #ifdef USE_OPENGL
  91. vsSourceCode_.Replace("uniform sampler", "// uniform sampler");
  92. vsSourceCode_.Replace("void VS(", "void main(");
  93. psSourceCode_.Replace("attribute ", "// attribute ");
  94. psSourceCode_.Replace("void PS(", "void main(");
  95. #endif
  96. // If variations had already been created, release them and require recompile
  97. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  98. i->second_->Release();
  99. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  100. i->second_->Release();
  101. SetMemoryUse(sizeof(Shader) + vsSourceCode_.Length() + psSourceCode_.Length() + (vsVariations_.Size() + psVariations_.Size()) *
  102. sizeof(ShaderVariation));
  103. return true;
  104. }
  105. ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
  106. {
  107. return GetVariation(type, defines.CString());
  108. }
  109. ShaderVariation* Shader::GetVariation(ShaderType type, const char* defines)
  110. {
  111. StringHash definesHash(defines);
  112. if (type == VS)
  113. {
  114. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(definesHash);
  115. // Create the shader variation now if not created yet
  116. if (i == vsVariations_.End())
  117. {
  118. i = vsVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  119. i->second_->SetName(GetFileName(GetName()) + " " + defines);
  120. i->second_->SetDefines(defines);
  121. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  122. }
  123. return i->second_;
  124. }
  125. else
  126. {
  127. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(definesHash);
  128. // Create the shader variation now if not created yet
  129. if (i == psVariations_.End())
  130. {
  131. i = psVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  132. i->second_->SetName(GetFileName(GetName()) + " " + defines);
  133. i->second_->SetDefines(defines);
  134. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  135. }
  136. return i->second_;
  137. }
  138. }
  139. String Shader::SanitateDefines(const String& definesIn)
  140. {
  141. String ret;
  142. ret.Reserve(definesIn.Length());
  143. unsigned numSpaces = 0;
  144. for (unsigned i = 0; i < definesIn.Length(); ++i)
  145. {
  146. // Ensure only one space in a row
  147. if (definesIn[i] == ' ')
  148. ++numSpaces;
  149. else
  150. numSpaces = 0;
  151. if (numSpaces <= 1)
  152. ret += definesIn[i];
  153. }
  154. return ret.Trimmed();
  155. }
  156. bool Shader::ProcessSource(String& code, Deserializer& source)
  157. {
  158. ResourceCache* cache = GetSubsystem<ResourceCache>();
  159. if (!cache)
  160. return false;
  161. // If the source if a non-packaged file, store the timestamp
  162. File* file = dynamic_cast<File*>(&source);
  163. if (file && !file->IsPackaged())
  164. {
  165. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  166. if (fileSystem)
  167. {
  168. String fullName = cache->GetResourceFileName(file->GetName());
  169. unsigned fileTimeStamp = fileSystem->GetLastModifiedTime(fullName);
  170. if (fileTimeStamp > timeStamp_)
  171. timeStamp_ = fileTimeStamp;
  172. }
  173. }
  174. // Store resource dependencies for includes so that we know to reload if any of them changes
  175. if (source.GetName() != GetName())
  176. cache->StoreResourceDependency(this, source.GetName());
  177. while (!source.IsEof())
  178. {
  179. String line = source.ReadLine();
  180. if (line.StartsWith("#include"))
  181. {
  182. String includeFileName = GetPath(source.GetName()) + line.Substring(9).Replaced("\"", "").Trimmed();
  183. SharedPtr<File> includeFile = cache->GetFile(includeFileName);
  184. if (!includeFile)
  185. return false;
  186. // Add the include file into the current code recursively
  187. if (!ProcessSource(code, *includeFile))
  188. return false;
  189. }
  190. else
  191. {
  192. code += line;
  193. code += "\n";
  194. }
  195. }
  196. // Finally insert an empty line to mark the space between files
  197. code += "\n";
  198. return true;
  199. }
  200. }