D3D11ShaderProgram.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../../Container/HashMap.h"
  5. #include "../../Graphics/Graphics.h"
  6. #include "../../GraphicsAPI/ConstantBuffer.h"
  7. #include "../../GraphicsAPI/ShaderVariation.h"
  8. namespace Urho3D
  9. {
  10. /// Combined information for specific vertex and pixel shaders.
  11. class URHO3D_API ShaderProgram_D3D11 : public RefCounted
  12. {
  13. public:
  14. /// Construct.
  15. ShaderProgram_D3D11(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader)
  16. {
  17. // Create needed constant buffers
  18. const unsigned* vsBufferSizes = vertexShader->GetConstantBufferSizes();
  19. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  20. {
  21. if (vsBufferSizes[i])
  22. vsConstantBuffers_[i] = graphics->GetOrCreateConstantBuffer(VS, i, vsBufferSizes[i]);
  23. }
  24. const unsigned* psBufferSizes = pixelShader->GetConstantBufferSizes();
  25. for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
  26. {
  27. if (psBufferSizes[i])
  28. psConstantBuffers_[i] = graphics->GetOrCreateConstantBuffer(PS, i, psBufferSizes[i]);
  29. }
  30. // Copy parameters, add direct links to constant buffers
  31. const HashMap<StringHash, ShaderParameter>& vsParams = vertexShader->GetParameters();
  32. for (HashMap<StringHash, ShaderParameter>::ConstIterator i = vsParams.Begin(); i != vsParams.End(); ++i)
  33. {
  34. parameters_[i->first_] = i->second_;
  35. parameters_[i->first_].bufferPtr_ = vsConstantBuffers_[i->second_.buffer_].Get();
  36. }
  37. const HashMap<StringHash, ShaderParameter>& psParams = pixelShader->GetParameters();
  38. for (HashMap<StringHash, ShaderParameter>::ConstIterator i = psParams.Begin(); i != psParams.End(); ++i)
  39. {
  40. parameters_[i->first_] = i->second_;
  41. parameters_[i->first_].bufferPtr_ = psConstantBuffers_[i->second_.buffer_].Get();
  42. }
  43. // Optimize shader parameter lookup by rehashing to next power of two
  44. parameters_.Rehash(NextPowerOfTwo(parameters_.Size()));
  45. }
  46. /// Destruct.
  47. virtual ~ShaderProgram_D3D11() override
  48. {
  49. }
  50. /// Combined parameters from the vertex and pixel shader.
  51. HashMap<StringHash, ShaderParameter> parameters_;
  52. /// Vertex shader constant buffers.
  53. SharedPtr<ConstantBuffer> vsConstantBuffers_[MAX_SHADER_PARAMETER_GROUPS];
  54. /// Pixel shader constant buffers.
  55. SharedPtr<ConstantBuffer> psConstantBuffers_[MAX_SHADER_PARAMETER_GROUPS];
  56. };
  57. }