OGLShader.cpp 7.6 KB

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