D3D9Shader.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "GraphicsImpl.h"
  29. #include "Log.h"
  30. #include "Profiler.h"
  31. #include "Shader.h"
  32. #include "ShaderVariation.h"
  33. /// Shader constant (uniform or sampler) structure for loading
  34. struct Constant
  35. {
  36. /// Parameter name
  37. String name_;
  38. /// Constant register or sampler
  39. unsigned register_;
  40. };
  41. OBJECTTYPESTATIC(Shader);
  42. Shader::Shader(Context* context) :
  43. Resource(context),
  44. shaderType_(VS),
  45. isSM3_(false)
  46. {
  47. }
  48. Shader::~Shader()
  49. {
  50. }
  51. void Shader::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<Shader>();
  54. }
  55. bool Shader::Load(Deserializer& source)
  56. {
  57. PROFILE(LoadShader);
  58. // Clear existing variations
  59. variations_.Clear();
  60. Graphics* graphics = GetSubsystem<Graphics>();
  61. if (!graphics)
  62. return false;
  63. // Check ID
  64. if (source.ReadID() != "USHD")
  65. {
  66. LOGERROR(source.GetName() + " is not a valid shader file");
  67. return false;
  68. }
  69. String fileName = GetFileName(source.GetName());
  70. shaderType_ = (ShaderType)source.ReadShort();
  71. isSM3_ = (source.ReadShort() == 3);
  72. Vector<Constant> parameters;
  73. Vector<Constant> textureUnits;
  74. // Read the parameters and texture units used by this shader; use information is specified in terms of them
  75. unsigned numParameters = source.ReadUInt();
  76. for (unsigned i = 0; i < numParameters; ++i)
  77. {
  78. Constant newParameter;
  79. newParameter.name_ = source.ReadString();
  80. newParameter.register_ = source.ReadUByte();
  81. parameters.Push(newParameter);
  82. graphics->DefineShaderParameter(StringHash(newParameter.name_), shaderType_, newParameter.register_);
  83. }
  84. unsigned numTextureUnits = source.ReadUInt();
  85. for (unsigned i = 0; i < numTextureUnits; ++i)
  86. {
  87. Constant newTextureUnit;
  88. newTextureUnit.name_ = source.ReadString();
  89. newTextureUnit.register_ = source.ReadUByte();
  90. textureUnits.Push(newTextureUnit);
  91. TextureUnit tuIndex = graphics->GetTextureUnit(newTextureUnit.name_);
  92. if (tuIndex == MAX_TEXTURE_UNITS)
  93. LOGWARNING("Unknown texture unit " + newTextureUnit.name_);
  94. }
  95. // Read the variations
  96. unsigned numVariations = source.ReadUInt();
  97. for (unsigned i = 0; i < numVariations; ++i)
  98. {
  99. SharedPtr<ShaderVariation> variation(new ShaderVariation(this, shaderType_, isSM3_));
  100. String variationName = source.ReadString();
  101. StringHash nameHash(variationName);
  102. if (!variationName.Empty())
  103. variation->SetName(fileName + "_" + variationName);
  104. else
  105. variation->SetName(fileName);
  106. // Fill the parameter & texture unit use information
  107. for (unsigned j = 0; j < numParameters; ++j)
  108. {
  109. if (source.ReadBool())
  110. variation->SetUseParameter(StringHash(parameters[j].name_), true);
  111. }
  112. for (unsigned j = 0; j < numTextureUnits; ++j)
  113. {
  114. if (source.ReadBool())
  115. {
  116. TextureUnit tuIndex = graphics->GetTextureUnit(textureUnits[j].name_);
  117. if (tuIndex != MAX_TEXTURE_UNITS)
  118. variation->SetUseTextureUnit(tuIndex, true);
  119. }
  120. }
  121. // Read the bytecode
  122. unsigned dataSize = source.ReadUInt();
  123. if (dataSize)
  124. {
  125. SharedArrayPtr<unsigned char> byteCode(new unsigned char[dataSize]);
  126. source.Read(byteCode.RawPtr(), dataSize);
  127. variation->SetByteCode(byteCode);
  128. }
  129. // Store the variation
  130. variations_[nameHash] = variation;
  131. }
  132. // This is not exactly accurate, but a reasonable estimate
  133. SetMemoryUse(source.GetSize());
  134. return true;
  135. }
  136. ShaderVariation* Shader::GetVariation(const String& name)
  137. {
  138. return GetVariation(StringHash(name));
  139. }
  140. ShaderVariation* Shader::GetVariation(StringHash nameHash)
  141. {
  142. Map<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations_.Find(nameHash);
  143. if (i != variations_.End())
  144. return i->second_;
  145. else
  146. return 0;
  147. }