D3D9ShaderVariation.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "GPUObject.h"
  24. #include "GraphicsDefs.h"
  25. #include "HashSet.h"
  26. #include "RefCounted.h"
  27. #include "ArrayPtr.h"
  28. namespace Urho3D
  29. {
  30. class Shader;
  31. /// %Shader parameter definition.
  32. struct ShaderParameter
  33. {
  34. /// Construct with defaults.
  35. ShaderParameter() :
  36. type_(VS),
  37. register_(M_MAX_UNSIGNED),
  38. regCount_(0)
  39. {
  40. }
  41. /// Construct with parameters.
  42. ShaderParameter(ShaderType type, unsigned reg, unsigned regCount) :
  43. type_(type),
  44. register_(reg),
  45. regCount_(regCount)
  46. {
  47. }
  48. /// %Shader type.
  49. ShaderType type_;
  50. /// Hardware register.
  51. unsigned register_;
  52. /// Number of registers.
  53. unsigned regCount_;
  54. };
  55. /// Vertex or pixel shader on the GPU.
  56. class URHO3D_API ShaderVariation : public RefCounted, public GPUObject
  57. {
  58. public:
  59. /// Construct.
  60. ShaderVariation(Shader* owner, ShaderType type);
  61. /// Destruct.
  62. virtual ~ShaderVariation();
  63. /// Create the shader program. Return true if successful.
  64. bool Create();
  65. /// Release shader.
  66. virtual void Release();
  67. /// Set name.
  68. void SetName(const String& name);
  69. /// Set bytecode.
  70. void SetByteCode(const SharedArrayPtr<unsigned char>& byteCode);
  71. /// Add a parameter.
  72. void AddParameter(StringHash param, const ShaderParameter& definition);
  73. /// Add a texture unit.
  74. void AddTextureUnit(TextureUnit unit);
  75. /// Clear parameters and texture unit use flags.
  76. void ClearParameters();
  77. /// Optimize the parameter map for optimal query speed
  78. void OptimizeParameters();
  79. /// Return shader type.
  80. ShaderType GetShaderType() const { return shaderType_; }
  81. /// Return full shader name.
  82. const String& GetName() const { return name_; }
  83. /// Return parent shader resource.
  84. Shader* GetOwner() const;
  85. /// Return whether created successfully.
  86. bool IsCreated() const;
  87. /// Return whether compile failed.
  88. bool IsFailed() const { return failed_; }
  89. /// Return whether uses a parameter.
  90. bool HasParameter(StringHash param) const { return parameters_.Contains(param); }
  91. /// Return whether uses a texture unit (only for pixel shaders.)
  92. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  93. /// Return all parameter definitions.
  94. const HashMap<StringHash, ShaderParameter>& GetParameters() const { return parameters_; }
  95. private:
  96. /// Parent shader resource.
  97. WeakPtr<Shader> owner_;
  98. /// Shader type.
  99. ShaderType shaderType_;
  100. /// Full shader name.
  101. String name_;
  102. /// Shader bytecode.
  103. SharedArrayPtr<unsigned char> byteCode_;
  104. /// Compile failed flag.
  105. bool failed_;
  106. /// Shader parameters.
  107. HashMap<StringHash, ShaderParameter> parameters_;
  108. /// Texture unit use flags.
  109. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  110. };
  111. }