OGLShaderVariation.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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 "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "Shader.h"
  27. #include "ShaderProgram.h"
  28. #include "ShaderVariation.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  33. GPUObject(owner->GetSubsystem<Graphics>()),
  34. shaderType_(type),
  35. sourceCodeLength_(0),
  36. compiled_(false)
  37. {
  38. }
  39. ShaderVariation::~ShaderVariation()
  40. {
  41. Release();
  42. }
  43. void ShaderVariation::OnDeviceLost()
  44. {
  45. GPUObject::OnDeviceLost();
  46. compiled_ = false;
  47. compilerOutput_.Clear();
  48. if (graphics_)
  49. graphics_->CleanupShaderPrograms();
  50. }
  51. void ShaderVariation::Release()
  52. {
  53. if (object_)
  54. {
  55. if (!graphics_)
  56. return;
  57. if (shaderType_ == VS)
  58. {
  59. if (graphics_->GetVertexShader() == this)
  60. graphics_->SetShaders(0, 0);
  61. }
  62. else
  63. {
  64. if (graphics_->GetPixelShader() == this)
  65. graphics_->SetShaders(0, 0);
  66. }
  67. glDeleteShader(object_);
  68. object_ = 0;
  69. compiled_ = false;
  70. compilerOutput_.Clear();
  71. graphics_->CleanupShaderPrograms();
  72. }
  73. }
  74. bool ShaderVariation::Create()
  75. {
  76. Release();
  77. if (!sourceCode_ || !sourceCodeLength_)
  78. return false;
  79. object_ = glCreateShader(shaderType_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  80. if (!object_)
  81. {
  82. compilerOutput_ = "Could not create shader object";
  83. return false;
  84. }
  85. // Prepend the defines to the shader code
  86. // Check if there is a version definition; it must stay in the beginning
  87. String shaderCode(sourceCode_.Get(), sourceCodeLength_);
  88. String defines;
  89. for (unsigned i = 0; i < defines_.Size(); ++i)
  90. defines += "#define " + defines_[i] + " " + defineValues_[i] + "\n";
  91. if (!defines_.Empty())
  92. defines += "\n";
  93. unsigned pos = 0;
  94. if (shaderCode.StartsWith("#version"))
  95. {
  96. pos = shaderCode.Find('\n');
  97. if (pos != String::NPOS)
  98. ++pos;
  99. else
  100. pos = 0;
  101. }
  102. shaderCode.Insert(pos, defines);
  103. const char* shaderCStr = shaderCode.CString();
  104. glShaderSource(object_, 1, &shaderCStr, 0);
  105. glCompileShader(object_);
  106. int compiled, length;
  107. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  108. compiled_ = compiled != 0;
  109. if (!compiled_)
  110. {
  111. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  112. compilerOutput_.Resize(length);
  113. int outLength;
  114. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  115. }
  116. else
  117. compilerOutput_.Clear();
  118. return compiled_;
  119. }
  120. void ShaderVariation::SetName(const String& name)
  121. {
  122. name_ = name;
  123. }
  124. void ShaderVariation::SetSourceCode(const SharedArrayPtr<char>& code, unsigned length)
  125. {
  126. sourceCode_ = code;
  127. sourceCodeLength_ = length;
  128. }
  129. void ShaderVariation::SetDefines(const Vector<String>& defines, const Vector<String>& defineValues)
  130. {
  131. if (defines.Size() == defineValues.Size())
  132. {
  133. defines_ = defines;
  134. defineValues_ = defineValues;
  135. }
  136. }
  137. }