Shader.cpp 6.9 KB

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