13#include <IOSurface/IOSurface.h>
15#include <OpenGL/CGLMacro.h>
17#define glGenVertexArrays glGenVertexArraysAPPLE
18#define glBindVertexArray glBindVertexArrayAPPLE
19#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
27 "title" :
"VuoImageRenderer",
45 GLuint outputFramebuffer;
48 GLuint quadDataBuffer;
91 static dispatch_once_t once = 0;
92 dispatch_once(&once, ^{
106 glFlushRenderAPPLE();
136 if (pixelsWide < 1 || pixelsHigh < 1)
139 GLuint internalFormat;
140 GLuint outputTexture = VuoImageRenderer_draw_internal(shader, pixelsWide, pixelsHigh, imageColorDepth,
false,
false, 0, &internalFormat);
148 outputImage->scaleFactor = firstInputImage->scaleFactor;
158unsigned long int VuoImageRenderer_draw_internal(
VuoShader shader,
unsigned int pixelsWide,
unsigned int pixelsHigh,
VuoImageColorDepth imageColorDepth,
bool outputToIOSurface,
bool outputToGlTextureRectangle,
unsigned int outputToSpecificTexture, GLuint *outputInternalFormat)
162 __block GLuint outputTexture = 0;
163 __block IOSurfaceID surfID = 0;
165 glViewport(0, 0, pixelsWide, pixelsHigh);
169 GLenum textureFormat = shaderOpaque ? GL_BGR : GL_BGRA;
170 GLuint textureTarget = (outputToIOSurface || outputToGlTextureRectangle) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
172 if (outputInternalFormat)
173 *outputInternalFormat = textureTargetInternalFormat;
176 if (outputToIOSurface)
179 else if (outputToSpecificTexture)
180 outputTexture = outputToSpecificTexture;
182 outputTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, textureFormat, NULL);
189 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, outputTexture, 0);
193 glClearColor(0,0,0,0);
194 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
199 GLint positionAttribute;
200 GLint textureCoordinateAttribute;
204 VDebugLog(
"Error: Failed to get attribute locations.");
205 glBindFramebuffer(GL_FRAMEBUFFER, 0);
206 if (outputToIOSurface)
208 else if (outputToSpecificTexture)
213 VuoGlTexture_release(VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, outputTexture);
223 VUserLog(
"Shader activation failed.");
233 glUniformMatrix4fv(projectionMatrixUniform, 1, GL_FALSE,
unityMatrix);
236 glUniformMatrix4fv(cameraMatrixInverseUniform, 1, GL_FALSE,
unityMatrix);
239 glUniform1i(useFisheyeProjectionUniform,
false);
242 glUniformMatrix4fv(modelviewMatrixUniform, 1, GL_FALSE,
unityMatrix);
245 if (aspectRatioUniform != -1)
246 glUniform1f(aspectRatioUniform, (
float)pixelsWide/(
float)pixelsHigh);
249 if (viewportSizeUniform != -1)
250 glUniform2f(viewportSizeUniform, (
float)pixelsWide, (
float)pixelsHigh);
254 glEnableVertexAttribArray(positionAttribute);
255 glVertexAttribPointer(positionAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
257 if (textureCoordinateAttribute != -1)
259 glEnableVertexAttribArray(textureCoordinateAttribute);
260 glVertexAttribPointer(textureCoordinateAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*16));
264 GLuint timeElapsedQuery;
265 glGenQueries(1, &timeElapsedQuery);
266 glBeginQuery(GL_TIME_ELAPSED_EXT, timeElapsedQuery);
269 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
273 glEndQuery(GL_TIME_ELAPSED_EXT);
275 glGetQueryObjectuiv(timeElapsedQuery, GL_QUERY_RESULT, &nanoseconds);
276 seconds = ((double)nanoseconds) / NSEC_PER_SEC;
277 glDeleteQueries(1, &timeElapsedQuery);
279 double objectPercent = seconds / (1./60.) * 100.;
280 VLog(
"%6.2f %% of 60 Hz frame %s", objectPercent, shader->name);
283 if (textureCoordinateAttribute != -1)
284 glDisableVertexAttribArray(textureCoordinateAttribute);
286 glDisableVertexAttribArray(positionAttribute);
288 glBindBuffer(GL_ARRAY_BUFFER, 0);
294 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, 0, 0);
295 glBindFramebuffer(GL_FRAMEBUFFER, 0);
297 if (outputToIOSurface)
303 glFlushRenderAPPLE();
306 if (outputToIOSurface)
309 return outputTexture;
322 static dispatch_once_t once = 0;
323 dispatch_once(&once, ^{
325 glBindBuffer(GL_ARRAY_BUFFER, 0);