我有一个opencl内核,可以对图像进行一些扭曲。这是一个前向映射,每个内核实例处理源图像中一个像素的映射/扭曲。这意味着某些内核实例(源图像中的像素)映射到同一目标像素。这意味着我无法控制写入目标图像中这些像素的值。它们只获取最后执行write命令的内核实例的值。有没有办法检查我是否写入已写入的位置(像素)?我有一个我正在扭曲的图像的深度图,理想情况下,如果更多像素映射到同一位置,我想写最前面的像素,这可能吗?
__kernel void dwarpIntThree(__read_only image2d_t src,
__read_only image2d_t dispmap,
float T,
__write_only image2d_t dst,
sampler_t sampler,
int dmin, int dmax,
sampler_t sampler_1, float posx, float posy
){
int2 srcCoords = (int2)( get_global_id(0), get_global_id(1));
if ((srcCoords.x >= get_image_width(dst)) | (srcCoords.y >= get_image_height(dst)))
return;
float true_depth = round((read_imagef(dispmap, sampler, srcCoords).x)*(dmax-dmin)+dmin);
int2 uv;
uv.x = srcCoords.x-true_depth*(posx/T);
uv.y = srcCoords.y-true_depth*(posy/T);
if ((uv.x >= get_image_width(dst)) || (uv.y >=get_image_height(dst))| (uv.x < 0) | (uv.y < 0)){
return;}
float4 srcPixel = read_imagef(src, sampler, srcCoords);
write_imagef(dst, uv, srcPixel);
}
所以我想做的是只将write_imagef src中的当前像素写入dst中的坐标uv,如果它的深度小于src中可能映射到uv的像素。但我不知道是否有可能检查,因为内核当然同时执行。
答案 0 :(得分:1)
这很难。例如,输出图像是只写的。如果您使用读写缓冲区,那么您在工作项之间存在争用。原子操作可以解决这个问题,但速度很慢。
是否可以重新设计使用反向映射?您必须更改深度图所代表的内容。
我认为使用OpenGL并利用深度缓冲更适合。