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