tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 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 | |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 13 | #import "RTCNV12TextureCache.h" |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 14 | #import "RTCShader+Private.h" |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 15 | #import "WebRTC/RTCLogging.h" |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 16 | #import "WebRTC/RTCVideoFrame.h" |
| 17 | |
| 18 | #include "webrtc/base/checks.h" |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 19 | #include "webrtc/base/optional.h" |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 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; |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 45 | RTCNV12TextureCache *_textureCache; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 46 | // Store current rotation and only upload new vertex data when rotation |
| 47 | // changes. |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 48 | rtc::Optional<RTCVideoRotation> _currentRotation; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | - (instancetype)initWithContext:(GlContextType *)context { |
| 52 | if (self = [super init]) { |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 53 | _textureCache = [[RTCNV12TextureCache alloc] initWithContext:context]; |
| 54 | if (!_textureCache || ![self setupNV12Program] || |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 55 | !RTCSetupVerticesForProgram(_nv12Program, &_vertexBuffer, nullptr)) { |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 56 | RTCLog(@"Failed to initialize RTCNativeNV12Shader."); |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 57 | self = nil; |
| 58 | } |
| 59 | } |
| 60 | return self; |
| 61 | } |
| 62 | |
| 63 | - (void)dealloc { |
| 64 | glDeleteProgram(_nv12Program); |
| 65 | glDeleteBuffers(1, &_vertexBuffer); |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 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 | |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 79 | - (BOOL)drawFrame:(RTCVideoFrame *)frame { |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 80 | glUseProgram(_nv12Program); |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 81 | if (![_textureCache uploadFrameToTextures:frame]) { |
| 82 | return NO; |
| 83 | } |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 84 | |
| 85 | // Y-plane. |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 86 | glActiveTexture(GL_TEXTURE0); |
| 87 | glUniform1i(_ySampler, 0); |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 88 | glBindTexture(GL_TEXTURE_2D, _textureCache.yTexture); |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 89 | |
| 90 | // UV-plane. |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 91 | glActiveTexture(GL_TEXTURE1); |
| 92 | glUniform1i(_uvSampler, 1); |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 93 | glBindTexture(GL_TEXTURE_2D, _textureCache.uvTexture); |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 94 | |
| 95 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 96 | if (!_currentRotation || frame.rotation != *_currentRotation) { |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 97 | _currentRotation = rtc::Optional<RTCVideoRotation>(frame.rotation); |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 98 | RTCSetVertexData(*_currentRotation); |
| 99 | } |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 100 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 101 | |
magjed | 15f0c68 | 2017-04-27 01:26:33 -0700 | [diff] [blame^] | 102 | [_textureCache releaseTextures]; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 103 | |
| 104 | return YES; |
| 105 | } |
| 106 | |
| 107 | @end |