#include "cl_helpers.h"
#include <mutex>
#include <vector>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace cl;
using namespace std;
const unsigned DIMX = 1000;
const unsigned DIMY = 800;
const float dx = 0.1;
const float FRANGE_START = 0.f;
const float FRANGE_END = 2 * 3.141592f;
const unsigned DATA_SIZE = ( FRANGE_END - FRANGE_START ) / dx;
#define USE_FORGE_OPENCL_COPY_HELPERS
static const std::string sinf_ocl_kernel = R"(
kernel void sinf(global float* out, const float dx, const unsigned DATA_SIZE, int fnCode)
{
unsigned x = get_global_id(0);
if(x < DATA_SIZE) {
out[2 * x] = x * dx ;
switch(fnCode) {
case 0:
out[ 2 * x + 1 ] = sin(x*dx);
break;
case 1:
out[ 2 * x + 1 ] = cos(x*dx);
break;
case 2:
out[ 2 * x + 1 ] = tan(x*dx);
break;
case 3:
out[ 2 * x + 1 ] = log10(x*dx);
break;
}
}
}
)";
void kernel(cl::Buffer& devOut, cl::CommandQueue& queue, int fnCode)
{
static std::once_flag compileFlag;
static cl::Program prog;
static cl::Kernel kern;
std::call_once(compileFlag,
[queue]() {
prog = cl::Program(queue.getInfo<CL_QUEUE_CONTEXT>(), sinf_ocl_kernel, true);
kern = cl::Kernel(prog, "sinf");
});
static const NDRange global(DATA_SIZE * 2);
kern.setArg(0, devOut);
kern.setArg(1, dx);
kern.setArg(2, DATA_SIZE);
kern.setArg(3, fnCode);
queue.enqueueNDRangeKernel(kern, cl::NullRange, global);
}
int main(void)
{
try {
wnd.makeCurrent();
chart.setAxesLimits(FRANGE_START, FRANGE_END, -1.0f, 1.0f);
context = createCLGLContext(wnd);
Device device = context.getInfo<CL_CONTEXT_DEVICES>()[0];
queue = CommandQueue(context, device);
cl::Buffer sinOut(context, CL_MEM_READ_WRITE, sizeof(float) * DATA_SIZE * 2);
cl::Buffer cosOut(context, CL_MEM_READ_WRITE, sizeof(float) * DATA_SIZE * 2);
cl::Buffer tanOut(context, CL_MEM_READ_WRITE, sizeof(float) * DATA_SIZE * 2);
cl::Buffer logOut(context, CL_MEM_READ_WRITE, sizeof(float) * DATA_SIZE * 2);
kernel(sinOut, queue, 0);
kernel(cosOut, queue, 1);
kernel(tanOut, queue, 2);
kernel(logOut, queue, 3);
do {
wnd.draw(chart);
} while(!wnd.close());
releaseGLBuffer(handles[0]);
releaseGLBuffer(handles[1]);
releaseGLBuffer(handles[2]);
releaseGLBuffer(handles[3]);
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
Plot is a line graph to display two dimensional data.
Definition: plot.h:198
FGAPI unsigned vertices() const
Get the buffer identifier for vertices.
FGAPI unsigned verticesSize() const
Get the vertex buffer size in bytes.
FGAPI void setLegend(const char *pLegend)
Set plot legend.
FGAPI void setColor(const forge::Color pColor)
Set the color of line graph(plot)
Window is where other objects such as Images, Plots etc.
Definition: window.h:300
@ FG_PLOT_SCATTER
Scatter plot.
Definition: defines.h:162
@ FG_PLOT_LINE
Line plot.
Definition: defines.h:161
@ FG_MARKER_NONE
No marker.
Definition: defines.h:167
@ FG_MARKER_TRIANGLE
Triangle marker.
Definition: defines.h:171
@ FG_MARKER_CROSS
Cross-hair marker.
Definition: defines.h:172
fg_color
Definition: defines.h:139
@ FG_RED
Definition: defines.h:140
@ FG_BLUE
Definition: defines.h:142
@ FG_YELLOW
Definition: defines.h:143
@ FG_CHART_2D
Two dimensional charts.
Definition: defines.h:118
@ f32
Definition: defines.h:193
Definition: ComputeCopy.h:80