Shader.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "ShaderProgram.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. variations_.Clear();
  58. Graphics* graphics = GetSubsystem<Graphics>();
  59. if (!graphics)
  60. return false;
  61. // Check ID
  62. if (source.ReadID() != "USHD")
  63. {
  64. LOGERROR(source.GetName() + " is not a valid shader file");
  65. return false;
  66. }
  67. shaderType_ = (ShaderType)source.ReadShort();
  68. isSM3_ = (source.ReadShort() == 3);
  69. Vector<Parameter> parameters;
  70. Vector<Parameter> textureUnits;
  71. // Read the parameters and texture units used by this shader; use information is specified in terms of them
  72. unsigned numParameters = source.ReadUInt();
  73. for (unsigned i = 0; i < numParameters; ++i)
  74. {
  75. Parameter newParameter;
  76. newParameter.name_ = source.ReadString();
  77. newParameter.register_ = source.ReadUByte();
  78. parameters.Push(newParameter);
  79. ShaderParameter paramIndex = graphics->GetShaderParameter(newParameter.name_);
  80. if (paramIndex != MAX_SHADER_PARAMETERS)
  81. graphics->SetShaderRegister(paramIndex, newParameter.register_);
  82. else
  83. LOGWARNING("Unknown shader parameter " + newParameter.name_);
  84. }
  85. unsigned numTextureUnits = source.ReadUInt();
  86. for (unsigned i = 0; i < numTextureUnits; ++i)
  87. {
  88. Parameter newTextureUnit;
  89. newTextureUnit.name_ = source.ReadString();
  90. newTextureUnit.register_ = source.ReadUByte();
  91. textureUnits.Push(newTextureUnit);
  92. TextureUnit tuIndex = graphics->GetTextureUnit(newTextureUnit.name_);
  93. if (tuIndex == MAX_TEXTURE_UNITS)
  94. LOGWARNING("Unknown texture unit " + newTextureUnit.name_);
  95. }
  96. // Read the variations
  97. unsigned numVariations = source.ReadUInt();
  98. for (unsigned i = 0; i < numVariations; ++i)
  99. {
  100. SharedPtr<ShaderProgram> variation(new ShaderProgram(this, shaderType_, isSM3_));
  101. variation->SetName(source.ReadString());
  102. // Fill the parameter & texture unit use information
  103. for (unsigned j = 0; j < numParameters; ++j)
  104. {
  105. if (source.ReadBool())
  106. {
  107. ShaderParameter paramIndex = graphics->GetShaderParameter(parameters[j].name_);
  108. if (paramIndex != MAX_SHADER_PARAMETERS)
  109. variation->SetUseParameter(paramIndex, true);
  110. }
  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.GetPtr(), dataSize);
  127. variation->SetByteCode(byteCode);
  128. }
  129. // Store the variation
  130. variations_[StringHash(variation->GetName())] = variation;
  131. }
  132. // This is not exactly accurate, but a reasonable estimate
  133. SetMemoryUse(source.GetSize());
  134. return true;
  135. }
  136. ShaderProgram* Shader::GetVariation(const String& name)
  137. {
  138. return GetVariation(StringHash(name));
  139. }
  140. ShaderProgram* Shader::GetVariation(StringHash nameHash)
  141. {
  142. Map<StringHash, SharedPtr<ShaderProgram> >::Iterator i = variations_.Find(nameHash);
  143. if (i == variations_.End())
  144. return 0;
  145. ShaderProgram* variation = i->second_;
  146. // Create shader object now if not yet created. If fails, remove the variation
  147. if (!variation->GetGPUObject())
  148. {
  149. PROFILE(CreateShaderProgram);
  150. bool success = variation->Create();
  151. if (!success)
  152. {
  153. LOGERROR("Failed to create variation " + variation->GetName() + " of shader " + GetName());
  154. variations_.Erase(i);
  155. return 0;
  156. }
  157. }
  158. return variation;
  159. }