13#include <IOSurface/IOSurface.h>
15#include <OpenGL/CGLMacro.h>
17#define glGenVertexArrays glGenVertexArraysAPPLE
18#define glBindVertexArray glBindVertexArrayAPPLE
19#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
26 "title" :
"VuoImageRenderer",
44 GLuint outputFramebuffer;
47 GLuint quadDataBuffer;
90 static dispatch_once_t once = 0;
91 dispatch_once(&once, ^{
105 glFlushRenderAPPLE();
135 if (pixelsWide < 1 || pixelsHigh < 1)
138 GLuint internalFormat;
139 GLuint outputTexture = VuoImageRenderer_draw_internal(shader, pixelsWide, pixelsHigh, imageColorDepth,
false,
false, 0, &internalFormat);
147 outputImage->scaleFactor = firstInputImage->scaleFactor;
157unsigned long int VuoImageRenderer_draw_internal(
VuoShader shader,
unsigned int pixelsWide,
unsigned int pixelsHigh,
VuoImageColorDepth imageColorDepth,
bool outputToIOSurface,
bool outputToGlTextureRectangle,
unsigned int outputToSpecificTexture, GLuint *outputInternalFormat)
161 __block GLuint outputTexture = 0;
162 __block IOSurfaceID surfID = 0;
164 glViewport(0, 0, pixelsWide, pixelsHigh);
168 GLenum textureFormat = shaderOpaque ? GL_BGR : GL_BGRA;
169 GLuint textureTarget = (outputToIOSurface || outputToGlTextureRectangle) ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D;
171 if (outputInternalFormat)
172 *outputInternalFormat = textureTargetInternalFormat;
175 if (outputToIOSurface)
178 else if (outputToSpecificTexture)
179 outputTexture = outputToSpecificTexture;
181 outputTexture =
VuoGlTexturePool_use(cgl_ctx, VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, textureFormat, NULL);
188 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, outputTexture, 0);
192 glClearColor(0,0,0,0);
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198 GLint positionAttribute;
199 GLint textureCoordinateAttribute;
203 VDebugLog(
"Error: Failed to get attribute locations.");
204 glBindFramebuffer(GL_FRAMEBUFFER, 0);
205 if (outputToIOSurface)
207 else if (outputToSpecificTexture)
212 VuoGlTexture_release(VuoGlTexturePool_Allocate, outputToGlTextureRectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D, textureTargetInternalFormat, pixelsWide, pixelsHigh, outputTexture);
222 VUserLog(
"Shader activation failed.");
232 glUniformMatrix4fv(projectionMatrixUniform, 1, GL_FALSE,
unityMatrix);
235 glUniformMatrix4fv(cameraMatrixInverseUniform, 1, GL_FALSE,
unityMatrix);
238 glUniform1i(useFisheyeProjectionUniform,
false);
241 glUniformMatrix4fv(modelviewMatrixUniform, 1, GL_FALSE,
unityMatrix);
244 if (aspectRatioUniform != -1)
245 glUniform1f(aspectRatioUniform, (
float)pixelsWide/(
float)pixelsHigh);
248 if (viewportSizeUniform != -1)
249 glUniform2f(viewportSizeUniform, (
float)pixelsWide, (
float)pixelsHigh);
253 glEnableVertexAttribArray(positionAttribute);
254 glVertexAttribPointer(positionAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)0);
256 if (textureCoordinateAttribute != -1)
258 glEnableVertexAttribArray(textureCoordinateAttribute);
259 glVertexAttribPointer(textureCoordinateAttribute, 4 , GL_FLOAT, GL_FALSE,
sizeof(GLfloat)*4, (
void*)(
sizeof(GLfloat)*16));
263 GLuint timeElapsedQuery;
264 glGenQueries(1, &timeElapsedQuery);
265 glBeginQuery(GL_TIME_ELAPSED_EXT, timeElapsedQuery);
268 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
272 glEndQuery(GL_TIME_ELAPSED_EXT);
274 glGetQueryObjectuiv(timeElapsedQuery, GL_QUERY_RESULT, &nanoseconds);
275 seconds = ((double)nanoseconds) / NSEC_PER_SEC;
276 glDeleteQueries(1, &timeElapsedQuery);
278 double objectPercent = seconds / (1./60.) * 100.;
279 VLog(
"%6.2f %% of 60 Hz frame %s", objectPercent, shader->name);
282 if (textureCoordinateAttribute != -1)
283 glDisableVertexAttribArray(textureCoordinateAttribute);
285 glDisableVertexAttribArray(positionAttribute);
287 glBindBuffer(GL_ARRAY_BUFFER, 0);
293 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureTarget, 0, 0);
294 glBindFramebuffer(GL_FRAMEBUFFER, 0);
296 if (outputToIOSurface)
302 glFlushRenderAPPLE();
305 if (outputToIOSurface)
308 return outputTexture;
321 static dispatch_once_t once = 0;
322 dispatch_once(&once, ^{
324 glBindBuffer(GL_ARRAY_BUFFER, 0);