Ниже представлена моя реализация камеры как на Java, так и на C. Я попытался сопоставить значения, чтобы избежать проблем с выравниванием памяти:
Код: Выделить всё
public class Camera {
public float aspect_ratio;
public int image_width;
public int image_height;
public float view_fov;
}
typedef struct {
float aspect_ratio;
int image_width;
int image_height;
float view_fov;
} Camera;
Код: Выделить всё
public ByteBuffer toByteBuffer(int bufferSize) {
ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
buffer.putFloat(aspect_ratio);
buffer.putInt(image_width);
buffer.putInt(image_height);
buffer.putFloat(view_fov);
buffer.flip();
return buffer;
}
Код: Выделить всё
int cameraBufferSize = camera.getCameraSize();
ByteBuffer cameraBuffer = camera.toByteBuffer(cameraBufferSize);
cl_mem cameraMem = clCreateBuffer(hostManager.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, cameraBuffer.capacity(), Pointer.to(cameraBuffer), null);
long[] globalWorkSize = new long[2];
globalWorkSize[0] = width;
globalWorkSize[1] = height;
clSetKernelArg(hostManager.getKernel(), 0, Sizeof.cl_mem, Pointer.to(outputImageMem));
cL.clSetKernelArg(hostManager.getKernel(), 3, Sizeof.cl_mem, Pointer.to(cameraMem));
clEnqueueNDRangeKernel(hostManager.getCommandQueue(), hostManager.getKernel(), 2, null, globalWorkSize, null, 0, null, null);
Код: Выделить всё
__kernel void render_kernel(__global uint* output, __global Camera* camera) {
printf("aspect ratio: %f\n", camera->aspect_ratio);
printf("image width: %d\n", camera->image_width);
printf("image height: %d\n", camera->image_height);
printf("view_fov: %f\n", camera->view_fov);
}
Итак, , для камеры, инициализированной со следующими атрибутами:
Код: Выделить всё
Camera camera = new Camera(16.0 / 9.0, 800, 450, 90.0);Код: Выделить всё
aspect ratio: 0.000273 //expected aspect ratio -> 1.7777
image width: 327680 //expected width -> 800
image height: -805175296 //expected height -> 450
view_fov: 0.000000 //expected view_fov -> 90
Подробнее здесь: https://stackoverflow.com/questions/790 ... ncl-kernel
Мобильная версия