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);
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 GLuint textureTarget = (outputToIOSurface || outputToGlTextureRectangle) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
168 if (outputInternalFormat)
169 *outputInternalFormat = textureTargetInternalFormat;
172 if (outputToIOSurface)
175 else if (outputToSpecificTexture)
176 outputTexture = outputToSpecificTexture;
178 outputTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, textureFormat, NULL);
185 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, outputTexture, 0);
187 glClearColor(0,0,0,0);
188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192 GLint positionAttribute;
193 GLint textureCoordinateAttribute;
197 VDebugLog(
"Error: Failed to get attribute locations.");
198 glBindFramebuffer(GL_FRAMEBUFFER, 0);
199 if (outputToIOSurface)
201 else if (outputToSpecificTexture)
206 VuoGlTexture_release(VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, outputTexture);
216 VUserLog(
"Shader activation failed.");
226 glUniformMatrix4fv(projectionMatrixUniform, 1, GL_FALSE,
unityMatrix);
229 glUniformMatrix4fv(cameraMatrixInverseUniform, 1, GL_FALSE,
unityMatrix);
232 glUniform1i(useFisheyeProjectionUniform,
false);
235 glUniformMatrix4fv(modelviewMatrixUniform, 1, GL_FALSE,
unityMatrix);
238 if (aspectRatioUniform != -1)
239 glUniform1f(aspectRatioUniform, (
float)pixelsWide/(
float)pixelsHigh);
242 if (viewportSizeUniform != -1)
243 glUniform2f(viewportSizeUniform, (
float)pixelsWide, (
float)pixelsHigh);
247 glEnableVertexAttribArray(positionAttribute);
248 glVertexAttribPointer(positionAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
250 if (textureCoordinateAttribute != -1)
252 glEnableVertexAttribArray(textureCoordinateAttribute);
253 glVertexAttribPointer(textureCoordinateAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*16));
257 GLuint timeElapsedQuery;
258 glGenQueries(1, &timeElapsedQuery);
259 glBeginQuery(GL_TIME_ELAPSED_EXT, timeElapsedQuery);
262 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
266 glEndQuery(GL_TIME_ELAPSED_EXT);
268 glGetQueryObjectuiv(timeElapsedQuery, GL_QUERY_RESULT, &nanoseconds);
269 seconds = ((double)nanoseconds) / NSEC_PER_SEC;
270 glDeleteQueries(1, &timeElapsedQuery);
272 double objectPercent = seconds / (1./60.) * 100.;
273 VLog(
"%6.2f %% of 60 Hz frame %s", objectPercent, shader->
name);
276 if (textureCoordinateAttribute != -1)
277 glDisableVertexAttribArray(textureCoordinateAttribute);
279 glDisableVertexAttribArray(positionAttribute);
281 glBindBuffer(GL_ARRAY_BUFFER, 0);
287 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, 0, 0);
288 glBindFramebuffer(GL_FRAMEBUFFER, 0);
290 if (outputToIOSurface)
296 glFlushRenderAPPLE();
299 if (outputToIOSurface)
302 return outputTexture;
313 glBindBuffer(GL_ARRAY_BUFFER, 0);