如何使用JOCL将结构体数组传递给内核

huangapple 未分类评论50阅读模式
英文:

How to pass an array of structs to kernel with JOCL

问题

我正在使用JOCL(Java的OpenCL绑定)编写一个OpenCL光线追踪器。我想要将一个结构体数组传递给我的内核。这个结构体的定义如下:

struct Sphere {
    float3 center;
    float radius;
    int materialIndex;
};

而内核接受的形式如下:

__kernel void raytraceKernel(
	__constant struct Sphere *spheres,
    // 其他参数...

在这种情况下,最佳的做法是什么?最初,这个结构体没有materialIndex字段,所以我只是在Java中创建了一个浮点数数组,将中心、半径和填充数据填充进去,然后将其发送到内核中。然而,当我添加了materialIndex字段后,我决定将我的程序转换为使用NIO缓冲区,但这并没有成功(数据损坏)。

在JOCL中向内核传递结构体数组是否有更好的方法呢?

英文:

I'm writing an OpenCL raytracer using JOCL (OpenCL bindings for Java). I want to pass an array of structs to my kernel. The struct looks like this:

struct Sphere {
    float3 center;
    float radius;
    int materialIndex;
};

and the kernel takes it as this:

__kernel void raytraceKernel(
	__constant struct Sphere *spheres,
    etc...

What is the best way to do this? Initially, the struct did not have the materialIndex field, so I simply created an array of floats in Java, populated it with center, radius, and padding, and sent it to the kernel. However, when I added materialIndex I decided to convert my program to use NIO buffers, which didn't work (data was corrupted).

Is there a better approach to passing an array of structs to a kernel with JOCL?

huangapple
  • 本文由 发表于 2020年5月30日 01:29:46
  • 转载请务必保留本文链接:https://java.coder-hub.com/62091539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定