[idd] driver: implement postprocess filters
Some checks failed
build / client (Debug, map[cc:clang cxx:clang++], libdecor) (push) Has been cancelled
build / client (Debug, map[cc:clang cxx:clang++], xdg-shell) (push) Has been cancelled
build / client (Debug, map[cc:gcc cxx:g++], libdecor) (push) Has been cancelled
build / client (Debug, map[cc:gcc cxx:g++], xdg-shell) (push) Has been cancelled
build / client (Release, map[cc:clang cxx:clang++], libdecor) (push) Has been cancelled
build / client (Release, map[cc:clang cxx:clang++], xdg-shell) (push) Has been cancelled
build / client (Release, map[cc:gcc cxx:g++], libdecor) (push) Has been cancelled
build / client (Release, map[cc:gcc cxx:g++], xdg-shell) (push) Has been cancelled
build / module (push) Has been cancelled
build / host-linux (push) Has been cancelled
build / host-windows-cross (push) Has been cancelled
build / host-windows-native (push) Has been cancelled
build / idd (push) Has been cancelled
build / obs (clang) (push) Has been cancelled
build / obs (gcc) (push) Has been cancelled
build / docs (push) Has been cancelled

This commit is contained in:
Geoffrey McRae
2026-06-03 08:55:18 +10:00
committed by Geoffrey McRae
parent a59040f0be
commit 1b7c00dc82
20 changed files with 1360 additions and 69 deletions

View File

@@ -0,0 +1,165 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include "CComputeEffect.h"
#include "CDebug.h"
#include <d3dcompiler.h>
#include <cstring>
namespace PostProcessUtil
{
bool CreateDefaultTexture(const ComPtr<ID3D12Device3>& device,
const D3D12_RESOURCE_DESC& desc, ComPtr<ID3D12Resource>& resource)
{
D3D12_HEAP_PROPERTIES heapProps = {};
heapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
heapProps.CreationNodeMask = 1;
heapProps.VisibleNodeMask = 1;
HRESULT hr = device->CreateCommittedResource(
&heapProps,
D3D12_HEAP_FLAG_CREATE_NOT_ZEROED,
&desc,
D3D12_RESOURCE_STATE_COPY_SOURCE,
nullptr,
IID_PPV_ARGS(&resource));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create post-processing destination texture");
return false;
}
return true;
}
}
bool CComputeEffect::InitCompute(const ComPtr<ID3D12Device3>& device,
const D3D12_DESCRIPTOR_RANGE * ranges, UINT rangeCount,
const D3D12_STATIC_SAMPLER_DESC * samplers, UINT samplerCount,
const char * shader)
{
D3D12_ROOT_PARAMETER rootParam = {};
rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParam.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParam.DescriptorTable.NumDescriptorRanges = rangeCount;
rootParam.DescriptorTable.pDescriptorRanges = ranges;
D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDesc = {};
rootSignatureDesc.Version = D3D_ROOT_SIGNATURE_VERSION_1;
rootSignatureDesc.Desc_1_0.NumParameters = 1;
rootSignatureDesc.Desc_1_0.pParameters = &rootParam;
rootSignatureDesc.Desc_1_0.NumStaticSamplers = samplerCount;
rootSignatureDesc.Desc_1_0.pStaticSamplers = samplers;
rootSignatureDesc.Desc_1_0.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
ComPtr<ID3DBlob> blob;
ComPtr<ID3DBlob> error;
HRESULT hr = D3D12SerializeVersionedRootSignature(
&rootSignatureDesc, &blob, &error);
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to serialize post-processing root signature");
if (error)
DEBUG_ERROR("%s", (const char *)error->GetBufferPointer());
return false;
}
hr = device->CreateRootSignature(
0,
blob->GetBufferPointer(),
blob->GetBufferSize(),
IID_PPV_ARGS(&m_rootSignature));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create post-processing root signature");
return false;
}
blob.Reset();
error.Reset();
hr = D3DCompile(
shader,
std::strlen(shader),
nullptr,
nullptr,
nullptr,
"main",
"cs_5_0",
0,
0,
&blob,
&error);
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to compile post-processing shader");
if (error)
DEBUG_ERROR("%s", (const char *)error->GetBufferPointer());
return false;
}
D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = m_rootSignature.Get();
psoDesc.CS.pShaderBytecode = blob->GetBufferPointer();
psoDesc.CS.BytecodeLength = blob->GetBufferSize();
hr = device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&m_pso));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create post-processing PSO");
return false;
}
UINT descriptorCount = 0;
for (UINT i = 0; i < rangeCount; ++i)
descriptorCount += ranges[i].NumDescriptors;
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heapDesc.NumDescriptors = descriptorCount;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
hr = device->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_descHeap));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create post-processing descriptor heap");
return false;
}
return true;
}
void CComputeEffect::Bind(const ComPtr<ID3D12GraphicsCommandList>& commandList)
{
ID3D12DescriptorHeap * heaps[] = { m_descHeap.Get() };
commandList->SetDescriptorHeaps(1, heaps);
commandList->SetPipelineState(m_pso.Get());
commandList->SetComputeRootSignature(m_rootSignature.Get());
commandList->SetComputeRootDescriptorTable(
0, m_descHeap->GetGPUDescriptorHandleForHeapStart());
}
void CComputeEffect::TransitionDst(
const ComPtr<ID3D12GraphicsCommandList>& commandList,
D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after)
{
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = m_dst.Get();
barrier.Transition.StateBefore = before;
barrier.Transition.StateAfter = after;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
commandList->ResourceBarrier(1, &barrier);
}

View File

@@ -0,0 +1,52 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#pragma once
#include "../CPostProcessor.h"
#define POST_PROCESS_THREADS_STR "8"
namespace PostProcessUtil
{
static constexpr unsigned Threads = 8;
template<typename T>
static constexpr T AlignTo(T value, T align)
{
return (value + (align - 1)) & ~(align - 1);
}
bool CreateDefaultTexture(const ComPtr<ID3D12Device3>& device,
const D3D12_RESOURCE_DESC& desc, ComPtr<ID3D12Resource>& resource);
}
class CComputeEffect : public CPostProcessEffect
{
protected:
ComPtr<ID3D12RootSignature> m_rootSignature;
ComPtr<ID3D12PipelineState> m_pso;
ComPtr<ID3D12DescriptorHeap> m_descHeap;
ComPtr<ID3D12Resource> m_dst;
unsigned m_threadsX = 0;
unsigned m_threadsY = 0;
bool InitCompute(const ComPtr<ID3D12Device3>& device,
const D3D12_DESCRIPTOR_RANGE * ranges, UINT rangeCount,
const D3D12_STATIC_SAMPLER_DESC * samplers, UINT samplerCount,
const char * shader);
void Bind(const ComPtr<ID3D12GraphicsCommandList>& commandList);
void TransitionDst(const ComPtr<ID3D12GraphicsCommandList>& commandList,
D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after);
};

View File

@@ -0,0 +1,269 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include "CDownsampleEffect.h"
#include "CDebug.h"
#include "../CSettings.h"
#include <algorithm>
#include <cmath>
#include <cwchar>
#include <cwctype>
#include <cstring>
using namespace PostProcessUtil;
bool CDownsampleEffect::ParseRules(const std::wstring& value)
{
m_rules.clear();
if (value.empty())
return false;
size_t pos = 0;
while (pos < value.size())
{
size_t comma = value.find(L',', pos);
std::wstring token = value.substr(pos,
comma == std::wstring::npos ? std::wstring::npos : comma - pos);
while (!token.empty() && std::iswspace(token.front()))
token.erase(token.begin());
while (!token.empty() && std::iswspace(token.back()))
token.pop_back();
if (!token.empty())
{
Rule rule = {};
const wchar_t * start = token.c_str();
if (*start == L'>')
{
rule.greater = true;
++start;
}
if (swscanf_s(start, L"%ux%u:%ux%u",
&rule.x, &rule.y, &rule.targetX, &rule.targetY) != 4)
{
DEBUG_ERROR("Unable to parse IDD downsample rule");
m_rules.clear();
return false;
}
DEBUG_INFO("idd:downsample rule: %ux%u -> %ux%u%s",
rule.x, rule.y, rule.targetX, rule.targetY,
rule.greater ? " (greater-than)" : "");
m_rules.push_back(rule);
}
if (comma == std::wstring::npos)
break;
pos = comma + 1;
}
return !m_rules.empty();
}
const CDownsampleEffect::Rule * CDownsampleEffect::MatchRule(
unsigned width, unsigned height) const
{
const Rule * match = nullptr;
for (const auto& rule : m_rules)
if (( rule.greater && (width > rule.x || height > rule.y)) ||
(!rule.greater && (width == rule.x && height == rule.y)))
match = &rule;
return match;
}
bool CDownsampleEffect::Init(const ComPtr<ID3D12Device3>& device)
{
if (!ParseRules(g_settings.ReadStringValue(L"Downsample")))
return false;
D3D12_STATIC_SAMPLER_DESC sampler = {};
sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
sampler.MaxLOD = D3D12_FLOAT32_MAX;
sampler.ShaderRegister = 0;
sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_DESCRIPTOR_RANGE ranges[3] = {};
ranges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
ranges[0].NumDescriptors = 1;
ranges[0].BaseShaderRegister = 0;
ranges[0].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
ranges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
ranges[1].NumDescriptors = 1;
ranges[1].BaseShaderRegister = 0;
ranges[1].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
ranges[2].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
ranges[2].NumDescriptors = 1;
ranges[2].BaseShaderRegister = 0;
ranges[2].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
const char * shader =
"cbuffer Constants : register(b0)\n"
"{\n"
" float Width;\n"
" float Height;\n"
"};\n"
"Texture2D <float4> src : register(t0);\n"
"RWTexture2D<float4> dst : register(u0);\n"
"SamplerState ss : register(s0);\n"
"[numthreads(" POST_PROCESS_THREADS_STR ", " POST_PROCESS_THREADS_STR ", 1)]\n"
"void main(uint3 dt : SV_DispatchThreadID)\n"
"{\n"
" dst[dt.xy] = src.SampleLevel(ss,\n"
" float2(\n"
" (float(dt.x) + 0.5f) / Width,\n"
" (float(dt.y) + 0.5f) / Height),\n"
" 0);\n"
"}\n";
if (!InitCompute(device, ranges, ARRAYSIZE(ranges), &sampler, 1, shader))
return false;
D3D12_HEAP_PROPERTIES heapProps = {};
heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC desc = {};
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
desc.Width = AlignTo(sizeof(m_consts),
(size_t)D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
HRESULT hr = device->CreateCommittedResource(&heapProps,
D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr, IID_PPV_ARGS(&m_constBuffer));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create Downsample constant buffer");
return false;
}
return true;
}
PostProcessStatus CDownsampleEffect::SetFormat(
const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst)
{
const Rule * rule = MatchRule((unsigned)src.desc.Width, src.desc.Height);
if (!rule ||
(rule->targetX == src.desc.Width && rule->targetY == src.desc.Height))
return PostProcessStatus::BYPASS_EFFECT;
D3D12_RESOURCE_DESC desc = src.desc;
desc.Width = rule->targetX;
desc.Height = rule->targetY;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
if (!CreateDefaultTexture(device, desc, m_dst))
return PostProcessStatus::FAILED;
m_consts.width = (float)rule->targetX;
m_consts.height = (float)rule->targetY;
void * data = nullptr;
D3D12_RANGE readRange = { 0, 0 };
HRESULT hr = m_constBuffer->Map(0, &readRange, &data);
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to map Downsample constant buffer");
return PostProcessStatus::FAILED;
}
std::memcpy(data, &m_consts, sizeof(m_consts));
m_constBuffer->Unmap(0, nullptr);
m_threadsX = ((unsigned)desc.Width + (Threads - 1)) / Threads;
m_threadsY = ((unsigned)desc.Height + (Threads - 1)) / Threads;
m_format = src.desc.Format;
m_scaleX = (double)desc.Width / src.desc.Width;
m_scaleY = (double)desc.Height / src.desc.Height;
m_width = (unsigned)desc.Width;
m_height = desc.Height;
dst.desc = desc;
dst.width = (unsigned)desc.Width;
dst.height = desc.Height;
return PostProcessStatus::SUCCESS;
}
void CDownsampleEffect::AdjustDamage(RECT dirtyRects[], unsigned * nbDirtyRects)
{
for (RECT * rect = dirtyRects; rect < dirtyRects + *nbDirtyRects; ++rect)
{
unsigned width = (unsigned)std::ceil((double)(rect->right - rect->left) * m_scaleX);
unsigned height = (unsigned)std::ceil((double)(rect->bottom - rect->top ) * m_scaleY);
rect->left = (LONG)max(0.0, std::floor((double)rect->left * m_scaleX));
rect->right = (LONG)min((double)m_width , (double)rect->left + width);
rect->top = (LONG)max(0.0, std::floor((double)rect->top * m_scaleY));
rect->bottom = (LONG)min((double)m_height, (double)rect->top + height);
if (rect->left > 0 ) rect->left -= 1;
if (rect->top > 0 ) rect->top -= 1;
if (rect->right < (LONG)m_width ) rect->right += 1;
if (rect->bottom < (LONG)m_height ) rect->bottom += 1;
}
}
ComPtr<ID3D12Resource> CDownsampleEffect::Run(
const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects)
{
UNREFERENCED_PARAMETER(dirtyRects);
UNREFERENCED_PARAMETER(nbDirtyRects);
TransitionDst(commandList, D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
D3D12_CPU_DESCRIPTOR_HANDLE handle =
m_descHeap->GetCPUDescriptorHandleForHeapStart();
const UINT inc = device->GetDescriptorHandleIncrementSize(
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.BufferLocation = m_constBuffer->GetGPUVirtualAddress();
cbvDesc.SizeInBytes = (UINT)AlignTo(sizeof(m_consts),
(size_t)D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
device->CreateConstantBufferView(&cbvDesc, handle);
handle.ptr += inc;
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = m_format;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Texture2D.MipLevels = 1;
device->CreateShaderResourceView(src.Get(), &srvDesc, handle);
handle.ptr += inc;
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = m_format;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
device->CreateUnorderedAccessView(m_dst.Get(), nullptr, &uavDesc, handle);
Bind(commandList);
commandList->Dispatch(m_threadsX, m_threadsY, 1);
TransitionDst(commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_COPY_SOURCE);
return m_dst;
}

View File

@@ -0,0 +1,62 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#pragma once
#include "CComputeEffect.h"
#include <vector>
#include <string>
class CDownsampleEffect : public CComputeEffect
{
private:
struct Rule
{
bool greater = false;
unsigned x = 0;
unsigned y = 0;
unsigned targetX = 0;
unsigned targetY = 0;
};
struct Consts
{
float width;
float height;
} m_consts = {};
std::vector<Rule> m_rules;
ComPtr<ID3D12Resource> m_constBuffer;
DXGI_FORMAT m_format = DXGI_FORMAT_UNKNOWN;
double m_scaleX = 1.0;
double m_scaleY = 1.0;
unsigned m_width = 0;
unsigned m_height = 0;
bool ParseRules(const std::wstring& value);
const Rule * MatchRule(unsigned width, unsigned height) const;
public:
const char * GetName() const override { return "Downsample"; }
bool Init(const ComPtr<ID3D12Device3>& device);
PostProcessStatus SetFormat(const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst) override;
void AdjustDamage(RECT dirtyRects[], unsigned * nbDirtyRects) override;
ComPtr<ID3D12Resource> Run(const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects) override;
};

View File

@@ -0,0 +1,152 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include "CHDR16to10Effect.h"
#include "CDebug.h"
#include <cstring>
using namespace PostProcessUtil;
bool CHDR16to10Effect::Init(const ComPtr<ID3D12Device3>& device)
{
D3D12_DESCRIPTOR_RANGE ranges[3] = {};
ranges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
ranges[0].NumDescriptors = 1;
ranges[0].BaseShaderRegister = 0;
ranges[0].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
ranges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
ranges[1].NumDescriptors = 1;
ranges[1].BaseShaderRegister = 0;
ranges[1].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
ranges[2].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
ranges[2].NumDescriptors = 1;
ranges[2].BaseShaderRegister = 0;
ranges[2].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
const char * shader =
"cbuffer Constants : register(b0)\n"
"{\n"
" float SDRWhiteLevel;\n"
"};\n"
"Texture2D<float4> src : register(t0);\n"
"RWTexture2D<float4> dst : register(u0);\n"
"[numthreads(" POST_PROCESS_THREADS_STR ", " POST_PROCESS_THREADS_STR ", 1)]\n"
"void main(uint3 dt : SV_DispatchThreadID)\n"
"{\n"
" dst[dt.xy] = float4(src[dt.xy].rgb * SDRWhiteLevel, src[dt.xy].a);\n"
"}\n";
if (!InitCompute(device, ranges, ARRAYSIZE(ranges), nullptr, 0, shader))
return false;
D3D12_HEAP_PROPERTIES heapProps = {};
heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC desc = {};
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
desc.Width = AlignTo(sizeof(m_consts),
(size_t)D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
HRESULT hr = device->CreateCommittedResource(&heapProps,
D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr, IID_PPV_ARGS(&m_constBuffer));
if (FAILED(hr))
{
DEBUG_ERROR_HR(hr, "Failed to create HDR16to10 constant buffer");
return false;
}
void * data = nullptr;
D3D12_RANGE readRange = { 0, 0 };
hr = m_constBuffer->Map(0, &readRange, &data);
if (FAILED(hr))
return false;
std::memcpy(data, &m_consts, sizeof(m_consts));
m_constBuffer->Unmap(0, nullptr);
return true;
}
PostProcessStatus CHDR16to10Effect::SetFormat(
const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst)
{
if (src.desc.Format != DXGI_FORMAT_R16G16B16A16_FLOAT || !src.hdr)
return PostProcessStatus::BYPASS_EFFECT;
D3D12_RESOURCE_DESC desc = src.desc;
desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
if (!CreateDefaultTexture(device, desc, m_dst))
return PostProcessStatus::FAILED;
m_threadsX = ((unsigned)desc.Width + (Threads - 1)) / Threads;
m_threadsY = ((unsigned)desc.Height + (Threads - 1)) / Threads;
dst.desc = desc;
dst.format = FRAME_TYPE_RGBA10;
dst.hdr = true;
dst.hdrPQ = false;
return PostProcessStatus::SUCCESS;
}
ComPtr<ID3D12Resource> CHDR16to10Effect::Run(
const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects)
{
UNREFERENCED_PARAMETER(dirtyRects);
UNREFERENCED_PARAMETER(nbDirtyRects);
TransitionDst(commandList, D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
D3D12_CPU_DESCRIPTOR_HANDLE handle =
m_descHeap->GetCPUDescriptorHandleForHeapStart();
const UINT inc = device->GetDescriptorHandleIncrementSize(
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.BufferLocation = m_constBuffer->GetGPUVirtualAddress();
cbvDesc.SizeInBytes = (UINT)AlignTo(sizeof(m_consts),
(size_t)D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
device->CreateConstantBufferView(&cbvDesc, handle);
handle.ptr += inc;
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Texture2D.MipLevels = 1;
device->CreateShaderResourceView(src.Get(), &srvDesc, handle);
handle.ptr += inc;
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
device->CreateUnorderedAccessView(m_dst.Get(), nullptr, &uavDesc, handle);
Bind(commandList);
commandList->Dispatch(m_threadsX, m_threadsY, 1);
TransitionDst(commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_COPY_SOURCE);
return m_dst;
}

View File

@@ -0,0 +1,37 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#pragma once
#include "CComputeEffect.h"
class CHDR16to10Effect : public CComputeEffect
{
private:
struct Consts
{
float SDRWhiteLevel;
} m_consts = { 1.0f };
ComPtr<ID3D12Resource> m_constBuffer;
public:
const char * GetName() const override { return "HDR16to10"; }
bool Init(const ComPtr<ID3D12Device3>& device);
PostProcessStatus SetFormat(const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst) override;
ComPtr<ID3D12Resource> Run(const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects) override;
};

View File

@@ -0,0 +1,123 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include "CRGB24Effect.h"
#include "../CSettings.h"
using namespace PostProcessUtil;
bool CRGB24Effect::Init(const ComPtr<ID3D12Device3>& device)
{
if (!g_settings.ReadBoolValue(L"AllowRGB24", false))
return false;
D3D12_DESCRIPTOR_RANGE ranges[2] = {};
ranges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
ranges[0].NumDescriptors = 1;
ranges[0].BaseShaderRegister = 0;
ranges[0].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
ranges[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
ranges[1].NumDescriptors = 1;
ranges[1].BaseShaderRegister = 0;
ranges[1].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
const char * shader =
"Texture2D <float4> src : register(t0);\n"
"RWTexture2D<float4> dst : register(u0);\n"
"[numthreads(" POST_PROCESS_THREADS_STR ", " POST_PROCESS_THREADS_STR ", 1)]\n"
"void main(uint3 dt : SV_DispatchThreadID)\n"
"{\n"
" uint fstInputX = (dt.x * 4) / 3;\n"
" float4 color0 = src[uint2(fstInputX, dt.y)];\n"
" uint sndInputX = fstInputX + 1;\n"
" float4 color3 = src[uint2(sndInputX, dt.y)];\n"
" uint xmod3 = dt.x % 3;\n"
" float4 color1 = xmod3 <= 1 ? color0 : color3;\n"
" float4 color2 = xmod3 == 0 ? color0 : color3;\n"
" float b = color0.bgr[xmod3];\n"
" float g = color1.grb[xmod3];\n"
" float r = color2.rbg[xmod3];\n"
" float a = color3.bgr[xmod3];\n"
" dst[dt.xy] = float4(r, g, b, a);\n"
"}\n";
return InitCompute(device, ranges, ARRAYSIZE(ranges), nullptr, 0, shader);
}
PostProcessStatus CRGB24Effect::SetFormat(const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst)
{
if (src.desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM)
return PostProcessStatus::BYPASS_EFFECT;
const unsigned packedPitch = AlignTo((unsigned)src.desc.Width * 3, 4u);
D3D12_RESOURCE_DESC desc = src.desc;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.Width = AlignTo(packedPitch / 4, 64u);
desc.Height = ((unsigned)src.desc.Width * src.desc.Height) / (packedPitch / 3);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
if (!CreateDefaultTexture(device, desc, m_dst))
return PostProcessStatus::FAILED;
m_threadsX = ((unsigned)desc.Width + (Threads - 1)) / Threads;
m_threadsY = ((unsigned)desc.Height + (Threads - 1)) / Threads;
dst.desc = desc;
dst.format = FRAME_TYPE_BGR_32;
return PostProcessStatus::SUCCESS;
}
ComPtr<ID3D12Resource> CRGB24Effect::Run(const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects)
{
UNREFERENCED_PARAMETER(dirtyRects);
UNREFERENCED_PARAMETER(nbDirtyRects);
TransitionDst(commandList, D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
D3D12_CPU_DESCRIPTOR_HANDLE handle =
m_descHeap->GetCPUDescriptorHandleForHeapStart();
const UINT inc = device->GetDescriptorHandleIncrementSize(
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Texture2D.MipLevels = 1;
device->CreateShaderResourceView(src.Get(), &srvDesc, handle);
handle.ptr += inc;
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
device->CreateUnorderedAccessView(m_dst.Get(), nullptr, &uavDesc, handle);
Bind(commandList);
commandList->Dispatch(m_threadsX, m_threadsY, 1);
TransitionDst(commandList, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_STATE_COPY_SOURCE);
for (RECT * rect = dirtyRects; rect < dirtyRects + *nbDirtyRects; ++rect)
{
unsigned width = rect->right - rect->left;
rect->left = (rect->left * 3) / 4;
rect->right = rect->left + (width * 3 + 3) / 4;
}
return m_dst;
}

View File

@@ -0,0 +1,30 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#pragma once
#include "CComputeEffect.h"
class CRGB24Effect : public CComputeEffect
{
public:
const char * GetName() const override { return "RGB24"; }
bool Init(const ComPtr<ID3D12Device3>& device);
PostProcessStatus SetFormat(const ComPtr<ID3D12Device3>& device,
const D12FrameFormat& src, D12FrameFormat& dst) override;
ComPtr<ID3D12Resource> Run(const ComPtr<ID3D12Device3>& device,
const ComPtr<ID3D12GraphicsCommandList>& commandList,
const ComPtr<ID3D12Resource>& src, RECT dirtyRects[],
unsigned * nbDirtyRects) override;
};