Skip to content

Commit

Permalink
Fix post-processing of focus view.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbucchia committed Jun 24, 2023
1 parent 3f5ce4a commit 4c45212
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion companion/ExperimentalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void LoadSettings()
}
else
{
forceHalf.Checked = forceThird.Checked = true;
forceHalf.Checked = forceThird.Checked = false;
}
// Convert value from microseconds to tenth of milliseconds.
timingBias.Value = multiplier == 0 ? ((int)key.GetValue("frame_time_override_offset", 0) / 100) : 0;
Expand Down
12 changes: 6 additions & 6 deletions pimax-openxr/AlphaBlending.hlsli
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
float4 processAlpha(float4 input, uint2 pos, uint2 widthHeight, int mode) {
float4 processAlpha(float4 input, uint2 pos, uint2 widthHeight, bool ignoreAlpha, bool isUnpremultipliedAlpha, bool isFocusView) {
float4 output = input;
if (mode & 1) {

if (ignoreAlpha) {
output.a = 1;
}
if (mode & 4) {
if (isFocusView) {
float2 transitionArea = 0.1 * widthHeight;
float2 s = smoothstep(float2(0, 0), transitionArea, pos) -
smoothstep(widthHeight - transitionArea, widthHeight, pos);
output.a = max(0.5, s.x * s.y);
// RGB need premultiplication (below).
output.a = max(0, s.x * s.y);
}
if (mode & 6) {
if (isUnpremultipliedAlpha) {
output.rgb = output.rgb * output.a;
}
return output;
Expand Down
7 changes: 5 additions & 2 deletions pimax-openxr/AlphaBlendingCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "AlphaBlending.hlsli"

cbuffer config : register(b0) {
int mode;
bool ignoreAlpha;
bool isUnpremultipliedAlpha;
bool isFocusView;
};

Texture2D in_texture : register(t0);
Expand All @@ -13,5 +15,6 @@ RWTexture2D<float4> out_texture : register(u0);
void main(uint2 pos : SV_DispatchThreadID) {
uint width, height;
in_texture.GetDimensions(width, height);
out_texture[pos] = processAlpha(in_texture[pos], pos, uint2(width, height), mode);
out_texture[pos] =
processAlpha(in_texture[pos], pos, uint2(width, height), ignoreAlpha, isUnpremultipliedAlpha, isFocusView);
}
7 changes: 5 additions & 2 deletions pimax-openxr/AlphaBlendingTexArrayCS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "AlphaBlending.hlsli"

cbuffer config : register(b0) {
int mode;
bool ignoreAlpha;
bool isUnpremultipliedAlpha;
bool isFocusView;
};

Texture2DArray in_texture : register(t0);
Expand All @@ -13,5 +15,6 @@ RWTexture2D<float4> out_texture : register(u0);
void main(uint2 pos : SV_DispatchThreadID) {
uint width, height, size;
in_texture.GetDimensions(width, height, size);
out_texture[pos] = processAlpha(in_texture[float3(pos, 0)], pos, uint2(width, height), mode);
out_texture[pos] = processAlpha(
in_texture[float3(pos, 0)], pos, uint2(width, height), ignoreAlpha, isUnpremultipliedAlpha, isFocusView);
}
14 changes: 12 additions & 2 deletions pimax-openxr/d3d11_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ namespace pimax_openxr {
using namespace pimax_openxr::log;
using namespace pimax_openxr::utils;

struct AlphaBlendingCSConstants {
alignas(4) bool ignoreAlpha;
alignas(4) bool isUnpremultipliedAlpha;
alignas(4) bool isFocusView;
};

// https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#xrGetD3D11GraphicsRequirementsKHR
XrResult OpenXrRuntime::xrGetD3D11GraphicsRequirementsKHR(XrInstance instance,
XrSystemId systemId,
Expand Down Expand Up @@ -526,11 +532,15 @@ namespace pimax_openxr {
// 0: shader for Tex2D, 1: shader for Tex2DArray.
const int shaderToUse = xrSwapchain.xrDesc.arraySize == 1 ? 0 : 1;
{
AlphaBlendingCSConstants constants{};
constants.ignoreAlpha = needClearAlpha;
constants.isUnpremultipliedAlpha = needPremultiplyAlpha;
constants.isFocusView = postProcessFocusView;

D3D11_MAPPED_SUBRESOURCE mappedResources;
CHECK_HRCMD(m_pvrSubmissionContext->Map(
xrSwapchain.convertConstants.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResources));
*(uint32_t*)mappedResources.pData =
(needClearAlpha ? 0x1 : 0) | (needPremultiplyAlpha ? 0x2 : 0) | (postProcessFocusView ? 0x4 : 0);
memcpy(mappedResources.pData, &constants, sizeof(constants));
m_pvrSubmissionContext->Unmap(xrSwapchain.convertConstants.Get(), 0);
m_pvrSubmissionContext->CSSetConstantBuffers(0, 1, xrSwapchain.convertConstants.GetAddressOf());

Expand Down
2 changes: 1 addition & 1 deletion pimax-openxr/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ namespace pimax_openxr {
bool m_debugFocusViews{false};
bool m_useDeferredFrameWait{true};
bool m_useDeferredFrameWaitThisFrame{false};
bool m_postProcessFocusView{false};
bool m_postProcessFocusView{true};
bool m_honorPremultiplyFlagOnProj0{false};
bool m_useRunningStart{true};

Expand Down
2 changes: 1 addition & 1 deletion pimax-openxr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ namespace pimax_openxr {

m_useDeferredFrameWait = getSetting("defer_frame_wait").value_or(false);

m_postProcessFocusView = getSetting("postprocess_focus_view").value_or(false);
m_postProcessFocusView = getSetting("postprocess_focus_view").value_or(true);

m_honorPremultiplyFlagOnProj0 = getSetting("honor_premultiply_flag_on_proj0").value_or(false);

Expand Down

0 comments on commit 4c45212

Please sign in to comment.