Skip to content

Instantly share code, notes, and snippets.

@jszuppe
Last active July 5, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jszuppe/a199697c53865bc6574eb547b73ede8a to your computer and use it in GitHub Desktop.
Save jszuppe/a199697c53865bc6574eb547b73ede8a to your computer and use it in GitHub Desktop.
// This is for not packed structs but _NOT_ for cl_typeN structs
#define BOOST_COMPUTE_ADAPT_STRUCT_NOT_PACKED(type, name, members) \
BOOST_COMPUTE_TYPE_NAME(type, name) \
namespace boost { namespace compute { \
template<> \
inline std::string type_definition<type>() \
{ \
std::stringstream declaration; \
declaration << "typedef struct {\n" \
BOOST_PP_SEQ_FOR_EACH( \
BOOST_COMPUTE_DETAIL_ADAPT_STRUCT_INSERT_MEMBER, \
type, \
BOOST_COMPUTE_PP_TUPLE_TO_SEQ(members) \
) \
<< "} " << type_name<type>() << ";\n"; \
return declaration.str(); \
} \
namespace detail { \
template<> \
struct inject_type_impl<type> \
{ \
void operator()(meta_kernel &kernel) \
{ \
kernel.add_type_declaration<type>(type_definition<type>()); \
} \
}; \
inline meta_kernel& operator<<(meta_kernel &k, type s) \
{ \
return k << "(" << #name << "){" \
BOOST_PP_SEQ_FOR_EACH_I( \
BOOST_COMPUTE_DETAIL_ADAPT_STRUCT_STREAM_MEMBER, \
s, \
BOOST_COMPUTE_PP_TUPLE_TO_SEQ(members) \
) \
<< "}"; \
} \
}}}
// Internal (This is for cl_typeN structs)
#define BOOST_COMPUTE_ADAPT_CL_VECTOR_STRUCT_NAME(type, n, name) \
BOOST_COMPUTE_TYPE_NAME(type, name) \
namespace boost { namespace compute { \
namespace detail { \
inline meta_kernel& operator<<(meta_kernel &k, type x) \
{ \
k << "(" << type_name<type>() << ")"; \
k << "("; \
for(size_t i = 0; i < n; i++){ \
k << k.lit(x.s[i]); \
\
if(i != n - 1){ \
k << ","; \
} \
} \
k << ")"; \
return k; \
} \
}}}
// This is for cl_typeN structs
#define BOOST_COMPUTE_ADAPT_CL_VECTOR_STRUCT(type, n) \
BOOST_COMPUTE_ADAPT_CL_VECTOR_STRUCT_NAME(BOOST_PP_CAT(cl_, BOOST_PP_CAT(type, n)), n, BOOST_PP_CAT(type, n))
BOOST_COMPUTE_ADAPT_CL_VECTOR_STRUCT(float, 2) // cl_float2
BOOST_COMPUTE_ADAPT_CL_VECTOR_STRUCT(uchar, 4) // cl_uchar4
BOOST_COMPUTE_ADAPT_STRUCT_NOT_PACKED(custom_type, custom_type, (someInt, somePoint))
// You have to rembember that in OpenCL cl_float2 is float2, so
// Boost Compute function that transforms float2 should look like that:
BOOST_COMPUTE_FUNCTION(cl_float2, func_cl_float2, (cl_float2 b),
{
float2 a = b; // not cl_float2 a = b;, as there is no cl_float2 defined in OpenCL
a.x = 5;
return a;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment