19#include <kodi/AddonBase.h>
20#include <kodi/Filesystem.h>
39 virtual bool Compile(
const std::string& extraBegin =
"",
const std::string& extraEnd =
"") = 0;
40 virtual void Free() = 0;
41 virtual GLuint Handle() = 0;
43 bool LoadSource(
const std::string& file)
54 size_t len = source.
Read(buffer,
sizeof(buffer));
55 m_source.assign(buffer);
61 bool OK()
const {
return m_compiled; }
65 std::string m_lastLog;
66 bool m_compiled =
false;
81 glDeleteShader(m_vertexShader);
85 bool Compile(
const std::string& extraBegin =
"",
const std::string& extraEnd =
"")
override
91 m_vertexShader = glCreateShader(GL_VERTEX_SHADER);
94 const char* sources[3];
95 if (!extraBegin.empty())
96 sources[count++] = extraBegin.c_str();
97 if (!m_source.empty())
98 sources[count++] = m_source.c_str();
99 if (!extraEnd.empty())
100 sources[count++] = extraEnd.c_str();
102 glShaderSource(m_vertexShader, count, sources,
nullptr);
103 glCompileShader(m_vertexShader);
104 glGetShaderiv(m_vertexShader, GL_COMPILE_STATUS, params);
105 if (params[0] != GL_TRUE)
107 GLchar
log[LOG_SIZE];
108 glGetShaderInfoLog(m_vertexShader, LOG_SIZE,
nullptr,
log);
110 fprintf(stderr,
"CVertexShader::%s: %s\n", __FUNCTION__,
log);
116 GLchar
log[LOG_SIZE];
117 glGetShaderInfoLog(m_vertexShader, LOG_SIZE,
nullptr,
log);
124 GLuint Handle()
override {
return m_vertexShader; }
127 GLuint m_vertexShader = 0;
141 glDeleteShader(m_pixelShader);
145 bool Compile(
const std::string& extraBegin =
"",
const std::string& extraEnd =
"")
override
151 m_pixelShader = glCreateShader(GL_FRAGMENT_SHADER);
154 const char* sources[3];
155 if (!extraBegin.empty())
156 sources[count++] = extraBegin.c_str();
157 if (!m_source.empty())
158 sources[count++] = m_source.c_str();
159 if (!extraEnd.empty())
160 sources[count++] = extraEnd.c_str();
162 glShaderSource(m_pixelShader, count, sources, 0);
163 glCompileShader(m_pixelShader);
164 glGetShaderiv(m_pixelShader, GL_COMPILE_STATUS, params);
165 if (params[0] != GL_TRUE)
167 GLchar
log[LOG_SIZE];
168 glGetShaderInfoLog(m_pixelShader, LOG_SIZE,
nullptr,
log);
170 fprintf(stderr,
"CPixelShader::%s: %s\n", __FUNCTION__,
log);
176 GLchar
log[LOG_SIZE];
177 glGetShaderInfoLog(m_pixelShader, LOG_SIZE,
nullptr,
log);
184 GLuint Handle()
override {
return m_pixelShader; }
187 GLuint m_pixelShader = 0;
295 CShaderProgram(
const std::string& vert,
const std::string& frag) { LoadShaderFiles(vert, frag); }
362 const std::string& vertexExtraEnd =
"",
363 const std::string& fragmentExtraBegin =
"",
364 const std::string& fragmentExtraEnd =
"")
373 if (!m_pVP.Compile(vertexExtraBegin, vertexExtraEnd))
380 if (!m_pFP.Compile(fragmentExtraBegin, fragmentExtraEnd))
388 m_shaderProgram = glCreateProgram();
389 if (!m_shaderProgram)
397 glAttachShader(m_shaderProgram, m_pVP.Handle());
398 glAttachShader(m_shaderProgram, m_pFP.Handle());
401 glLinkProgram(m_shaderProgram);
402 glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, params);
403 if (params[0] != GL_TRUE)
405 GLchar
log[LOG_SIZE];
406 glGetProgramInfoLog(m_shaderProgram, LOG_SIZE,
nullptr,
log);
408 fprintf(stderr,
"CShaderProgram::%s: %s@n", __FUNCTION__,
log);
415 OnCompiledAndLinked();
434 glUseProgram(m_shaderProgram);
441 glValidateProgram(m_shaderProgram);
442 glGetProgramiv(m_shaderProgram, GL_VALIDATE_STATUS, params);
443 if (params[0] != GL_TRUE)
445 GLchar
log[LOG_SIZE];
446 glGetProgramInfoLog(m_shaderProgram, LOG_SIZE,
nullptr,
log);
448 fprintf(stderr,
"CShaderProgram::%s: %s\n", __FUNCTION__,
log);
487 ATTR_FORCEINLINE
bool ShaderOK()
const {
return m_ok; }
554 glDeleteProgram(m_shaderProgram);
561 GLuint m_shaderProgram = 0;
563 bool m_validated =
false;
CPixelShader.
Definition Shader.h:134
CShader - base class.
Definition Shader.h:35
CVertexShader.
Definition Shader.h:73
Definition Filesystem.h:1835
@ ADDON_LOG_ERROR
3 : To report error messages in the log file.
Definition addon_base.h:193
virtual void OnCompiledAndLinked()
Mandatory child function to set the necessary CPU to GPU data.
Definition Shader.h:529
virtual void OnDisabled()
Optional child function that may have to be performed when switching off the shader.
Definition Shader.h:546
virtual bool OnEnabled()
Optional function to exchange data between CPU and GPU while activating the shader.
Definition Shader.h:539
CShaderProgram(const std::string &vert, const std::string &frag)
Construct a new shader and load defined shader files.
Definition Shader.h:295
bool LoadShaderFiles(const std::string &vert, const std::string &frag)
To load manually the needed shader files.
Definition Shader.h:316
ATTR_FORCEINLINE GLuint ProgramHandle()
Used to get the definition created in the OpenGL itself.
Definition Shader.h:514
CShaderProgram()=default
Construct a new shader.
ATTR_FORCEINLINE bool ShaderOK() const
Used to check if shader has been loaded before.
Definition Shader.h:487
bool EnableShader()
To activate the shader and use it on the GPU.
Definition Shader.h:430
bool CompileAndLink(const std::string &vertexExtraBegin="", const std::string &vertexExtraEnd="", const std::string &fragmentExtraBegin="", const std::string &fragmentExtraEnd="")
To compile and link the shader to the GL interface.
Definition Shader.h:361
ATTR_FORCEINLINE CVertexShader & VertexShader()
To get the vertex shader class used by Kodi at the addon.
Definition Shader.h:496
ATTR_FORCEINLINE CPixelShader & PixelShader()
To get the fragment shader class used by Kodi at the addon.
Definition Shader.h:505
void DisableShader()
To deactivate the shader use on the GPU.
Definition Shader.h:469
virtual ~CShaderProgram()
Destructor.
Definition Shader.h:302
void Close()
Close an open file.
Definition Filesystem.h:1921
bool OpenFile(const std::string &filename, unsigned int flags=0)
Open the file with filename via Kodi's CFile. Needs to be closed by calling Close() when done.
Definition Filesystem.h:1861
ssize_t Read(void *ptr, size_t size)
Read from an open file.
Definition Filesystem.h:2005
bool ATTR_DLL_LOCAL FileExists(const std::string &filename, bool usecache=false)
Check if a file exists.
Definition Filesystem.h:945
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1938
log(...)
<h4><code><span style="font-style: italic;">Function: </span><span style="font-style: bold;"><font co...