using System;
using System.Diagnostics.CodeAnalysis;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.General;
using NvAPIWrapper.Native.GPU;
using NvAPIWrapper.Native.GPU.Structures;
using NvAPIWrapper.Native.Helpers;
using NvAPIWrapper.Native.Helpers.Structures;
using NvAPIWrapper.Native.Interfaces.GPU;
namespace NvAPIWrapper.Native
{
public static partial class GPUApi
{
///
/// [PRIVATE]
/// Enables the overclocked performance states
///
/// The handle of the GPU to perform the operation on.
public static void EnableOverclockedPStates(PhysicalGPUHandle gpuHandle)
{
var status = DelegateFactory.GetDelegate()(
gpuHandle
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
///
/// This function retrieves the clock frequencies information from an specific physical GPU and fills the structure
///
///
/// Handle of the physical GPU for which the clock frequency information is to be
/// retrieved.
///
///
/// The structure that holds options for the operations and should be filled with the
/// results, use null to return current clock frequencies
///
/// The device clock frequencies information.
/// This operation is not supported.
/// Status.NvidiaDeviceNotFound: No NVIDIA GPU driving a display was found.
/// A delegate callback throws an exception.
public static IClockFrequencies GetAllClockFrequencies(
PhysicalGPUHandle physicalGPUHandle,
IClockFrequencies clockFrequencyOptions = null)
{
var getClocksInfo = DelegateFactory.GetDelegate();
if (clockFrequencyOptions == null)
{
foreach (var acceptType in getClocksInfo.Accepts())
{
var instance = acceptType.Instantiate();
using (var clockFrequenciesInfo = ValueTypeReference.FromValueType(instance, acceptType))
{
var status = getClocksInfo(physicalGPUHandle, clockFrequenciesInfo);
if (status == Status.IncompatibleStructureVersion)
{
continue;
}
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockFrequenciesInfo.ToValueType(acceptType);
}
}
}
else
{
using (var clockFrequenciesInfo =
ValueTypeReference.FromValueType(clockFrequencyOptions, clockFrequencyOptions.GetType()))
{
var status = getClocksInfo(physicalGPUHandle, clockFrequenciesInfo);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockFrequenciesInfo.ToValueType(clockFrequencyOptions.GetType());
}
}
throw new NVIDIANotSupportedException("This operation is not supported.");
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the clock boost lock for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The GPU clock boost lock.
public static PrivateClockBoostLockV2 GetClockBoostLock(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateClockBoostLockV2).Instantiate();
using (var clockLockReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockLockReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockLockReference.ToValueType(typeof(PrivateClockBoostLockV2));
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the clock boost mask for passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The GPI clock boost mask.
public static PrivateClockBoostMasksV1 GetClockBoostMask(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateClockBoostMasksV1).Instantiate();
using (var clockBoostReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockBoostReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockBoostReference.ToValueType(typeof(PrivateClockBoostMasksV1));
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the clock boost ranges for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The GPU clock boost ranges.
public static PrivateClockBoostRangesV1 GetClockBoostRanges(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateClockBoostRangesV1).Instantiate();
using (var clockRangesReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockRangesReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockRangesReference.ToValueType(typeof(PrivateClockBoostRangesV1));
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the clock boost table for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The GPU clock boost table.
public static PrivateClockBoostTableV1 GetClockBoostTable(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateClockBoostTableV1).Instantiate();
using (var clockTableReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockTableReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return clockTableReference.ToValueType(typeof(PrivateClockBoostTableV1));
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the core voltage boost percentage for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The voltage boost percentage.
public static PrivateVoltageBoostPercentV1 GetCoreVoltageBoostPercent(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateVoltageBoostPercentV1).Instantiate();
using (var voltageBoostReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
voltageBoostReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return voltageBoostReference.ToValueType(
typeof(PrivateVoltageBoostPercentV1));
}
}
///
/// This function returns the current performance state (P-State).
///
/// GPU handle to get information about
/// The current performance state.
/// Status.InvalidArgument: gpuHandle is NULL
public static PerformanceStateId GetCurrentPerformanceState(PhysicalGPUHandle gpuHandle)
{
var status =
DelegateFactory.GetDelegate()(gpuHandle,
out var performanceState);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return performanceState;
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the current voltage status for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The voltage status of the GPU.
public static PrivateVoltageStatusV1 GetCurrentVoltage(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateVoltageStatusV1).Instantiate();
using (var voltageStatusReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
voltageStatusReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return voltageStatusReference.ToValueType(typeof(PrivateVoltageStatusV1));
}
}
///
/// This function retrieves all available performance states (P-States) information.
/// P-States are GPU active/executing performance capability and power consumption states.
///
/// GPU handle to get information about.
/// Flag to get specific information about a performance state.
/// Retrieved performance states information
/// Status.InvalidArgument: gpuHandle is NULL
/// Status.ExpectedPhysicalGPUHandle: gpuHandle was not a physical GPU handle
[SuppressMessage("ReSharper", "EventExceptionNotDocumented")]
public static IPerformanceStatesInfo GetPerformanceStates(
PhysicalGPUHandle physicalGPUHandle,
GetPerformanceStatesInfoFlags flags)
{
var getPerformanceStatesInfo = DelegateFactory.GetDelegate();
foreach (var acceptType in getPerformanceStatesInfo.Accepts())
{
var instance = acceptType.Instantiate();
using (var performanceStateInfo = ValueTypeReference.FromValueType(instance, acceptType))
{
var status = getPerformanceStatesInfo(physicalGPUHandle, performanceStateInfo, flags);
if (status == Status.IncompatibleStructureVersion)
{
continue;
}
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return performanceStateInfo.ToValueType(acceptType);
}
}
throw new NVIDIANotSupportedException("This operation is not supported.");
}
///
/// This function retrieves all available performance states (P-States) 2.0 information.
/// P-States are GPU active/executing performance capability and power consumption states.
///
/// GPU handle to get information about.
/// Retrieved performance states 2.0 information
/// Status.InvalidArgument: gpuHandle is NULL
/// Status.ExpectedPhysicalGPUHandle: gpuHandle was not a physical GPU handle
[SuppressMessage("ReSharper", "EventExceptionNotDocumented")]
public static IPerformanceStates20Info GetPerformanceStates20(PhysicalGPUHandle physicalGPUHandle)
{
var getPerformanceStates20 = DelegateFactory.GetDelegate();
foreach (var acceptType in getPerformanceStates20.Accepts())
{
var instance = acceptType.Instantiate();
using (var performanceStateInfo = ValueTypeReference.FromValueType(instance, acceptType))
{
var status = getPerformanceStates20(physicalGPUHandle, performanceStateInfo);
if (status == Status.IncompatibleStructureVersion)
{
continue;
}
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return performanceStateInfo.ToValueType(acceptType);
}
}
throw new NVIDIANotSupportedException("This operation is not supported.");
}
///
/// [PRIVATE] - [Pascal Only]
/// Gets the GPU boost frequency curve controls for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The retrieved VFP curve.
public static PrivateVFPCurveV1 GetVFPCurve(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateVFPCurveV1).Instantiate();
using (var vfpCurveReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
vfpCurveReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return vfpCurveReference.ToValueType(typeof(PrivateVFPCurveV1));
}
}
///
/// [PRIVATE]
/// Gets the performance policies current information for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The performance policies information.
public static PrivatePerformanceInfoV1 PerformancePoliciesGetInfo(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivatePerformanceInfoV1).Instantiate();
using (var performanceInfoReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
performanceInfoReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return performanceInfoReference.ToValueType(typeof(PrivatePerformanceInfoV1));
}
}
///
/// [PRIVATE]
/// Gets the performance policies status for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The performance policies status of the GPU.
public static PrivatePerformanceStatusV1 PerformancePoliciesGetStatus(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivatePerformanceStatusV1).Instantiate();
using (var performanceStatusReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
performanceStatusReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return performanceStatusReference.ToValueType(
typeof(PrivatePerformanceStatusV1));
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Sets the clock boost lock status for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The new clock boost lock status.
public static void SetClockBoostLock(PhysicalGPUHandle gpuHandle, PrivateClockBoostLockV2 clockBoostLock)
{
using (var clockLockReference = ValueTypeReference.FromValueType(clockBoostLock))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockLockReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Sets the clock boost table for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The new clock table.
public static void SetClockBoostTable(PhysicalGPUHandle gpuHandle, PrivateClockBoostTableV1 clockBoostTable)
{
using (var clockTableReference = ValueTypeReference.FromValueType(clockBoostTable))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
clockTableReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
}
///
/// [PRIVATE] - [Pascal Only]
/// Sets the core voltage boost percentage
///
/// The handle of the GPU to perform the operation on.
/// The voltage boost percentages.
public static void SetCoreVoltageBoostPercent(
PhysicalGPUHandle gpuHandle,
PrivateVoltageBoostPercentV1 boostPercent)
{
using (var boostPercentReference = ValueTypeReference.FromValueType(boostPercent))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
boostPercentReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
}
///
/// [PRIVATE]
/// This function sets the performance states (P-States) 2.0 information.
/// P-States are GPU active/executing performance capability and power consumption states.
///
/// GPU handle to get information about.
/// Performance status 2.0 information to set
/// Status.InvalidArgument: gpuHandle is NULL
/// Status.ExpectedPhysicalGPUHandle: gpuHandle was not a physical GPU handle
public static void SetPerformanceStates20(
PhysicalGPUHandle physicalGPUHandle,
IPerformanceStates20Info performanceStates20Info)
{
using (var performanceStateInfo =
ValueTypeReference.FromValueType(performanceStates20Info, performanceStates20Info.GetType()))
{
var status = DelegateFactory.GetDelegate()(
physicalGPUHandle,
performanceStateInfo
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
}
}
}