D3D9Shader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "File.h"
  26. #include "FileSystem.h"
  27. #include "Graphics.h"
  28. #include "GraphicsImpl.h"
  29. #include "Log.h"
  30. #include "Profiler.h"
  31. #include "ResourceCache.h"
  32. #include "Shader.h"
  33. #include "ShaderVariation.h"
  34. #include "XMLFile.h"
  35. #include "DebugNew.h"
  36. namespace Urho3D
  37. {
  38. OBJECTTYPESTATIC(Shader);
  39. Shader::Shader(Context* context) :
  40. Resource(context),
  41. sourceModifiedTime_(0)
  42. {
  43. }
  44. Shader::~Shader()
  45. {
  46. ResourceCache* cache = GetSubsystem<ResourceCache>();
  47. if (cache)
  48. cache->ResetDependencies(this);
  49. }
  50. void Shader::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<Shader>();
  53. }
  54. bool Shader::Load(Deserializer& source)
  55. {
  56. PROFILE(LoadShader);
  57. ResourceCache* cache = GetSubsystem<ResourceCache>();
  58. cache->ResetDependencies(this);
  59. Graphics* graphics = GetSubsystem<Graphics>();
  60. if (!graphics)
  61. return false;
  62. if (graphics->GetSM3Support())
  63. {
  64. vsExtension_ = ".vs3";
  65. psExtension_ = ".ps3";
  66. subDir_ = "SM3/";
  67. }
  68. else
  69. {
  70. vsExtension_ = ".vs2";
  71. psExtension_ = ".ps2";
  72. subDir_ = "SM2/";
  73. }
  74. // Get absolute file name of the shader in case we need to invoke ShaderCompiler. This only works if the shader was not
  75. // loaded from a package file
  76. fullFileName_.Clear();
  77. sourceModifiedTime_ = 0;
  78. File* sourceFile = dynamic_cast<File*>(&source);
  79. if (sourceFile && !sourceFile->IsPackaged())
  80. {
  81. PROFILE(CheckTimestamps);
  82. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  83. if (fileSystem && !fileSystem->HasRegisteredPaths())
  84. {
  85. fullFileName_ = cache->GetResourceFileName(GetName());
  86. if (!fullFileName_.Empty())
  87. {
  88. // Get last modified time of the main HLSL source file
  89. String path, fileName, extension;
  90. SplitPath(fullFileName_, path, fileName, extension);
  91. String hlslFileName = path + fileName + ".hlsl";
  92. sourceModifiedTime_ = fileSystem->GetLastModifiedTime(hlslFileName);
  93. String resourcePath = GetPath(GetName());
  94. cache->StoreResourceDependency(this, resourcePath + fileName + ".hlsl");
  95. // Check also timestamps of any included files and the shader description file
  96. if (sourceModifiedTime_)
  97. {
  98. SharedPtr<File> file(new File(context_, hlslFileName));
  99. while (file->IsOpen() && !file->IsEof())
  100. {
  101. String line = file->ReadLine();
  102. if (line.StartsWith("#include"))
  103. {
  104. String includeName = line.Substring(9).Replaced("\"", "").Trimmed();
  105. String includeFullName = path + includeName;
  106. cache->StoreResourceDependency(this, resourcePath + includeName);
  107. unsigned includeFileTime = fileSystem->GetLastModifiedTime(includeFullName);
  108. if (includeFileTime > sourceModifiedTime_)
  109. sourceModifiedTime_ = includeFileTime;
  110. }
  111. }
  112. unsigned descriptionFileTime = fileSystem->GetLastModifiedTime(fullFileName_);
  113. if (descriptionFileTime > sourceModifiedTime_)
  114. sourceModifiedTime_ = descriptionFileTime;
  115. }
  116. else
  117. {
  118. // If the HLSL file was not found, do not attempt to compile shaders
  119. fullFileName_.Clear();
  120. }
  121. }
  122. }
  123. }
  124. SharedPtr<XMLFile> xml(new XMLFile(context_));
  125. if (!xml->Load(source))
  126. return false;
  127. XMLElement shaders = xml->GetRoot("shaders");
  128. if (!shaders)
  129. {
  130. LOGERROR("No shaders element in " + source.GetName());
  131. return false;
  132. }
  133. Vector<String> globalDefines;
  134. Vector<String> globalDefineValues;
  135. if (graphics->GetSM3Support())
  136. {
  137. globalDefines.Push("SM3");
  138. globalDefineValues.Push("1");
  139. }
  140. {
  141. PROFILE(ParseShaderDefinition);
  142. if (!vsParser_.Parse(VS, shaders, globalDefines, globalDefineValues))
  143. {
  144. LOGERROR("VS: " + vsParser_.GetErrorMessage());
  145. return false;
  146. }
  147. if (!psParser_.Parse(PS, shaders, globalDefines, globalDefineValues))
  148. {
  149. LOGERROR("PS: " + psParser_.GetErrorMessage());
  150. return false;
  151. }
  152. }
  153. // If variations had already been created, clear their bytecode
  154. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Begin(); i != vsVariations_.End(); ++i)
  155. {
  156. i->second_->Release();
  157. i->second_->SetByteCode(SharedArrayPtr<unsigned char>());
  158. }
  159. for (HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Begin(); i != psVariations_.End(); ++i)
  160. {
  161. i->second_->Release();
  162. i->second_->SetByteCode(SharedArrayPtr<unsigned char>());
  163. }
  164. SetMemoryUse(sizeof(Shader) + 2 * sizeof(ShaderParser) + (vsVariations_.Size() + psVariations_.Size()) *
  165. sizeof(ShaderVariation));
  166. return true;
  167. }
  168. ShaderVariation* Shader::GetVariation(ShaderType type, const String& name)
  169. {
  170. Graphics* graphics = GetSubsystem<Graphics>();
  171. if (!graphics)
  172. return 0;
  173. StringHash nameHash(name);
  174. ShaderParser& parser = type == VS ? vsParser_ : psParser_;
  175. HashMap<StringHash, SharedPtr<ShaderVariation> >& variations = type == VS ? vsVariations_ : psVariations_;
  176. if (parser.HasCombination(name))
  177. {
  178. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations.Find(nameHash);
  179. // Create the shader variation now if not created yet
  180. if (i == variations.End())
  181. {
  182. String path, fileName, extension;
  183. SplitPath(GetName(), path, fileName, extension);
  184. String compiledShaderName = path + subDir_ + fileName;
  185. if (!name.Empty())
  186. compiledShaderName += "_" + name;
  187. compiledShaderName += type == VS ? vsExtension_ : psExtension_;
  188. i = variations.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, type))));
  189. i->second_->SetName(compiledShaderName);
  190. SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
  191. }
  192. return i->second_;
  193. }
  194. else
  195. return 0;
  196. }
  197. bool Shader::PrepareVariation(ShaderVariation* variation)
  198. {
  199. if (!variation)
  200. return false;
  201. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  202. Graphics* graphics = GetSubsystem<Graphics>();
  203. ResourceCache* cache = GetSubsystem<ResourceCache>();
  204. if (!fileSystem || !graphics || !cache)
  205. return false;
  206. String shaderName = variation->GetName();
  207. String variationName = GetFileName(shaderName);
  208. unsigned pos = variationName.Find('_');
  209. if (pos != String::NPOS)
  210. variationName = variationName.Substring(pos + 1);
  211. else
  212. variationName.Clear();
  213. if (!fullFileName_.Empty())
  214. {
  215. if (fileSystem->GetLastModifiedTime(cache->GetResourceFileName(shaderName)) < sourceModifiedTime_)
  216. {
  217. PROFILE(CompileShader);
  218. LOGINFO("Compiling shader " + shaderName);
  219. Vector<String> arguments;
  220. arguments.Push("\"" + fullFileName_ + "\"");
  221. arguments.Push("\"" + GetPath(fullFileName_) + subDir_ + "\"");
  222. arguments.Push(variation->GetShaderType() == VS ? "-tVS" : "-tPS");
  223. arguments.Push("-v" + variationName);
  224. if (graphics->GetSM3Support())
  225. arguments.Push("-dSM3");
  226. if (fileSystem->SystemRun(fileSystem->GetProgramDir() + "ShaderCompiler", arguments) != 0)
  227. {
  228. LOGERROR("Failed to compile shader " + shaderName);
  229. return false;
  230. }
  231. }
  232. }
  233. SharedPtr<File> file(cache->GetFile(shaderName));
  234. if (!file)
  235. return false;
  236. if (file->ReadFileID() != "USHD")
  237. {
  238. LOGERROR(shaderName + " is not a valid shader bytecode file");
  239. return false;
  240. }
  241. /// \todo Check that shader type and model match
  242. unsigned short shaderType = file->ReadUShort();
  243. unsigned short shaderModel = file->ReadUShort();
  244. unsigned numParameters = file->ReadUInt();
  245. for (unsigned i = 0; i < numParameters; ++i)
  246. {
  247. String paramName = file->ReadString();
  248. unsigned reg = file->ReadUByte();
  249. unsigned regCount = file->ReadUByte();
  250. StringHash nameHash(paramName);
  251. ShaderParameter parameter(variation->GetShaderType(), reg, regCount);
  252. variation->AddParameter(nameHash, parameter);
  253. // Register the parameter globally
  254. graphics->RegisterShaderParameter(nameHash, parameter);
  255. }
  256. unsigned numTextureUnits = file->ReadUInt();
  257. for (unsigned i = 0; i < numTextureUnits; ++i)
  258. {
  259. String unitName = file->ReadString();
  260. unsigned sampler = file->ReadUByte();
  261. TextureUnit tuIndex = graphics->GetTextureUnit(unitName);
  262. if (tuIndex < MAX_TEXTURE_UNITS)
  263. variation->AddTextureUnit(tuIndex);
  264. else if (sampler < MAX_TEXTURE_UNITS)
  265. variation->AddTextureUnit((TextureUnit)sampler);
  266. }
  267. unsigned byteCodeSize = file->ReadUInt();
  268. if (byteCodeSize)
  269. {
  270. SharedArrayPtr<unsigned char> byteCode(new unsigned char[byteCodeSize]);
  271. file->Read(byteCode.Get(), byteCodeSize);
  272. variation->SetByteCode(byteCode);
  273. SetMemoryUse(GetMemoryUse() + byteCodeSize);
  274. return true;
  275. }
  276. else
  277. {
  278. LOGERROR("Shader " + shaderName + " has zero length bytecode");
  279. return false;
  280. }
  281. }
  282. }