D3D9ShaderVariation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. namespace Urho3D
  31. {
  32. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  33. GPUObject(owner->GetSubsystem<Graphics>()),
  34. owner_(owner),
  35. shaderType_(type),
  36. failed_(false)
  37. {
  38. ClearParameters();
  39. }
  40. ShaderVariation::~ShaderVariation()
  41. {
  42. Release();
  43. }
  44. bool ShaderVariation::Create()
  45. {
  46. Release();
  47. if (!graphics_)
  48. return false;
  49. // Load bytecode and/or compile shader if necessary
  50. if (!byteCode_ && owner_)
  51. owner_->PrepareVariation(this);
  52. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  53. if (shaderType_ == VS)
  54. {
  55. if (!byteCode_ || !device || FAILED(device->CreateVertexShader(
  56. (const DWORD*)byteCode_.Get(),
  57. (IDirect3DVertexShader9**)&object_)))
  58. failed_ = true;
  59. }
  60. else
  61. {
  62. if (!byteCode_ || !device || FAILED(device->CreatePixelShader(
  63. (const DWORD*)byteCode_.Get(),
  64. (IDirect3DPixelShader9**)&object_)))
  65. failed_ = true;
  66. }
  67. return !failed_;
  68. }
  69. void ShaderVariation::Release()
  70. {
  71. if (object_)
  72. {
  73. if (!graphics_)
  74. return;
  75. if (shaderType_ == VS)
  76. {
  77. if (graphics_->GetVertexShader() == this)
  78. graphics_->SetShaders(0, 0);
  79. ((IDirect3DVertexShader9*)object_)->Release();
  80. }
  81. else
  82. {
  83. if (graphics_->GetPixelShader() == this)
  84. graphics_->SetShaders(0, 0);
  85. ((IDirect3DPixelShader9*)object_)->Release();
  86. }
  87. object_ = 0;
  88. }
  89. failed_ = false;
  90. }
  91. void ShaderVariation::SetName(const String& name)
  92. {
  93. name_ = name;
  94. }
  95. void ShaderVariation::SetByteCode(const SharedArrayPtr<unsigned char>& byteCode)
  96. {
  97. byteCode_ = byteCode;
  98. }
  99. void ShaderVariation::AddParameter(StringHash param, const ShaderParameter& definition)
  100. {
  101. parameters_[param] = definition;
  102. }
  103. void ShaderVariation::AddTextureUnit(TextureUnit unit)
  104. {
  105. useTextureUnit_[unit] = true;
  106. }
  107. void ShaderVariation::ClearParameters()
  108. {
  109. parameters_.Clear();
  110. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  111. useTextureUnit_[i] = false;
  112. }
  113. void ShaderVariation::OptimizeParameters()
  114. {
  115. parameters_.Rehash(NextPowerOfTwo(parameters_.Size()));
  116. }
  117. bool ShaderVariation::IsCreated() const
  118. {
  119. return object_ != 0;
  120. }
  121. }