From d20896ced5f4204d84916d77b90d379ad912b93d Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Fri, 8 Nov 2019 12:49:30 -0800 Subject: [PATCH] Make visiond compile on pc (#874) --- selfdrive/common/visionbuf_cl.c | 89 +++++++++++++++++++ selfdrive/visiond/build_from_src.mk | 8 +- .../visiond/cameras/camera_frame_stream.h | 2 + 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 selfdrive/common/visionbuf_cl.c diff --git a/selfdrive/common/visionbuf_cl.c b/selfdrive/common/visionbuf_cl.c new file mode 100644 index 00000000000000..58c66a854d9c3c --- /dev/null +++ b/selfdrive/common/visionbuf_cl.c @@ -0,0 +1,89 @@ +#include "visionbuf.h" + +#include +#include +#include + +#ifdef __APPLE__ +#include +#else +#include +#endif + +VisionBuf visionbuf_allocate(size_t len) { + // const size_t alignment = 4096; + // void* addr = aligned_alloc(alignment, alignment * ((len - 1) / alignment + 1)); + void* addr = calloc(1, len); + + return (VisionBuf){ + .len = len, .addr = addr, .handle = 1, .fd = -1, + }; +} + +cl_mem visionbuf_to_cl(const VisionBuf* buf, cl_device_id device_id, cl_context ctx) { + // HACK because this platform is just for convenience + VisionBuf *w_buf = (VisionBuf*)buf; + cl_mem ret; + *w_buf = visionbuf_allocate_cl(buf->len, device_id, ctx, &ret); + return ret; +} + +VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context ctx, cl_mem *out_mem) { + int err; + assert(out_mem); + +#if __OPENCL_VERSION__ >= 200 + void* host_ptr = + clSVMAlloc(ctx, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, len, 0); + assert(host_ptr); +#else + void* host_ptr = calloc(1, len); + + cl_command_queue q = clCreateCommandQueue(ctx, device_id, 0, &err); + assert(err == 0); +#endif + + cl_mem mem = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, len, host_ptr, &err); + assert(err == 0); + + *out_mem = mem; + + return (VisionBuf){ + .len = len, .addr = host_ptr, .handle = 0, .fd = -1, + .device_id = device_id, .ctx = ctx, .buf_cl = mem, + +#if __OPENCL_VERSION__ < 200 + .copy_q = q, +#endif + + }; +} + +void visionbuf_sync(const VisionBuf* buf, int dir) { + int err = 0; + if (!buf->buf_cl) return; + +#if __OPENCL_VERSION__ < 200 + if (dir == VISIONBUF_SYNC_FROM_DEVICE) { + err = clEnqueueReadBuffer(buf->copy_q, buf->buf_cl, CL_FALSE, 0, buf->len, buf->addr, 0, NULL, NULL); + } else { + err = clEnqueueWriteBuffer(buf->copy_q, buf->buf_cl, CL_FALSE, 0, buf->len, buf->addr, 0, NULL, NULL); + } + assert(err == 0); + clFinish(buf->copy_q); +#endif +} + +void visionbuf_free(const VisionBuf* buf) { + if (buf->handle) { + free(buf->addr); + } else { + int err = clReleaseMemObject(buf->buf_cl); + assert(err == 0); +#if __OPENCL_VERSION__ >= 200 + clSVMFree(buf->ctx, buf->addr); +#else + free(buf->addr); +#endif + } +} diff --git a/selfdrive/visiond/build_from_src.mk b/selfdrive/visiond/build_from_src.mk index 639f0a95df7741..725b345fa42002 100644 --- a/selfdrive/visiond/build_from_src.mk +++ b/selfdrive/visiond/build_from_src.mk @@ -54,9 +54,9 @@ else OPENCL_LIBS = -lOpenCL - TF_FLAGS = -I$(EXTERNAL)/tensorflow/include - TF_LIBS = -L$(EXTERNAL)/tensorflow/lib -ltensorflow \ - -Wl,-rpath $(EXTERNAL)/tensorflow/lib + #TF_FLAGS = -I$(EXTERNAL)/tensorflow/include + #TF_LIBS = -L$(EXTERNAL)/tensorflow/lib -ltensorflow \ + # -Wl,-rpath $(EXTERNAL)/tensorflow/lib SNPE_FLAGS = -I$(PHONELIBS)/snpe/include/ SNPE_LIBS = -L$(PHONELIBS)/snpe/x86_64-linux-clang/ \ @@ -69,7 +69,7 @@ else PLATFORM_OBJS = cameras/camera_frame_stream.o \ ../common/visionbuf_cl.o \ ../common/visionimg.o \ - runners/tfmodel.o + # runners/tfmodel.o endif SSL_FLAGS = -I/usr/include/openssl/ diff --git a/selfdrive/visiond/cameras/camera_frame_stream.h b/selfdrive/visiond/cameras/camera_frame_stream.h index 6351a183d35c3c..5cb7e27c5fa72a 100644 --- a/selfdrive/visiond/cameras/camera_frame_stream.h +++ b/selfdrive/visiond/cameras/camera_frame_stream.h @@ -33,6 +33,8 @@ typedef struct CameraState { int fps; float digital_gain; + float cur_gain_frac; + mat3 transform; } CameraState;