ShaderVariation.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ShaderVariation.h"
  28. #include <ctype.h>
  29. #include "DebugNew.h"
  30. ShaderVariation::ShaderVariation(Shader* shader, ShaderType type, bool isSM3) :
  31. GPUObject(shader->GetSubsystem<Graphics>()),
  32. shader_(shader),
  33. shaderType_(type),
  34. isSM3_(isSM3)
  35. {
  36. ClearParameters();
  37. }
  38. ShaderVariation::~ShaderVariation()
  39. {
  40. Release();
  41. }
  42. bool ShaderVariation::Create()
  43. {
  44. Release();
  45. if ((!graphics_) || (!byteCode_))
  46. return false;
  47. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  48. if (shaderType_ == VS)
  49. {
  50. if ((!device) || (FAILED(device->CreateVertexShader(
  51. (const DWORD*)byteCode_.GetPtr(),
  52. (IDirect3DVertexShader9**)&object_))))
  53. return false;
  54. }
  55. else
  56. {
  57. if ((!device) || (FAILED(device->CreatePixelShader(
  58. (const DWORD*)byteCode_.GetPtr(),
  59. (IDirect3DPixelShader9**)&object_))))
  60. return false;
  61. }
  62. return true;
  63. }
  64. void ShaderVariation::Release()
  65. {
  66. if (object_)
  67. {
  68. if (!graphics_)
  69. return;
  70. if (shaderType_ == VS)
  71. {
  72. if (graphics_->GetPixelShader() == this)
  73. graphics_->SetShaders(0, 0);
  74. ((IDirect3DVertexShader9*)object_)->Release();
  75. }
  76. else
  77. {
  78. if (graphics_->GetVertexShader() == this)
  79. graphics_->SetShaders(0, 0);
  80. ((IDirect3DPixelShader9*)object_)->Release();
  81. }
  82. object_ = 0;
  83. }
  84. }
  85. void ShaderVariation::SetName(const String& name)
  86. {
  87. name_ = name;
  88. }
  89. void ShaderVariation::SetByteCode(const SharedArrayPtr<unsigned char>& byteCode)
  90. {
  91. byteCode_ = byteCode;
  92. // Recreate object if already created from previous bytecode
  93. if (object_)
  94. Create();
  95. }
  96. void ShaderVariation::SetUseParameter(ShaderParameter param, bool enable)
  97. {
  98. useParameter_[param] = enable;
  99. }
  100. void ShaderVariation::SetUseTextureUnit(TextureUnit unit, bool enable)
  101. {
  102. useTextureUnit_[unit] = enable;
  103. }
  104. void ShaderVariation::ClearParameters()
  105. {
  106. for (unsigned i = 0; i < MAX_SHADER_PARAMETERS; ++i)
  107. useParameter_[i] = false;
  108. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  109. useTextureUnit_[i] = false;
  110. }
  111. Shader* ShaderVariation::GetShader() const
  112. {
  113. return shader_;
  114. }