OGLShaderVariation.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "Shader.h"
  27. #include "ShaderProgram.h"
  28. #include "ShaderVariation.h"
  29. #include <GLee.h>
  30. #include "DebugNew.h"
  31. ShaderVariation::ShaderVariation(Shader* shader, ShaderType type) :
  32. GPUObject(shader->GetSubsystem<Graphics>()),
  33. shader_(shader),
  34. shaderType_(type),
  35. compiled_(false)
  36. {
  37. }
  38. ShaderVariation::~ShaderVariation()
  39. {
  40. Release();
  41. }
  42. void ShaderVariation::Release()
  43. {
  44. if (object_)
  45. {
  46. if (!graphics_)
  47. return;
  48. if (shaderType_ == VS)
  49. {
  50. if (graphics_->GetPixelShader() == this)
  51. graphics_->SetShaders(0, 0);
  52. }
  53. else
  54. {
  55. if (graphics_->GetVertexShader() == this)
  56. graphics_->SetShaders(0, 0);
  57. }
  58. // Release the linked shader program if exists
  59. if (shaderProgram_)
  60. {
  61. shaderProgram_->Release();
  62. shaderProgram_.Reset();
  63. graphics_->CleanupShaderPrograms();
  64. }
  65. glDeleteShader(object_);
  66. object_ = 0;
  67. compiled_ = false;
  68. }
  69. }
  70. bool ShaderVariation::Create()
  71. {
  72. Release();
  73. if (!shader_)
  74. return false;
  75. object_ = glCreateShader((shaderType_ == VS) ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  76. if (!object_)
  77. {
  78. compilerOutput_ = "Could not create shader object";
  79. return false;
  80. }
  81. // Prepend the defines to the shader code
  82. String shaderCode;
  83. for (unsigned i = 0; i < defines_.Size(); ++i)
  84. shaderCode += "#define " + defines_[i] + "\n";
  85. shaderCode += shader_->GetShaderCode();
  86. const char* shaderCStr = shaderCode.CString();
  87. glShaderSource(object_, 1, &shaderCStr, 0);
  88. glCompileShader(object_);
  89. int compiled, length;
  90. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  91. compiled_ = compiled != 0;
  92. if (!compiled_)
  93. {
  94. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  95. compilerOutput_.Resize(length);
  96. glGetShaderInfoLog(object_, length, &length, &compilerOutput_[0]);
  97. }
  98. else
  99. compilerOutput_.Clear();
  100. return compiled_;
  101. }
  102. void ShaderVariation::SetName(const String& name)
  103. {
  104. name_ = name;
  105. }
  106. void ShaderVariation::SetDefines(const Vector<String>& defines)
  107. {
  108. defines_ = defines;
  109. }
  110. Shader* ShaderVariation::GetShader() const
  111. {
  112. return shader_;
  113. }