PageRenderTime 68ms CodeModel.GetById 49ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  9#pragma once
 10
 11#include <string>
 12#include <vector>
 13
 14#include "system_gl.h"
 15
 16namespace Shaders {
 17
 18  //////////////////////////////////////////////////////////////////////
 19  // CShader - base class
 20  //////////////////////////////////////////////////////////////////////
 21  class CShader
 22  {
 23  public:
 24    CShader() = default;
 25    virtual ~CShader() = default;
 26    virtual bool Compile() = 0;
 27    virtual void Free() = 0;
 28    virtual GLuint Handle() = 0;
 29    virtual void SetSource(const std::string& src) { m_source = src; }
 30    virtual bool LoadSource(const std::string& filename, const std::string& prefix = "");
 31    virtual bool AppendSource(const std::string& filename);
 32    virtual bool InsertSource(const std::string& filename, const std::string& loc);
 33    bool OK() const { return m_compiled; }
 34
 35    std::string GetName() const { return m_filenames; }
 36    std::string GetSourceWithLineNumbers() const;
 37
 38  protected:
 39    std::string m_source;
 40    std::string m_lastLog;
 41    std::vector<std::string> m_attr;
 42    bool m_compiled = false;
 43
 44  private:
 45    std::string m_filenames;
 46  };
 47
 48
 49  //////////////////////////////////////////////////////////////////////
 50  // CVertexShader - vertex shader class
 51  //////////////////////////////////////////////////////////////////////
 52  class CVertexShader : public CShader
 53  {
 54  public:
 55    CVertexShader() = default;
 56    ~CVertexShader() override { Free(); }
 57    void Free() override {}
 58    GLuint Handle() override { return m_vertexShader; }
 59
 60  protected:
 61    GLuint m_vertexShader = 0;
 62  };
 63
 64  class CGLSLVertexShader : public CVertexShader
 65  {
 66  public:
 67    void Free() override;
 68    bool Compile() override;
 69  };
 70
 71
 72  //////////////////////////////////////////////////////////////////////
 73  // CPixelShader - abstract pixel shader class
 74  //////////////////////////////////////////////////////////////////////
 75  class CPixelShader : public CShader
 76  {
 77  public:
 78    CPixelShader() = default;
 79    ~CPixelShader() override { Free(); }
 80    void Free() override {}
 81    GLuint Handle() override { return m_pixelShader; }
 82
 83  protected:
 84    GLuint m_pixelShader = 0;
 85  };
 86
 87  class CGLSLPixelShader : public CPixelShader
 88  {
 89  public:
 90    void Free() override;
 91    bool Compile() override;
 92  };
 93
 94  //////////////////////////////////////////////////////////////////////
 95  // CShaderProgram - the complete shader consisting of both the vertex
 96  //                  and pixel programs. (abstract)
 97  //////////////////////////////////////////////////////////////////////
 98  class CShaderProgram
 99  {
100  public:
101    CShaderProgram() = default;
102
103    virtual ~CShaderProgram()
104      {
105        delete m_pFP;
106        delete m_pVP;
107      }
108
109    // enable the shader
110    virtual bool Enable() = 0;
111
112    // disable the shader
113    virtual void Disable() = 0;
114
115    // returns true if shader is compiled and linked
116    bool OK() const { return m_ok; }
117
118    // return the vertex shader object
119    CVertexShader* VertexShader() { return m_pVP; }
120
121    // return the pixel shader object
122    CPixelShader* PixelShader() { return m_pFP; }
123
124    // compile and link the shaders
125    virtual bool CompileAndLink() = 0;
126
127    // override to to perform custom tasks on successful compilation
128    // and linkage. E.g. obtaining handles to shader attributes.
129    virtual void OnCompiledAndLinked() {}
130
131    // override to to perform custom tasks before shader is enabled
132    // and after it is disabled. Return false in OnDisabled() to
133    // disable shader.
134    // E.g. setting attributes, disabling texture unites, etc
135    virtual bool OnEnabled() { return true; }
136    virtual void OnDisabled() { }
137
138    virtual GLuint ProgramHandle() { return m_shaderProgram; }
139
140  protected:
141    CVertexShader* m_pVP = nullptr;
142    CPixelShader*  m_pFP = nullptr;
143    GLuint m_shaderProgram = 0;
144    bool m_ok = false;
145  };
146
147
148  class CGLSLShaderProgram : virtual public CShaderProgram
149  {
150  public:
151    CGLSLShaderProgram();
152    CGLSLShaderProgram(const std::string& vert
153                       , const std::string& frag);
154    ~CGLSLShaderProgram() override;
155
156    // enable the shader
157    bool Enable() override;
158
159    // disable the shader
160    void Disable() override;
161
162    // compile and link the shaders
163    bool CompileAndLink() override;
164
165  protected:
166    void Free();
167
168    GLint m_lastProgram;
169    bool m_validated = false;
170  };
171
172
173} // close namespace
174