1#include "opengl_sl.h"
2
3#include <base/detect.h>
4#include <base/log.h>
5
6#if defined(BACKEND_AS_OPENGL_ES) || !defined(CONF_BACKEND_OPENGL_ES)
7
8#include <engine/client/backend/glsl_shader_compiler.h>
9#include <engine/graphics.h>
10#include <engine/shared/linereader.h>
11#include <engine/storage.h>
12
13#include <cstdio>
14#include <string>
15#include <vector>
16
17#ifndef BACKEND_AS_OPENGL_ES
18#include <GL/glew.h>
19#else
20#include <GLES3/gl3.h>
21#endif
22
23bool CGLSL::LoadShader(CGLSLCompiler *pCompiler, IStorage *pStorage, const char *pFile, int Type)
24{
25 if(m_IsLoaded)
26 return true;
27
28 CLineReader LineReader;
29 std::vector<std::string> vLines;
30 if(!LineReader.OpenFile(File: pStorage->OpenFile(pFilename: pFile, Flags: IOFLAG_READ, Type: IStorage::TYPE_ALL)))
31 {
32 return false;
33 }
34
35 EBackendType BackendType = pCompiler->m_IsOpenGLES ? BACKEND_TYPE_OPENGL_ES : BACKEND_TYPE_OPENGL;
36 bool IsNewOpenGL = (BackendType == BACKEND_TYPE_OPENGL ? (pCompiler->m_OpenGLVersionMajor >= 4 || (pCompiler->m_OpenGLVersionMajor == 3 && pCompiler->m_OpenGLVersionMinor == 3)) : pCompiler->m_OpenGLVersionMajor >= 3);
37 std::string GLShaderStringPostfix = std::string(" core\r\n");
38 if(BackendType == BACKEND_TYPE_OPENGL_ES)
39 GLShaderStringPostfix = std::string(" es\r\n");
40 //add compiler specific values
41 if(IsNewOpenGL)
42 vLines.push_back(x: std::string("#version ") + std::string(std::to_string(val: pCompiler->m_OpenGLVersionMajor)) + std::string(std::to_string(val: pCompiler->m_OpenGLVersionMinor)) + std::string(std::to_string(val: pCompiler->m_OpenGLVersionPatch)) + GLShaderStringPostfix);
43 else
44 {
45 if(pCompiler->m_OpenGLVersionMajor == 3)
46 {
47 if(pCompiler->m_OpenGLVersionMinor == 0)
48 vLines.emplace_back(args: "#version 130 \r\n");
49 if(pCompiler->m_OpenGLVersionMinor == 1)
50 vLines.emplace_back(args: "#version 140 \r\n");
51 if(pCompiler->m_OpenGLVersionMinor == 2)
52 vLines.emplace_back(args: "#version 150 \r\n");
53 }
54 else if(pCompiler->m_OpenGLVersionMajor == 2)
55 {
56 if(pCompiler->m_OpenGLVersionMinor == 0)
57 vLines.emplace_back(args: "#version 110 \r\n");
58 if(pCompiler->m_OpenGLVersionMinor == 1)
59 vLines.emplace_back(args: "#version 120 \r\n");
60 }
61 }
62
63 if(BackendType == BACKEND_TYPE_OPENGL_ES)
64 {
65 if(Type == GL_FRAGMENT_SHADER)
66 {
67 vLines.emplace_back(args: "precision highp float; \r\n");
68 vLines.emplace_back(args: "precision highp sampler2D; \r\n");
69 vLines.emplace_back(args: "precision highp sampler3D; \r\n");
70 vLines.emplace_back(args: "precision highp samplerCube; \r\n");
71 vLines.emplace_back(args: "precision highp samplerCubeShadow; \r\n");
72 vLines.emplace_back(args: "precision highp sampler2DShadow; \r\n");
73 vLines.emplace_back(args: "precision highp sampler2DArray; \r\n");
74 vLines.emplace_back(args: "precision highp sampler2DArrayShadow; \r\n");
75 }
76 }
77
78 for(const CGLSLCompiler::SGLSLCompilerDefine &Define : pCompiler->m_vDefines)
79 {
80 vLines.push_back(x: std::string("#define ") + Define.m_DefineName + std::string(" ") + Define.m_DefineValue + std::string("\r\n"));
81 }
82
83 if(Type == GL_FRAGMENT_SHADER && !IsNewOpenGL && pCompiler->m_OpenGLVersionMajor <= 3 && pCompiler->m_HasTextureArray)
84 {
85 vLines.emplace_back(args: "#extension GL_EXT_texture_array : enable\r\n");
86 }
87
88 while(const char *pReadLine = LineReader.Get())
89 {
90 std::string Line;
91 pCompiler->ParseLine(Line, pReadLine, Type: Type == GL_FRAGMENT_SHADER ? GLSL_SHADER_COMPILER_TYPE_FRAGMENT : GLSL_SHADER_COMPILER_TYPE_VERTEX);
92 Line.append(s: "\r\n");
93 vLines.push_back(x: Line);
94 }
95
96 const char **ShaderCode = new const char *[vLines.size()];
97
98 for(size_t i = 0; i < vLines.size(); ++i)
99 {
100 ShaderCode[i] = vLines[i].c_str();
101 }
102
103 const TWGLuint ShaderId = glCreateShader(Type);
104
105 glShaderSource(ShaderId, vLines.size(), ShaderCode, NULL);
106 glCompileShader(ShaderId);
107
108 delete[] ShaderCode;
109
110 TWGLint CompilationStatus;
111 glGetShaderiv(ShaderId, GL_COMPILE_STATUS, &CompilationStatus);
112
113 if(CompilationStatus == GL_FALSE)
114 {
115 TWGLint LogLength = 0;
116 glGetShaderiv(ShaderId, GL_INFO_LOG_LENGTH, &LogLength);
117 if(LogLength > 0)
118 {
119 std::string Log(LogLength, '\0');
120 glGetShaderInfoLog(ShaderId, Log.size(), nullptr, &Log.front());
121 if(Log[Log.size() - 2] == '\n')
122 {
123 Log[Log.size() - 2] = '\0';
124 }
125 log_error("gfx/opengl/shader", "Failed to compile shader file '%s'. The compiler returned:\n%s", pFile, Log.c_str());
126 }
127 else
128 {
129 log_error("gfx/opengl/shader", "Failed to compile shader file '%s'. The compiler did not return an error.", pFile);
130 }
131 glDeleteShader(ShaderId);
132 return false;
133 }
134
135 m_Type = Type;
136 m_IsLoaded = true;
137 m_ShaderId = ShaderId;
138 return true;
139}
140
141void CGLSL::DeleteShader()
142{
143 if(!IsLoaded())
144 return;
145 m_IsLoaded = false;
146 glDeleteShader(m_ShaderId);
147}
148
149bool CGLSL::IsLoaded() const
150{
151 return m_IsLoaded;
152}
153
154TWGLuint CGLSL::GetShaderId() const
155{
156 return m_ShaderId;
157}
158
159CGLSL::CGLSL()
160{
161 m_IsLoaded = false;
162}
163
164CGLSL::~CGLSL()
165{
166 DeleteShader();
167}
168
169#endif
170