VertexShader.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "SharedArrayPtr.h"
  32. #include "StringUtils.h"
  33. #include "VertexShader.h"
  34. #include "XMLFile.h"
  35. #include <ctype.h>
  36. #include "DebugNew.h"
  37. OBJECTTYPESTATIC(VertexShader);
  38. VertexShader::VertexShader(Context* context) :
  39. Resource(context),
  40. GPUObject(GetSubsystem<Graphics>()),
  41. isSM3_(false)
  42. {
  43. for (unsigned i = 0; i < MAX_VS_PARAMETERS; ++i)
  44. useParameter_[i] = false;
  45. }
  46. VertexShader::~VertexShader()
  47. {
  48. Release();
  49. }
  50. void VertexShader::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<VertexShader>();
  53. }
  54. bool VertexShader::Load(Deserializer& source)
  55. {
  56. PROFILE(LoadVertexShader);
  57. Release();
  58. if (!graphics_)
  59. return false;
  60. unsigned dataSize = source.GetSize();
  61. SetMemoryUse(dataSize);
  62. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  63. source.Read((void*)buffer.GetPtr(), dataSize);
  64. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  65. if ((!device) || (FAILED(graphics_->GetImpl()->GetDevice()->CreateVertexShader(
  66. (const DWORD*)buffer.GetPtr(),
  67. (IDirect3DVertexShader9**)&object_))))
  68. {
  69. LOGERROR("Could not create vertex shader " + GetName());
  70. return false;
  71. }
  72. LoadParameters();
  73. return true;
  74. }
  75. bool VertexShader::NeedParameterUpdate(VSParameter parameter, const void* source)
  76. {
  77. if ((useParameter_[parameter]) && (graphics_->GetVSParameterSource(parameter) != source))
  78. {
  79. graphics_->SetVSParameterSource(parameter, source);
  80. return true;
  81. }
  82. return false;
  83. }
  84. void VertexShader::Release()
  85. {
  86. if (object_)
  87. {
  88. if (!graphics_)
  89. return;
  90. if (graphics_->GetVertexShader() == this)
  91. graphics_->SetShaders(0, 0);
  92. ((IDirect3DVertexShader9*)object_)->Release();
  93. object_ = 0;
  94. }
  95. }
  96. void VertexShader::LoadParameters()
  97. {
  98. ResourceCache* cache = GetSubsystem<ResourceCache>();
  99. if ((!cache) || (!graphics_))
  100. return;
  101. std::string shaderPath;
  102. std::string shaderName;
  103. std::string shaderExt;
  104. SplitPath(GetName(), shaderPath, shaderName, shaderExt);
  105. isSM3_ = (shaderExt.find('3') != std::string::npos);
  106. // Take the first part of the shader name (shader group)
  107. size_t index = 2;
  108. while ((index < shaderName.length()) && (shaderName[index] != '_'))
  109. index++;
  110. std::string shaderGroup = shaderName.substr(0, index);
  111. // Load shader information XML file
  112. XMLFile* file = cache->GetResource<XMLFile>(shaderPath + shaderGroup + ".xml");
  113. if (!file)
  114. return;
  115. // Update (global) parameter register mappings
  116. XMLElement shadersElem = file->GetRootElement();
  117. XMLElement paramsElem = shadersElem.GetChildElement("vsparameters");
  118. XMLElement paramElem = paramsElem.GetChildElement("parameter");
  119. while (paramElem)
  120. {
  121. std::string name = paramElem.GetString("name");
  122. VSParameter param = graphics_->GetVSParameter(name);
  123. if (param != MAX_VS_PARAMETERS)
  124. graphics_->SetVSRegister(param, paramElem.GetInt("index"));
  125. else
  126. LOGERROR("Unknown vertex shader parameter " + name + " in " + GetName());
  127. paramElem = paramElem.GetNextElement("parameter");
  128. }
  129. // Get parameters used by this shader
  130. XMLElement shaderElem = shadersElem.GetChildElement("shader");
  131. while (shaderElem)
  132. {
  133. std::string name = shaderElem.GetString("name");
  134. std::string type = shaderElem.GetStringLower("type");
  135. if ((name == shaderName) && (type == "vs"))
  136. {
  137. for (unsigned i = 0; i < MAX_VS_PARAMETERS; ++i)
  138. useParameter_[i] = false;
  139. XMLElement shaderParamElem = shaderElem.GetChildElement("parameter");
  140. while (shaderParamElem)
  141. {
  142. std::string name = shaderParamElem.GetString("name");
  143. VSParameter param = graphics_->GetVSParameter(name);
  144. if (param != MAX_VS_PARAMETERS)
  145. useParameter_[param] = true;
  146. shaderParamElem = shaderParamElem.GetNextElement("parameter");
  147. }
  148. return;
  149. }
  150. shaderElem = shaderElem.GetNextElement("shader");
  151. }
  152. LOGERROR("Shader " + shaderName + " not found in shader description XML file");
  153. }