blob: 33f8bc69dcd55059df660b2708267f0292978584 [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
49 std::vector<GLchar> infoLog(infoLogLength);
Cooper Partin4d61f7e2015-08-12 10:56:50 -070050 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
Geoff Lang49be2ad2014-02-28 13:05:51 -050051
Jamie Madillb4fd0c92014-10-01 17:40:24 -040052 std::cerr << "shader compilation failed: " << &infoLog[0];
Geoff Lang49be2ad2014-02-28 13:05:51 -050053
54 glDeleteShader(shader);
55 shader = 0;
56 }
57
58 return shader;
59}
60
Geoff Lang712e3f42014-03-03 11:14:15 -050061GLuint 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 Madillca03b352015-09-02 12:38:13 -040072GLuint CompileProgramWithTransformFeedback(
73 const std::string &vsSource,
74 const std::string &fsSource,
75 const std::vector<std::string> &transformFeedbackVaryings,
76 GLenum bufferMode)
Geoff Lang49be2ad2014-02-28 13:05:51 -050077{
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 Madillca03b352015-09-02 12:38:13 -040097 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 Lang49be2ad2014-02-28 13:05:51 -0500110 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 Partin4d61f7e2015-08-12 10:56:50 -0700121 glGetProgramInfoLog(program, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500122
Jamie Madillb4fd0c92014-10-01 17:40:24 -0400123 std::cerr << "program link failed: " << &infoLog[0];
Geoff Lang49be2ad2014-02-28 13:05:51 -0500124
125 glDeleteProgram(program);
126 return 0;
127 }
128
129 return program;
130}
Geoff Lang712e3f42014-03-03 11:14:15 -0500131
Jamie Madillca03b352015-09-02 12:38:13 -0400132GLuint 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 Lang712e3f42014-03-03 11:14:15 -0500138GLuint 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}