D3D9Shader.cpp 5.6 KB

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