-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
WinRenderer.cpp
1533 lines (1292 loc) · 45.2 KB
/
WinRenderer.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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-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 "Util.h"
#include "WinRenderer.h"
#include "cores/dvdplayer/DVDCodecs/Video/DVDVideoCodec.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "filesystem/File.h"
#include "guilib/LocalizeStrings.h"
#include "guilib/Texture.h"
#include "settings/AdvancedSettings.h"
#include "settings/DisplaySettings.h"
#include "settings/MediaSettings.h"
#include "settings/Settings.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "utils/MathUtils.h"
#include "utils/SystemInfo.h"
#include "VideoShaders/WinVideoFilter.h"
#include "win32/WIN32Util.h"
#include "windowing/WindowingFactory.h"
#include "cores/FFmpeg.h"
typedef struct {
RenderMethod method;
const char *name;
} RenderMethodDetail;
static RenderMethodDetail RenderMethodDetails[] = {
{ RENDER_SW , "Software" },
{ RENDER_PS , "Pixel Shaders" },
{ RENDER_DXVA , "DXVA" },
{ RENDER_INVALID, NULL }
};
static RenderMethodDetail *FindRenderMethod(RenderMethod m)
{
for (unsigned i = 0; RenderMethodDetails[i].method != RENDER_INVALID; i++) {
if (RenderMethodDetails[i].method == m)
return &RenderMethodDetails[i];
}
return NULL;
}
CWinRenderer::CWinRenderer()
{
m_iYV12RenderBuffer = 0;
m_NumYV12Buffers = 0;
m_colorShader = NULL;
m_scalerShader = NULL;
m_extended_format = 0;
m_iRequestedMethod = RENDER_METHOD_AUTO;
m_renderMethod = RENDER_PS;
m_scalingMethod = VS_SCALINGMETHOD_LINEAR;
m_scalingMethodGui = (ESCALINGMETHOD)-1;
m_TextureFilter = D3DTEXF_POINT;
m_bUseHQScaler = false;
m_bFilterInitialized = false;
for (int i = 0; i<NUM_BUFFERS; i++)
m_VideoBuffers[i] = NULL;
m_sw_scale_ctx = NULL;
m_destWidth = 0;
m_destHeight = 0;
m_bConfigured = false;
m_clearColour = 0;
m_format = RENDER_FMT_NONE;
m_processor = NULL;
m_neededBuffers = 0;
}
CWinRenderer::~CWinRenderer()
{
UnInit();
}
static enum PixelFormat PixelFormatFromFormat(ERenderFormat format)
{
if (format == RENDER_FMT_YUV420P) return PIX_FMT_YUV420P;
else if (format == RENDER_FMT_YUV420P10) return PIX_FMT_YUV420P10;
else if (format == RENDER_FMT_YUV420P16) return PIX_FMT_YUV420P16;
else if (format == RENDER_FMT_NV12) return PIX_FMT_NV12;
else if (format == RENDER_FMT_UYVY422) return PIX_FMT_UYVY422;
else if (format == RENDER_FMT_YUYV422) return PIX_FMT_YUYV422;
else return PIX_FMT_NONE;
}
void CWinRenderer::ManageTextures()
{
if( m_NumYV12Buffers < m_neededBuffers )
{
for(int i = m_NumYV12Buffers; i<m_neededBuffers;i++)
CreateYV12Texture(i);
m_NumYV12Buffers = m_neededBuffers;
}
else if( m_NumYV12Buffers > m_neededBuffers )
{
m_NumYV12Buffers = m_neededBuffers;
m_iYV12RenderBuffer = m_iYV12RenderBuffer % m_NumYV12Buffers;
for(int i = m_NumYV12Buffers-1; i>=m_neededBuffers;i--)
DeleteYV12Texture(i);
}
}
void CWinRenderer::SelectRenderMethod()
{
// Set rendering to dxva before trying it, in order to open the correct processor immediately, when deinterlacing method is auto.
// Force dxva renderer after dxva decoding: PS and SW renderers have performance issues after dxva decode.
if (g_advancedSettings.m_DXVAForceProcessorRenderer && m_format == RENDER_FMT_DXVA)
{
CLog::Log(LOGNOTICE, "D3D: rendering method forced to DXVA processor");
m_renderMethod = RENDER_DXVA;
if (!m_processor || !m_processor->Open(m_sourceWidth, m_sourceHeight, m_iFlags, m_format, m_extended_format))
{
CLog::Log(LOGNOTICE, "D3D: unable to open DXVA processor");
if (m_processor)
m_processor->Close();
m_renderMethod = RENDER_INVALID;
}
}
else
{
CLog::Log(LOGDEBUG, __FUNCTION__": Requested render method: %d", m_iRequestedMethod);
switch(m_iRequestedMethod)
{
case RENDER_METHOD_DXVAHD:
case RENDER_METHOD_DXVA:
if (!m_processor || !m_processor->Open(m_sourceWidth, m_sourceHeight, m_iFlags, m_format, m_extended_format))
{
CLog::Log(LOGNOTICE, "D3D: unable to open DXVA processor");
if (m_processor)
m_processor->Close();
}
else
{
m_renderMethod = RENDER_DXVA;
break;
}
// Drop through to pixel shader
case RENDER_METHOD_AUTO:
case RENDER_METHOD_D3D_PS:
// Try the pixel shaders support
if (m_deviceCaps.PixelShaderVersion >= D3DPS_VERSION(2, 0))
{
CTestShader shader;
if (shader.Create())
{
m_renderMethod = RENDER_PS;
break;
}
else
{
CLog::Log(LOGNOTICE, "D3D: unable to load test shader - D3D installation is most likely incomplete");
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, "DirectX", g_localizeStrings.Get(2101));
}
}
else
{
CLog::Log(LOGNOTICE, "D3D: graphics adapter does not support Pixel Shaders 2.0");
}
CLog::Log(LOGNOTICE, "D3D: falling back to SW mode");
// drop through to software
case RENDER_METHOD_SOFTWARE:
default:
// So we'll do the color conversion in software.
m_renderMethod = RENDER_SW;
break;
}
}
RenderMethodDetail *rmdet = FindRenderMethod(m_renderMethod);
CLog::Log(LOGDEBUG, __FUNCTION__": Selected render method %d: %s", m_renderMethod, rmdet != NULL ? rmdet->name : "unknown");
m_frameIdx = 0;
}
bool CWinRenderer::UpdateRenderMethod()
{
if (m_SWTarget.Get())
m_SWTarget.Release();
if (m_renderMethod == RENDER_SW)
{
if(!m_SWTarget.Create(m_sourceWidth, m_sourceHeight, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT))
{
CLog::Log(LOGNOTICE, __FUNCTION__": Failed to create sw render target.");
return false;
}
}
return true;
}
bool CWinRenderer::Configure(unsigned int width, unsigned int height, unsigned int d_width, unsigned int d_height, float fps, unsigned flags, ERenderFormat format, unsigned extended_format, unsigned int orientation)
{
if(m_sourceWidth != width
|| m_sourceHeight != height)
{
m_sourceWidth = width;
m_sourceHeight = height;
// need to recreate textures
m_NumYV12Buffers = 0;
m_iYV12RenderBuffer = 0;
// reinitialize the filters/shaders
m_bFilterInitialized = false;
}
else
{
if (m_VideoBuffers[m_iYV12RenderBuffer] != NULL)
m_VideoBuffers[m_iYV12RenderBuffer]->StartDecode();
m_iYV12RenderBuffer = 0;
if (m_VideoBuffers[0] != NULL)
m_VideoBuffers[0]->StartRender();
}
m_fps = fps;
m_iFlags = flags;
m_format = format;
m_extended_format = extended_format;
// calculate the input frame aspect ratio
CalculateFrameAspectRatio(d_width, d_height);
ChooseBestResolution(fps);
m_destWidth = g_graphicsContext.GetResInfo(m_resolution).iWidth;
m_destHeight = g_graphicsContext.GetResInfo(m_resolution).iHeight;
SetViewMode(CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode);
ManageDisplay();
m_bConfigured = true;
SelectRenderMethod();
UpdateRenderMethod();
return true;
}
int CWinRenderer::NextYV12Texture()
{
if(m_NumYV12Buffers)
return (m_iYV12RenderBuffer + 1) % m_NumYV12Buffers;
else
return -1;
}
bool CWinRenderer::AddVideoPicture(DVDVideoPicture* picture, int index)
{
if (!m_NumYV12Buffers)
{
return false;
}
if (m_renderMethod == RENDER_DXVA)
{
int source = index;
if(source < 0 || NextYV12Texture() < 0)
return false;
DXVABuffer *buf = (DXVABuffer*)m_VideoBuffers[source];
SAFE_RELEASE(buf->pic);
if (picture->format == RENDER_FMT_DXVA)
{
buf->pic = picture->dxva->Acquire();
}
else
{
buf->pic = m_processor->Convert(picture);
}
buf->frameIdx = m_frameIdx;
m_frameIdx += 2;
return true;
}
return false;
}
int CWinRenderer::GetImage(YV12Image *image, int source, bool readonly)
{
/* take next available buffer */
if( source == AUTOSOURCE )
source = NextYV12Texture();
if( source < 0 || NextYV12Texture() < 0)
return -1;
YUVBuffer *buf = (YUVBuffer*)m_VideoBuffers[source];
if (buf->IsReadyToRender())
return -1;
image->cshift_x = 1;
image->cshift_y = 1;
image->height = m_sourceHeight;
image->width = m_sourceWidth;
image->flags = 0;
if(m_format == RENDER_FMT_YUV420P10
|| m_format == RENDER_FMT_YUV420P16)
image->bpp = 2;
else
image->bpp = 1;
for(int i=0;i<3;i++)
{
image->stride[i] = buf->planes[i].rect.Pitch;
image->plane[i] = (BYTE*)buf->planes[i].rect.pBits;
}
return source;
}
void CWinRenderer::ReleaseImage(int source, bool preserve)
{
// no need to release anything here since we're using system memory
}
void CWinRenderer::Reset()
{
}
void CWinRenderer::Update()
{
if (!m_bConfigured) return;
ManageDisplay();
}
void CWinRenderer::RenderUpdate(bool clear, DWORD flags, DWORD alpha)
{
LPDIRECT3DDEVICE9 pD3DDevice = g_Windowing.Get3DDevice();
if (clear)
g_graphicsContext.Clear(m_clearColour);
if(alpha < 255)
pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
else
pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
if (!m_bConfigured)
return;
ManageTextures();
CSingleLock lock(g_graphicsContext);
ManageDisplay();
Render(flags);
}
void CWinRenderer::FlipPage(int source)
{
if(source == AUTOSOURCE)
source = NextYV12Texture();
if (m_VideoBuffers[m_iYV12RenderBuffer] != NULL)
m_VideoBuffers[m_iYV12RenderBuffer]->StartDecode();
if( source >= 0 && source < m_NumYV12Buffers )
m_iYV12RenderBuffer = source;
else
m_iYV12RenderBuffer = 0;
if (m_VideoBuffers[m_iYV12RenderBuffer] != NULL)
m_VideoBuffers[m_iYV12RenderBuffer]->StartRender();
#ifdef MP_DIRECTRENDERING
__asm wbinvd
#endif
return;
}
unsigned int CWinRenderer::PreInit()
{
CSingleLock lock(g_graphicsContext);
m_bConfigured = false;
UnInit();
m_resolution = CDisplaySettings::Get().GetCurrentResolution();
if ( m_resolution == RES_WINDOW )
m_resolution = RES_DESKTOP;
// setup the background colour
m_clearColour = (g_advancedSettings.m_videoBlackBarColour & 0xff) * 0x010101;
g_Windowing.Get3DDevice()->GetDeviceCaps(&m_deviceCaps);
m_formats.clear();
m_formats.push_back(RENDER_FMT_YUV420P);
m_iRequestedMethod = CSettings::Get().GetInt("videoplayer.rendermethod");
if (g_advancedSettings.m_DXVAForceProcessorRenderer
|| m_iRequestedMethod == RENDER_METHOD_DXVA
|| m_iRequestedMethod == RENDER_METHOD_DXVAHD)
{
if (m_iRequestedMethod != RENDER_METHOD_DXVA && CSysInfo::IsWindowsVersionAtLeast(CSysInfo::WindowsVersionWin7))
{
m_processor = new DXVA::CProcessorHD();
if (!m_processor->PreInit())
{
CLog::Log(LOGNOTICE, "CWinRenderer::Preinit - could not init DXVA-HD processor - skipping");
SAFE_DELETE(m_processor);
}
}
if (!m_processor)
{
m_processor = new DXVA::CProcessor();
if (!m_processor->PreInit())
{
CLog::Log(LOGNOTICE, "CWinRenderer::Preinit - could not init DXVA2 processor - skipping");
SAFE_DELETE(m_processor);
}
}
}
// allow other color spaces besides YV12 in case DXVA rendering is not used or not available
if (!m_processor || (m_iRequestedMethod != RENDER_METHOD_DXVA && m_iRequestedMethod != RENDER_METHOD_DXVAHD))
{
if (g_Windowing.IsTextureFormatOk(D3DFMT_L16, 0))
{
m_formats.push_back(RENDER_FMT_YUV420P10);
m_formats.push_back(RENDER_FMT_YUV420P16);
}
m_formats.push_back(RENDER_FMT_NV12);
m_formats.push_back(RENDER_FMT_YUYV422);
m_formats.push_back(RENDER_FMT_UYVY422);
}
return 0;
}
void CWinRenderer::UnInit()
{
CSingleLock lock(g_graphicsContext);
if (m_SWTarget.Get())
m_SWTarget.Release();
if (m_IntermediateTarget.Get())
m_IntermediateTarget.Release();
SAFE_DELETE(m_colorShader);
SAFE_DELETE(m_scalerShader);
m_bConfigured = false;
m_bFilterInitialized = false;
for(int i = 0; i < NUM_BUFFERS; i++)
DeleteYV12Texture(i);
m_NumYV12Buffers = 0;
if (m_sw_scale_ctx)
{
sws_freeContext(m_sw_scale_ctx);
m_sw_scale_ctx = NULL;
}
if (m_processor)
{
m_processor->UnInit();
SAFE_DELETE(m_processor);
}
}
void CWinRenderer::Flush()
{
PreInit();
SetViewMode(CMediaSettings::Get().GetCurrentVideoSettings().m_ViewMode);
ManageDisplay();
m_bConfigured = true;
SelectRenderMethod();
UpdateRenderMethod();
}
bool CWinRenderer::CreateIntermediateRenderTarget(unsigned int width, unsigned int height)
{
// Initialize a render target for intermediate rendering
LPDIRECT3DDEVICE9 pD3DDevice = g_Windowing.Get3DDevice();
D3DFORMAT format = D3DFMT_X8R8G8B8;
DWORD usage = D3DUSAGE_RENDERTARGET;
if (m_renderMethod == RENDER_DXVA) format = D3DFMT_X8R8G8B8;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_A2R10G10B10, usage)) format = D3DFMT_A2R10G10B10;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_A2B10G10R10, usage)) format = D3DFMT_A2B10G10R10;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_A8R8G8B8, usage)) format = D3DFMT_A8R8G8B8;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_A8B8G8R8, usage)) format = D3DFMT_A8B8G8R8;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_X8R8G8B8, usage)) format = D3DFMT_X8R8G8B8;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_X8B8G8R8, usage)) format = D3DFMT_X8B8G8R8;
else if (g_Windowing.IsTextureFormatOk(D3DFMT_R8G8B8, usage)) format = D3DFMT_R8G8B8;
// don't create new one if it exists with requested size and format
if ( m_IntermediateTarget.Get() && m_IntermediateTarget.GetFormat() == format
&& m_IntermediateTarget.GetWidth() == width && m_IntermediateTarget.GetHeight() == height)
return true;
if (m_IntermediateTarget.Get())
m_IntermediateTarget.Release();
CLog::Log(LOGDEBUG, __FUNCTION__": format %i", format);
if(!m_IntermediateTarget.Create(width, height, 1, usage, format, D3DPOOL_DEFAULT))
{
CLog::Log(LOGERROR, __FUNCTION__": intermediate render target creation failed.", format);
return false;
}
return true;
}
void CWinRenderer::SelectSWVideoFilter()
{
switch (m_scalingMethod)
{
case VS_SCALINGMETHOD_AUTO:
case VS_SCALINGMETHOD_LINEAR:
if (Supports(VS_SCALINGMETHOD_LINEAR))
{
m_TextureFilter = D3DTEXF_LINEAR;
break;
}
// fall through for fallback
case VS_SCALINGMETHOD_NEAREST:
default:
m_TextureFilter = D3DTEXF_POINT;
break;
}
}
void CWinRenderer::SelectPSVideoFilter()
{
m_bUseHQScaler = false;
switch (m_scalingMethod)
{
case VS_SCALINGMETHOD_NEAREST:
case VS_SCALINGMETHOD_LINEAR:
break;
case VS_SCALINGMETHOD_CUBIC:
case VS_SCALINGMETHOD_LANCZOS2:
case VS_SCALINGMETHOD_SPLINE36_FAST:
case VS_SCALINGMETHOD_LANCZOS3_FAST:
m_bUseHQScaler = true;
break;
case VS_SCALINGMETHOD_SPLINE36:
case VS_SCALINGMETHOD_LANCZOS3:
m_bUseHQScaler = true;
break;
case VS_SCALINGMETHOD_SINC8:
case VS_SCALINGMETHOD_NEDI:
CLog::Log(LOGERROR, "D3D: TODO: This scaler has not yet been implemented");
break;
case VS_SCALINGMETHOD_BICUBIC_SOFTWARE:
case VS_SCALINGMETHOD_LANCZOS_SOFTWARE:
case VS_SCALINGMETHOD_SINC_SOFTWARE:
CLog::Log(LOGERROR, "D3D: TODO: Software scaling has not yet been implemented");
break;
default:
break;
}
if (m_scalingMethod == VS_SCALINGMETHOD_AUTO)
{
bool scaleSD = m_sourceHeight < 720 && m_sourceWidth < 1280;
bool scaleUp = (int)m_sourceHeight < g_graphicsContext.GetHeight() && (int)m_sourceWidth < g_graphicsContext.GetWidth();
bool scaleFps = m_fps < (g_advancedSettings.m_videoAutoScaleMaxFps + 0.01f);
if (m_renderMethod == RENDER_DXVA)
{
m_scalingMethod = VS_SCALINGMETHOD_DXVA_HARDWARE;
m_bUseHQScaler = false;
}
else if (scaleSD && scaleUp && scaleFps && Supports(VS_SCALINGMETHOD_LANCZOS3_FAST))
{
m_scalingMethod = VS_SCALINGMETHOD_LANCZOS3_FAST;
m_bUseHQScaler = true;
}
}
}
void CWinRenderer::UpdatePSVideoFilter()
{
SAFE_DELETE(m_scalerShader);
if (m_bUseHQScaler)
{
// First try the more efficient two pass convolution scaler
m_scalerShader = new CConvolutionShaderSeparable();
if (!m_scalerShader->Create(m_scalingMethod))
{
SAFE_DELETE(m_scalerShader);
CLog::Log(LOGNOTICE, __FUNCTION__": two pass convolution shader init problem, falling back to one pass.");
}
// Fallback on the one pass version
if (m_scalerShader == NULL)
{
m_scalerShader = new CConvolutionShader1Pass();
if (!m_scalerShader->Create(m_scalingMethod))
{
SAFE_DELETE(m_scalerShader);
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(34400), g_localizeStrings.Get(34401));
m_bUseHQScaler = false;
}
}
}
if (m_bUseHQScaler && !CreateIntermediateRenderTarget(m_sourceWidth, m_sourceHeight))
{
SAFE_DELETE(m_scalerShader);
m_bUseHQScaler = false;
}
SAFE_DELETE(m_colorShader);
if (m_renderMethod == RENDER_DXVA)
{
// we'd use m_IntermediateTarget as rendering target for possible anaglyph stereo with dxva processor.
if (!m_bUseHQScaler)
CreateIntermediateRenderTarget(m_destWidth, m_destHeight);
// When using DXVA, we are already setup at this point, color shader is not needed
return;
}
if (m_bUseHQScaler)
{
m_colorShader = new CYUV2RGBShader();
if (!m_colorShader->Create(m_sourceWidth, m_sourceHeight, m_format))
{
// Try again after disabling the HQ scaler and freeing its resources
m_IntermediateTarget.Release();
SAFE_DELETE(m_scalerShader);
SAFE_DELETE(m_colorShader);
m_bUseHQScaler = false;
}
}
if (!m_bUseHQScaler) //fallback from HQ scalers and multipass creation above
{
m_colorShader = new CYUV2RGBShader();
if (!m_colorShader->Create(m_sourceWidth, m_sourceHeight, m_format))
SAFE_DELETE(m_colorShader);
// we're in big trouble - should fallback on D3D accelerated or sw method
}
}
void CWinRenderer::UpdateVideoFilter()
{
if (m_scalingMethodGui == CMediaSettings::Get().GetCurrentVideoSettings().m_ScalingMethod && m_bFilterInitialized)
return;
m_bFilterInitialized = true;
m_scalingMethodGui = CMediaSettings::Get().GetCurrentVideoSettings().m_ScalingMethod;
m_scalingMethod = m_scalingMethodGui;
if (!Supports(m_scalingMethod))
{
CLog::Log(LOGWARNING, __FUNCTION__" - chosen scaling method %d is not supported by renderer", (int)m_scalingMethod);
m_scalingMethod = VS_SCALINGMETHOD_AUTO;
}
switch(m_renderMethod)
{
case RENDER_SW:
SelectSWVideoFilter();
break;
case RENDER_PS:
case RENDER_DXVA:
SelectPSVideoFilter();
UpdatePSVideoFilter();
break;
default:
return;
}
}
void CWinRenderer::Render(DWORD flags)
{
if (m_renderMethod == RENDER_DXVA)
{
UpdateVideoFilter();
if (m_bUseHQScaler)
g_Windowing.FlushGPU();
CWinRenderer::RenderProcessor(flags);
return;
}
if (!m_VideoBuffers[m_iYV12RenderBuffer]->IsReadyToRender())
return;
UpdateVideoFilter();
// Optimize later? we could get by with bilinear under some circumstances
/*if(!m_bUseHQScaler
|| !g_graphicsContext.IsFullScreenVideo()
|| g_graphicsContext.IsCalibrating()
|| (m_destRect.Width() == m_sourceWidth && m_destRect.Height() == m_sourceHeight))
*/
CSingleLock lock(g_graphicsContext);
// Don't need a stencil/depth buffer and a buffer smaller than the render target causes D3D complaints and nVidia issues
// Save & restore when we're done.
LPDIRECT3DSURFACE9 pZBuffer;
LPDIRECT3DDEVICE9 pD3DDevice = g_Windowing.Get3DDevice();
pD3DDevice->GetDepthStencilSurface(&pZBuffer);
pD3DDevice->SetDepthStencilSurface(NULL);
if (m_renderMethod == RENDER_SW)
RenderSW();
else if (m_renderMethod == RENDER_PS)
RenderPS();
pD3DDevice->SetDepthStencilSurface(pZBuffer);
pZBuffer->Release();
}
void CWinRenderer::RenderSW()
{
enum PixelFormat format = PixelFormatFromFormat(m_format);
// 1. convert yuv to rgb
m_sw_scale_ctx = sws_getCachedContext(m_sw_scale_ctx,
m_sourceWidth, m_sourceHeight, format,
m_sourceWidth, m_sourceHeight, PIX_FMT_BGRA,
SWS_FAST_BILINEAR | SwScaleCPUFlags(), NULL, NULL, NULL);
YUVBuffer* buf = (YUVBuffer*)m_VideoBuffers[m_iYV12RenderBuffer];
D3DLOCKED_RECT srclr[MAX_PLANES];
uint8_t *src[MAX_PLANES];
int srcStride[MAX_PLANES];
for (unsigned int idx = 0; idx < buf->GetActivePlanes(); idx++)
{
if(!(buf->planes[idx].texture.LockRect(0, &srclr[idx], NULL, D3DLOCK_READONLY)))
CLog::Log(LOGERROR, __FUNCTION__" - failed to lock yuv textures into memory");
else
{
src[idx] = (uint8_t*)srclr[idx].pBits;
srcStride[idx] = srclr[idx].Pitch;
}
}
D3DLOCKED_RECT destlr = {0,0};
if (!m_SWTarget.LockRect(0, &destlr, NULL, D3DLOCK_DISCARD))
CLog::Log(LOGERROR, __FUNCTION__" - failed to lock swtarget texture into memory");
uint8_t *dst[] = { (uint8_t*) destlr.pBits, 0, 0, 0 };
int dstStride[] = { destlr.Pitch, 0, 0, 0 };
sws_scale(m_sw_scale_ctx, src, srcStride, 0, m_sourceHeight, dst, dstStride);
for (unsigned int idx = 0; idx < buf->GetActivePlanes(); idx++)
if(!(buf->planes[idx].texture.UnlockRect(0)))
CLog::Log(LOGERROR, __FUNCTION__" - failed to unlock yuv textures");
if (!m_SWTarget.UnlockRect(0))
CLog::Log(LOGERROR, __FUNCTION__" - failed to unlock swtarget texture");
// 2. scale to display
// Don't know where this martian comes from but it can happen in the initial frames of a video
if ((m_destRect.x1 < 0 && m_destRect.x2 < 0) || (m_destRect.y1 < 0 && m_destRect.y2 < 0))
return;
ScaleFixedPipeline();
}
/*
Code kept for reference, as a basis to re-enable StretchRect and
do the color conversion with it.
See IDirect3D9::CheckDeviceFormat() for support of non-standard fourcc textures
and IDirect3D9::CheckDeviceFormatConversion for color conversion support
void CWinRenderer::ScaleStretchRect()
{
// Test HW scaler support. StretchRect is slightly faster than drawing a quad.
// If linear filtering is not supported, drop back to quads, as most HW has linear filtering for quads.
//if(m_deviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES
//&& m_deviceCaps.StretchRectFilterCaps & D3DPTFILTERCAPS_MINFLINEAR
//&& m_deviceCaps.StretchRectFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
//{
// m_StretchRectSupported = true;
//}
CRect sourceRect = m_sourceRect;
CRect destRect = m_destRect;
D3DSURFACE_DESC desc;
if (FAILED(target->GetDesc(&desc)))
CLog::Log(LOGERROR, "CWinRenderer::Render - failed to get back buffer description");
CRect tgtRect(0, 0, desc.Width, desc.Height);
// Need to manipulate the coordinates since StretchRect doesn't accept off-screen coordinates.
CWIN32Util::CropSource(sourceRect, destRect, tgtRect);
RECT srcRect = { sourceRect.x1, sourceRect.y1, sourceRect.x2, sourceRect.y2 };
IDirect3DSurface9* source;
if(!m_SWTarget.GetSurfaceLevel(0, &source))
CLog::Log(LOGERROR, "CWinRenderer::Render - failed to get source");
RECT dstRect = { destRect.x1, destRect.y1, destRect.x2, destRect.y2 };
IDirect3DSurface9* target;
if(FAILED(g_Windowing.Get3DDevice()->GetRenderTarget(0, &target)))
CLog::Log(LOGERROR, "CWinRenderer::Render - failed to get back buffer");
HRESULT hr;
LPDIRECT3DDEVICE9 pD3DDevice = g_Windowing.Get3DDevice();
if(FAILED(hr = pD3DDevice->StretchRect(source, &srcRect, target, &dstRect, m_TextureFilter)))
CLog::Log(LOGERROR, __FUNCTION__" - StretchRect failed (0x%08X)", hr);
target->Release();
source->Release();
}
*/
void CWinRenderer::ScaleFixedPipeline()
{
HRESULT hr;
IDirect3DDevice9 *pD3DDev = g_Windowing.Get3DDevice();
D3DSURFACE_DESC srcDesc;
if (FAILED(hr = m_SWTarget.Get()->GetLevelDesc(0, &srcDesc)))
CLog::Log(LOGERROR, __FUNCTION__": GetLevelDesc failed. %s", CRenderSystemDX::GetErrorDescription(hr).c_str());
float srcWidth = (float)srcDesc.Width;
float srcHeight = (float)srcDesc.Height;
bool cbcontrol = (CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast != 50.0f || CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness != 50.0f);
unsigned int contrast = (unsigned int)(CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast *.01f * 255.0f); // we have to divide by two here/multiply by two later
unsigned int brightness = (unsigned int)(CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness * .01f * 255.0f);
D3DCOLOR diffuse = D3DCOLOR_ARGB(255, contrast, contrast, contrast);
D3DCOLOR specular = D3DCOLOR_ARGB(255, brightness, brightness, brightness);
struct VERTEX
{
FLOAT x,y,z,rhw;
D3DCOLOR diffuse;
D3DCOLOR specular;
FLOAT tu, tv;
};
// Vertex format ignores viewport offsets, so correct for that here
CRect dest = g_graphicsContext.StereoCorrection(m_destRect);
VERTEX vertex[] =
{
{dest.x1, dest.y1, 0.0f, 1.0f, diffuse, specular, m_sourceRect.x1 / srcWidth, m_sourceRect.y1 / srcHeight},
{dest.x2, dest.y1, 0.0f, 1.0f, diffuse, specular, m_sourceRect.x2 / srcWidth, m_sourceRect.y1 / srcHeight},
{dest.x2, dest.y2, 0.0f, 1.0f, diffuse, specular, m_sourceRect.x2 / srcWidth, m_sourceRect.y2 / srcHeight},
{dest.x1, dest.y2, 0.0f, 1.0f, diffuse, specular, m_sourceRect.x1 / srcWidth, m_sourceRect.y2 / srcHeight},
};
// Compensate for D3D coordinates system
for(int i = 0; i < ARRAY_SIZE(vertex); i++)
{
vertex[i].x -= 0.5f;
vertex[i].y -= 0.5f;
};
pD3DDev->SetTexture(0, m_SWTarget.Get());
if (!cbcontrol)
{
pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pD3DDev->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
pD3DDev->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
}
else
{
pD3DDev->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE2X );
pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pD3DDev->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pD3DDev->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
pD3DDev->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED );
pD3DDev->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );
pD3DDev->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_SPECULAR );
pD3DDev->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pD3DDev->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
pD3DDev->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
pD3DDev->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
}
pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pD3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);
pD3DDev->SetRenderState(D3DRS_ZENABLE, FALSE);
pD3DDev->SetRenderState(D3DRS_STENCILENABLE, FALSE);
pD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
pD3DDev->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
pD3DDev->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
pD3DDev->SetSamplerState(0, D3DSAMP_MAGFILTER, m_TextureFilter);
pD3DDev->SetSamplerState(0, D3DSAMP_MINFILTER, m_TextureFilter);
pD3DDev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
pD3DDev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
pD3DDev->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1);
if (FAILED(hr = pD3DDev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertex, sizeof(VERTEX))))
CLog::Log(LOGERROR, __FUNCTION__": DrawPrimitiveUP failed. %s", CRenderSystemDX::GetErrorDescription(hr).c_str());
pD3DDev->SetTexture(0, NULL);
}
void CWinRenderer::RenderPS()
{
if (!m_bUseHQScaler)
{
Stage1();
}
else
{
Stage1();
Stage2();
}
}
void CWinRenderer::Stage1()
{
if (!m_bUseHQScaler)
{
m_colorShader->Render(m_sourceRect, g_graphicsContext.StereoCorrection(m_destRect),
CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast,
CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness,
m_iFlags,
(YUVBuffer*)m_VideoBuffers[m_iYV12RenderBuffer]);
}
else
{
// Switch the render target to the temporary destination
LPDIRECT3DDEVICE9 pD3DDevice = g_Windowing.Get3DDevice();
LPDIRECT3DSURFACE9 newRT, oldRT;
m_IntermediateTarget.GetSurfaceLevel(0, &newRT);
pD3DDevice->GetRenderTarget(0, &oldRT);
pD3DDevice->SetRenderTarget(0, newRT);
CRect srcRect(0.0f, 0.0f, m_sourceWidth, m_sourceHeight);
CRect rtRect(0.0f, 0.0f, m_sourceWidth, m_sourceHeight);
m_colorShader->Render(srcRect, rtRect,
CMediaSettings::Get().GetCurrentVideoSettings().m_Contrast,
CMediaSettings::Get().GetCurrentVideoSettings().m_Brightness,
m_iFlags,
(YUVBuffer*)m_VideoBuffers[m_iYV12RenderBuffer]);
// Restore the render target
pD3DDevice->SetRenderTarget(0, oldRT);
// MSDN says: Setting a new render target will cause the viewport
// to be set to the full size of the new render target.
// So we need restore our viewport
g_Windowing.RestoreViewPort();
oldRT->Release();
newRT->Release();
}
}