openMSX
V9990SDLRasterizer.cc
Go to the documentation of this file.
2#include "V9990.hh"
3#include "RawFrame.hh"
4#include "PostProcessor.hh"
5#include "Display.hh"
6#include "OutputSurface.hh"
7#include "RenderSettings.hh"
8#include "MemoryOps.hh"
9#include "enumerate.hh"
10#include "one_of.hh"
11#include "xrange.hh"
12#include "build-info.hh"
13#include "components.hh"
14#include <algorithm>
15#include <cstdint>
16#include <memory>
17
18namespace openmsx {
19
20template<std::unsigned_integral Pixel>
22 V9990& vdp_, Display& display, OutputSurface& screen_,
23 std::unique_ptr<PostProcessor> postProcessor_)
24 : vdp(vdp_), vram(vdp.getVRAM())
25 , screen(screen_)
26 , workFrame(std::make_unique<RawFrame>(screen.getPixelFormat(), 1280, 240))
27 , renderSettings(display.getRenderSettings())
28 , displayMode(P1) // dummy value
29 , colorMode(PP) // avoid UMR
30 , postProcessor(std::move(postProcessor_))
31 , bitmapConverter(vdp, palette64, palette64_32768, palette256, palette256_32768, palette32768)
32 , p1Converter(vdp, palette64)
33 , p2Converter(vdp, palette64)
34{
35 // Fill palettes
37
38 renderSettings.getGammaSetting() .attach(*this);
39 renderSettings.getBrightnessSetting() .attach(*this);
40 renderSettings.getContrastSetting() .attach(*this);
41 renderSettings.getColorMatrixSetting().attach(*this);
42}
43
44template<std::unsigned_integral Pixel>
46{
47 renderSettings.getColorMatrixSetting().detach(*this);
48 renderSettings.getGammaSetting() .detach(*this);
49 renderSettings.getBrightnessSetting() .detach(*this);
50 renderSettings.getContrastSetting() .detach(*this);
51}
52
53template<std::unsigned_integral Pixel>
55{
56 return postProcessor.get();
57}
58
59template<std::unsigned_integral Pixel>
61{
62 return postProcessor->needRender() &&
63 vdp.getMotherBoard().isActive() &&
64 !vdp.getMotherBoard().isFastForwarding();
65}
66
67template<std::unsigned_integral Pixel>
69{
70 setDisplayMode(vdp.getDisplayMode());
71 setColorMode(vdp.getColorMode());
72 resetPalette();
73}
74
75template<std::unsigned_integral Pixel>
77{
78 const V9990DisplayPeriod& horTiming = vdp.getHorizontalTiming();
79 const V9990DisplayPeriod& verTiming = vdp.getVerticalTiming();
80
81 // Center image on the window.
82
83 // In SDLLo, one window pixel represents 8 UC clockticks, so the
84 // window = 320 * 8 UC ticks wide. In SDLHi, one pixel is 4 clock-
85 // ticks and the window 640 pixels wide -- same amount of UC ticks.
86 colZero = horTiming.blank + horTiming.border1 +
87 (horTiming.display - SCREEN_WIDTH * 8) / 2;
88
89 // 240 display lines can be shown. In SDLHi, we can do interlace,
90 // but still 240 lines per frame.
91 lineRenderTop = verTiming.blank + verTiming.border1 +
92 (verTiming.display - SCREEN_HEIGHT) / 2;
93}
94
95template<std::unsigned_integral Pixel>
96void V9990SDLRasterizer<Pixel>::frameEnd(EmuTime::param time)
97{
98 workFrame = postProcessor->rotateFrames(std::move(workFrame), time);
99 workFrame->init(
100 vdp.isInterlaced() ? (vdp.getEvenOdd() ? RawFrame::FIELD_EVEN
103}
104
105template<std::unsigned_integral Pixel>
107{
108 displayMode = mode;
109 bitmapConverter.setColorMode(colorMode, displayMode);
110}
111
112template<std::unsigned_integral Pixel>
114{
115 colorMode = mode;
116 bitmapConverter.setColorMode(colorMode, displayMode);
117}
118
119template<std::unsigned_integral Pixel>
121 int fromX, int fromY, int limitX, int limitY)
122{
123 Pixel bgColor = vdp.isOverScan()
124 ? 0
125 : palette64[vdp.getBackDropColor() & 63];
126
127 int startY = std::max(fromY - lineRenderTop, 0);
128 int endY = std::min(limitY - lineRenderTop, 240);
129 if (startY >= endY) return;
130
131 if ((fromX == 0) && (limitX == V9990DisplayTiming::UC_TICKS_PER_LINE)) {
132 // optimization
133 for (auto y : xrange(startY, endY)) {
134 workFrame->setBlank(y, bgColor);
135 }
136 return;
137 }
138
139 static int const screenW = SCREEN_WIDTH * 8; // in ticks
140 int startX = std::max(0, V9990::UCtoX(fromX - colZero, displayMode));
141 int endX = V9990::UCtoX(
143 ? screenW : std::min(screenW, limitX - colZero), displayMode);
144 if (startX >= endX) return;
145
146 unsigned lineWidth = vdp.getLineWidth();
148 for (auto y : xrange(startY, endY)) {
149 memset(workFrame->getLinePtrDirect<Pixel>(y) + startX,
150 endX - startX, bgColor);
151 workFrame->setLineWidth(y, lineWidth);
152 }
153}
154
155template<std::unsigned_integral Pixel>
157 int fromX, int fromY, int toX, int toY,
158 int displayX, int displayY, int displayYA, int displayYB)
159{
160 static int const screenW = SCREEN_WIDTH * 8;
161 static int const screenH = SCREEN_HEIGHT;
162
163 // from VDP coordinates to screen coordinates
164 fromX -= colZero;
165 toX -= colZero;
166 fromY -= lineRenderTop;
167 toY -= lineRenderTop;
168
169 // Clip to screen
170 if (fromX < 0) {
171 displayX -= fromX;
172 fromX = 0;
173 }
174 if (fromY < 0) {
175 displayY -= fromY;
176 displayYA -= fromY;
177 displayYB -= fromY;
178 fromY = 0;
179 }
180 if (toX > screenW) {
181 toX = screenW;
182 }
183 if (toY > screenH) {
184 toY = screenH;
185 }
186 fromX = V9990::UCtoX(fromX, displayMode);
187 toX = V9990::UCtoX(toX, displayMode);
188
189 if ((toX > fromX) && (toY > fromY)) {
190 bool drawSprites = vdp.spritesEnabled() &&
191 !renderSettings.getDisableSprites();
192
193 displayX = V9990::UCtoX(displayX, displayMode);
194 int displayWidth = toX - fromX;
195 int displayHeight = toY - fromY;
196
197 if (displayMode == P1) {
198 drawP1Mode(fromX, fromY, displayX,
199 displayY, displayYA, displayYB,
200 displayWidth, displayHeight,
201 drawSprites);
202 } else if (displayMode == P2) {
203 drawP2Mode(fromX, fromY, displayX,
204 displayY, displayYA,
205 displayWidth, displayHeight,
206 drawSprites);
207 } else {
208 drawBxMode(fromX, fromY, displayX,
209 displayY, displayYA,
210 displayWidth, displayHeight,
211 drawSprites);
212 }
213 }
214}
215
216template<std::unsigned_integral Pixel>
218 int fromX, int fromY, int displayX,
219 int displayY, int displayYA, int displayYB,
220 int displayWidth, int displayHeight, bool drawSprites)
221{
222 while (displayHeight--) {
223 Pixel* pixelPtr = workFrame->getLinePtrDirect<Pixel>(fromY) + fromX;
224 p1Converter.convertLine(pixelPtr, displayX, displayWidth,
225 displayY, displayYA, displayYB,
226 drawSprites);
227 workFrame->setLineWidth(fromY, 320);
228 ++fromY;
229 ++displayY;
230 ++displayYA;
231 ++displayYB;
232 }
233}
234
235template<std::unsigned_integral Pixel>
237 int fromX, int fromY, int displayX, int displayY, int displayYA,
238 int displayWidth, int displayHeight, bool drawSprites)
239{
240 while (displayHeight--) {
241 Pixel* pixelPtr = workFrame->getLinePtrDirect<Pixel>(fromY) + fromX;
242 p2Converter.convertLine(pixelPtr, displayX, displayWidth,
243 displayY, displayYA, drawSprites);
244 workFrame->setLineWidth(fromY, 640);
245 ++fromY;
246 ++displayY;
247 ++displayYA;
248 }
249}
250
251template<std::unsigned_integral Pixel>
253 int fromX, int fromY, int displayX, int displayY, int displayYA,
254 int displayWidth, int displayHeight, bool drawSprites)
255{
256 unsigned scrollX = vdp.getScrollAX();
257 unsigned x = displayX + scrollX;
258
259 int lineStep = 1;
260 if (vdp.isEvenOddEnabled()) {
261 if (vdp.getEvenOdd()) {
262 ++displayY;
263 ++displayYA;
264 }
265 lineStep = 2;
266 }
267
268 unsigned scrollY = vdp.getScrollAY();
269 unsigned rollMask = vdp.getRollMask(0x1FFF);
270 unsigned scrollYBase = scrollY & ~rollMask & 0x1FFF;
271 int cursorY = displayY - vdp.getCursorYOffset();
272 while (displayHeight--) {
273 // Note: convertLine() can draw up to 3 pixels too many. But
274 // that's ok, the buffer is big enough: buffer can hold 1280
275 // pixels, max displayWidth is 1024 pixels. When taking the
276 // position of the borders into account, the display area
277 // plus 3 pixels cannot go beyond the end of the buffer.
278 unsigned y = scrollYBase + ((displayYA + scrollY) & rollMask);
279 Pixel* pixelPtr = workFrame->getLinePtrDirect<Pixel>(fromY) + fromX;
280 bitmapConverter.convertLine(pixelPtr, x, y, displayWidth,
281 cursorY, drawSprites);
282 workFrame->setLineWidth(fromY, vdp.getLineWidth());
283 ++fromY;
284 displayYA += lineStep;
285 cursorY += lineStep;
286 }
287}
288
289
290template<std::unsigned_integral Pixel>
292{
293 // the 32768 color palette
294 if (renderSettings.isColorMatrixIdentity()) {
295 // Most users use the "normal" monitor type; making this a
296 // special case speeds up palette precalculation a lot.
297 int intensity[32];
298 for (auto [i, r] : enumerate(intensity)) {
299 r = int(255 * renderSettings.transformComponent(i / 31.0f));
300 }
301 for (auto [grb, col] : enumerate(palette32768)) {
302 col = screen.mapKeyedRGB255<Pixel>(gl::ivec3(
303 intensity[(grb >> 5) & 31],
304 intensity[(grb >> 10) & 31],
305 intensity[(grb >> 0) & 31]));
306 }
307 } else {
308 for (auto g : xrange(32)) {
309 for (auto r : xrange(32)) {
310 for (auto b : xrange(32)) {
311 palette32768[(g << 10) + (r << 5) + b] =
312 screen.mapRGB(renderSettings.transformRGB(
313 gl::vec3(r, g, b) / 31.0f));
314 }
315 }
316 }
317 }
318
319 // the 256 color palette
320 int mapRG[8] = { 0, 4, 9, 13, 18, 22, 27, 31 };
321 int mapB [4] = { 0, 11, 21, 31 };
322 for (auto g : xrange(8)) {
323 for (auto r : xrange(8)) {
324 for (auto b : xrange(4)) {
325 auto idx256 = (g << 5) | (r << 2) | b;
326 auto idx32768 = (mapRG[g] << 10) | (mapRG[r] << 5) | mapB[b];
327 palette256_32768[idx256] = idx32768;
328 palette256[idx256] = palette32768[idx32768];
329 }
330 }
331 }
332
333 resetPalette();
334}
335
336template<std::unsigned_integral Pixel>
338 byte r, byte g, byte b, bool ys)
339{
340 auto idx32768 = ((g & 31) << 10) | ((r & 31) << 5) | ((b & 31) << 0);
341 palette64_32768[index & 63] = idx32768; // TODO what with ys?
342 palette64[index & 63] = ys ? screen.getKeyColor<Pixel>()
343 : palette32768[idx32768];
344}
345
346template<std::unsigned_integral Pixel>
348{
349 // get 64 color palette from VDP
350 for (auto i : xrange(64)) {
351 auto [r, g, b, ys] = vdp.getPalette(i);
352 setPalette(i, r, g, b, ys);
353 }
354 palette256[0] = vdp.isSuperimposing() ? screen.getKeyColor<Pixel>()
355 : palette32768[0];
356 // TODO what with palette256_32768[0]?
357}
358
359template<std::unsigned_integral Pixel>
361{
362 resetPalette();
363}
364
365template<std::unsigned_integral Pixel>
367{
368 return postProcessor->isRecording();
369}
370
371template<std::unsigned_integral Pixel>
373{
374 if (&setting == one_of(&renderSettings.getGammaSetting(),
375 &renderSettings.getBrightnessSetting(),
376 &renderSettings.getContrastSetting(),
377 &renderSettings.getColorMatrixSetting())) {
378 preCalcPalettes();
379 }
380}
381
382// Force template instantiation.
383#if HAVE_16BPP
384template class V9990SDLRasterizer<uint16_t>;
385#endif
386#if HAVE_32BPP || COMPONENT_GL
387template class V9990SDLRasterizer<uint32_t>;
388#endif
389
390} // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:27
int g
Definition: one_of.hh:7
Represents the output window/screen of openMSX.
Definition: Display.hh:33
@ FIELD_NONINTERLACED
Interlacing is off for this frame.
Definition: FrameSource.hh:24
@ FIELD_EVEN
Interlacing is on and this is an even frame.
Definition: FrameSource.hh:27
@ FIELD_ODD
Interlacing is on and this is an odd frame.
Definition: FrameSource.hh:30
A frame buffer where pixels can be written to.
Abstract base class for post processors.
A video frame as output by the VDP scanline conversion unit, before any postprocessing filters are ap...
Definition: RawFrame.hh:15
FloatSetting & getContrastSetting()
Contrast video setting.
FloatSetting & getBrightnessSetting()
Brightness video setting.
FloatSetting & getGammaSetting()
The amount of gamma correction.
StringSetting & getColorMatrixSetting()
Color matrix setting.
void attach(Observer< T > &observer)
Definition: Subject.hh:50
static constexpr int UC_TICKS_PER_LINE
The number of clockticks per line is independent of the crystal used or the display mode (NTSC/PAL)
void drawP1Mode(int fromX, int fromY, int displayX, int displayY, int displayYA, int displayYB, int displayWidth, int displayHeight, bool drawSprites)
void frameStart() override
Indicates the start of a new frame.
void setPalette(int index, byte r, byte g, byte b, bool ys) override
Set RGB values for a palette entry.
void reset() override
Resynchronize with VDP - flush caches etc.
void setSuperimpose(bool enabled) override
Is superimpose enabled?
bool isActive() override
Will the output of this Rasterizer be displayed? There is no point in producing a frame that will not...
bool isRecording() const override
Is video recording active?
void drawBxMode(int fromX, int fromY, int displayX, int displayY, int displayYA, int displayWidth, int displayHeight, bool drawSprites)
void drawBorder(int fromX, int fromY, int limitX, int limitY) override
Render a rectangle of border pixels on the host screen.
void drawDisplay(int fromX, int fromY, int toX, int toY, int displayX, int displayY, int displayYA, int displayYB) override
Render a rectangle of display pixels on the host screen.
void drawP2Mode(int fromX, int fromY, int displayX, int displayY, int displayYA, int displayWidth, int displayHeight, bool drawSprites)
PostProcessor * getPostProcessor() const override
See V9990::getPostProcessor().
void setDisplayMode(V9990DisplayMode displayMode) override
The display mode determines the screens geometry and how V9990 pixels are mapped to pixels on screen.
void update(const Setting &setting) noexcept override
void preCalcPalettes()
Fill the palettes.
void frameEnd(EmuTime::param time) override
Indicates the end of the current frame.
void setColorMode(V9990ColorMode colorMode) override
The color mode determines how the V9990 VRAM data gets converted to pixel colors.
V9990SDLRasterizer(V9990 &vdp, Display &display, OutputSurface &screen, std::unique_ptr< PostProcessor > postProcessor)
Implementation of the Yamaha V9990 VDP as used in the GFX9000 cartridge by Sunrise.
Definition: V9990.hh:32
static int UCtoX(int ticks, V9990DisplayMode mode)
Convert UC ticks to V9990 pixel position on a line.
Definition: V9990.hh:171
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
vecN< 3, int > ivec3
Definition: gl_vec.hh:152
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:265
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:283
This file implemented 3 utility functions:
Definition: Autofire.cc:9
uint32_t Pixel
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:127
STL namespace.
A period, either horizontal or vertical, starts with a synchronisation pulse followed by a blank peri...
constexpr auto xrange(T e)
Definition: xrange.hh:133