OGLShader.cpp 8.4 KB

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