OGLShaderVariation.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Graphics.h"
  24. #include "GraphicsImpl.h"
  25. #include "Shader.h"
  26. #include "ShaderProgram.h"
  27. #include "ShaderVariation.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  32. GPUObject(owner->GetSubsystem<Graphics>()),
  33. shaderType_(type),
  34. sourceCodeLength_(0),
  35. compiled_(false)
  36. {
  37. }
  38. ShaderVariation::~ShaderVariation()
  39. {
  40. Release();
  41. }
  42. void ShaderVariation::OnDeviceLost()
  43. {
  44. GPUObject::OnDeviceLost();
  45. compiled_ = false;
  46. compilerOutput_.Clear();
  47. if (graphics_)
  48. graphics_->CleanupShaderPrograms();
  49. }
  50. void ShaderVariation::Release()
  51. {
  52. if (object_)
  53. {
  54. if (!graphics_)
  55. return;
  56. if (shaderType_ == VS)
  57. {
  58. if (graphics_->GetVertexShader() == this)
  59. graphics_->SetShaders(0, 0);
  60. }
  61. else
  62. {
  63. if (graphics_->GetPixelShader() == this)
  64. graphics_->SetShaders(0, 0);
  65. }
  66. glDeleteShader(object_);
  67. object_ = 0;
  68. compiled_ = false;
  69. compilerOutput_.Clear();
  70. graphics_->CleanupShaderPrograms();
  71. }
  72. }
  73. bool ShaderVariation::Create()
  74. {
  75. Release();
  76. if (!sourceCode_ || !sourceCodeLength_)
  77. return false;
  78. object_ = glCreateShader(shaderType_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  79. if (!object_)
  80. {
  81. compilerOutput_ = "Could not create shader object";
  82. return false;
  83. }
  84. // Prepend the defines to the shader code
  85. // Check if there is a version definition; it must stay in the beginning
  86. String shaderCode(sourceCode_.Get(), sourceCodeLength_);
  87. String defines;
  88. for (unsigned i = 0; i < defines_.Size(); ++i)
  89. defines += "#define " + defines_[i] + " " + defineValues_[i] + "\n";
  90. if (!defines_.Empty())
  91. defines += "\n";
  92. unsigned pos = 0;
  93. if (shaderCode.StartsWith("#version"))
  94. {
  95. pos = shaderCode.Find('\n');
  96. if (pos != String::NPOS)
  97. ++pos;
  98. else
  99. pos = 0;
  100. }
  101. shaderCode.Insert(pos, defines);
  102. const char* shaderCStr = shaderCode.CString();
  103. glShaderSource(object_, 1, &shaderCStr, 0);
  104. glCompileShader(object_);
  105. int compiled, length;
  106. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  107. compiled_ = compiled != 0;
  108. if (!compiled_)
  109. {
  110. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  111. compilerOutput_.Resize(length);
  112. int outLength;
  113. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  114. }
  115. else
  116. compilerOutput_.Clear();
  117. return compiled_;
  118. }
  119. void ShaderVariation::SetName(const String& name)
  120. {
  121. name_ = name;
  122. }
  123. void ShaderVariation::SetSourceCode(const SharedArrayPtr<char>& code, unsigned length)
  124. {
  125. sourceCode_ = code;
  126. sourceCodeLength_ = length;
  127. }
  128. void ShaderVariation::SetDefines(const Vector<String>& defines, const Vector<String>& defineValues)
  129. {
  130. if (defines.Size() == defineValues.Size())
  131. {
  132. defines_ = defines;
  133. defineValues_ = defineValues;
  134. }
  135. }
  136. }