blob: 3e0efbabaa38af31a3b6d15e047f675fda4e23a8 [file] [log] [blame]
Geoff Lang49be2ad2014-02-28 13:05:51 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "shader_utils.h"
8
9#include <vector>
10#include <iostream>
Geoff Lang712e3f42014-03-03 11:14:15 -050011#include <fstream>
12
13static std::string ReadFileToString(const std::string &source)
14{
Corentin Wallezf0ca9a02015-09-14 11:53:10 -070015 std::ifstream stream(source.c_str());
Geoff Lang712e3f42014-03-03 11:14:15 -050016 if (!stream)
17 {
18 std::cerr << "Failed to load shader file: " << source;
19 return "";
20 }
21
22 std::string result;
23
24 stream.seekg(0, std::ios::end);
Minmin Gong794e0002015-04-07 18:31:54 -070025 result.reserve(static_cast<unsigned int>(stream.tellg()));
Geoff Lang712e3f42014-03-03 11:14:15 -050026 stream.seekg(0, std::ios::beg);
27
28 result.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
29
30 return result;
31}
Geoff Lang49be2ad2014-02-28 13:05:51 -050032
33GLuint CompileShader(GLenum type, const std::string &source)
34{
35 GLuint shader = glCreateShader(type);
36
37 const char *sourceArray[1] = { source.c_str() };
Yunchao Hef81ce4a2017-04-24 10:49:17 +080038 glShaderSource(shader, 1, sourceArray, nullptr);
Geoff Lang49be2ad2014-02-28 13:05:51 -050039 glCompileShader(shader);
40
41 GLint compileResult;
42 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
43
44 if (compileResult == 0)
45 {
46 GLint infoLogLength;
47 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
48
Olli Etuaho67946762016-03-08 15:43:55 +020049 // Info log length includes the null terminator, so 1 means that the info log is an empty
50 // string.
51 if (infoLogLength > 1)
Jamie Madill90c253a2015-12-15 15:14:08 -050052 {
53 std::vector<GLchar> infoLog(infoLogLength);
Yunchao Hef81ce4a2017-04-24 10:49:17 +080054 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), nullptr, &infoLog[0]);
Jamie Madill90c253a2015-12-15 15:14:08 -050055 std::cerr << "shader compilation failed: " << &infoLog[0];
56 }
57 else
58 {
59 std::cerr << "shader compilation failed. <Empty log message>";
60 }
Geoff Lang49be2ad2014-02-28 13:05:51 -050061
Jamie Madillbd044ed2017-06-05 12:59:21 -040062 std::cerr << std::endl;
63
Geoff Lang49be2ad2014-02-28 13:05:51 -050064 glDeleteShader(shader);
65 shader = 0;
66 }
67
68 return shader;
69}
70
Geoff Lang712e3f42014-03-03 11:14:15 -050071GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath)
72{
73 std::string source = ReadFileToString(sourcePath);
74 if (source.empty())
75 {
76 return 0;
77 }
78
79 return CompileShader(type, source);
80}
81
Martin Radev4c4c8e72016-08-04 12:25:34 +030082GLuint CheckLinkStatusAndReturnProgram(GLuint program, bool outputErrorMessages)
83{
Jamie Madilla7d12dc2016-12-13 15:08:19 -050084 if (glGetError() != GL_NO_ERROR)
85 return 0;
86
Martin Radev4c4c8e72016-08-04 12:25:34 +030087 GLint linkStatus;
88 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
89 if (linkStatus == 0)
90 {
91 if (outputErrorMessages)
92 {
93 GLint infoLogLength;
94 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
95
96 // Info log length includes the null terminator, so 1 means that the info log is an
97 // empty string.
98 if (infoLogLength > 1)
99 {
100 std::vector<GLchar> infoLog(infoLogLength);
101 glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), nullptr,
102 &infoLog[0]);
103
104 std::cerr << "program link failed: " << &infoLog[0];
105 }
106 else
107 {
108 std::cerr << "program link failed. <Empty log message>";
109 }
110 }
111
112 glDeleteProgram(program);
113 return 0;
114 }
115
116 return program;
117}
118
Jamie Madillca03b352015-09-02 12:38:13 -0400119GLuint CompileProgramWithTransformFeedback(
120 const std::string &vsSource,
121 const std::string &fsSource,
122 const std::vector<std::string> &transformFeedbackVaryings,
123 GLenum bufferMode)
Geoff Lang49be2ad2014-02-28 13:05:51 -0500124{
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800125 return CompileProgramWithGSAndTransformFeedback(vsSource, "", fsSource,
126 transformFeedbackVaryings, bufferMode);
127}
128
jchen107ae70d82018-07-06 13:47:01 +0800129static GLuint CompileAndLinkProgram(const std::string &vsSource,
130 const std::string &gsSource,
131 const std::string &fsSource,
132 const std::vector<std::string> &transformFeedbackVaryings,
133 GLenum bufferMode)
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800134{
Geoff Lang49be2ad2014-02-28 13:05:51 -0500135 GLuint program = glCreateProgram();
136
137 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
138 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
139
140 if (vs == 0 || fs == 0)
141 {
142 glDeleteShader(fs);
143 glDeleteShader(vs);
144 glDeleteProgram(program);
145 return 0;
146 }
147
148 glAttachShader(program, vs);
149 glDeleteShader(vs);
150
151 glAttachShader(program, fs);
152 glDeleteShader(fs);
153
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800154 if (!gsSource.empty())
155 {
156 GLuint gs = CompileShader(GL_GEOMETRY_SHADER_EXT, gsSource);
157 if (gs == 0)
158 {
159 glDeleteShader(vs);
160 glDeleteShader(fs);
161 glDeleteProgram(program);
162 return 0;
163 }
164
165 glAttachShader(program, gs);
166 glDeleteShader(gs);
167 }
168
Jamie Madillca03b352015-09-02 12:38:13 -0400169 if (transformFeedbackVaryings.size() > 0)
170 {
171 std::vector<const char *> constCharTFVaryings;
172
173 for (const std::string &transformFeedbackVarying : transformFeedbackVaryings)
174 {
175 constCharTFVaryings.push_back(transformFeedbackVarying.c_str());
176 }
177
178 glTransformFeedbackVaryings(program, static_cast<GLsizei>(transformFeedbackVaryings.size()),
179 &constCharTFVaryings[0], bufferMode);
180 }
181
Geoff Lang49be2ad2014-02-28 13:05:51 -0500182 glLinkProgram(program);
183
jchen107ae70d82018-07-06 13:47:01 +0800184 return program;
185}
186
187GLuint CompileProgramWithGSAndTransformFeedback(
188 const std::string &vsSource,
189 const std::string &gsSource,
190 const std::string &fsSource,
191 const std::vector<std::string> &transformFeedbackVaryings,
192 GLenum bufferMode)
193{
194 GLuint program =
195 CompileAndLinkProgram(vsSource, gsSource, fsSource, transformFeedbackVaryings, bufferMode);
196 if (program == 0)
197 {
198 return 0;
199 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300200 return CheckLinkStatusAndReturnProgram(program, true);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500201}
Geoff Lang712e3f42014-03-03 11:14:15 -0500202
Jamie Madillca03b352015-09-02 12:38:13 -0400203GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
204{
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800205 return CompileProgramWithGS(vsSource, "", fsSource);
206}
207
208GLuint CompileProgramWithGS(const std::string &vsSource,
209 const std::string &gsSource,
210 const std::string &fsSource)
211{
Jamie Madillca03b352015-09-02 12:38:13 -0400212 std::vector<std::string> emptyVector;
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800213 return CompileProgramWithGSAndTransformFeedback(vsSource, gsSource, fsSource, emptyVector,
214 GL_NONE);
Jamie Madillca03b352015-09-02 12:38:13 -0400215}
216
Geoff Lang712e3f42014-03-03 11:14:15 -0500217GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
218{
219 std::string vsSource = ReadFileToString(vsPath);
220 std::string fsSource = ReadFileToString(fsPath);
221 if (vsSource.empty() || fsSource.empty())
222 {
223 return 0;
224 }
225
226 return CompileProgram(vsSource, fsSource);
227}
Martin Radev4c4c8e72016-08-04 12:25:34 +0300228
229GLuint CompileComputeProgram(const std::string &csSource, bool outputErrorMessages)
230{
231 GLuint program = glCreateProgram();
232
233 GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
234 if (cs == 0)
235 {
236 glDeleteProgram(program);
237 return 0;
238 }
239
240 glAttachShader(program, cs);
241
242 glLinkProgram(program);
243
244 return CheckLinkStatusAndReturnProgram(program, outputErrorMessages);
245}
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500246
247GLuint LoadBinaryProgramOES(const std::vector<uint8_t> &binary, GLenum binaryFormat)
248{
249 GLuint program = glCreateProgram();
250 glProgramBinaryOES(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
251 return CheckLinkStatusAndReturnProgram(program, true);
252}
253
254GLuint LoadBinaryProgramES3(const std::vector<uint8_t> &binary, GLenum binaryFormat)
255{
256 GLuint program = glCreateProgram();
257 glProgramBinary(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
258 return CheckLinkStatusAndReturnProgram(program, true);
259}
Jamie Madill401345e2017-08-21 10:52:40 -0400260
261bool LinkAttachedProgram(GLuint program)
262{
263 glLinkProgram(program);
264 return (CheckLinkStatusAndReturnProgram(program, true) != 0);
265}
Olli Etuaho5804dc82018-04-13 14:11:46 +0300266
267namespace angle
268{
269
270namespace essl1_shaders
271{
272
273const char *PositionAttrib()
274{
275 return "a_position";
276}
277const char *ColorUniform()
278{
279 return "u_color";
280}
281
282namespace vs
283{
284
285// A shader that sets gl_Position to zero.
286const char *Zero()
287{
288 return R"(void main()
289{
290 gl_Position = vec4(0);
291})";
292}
293
294// A shader that sets gl_Position to attribute a_position.
295const char *Simple()
296{
297 return R"(precision highp float;
298attribute vec4 a_position;
299
300void main()
301{
302 gl_Position = a_position;
303})";
304}
305
306// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
Olli Etuahodff32a02018-08-28 14:35:50 +0300307// v_position.
Olli Etuaho5804dc82018-04-13 14:11:46 +0300308const char *Passthrough()
309{
310 return R"(precision highp float;
311attribute vec4 a_position;
312varying vec4 v_position;
313
314void main()
315{
316 gl_Position = a_position;
317 v_position = a_position;
318})";
319}
320
321} // namespace vs
322
323namespace fs
324{
325
326// A shader that renders a simple checker pattern of red and green. X axis and y axis separate the
327// different colors. Needs varying v_position.
328const char *Checkered()
329{
330 return R"(precision highp float;
331varying vec4 v_position;
332
333void main()
334{
335 if (v_position.x * v_position.y > 0.0)
336 {
337 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
338 }
339 else
340 {
341 gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
342 }
343})";
344}
345
346// A shader that fills with color taken from uniform named "color".
347const char *UniformColor()
348{
349 return R"(uniform mediump vec4 u_color;
350void main(void)
351{
352 gl_FragColor = u_color;
353})";
354}
355
356// A shader that fills with 100% opaque red.
357const char *Red()
358{
359 return R"(precision mediump float;
360
361void main()
362{
363 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
364})";
365}
366
367// A shader that fills with 100% opaque blue.
368const char *Blue()
369{
370 return R"(precision mediump float;
371
372void main()
373{
374 gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
375})";
376}
377
378} // namespace fs
379} // namespace essl1_shaders
380
381namespace essl3_shaders
382{
383
384const char *PositionAttrib()
385{
386 return "a_position";
387}
388
389namespace vs
390{
391
392// A shader that sets gl_Position to zero.
393const char *Zero()
394{
395 return R"(#version 300 es
396void main()
397{
398 gl_Position = vec4(0);
399})";
400}
401
402// A shader that sets gl_Position to attribute a_position.
403const char *Simple()
404{
405 return R"(#version 300 es
406in vec4 a_position;
407void main()
408{
409 gl_Position = a_position;
410})";
411}
412
413} // namespace vs
414
415namespace fs
416{
417
418// A shader that fills with 100% opaque red.
419const char *Red()
420{
421 return R"(#version 300 es
422precision highp float;
423out vec4 my_FragColor;
424void main()
425{
426 my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
427})";
428}
429
430} // namespace fs
431} // namespace essl3_shaders
432
433namespace essl31_shaders
434{
435
436const char *PositionAttrib()
437{
438 return "a_position";
439}
440
441namespace vs
442{
443
444// A shader that sets gl_Position to zero.
445const char *Zero()
446{
447 return R"(#version 310 es
448void main()
449{
450 gl_Position = vec4(0);
451})";
452}
453
454// A shader that sets gl_Position to attribute a_position.
455const char *Simple()
456{
457 return R"(#version 310 es
458in vec4 a_position;
459void main()
460{
461 gl_Position = a_position;
462})";
463}
464
Olli Etuahodff32a02018-08-28 14:35:50 +0300465// A shader that simply passes through attribute a_position, setting it to gl_Position and varying
466// v_position.
467const char *Passthrough()
468{
469 return R"(#version 310 es
470in vec4 a_position;
471out vec4 v_position;
472void main()
473{
474 gl_Position = a_position;
475 v_position = a_position;
476})";
477}
478
Olli Etuaho5804dc82018-04-13 14:11:46 +0300479} // namespace vs
480
481namespace fs
482{
483
484// A shader that fills with 100% opaque red.
485const char *Red()
486{
487 return R"(#version 310 es
488precision highp float;
489out vec4 my_FragColor;
490void main()
491{
492 my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
493})";
494}
495
496} // namespace fs
497} // namespace essl31_shaders
498} // namespace angle