/xbmc/guilib/Shader.h

http://github.com/xbmc/xbmc · C Header · 174 lines · 101 code · 38 blank · 35 comment · 0 complexity · d01421e35057914574562d5645a4ac07 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. #include <string>
  10. #include <vector>
  11. #include "system_gl.h"
  12. namespace Shaders {
  13. //////////////////////////////////////////////////////////////////////
  14. // CShader - base class
  15. //////////////////////////////////////////////////////////////////////
  16. class CShader
  17. {
  18. public:
  19. CShader() = default;
  20. virtual ~CShader() = default;
  21. virtual bool Compile() = 0;
  22. virtual void Free() = 0;
  23. virtual GLuint Handle() = 0;
  24. virtual void SetSource(const std::string& src) { m_source = src; }
  25. virtual bool LoadSource(const std::string& filename, const std::string& prefix = "");
  26. virtual bool AppendSource(const std::string& filename);
  27. virtual bool InsertSource(const std::string& filename, const std::string& loc);
  28. bool OK() const { return m_compiled; }
  29. std::string GetName() const { return m_filenames; }
  30. std::string GetSourceWithLineNumbers() const;
  31. protected:
  32. std::string m_source;
  33. std::string m_lastLog;
  34. std::vector<std::string> m_attr;
  35. bool m_compiled = false;
  36. private:
  37. std::string m_filenames;
  38. };
  39. //////////////////////////////////////////////////////////////////////
  40. // CVertexShader - vertex shader class
  41. //////////////////////////////////////////////////////////////////////
  42. class CVertexShader : public CShader
  43. {
  44. public:
  45. CVertexShader() = default;
  46. ~CVertexShader() override { Free(); }
  47. void Free() override {}
  48. GLuint Handle() override { return m_vertexShader; }
  49. protected:
  50. GLuint m_vertexShader = 0;
  51. };
  52. class CGLSLVertexShader : public CVertexShader
  53. {
  54. public:
  55. void Free() override;
  56. bool Compile() override;
  57. };
  58. //////////////////////////////////////////////////////////////////////
  59. // CPixelShader - abstract pixel shader class
  60. //////////////////////////////////////////////////////////////////////
  61. class CPixelShader : public CShader
  62. {
  63. public:
  64. CPixelShader() = default;
  65. ~CPixelShader() override { Free(); }
  66. void Free() override {}
  67. GLuint Handle() override { return m_pixelShader; }
  68. protected:
  69. GLuint m_pixelShader = 0;
  70. };
  71. class CGLSLPixelShader : public CPixelShader
  72. {
  73. public:
  74. void Free() override;
  75. bool Compile() override;
  76. };
  77. //////////////////////////////////////////////////////////////////////
  78. // CShaderProgram - the complete shader consisting of both the vertex
  79. // and pixel programs. (abstract)
  80. //////////////////////////////////////////////////////////////////////
  81. class CShaderProgram
  82. {
  83. public:
  84. CShaderProgram() = default;
  85. virtual ~CShaderProgram()
  86. {
  87. delete m_pFP;
  88. delete m_pVP;
  89. }
  90. // enable the shader
  91. virtual bool Enable() = 0;
  92. // disable the shader
  93. virtual void Disable() = 0;
  94. // returns true if shader is compiled and linked
  95. bool OK() const { return m_ok; }
  96. // return the vertex shader object
  97. CVertexShader* VertexShader() { return m_pVP; }
  98. // return the pixel shader object
  99. CPixelShader* PixelShader() { return m_pFP; }
  100. // compile and link the shaders
  101. virtual bool CompileAndLink() = 0;
  102. // override to to perform custom tasks on successful compilation
  103. // and linkage. E.g. obtaining handles to shader attributes.
  104. virtual void OnCompiledAndLinked() {}
  105. // override to to perform custom tasks before shader is enabled
  106. // and after it is disabled. Return false in OnDisabled() to
  107. // disable shader.
  108. // E.g. setting attributes, disabling texture unites, etc
  109. virtual bool OnEnabled() { return true; }
  110. virtual void OnDisabled() { }
  111. virtual GLuint ProgramHandle() { return m_shaderProgram; }
  112. protected:
  113. CVertexShader* m_pVP = nullptr;
  114. CPixelShader* m_pFP = nullptr;
  115. GLuint m_shaderProgram = 0;
  116. bool m_ok = false;
  117. };
  118. class CGLSLShaderProgram : virtual public CShaderProgram
  119. {
  120. public:
  121. CGLSLShaderProgram();
  122. CGLSLShaderProgram(const std::string& vert
  123. , const std::string& frag);
  124. ~CGLSLShaderProgram() override;
  125. // enable the shader
  126. bool Enable() override;
  127. // disable the shader
  128. void Disable() override;
  129. // compile and link the shaders
  130. bool CompileAndLink() override;
  131. protected:
  132. void Free();
  133. GLint m_lastProgram;
  134. bool m_validated = false;
  135. };
  136. } // close namespace