struct CLContext

pull/2744/head
deanlee 5 years ago
parent f2e861413a
commit 882e413f22
  1. 22
      selfdrive/camerad/cameras/camera_common.cc
  2. 2
      selfdrive/camerad/cameras/camera_common.h
  3. 10
      selfdrive/camerad/cameras/camera_frame_stream.cc
  4. 10
      selfdrive/camerad/cameras/camera_frame_stream.h
  5. 14
      selfdrive/camerad/cameras/camera_qcom.cc
  6. 2
      selfdrive/camerad/cameras/camera_qcom.h
  7. 12
      selfdrive/camerad/cameras/camera_qcom2.cc
  8. 2
      selfdrive/camerad/cameras/camera_qcom2.h
  9. 10
      selfdrive/camerad/cameras/camera_webcam.cc
  10. 2
      selfdrive/camerad/cameras/camera_webcam.h
  11. 13
      selfdrive/camerad/main.cc
  12. 6
      selfdrive/camerad/transforms/rgb_to_yuv.c
  13. 13
      selfdrive/camerad/transforms/rgb_to_yuv.h
  14. 22
      selfdrive/camerad/transforms/rgb_to_yuv_test.cc
  15. 20
      selfdrive/common/clutil.cc
  16. 11
      selfdrive/common/clutil.h
  17. 12
      selfdrive/common/visionbuf.h
  18. 18
      selfdrive/common/visionbuf_cl.c
  19. 5
      selfdrive/common/visionbuf_ion.c
  20. 4
      selfdrive/common/visionimg.cc
  21. 8
      selfdrive/common/visionimg.h
  22. 10
      selfdrive/modeld/modeld.cc
  23. 15
      selfdrive/modeld/models/commonmodel.cc
  24. 11
      selfdrive/modeld/models/commonmodel.h
  25. 7
      selfdrive/modeld/models/driving.cc
  26. 3
      selfdrive/modeld/models/driving.h
  27. 6
      selfdrive/modeld/transforms/loadyuv.c
  28. 13
      selfdrive/modeld/transforms/loadyuv.h
  29. 10
      selfdrive/modeld/transforms/transform.cc
  30. 15
      selfdrive/modeld/transforms/transform.h
  31. 7
      selfdrive/modeld/visiontest.c

@ -30,7 +30,7 @@ const int env_ymin = getenv("YMIN") ? atoi(getenv("YMIN")) : 0;
const int env_ymax = getenv("YMAX") ? atoi(getenv("YMAX")) : -1;
const int env_scale = getenv("SCALE") ? atoi(getenv("SCALE")) : 1;
static cl_program build_debayer_program(cl_device_id device_id, cl_context context, const CameraInfo *ci, const CameraBuf *b) {
static cl_program build_debayer_program(CLContext *ctx, const CameraInfo *ci, const CameraBuf *b) {
char args[4096];
snprintf(args, sizeof(args),
"-cl-fast-relaxed-math -cl-denorms-are-zero "
@ -41,13 +41,13 @@ static cl_program build_debayer_program(cl_device_id device_id, cl_context conte
b->rgb_width, b->rgb_height, b->rgb_stride,
ci->bayer_flip, ci->hdr);
#ifdef QCOM2
return cl_program_from_file(context, device_id, "cameras/real_debayer.cl", args);
return cl_program_from_file(ctx, "cameras/real_debayer.cl", args);
#else
return cl_program_from_file(context, device_id, "cameras/debayer.cl", args);
return cl_program_from_file(ctx, "cameras/debayer.cl", args);
#endif
}
void CameraBuf::init(cl_device_id device_id, cl_context context, CameraState *s, int frame_cnt,
void CameraBuf::init(CLContext *ctx, CameraState *s, int frame_cnt,
const char *name, release_cb relase_callback) {
const CameraInfo *ci = &s->ci;
camera_state = s;
@ -57,7 +57,7 @@ void CameraBuf::init(cl_device_id device_id, cl_context context, CameraState *s,
camera_bufs = std::make_unique<VisionBuf[]>(frame_buf_count);
camera_bufs_metadata = std::make_unique<FrameMetadata[]>(frame_buf_count);
for (int i = 0; i < frame_buf_count; i++) {
camera_bufs[i] = visionbuf_allocate_cl(frame_size, device_id, context);
camera_bufs[i] = visionbuf_allocate_cl(ctx, frame_size);
}
rgb_width = ci->frame_width;
@ -80,7 +80,7 @@ void CameraBuf::init(cl_device_id device_id, cl_context context, CameraState *s,
}
for (int i = 0; i < UI_BUF_COUNT; i++) {
VisionImg img = visionimg_alloc_rgb24(device_id, context, rgb_width, rgb_height, &rgb_bufs[i]);
VisionImg img = visionimg_alloc_rgb24(ctx, rgb_width, rgb_height, &rgb_bufs[i]);
if (i == 0) {
rgb_stride = img.stride;
}
@ -97,25 +97,25 @@ void CameraBuf::init(cl_device_id device_id, cl_context context, CameraState *s,
yuv_buf_size = rgb_width * rgb_height * 3 / 2;
for (int i = 0; i < YUV_COUNT; i++) {
yuv_ion[i] = visionbuf_allocate_cl(yuv_buf_size, device_id, context);
yuv_ion[i] = visionbuf_allocate_cl(ctx, yuv_buf_size);
yuv_bufs[i].y = (uint8_t *)yuv_ion[i].addr;
yuv_bufs[i].u = yuv_bufs[i].y + (yuv_width * yuv_height);
yuv_bufs[i].v = yuv_bufs[i].u + (yuv_width / 2 * yuv_height / 2);
}
if (ci->bayer) {
cl_program prg_debayer = build_debayer_program(device_id, context, ci, this);
cl_program prg_debayer = build_debayer_program(ctx, ci, this);
krnl_debayer = CL_CHECK_ERR(clCreateKernel(prg_debayer, "debayer10", &err));
CL_CHECK(clReleaseProgram(prg_debayer));
}
rgb_to_yuv_init(&rgb_to_yuv_state, context, device_id, yuv_width, yuv_height, rgb_stride);
rgb_to_yuv_init(&rgb_to_yuv_state, ctx, yuv_width, yuv_height, rgb_stride);
#ifdef __APPLE__
q = CL_CHECK_ERR(clCreateCommandQueue(context, device_id, 0, &err));
q = CL_CHECK_ERR(clCreateCommandQueue(ctx->context, ctx->device_id, 0, &err));
#else
const cl_queue_properties props[] = {0}; //CL_QUEUE_PRIORITY_KHR, CL_QUEUE_PRIORITY_HIGH_KHR, 0};
q = CL_CHECK_ERR(clCreateCommandQueueWithProperties(context, device_id, props, &err));
q = CL_CHECK_ERR(clCreateCommandQueueWithProperties(ctx->context, ctx->device_id, props, &err));
#endif
}

@ -122,7 +122,7 @@ public:
CameraBuf() = default;
~CameraBuf();
void init(cl_device_id device_id, cl_context context, CameraState *s, int frame_cnt,
void init(CLContext *ctx, CameraState *s, int frame_cnt,
const char *name = "frame", release_cb relase_callback = nullptr);
bool acquire();
void release();

@ -30,13 +30,13 @@ void camera_close(CameraState *s) {
s->buf.stop();
}
void camera_init(CameraState *s, int camera_id, unsigned int fps, cl_device_id device_id, cl_context ctx) {
void camera_init(CameraState *s, int camera_id, unsigned int fps, CLContext *ctx) {
assert(camera_id < ARRAYSIZE(cameras_supported));
s->ci = cameras_supported[camera_id];
assert(s->ci.frame_width != 0);
s->fps = fps;
s->buf.init(device_id, ctx, s, FRAME_BUF_COUNT, "camera");
s->buf.init(ctx, s, FRAME_BUF_COUNT, "camera");
}
void run_frame_stream(MultiCameraState *s) {
@ -87,15 +87,15 @@ CameraInfo cameras_supported[CAMERA_ID_MAX] = {
},
};
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
camera_init(&s->rear, CAMERA_ID_IMX298, 20, device_id, ctx);
void cameras_init(MultiCameraState *s, CLContext *ctx) {
camera_init(&s->rear, CAMERA_ID_IMX298, 20, ctx);
s->rear.transform = (mat3){{
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
}};
camera_init(&s->front, CAMERA_ID_OV8865, 10, device_id, ctx);
camera_init(&s->front, CAMERA_ID_OV8865, 10, ctx);
s->front.transform = (mat3){{
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,

@ -2,13 +2,7 @@
#include <stdbool.h>
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#include "camera_common.h"
#define FRAME_BUF_COUNT 16
@ -36,7 +30,7 @@ typedef struct MultiCameraState {
PubMaster *pm;
} MultiCameraState;
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx);
void cameras_init(MultiCameraState *s, CLContext *ctx);
void cameras_open(MultiCameraState *s);
void cameras_run(MultiCameraState *s);
void cameras_close(MultiCameraState *s);

@ -100,7 +100,7 @@ static void camera_release_buffer(void* cookie, int buf_idx) {
static void camera_init(CameraState *s, int camera_id, int camera_num,
uint32_t pixel_clock, uint32_t line_length_pclk,
unsigned int max_gain, unsigned int fps, cl_device_id device_id, cl_context ctx) {
unsigned int max_gain, unsigned int fps, CLContext *ctx) {
s->camera_num = camera_num;
s->camera_id = camera_id;
@ -115,7 +115,7 @@ static void camera_init(CameraState *s, int camera_id, int camera_num,
s->self_recover = 0;
s->buf.init(device_id, ctx, s, FRAME_BUF_COUNT, "frame", camera_release_buffer);
s->buf.init(ctx, s, FRAME_BUF_COUNT, "frame", camera_release_buffer);
pthread_mutex_init(&s->frame_info_lock, NULL);
}
@ -251,7 +251,7 @@ cl_program build_conv_program(cl_device_id device_id, cl_context context, int im
return cl_program_from_file(context, device_id, "imgproc/conv.cl", args);
}
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
void cameras_init(MultiCameraState *s, CLContext *ctx) {
char project_name[1024] = {0};
property_get("ro.boot.project_name", project_name, "");
@ -295,23 +295,23 @@ void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
#else
/*fps*/ 20,
#endif
device_id, ctx);
ctx);
s->rear.apply_exposure = imx298_apply_exposure;
if (s->device == DEVICE_OP3T) {
camera_init(&s->front, CAMERA_ID_S5K3P8SP, 1,
/*pixel_clock=*/560000000, /*line_length_pclk=*/5120,
/*max_gain=*/510, 10, device_id, ctx);
/*max_gain=*/510, 10, ctx);
s->front.apply_exposure = imx179_s5k3p8sp_apply_exposure;
} else if (s->device == DEVICE_LP3) {
camera_init(&s->front, CAMERA_ID_OV8865, 1,
/*pixel_clock=*/72000000, /*line_length_pclk=*/1602,
/*max_gain=*/510, 10, device_id, ctx);
/*max_gain=*/510, 10, ctx);
s->front.apply_exposure = ov8865_apply_exposure;
} else {
camera_init(&s->front, CAMERA_ID_IMX179, 1,
/*pixel_clock=*/251200000, /*line_length_pclk=*/3440,
/*max_gain=*/224, 20, device_id, ctx);
/*max_gain=*/224, 20, ctx);
s->front.apply_exposure = imx179_s5k3p8sp_apply_exposure;
}

@ -148,7 +148,7 @@ typedef struct MultiCameraState {
} MultiCameraState;
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx);
void cameras_init(MultiCameraState *s, CLContext *ctx);
void cameras_open(MultiCameraState *s);
void cameras_run(MultiCameraState *s);
void cameras_close(MultiCameraState *s);

@ -555,7 +555,7 @@ void enqueue_req_multi(struct CameraState *s, int start, int n, bool dp) {
// ******************* camera *******************
static void camera_init(CameraState *s, int camera_id, int camera_num, unsigned int fps, cl_device_id device_id, cl_context ctx) {
static void camera_init(CameraState *s, int camera_id, int camera_num, unsigned int fps, CLContext *ctx) {
LOGD("camera init %d", camera_num);
assert(camera_id < ARRAYSIZE(cameras_supported));
@ -586,7 +586,7 @@ static void camera_init(CameraState *s, int camera_id, int camera_num, unsigned
s->debayer_cl_localWorkSize[0] = DEBAYER_LOCAL_WORKSIZE;
s->debayer_cl_localWorkSize[1] = DEBAYER_LOCAL_WORKSIZE;
s->buf.init(device_id, ctx, s, FRAME_BUF_COUNT, "frame");
s->buf.init(ctx, s, FRAME_BUF_COUNT, "frame");
}
// TODO: refactor this to somewhere nicer, perhaps use in camera_qcom as well
@ -809,12 +809,12 @@ static void camera_open(CameraState *s) {
enqueue_req_multi(s, 1, FRAME_BUF_COUNT, 0);
}
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
camera_init(&s->rear, CAMERA_ID_AR0231, 1, 20, device_id, ctx); // swap left/right
void cameras_init(MultiCameraState *s, CLContext *ctx) {
camera_init(&s->rear, CAMERA_ID_AR0231, 1, 20, ctx); // swap left/right
printf("rear initted \n");
camera_init(&s->wide, CAMERA_ID_AR0231, 0, 20, device_id, ctx);
camera_init(&s->wide, CAMERA_ID_AR0231, 0, 20, ctx);
printf("wide initted \n");
camera_init(&s->front, CAMERA_ID_AR0231, 2, 20, device_id, ctx);
camera_init(&s->front, CAMERA_ID_AR0231, 2, 20, ctx);
printf("front initted \n");
s->sm = new SubMaster({"driverState"});

@ -88,7 +88,7 @@ typedef struct MultiCameraState {
PubMaster *pm;
} MultiCameraState;
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx);
void cameras_init(MultiCameraState *s, CLContext *ctx);
void cameras_open(MultiCameraState *s);
void cameras_run(MultiCameraState *s);
void camera_autoexposure(CameraState *s, float grey_frac);

@ -35,14 +35,14 @@ void camera_close(CameraState *s) {
s->buf.stop();
}
void camera_init(CameraState *s, int camera_id, unsigned int fps, cl_device_id device_id, cl_context ctx) {
void camera_init(CameraState *s, int camera_id, unsigned int fps, CLContext *ctx) {
assert(camera_id < ARRAYSIZE(cameras_supported));
s->ci = cameras_supported[camera_id];
assert(s->ci.frame_width != 0);
s->fps = fps;
s->buf.init(device_id, ctx, s, FRAME_BUF_COUNT, "frame");
s->buf.init(ctx, s, FRAME_BUF_COUNT, "frame");
}
static void* rear_thread(void *arg) {
@ -217,16 +217,16 @@ CameraInfo cameras_supported[CAMERA_ID_MAX] = {
},
};
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
void cameras_init(MultiCameraState *s, CLContext *ctx) {
camera_init(&s->rear, CAMERA_ID_LGC920, 20, device_id, ctx);
camera_init(&s->rear, CAMERA_ID_LGC920, 20, ctx);
s->rear.transform = (mat3){{
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
}};
camera_init(&s->front, CAMERA_ID_LGC615, 10, device_id, ctx);
camera_init(&s->front, CAMERA_ID_LGC615, 10, ctx);
s->front.transform = (mat3){{
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,

@ -29,7 +29,7 @@ typedef struct MultiCameraState {
PubMaster *pm;
} MultiCameraState;
void cameras_init(MultiCameraState *s, cl_device_id device_id, cl_context ctx);
void cameras_init(MultiCameraState *s, CLContext *ctx);
void cameras_open(MultiCameraState *s);
void cameras_run(MultiCameraState *s);
void cameras_close(MultiCameraState *s);

@ -301,11 +301,11 @@ void* visionserver_thread(void* arg) {
return NULL;
}
void party(cl_device_id device_id, cl_context context) {
void party(CLContext *ctx) {
VisionState state = {};
VisionState *s = &state;
cameras_init(&s->cameras, device_id, context);
cameras_init(&s->cameras, ctx);
cameras_open(&s->cameras);
std::thread server_thread(visionserver_thread, s);
@ -330,10 +330,7 @@ int main(int argc, char *argv[]) {
signal(SIGINT, (sighandler_t)set_do_exit);
signal(SIGTERM, (sighandler_t)set_do_exit);
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
cl_context context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));
party(device_id, context);
CL_CHECK(clReleaseContext(context));
CLContext ctx = cl_init_context(CL_DEVICE_TYPE_DEFAULT);
party(&ctx);
cl_free_context(&ctx);
}

@ -1,11 +1,9 @@
#include <string.h>
#include <assert.h>
#include "clutil.h"
#include "rgb_to_yuv.h"
void rgb_to_yuv_init(RGBToYUVState* s, cl_context ctx, cl_device_id device_id, int width, int height, int rgb_stride) {
void rgb_to_yuv_init(RGBToYUVState* s, CLContext *ctx, int width, int height, int rgb_stride) {
memset(s, 0, sizeof(*s));
printf("width %d, height %d, rgb_stride %d\n", width, height, rgb_stride);
assert(width % 2 == 0);
@ -20,7 +18,7 @@ void rgb_to_yuv_init(RGBToYUVState* s, cl_context ctx, cl_device_id device_id, i
#endif
"-DWIDTH=%d -DHEIGHT=%d -DUV_WIDTH=%d -DUV_HEIGHT=%d -DRGB_STRIDE=%d -DRGB_SIZE=%d",
width, height, width/ 2, height / 2, rgb_stride, width * height);
cl_program prg = cl_program_from_file(ctx, device_id, "transforms/rgb_to_yuv.cl", args);
cl_program prg = cl_program_from_file(ctx, "transforms/rgb_to_yuv.cl", args);
s->rgb_to_yuv_krnl = CL_CHECK_ERR(clCreateKernel(prg, "rgb_to_yuv", &err));
// done with this

@ -1,14 +1,9 @@
#ifndef RGB_TO_YUV_H
#define RGB_TO_YUV_H
#pragma once
#include <inttypes.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#ifdef __cplusplus
extern "C" {
@ -19,7 +14,7 @@ typedef struct {
cl_kernel rgb_to_yuv_krnl;
} RGBToYUVState;
void rgb_to_yuv_init(RGBToYUVState* s, cl_context ctx, cl_device_id device_id, int width, int height, int rgb_stride);
void rgb_to_yuv_init(RGBToYUVState* s, CLContext *ctx, int width, int height, int rgb_stride);
void rgb_to_yuv_destroy(RGBToYUVState* s);
@ -28,5 +23,3 @@ void rgb_to_yuv_queue(RGBToYUVState* s, cl_command_queue q, cl_mem rgb_cl, cl_me
#ifdef __cplusplus
}
#endif
#endif // RGB_TO_YUV_H

@ -28,8 +28,6 @@
#include <libyuv.h>
#include <CL/cl.h>
#include "clutil.h"
#include "rgb_to_yuv.h"
@ -40,12 +38,6 @@ static inline double millis_since_boot() {
return t.tv_sec * 1000.0 + t.tv_nsec * 1e-6;
}
void cl_init(cl_device_id &device_id, cl_context &context) {
device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));
}
bool compare_results(uint8_t *a, uint8_t *b, int len, int stride, int width, int height, uint8_t *rgb) {
int min_diff = 0., max_diff = 0., max_e = 0.;
int e1 = 0, e0 = 0;
@ -101,13 +93,11 @@ bool compare_results(uint8_t *a, uint8_t *b, int len, int stride, int width, int
int main(int argc, char** argv) {
srand(1337);
cl_device_id device_id;
cl_context context;
cl_init(device_id, context) ;
CLContext ctx = cl_init_context(CL_DEVICE_TYPE_DEFAULT);
int err;
const cl_queue_properties props[] = {0}; //CL_QUEUE_PRIORITY_KHR, CL_QUEUE_PRIORITY_HIGH_KHR, 0};
cl_command_queue q = clCreateCommandQueueWithProperties(context, device_id, props, &err);
cl_command_queue q = clCreateCommandQueueWithProperties(ctx.context, ctx.device_id, props, &err);
if(err != 0) {
std::cout << "clCreateCommandQueueWithProperties error: " << err << std::endl;
}
@ -132,16 +122,16 @@ int main(int argc, char** argv) {
RGBToYUVState rgb_to_yuv_state;
rgb_to_yuv_init(&rgb_to_yuv_state, context, device_id, width, height, width * 3);
rgb_to_yuv_init(&rgb_to_yuv_state, &ctx, width, height, width * 3);
int frame_yuv_buf_size = width * height * 3 / 2;
cl_mem yuv_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE, frame_yuv_buf_size, (void*)NULL, &err));
cl_mem yuv_cl = CL_CHECK_ERR(clCreateBuffer(ctx.context, CL_MEM_READ_WRITE, frame_yuv_buf_size, (void*)NULL, &err));
uint8_t *frame_yuv_buf = new uint8_t[frame_yuv_buf_size];
uint8_t *frame_yuv_ptr_y = frame_yuv_buf;
uint8_t *frame_yuv_ptr_u = frame_yuv_buf + (width * height);
uint8_t *frame_yuv_ptr_v = frame_yuv_ptr_u + ((width/2) * (height/2));
cl_mem rgb_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE, width * height * 3, (void*)NULL, &err));
cl_mem rgb_cl = CL_CHECK_ERR(clCreateBuffer(ctx.context, CL_MEM_READ_WRITE, width * height * 3, (void*)NULL, &err));
int mismatched = 0;
int counter = 0;
srand (time(NULL));
@ -182,7 +172,7 @@ int main(int argc, char** argv) {
delete[] frame_yuv_buf;
rgb_to_yuv_destroy(&rgb_to_yuv_state);
clReleaseContext(context);
cl_free_context(&ctx)
delete[] rgb_frame;
if (mismatched == 0)

@ -103,13 +103,25 @@ cl_device_id cl_get_device_id(cl_device_type device_type) {
return nullptr;
}
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args) {
CLContext cl_init_context(cl_device_type device_type) {
cl_device_id device_id = cl_get_device_id(device_type);
cl_context ctx = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));
return CLContext{.device_id = device_id, .context = ctx};
}
void cl_free_context(CLContext *ctx) {
clReleaseContext(ctx->context);
ctx->context = nullptr;
ctx->device_id = nullptr;
}
cl_program cl_program_from_file(CLContext *ctx, const char* path, const char* args) {
char* src = (char*)read_file(path, nullptr);
assert(src != nullptr);
cl_program prg = CL_CHECK_ERR(clCreateProgramWithSource(ctx, 1, (const char**)&src, NULL, &err));
cl_program prg = CL_CHECK_ERR(clCreateProgramWithSource(ctx->context, 1, (const char**)&src, NULL, &err));
free(src);
if (int err = clBuildProgram(prg, 1, &device_id, args, NULL, NULL); err != 0) {
cl_print_build_errors(prg, device_id);
if (int err = clBuildProgram(prg, 1, &ctx->device_id, args, NULL, NULL); err != 0) {
cl_print_build_errors(prg, ctx->device_id);
assert(0);
}
return prg;

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
@ -27,8 +27,15 @@ extern "C" {
_ret; \
})
typedef struct CLContext{
cl_context context;
cl_device_id device_id;
}CLContext;
CLContext cl_init_context(cl_device_type device_type);
cl_device_id cl_get_device_id(cl_device_type device_type);
cl_program cl_program_from_file(cl_context ctx, cl_device_id device_id, const char* path, const char* args);
void cl_free_context(CLContext *ctx);
cl_program cl_program_from_file(CLContext *ctx, const char* path, const char* args);
const char* cl_get_error_string(int err);
#ifdef __cplusplus

@ -1,11 +1,6 @@
#pragma once
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#ifdef __cplusplus
extern "C" {
@ -18,8 +13,7 @@ typedef struct VisionBuf {
int handle;
int fd;
cl_context ctx;
cl_device_id device_id;
CLContext *ctx;
cl_mem buf_cl;
cl_command_queue copy_q;
} VisionBuf;
@ -28,7 +22,7 @@ typedef struct VisionBuf {
#define VISIONBUF_SYNC_TO_DEVICE 1
VisionBuf visionbuf_allocate(size_t len);
VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context ctx);
VisionBuf visionbuf_allocate_cl(CLContext *ctx, size_t len);
void visionbuf_sync(const VisionBuf* buf, int dir);
void visionbuf_free(const VisionBuf* buf);

@ -7,14 +7,6 @@
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "common/clutil.h"
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
int offset = 0;
void *malloc_with_fd(size_t len, int *fd) {
@ -46,7 +38,7 @@ VisionBuf visionbuf_allocate(size_t len) {
};
}
VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context ctx) {
VisionBuf visionbuf_allocate_cl(CLContext *ctx, size_t len) {
#if __OPENCL_VERSION__ >= 200
void* host_ptr =
clSVMAlloc(ctx, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, len, 0);
@ -55,14 +47,14 @@ VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context c
int fd;
void* host_ptr = malloc_with_fd(len, &fd);
cl_command_queue q = CL_CHECK_ERR(clCreateCommandQueue(ctx, device_id, 0, &err));
cl_command_queue q = CL_CHECK_ERR(clCreateCommandQueue(ctx->context, ctx->device_id, 0, &err));
#endif
cl_mem mem = CL_CHECK_ERR(clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, len, host_ptr, &err));
cl_mem mem = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, len, host_ptr, &err));
return (VisionBuf){
.len = len, .addr = host_ptr, .handle = 0, .fd = fd,
.device_id = device_id, .ctx = ctx, .buf_cl = mem,
.ctx = ctx, .buf_cl = mem,
#if __OPENCL_VERSION__ < 200
.copy_q = q,
@ -91,7 +83,7 @@ void visionbuf_free(const VisionBuf* buf) {
} else {
CL_CHECK(clReleaseMemObject(buf->buf_cl));
#if __OPENCL_VERSION__ >= 200
clSVMFree(buf->ctx, buf->addr);
clSVMFree(buf->ctx->context, buf->addr);
#else
CL_CHECK(clReleaseCommandQueue(buf->copy_q));
munmap(buf->addr, buf->len);

@ -10,7 +10,6 @@
#include <unistd.h>
#include <linux/ion.h>
#include <CL/cl_ext.h>
#include "common/clutil.h"
#include <msm_ion.h>
#include "visionbuf.h"
@ -71,7 +70,7 @@ VisionBuf visionbuf_allocate(size_t len) {
};
}
VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context ctx) {
VisionBuf visionbuf_allocate_cl(CLContext *ctx, size_t len) {
VisionBuf buf = visionbuf_allocate(len);
assert(((uintptr_t)buf.addr % DEVICE_PAGE_SIZE_CL) == 0);
@ -82,7 +81,7 @@ VisionBuf visionbuf_allocate_cl(size_t len, cl_device_id device_id, cl_context c
ion_cl.ion_filedesc = buf.fd;
ion_cl.ion_hostptr = buf.addr;
buf.buf_cl = CL_CHECK_ERR(clCreateBuffer(ctx,
buf.buf_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context,
CL_MEM_USE_HOST_PTR | CL_MEM_EXT_HOST_PTR_QCOM,
buf.len, &ion_cl, &err));
return buf;

@ -44,7 +44,7 @@ void visionimg_compute_aligned_width_and_height(int width, int height, int *alig
#endif
}
VisionImg visionimg_alloc_rgb24(cl_device_id device_id, cl_context ctx, int width, int height, VisionBuf *out_buf) {
VisionImg visionimg_alloc_rgb24(CLContext *ctx, int width, int height, VisionBuf *out_buf) {
assert(out_buf != nullptr);
int aligned_w = 0, aligned_h = 0;
visionimg_compute_aligned_width_and_height(width, height, &aligned_w, &aligned_h);
@ -52,7 +52,7 @@ VisionImg visionimg_alloc_rgb24(cl_device_id device_id, cl_context ctx, int widt
int stride = aligned_w * 3;
size_t size = (size_t) aligned_w * aligned_h * 3;
*out_buf = visionbuf_allocate_cl(size, device_id, ctx);
*out_buf = visionbuf_allocate_cl(ctx, size);
return (VisionImg){
.fd = out_buf->fd,

@ -1,8 +1,8 @@
#ifndef VISIONIMG_H
#define VISIONIMG_H
#pragma once
#include "common/visionbuf.h"
#include "common/glutil.h"
#include "common/clutil.h"
#ifdef QCOM
#include <EGL/egl.h>
@ -28,7 +28,7 @@ typedef struct VisionImg {
} VisionImg;
void visionimg_compute_aligned_width_and_height(int width, int height, int *aligned_w, int *aligned_h);
VisionImg visionimg_alloc_rgb24(cl_device_id device_id, cl_context ctx, int width, int height, VisionBuf *out_buf);
VisionImg visionimg_alloc_rgb24(CLContext *ctx, int width, int height, VisionBuf *out_buf);
EGLClientBuffer visionimg_to_egl(const VisionImg *img, void **pph);
GLuint visionimg_to_gl(const VisionImg *img, EGLImageKHR *pkhr, void **pph);
@ -37,5 +37,3 @@ void visionimg_destroy_gl(EGLImageKHR khr, void *ph);
#ifdef __cplusplus
} // extern "C"
#endif
#endif

@ -115,12 +115,10 @@ int main(int argc, char **argv) {
SubMaster sm({"pathPlan", "frame"});
// cl init
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
cl_context context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));
CLContext ctx = cl_init_context(CL_DEVICE_TYPE_DEFAULT);
// init the models
ModelState model;
model_init(&model, device_id, context);
model_init(&model, &ctx);
LOGW("models loaded, modeld starting");
// loop
@ -142,7 +140,7 @@ int main(int argc, char **argv) {
float frames_dropped = 0;
// one frame in memory
VisionBuf yuv_ion = visionbuf_allocate_cl(buf_info.buf_len, device_id, context);
VisionBuf yuv_ion = visionbuf_allocate_cl(&ctx, buf_info.buf_len);
uint32_t frame_id = 0, last_vipc_frame_id = 0;
double last = 0;
@ -214,7 +212,7 @@ int main(int argc, char **argv) {
LOG("joining live_thread");
err = pthread_join(live_thread_handle, NULL);
assert(err == 0);
CL_CHECK(clReleaseContext(context));
cl_free_context(&ctx);
pthread_mutex_destroy(&transform_lock);
return 0;
}

@ -5,22 +5,21 @@
#include "common/mat.h"
#include "common/timing.h"
void frame_init(ModelFrame* frame, int width, int height,
cl_device_id device_id, cl_context context) {
transform_init(&frame->transform, context, device_id);
void frame_init(ModelFrame* frame, CLContext *ctx, int width, int height) {
transform_init(&frame->transform, ctx);
frame->transformed_width = width;
frame->transformed_height = height;
frame->transformed_y_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE,
frame->transformed_y_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE,
(size_t)frame->transformed_width*frame->transformed_height, NULL, &err));
frame->transformed_u_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE,
frame->transformed_u_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE,
(size_t)(frame->transformed_width/2)*(frame->transformed_height/2), NULL, &err));
frame->transformed_v_cl = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE,
frame->transformed_v_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE,
(size_t)(frame->transformed_width/2)*(frame->transformed_height/2), NULL, &err));
frame->net_input_size = ((width*height*3)/2)*sizeof(float);
frame->net_input = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_WRITE,
frame->net_input = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE,
frame->net_input_size, (void*)NULL, &err));
loadyuv_init(&frame->loadyuv, context, device_id, frame->transformed_width, frame->transformed_height);
loadyuv_init(&frame->loadyuv, ctx, frame->transformed_width, frame->transformed_height);
}
float *frame_prepare(ModelFrame* frame, cl_command_queue q,

@ -1,11 +1,5 @@
#pragma once
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#include <float.h>
#include <stdlib.h>
#include "common/mat.h"
@ -27,8 +21,7 @@ typedef struct ModelFrame {
size_t net_input_size;
} ModelFrame;
void frame_init(ModelFrame* frame, int width, int height,
cl_device_id device_id, cl_context context);
void frame_init(ModelFrame* frame, CLContext *ctx, int width, int height);
float *frame_prepare(ModelFrame* frame, cl_command_queue q,
cl_mem yuv_cl, int width, int height,
const mat3 &transform);

@ -8,7 +8,6 @@
#include "common/timing.h"
#include "common/params.h"
#include "driving.h"
#include "clutil.h"
constexpr int MODEL_WIDTH = 512;
constexpr int MODEL_HEIGHT = 256;
@ -52,8 +51,8 @@ Eigen::Matrix<float, MODEL_PATH_DISTANCE, POLYFIT_DEGREE - 1> vander;
float X_IDXS[TRAJECTORY_SIZE];
float T_IDXS[TRAJECTORY_SIZE];
void model_init(ModelState* s, cl_device_id device_id, cl_context context) {
frame_init(&s->frame, MODEL_WIDTH, MODEL_HEIGHT, device_id, context);
void model_init(ModelState* s, CLContext *ctx) {
frame_init(&s->frame, ctx, MODEL_WIDTH, MODEL_HEIGHT);
s->input_frames = std::make_unique<float[]>(MODEL_FRAME_SIZE * 2);
constexpr int output_size = OUTPUT_SIZE + TEMPORAL_SIZE;
@ -83,7 +82,7 @@ void model_init(ModelState* s, cl_device_id device_id, cl_context context) {
}
}
s->q = CL_CHECK_ERR(clCreateCommandQueue(context, device_id, 0, &err));
s->q = CL_CHECK_ERR(clCreateCommandQueue(ctx->context, ctx->device_id, 0, &err));
}
ModelDataRaw model_eval_frame(ModelState* s, cl_mem yuv_cl, int width, int height,

@ -7,6 +7,7 @@
#include "common/mat.h"
#include "common/util.h"
#include "clutil.h"
#include "common/modeldata.h"
#include "commonmodel.h"
@ -47,7 +48,7 @@ typedef struct ModelState {
#endif
} ModelState;
void model_init(ModelState* s, cl_device_id device_id, cl_context context);
void model_init(ModelState* s, CLContext *ctx);
ModelDataRaw model_eval_frame(ModelState* s, cl_mem yuv_cl, int width, int height,
const mat3 &transform, float *desire_in);
void model_free(ModelState* s);

@ -1,11 +1,9 @@
#include <string.h>
#include <assert.h>
#include "clutil.h"
#include "loadyuv.h"
void loadyuv_init(LoadYUVState* s, cl_context ctx, cl_device_id device_id, int width, int height) {
void loadyuv_init(LoadYUVState* s, CLContext *ctx, int width, int height) {
memset(s, 0, sizeof(*s));
s->width = width;
@ -16,7 +14,7 @@ void loadyuv_init(LoadYUVState* s, cl_context ctx, cl_device_id device_id, int w
"-cl-fast-relaxed-math -cl-denorms-are-zero "
"-DTRANSFORMED_WIDTH=%d -DTRANSFORMED_HEIGHT=%d",
width, height);
cl_program prg = cl_program_from_file(ctx, device_id, "transforms/loadyuv.cl", args);
cl_program prg = cl_program_from_file(ctx, "transforms/loadyuv.cl", args);
s->loadys_krnl = CL_CHECK_ERR(clCreateKernel(prg, "loadys", &err));
s->loaduv_krnl = CL_CHECK_ERR(clCreateKernel(prg, "loaduv", &err));

@ -1,14 +1,9 @@
#ifndef LOADYUV_H
#define LOADYUV_H
#pragma once
#include <inttypes.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#ifdef __cplusplus
extern "C" {
@ -19,7 +14,7 @@ typedef struct {
cl_kernel loadys_krnl, loaduv_krnl;
} LoadYUVState;
void loadyuv_init(LoadYUVState* s, cl_context ctx, cl_device_id device_id, int width, int height);
void loadyuv_init(LoadYUVState* s, CLContext *ctx, int width, int height);
void loadyuv_destroy(LoadYUVState* s);
@ -30,5 +25,3 @@ void loadyuv_queue(LoadYUVState* s, cl_command_queue q,
#ifdef __cplusplus
}
#endif
#endif // LOADYUV_H

@ -1,20 +1,18 @@
#include <string.h>
#include <assert.h>
#include "clutil.h"
#include "transform.h"
void transform_init(Transform* s, cl_context ctx, cl_device_id device_id) {
void transform_init(Transform* s, CLContext *ctx) {
memset(s, 0, sizeof(*s));
cl_program prg = cl_program_from_file(ctx, device_id, "transforms/transform.cl", "");
cl_program prg = cl_program_from_file(ctx, "transforms/transform.cl", "");
s->krnl = CL_CHECK_ERR(clCreateKernel(prg, "warpPerspective", &err));
// done with this
CL_CHECK(clReleaseProgram(prg));
s->m_y_cl = CL_CHECK_ERR(clCreateBuffer(ctx, CL_MEM_READ_WRITE, 3*3*sizeof(float), NULL, &err));
s->m_uv_cl = CL_CHECK_ERR(clCreateBuffer(ctx, CL_MEM_READ_WRITE, 3*3*sizeof(float), NULL, &err));
s->m_y_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE, 3*3*sizeof(float), NULL, &err));
s->m_uv_cl = CL_CHECK_ERR(clCreateBuffer(ctx->context, CL_MEM_READ_WRITE, 3*3*sizeof(float), NULL, &err));
}
void transform_destroy(Transform* s) {

@ -1,16 +1,9 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H
#pragma once
#include <inttypes.h>
#include <stdbool.h>
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#include "common/mat.h"
#ifdef __cplusplus
@ -22,7 +15,7 @@ typedef struct {
cl_mem m_y_cl, m_uv_cl;
} Transform;
void transform_init(Transform* s, cl_context ctx, cl_device_id device_id);
void transform_init(Transform* s, CLContext *ctx);
void transform_destroy(Transform* transform);
@ -35,5 +28,3 @@ void transform_queue(Transform* s, cl_command_queue q,
#ifdef __cplusplus
}
#endif
#endif // TRANSFORM_H

@ -2,13 +2,6 @@
#include <stdio.h>
#include <string.h>
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "clutil.h"
#include "transforms/transform.h"

Loading…
Cancel
Save