STGLPreview.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // STGLPreview.m
  3. //
  4. // Created by sluin on 2017/1/11.
  5. // Copyright © 2017年 SenseTime. All rights reserved.
  6. //
  7. #import "STGLPreview.h"
  8. #import "STVFShader.h"
  9. // Attribute index.
  10. enum {
  11. ATTRIB_VERTEX,
  12. ATTRIB_TEXTUREPOSITON,
  13. NUM_ATTRIBUTES
  14. };
  15. @interface STGLPreview ()
  16. {
  17. /* The pixel dimensions of the backbuffer */
  18. GLint backingWidth, backingHeight;
  19. /* OpenGL names for the renderbuffer and framebuffers used to render to this view */
  20. GLuint viewRenderbuffer, viewFramebuffer;
  21. GLuint positionRenderTexture;
  22. GLuint positionRenderbuffer, positionFramebuffer;
  23. GLuint stDisplayProgram;
  24. int uniformLocation;
  25. }
  26. @end
  27. @implementation STGLPreview
  28. // Override the class method to return the OpenGL layer, as opposed to the normal CALayer
  29. + (Class) layerClass
  30. {
  31. return [CAEAGLLayer class];
  32. }
  33. - (instancetype)initWithFrame:(CGRect)frame context:(EAGLContext *)context
  34. {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. // Set scaling to account for Retina display
  38. if ([self respondsToSelector:@selector(setContentScaleFactor:)])
  39. {
  40. self.contentScaleFactor = [[UIScreen mainScreen] scale];
  41. }
  42. // Do OpenGL Core Animation layer setup
  43. CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  44. eaglLayer.opaque = YES;
  45. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  46. _glContext = context;
  47. if (!_glContext) {
  48. return nil;
  49. }
  50. if ([EAGLContext currentContext] != _glContext) {
  51. if (![EAGLContext setCurrentContext:_glContext]) {
  52. return nil;
  53. }
  54. }
  55. if (![self createFramebuffers]) {
  56. return nil;
  57. }
  58. [self loadVertexShader:@"STDisplayShader"
  59. fragmentShader:@"STDisplayShader"
  60. forProgram:&stDisplayProgram];
  61. self.clipsToBounds = YES;
  62. self.contentMode = UIViewContentModeScaleAspectFill;
  63. }
  64. return self;
  65. }
  66. - (void)dealloc
  67. {
  68. [self destroyFramebuffer];
  69. }
  70. - (BOOL)createFramebuffers
  71. {
  72. glEnable(GL_TEXTURE_2D);
  73. glDisable(GL_DEPTH_TEST);
  74. // Onscreen framebuffer object
  75. glGenFramebuffers(1, &viewFramebuffer);
  76. glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
  77. glGenRenderbuffers(1, &viewRenderbuffer);
  78. glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
  79. [_glContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  80. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
  81. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
  82. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer);
  83. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  84. NSLog(@"STGLPreview : failure with framebuffer generation");
  85. return NO;
  86. }
  87. // Offscreen position framebuffer object
  88. glGenFramebuffers(1, &positionFramebuffer);
  89. glBindFramebuffer(GL_FRAMEBUFFER, positionFramebuffer);
  90. glGenRenderbuffers(1, &positionRenderbuffer);
  91. glBindRenderbuffer(GL_RENDERBUFFER, positionRenderbuffer);
  92. // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, (GLsizei)self.frame.size.width, (GLsizei)self.frame.size.height);
  93. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, positionRenderbuffer);
  94. // Offscreen position framebuffer texture target
  95. glGenTextures(1, &positionRenderTexture);
  96. glBindTexture(GL_TEXTURE_2D, positionRenderTexture);
  97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  100. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  101. glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
  102. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)self.frame.size.width, (GLsizei)self.frame.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  103. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionRenderTexture, 0);
  104. return YES;
  105. }
  106. - (void)destroyFramebuffer;
  107. {
  108. if (viewFramebuffer) {
  109. glDeleteFramebuffers(1, &viewFramebuffer);
  110. viewFramebuffer = 0;
  111. }
  112. if (viewRenderbuffer) {
  113. glDeleteRenderbuffers(1, &viewRenderbuffer);
  114. viewRenderbuffer = 0;
  115. }
  116. }
  117. - (BOOL)loadVertexShader:(NSString *)vertexShaderName
  118. fragmentShader:(NSString *)fragmentShaderName
  119. forProgram:(GLuint *)programPointer;
  120. {
  121. GLuint vertexShader, fragShader;
  122. // Create shader program.
  123. *programPointer = glCreateProgram();
  124. // Create and compile vertex shader.
  125. if (![self compileShader:&vertexShader type:GL_VERTEX_SHADER shaderString:vsh]) {
  126. NSLog(@"STGLPreview : failed to compile vertex shader");
  127. return NO;
  128. }
  129. // Create and compile fragment shader.
  130. if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER shaderString:fsh]) {
  131. NSLog(@"STGLPreview : failed to compile fragment shader");
  132. return NO;
  133. }
  134. // Attach vertex shader to program.
  135. glAttachShader(*programPointer, vertexShader);
  136. // Attach fragment shader to program.
  137. glAttachShader(*programPointer, fragShader);
  138. // Bind attribute locations.
  139. // This needs to be done prior to linking.
  140. glBindAttribLocation(*programPointer, ATTRIB_VERTEX, "position");
  141. glBindAttribLocation(*programPointer, ATTRIB_TEXTUREPOSITON, "inputTextureCoordinate");
  142. // Link program.
  143. if (![self linkProgram:*programPointer]) {
  144. NSLog(@"STGLPreview : failed to link program: %d", *programPointer);
  145. if (vertexShader) {
  146. glDeleteShader(vertexShader);
  147. vertexShader = 0;
  148. }
  149. if (fragShader) {
  150. glDeleteShader(fragShader);
  151. fragShader = 0;
  152. }
  153. if (*programPointer) {
  154. glDeleteProgram(*programPointer);
  155. *programPointer = 0;
  156. }
  157. return NO;
  158. }
  159. // Get uniform locations.
  160. uniformLocation = glGetUniformLocation(*programPointer, "videoFrame");
  161. // Release vertex and fragment shaders.
  162. if (vertexShader) {
  163. glDeleteShader(vertexShader);
  164. }
  165. if (fragShader) {
  166. glDeleteShader(fragShader);
  167. }
  168. return YES;
  169. }
  170. - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type shaderString:(const char *)str
  171. {
  172. GLint status;
  173. const GLchar *source;
  174. source = (GLchar *)str;
  175. if (!source) {
  176. NSLog(@"STGLPreview : failed to load vertex shader");
  177. return NO;
  178. }
  179. *shader = glCreateShader(type);
  180. glShaderSource(*shader, 1, &source, NULL);
  181. glCompileShader(*shader);
  182. glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
  183. if (status == 0) {
  184. glDeleteShader(*shader);
  185. return NO;
  186. }
  187. return YES;
  188. }
  189. - (BOOL)linkProgram:(GLuint)prog
  190. {
  191. GLint status;
  192. glLinkProgram(prog);
  193. glGetProgramiv(prog, GL_LINK_STATUS, &status);
  194. if (0 == status) {
  195. return NO;
  196. }else{
  197. return YES;
  198. }
  199. }
  200. - (void)renderTexture:(GLuint)texture
  201. {
  202. if ([EAGLContext setCurrentContext:self.glContext]) {
  203. [self drawFrameWithTexture:texture];
  204. }
  205. }
  206. - (BOOL)drawFrameWithTexture:(GLuint)texture
  207. {
  208. static const GLfloat squareVertices[] = {
  209. -1.0f, -1.0f,
  210. 1.0f, -1.0f,
  211. -1.0f, 1.0f,
  212. 1.0f, 1.0f,
  213. };
  214. static const GLfloat textureVertices[] = {
  215. 0.0f, 1.0f,
  216. 1.0f, 1.0f,
  217. 0.0f, 0.0f,
  218. 1.0f, 0.0f,
  219. };
  220. // Use shader program.
  221. if (!viewFramebuffer) {
  222. [self createFramebuffers];
  223. }
  224. glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
  225. glViewport(0, 0, backingWidth, backingHeight);
  226. glUseProgram(stDisplayProgram);
  227. glActiveTexture(GL_TEXTURE0);
  228. glBindTexture(GL_TEXTURE_2D, texture);
  229. // Update uniform values
  230. glUniform1i(uniformLocation, 0);
  231. // Update attribute values.
  232. glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
  233. glEnableVertexAttribArray(ATTRIB_VERTEX);
  234. glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
  235. glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);
  236. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  237. BOOL isSuccess = NO;
  238. if (_glContext) {
  239. glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
  240. isSuccess = [_glContext presentRenderbuffer:GL_RENDERBUFFER];
  241. }
  242. return isSuccess;
  243. }
  244. @end