Use OpenCLExecutionContextScope

pull/3471/head
kallaballa 2 years ago
parent 94fc18e87c
commit 2ebfb0cb0d
  1. 12
      src/beauty/beauty-demo.cpp
  2. 65
      src/common/subsystems.hpp
  3. 6
      src/font/font-demo.cpp
  4. 6
      src/nanovg/nanovg-demo.cpp
  5. 20
      src/optflow/optflow-demo.cpp
  6. 8
      src/pedestrian/pedestrian-demo.cpp
  7. 8
      src/tetra/tetra-demo.cpp
  8. 6
      src/video/video-demo.cpp

@ -227,7 +227,7 @@ int main(int argc, char **argv) {
vector<FaceFeatures> featuresList;
while (true) {
bool success = va::read([&capture](CLExecContext_t& clclx, cv::UMat& videoFrame){
bool success = va::read([&capture](cv::UMat& videoFrame){
//videoFrame will be converted to BGRA and stored in the frameBuffer.
capture >> videoFrame;
});
@ -235,7 +235,7 @@ int main(int argc, char **argv) {
if(!success)
break;
cl::compute([&](CLExecContext_t& clclx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
cvtColor(frameBuffer,rgb,cv::COLOR_BGRA2RGB);
cv::resize(rgb, down, cv::Size(0, 0), SCALE, SCALE);
cvtColor(down, downGrey, cv::COLOR_BGRA2GRAY);
@ -261,7 +261,7 @@ int main(int argc, char **argv) {
draw_face_bg_mask(vg, featuresList);
});
cl::compute([&](CLExecContext_t& clclx, cv::UMat &frameBuffer) {
cl::compute([&](cv::UMat &frameBuffer) {
//Convert/Copy the mask
cvtColor(frameBuffer, faceBgMask, cv::COLOR_BGRA2BGR);
cvtColor(frameBuffer, faceBgMaskGrey, cv::COLOR_BGRA2GRAY);
@ -273,7 +273,7 @@ int main(int argc, char **argv) {
draw_face_fg_mask(vg, featuresList);
});
cl::compute([&](CLExecContext_t& clclx, cv::UMat &frameBuffer) {
cl::compute([&](cv::UMat &frameBuffer) {
//Convert/Copy the mask
cvtColor(frameBuffer, faceFgMaskGrey, cv::COLOR_BGRA2GRAY);
@ -307,7 +307,7 @@ int main(int argc, char **argv) {
cvtColor(frameOut, frameBuffer, cv::COLOR_BGR2RGBA);
});
} else {
cl::compute([&](CLExecContext_t& clclx, cv::UMat &frameBuffer) {
cl::compute([&](cv::UMat &frameBuffer) {
frameOut = cv::Scalar::all(0);
cv::resize(rgb, lhalf, cv::Size(0, 0), 0.5, 0.5);
lhalf.copyTo(frameOut(cv::Rect(0, 0, lhalf.size().width, lhalf.size().height)));
@ -316,7 +316,7 @@ int main(int argc, char **argv) {
});
}
va::write([&writer](CLExecContext_t& clclx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -29,6 +29,8 @@ using std::string;
namespace kb {
typedef cv::ocl::OpenCLExecutionContext CLExecContext_t;
typedef cv::ocl::OpenCLExecutionContextScope CLExecScope_t;
void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression) {
GLint errorCode = glGetError();
@ -111,7 +113,7 @@ void error_callback(int error, const char *description) {
fprintf(stderr, "Error: %s\n", description);
}
void init(const string &title, int major, int minor, int samples = 4, bool debug = false) {
void init(const string &title, int major, int minor, int samples, bool debug) {
assert(glfwInit() == GLFW_TRUE);
glfwSetErrorCallback(error_callback);
@ -129,8 +131,8 @@ void init(const string &title, int major, int minor, int samples = 4, bool debug
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#endif
@ -161,10 +163,6 @@ GLuint frame_buf;
GLuint render_buf;
CLExecContext_t context;
void bind() {
gl::context.bind();
}
void begin() {
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frame_buf));
GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, render_buf));
@ -185,7 +183,7 @@ void end() {
}
void render(std::function<void(int,int)> fn) {
gl::bind();
CLExecScope_t scope(gl::context);
gl::begin();
fn(app::window_width, app::window_height);
gl::end();
@ -244,10 +242,10 @@ void release_to_gl(cv::UMat& m) {
gl::end();
}
void compute(std::function<void(CLExecContext_t&, cv::UMat&)> fn) {
gl::bind();
void compute(std::function<void(cv::UMat&)> fn) {
CLExecScope_t scope(gl::context);
acquire_from_gl(cl::frameBuffer);
fn(CLExecContext_t::getCurrent(), cl::frameBuffer);
fn(cl::frameBuffer);
release_to_gl(cl::frameBuffer);
}
@ -284,30 +282,30 @@ void copy() {
va::context = CLExecContext_t::getCurrent();
}
void bind() {
va::context.bind();
}
bool read(std::function<void(CLExecContext_t&, cv::UMat&)> fn) {
va::bind();
fn(va::context, va::videoFrame);
gl::bind();
cl::acquire_from_gl(cl::frameBuffer);
if(va::videoFrame.empty())
return false;
//Color-conversion from RGB to BGRA (OpenCL)
cv::cvtColor(va::videoFrame, cl::frameBuffer, cv::COLOR_RGB2BGRA);
cv::resize(cl::frameBuffer, cl::frameBuffer, cv::Size(app::window_width, app::window_height));
cl::release_to_gl(cl::frameBuffer);
bool read(std::function<void(cv::UMat&)> fn) {
{
CLExecScope_t scope(va::context);
fn(va::videoFrame);
}
{
CLExecScope_t scope(gl::context);
cl::acquire_from_gl(cl::frameBuffer);
if(va::videoFrame.empty())
return false;
//Color-conversion from RGB to BGRA (OpenCL)
cv::cvtColor(va::videoFrame, cl::frameBuffer, cv::COLOR_RGB2BGRA);
cv::resize(cl::frameBuffer, cl::frameBuffer, cv::Size(app::window_width, app::window_height));
cl::release_to_gl(cl::frameBuffer);
}
return true;
}
void write(std::function<void(CLExecContext_t&, const cv::UMat&)> fn) {
va::bind();
void write(std::function<void(const cv::UMat&)> fn) {
CLExecScope_t scope(va::context);
//Color-conversion from BGRA to RGB. (OpenCL)
cv::cvtColor(cl::frameBuffer, va::videoFrame, cv::COLOR_BGRA2RGB);
cv::flip(va::videoFrame, va::videoFrame, 0);
fn(va::context, va::videoFrame);
fn(va::videoFrame);
}
} // namespace va
@ -386,7 +384,7 @@ void set_visible(bool v) {
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
}
void update_size(float pixelRatio = display::get_pixel_ratio(display::window)) {
void update_size(float pixelRatio = display::get_pixel_ratio()) {
gui::screen->set_size(nanogui::Vector2i(app::window_width * pixelRatio, app::window_height * pixelRatio));
display::update_size(pixelRatio);
}
@ -419,7 +417,7 @@ void end() {
}
void render(std::function<void(NVGcontext*,int,int)> fn) {
gl::bind();
CLExecScope_t scope(gl::context);
nvg::begin();
fn(gui::screen->nvg_context(), app::window_width, app::window_height);
nvg::end();
@ -441,19 +439,18 @@ void print_system_info() {
cerr << "OpenCL Platforms: " << cl::get_info() << endl;
}
void init(const string &windowTitle, unsigned int width, unsigned int height, bool offscreen = false, bool fullscreen = false, int major = 4, int minor = 6, int samples = 4, bool debugContext = false) {
void init(const string &windowTitle, unsigned int width, unsigned int height, bool offscreen = false, bool fullscreen = false, int major = 4, int minor = 6, int samples = 0, bool debugContext = false) {
using namespace kb::gui;
app::window_width = width;
app::window_height = height;
app::offscreen = offscreen;
display::init(windowTitle, samples, debugContext);
display::init(windowTitle, major, minor, samples, debugContext);
gui::init(width, height);
gl::init();
nvg::init();
}
void run(std::function<void()> fn) {
if(!app::offscreen)
gui::set_visible(true);

@ -84,7 +84,7 @@ int main(int argc, char **argv) {
}
});
cl::compute([&](kb::CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
frameBuffer.copyTo(stars);
});
@ -129,14 +129,14 @@ int main(int argc, char **argv) {
break;
}
cl::compute([&](kb::CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
//Pseudo 3D text effect.
cv::warpPerspective(frameBuffer, warped, tm, frameBuffer.size(), cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar());
//Combine layers
cv::add(stars, warped, frameBuffer);
});
va::write([&writer](kb::CLExecContext_t& clctx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -165,7 +165,7 @@ int main(int argc, char **argv) {
//opencv hue fading between 0 and 255
int cvHue = (42 + uint8_t(std::round(((1.0 - sinf(time*0.12f))+1.0f) * 128.0))) % 255;
bool success = va::read([&capture](CLExecContext_t& clctx, cv::UMat& videoFrame){
bool success = va::read([&capture](cv::UMat& videoFrame){
//videoFrame will be converted to BGRA and stored in the frameBuffer.
capture >> videoFrame;
});
@ -173,7 +173,7 @@ int main(int argc, char **argv) {
if(!success)
break;
cl::compute([&](CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
cvtColor(frameBuffer,rgb,cv::COLOR_BGRA2RGB);
//Color-conversion from RGB to HSV. (OpenCL)
cv::cvtColor(rgb, hsv, cv::COLOR_RGB2HSV_FULL);
@ -194,7 +194,7 @@ int main(int argc, char **argv) {
drawColorwheel(vg, w - 300, h - 300, 250.0f, 250.0f, nvgHue);
});
va::write([&writer](CLExecContext_t& clctx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -46,7 +46,7 @@ int glow_kernel_size = std::max(int(DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG
// Keep alpha separate for the GUI
float alpha = 0.1f;
// Red, green, blue and alpha. All from 0.0f to 1.0f
nanogui::Color EFFECT_COLOR(1.0f, 0.75f, 0.4f, 1.0f);
nanogui::Color effect_color(1.0f, 0.75f, 0.4f, 1.0f);
//display on-screen FPS
bool show_fps = true;
//Use OpenCL or not
@ -211,12 +211,12 @@ void setup_gui() {
glowKernel->set_callback([](const int& k) {
glow_kernel_size = std::max(int(k % 2 == 0 ? k + 1 : k), 1);
});
auto color = form->add_variable("Color", EFFECT_COLOR);
auto color = form->add_variable("Color", effect_color);
color->set_tooltip("The effect color");
color->set_final_callback([](const nanogui::Color &c) {
EFFECT_COLOR[0] = c[0];
EFFECT_COLOR[1] = c[1];
EFFECT_COLOR[2] = c[2];
effect_color[0] = c[0];
effect_color[1] = c[1];
effect_color[2] = c[2];
});
make_gui_variable("Alpha", alpha, 0.0f, 1.0f, true, "", "The opacity of the effect");
@ -270,7 +270,7 @@ int main(int argc, char **argv) {
vector<cv::Point2f> detectedPoints;
while (true) {
bool success = va::read([&capture](CLExecContext_t& clctx, cv::UMat& videoFrame){
bool success = va::read([&capture](cv::UMat& videoFrame){
//videoFrame will be converted to BGRA and stored in the frameBuffer.
capture >> videoFrame;
});
@ -278,7 +278,7 @@ int main(int argc, char **argv) {
if(!success)
break;
cl::compute([&](CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
cv::resize(frameBuffer, down, cv::Size(WIDTH * fg_scale, HEIGHT * fg_scale));
cv::cvtColor(frameBuffer, background, cv::COLOR_RGB2BGRA);
cv::cvtColor(down, downNextGrey, cv::COLOR_RGB2GRAY);
@ -295,7 +295,7 @@ int main(int argc, char **argv) {
//We don't want the algorithm to get out of hand when there is a scene change, so we suppress it when we detect one.
if (!detect_scene_change(downMotionMaskGrey, scene_change_thresh, scene_change_thresh_diff)) {
//Visualize the sparse optical flow using nanovg
cv::Scalar color = cv::Scalar(EFFECT_COLOR.r() * 255.0f, EFFECT_COLOR.g() * 255.0f, EFFECT_COLOR.b() * 255.0f, alpha * 255.0f);
cv::Scalar color = cv::Scalar(effect_color.r() * 255.0f, effect_color.g() * 255.0f, effect_color.b() * 255.0f, alpha * 255.0f);
visualize_sparse_optical_flow(vg, downPrevGrey, downNextGrey, detectedPoints, fg_scale, max_stroke, color, max_points, point_loss);
}
}
@ -303,12 +303,12 @@ int main(int argc, char **argv) {
downPrevGrey = downNextGrey.clone();
cl::compute([&](CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
//Put it all together (OpenCL)
composite_layers(background, foreground, frameBuffer, frameBuffer, glow_kernel_size, fg_loss);
});
va::write([&writer](CLExecContext_t& clctx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -154,7 +154,7 @@ int main(int argc, char **argv) {
vector<double> probs;
while (true) {
bool success = va::read([&capture](CLExecContext_t& clclx, cv::UMat& videoFrame){
bool success = va::read([&capture](cv::UMat& videoFrame){
//videoFrame will be converted to BGRA and stored in the frameBuffer.
capture >> videoFrame;
});
@ -162,7 +162,7 @@ int main(int argc, char **argv) {
if(!success)
break;
cl::compute([&](CLExecContext_t& clclx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
cvtColor(frameBuffer,videoFrameUp,cv::COLOR_BGRA2RGB);
cv::resize(frameBuffer, videoFrameDown, cv::Size(DOWNSIZE_WIDTH, DOWNSIZE_HEIGHT));
cv::cvtColor(videoFrameDown, videoFrameDownGrey, cv::COLOR_RGB2GRAY);
@ -198,12 +198,12 @@ int main(int argc, char **argv) {
nvgStroke(vg);
});
cl::compute([&](CLExecContext_t& clclx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
//Put it all together
composite_layers(background, foreground, frameBuffer, frameBuffer, BLUR_KERNEL_SIZE, fg_loss);
});
va::write([&writer](CLExecContext_t& clclx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -2,8 +2,8 @@
#include "../common/subsystems.hpp"
constexpr long unsigned int WIDTH = 3840;
constexpr long unsigned int HEIGHT = 2160;
constexpr long unsigned int WIDTH = 1920;
constexpr long unsigned int HEIGHT = 1080;
constexpr double FPS = 60;
constexpr bool OFFSCREEN = false;
constexpr const char* OUTPUT_FILENAME = "tetra-demo.mkv";
@ -110,12 +110,12 @@ int main(int argc, char **argv) {
});
//Aquire the frame buffer for use by OpenCL
cl::compute([](CLExecContext_t& clctx, cv::UMat &frameBuffer) {
cl::compute([](cv::UMat &frameBuffer) {
//Glow effect (OpenCL)
glow_effect(frameBuffer, frameBuffer, glow_kernel_size);
});
va::write([&writer](CLExecContext_t& clctx, const cv::UMat& videoFrame){
va::write([&writer](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

@ -121,7 +121,7 @@ int main(int argc, char **argv) {
});
while (true) {
bool success = va::read([&capture](CLExecContext_t& clctx, cv::UMat& videoFrame){
bool success = va::read([&capture](cv::UMat& videoFrame){
//videoFrame will be converted to BGRA and stored in the frameBuffer.
capture >> videoFrame;
});
@ -134,12 +134,12 @@ int main(int argc, char **argv) {
render_scene(w, h);
});
cl::compute([&](CLExecContext_t& clctx, cv::UMat& frameBuffer){
cl::compute([&](cv::UMat& frameBuffer){
//Glow effect (OpenCL)
glow_effect(frameBuffer, frameBuffer, glow_kernel_size);
});
va::write([&](CLExecContext_t& clctx, const cv::UMat& videoFrame){
va::write([&](const cv::UMat& videoFrame){
//videoFrame is the frameBuffer converted to BGR. Ready to be written.
writer << videoFrame;
});

Loading…
Cancel
Save