File: NstDirect2D.hpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (791 lines) | stat: -rw-r--r-- 15,290 bytes parent folder | download | duplicates (3)
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
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia 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 Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#ifndef NST_DIRECTX_DIRECT2D_H
#define NST_DIRECTX_DIRECT2D_H

#pragma once

#include <set>
#include <vector>
#include "NstWindowRect.hpp"
#include "NstDirectX.hpp"

#ifdef NST_DEBUG
#define D3D_DEBUG_INFO
#endif

#include <d3d9.h>
#include <d3dx9.h>

namespace Nestopia
{
	namespace DirectX
	{
		class Direct2D
		{
		public:

			explicit Direct2D(HWND);
			~Direct2D();

			typedef Window::Rect Rect;
			typedef Window::Point Point;

			enum
			{
				MAX_SCANLINES = 100
			};

			struct Mode
			{
				typedef std::set<uchar> Rates;

				explicit Mode(uint=0,uint=0,uint=0);

				enum
				{
					MIN_WIDTH = 256,
					MIN_HEIGHT = 240,
					DEFAULT_RATE = 60,
					MAX_RATE = 120
				};

				bool operator == (const Mode&) const;
				bool operator <  (const Mode&) const;

				uint width, height, bpp;
				Rates rates;

				bool operator != (const Mode& mode) const
				{
					return !(*this == mode);
				}

				Point Size() const
				{
					return Point( width, height );
				}
			};

			struct Adapter : BaseAdapter
			{
				typedef std::set<Mode> Modes;

				enum DeviceType
				{
					DEVICE_HAL,
					DEVICE_HEL
				};

				enum Filter
				{
					FILTER_NONE,
					FILTER_BILINEAR
				};

				/** used to identify multiple monitors on the same video adapter */
				uint guidIndex;
				uint ordinal;
				DeviceType deviceType;
				Point maxScreenSize;
				bool videoMemScreen;
				bool anyTextureSize;
				bool canDoScanlineEffect;
				bool intervalTwo;
				bool intervalThree;
				bool intervalFour;
				bool modern;
				uint filters;
				Modes modes;
			};

			typedef std::vector<Adapter> Adapters;

			enum
			{
				RENDER_PICTURE = 0x01,
				RENDER_FPS     = 0x04,
				RENDER_MSG     = 0x08,
				RENDER_NFO     = 0x10
			};

			void SelectAdapter(const Adapters::const_iterator);
			void RenderScreen(uint);
			bool CanSwitchFullscreen(const Adapter::Modes::const_iterator) const;
			bool SwitchFullscreen(const Adapter::Modes::const_iterator);
			bool SwitchWindowed();
			void UpdateWindowView();
			void UpdateWindowView(const Point&,const Rect&,uint,int,Adapter::Filter,bool);
			void UpdateFullscreenView(const Rect&,const Point&,const Rect&,uint,int,Adapter::Filter,bool);
			void UpdateFrameRate(uint,bool,bool);
			void EnableDialogBoxMode(bool);
			bool Repair();

			enum ScreenShotResult
			{
				SCREENSHOT_OK,
				SCREENSHOT_UNSUPPORTED,
				SCREENSHOT_ERROR
			};

			ScreenShotResult SaveScreenShot(wcstring,uint) const;

		private:

			enum
			{
				INVALID_RECT = MAKE_HRESULT(SEVERITY_ERROR,0x123,2),
				TSL_PATCHES = 32
			};

			void FlushObjects();
			void InvalidateObjects();
			void ValidateObjects();

			class Base
			{
			public:

				Base();
				~Base();

				inline operator IDirect3D9& () const;

				static uint FormatToBpp(D3DFORMAT);
				static void FormatToMask(D3DFORMAT,ulong&,ulong&,ulong&);

			private:

				static IDirect3D9& Create();
				static const Adapters EnumerateAdapters(IDirect3D9&);

				IDirect3D9& com;
				const Adapters adapters;

			public:

				const Adapters& GetAdapters() const
				{
					return adapters;
				}

				const Adapter& GetAdapter(uint i) const
				{
					NST_ASSERT( i < adapters.size() );
					return adapters[i];
				}
			};

			class Device
			{
			public:

				Device(HWND,const Base&);

				void Create(IDirect3D9&,const Adapter&);

				bool CanSwitchFullscreen(const Mode&) const;
				bool CanToggleDialogBoxMode(bool) const;
				bool ResetFrameRate(uint,bool,bool,const Base&);
				uint GetMaxMessageLength() const;

				void SwitchFullscreen(const Mode&);
				void SwitchWindowed();

				NST_SINGLE_CALL HRESULT RenderScreen(uint,uint,uint) const;

				HRESULT ResetWindowClient(const Point&,HRESULT);
				HRESULT ToggleDialogBoxMode();
				HRESULT Repair(HRESULT);
				HRESULT Reset();

				inline operator IDirect3DDevice9& () const;

			private:

				void  Prepare() const;
				void  LogDisplaySwitch() const;
				uint  GetRefreshRate() const;
				DWORD GetPresentationFlags() const;
				D3DSWAPEFFECT GetSwapEffect() const;
				uint  GetDesiredPresentationRate(const Mode&) const;
				DWORD GetDesiredPresentationInterval(uint) const;
				DWORD GetDesiredPresentationInterval() const;
				bool  GetDisplayMode(D3DDISPLAYMODE&) const;

				struct Timing
				{
					Timing();

					bool autoHz;
					bool vsync;
					bool tripleBuffering;
					uchar frameRate;
				};

				class Fonts
				{
				public:

					Fonts();

					void Create(const Device&);
					void Destroy(bool);
					void OnReset() const;
					void OnLost() const;

					NST_SINGLE_CALL void Render(const D3DPRESENT_PARAMETERS&,uint) const;

				private:

					class Font
					{
					public:

						void Create(const Device&);
						void Destroy();
						void Update(const GenericString&);
						uint Width() const;
						void OnReset() const;
						void OnLost() const;

						inline bool CanDraw() const;
						inline void Draw(D3DCOLOR,DWORD,Rect) const;

					private:

						ComInterface<ID3DXFont> com;
						HeapString string;
						uint length;

					public:

						void Clear()
						{
							length = 0;
						}
					};

					Font fps;
					Font msg;
					Font nfo;
					uint width;

				public:

					void UpdateFps(const GenericString& string)
					{
						fps.Update( string );
					}

					void ClearFps()
					{
						fps.Clear();
					}

					void UpdateMsg(const GenericString& string)
					{
						msg.Update( string );
					}

					void ClearMsg()
					{
						msg.Clear();
					}

					void UpdateNfo(const GenericString& string)
					{
						nfo.Update( string );
					}

					void ClearNfo()
					{
						nfo.Clear();
					}

					uint Width() const
					{
						return width;
					}
				};

				ComInterface<IDirect3DDevice9> com;
				Timing timing;
				Fonts fonts;
				uchar ordinal;
				uchar intervalTwo;
				uchar intervalThree;
				uchar intervalFour;
				D3DPRESENT_PARAMETERS presentation;
				bool dialogBoxMode;

			public:

				uint GetOrdinal() const
				{
					return ordinal;
				}

				HRESULT ClearScreen() const
				{
					return com->Clear( 0, NULL, D3DCLEAR_TARGET, 0, 1.f, 0 );
				}

				HRESULT PresentScreen() const
				{
					return com->Present( NULL, NULL, NULL, NULL );
				}

				const D3DPRESENT_PARAMETERS& GetPresentation() const
				{
					return presentation;
				}

				void DrawFps(const GenericString& string)
				{
					fonts.UpdateFps( string );
				}

				void ClearFps()
				{
					fonts.ClearFps();
				}

				void DrawMsg(const GenericString& string)
				{
					fonts.UpdateMsg( string );
				}

				void ClearMsg()
				{
					fonts.ClearMsg();
				}

				void DrawNfo(const GenericString& string)
				{
					fonts.UpdateNfo( string );
				}

				void ClearNfo()
				{
					fonts.ClearNfo();
				}

				void EnableAutoFrequency(bool enable)
				{
					timing.autoHz = enable;
				}

				bool ThrottleRequired(uint speed) const
				{
					return
					(
						presentation.PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE ||
						speed != timing.frameRate
					);
				}

				bool SmoothFrameRate() const
				{
					return presentation.PresentationInterval != D3DPRESENT_INTERVAL_IMMEDIATE;
				}
			};

			class Textures
			{
			public:

				explicit Textures(D3DFORMAT);

				void Update(const Adapter&,const Point&,uint,Adapter::Filter,bool);
				HRESULT Validate(IDirect3DDevice9&,const Adapter&,D3DFORMAT);
				void Invalidate();
				void Flush();
				bool SaveToFile(wcstring,D3DXIMAGE_FILEFORMAT) const;

				inline double GetScreenLeftU(uint) const;
				inline double GetScreenRightU(uint) const;
				inline double GetScreenTopV(uint) const;
				inline double GetScreenBottomV(uint) const;
				inline double GetEffectLeftU(uint) const;
				inline double GetEffectRightU(uint) const;
				inline double GetEffectTopV(uint) const;
				inline double GetEffectBottomV(uint) const;

			private:

				class Texture : public ImplicitBool<Texture>
				{
				public:

					void Invalidate();

				protected:

					explicit Texture(uint,D3DFORMAT);
					~Texture();

					bool Validate(IDirect3DDevice9&,D3DFORMAT,bool);

					static uint GetSquared(const Point&);

					ComInterface<IDirect3DTexture9> com;
					Object::Pod<D3DSURFACE_DESC> desc;
					Point size;
					const uint stage;

				public:

					bool operator ! () const
					{
						return !com;
					}

					void GetBitMask(ulong& r,ulong& g,ulong& b) const
					{
						Base::FormatToMask( desc.Format, r, g, b );
					}

					uint GetBitsPerPixel() const
					{
						return Base::FormatToBpp( desc.Format );
					}

					NST_FORCE_INLINE HRESULT Lock(D3DLOCKED_RECT& lockedRect) const
					{
						const RECT rect = {0,0,size.x,size.y};

						if (com)
						{
							return com->LockRect
							(
								0,
								&lockedRect,
								(desc.Usage & D3DUSAGE_DYNAMIC) ? NULL : &rect,
								(desc.Usage & D3DUSAGE_DYNAMIC) ? (D3DLOCK_DISCARD|D3DLOCK_NOSYSLOCK) : D3DLOCK_NOSYSLOCK
							);
						}
						else
						{
							return D3DERR_DEVICELOST;
						}
					}

					NST_FORCE_INLINE void Unlock() const
					{
						com->UnlockRect( 0 );
					}
				};

				class ScreenTexture : public Texture
				{
				public:

					explicit ScreenTexture(D3DFORMAT);

					void Update(const Adapter&,Point,bool);
					void Flush();
					bool Validate(IDirect3DDevice9&,D3DFORMAT,const Adapter&);
					bool SaveToFile(wcstring,D3DXIMAGE_FILEFORMAT) const;

					inline double GetLeftU(uint) const;
					inline double GetRightU(uint) const;
					inline double GetTopV(uint) const;
					inline double GetBottomV(uint) const;

				private:

					bool useVidMem;
				};

				class EffectTexture : public Texture
				{
				public:

					explicit EffectTexture(D3DFORMAT);

					void Update(const Adapter&,Point,uint);
					bool Validate(IDirect3DDevice9&,D3DFORMAT);

					inline double GetLeftU(uint) const;
					inline double GetRightU(uint) const;
					inline double GetTopV(uint) const;
					inline double GetBottomV(uint) const;

				private:

					uint scanlines;
					bool dirty;
				};

				ScreenTexture screenTexture;
				EffectTexture effectTexture;
				Adapter::Filter filter;

			public:

				NST_FORCE_INLINE HRESULT LockScreen(void*& data,long& pitch) const
				{
					D3DLOCKED_RECT lockedRect;
					const HRESULT hResult = screenTexture.Lock( lockedRect );

					if (SUCCEEDED(hResult))
					{
						data = lockedRect.pBits;
						pitch = lockedRect.Pitch;
					}

					return hResult;
				}

				NST_FORCE_INLINE void UnlockScreen() const
				{
					screenTexture.Unlock();
				}

				void GetScreenBitMask(ulong& r,ulong& g,ulong& b) const
				{
					screenTexture.GetBitMask( r, g, b );
				}

				uint GetScreenBitsPerPixel() const
				{
					return screenTexture.GetBitsPerPixel();
				}
			};

			class VertexBuffer
			{
			public:

				VertexBuffer();
				~VertexBuffer();

				void Update(const Rect&,const Rect&,int);
				HRESULT Validate(IDirect3DDevice9&,const Textures&);
				void Invalidate();

				inline uint NumVertices() const;

			private:

				enum
				{
					FVF = D3DFVF_XYZRHW|D3DFVF_TEX2
				};

				#pragma pack(push,1)

				struct Vertex
				{
					float x,y,z,rhw,u0,v0,u1,v1;
				};

				#pragma pack(pop)

				NST_COMPILE_ASSERT( sizeof(Vertex) == 32 );

				ComInterface<IDirect3DVertexBuffer9> com;
				Rect rect;
				Rect clip;
				uint numVertices;
				int screenCurvature;
				bool dirty;

			public:

				const Rect& GetRect() const
				{
					return rect;
				}
			};

			class IndexBuffer
			{
			public:

				IndexBuffer();
				~IndexBuffer();

				void Update(bool);
				HRESULT Validate(IDirect3DDevice9&);
				void Invalidate();

				inline uint NumStrips() const;

			private:

				ComInterface<IDirect3DIndexBuffer9> com;
				uint numStrips;
			};

			Base base;
			Device device;
			Textures textures;
			VertexBuffer vertexBuffer;
			IndexBuffer indexBuffer;
			HRESULT lastResult;

		public:

			bool ValidScreen() const
			{
				return SUCCEEDED(lastResult);
			}

			bool Windowed() const
			{
				return device.GetPresentation().Windowed;
			}

			bool ThrottleRequired(uint speed) const
			{
				return device.ThrottleRequired( speed ) || FAILED(lastResult);
			}

			const Adapters& GetAdapters() const
			{
				return base.GetAdapters();
			}

			const Adapter& GetAdapter() const
			{
				return base.GetAdapter( device.GetOrdinal() );
			}

			uint GetBitsPerPixel() const
			{
				return textures.GetScreenBitsPerPixel();
			}

			void GetBitMask(ulong& r,ulong& g,ulong& b) const
			{
				textures.GetScreenBitMask( r, g, b );
			}

			const Rect& GetScreenRect() const
			{
				return vertexBuffer.GetRect();
			}

			NST_FORCE_INLINE bool LockScreen(void*& data,long& pitch)
			{
				if (SUCCEEDED(lastResult))
					lastResult = textures.LockScreen( data, pitch );

				return SUCCEEDED(lastResult);
			}

			NST_FORCE_INLINE void UnlockScreen() const
			{
				NST_VERIFY( SUCCEEDED(lastResult) );
				textures.UnlockScreen();
			}

			bool ClearScreen()
			{
				if (SUCCEEDED(lastResult))
				{
					lastResult = device.ClearScreen();
					return SUCCEEDED(lastResult);
				}
				else
				{
					return lastResult == INVALID_RECT;
				}
			}

			bool PresentScreen()
			{
				if (SUCCEEDED(lastResult))
				{
					lastResult = device.PresentScreen();
					return SUCCEEDED(lastResult);
				}
				else
				{
					return lastResult == INVALID_RECT;
				}
			}

			void DrawFps(const GenericString& string)
			{
				device.DrawFps( string );
			}

			void ClearFps()
			{
				device.ClearFps();
			}

			void DrawMsg(const GenericString& string)
			{
				device.DrawMsg( string );
			}

			void ClearMsg()
			{
				device.ClearMsg();
			}

			void DrawNfo(const GenericString& string)
			{
				device.DrawNfo( string );
			}

			void ClearNfo()
			{
				device.ClearNfo();
			}

			void EnableAutoFrequency(bool enable)
			{
				device.EnableAutoFrequency( enable );
			}

			uint GetMaxMessageLength() const
			{
				return device.GetMaxMessageLength();
			}

			const Point GetFullscreenDisplayMode() const
			{
				return Point( device.GetPresentation().BackBufferWidth, device.GetPresentation().BackBufferHeight );
			}

			bool ModernGPU() const
			{
				return base.GetAdapters()[device.GetOrdinal()].modern;
			}

			bool SmoothFrameRate() const
			{
				return device.SmoothFrameRate();
			}
		};
	}
}

#endif