blob: 88082064dfdf3749f4948a7b0be9bda35378572b [file] [log] [blame]
tkchin04dbb342016-08-08 03:10:07 -07001/*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "RTCShader.h"
12
13#include <memory>
14
15#import "RTCShader+Private.h"
16
17#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
19
20// Vertex shader doesn't do anything except pass coordinates through.
21const char kRTCVertexShaderSource[] =
22 SHADER_VERSION
23 VERTEX_SHADER_IN " vec2 position;\n"
24 VERTEX_SHADER_IN " vec2 texcoord;\n"
25 VERTEX_SHADER_OUT " vec2 v_texcoord;\n"
26 "void main() {\n"
27 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"
28 " v_texcoord = texcoord;\n"
29 "}\n";
30
31// When modelview and projection matrices are identity (default) the world is
32// contained in the square around origin with unit size 2. Drawing to these
33// coordinates is equivalent to drawing to the entire screen. The texture is
34// stretched over that square using texture coordinates (u, v) that range
35// from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically
36// here because the incoming frame has origin in upper left hand corner but
37// OpenGL expects origin in bottom left corner.
38static const GLfloat gVertices[] = {
39 // X, Y, U, V.
40 -1, -1, 0, 1, // Bottom left.
41 1, -1, 1, 1, // Bottom right.
42 1, 1, 1, 0, // Top right.
43 -1, 1, 0, 0, // Top left.
44};
45
46// Compiles a shader of the given |type| with GLSL source |source| and returns
47// the shader handle or 0 on error.
48GLuint RTCCreateShader(GLenum type, const GLchar *source) {
49 GLuint shader = glCreateShader(type);
50 if (!shader) {
51 return 0;
52 }
53 glShaderSource(shader, 1, &source, NULL);
54 glCompileShader(shader);
55 GLint compileStatus = GL_FALSE;
56 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
57 if (compileStatus == GL_FALSE) {
58 GLint logLength = 0;
59 // The null termination character is included in the returned log length.
60 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
61 if (logLength > 0) {
62 std::unique_ptr<char[]> compileLog(new char[logLength]);
63 // The returned string is null terminated.
64 glGetShaderInfoLog(shader, logLength, NULL, compileLog.get());
65 LOG(LS_ERROR) << "Shader compile error: " << compileLog.get();
66 }
67 glDeleteShader(shader);
68 shader = 0;
69 }
70 return shader;
71}
72
73// Links a shader program with the given vertex and fragment shaders and
74// returns the program handle or 0 on error.
75GLuint RTCCreateProgram(GLuint vertexShader, GLuint fragmentShader) {
76 if (vertexShader == 0 || fragmentShader == 0) {
77 return 0;
78 }
79 GLuint program = glCreateProgram();
80 if (!program) {
81 return 0;
82 }
83 glAttachShader(program, vertexShader);
84 glAttachShader(program, fragmentShader);
85 glLinkProgram(program);
86 GLint linkStatus = GL_FALSE;
87 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
88 if (linkStatus == GL_FALSE) {
89 glDeleteProgram(program);
90 program = 0;
91 }
92 return program;
93}
94
95// Creates and links a shader program with the given fragment shader source and
96// a plain vertex shader. Returns the program handle or 0 on error.
97GLuint RTCCreateProgramFromFragmentSource(const char fragmentShaderSource[]) {
98 GLuint vertexShader = RTCCreateShader(GL_VERTEX_SHADER, kRTCVertexShaderSource);
99 RTC_CHECK(vertexShader) << "failed to create vertex shader";
100 GLuint fragmentShader =
101 RTCCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
102 RTC_CHECK(fragmentShader) << "failed to create fragment shader";
103 GLuint program = RTCCreateProgram(vertexShader, fragmentShader);
104 // Shaders are created only to generate program.
105 if (vertexShader) {
106 glDeleteShader(vertexShader);
107 }
108 if (fragmentShader) {
109 glDeleteShader(fragmentShader);
110 }
111 return program;
112}
113
114// Set vertex shader variables 'position' and 'texcoord' in |program| to the
115// |gVertices| array above. It will use |vertexBuffer| and |vertexArray| to
116// store the vertex data.
117BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* vertexArray) {
118 GLint position = glGetAttribLocation(program, "position");
119 GLint texcoord = glGetAttribLocation(program, "texcoord");
120 if (position < 0 || texcoord < 0) {
121 return NO;
122 }
123#if !TARGET_OS_IPHONE
124 glGenVertexArrays(1, vertexArray);
125 if (*vertexArray == 0) {
126 return NO;
127 }
128 glBindVertexArray(*vertexArray);
129#endif
130 glGenBuffers(1, vertexBuffer);
131 if (*vertexBuffer == 0) {
132 return NO;
133 }
134 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer);
135 glBufferData(GL_ARRAY_BUFFER, sizeof(gVertices), gVertices, GL_DYNAMIC_DRAW);
136
137 // Read position attribute from |gVertices| with size of 2 and stride of 4
138 // beginning at the start of the array. The last argument indicates offset
139 // of data within |gVertices| as supplied to the vertex buffer.
140 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
141 (void *)0);
142 glEnableVertexAttribArray(position);
143
144 // Read texcoord attribute from |gVertices| with size of 2 and stride of 4
145 // beginning at the first texcoord in the array. The last argument indicates
146 // offset of data within |gVertices| as supplied to the vertex buffer.
147 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
148 (void *)(2 * sizeof(GLfloat)));
149 glEnableVertexAttribArray(texcoord);
150
151 return YES;
152}