Skip to content

Commit

Permalink
When snipping, limit the maximum tonemap light level to display's lim…
Browse files Browse the repository at this point in the history
…it if the content light level exceeds it.
  • Loading branch information
Kaldaien committed Jul 22, 2024
1 parent 5d79b33 commit fd47717
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
28 changes: 15 additions & 13 deletions include/utility/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,18 @@ HRESULT SKIV_Image_SaveToDisk_HDR (const DirectX::Image& image, const wchar_t*
HRESULT SKIV_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileName, bool force_sRGB);
HRESULT SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, POINT pos, int flags = 0x0);
void SKIV_Image_CaptureRegion (ImRect capture_area);
HRESULT SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& final_sdr);
HRESULT SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& final_sdr, float mastering_max_nits);

// Structs

struct skiv_image_desktop_s {
CComPtr <ID3D11ShaderResourceView> _srv = nullptr;
CComPtr <ID3D11Resource> _res = nullptr;
bool _hdr_image = false;
ImVec2 _resolution = ImVec2 (0.0f, 0.0f);
ImVec2 _desktop_pos = ImVec2 (0.0f, 0.0f);
DXGI_MODE_ROTATION _rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
CComPtr <ID3D11ShaderResourceView> _srv = nullptr;
CComPtr <ID3D11Resource> _res = nullptr;
bool _hdr_image = false;
ImVec2 _resolution = ImVec2 (0.0f, 0.0f);
ImVec2 _desktop_pos = ImVec2 (0.0f, 0.0f);
DXGI_MODE_ROTATION _rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
float _max_display_nits = 1000.0f;

bool process (void)
{
Expand Down Expand Up @@ -180,11 +181,12 @@ struct skiv_image_desktop_s {

void clear (void)
{
_res = nullptr;
_srv = nullptr;
_hdr_image = false;
_resolution = ImVec2 (0.0f, 0.0f);
_desktop_pos = ImVec2 (0.0f, 0.0f);
_rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
_res = nullptr;
_srv = nullptr;
_hdr_image = false;
_resolution = ImVec2 (0.0f, 0.0f);
_desktop_pos = ImVec2 (0.0f, 0.0f);
_max_display_nits = 1000.0f;
_rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
}
};
7 changes: 2 additions & 5 deletions src/SKIV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,9 +2015,6 @@ wWinMain ( _In_ HINSTANCE hInstance,

auto _IgnoreWindow = [&](HWND hWnd) -> bool
{
if (hWnd == SKIF_ImGui_hWnd)
return true;

if (ignoredWindows.count (hWnd))
return true;

Expand All @@ -2038,8 +2035,8 @@ wWinMain ( _In_ HINSTANCE hInstance,
|| ! IsWindowVisible (hWnd)
|| (RealGetWindowClassW (hWnd, wszWindowTextBuffer, 64) && ignoreClassNames.count (wszWindowTextBuffer))
|| ! GetWindowTextW (hWnd, wszWindowTextBuffer, 64)
//|| (GetWindowLongPtr (hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW)
//|| (GetWindowLongPtr (hWnd, GWL_STYLE) & WS_POPUP) // Ignores some games, but allows some invisible stupid windows (UWP)
|| (GetWindowLongPtr (hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW)
//|| (GetWindowLongPtr (hWnd, GWL_STYLE) & WS_POPUP) // Ignores some games, but allows some invisible stupid windows (UWP)
)
{
ignoredWindows.insert (hWnd);
Expand Down
25 changes: 18 additions & 7 deletions src/utility/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include <ShlObj_core.h>
#include <utility/fsutil.h>
#include <dxgi1_5.h>
#include <dxgi1_6.h>
#include <Shlwapi.h>
#include <windowsx.h>
#include <ImGuiNotify.hpp>
#include <utility/skif_imgui.h>
#include <utility/utility.h>

skiv_image_desktop_s SKIV_DesktopImage;

extern std::wstring defaultHDRFileExt;
extern std::wstring defaultSDRFileExt;
extern CComPtr <ID3D11Device> SKIF_D3D11_GetDevice (bool bWait = true);
Expand Down Expand Up @@ -988,7 +991,7 @@ bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, boo
DirectX::ScratchImage tonemapped_sdr;
if (_registry._SnippingTonemapsHDR && isHDR)
{
if (SUCCEEDED (SKIV_Image_TonemapToSDR (*pImage, tonemapped_sdr)))
if (SUCCEEDED (SKIV_Image_TonemapToSDR (*pImage, tonemapped_sdr, SKIV_DesktopImage._max_display_nits)))
{
pImage = tonemapped_sdr.GetImage (0,0,0);
}
Expand Down Expand Up @@ -1097,7 +1100,7 @@ bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, boo
}

HRESULT
SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& final_sdr)
SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& final_sdr, float mastering_max_nits)
{
using namespace DirectX;

Expand Down Expand Up @@ -1168,7 +1171,8 @@ SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& fin
XMVECTOR maxTonemappedRGB = g_XMZero;

// If it's too bright, don't bother trying to tonemap the full range...
static constexpr float _maxNitsToTonemap = 10000.0f/80.0f;
const float _maxNitsToTonemap = (mastering_max_nits != 0.0f ? mastering_max_nits
: 1000.0f) / 80.0f;

const float maxYInPQ =
SKIV_Image_LinearToPQY (std::min (_maxNitsToTonemap, XMVectorGetY (maxLum))),
Expand All @@ -1184,7 +1188,7 @@ SKIV_Image_TonemapToSDR (const DirectX::Image& image, DirectX::ScratchImage& fin

auto TonemapHDR = [](float L, float Lc, float Ld) -> float
{
float a = ( Ld / pow (Lc, 2.0f));
float a = ( Ld / pow (Lc, 2.15f));
float b = (1.0f / Ld);

return
Expand Down Expand Up @@ -1392,7 +1396,7 @@ SKIV_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileNa

auto TonemapHDR = [](float L, float Lc, float Ld) -> float
{
float a = ( Ld / pow (Lc, 2.0f));
float a = ( Ld / pow (Lc, 2.15f));
float b = (1.0f / Ld);

return
Expand Down Expand Up @@ -1641,8 +1645,6 @@ SKIV_Image_SaveToDisk_HDR (const DirectX::Image& image, const wchar_t* wszFileNa
wszImplicitFileName, nullptr, SK_WIC_SetMaximumQuality);
}

skiv_image_desktop_s SKIV_DesktopImage;

HRESULT
SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, POINT point, int flags)
{
Expand Down Expand Up @@ -1670,6 +1672,7 @@ SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, POINT point, int flags)

CComPtr <IDXGIOutput> pCursorOutput;
DXGI_OUTPUT_DESC out_desc = {};
DXGI_OUTPUT_DESC1 out_desc1= {};

while (SUCCEEDED (pFactory->EnumAdapters (uiAdapter++, &pAdapter.p)))
{
Expand All @@ -1680,6 +1683,13 @@ SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, POINT point, int flags)
{
pOutput->GetDesc (&out_desc);

CComQIPtr <IDXGIOutput6>
pOutput6 (pOutput);
if (pOutput6.p != nullptr)
{
pOutput6->GetDesc1 (&out_desc1);
}

if (out_desc.AttachedToDesktop && PtInRect (&out_desc.DesktopCoordinates, point))
{
pCursorOutput = pOutput;
Expand Down Expand Up @@ -1860,6 +1870,7 @@ SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, POINT point, int flags)
SKIV_DesktopImage._desktop_pos =
ImVec2 (static_cast <float> (out_desc.DesktopCoordinates.left),
static_cast <float> (out_desc.DesktopCoordinates.top));
SKIV_DesktopImage._max_display_nits = out_desc1.MaxLuminance;

pDevCtx->Flush ();

Expand Down

0 comments on commit fd47717

Please sign in to comment.