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;
88 static dispatch_once_t once = 0;
89 dispatch_once(&once, ^{
101 glFlushRenderAPPLE();
133 if (pixelsWide < 1 || pixelsHigh < 1)
136 GLuint internalFormat;
137 GLuint outputTexture = VuoImageRenderer_draw_internal(shader, pixelsWide, pixelsHigh, imageColorDepth,
false,
false, 0, &internalFormat);
145 outputImage->scaleFactor = firstInputImage->scaleFactor;
155 unsigned long int VuoImageRenderer_draw_internal(
VuoShader shader,
unsigned int pixelsWide,
unsigned int pixelsHigh,
VuoImageColorDepth imageColorDepth,
bool outputToIOSurface,
bool outputToGlTextureRectangle,
unsigned int outputToSpecificTexture, GLuint *outputInternalFormat)
159 __block GLuint outputTexture = 0;
160 __block IOSurfaceID surfID = 0;
162 glViewport(0, 0, pixelsWide, pixelsHigh);
166 GLenum textureFormat = shaderOpaque ? GL_BGR : GL_BGRA;
167 GLuint textureTarget = (outputToIOSurface || outputToGlTextureRectangle) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
169 if (outputInternalFormat)
170 *outputInternalFormat = textureTargetInternalFormat;
173 if (outputToIOSurface)
176 else if (outputToSpecificTexture)
177 outputTexture = outputToSpecificTexture;
179 outputTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, textureFormat, NULL);
186 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, outputTexture, 0);
190 glClearColor(0,0,0,0);
191 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196 GLint positionAttribute;
197 GLint textureCoordinateAttribute;
201 VDebugLog(
"Error: Failed to get attribute locations.");
202 glBindFramebuffer(GL_FRAMEBUFFER, 0);
203 if (outputToIOSurface)
205 else if (outputToSpecificTexture)
210 VuoGlTexture_release(VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, outputTexture);
220 VUserLog(
"Shader activation failed.");
230 glUniformMatrix4fv(projectionMatrixUniform, 1, GL_FALSE,
unityMatrix);
233 glUniformMatrix4fv(cameraMatrixInverseUniform, 1, GL_FALSE,
unityMatrix);
236 glUniform1i(useFisheyeProjectionUniform,
false);
239 glUniformMatrix4fv(modelviewMatrixUniform, 1, GL_FALSE,
unityMatrix);
242 if (aspectRatioUniform != -1)
243 glUniform1f(aspectRatioUniform, (
float)pixelsWide/(
float)pixelsHigh);
246 if (viewportSizeUniform != -1)
247 glUniform2f(viewportSizeUniform, (
float)pixelsWide, (
float)pixelsHigh);
251 glEnableVertexAttribArray(positionAttribute);
252 glVertexAttribPointer(positionAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
254 if (textureCoordinateAttribute != -1)
256 glEnableVertexAttribArray(textureCoordinateAttribute);
257 glVertexAttribPointer(textureCoordinateAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*16));
261 GLuint timeElapsedQuery;
262 glGenQueries(1, &timeElapsedQuery);
263 glBeginQuery(GL_TIME_ELAPSED_EXT, timeElapsedQuery);
266 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
270 glEndQuery(GL_TIME_ELAPSED_EXT);
272 glGetQueryObjectuiv(timeElapsedQuery, GL_QUERY_RESULT, &nanoseconds);
273 seconds = ((double)nanoseconds) / NSEC_PER_SEC;
274 glDeleteQueries(1, &timeElapsedQuery);
276 double objectPercent = seconds / (1./60.) * 100.;
277 VLog(
"%6.2f %% of 60 Hz frame %s", objectPercent, shader->name);
280 if (textureCoordinateAttribute != -1)
281 glDisableVertexAttribArray(textureCoordinateAttribute);
283 glDisableVertexAttribArray(positionAttribute);
285 glBindBuffer(GL_ARRAY_BUFFER, 0);
291 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, 0, 0);
292 glBindFramebuffer(GL_FRAMEBUFFER, 0);
294 if (outputToIOSurface)
300 glFlushRenderAPPLE();
303 if (outputToIOSurface)
306 return outputTexture;
317 glBindBuffer(GL_ARRAY_BUFFER, 0);