Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.60 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #include <GL/glew.h>
  4. #include <GL/wglew.h>
  5.  
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9. #include <cstdio>
  10. #include <vector>
  11. #include <cstdint>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <string>
  15.  
  16.  
  17. #include <iostream>
  18. #include <string.h>
  19.  
  20. #include <CL/cl.h>
  21. #include <CL/cl_gl.h>
  22. #include <CL/cl_gl_ext.h>
  23.  
  24. #include "ocl_util.h"
  25.  
  26. #pragma comment(lib, "opengl32.lib")
  27. #ifdef _DEBUG
  28. #pragma comment(lib, "glew32sd.lib")
  29. #else
  30. #pragma comment(lib, "glew32s.lib")
  31. #endif
  32. #pragma comment(lib, "OpenCL.lib")
  33.  
  34. // for update loop timing
  35. LARGE_INTEGER lastUpdate;
  36.  
  37. // windows window handles
  38. HDC hdc = 0;
  39. HGLRC hrc = 0;
  40. HWND hWnd = 0;
  41. int width = 512;
  42. int height = 512;
  43.  
  44. // opengl "scene" data, quad vertices, uv coords, etc.
  45. //glm::vec2 pos[] = { glm::vec2(-1, -1), glm::vec2(-1, 1), glm::vec2(1, 1), glm::vec2(1, -1) };
  46. glm::vec2 pos[] = { glm::vec2(-1, -1), glm::vec2(-1, 1), glm::vec2(0, 1), glm::vec2(0, -1) };
  47. glm::vec2 uv[]  = { glm::vec2(0, 0), glm::vec2(0, 1), glm::vec2(1, 1), glm::vec2(1, 0) };
  48.  
  49. // opengl buffers and objects
  50. GLuint vertexbuffer = 0;
  51. GLuint uvBuffer = 0;
  52. GLuint program = 0;
  53. GLuint texture = 0;
  54. GLuint sampler = 0;
  55.  
  56. // opencl buffers and objects
  57. //CL::Image2D<CL::NullType> *img;
  58. //CL::Context *context;
  59. cl_platform_id platform;
  60. cl_device_id device;
  61. cl_context context;
  62. cl_command_queue queue;
  63. cl_program program_cl;
  64. cl_kernel kernel;
  65.  
  66. // utility functions
  67. unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
  68. {
  69.     return ((iA << 24) | (iB << 16) | (iG << 8) | iR);
  70. }
  71. uint64_t GetFileSize(std::string path)
  72. {
  73.     std::ifstream stream(path, std::ios::binary | std::ios::ate);
  74.     return stream.tellg();
  75. }
  76. void ReadFile(std::string path, std::vector<uint8_t>& data, std::ios::openmode mode)
  77. {
  78.     std::ifstream stream(path, mode);
  79.  
  80.     uint64_t size = GetFileSize(path);
  81.     data.resize((std::size_t)size);
  82.     stream.read((char*)data.data(), data.size());
  83. }
  84. std::string ReadFile(std::string path, std::ios::openmode mode)
  85. {
  86.     std::vector<uint8_t> data;
  87.     ReadFile(path, data, mode);
  88.     return std::string(data.begin(), data.end());
  89. }
  90.  
  91. // opengl
  92. void GLAPIENTRY DebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
  93. {
  94.     std::stringstream ss;
  95.     ss << "OpenGL: " << message << " [source=";
  96.     switch(source)
  97.     {
  98.         case GL_DEBUG_SOURCE_API:             ss << "OPENGL"; break;
  99.         case GL_DEBUG_SOURCE_WINDOW_SYSTEM:   ss << "WINDOW_SYSTEM"; break;
  100.         case GL_DEBUG_SOURCE_SHADER_COMPILER: ss << "SHADER_COMPILER"; break;
  101.         case GL_DEBUG_SOURCE_THIRD_PARTY:     ss << "THIRD_PARTY"; break;
  102.         case GL_DEBUG_SOURCE_APPLICATION:     ss << "APPLICATION"; break;
  103.         case GL_DEBUG_SOURCE_OTHER:           ss << "OTHER"; break;
  104.         default:                              ss << "UNDEFINED(0x" << std::hex << source << std::dec << ")"; break;
  105.     }
  106.  
  107.     ss << " type=";
  108.     switch(type)
  109.     {
  110.         case GL_DEBUG_TYPE_ERROR:               ss << "ERROR"; break;
  111.         case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: ss << "DEPRECATED_BEHAVIOR"; break;
  112.         case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:  ss << "UNDEFINED_BEHAVIOR"; break;
  113.         case GL_DEBUG_TYPE_PORTABILITY:         ss << "PORTABILITY"; break;
  114.         case GL_DEBUG_TYPE_PERFORMANCE:         ss << "PERFORMANCE"; break;
  115.         case GL_DEBUG_TYPE_OTHER:               ss << "OTHER"; break;
  116.         default:                                ss << "UNDEFINED(0x" << std::hex << source << std::dec << ")"; break;
  117.     }
  118.  
  119.     ss << " severity=";
  120.     switch(severity)
  121.     {
  122.         case GL_DEBUG_SEVERITY_HIGH:   ss << "HIGH";   break;
  123.         case GL_DEBUG_SEVERITY_MEDIUM: ss << "MEDIUM"; break;
  124.         case GL_DEBUG_SEVERITY_LOW:    ss << "LOW"; break;
  125.         default:                       ss << "UNDEFINED"; break;
  126.     }
  127.  
  128.     ss << " id=" << id << "]";
  129.  
  130.     printf("%s", ss.str().c_str());
  131. }
  132. bool initOpenGL()
  133. {
  134.     // create default opengl context
  135.     hdc = GetDC(hWnd);
  136.  
  137.     PIXELFORMATDESCRIPTOR pfd;
  138.     memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  139.     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  140.     pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  141.     pfd.iPixelType = PFD_TYPE_RGBA;
  142.     pfd.cColorBits = 32;
  143.     pfd.cDepthBits = 24;
  144.     pfd.cStencilBits = 8;
  145.     pfd.iLayerType = PFD_MAIN_PLANE;
  146.  
  147.     int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
  148.     SetPixelFormat(hdc, nPixelFormat, &pfd);
  149.  
  150.     HGLRC tempOpenGLContext = wglCreateContext(hdc);
  151.     wglMakeCurrent(hdc, tempOpenGLContext);
  152.  
  153.     // init glew after we have our opengl context
  154.     GLenum error = glewInit();
  155.     if(error != GLEW_OK)
  156.         return false;
  157.  
  158.     // set all atributes needed for a opengl 3.0+ context
  159.     int attributes[] = {
  160.         WGL_CONTEXT_MAJOR_VERSION_ARB, 4, // Set the MAJOR version of OpenGL to 3  
  161.         WGL_CONTEXT_MINOR_VERSION_ARB, 3, // Set the MINOR version of OpenGL to 2  
  162.         WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, // Set our OpenGL context to be forward compatible
  163.         WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
  164.         0
  165.     };
  166.  
  167.     // create new context and delete
  168.     hrc = wglCreateContextAttribsARB(hdc, NULL, attributes);
  169.     wglMakeCurrent(NULL, NULL);
  170.     wglDeleteContext(tempOpenGLContext);
  171.     wglMakeCurrent(hdc, hrc);
  172.  
  173.     // check version we actually created
  174.     int glVersion[2] = { -1, -1 };
  175.     glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]);
  176.     glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);
  177.  
  178.     // check if is debug context
  179.     bool isDebug = false;
  180.     GLint flags;
  181.     glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
  182.     if(flags & GL_CONTEXT_FLAG_DEBUG_BIT)
  183.     {
  184.         isDebug = true;
  185.     }
  186.  
  187.     // enable opengl debugging output if accessible
  188.     bool useErrorChecking = false;
  189.     if(isDebug && glewGetExtension("GL_KHR_debug"))
  190.     {
  191.         glDebugMessageCallback(&DebugCallback, NULL);
  192.         useErrorChecking = true;
  193.     }
  194.  
  195.     // enable vsync
  196.     //wglSwapIntervalEXT(1);
  197.    
  198.     printf("using OpenGL version %d.%d (context=%s, errorChecking=%s)\n", glVersion[0], glVersion[1], isDebug ? "debug" : "default", useErrorChecking ? "true" : "false");
  199.  
  200.     return true;
  201. }
  202. void genShaders()
  203. {
  204.     const char *vert = "#version 330\n"             \
  205.         "layout(location = 0) in vec2 in_position;" \
  206.         "layout(location = 1) in vec2 in_uv;"       \
  207.         "out vec2 uv;"                              \
  208.         "void main() {"                             \
  209.         "    gl_Position = vec4(in_position, 0, 1);"\
  210.         "    uv = in_uv;"                           \
  211.         "}";
  212.  
  213.     const char *frag = "#version 330\n"             \
  214.         "out vec4 outColor;"                        \
  215.         "in vec2 uv;"                               \
  216.         "uniform sampler2D imgSampler;"             \
  217.         "void main() {"                             \
  218.         "    outColor = texture(imgSampler, uv);"   \
  219.         "}";
  220.  
  221.     // Create the shaders
  222.     GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  223.     GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  224.  
  225.     GLint Result = GL_FALSE;
  226.     int InfoLogLength;
  227.  
  228.     // Compile Vertex Shader
  229.     glShaderSource(VertexShaderID, 1, &vert, NULL);
  230.     glCompileShader(VertexShaderID);
  231.  
  232.     // Check Vertex Shader
  233.     glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  234.     if(Result == GL_FALSE)
  235.     {
  236.         glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  237.         std::vector<char> VertexShaderErrorMessage(InfoLogLength);
  238.         glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  239.         fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
  240.     }
  241.  
  242.     // Compile Fragment Shader
  243.     glShaderSource(FragmentShaderID, 1, &frag, NULL);
  244.     glCompileShader(FragmentShaderID);
  245.  
  246.     // Check Fragment Shader
  247.     glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  248.     if(Result == GL_FALSE)
  249.     {
  250.         glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  251.         std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
  252.         glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  253.         fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
  254.     }
  255.  
  256.     // Link the program
  257.     program = glCreateProgram();
  258.     glAttachShader(program, VertexShaderID);
  259.     glAttachShader(program, FragmentShaderID);
  260.     glLinkProgram(program);
  261.  
  262.     // Check the program
  263.     glGetProgramiv(program, GL_LINK_STATUS, &Result);
  264.     if(Result == GL_FALSE)
  265.     {
  266.         glGetProgramiv(program, GL_INFO_LOG_LENGTH, &InfoLogLength);
  267.         std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
  268.         glGetProgramInfoLog(program, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  269.         fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
  270.     }
  271.  
  272.     glDeleteShader(VertexShaderID);
  273.     glDeleteShader(FragmentShaderID);
  274. }
  275. void setupScene()
  276. {
  277.     glClearColor(0.4f, 0.6f, 0.9f, 1.0f);
  278.     //glClearColor(1, 1, 1, 1);
  279.     glViewport(0, 0, width, height);
  280.  
  281.     // quad positions in ndc space
  282.     glGenBuffers(1, &vertexbuffer);
  283.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  284.     glBufferData(GL_ARRAY_BUFFER, sizeof(pos) * sizeof(glm::vec2), &pos, GL_STATIC_DRAW);
  285.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  286.  
  287.     // base uv data for our quad
  288.     glGenBuffers(1, &uvBuffer);
  289.     glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
  290.     glBufferData(GL_ARRAY_BUFFER, sizeof(uv) * sizeof(glm::vec2), &uv, GL_STATIC_DRAW);
  291.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  292.  
  293.     // basic shader rendering our texture data
  294.     genShaders();
  295.  
  296.     // sampler for reading the texture
  297.     glGenSamplers(1, &sampler);
  298.     glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  299.     glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  300.     glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  301.     glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  302.     glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
  303.  
  304.     // create texture with inital data
  305.     glGenTextures(1, &texture);
  306.     glBindTexture(GL_TEXTURE_2D, texture);
  307.     std::vector<uint32_t> initData(width * height, RGBA2DWORD(255, 0, 0, 32));
  308.     //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, initData.data());
  309.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, initData.data());
  310.     glBindTexture(GL_TEXTURE_2D, 0);
  311.  
  312.     // enable blending
  313.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  314.     glEnable(GL_BLEND);
  315. }
  316. void resize(int newWidth, int newHeight)
  317. {
  318.     width = newWidth;
  319.     height = newHeight;
  320.  
  321.     glViewport(0, 0, width, height);
  322. }
  323.  
  324. // opencl
  325. bool initOpenCL()
  326. {
  327.     cl_int error;
  328.  
  329.     platform;
  330.     error = clGetPlatformIDs(1, &platform, NULL);
  331.    
  332.     device;
  333.     error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL);
  334.  
  335.     size_t ext_size = 1024;
  336.     char ext_string[1024];
  337.     error = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ext_size, ext_string, &ext_size);
  338.  
  339.     cl_context_properties properties[] =
  340.     {
  341.         CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platform),
  342.         CL_GL_CONTEXT_KHR, reinterpret_cast<cl_context_properties>(wglGetCurrentContext()),
  343.         CL_WGL_HDC_KHR, reinterpret_cast<cl_context_properties>(wglGetCurrentDC()),
  344.         0
  345.     };
  346.  
  347.     context = clCreateContext(properties, 1, &device, NULL, NULL, &error);
  348.  
  349.     queue = clCreateCommandQueue(context, device, 0, &error);
  350.    
  351.     std::string kernelSrc = ReadFile("../data/kernel.cl", std::ios::binary);
  352.     const char *source = kernelSrc.c_str();
  353.     program_cl = clCreateProgramWithSource(context, 1, &source, NULL, &error);
  354.  
  355.     error = clBuildProgram(program_cl, 1, &device, NULL, NULL, NULL);
  356.  
  357.     if(error != CL_SUCCESS)
  358.     {
  359.         size_t retSize = 0;
  360.         cl_int ret = 0;
  361.  
  362.         ret = clGetProgramBuildInfo(program_cl, device, CL_PROGRAM_BUILD_LOG, NULL, NULL, &retSize);
  363.         std::vector<char> log(retSize);
  364.         ret = clGetProgramBuildInfo(program_cl, device, CL_PROGRAM_BUILD_LOG, log.size(), &log[0], NULL);
  365.  
  366.         printf("build error: %d\n%s", error, std::string(log.begin(), log.end()).c_str());
  367.     }
  368.  
  369.     kernel = clCreateKernel(program_cl, "image_write", &error);
  370.  
  371.     return true;
  372.  
  373.     /*try
  374.     {
  375.         auto platforms = CL::Info::GetPlatforms();
  376.         auto devices = CL::Info::GetDevices(platforms[0], CL::GPU);
  377.  
  378.         printf("using OpenCL: %s\n", platforms[0].version.c_str());
  379.  
  380.         CL::ContextInit init(devices[0], platforms[0]);
  381.         init.AddPropertyPair(CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC());
  382.         init.AddPropertyPair(CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext());
  383.  
  384.         context = new CL::Context(init);
  385.  
  386.         std::string kernel = ReadFile("../data/kernel.cl", std::ios::binary);
  387.         context->LoadProgramm(kernel, "");
  388.         context->LoadKernel("muh");
  389.  
  390.         img = new CL::Image2D<CL::NullType>(*context, texture);
  391.     }
  392.     catch(CL::ocl_exception &e)
  393.     {
  394.         printf("%s", e.what());
  395.         return false;
  396.     }
  397.  
  398.     return true;*/
  399. }
  400. void setupOpenCLInteropBuffers()
  401. {
  402.     cl_int error;
  403.  
  404.     cl_mem buffer = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texture, &error);
  405.  
  406.     cl_image_format image_format;
  407.     clGetImageInfo(buffer, CL_IMAGE_FORMAT, sizeof(cl_image_format), &image_format, NULL);
  408.  
  409.     glFinish();
  410.  
  411.     error = clEnqueueAcquireGLObjects(queue, 1, &buffer, 0, NULL, NULL);
  412.  
  413.     error = clSetKernelArg(kernel, 0, sizeof(buffer), (void*)&buffer);
  414.     float val = 5.0f;
  415.     error = clSetKernelArg(kernel, 1, sizeof(float), &val);
  416.  
  417.     size_t global_work_size = width*height;
  418.     error = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, &global_work_size, NULL, 0, NULL, NULL);
  419.  
  420.     error = clEnqueueReleaseGLObjects(queue, 1, &buffer, 0, NULL, NULL);
  421.  
  422.     clFinish(queue);
  423. }
  424. // main loop
  425. void update(double delta)
  426. {
  427.     // make memory up to date in case opengl changed buffer
  428.     //glFlush();
  429.     /*glFinish();
  430.  
  431.     context->AquireOpenGLBuffer(*img);
  432.  
  433.     context->SetArgs(*img, 0.5f);
  434.     context->DoWork2D(width, height, 16, 16);
  435.  
  436.     context->ReleaseOpenGLBuffer(*img);
  437.  
  438.     context->Finish();*/
  439. }
  440. void render()
  441. {
  442.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  443.  
  444.     glUseProgram(program);
  445.  
  446.     glActiveTexture(GL_TEXTURE0);
  447.     GLint pos = glGetUniformLocation(program, "imgSampler");
  448.     glUniform1i(pos, 0);
  449.     glBindSampler(0, sampler);
  450.  
  451.     glBindTexture(GL_TEXTURE_2D, texture),
  452.  
  453.     glEnableVertexAttribArray(0);
  454.     glEnableVertexAttribArray(1);
  455.  
  456.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  457.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
  458.  
  459.     glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
  460.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
  461.  
  462.     glDrawArrays(GL_QUADS, 0, sizeof(pos));
  463.  
  464.     glDisableVertexAttribArray(0);
  465.     glDisableVertexAttribArray(1);
  466.  
  467.     glBindTexture(GL_TEXTURE_2D, 0);
  468.  
  469.     glUseProgram(0);
  470. }
  471.  
  472. // winapi window stuff
  473. LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  474. {
  475.     switch(msg)
  476.     {
  477.     case WM_DESTROY:
  478.     {
  479.         PostQuitMessage(0);
  480.         return 0;
  481.     }
  482.     case WM_SIZE:
  483.     {
  484.         int newWidth = LOWORD(lParam);
  485.         int newHeight = HIWORD(lParam);
  486.         resize(newWidth, newHeight);
  487.     }
  488.         break;
  489.     }
  490.  
  491.     return DefWindowProc(hWnd, msg, wParam, lParam);
  492. }
  493. int createWindow()
  494. {
  495.     HINSTANCE hInst = GetModuleHandle(NULL);
  496.  
  497.     WNDCLASSEX wClass;
  498.     ZeroMemory(&wClass, sizeof(WNDCLASSEX));
  499.     wClass.cbClsExtra = NULL;
  500.     wClass.cbSize = sizeof(WNDCLASSEX);
  501.     wClass.cbWndExtra = NULL;
  502.     wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  503.     wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  504.     wClass.hIcon = NULL;
  505.     wClass.hIconSm = NULL;
  506.     wClass.hInstance = hInst;
  507.     wClass.lpfnWndProc = (WNDPROC)WinProc;
  508.     wClass.lpszClassName = "Window Class";
  509.     wClass.lpszMenuName = NULL;
  510.     wClass.style = CS_HREDRAW|CS_VREDRAW;
  511.  
  512.     if(!RegisterClassEx(&wClass))
  513.     {
  514.         int nResult = GetLastError();
  515.         MessageBox(NULL, "Window class creation failed", "Window Class Failed", MB_ICONERROR);
  516.     }
  517.  
  518.     RECT wr = { 0, 0, width, height };
  519.     AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  520.  
  521.     hWnd = CreateWindowEx(NULL, "Window Class", "Windows application", WS_OVERLAPPEDWINDOW, 200, 200, wr.right - wr.left, wr.bottom - wr.top, NULL, NULL, hInst, NULL);
  522.  
  523.     if(!hWnd)
  524.     {
  525.         int nResult = GetLastError();
  526.         MessageBox(NULL, "Window creation failed", "Window Creation Failed", MB_ICONERROR);
  527.     }
  528.  
  529.     //////////////////////////////////////////////////////////////////////////
  530.     // opengls stuff
  531.     if(!initOpenGL())
  532.     {
  533.         return false;
  534.     }
  535.  
  536.     //////////////////////////////////////////////////////////////////////////
  537.  
  538.     if(!initOpenCL())
  539.     {
  540.         return false;
  541.     }
  542.  
  543.     //////////////////////////////////////////////////////////////////////////
  544.  
  545.     setupScene();
  546.     setupOpenCLInteropBuffers();
  547.  
  548.     //////////////////////////////////////////////////////////////////////////
  549.  
  550.     ShowWindow(hWnd, SW_SHOW);
  551.  
  552.     return true;
  553. }
  554. void run()
  555. {
  556.     MSG msg;
  557.     ZeroMemory(&msg, sizeof(MSG));
  558.  
  559.     LARGE_INTEGER frequency;
  560.     QueryPerformanceFrequency(&frequency);
  561.     QueryPerformanceCounter(&lastUpdate);
  562.  
  563.     while(true)
  564.     {
  565.         while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  566.         {
  567.             // Translate the message and dispatch it to WindowProc()
  568.             TranslateMessage(&msg);
  569.             DispatchMessage(&msg);
  570.         }
  571.  
  572.         if(msg.message == WM_QUIT)
  573.             break;
  574.  
  575.         LARGE_INTEGER now;
  576.         QueryPerformanceCounter(&now);
  577.  
  578.         double delta = static_cast<double>(now.QuadPart - lastUpdate.QuadPart) / frequency.QuadPart;
  579.         lastUpdate = now;
  580.  
  581.         update(delta);
  582.         render();
  583.  
  584.         SwapBuffers(hdc);
  585.     }
  586. }
  587.  
  588. // create window and run
  589. int main()
  590. {
  591.     if(!createWindow())
  592.         return -1;
  593.  
  594.     run();
  595.  
  596.     return 0;
  597. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement