OGLShader.cpp 8.5 KB

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