Shader.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2009-2021, 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/Math.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. namespace anki
  10. {
  11. /// @addtogroup graphics
  12. /// @{
  13. /// Specialization constant value.
  14. class ShaderSpecializationConstValue
  15. {
  16. public:
  17. union
  18. {
  19. F32 m_float;
  20. I32 m_int;
  21. U32 m_uint;
  22. };
  23. U32 m_constantId = MAX_U32;
  24. ShaderVariableDataType m_dataType;
  25. ShaderSpecializationConstValue()
  26. : m_int(0)
  27. , m_dataType(ShaderVariableDataType::NONE)
  28. {
  29. }
  30. explicit ShaderSpecializationConstValue(F32 f)
  31. : m_float(f)
  32. , m_dataType(ShaderVariableDataType::F32)
  33. {
  34. }
  35. explicit ShaderSpecializationConstValue(I32 i)
  36. : m_int(i)
  37. , m_dataType(ShaderVariableDataType::I32)
  38. {
  39. }
  40. explicit ShaderSpecializationConstValue(U32 i)
  41. : m_int(i)
  42. , m_dataType(ShaderVariableDataType::U32)
  43. {
  44. }
  45. ShaderSpecializationConstValue(const ShaderSpecializationConstValue&) = default;
  46. ShaderSpecializationConstValue& operator=(const ShaderSpecializationConstValue&) = default;
  47. };
  48. /// Shader init info.
  49. class ShaderInitInfo : public GrBaseInitInfo
  50. {
  51. public:
  52. ShaderType m_shaderType = ShaderType::COUNT;
  53. ConstWeakArray<U8> m_binary = {};
  54. /// @note It's OK to have entries in that array with consts that do not appear in the shader.
  55. ConstWeakArray<ShaderSpecializationConstValue> m_constValues;
  56. ShaderInitInfo()
  57. {
  58. }
  59. ShaderInitInfo(CString name)
  60. : GrBaseInitInfo(name)
  61. {
  62. }
  63. ShaderInitInfo(ShaderType type, ConstWeakArray<U8> bin, CString name = {})
  64. : GrBaseInitInfo(name)
  65. , m_shaderType(type)
  66. , m_binary(bin)
  67. {
  68. }
  69. };
  70. /// GPU shader.
  71. class Shader : public GrObject
  72. {
  73. ANKI_GR_OBJECT
  74. public:
  75. static const GrObjectType CLASS_TYPE = GrObjectType::SHADER;
  76. ShaderType getShaderType() const
  77. {
  78. ANKI_ASSERT(m_shaderType != ShaderType::COUNT);
  79. return m_shaderType;
  80. }
  81. protected:
  82. ShaderType m_shaderType = ShaderType::COUNT;
  83. /// Construct.
  84. Shader(GrManager* manager, CString name)
  85. : GrObject(manager, CLASS_TYPE, name)
  86. {
  87. }
  88. /// Destroy.
  89. ~Shader()
  90. {
  91. }
  92. private:
  93. /// Allocate and initialize a new instance.
  94. static ANKI_USE_RESULT Shader* newInstance(GrManager* manager, const ShaderInitInfo& init);
  95. };
  96. /// @}
  97. } // end namespace anki