D3D9ShaderVariation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type, bool isSM3) :
  31. GPUObject(owner->GetSubsystem<Graphics>()),
  32. shaderType_(type),
  33. isSM3_(isSM3),
  34. failed_(false)
  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_.Get(),
  52. (IDirect3DVertexShader9**)&object_)))
  53. failed_ = true;
  54. }
  55. else
  56. {
  57. if (!device || FAILED(device->CreatePixelShader(
  58. (const DWORD*)byteCode_.Get(),
  59. (IDirect3DPixelShader9**)&object_)))
  60. failed_ = true;
  61. }
  62. return !failed_;
  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. failed_ = false;
  85. }
  86. void ShaderVariation::SetName(const String& name)
  87. {
  88. name_ = name;
  89. }
  90. void ShaderVariation::SetByteCode(const SharedArrayPtr<unsigned char>& byteCode)
  91. {
  92. byteCode_ = byteCode;
  93. // Recreate object if already created from previous bytecode
  94. if (object_)
  95. Create();
  96. }
  97. void ShaderVariation::AddParameter(StringHash param, const ShaderParameter& definition)
  98. {
  99. parameters_[param] = definition;
  100. }
  101. void ShaderVariation::AddTextureUnit(TextureUnit unit)
  102. {
  103. useTextureUnit_[unit] = true;
  104. }
  105. void ShaderVariation::ClearParameters()
  106. {
  107. parameters_.Clear();
  108. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  109. useTextureUnit_[i] = false;
  110. }
  111. void ShaderVariation::OptimizeParameters()
  112. {
  113. parameters_.Rehash(NextPowerOfTwo(parameters_.Size()));
  114. }
  115. bool ShaderVariation::IsCreated() const
  116. {
  117. return object_ != 0;
  118. }