OGLShader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "Graphics.h"
  27. #include "GraphicsImpl.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. {
  39. }
  40. Shader::~Shader()
  41. {
  42. }
  43. void Shader::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<Shader>();
  46. }
  47. bool Shader::Load(Deserializer& source)
  48. {
  49. PROFILE(LoadShader);
  50. // Clear existing variations
  51. variations_.Clear();
  52. Graphics* graphics = GetSubsystem<Graphics>();
  53. if (!graphics)
  54. return false;
  55. unsigned codeLength = source.GetSize();
  56. shaderCode_.Resize(codeLength);
  57. source.Read(&shaderCode_[0], codeLength);
  58. String xmlName = source.GetName() + ".xml";
  59. XMLFile* file = GetSubsystem<ResourceCache>()->GetResource<XMLFile>(xmlName);
  60. if (!file)
  61. return false;
  62. XMLElement shaderElem = file->GetRootElement();
  63. shaderType_ = (shaderElem.GetString("type") == "vs") ? VS : PS;
  64. XMLElement variationElem = shaderElem.GetChildElement("variation");
  65. while (variationElem)
  66. {
  67. String variationName = variationElem.GetString("name");
  68. StringHash nameHash(variationName);
  69. SharedPtr<ShaderVariation> newVariation(new ShaderVariation(this, shaderType_));
  70. newVariation->SetName(variationName);
  71. newVariation->SetDefines(variationElem.GetString("defines").Split(' '));
  72. variations_[nameHash] = newVariation;
  73. variationElem = variationElem.GetNextElement();
  74. }
  75. return true;
  76. }
  77. ShaderVariation* Shader::GetVariation(const String& name)
  78. {
  79. return GetVariation(StringHash(name));
  80. }
  81. ShaderVariation* Shader::GetVariation(StringHash nameHash)
  82. {
  83. Map<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations_.Find(nameHash);
  84. if (i == variations_.End())
  85. return 0;
  86. ShaderVariation* variation = i->second_;
  87. // Create shader object now if not yet created. If fails, remove the variation
  88. if (!variation->GetGPUObject())
  89. {
  90. LOGDEBUG("Creating variation " + variation->GetName() + " of shader " + GetName());
  91. PROFILE(CreateShaderVariation);
  92. bool success = variation->Create();
  93. if (!success)
  94. {
  95. LOGERROR("Failed to create variation " + variation->GetName() + " of shader " + GetName() + ":\n" +
  96. variation->GetCompilerOutput());
  97. variations_.Erase(i);
  98. return 0;
  99. }
  100. }
  101. return variation;
  102. }