D3D9ShaderVariation.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #pragma once
  24. #include "GPUObject.h"
  25. #include "GraphicsDefs.h"
  26. #include "HashSet.h"
  27. #include "RefCounted.h"
  28. #include "ArrayPtr.h"
  29. class Shader;
  30. /// %Shader parameter definition.
  31. struct ShaderParameter
  32. {
  33. /// Construct with defaults.
  34. ShaderParameter() :
  35. type_(VS),
  36. register_(M_MAX_UNSIGNED),
  37. regCount_(0),
  38. lastSource_((void*)M_MAX_UNSIGNED)
  39. {
  40. }
  41. /// Construct with parameters.
  42. ShaderParameter(ShaderType type, unsigned reg, unsigned regCount) :
  43. type_(type),
  44. register_(reg),
  45. regCount_(regCount),
  46. lastSource_((void*)M_MAX_UNSIGNED)
  47. {
  48. }
  49. /// %Shader type.
  50. ShaderType type_;
  51. /// Hardware register.
  52. unsigned register_;
  53. /// Number of registers.
  54. unsigned regCount_;
  55. /// Last data source.
  56. const void* lastSource_;
  57. };
  58. /// Vertex or pixel shader on the GPU.
  59. class ShaderVariation : public RefCounted, public GPUObject
  60. {
  61. public:
  62. /// Construct.
  63. ShaderVariation(Shader* owner, ShaderType type, bool isSM3);
  64. /// Destruct.
  65. virtual ~ShaderVariation();
  66. /// Create the shader program. Return true if successful.
  67. bool Create();
  68. /// Release shader.
  69. virtual void Release();
  70. /// %Set name.
  71. void SetName(const String& name);
  72. /// %Set bytecode.
  73. void SetByteCode(const SharedArrayPtr<unsigned char>& byteCode);
  74. /// Add a parameter.
  75. void AddParameter(StringHash param, const ShaderParameter& definition);
  76. /// Add a texture unit.
  77. void AddTextureUnit(TextureUnit unit);
  78. /// Clear parameters and texture unit use flags.
  79. void ClearParameters();
  80. /// Optimize the parameter map for optimal query speed
  81. void OptimizeParameters();
  82. /// Return shader type.
  83. ShaderType GetShaderType() const { return shaderType_; }
  84. /// Return full shader name.
  85. const String& GetName() const { return name_; }
  86. /// Return whether created successfully.
  87. bool IsCreated() const;
  88. /// Return whether compile failed.
  89. bool IsFailed() const { return failed_; }
  90. /// Return whether requires Shader Model 3.
  91. bool IsSM3() const { return isSM3_; }
  92. /// Return whether uses a parameter.
  93. bool HasParameter(StringHash param) const { return parameters_.Contains(param); }
  94. /// Return whether uses a texture unit (only for pixel shaders.)
  95. bool HasTextureUnit(TextureUnit unit) const { return useTextureUnit_[unit]; }
  96. /// Return all parameter definitions.
  97. const HashMap<StringHash, ShaderParameter>& GetParameters() const { return parameters_; }
  98. private:
  99. /// Shader type.
  100. ShaderType shaderType_;
  101. /// Full shader name.
  102. String name_;
  103. /// Shader bytecode.
  104. SharedArrayPtr<unsigned char> byteCode_;
  105. /// Shader Model 3 flag.
  106. bool isSM3_;
  107. /// Compile failed flag.
  108. bool failed_;
  109. /// Shader parameters.
  110. HashMap<StringHash, ShaderParameter> parameters_;
  111. /// Texture unit use flags.
  112. bool useTextureUnit_[MAX_TEXTURE_UNITS];
  113. };