blob: 4970f62c5c47588df8ff46864893c031be5dede7 [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() };
38 glShaderSource(shader, 1, sourceArray, NULL);
39 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);
54 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
55 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
62 glDeleteShader(shader);
63 shader = 0;
64 }
65
66 return shader;
67}
68
Geoff Lang712e3f42014-03-03 11:14:15 -050069GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath)
70{
71 std::string source = ReadFileToString(sourcePath);
72 if (source.empty())
73 {
74 return 0;
75 }
76
77 return CompileShader(type, source);
78}
79
Martin Radev4c4c8e72016-08-04 12:25:34 +030080GLuint CheckLinkStatusAndReturnProgram(GLuint program, bool outputErrorMessages)
81{
Jamie Madilla7d12dc2016-12-13 15:08:19 -050082 if (glGetError() != GL_NO_ERROR)
83 return 0;
84
Martin Radev4c4c8e72016-08-04 12:25:34 +030085 GLint linkStatus;
86 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
87 if (linkStatus == 0)
88 {
89 if (outputErrorMessages)
90 {
91 GLint infoLogLength;
92 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
93
94 // Info log length includes the null terminator, so 1 means that the info log is an
95 // empty string.
96 if (infoLogLength > 1)
97 {
98 std::vector<GLchar> infoLog(infoLogLength);
99 glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), nullptr,
100 &infoLog[0]);
101
102 std::cerr << "program link failed: " << &infoLog[0];
103 }
104 else
105 {
106 std::cerr << "program link failed. <Empty log message>";
107 }
108 }
109
110 glDeleteProgram(program);
111 return 0;
112 }
113
114 return program;
115}
116
Jamie Madillca03b352015-09-02 12:38:13 -0400117GLuint CompileProgramWithTransformFeedback(
118 const std::string &vsSource,
119 const std::string &fsSource,
120 const std::vector<std::string> &transformFeedbackVaryings,
121 GLenum bufferMode)
Geoff Lang49be2ad2014-02-28 13:05:51 -0500122{
123 GLuint program = glCreateProgram();
124
125 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
126 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
127
128 if (vs == 0 || fs == 0)
129 {
130 glDeleteShader(fs);
131 glDeleteShader(vs);
132 glDeleteProgram(program);
133 return 0;
134 }
135
136 glAttachShader(program, vs);
137 glDeleteShader(vs);
138
139 glAttachShader(program, fs);
140 glDeleteShader(fs);
141
Jamie Madillca03b352015-09-02 12:38:13 -0400142 if (transformFeedbackVaryings.size() > 0)
143 {
144 std::vector<const char *> constCharTFVaryings;
145
146 for (const std::string &transformFeedbackVarying : transformFeedbackVaryings)
147 {
148 constCharTFVaryings.push_back(transformFeedbackVarying.c_str());
149 }
150
151 glTransformFeedbackVaryings(program, static_cast<GLsizei>(transformFeedbackVaryings.size()),
152 &constCharTFVaryings[0], bufferMode);
153 }
154
Geoff Lang49be2ad2014-02-28 13:05:51 -0500155 glLinkProgram(program);
156
Martin Radev4c4c8e72016-08-04 12:25:34 +0300157 return CheckLinkStatusAndReturnProgram(program, true);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500158}
Geoff Lang712e3f42014-03-03 11:14:15 -0500159
Jamie Madillca03b352015-09-02 12:38:13 -0400160GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
161{
162 std::vector<std::string> emptyVector;
163 return CompileProgramWithTransformFeedback(vsSource, fsSource, emptyVector, GL_NONE);
164}
165
Geoff Lang712e3f42014-03-03 11:14:15 -0500166GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
167{
168 std::string vsSource = ReadFileToString(vsPath);
169 std::string fsSource = ReadFileToString(fsPath);
170 if (vsSource.empty() || fsSource.empty())
171 {
172 return 0;
173 }
174
175 return CompileProgram(vsSource, fsSource);
176}
Martin Radev4c4c8e72016-08-04 12:25:34 +0300177
178GLuint CompileComputeProgram(const std::string &csSource, bool outputErrorMessages)
179{
180 GLuint program = glCreateProgram();
181
182 GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
183 if (cs == 0)
184 {
185 glDeleteProgram(program);
186 return 0;
187 }
188
189 glAttachShader(program, cs);
190
191 glLinkProgram(program);
192
193 return CheckLinkStatusAndReturnProgram(program, outputErrorMessages);
194}
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500195
196GLuint LoadBinaryProgramOES(const std::vector<uint8_t> &binary, GLenum binaryFormat)
197{
198 GLuint program = glCreateProgram();
199 glProgramBinaryOES(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
200 return CheckLinkStatusAndReturnProgram(program, true);
201}
202
203GLuint LoadBinaryProgramES3(const std::vector<uint8_t> &binary, GLenum binaryFormat)
204{
205 GLuint program = glCreateProgram();
206 glProgramBinary(program, binaryFormat, binary.data(), static_cast<GLint>(binary.size()));
207 return CheckLinkStatusAndReturnProgram(program, true);
208}