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