#include "cl_helpers.h"
#include <mutex>
#include <complex>
#include <cmath>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
static const float XMIN = -8.0f;
static const float XMAX = 8.f;
static const float YMIN = -8.0f;
static const float YMAX = 8.f;
const float DX = 0.5;
const unsigned XSIZE = (XMAX-XMIN)/DX;
const unsigned YSIZE = (YMAX-YMIN)/DX;
using namespace std;
#define USE_FORGE_OPENCL_COPY_HELPERS
static const std::string sin_surf_kernel =
R"EOK(
kernel
void surf(global float* out, const float dx,
const float xmin, const float ymin,
const unsigned w, const unsigned h)
{
int i = get_global_id(0);
int j = get_global_id(1);
float x = xmin + i*dx;
float y = ymin + j*dx;
if (i<w && j<h) {
int offset = j + i * h;
out[ 3 * offset ] = x;
out[ 3 * offset + 1 ] = y;
float z = sqrt(x*x+y*y) + 2.2204e-16;
out[ 3 * offset + 2 ] = sin(z)/z;
}
}
)EOK";
inline
int divup(int a, int b)
{
return (a+b-1)/b;
}
void kernel(cl::Buffer& devOut, cl::CommandQueue& queue, cl::Device& device)
{
static bool compileFlag = true;
static cl::Program prog;
static cl::Kernel kern;
if (compileFlag) {
try {
prog = cl::Program(queue.getInfo<CL_QUEUE_CONTEXT>(), sin_surf_kernel, false);
std::vector<cl::Device> devs;
devs.push_back(device);
prog.build(devs);
kern = cl::Kernel(prog, "surf");
} catch (cl::Error err) {
std::cout << "Compile Errors: " << std::endl;
std::cout << err.what() << err.err() << std::endl;
std::cout << prog.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << std::endl;
exit(255);
}
std::cout<< "Kernels compiled successfully" << std::endl;
compileFlag = false;
}
NDRange local(8, 8);
NDRange global(local[0]*divup(XSIZE, local[0]),
local[1]*divup(YSIZE, local[1]));
kern.setArg(0, devOut);
kern.setArg(1, DX);
kern.setArg(2, XMIN);
kern.setArg(3, YMIN);
kern.setArg(4, XSIZE);
kern.setArg(5, YSIZE);
queue.enqueueNDRangeKernel(kern, cl::NullRange, global, local);
}
int main(void)
{
try {
wnd.makeCurrent();
chart.setAxesLimits(-10.f, 10.f, -10.f, 10.f, -0.5f, 1.f);
chart.setAxesTitles("x-axis", "y-axis", "z-axis");
context = createCLGLContext(wnd);
Device device = context.getInfo<CL_CONTEXT_DEVICES>()[0];
queue = CommandQueue(context, device);
cl::Buffer devOut(context, CL_MEM_READ_WRITE, sizeof(float) * XSIZE * YSIZE * 3);
kernel(devOut, queue, device);
do {
wnd.draw(chart);
} while(!wnd.close());
releaseGLBuffer(handle);
std::cout << err.
what() <<
"(" << err.
err() <<
")" << std::endl;
} catch (cl::Error err) {
std::cout << err.what() << "(" << err.err() << ")" << std::endl;
}
return 0;
}
void * ComputeResourceHandle
A backend-agnostic handle to a compute memory resource originating from an OpenGL resource.
Definition ComputeCopy.h:73
@ FORGE_VERTEX_BUFFER
OpenGL Vertex Buffer Object.
Definition ComputeCopy.h:77
Chart is base canvas where other plottable objects are rendered.
Definition chart.h:316
Definition exception.h:22
ErrorCode err() const
Definition exception.h:31
virtual const char * what() const
Definition exception.h:46
Surface is a graph to display three dimensional data.
Definition surface.h:163
FGAPI unsigned vertices() const
Get the buffer identifier for vertices.
FGAPI unsigned verticesSize() const
Get the vertex buffer size in bytes.
FGAPI void setColor(const forge::Color pColor)
Set the color of line graph(surface)
Window is where other objects such as Images, Plots etc.
Definition window.h:300
@ FG_YELLOW
Definition defines.h:143
@ FG_CHART_3D
Three dimensional charts.
Definition defines.h:119
@ f32
Definition defines.h:193
Definition ComputeCopy.h:80