D3D9Shader.cpp 6.1 KB

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