Created
December 20, 2013 05:51
-
-
Save brianchasalow/8050924 to your computer and use it in GitHub Desktop.
derp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bindTextureFromPixelBufferFastPathES(VideoCacheData* videoInstance) | |
{ | |
@autoreleasepool | |
{ | |
UnityAVFoundationPlayer* videoPtr = (UnityAVFoundationPlayer*) videoInstance->videoPtr; | |
if(videoPtr == 0 || videoPtr == nil) | |
return; | |
if(!glIsTexture(videoInstance->textureID)) | |
{ | |
// NSLog(@"%i is not a texture! cannot publish client texture.", videoInstance->textureID); | |
return; | |
} | |
if(videoInstance->textureWidth == 0 || videoInstance->textureHeight == 0) | |
{ | |
// NSLog(@"w or h is zero, returning "); | |
return; | |
} | |
BOOL consumable = videoPtr->isFrameNew(); | |
if(consumable) | |
{ | |
CVPixelBufferRef latestPixelBuffer = videoPtr->getPixelsRef(); | |
CVPixelBufferLockBaseAddress(latestPixelBuffer, kCVPixelBufferLock_ReadOnly); | |
if(latestPixelBuffer != NULL) | |
{ | |
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) | |
#else | |
VideoTextureBackingType backingType = videoPtr->getBackingType(); | |
if(backingType==PIXELS_ONLY) | |
{ | |
//do some shit here, then return! | |
copyAndFlipVideoPixels(latestPixelBuffer, videoInstance); | |
CVPixelBufferUnlockBaseAddress(latestPixelBuffer, kCVPixelBufferLock_ReadOnly); | |
CVPixelBufferRelease(latestPixelBuffer); | |
return; | |
} | |
if(backingType==TEXTURE_AND_PIXELS) | |
{ | |
copyAndFlipVideoPixels(latestPixelBuffer, videoInstance); | |
//do some shit, then continue! you're already pixel buffer locked... | |
} | |
YUVTextureRefStruct textureRef = videoPtr->GetLatestTextureFromCache(latestPixelBuffer); | |
CVPixelBufferUnlockBaseAddress(latestPixelBuffer, kCVPixelBufferLock_ReadOnly); | |
CVPixelBufferRelease(latestPixelBuffer); | |
if(!textureRef.y || !textureRef.uv) | |
{ | |
NSLog(@"error: no texture returned from texture cache?"); | |
if(textureRef.y){ | |
CFRelease(textureRef.y); | |
textureRef.y = nil; | |
} | |
if(textureRef.uv){ | |
CFRelease(textureRef.uv); | |
textureRef.uv = nil; | |
} | |
return; | |
} | |
GLint oldFBO; | |
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFBO); | |
glBindFramebuffer(GL_FRAMEBUFFER, videoPtr->getRenderingFBO()); | |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, videoInstance->textureID, 0); | |
GLenum status; | |
status = glCheckFramebufferStatus(GL_FRAMEBUFFER); | |
if(status != GL_FRAMEBUFFER_COMPLETE) | |
{ | |
NSLog(@"ERROR: UNITY PLUGIN CANNOT MAKE VALID FBO ATTACHMENT FROM UNITY TEXTURE ID. tex id is %i, error is %i", videoInstance->textureID, status); | |
if(textureRef.y){ | |
CFRelease(textureRef.y); | |
textureRef.y = nil; | |
} | |
if(textureRef.uv){ | |
CFRelease(textureRef.uv); | |
textureRef.uv = nil; | |
} | |
return; | |
} | |
glDisable (GL_CULL_FACE); | |
// glDisable (GL_LIGHTING); | |
glDisable (GL_BLEND); | |
// glDisable (GL_ALPHA_TEST); | |
glDepthFunc (GL_LEQUAL); | |
glEnable (GL_DEPTH_TEST); | |
glDepthMask (GL_FALSE); | |
glViewport(0, 0, videoInstance->textureWidth, videoInstance->textureHeight); | |
glUseProgram([[VideoManager sharedVideoManager] _program]); | |
glActiveTexture(GL_TEXTURE1); | |
glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(textureRef.uv)); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
glActiveTexture(GL_TEXTURE0); | |
glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(textureRef.y)); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
// populate initial texture coordinates | |
GLfloat textureCoordinates[8]; | |
// CVOpenGLESTextureGetCleanTexCoords outputs lower left, lower right, upper right, upper left | |
// but we want lower right, lower left, upper right, upper left | |
// if(CVImageBufferIsFlipped(textureRef.y)) | |
CVOpenGLESTextureGetCleanTexCoords(textureRef.y, &textureCoordinates[2], &textureCoordinates[0], &textureCoordinates[4], &textureCoordinates[6]); | |
GLfloat squareVertices[] = { | |
1.0f, -1.0f, | |
-1.0f, -1.0f, | |
1.0f, 1.0f, | |
-1.0f, 1.0f, | |
}; | |
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0,0, squareVertices); | |
glEnableVertexAttribArray(ATTRIB_VERTEX); | |
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, 0,0, textureCoordinates); | |
glEnableVertexAttribArray(ATTRIB_TEXCOORD); | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
//i dont need these? | |
// glDisableVertexAttribArray(ATTRIB_VERTEX); | |
// glDisableVertexAttribArray(ATTRIB_TEXCOORD); | |
glUseProgram(0); | |
glBindTexture(GL_TEXTURE_2D, 0); | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
glFlush(); | |
#endif | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment