NstVideoRenderer.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Nestopia - NES/Famicom emulator written in C++
  4. //
  5. // Copyright (C) 2003-2008 Martin Freij
  6. //
  7. // This file is part of Nestopia.
  8. //
  9. // Nestopia is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // Nestopia is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with Nestopia; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24. #ifndef NST_VIDEO_RENDERER_H
  25. #define NST_VIDEO_RENDERER_H
  26. #include <cstdlib>
  27. #include "api/NstApiVideo.hpp"
  28. #include "NstVideoScreen.hpp"
  29. #ifdef NST_PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32. namespace Nes
  33. {
  34. namespace Core
  35. {
  36. namespace Video
  37. {
  38. class Renderer
  39. {
  40. typedef Api::Video::RenderState RenderState;
  41. typedef Api::Video::Decoder Decoder;
  42. typedef Screen Input;
  43. public:
  44. Renderer();
  45. ~Renderer();
  46. enum PaletteType
  47. {
  48. PALETTE_YUV,
  49. PALETTE_PC10,
  50. PALETTE_VS1,
  51. PALETTE_VS2,
  52. PALETTE_VS3,
  53. PALETTE_VS4,
  54. PALETTE_CUSTOM
  55. };
  56. enum
  57. {
  58. WIDTH = Input::WIDTH,
  59. HEIGHT = Input::HEIGHT,
  60. PIXELS = Input::PIXELS,
  61. PALETTE = Input::PALETTE,
  62. DEFAULT_PALETTE = PALETTE_YUV
  63. };
  64. Result SetState(const RenderState&);
  65. Result GetState(RenderState&) const;
  66. Result SetHue(int);
  67. void Blit(Output&,Input&,uint);
  68. Result SetDecoder(const Decoder&);
  69. Result SetPaletteType(PaletteType);
  70. Result LoadCustomPalette(const byte (*)[3],bool);
  71. void ResetCustomPalette();
  72. void EnableFieldMerging(bool);
  73. void EnableForcedFieldMerging(bool);
  74. typedef byte PaletteEntries[PALETTE][3];
  75. const PaletteEntries& GetPalette();
  76. private:
  77. void UpdateFilter(Input&);
  78. class Palette
  79. {
  80. public:
  81. Palette();
  82. ~Palette();
  83. Result SetType(PaletteType);
  84. Result LoadCustom(const byte (*)[3],bool);
  85. uint SaveCustom(byte (*)[3],bool) const;
  86. bool ResetCustom();
  87. void Update(int,int,int,int);
  88. Result SetDecoder(const Decoder&);
  89. inline const PaletteEntries& Get() const;
  90. private:
  91. struct Constants
  92. {
  93. static const double pi;
  94. static const double deg;
  95. static const double levels[2][4];
  96. };
  97. struct Custom
  98. {
  99. inline Custom();
  100. inline ~Custom();
  101. bool EnableEmphasis(bool);
  102. byte palette[64][3];
  103. byte (*emphasis)[64][3];
  104. };
  105. void Generate(int,int,int,int);
  106. void Build(int,int,int,int);
  107. static void GenerateEmphasis(uint,double,double&,double&,double&);
  108. static void Store(const double (&)[3],byte (&)[3]);
  109. PaletteType type;
  110. Custom* custom;
  111. Decoder decoder;
  112. byte palette[64*8][3];
  113. static const byte pc10Palette[64][3];
  114. static const byte vsPalette[4][64][3];
  115. public:
  116. PaletteType GetType() const
  117. {
  118. return type;
  119. }
  120. const Decoder& GetDecoder() const
  121. {
  122. return decoder;
  123. }
  124. bool HasCustomEmphasis() const
  125. {
  126. return custom && custom->emphasis;
  127. }
  128. };
  129. class FilterNone;
  130. class FilterNtsc;
  131. #ifndef NST_NO_SCALEX
  132. class FilterScaleX;
  133. #endif
  134. #ifndef NST_NO_HQ2X
  135. class FilterHqX;
  136. #endif
  137. #ifndef NST_NO_2XSAI
  138. class Filter2xSaI;
  139. #endif
  140. #ifndef NST_NO_XBR
  141. class FilterxBR;
  142. #endif
  143. class NST_NO_VTABLE Filter
  144. {
  145. struct Format
  146. {
  147. explicit Format(const RenderState&);
  148. dword masks[3];
  149. byte shifts[3];
  150. const byte bpp;
  151. };
  152. protected:
  153. explicit Filter(const RenderState&);
  154. public:
  155. virtual ~Filter() {}
  156. virtual void Blit(const Input&,const Output&,uint) = 0;
  157. virtual void Transform(const byte (&)[PALETTE][3],Input::Palette&) const;
  158. const Format format;
  159. uint bgColor;
  160. };
  161. struct State
  162. {
  163. State();
  164. enum
  165. {
  166. UPDATE_PALETTE = 0x1,
  167. UPDATE_FILTER = 0x2,
  168. UPDATE_NTSC = 0x4,
  169. UPDATE_FILTER_STATE = 0x8,
  170. FIELD_MERGING_USER = 0x1,
  171. FIELD_MERGING_FORCED = 0x2
  172. };
  173. word width;
  174. word height;
  175. byte filter;
  176. byte update;
  177. byte fieldMerging;
  178. schar brightness;
  179. schar saturation;
  180. schar hue;
  181. schar contrast;
  182. schar sharpness;
  183. schar resolution;
  184. schar bleed;
  185. schar artifacts;
  186. schar fringing;
  187. schar blendPixels;
  188. schar xbr_corner_rounding;
  189. RenderState::Bits::Mask mask;
  190. };
  191. Result SetLevel(schar&,int,uint=State::UPDATE_PALETTE|State::UPDATE_FILTER);
  192. Filter* filter;
  193. State state;
  194. Palette palette;
  195. public:
  196. uint bgColor;
  197. Result SetBrightness(int brightness)
  198. {
  199. return SetLevel( state.brightness, brightness );
  200. }
  201. Result SetSaturation(int saturation)
  202. {
  203. return SetLevel( state.saturation, saturation );
  204. }
  205. Result SetContrast(int contrast)
  206. {
  207. return SetLevel( state.contrast, contrast );
  208. }
  209. Result SetSharpness(int sharpness)
  210. {
  211. return SetLevel( state.sharpness, sharpness, State::UPDATE_NTSC );
  212. }
  213. Result SetColorResolution(int resolution)
  214. {
  215. return SetLevel( state.resolution, resolution, State::UPDATE_NTSC );
  216. }
  217. Result SetColorBleed(int bleed)
  218. {
  219. return SetLevel( state.bleed, bleed, State::UPDATE_NTSC );
  220. }
  221. Result SetColorArtifacts(int artifacts)
  222. {
  223. return SetLevel( state.artifacts, artifacts, State::UPDATE_NTSC );
  224. }
  225. Result SetColorFringing(int fringing)
  226. {
  227. return SetLevel( state.fringing, fringing, State::UPDATE_NTSC );
  228. }
  229. Result SetBlend(bool correct)
  230. {
  231. return SetLevel( state.blendPixels, correct, State::UPDATE_FILTER_STATE );
  232. }
  233. Result SetCornerRounding(int mode)
  234. {
  235. return SetLevel( state.xbr_corner_rounding, mode, State::UPDATE_FILTER_STATE );
  236. }
  237. /**
  238. * Workaround for black screen issue if this flag is set while the emulator is loading settings
  239. */
  240. void ClearFilterUpdateFlag()
  241. {
  242. //state.update ^= State::UPDATE_FILTER_STATE;
  243. state.update = 1;
  244. }
  245. int GetBlend() const
  246. {
  247. return state.blendPixels;
  248. }
  249. int GetCornerRounding() const
  250. {
  251. return state.xbr_corner_rounding;
  252. }
  253. int GetBrightness() const
  254. {
  255. return state.brightness;
  256. }
  257. int GetSaturation() const
  258. {
  259. return state.saturation;
  260. }
  261. int GetContrast() const
  262. {
  263. return state.contrast;
  264. }
  265. int GetSharpness() const
  266. {
  267. return state.sharpness;
  268. }
  269. int GetColorResolution() const
  270. {
  271. return state.resolution;
  272. }
  273. int GetColorBleed() const
  274. {
  275. return state.bleed;
  276. }
  277. int GetColorArtifacts() const
  278. {
  279. return state.artifacts;
  280. }
  281. int GetColorFringing() const
  282. {
  283. return state.fringing;
  284. }
  285. int GetHue() const
  286. {
  287. return state.hue;
  288. }
  289. bool IsFieldMergingEnabled() const
  290. {
  291. return state.fieldMerging & uint(State::FIELD_MERGING_USER);
  292. }
  293. PaletteType GetPaletteType() const
  294. {
  295. return palette.GetType();
  296. }
  297. bool HasCustomPaletteEmphasis() const
  298. {
  299. return palette.HasCustomEmphasis();
  300. }
  301. uint SaveCustomPalette(byte (*colors)[3],bool emphasis) const
  302. {
  303. return palette.SaveCustom( colors, emphasis );
  304. }
  305. const Decoder& GetDecoder() const
  306. {
  307. return palette.GetDecoder();
  308. }
  309. bool IsReady() const
  310. {
  311. return filter;
  312. }
  313. };
  314. }
  315. }
  316. }
  317. #endif