-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
WinVideoFilter.cpp
918 lines (782 loc) · 27.8 KB
/
WinVideoFilter.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
* Copyright (C) 2007-2013 Team XBMC
* http://xbmc.org
*
* 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, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAS_DX
#include "WinVideoFilter.h"
#include "ConvolutionKernels.h"
#include <DirectXPackedVector.h>
#include "FileSystem/File.h"
#include "guilib/GraphicContext.h"
#include "Util.h"
#include "utils/log.h"
#include "win32/WIN32Util.h"
#include "windowing/WindowingFactory.h"
#include "YUV2RGBShader.h"
#include <map>
using namespace DirectX::PackedVector;
CYUV2RGBMatrix::CYUV2RGBMatrix()
{
m_NeedRecalc = true;
m_blacklevel = 0.0f;
m_contrast = 0.0f;
m_flags = 0;
m_format = RENDER_FMT_NONE;
}
void CYUV2RGBMatrix::SetParameters(float contrast, float blacklevel, unsigned int flags, ERenderFormat format)
{
if (m_contrast != contrast)
{
m_NeedRecalc = true;
m_contrast = contrast;
}
if (m_blacklevel != blacklevel)
{
m_NeedRecalc = true;
m_blacklevel = blacklevel;
}
if (m_flags != flags)
{
m_NeedRecalc = true;
m_flags = flags;
}
if (m_format != format)
{
m_NeedRecalc = true;
m_format = format;
}
}
XMFLOAT4X4* CYUV2RGBMatrix::Matrix()
{
if (m_NeedRecalc)
{
TransformMatrix matrix;
CalculateYUVMatrix(matrix, m_flags, m_format, m_blacklevel, m_contrast);
m_mat._11 = matrix.m[0][0];
m_mat._12 = matrix.m[1][0];
m_mat._13 = matrix.m[2][0];
m_mat._14 = 0.0f;
m_mat._21 = matrix.m[0][1];
m_mat._22 = matrix.m[1][1];
m_mat._23 = matrix.m[2][1];
m_mat._24 = 0.0f;
m_mat._31 = matrix.m[0][2];
m_mat._32 = matrix.m[1][2];
m_mat._33 = matrix.m[2][2];
m_mat._34 = 0.0f;
m_mat._41 = matrix.m[0][3];
m_mat._42 = matrix.m[1][3];
m_mat._43 = matrix.m[2][3];
m_mat._44 = 1.0f;
m_NeedRecalc = false;
}
return &m_mat;
}
//===================================================================
CWinShader::~CWinShader()
{
if (m_effect.Get())
m_effect.Release();
if (m_vb.Get())
m_vb.Release();
if (m_ib.Get())
m_ib.Release();
SAFE_RELEASE(m_inputLayout);
}
bool CWinShader::CreateVertexBuffer(unsigned int vertCount, unsigned int vertSize)
{
if (!m_vb.Create(D3D11_BIND_VERTEX_BUFFER, vertCount, vertSize, DXGI_FORMAT_UNKNOWN, D3D11_USAGE_DYNAMIC))
return false;
uint16_t id[4] = { 3, 0, 2, 1 };
if (!m_ib.Create(D3D11_BIND_INDEX_BUFFER, ARRAYSIZE(id), sizeof(uint16_t), DXGI_FORMAT_R16_UINT, D3D11_USAGE_IMMUTABLE, id))
return false;
m_vbsize = vertCount * vertSize;
m_vertsize = vertSize;
return true;
}
bool CWinShader::CreateInputLayout(D3D11_INPUT_ELEMENT_DESC *layout, unsigned numElements)
{
D3DX11_PASS_DESC desc = {};
if (FAILED(m_effect.Get()->GetTechniqueByIndex(0)->GetPassByIndex(0)->GetDesc(&desc)))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to get first pass description.");
return false;
}
return S_OK == g_Windowing.Get3D11Device()->CreateInputLayout(layout, numElements, desc.pIAInputSignature, desc.IAInputSignatureSize, &m_inputLayout);
}
bool CWinShader::LockVertexBuffer(void **data)
{
if (!m_vb.Map(data))
{
CLog::Log(LOGERROR, __FUNCTION__" - failed to lock vertex buffer");
return false;
}
return true;
}
bool CWinShader::UnlockVertexBuffer()
{
if (!m_vb.Unmap())
{
CLog::Log(LOGERROR, __FUNCTION__" - failed to unlock vertex buffer");
return false;
}
return true;
}
bool CWinShader::LoadEffect(const std::string& filename, DefinesMap* defines)
{
CLog::Log(LOGDEBUG, __FUNCTION__" - loading shader %s", filename.c_str());
XFILE::CFileStream file;
if(!file.Open(filename))
{
CLog::Log(LOGERROR, __FUNCTION__" - failed to open file %s", filename.c_str());
return false;
}
std::string pStrEffect;
getline(file, pStrEffect, '\0');
if (!m_effect.Create(pStrEffect, defines))
{
CLog::Log(LOGERROR, __FUNCTION__" %s failed", pStrEffect.c_str());
return false;
}
return true;
}
bool CWinShader::Execute(std::vector<ID3D11RenderTargetView*> *vecRT, unsigned int vertexIndexStep)
{
ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context();
ID3D11RenderTargetView* oldRT = nullptr;
// The render target will be overriden: save the caller's original RT
if (vecRT != nullptr && !vecRT->empty())
pContext->OMGetRenderTargets(1, &oldRT, nullptr);
UINT cPasses, iPass;
if (!m_effect.Begin(&cPasses, 0))
{
CLog::Log(LOGERROR, __FUNCTION__" - failed to begin d3d effect");
return false;
}
ID3D11Buffer* vertexBuffer = m_vb.Get();
ID3D11Buffer* indexBuffer = m_ib.Get();
unsigned int stride = m_vb.GetStride();
unsigned int offset = 0;
pContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
pContext->IASetIndexBuffer(indexBuffer, m_ib.GetFormat(), 0);
pContext->IASetInputLayout(m_inputLayout);
pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
for (iPass = 0; iPass < cPasses; iPass++)
{
if (vecRT != nullptr && vecRT->size() > iPass)
pContext->OMSetRenderTargets(1, &vecRT->at(iPass), nullptr);
SetStepParams(iPass);
if (!m_effect.BeginPass(iPass))
{
CLog::Log(LOGERROR, __FUNCTION__" - failed to begin d3d effect pass");
break;
}
pContext->DrawIndexed(4, 0, iPass * vertexIndexStep);
if (!m_effect.EndPass())
CLog::Log(LOGERROR, __FUNCTION__" - failed to end d3d effect pass");
CD3DHelper::PSClearShaderResources(pContext);
}
if (!m_effect.End())
CLog::Log(LOGERROR, __FUNCTION__" - failed to end d3d effect");
if (oldRT != nullptr)
{
pContext->OMSetRenderTargets(1, &oldRT, nullptr);
oldRT->Release();
}
return true;
}
//==================================================================================
bool CYUV2RGBShader::Create(unsigned int sourceWidth, unsigned int sourceHeight, ERenderFormat fmt)
{
CWinShader::CreateVertexBuffer(4, sizeof(CUSTOMVERTEX));
m_sourceWidth = sourceWidth;
m_sourceHeight = sourceHeight;
m_format = fmt;
unsigned int texWidth;
DefinesMap defines;
switch (fmt)
{
case RENDER_FMT_YUV420P:
case RENDER_FMT_YUV420P10:
case RENDER_FMT_YUV420P16:
defines["XBMC_YV12"] = "";
texWidth = sourceWidth;
break;
case RENDER_FMT_NV12:
defines["XBMC_NV12"] = "";
texWidth = sourceWidth;
// FL 9.x doesn't support DXGI_FORMAT_R8G8_UNORM, so we have to use SNORM and correct values in shader
if (!g_Windowing.IsFormatSupport(DXGI_FORMAT_R8G8_UNORM, D3D11_FORMAT_SUPPORT_TEXTURE2D))
defines["NV12_SNORM_UV"] = "";
break;
case RENDER_FMT_UYVY422:
defines["XBMC_UYVY"] = "";
texWidth = sourceWidth >> 1;
break;
case RENDER_FMT_YUYV422:
defines["XBMC_YUY2"] = "";
texWidth = sourceWidth >> 1;
break;
default:
return false;
break;
}
m_texSteps[0] = 1.0f/(float)texWidth;
m_texSteps[1] = 1.0f/(float)sourceHeight;
std::string effectString = "special://xbmc/system/shaders/yuv2rgb_d3d.fx";
if(!LoadEffect(effectString, &defines))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to load shader %s.", effectString.c_str());
return false;
}
// Create input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 2, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
if (!CWinShader::CreateInputLayout(layout, ARRAYSIZE(layout)))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to create input layout for Input Assembler.");
return false;
}
return true;
}
void CYUV2RGBShader::Render(CRect sourceRect, CRect destRect,
float contrast,
float brightness,
unsigned int flags,
YUVBuffer* YUVbuf)
{
PrepareParameters(sourceRect, destRect,
contrast, brightness, flags);
SetShaderParameters(YUVbuf);
Execute(nullptr, 4);
// we changed view port, so we need to restore our real viewport.
g_Windowing.RestoreViewPort();
}
CYUV2RGBShader::~CYUV2RGBShader()
{
}
void CYUV2RGBShader::PrepareParameters(CRect sourceRect,
CRect destRect,
float contrast,
float brightness,
unsigned int flags)
{
if (m_sourceRect != sourceRect || m_destRect != destRect)
{
m_sourceRect = sourceRect;
m_destRect = destRect;
CUSTOMVERTEX* v;
CWinShader::LockVertexBuffer((void**)&v);
v[0].x = destRect.x1;
v[0].y = destRect.y1;
v[0].z = 0.0f;
v[0].tu = sourceRect.x1 / m_sourceWidth;
v[0].tv = sourceRect.y1 / m_sourceHeight;
v[0].tu2 = v[0].tu3 = (sourceRect.x1 / 2.0f) / (m_sourceWidth>>1);
v[0].tv2 = v[0].tv3 = (sourceRect.y1 / 2.0f) / (m_sourceHeight>>1);
v[1].x = destRect.x2;
v[1].y = destRect.y1;
v[1].z = 0.0f;
v[1].tu = sourceRect.x2 / m_sourceWidth;
v[1].tv = sourceRect.y1 / m_sourceHeight;
v[1].tu2 = v[1].tu3 = (sourceRect.x2 / 2.0f) / (m_sourceWidth>>1);
v[1].tv2 = v[1].tv3 = (sourceRect.y1 / 2.0f) / (m_sourceHeight>>1);
v[2].x = destRect.x2;
v[2].y = destRect.y2;
v[2].z = 0.0f;
v[2].tu = sourceRect.x2 / m_sourceWidth;
v[2].tv = sourceRect.y2 / m_sourceHeight;
v[2].tu2 = v[2].tu3 = (sourceRect.x2 / 2.0f) / (m_sourceWidth>>1);
v[2].tv2 = v[2].tv3 = (sourceRect.y2 / 2.0f) / (m_sourceHeight>>1);
v[3].x = destRect.x1;
v[3].y = destRect.y2;
v[3].z = 0.0f;
v[3].tu = sourceRect.x1 / m_sourceWidth;
v[3].tv = sourceRect.y2 / m_sourceHeight;
v[3].tu2 = v[3].tu3 = (sourceRect.x1 / 2.0f) / (m_sourceWidth>>1);
v[3].tv2 = v[3].tv3 = (sourceRect.y2 / 2.0f) / (m_sourceHeight>>1);
CWinShader::UnlockVertexBuffer();
}
m_matrix.SetParameters(contrast * 0.02f,
brightness * 0.01f - 0.5f,
flags,
m_format);
}
void CYUV2RGBShader::SetShaderParameters(YUVBuffer* YUVbuf)
{
m_effect.SetTechnique("YUV2RGB_T");
m_effect.SetMatrix("g_ColorMatrix", m_matrix.Matrix());
m_effect.SetTexture("g_YTexture", YUVbuf->planes[0].texture);
if (YUVbuf->GetActivePlanes() > 1)
m_effect.SetTexture("g_UTexture", YUVbuf->planes[1].texture);
if (YUVbuf->GetActivePlanes() > 2)
m_effect.SetTexture("g_VTexture", YUVbuf->planes[2].texture);
m_effect.SetFloatArray("g_StepXY", m_texSteps, ARRAY_SIZE(m_texSteps));
// we need to set view port to the full size of current render target
ID3D11RenderTargetView* rtView = nullptr;
g_Windowing.Get3D11Context()->OMGetRenderTargets(1, &rtView, nullptr);
// get dimention of render target
ID3D11Resource* rtResource = nullptr;
rtView->GetResource(&rtResource);
ID3D11Texture2D* rtTexture = nullptr;
HRESULT hr = rtResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&rtTexture));
float viewPortWidth = 0.0f, viewPortHeight = 0.0f;
if (S_OK == hr && rtTexture)
{
D3D11_TEXTURE2D_DESC rtDescr = {};
rtTexture->GetDesc(&rtDescr);
viewPortWidth = static_cast<float>(rtDescr.Width);
viewPortHeight = static_cast<float>(rtDescr.Height);
}
SAFE_RELEASE(rtTexture);
SAFE_RELEASE(rtResource);
SAFE_RELEASE(rtView);
D3D11_VIEWPORT viewPort = { 0.0f, 0.0f, viewPortWidth, viewPortHeight, 0.0, 1.0f };
g_Windowing.Get3D11Context()->RSSetViewports(1, &viewPort);
m_effect.SetFloatArray("g_viewPort", &viewPort.Width, 2);
}
//==================================================================================
CConvolutionShader::~CConvolutionShader()
{
if(m_HQKernelTexture.Get())
m_HQKernelTexture.Release();
}
bool CConvolutionShader::ChooseKernelD3DFormat()
{
if (g_Windowing.IsFormatSupport(DXGI_FORMAT_R16G16B16A16_FLOAT, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE))
{
m_KernelFormat = DXGI_FORMAT_R16G16B16A16_FLOAT;
m_floattex = true;
m_rgba = true;
}
else if (g_Windowing.IsFormatSupport(DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE))
{
m_KernelFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
m_floattex = false;
m_rgba = true;
}
else if (g_Windowing.IsFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE))
{
m_KernelFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
m_floattex = false;
m_rgba = false;
}
else
return false;
return true;
}
bool CConvolutionShader::CreateHQKernel(ESCALINGMETHOD method)
{
CConvolutionKernel kern(method, 256);
void *kernelVals;
int kernelValsSize;
if (m_floattex)
{
float *rawVals = kern.GetFloatPixels();
HALF* float16Vals = new HALF[kern.GetSize() * 4];
XMConvertFloatToHalfStream(float16Vals, sizeof(HALF), rawVals, sizeof(float), kern.GetSize()*4);
kernelVals = float16Vals;
kernelValsSize = sizeof(HALF)*kern.GetSize() * 4;
}
else
{
kernelVals = kern.GetUint8Pixels();
kernelValsSize = sizeof(uint8_t)*kern.GetSize() * 4;
}
if (!m_HQKernelTexture.Create(kern.GetSize(), 1, 1, D3D11_USAGE_IMMUTABLE, m_KernelFormat, kernelVals, kernelValsSize))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to create kernel texture.");
return false;
}
if (m_floattex)
delete[] kernelVals;
return true;
}
//==================================================================================
bool CConvolutionShader1Pass::Create(ESCALINGMETHOD method)
{
std::string effectString;
switch(method)
{
case VS_SCALINGMETHOD_CUBIC:
case VS_SCALINGMETHOD_LANCZOS2:
case VS_SCALINGMETHOD_SPLINE36_FAST:
case VS_SCALINGMETHOD_LANCZOS3_FAST:
effectString = "special://xbmc/system/shaders/convolution-4x4_d3d.fx";
break;
case VS_SCALINGMETHOD_SPLINE36:
case VS_SCALINGMETHOD_LANCZOS3:
effectString = "special://xbmc/system/shaders/convolution-6x6_d3d.fx";
break;
default:
CLog::Log(LOGERROR, __FUNCTION__": scaling method %d not supported.", method);
return false;
}
if (!ChooseKernelD3DFormat())
{
CLog::Log(LOGERROR, __FUNCTION__": failed to find a compatible texture format for the kernel.");
return false;
}
CWinShader::CreateVertexBuffer(4, sizeof(CUSTOMVERTEX));
DefinesMap defines;
if (m_floattex)
defines["HAS_FLOAT_TEXTURE"] = "";
if (m_rgba)
defines["HAS_RGBA"] = "";
if(!LoadEffect(effectString, &defines))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to load shader %s.", effectString.c_str());
return false;
}
if (!CreateHQKernel(method))
return false;
// Create input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
return CWinShader::CreateInputLayout(layout, ARRAYSIZE(layout));
}
void CConvolutionShader1Pass::Render(CD3DTexture &sourceTexture,
unsigned int sourceWidth, unsigned int sourceHeight,
unsigned int destWidth, unsigned int destHeight,
CRect sourceRect,
CRect destRect)
{
PrepareParameters(sourceWidth, sourceHeight, sourceRect, destRect);
float texSteps[] = { 1.0f/(float)sourceWidth, 1.0f/(float)sourceHeight};
SetShaderParameters(sourceTexture, &texSteps[0], ARRAY_SIZE(texSteps));
Execute(nullptr, 4);
}
void CConvolutionShader1Pass::PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
CRect sourceRect,
CRect destRect)
{
if(m_sourceWidth != sourceWidth || m_sourceHeight != sourceHeight
|| m_sourceRect != sourceRect || m_destRect != destRect)
{
m_sourceWidth = sourceWidth;
m_sourceHeight = sourceHeight;
m_sourceRect = sourceRect;
m_destRect = destRect;
CUSTOMVERTEX* v;
CWinShader::LockVertexBuffer((void**)&v);
v[0].x = destRect.x1;
v[0].y = destRect.y1;
v[0].z = 0;
v[0].tu = sourceRect.x1 / sourceWidth;
v[0].tv = sourceRect.y1 / sourceHeight;
v[1].x = destRect.x2;
v[1].y = destRect.y1;
v[1].z = 0;
v[1].tu = sourceRect.x2 / sourceWidth;
v[1].tv = sourceRect.y1 / sourceHeight;
v[2].x = destRect.x2;
v[2].y = destRect.y2;
v[2].z = 0;
v[2].tu = sourceRect.x2 / sourceWidth;
v[2].tv = sourceRect.y2 / sourceHeight;
v[3].x = destRect.x1;
v[3].y = destRect.y2;
v[3].z = 0;
v[3].tu = sourceRect.x1 / sourceWidth;
v[3].tv = sourceRect.y2 / sourceHeight;
CWinShader::UnlockVertexBuffer();
}
}
void CConvolutionShader1Pass::SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps, int texStepsCount)
{
m_effect.SetTechnique( "SCALER_T" );
m_effect.SetTexture( "g_Texture", sourceTexture ) ;
m_effect.SetTexture( "g_KernelTexture", m_HQKernelTexture );
m_effect.SetFloatArray("g_StepXY", texSteps, texStepsCount);
UINT numVP = 1;
D3D11_VIEWPORT viewPort = {};
g_Windowing.Get3D11Context()->RSGetViewports(&numVP, &viewPort);
m_effect.SetFloatArray("g_viewPort", &viewPort.Width, 2);
}
//==================================================================================
CConvolutionShaderSeparable::CConvolutionShaderSeparable() : CConvolutionShader()
{
m_sourceWidth = -1;
m_sourceHeight = -1;
m_destWidth = -1;
m_destHeight = -1;
}
bool CConvolutionShaderSeparable::Create(ESCALINGMETHOD method)
{
std::string effectString;
switch(method)
{
case VS_SCALINGMETHOD_CUBIC:
case VS_SCALINGMETHOD_LANCZOS2:
case VS_SCALINGMETHOD_SPLINE36_FAST:
case VS_SCALINGMETHOD_LANCZOS3_FAST:
effectString = "special://xbmc/system/shaders/convolutionsep-4x4_d3d.fx";
break;
case VS_SCALINGMETHOD_SPLINE36:
case VS_SCALINGMETHOD_LANCZOS3:
effectString = "special://xbmc/system/shaders/convolutionsep-6x6_d3d.fx";
break;
default:
CLog::Log(LOGERROR, __FUNCTION__": scaling method %d not supported.", method);
return false;
}
if (!ChooseIntermediateD3DFormat())
{
CLog::Log(LOGERROR, __FUNCTION__": failed to find a compatible texture format for the intermediate render target.");
return false;
}
if (!ChooseKernelD3DFormat())
{
CLog::Log(LOGERROR, __FUNCTION__": failed to find a compatible texture format for the kernel.");
return false;
}
CWinShader::CreateVertexBuffer(8, sizeof(CUSTOMVERTEX));
DefinesMap defines;
if (m_floattex)
defines["HAS_FLOAT_TEXTURE"] = "";
if (m_rgba)
defines["HAS_RGBA"] = "";
if(!LoadEffect(effectString, &defines))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to load shader %s.", effectString.c_str());
return false;
}
if (!CreateHQKernel(method))
return false;
// Create input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
return CWinShader::CreateInputLayout(layout, ARRAYSIZE(layout));
}
void CConvolutionShaderSeparable::Render(CD3DTexture &sourceTexture,
unsigned int sourceWidth, unsigned int sourceHeight,
unsigned int destWidth, unsigned int destHeight,
CRect sourceRect,
CRect destRect)
{
if(m_destWidth != destWidth || m_sourceHeight != sourceHeight)
CreateIntermediateRenderTarget(destWidth, sourceHeight);
PrepareParameters(sourceWidth, sourceHeight, destWidth, destHeight, sourceRect, destRect);
float texSteps[] =
{
1.0f / static_cast<float>(sourceWidth),
1.0f / static_cast<float>(sourceHeight),
1.0f / static_cast<float>(destWidth),
1.0f / static_cast<float>(sourceHeight)
};
SetShaderParameters(sourceTexture, texSteps, 4);
Execute(nullptr, 4);
// we changed view port, so we need to restore our real viewport.
g_Windowing.RestoreViewPort();
}
CConvolutionShaderSeparable::~CConvolutionShaderSeparable()
{
if (m_IntermediateTarget.Get())
m_IntermediateTarget.Release();
SAFE_RELEASE(m_oldRenderTarget);
}
bool CConvolutionShaderSeparable::ChooseIntermediateD3DFormat()
{
D3D11_FORMAT_SUPPORT usage = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
// Need a float texture, as the output of the first pass can contain negative values.
if (g_Windowing.IsFormatSupport(DXGI_FORMAT_R16G16B16A16_FLOAT, usage)) m_IntermediateFormat = DXGI_FORMAT_R16G16B16A16_FLOAT;
else if (g_Windowing.IsFormatSupport(DXGI_FORMAT_R32G32B32A32_FLOAT, usage)) m_IntermediateFormat = DXGI_FORMAT_R32G32B32A32_FLOAT;
else
{
CLog::Log(LOGNOTICE, __FUNCTION__": no float format available for the intermediate render target");
return false;
}
CLog::Log(LOGDEBUG, __FUNCTION__": format %i", m_IntermediateFormat);
return true;
}
bool CConvolutionShaderSeparable::CreateIntermediateRenderTarget(unsigned int width, unsigned int height)
{
if (m_IntermediateTarget.Get())
m_IntermediateTarget.Release();
if (!m_IntermediateTarget.Create(width, height, 1, D3D11_USAGE_DEFAULT, m_IntermediateFormat))
{
CLog::Log(LOGERROR, __FUNCTION__": render target creation failed.");
return false;
}
return true;
}
bool CConvolutionShaderSeparable::ClearIntermediateRenderTarget()
{
float color[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
ID3D11RenderTargetView* intermediateRT = m_IntermediateTarget.GetRenderTarget();
g_Windowing.Get3D11Context()->ClearRenderTargetView(intermediateRT, color);
return true;
}
void CConvolutionShaderSeparable::PrepareParameters(unsigned int sourceWidth, unsigned int sourceHeight,
unsigned int destWidth, unsigned int destHeight,
CRect sourceRect,
CRect destRect)
{
if(m_sourceWidth != sourceWidth || m_sourceHeight != sourceHeight
|| m_destWidth != destWidth || m_destHeight != destHeight
|| m_sourceRect != sourceRect || m_destRect != destRect)
{
ClearIntermediateRenderTarget();
m_sourceWidth = sourceWidth;
m_sourceHeight = sourceHeight;
m_destWidth = destWidth;
m_destHeight = destHeight;
m_sourceRect = sourceRect;
m_destRect = destRect;
CUSTOMVERTEX* v;
CWinShader::LockVertexBuffer((void**)&v);
// Alter rectangles the destination rectangle exceeds the intermediate target width when zooming and causes artifacts.
// Work on the parameters rather than the members to avoid disturbing the parameter change detection the next time the function is called
CRect tgtRect(0, 0, static_cast<float>(destWidth), static_cast<float>(destHeight));
CWIN32Util::CropSource(sourceRect, destRect, tgtRect);
// Manipulate the coordinates to work only on the active parts of the textures,
// and therefore avoid the need to clear surfaces/render targets
// Pass 1:
// Horizontal dimension: crop/zoom, so that it is completely done with the convolution shader. Scaling to display width in pass1 and
// cropping/zooming in pass 2 would use bilinear in pass2, which we don't want.
// Vertical dimension: crop using sourceRect to save memory bandwidth for high zoom values, but don't stretch/shrink in any way in this pass.
v[0].x = 0;
v[0].y = 0;
v[0].z = 0;
v[0].tu = sourceRect.x1 / sourceWidth;
v[0].tv = sourceRect.y1 / sourceHeight;
v[1].x = destRect.x2 - destRect.x1;
v[1].y = 0;
v[1].z = 0;
v[1].tu = sourceRect.x2 / sourceWidth;
v[1].tv = sourceRect.y1 / sourceHeight;
v[2].x = destRect.x2 - destRect.x1;
v[2].y = sourceRect.y2 - sourceRect.y1;
v[2].z = 0;
v[2].tu = sourceRect.x2 / sourceWidth;
v[2].tv = sourceRect.y2 / sourceHeight;
v[3].x = 0;
v[3].y = sourceRect.y2 - sourceRect.y1;
v[3].z = 0;
v[3].tu = sourceRect.x1 / sourceWidth;
v[3].tv = sourceRect.y2 / sourceHeight;
// Pass 2: pass the horizontal data untouched, resize vertical dimension for final result.
v[4].x = destRect.x1;
v[4].y = destRect.y1;
v[4].z = 0;
v[4].tu = 0;
v[4].tv = 0;
v[5].x = destRect.x2;
v[5].y = destRect.y1;
v[5].z = 0;
v[5].tu = (destRect.x2 - destRect.x1) / destWidth;
v[5].tv = 0;
v[6].x = destRect.x2;
v[6].y = destRect.y2;
v[6].z = 0;
v[6].tu = (destRect.x2 - destRect.x1) / destWidth;
v[6].tv = (sourceRect.y2 - sourceRect.y1) / sourceHeight;
v[7].x = destRect.x1;
v[7].y = destRect.y2;
v[7].z = 0;
v[7].tu = 0;
v[7].tv = (sourceRect.y2 - sourceRect.y1) / sourceHeight;
CWinShader::UnlockVertexBuffer();
}
}
void CConvolutionShaderSeparable::SetShaderParameters(CD3DTexture &sourceTexture, float* texSteps, int texStepsCount)
{
m_effect.SetTechnique( "SCALER_T" );
m_effect.SetTexture( "g_Texture", sourceTexture );
m_effect.SetTexture( "g_KernelTexture", m_HQKernelTexture );
m_effect.SetFloatArray("g_StepXY", texSteps, texStepsCount);
}
void CConvolutionShaderSeparable::SetStepParams(UINT iPass)
{
CD3D11_VIEWPORT viewPort(.0f, .0f, .0f, .0f);
ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context();
if (iPass == 0)
{
// store old RT
pContext->OMGetRenderTargets(1, &m_oldRenderTarget, nullptr);
// setting new RT
ID3D11RenderTargetView* newRT = m_IntermediateTarget.GetRenderTarget();
pContext->OMSetRenderTargets(1, &newRT, nullptr);
// new viewport
viewPort = CD3D11_VIEWPORT(0.0f, 0.0f,
static_cast<float>(m_IntermediateTarget.GetWidth()),
static_cast<float>(m_IntermediateTarget.GetHeight()));
// reset scissor
g_Windowing.ResetScissors();
}
else if (iPass == 1)
{
if (m_oldRenderTarget)
{
// get dimention of old render target
ID3D11Resource* rtResource = nullptr;
m_oldRenderTarget->GetResource(&rtResource);
ID3D11Texture2D* rtTexture = nullptr;
if (SUCCEEDED(rtResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&rtTexture))))
{
D3D11_TEXTURE2D_DESC rtDescr = {};
rtTexture->GetDesc(&rtDescr);
viewPort = CD3D11_VIEWPORT(0.0f, 0.0f,
static_cast<float>(rtDescr.Width),
static_cast<float>(rtDescr.Height));
}
SAFE_RELEASE(rtTexture);
SAFE_RELEASE(rtResource);
}
else
{
// current RT is null so try to restore viewport
CRect winViewPort;
g_Windowing.GetViewPort(winViewPort);
viewPort = CD3D11_VIEWPORT(winViewPort.x1, winViewPort.y1, winViewPort.Width(), winViewPort.Height());
}
pContext->OMSetRenderTargets(1, &m_oldRenderTarget, nullptr);
SAFE_RELEASE(m_oldRenderTarget);
// at the second pass m_IntermediateTarget is a source of data
m_effect.SetTexture("g_Texture", m_IntermediateTarget);
// restore scissor
g_Windowing.SetScissors(g_graphicsContext.StereoCorrection(g_graphicsContext.GetScissors()));
}
// seting view port
pContext->RSSetViewports(1, &viewPort);
// pass viewport dimention to the shaders
m_effect.SetFloatArray("g_viewPort", &viewPort.Width, 2);
}
//==========================================================
bool CTestShader::Create()
{
std::string effectString = "special://xbmc/system/shaders/testshader.fx";
if(!LoadEffect(effectString, nullptr))
{
CLog::Log(LOGERROR, __FUNCTION__": Failed to load shader %s.", effectString.c_str());
return false;
}
return true;
}
#endif