Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #import "RTCShader.h" | 11 #import "RTCShader.h" |
| 12 | 12 |
| 13 #if TARGET_OS_IPHONE | |
| 14 #import <OpenGLES/ES3/gl.h> | |
| 15 #else | |
| 16 #import <OpenGL/gl3.h> | |
| 17 #endif | |
| 18 | |
| 13 #include <algorithm> | 19 #include <algorithm> |
| 14 #include <array> | 20 #include <array> |
| 15 #include <memory> | 21 #include <memory> |
| 16 | 22 |
| 17 #import "RTCShader+Private.h" | 23 #import "RTCOpenGLDefines.h" |
| 18 | 24 |
| 19 #include "webrtc/base/checks.h" | 25 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" | 26 #include "webrtc/base/logging.h" |
| 21 | 27 |
| 22 // Vertex shader doesn't do anything except pass coordinates through. | 28 // Vertex shader doesn't do anything except pass coordinates through. |
| 23 const char kRTCVertexShaderSource[] = | 29 const char kRTCVertexShaderSource[] = |
| 24 SHADER_VERSION | 30 SHADER_VERSION |
| 25 VERTEX_SHADER_IN " vec2 position;\n" | 31 VERTEX_SHADER_IN " vec2 position;\n" |
| 26 VERTEX_SHADER_IN " vec2 texcoord;\n" | 32 VERTEX_SHADER_IN " vec2 texcoord;\n" |
| 27 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" | 33 VERTEX_SHADER_OUT " vec2 v_texcoord;\n" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 RTCCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource); | 94 RTCCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource); |
| 89 RTC_CHECK(fragmentShader) << "failed to create fragment shader"; | 95 RTC_CHECK(fragmentShader) << "failed to create fragment shader"; |
| 90 GLuint program = RTCCreateProgram(vertexShader, fragmentShader); | 96 GLuint program = RTCCreateProgram(vertexShader, fragmentShader); |
| 91 // Shaders are created only to generate program. | 97 // Shaders are created only to generate program. |
| 92 if (vertexShader) { | 98 if (vertexShader) { |
| 93 glDeleteShader(vertexShader); | 99 glDeleteShader(vertexShader); |
| 94 } | 100 } |
| 95 if (fragmentShader) { | 101 if (fragmentShader) { |
| 96 glDeleteShader(fragmentShader); | 102 glDeleteShader(fragmentShader); |
| 97 } | 103 } |
| 104 | |
| 105 // Set vertex shader variables 'position' and 'texcoord' in program. | |
| 106 GLint position = glGetAttribLocation(program, "position"); | |
| 107 GLint texcoord = glGetAttribLocation(program, "texcoord"); | |
| 108 if (position < 0 || texcoord < 0) { | |
| 109 glDeleteProgram(program); | |
| 110 return 0; | |
| 111 } | |
| 112 | |
| 113 // Read position attribute with size of 2 and stride of 4 beginning at the sta rt of the array. The | |
| 114 // last argument indicates offset of data within the vertex buffer. | |
| 115 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (v oid *)0); | |
| 116 glEnableVertexAttribArray(position); | |
| 117 | |
| 118 // Read texcoord attribute with size of 2 and stride of 4 beginning at the fi rst texcoord in the | |
| 119 // array. The last argument indicates offset of data within the vertex buffer. | |
| 120 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), | |
| 121 (void *)(2 * sizeof(GLfloat))); | |
| 122 glEnableVertexAttribArray(texcoord); | |
| 123 | |
| 98 return program; | 124 return program; |
| 99 } | 125 } |
| 100 | 126 |
| 101 // Set vertex shader variables 'position' and 'texcoord' in |program| to use | 127 BOOL RTCCreateVertexBuffer(GLuint *vertexBuffer, GLuint *vertexArray) { |
| 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 | 128 #if !TARGET_OS_IPHONE |
| 110 glGenVertexArrays(1, vertexArray); | 129 glGenVertexArrays(1, vertexArray); |
| 111 if (*vertexArray == 0) { | 130 if (*vertexArray == 0) { |
| 112 return NO; | 131 return NO; |
| 113 } | 132 } |
| 114 glBindVertexArray(*vertexArray); | 133 glBindVertexArray(*vertexArray); |
| 115 #endif | 134 #endif |
| 116 glGenBuffers(1, vertexBuffer); | 135 glGenBuffers(1, vertexBuffer); |
| 117 if (*vertexBuffer == 0) { | 136 if (*vertexBuffer == 0) { |
| 118 return NO; | 137 return NO; |
|
sakal
2017/05/11 10:45:47
Delete vertex array if we created one?
magjed_webrtc
2017/05/11 12:38:06
Done.
| |
| 119 } | 138 } |
| 120 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); | 139 glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer); |
| 121 glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW); | 140 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; | 141 return YES; |
| 138 } | 142 } |
| 139 | 143 |
| 140 // Set vertex data to the currently bound vertex buffer. | 144 // Set vertex data to the currently bound vertex buffer. |
| 141 void RTCSetVertexData(RTCVideoRotation rotation) { | 145 void RTCSetVertexData(RTCVideoRotation rotation) { |
| 142 // When modelview and projection matrices are identity (default) the world is | 146 // 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 | 147 // 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 | 148 // coordinates is equivalent to drawing to the entire screen. The texture is |
| 145 // stretched over that square using texture coordinates (u, v) that range | 149 // stretched over that square using texture coordinates (u, v) that range |
| 146 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically | 150 // from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 175 const GLfloat gVertices[] = { | 179 const GLfloat gVertices[] = { |
| 176 // X, Y, U, V. | 180 // X, Y, U, V. |
| 177 -1, -1, UVCoords[0][0], UVCoords[0][1], | 181 -1, -1, UVCoords[0][0], UVCoords[0][1], |
| 178 1, -1, UVCoords[1][0], UVCoords[1][1], | 182 1, -1, UVCoords[1][0], UVCoords[1][1], |
| 179 1, 1, UVCoords[2][0], UVCoords[2][1], | 183 1, 1, UVCoords[2][0], UVCoords[2][1], |
| 180 -1, 1, UVCoords[3][0], UVCoords[3][1], | 184 -1, 1, UVCoords[3][0], UVCoords[3][1], |
| 181 }; | 185 }; |
| 182 | 186 |
| 183 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices); | 187 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices); |
| 184 } | 188 } |
| OLD | NEW |