コード例 #1
0
bool
CLDeviceContext::Initialize() {

#ifdef OPENSUBDIV_HAS_CLEW
    if (!clGetPlatformIDs) {
        error("Error clGetPlatformIDs function not bound.\n");
        return false;
    }
#endif

    cl_int ciErrNum;
    cl_platform_id cpPlatform = findPlatform();

#if defined(_WIN32)
    cl_context_properties props[] = {
        CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
        CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#elif defined(__APPLE__)
    CGLContextObj kCGLContext = CGLGetCurrentContext();
    CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
    cl_context_properties props[] = {
        CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
        0
    };
#else
    cl_context_properties props[] = {
        CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
        CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#endif

#if defined(__APPLE__)
    _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE,
                                 NULL, &ciErrNum);
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        return false;
    }

    size_t devicesSize = 0;
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            0, NULL, &devicesSize);
    int numDevices = int(devicesSize / sizeof(cl_device_id));
    if (numDevices == 0) {
        error("No sharable devices.\n");
        return false;
    }
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            numDevices * sizeof(cl_device_id), clDevices, NULL);
    int clDeviceUsed = 0;

#else   // not __APPLE__

    // get the number of GPU devices available to the platform
    cl_uint numDevices = 0;
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
    if (numDevices == 0) {
        error("No CL GPU device found.\n");
        return false;
    }

    // create the device list
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, numDevices, clDevices, NULL);

    const char *extension = "cl_khr_gl_sharing";
    int clDeviceUsed = findExtensionSupportedDevice(clDevices, numDevices,
                                                    extension);

    if (clDeviceUsed < 0) {
        error("No device found that supports CL/GL context sharing\n");
        delete[] clDevices;
        return false;
    }

    _clContext = clCreateContext(props, 1, &clDevices[clDeviceUsed],
                                 NULL, NULL, &ciErrNum);

#endif   // not __APPLE__

    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        delete[] clDevices;
        return false;
    }

    _clCommandQueue = clCreateCommandQueue(_clContext, clDevices[clDeviceUsed],
                                    0, &ciErrNum);
    delete[] clDevices;
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateCommandQueue\n", ciErrNum);
        return false;
    }
    return true;
}
コード例 #2
0
ファイル: cl-helper.c プロジェクト: kristofe/OpenCLFluids
void create_context_on(const char *plat_name, const char*dev_name, cl_uint idx,
    cl_context *ctx, cl_command_queue *queue, int enable_profiling)
{
  char dev_sel_buf[MAX_NAME_LEN];
  char platform_sel_buf[MAX_NAME_LEN];

  // get number of platforms
  cl_uint plat_count;
  CALL_CL_GUARDED(clGetPlatformIDs, (0, NULL, &plat_count));

  // allocate memory, get list of platform handles
  cl_platform_id *platforms =
    (cl_platform_id *) malloc(plat_count*sizeof(cl_platform_id));
  CHECK_SYS_ERROR(!platforms, "allocating platform array");
  CALL_CL_GUARDED(clGetPlatformIDs, (plat_count, platforms, NULL));

  // print menu, if requested
#ifndef CL_HELPER_FORCE_INTERACTIVE
  if (plat_name == CHOOSE_INTERACTIVELY) // yes, we want exactly that pointer
#endif
  {
    puts("Choose platform:");
    for (cl_uint i = 0; i < plat_count; ++i)
    {
      char buf[MAX_NAME_LEN];
      CALL_CL_GUARDED(clGetPlatformInfo, (platforms[i], CL_PLATFORM_VENDOR,
            sizeof(buf), buf, NULL));
      printf("[%d] %s\n", i, buf);
    }

    printf("Enter choice: ");
    fflush(stdout);

    char *sel = read_a_line();
    if (!sel)
    {
      fprintf(stderr, "error reading line from stdin");
      abort();
    }

    int sel_int = MIN(MAX(0, atoi(sel)), (int) plat_count-1);
    free(sel);

    CALL_CL_GUARDED(clGetPlatformInfo, (platforms[sel_int], CL_PLATFORM_VENDOR,
          sizeof(platform_sel_buf), platform_sel_buf, NULL));
    plat_name = platform_sel_buf;
  }

  // iterate over platforms
  for (cl_uint i = 0; i < plat_count; ++i)
  {
    // get platform name
    char buf[MAX_NAME_LEN];
    CALL_CL_GUARDED(clGetPlatformInfo, (platforms[i], CL_PLATFORM_VENDOR,
          sizeof(buf), buf, NULL));

    // does it match?
    if (!plat_name || strstr(buf, plat_name))
    {
      // get number of devices in platform
      cl_uint dev_count;
      CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
            0, NULL, &dev_count));

      // allocate memory, get list of device handles in platform
      cl_device_id *devices =
        (cl_device_id *) malloc(dev_count*sizeof(cl_device_id));
      CHECK_SYS_ERROR(!devices, "allocating device array");

      CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
            dev_count, devices, NULL));

      // {{{ print device menu, if requested
#ifndef CL_HELPER_FORCE_INTERACTIVE
      if (dev_name == CHOOSE_INTERACTIVELY) // yes, we want exactly that pointer
#endif
      {
        puts("Choose device:");
        for (cl_uint j = 0; j < dev_count; ++j)
        {
          char buf[MAX_NAME_LEN];
          CALL_CL_GUARDED(clGetDeviceInfo, (devices[j], CL_DEVICE_NAME,
                sizeof(buf), buf, NULL));
          printf("[%d] %s\n", j, buf);
        }

        printf("Enter choice: ");
        fflush(stdout);

        char *sel = read_a_line();
        if (!sel)
        {
          fprintf(stderr, "error reading line from stdin");
          abort();
        }

        int int_sel = MIN(MAX(0, atoi(sel)), (int) dev_count-1);
        free(sel);

        CALL_CL_GUARDED(clGetDeviceInfo, (devices[int_sel], CL_DEVICE_NAME,
              sizeof(dev_sel_buf), dev_sel_buf, NULL));
        dev_name = dev_sel_buf;
      }

      // }}}

      // iterate over devices
      for (cl_uint j = 0; j < dev_count; ++j)
      {
        // get device name
        char buf[MAX_NAME_LEN];
        CALL_CL_GUARDED(clGetDeviceInfo, (devices[j], CL_DEVICE_NAME,
              sizeof(buf), buf, NULL));

        // does it match?
        if (!dev_name || strstr(buf, dev_name))
        {
          if (idx == 0)
          {
            cl_platform_id plat = platforms[i];
            cl_device_id dev = devices[j];

            free(devices);
            free(platforms);

            cl_int status;
            
            // create a context
#if OPENCL_SHARE_WITH_OPENGL
  #if __APPLE__
//              CGLContextObj kCGLContext = CGLGetCurrentContext();
//              CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
//              cl_context_properties cps[] = {
//                CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
//                CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0 };
//            
            
            CGLContextObj gl_context = CGLGetCurrentContext();
            CGLShareGroupObj share_group = CGLGetShareGroup(gl_context);
            
            cl_context_properties properties[] = {
              CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
              (cl_context_properties)share_group, 0
            };
            *ctx = clCreateContext(properties, 0, 0, 0, 0, 0);
            clGetGLContextInfoAPPLE(*ctx, gl_context,
                                    CL_CGL_DEVICE_FOR_CURRENT_VIRTUAL_SCREEN_APPLE, sizeof(dev),
                                    &dev, NULL);
            
            
            
  #elif WIN32
              cl_context_properties cps[] = {
                CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0};
            
            //Probably won't work because &dev should correspond to glContext
            *ctx = clCreateContext(cps, 1, &dev, NULL, NULL, &status);
            CHECK_CL_ERROR(status, "clCreateContext");
  #else
              // Linux
              cl_context_properties cps[] = {
                CL_GL_CONTEXT_KHR, ( cl_context_properties) glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0 };
            //Probably won't work because &dev should correspond to glContext
            *ctx = clCreateContext(cps, 1, &dev, NULL, NULL, &status);
            CHECK_CL_ERROR(status, "clCreateContext");
#endif

#else
            // create a context
            cl_context_properties cps[3] = {
              CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0 };
            // create a command queue
            cl_command_queue_properties qprops = 0;
            if (enable_profiling)
              qprops |= CL_QUEUE_PROFILING_ENABLE;
            
            *queue = clCreateCommandQueue(*ctx, dev, qprops, &status);
            CHECK_CL_ERROR(status, "clCreateCommandQueue");
#endif
//            *ctx = clCreateContext(
//                                   cps, 1, &dev, NULL, NULL, &status);
//            CHECK_CL_ERROR(status, "clCreateContext");

//            // create a command queue
            cl_command_queue_properties qprops = 0;
            if (enable_profiling)
              qprops |= CL_QUEUE_PROFILING_ENABLE;

            *queue = clCreateCommandQueue(*ctx, dev, qprops, &status);
            CHECK_CL_ERROR(status, "clCreateCommandQueue");

            return;
          }
          else
            --idx;
        }
      }

      free(devices);
    }
  }

  free(platforms);

  fputs("create_context_on: specified device not found.\n", stderr);
  abort();
}
コード例 #3
0
ファイル: main.c プロジェクト: wolfviking0/webcl-translator
static void test_opencl_opengl_interop()
{
  cl_int status;

	cl_device_id renderer;

	#ifndef __EMSCRIPTEN__  
  CGLContextObj gl_context = CGLGetCurrentContext();
//  const char * err = CGLErrorString(kCGLContext);
  
  CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(gl_context);
  
  cl_context_properties properties[] = {
    CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
    (cl_context_properties)kCGLShareGroup, 0
  };

  clData.ctx = clCreateContext(properties, 0, 0, 0, 0, &status);
  CHECK_CL_ERROR(status, "clCreateContext");
  
  // And now we can ask OpenCL which particular device is being used by
  // OpenGL to do the rendering, currently:

  clGetGLContextInfoAPPLE(clData.ctx, gl_context,
                          CL_CGL_DEVICE_FOR_CURRENT_VIRTUAL_SCREEN_APPLE, sizeof(renderer),
                          &renderer, NULL);
  #else
	
    cl_context_properties cps[] = {
    CL_GL_CONTEXT_KHR, (cl_context_properties) 0, CL_WGL_HDC_KHR, (cl_context_properties) 0, 0};
            
    //Probably won't work because &dev should correspond to glContext
     clData.ctx = clCreateContext(cps, 1, &renderer, NULL, NULL, &status);

  CHECK_CL_ERROR(status, "clCreateContext");

  #endif
  
  cl_uint id_in_use;
  clGetDeviceInfo(renderer, CL_DEVICE_VENDOR_ID, sizeof(cl_uint),
                  &id_in_use, NULL);
  
  clData.device = renderer;
  
  cl_command_queue_properties qprops = 0;
  
  clData.queue = clCreateCommandQueue(clData.ctx, clData.device, qprops, &status);
  CHECK_CL_ERROR(status, "clCreateCommandQueue");
  
  
  
  
  int extensionExists = 0;
  
  size_t extensionSize;
  int ciErrNum = clGetDeviceInfo( clData.device, CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize );
  char* extensions = (char*) malloc( extensionSize);
  ciErrNum = clGetDeviceInfo( clData.device, CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize);
  
  char * pch;
  //printf ("Splitting extensions string \"%s\" into tokens:\n",extensions);
  pch = strtok (extensions," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    if(strcmp(pch, GL_SHARING_EXTENSION) == 0) {
      printf("Device supports gl sharing\n");
      extensionExists = 1;
      break;
    }
    pch = strtok (NULL, " ");
  }
  
  
  
}