OGLShader.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "Log.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Shader.h"
  32. #include "ShaderVariation.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. OBJECTTYPESTATIC(Shader);
  36. Shader::Shader(Context* context) :
  37. Resource(context),
  38. shaderType_(VS),
  39. sourceCodeLength_(0)
  40. {
  41. }
  42. Shader::~Shader()
  43. {
  44. }
  45. void Shader::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Shader>();
  48. }
  49. bool Shader::Load(Deserializer& source)
  50. {
  51. PROFILE(LoadShader);
  52. // Clear existing variations
  53. variations_.Clear();
  54. Graphics* graphics = GetSubsystem<Graphics>();
  55. if (!graphics)
  56. return false;
  57. unsigned memoryUse = sizeof(Shader);
  58. sourceCodeLength_ = source.GetSize();
  59. sourceCode_ = new char[sourceCodeLength_];
  60. source.Read(&sourceCode_[0], sourceCodeLength_);
  61. memoryUse += sourceCodeLength_;
  62. String fileName = GetFileName(source.GetName());
  63. String xmlName = source.GetName() + ".xml";
  64. XMLFile* file = GetSubsystem<ResourceCache>()->GetResource<XMLFile>(xmlName);
  65. if (!file)
  66. return false;
  67. XMLElement shaderElem = file->GetRoot();
  68. shaderType_ = String(shaderElem.GetAttribute("type")) == "vs" ? VS : PS;
  69. XMLElement variationElem = shaderElem.GetChild("variation");
  70. while (variationElem)
  71. {
  72. String variationName = variationElem.GetAttribute("name");
  73. StringHash nameHash(variationName);
  74. SharedPtr<ShaderVariation> newVariation(new ShaderVariation(this, shaderType_));
  75. if (!variationName.Empty())
  76. newVariation->SetName(fileName + "_" + variationName);
  77. else
  78. newVariation->SetName(fileName);
  79. newVariation->SetSourceCode(sourceCode_, sourceCodeLength_);
  80. newVariation->SetDefines(String(variationElem.GetAttribute("defines")).Split(' '));
  81. if (variations_.Contains(nameHash))
  82. LOGERROR("Shader variation name hash collision: " + variationName);
  83. variations_[nameHash] = newVariation;
  84. variationElem = variationElem.GetNext();
  85. memoryUse += sizeof(ShaderVariation);
  86. }
  87. SetMemoryUse(memoryUse);
  88. return true;
  89. }
  90. ShaderVariation* Shader::GetVariation(const String& name)
  91. {
  92. StringHash nameHash(name);
  93. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations_.Find(nameHash);
  94. if (i != variations_.End())
  95. return i->second_;
  96. else
  97. return 0;
  98. }