| 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 "RTCI420TextureCache.h" | |
| 14 #import "RTCShader+Private.h" | |
| 15 #import "WebRTC/RTCLogging.h" | |
| 16 #import "WebRTC/RTCVideoFrame.h" | |
| 17 | |
| 18 #include "webrtc/base/optional.h" | |
| 19 | |
| 20 // Fragment shader converts YUV values from input textures into a final RGB | |
| 21 // pixel. The conversion formula is from http://www.fourcc.org/fccyvrgb.php. | |
| 22 static const char kI420FragmentShaderSource[] = | |
| 23 SHADER_VERSION | |
| 24 "precision highp float;" | |
| 25 FRAGMENT_SHADER_IN " vec2 v_texcoord;\n" | |
| 26 "uniform lowp sampler2D s_textureY;\n" | |
| 27 "uniform lowp sampler2D s_textureU;\n" | |
| 28 "uniform lowp sampler2D s_textureV;\n" | |
| 29 FRAGMENT_SHADER_OUT | |
| 30 "void main() {\n" | |
| 31 " float y, u, v, r, g, b;\n" | |
| 32 " y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n" | |
| 33 " u = " FRAGMENT_SHADER_TEXTURE "(s_textureU, v_texcoord).r;\n" | |
| 34 " v = " FRAGMENT_SHADER_TEXTURE "(s_textureV, v_texcoord).r;\n" | |
| 35 " u = u - 0.5;\n" | |
| 36 " v = v - 0.5;\n" | |
| 37 " r = y + 1.403 * v;\n" | |
| 38 " g = y - 0.344 * u - 0.714 * v;\n" | |
| 39 " b = y + 1.770 * u;\n" | |
| 40 " " FRAGMENT_SHADER_COLOR " = vec4(r, g, b, 1.0);\n" | |
| 41 " }\n"; | |
| 42 | |
| 43 @implementation RTCI420Shader { | |
| 44 RTCI420TextureCache* textureCache; | |
| 45 // Handles for OpenGL constructs. | |
| 46 GLuint _i420Program; | |
| 47 GLuint _vertexArray; | |
| 48 GLuint _vertexBuffer; | |
| 49 GLint _ySampler; | |
| 50 GLint _uSampler; | |
| 51 GLint _vSampler; | |
| 52 // Store current rotation and only upload new vertex data when rotation | |
| 53 // changes. | |
| 54 rtc::Optional<RTCVideoRotation> _currentRotation; | |
| 55 } | |
| 56 | |
| 57 - (instancetype)initWithContext:(GlContextType *)context { | |
| 58 if (self = [super init]) { | |
| 59 textureCache = [[RTCI420TextureCache alloc] initWithContext:context]; | |
| 60 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 61 if (![self setupI420Program] || | |
| 62 !RTCSetupVerticesForProgram(_i420Program, &_vertexBuffer, &_vertexArray)
) { | |
| 63 RTCLog(@"Failed to initialize RTCI420Shader."); | |
| 64 self = nil; | |
| 65 } | |
| 66 } | |
| 67 return self; | |
| 68 } | |
| 69 | |
| 70 - (void)dealloc { | |
| 71 glDeleteProgram(_i420Program); | |
| 72 glDeleteBuffers(1, &_vertexBuffer); | |
| 73 glDeleteVertexArrays(1, &_vertexArray); | |
| 74 } | |
| 75 | |
| 76 - (BOOL)setupI420Program { | |
| 77 _i420Program = RTCCreateProgramFromFragmentSource(kI420FragmentShaderSource); | |
| 78 if (!_i420Program) { | |
| 79 return NO; | |
| 80 } | |
| 81 _ySampler = glGetUniformLocation(_i420Program, "s_textureY"); | |
| 82 _uSampler = glGetUniformLocation(_i420Program, "s_textureU"); | |
| 83 _vSampler = glGetUniformLocation(_i420Program, "s_textureV"); | |
| 84 | |
| 85 return (_ySampler >= 0 && _uSampler >= 0 && _vSampler >= 0); | |
| 86 } | |
| 87 | |
| 88 - (BOOL)drawFrame:(RTCVideoFrame*)frame { | |
| 89 glUseProgram(_i420Program); | |
| 90 | |
| 91 [textureCache uploadFrameToTextures:frame]; | |
| 92 | |
| 93 #if !TARGET_OS_IPHONE | |
| 94 glBindVertexArray(_vertexArray); | |
| 95 #endif | |
| 96 | |
| 97 glActiveTexture(GL_TEXTURE0); | |
| 98 glBindTexture(GL_TEXTURE_2D, textureCache.yTexture); | |
| 99 glUniform1i(_ySampler, 0); | |
| 100 | |
| 101 glActiveTexture(GL_TEXTURE1); | |
| 102 glBindTexture(GL_TEXTURE_2D, textureCache.uTexture); | |
| 103 glUniform1i(_uSampler, 1); | |
| 104 | |
| 105 glActiveTexture(GL_TEXTURE2); | |
| 106 glBindTexture(GL_TEXTURE_2D, textureCache.vTexture); | |
| 107 glUniform1i(_vSampler, 2); | |
| 108 | |
| 109 glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); | |
| 110 if (!_currentRotation || frame.rotation != *_currentRotation) { | |
| 111 _currentRotation = rtc::Optional<RTCVideoRotation>(frame.rotation); | |
| 112 RTCSetVertexData(*_currentRotation); | |
| 113 } | |
| 114 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | |
| 115 | |
| 116 return YES; | |
| 117 } | |
| 118 | |
| 119 @end | |
| OLD | NEW |