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