OGLShader.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "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. OBJECTTYPESTATIC(Shader);
  35. Shader::Shader(Context* context) :
  36. Resource(context),
  37. shaderType_(VS),
  38. sourceCodeLength_(0)
  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. unsigned memoryUse = sizeof(Shader);
  57. sourceCodeLength_ = source.GetSize();
  58. sourceCode_ = new char[sourceCodeLength_];
  59. source.Read(&sourceCode_[0], sourceCodeLength_);
  60. memoryUse += sourceCodeLength_;
  61. String fileName = GetFileName(source.GetName());
  62. String xmlName = source.GetName() + ".xml";
  63. XMLFile* file = GetSubsystem<ResourceCache>()->GetResource<XMLFile>(xmlName);
  64. if (!file)
  65. return false;
  66. XMLElement shaderElem = file->GetRoot();
  67. shaderType_ = shaderElem.GetString("type") == "vs" ? VS : PS;
  68. XMLElement variationElem = shaderElem.GetChild("variation");
  69. while (variationElem)
  70. {
  71. String variationName = variationElem.GetString("name");
  72. StringHash nameHash(variationName);
  73. SharedPtr<ShaderVariation> newVariation(new ShaderVariation(this, shaderType_));
  74. if (!variationName.Empty())
  75. newVariation->SetName(fileName + "_" + variationName);
  76. else
  77. newVariation->SetName(fileName);
  78. newVariation->SetSourceCode(sourceCode_, sourceCodeLength_);
  79. newVariation->SetDefines(variationElem.GetString("defines").Split(' '));
  80. if (variations_.Contains(nameHash))
  81. LOGERROR("Shader variation name hash collision: " + variationName);
  82. variations_[nameHash] = newVariation;
  83. variationElem = variationElem.GetNext();
  84. memoryUse += sizeof(ShaderVariation);
  85. }
  86. SetMemoryUse(memoryUse);
  87. return true;
  88. }
  89. ShaderVariation* Shader::GetVariation(const String& name)
  90. {
  91. StringHash nameHash(name);
  92. HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations_.Find(nameHash);
  93. if (i != variations_.End())
  94. return i->second_;
  95. else
  96. return 0;
  97. }