6

I've seen this done long ago with hlsl/glsl shader code -- using an #include on the source code file that pastes the code into a char* so that no file IO happens at runtime.

If I were to represent it as pseudo-code, it would look a little like this:

#define CLSourceToString(filename) " #include "filename" "
const char* kernel = CLSourceToString("kernel.cl");

Now of course that #define isn't going to work because it'll just try to use those quotation marks to start strings.

a3f
  • 8,517
  • 1
  • 41
  • 46
Adam
  • 245
  • 3
  • 5

2 Answers2

12

See the bullet physics engines use of OpenCL for how to do this to a kernel.

In C++ / C source

#define MSTRINGIFY(A) #A
char* stringifiedSourceCL = 
#include "VectorAddKernels.cl"

In the OpenCL source

MSTRINGIFY(
   __kernel void VectorAdd(__global float8* c)
   {
    // snipped out OpenCL code...
    return;
   }
);
grrussel
  • 7,209
  • 8
  • 51
  • 71
  • It doesn't work if your .cl file contains commas that are not contained between parentheses, like "int x, y;", it ends your string after "int x". – Michel Rouzic Jan 12 '16 at 16:16
4

According to this, it's not possible, but you can use xxd -i to archieve the same effect.

Community
  • 1
  • 1
sepp2k
  • 363,768
  • 54
  • 674
  • 675