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);
141 return VuoImage_make(outputTexture, internalFormat, pixelsWide, pixelsHigh);
149 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)
153 __block GLuint outputTexture = 0;
154 __block IOSurfaceID surfID = 0;
156 glViewport(0, 0, pixelsWide, pixelsHigh);
160 GLuint textureTarget = (outputToIOSurface || outputToGlTextureRectangle) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
162 if (outputInternalFormat)
163 *outputInternalFormat = textureTargetInternalFormat;
166 if (outputToIOSurface)
169 else if (outputToSpecificTexture)
170 outputTexture = outputToSpecificTexture;
172 outputTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, textureFormat, NULL);
179 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, outputTexture, 0);
181 glClearColor(0,0,0,0);
182 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
186 GLint positionAttribute;
187 GLint textureCoordinateAttribute;
191 VDebugLog(
"Error: Failed to get attribute locations.");
192 glBindFramebuffer(GL_FRAMEBUFFER, 0);
193 if (outputToIOSurface)
195 else if (outputToSpecificTexture)
200 VuoGlTexture_release(VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, outputTexture);
210 VUserLog(
"Shader activation failed.");
220 glUniformMatrix4fv(projectionMatrixUniform, 1, GL_FALSE,
unityMatrix);
223 glUniformMatrix4fv(cameraMatrixInverseUniform, 1, GL_FALSE,
unityMatrix);
226 glUniform1i(useFisheyeProjectionUniform,
false);
229 glUniformMatrix4fv(modelviewMatrixUniform, 1, GL_FALSE,
unityMatrix);
232 if (aspectRatioUniform != -1)
233 glUniform1f(aspectRatioUniform, (
float)pixelsWide/(
float)pixelsHigh);
236 if (viewportSizeUniform != -1)
237 glUniform2f(viewportSizeUniform, (
float)pixelsWide, (
float)pixelsHigh);
241 glEnableVertexAttribArray(positionAttribute);
242 glVertexAttribPointer(positionAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
244 if (textureCoordinateAttribute != -1)
246 glEnableVertexAttribArray(textureCoordinateAttribute);
247 glVertexAttribPointer(textureCoordinateAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*16));
251 GLuint timeElapsedQuery;
252 glGenQueries(1, &timeElapsedQuery);
253 glBeginQuery(GL_TIME_ELAPSED_EXT, timeElapsedQuery);
256 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
260 glEndQuery(GL_TIME_ELAPSED_EXT);
262 glGetQueryObjectuiv(timeElapsedQuery, GL_QUERY_RESULT, &nanoseconds);
263 seconds = ((double)nanoseconds) / NSEC_PER_SEC;
264 glDeleteQueries(1, &timeElapsedQuery);
266 double objectPercent = seconds / (1./60.) * 100.;
267 VLog(
"%6.2f %% of 60 Hz frame %s", objectPercent, shader->
name);
270 if (textureCoordinateAttribute != -1)
271 glDisableVertexAttribArray(textureCoordinateAttribute);
273 glDisableVertexAttribArray(positionAttribute);
275 glBindBuffer(GL_ARRAY_BUFFER, 0);
281 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, 0, 0);
282 glBindFramebuffer(GL_FRAMEBUFFER, 0);
284 if (outputToIOSurface)
290 glFlushRenderAPPLE();
293 if (outputToIOSurface)
296 return outputTexture;
307 glBindBuffer(GL_ARRAY_BUFFER, 0);