Shader.h 2.3 KB

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