2017-09-02 13:24:07 +00:00
|
|
|
#include "opengl_sl.h"
|
2017-09-14 17:35:31 +00:00
|
|
|
#include <engine/shared/linereader.h>
|
2017-12-02 21:19:57 +00:00
|
|
|
#include <engine/storage.h>
|
2017-09-02 13:24:07 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
|
2020-08-19 05:05:51 +00:00
|
|
|
bool CGLSL::LoadShader(CGLSLCompiler* pCompiler, IStorage *pStorage, const char *pFile, int Type)
|
2017-09-27 12:52:06 +00:00
|
|
|
{
|
2017-12-02 21:19:57 +00:00
|
|
|
if (m_IsLoaded)
|
|
|
|
return true;
|
|
|
|
IOHANDLE f = pStorage->OpenFile(pFile, IOFLAG_READ, IStorage::TYPE_ALL);
|
|
|
|
|
2017-09-02 13:24:07 +00:00
|
|
|
std::vector<std::string> Lines;
|
2017-09-27 12:52:06 +00:00
|
|
|
if (f)
|
|
|
|
{
|
2020-08-19 05:05:51 +00:00
|
|
|
//add compiler specific values
|
|
|
|
Lines.push_back(std::string("#version ") + std::string(std::to_string(pCompiler->m_OpenGLVersionMajor)) + std::string(std::to_string(pCompiler->m_OpenGLVersionMinor)) + std::string(std::to_string(pCompiler->m_OpenGLVersionPatch)) + std::string(" core\r\n"));
|
|
|
|
|
|
|
|
for(CGLSLCompiler::SGLSLCompilerDefine& Define : pCompiler->m_Defines)
|
|
|
|
{
|
|
|
|
Lines.push_back(std::string("#define ") + Define.m_DefineName + std::string(" ") + Define.m_DefineValue + std::string("\r\n"));
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:35:31 +00:00
|
|
|
CLineReader LineReader;
|
|
|
|
LineReader.Init(f);
|
|
|
|
char* ReadLine = NULL;
|
2017-09-27 12:52:06 +00:00
|
|
|
while ((ReadLine = LineReader.Get()))
|
|
|
|
{
|
2017-09-14 17:35:31 +00:00
|
|
|
Lines.push_back(ReadLine);
|
|
|
|
Lines.back().append("\r\n");
|
|
|
|
}
|
2017-09-02 13:24:07 +00:00
|
|
|
io_close(f);
|
|
|
|
|
|
|
|
const char** ShaderCode = new const char*[Lines.size()];
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
for (size_t i = 0; i < Lines.size(); ++i)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
ShaderCode[i] = Lines[i].c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint shader = glCreateShader(Type);
|
|
|
|
|
|
|
|
glShaderSource(shader, Lines.size(), ShaderCode, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
delete[] ShaderCode;
|
|
|
|
|
|
|
|
int CompilationStatus;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &CompilationStatus);
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
if (CompilationStatus == GL_FALSE)
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
char buff[3000];
|
|
|
|
|
|
|
|
GLint maxLength = 0;
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
glGetShaderInfoLog(shader, maxLength, &maxLength, buff);
|
|
|
|
|
2017-09-27 13:01:38 +00:00
|
|
|
dbg_msg("GLSL", "%s", buff);
|
2017-09-02 13:24:07 +00:00
|
|
|
glDeleteShader(shader);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_Type = Type;
|
|
|
|
m_IsLoaded = true;
|
|
|
|
|
|
|
|
m_ShaderID = shader;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
void CGLSL::DeleteShader()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
if (!IsLoaded()) return;
|
|
|
|
m_IsLoaded = false;
|
|
|
|
glDeleteShader(m_ShaderID);
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
bool CGLSL::IsLoaded()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
return m_IsLoaded;
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
GLuint CGLSL::GetShaderID()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
return m_ShaderID;
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
CGLSL::CGLSL()
|
|
|
|
{
|
2017-09-02 13:24:07 +00:00
|
|
|
m_IsLoaded = false;
|
2017-09-14 17:35:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 12:52:06 +00:00
|
|
|
CGLSL::~CGLSL()
|
|
|
|
{
|
2017-09-14 17:35:31 +00:00
|
|
|
DeleteShader();
|
2017-10-20 07:31:42 +00:00
|
|
|
}
|
2020-08-19 05:05:51 +00:00
|
|
|
|
|
|
|
CGLSLCompiler::CGLSLCompiler(int OpenGLVersionMajor, int OpenGLVersionMinor, int OpenGLVersionPatch)
|
|
|
|
{
|
|
|
|
m_OpenGLVersionMajor = OpenGLVersionMajor;
|
|
|
|
m_OpenGLVersionMinor = OpenGLVersionMinor;
|
|
|
|
m_OpenGLVersionPatch = OpenGLVersionPatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGLSLCompiler::AddDefine(const std::string& DefineName, const std::string& DefineValue)
|
|
|
|
{
|
|
|
|
m_Defines.emplace_back(SGLSLCompilerDefine(DefineName, DefineValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGLSLCompiler::AddDefine(const char* pDefineName, const char* pDefineValue)
|
|
|
|
{
|
|
|
|
AddDefine(std::string(pDefineName), std::string(pDefineValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGLSLCompiler::ClearDefines()
|
|
|
|
{
|
|
|
|
m_Defines.clear();
|
|
|
|
}
|