blob: c0769c05cda4f3e169bee3a29cbfd8c28ddc299f [file] [log] [blame]
tkchin04dbb342016-08-08 03:10:07 -07001/*
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
magjed15f0c682017-04-27 01:26:33 -070013#import "RTCNV12TextureCache.h"
tkchin04dbb342016-08-08 03:10:07 -070014#import "RTCShader+Private.h"
magjed2f7f9b82017-04-13 04:15:53 -070015#import "WebRTC/RTCLogging.h"
tkchin04dbb342016-08-08 03:10:07 -070016#import "WebRTC/RTCVideoFrame.h"
17
18#include "webrtc/base/checks.h"
magjedfb372f02016-08-10 07:58:29 -070019#include "webrtc/base/optional.h"
tkchin04dbb342016-08-08 03:10:07 -070020
21static 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;
magjed15f0c682017-04-27 01:26:33 -070045 RTCNV12TextureCache *_textureCache;
magjedfb372f02016-08-10 07:58:29 -070046 // Store current rotation and only upload new vertex data when rotation
47 // changes.
magjed7ee51252017-02-21 04:19:46 -080048 rtc::Optional<RTCVideoRotation> _currentRotation;
tkchin04dbb342016-08-08 03:10:07 -070049}
50
51- (instancetype)initWithContext:(GlContextType *)context {
52 if (self = [super init]) {
magjed15f0c682017-04-27 01:26:33 -070053 _textureCache = [[RTCNV12TextureCache alloc] initWithContext:context];
54 if (!_textureCache || ![self setupNV12Program] ||
tkchin04dbb342016-08-08 03:10:07 -070055 !RTCSetupVerticesForProgram(_nv12Program, &_vertexBuffer, nullptr)) {
magjed2f7f9b82017-04-13 04:15:53 -070056 RTCLog(@"Failed to initialize RTCNativeNV12Shader.");
tkchin04dbb342016-08-08 03:10:07 -070057 self = nil;
58 }
59 }
60 return self;
61}
62
63- (void)dealloc {
64 glDeleteProgram(_nv12Program);
65 glDeleteBuffers(1, &_vertexBuffer);
tkchin04dbb342016-08-08 03:10:07 -070066}
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
tkchin04dbb342016-08-08 03:10:07 -070079- (BOOL)drawFrame:(RTCVideoFrame *)frame {
tkchin04dbb342016-08-08 03:10:07 -070080 glUseProgram(_nv12Program);
magjed15f0c682017-04-27 01:26:33 -070081 if (![_textureCache uploadFrameToTextures:frame]) {
82 return NO;
83 }
tkchin04dbb342016-08-08 03:10:07 -070084
85 // Y-plane.
tkchin04dbb342016-08-08 03:10:07 -070086 glActiveTexture(GL_TEXTURE0);
87 glUniform1i(_ySampler, 0);
magjed15f0c682017-04-27 01:26:33 -070088 glBindTexture(GL_TEXTURE_2D, _textureCache.yTexture);
tkchin04dbb342016-08-08 03:10:07 -070089
90 // UV-plane.
tkchin04dbb342016-08-08 03:10:07 -070091 glActiveTexture(GL_TEXTURE1);
92 glUniform1i(_uvSampler, 1);
magjed15f0c682017-04-27 01:26:33 -070093 glBindTexture(GL_TEXTURE_2D, _textureCache.uvTexture);
tkchin04dbb342016-08-08 03:10:07 -070094
95 glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
magjedfb372f02016-08-10 07:58:29 -070096 if (!_currentRotation || frame.rotation != *_currentRotation) {
magjed7ee51252017-02-21 04:19:46 -080097 _currentRotation = rtc::Optional<RTCVideoRotation>(frame.rotation);
magjedfb372f02016-08-10 07:58:29 -070098 RTCSetVertexData(*_currentRotation);
99 }
tkchin04dbb342016-08-08 03:10:07 -0700100 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
101
magjed15f0c682017-04-27 01:26:33 -0700102 [_textureCache releaseTextures];
tkchin04dbb342016-08-08 03:10:07 -0700103
104 return YES;
105}
106
107@end