Raycaster 不支持 OpenCL
目前我正在尝试使用 OpenCL 和 Java (LWJGL) 编写一个光线投射引擎。
然而,在运行内核时,我无法 printf 或写入调试变量。我有一种感觉,内核突然被切断,而且因为报告的时间(opencl 之外)显示出非常快的结果(60fps),比 mandelbrot 示例中的速度更快!
我正在考虑 gDEBugger,但是我认为它不支持 Java。
也非常欢迎对我的代码构成提出任何评论,因为我是初学者。也欢迎对我的光线投射数学发表评论。
#ifdef USE_TEXTURE
#define OUTPUT_TYPE __write_only image2d_t
#else
#define OUTPUT_TYPE global uint *
#endif
#define MAX_RAY_LENGTH 1024
bool isSolid(int x, int y, int z, global uchar *objectIsSolidStorage, int width, int height, int depth) {
uint placex = x / 8;
uint placey = y / 8;
uint placez = z / 8;
uint widthbychar = (width/8);
uint heightbychar = (height/8);
int calcbychar = placex + placey * widthbychar + placez * widthbychar * heightbychar;
int leftover = x % 8;
uchar solidchar = objectIsSolidStorage[calcbychar];
if (x > width || y > height || z >> depth ||
x < 0 || y < 0 || z < 0) return 1;
if ((solidchar & ( 1 << leftover )) >> leftover) {
return 1;
}
return 0;
}
int getColor(int x, int y, int z)
{
return 0xFFFFFFFF;
}
kernel void raycast(
const int width,
const int height,
const float positionX,
const float positionY,
const float positionZ,
const float directionX,
const float directionY,
const float directionZ,
OUTPUT_TYPE output,
global uchar *objectIsSolidStorage,
const int objectStorageWidth,
const int objectStorageHeight,
const int objectStorageDepth,
global uint *objectColorStorage,
global uint *smallObjectsMap,
global uint *hangableObjectsMap,
global uint *largeObjectsMap,
const int objectMapWidth,
const int objectMapHeight,
const float frustrumAngleX,
const float frustrumAngleY,
global char debug
) {
unsigned int rayx = get_global_id(0);
unsigned int rayy = get_global_id(1);
bool hit = false;
int itr = 0;
float x = positionX;
float y = positionY;
float z = positionZ;
printf("work");
int addX = (rayx - width / 2) * frustrumAngleX;
int addY = (rayy - height / 2) * frustrumAngleY;
int color = 0xFFAAFF;
while(!hit) {
x += addX;
y += addY;
z += itr;
if (MAX_RAY_LENGTH < itr) {
hit = true;
color = 0xFFFFFFFF;
break;
} else if (isSolid(x,y,z,objectIsSolidStorage,objectStorageWidth,objectStorageHeight,objectStorageDepth)) {
hit = true;
color = 0xAAAAAAAA;
break;
}
itr++;
}
#ifdef USE_TEXTURE
float4 oc = (float4)(
(c & 0xFF) >> 0,
(c & 0xFF00) >> 8,
(c & 0xFF0000) >> 16,
255.0f
);
write_imagef(output, (int2)(rayx, rayy), color / 255.0f);
#else
output[rayy * width + rayx] = color;
#endif
}
Momentarily I'm trying to write a raycaster engine with OpenCL and Java (LWJGL).
However on running the kernel, I can not printf, or write to a debug variable for that matter. I have the feeling the kernel is abruptly being cut off, also since times reported (outside opencl) are showing very fast results (60fps), faster than in the mandelbrot example!
I'm looking at gDEBugger but, I do not think it will support Java.
Any comments on my code-makeup are very much welcome as well, since I'm a beginner. Also comments on my raycast-mathmatics are also welcome.
#ifdef USE_TEXTURE
#define OUTPUT_TYPE __write_only image2d_t
#else
#define OUTPUT_TYPE global uint *
#endif
#define MAX_RAY_LENGTH 1024
bool isSolid(int x, int y, int z, global uchar *objectIsSolidStorage, int width, int height, int depth) {
uint placex = x / 8;
uint placey = y / 8;
uint placez = z / 8;
uint widthbychar = (width/8);
uint heightbychar = (height/8);
int calcbychar = placex + placey * widthbychar + placez * widthbychar * heightbychar;
int leftover = x % 8;
uchar solidchar = objectIsSolidStorage[calcbychar];
if (x > width || y > height || z >> depth ||
x < 0 || y < 0 || z < 0) return 1;
if ((solidchar & ( 1 << leftover )) >> leftover) {
return 1;
}
return 0;
}
int getColor(int x, int y, int z)
{
return 0xFFFFFFFF;
}
kernel void raycast(
const int width,
const int height,
const float positionX,
const float positionY,
const float positionZ,
const float directionX,
const float directionY,
const float directionZ,
OUTPUT_TYPE output,
global uchar *objectIsSolidStorage,
const int objectStorageWidth,
const int objectStorageHeight,
const int objectStorageDepth,
global uint *objectColorStorage,
global uint *smallObjectsMap,
global uint *hangableObjectsMap,
global uint *largeObjectsMap,
const int objectMapWidth,
const int objectMapHeight,
const float frustrumAngleX,
const float frustrumAngleY,
global char debug
) {
unsigned int rayx = get_global_id(0);
unsigned int rayy = get_global_id(1);
bool hit = false;
int itr = 0;
float x = positionX;
float y = positionY;
float z = positionZ;
printf("work");
int addX = (rayx - width / 2) * frustrumAngleX;
int addY = (rayy - height / 2) * frustrumAngleY;
int color = 0xFFAAFF;
while(!hit) {
x += addX;
y += addY;
z += itr;
if (MAX_RAY_LENGTH < itr) {
hit = true;
color = 0xFFFFFFFF;
break;
} else if (isSolid(x,y,z,objectIsSolidStorage,objectStorageWidth,objectStorageHeight,objectStorageDepth)) {
hit = true;
color = 0xAAAAAAAA;
break;
}
itr++;
}
#ifdef USE_TEXTURE
float4 oc = (float4)(
(c & 0xFF) >> 0,
(c & 0xFF00) >> 8,
(c & 0xFF0000) >> 16,
255.0f
);
write_imagef(output, (int2)(rayx, rayy), color / 255.0f);
#else
output[rayy * width + rayx] = color;
#endif
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该适用于您的 OpenCL+java 开发:附加到 java 进程并轻松调试 OpenCL 内核
http://suhorukov.blogspot.com/2011/12/opencl-kernel-debugging-for-java-host.html
it's should be applicable for your OpenCL+java development: attach to java process and debugging OpenCL kernels with easy
http://suhorukov.blogspot.com/2011/12/opencl-kernel-debugging-for-java-host.html
您可以在CPU上运行它进行调试(amd扩展)
#pragma OPENCL EXTENSION cl_amd_printf : enable
you can run it on the CPU for debugging (amd extension)
#pragma OPENCL EXTENSION cl_amd_printf : enable