2017-10-11 03:09:04 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "dxgi_adapter.h"
|
|
|
|
#include "dxgi_output.h"
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
#include "../dxvk/dxvk_format.h"
|
|
|
|
|
2017-10-11 03:09:04 +02:00
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
DxgiOutput::DxgiOutput(
|
|
|
|
DxgiAdapter* adapter,
|
2018-01-13 16:32:46 +01:00
|
|
|
HMONITOR monitor)
|
|
|
|
: m_adapter(adapter),
|
|
|
|
m_monitor(monitor) {
|
2017-11-26 14:01:41 +01:00
|
|
|
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxgiOutput::~DxgiOutput() {
|
2017-11-26 14:01:41 +01:00
|
|
|
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::QueryInterface(REFIID riid, void** ppvObject) {
|
2018-04-02 12:52:02 +02:00
|
|
|
*ppvObject = nullptr;
|
|
|
|
|
|
|
|
if (riid == __uuidof(IUnknown)
|
|
|
|
|| riid == __uuidof(IDXGIObject)
|
|
|
|
|| riid == __uuidof(IDXGIOutput)) {
|
|
|
|
*ppvObject = ref(this);
|
|
|
|
return S_OK;
|
|
|
|
}
|
2017-10-11 03:09:04 +02:00
|
|
|
|
|
|
|
Logger::warn("DxgiOutput::QueryInterface: Unknown interface query");
|
2018-03-12 12:05:43 +01:00
|
|
|
Logger::warn(str::format(riid));
|
2017-10-11 03:09:04 +02:00
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-28 11:56:58 +02:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetParent(REFIID riid, void **ppParent) {
|
2017-10-11 03:09:04 +02:00
|
|
|
return m_adapter->QueryInterface(riid, ppParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::FindClosestMatchingMode(
|
2017-10-11 03:09:04 +02:00
|
|
|
const DXGI_MODE_DESC *pModeToMatch,
|
|
|
|
DXGI_MODE_DESC *pClosestMatch,
|
|
|
|
IUnknown *pConcernedDevice) {
|
2018-03-12 08:52:32 +01:00
|
|
|
if (pModeToMatch == nullptr) {
|
|
|
|
Logger::err("DxgiOutput::FindClosestMatchingMode: pModeToMatch is nullptr");
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pClosestMatch == nullptr) {
|
|
|
|
Logger::err("DxgiOutput::FindClosestMatchingMode: pClosestMatch is nullptr");
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pModeToMatch->Format == DXGI_FORMAT_UNKNOWN && pConcernedDevice == nullptr) {
|
|
|
|
Logger::err("DxgiOutput::FindClosestMatchingMode: no pointer to device was provided for DXGI_FORMAT_UNKNOWN format");
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
}
|
2018-03-24 11:31:39 +01:00
|
|
|
|
|
|
|
// If no format was specified, fall back to a standard
|
|
|
|
// SRGB format, which is supported on all devices.
|
2018-03-24 13:42:23 +01:00
|
|
|
DXGI_FORMAT targetFormat = pModeToMatch->Format;
|
2018-03-24 11:31:39 +01:00
|
|
|
|
2018-03-24 13:42:23 +01:00
|
|
|
if (targetFormat == DXGI_FORMAT_UNKNOWN)
|
|
|
|
targetFormat = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
|
|
|
|
|
|
|
|
UINT targetRefreshRate = 0;
|
|
|
|
|
|
|
|
if (pModeToMatch->RefreshRate.Denominator != 0
|
|
|
|
&& pModeToMatch->RefreshRate.Numerator != 0) {
|
|
|
|
targetRefreshRate = pModeToMatch->RefreshRate.Numerator
|
|
|
|
/ pModeToMatch->RefreshRate.Denominator;
|
|
|
|
}
|
2018-03-24 11:31:39 +01:00
|
|
|
|
|
|
|
// List all supported modes and filter
|
|
|
|
// out those we don't actually need
|
|
|
|
UINT modeCount = 0;
|
2018-03-24 13:42:23 +01:00
|
|
|
GetDisplayModeList(targetFormat, DXGI_ENUM_MODES_SCALING, &modeCount, nullptr);
|
2018-03-24 11:31:39 +01:00
|
|
|
|
|
|
|
if (modeCount == 0) {
|
|
|
|
Logger::err("DxgiOutput::FindClosestMatchingMode: No modes found");
|
2018-03-12 08:52:32 +01:00
|
|
|
return DXGI_ERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2018-03-24 11:31:39 +01:00
|
|
|
std::vector<DXGI_MODE_DESC> modes(modeCount);
|
2018-03-24 13:42:23 +01:00
|
|
|
GetDisplayModeList(targetFormat, DXGI_ENUM_MODES_SCALING, &modeCount, modes.data());
|
|
|
|
|
|
|
|
for (auto it = modes.begin(); it != modes.end(); ) {
|
|
|
|
bool skipMode = false;
|
2018-03-24 11:31:39 +01:00
|
|
|
|
2018-03-24 13:42:23 +01:00
|
|
|
// Remove modes with a different refresh rate
|
|
|
|
if (targetRefreshRate != 0) {
|
2018-03-24 11:31:39 +01:00
|
|
|
UINT modeRefreshRate = it->RefreshRate.Numerator
|
|
|
|
/ it->RefreshRate.Denominator;
|
2018-03-24 13:42:23 +01:00
|
|
|
skipMode |= modeRefreshRate != targetRefreshRate;
|
2018-03-12 08:52:32 +01:00
|
|
|
}
|
2018-03-24 13:42:23 +01:00
|
|
|
|
|
|
|
// Remove modes with incorrect scaling
|
|
|
|
if (pModeToMatch->Scaling != DXGI_MODE_SCALING_UNSPECIFIED)
|
|
|
|
skipMode |= it->Scaling != pModeToMatch->Scaling;
|
|
|
|
|
|
|
|
it = skipMode ? modes.erase(it) : ++it;
|
2018-03-12 08:52:32 +01:00
|
|
|
}
|
2018-03-24 11:31:39 +01:00
|
|
|
|
|
|
|
// No matching modes found
|
|
|
|
if (modes.size() == 0)
|
2018-03-12 08:52:32 +01:00
|
|
|
return DXGI_ERROR_NOT_FOUND;
|
|
|
|
|
2018-03-24 11:31:39 +01:00
|
|
|
// Select mode with minimal height+width difference
|
2018-04-02 18:49:19 +02:00
|
|
|
UINT minDifference = std::numeric_limits<unsigned int>::max();
|
2018-03-24 13:42:23 +01:00
|
|
|
|
2018-03-12 08:52:32 +01:00
|
|
|
for (auto& mode : modes) {
|
2018-03-24 13:42:23 +01:00
|
|
|
UINT currDifference = std::abs(int(pModeToMatch->Width - mode.Width))
|
|
|
|
+ std::abs(int(pModeToMatch->Height - mode.Height));
|
2018-03-12 08:52:32 +01:00
|
|
|
|
|
|
|
if (currDifference < minDifference) {
|
|
|
|
minDifference = currDifference;
|
|
|
|
*pClosestMatch = mode;
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 11:31:39 +01:00
|
|
|
|
2018-03-12 08:52:32 +01:00
|
|
|
return S_OK;
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetDesc(DXGI_OUTPUT_DESC *pDesc) {
|
2017-10-11 03:09:04 +02:00
|
|
|
if (pDesc == nullptr)
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
|
2018-01-18 23:59:40 +01:00
|
|
|
::MONITORINFOEX monInfo;
|
|
|
|
monInfo.cbSize = sizeof(monInfo);
|
2018-03-24 13:42:23 +01:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
if (!::GetMonitorInfo(m_monitor, &monInfo)) {
|
|
|
|
Logger::err("DxgiOutput: Failed to query monitor info");
|
|
|
|
return E_FAIL;
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::memset(pDesc->DeviceName, 0, sizeof(pDesc->DeviceName));
|
2018-04-02 18:49:03 +02:00
|
|
|
std::mbstowcs(pDesc->DeviceName, monInfo.szDevice, std::size(pDesc->DeviceName) - 1);
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
pDesc->DesktopCoordinates = monInfo.rcMonitor;
|
2017-10-11 03:09:04 +02:00
|
|
|
pDesc->AttachedToDesktop = 1;
|
|
|
|
pDesc->Rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
|
2018-01-13 16:32:46 +01:00
|
|
|
pDesc->Monitor = m_monitor;
|
2017-10-11 03:09:04 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetDisplayModeList(
|
2017-10-11 03:09:04 +02:00
|
|
|
DXGI_FORMAT EnumFormat,
|
|
|
|
UINT Flags,
|
|
|
|
UINT *pNumModes,
|
|
|
|
DXGI_MODE_DESC *pDesc) {
|
|
|
|
if (pNumModes == nullptr)
|
|
|
|
return DXGI_ERROR_INVALID_CALL;
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
// Query monitor info to get the device name
|
2018-01-18 23:59:40 +01:00
|
|
|
::MONITORINFOEX monInfo;
|
|
|
|
monInfo.cbSize = sizeof(monInfo);
|
2018-03-24 13:42:23 +01:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
if (!::GetMonitorInfo(m_monitor, &monInfo)) {
|
|
|
|
Logger::err("DxgiOutput: Failed to query monitor info");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
// Walk over all modes that the display supports and
|
|
|
|
// return those that match the requested format etc.
|
|
|
|
DEVMODE devMode;
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
uint32_t srcModeId = 0;
|
|
|
|
uint32_t dstModeId = 0;
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-03-24 13:42:23 +01:00
|
|
|
const bool includeStretchedModes = (Flags & DXGI_ENUM_MODES_SCALING);
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
while (::EnumDisplaySettings(monInfo.szDevice, srcModeId++, &devMode)) {
|
|
|
|
// Skip interlaced modes altogether
|
|
|
|
if (devMode.dmDisplayFlags & DM_INTERLACED)
|
|
|
|
continue;
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
// Skip modes with incompatible formats
|
|
|
|
if (devMode.dmBitsPerPel != GetFormatBpp(EnumFormat))
|
|
|
|
continue;
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2018-03-24 13:42:23 +01:00
|
|
|
// Skip stretched modes unless they are requested
|
|
|
|
const bool isStretched = devMode.dmPelsWidth != UINT(monInfo.rcMonitor.right - monInfo.rcMonitor.left)
|
|
|
|
|| devMode.dmPelsHeight != UINT(monInfo.rcMonitor.bottom - monInfo.rcMonitor.top);
|
|
|
|
|
|
|
|
if (isStretched && !includeStretchedModes)
|
|
|
|
continue;
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
// Write back display mode
|
|
|
|
if (pDesc != nullptr) {
|
|
|
|
if (dstModeId >= *pNumModes)
|
|
|
|
return DXGI_ERROR_MORE_DATA;
|
2017-10-11 03:09:04 +02:00
|
|
|
|
2017-12-31 00:23:34 +01:00
|
|
|
DXGI_MODE_DESC mode;
|
2018-01-13 16:32:46 +01:00
|
|
|
mode.Width = devMode.dmPelsWidth;
|
|
|
|
mode.Height = devMode.dmPelsHeight;
|
|
|
|
mode.RefreshRate = { devMode.dmDisplayFrequency, 1 };
|
|
|
|
mode.Format = EnumFormat;
|
|
|
|
mode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
|
2018-03-24 13:42:23 +01:00
|
|
|
mode.Scaling = isStretched
|
|
|
|
? DXGI_MODE_SCALING_STRETCHED
|
|
|
|
: DXGI_MODE_SCALING_CENTERED;
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
pDesc[dstModeId] = mode;
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
2018-01-13 16:32:46 +01:00
|
|
|
|
|
|
|
dstModeId += 1;
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
*pNumModes = dstModeId;
|
|
|
|
return S_OK;
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetDisplaySurfaceData(IDXGISurface *pDestination) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::GetDisplaySurfaceData: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetFrameStatistics(DXGI_FRAME_STATISTICS *pStats) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::GetFrameStatistics: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetGammaControl(DXGI_GAMMA_CONTROL *pArray) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::GetGammaControl: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::GetGammaControlCapabilities(DXGI_GAMMA_CONTROL_CAPABILITIES *pGammaCaps) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::GetGammaControlCapabilities: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
void STDMETHODCALLTYPE DxgiOutput::ReleaseOwnership() {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::warn("DxgiOutput::ReleaseOwnership: Stub");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::SetDisplaySurface(IDXGISurface *pScanoutSurface) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::SetDisplaySurface: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::SetGammaControl(const DXGI_GAMMA_CONTROL *pArray) {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::err("DxgiOutput::SetGammaControl: Not implemented");
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::TakeOwnership(
|
2017-10-11 03:09:04 +02:00
|
|
|
IUnknown *pDevice,
|
|
|
|
BOOL Exclusive) {
|
|
|
|
Logger::warn("DxgiOutput::TakeOwnership: Stub");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
HRESULT STDMETHODCALLTYPE DxgiOutput::WaitForVBlank() {
|
2017-10-11 03:09:04 +02:00
|
|
|
Logger::warn("DxgiOutput::WaitForVBlank: Stub");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:32:46 +01:00
|
|
|
|
|
|
|
uint32_t DxgiOutput::GetFormatBpp(DXGI_FORMAT Format) const {
|
|
|
|
DxgiFormatInfo formatInfo = m_adapter->LookupFormat(Format, DxgiFormatMode::Any);
|
|
|
|
return imageFormatInfo(formatInfo.format)->elementSize * 8;
|
|
|
|
}
|
|
|
|
|
2017-10-11 03:09:04 +02:00
|
|
|
}
|