Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 1 | // |
| 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 Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 11 | #include <fstream> |
| 12 | |
| 13 | static std::string ReadFileToString(const std::string &source) |
| 14 | { |
Corentin Wallez | f0ca9a0 | 2015-09-14 11:53:10 -0700 | [diff] [blame] | 15 | std::ifstream stream(source.c_str()); |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 16 | 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 Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 25 | result.reserve(static_cast<unsigned int>(stream.tellg())); |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 26 | 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 Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 32 | |
| 33 | GLuint CompileShader(GLenum type, const std::string &source) |
| 34 | { |
| 35 | GLuint shader = glCreateShader(type); |
| 36 | |
| 37 | const char *sourceArray[1] = { source.c_str() }; |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 38 | glShaderSource(shader, 1, sourceArray, nullptr); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 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 Etuaho | 6794676 | 2016-03-08 15:43:55 +0200 | [diff] [blame] | 49 | // Info log length includes the null terminator, so 1 means that the info log is an empty |
| 50 | // string. |
| 51 | if (infoLogLength > 1) |
Jamie Madill | 90c253a | 2015-12-15 15:14:08 -0500 | [diff] [blame] | 52 | { |
| 53 | std::vector<GLchar> infoLog(infoLogLength); |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 54 | glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), nullptr, &infoLog[0]); |
Jamie Madill | 90c253a | 2015-12-15 15:14:08 -0500 | [diff] [blame] | 55 | std::cerr << "shader compilation failed: " << &infoLog[0]; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | std::cerr << "shader compilation failed. <Empty log message>"; |
| 60 | } |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 61 | |
| 62 | glDeleteShader(shader); |
| 63 | shader = 0; |
| 64 | } |
| 65 | |
| 66 | return shader; |
| 67 | } |
| 68 | |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 69 | GLuint 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 Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 80 | GLuint CheckLinkStatusAndReturnProgram(GLuint program, bool outputErrorMessages) |
| 81 | { |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 82 | if (glGetError() != GL_NO_ERROR) |
| 83 | return 0; |
| 84 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 85 | 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 Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 117 | GLuint CompileProgramWithTransformFeedback( |
| 118 | const std::string &vsSource, |
| 119 | const std::string &fsSource, |
| 120 | const std::vector<std::string> &transformFeedbackVaryings, |
| 121 | GLenum bufferMode) |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 122 | { |
| 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 Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 142 | 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 Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 155 | glLinkProgram(program); |
| 156 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 157 | return CheckLinkStatusAndReturnProgram(program, true); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 158 | } |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 159 | |
Jamie Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 160 | GLuint 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 Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 166 | GLuint 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 Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 177 | |
| 178 | GLuint 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 Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 195 | |
| 196 | GLuint 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 | |
| 203 | GLuint 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 | } |