#include <CL/cl.h>
#include <iostream>
#include <string>
#include <fstream>
#pragma comment(lib, "OpenCL.lib")
const char * loadfile(const char * fileName)
{
std::ifstream fs(fileName, std::ios::binary);
fs.seekg(, std::ios::end); int size = fs.tellg();
char * data = new char[size + ];
fs.seekg();
fs.read(data, size);
fs.close();
data[size] = ;
return data;
} int main()
{
cl_platform_id platform;
clGetPlatformIDs(, &platform, NULL); cl_device_id device;
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, , &device, NULL); cl_context context = clCreateContext(NULL, , &device, NULL, NULL, NULL);
cl_command_queue queue = clCreateCommandQueue(context, device, , NULL); const char * clSourceFile = loadfile("H:/QtTool/build/TritonRayTracing/kernel.txt");
cl_program program = clCreateProgramWithSource(context, , &clSourceFile, NULL, NULL);
cl_int result = clBuildProgram(program, , &device, NULL, NULL, NULL);
if (result)
{
std::cout << "Error buring compilation" << std::endl;
}
cl_kernel kernel = clCreateKernel(program, "main", NULL);
cl_mem output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, * sizeof(cl_int), NULL, );
cl_mem buffer1 = clCreateBuffer(context, CL_MEM_READ_WRITE, * sizeof(cl_int), NULL, );
cl_mem buffer2 = clCreateBuffer(context, CL_MEM_READ_WRITE, * sizeof(cl_int), NULL, );
clSetKernelArg(kernel, , sizeof(output), (void *)&output);
clSetKernelArg(kernel, , sizeof(buffer1), (void *)&buffer1);
clSetKernelArg(kernel, , sizeof(buffer2), (void *)&buffer2);
cl_int * buffer1Ptr = (cl_int *)clEnqueueMapBuffer(queue,
buffer1,
CL_TRUE,
CL_MAP_WRITE,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
cl_int * buffer2Ptr = (cl_int *)clEnqueueMapBuffer(queue,
buffer2,
CL_TRUE,
CL_MAP_WRITE,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
for (int i = ; i < ; ++i)
{
buffer1Ptr[i] = i;
buffer2Ptr[i] = i;
}
clEnqueueUnmapMemObject(queue, buffer1, buffer1Ptr, , , );
clEnqueueUnmapMemObject(queue, buffer2, buffer2Ptr, , , );
size_t global_work_size = ;
clEnqueueNDRangeKernel(queue,
kernel,
,
NULL,
&global_work_size,
NULL, , NULL, NULL);
cl_int * resultBufferPtr = (cl_int *)clEnqueueMapBuffer(queue,
output,
CL_TRUE,
CL_MAP_READ,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
for (int i = ; i < ; i++)
{
std::cout << "ptr[" << i << "] = " << resultBufferPtr[i] << std::endl;
}
return ;
}
05-07 15:12