Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ if (USE_FLOAT_EXCEPTIONS)
add_definitions(-DDAEMON_USE_FLOAT_EXCEPTIONS)
endif()

if (NOT NACL AND BUILD_CLIENT)
option(USE_OPENMP "Use OpenMP to parallelize some tasks" OFF)
endif()

if (MSVC)
set_c_cxx_flag("/MP")

Expand All @@ -267,6 +271,14 @@ if (MSVC)
set_cxx_flag("/std:c++23preview")
endif()

if (NOT NACL AND BUILD_CLIENT AND USE_OPENMP)
try_cxx_flag(OPENMP "/openmp")

if (NOT FLAG_OPENMP)
message(WARNING "Missing OpenMP")
endif()
endif()

if (USE_FAST_MATH)
set_c_cxx_flag("/fp:fast")
else()
Expand Down Expand Up @@ -362,6 +374,14 @@ else()
endif()
endif()

if (NOT NACL AND BUILD_CLIENT AND USE_OPENMP)
try_cxx_flag(FOPENMP "-fopenmp")

if (NOT FLAG_FOPENMP)
message(WARNING "Missing OpenMP")
endif()
endif()

if (NACL AND USE_NACL_SAIGO AND SAIGO_ARCH STREQUAL "arm")
# This should be set for every build type because build type flags
# are set after the other custom flags and then have the last word.
Expand Down
2 changes: 2 additions & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ set(ENGINELIST
${ENGINE_DIR}/framework/CvarSystem.h
${ENGINE_DIR}/framework/LogSystem.cpp
${ENGINE_DIR}/framework/LogSystem.h
${ENGINE_DIR}/framework/Omp.cpp
${ENGINE_DIR}/framework/Omp.h
${ENGINE_DIR}/framework/Resource.cpp
${ENGINE_DIR}/framework/Resource.h
${ENGINE_DIR}/framework/System.cpp
Expand Down
104 changes: 104 additions & 0 deletions src/engine/framework/Omp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2025, Daemon Developers
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/

#include <algorithm>

#include "CvarSystem.h"
#include "Omp.h"

#if defined(_OPENMP)
#include "omp.h"
#endif

#if defined(_OPENMP)
static Cvar::Range<Cvar::Cvar<int>> common_ompThreads(
"common.ompThreads", "OpenMP threads", Cvar::NONE, 0, 0, 32 );
#endif

namespace Omp {
#if defined(_OPENMP)
static int ompMaxThreads = 1;
#endif

static int ompThreads = 1;

static void ReadMaxThreads()
{
#if defined(_OPENMP)
ompMaxThreads = omp_get_max_threads();
#endif
}

void EnlistThreads()
{
#if defined(_OPENMP)
omp_set_num_threads( ompThreads );
#endif
}

void SetupThreads()
{
#if defined(_OPENMP)
if ( common_ompThreads.Get() )
{
ompThreads = common_ompThreads.Get();
return;
}

if ( ompMaxThreads <= 4 )
{
ompThreads = ompMaxThreads;
return;
}

if ( ompMaxThreads <= 16 )
{
ompThreads = ompMaxThreads - ( ompMaxThreads / 4 );
return;
}

ompThreads = 16;
#endif

EnlistThreads();
}

void Init()
{
ReadMaxThreads();
SetupThreads();
EnlistThreads();
}

int GetThreads()
{
return ompThreads;
}
}
41 changes: 41 additions & 0 deletions src/engine/framework/Omp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2025, Daemon Developers
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/

#ifndef COMMON_OMP_H_
#define COMMON_OMP_H_

namespace Omp {
void EnlistThreads();
void SetupThreads();
void Init();
int GetThreads();
};

#endif // COMMON_OMP_H_
3 changes: 3 additions & 0 deletions src/engine/framework/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "ConsoleHistory.h"
#include "CommandSystem.h"
#include "LogSystem.h"
#include "Omp.h"
#include "System.h"
#include "CrashDump.h"
#include "CvarSystem.h"
Expand Down Expand Up @@ -829,6 +830,8 @@ static void SetCvarsWithInitFlag(cmdlineArgs_t& cmdlineArgs)
// Initialize the engine
static void Init(int argc, char** argv)
{
Omp::Init();

cmdlineArgs_t cmdlineArgs;

#ifdef _WIN32
Expand Down
3 changes: 3 additions & 0 deletions src/engine/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Maryland 20850 USA.
#include "framework/CommandSystem.h"
#include "framework/CvarSystem.h"
#include "framework/LogSystem.h"
#include "framework/Omp.h"
#include "framework/System.h"
#include "sys/sys_events.h"
#include <common/FileSystem.h>
Expand Down Expand Up @@ -783,6 +784,8 @@ static Cvar::Cvar<bool> showTraceStats("common.showTraceStats", "are physics tra

void Com_Frame()
{
Omp::SetupThreads();

int msec, minMsec;
static int lastTime = 0;
//int key;
Expand Down
3 changes: 3 additions & 0 deletions src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// tr_backend.c

#include "framework/Omp.h"
#include "tr_local.h"
#include "gl_shader.h"
#include "Material.h"
Expand Down Expand Up @@ -3829,6 +3830,8 @@ RB_RenderThread
*/
void RB_RenderThread()
{
Omp::EnlistThreads();

const void *data;

// wait for either a rendering command or a quit command
Expand Down
16 changes: 16 additions & 0 deletions src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// tr_init.c -- functions that are not called every frame
#include "tr_local.h"
#include "framework/CvarSystem.h"
#include "framework/Omp.h"
#include "DetectGLVendors.h"
#include "Material.h"
#include "GeometryCache.h"
Expand Down Expand Up @@ -1055,6 +1056,21 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
Log::Notice("Using dual processor acceleration." );
}

#if defined(_OPENMP)
int ompThreads = Omp::GetThreads();

if ( ompThreads == 1 )
{
Log::Notice("%sNot using OpenMP parallelism: only one thread.", Color::ToString( Color::Red ) );
}
else
{
Log::Notice("%sUsing OpenMP parallelism with %d threads.", Color::ToString( Color::Green ), ompThreads );
}
#else
Log::Notice("%sNot using OpenMP parallelism: unavailable.", Color::ToString( Color::Red ) );
#endif

if ( r_finish->integer )
{
Log::Notice("Forcing glFinish." );
Expand Down
Loading