D3D9Shader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. OBJECTTYPESTATIC(Shader);
  34. Shader::Shader(Context* context) :
  35. Resource(context),
  36. shaderType_(VS),
  37. isSM3_(false)
  38. {
  39. }
  40. Shader::~Shader()
  41. {
  42. }
  43. void Shader::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<Shader>();
  46. }
  47. bool Shader::Load(Deserializer& source)
  48. {
  49. PROFILE(LoadShader);
  50. // Clear existing variations
  51. variations_.Clear();
  52. Graphics* graphics = GetSubsystem<Graphics>();
  53. if (!graphics)
  54. return false;
  55. // Check ID
  56. if (source.ReadFileID() != "USHD")
  57. {
  58. LOGERROR(source.GetName() + " is not a valid shader file");
  59. return false;
  60. }
  61. unsigned memoryUse = sizeof(Shader);
  62. String fileName = GetFileName(source.GetName());
  63. shaderType_ = (ShaderType)source.ReadShort();
  64. isSM3_ = (source.ReadShort() == 3);
  65. // Read the parameter & texture unit hash-to-string mappings
  66. HashMap<StringHash, String> parameterNames;
  67. HashMap<StringHash, String> textureUnitNames;
  68. unsigned numParameterNames = source.ReadUInt();
  69. for (unsigned i = 0; i < numParameterNames; ++i)
  70. {
  71. String name = source.ReadString();
  72. parameterNames[StringHash(name)] = name;
  73. }
  74. unsigned numTextureUnitNames = source.ReadUInt();
  75. for (unsigned i = 0; i < numTextureUnitNames; ++i)
  76. {
  77. String name = source.ReadString();
  78. textureUnitNames[StringHash(name)] = name;
  79. }
  80. // Read the variations
  81. unsigned numVariations = source.ReadUInt();
  82. for (unsigned i = 0; i < numVariations; ++i)
  83. {
  84. SharedPtr<ShaderVariation> variation(new ShaderVariation(this, shaderType_, isSM3_));
  85. String variationName = source.ReadString();
  86. StringHash nameHash(variationName);
  87. if (!variationName.Empty())
  88. variation->SetName(fileName + "_" + variationName);
  89. else
  90. variation->SetName(fileName);
  91. // Read the parameters & texture units
  92. unsigned numParameters = source.ReadInt();
  93. for (unsigned j = 0; j < numParameters; ++j)
  94. {
  95. StringHash param = source.ReadStringHash();
  96. unsigned reg = source.ReadUByte();
  97. unsigned regCount = source.ReadUByte();
  98. ShaderParameter newParameter(shaderType_, reg, regCount);
  99. variation->AddParameter(param, newParameter);
  100. // Remember the parameter globally. The parameter is only stored the first time; it can have different register
  101. // index in different shaders, but must have same shader type (either vertex or pixel) and register count
  102. graphics->RegisterShaderParameter(param, newParameter);
  103. }
  104. unsigned numTextureUnits = source.ReadInt();
  105. for (unsigned j = 0; j < numTextureUnits; ++j)
  106. {
  107. StringHash unit = source.ReadStringHash();
  108. String unitName = textureUnitNames[unit];
  109. unsigned sampler = source.ReadUByte();
  110. TextureUnit tuIndex = graphics->GetTextureUnit(unitName);
  111. if (tuIndex != MAX_TEXTURE_UNITS)
  112. variation->AddTextureUnit(tuIndex);
  113. else
  114. LOGWARNING("Unknown texture unit " + unitName);
  115. }
  116. variation->OptimizeParameters();
  117. // Read the bytecode
  118. unsigned dataSize = source.ReadUInt();
  119. if (dataSize)
  120. {
  121. SharedArrayPtr<unsigned char> byteCode(new unsigned char[dataSize]);
  122. source.Read(byteCode.Get(), dataSize);
  123. variation->SetByteCode(byteCode);
  124. }
  125. // Store the variation
  126. if (variations_.Contains(nameHash))
  127. LOGERROR("Shader variation name hash collision: " + variationName);
  128. variations_[nameHash] = variation;
  129. memoryUse += sizeof(ShaderVariation) + dataSize;
  130. }
  131. SetMemoryUse(memoryUse);
  132. return true;
  133. }
  134. ShaderVariation* Shader::GetVariation(const String& name)
  135. {
  136. StringHash nameHash(name);
  137. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations_.Find(nameHash);
  138. if (i != variations_.End())
  139. return i->second_;
  140. else
  141. return 0;
  142. }