forked from snes9xgit/snes9x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDirect3D.cpp
1075 lines (888 loc) · 29.5 KB
/
CDirect3D.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
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
Jerremy Koot ([email protected])
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman ([email protected])
(c) Copyright 2002 - 2006 funkyass ([email protected]),
Kris Bleakley ([email protected])
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
Nach ([email protected]),
(c) Copyright 2002 - 2011 zones ([email protected])
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2016 BearOso,
OV2
(c) Copyright 2011 - 2016 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
Nach,
zsKnight ([email protected])
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive ([email protected]),
Gary Henderson,
Ivar ([email protected]),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti ([email protected])
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare ([email protected]),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault ([email protected]),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green ([email protected])
S-SMP emulator code used in 1.54+
(c) Copyright 2016 byuu
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin ([email protected])
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2016 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2016 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Libretro port
(c) Copyright 2011 - 2016 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or distribute Snes9x in both binary
and source form, for non-commercial purposes, is hereby granted without
fee, providing that this license information and copyright notice appear
with all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
***********************************************************************************/
#pragma comment( lib, "d3d9" )
#pragma comment( lib, "d3dx9" )
#include "cdirect3d.h"
#include "win32_display.h"
#include "../snes9x.h"
#include "../gfx.h"
#include "../display.h"
#include "wsnes9x.h"
#include <Dxerr.h>
#include <commctrl.h>
#include "CXML.h"
#include "../filter/hq2x.h"
#include "../filter/2xsai.h"
#ifndef max
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
const D3DVERTEXELEMENT9 CDirect3D::vertexElems[4] = {
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
D3DDECL_END()
};
/* CDirect3D::CDirect3D()
sets default values for the variables
*/
CDirect3D::CDirect3D()
{
init_done = false;
pD3D = NULL;
pDevice = NULL;
drawSurface = NULL;
vertexBuffer = NULL;
afterRenderWidth = 0;
afterRenderHeight = 0;
quadTextureSize = 0;
fullscreen = false;
filterScale = 1;
for(int i = 0; i < MAX_SHADER_TEXTURES; i++) {
rubyLUT[i] = NULL;
}
effect=NULL;
shader_type = D3D_SHADER_NONE;
shaderTimer = 1.0f;
shaderTimeStart = 0;
shaderTimeElapsed = 0;
frameCount = 0;
cgContext = NULL;
cgAvailable = false;
cgShader = NULL;
vertexDeclaration = NULL;
}
/* CDirect3D::~CDirect3D()
releases allocated objects
*/
CDirect3D::~CDirect3D()
{
DeInitialize();
}
/* CDirect3D::Initialize
Initializes Direct3D (always in window mode)
IN:
hWnd - the HWND of the window in which we render/the focus window for fullscreen
-----
returns true if successful, false otherwise
*/
bool CDirect3D::Initialize(HWND hWnd)
{
if(init_done)
return true;
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(pD3D == NULL) {
DXTRACE_ERR_MSGBOX(TEXT("Error creating initial D3D9 object"), 0);
return false;
}
memset(&dPresentParams, 0, sizeof(dPresentParams));
dPresentParams.hDeviceWindow = hWnd;
dPresentParams.Windowed = true;
dPresentParams.BackBufferCount = GUI.DoubleBuffered?2:1;
dPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
dPresentParams.BackBufferFormat = D3DFMT_UNKNOWN;
HRESULT hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_MIXED_VERTEXPROCESSING,
&dPresentParams,
&pDevice);
if(FAILED(hr)) {
DXTRACE_ERR_MSGBOX(TEXT("Error creating D3D9 device"), hr);
return false;
}
hr = pDevice->CreateVertexBuffer(sizeof(vertexStream),D3DUSAGE_WRITEONLY,0,D3DPOOL_MANAGED,&vertexBuffer,NULL);
if(FAILED(hr)) {
DXTRACE_ERR_MSGBOX(TEXT("Error creating vertex buffer"), hr);
return false;
}
hr = pDevice->CreateVertexDeclaration(vertexElems,&vertexDeclaration);
if(FAILED(hr)) {
DXTRACE_ERR_MSGBOX(TEXT("Error creating vertex declaration"), hr);
return false;
}
cgAvailable = loadCgFunctions();
if(cgAvailable) {
cgContext = cgCreateContext();
hr = cgD3D9SetDevice(pDevice);
if(FAILED(hr)) {
DXTRACE_ERR_MSGBOX(TEXT("Error setting cg device"), hr);
}
cgShader = new CD3DCG(cgContext,pDevice);
}
pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->SetRenderState( D3DRS_ZENABLE, FALSE);
pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
init_done = true;
ApplyDisplayChanges();
return true;
}
void CDirect3D::DeInitialize()
{
DestroyDrawSurface();
SetShader(NULL);
if(cgShader) {
delete cgShader;
cgShader = NULL;
}
if(cgContext) {
cgDestroyContext(cgContext);
cgContext = NULL;
}
if(cgAvailable)
cgD3D9SetDevice(NULL);
if(vertexBuffer) {
vertexBuffer->Release();
vertexBuffer = NULL;
}
if(vertexDeclaration) {
vertexDeclaration->Release();
vertexDeclaration = NULL;
}
if( pDevice ) {
pDevice->Release();
pDevice = NULL;
}
if( pD3D ) {
pD3D->Release();
pD3D = NULL;
}
init_done = false;
afterRenderWidth = 0;
afterRenderHeight = 0;
quadTextureSize = 0;
fullscreen = false;
filterScale = 0;
if(cgAvailable)
unloadCgLibrary();
cgAvailable = false;
}
bool CDirect3D::SetShader(const TCHAR *file)
{
SetShaderCG(NULL);
SetShaderHLSL(NULL);
shader_type = D3D_SHADER_NONE;
if(file!=NULL &&
(lstrlen(file)>3 && _tcsncicmp(&file[lstrlen(file)-3],TEXT(".cg"),3)==0) ||
(lstrlen(file)>4 && _tcsncicmp(&file[lstrlen(file)-4],TEXT(".cgp"),4)==0)){
return SetShaderCG(file);
} else {
return SetShaderHLSL(file);
}
}
void CDirect3D::checkForCgError(const char *situation)
{
char buffer[4096];
CGerror error = cgGetError();
const char *string = cgGetErrorString(error);
if (error != CG_NO_ERROR) {
sprintf(buffer,
"Situation: %s\n"
"Error: %s\n\n"
"Cg compiler output...\n", situation, string);
MessageBoxA(0, buffer,
"Cg error", MB_OK|MB_ICONEXCLAMATION);
if (error == CG_COMPILER_ERROR) {
MessageBoxA(0, cgGetLastListing(cgContext),
"Cg compilation error", MB_OK|MB_ICONEXCLAMATION);
}
}
}
bool CDirect3D::SetShaderCG(const TCHAR *file)
{
if(!cgAvailable) {
if(file)
MessageBox(NULL, TEXT("The CG runtime is unavailable, CG shaders will not run.\nConsult the snes9x readme for information on how to obtain the runtime."), TEXT("CG Error"),
MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(!cgShader->LoadShader(file))
return false;
shader_type = D3D_SHADER_CG;
return true;
}
bool CDirect3D::SetShaderHLSL(const TCHAR *file)
{
//MUDLORD: the guts
//Compiles a shader from files on disc
//Sets LUT textures to texture files in PNG format.
TCHAR folder[MAX_PATH];
TCHAR rubyLUTfileName[MAX_PATH];
TCHAR *slash;
TCHAR errorMsg[MAX_PATH + 50];
shaderTimer = 1.0f;
shaderTimeStart = 0;
shaderTimeElapsed = 0;
if(effect) {
effect->Release();
effect = NULL;
}
for(int i = 0; i < MAX_SHADER_TEXTURES; i++) {
if (rubyLUT[i] != NULL) {
rubyLUT[i]->Release();
rubyLUT[i] = NULL;
}
}
if (file == NULL || *file==TEXT('\0'))
return true;
CXML xml;
if(!xml.loadXmlFile(file))
return false;
TCHAR *lang = xml.getAttribute(TEXT("/shader"),TEXT("language"));
if(lstrcmpi(lang,TEXT("hlsl"))) {
_stprintf(errorMsg,TEXT("Shader language is <%s>, expected <HLSL> in file:\n%s"),lang,file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
return false;
}
TCHAR *shaderText = xml.getNodeContent(TEXT("/shader/source"));
if(!shaderText) {
_stprintf(errorMsg,TEXT("No HLSL shader program in file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"),
MB_OK|MB_ICONEXCLAMATION);
return false;
}
LPD3DXBUFFER pBufferErrors = NULL;
#ifdef UNICODE
HRESULT hr = D3DXCreateEffect( pDevice,WideToCP(shaderText,CP_ACP),strlen(WideToCP(shaderText,CP_ACP)),NULL, NULL,
D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect,
&pBufferErrors );
#else
HRESULT hr = D3DXCreateEffect( pDevice,shaderText,strlen(shaderText),NULL, NULL,
D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &effect,
&pBufferErrors );
#endif
if( FAILED(hr) ) {
_stprintf(errorMsg,TEXT("Error parsing HLSL shader file:\n%s"),file);
MessageBox(NULL, errorMsg, TEXT("Shader Loading Error"), MB_OK|MB_ICONEXCLAMATION);
if(pBufferErrors) {
LPVOID pCompilErrors = pBufferErrors->GetBufferPointer();
MessageBox(NULL, (const TCHAR*)pCompilErrors, TEXT("FX Compile Error"),
MB_OK|MB_ICONEXCLAMATION);
}
return false;
}
lstrcpy(folder,file);
slash = _tcsrchr(folder,TEXT('\\'));
if(slash)
*(slash+1)=TEXT('\0');
else
*folder=TEXT('\0');
SetCurrentDirectory(S9xGetDirectoryT(DEFAULT_DIR));
for(int i = 0; i < MAX_SHADER_TEXTURES; i++) {
_stprintf(rubyLUTfileName, TEXT("%srubyLUT%d.png"), folder, i);
hr = D3DXCreateTextureFromFile(pDevice,rubyLUTfileName,&rubyLUT[i]);
if FAILED(hr){
rubyLUT[i] = NULL;
}
}
D3DXHANDLE hTech;
effect->FindNextValidTechnique(NULL,&hTech);
effect->SetTechnique( hTech );
shader_type = D3D_SHADER_HLSL;
return true;
}
void CDirect3D::SetShaderVars(bool setMatrix)
{
if(shader_type == D3D_SHADER_HLSL) {
D3DXVECTOR4 rubyTextureSize;
D3DXVECTOR4 rubyInputSize;
D3DXVECTOR4 rubyOutputSize;
D3DXHANDLE rubyTimer;
int shaderTime = GetTickCount();
shaderTimeElapsed += shaderTime - shaderTimeStart;
shaderTimeStart = shaderTime;
if(shaderTimeElapsed > 100) {
shaderTimeElapsed = 0;
shaderTimer += 0.01f;
}
rubyTextureSize.x = rubyTextureSize.y = (float)quadTextureSize;
rubyTextureSize.z = rubyTextureSize.w = 1.0 / quadTextureSize;
rubyInputSize.x = (float)afterRenderWidth;
rubyInputSize.y = (float)afterRenderHeight;
rubyInputSize.z = 1.0 / rubyInputSize.y;
rubyInputSize.w = 1.0 / rubyInputSize.z;
rubyOutputSize.x = GUI.Stretch?(float)dPresentParams.BackBufferWidth:(float)afterRenderWidth;
rubyOutputSize.y = GUI.Stretch?(float)dPresentParams.BackBufferHeight:(float)afterRenderHeight;
rubyOutputSize.z = 1.0 / rubyOutputSize.y;
rubyOutputSize.w = 1.0 / rubyOutputSize.x;
rubyTimer = effect->GetParameterByName(0, "rubyTimer");
effect->SetFloat(rubyTimer, shaderTimer);
effect->SetVector("rubyTextureSize", &rubyTextureSize);
effect->SetVector("rubyInputSize", &rubyInputSize);
effect->SetVector("rubyOutputSize", &rubyOutputSize);
effect->SetTexture("rubyTexture", drawSurface);
for(int i = 0; i < MAX_SHADER_TEXTURES; i++) {
char rubyLUTName[256];
sprintf(rubyLUTName, "rubyLUT%d", i);
if (rubyLUT[i] != NULL) {
effect->SetTexture( rubyLUTName, rubyLUT[i] );
}
}
}/* else if(shader_type == D3D_SHADER_CG) {
D3DXVECTOR2 videoSize;
D3DXVECTOR2 textureSize;
D3DXVECTOR2 outputSize;
float frameCnt;
videoSize.x = (float)afterRenderWidth;
videoSize.y = (float)afterRenderHeight;
textureSize.x = textureSize.y = (float)quadTextureSize;
outputSize.x = GUI.Stretch?(float)dPresentParams.BackBufferWidth:(float)afterRenderWidth;
outputSize.y = GUI.Stretch?(float)dPresentParams.BackBufferHeight:(float)afterRenderHeight;
frameCnt = (float)++frameCount;
videoSize = textureSize;
#define setProgramUniform(program,varname,floats)\
{\
CGparameter cgp = cgGetNamedParameter(program, varname);\
if(cgp)\
cgD3D9SetUniform(cgp,floats);\
}\
setProgramUniform(cgFragmentProgram,"IN.video_size",&videoSize);
setProgramUniform(cgFragmentProgram,"IN.texture_size",&textureSize);
setProgramUniform(cgFragmentProgram,"IN.output_size",&outputSize);
setProgramUniform(cgFragmentProgram,"IN.frame_count",&frameCnt);
setProgramUniform(cgVertexProgram,"IN.video_size",&videoSize);
setProgramUniform(cgVertexProgram,"IN.texture_size",&textureSize);
setProgramUniform(cgVertexProgram,"IN.output_size",&outputSize);
setProgramUniform(cgVertexProgram,"IN.frame_count",&frameCnt);
if(setMatrix) {
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProj;
D3DXMATRIX mvp;
pDevice->GetTransform(D3DTS_WORLD,&matWorld);
pDevice->GetTransform(D3DTS_VIEW,&matView);
pDevice->GetTransform(D3DTS_PROJECTION,&matProj);
mvp = matWorld * matView * matProj;
D3DXMatrixTranspose(&mvp,&mvp);
CGparameter cgpModelViewProj = cgGetNamedParameter(cgVertexProgram, "modelViewProj");
if(cgpModelViewProj)
cgD3D9SetUniformMatrix(cgpModelViewProj,&mvp);
}
}*/
}
/* CDirect3D::Render
does the actual rendering, changes the draw surface if necessary and recalculates
the vertex information if filter output size changes
IN:
Src - the input surface
*/
void CDirect3D::Render(SSurface Src)
{
SSurface Dst;
RECT dstRect;
unsigned int newFilterScale;
D3DLOCKED_RECT lr;
D3DLOCKED_RECT lrConv;
HRESULT hr;
if(!init_done) return;
//create a new draw surface if the filter scale changes
//at least factor 2 so we can display unscaled hi-res images
newFilterScale = max(2,max(GetFilterScale(GUI.ScaleHiRes),GetFilterScale(GUI.Scale)));
if(newFilterScale!=filterScale) {
ChangeDrawSurfaceSize(newFilterScale);
}
if(FAILED(hr = pDevice->TestCooperativeLevel())) {
switch(hr) {
case D3DERR_DEVICELOST: //do no rendering until device is restored
return;
case D3DERR_DEVICENOTRESET: //we can reset now
ResetDevice();
return;
default:
DXTRACE_ERR_MSGBOX(TEXT("Internal driver error"), hr);
return;
}
}
//BlankTexture(drawSurface);
if(FAILED(hr = drawSurface->LockRect(0, &lr, NULL, 0))) {
DXTRACE_ERR_MSGBOX(TEXT("Unable to lock texture"), hr);
return;
} else {
Dst.Surface = (unsigned char *)lr.pBits;
Dst.Height = quadTextureSize;
Dst.Width = quadTextureSize;
Dst.Pitch = lr.Pitch;
RenderMethod (Src, Dst, &dstRect);
if(!Settings.AutoDisplayMessages) {
WinSetCustomDisplaySurface((void *)Dst.Surface, Dst.Pitch/2, dstRect.right-dstRect.left, dstRect.bottom-dstRect.top, GetFilterScale(CurrentScale));
S9xDisplayMessages ((uint16*)Dst.Surface, Dst.Pitch/2, dstRect.right-dstRect.left, dstRect.bottom-dstRect.top, GetFilterScale(CurrentScale));
}
drawSurface->UnlockRect(0);
}
if(!GUI.Stretch||GUI.AspectRatio)
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//if the output size of the render method changes we need to update the viewport
if(afterRenderHeight != dstRect.bottom || afterRenderWidth != dstRect.right) {
afterRenderHeight = dstRect.bottom;
afterRenderWidth = dstRect.right;
SetViewport();
}
pDevice->SetTexture(0, drawSurface);
pDevice->SetVertexDeclaration(vertexDeclaration);
pDevice->SetStreamSource(0,vertexBuffer,0,sizeof(VERTEX));
if (shader_type == D3D_SHADER_HLSL) {
SetShaderVars();
SetFiltering();
UINT passes;
pDevice->BeginScene();
hr = effect->Begin(&passes, 0);
for(UINT pass = 0; pass < passes; pass++ ) {
effect->BeginPass(pass);
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
effect->EndPass();
}
effect->End();
pDevice->EndScene();
} else {
if(shader_type == D3D_SHADER_CG) {
RECT displayRect;
//Get maximum rect respecting AR setting
displayRect=CalculateDisplayRect(dPresentParams.BackBufferWidth,dPresentParams.BackBufferHeight,
dPresentParams.BackBufferWidth,dPresentParams.BackBufferHeight);
cgShader->Render(drawSurface,
D3DXVECTOR2((float)quadTextureSize, (float)quadTextureSize),
D3DXVECTOR2((float)afterRenderWidth, (float)afterRenderHeight),
D3DXVECTOR2((float)(displayRect.right - displayRect.left),
(float)(displayRect.bottom - displayRect.top)),
D3DXVECTOR2((float)dPresentParams.BackBufferWidth, (float)dPresentParams.BackBufferHeight));
}
SetFiltering();
pDevice->SetVertexDeclaration(vertexDeclaration);
pDevice->BeginScene();
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
pDevice->EndScene();
}
pDevice->Present(NULL, NULL, NULL, NULL);
return;
}
/* CDirect3D::CreateDrawSurface
calculates the necessary texture size (multiples of 2)
and creates a new texture
*/
void CDirect3D::CreateDrawSurface()
{
unsigned int neededSize;
HRESULT hr;
//we need at least 512 pixels (SNES_WIDTH * 2) so we can start with that value
quadTextureSize = 512;
neededSize = SNES_WIDTH * filterScale;
while(quadTextureSize < neededSize)
quadTextureSize *=2;
if(!drawSurface) {
hr = pDevice->CreateTexture(
quadTextureSize, quadTextureSize,
1, // 1 level, no mipmaps
0, // dynamic textures can be locked
D3DFMT_R5G6B5,
D3DPOOL_MANAGED,
&drawSurface,
NULL );
if(FAILED(hr)) {
DXTRACE_ERR_MSGBOX(TEXT("Error while creating texture"), hr);
return;
}
}
}
/* CDirect3D::DestroyDrawSurface
releases the old textures (if allocated)
*/
void CDirect3D::DestroyDrawSurface()
{
if(drawSurface) {
drawSurface->Release();
drawSurface = NULL;
}
}
/* CDirect3D::BlankTexture
clears a texture (fills it with zeroes)
IN:
texture - the texture to be blanked
-----
returns true if successful, false otherwise
*/
bool CDirect3D::BlankTexture(LPDIRECT3DTEXTURE9 texture)
{
D3DLOCKED_RECT lr;
HRESULT hr;
if(FAILED(hr = texture->LockRect(0, &lr, NULL, 0))) {
DXTRACE_ERR_MSGBOX(TEXT("Unable to lock texture"), hr);
return false;
} else {
memset(lr.pBits, 0, lr.Pitch * quadTextureSize);
texture->UnlockRect(0);
return true;
}
}
/* CDirect3D::ChangeDrawSurfaceSize
changes the draw surface size: deletes the old textures, creates a new texture
and calculate new vertices
IN:
scale - the scale that has to fit into the textures
-----
returns true if successful, false otherwise
*/
bool CDirect3D::ChangeDrawSurfaceSize(unsigned int scale)
{
filterScale = scale;
if(pDevice) {
DestroyDrawSurface();
CreateDrawSurface();
SetupVertices();
return true;
}
return false;
}
/* CDirect3D::SetupVertices
calculates the vertex coordinates
(respecting the stretch and aspect ratio settings)
*/
void CDirect3D::SetupVertices()
{
void *pLockedVertexBuffer;
float tX = (float)afterRenderWidth / (float)quadTextureSize;
float tY = (float)afterRenderHeight / (float)quadTextureSize;
vertexStream[0] = VERTEX(0.0f,0.0f,0.0f,0.0f,tY,0.0f,0.0f);
vertexStream[1] = VERTEX(0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,0.0f);
vertexStream[2] = VERTEX(1.0f,0.0f,0.0f,tX,tY,0.0f,0.0f);
vertexStream[3] = VERTEX(1.0f,1.0f,0.0f,tX,0.0f,0.0f,0.0f);
for(int i=0;i<4;i++) {
vertexStream[i].x -= 0.5f / (float)dPresentParams.BackBufferWidth;
vertexStream[i].y += 0.5f / (float)dPresentParams.BackBufferHeight;
}
HRESULT hr = vertexBuffer->Lock(0,0,&pLockedVertexBuffer,NULL);
memcpy(pLockedVertexBuffer,vertexStream,sizeof(vertexStream));
vertexBuffer->Unlock();
}
void CDirect3D::SetViewport()
{
D3DXMATRIX matIdentity;
D3DXMATRIX matProjection;
D3DXMatrixOrthoOffCenterLH(&matProjection,0.0f,1.0f,0.0f,1.0f,0.0f,1.0f);
D3DXMatrixIdentity(&matIdentity);
pDevice->SetTransform(D3DTS_WORLD,&matIdentity);
pDevice->SetTransform(D3DTS_VIEW,&matIdentity);
pDevice->SetTransform(D3DTS_PROJECTION,&matProjection);
SetShaderVars(true);
RECT drawRect = CalculateDisplayRect(afterRenderWidth,afterRenderHeight,dPresentParams.BackBufferWidth,dPresentParams.BackBufferHeight);
D3DVIEWPORT9 viewport;
viewport.X = drawRect.left;
viewport.Y = drawRect.top;
viewport.Height = drawRect.bottom - drawRect.top;
viewport.Width = drawRect.right - drawRect.left;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
HRESULT hr = pDevice->SetViewport(&viewport);
SetupVertices();
}
/* CDirect3D::ChangeRenderSize
determines if we need to reset the device (if the size changed)
called with (0,0) whenever we want new settings to take effect
IN:
newWidth,newHeight - the new window size
-----
returns true if successful, false otherwise
*/
bool CDirect3D::ChangeRenderSize(unsigned int newWidth, unsigned int newHeight)
{
if(!init_done)
return false;
//if we already have the desired size no change is necessary
//during fullscreen no changes are allowed
if(dPresentParams.BackBufferWidth == newWidth && dPresentParams.BackBufferHeight == newHeight)
return true;
if(!ResetDevice())
return false;
return true;
}
/* CDirect3D::ResetDevice
resets the device
called if surface was lost or the settings/display size require a device reset
-----
returns true if successful, false otherwise
*/
bool CDirect3D::ResetDevice()
{
if(!init_done) return false;
HRESULT hr;
//release prior to reset
DestroyDrawSurface();
if(cgAvailable) {
cgShader->OnLostDevice();
cgD3D9SetDevice(NULL);
}
if(effect)
effect->OnLostDevice();
//zero or unknown values result in the current window size/display settings
dPresentParams.BackBufferWidth = 0;
dPresentParams.BackBufferHeight = 0;
dPresentParams.BackBufferCount = GUI.DoubleBuffered?2:1;
dPresentParams.BackBufferFormat = D3DFMT_UNKNOWN;
dPresentParams.FullScreen_RefreshRateInHz = 0;
dPresentParams.Windowed = true;
dPresentParams.PresentationInterval = GUI.Vsync?D3DPRESENT_INTERVAL_ONE:D3DPRESENT_INTERVAL_IMMEDIATE;
if(fullscreen) {
dPresentParams.BackBufferWidth = GUI.FullscreenMode.width;
dPresentParams.BackBufferHeight = GUI.FullscreenMode.height;
dPresentParams.BackBufferCount = GUI.DoubleBuffered?2:1;
dPresentParams.Windowed = false;
if(GUI.FullscreenMode.depth == 32)
dPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
else
dPresentParams.BackBufferFormat = D3DFMT_R5G6B5;
dPresentParams.FullScreen_RefreshRateInHz = GUI.FullscreenMode.rate;
}
if(FAILED(hr = pDevice->Reset(&dPresentParams))) {
DXTRACE_ERR(TEXT("Unable to reset device"), hr);
return false;
}
if(effect)
effect->OnResetDevice();
if(cgAvailable) {
cgD3D9SetDevice(pDevice);
cgShader->OnResetDevice();
}
pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//recreate the surface
CreateDrawSurface();
SetViewport();
return true;
}
/* CDirect3D::SetSnes9xColorFormat
sets the color format to 16bit (since the texture is always 16bit)
no depth conversion is necessary (done by D3D)
*/
void CDirect3D::SetSnes9xColorFormat()
{
GUI.ScreenDepth = 16;
GUI.BlueShift = 0;
GUI.GreenShift = 6;
GUI.RedShift = 11;
S9xSetRenderPixelFormat (RGB565);
S9xBlit2xSaIFilterInit();
S9xBlitHQ2xFilterInit();
GUI.NeedDepthConvert = FALSE;
GUI.DepthConverted = TRUE;
return;
}
/* CDirect3D::SetFullscreen
enables/disables fullscreen mode
IN:
fullscreen - determines if fullscreen is enabled/disabled
-----
returns true if successful, false otherwise
*/
bool CDirect3D::SetFullscreen(bool fullscreen)
{
if(!init_done)
return false;
if(this->fullscreen==fullscreen)
return true;
this->fullscreen = fullscreen;
if(!ResetDevice())
return false;