OGLShader.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = vsParser_.GetCombination(name);
  99. i = vsVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
  100. String path, fileName, extension;
  101. SplitPath(GetName(), path, fileName, extension);
  102. String fullName = path + fileName + "_" + name;
  103. if (fullName.EndsWith("_"))
  104. fullName.Resize(fullName.Length() - 1);
  105. i->second_->SetName(fullName);
  106. i->second_->SetSourceCode(vsSourceCode_, vsSourceCodeLength_);
  107. i->second_->SetDefines(combination.defines_, combination.defineValues_);
  108. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  109. }
  110. return i->second_;
  111. }
  112. else
  113. return 0;
  114. }
  115. else
  116. {
  117. if (psParser_.HasCombination(name))
  118. {
  119. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(nameHash);
  120. // Create the shader variation now if not created yet
  121. if (i == psVariations_.End())
  122. {
  123. ShaderCombination combination = psParser_.GetCombination(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. }
  188. }