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 | IOHANDLE f = pStorage->OpenFile(pFilename: pFile, Flags: IOFLAG_READ | IOFLAG_SKIP_BOM, Type: IStorage::TYPE_ALL); |
27 | |
28 | std::vector<std::string> vLines; |
29 | if(f) |
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 | CLineReader LineReader; |
85 | LineReader.Init(File: f); |
86 | char *pReadLine = NULL; |
87 | while((pReadLine = LineReader.Get())) |
88 | { |
89 | std::string Line; |
90 | pCompiler->ParseLine(Line, pReadLine, Type: Type == GL_FRAGMENT_SHADER ? GLSL_SHADER_COMPILER_TYPE_FRAGMENT : GLSL_SHADER_COMPILER_TYPE_VERTEX); |
91 | Line.append(s: "\r\n" ); |
92 | vLines.push_back(x: Line); |
93 | } |
94 | io_close(io: f); |
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 | TWGLuint shader = glCreateShader(Type); |
104 | |
105 | glShaderSource(shader, vLines.size(), ShaderCode, NULL); |
106 | glCompileShader(shader); |
107 | |
108 | delete[] ShaderCode; |
109 | |
110 | int CompilationStatus; |
111 | glGetShaderiv(shader, GL_COMPILE_STATUS, &CompilationStatus); |
112 | |
113 | if(CompilationStatus == GL_FALSE) |
114 | { |
115 | char aBuf[3000]; |
116 | |
117 | TWGLint maxLength = 0; |
118 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); |
119 | |
120 | glGetShaderInfoLog(shader, maxLength, &maxLength, aBuf); |
121 | |
122 | dbg_msg(sys: "glsl" , fmt: "%s: %s" , pFile, aBuf); |
123 | glDeleteShader(shader); |
124 | return false; |
125 | } |
126 | m_Type = Type; |
127 | m_IsLoaded = true; |
128 | |
129 | m_ShaderId = shader; |
130 | |
131 | return true; |
132 | } |
133 | else |
134 | return false; |
135 | } |
136 | |
137 | void CGLSL::DeleteShader() |
138 | { |
139 | if(!IsLoaded()) |
140 | return; |
141 | m_IsLoaded = false; |
142 | glDeleteShader(m_ShaderId); |
143 | } |
144 | |
145 | bool CGLSL::IsLoaded() const |
146 | { |
147 | return m_IsLoaded; |
148 | } |
149 | |
150 | TWGLuint CGLSL::GetShaderId() const |
151 | { |
152 | return m_ShaderId; |
153 | } |
154 | |
155 | CGLSL::CGLSL() |
156 | { |
157 | m_IsLoaded = false; |
158 | } |
159 | |
160 | CGLSL::~CGLSL() |
161 | { |
162 | DeleteShader(); |
163 | } |
164 | |
165 | #endif |
166 | |