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 #import "RTCNV12TextureCache.h" |
| 14 #import "RTCShader+Private.h" |
| 15 #import "WebRTC/RTCLogging.h" |
| 16 #import "WebRTC/RTCVideoFrame.h" |
| 17 |
| 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/optional.h" |
| 20 |
| 21 static const char kNV12FragmentShaderSource[] = |
| 22 SHADER_VERSION |
| 23 "precision mediump float;" |
| 24 FRAGMENT_SHADER_IN " vec2 v_texcoord;\n" |
| 25 "uniform lowp sampler2D s_textureY;\n" |
| 26 "uniform lowp sampler2D s_textureUV;\n" |
| 27 FRAGMENT_SHADER_OUT |
| 28 "void main() {\n" |
| 29 " mediump float y;\n" |
| 30 " mediump vec2 uv;\n" |
| 31 " y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n" |
| 32 " uv = " FRAGMENT_SHADER_TEXTURE "(s_textureUV, v_texcoord).ra -\n" |
| 33 " vec2(0.5, 0.5);\n" |
| 34 " " FRAGMENT_SHADER_COLOR " = vec4(y + 1.403 * uv.y,\n" |
| 35 " y - 0.344 * uv.x - 0.714 * uv.y,\n" |
| 36 " y + 1.770 * uv.x,\n" |
| 37 " 1.0);\n" |
| 38 " }\n"; |
| 39 |
| 40 @implementation RTCNativeNV12Shader { |
| 41 GLuint _vertexBuffer; |
| 42 GLuint _nv12Program; |
| 43 GLint _ySampler; |
| 44 GLint _uvSampler; |
| 45 RTCNV12TextureCache *_textureCache; |
| 46 // Store current rotation and only upload new vertex data when rotation |
| 47 // changes. |
| 48 rtc::Optional<RTCVideoRotation> _currentRotation; |
| 49 } |
| 50 |
| 51 - (instancetype)initWithContext:(GlContextType *)context { |
| 52 if (self = [super init]) { |
| 53 _textureCache = [[RTCNV12TextureCache alloc] initWithContext:context]; |
| 54 if (!_textureCache || ![self setupNV12Program] || |
| 55 !RTCSetupVerticesForProgram(_nv12Program, &_vertexBuffer, nullptr)) { |
| 56 RTCLog(@"Failed to initialize RTCNativeNV12Shader."); |
| 57 self = nil; |
| 58 } |
| 59 } |
| 60 return self; |
| 61 } |
| 62 |
| 63 - (void)dealloc { |
| 64 glDeleteProgram(_nv12Program); |
| 65 glDeleteBuffers(1, &_vertexBuffer); |
| 66 } |
| 67 |
| 68 - (BOOL)setupNV12Program { |
| 69 _nv12Program = RTCCreateProgramFromFragmentSource(kNV12FragmentShaderSource); |
| 70 if (!_nv12Program) { |
| 71 return NO; |
| 72 } |
| 73 _ySampler = glGetUniformLocation(_nv12Program, "s_textureY"); |
| 74 _uvSampler = glGetUniformLocation(_nv12Program, "s_textureUV"); |
| 75 |
| 76 return (_ySampler >= 0 && _uvSampler >= 0); |
| 77 } |
| 78 |
| 79 - (BOOL)drawFrame:(RTCVideoFrame *)frame { |
| 80 glUseProgram(_nv12Program); |
| 81 if (![_textureCache uploadFrameToTextures:frame]) { |
| 82 return NO; |
| 83 } |
| 84 |
| 85 // Y-plane. |
| 86 glActiveTexture(GL_TEXTURE0); |
| 87 glUniform1i(_ySampler, 0); |
| 88 glBindTexture(GL_TEXTURE_2D, _textureCache.yTexture); |
| 89 |
| 90 // UV-plane. |
| 91 glActiveTexture(GL_TEXTURE1); |
| 92 glUniform1i(_uvSampler, 1); |
| 93 glBindTexture(GL_TEXTURE_2D, _textureCache.uvTexture); |
| 94 |
| 95 glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); |
| 96 if (!_currentRotation || frame.rotation != *_currentRotation) { |
| 97 _currentRotation = rtc::Optional<RTCVideoRotation>(frame.rotation); |
| 98 RTCSetVertexData(*_currentRotation); |
| 99 } |
| 100 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 101 |
| 102 [_textureCache releaseTextures]; |
| 103 |
| 104 return YES; |
| 105 } |
| 106 |
| 107 @end |
OLD | NEW |