Skip to content

Commit

Permalink
OpenGL: Add option to control desired adapter type in OpenGL (close #450
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MikhailGorobets committed Jan 10, 2024
1 parent 1210c85 commit 634a52e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Graphics/GraphicsEngine/interface/GraphicsTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,12 @@ struct EngineGLCreateInfo DILIGENT_DERIVE(EngineCreateInfo)
/// Use IRenderDevice::GetDeviceInfo().NDC to get current NDC.
Bool ZeroToOneNDZ DEFAULT_INITIALIZER(false);

/// The GPU preference allows you to request either the integrated or dedicated GPU
/// on systems having both onboard and dedicated GPUs. Currently this works only on Windows and Linux
/// - On Windows this is done by setting the `NvOptimusEnablement` and `AmdPowerXpressRequestHighPerformance`
/// - On Linux this affects the `DRI_PRIME` environment variable that's used by Mesa drivers that support PRIME.
ADAPTER_TYPE PreferredAdapterType DEFAULT_INITIALIZER(ADAPTER_TYPE_UNKNOWN);

#if PLATFORM_EMSCRIPTEN
/// WebGL context attributes.
WebGLContextAttribs WebGLAttribs;
Expand Down
33 changes: 33 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@
# include "RenderDeviceGLESImpl.hpp"
#endif

#if PLATFORM_WIN32 || PLATFORM_UNIVERSAL_WINDOWS
# define DILIGENT_INTERFACE_EXPORT
#elif PLATFORM_LINUX
# define DILIGENT_INTERFACE_EXPORT __attribute__((visibility("default")))
#else
# define DILIGENT_INTERFACE_EXPORT
#endif

/// Let the NVIDIA and AMD know we want to use their graphics card on a dual graphics card system.
extern "C"
{
/// https://community.amd.com/thread/169965
DILIGENT_INTERFACE_EXPORT uint64_t AmdPowerXpressRequestHighPerformance = 0x0;

/// http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/OptimusRenderingPolicies.pdf
DILIGENT_INTERFACE_EXPORT uint64_t NvOptimusEnablement = 0x0;
}

namespace Diligent
{

Expand Down Expand Up @@ -137,6 +155,20 @@ static void SetDefaultGraphicsAdapterInfo(GraphicsAdapterInfo& AdapterInfo)
AdapterInfo.Queues[0].TextureCopyGranularity[2] = 1;
}

static void SetPreferredAdapter(const EngineGLCreateInfo& EngineCI)
{
bool IsEnableDedicatedGPU = EngineCI.PreferredAdapterType == ADAPTER_TYPE_DISCRETE;
#if PLATFORM_WIN32 || PLATFORM_UNIVERSAL_WINDOWS
AmdPowerXpressRequestHighPerformance = IsEnableDedicatedGPU;
NvOptimusEnablement = IsEnableDedicatedGPU;
#elif PLAtTFORM_LINUX
setenv("DRI_PRIME", IsEnableDedicatedGPU ? "1" : "0", 1);
#else
if (EngineCI.PreferredAdapterType != ADAPTER_TYPE_UNKNOWN)
LOG_WARNING_MESSAGE("Setting prefered adapter type isn't supported on this platform")

Check failure on line 168 in Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp

View workflow job for this annotation

GitHub Actions / Linux -> Pre-checks

prefered ==> preferred
#endif
}

void EngineFactoryOpenGLImpl::EnumerateAdapters(Version MinVersion,
Uint32& NumAdapters,
GraphicsAdapterInfo* Adapters) const
Expand Down Expand Up @@ -303,6 +335,7 @@ void EngineFactoryOpenGLImpl::AttachToActiveGLContext(const EngineGLCreateInfo&
SetRawAllocator(EngineCI.pRawMemAllocator);
auto& RawMemAllocator = GetRawAllocator();

SetPreferredAdapter(EngineCI);
RenderDeviceGLImpl* pRenderDeviceOpenGL{
NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)(
RawMemAllocator, this, EngineCI //
Expand Down

0 comments on commit 634a52e

Please sign in to comment.