AnsweredAssumed Answered

question about opencl and d3d9 surface interop

Question asked by y06480509 on Dec 1, 2014

I was use opencl to Improve performance,but when i want show the result image with d3d9, is can't get the result image data. what's wrong in my code?

the code:

    m_iPlatformType = 0;
    cl_int    status;
    if (getPlatform(m_OCLplatform, m_iPlatformType) == -1)
    {
     AfxMessageBox("wzError:1002");
     return -1;
    }

    m_OCLdevices= getCl_device_id(m_OCLplatform, pDirect3DDevice9Ex);
//     cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)m_OCLplatform, CL_CONTEXT_ADAPTER_D3D9EX_KHR, (cl_context_properties)(pDirect3DDevice9Ex), CL_CONTEXT_INTEROP_USER_SYNC, CL_FALSE, NULL, NULL };

    cl_context_properties props[6] = {CL_CONTEXT_ADAPTER_D3D9EX_KHR, (cl_context_properties) (pDirect3DDevice9Ex), CL_CONTEXT_INTEROP_USER_SYNC, CL_FALSE, NULL, NULL};

    m_OCLcontext = clCreateContext(props, 1,  m_OCLdevices, NULL, NULL, &status);

    const char *filename = "function.cl";
    string sourceStr;
    status = convertToString(filename, sourceStr);
    const char *source = sourceStr.c_str();
    size_t sourceSize[] = {strlen(source)};
    m_OCLprogram  = clCreateProgramWithSource(m_OCLcontext, 1, &source, sourceSize, NULL);

        status=clBuildProgram(m_OCLprogram, 1, m_OCLdevices, NULL, NULL, NULL);
    if(status != 0)
    {    
     char tbuf[0x10000];
     clGetProgramBuildInfo(m_OCLprogram, *m_OCLdevices, CL_PROGRAM_BUILD_LOG, 0x10000, tbuf, NULL);
     AfxMessageBox(tbuf);
     /*printf("\n%s\n", tbuf);*/
     return -1;
    }
    m_OCLkernel = clCreateKernel(m_OCLprogram, "func3DImageCompose", NULL);

    m_CommandQueue = clCreateCommandQueue(m_OCLcontext,  m_OCLdevices[0], 0, NULL);

......

   cl_int status;

   cl_event writeEvt;

   status = clEnqueueWriteBuffer (

    m_CommandQueue , m_OCLinputBuffer, CL_TRUE,

    0, sizeof( unsigned char) * iSrcWidthStep * iSrcHeight ,  (void *)srcImageData,

    0, NULL, &writeEvt);

   status = clFlush(m_CommandQueue);

   clWaitForEvents(1, &writeEvt); ///wait

   clReleaseEvent(writeEvt);

     _cl_dx9_surface_info_khr adapter_type ={m_d3d_surface, m_d3d_surface_handle};

     m_OCLoutputBufferSuff = clCreateFromDX9MediaSurfaceKHR(m_OCLcontext, CL_MEM_READ_WRITE , CL_ADAPTER_D3D9EX_KHR, &adapter_type, 0 ,&strus);

   status = clEnqueueAcquireDX9MediaSurfacesKHR(m_CommandQueue, 1, &m_OCLoutputBufferSuff, 0, NULL, NULL);

 

   status = clSetKernelArg(m_OCLkernel, 0, sizeof(cl_mem), (void *)&m_OCLinputBuffer);

   status = clSetKernelArg(m_OCLkernel, 1, sizeof(cl_mem), (void *)&m_OCLoutputBufferSuff);

   cl_event eventPoint;
   status = clEnqueueNDRangeKernel(m_CommandQueue, m_OCLkernel, 2, NULL, m_global_work_size, m_localThreads, 0, NULL, &eventPoint);
   clWaitForEvents(1, &eventPoint); ///wait
   clReleaseEvent(eventPoint);
   status = clEnqueueReleaseDX9MediaSurfacesKHR( m_CommandQueue, 1, &m_OCLoutputBufferSuff, 0, NULL, NULL);
   status = clFinish(m_CommandQueue); 

....Other .code...

/* Rendering code */
hr = m_d3d_device->BeginScene();
if (FAILED(hr))
{
  // printf("BeginScene failed.hr=0x%0lX\n", hr);
  return false;
}
m_d3d_device->Clear(0, NULL,
D3DCLEAR_TARGET, 0, 0, 0);
hr = m_d3d_device->StretchRect(m_d3d_surface,
  NULL,
  m_d3d_backbuf,
  NULL,
  D3DTEXF_NONE);  //D3DTEXF_NONE //D3DTEXF_LINEAR
if (FAILED(hr))
{
  // printf("Copying frame to the backbuffer failed.hr=0x%0lX\n", hr);
  return false;
}

hr = m_d3d_device->EndScene();
if (FAILED(hr))
{
  // printf("EndScene failed.\n");
  return false;
}

Outcomes