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