2022-11-20 13:07:45 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 20/11/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_3_CL_H
|
|
|
|
#define STEP_3_CL_H
|
|
|
|
|
|
|
|
// OpenCL includes
|
|
|
|
#include <CL/cl.h>
|
2022-11-23 11:55:40 -05:00
|
|
|
#include <CL/cl_gl.h>
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef COMPILE_GUI
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-11-20 13:07:45 -05:00
|
|
|
|
|
|
|
#include <engine/util/std.h>
|
|
|
|
|
|
|
|
namespace Raytracing {
|
2022-11-23 11:55:40 -05:00
|
|
|
|
|
|
|
|
2022-11-20 17:32:53 -05:00
|
|
|
class CLProgram {
|
|
|
|
private:
|
|
|
|
cl_int m_CL_ERR{};
|
|
|
|
std::string m_source;
|
|
|
|
|
|
|
|
cl_device_id m_deviceID{};
|
|
|
|
cl_context m_context{};
|
|
|
|
cl_command_queue m_commandQueue{};
|
|
|
|
cl_program m_program{};
|
|
|
|
|
|
|
|
std::unordered_map<std::string, cl_mem> buffers;
|
|
|
|
std::unordered_map<std::string, cl_kernel> kernels;
|
|
|
|
|
2022-11-23 11:55:40 -05:00
|
|
|
void checkBasicErrors() const;
|
2022-11-20 17:32:53 -05:00
|
|
|
public:
|
|
|
|
explicit CLProgram(const std::string& file);
|
|
|
|
void loadCLShader(cl_context context, cl_device_id deviceID);
|
|
|
|
|
|
|
|
void createKernel(const std::string& kernelName);
|
|
|
|
void createBuffer(const std::string& bufferName, cl_mem_flags flags, size_t bytes);
|
|
|
|
void createBuffer(const std::string& bufferName, cl_mem_flags flags, size_t bytes, void* ptr);
|
2022-11-23 11:55:40 -05:00
|
|
|
void createImage(const std::string& imageName, int width, int height);
|
2022-11-20 17:32:53 -05:00
|
|
|
|
|
|
|
void setKernelArgument(const std::string& kernel, const std::string& buffer, int argIndex);
|
|
|
|
|
2022-11-23 11:55:40 -05:00
|
|
|
void runKernel(const std::string& kernel, size_t globalWorkSize, size_t localWorkSize, const size_t* globalWorkOffset = NULL);
|
|
|
|
void runKernel(const std::string& kernel, size_t* globalWorkSize, size_t* localWorkSize, cl_uint workDim = 1, const size_t* globalWorkOffset = NULL);
|
|
|
|
|
2022-11-20 17:32:53 -05:00
|
|
|
void writeBuffer(const std::string& buffer, size_t bytes, void* ptr, cl_bool blocking = CL_TRUE, size_t offset = 0);
|
|
|
|
void readBuffer(const std::string& buffer, size_t bytes, void* ptr, cl_bool blocking = CL_TRUE, size_t offset = 0);
|
2022-11-23 11:55:40 -05:00
|
|
|
void readImage(const std::string& imageName, size_t width, size_t height, void* ptr, cl_bool blocking = CL_TRUE, size_t x = 0, size_t y = 0);
|
2022-11-20 17:32:53 -05:00
|
|
|
|
|
|
|
void flushCommands();
|
|
|
|
void finishCommands();
|
|
|
|
|
|
|
|
~CLProgram();
|
|
|
|
};
|
|
|
|
|
2022-11-20 13:07:45 -05:00
|
|
|
class OpenCL {
|
|
|
|
private:
|
2022-11-20 17:32:53 -05:00
|
|
|
cl_int m_CL_ERR;
|
|
|
|
cl_uint m_numPlatforms;
|
|
|
|
int m_activePlatform;
|
|
|
|
|
|
|
|
cl_platform_id* m_platformIDs;
|
2022-11-22 00:26:23 -05:00
|
|
|
cl_uint m_numOfPlatformIDs{};
|
2022-11-20 13:07:45 -05:00
|
|
|
|
2022-11-22 00:26:23 -05:00
|
|
|
cl_device_id m_deviceID{};
|
|
|
|
cl_uint m_numOfDevices{};
|
2022-11-20 13:07:45 -05:00
|
|
|
|
2022-11-23 11:55:40 -05:00
|
|
|
cl_uint m_computeUnits;
|
|
|
|
cl_uint m_deviceClockFreq;
|
|
|
|
|
2022-11-20 17:32:53 -05:00
|
|
|
cl_context m_context;
|
2022-11-20 13:07:45 -05:00
|
|
|
|
2022-11-23 11:55:40 -05:00
|
|
|
void printDeviceInfo(cl_device_id device);
|
2022-11-20 13:07:45 -05:00
|
|
|
public:
|
2022-11-20 17:32:53 -05:00
|
|
|
explicit OpenCL(int platformID = 0, int deviceID = 0);
|
2022-11-20 13:07:45 -05:00
|
|
|
static void init();
|
2022-11-20 17:32:53 -05:00
|
|
|
static void createCLProgram(CLProgram& program);
|
2022-11-23 11:55:40 -05:00
|
|
|
static cl_uint activeDeviceComputeUnits();
|
|
|
|
static cl_uint activeDeviceFrequency();
|
2022-11-20 13:07:45 -05:00
|
|
|
|
|
|
|
~OpenCL();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_3_CL_H
|