Skip to content

Commit

Permalink
Replace PPL (Windows only) by OpenMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Mar 25, 2024
1 parent f67adba commit a71c804
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
8 changes: 6 additions & 2 deletions addons/gdcef/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
# Godot .gdns and .gdnlib files inside the libs folders in GDCEF_EXAMPLES_PATH the
# demos folder.
CEF_ARTIFACTS_FOLDER_NAME = "cef_artifacts"
# Use OpenMP for using CPU parallelim (i.e. for copying textures)
CEF_USE_CPU_PARALLELISM = "yes" # or "no"

###############################################################################
### Project internal paths local from this script. Do not change them!
Expand Down Expand Up @@ -401,13 +403,15 @@ def gdnative_scons_cmd(plateform):
"cef_artifacts_folder=\\\"" + CEF_ARTIFACTS_FOLDER_NAME + "\\\"",
"build_path=" + CEF_ARTIFACTS_BUILD_PATH,
"target=" + MODULE_TARGET, "--jobs=" + NPROC,
"arch=" + ARCHI, "platform=" + plateform], check=True)
"arch=" + ARCHI, "platform=" + plateform,
"cpu_parallelism=" + CEF_USE_CPU_PARALLELISM], check=True)
else:
run(SCONS + ["api_path=" + GODOT_CPP_API_PATH,
"cef_artifacts_folder=\\\"" + CEF_ARTIFACTS_FOLDER_NAME + "\\\"",
"build_path=" + CEF_ARTIFACTS_BUILD_PATH,
"target=" + MODULE_TARGET, "--jobs=" + NPROC,
"platform=" + plateform], check=True)
"platform=" + plateform,
"cpu_parallelism=" + CEF_USE_CPU_PARALLELISM], check=True)

###############################################################################
### Compile Godot CEF module named GDCef and its subprocess
Expand Down
7 changes: 7 additions & 0 deletions addons/gdcef/gdcef/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ opts.Add(BoolVariable('use_llvm', 'Use the LLVM / Clang compiler', 'no'))
opts.Add(PathVariable('api_path', 'Godot C++ API', ''))
opts.Add(PathVariable('build_path', 'Build path', ''))
opts.Add(PathVariable('cef_artifacts_folder', 'CEF artifacts folder', '', PathVariable.PathAccept))
opts.Add(BoolVariable('cpu_parallelism', 'Use CPU parallelism', 'yes'))

# Updates the environment with the option variables.
opts.Update(env)
Expand Down Expand Up @@ -163,6 +164,12 @@ if platform == 'linux' or platform == 'osx':
'-Werror=implicit-function-declaration',
'-Wtautological-compare'])

# OpenMP
if env['cpu_parallelism']:
env.Append(CCFLAGS=['-fopenmp'])
env.Append(CXXFLAGS=['-fopenmp'])
env.Append(LINKFLAGS=['-fopenmp'])

# User defines
env.Append(CXXFLAGS=['-DCEF_ARTIFACTS_FOLDER=' + env['cef_artifacts_folder']])

Expand Down
36 changes: 22 additions & 14 deletions addons/gdcef/gdcef/src/gdbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/godot.hpp>
#define USING_CONCURRENCY 1
#if USING_CONCURRENCY
#include <ppl.h>

#ifdef _OPENMP
# include <omp.h>
#endif

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,6 +109,14 @@ godot::String GDBrowserView::getError()
int GDBrowserView::init(godot::String const& url, CefBrowserSettings const& settings,
CefWindowInfo const& window_info)
{
#ifdef _OPENMP
#pragma omp parallel
{
#pragma omp single
GDCEF_DEBUG_VAL("OpenMP number of threads = " << omp_get_num_threads());
}
#endif

// Create a new browser using the window parameters specified by
// |windowInfo|. If |request_context| is empty the global request context
// will be used. This method can only be called on the browser process UI
Expand Down Expand Up @@ -186,7 +194,7 @@ void GDBrowserView::onPaint(CefRefPtr<CefBrowser> /*browser*/,
// Copy CEF image buffer to Godot PoolByteArray
m_data.resize(TEXTURE_SIZE);

// Copy per line func for concurrency
// Copy per line func for OpenMP/PPL
unsigned char* imageData = m_data.ptrw();
const unsigned char* cbuffer = (const unsigned char*)buffer;
auto doCopyLine = [imageData, cbuffer, width, COLOR_CHANELS](int line, int x, int copyWidth)
Expand All @@ -205,15 +213,15 @@ void GDBrowserView::onPaint(CefRefPtr<CefBrowser> /*browser*/,

if (bResized)
{
#if USING_CONCURRENCY
concurrency::parallel_for(0, height,
std::bind(doCopyLine, std::placeholders::_1, 0, width));
#else
// PPL
//concurrency::parallel_for(0, height,
// std::bind(doCopyLine, std::placeholders::_1, 0, width));

#pragma omp parallel
for (int y = 0; y < height; ++y)
{
doCopyLine(y, 0, width);
}
#endif

// Copy Godot PoolByteArray to Godot texture.
m_image->set_data(width, height, false, godot::Image::FORMAT_RGBA8, m_data);
Expand All @@ -223,15 +231,15 @@ void GDBrowserView::onPaint(CefRefPtr<CefBrowser> /*browser*/,
{
for (const CefRect& rect : dirtyRects)
{
#if USING_CONCURRENCY
concurrency::parallel_for(rect.y, rect.y + rect.height,
std::bind(doCopyLine, std::placeholders::_1, rect.x, rect.width));
#else
//PPL
//concurrency::parallel_for(rect.y, rect.y + rect.height,
// std::bind(doCopyLine, std::placeholders::_1, rect.x, rect.width));

#pragma omp parallel
for (int y = rect.y; y < rect.y + rect.height; ++y)
{
doCopyLine(y, rect.x, rect.width);
}
#endif
}

// Copy Godot PoolByteArray to Godot texture.
Expand Down

0 comments on commit a71c804

Please sign in to comment.