20 #include <CoreFoundation/CoreFoundation.h>
22 #include <IOSurface/IOSurfaceAPI.h>
24 #include <OpenGL/OpenGL.h>
25 #include <OpenGL/CGLMacro.h>
37 "description" :
"An image residing in GPU memory (GL Texture Object).",
51 "CoreFoundation.framework",
72 if (t->cpuQueueInitialized && json_object_object_length(t->cpuData))
75 glBindTexture(t->glTextureTarget, t->glTextureName);
76 GLenum format = GL_BGRA;
77 if (t->glInternalFormat == GL_DEPTH_COMPONENT)
78 format = GL_DEPTH_COMPONENT;
81 glTexImage2D(t->glTextureTarget, 0, t->glInternalFormat, t->pixelsWide, t->pixelsHigh, 0, format, type, NULL);
82 glBindTexture(t->glTextureTarget, 0);
86 VuoGlTexture_release(VuoGlTexturePool_Allocate, t->glTextureTarget, t->glInternalFormat, t->pixelsWide, t->pixelsHigh, t->glTextureName);
88 if (t->cpuQueueInitialized)
90 dispatch_release(t->cpuQueue);
92 json_object_object_foreach(t->cpuData, key, value)
96 json_object_object_get_ex(value,
"buffer", &o);
97 void *buffer = (
void *)json_object_get_int64(o);
99 json_object_object_get_ex(value,
"freeCallback", &o);
100 void (^freeCallback)(
void *) = (
void (^)(
void *))json_object_get_int64(o);
102 freeCallback(buffer);
104 Block_release(freeCallback);
107 json_object_put(t->cpuData);
121 t->glTextureName = glTextureName;
122 t->glTextureTarget = GL_TEXTURE_2D;
123 t->glInternalFormat = glInternalFormat;
124 t->pixelsWide = pixelsWide;
125 t->pixelsHigh = pixelsHigh;
128 t->freeCallbackContext = freeCallbackContext;
132 t->cpuQueueInitialized = 0;
156 VuoImage VuoImage_make(
unsigned int glTextureName,
unsigned int glInternalFormat,
unsigned long int pixelsWide,
unsigned long int pixelsHigh)
158 if (!glTextureName || !pixelsWide || !pixelsHigh)
191 VUserLog(
"Error: freeCallback may not be NULL.");
195 if (!glTextureName || !pixelsWide || !pixelsHigh)
198 return VuoImage_make_internal(glTextureName, glInternalFormat, pixelsWide, pixelsHigh, freeCallback, freeCallbackContext);
231 t->glTextureTarget = GL_TEXTURE_RECTANGLE_ARB;
269 if (!pixels || !pixelsWide || !pixelsHigh)
274 static bool freeCallbackWarningEmitted =
false;
275 if (!freeCallbackWarningEmitted)
277 freeCallbackWarningEmitted =
true;
278 VUserLog(
"VuoImage_makeFromBuffer() and VuoImage_makeFromBufferWithStride() now take ownership of `pixels`, and therefore `freeCallback` is required. Since there's no `freeCallback`, I'm giving up and outputting an empty image. Please update the plugin listed in the backtrace below.");
284 __block GLenum internalformat;
285 __block GLuint glTextureName;
286 __block
int alignment = 1;
287 __block
bool customRowLength =
false;
292 glTextureName =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, GL_TEXTURE_2D, internalformat, pixelsWide, pixelsHigh, format, NULL);
298 if (colorDepth == VuoImageColorDepth_8)
300 else if (colorDepth == VuoImageColorDepth_16)
302 glType = GL_HALF_FLOAT_ARB;
312 || bytesPerRow == bytesPerPixel * pixelsWide)
317 if (bytesPerRow % 4 == 0)
319 else if (bytesPerRow % 8 == 0)
321 else if (bytesPerRow % 2 == 0)
323 else if (bytesPerRow % bytesPerPixel == 0)
325 GLuint rowPixels = bytesPerRow / bytesPerPixel;
326 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowPixels);
327 customRowLength =
true;
331 VUserLog(
"Not sure how to handle this stride:");
332 VUserLog(
" %dx%d",pixelsWide,pixelsHigh);
333 VUserLog(
" bytesPerRow = %d",bytesPerRow);
334 VUserLog(
" bytesPerPixel = %d",bytesPerPixel);
335 GLint leftoverBytes = bytesPerRow - bytesPerPixel*pixelsWide;
336 VUserLog(
" leftoverBytes = %d",leftoverBytes);
341 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
343 glBindTexture(GL_TEXTURE_2D, glTextureName);
344 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
346 glTexImage2D(GL_TEXTURE_2D, 0, internalformat, pixelsWide, pixelsHigh, 0, format, glType, (GLvoid *)pixels);
347 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
348 glBindTexture(GL_TEXTURE_2D, 0);
351 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
353 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
358 glFlushRenderAPPLE();
367 dispatch_once(&image->cpuQueueInitialized, ^{});
368 image->cpuQueue = dispatch_queue_create(
"org.vuo.image.cpu", NULL);
369 image->cpuData = json_object_new_object();
373 char *keyWithAlignment;
374 asprintf(&keyWithAlignment,
"%s alignment=%d", key, alignment);
376 key = keyWithAlignment;
380 char *keyWithRowLength;
381 asprintf(&keyWithRowLength,
"%s rowLength=%d", key, bytesPerRow);
383 key = keyWithRowLength;
386 json_object_object_add(cpuEntry,
"buffer", json_object_new_int64((
long long)pixels));
387 json_object_object_add(cpuEntry,
"freeCallback", json_object_new_int64((
long long)Block_copy(freeCallback)));
388 json_object_object_add(image->cpuData, key, cpuEntry);
424 if (image->glInternalFormat == GL_DEPTH_COMPONENT && requestedFormat != GL_DEPTH_COMPONENT16)
426 VUserLog(
"Error: Image has format GL_DEPTH_COMPONENT, which must be fetched as GL_DEPTH_COMPONENT16.");
430 dispatch_once(&image->cpuQueueInitialized, ^{
431 image->cpuQueue = dispatch_queue_create(
"org.vuo.image.cpu", NULL);
432 image->cpuData = json_object_new_object();
435 __block
unsigned char *pixels = NULL;
436 dispatch_sync(image->cpuQueue, ^{
437 char *key = VuoGl_stringForConstant(requestedFormat);
438 struct json_object *value;
439 if (json_object_object_get_ex(image->cpuData, key, &value))
442 json_object_object_get_ex(value,
"buffer", &o);
443 pixels = (unsigned char *)json_object_get_int64(o);
447 unsigned int channels;
448 if (requestedFormat == GL_LUMINANCE
449 || requestedFormat == GL_R16
450 || requestedFormat == GL_DEPTH_COMPONENT16)
452 else if (requestedFormat == GL_LUMINANCE_ALPHA)
454 else if (requestedFormat == GL_RGB
455 || requestedFormat == GL_BGR)
457 else if (requestedFormat == GL_RGBA
458 || requestedFormat == GL_BGRA
459 || requestedFormat == GL_RGBA16I_EXT
460 || requestedFormat == GL_RGBA16F_ARB
461 || requestedFormat == GL_RGBA32F_ARB)
465 VUserLog(
"Error: Unknown format %s.", VuoGl_stringForConstant(requestedFormat));
469 unsigned int bytesPerChannel = 1;
470 GLuint type = GL_UNSIGNED_BYTE;
471 if (requestedFormat == GL_RGBA16I_EXT
472 || requestedFormat == GL_R16
473 || requestedFormat == GL_DEPTH_COMPONENT16)
476 type = GL_UNSIGNED_SHORT;
478 else if (requestedFormat == GL_RGBA16F_ARB)
481 type = GL_HALF_FLOAT_ARB;
483 else if (requestedFormat == GL_RGBA32F_ARB)
489 GLuint actualFormat = requestedFormat;
490 if (requestedFormat == GL_RGBA16I_EXT
491 || requestedFormat == GL_RGBA16F_ARB
492 || requestedFormat == GL_RGBA32F_ARB)
493 actualFormat = GL_BGRA;
494 else if (requestedFormat == GL_DEPTH_COMPONENT16)
495 actualFormat = GL_DEPTH_COMPONENT;
496 else if (requestedFormat == GL_R16)
497 actualFormat = GL_RED;
499 size_t pixelBufferSize = image->pixelsWide * image->pixelsHigh * channels * bytesPerChannel;
500 pixels = (
unsigned char *)malloc(pixelBufferSize);
504 const char *tamperEvidentSeal =
"Vuo\0Ima\0ge_\0get\0Buf\0fer\0()\0";
505 int tamperEvidentSealLength = strlen(tamperEvidentSeal);
506 if (pixelBufferSize > tamperEvidentSealLength)
507 strlcpy((
char *)pixels, tamperEvidentSeal, pixelBufferSize);
512 bool openGLAddsPadding = ((image->pixelsWide * channels * bytesPerChannel) % 4 != 0);
513 if (openGLAddsPadding)
515 glPixelStorei(GL_PACK_ALIGNMENT, 1);
517 glBindTexture(image->glTextureTarget, image->glTextureName);
519 glGetTexImage(image->glTextureTarget, 0, actualFormat, type, (GLvoid *)pixels);
520 glBindTexture(image->glTextureTarget, 0);
522 if (openGLAddsPadding)
524 glPixelStorei(GL_PACK_ALIGNMENT, 4);
526 if (pixelBufferSize > tamperEvidentSealLength && strncmp((
char *)pixels, tamperEvidentSeal, strlen(tamperEvidentSeal)) == 0)
528 GLenum error = glGetError();
529 if (error == GL_NO_ERROR)
533 VUserLog(
"Warning: glGetTexImage() says it was successful, but it didn't actually copy any data. This might happen if the input texture has an IOSurface bound to it.");
535 VUserLog(
"OpenGL Error: %d", error);
546 json_object_object_add(cpuEntry,
"buffer", json_object_new_int64((
long long)pixels));
547 json_object_object_add(cpuEntry,
"freeCallback", json_object_new_int64((
long long)Block_copy( ^(
void *buffer){ free(buffer); } )));
548 json_object_object_add(image->cpuData, key, cpuEntry);
562 __block GLint wrapModeGL;
565 glBindTexture(image->glTextureTarget, image->glTextureName);
567 glGetTexParameteriv(image->glTextureTarget, GL_TEXTURE_WRAP_S, &wrapModeGL);
570 glBindTexture(image->glTextureTarget, 0);
574 if (wrapModeGL == GL_CLAMP_TO_EDGE)
575 return VuoImageWrapMode_ClampEdge;
576 else if (wrapModeGL == GL_REPEAT)
577 return VuoImageWrapMode_Repeat;
578 else if (wrapModeGL == GL_MIRRORED_REPEAT)
579 return VuoImageWrapMode_MirroredRepeat;
581 return VuoImageWrapMode_None;
591 glBindTexture(image->glTextureTarget, image->glTextureName);
593 GLint wrapModeGL = GL_CLAMP_TO_BORDER;
594 if (wrapMode == VuoImageWrapMode_ClampEdge)
595 wrapModeGL = GL_CLAMP_TO_EDGE;
596 else if (wrapMode == VuoImageWrapMode_Repeat)
597 wrapModeGL = GL_REPEAT;
598 else if (wrapMode == VuoImageWrapMode_MirroredRepeat)
599 wrapModeGL = GL_MIRRORED_REPEAT;
601 glTexParameteri(image->glTextureTarget, GL_TEXTURE_WRAP_S, wrapModeGL);
602 glTexParameteri(image->glTextureTarget, GL_TEXTURE_WRAP_T, wrapModeGL);
604 glBindTexture(image->glTextureTarget, 0);
608 glFlushRenderAPPLE();
618 if (!pixelsWide || !pixelsHigh)
642 if (image->glTextureTarget == GL_TEXTURE_2D)
644 else if (image->glTextureTarget == GL_TEXTURE_RECTANGLE_ARB)
654 shader->isTransparent =
true;
657 forcePixelsWide ? forcePixelsWide : image->pixelsWide,
658 forcePixelsHigh ? forcePixelsHigh : image->pixelsHigh,
676 GLuint textureName = VuoImageRenderer_draw_internal(frag, image->pixelsWide, image->pixelsHigh,
VuoImage_getColorDepth(image),
false,
true, 0, NULL);
679 img->glTextureTarget = GL_TEXTURE_RECTANGLE_ARB;
696 if (a->pixelsWide != b->pixelsWide
697 || a->pixelsHigh != b->pixelsHigh)
700 if (a->glTextureName == b->glTextureName)
708 if (aChannels == 4 && bChannels == 1)
711 for (
unsigned int i = 0; i < a->pixelsWide * a->pixelsHigh; ++i)
712 if (abs(aPixels[i*4+0] - bPixels[i*4+2]) > tolerance
713 || abs(aPixels[i*4+1] - bPixels[i*4+2]) > tolerance
714 || abs(aPixels[i*4+2] - bPixels[i*4+2]) > tolerance
715 || abs(aPixels[i*4+3] - bPixels[i*4+3]) > tolerance)
717 VDebugLog(
"Difference found at pixel coordinate (%ld,%ld): RGBA %d,%d,%d,%d vs %d,%d,%d,%d",
718 i%a->pixelsWide, i/a->pixelsWide,
719 aPixels[i*4+2],aPixels[i*4+1],aPixels[i*4+0],aPixels[i*4+3],
720 bPixels[i*4+2],bPixels[i*4+2],bPixels[i*4+2],bPixels[i*4+3]);
725 else if (aChannels == 1 && bChannels == 4)
728 for (
unsigned int i = 0; i < a->pixelsWide * a->pixelsHigh; ++i)
729 if (abs(aPixels[i*4+2] - bPixels[i*4+0]) > tolerance
730 || abs(aPixels[i*4+2] - bPixels[i*4+1]) > tolerance
731 || abs(aPixels[i*4+2] - bPixels[i*4+2]) > tolerance
732 || abs(aPixels[i*4+3] - bPixels[i*4+3]) > tolerance)
734 VDebugLog(
"Difference found at pixel coordinate (%ld,%ld): RGBA %d,%d,%d,%d vs %d,%d,%d,%d",
735 i%a->pixelsWide, i/a->pixelsWide,
736 aPixels[i*4+2],aPixels[i*4+2],aPixels[i*4+2],aPixels[i*4+3],
737 bPixels[i*4+2],bPixels[i*4+1],bPixels[i*4+0],bPixels[i*4+3]);
743 for (
unsigned int i = 0; i < a->pixelsWide * a->pixelsHigh * 4; ++i)
744 if (abs(aPixels[i] - bPixels[i]) > tolerance)
746 unsigned int p = (i/4)*4;
747 VDebugLog(
"Difference found at pixel coordinate (%ld,%ld): abs(%d - %d) > %d (RGBA %d,%d,%d,%d vs %d,%d,%d,%d)",
748 i%a->pixelsWide, i/a->pixelsWide,
749 aPixels[i], bPixels[i], tolerance,
750 aPixels[p+2],aPixels[p+1],aPixels[p+0],aPixels[p+3],
751 bPixels[p+2],bPixels[p+1],bPixels[p+0],bPixels[p+3]);
786 if (a->pixelsWide < b->pixelsWide)
return true;
787 if (b->pixelsWide < a->pixelsWide)
return false;
789 if (a->pixelsHigh < b->pixelsHigh)
return true;
800 if (!image || image->pixelsWide == 0 || image->pixelsHigh == 0)
804 bool foundSubstantialPixel =
false;
805 for (
unsigned int p = 3; p < image->pixelsWide * image->pixelsHigh * 4; p += 4)
808 foundSubstantialPixel =
true;
811 return !foundSubstantialPixel;
822 if (!image || image->pixelsWide == 0 || image->pixelsHigh == 0)
826 bool foundNonBlackPixel =
false;
827 for (
unsigned int p = 0; p < image->pixelsWide * image->pixelsHigh; ++p)
828 if (pixels[p] > tolerance)
830 foundNonBlackPixel =
true;
833 return !foundNonBlackPixel;
843 return (image && image->pixelsWide && image->pixelsHigh);
860 return VuoImageColorDepth_8;
862 if (image->glInternalFormat == GL_LUMINANCE8
863 || image->glInternalFormat == GL_LUMINANCE8_ALPHA8
864 || image->glInternalFormat == GL_RGB
865 || image->glInternalFormat == GL_RGBA
866 || image->glInternalFormat == GL_RGBA8
867 || image->glInternalFormat == GL_BGRA
868 || image->glInternalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT
869 || image->glInternalFormat == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
870 return VuoImageColorDepth_8;
871 else if (image->glInternalFormat == GL_LUMINANCE16F_ARB
872 || image->glInternalFormat == GL_LUMINANCE_ALPHA16F_ARB
873 || image->glInternalFormat == GL_DEPTH_COMPONENT
874 || image->glInternalFormat == GL_RGB16
875 || image->glInternalFormat == GL_RGBA16
876 || image->glInternalFormat == GL_RGB16F_ARB
877 || image->glInternalFormat == GL_RGBA16F_ARB)
878 return VuoImageColorDepth_16;
879 else if (image->glInternalFormat == GL_LUMINANCE32F_ARB
880 || image->glInternalFormat == GL_LUMINANCE_ALPHA32F_ARB
881 || image->glInternalFormat == GL_RGB32F_ARB
882 || image->glInternalFormat == GL_RGBA32F_ARB)
883 return VuoImageColorDepth_32;
886 VUserLog(
"Error: Unknown glInternalFormat %x (%s)", image->glInternalFormat, formatString);
888 return VuoImageColorDepth_8;
938 VuoGlTexturePool_disuse(VuoGlTexturePool_AllocateIOSurface, i->glTextureTarget, i->glInternalFormat, i->pixelsWide, i->pixelsHigh, i->glTextureName);
959 if (json_object_object_get_ex(js,
"pointer", &o))
962 if ((requestedPixelsWide == 0 && requestedPixelsHigh == 0)
963 || (im->pixelsWide == requestedPixelsWide && im->pixelsHigh == requestedPixelsHigh))
967 VuoImage outputImage =
VuoImage_make(im->glTextureName, im->glInternalFormat, requestedPixelsWide, requestedPixelsHigh);
968 outputImage->glTextureTarget = im->glTextureTarget;
969 outputImage->scaleFactor = im->scaleFactor;
975 __block
unsigned int glInternalFormat = 0;
976 unsigned long int pixelsWide;
977 unsigned long int pixelsHigh;
978 float scaleFactor = 1;
982 if (json_object_object_get_ex(js,
"pixelsWide", &o))
983 pixelsWide = json_object_get_int64(o);
989 if (json_object_object_get_ex(js,
"pixelsHigh", &o))
990 pixelsHigh = json_object_get_int64(o);
994 if (pixelsWide == 0 || pixelsHigh == 0)
999 if (json_object_object_get_ex(js,
"scaleFactor", &o))
1000 scaleFactor = json_object_get_double(o);
1005 if (json_object_object_get_ex(js,
"color", &o))
1011 if (json_object_object_get_ex(js,
"ioSurface", &o))
1013 IOSurfaceID surfID = json_object_get_int(o);
1017 __block IOSurfaceRef surf = NULL;
1018 __block GLuint textureRect;
1020 glInternalFormat = GL_RGBA;
1021 surf = IOSurfaceLookup(surfID);
1024 VUserLog(
"Error: IOSurfaceLookup(%d) failed.", surfID);
1027 textureRect =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_AllocateIOSurface, GL_TEXTURE_RECTANGLE_ARB, glInternalFormat, pixelsWide, pixelsHigh, GL_BGRA, surf);
1028 glFlushRenderAPPLE();
1038 imageRect->glTextureTarget = GL_TEXTURE_RECTANGLE_ARB;
1045 requestedPixelsWide ? requestedPixelsWide : pixelsWide,
1046 requestedPixelsHigh ? requestedPixelsHigh : pixelsHigh,
1053 image2d->scaleFactor = scaleFactor;
1086 if (!json_object_object_get_ex(js,
"pixelsWide", &o))
1088 *outputPixelsWide = json_object_get_int64(o);
1090 if (!json_object_object_get_ex(js,
"pixelsHigh", &o))
1092 *outputPixelsHigh = json_object_get_int64(o);
1094 if (!json_object_object_get_ex(js,
"ioSurface", &o))
1096 IOSurfaceID surfID = json_object_get_int(o);
1098 GLuint textureRect = provider(*outputPixelsWide, *outputPixelsHigh);
1103 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureRect);
1105 IOSurfaceRef *surf = outputIOSurface;
1106 *surf = IOSurfaceLookup(surfID);
1109 VUserLog(
"Error: IOSurfaceLookup(%d) failed.", surfID);
1113 CGLError err = CGLTexImageIOSurface2D(cgl_ctx, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, (GLsizei)*outputPixelsWide, (GLsizei)*outputPixelsHigh, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, *surf, 0);
1114 if(err != kCGLNoError)
1116 VUserLog(
"Error in CGLTexImageIOSurface2D(): %s", CGLErrorString(err));
1119 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
1120 glFlushRenderAPPLE();
1151 if (!clientTextureName)
1156 if (!json_object_object_get_ex(js,
"pixelsWide", &o))
1158 unsigned long inputPixelsWide = json_object_get_int64(o);
1160 if (!json_object_object_get_ex(js,
"pixelsHigh", &o))
1162 unsigned long inputPixelsHigh = json_object_get_int64(o);
1164 if (!json_object_object_get_ex(js,
"ioSurface", &o))
1166 IOSurfaceID surfID = json_object_get_int(o);
1168 __block
bool success =
true;
1170 IOSurfaceRef *surf = outputIOSurface;
1171 *surf = IOSurfaceLookup(surfID);
1174 VUserLog(
"Error: IOSurfaceLookup(%d) failed.", surfID);
1179 bool shouldResize = (inputPixelsWide != pixelsWide
1180 || inputPixelsHigh != pixelsHigh);
1185 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, clientTextureName);
1186 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, pixelsWide, pixelsHigh, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
1187 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
1189 GLuint outputFramebuffer;
1190 glGenFramebuffers(1, &outputFramebuffer);
1191 glBindFramebuffer(GL_FRAMEBUFFER, outputFramebuffer);
1192 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, clientTextureName, 0);
1194 glViewport(0, 0, pixelsWide, pixelsHigh);
1198 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, 0, 0);
1199 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1200 glDeleteFramebuffers(1, &outputFramebuffer);
1204 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, clientTextureName);
1206 CGLError err = CGLTexImageIOSurface2D(cgl_ctx, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, (GLsizei)inputPixelsWide, (GLsizei)inputPixelsHigh, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, *surf, 0);
1207 if (err != kCGLNoError)
1209 VUserLog(
"Error in CGLTexImageIOSurface2D(): %s", CGLErrorString(err));
1214 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
1217 glFlushRenderAPPLE();
1241 if (!json_object_object_get_ex(js,
"pixelsWide", &o))
1243 GLsizei pixelsWide = json_object_get_int64(o);
1245 if (!json_object_object_get_ex(js,
"pixelsHigh", &o))
1247 GLsizei pixelsHigh = json_object_get_int64(o);
1249 if (!json_object_object_get_ex(js,
"ioSurface", &o))
1251 IOSurfaceID surfID = json_object_get_int(o);
1252 IOSurfaceRef surf = IOSurfaceLookup(surfID);
1255 VUserLog(
"Error: IOSurfaceLookup(%d) failed.", surfID);
1269 static GLuint
CompileShader(CGLContextObj cgl_ctx, GLenum type,
const char *source)
1271 GLint length = (GLint)strlen(source);
1272 GLuint shader = glCreateShader(type);
1273 glShaderSource(shader, 1, (
const GLchar**)&source, &length);
1274 glCompileShader(shader);
1276 int infologLength = 0;
1277 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
1278 if (infologLength > 0)
1280 char *infoLog = (
char *)malloc(infologLength);
1281 int charsWritten = 0;
1282 glGetShaderInfoLog(shader, infologLength, &charsWritten, infoLog);
1297 CGLContextObj cgl_ctx = (CGLContextObj)context;
1299 static bool openGL32Core;
1300 static GLuint vertexArray;
1301 static GLuint program;
1302 static GLuint receiveTextureOffsetAndSizeUniform;
1306 char *vertexShaderSource;
1307 char *fragmentShaderSource;
1311 CGLSetCurrentContext(cgl_ctx);
1318 in vec2 textureCoordinate;
1319 out vec2 fragmentTextureCoordinate;
1322 fragmentTextureCoordinate = textureCoordinate;
1323 gl_Position = vec4(position.x, position.y, 0., 1.);
1327 uniform sampler2DRect receiveTexture;
1328 uniform vec4 receiveTextureOffsetAndSize;
1329 in vec2 fragmentTextureCoordinate;
1333 FragColor = texture(receiveTexture, receiveTextureOffsetAndSize.xy + fragmentTextureCoordinate * receiveTextureOffsetAndSize.zw);
1341 glGenVertexArraysAPPLE(1, &vertexArray);
1342 glBindVertexArrayAPPLE(vertexArray);
1345 attribute vec2 position;
1346 attribute vec2 textureCoordinate;
1347 varying vec2 fragmentTextureCoordinate;
1350 fragmentTextureCoordinate = textureCoordinate;
1351 gl_Position = vec4(position.x, position.y, 0., 1.);
1355 uniform sampler2DRect receiveTexture;
1356 uniform vec4 receiveTextureOffsetAndSize;
1357 varying vec2 fragmentTextureCoordinate;
1360 gl_FragColor = texture2DRect(receiveTexture, receiveTextureOffsetAndSize.xy + fragmentTextureCoordinate * receiveTextureOffsetAndSize.zw);
1366 const GLfloat quadPositionsAndTextureCoordinates[] = {
1376 GLuint quadPTCBuffer;
1377 glGenBuffers(1, &quadPTCBuffer);
1378 glBindBuffer(GL_ARRAY_BUFFER, quadPTCBuffer);
1379 glBufferData(GL_ARRAY_BUFFER,
sizeof(quadPositionsAndTextureCoordinates), quadPositionsAndTextureCoordinates, GL_STATIC_DRAW);
1382 GLuint vertexShader =
CompileShader(context, GL_VERTEX_SHADER, vertexShaderSource);
1383 GLuint fragmentShader =
CompileShader(context, GL_FRAGMENT_SHADER, fragmentShaderSource);
1384 program = glCreateProgram();
1385 glAttachShader(program, vertexShader);
1386 glAttachShader(program, fragmentShader);
1387 glLinkProgram(program);
1388 GLuint positionAttribute = glGetAttribLocation(program,
"position");
1389 GLuint textureCoordinateAttribute = glGetAttribLocation(program,
"textureCoordinate");
1390 GLuint receiveTextureUniform = glGetUniformLocation(program,
"receiveTexture");
1391 receiveTextureOffsetAndSizeUniform = glGetUniformLocation(program,
"receiveTextureOffsetAndSize");
1393 glUseProgram(program);
1395 glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
1396 glEnableVertexAttribArray(positionAttribute);
1398 glVertexAttribPointer(textureCoordinateAttribute, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*2));
1399 glEnableVertexAttribArray(textureCoordinateAttribute);
1401 glUniform1i(receiveTextureUniform, 0);
1407 CGLSetCurrentContext(cgl_ctx);
1411 glBindVertexArrayAPPLE(vertexArray);
1413 GLuint textureRect =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_AllocateIOSurface, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, pixelsWide, pixelsHigh, GL_BGRA, surf);
1416 VUserLog(
"Error: Couldn't allocate texture.");
1422 glActiveTexture(GL_TEXTURE0);
1423 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureRect);
1425 glUseProgram(program);
1428 glUniform4f(receiveTextureOffsetAndSizeUniform, 0, flip ? pixelsHigh : 0, pixelsWide, pixelsHigh * (flip ? -1 : 1));
1433 glGetIntegerv(GL_VIEWPORT, viewport);
1435 glUniform4f(receiveTextureOffsetAndSizeUniform,
1436 ((
float)pixelsWide - viewport[2]) / 2,
1437 flip ? viewport[3] : 0,
1439 viewport[3] * (flip ? -1 : 1));
1442 glDrawArrays(GL_TRIANGLES, 0, 6);
1444 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
1445 VuoGlTexture_release(VuoGlTexturePool_AllocateIOSurface, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, pixelsWide, pixelsHigh, textureRect);
1450 glBindVertexArrayAPPLE(0);
1452 glBindBuffer(GL_ARRAY_BUFFER, 0);
1469 json_object_object_add(js,
"pointer", json_object_new_int64((int64_t)value));
1487 __block IOSurfaceID surfID = 0;
1491 if (value->glTextureTarget == GL_TEXTURE_2D)
1493 else if (value->glTextureTarget == GL_TEXTURE_RECTANGLE_ARB)
1497 surfID = VuoImageRenderer_draw_internal(shader, value->pixelsWide, value->pixelsHigh,
VuoImage_getColorDepth(value),
true,
true, 0, NULL);
1502 glFlushRenderAPPLE();
1510 json_object_object_add(js,
"ioSurface", o);
1514 json_object * o = json_object_new_int64(value->pixelsWide);
1515 json_object_object_add(js,
"pixelsWide", o);
1518 json_object * o = json_object_new_int64(value->pixelsHigh);
1519 json_object_object_add(js,
"pixelsHigh", o);
1522 json_object *o = json_object_new_double(value->scaleFactor);
1523 json_object_object_add(js,
"scaleFactor", o);
1544 return strdup(
"No image");
1547 switch (value->glInternalFormat)
1549 case GL_RGB: type =
"RGB, each channel 8-bit unsigned integer";
break;
1550 case GL_RGB16F_ARB: type =
"RGB, each channel 16-bit signed float";
break;
1551 case GL_RGB32F_ARB: type =
"RGB, each channel 32-bit signed float";
break;
1552 case GL_RGBA: type =
"RGBA, each channel 8-bit unsigned integer";
break;
1553 case GL_RGBA16F_ARB: type =
"RGBA, each channel 16-bit signed float";
break;
1554 case GL_RGBA32F_ARB: type =
"RGBA, each channel 32-bit signed float";
break;
1555 case GL_LUMINANCE8: type =
"intensity, 8-bit unsigned integer";
break;
1556 case GL_LUMINANCE16F_ARB: type =
"intensity, 16-bit signed float";
break;
1557 case GL_LUMINANCE32F_ARB: type =
"intensity, 32-bit signed float";
break;
1558 case GL_LUMINANCE8_ALPHA8: type =
"intensity+alpha, each channel 8-bit unsigned integer";
break;
1559 case GL_LUMINANCE_ALPHA16F_ARB: type =
"intensity+alpha, each channel 16-bit signed float";
break;
1560 case GL_LUMINANCE_ALPHA32F_ARB: type =
"intensity+alpha, each channel 32-bit signed float";
break;
1561 case GL_DEPTH_COMPONENT: type =
"intensity, 16-bit signed float";
break;
1562 default: type =
"(unknown)";
1568 char *summary =
VuoText_format(
"<div>%luĂ—%lu pixels @ %gx</div><div>%s</div><div>OpenGL: %s, %s, ID %u</div>",
1569 value->pixelsWide, value->pixelsHigh,
1574 value->glTextureName);
1576 free(internalformat);