OGLShader.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "FileSystem.h"
  27. #include "Graphics.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Shader.h"
  32. #include "ShaderVariation.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. OBJECTTYPESTATIC(Shader);
  38. Shader::Shader(Context* context) :
  39. Resource(context),
  40. vsSourceCodeLength_(0),
  41. psSourceCodeLength_(0)
  42. {
  43. }
  44. Shader::~Shader()
  45. {
  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. vsSourceCodeLength_ = 0;
  58. psSourceCodeLength_ = 0;
  59. SharedPtr<XMLFile> xml(new XMLFile(context_));
  60. if (!xml->Load(source))
  61. return false;
  62. if (!vsParser_.Parse(VS, xml->GetRoot("shaders")))
  63. {
  64. LOGERROR("VS: " + vsParser_.GetErrorMessage());
  65. return false;
  66. }
  67. if (!psParser_.Parse(PS, xml->GetRoot("shaders")))
  68. {
  69. LOGERROR("PS: " + psParser_.GetErrorMessage());
  70. return false;
  71. }
  72. String path, fileName, extension;
  73. SplitPath(GetName(), path, fileName, extension);
  74. if (!ProcessSource(vsSourceCode_, vsSourceCodeLength_, path + fileName + ".vert"))
  75. return false;
  76. if (!ProcessSource(psSourceCode_, psSourceCodeLength_, path + fileName + ".frag"))
  77. return false;
  78. // If variations had already been created, release them
  79. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  80. i->second_->Release();
  81. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  82. i->second_->Release();
  83. SetMemoryUse(sizeof(Shader) + 2 * sizeof(ShaderParser) + (vsVariations_.Size() + psVariations_.Size()) *
  84. sizeof(ShaderVariation));
  85. return true;
  86. }
  87. ShaderVariation* Shader::GetVariation(ShaderType type, const String& name)
  88. {
  89. StringHash nameHash(name);
  90. if (type == VS)
  91. {
  92. if (vsParser_.HasCombination(name))
  93. {
  94. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(nameHash);
  95. // Create the shader variation now if not created yet
  96. if (i == vsVariations_.End())
  97. {
  98. ShaderCombination combination;
  99. vsParser_.GetCombination(combination, name);
  100. i = vsVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  101. String path, fileName, extension;
  102. SplitPath(GetName(), path, fileName, extension);
  103. String fullName = path + fileName + "_" + name;
  104. if (fullName.EndsWith("_"))
  105. fullName.Resize(fullName.Length() - 1);
  106. i->second_->SetName(fullName);
  107. i->second_->SetSourceCode(vsSourceCode_, vsSourceCodeLength_);
  108. i->second_->SetDefines(combination.defines_, combination.defineValues_);
  109. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  110. }
  111. return i->second_;
  112. }
  113. else
  114. return 0;
  115. }
  116. else
  117. {
  118. if (psParser_.HasCombination(name))
  119. {
  120. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(nameHash);
  121. // Create the shader variation now if not created yet
  122. if (i == psVariations_.End())
  123. {
  124. ShaderCombination combination;
  125. psParser_.GetCombination(combination, name);
  126. i = psVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
  127. String path, fileName, extension;
  128. SplitPath(GetName(), path, fileName, extension);
  129. String fullName = path + fileName + "_" + name;
  130. if (fullName.EndsWith("_"))
  131. fullName.Resize(fullName.Length() - 1);
  132. i->second_->SetName(fullName);
  133. i->second_->SetSourceCode(psSourceCode_, psSourceCodeLength_);
  134. i->second_->SetDefines(combination.defines_, combination.defineValues_);
  135. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  136. }
  137. return i->second_;
  138. }
  139. else
  140. return 0;
  141. }
  142. }
  143. bool Shader::ProcessSource(SharedArrayPtr<char>& dest, unsigned& length, const String& fileName)
  144. {
  145. ResourceCache* cache = GetSubsystem<ResourceCache>();
  146. if (!cache)
  147. return false;
  148. Vector<String> glslCode;
  149. // Load the shader source code
  150. SharedPtr<File> glslFile = cache->GetFile(fileName);
  151. if (!glslFile)
  152. return false;
  153. while (!glslFile->IsEof())
  154. glslCode.Push(glslFile->ReadLine());
  155. // Process the code for includes
  156. for (unsigned i = 0; i < glslCode.Size(); ++i)
  157. {
  158. if (glslCode[i].StartsWith("#include"))
  159. {
  160. String includeFileName = GetPath(fileName) + glslCode[i].Substring(9).Replaced("\"", "").Trimmed();
  161. SharedPtr<File> glslIncludeFile = cache->GetFile(includeFileName);
  162. if (!glslIncludeFile)
  163. return false;
  164. // Remove the #include line, then include the code
  165. glslCode.Erase(i);
  166. unsigned pos = i;
  167. while (!glslIncludeFile->IsEof())
  168. {
  169. glslCode.Insert(pos, glslIncludeFile->ReadLine());
  170. ++pos;
  171. }
  172. // Finally insert an empty line to mark the space between files
  173. glslCode.Insert(pos, "");
  174. }
  175. }
  176. // Copy the final code into one memory block
  177. length = 0;
  178. for (unsigned i = 0; i < glslCode.Size(); ++i)
  179. length += glslCode[i].Length() + 1;
  180. dest = new char[length];
  181. char* destPtr = dest.Get();
  182. for (unsigned i = 0; i < glslCode.Size(); ++i)
  183. {
  184. memcpy(destPtr, glslCode[i].CString(), glslCode[i].Length());
  185. destPtr += glslCode[i].Length();
  186. *destPtr++ = '\n';
  187. }
  188. return true;
  189. }
  190. }