blob: 62223b524f10b735cb536f8b54d50c4fab1756cb [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
Jamie Madillca03b352015-09-02 12:38:13 -040080GLuint CompileProgramWithTransformFeedback(
81 const std::string &vsSource,
82 const std::string &fsSource,
83 const std::vector<std::string> &transformFeedbackVaryings,
84 GLenum bufferMode)
Geoff Lang49be2ad2014-02-28 13:05:51 -050085{
86 GLuint program = glCreateProgram();
87
88 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
89 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
90
91 if (vs == 0 || fs == 0)
92 {
93 glDeleteShader(fs);
94 glDeleteShader(vs);
95 glDeleteProgram(program);
96 return 0;
97 }
98
99 glAttachShader(program, vs);
100 glDeleteShader(vs);
101
102 glAttachShader(program, fs);
103 glDeleteShader(fs);
104
Jamie Madillca03b352015-09-02 12:38:13 -0400105 if (transformFeedbackVaryings.size() > 0)
106 {
107 std::vector<const char *> constCharTFVaryings;
108
109 for (const std::string &transformFeedbackVarying : transformFeedbackVaryings)
110 {
111 constCharTFVaryings.push_back(transformFeedbackVarying.c_str());
112 }
113
114 glTransformFeedbackVaryings(program, static_cast<GLsizei>(transformFeedbackVaryings.size()),
115 &constCharTFVaryings[0], bufferMode);
116 }
117
Geoff Lang49be2ad2014-02-28 13:05:51 -0500118 glLinkProgram(program);
119
120 GLint linkStatus;
121 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
122
123 if (linkStatus == 0)
124 {
125 GLint infoLogLength;
126 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
127
Olli Etuaho67946762016-03-08 15:43:55 +0200128 // Info log length includes the null terminator, so 1 means that the info log is an empty
129 // string.
130 if (infoLogLength > 1)
131 {
132 std::vector<GLchar> infoLog(infoLogLength);
133 glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), nullptr, &infoLog[0]);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500134
Olli Etuaho67946762016-03-08 15:43:55 +0200135 std::cerr << "program link failed: " << &infoLog[0];
136 }
137 else
138 {
139 std::cerr << "program link failed. <Empty log message>";
140 }
Geoff Lang49be2ad2014-02-28 13:05:51 -0500141
142 glDeleteProgram(program);
143 return 0;
144 }
145
146 return program;
147}
Geoff Lang712e3f42014-03-03 11:14:15 -0500148
Jamie Madillca03b352015-09-02 12:38:13 -0400149GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
150{
151 std::vector<std::string> emptyVector;
152 return CompileProgramWithTransformFeedback(vsSource, fsSource, emptyVector, GL_NONE);
153}
154
Geoff Lang712e3f42014-03-03 11:14:15 -0500155GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
156{
157 std::string vsSource = ReadFileToString(vsPath);
158 std::string fsSource = ReadFileToString(fsPath);
159 if (vsSource.empty() || fsSource.empty())
160 {
161 return 0;
162 }
163
164 return CompileProgram(vsSource, fsSource);
165}