OGLShader.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. OBJECTTYPESTATIC(Shader);
  37. Shader::Shader(Context* context) :
  38. Resource(context),
  39. vsSourceCodeLength_(0),
  40. psSourceCodeLength_(0)
  41. {
  42. }
  43. Shader::~Shader()
  44. {
  45. ResourceCache* cache = GetSubsystem<ResourceCache>();
  46. if (cache)
  47. cache->ResetDependencies(this);
  48. }
  49. void Shader::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<Shader>();
  52. }
  53. bool Shader::Load(Deserializer& source)
  54. {
  55. PROFILE(LoadShader);
  56. Graphics* graphics = GetSubsystem<Graphics>();
  57. if (!graphics)
  58. return false;
  59. vsSourceCodeLength_ = 0;
  60. psSourceCodeLength_ = 0;
  61. SharedPtr<XMLFile> xml(new XMLFile(context_));
  62. if (!xml->Load(source))
  63. return false;
  64. if (!vsParser_.Parse(VS, xml->GetRoot("shaders")))
  65. {
  66. LOGERROR("VS: " + vsParser_.GetErrorMessage());
  67. return false;
  68. }
  69. if (!psParser_.Parse(PS, xml->GetRoot("shaders")))
  70. {
  71. LOGERROR("PS: " + psParser_.GetErrorMessage());
  72. return false;
  73. }
  74. String path, fileName, extension;
  75. SplitPath(GetName(), path, fileName, extension);
  76. if (!ProcessSource(vsSourceCode_, vsSourceCodeLength_, path + fileName + ".vert"))
  77. return false;
  78. if (!ProcessSource(psSourceCode_, psSourceCodeLength_, path + fileName + ".frag"))
  79. return false;
  80. // If variations had already been created, release them and set new source code
  81. /// \todo Should also update defines
  82. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  83. {
  84. i->second_->Release();
  85. i->second_->SetSourceCode(vsSourceCode_, vsSourceCodeLength_);
  86. }
  87. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  88. {
  89. i->second_->Release();
  90. i->second_->SetSourceCode(psSourceCode_, psSourceCodeLength_);
  91. }
  92. SetMemoryUse(sizeof(Shader) + 2 * sizeof(ShaderParser) + (vsVariations_.Size() + psVariations_.Size()) *
  93. sizeof(ShaderVariation));
  94. return true;
  95. }
  96. ShaderVariation* Shader::GetVariation(ShaderType type, const String& name)
  97. {
  98. StringHash nameHash(name);
  99. if (type == VS)
  100. {
  101. if (vsParser_.HasCombination(name))
  102. {
  103. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(nameHash);
  104. // Create the shader variation now if not created yet
  105. if (i == vsVariations_.End())
  106. {
  107. ShaderCombination combination = vsParser_.GetCombination(name);
  108. i = vsVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  109. String path, fileName, extension;
  110. SplitPath(GetName(), path, fileName, extension);
  111. String fullName = path + fileName + "_" + name;
  112. if (fullName.EndsWith("_"))
  113. fullName.Resize(fullName.Length() - 1);
  114. i->second_->SetName(fullName);
  115. i->second_->SetSourceCode(vsSourceCode_, vsSourceCodeLength_);
  116. i->second_->SetDefines(combination.defines_, combination.defineValues_);
  117. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  118. }
  119. return i->second_;
  120. }
  121. else
  122. return 0;
  123. }
  124. else
  125. {
  126. if (psParser_.HasCombination(name))
  127. {
  128. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(nameHash);
  129. // Create the shader variation now if not created yet
  130. if (i == psVariations_.End())
  131. {
  132. ShaderCombination combination = psParser_.GetCombination(name);
  133. i = psVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  134. String path, fileName, extension;
  135. SplitPath(GetName(), path, fileName, extension);
  136. String fullName = path + fileName + "_" + name;
  137. if (fullName.EndsWith("_"))
  138. fullName.Resize(fullName.Length() - 1);
  139. i->second_->SetName(fullName);
  140. i->second_->SetSourceCode(psSourceCode_, psSourceCodeLength_);
  141. i->second_->SetDefines(combination.defines_, combination.defineValues_);
  142. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  143. }
  144. return i->second_;
  145. }
  146. else
  147. return 0;
  148. }
  149. }
  150. bool Shader::ProcessSource(SharedArrayPtr<char>& dest, unsigned& length, const String& fileName)
  151. {
  152. ResourceCache* cache = GetSubsystem<ResourceCache>();
  153. if (!cache)
  154. return false;
  155. cache->StoreResourceDependency(this, fileName);
  156. Vector<String> glslCode;
  157. // Load the shader source code
  158. SharedPtr<File> glslFile = cache->GetFile(fileName);
  159. if (!glslFile)
  160. return false;
  161. while (!glslFile->IsEof())
  162. glslCode.Push(glslFile->ReadLine());
  163. // Process the code for includes
  164. for (unsigned i = 0; i < glslCode.Size(); ++i)
  165. {
  166. if (glslCode[i].StartsWith("#include"))
  167. {
  168. String includeFileName = GetPath(fileName) + glslCode[i].Substring(9).Replaced("\"", "").Trimmed();
  169. SharedPtr<File> glslIncludeFile = cache->GetFile(includeFileName);
  170. if (!glslIncludeFile)
  171. return false;
  172. // Remove the #include line, then include the code
  173. glslCode.Erase(i);
  174. unsigned pos = i;
  175. while (!glslIncludeFile->IsEof())
  176. {
  177. glslCode.Insert(pos, glslIncludeFile->ReadLine());
  178. ++pos;
  179. }
  180. // Finally insert an empty line to mark the space between files
  181. glslCode.Insert(pos, "");
  182. }
  183. }
  184. // Copy the final code into one memory block
  185. length = 0;
  186. for (unsigned i = 0; i < glslCode.Size(); ++i)
  187. length += glslCode[i].Length() + 1;
  188. dest = new char[length];
  189. char* destPtr = dest.Get();
  190. for (unsigned i = 0; i < glslCode.Size(); ++i)
  191. {
  192. memcpy(destPtr, glslCode[i].CString(), glslCode[i].Length());
  193. destPtr += glslCode[i].Length();
  194. *destPtr++ = '\n';
  195. }
  196. return true;
  197. }
  198. }