Shader.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(), comment out vertex attributes in pixel shaders
  90. #ifdef USE_OPENGL
  91. vsSourceCode_.Replace("void VS(", "void main(");
  92. psSourceCode_.Replace("void PS(", "void main(");
  93. psSourceCode_.Replace("attribute ", "// attribute ");
  94. #endif
  95. // If variations had already been created, release them and require recompile
  96. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  97. i->second_->Release();
  98. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  99. i->second_->Release();
  100. SetMemoryUse(sizeof(Shader) + vsSourceCode_.Length() + psSourceCode_.Length() + (vsVariations_.Size() + psVariations_.Size()) *
  101. sizeof(ShaderVariation));
  102. return true;
  103. }
  104. ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
  105. {
  106. return GetVariation(type, defines.CString());
  107. }
  108. ShaderVariation* Shader::GetVariation(ShaderType type, const char* defines)
  109. {
  110. StringHash definesHash(defines);
  111. if (type == VS)
  112. {
  113. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(definesHash);
  114. // Create the shader variation now if not created yet
  115. if (i == vsVariations_.End())
  116. {
  117. i = vsVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  118. i->second_->SetName(GetFileName(GetName()) + " " + defines);
  119. i->second_->SetDefines(defines);
  120. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  121. }
  122. return i->second_;
  123. }
  124. else
  125. {
  126. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(definesHash);
  127. // Create the shader variation now if not created yet
  128. if (i == psVariations_.End())
  129. {
  130. i = psVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  131. i->second_->SetName(GetFileName(GetName()) + " " + defines);
  132. i->second_->SetDefines(defines);
  133. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  134. }
  135. return i->second_;
  136. }
  137. }
  138. String Shader::SanitateDefines(const String& definesIn)
  139. {
  140. String ret;
  141. ret.Reserve(definesIn.Length());
  142. unsigned numSpaces = 0;
  143. for (unsigned i = 0; i < definesIn.Length(); ++i)
  144. {
  145. // Ensure only one space in a row
  146. if (definesIn[i] == ' ')
  147. ++numSpaces;
  148. else
  149. numSpaces = 0;
  150. if (numSpaces <= 1)
  151. ret += definesIn[i];
  152. }
  153. return ret.Trimmed();
  154. }
  155. bool Shader::ProcessSource(String& code, Deserializer& source)
  156. {
  157. ResourceCache* cache = GetSubsystem<ResourceCache>();
  158. if (!cache)
  159. return false;
  160. // If the source if a non-packaged file, store the timestamp
  161. File* file = dynamic_cast<File*>(&source);
  162. if (file && !file->IsPackaged())
  163. {
  164. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  165. if (fileSystem)
  166. {
  167. String fullName = cache->GetResourceFileName(file->GetName());
  168. unsigned fileTimeStamp = fileSystem->GetLastModifiedTime(fullName);
  169. if (fileTimeStamp > timeStamp_)
  170. timeStamp_ = fileTimeStamp;
  171. }
  172. }
  173. // Store resource dependencies for includes so that we know to reload if any of them changes
  174. if (source.GetName() != GetName())
  175. cache->StoreResourceDependency(this, source.GetName());
  176. while (!source.IsEof())
  177. {
  178. String line = source.ReadLine();
  179. if (line.StartsWith("#include"))
  180. {
  181. String includeFileName = GetPath(source.GetName()) + line.Substring(9).Replaced("\"", "").Trimmed();
  182. SharedPtr<File> includeFile = cache->GetFile(includeFileName);
  183. if (!includeFile)
  184. return false;
  185. // Add the include file into the current code recursively
  186. if (!ProcessSource(code, *includeFile))
  187. return false;
  188. }
  189. else
  190. {
  191. code += line;
  192. code += "\n";
  193. }
  194. }
  195. // Finally insert an empty line to mark the space between files
  196. code += "\n";
  197. return true;
  198. }
  199. }