forked from ChasBoyd/DisplayHDRTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundNoiseEffect.cpp
329 lines (282 loc) · 9.75 KB
/
BackgroundNoiseEffect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "pch.h"
#include <initguid.h>
#include "BackgroundNoiseEffect.h"
#define XML(X) TEXT(#X)
BackgroundNoiseEffect::BackgroundNoiseEffect() :
m_refCount(1)
{
m_constants.APL = 0.1f;
m_constants.Clamp = 1000.0f; // Limit is high by default
m_constants.iTime = 1.0f;
}
HRESULT __stdcall BackgroundNoiseEffect::CreateBackgroundNoiseImpl(_Outptr_ IUnknown** ppEffectImpl)
{
// Since the object's refcount is initialized to 1, we don't need to AddRef here.
*ppEffectImpl = static_cast<ID2D1EffectImpl*>(new (std::nothrow) BackgroundNoiseEffect());
if (*ppEffectImpl == nullptr)
{
return E_OUTOFMEMORY;
}
else
{
return S_OK;
}
}
HRESULT BackgroundNoiseEffect::Register(_In_ ID2D1Factory1* pFactory)
{
// The inspectable metadata of an effect is defined in XML. This can be passed in from an external source
// as well, however for simplicity we just inline the XML.
PCWSTR pszXml =
XML(
<?xml version='1.0'?>
<Effect>
<!-- System Properties -->
<Property name='DisplayName' type='string' value='BackgroundNoise'/>
<Property name='Author' type='string' value='VESA'/>
<Property name='Category' type='string' value='Source'/>
<Property name='Description' type='string' value='Generates pixel values with a given probability distribution'/>
<Inputs />
<!-- Custom Properties go here -->
<Property name = 'APL' type = 'float'>
<Property name = 'DisplayName' type = 'string' value = 'Average Picture Level in nits'/>
<Property name = 'Min' type = 'float' value = '0.01'/>
<Property name = 'Max' type = 'float' value = '10.00'/>
<Property name = 'Default' type = 'float' value = '0.10'/>
</Property>
<Property name = 'Clamp' type = 'float'>
<Property name = 'DisplayName' type = 'string' value = 'No pixels can be above this limit (nits)'/>
<Property name = 'Min' type = 'float' value = '0.1'/>
<Property name = 'Max' type = 'float' value = '1000.0'/>
<Property name = 'Default' type = 'float' value = '10.0'/>
</Property>
<Property name = 'iTime' type = 'float'>
<Property name = 'DisplayName' type = 'string' value = 'Time since app start (sec)'/>
<Property name = 'Min' type = 'float' value = '0.0'/>
<Property name = 'Max' type = 'float' value = '1000000.0'/>
<Property name = 'Default' type = 'float' value = '1.0'/>
</Property>
</Effect>
);
const D2D1_PROPERTY_BINDING bindings[] =
{
D2D1_VALUE_TYPE_BINDING(L"APL", &SetAPL, &GetAPL),
D2D1_VALUE_TYPE_BINDING(L"Clamp", &SetClamp, &GetClamp),
D2D1_VALUE_TYPE_BINDING(L"iTime", &SetiTime, &GetiTime),
};
// This registers the effect with the factory, which will make the effect
// instantiatable.
return pFactory->RegisterEffectFromString(
CLSID_CustomBackgroundNoiseEffect,
pszXml,
bindings,
ARRAYSIZE(bindings),
CreateBackgroundNoiseImpl
);
}
IFACEMETHODIMP BackgroundNoiseEffect::Initialize(
_In_ ID2D1EffectContext* pEffectContext,
_In_ ID2D1TransformGraph* pTransformGraph
)
{
// To maintain consistency across different DPIs, this effect needs to cover more pixels at
// higher than normal DPIs. The context is saved here so the effect can later retrieve the DPI.
m_effectContext = pEffectContext;
byte* data = nullptr;
UINT size = 0;
HRESULT hr = S_OK;
try
{
DX::ReadDataFromFile(L"BackgroundNoiseEffect.cso", &data, &size);
}
catch (std::exception)
{
// Most likely is caused by a missing or invalid file.
hr = D2DERR_FILE_NOT_FOUND;
}
if (SUCCEEDED(hr))
{
hr = pEffectContext->LoadPixelShader(GUID_BackgroundNoisePixelShader, data, size);
}
// This loads the shader into the Direct2D image effects system and associates it with the GUID passed in.
// If this method is called more than once (say by other instances of the effect) with the same GUID,
// the system will simply do nothing, ensuring that only one instance of a shader is stored regardless of how
// many time it is used.
if (SUCCEEDED(hr))
{
// The graph consists of a single transform. In fact, this class is the transform,
// reducing the complexity of implementing an effect when all we need to
// do is use a single pixel shader.
hr = pTransformGraph->SetSingleTransformNode(this);
}
return hr;
}
HRESULT BackgroundNoiseEffect::UpdateConstants()
{
// Update the DPI if it has changed. This allows the effect to scale across different DPIs automatically.
m_effectContext->GetDpi(&m_dpi, &m_dpi);
m_constants.dpi = m_dpi;
return m_drawInfo->SetPixelShaderConstantBuffer(reinterpret_cast<BYTE*>(&m_constants), sizeof(m_constants));
}
IFACEMETHODIMP BackgroundNoiseEffect::PrepareForRender(D2D1_CHANGE_TYPE changeType)
{
return UpdateConstants();
}
// SetGraph is only called when the number of inputs changes. This never happens as we publish this effect
// as a single input effect.
IFACEMETHODIMP BackgroundNoiseEffect::SetGraph(_In_ ID2D1TransformGraph* pGraph)
{
return E_NOTIMPL;
}
// Called to assign a new render info class, which is used to inform D2D on
// how to set the state of the GPU.
IFACEMETHODIMP BackgroundNoiseEffect::SetDrawInfo(_In_ ID2D1DrawInfo* pDrawInfo)
{
m_drawInfo = pDrawInfo;
return m_drawInfo->SetPixelShader(GUID_BackgroundNoisePixelShader);
}
// Calculates the mapping between the output and input rects.
IFACEMETHODIMP BackgroundNoiseEffect::MapOutputRectToInputRects(
_In_ const D2D1_RECT_L* pOutputRect,
_Out_writes_(inputRectCount) D2D1_RECT_L* pInputRects,
UINT32 inputRectCount
) const
{
// This effect has no inputs.
if (inputRectCount != 0)
{
return E_INVALIDARG;
}
// There should never be any inputs to map to.
return S_OK;
}
IFACEMETHODIMP BackgroundNoiseEffect::MapInputRectsToOutputRect(
_In_reads_(inputRectCount) CONST D2D1_RECT_L* pInputRects,
_In_reads_(inputRectCount) CONST D2D1_RECT_L* pInputOpaqueSubRects,
UINT32 inputRectCount,
_Out_ D2D1_RECT_L* pOutputRect,
_Out_ D2D1_RECT_L* pOutputOpaqueSubRect
)
{
// This effect has no inputs.
if (inputRectCount != 0)
{
return E_INVALIDARG;
}
// The output is infinite and always opaque.
*pOutputRect = { LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX };
*pOutputOpaqueSubRect = { LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX };
return S_OK;
}
IFACEMETHODIMP BackgroundNoiseEffect::MapInvalidRect(
UINT32 inputIndex,
D2D1_RECT_L invalidInputRect,
_Out_ D2D1_RECT_L* pInvalidOutputRect
) const
{
HRESULT hr = S_OK;
// Indicate that the entire output may be invalid.
*pInvalidOutputRect = m_inputRect;
return hr;
}
IFACEMETHODIMP_(UINT32) BackgroundNoiseEffect::GetInputCount() const
{
return 0;
}
// D2D ensures that that effects are only referenced from one thread at a time.
// To improve performance, we simply increment/decrement our reference count
// rather than use atomic InterlockedIncrement()/InterlockedDecrement() functions.
IFACEMETHODIMP_(ULONG) BackgroundNoiseEffect::AddRef()
{
m_refCount++;
return m_refCount;
}
IFACEMETHODIMP_(ULONG) BackgroundNoiseEffect::Release()
{
m_refCount--;
if (m_refCount == 0)
{
delete this;
return 0;
}
else
{
return m_refCount;
}
}
// This enables the stack of parent interfaces to be queried. In the instance
// of the BackgroundNoise interface, this method simply enables the developer
// to cast a BackgroundNoise instance to an ID2D1EffectImpl or IUnknown instance.
IFACEMETHODIMP BackgroundNoiseEffect::QueryInterface(
_In_ REFIID riid,
_Outptr_ void** ppOutput
)
{
*ppOutput = nullptr;
HRESULT hr = S_OK;
if (riid == __uuidof(ID2D1EffectImpl))
{
*ppOutput = reinterpret_cast<ID2D1EffectImpl*>(this);
}
else if (riid == __uuidof(ID2D1DrawTransform))
{
*ppOutput = static_cast<ID2D1DrawTransform*>(this);
}
else if (riid == __uuidof(ID2D1Transform))
{
*ppOutput = static_cast<ID2D1Transform*>(this);
}
else if (riid == __uuidof(ID2D1TransformNode))
{
*ppOutput = static_cast<ID2D1TransformNode*>(this);
}
else if (riid == __uuidof(IUnknown))
{
*ppOutput = this;
}
else
{
hr = E_NOINTERFACE;
}
if (*ppOutput != nullptr)
{
AddRef();
}
return hr;
}
HRESULT BackgroundNoiseEffect::SetAPL( float apl )
{
m_constants.APL = apl;
return S_OK;
}
float BackgroundNoiseEffect::GetAPL() const
{
return m_constants.APL;
}
HRESULT BackgroundNoiseEffect::SetClamp(float lim )
{
m_constants.Clamp = lim;
return S_OK;
}
float BackgroundNoiseEffect::GetClamp() const
{
return m_constants.Clamp;
}
HRESULT BackgroundNoiseEffect::SetiTime(float t)
{
m_constants.iTime = t;
return S_OK;
}
float BackgroundNoiseEffect::GetiTime() const
{
return m_constants.iTime;
}