Lets say I created the tree and initilized it. Now, how am I going to move nodes vector to the GPU in openCL?
struct BVHNode {
BoundingBox bbox;
BoundingSphere bsphere;
std::vector<int> obj_triangles; // Store triangle indices that is inside node's volume
int parentIndex; // Index of the parent node (-1 for root)
int level;
std::vector<int> childrenIndices; // Indices of the child nodes
}
class BVHTree {
public:
std::vector<BVHNode> nodes;
int maxDepth;
int nodeSize;
}
I tried to move like this
size_t dataSize = bvhTree.nodes.size() * sizeof(BVHNode);
cl::Buffer d_BVHtree_buf(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, dataSize, BVHTree.nodes.data());
but it didn't work.
GPUs support neither function recursion nor classes, and memory bandwidth is very poor for AoS (no coalescence).
Flatten the tree data structure into a structure of arrays layout. You might need to limit tree depth and add padding nodes such that data indices are regularly spaced. Alternatively, you can use indirect addressing, meaning adding an additional an array that contains the memory locations for nodes; this saves memory capacity but will kill memory coalescence. You can do fixed-depth recursion as a loop or chain of function calls.