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 | { |
| 15 | std::ifstream stream(source); |
| 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() }; |
| 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 | |
| 49 | std::vector<GLchar> infoLog(infoLogLength); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 50 | glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 51 | |
Jamie Madill | b4fd0c9 | 2014-10-01 17:40:24 -0400 | [diff] [blame] | 52 | std::cerr << "shader compilation failed: " << &infoLog[0]; |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 53 | |
| 54 | glDeleteShader(shader); |
| 55 | shader = 0; |
| 56 | } |
| 57 | |
| 58 | return shader; |
| 59 | } |
| 60 | |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 61 | GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath) |
| 62 | { |
| 63 | std::string source = ReadFileToString(sourcePath); |
| 64 | if (source.empty()) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | return CompileShader(type, source); |
| 70 | } |
| 71 | |
Jamie Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 72 | GLuint CompileProgramWithTransformFeedback( |
| 73 | const std::string &vsSource, |
| 74 | const std::string &fsSource, |
| 75 | const std::vector<std::string> &transformFeedbackVaryings, |
| 76 | GLenum bufferMode) |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 77 | { |
| 78 | GLuint program = glCreateProgram(); |
| 79 | |
| 80 | GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource); |
| 81 | GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource); |
| 82 | |
| 83 | if (vs == 0 || fs == 0) |
| 84 | { |
| 85 | glDeleteShader(fs); |
| 86 | glDeleteShader(vs); |
| 87 | glDeleteProgram(program); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | glAttachShader(program, vs); |
| 92 | glDeleteShader(vs); |
| 93 | |
| 94 | glAttachShader(program, fs); |
| 95 | glDeleteShader(fs); |
| 96 | |
Jamie Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 97 | if (transformFeedbackVaryings.size() > 0) |
| 98 | { |
| 99 | std::vector<const char *> constCharTFVaryings; |
| 100 | |
| 101 | for (const std::string &transformFeedbackVarying : transformFeedbackVaryings) |
| 102 | { |
| 103 | constCharTFVaryings.push_back(transformFeedbackVarying.c_str()); |
| 104 | } |
| 105 | |
| 106 | glTransformFeedbackVaryings(program, static_cast<GLsizei>(transformFeedbackVaryings.size()), |
| 107 | &constCharTFVaryings[0], bufferMode); |
| 108 | } |
| 109 | |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 110 | glLinkProgram(program); |
| 111 | |
| 112 | GLint linkStatus; |
| 113 | glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 114 | |
| 115 | if (linkStatus == 0) |
| 116 | { |
| 117 | GLint infoLogLength; |
| 118 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 119 | |
| 120 | std::vector<GLchar> infoLog(infoLogLength); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 121 | glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]); |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 122 | |
Jamie Madill | b4fd0c9 | 2014-10-01 17:40:24 -0400 | [diff] [blame] | 123 | std::cerr << "program link failed: " << &infoLog[0]; |
Geoff Lang | 49be2ad | 2014-02-28 13:05:51 -0500 | [diff] [blame] | 124 | |
| 125 | glDeleteProgram(program); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | return program; |
| 130 | } |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 131 | |
Jamie Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 132 | GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource) |
| 133 | { |
| 134 | std::vector<std::string> emptyVector; |
| 135 | return CompileProgramWithTransformFeedback(vsSource, fsSource, emptyVector, GL_NONE); |
| 136 | } |
| 137 | |
Geoff Lang | 712e3f4 | 2014-03-03 11:14:15 -0500 | [diff] [blame] | 138 | GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath) |
| 139 | { |
| 140 | std::string vsSource = ReadFileToString(vsPath); |
| 141 | std::string fsSource = ReadFileToString(fsPath); |
| 142 | if (vsSource.empty() || fsSource.empty()) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | return CompileProgram(vsSource, fsSource); |
| 148 | } |