Shader.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/GrObject.h>
  7. #include <AnKi/Util/WeakArray.h>
  8. namespace anki {
  9. /// @addtogroup graphics
  10. /// @{
  11. /// Specialization constant value.
  12. class ShaderSpecializationConstValue
  13. {
  14. public:
  15. union
  16. {
  17. F32 m_float;
  18. I32 m_int;
  19. U32 m_uint;
  20. };
  21. U32 m_constantId = kMaxU32;
  22. ShaderVariableDataType m_dataType;
  23. ShaderSpecializationConstValue()
  24. : m_int(0)
  25. , m_dataType(ShaderVariableDataType::kNone)
  26. {
  27. }
  28. explicit ShaderSpecializationConstValue(F32 f)
  29. : m_float(f)
  30. , m_dataType(ShaderVariableDataType::kF32)
  31. {
  32. }
  33. explicit ShaderSpecializationConstValue(I32 i)
  34. : m_int(i)
  35. , m_dataType(ShaderVariableDataType::kI32)
  36. {
  37. }
  38. explicit ShaderSpecializationConstValue(U32 i)
  39. : m_int(i)
  40. , m_dataType(ShaderVariableDataType::kU32)
  41. {
  42. }
  43. ShaderSpecializationConstValue(const ShaderSpecializationConstValue&) = default;
  44. ShaderSpecializationConstValue& operator=(const ShaderSpecializationConstValue&) = default;
  45. };
  46. /// Shader init info.
  47. class ShaderInitInfo : public GrBaseInitInfo
  48. {
  49. public:
  50. ShaderType m_shaderType = ShaderType::kCount;
  51. ConstWeakArray<U8> m_binary;
  52. ShaderReflection m_reflection;
  53. ShaderInitInfo()
  54. {
  55. }
  56. ShaderInitInfo(CString name)
  57. : GrBaseInitInfo(name)
  58. {
  59. }
  60. ShaderInitInfo(ShaderType type, ConstWeakArray<U8> bin, CString name = {})
  61. : GrBaseInitInfo(name)
  62. , m_shaderType(type)
  63. , m_binary(bin)
  64. {
  65. }
  66. void validate() const
  67. {
  68. ANKI_ASSERT(m_shaderType != ShaderType::kCount);
  69. ANKI_ASSERT(m_binary.getSize() > 0);
  70. m_reflection.validate();
  71. }
  72. };
  73. /// GPU shader.
  74. class Shader : public GrObject
  75. {
  76. ANKI_GR_OBJECT
  77. public:
  78. static constexpr GrObjectType kClassType = GrObjectType::kShader;
  79. ShaderType getShaderType() const
  80. {
  81. ANKI_ASSERT(m_shaderType != ShaderType::kCount);
  82. return m_shaderType;
  83. }
  84. U32 getShaderBinarySize() const
  85. {
  86. ANKI_ASSERT(m_shaderBinarySize);
  87. return m_shaderBinarySize;
  88. }
  89. /// Pixel shader had a discard.
  90. U32 hasDiscard() const
  91. {
  92. ANKI_ASSERT(m_shaderType == ShaderType::kPixel);
  93. return m_hasDiscard;
  94. }
  95. protected:
  96. U32 m_shaderBinarySize = 0;
  97. ShaderType m_shaderType = ShaderType::kCount;
  98. Bool m_hasDiscard = false;
  99. /// Construct.
  100. Shader(CString name)
  101. : GrObject(kClassType, name)
  102. {
  103. }
  104. /// Destroy.
  105. ~Shader()
  106. {
  107. }
  108. private:
  109. /// Allocate and initialize a new instance.
  110. [[nodiscard]] static Shader* newInstance(const ShaderInitInfo& init);
  111. };
  112. /// @}
  113. } // end namespace anki