OGLShader.cpp 7.6 KB

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