void NLGLWidget::initializeTexture() { if (_texture >= 0) { cleanupTexture(); } // Create the texture for displaying the result: GLuint gltex; glGenTextures(1, &gltex); _texture = gltex; reportGLError("setupGLRendering() genTexture"); // Bind texture: glBindTexture(GL_TEXTURE_2D, _texture); reportGLError("setupGLRendering() bindTexture"); // Allocate texture: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, _img_width, _img_height, 0, GL_RGBA, GL_FLOAT, NULL); reportGLError("setupGLRendering() texImage2D"); // Set texture parameters: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); reportGLError("setupGLRendering() glTexParameteri"); // Unbind texture: glBindTexture(GL_TEXTURE_2D, 0); reportGLError("setupGLRendering() bindTexture(0)"); // Register the buffer object: checkCUDAError("Pre gl register"); cudaGraphicsGLRegisterImage(&_graphicsResource, _texture, GL_TEXTURE_2D, cudaGraphicsMapFlagsWriteDiscard); checkCUDAError("Post gl register"); }
NLGLWidget::NLGLWidget(QWidget *parent, NLSynthesizer* synthesizer) : QGLWidget(parent), _synthesizer(synthesizer) { setFocusPolicy(Qt::StrongFocus); _graphicsResource = 0; _gl_width = 0; _gl_height = 0; _img_width = 0; _img_height = 0; _texture = -1; _checkboardTexture = -1; _use_Checkboard = true; _use_sRgb = false; connect(_synthesizer, SIGNAL(synthesisAdvanced()), this, SLOT(update())); connect(_synthesizer, SIGNAL(cleaningUp()), this, SLOT(cleanupTexture())); }
void wesTriangleRateBenchmark(AppState *as) { int startTime, endTime; size_t totalTris=0, totalVerts=0; float elapsedTimeMS, trisPerSecond, vertsPerSecond; Vertex2D *baseVerts; Color3D *baseColors; Vertex3D *baseNormals; Vertex2D *baseTCs; GLuint trianglesListIndx = 0; int nVertsPerAxis; int nFrames=0; Vertex2D *dispatchVerts=NULL; Color3D *dispatchColors=NULL; Vertex3D *dispatchNormals=NULL; Vertex2D *dispatchTCs=NULL; GLuint *dispatchIndices=NULL; int dispatchVertexCount, dispatchTriangles; int screenWidth = as->imgWidth; GLfloat r=screenWidth; int screenHeight = as->imgHeight; int testDurationMS = as->testDurationMS; size_t triangleLimit = as->triangleLimit; double triangleAreaPixels = as->triangleAreaInPixels; int lighting=as->enableLighting; int colorMaterial = as->enableColorMaterial; int texturingEnabled = (as->textureSize > 0) ? 1 : 0; int textureSize = as->textureSize; /* * Objective(s): * 1. Want triangles with a specified area (e.g., 8sq pixels) for the * purpose of generating a graphics load with reasonably precise * characteristics . * 2. Want triangles to *all* remain on-screen so that triangle rate * reflects the cost of rendering visible triangles/fragments. * * Approach: * 1. build a base mesh of vertices. This set of verts is positioned * so that it will remain entirely within the view frustum no matter * how we rotate it (in 2D) * 2. dispatch off the mesh vertices as disjoint triangles using * vertex arrays. This approach is not the most efficient way to * represent densely packed triangles, but has a couple of benefits: * - It allows us to easily set the maximum number of triangles per * frame to be dispatched off to the renderer w/o having to resort * to tricks like inserting degenerate triangles in strips to connect * up two disjoint strips. * - It allows us to glDrawArrays rather than glDrawElements. The latter * uses one level of indirection, and will be less efficient. * - In the end, we care about vertex rate, not triangle rate. * * An early version of this code used the glDrawElements with indices. * Rather than insert compile-time switches, this old code is simply * cut-n-pasted and commented out at the end of this file. */ /* Make sure we are drawing to the front buffer */ glDrawBuffer(GL_FRONT); glDisable(GL_DEPTH_TEST); /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT); if (as->outlineMode != 0) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* Load identities */ glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity( ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); /* change range from -1..1 to 0..[screenWidth, screenHeight] */ glTranslatef(-1.0, -1.0, 0.0); glScalef(2.0/r, 2.0/r, 1.0F); if (lighting) { /* turn on OpenGL lighting stuff */ GLfloat lightPosition[] = {(float)screenWidth*0.5F, (float)screenHeight*0.5F, 200.0F, 1.0F}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); if (colorMaterial) { glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); } /* just use OpenGL's default light values: directional light source */ } else { glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); } if (texturingEnabled) { buildAndDownloadTexture(textureSize); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); } else glDisable(GL_TEXTURE_2D); /* build the base quadmesh vertex array */ buildBaseArrays(triangleAreaPixels, as->imgWidth, as->imgHeight, &nVertsPerAxis, &baseVerts, &baseColors, &baseNormals, &baseTCs); /* now, repackage that information into bundles suitable for submission to GL using the specified primitive type */ if (as->triangleType == DISJOINT_TRIANGLES) { int triangleLimit; if ((as->triangleLimit*3) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit/3; else triangleLimit = as->triangleLimit; buildDisjointTriangleArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs); as->computedVertsPerArrayCall = dispatchVertexCount; as->computedIndicesPerArrayCall = 0; } else if (as->triangleType == TRIANGLE_STRIPS) { if ((as->triangleLimit+2) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit-2; else triangleLimit = as->triangleLimit; buildTriangleStripArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs); as->computedVertsPerArrayCall = dispatchVertexCount; as->computedIndicesPerArrayCall = 0; } else if (as->triangleType == INDEXED_DISJOINT_TRIANGLES) { if ((as->triangleLimit*3) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit/3; else triangleLimit = as->triangleLimit; buildIndexedDisjointTriangleArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs, &dispatchIndices); as->computedVertsPerArrayCall = (nVertsPerAxis+1)*(nVertsPerAxis+1); as->computedIndicesPerArrayCall = dispatchVertexCount; } else if (as->triangleType == INDEXED_TRIANGLE_STRIPS) { if ((as->triangleLimit+2) > as->vertexBufLimit) triangleLimit = as->vertexBufLimit-2; else triangleLimit = as->triangleLimit; buildIndexedTriangleStripArrays(nVertsPerAxis, triangleLimit, &dispatchTriangles, &dispatchVertexCount, baseVerts, baseColors, baseNormals, baseTCs, &dispatchVerts, &dispatchColors, &dispatchNormals, &dispatchTCs, &dispatchIndices); as->computedVertsPerArrayCall = (nVertsPerAxis+1)*(nVertsPerAxis+1); as->computedIndicesPerArrayCall = dispatchVertexCount; } /* Set up the pointers */ glVertexPointer(2, GL_FLOAT, 0, (const GLvoid *)dispatchVerts); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(3, GL_FLOAT, 0, (const GLvoid *)dispatchColors); glEnableClientState(GL_COLOR_ARRAY); if (lighting) { glNormalPointer(GL_FLOAT, 0, (const GLvoid *)dispatchNormals); glEnableClientState(GL_NORMAL_ARRAY); } if (texturingEnabled) { glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid *)dispatchTCs); glEnableClientState(GL_TEXTURE_COORD_ARRAY); } glFinish(); /* make sure all setup is finished */ startTime = endTime = glutGet(GLUT_ELAPSED_TIME); while ((endTime - startTime) < testDurationMS) { int buildingList=0; if (as->clearPerFrame != 0) glClear(GL_COLOR_BUFFER_BIT); /* tmp for debug */ #if SCREENSHOT_MODE /* screenshot mode won't work with -retained */ /* Clear the screen */ glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); fprintf(stderr," You have screenshot mode enabled! \n"); fflush(stderr); sleep(5); break; #endif if (as->retainedModeEnabled != 0) { if (trianglesListIndx == 0) /* no list yet, build one */ { trianglesListIndx = glGenLists(1); /* glNewList(trianglesListIndx, GL_COMPILE_AND_EXECUTE); */ glNewList(trianglesListIndx, GL_COMPILE); buildingList = 1; } else glCallList(trianglesListIndx); } if ((buildingList == 0) || (as->retainedModeEnabled != 0)) { switch (as->triangleType) { case DISJOINT_TRIANGLES: glDrawArrays(GL_TRIANGLES, 0, dispatchVertexCount); break; case TRIANGLE_STRIPS: glDrawArrays(GL_TRIANGLE_STRIP, 0, dispatchVertexCount); break; case INDEXED_DISJOINT_TRIANGLES: glDrawElements(GL_TRIANGLES, dispatchVertexCount, GL_UNSIGNED_INT, dispatchIndices); break; case INDEXED_TRIANGLE_STRIPS: glDrawElements(GL_TRIANGLE_STRIP, dispatchVertexCount, GL_UNSIGNED_INT, dispatchIndices); break; default: break; } } glTranslatef(r/2.0, r/2.0, 0.0F); glRotatef(0.01F, 0.0F, 0.0F, 1.0F); glTranslatef(-r/2.0, -r/2.0, 0.0F); if (buildingList == 1) { glEndList(); glCallList(trianglesListIndx); /* cheat - reset the clock to eliminate the time required for creating the display list from the frame rate computation */ startTime = endTime = glutGet(GLUT_ELAPSED_TIME); } endTime = glutGet(GLUT_ELAPSED_TIME); nFrames++; totalTris += dispatchTriangles; totalVerts += as->computedVertsPerArrayCall; } /* TODO: Ask about how to get it to draw */ glFinish(); endTime = glutGet(GLUT_ELAPSED_TIME); /* Restore the gl stack */ glMatrixMode( GL_MODELVIEW ); glPopMatrix(); glMatrixMode( GL_PROJECTION ); glPopMatrix(); /* Before printing the results, make sure we didn't have ** any GL related errors. */ check_gl_errors(); /* the pause that refreshes -- */ #ifdef _WIN32 Sleep(250); #else usleep(250000); #endif elapsedTimeMS = endTime - startTime; float elapsedTimeS = elapsedTimeMS / 1000; printf("Elapsed Time: %f s\n", elapsedTimeS); printf("Total Triangles: %d\n", (int) totalTris); printf("Total Verticies: %d\n", (int) totalVerts); printf("Frames per Second (FPS): %f\n", nFrames / elapsedTimeS); /* * myAppState.appName, * myAppState.triangleAreaInPixels, * myAppState.computedMTrisPerSecond, * myAppState.computedMVertexOpsPerSecond, * myAppState.computedVertsPerArrayCall, * myAppState.computedMFragsPerSecond, * (int) myAppState.computedIndicesPerArrayCall); */ myAppState.computedMTrisPerSecond = (totalTris / 1000000.0f) / elapsedTimeS; myAppState.computedMVertexOpsPerSecond = (totalVerts / 1000000.0f) / elapsedTimeS; myAppState.computedMFragsPerSecond = myAppState.computedMTrisPerSecond * myAppState.triangleAreaInPixels; printf("verts/frame = %d \n", dispatchVertexCount); printf("nframes = %d \n", nFrames); free((void *)baseVerts); free((void *)baseColors); free((void *)baseNormals); free((void *)baseTCs); free((void *)dispatchVerts); free((void *)dispatchColors); free((void *)dispatchNormals); free((void *)dispatchTCs); if (dispatchIndices != NULL) free((void *)dispatchIndices); if (texturingEnabled) cleanupTexture(); }