void display(){


		runCuda();

		string title = "565Raytracer | " + utilityCore::convertIntToString(iterations) + " Frames";
		glutSetWindowTitle(title.c_str());

		glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
		glBindTexture(GL_TEXTURE_2D, displayImage);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 
			  GL_RGBA, GL_UNSIGNED_BYTE, NULL);

		glClear(GL_COLOR_BUFFER_BIT);   

		// VAO, shader program, and texture already bound
		glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);
		


		//draw text

		
		char displaystring[200];

		strcpy ( displaystring, "Press H for help." );		
		drawText ( 50, 50, displaystring); 
		

		glutPostRedisplay();
		glutSwapBuffers();
	}
void initCuda()
{
  // First initialize OpenGL context, so we can properly set the GL
  // for CUDA.  NVIDIA notes this is necessary in order to achieve
  // optimal performance with OpenGL/CUDA interop.  use command-line
  // specified CUDA device, otherwise use device with highest Gflops/s
  int devCount= 0;
  cudaGetDeviceCount(&devCount);
  if( devCount < 1 )
  {
     printf("No GPUS detected\n");
     exit(EXIT_FAILURE);
  }
  cudaGLSetGLDevice( 0 );
  //Set Up scenary
  setup_scene(); 
  
  createPBO(&pbo);
  createTexture(&textureID,image_width,image_height);
 
  // Clean up on program exit
  atexit(cleanupCuda);
  


  runCuda();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer to a black color
    float ClearColor[4] = {0, 0, 0, 0};
    g_pd3dDevice->ClearRenderTargetView(g_pSwapChainRTV, ClearColor);

    // Run CUDA to update vertex positions
    runCuda();

    // Draw frame
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();

        // Render the vertex buffer contents
        UINT stride = sizeof(CUSTOMVERTEX);
        UINT offset = 0;
        g_pd3dDevice->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);

        g_pSimpleTechnique->GetPassByIndex(0)->Apply(0);
        g_pd3dDevice->Draw(g_NumVertices, 0);
    }

    // Present the backbuffer contents to the display
    g_pSwapChain->Present(0, 0);

    anim += 0.01f;
}
Exemple #4
0
  void display(){

    // DEBUG: display only one frame 
    /*
    if ( frame > 5 )
      return;
    */
    runCuda();
	time_t seconds2 = time (NULL);

    if(seconds2-seconds >= 1){

      fps = fpstracker/(seconds2-seconds);
      fpstracker = 0;
      seconds = seconds2;

    }

    string title = "CIS565 Rasterizer | "+ utilityCore::convertIntToString((int)fps) + "FPS";
    glutSetWindowTitle(title.c_str());

    glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
    glBindTexture(GL_TEXTURE_2D, displayImage);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 
        GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glClear(GL_COLOR_BUFFER_BIT);   

    // VAO, shader program, and texture already bound
    glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);

    glutPostRedisplay();
    glutSwapBuffers();
  }
void mainLoop() {
  while(!glfwWindowShouldClose(window)){
    glfwPollEvents();
    runCuda();

    time_t seconds2 = time (NULL);

    if(seconds2-seconds >= 1){

        fps = fpstracker/(seconds2-seconds);
        fpstracker = 0;
        seconds = seconds2;
    }

    string title = "CIS565 Rasterizer | " + utilityCore::convertIntToString((int)fps) + " FPS";
		glfwSetWindowTitle(window, title.c_str());
    
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
    glBindTexture(GL_TEXTURE_2D, displayImage);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glClear(GL_COLOR_BUFFER_BIT);   

    // VAO, shader program, and texture already bound
    glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);
    glfwSwapBuffers(window);
  }
  glfwDestroyWindow(window);
  glfwTerminate();
}
Exemple #6
0
Fichier : main.c Projet : ipa/nbody
int main( int argc, char *argv[] ) {
	initDisplay();
	// get number of SMs on this GPU
	int devID;
	/*cudaDeviceProp props;
	cudaGetDevice(&devID);
	cudaGetDeviceProperties(&props, devID);
	check();

	printf("using device %s \n", props.name);
	printf("number of MPs %d \n", props.multiProcessorCount);
	printf("max threads per block %d \n", props.maxThreadsPerBlock);
	printf("max concurrent kernels %d \n", props.concurrentKernels);
	printf("number of bodies %d \n", numBodies);
*/

	if(argc == 2)
	{
		if(strcmp(argv[1], "--cpu") == 0)
		{
			gpu = false;
		}
		else if(strcmp(argv[1], "--gpu") == 0)
		{
			gpu = true;
		}
		else
		{
			printf("wrong argument %s \nallowed: NULL, --cpu, --gpu \n", argv[1]);
			exit(EXIT_FAILURE);
		}
	}

	if(randData){
		loadDataRand(numBodies);
	}else{
		loadData("data/data.tab", numBodies);
	}
	init(numBodies);

	printf("stop with CTRL+C\n");
	
	do {
		runCuda();
		showGalaxy(h_particleData, numBodies, false);
		//count++;
	} while (count < 64);

	printf("finished...\n");

	if(gpu)
	{
		cudaFree(d_particleData);
	}
	closeWindow();
	printf("close...\n");
	return EXIT_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
CUTBoolean runTest(int argc, char** argv)
{
    if (!cutCheckCmdLineFlag(argc, (const char **)argv, "noqatest") ||
		cutCheckCmdLineFlag(argc, (const char **)argv, "noprompt")) 
	{
        g_bQAReadback = true;
        fpsLimit = frameCheckNumber;
    }

    // First initialize OpenGL context, so we can properly set the GL for CUDA.
    // This is necessary in order to achieve optimal performance with OpenGL/CUDA interop.
    if (CUTFalse == initGL(argc, argv)) {
        return CUTFalse;
    }

	// use command-line specified CUDA device, otherwise use device with highest Gflops/s
    if( cutCheckCmdLineFlag(argc, (const char**)argv, "device") )
        cutilGLDeviceInit(argc, argv);
    else {
        cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );
    }

    // Create the CUTIL timer
    cutilCheckError( cutCreateTimer( &timer));

    // register callbacks
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    if (g_bQAReadback) {
        g_CheckRender = new CheckBackBuffer(window_width, window_height, 4);
        g_CheckRender->setPixelFormat(GL_RGBA);
        g_CheckRender->setExecPath(argv[0]);
        g_CheckRender->EnableQAReadback(true);
    }

    // create VBO
    createVBO(&vbo);

    // run the cuda part
    runCuda(vbo);

    // check result of Cuda step
    checkResultCuda(argc, argv, vbo);

    atexit(cleanup);

    // start rendering mainloop
    glutMainLoop();

    cudaThreadExit();

	return CUTTrue;
}
Exemple #8
0
void initCuda(){
  // Use device with highest Gflops/s
  cudaGLSetGLDevice( compat_getMaxGflopsDeviceId() );

  initPBO(&pbo);

  // Clean up on program exit
  atexit(cleanupCuda);

  runCuda();
}
Exemple #9
0
void initCuda(){
  // Use device with highest Gflops/s
  // Had to update this to remove cutil version
  cudaGLSetGLDevice( gpuGetMaxGflopsDeviceId() );

  initPBO(&pbo);

  // Clean up on program exit
  atexit(cleanupCuda);

  runCuda();
}
Exemple #10
0
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
  // run CUDA kernel to generate vertex positions
  boost::shared_ptr<thrust::device_vector<ms::point> > dv = runCuda();

  if (!dv)
    return;

  ms::gl_buffer<ms::point> glb(dv->size());

  glb << *dv;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // set view matrix
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0, 0.0, translate_z);
  glRotatef(rotate_x, 1.0, 0.0, 0.0);
  glRotatef(rotate_y, 0.0, 1.0, 0.0);

  // render from the vbo
  glBindBuffer(GL_ARRAY_BUFFER, 
               glb.buffer());

  glVertexPointer(3,  // GLint size
                  GL_FLOAT, // GLenum type
                  24,  // GLsizei stride
                  0); // offset

  //  assert (displayable.size_bytes == mesh_width * mesh_height * sizeof(mephitis::point));

  //glColor3f(1.0, 1.0, 1.0);

  glColorPointer(3, 
		 GL_FLOAT, 
		 24, 
		 (const GLvoid*)(sizeof(float) * 3));

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_COLOR_ARRAY);
  glDrawArrays(GL_POINTS, // GLenum mode
               0, // GLint first
               glb.size() // GLsizei count
               );

  glDisableClientState(GL_COLOR_ARRAY);
  glDisableClientState(GL_VERTEX_ARRAY);

  glutSwapBuffers();
  glutPostRedisplay();
}
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
    sdkStartTimer(&timer);

    // run CUDA kernel to generate vertex positions
    runCuda(&cuda_vbo_resource);

	//簡易ライトセット
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_POSITION, gkLightPos);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, gkLightDiff);
//	glLightfv(GL_LIGHT0, GL_AMBIENT, gkLightAmb);
	glEnable(GL_LIGHT1);
	glLightfv(GL_LIGHT1, GL_POSITION, gkLightPos2);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, gkLightDiff2);

	//Zバッファ有効
	glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // set view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, translate_z);
    glRotatef(rotate_x, 1.0, 0.0, 0.0);
    glRotatef(rotate_y, 0.0, 1.0, 0.0);

	// Earth
//	glMaterialfv(GL_FRONT, GL_DIFFUSE, gkMaterial);
	glutSolidSphere(50.0 * h_axis_radius, 20, 20);
	glDisable(GL_LIGHTING);

    // render from the vbo
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexPointer(4, GL_FLOAT, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glColor3f(1.0, 1.0, 1.0);
    glDrawArrays(GL_POINTS, 0, mesh_width * mesh_height);
    glDisableClientState(GL_VERTEX_ARRAY);

    glutSwapBuffers();

    g_fAnim += 0.01f;

    sdkStopTimer(&timer);
    computeFPS();
}
Exemple #12
0
void initCuda(){
  // Use device with highest Gflops/s
#if CUDA_VERSION >= 5000
	cudaGLSetGLDevice( gpuGetMaxGflopsDeviceId() );
#else
	cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );
#endif

  initPBO(&pbo);

  // Clean up on program exit
  atexit(cleanupCuda);

  runCuda();
}
void initCuda(){
  // Use device with highest Gflops/s
  cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );

  initPBO(&pbo);
  dptr=NULL;
  cudaGLMapBufferObject((void**)&dptr, pbo);
  clearPBOpos(dptr,width,height);
  cudaGLUnmapBufferObject(pbo);
  // Clean up on program exit
  atexit(cleanupCuda);
  SetScissorWindow(glm::vec4(300,300,500,500));
  texture.mapptr = stbi_load("cow.jpeg",&texture.width, &texture.height,&texture.depth,0);
  runCuda();
}
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
    cutilCheckError(cutStartTimer(timer));  

    // run CUDA kernel to generate vertex positions
    runCuda(vbo);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // set view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, translate_z);
    glRotatef(rotate_x, 1.0, 0.0, 0.0);
    glRotatef(rotate_y, 0.0, 1.0, 0.0);

    // render from the vbo
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexPointer(4, GL_FLOAT, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glColor3f(1.0, 0.0, 0.0);
    glDrawArrays(GL_POINTS, 0, mesh_width * mesh_height);
    glDisableClientState(GL_VERTEX_ARRAY);

    if (g_CheckRender && g_CheckRender->IsQAReadback() && g_Verify) {
        // readback for QA testing
        printf("> (Frame %d) Readback BackBuffer\n", frameCount);
        g_CheckRender->readback( window_width, window_height );
        g_CheckRender->savePPM(sOriginal[g_Index], true, NULL);
        if (!g_CheckRender->PPMvsPPM(sOriginal[g_Index], sReference[g_Index], MAX_EPSILON_ERROR, 0.15f)) {
            g_TotalErrors++;
        }
        else
        {
        	printf( "TEST PASSED\n" );
        }
        g_Verify = false;
    }

    glutSwapBuffers();
    glutPostRedisplay();

    anim += 0.01;

    cutilCheckError(cutStopTimer(timer));  
    computeFPS();
}
void initCuda(){
  // Use device with highest Gflops/s
  cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );


  initPBO(&pbo);

    paraMap = new float[(int)renderCam->resolution.x *(int)renderCam->resolution.y];
   effectiveRayMap =new int[(int)renderCam->resolution.x *(int)renderCam->resolution.y];
   initialRayMap = new ray[(int)renderCam->resolution.x * (int)renderCam->resolution.y];
   generateRayMap(renderCam, targetFrame);
  // Clean up on program exit
  atexit(cleanupCuda);

  runCuda();
}
Exemple #16
0
void display(){
    runCuda();

    glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
    glBindTexture(GL_TEXTURE_2D, image);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 
          GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glClear(GL_COLOR_BUFFER_BIT);   

    // VAO, shader program, and texture already bound
    glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);

	glutPostRedisplay();
    glutSwapBuffers();
}
Exemple #17
0
void initCuda(){
  // Use device with highest Gflops/s
  cudaGLSetGLDevice( compat_getMaxGflopsDeviceId() );

  initPBO(&pbo);

  modelView = glm::lookAt(cameraPosition, lookat, up);
  projection = glm::perspective(fovy, float(width) / float(height), zNear, zFar);  
  viewPort[0] = glm::vec4(-float(width)/2,0,0,0.0f);
  viewPort[1] = glm::vec4(0,-(float)height/2,0,0.0f);
  viewPort[2] = glm::vec4(0,0,1.f/2.0f,0.0f);
  viewPort[3] = glm::vec4((float)width/2,(float)height/2,1.0f/2.0f,1.0f);
  // Clean up on program exit
  atexit(cleanupCuda);

  runCuda();
}
Exemple #18
0
	void display(){
		runCuda();

		string title = "CIS565 Render | " + utilityCore::convertIntToString(iterations) + " Iterations";
		glfwSetWindowTitle(title.c_str());

		glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
		glBindTexture(GL_TEXTURE_2D, displayImage);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 
			  GL_RGBA, GL_UNSIGNED_BYTE, NULL);

		glClear(GL_COLOR_BUFFER_BIT);   

		// VAO, shader program, and texture already bound
		glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);

		glfwSwapBuffers();
	}
Exemple #19
0
void display()
{
    // run CUDA kernel
    runCuda();

    // Create a texture from the buffer
    glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);

    // bind texture from PBO
    glBindTexture(GL_TEXTURE_2D, textureID);


    // Note: glTexSubImage2D will perform a format conversion if the
    // buffer is a different format from the texture. We created the
    // texture with format GL_RGBA8. In glTexSubImage2D we specified
    // GL_BGRA and GL_UNSIGNED_INT. This is a fast-path combination

    // Note: NULL indicates the data resides in device memory
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height,
                    GL_RGBA, GL_UNSIGNED_BYTE, NULL);


    // Draw a single Quad with texture coordinates for each vertex.

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,1.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(0.0f,1.0f,0.0f);
    glTexCoord2f(1.0f,0.0f);
    glVertex3f(1.0f,1.0f,0.0f);
    glTexCoord2f(1.0f,1.0f);
    glVertex3f(1.0f,0.0f,0.0f);
    glEnd();

    // Don't forget to swap the buffers!
    glutSwapBuffers();

    // if animFlag is true, then indicate the display needs to be redrawn
    if(animFlag) {
        glutPostRedisplay();
        animTime += animInc;
    }
}
void display()
{
    static float fps = 0;
    frame++;
    int time=glutGet(GLUT_ELAPSED_TIME);

    if (time - timebase > 1000) {
        fps = frame*1000.0f/(time-timebase);
        timebase = time;
        frame = 0;
    }
	float executionTime = glutGet(GLUT_ELAPSED_TIME) - timeSinceLastFrame;
	timeSinceLastFrame = glutGet(GLUT_ELAPSED_TIME);
    runCuda();

	char title[100];
	sprintf( title, "Flocking Simulation [%0.2f fps] [%0.2fms] ", fps, executionTime);
    glutSetWindowTitle(title);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
#if VISUALIZE == 1

    glUseProgram(program[PASS_THROUGH]);

    glEnableVertexAttribArray(positionLocation);
    glBindBuffer(GL_ARRAY_BUFFER, planetVBO);
    glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); 

	glEnableVertexAttribArray(velocityLocation);
	glBindBuffer(GL_ARRAY_BUFFER, velocityVBO);
	glVertexAttribPointer((GLuint)velocityLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planetIBO);
   
    glPointSize(4.0f); 
    glDrawElements(GL_POINTS, N_FOR_VIS, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(positionLocation);
//	Draw_Axes();
#endif
    glutPostRedisplay();
    glutSwapBuffers();
}
Exemple #21
0
void mainLoop() {
  while(!glfwWindowShouldClose(window)){
    glfwPollEvents();
    runCuda();

    string title = "CIS565 Render | " + utilityCore::convertIntToString(iterations) + " Iterations";
		glfwSetWindowTitle(window, title.c_str());
    
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
    glBindTexture(GL_TEXTURE_2D, displayImage);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glClear(GL_COLOR_BUFFER_BIT);   

    // VAO, shader program, and texture already bound
    glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);
    glfwSwapBuffers(window);
  }
  glfwDestroyWindow(window);
  glfwTerminate();
}
Exemple #22
0
	void display(){
		theFpsTracker.timestamp();

		cudaEvent_t start, stop;
		float time;

		// Keep track of time
		cudaEventCreate(&start);
		cudaEventCreate(&stop);

		cudaEventRecord( start, 0 );

		runCuda();

		cudaEventRecord( stop, 0 );
		cudaEventSynchronize( stop );

		cudaEventElapsedTime( &time, start, stop );
		cudaEventDestroy( start );
		cudaEventDestroy( stop );

		char info[1024];
		sprintf(info, "565Raytracer | %i Iterations | Framerate : %3.1f fps | GPU Elapsed Time : %3.1f ms", iterations, theFpsTracker.fpsAverage(), time);
		string title(info);
		
		glutSetWindowTitle(title.c_str());

		glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
		glBindTexture(GL_TEXTURE_2D, displayImage);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 
			  GL_RGBA, GL_UNSIGNED_BYTE, NULL);

		glClear(GL_COLOR_BUFFER_BIT);   

		// VAO, shader program, and texture already bound
		glDrawElements(GL_TRIANGLES, 6,  GL_UNSIGNED_SHORT, 0);

		glutPostRedisplay();
		glutSwapBuffers();
	}
Exemple #23
0
//! Display callback for GLUT
//! Keyboard events handler for GLUT
//! Display callback for GLUT
void display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // set view matrix
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0, 0.0, translate_z);
  glRotatef(rotate_x, 1.0, 0.0, 0.0);
  glRotatef(rotate_y, 0.0, 1.0, 0.0);
  
  // run CUDA kernel to generate vertex positions
  runCuda();
  
  // render the data
  renderCuda(drawMode);
  
  glutSwapBuffers();
  glutPostRedisplay();
  
  animTime += 0.01;
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest(int argc, char **argv, char *ref_file)
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "CUDA/D3D10 simpleD3D10", NULL
                    };
    RegisterClassEx(&wc);

    // Create the application's window
    int xBorder = ::GetSystemMetrics(SM_CXSIZEFRAME);
    int yMenu = ::GetSystemMetrics(SM_CYMENU);
    int yBorder = ::GetSystemMetrics(SM_CYSIZEFRAME);
    HWND hWnd = CreateWindow(wc.lpszClassName, "CUDA/D3D10 simpleD3D10",
                             WS_OVERLAPPEDWINDOW, 0, 0, g_WindowWidth + 2*xBorder, g_WindowHeight+ 2*yBorder+yMenu,
                             NULL, NULL, wc.hInstance, NULL);

    // Initialize Direct3D
    if (SUCCEEDED(InitD3D(hWnd)))
    {
        // Create the scene geometry
        if (SUCCEEDED(InitGeometry()))
        {
            // Initialize interoperability between CUDA and Direct3D
            // Register vertex buffer with CUDA
            // DEPRECATED: cudaD3D10RegisterResource(g_pVB, cudaD3D10RegisterFlagsNone);
            cudaGraphicsD3D10RegisterResource(&cuda_VB_resource, g_pVB, cudaD3D10RegisterFlagsNone);
            getLastCudaError("cudaGraphicsD3D10RegisterResource (g_pVB) failed");

            // Initialize vertex buffer with CUDA
            runCuda();

            // Save result
            SaveResult(argc, argv);

            // Show the window
            ShowWindow(hWnd, SW_SHOWDEFAULT);
            UpdateWindow(hWnd);

            // Enter the message loop
            MSG msg;
            ZeroMemory(&msg, sizeof(msg));

            while (msg.message!=WM_QUIT)
            {
                if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
                else
                {
                    Render();

                    if (ref_file != NULL)
                    {
                        for (int count=0; count<g_iFrameToCompare; count++)
                        {
                            Render();
                        }

                        const char *cur_image_path = "simpleD3D10.ppm";

                        // Save a reference of our current test run image
                        CheckRenderD3D10::ActiveRenderTargetToPPM(g_pd3dDevice,cur_image_path);

                        // compare to offical reference image, printing PASS or FAIL.
                        g_bPassed = CheckRenderD3D10::PPMvsPPM(cur_image_path, ref_file, argv[0],MAX_EPSILON, 0.15f);

                        Cleanup();

                        PostQuitMessage(0);
                    }
                }
            }
        }
    }

    // Release D3D Library (after message loop)
    dynlinkUnloadD3D10API();

    UnregisterClass(wc.lpszClassName, wc.hInstance);
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    g_pd3dDevice->RSSetState(g_pRasterState);

    // Draw frame
    {
        static float time = 0.f;
        time += 0.001f;
        g_pTime->SetFloat(time);

        // Clear the Color to a black color
        float ClearColor[4] = {0.f, 0.1f, 0.1f, 1.f};
        g_pd3dDevice->ClearRenderTargetView(g_color.pBufferRTV, ClearColor);
        g_pd3dDevice->OMSetRenderTargets(1, &g_color.pBufferRTV, NULL);

        g_pd3dDevice->IASetInputLayout(0);
        g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

        g_pDisplayTechnique->GetPassByIndex(0)->Apply(0);
        g_pd3dDevice->DrawInstanced(4, 400, 0, 0);
    }

    // Run CUDA to compute the histogram
    runCuda();

    // draw the 2d texture
    {
        // Clear the Color to a black color
        float ClearColor[4] = {0, 0, 0, 1.f};
        g_pd3dDevice->ClearRenderTargetView(g_pSwapChainRTV, ClearColor);
        g_pd3dDevice->OMSetRenderTargets(1, &g_pSwapChainRTV, NULL);

        g_pd3dDevice->IASetInputLayout(0);
        g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

        g_pTexture2D->SetResource(g_color.pBufferSRV);
        g_pHistogram->SetResource(g_histogram.pBufferSRV);

        g_pUseCase->SetInt(0);
        float quadRect1[4] = { -1.0f, -0.8f, 2.0f, 1.8f };
        g_pvQuadRect->SetFloatVector((float *) &quadRect1);

        g_pCompositeTechnique->GetPassByIndex(0)->Apply(0);
        g_pd3dDevice->Draw(4, 0);

        g_pUseCase->SetInt(1);
        float quadRect2[4] = { -0.8f, -0.99f, 1.6f, 0.19f };
        g_pvQuadRect->SetFloatVector((float *) &quadRect2);

        g_pCompositeTechnique->GetPassByIndex(0)->Apply(0);
        g_pd3dDevice->Draw(4, 0);

        g_pTexture2D->SetResource(NULL);
        g_pHistogram->SetResource(NULL);
        g_pCompositeTechnique->GetPassByIndex(0)->Apply(0);

    }

    // Present the backbuffer contents to the display
    g_pSwapChain->Present(0, 0);
}
void display()
{
    static float fps = 0;
    frame++;
    int time=glutGet(GLUT_ELAPSED_TIME);

    if (time - timebase > 1000) {
        fps = frame*1000.0f/(time-timebase);
        timebase = time;
        frame = 0;
    }
    runCuda();
	projection = glm::perspective(fovy, float(width)/float(height), zNear, zFar);
	view = camera.getViewMatrix();

    projection = projection * view;

    char title[100];
    sprintf( title, "565 NBody sim [%0.2f fps]", fps );
    glutSetWindowTitle(title);


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
#if VISUALIZE == 1
    // VAO, shader program, and texture already bound
    //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    //glDrawElements(GL_TRIANGLES, 6*field_width*field_height,  GL_UNSIGNED_INT, 0);

    glUseProgram(program[HEIGHT_FIELD]);
	GLint location;

	if ((location = glGetUniformLocation(program[0], "u_image")) != -1)
    {
        glUniform1i(location, 0);
    }
    if ((location = glGetUniformLocation(program[0], "u_projMatrix")) != -1)
    {
        glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
    }
    if ((location = glGetUniformLocation(program[0], "u_height")) != -1)
    {
        glUniform1i(location, 0);
    }

    glEnableVertexAttribArray(positionLocation);
    glEnableVertexAttribArray(texcoordsLocation);
    
    glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
    glVertexAttribPointer((GLuint)positionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); 

    glBindBuffer(GL_ARRAY_BUFFER, planeTBO);
    glVertexAttribPointer((GLuint)texcoordsLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeIBO);

    glDrawElements(GL_TRIANGLES, 6*field_width*field_height,  GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(positionLocation);
    glDisableVertexAttribArray(texcoordsLocation);

    glUseProgram(program[PASS_THROUGH]);

	if ((location = glGetUniformLocation(program[1], "u_projMatrix")) != -1)
    {
        glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
    }
    if ((location = glGetUniformLocation(program[1], "u_cameraPos")) != -1)
    {
        glUniform3fv(location, 1, &camera.Position[0]);
    }
	if ((location = glGetUniformLocation(program[1], "sideLen")) != -1)
    {
		glUniform1i(location, (int)sqrt((float)N_FOR_VIS));
    }
	if ((location = glGetUniformLocation(program[1], "matrixColoring")) != -1)
    {
#if COLOR_MODE == COLOR_MIXED
		int colorAsMatrix = 1;
#else 
		int colorAsMatrix = 0;
#endif
		glUniform1i(location, colorAsMatrix);
    }


    glEnableVertexAttribArray(positionLocation);
	glEnableVertexAttribArray(indexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, planetVBO);
    glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); 

	glBindBuffer(GL_ARRAY_BUFFER, planetInBO);
	glVertexAttribIPointer((GLuint)indexLocation, 1, GL_INT, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planetIBO);
   
    glPointSize(4.0f); 
    glDrawElements(GL_POINTS, N_FOR_VIS+1, GL_UNSIGNED_INT, 0);

    glPointSize(1.0f);

    glDisableVertexAttribArray(positionLocation);
	glDisableVertexAttribArray(indexLocation);

#endif
    glutPostRedisplay();
    glutSwapBuffers();
}
Exemple #27
0
void display()
{
    static float fps = 0;
    frame++;
    int time=glutGet(GLUT_ELAPSED_TIME);

    if (time - timebase > 1000) {
        fps = frame*1000.0f/(time-timebase);
        timebase = time;
        frame = 0;
    }
    runCuda();

    char title[100];
    sprintf( title, "565 NBody sim [%0.2f fps]", fps );
    glutSetWindowTitle(title);

    glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
    glBindTexture(GL_TEXTURE_2D, displayImage);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, field_width, field_height, 
            GL_RGBA, GL_FLOAT, NULL);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
#if VISUALIZE == 1
    // VAO, shader program, and texture already bound

    glUseProgram(program[HEIGHT_FIELD]);

    glEnableVertexAttribArray(positionLocation);
    glEnableVertexAttribArray(texcoordsLocation);
    
    glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
    glVertexAttribPointer((GLuint)positionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); 

    glBindBuffer(GL_ARRAY_BUFFER, planeTBO);
    glVertexAttribPointer((GLuint)texcoordsLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planeIBO);

    glDrawElements(GL_TRIANGLES, 6*field_width*field_height,  GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(positionLocation);
    glDisableVertexAttribArray(texcoordsLocation);

    glUseProgram(program[PASS_THROUGH]);

    glEnableVertexAttribArray(positionLocation);

    glBindBuffer(GL_ARRAY_BUFFER, planetVBO);
    glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, planetIBO);
   
    glPointSize(4.0f); 
    glDrawElements(GL_POINTS, N_FOR_VIS+1, GL_UNSIGNED_INT, 0);

    glPointSize(1.0f);

    glDisableVertexAttribArray(positionLocation);

#endif
    glutPostRedisplay();
    glutSwapBuffers();
}
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
    // run CUDA kernel to generate vertex positions
    if (animate) {
        runCuda();
    }

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // set view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(translateX, translateY, translateZ);
    glRotatef(rotateX, 1.0, 0.0, 0.0);
    glRotatef(rotateY, 0.0, 1.0, 0.0);

    // render from the vbo
    glBindBuffer(GL_ARRAY_BUFFER, posVertexBuffer);
    glVertexPointer(4, GL_FLOAT, 0, 0);
    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, heightVertexBuffer);
    glClientActiveTexture(GL_TEXTURE0);
    glTexCoordPointer(1, GL_FLOAT, 0, 0);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, slopeVertexBuffer);
    glClientActiveTexture(GL_TEXTURE1);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glUseProgram(shaderProg);
    
    // Set default uniform variables parameters for the vertex shader
    GLuint uniHeightScale, uniChopiness, uniSize;

    uniHeightScale = glGetUniformLocation(shaderProg, "heightScale");
    glUniform1f(uniHeightScale, 0.5f);

    uniChopiness   = glGetUniformLocation(shaderProg, "chopiness");
    glUniform1f(uniChopiness, 1.0f);

    uniSize        = glGetUniformLocation(shaderProg, "size");
    glUniform2f(uniSize, meshW, meshH);

    // Set default uniform variables parameters for the pixel shader
    GLuint uniDeepColor, uniShallowColor, uniSkyColor, uniLightDir;

    uniDeepColor = glGetUniformLocation(shaderProg, "deepColor");
    glUniform4f(uniDeepColor, 0.0f, 0.0f, 0.1f, 1.0f);

    uniShallowColor = glGetUniformLocation(shaderProg, "shallowColor");
    glUniform4f(uniShallowColor, 0.1f, 0.4f, 0.3f, 1.0f);

    uniSkyColor = glGetUniformLocation(shaderProg, "skyColor");
    glUniform4f(uniSkyColor, 0.5f, 0.5f, 0.5f, 1.0f);

    uniLightDir = glGetUniformLocation(shaderProg, "lightDir");
    glUniform3f(uniLightDir, 0.0f, 1.0f, 0.0f);
    // end of uniform settings

    glColor3f(1.0, 1.0, 1.0);
    if (drawPoints) {
        glDrawArrays(GL_POINTS, 0, meshW * meshH);
    } else {
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

        glPolygonMode(GL_FRONT_AND_BACK, wireFrame ? GL_LINE : GL_FILL);
            glDrawElements(GL_TRIANGLE_STRIP, ((meshW*2)+2)*(meshH-1), GL_UNSIGNED_INT, 0);        
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }
    glDisableClientState(GL_VERTEX_ARRAY);
    glClientActiveTexture(GL_TEXTURE0);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glClientActiveTexture(GL_TEXTURE1);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glUseProgram(0);

    if (g_CheckRender && g_CheckRender->IsQAReadback() && g_Verify) {
        printf("> (Frame %d) Readback BackBuffer\n", frameCount);
        g_CheckRender->readback( windowW, windowH );
        g_CheckRender->savePPM ( sOriginal[0], true, NULL);
        if (!g_CheckRender->PPMvsPPM( sOriginal[0], sReference[0], MAX_EPSILON, THRESHOLD)) {
            g_TotalErrors++;   
        }
        g_Verify = false;
    }
    glutSwapBuffers();

    computeFPS();
}
////////////////////////////////////////////////////////////////////////////////
//! Run test
////////////////////////////////////////////////////////////////////////////////
void runGraphicsTest(int argc, char** argv)
{
    printf("[%s] ", sSDKsample);
    if (g_bOpenGLQA)   printf("[OpenGL Readback Comparisons] ");
    printf("\n");

    if ( cutCheckCmdLineFlag(argc, (const char **)argv, "device") ) { 
		printf("[%s]\n", argv[0]);
		printf("   Does not explicitly support -device=n in OpenGL mode\n");
		printf("   To use -device=n, the sample must be running w/o OpenGL\n\n");
		printf(" > %s -device=n -qatest\n", argv[0]);
		printf("exiting...\n");
		exit(0);
    }

    // First initialize OpenGL context, so we can properly set the GL for CUDA.
    // This is necessary in order to achieve optimal performance with OpenGL/CUDA interop.
    if(CUTFalse == initGL( &argc, argv )) {
        cudaThreadExit();
        return;
    }

    cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );

    // create FFT plan
    CUFFT_SAFE_CALL(cufftPlan2d(&fftPlan, meshW, meshH, CUFFT_C2R) );

    // allocate memory
    fftInputW = (meshW / 2)+1;
    fftInputH = meshH;
    fftInputSize = (fftInputW*fftInputH)*sizeof(float2);

    cutilSafeCall(cudaMalloc((void **)&d_h0, fftInputSize) );
    cutilSafeCall(cudaMalloc((void **)&d_ht, fftInputSize) );
    h_h0 = (float2 *) malloc(fftInputSize);
    generate_h0();
    cutilSafeCall(cudaMemcpy(d_h0, h_h0, fftInputSize, cudaMemcpyHostToDevice) );

    cutilSafeCall(cudaMalloc((void **)&d_slope, meshW*meshH*sizeof(float2)) );

    cutCreateTimer(&timer);
    cutStartTimer(timer);
    prevTime = cutGetTimerValue(timer);

    // create vertex buffers and register with CUDA
    createVBO(&heightVertexBuffer, meshW*meshH*sizeof(float));
    // DEPRECATED: cutilSafeCall(cudaGLRegisterBufferObject(heightVertexBuffer));
	cutilSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_heightVB_resource, heightVertexBuffer, cudaGraphicsMapFlagsWriteDiscard));

    createVBO(&slopeVertexBuffer, meshW*meshH*sizeof(float2));
    // DEPRECATED: cutilSafeCall(cudaGLRegisterBufferObject(slopeVertexBuffer));
	cutilSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_slopeVB_resource, slopeVertexBuffer, cudaGraphicsMapFlagsWriteDiscard));

    // create vertex and index buffer for mesh
    createMeshPositionVBO(&posVertexBuffer, meshW, meshH);
    createMeshIndexBuffer(&indexBuffer, meshW, meshH);

    // Creating the Auto-Validation Code
    if (g_bOpenGLQA) {
        g_CheckRender = new CheckBackBuffer(windowH, windowH, 4);
        g_CheckRender->setPixelFormat(GL_RGBA);
        g_CheckRender->setExecPath(argv[0]);
        g_CheckRender->EnableQAReadback(true);
    }

    runCuda();

    // register callbacks
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);

    // start rendering mainloop
    glutMainLoop();
    cudaThreadExit();
}