Skip to content

Commit

Permalink
Fix issue using sRGB for the mirror window.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbucchia committed Jun 30, 2023
1 parent e623b2b commit 6a335f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
11 changes: 8 additions & 3 deletions pimax-openxr/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ namespace pimax_openxr {

std::set<std::pair<pvrTextureSwapChain, uint32_t>> committedSwapchainImages;

bool firstProjectionLayer = true;
bool isProj0SRGB = false;
bool isFirstProjectionLayer = true;

// Construct the list of layers.
std::vector<pvrLayer_Union> layersAllocator;
Expand Down Expand Up @@ -547,6 +548,10 @@ namespace pimax_openxr {
return XR_ERROR_VALIDATION_FAILURE;
}

if (isFirstProjectionLayer) {
isProj0SRGB = isSRGBFormat(xrSwapchain.dxgiFormatForSubmission);
}

const uint32_t pvrViewIndex = viewIndex % xr::StereoView::Count;

// Fill out color buffer information.
Expand Down Expand Up @@ -678,7 +683,7 @@ namespace pimax_openxr {
// Warning: quad views might have created 2 layers above!
// One of them was pushed already to the list of layers.

firstProjectionLayer = false;
isFirstProjectionLayer = false;

} else if (frameEndInfo->layers[i]->type == XR_TYPE_COMPOSITION_LAYER_QUAD) {
const XrCompositionLayerQuad* quad =
Expand Down Expand Up @@ -928,7 +933,7 @@ namespace pimax_openxr {
if (m_useMirrorWindow && !m_mirrorWindowThread.joinable()) {
createMirrorWindow();
}
updateMirrorWindow();
updateMirrorWindow(isProj0SRGB);

// When using RenderDoc, signal a frame through the dummy swapchain.
if (m_dxgiSwapchain) {
Expand Down
74 changes: 38 additions & 36 deletions pimax-openxr/mirror_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,6 @@ namespace pimax_openxr {
CHECK_MSG(m_mirrorWindowHwnd, "Failed to CreateWindowW()");
m_mirrorWindowReady = true;

// Create the swapchain.
ComPtr<IDXGIFactory2> dxgiFactory;
{
ComPtr<IDXGIDevice1> dxgiDevice;
CHECK_HRCMD(m_pvrSubmissionDevice->QueryInterface(IID_PPV_ARGS(dxgiDevice.ReleaseAndGetAddressOf())));

ComPtr<IDXGIAdapter> dxgiAdapter;
CHECK_HRCMD(dxgiDevice->GetAdapter(&dxgiAdapter));
CHECK_HRCMD(dxgiAdapter->GetParent(IID_PPV_ARGS(dxgiFactory.ReleaseAndGetAddressOf())));
}

RECT rect = {0, 0, defaultWidth, defaultHeight};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
const auto width = rect.right - rect.left;
const auto height = rect.bottom - rect.top;

DXGI_SWAP_CHAIN_DESC1 swapchainDesc{};
swapchainDesc.Width = width;
swapchainDesc.Height = height;
swapchainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
swapchainDesc.SampleDesc.Count = 1;
swapchainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapchainDesc.BufferCount = 2;
swapchainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
CHECK_HRCMD(dxgiFactory->CreateSwapChainForHwnd(m_pvrSubmissionDevice.Get(),
m_mirrorWindowHwnd,
&swapchainDesc,
nullptr,
nullptr,
m_mirrorWindowSwapchain.ReleaseAndGetAddressOf()));

ShowWindow(m_mirrorWindowHwnd, SW_SHOW);
UpdateWindow(m_mirrorWindowHwnd);

Expand All @@ -121,10 +90,10 @@ namespace pimax_openxr {
});
}

void OpenXrRuntime::updateMirrorWindow() {
void OpenXrRuntime::updateMirrorWindow(bool preferSRGB) {
std::unique_lock lock(m_mirrorWindowMutex);

if (!m_mirrorWindowSwapchain) {
if (!m_mirrorWindowReady) {
return;
}

Expand All @@ -139,12 +108,44 @@ namespace pimax_openxr {
return;
}

// Check for resizing or initial creation.
// Workaround: PVR does not seem to correctly write to non-SRGB, so for now we always prefer non-SRGB.
preferSRGB = false;

bool isSRGB = preferSRGB;
D3D11_TEXTURE2D_DESC mirrorDesc;
if (m_mirrorTexture) {
m_mirrorTexture->GetDesc(&mirrorDesc);
isSRGB = isSRGBFormat(mirrorDesc.Format);
}
if (!m_mirrorTexture || mirrorDesc.Width != width || mirrorDesc.Height != height) {

// Create the DXGI swapchain for the window.
if (!m_mirrorWindowSwapchain || preferSRGB != isSRGB) {
ComPtr<IDXGIFactory2> dxgiFactory;
ComPtr<IDXGIDevice1> dxgiDevice;
CHECK_HRCMD(m_pvrSubmissionDevice->QueryInterface(IID_PPV_ARGS(dxgiDevice.ReleaseAndGetAddressOf())));

ComPtr<IDXGIAdapter> dxgiAdapter;
CHECK_HRCMD(dxgiDevice->GetAdapter(&dxgiAdapter));
CHECK_HRCMD(dxgiAdapter->GetParent(IID_PPV_ARGS(dxgiFactory.ReleaseAndGetAddressOf())));

DXGI_SWAP_CHAIN_DESC1 swapchainDesc{};
swapchainDesc.Width = width;
swapchainDesc.Height = height;
swapchainDesc.Format = preferSRGB ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;
swapchainDesc.SampleDesc.Count = 1;
swapchainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapchainDesc.BufferCount = 2;
swapchainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
CHECK_HRCMD(dxgiFactory->CreateSwapChainForHwnd(m_pvrSubmissionDevice.Get(),
m_mirrorWindowHwnd,
&swapchainDesc,
nullptr,
nullptr,
m_mirrorWindowSwapchain.ReleaseAndGetAddressOf()));
}

// Check for resizing or initial creation.
if (!m_mirrorTexture || mirrorDesc.Width != width || mirrorDesc.Height != height || preferSRGB != isSRGB) {
TraceLoggingWrite(g_traceProvider, "MirrorWindow", TLArg(width, "Width"), TLArg(height, "Height"));

CHECK_HRCMD(m_mirrorWindowSwapchain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0));
Expand All @@ -156,7 +157,8 @@ namespace pimax_openxr {
}

pvrMirrorTextureDesc mirrorDesc;
mirrorDesc.Format = pvrTextureFormat::PVR_FORMAT_R8G8B8A8_UNORM_SRGB;
mirrorDesc.Format = preferSRGB ? pvrTextureFormat::PVR_FORMAT_R8G8B8A8_UNORM_SRGB
: pvrTextureFormat::PVR_FORMAT_R8G8B8A8_UNORM;
mirrorDesc.Width = width;
mirrorDesc.Height = height;
mirrorDesc.SampleCount = 1;
Expand Down
2 changes: 1 addition & 1 deletion pimax-openxr/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ namespace pimax_openxr {

// mirror_window.cpp
void createMirrorWindow();
void updateMirrorWindow();
void updateMirrorWindow(bool preferSRGB = false);
LRESULT CALLBACK mirrorWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
friend LRESULT CALLBACK wndProcWrapper(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

Expand Down

0 comments on commit 6a335f7

Please sign in to comment.