|
1 | 1 | /* |
2 | | - * Copyright 2019-2023 Diligent Graphics LLC |
| 2 | + * Copyright 2019-2024 Diligent Graphics LLC |
3 | 3 | * Copyright 2015-2019 Egor Yusov |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
@@ -137,6 +137,38 @@ static void SetDefaultGraphicsAdapterInfo(GraphicsAdapterInfo& AdapterInfo) |
137 | 137 | AdapterInfo.Queues[0].TextureCopyGranularity[2] = 1; |
138 | 138 | } |
139 | 139 |
|
| 140 | +static void SetPreferredAdapter(const EngineGLCreateInfo& EngineCI) |
| 141 | +{ |
| 142 | + bool EnableDedicatedGPU = EngineCI.PreferredAdapterType == ADAPTER_TYPE_DISCRETE; |
| 143 | +#if PLATFORM_WIN32 |
| 144 | + const HMODULE ModuleHandle = GetModuleHandle(nullptr); |
| 145 | + Uint64* const NvOptimusEnablement = reinterpret_cast<Uint64*>(GetProcAddress(ModuleHandle, "NvOptimusEnablement")); |
| 146 | + Uint64* const AmdPowerXpressRequestHighPerformance = reinterpret_cast<Uint64*>(GetProcAddress(ModuleHandle, "AmdPowerXpressRequestHighPerformance")); |
| 147 | + if (!NvOptimusEnablement && !AmdPowerXpressRequestHighPerformance) |
| 148 | + { |
| 149 | + LOG_WARNING_MESSAGE("Neither NvOptimusEnablement nor AmdPowerXpressRequestHighPerformance symbols found. " |
| 150 | + "You need to explicitly define these variables in your executable file: " |
| 151 | + "https://gist.github.com/statico/6809850727c708f08458XpressRequestHighPerformance, " |
| 152 | + "or you can use the `Diligent-GLAdapterSelector` object library as source input to your executable target: " |
| 153 | + "`target_sources(MyExecutable PRIVATE $<TARGET_OBJECTS:Diligent-GLAdapterSelector>)`, " |
| 154 | + "see https://cmake.org/cmake/help/v3.16/manual/cmake-buildsystem.7.html#object-libraries)."); |
| 155 | + } |
| 156 | + if (AmdPowerXpressRequestHighPerformance) |
| 157 | + { |
| 158 | + *AmdPowerXpressRequestHighPerformance = EnableDedicatedGPU; |
| 159 | + } |
| 160 | + if (NvOptimusEnablement) |
| 161 | + { |
| 162 | + *NvOptimusEnablement = EnableDedicatedGPU; |
| 163 | + } |
| 164 | +#elif PLAtTFORM_LINUX |
| 165 | + setenv("DRI_PRIME", EnableDedicatedGPU ? "1" : "0", 1); |
| 166 | +#else |
| 167 | + if (EnableDedicatedGPU) |
| 168 | + LOG_WARNING_MESSAGE("Setting preferred adapter type isn't supported on this platform"); |
| 169 | +#endif |
| 170 | +} |
| 171 | + |
140 | 172 | void EngineFactoryOpenGLImpl::EnumerateAdapters(Version MinVersion, |
141 | 173 | Uint32& NumAdapters, |
142 | 174 | GraphicsAdapterInfo* Adapters) const |
@@ -205,6 +237,7 @@ void EngineFactoryOpenGLImpl::CreateDeviceAndSwapChainGL(const EngineGLCreateInf |
205 | 237 | SetRawAllocator(EngineCI.pRawMemAllocator); |
206 | 238 | auto& RawMemAllocator = GetRawAllocator(); |
207 | 239 |
|
| 240 | + SetPreferredAdapter(EngineCI); |
208 | 241 | RenderDeviceGLImpl* pRenderDeviceOpenGL{ |
209 | 242 | NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)( |
210 | 243 | RawMemAllocator, this, EngineCI, &SCDesc // |
@@ -303,6 +336,7 @@ void EngineFactoryOpenGLImpl::AttachToActiveGLContext(const EngineGLCreateInfo& |
303 | 336 | SetRawAllocator(EngineCI.pRawMemAllocator); |
304 | 337 | auto& RawMemAllocator = GetRawAllocator(); |
305 | 338 |
|
| 339 | + SetPreferredAdapter(EngineCI); |
306 | 340 | RenderDeviceGLImpl* pRenderDeviceOpenGL{ |
307 | 341 | NEW_RC_OBJ(RawMemAllocator, "TRenderDeviceGLImpl instance", TRenderDeviceGLImpl)( |
308 | 342 | RawMemAllocator, this, EngineCI // |
|
0 commit comments