aboutsummaryrefslogtreecommitdiff
path: root/backends/graphics/psp2sdl/psp2sdl-graphics.cpp
blob: 222cf0c6efb2c584d3a9d59393de280d8ad64990 (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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
 * of the License, 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "common/scummsys.h"

#if defined(SDL_BACKEND)

#include "backends/graphics/psp2sdl/psp2sdl-graphics.h"
#include "backends/events/sdl/sdl-events.h"
#include "backends/platform/sdl/sdl.h"
#include "common/config-manager.h"
#include "common/mutex.h"
#include "common/textconsole.h"
#include "common/translation.h"
#include "common/util.h"
#include "common/frac.h"
#ifdef USE_RGB_COLOR
#include "common/list.h"
#endif
#include "graphics/font.h"
#include "graphics/fontman.h"
#include "graphics/scaler.h"
#include "graphics/scaler/aspect.h"
#include "graphics/surface.h"
#include "gui/EventRecorder.h"

#include <vita2d_fbo.h>
#include <lcd3x_v.h>
#include <lcd3x_f.h>
#include <texture_v.h>
#include <texture_f.h>
#include <advanced_aa_v.h>
#include <advanced_aa_f.h>
#include <scale2x_f.h>
#include <scale2x_v.h>
#include <sharp_bilinear_f.h>
#include <sharp_bilinear_v.h>
#include <sharp_bilinear_simple_f.h>
#include <sharp_bilinear_simple_v.h>

#define GFX_SHADER_NONE 0
#define GFX_SHADER_LCD3X 1
#define GFX_SHADER_SHARP 2
#define GFX_SHADER_SHARP_SCAN 3
#define GFX_SHADER_AAA 4
#define GFX_SHADER_SCALE2X 5

static const OSystem::GraphicsMode s_supportedShadersPSP2[] = {
	{"NONE", "Normal (no shader)", GFX_SHADER_NONE},
	{"LCD", "LCD", GFX_SHADER_LCD3X},
	{"Sharp", "Sharp", GFX_SHADER_SHARP},
	{"Scan", "Scan", GFX_SHADER_SHARP_SCAN},
	{"AAA", "Super2xSAI", GFX_SHADER_AAA},
	{"Scale", "Scale", GFX_SHADER_SCALE2X},
	{0, 0, 0}
};

PSP2SdlGraphicsManager::PSP2SdlGraphicsManager(SdlEventSource *sdlEventSource, SdlWindow *window)
	:
	SurfaceSdlGraphicsManager(sdlEventSource, window),
	_vitatex_hwscreen(nullptr),
	_sdlpixels_hwscreen(nullptr) {

	// do aspect ratio correction in hardware on the Vita
	if (_videoMode.aspectRatioCorrection == true) {
		_hardwareAspectRatioCorrection = true;
	} else {
		_hardwareAspectRatioCorrection = false;
	}
	_videoMode.aspectRatioCorrection = false;

	// shader number 0 is the entry NONE (no shader)
	const OSystem::GraphicsMode *p = s_supportedShadersPSP2;
	_numShaders = 0;
	while (p->name) { 
		_numShaders++;
		p++;
	}
	_currentShader = ConfMan.getInt("shader");
	if (_currentShader < 0 || _currentShader >= _numShaders) {
		_currentShader = 0;
	}

	_shaders[0] = NULL;
}

PSP2SdlGraphicsManager::~PSP2SdlGraphicsManager() {
	if (_vitatex_hwscreen) {
		vita2d_free_texture(_vitatex_hwscreen);
		for (int i = 0; i < 6; i++) {
			vita2d_free_shader(_shaders[i]);
			_shaders[i] = NULL;
		}
		_vitatex_hwscreen = NULL;
	}
	if (_hwScreen) {
		_hwScreen->pixels = _sdlpixels_hwscreen;
	}
	_sdlpixels_hwscreen = nullptr;
}

OSystem::TransactionError PSP2SdlGraphicsManager::endGFXTransaction() {
		OSystem::TransactionError returnValue = SurfaceSdlGraphicsManager::endGFXTransaction();

		// force update of filtering on Vita
		PSP2_UpdateFiltering();

		return returnValue;
}

void PSP2SdlGraphicsManager::setGraphicsModeIntern() {
	SurfaceSdlGraphicsManager::setGraphicsModeIntern();
	PSP2_UpdateFiltering();
}

void PSP2SdlGraphicsManager::PSP2_UpdateFiltering() {
	if (_vitatex_hwscreen) {
		if (_videoMode.filtering) {
			vita2d_texture_set_filters(_vitatex_hwscreen, SCE_GXM_TEXTURE_FILTER_LINEAR, SCE_GXM_TEXTURE_FILTER_LINEAR);
		} else {
			vita2d_texture_set_filters(_vitatex_hwscreen, SCE_GXM_TEXTURE_FILTER_POINT, SCE_GXM_TEXTURE_FILTER_POINT);
		}
	}
}

const OSystem::GraphicsMode *PSP2SdlGraphicsManager::getSupportedShaders() const {
	return s_supportedShadersPSP2;
}

void PSP2SdlGraphicsManager::unloadGFXMode() {
	if (_screen) {
		SDL_FreeSurface(_screen);
		_screen = NULL;
	}

	deinitializeRenderer();

	if (_hwScreen) {
		if (_vitatex_hwscreen) {
			vita2d_free_texture(_vitatex_hwscreen);
			for (int i = 0; i < 6; i++) {
				vita2d_free_shader(_shaders[i]);
				_shaders[i] = NULL;
			}
			_vitatex_hwscreen = NULL;
		}
		_hwScreen->pixels = _sdlpixels_hwscreen;
	}
	SurfaceSdlGraphicsManager::unloadGFXMode();
}

bool PSP2SdlGraphicsManager::hotswapGFXMode() {
	if (!_screen)
		return false;

	// Release the HW screen surface
	if (_hwScreen) {
		if (_vitatex_hwscreen) {
			vita2d_free_texture(_vitatex_hwscreen);
			for (int i = 0; i < 6; i++) {
				vita2d_free_shader(_shaders[i]);
				_shaders[i] = NULL;
			}
			_vitatex_hwscreen = NULL;
		}
		_hwScreen->pixels = _sdlpixels_hwscreen;
	}
	return SurfaceSdlGraphicsManager::hotswapGFXMode();
}

void PSP2SdlGraphicsManager::updateShader() {
// shader init code goes here
// currently only used on Vita port
// the user-selected shaderID should be obtained via ConfMan.getInt("shader")
// and the corresponding shader should then be activated here
// this way the user can combine any software scaling (scalers)
// with any hardware shading (shaders). The shaders could provide
// scanline masks, overlays, but could also serve for
// hardware-based up-scaling (sharp-bilinear-simple, etc.)
	if (_vitatex_hwscreen) {
		if (_shaders[0] == NULL) { 
			// load shaders
			_shaders[GFX_SHADER_NONE] = vita2d_create_shader((const SceGxmProgram *)texture_v, (const SceGxmProgram *)texture_f);
			_shaders[GFX_SHADER_LCD3X] = vita2d_create_shader((const SceGxmProgram *)lcd3x_v, (const SceGxmProgram *)lcd3x_f);
			_shaders[GFX_SHADER_SHARP] = vita2d_create_shader((const SceGxmProgram *)sharp_bilinear_simple_v, (const SceGxmProgram *)sharp_bilinear_simple_f);
			_shaders[GFX_SHADER_SHARP_SCAN] = vita2d_create_shader((const SceGxmProgram *)sharp_bilinear_v, (const SceGxmProgram *)sharp_bilinear_f);
			_shaders[GFX_SHADER_AAA] = vita2d_create_shader((const SceGxmProgram *)advanced_aa_v, (const SceGxmProgram *)advanced_aa_f);
			_shaders[GFX_SHADER_SCALE2X] = vita2d_create_shader((const SceGxmProgram *)scale2x_v, (const SceGxmProgram *)scale2x_f);
		}
		if (_currentShader >= 0 && _currentShader < _numShaders) {
			vita2d_texture_set_program(_shaders[_currentShader]->vertexProgram, _shaders[_currentShader]->fragmentProgram);
			vita2d_texture_set_wvp(_shaders[_currentShader]->wvpParam);
			vita2d_texture_set_vertexInput(&_shaders[_currentShader]->vertexInput);
			vita2d_texture_set_fragmentInput(&_shaders[_currentShader]->fragmentInput);
		}
	}
}

void PSP2SdlGraphicsManager::setAspectRatioCorrection(bool enable) {
	Common::StackLock lock(_graphicsMutex);

	if (_oldVideoMode.setup && _hardwareAspectRatioCorrection == enable)
		return;

	if (_transactionMode == kTransactionActive) {
		_videoMode.aspectRatioCorrection = false;
		_hardwareAspectRatioCorrection = enable;
		// erase the screen for both buffers
		if (_vitatex_hwscreen) {
			for (int i = 0; i <= 10; i++) {
				vita2d_start_drawing();
				vita2d_clear_screen();
				vita2d_end_drawing();
				vita2d_swap_buffers();
			}
		}
	}
}

SDL_Surface *PSP2SdlGraphicsManager::SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags) {

	SDL_Surface *screen = SurfaceSdlGraphicsManager::SDL_SetVideoMode(width, height, bpp, flags);
	
	if (screen != nullptr) {
		vita2d_set_vblank_wait(true);
		_vitatex_hwscreen = vita2d_create_empty_texture_format(width, height, SCE_GXM_TEXTURE_FORMAT_R5G6B5);
		_sdlpixels_hwscreen = screen->pixels; // for SDL_FreeSurface...
		screen->pixels = vita2d_texture_get_datap(_vitatex_hwscreen);
		screen->pitch = vita2d_texture_get_stride(_vitatex_hwscreen);
		updateShader();
	}
	return screen;
}

void PSP2SdlGraphicsManager::SDL_UpdateRects(SDL_Surface *screen, int numrects, SDL_Rect *rects) {
	int screenH = screen->h;
	int screenW = screen->w;

	bool fullscreen = _videoMode.fullscreen;
	bool aspectRatioCorrection = _hardwareAspectRatioCorrection;

	const int dispW = 960;
	const int dispH = 544;

	int x, y, w, h;
	float sx, sy;
	float ratio = (float)screenW / (float)screenH;

	if (aspectRatioCorrection && (screenH == 200 || screenH == 400)) {
		ratio = 4.0 / 3.0;
	}

	if (fullscreen || screenH >= dispH) {
		h = dispH;
		w = h * ratio;
	} else {
		if (screenH <= dispH / 2 && screenW <= dispW / 2) {
			// Use Vita hardware 2x scaling if the picture is really small
			// this uses the current shader and filtering mode
			h = screenH * 2;
			w = screenW * 2;
		} else {
			h = screenH;
			w = screenW;
		}
		if (aspectRatioCorrection && (screenH == 200 || screenH == 400)) {
			// stretch the height only if it fits, otherwise make the width smaller
			if (((float)w * (1.0 / ratio)) <= (float)dispH) {
				h = w * (1.0 / ratio);
			} else {
				w = h * ratio;
			}
		}
	}

	x = (dispW - w) / 2;
	y = (dispH - h) / 2;
	sx = (float)w / (float)screenW;
	sy = (float)h / (float)screenH;
	if (_vitatex_hwscreen) {
		vita2d_start_drawing();
		vita2d_draw_texture_scale(_vitatex_hwscreen, x, y, sx, sy);
		vita2d_end_drawing();
		vita2d_swap_buffers();
	}
}

#endif