Skip to content

Commit

Permalink
Make visiond compile on pc (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm authored Nov 8, 2019
1 parent a649732 commit d20896c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
89 changes: 89 additions & 0 deletions selfdrive/common/visionbuf_cl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "visionbuf.h"

#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#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
}
}
8 changes: 4 additions & 4 deletions selfdrive/visiond/build_from_src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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/ \
Expand All @@ -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/
Expand Down
2 changes: 2 additions & 0 deletions selfdrive/visiond/cameras/camera_frame_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ typedef struct CameraState {
int fps;
float digital_gain;

float cur_gain_frac;

mat3 transform;
} CameraState;

Expand Down

0 comments on commit d20896c

Please sign in to comment.