OGLShader.cpp 3.4 KB

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