OLD | NEW |
| (Empty) |
1 /* | |
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 <algorithm> | |
14 #include <array> | |
15 #include <memory> | |
16 | |
17 #import "RTCShader+Private.h" | |
18 | |
19 #include "webrtc/base/checks.h" | |
20 #include "webrtc/base/logging.h" | |
21 | |
22 // Vertex shader doesn't do anything except pass coordinates through. | |
23 const char kRTCVertexShaderSource[] = | |
24 SHADER_VERSION | |
25 VERTEX_SHADER_IN " vec2 position;\n" | |
26 VERTEX_SHADER_IN " vec2 texcoord;\n" | |
27 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" | |
28 "void main() {\n" | |
29 " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" | |
30 " v_texcoord = texcoord;\n" | |
31 "}\n"; | |
32 | |
33 // Compiles a shader of the given |type| with GLSL source |source| and returns | |
34 // the shader handle or 0 on error. | |
35 GLuint RTCCreateShader(GLenum type, const GLchar *source) { | |
36 GLuint shader = glCreateShader(type); | |
37 if (!shader) { | |
38 return 0; | |
39 } | |
40 glShaderSource(shader, 1, &source, NULL); | |
41 glCompileShader(shader); | |
42 GLint compileStatus = GL_FALSE; | |
43 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); | |
44 if (compileStatus == GL_FALSE) { | |
45 GLint logLength = 0; | |
46 // The null termination character is included in the returned log length. | |
47 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength); | |
48 if (logLength > 0) { | |
49 std::unique_ptr<char[]> compileLog(new char[logLength]); | |
50 // The returned string is null terminated. | |
51 glGetShaderInfoLog(shader, logLength, NULL, compileLog.get()); | |
52 LOG(LS_ERROR) << "Shader compile error: " << compileLog.get(); | |
53 } | |
54 glDeleteShader(shader); | |
55 shader = 0; | |
56 } | |
57 return shader; | |
58 } | |
59 | |
60 // Links a shader program with the given vertex and fragment shaders and | |
61 // returns the program handle or 0 on error. | |
62 GLuint RTCCreateProgram(GLuint vertexShader, GLuint fragmentShader) { | |
63 if (vertexShader == 0 || fragmentShader == 0) { | |
64 return 0; | |
65 } | |
66 GLuint program = glCreateProgram(); | |
67 if (!program) { | |
68 return 0; | |
69 } | |
70 glAttachShader(program, vertexShader); | |
71 glAttachShader(program, fragmentShader); | |
72 glLinkProgram(program); | |
73 GLint linkStatus = GL_FALSE; | |
74 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); | |
75 if (linkStatus == GL_FALSE) { | |
76 glDeleteProgram(program); | |
77 program = 0; | |
78 } | |
79 return program; | |
80 } | |
81 | |
82 // Creates and links a shader program with the given fragment shader source and | |
83 // a plain vertex shader. Returns the program handle or 0 on error. | |
84 GLuint RTCCreateProgramFromFragmentSource(const char fragmentShaderSource[]) { | |
85 GLuint vertexShader = RTCCreateShader(GL_VERTEX_SHADER, kRTCVertexShaderSource
); | |
86 RTC_CHECK(vertexShader) << "failed to create vertex shader"; | |
87 GLuint fragmentShader = | |
88 RTCCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource); | |
89 RTC_CHECK(fragmentShader) << "failed to create fragment shader"; | |
90 GLuint program = RTCCreateProgram(vertexShader, fragmentShader); | |
91 // Shaders are created only to generate program. | |
92 if (vertexShader) { | |
93 glDeleteShader(vertexShader); | |
94 } | |
95 if (fragmentShader) { | |
96 glDeleteShader(fragmentShader); | |
97 } | |
98 return program; | |
99 } | |
100 | |
101 // Set vertex shader variables 'position' and 'texcoord' in |program| to use | |
102 // |vertexBuffer| and |vertexArray| to store the vertex data. | |
103 BOOL RTCSetupVerticesForProgram(GLuint program, GLuint* vertexBuffer, GLuint* ve
rtexArray) { | |
104 GLint position = glGetAttribLocation(program, "position"); | |
105 GLint texcoord = glGetAttribLocation(program, "texcoord"); | |
106 if (position < 0 || texcoord < 0) { | |
107 return NO; | |
108 } | |
109 #if !TARGET_OS_IPHONE | |
110 glGenVertexArrays(1, vertexArray); | |
111 if (*vertexArray == 0) { | |
112 return NO; | |
113 } | |
114 glBindVertexArray(*vertexArray); | |
115 #endif | |
116 glGenBuffers(1, vertexBuffer); | |
117 if (*vertexBuffer == 0) { | |
118 return NO; | |
119 } | |
120 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); | |
121 glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); | |
122 | |
123 // Read position attribute with size of 2 and stride of 4 beginning at the | |
124 // start of the array. The last argument indicates offset of data within the | |
125 // vertex buffer. | |
126 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | |
127 (void *)0); | |
128 glEnableVertexAttribArray(position); | |
129 | |
130 // Read texcoord attribute from |gVertices| with size of 2 and stride of 4 | |
131 // beginning at the first texcoord in the array. The last argument indicates | |
132 // offset of data within |gVertices| as supplied to the vertex buffer. | |
133 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | |
134 (void *)(2 * sizeof(GLfloat))); | |
135 glEnableVertexAttribArray(texcoord); | |
136 | |
137 return YES; | |
138 } | |
139 | |
140 // Set vertex data to the currently bound vertex buffer. | |
141 void RTCSetVertexData(RTCVideoRotation rotation) { | |
142 // When modelview and projection matrices are identity (default) the world is | |
143 // contained in the square around origin with unit size 2. Drawing to these | |
144 // coordinates is equivalent to drawing to the entire screen. The texture is | |
145 // stretched over that square using texture coordinates (u, v) that range | |
146 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically | |
147 // here because the incoming frame has origin in upper left hand corner but | |
148 // OpenGL expects origin in bottom left corner. | |
149 std::array<std::array<GLfloat, 2>, 4> UVCoords = {{ | |
150 {{0, 1}}, // Lower left. | |
151 {{1, 1}}, // Lower right. | |
152 {{1, 0}}, // Upper right. | |
153 {{0, 0}}, // Upper left. | |
154 }}; | |
155 | |
156 // Rotate the UV coordinates. | |
157 int rotation_offset; | |
158 switch (rotation) { | |
159 case RTCVideoRotation_0: | |
160 rotation_offset = 0; | |
161 break; | |
162 case RTCVideoRotation_90: | |
163 rotation_offset = 1; | |
164 break; | |
165 case RTCVideoRotation_180: | |
166 rotation_offset = 2; | |
167 break; | |
168 case RTCVideoRotation_270: | |
169 rotation_offset = 3; | |
170 break; | |
171 } | |
172 std::rotate(UVCoords.begin(), UVCoords.begin() + rotation_offset, | |
173 UVCoords.end()); | |
174 | |
175 const GLfloat gVertices[] = { | |
176 // X, Y, U, V. | |
177 -1, -1, UVCoords[0][0], UVCoords[0][1], | |
178 1, -1, UVCoords[1][0], UVCoords[1][1], | |
179 1, 1, UVCoords[2][0], UVCoords[2][1], | |
180 -1, 1, UVCoords[3][0], UVCoords[3][1], | |
181 }; | |
182 | |
183 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices); | |
184 } | |
OLD | NEW |