blob: 4a710175fcb50adfae99851c77efc953550267f9 [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{
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 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);
Jamie Madillb4fd0c92014-10-01 17:40:24 -040050 glGetShaderInfoLog(shader, 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
Geoff Lang49be2ad2014-02-28 13:05:51 -050072GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
73{
74 GLuint program = glCreateProgram();
75
76 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
77 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
78
79 if (vs == 0 || fs == 0)
80 {
81 glDeleteShader(fs);
82 glDeleteShader(vs);
83 glDeleteProgram(program);
84 return 0;
85 }
86
87 glAttachShader(program, vs);
88 glDeleteShader(vs);
89
90 glAttachShader(program, fs);
91 glDeleteShader(fs);
92
93 glLinkProgram(program);
94
95 GLint linkStatus;
96 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
97
98 if (linkStatus == 0)
99 {
100 GLint infoLogLength;
101 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
102
103 std::vector<GLchar> infoLog(infoLogLength);
Jamie Madillb4fd0c92014-10-01 17:40:24 -0400104 glGetProgramInfoLog(program, infoLog.size(), NULL, &infoLog[0]);
Geoff Lang49be2ad2014-02-28 13:05:51 -0500105
Jamie Madillb4fd0c92014-10-01 17:40:24 -0400106 std::cerr << "program link failed: " << &infoLog[0];
Geoff Lang49be2ad2014-02-28 13:05:51 -0500107
108 glDeleteProgram(program);
109 return 0;
110 }
111
112 return program;
113}
Geoff Lang712e3f42014-03-03 11:14:15 -0500114
115GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
116{
117 std::string vsSource = ReadFileToString(vsPath);
118 std::string fsSource = ReadFileToString(fsPath);
119 if (vsSource.empty() || fsSource.empty())
120 {
121 return 0;
122 }
123
124 return CompileProgram(vsSource, fsSource);
125}