Compare commits

...
104 Commits
Author SHA1 Message Date
Themaister 844a523706 Should fix error with scaling. 2011-03-30 14:57:45 +02:00
Themaister d05379b340 Should fix FBO issue. 2011-03-30 11:12:27 +02:00
Themaister 006948b973 Warmup for 0.4-beta. Some FBO stuff is still borked. 2011-03-30 11:05:56 +02:00
Themaister e5196026a1 Add more precise error messages. 2011-03-30 00:10:16 +02:00
Themaister 66d18e4ad7 Adhere more closely to XML shader specs. 2011-03-29 23:45:10 +02:00
Themaister b539df1ccd Added settings to ssnes-joyconfig. 2011-03-29 23:40:12 +02:00
Themaister c0117bfbb6 Remove debugging fprintf. 2011-03-29 23:38:11 +02:00
Themaister 074234f182 Oh damn you Windows and your spoony file handling... 2011-03-29 23:21:56 +02:00
Themaister bab03ba8c5 No need for these. 2011-03-29 19:27:53 +02:00
Themaister 4878a283ed Properly deinit Cg. 2011-03-29 19:18:06 +02:00
Themaister d3eed4f8c4 Properly ifdef. 2011-03-29 19:15:02 +02:00
Themaister 647db4d5de Hrm. 2011-03-29 19:13:14 +02:00
Themaister 3455f83fa7 Hopefully fixes awkward issue with scale not being applied correctly. 2011-03-29 19:09:10 +02:00
Themaister f730ba4043 Oh yeah :) Can toggle shader in run-time! 2011-03-29 18:59:06 +02:00
Themaister caf87b1198 Font handling and stuff. 2011-03-29 18:28:31 +02:00
Themaister 1706763a4c Start supporting shaders hiding in a directory. 2011-03-29 18:04:41 +02:00
Themaister c1b8cdb35b Fix issue with size attribute first. 2011-03-27 21:34:33 +02:00
Themaister 936b7f5395 Seems to work now. 2011-03-27 21:21:46 +02:00
Themaister a01e3347f7 Seems to work :) 2011-03-27 20:54:14 +02:00
Themaister 142411c08c Fix some garbage. 2011-03-27 20:33:14 +02:00
Themaister 29808c4421 Start on outscale/size support. 2011-03-27 20:29:47 +02:00
Themaister 48dcd174a7 Add resetting support. (About time, ey?! :D) 2011-03-24 20:41:28 +01:00
Themaister 6c85e6e702 Configurable FBO support. 2011-03-23 23:48:13 +01:00
Themaister e484e22e2f Fix up UPS :) Should work fine now at least for .sfc base roms. 2011-03-23 23:31:33 +01:00
Themaister 8ef54de812 Start adding UPS support. 2011-03-23 12:18:24 +01:00
Themaister c1d5a6e140 Fixes scaling bug for shaders. 2011-03-23 11:05:18 +01:00
Themaister ea153f9df2 Install ssnes-zip 2011-03-20 22:48:13 +01:00
Themaister 1ba9854070 Typo 2011-03-20 21:46:34 +01:00
Themaister 012e955a05 RAR. Untested. 2011-03-20 19:46:04 +01:00
Themaister 37c0b49a99 7z 2011-03-20 19:16:22 +01:00
Themaister de30ce9ef5 Start on a zip loader wrapper. 2011-03-20 13:40:01 +01:00
Themaister 027a21278e Whoops 2011-03-20 02:13:44 +01:00
Themaister f77fe62ab0 Should fix viewport bug when rendering fonts. 2011-03-20 02:10:02 +01:00
Themaister d42c35e1aa Update makefile for win32. 2011-03-19 20:42:49 +01:00
Themaister d5761b6385 Can disable netplay. 2011-03-19 20:41:07 +01:00
Themaister bb3ab3b49c Lots of cleanups for XVideo.
Render video @ 2x scale to combat chroma downsampling. Fix 32-bit
rendering.
2011-03-19 18:53:37 +01:00
Themaister 0441725045 XSync. 2011-03-19 17:19:51 +01:00
Themaister ac4e6e89a4 Bigger buffer for gfx_window_title. 2011-03-19 16:54:29 +01:00
Themaister 12d21cf7a6 Make title handling more sane. 2011-03-26 18:34:58 +01:00
Themaister 3fda3effda Fix crash in XVideo font rendering. 2011-03-26 18:04:18 +01:00
Themaister 7e07b821aa Forgot Makefile.win32 2011-03-17 22:29:43 +01:00
Themaister e502a8b566 Updates for 0.3.2 2011-03-17 22:28:44 +01:00
Themaister ec51ceb010 Add usage of snes_library_id(). 2011-03-17 01:25:44 +01:00
Themaister 3fa037daee Fix minor issues. 2011-03-16 21:48:44 +01:00
Themaister 46c6955cfe Fix odd viewport problem with shaders. 2011-03-16 12:47:31 +01:00
Themaister 252425c35c Some cleanups. 2011-03-16 12:28:02 +01:00
Themaister d309d472e1 Code cleanup. Accumulate scale values. 2011-03-15 18:14:01 +01:00
Themaister 898c4e8c6c Works quite decently now. :) 2011-03-14 23:59:31 +01:00
Themaister 56ac7b4649 Something's working :D 2011-03-14 23:48:19 +01:00
Themaister 9dd05b7101 New params, but is broken for now. 2011-03-14 23:20:51 +01:00
Themaister e2daab9ce9 Fetch some scale values (this is not in the standard (yet!)) 2011-03-14 22:51:03 +01:00
Themaister ef19ea7a85 Add fbo structures, ready to implement. :v 2011-03-14 22:09:35 +01:00
Themaister 01cf24f15f Add filter attributes. 2011-03-14 21:28:30 +01:00
Themaister f870080497 Render fonts. Very dirty. :( 2011-03-13 20:52:06 +01:00
Themaister c5942ec5a2 Fix Jack pkgconf version. 2011-03-13 18:09:27 +01:00
Themaister 50d78fb804 Endian fixes 2011-03-13 15:49:00 +01:00
Themaister 8053aca3c3 Render 32-bit 2011-03-13 15:28:57 +01:00
Themaister 0f3171edea Title 2011-03-13 15:11:53 +01:00
Themaister 6b478b9dda Fullscreen :D 2011-03-13 14:46:27 +01:00
Themaister caaa9cea40 Hide cursor 2011-03-13 14:26:22 +01:00
Themaister ea7f8f5552 Add proper close handling. 2011-03-13 14:12:43 +01:00
Themaister 69caca6017 Fixes for resize. 2011-03-13 13:11:33 +01:00
Themaister 703cfadf63 Fix up X input. 2011-03-13 12:02:06 +01:00
Themaister adfd461c3e Start on XVideo 2011-03-13 04:51:09 +01:00
Themaister bb79dbaa98 Getting ready for an eventual XVideo driver. 2011-03-13 00:44:49 +01:00
Themaister 394de09907 Should hopefully fix flickering :v 2011-03-12 19:28:56 +01:00
Themaister 560cafccaf Futile attempt to try and fix font flickering. 2011-03-12 19:09:25 +01:00
Themaister 0b09032164 Hrm 2011-03-12 18:42:59 +01:00
Themaister 1ee7162784 It's not copypasta anymore :D 2011-03-12 17:01:57 +01:00
Themaister 4949a131f8 Multi-passing in XML should work. 2011-03-12 16:33:01 +01:00
Themaister 8b241c3deb Preparation for new XML shader specs. 2011-03-12 15:30:57 +01:00
Themaister 78c457b996 Update Roar driver. 2011-03-11 19:23:32 +01:00
Themaister 0be1b0b88b More build fixes. 2011-03-07 20:22:55 +01:00
Themaister 3ef0276b2a Will probably not need, but hey. 2011-03-07 20:17:27 +01:00
Themaister 42d19f1a35 Fix build for Win32. 2011-03-07 20:05:03 +01:00
Themaister 1cf9381cb0 Fix colormaps and stuff. 2011-03-07 19:56:40 +01:00
Themaister f94dc125e5 An initial take on bsnes CPU filters. 2011-03-07 19:12:14 +01:00
Themaister 96b978d55a Start on modular CPU filters. Abandon the old CPU filter stuff. 2011-03-07 17:22:03 +01:00
Themaister ec367d333d Add error message when FBO fails. 2011-03-07 00:03:10 +01:00
Themaister 9535097894 Fix build for Win32. 2011-03-06 22:18:25 +01:00
Themaister b541c27226 FBO seems to work! :D 2011-03-06 19:56:35 +01:00
Themaister 41b2af66d5 Add settings. 2011-03-06 18:19:31 +01:00
Themaister 84b2b9c7a5 Fix up Jack deprecation. 2011-03-06 16:58:33 +01:00
Themaister 52c8d96dca FBOs. Hacky atm, need more flexibility. 2011-03-06 16:52:49 +01:00
Themaister 8c6519a53b Should fix an awkward bug with Quickbuild. 2011-03-03 22:06:58 +01:00
Themaister 336adb9f85 Preparations for 0.3.1 2011-03-01 21:45:04 +01:00
Themaister 7053006e77 Add more verbose messages, should fix netplay hangup on Win32. 2011-03-01 14:46:58 +01:00
Themaister bbf4804d12 Obey CFLAGS and LDFLAGS when testing libs (nice when profiling.) 2011-02-28 22:30:06 +01:00
Themaister 7c16424ba3 Shader enabling, and automatic resolution detection. 2011-02-28 16:59:31 +01:00
Themaister de9dc686ac Add hicontrast. 2011-02-27 20:42:02 +01:00
Themaister 069e9b574f Some waterpaint shader :) 2011-02-27 01:16:33 +01:00
Themaister d5319a879a Had missed out pause binding completely. 2011-02-27 00:24:50 +01:00
Themaister f64880aee1 Try glFlush() to remedy occational font flicker... 2011-02-26 23:56:45 +01:00
Themaister fed1857918 Might fix odd font flickering. 2011-02-26 19:32:59 +01:00
Themaister 9aeb40155e Add a comment or two. 2011-02-26 18:49:41 +01:00
Themaister e49aeb1071 Remove redundant define. 2011-02-26 18:48:17 +01:00
Themaister 13f8ec6f3b Added "nul" binds. 2011-02-26 18:47:32 +01:00
Themaister f75c6df270 Should force granularity to 1 when using movies. 2011-02-26 15:09:04 +01:00
Themaister 60b0afce95 Is this it? :D 2011-02-26 01:13:52 +01:00
Themaister f24653768f It does work, somewhat! :D 2011-02-26 00:59:17 +01:00
Themaister f135972a6b Arghh... This is so goddamn annoying :( 2011-02-26 00:31:13 +01:00
Themaister 42c68e21a2 Movie rewind still very broken :( 2011-02-25 12:37:05 +01:00
Themaister e8865060ca A start on movie rewind. 2011-02-25 11:47:27 +01:00
Themaister b07827a856 Should fix GLSL issue on OSX. 2011-02-22 23:53:01 +01:00
62 changed files with 3602 additions and 2926 deletions

No files matched your search

+26 -10
View File
@@ -2,11 +2,11 @@ include config.mk
TARGET = ssnes tools/ssnes-joyconfig
OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o autosave.o netplay.o
OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o
JOYCONFIG_OBJ = tools/ssnes-joyconfig.o conf/config_file.o
HEADERS = $(wildcard */*.h) $(wildcard *.h)
LIBS =
LIBS = -lm
DEFINES = -DHAVE_CONFIG_H
ifneq ($(findstring Darwin,$(shell uname -a)),)
@@ -27,6 +27,10 @@ ifeq ($(HAVE_CONFIGFILE), 1)
OBJ += conf/config_file.o
endif
ifeq ($(HAVE_NETPLAY), 1)
OBJ += netplay.o
endif
ifeq ($(HAVE_RSOUND), 1)
OBJ += audio/rsound.o
LIBS += -lrsound
@@ -53,7 +57,8 @@ endif
endif
ifeq ($(HAVE_JACK),1)
OBJ += audio/jack.o
LIBS += -ljack
LIBS += $(JACK_LIBS)
DEFINES += $(JACK_CFLAGS)
endif
ifeq ($(HAVE_PULSE), 1)
OBJ += audio/pulse.o
@@ -72,6 +77,15 @@ else
endif
endif
ifeq ($(HAVE_FBO), 1)
DEFINES += -DHAVE_FBO
endif
ifeq ($(HAVE_XVIDEO), 1)
OBJ += gfx/xvideo.o input/x11_input.o
LIBS += -lXv -lX11
endif
ifeq ($(HAVE_CG), 1)
OBJ += gfx/shader_cg.o
LIBS += -lCg -lCgGL
@@ -84,11 +98,7 @@ ifeq ($(HAVE_XML), 1)
endif
ifeq ($(HAVE_FILTER), 1)
OBJ += hqflt/hq.o
OBJ += hqflt/grayscale.o
OBJ += hqflt/bleed.o
OBJ += hqflt/ntsc.o
OBJ += hqflt/snes_ntsc/snes_ntsc.o
LIBS += -ldl
endif
ifeq ($(HAVE_FREETYPE), 1)
@@ -113,7 +123,12 @@ ifneq ($(V),1)
Q := @
endif
CFLAGS = -Wall -O3 -g -std=gnu99 -I.
CFLAGS += -Wall -O3 -g -I.
ifneq ($(findstring icc,$(CC)),)
CFLAGS += -std=c99
else
CFLAGS += -std=gnu99
endif
all: $(TARGET) config.mk
@@ -141,9 +156,10 @@ install: $(TARGET)
install -m644 ssnes.cfg $(DESTDIR)/etc/ssnes.cfg
install -m644 docs/ssnes.1 $(DESTDIR)$(PREFIX)/share/man/man1
install -m644 docs/ssnes-joyconfig.1 $(DESTDIR)$(PREFIX)/share/man/man1
install -m755 ssnes-zip $(DESTDIR)$(PREFIX)/bin
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/{ssnes,ssnes-joyconfig}
rm -f $(DESTDIR)$(PREFIX)/bin/{ssnes,ssnes-joyconfig,ssnes-zip}
rm -f $(DESTDIR)/etc/ssnes.cfg
rm -f $(DESTDIR)$(PREFIX)/share/man/man1/{ssnes,ssnes-joyconfig}.1
+20 -3
View File
@@ -1,6 +1,6 @@
TARGET = ssnes.exe
JTARGET = ssnes-joyconfig.exe
OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o netplay.o
OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o
JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o
CC = gcc
@@ -12,9 +12,12 @@ HAVE_XML = 1
HAVE_FREETYPE = 1
HAVE_XAUDIO = 1
HAVE_RSOUND = 1
HAVE_FILTER = 1
HAVE_NETPLAY = 1
HAVE_FBO = 1
libsnes ?= -lsnes
LIBS = -lws2_32
LIBS = -lm
DEFINES = -I. -DHAVE_CONFIGFILE
LDFLAGS = -L. -static-libgcc -s
@@ -52,6 +55,16 @@ ifeq ($(HAVE_XML), 1)
LIBS += -lxml2
endif
ifeq ($(HAVE_FILTER), 1)
DEFINES += -DHAVE_FILTER
endif
ifeq ($(HAVE_NETPLAY), 1)
DEFINES += -DHAVE_NETPLAY
OBJ += netplay.o
LIBS += -lws2_32
endif
ifeq ($(HAVE_FREETYPE), 1)
OBJ += gfx/fonts.o
DEFINES += -DHAVE_FREETYPE -Ifreetype2
@@ -64,6 +77,10 @@ else
LIBS += $(libsnes)
endif
ifeq ($(HAVE_FBO), 1)
DEFINES += -DHAVE_FBO
endif
ifneq ($(V),1)
Q := @
endif
@@ -99,7 +116,7 @@ clean:
rm -f tools/*.o
dist: all
zip -r ssnes-win32-0.3.zip $(TARGET) ssnes.cfg snes.dll libxml2.dll iconv.dll zlib1.dll SDL.dll freetype6.dll xaudio-c.dll rsound.dll pthreadGC2.dll README.win32.txt $(JTARGET)
zip -r ssnes-win32-0.4-beta.zip $(TARGET) ssnes.cfg snes.dll libxml2.dll iconv.dll zlib1.dll SDL.dll freetype6.dll xaudio-c.dll rsound.dll pthreadGC2.dll README.win32.txt $(JTARGET)
libs:
wget https://github.com/downloads/Themaister/SSNES/SSNES-win32-libs.zip --no-check-certificate
+9 -1
View File
@@ -103,7 +103,15 @@ static size_t find_buffersize(jack_t *jd, int latency)
{
int frames = latency * g_settings.audio.out_rate / 1000;
int jack_latency = jack_port_get_total_latency(jd->client, jd->ports[0]);
jack_latency_range_t range;
int jack_latency = 0;
for (int i = 0; i < 2; i++)
{
jack_port_get_latency_range(jd->ports[i], JackPlaybackLatency, &range);
if (range.max > jack_latency)
jack_latency = range.max;
}
SSNES_LOG("JACK: Jack latency is %d frames.\n", jack_latency);
int buffer_frames = frames - jack_latency;
+10 -7
View File
@@ -22,6 +22,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include "general.h"
typedef struct
{
@@ -37,13 +38,14 @@ static void* __roar_init(const char* device, int rate, int latency)
return NULL;
roar_vs_t *vss;
if ( (vss = roar_vs_new_simple(NULL, NULL, rate, 2, ROAR_CODEC_PCM_S_LE, 16, ROAR_DIR_PLAY, &err)) == NULL )
if ((vss = roar_vs_new_simple(device, "SSNES", rate, 2, ROAR_CODEC_PCM_S, 16, ROAR_DIR_PLAY, &err)) == NULL)
{
fprintf(stderr, "roar_vs: \"%s\"\n", roar_vs_strerr(err));
SSNES_ERR("RoarAudio: \"%s\"\n", roar_vs_strerr(err));
free(roar);
return NULL;
}
roar_vs_role(vss, ROAR_ROLE_GAME, NULL);
roar->vss = vss;
return roar;
@@ -54,19 +56,20 @@ static ssize_t __roar_write(void* data, const void* buf, size_t size)
roar_t *roar = data;
ssize_t rc;
if ( size == 0 )
if (size == 0)
return 0;
int err;
size_t written = 0;
while (written < size)
{
if ((rc = roar_vs_write(roar->vss, (const char*)buf + written, size - written, &err)) < size)
size_t write_amt = size - written;
if ((rc = roar_vs_write(roar->vss, (const char*)buf + written, write_amt, &err)) < write_amt)
{
if (rc < 0)
if (roar->nonblocking)
return rc;
else if (rc < 0)
return -1;
else if (roar->nonblocking)
return 0;
}
written += rc;
}
-13
View File
@@ -51,19 +51,6 @@ static void sdl_audio_cb(void *data, Uint8 *stream, int len)
memset(stream + write_size, 0, len - write_size);
}
// Interesting hack from http://www-graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static inline int find_num_frames(int rate, int latency)
{
int frames = (rate * latency) / 1000;
-13
View File
@@ -67,19 +67,6 @@ static bool init_lib(void)
return true;
}
// Interesting hack from http://www-graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static void* __xa_init(const char* device, int rate, int latency)
{
if (!init_lib())
+21 -2
View File
@@ -43,6 +43,7 @@
///////////////// Drivers
#define VIDEO_GL 0
#define VIDEO_XVIDEO 11
////////////////////////
#define AUDIO_RSOUND 1
#define AUDIO_OSS 2
@@ -55,9 +56,14 @@
#define AUDIO_PULSE 10
////////////////////////
#define INPUT_SDL 7
#define INPUT_X 12
////////////////////////
#if defined(HAVE_SDL)
#define VIDEO_DEFAULT_DRIVER VIDEO_GL
#elif defined(HAVE_XVIDEO)
#define VIDEO_DEFAULT_DRIVER VIDEO_XVIDEO
#endif
#if defined(HAVE_ALSA)
#define AUDIO_DEFAULT_DRIVER AUDIO_ALSA
@@ -81,7 +87,11 @@
#error Need at least one audio driver!
#endif
#if defined(HAVE_SDL)
#define INPUT_DEFAULT_DRIVER INPUT_SDL
#elif defined(HAVE_XVIDEO)
#define INPUT_DEFAULT_DRIVER INPUT_X
#endif
////////////////
@@ -94,8 +104,8 @@ static const float yscale = 3.0; // Real y res = 224 * yscale
// Fullscreen
static const bool fullscreen = false; // To start in Fullscreen or not
static const unsigned fullscreen_x = 1280;
static const unsigned fullscreen_y = 720;
static const unsigned fullscreen_x = 0; // Fullscreen resolution. A value of 0 uses the desktop resolution.
static const unsigned fullscreen_y = 0;
// Video VSYNC (recommended)
static const bool vsync = true;
@@ -113,6 +123,12 @@ static const unsigned font_size = 48;
static const float message_pos_offset_x = 0.05;
static const float message_pos_offset_y = 0.05;
// Render-to-texture before rendering to screen (multi-pass shaders)
static const bool render_to_texture = false;
static const float fbo_scale_x = 2.0;
static const float fbo_scale_y = 2.0;
static const bool second_pass_smooth = true;
#define SNES_ASPECT_RATIO (4.0/3)
////////////////
@@ -204,6 +220,9 @@ static const struct snes_keybind snes_keybinds_1[] = {
{ SSNES_REWIND, SDLK_r, NO_BTN, AXIS_NONE },
{ SSNES_MOVIE_RECORD_TOGGLE, SDLK_o, NO_BTN, AXIS_NONE },
{ SSNES_PAUSE_TOGGLE, SDLK_p, NO_BTN, AXIS_NONE },
{ SSNES_RESET, SDLK_h, NO_BTN, AXIS_NONE },
{ SSNES_SHADER_NEXT, SDLK_m, NO_BTN, AXIS_NONE },
{ SSNES_SHADER_PREV, SDLK_n, NO_BTN, AXIS_NONE },
{ -1 }
};
+12
View File
@@ -80,6 +80,12 @@ static const bool _xml_supp = true;
static const bool _xml_supp = false;
#endif
#ifdef HAVE_FBO
static const bool _fbo_supp = true;
#else
static const bool _fbo_supp = false;
#endif
#ifdef HAVE_DYNAMIC
static const bool _dynamic_supp = true;
#else
@@ -110,4 +116,10 @@ static const bool _freetype_supp = true;
static const bool _freetype_supp = false;
#endif
#ifdef HAVE_NETPLAY
static const bool _netplay_supp = true;
#else
static const bool _netplay_supp = false;
#endif
#endif
+5
View File
@@ -126,5 +126,10 @@ Set FRAMES to 0 to have perfect sync. 0 frames is only suitable for LAN. Default
\fB--port PORT\fR
Network port used for netplay. This defaults to 55435. This option affects both TCP and UDP.
.TP
\fB--ups PATCH, -U PATCH\fR
Attempts to apply an UPS patch to the current ROM image. No files are altered.
If this flag is not specified, SSNES will look for a .ups file with same basename as ROM specified.
.SH "SEE ALSO"
\fBssnes-joyconfig\fR(1)
+124 -17
View File
@@ -18,10 +18,11 @@
#include "driver.h"
#include "general.h"
#include "file.h"
#include <stdio.h>
#include <string.h>
#include "hqflt/filters.h"
#include <assert.h>
#include <math.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -61,12 +62,18 @@ static const video_driver_t *video_drivers[] = {
#ifdef HAVE_SDL
&video_gl,
#endif
#ifdef HAVE_XVIDEO
&video_xvideo,
#endif
};
static const input_driver_t *input_drivers[] = {
#ifdef HAVE_SDL
&input_sdl,
#endif
//#ifdef HAVE_XVIDEO
// &input_x,
//#endif
};
static void find_audio_driver(void)
@@ -212,28 +219,121 @@ void uninit_audio(void)
free(g_extern.audio_data.conv_outsamples); g_extern.audio_data.conv_outsamples = NULL;
}
#ifdef HAVE_FILTER
static void init_filter(void)
{
if (g_extern.filter.active)
return;
if (*g_settings.video.filter_path == '\0')
return;
SSNES_LOG("Loading bSNES filter from \"%s\"\n", g_settings.video.filter_path);
g_extern.filter.lib = dylib_load(g_settings.video.filter_path);
if (!g_extern.filter.lib)
{
SSNES_ERR("Failed to load filter \"%s\"\n", g_settings.video.filter_path);
return;
}
g_extern.filter.psize = dylib_proc(g_extern.filter.lib, "filter_size");
g_extern.filter.prender = dylib_proc(g_extern.filter.lib, "filter_render");
if (!g_extern.filter.psize || !g_extern.filter.prender)
{
SSNES_ERR("Failed to find functions in filter...\n");
dylib_close(g_extern.filter.lib);
g_extern.filter.lib = NULL;
return;
}
g_extern.filter.active = true;
unsigned width = 512;
unsigned height = 512;
g_extern.filter.psize(&width, &height);
unsigned pow2_x = next_pow2(ceil(width));
unsigned pow2_y = next_pow2(ceil(height));
unsigned maxsize = pow2_x > pow2_y ? pow2_x : pow2_y;
g_extern.filter.scale = maxsize / 256;
g_extern.filter.buffer = malloc(256 * 256 * g_extern.filter.scale * g_extern.filter.scale * sizeof(uint32_t));
g_extern.filter.pitch = 256 * g_extern.filter.scale * sizeof(uint32_t);
assert(g_extern.filter.buffer);
g_extern.filter.colormap = malloc(32768 * sizeof(uint32_t));
assert(g_extern.filter.colormap);
// Set up conversion map from 16-bit XBGR1555 to 32-bit RGBA.
for (int i = 0; i < 32768; i++)
{
unsigned r = (i >> 10) & 31;
unsigned g = (i >> 5) & 31;
unsigned b = (i >> 0) & 31;
r = (r << 3) | (r >> 2);
g = (g << 3) | (g >> 2);
b = (b << 3) | (b >> 2);
g_extern.filter.colormap[i] = (r << 24) | (g << 16) | (b << 8);
}
}
#endif
static void deinit_filter(void)
{
if (g_extern.filter.active)
{
g_extern.filter.active = false;
dylib_close(g_extern.filter.lib);
g_extern.filter.lib = NULL;
free(g_extern.filter.buffer);
free(g_extern.filter.colormap);
}
}
#ifdef HAVE_XML
static void init_shader_dir(void)
{
if (!*g_settings.video.shader_dir)
return;
g_extern.shader_dir.elems = dir_list_new(g_settings.video.shader_dir, ".shader");
g_extern.shader_dir.size = 0;
g_extern.shader_dir.ptr = 0;
if (g_extern.shader_dir.elems)
{
while (g_extern.shader_dir.elems[g_extern.shader_dir.size])
{
SSNES_LOG("Found shader \"%s\"\n", g_extern.shader_dir.elems[g_extern.shader_dir.size]);
g_extern.shader_dir.size++;
}
}
}
static void deinit_shader_dir(void)
{
// It handles NULL, no worries :D
dir_list_free(g_extern.shader_dir.elems);
}
#endif
void init_video_input(void)
{
int scale = 2;
#ifdef HAVE_FILTER
init_filter();
#endif
#ifdef HAVE_XML
init_shader_dir();
#endif
// We use at least 512x512 textures to accomodate for hi-res games.
unsigned scale = 2;
find_video_driver();
find_input_driver();
// We multiply scales with 2 to allow for hi-res games.
#if HAVE_FILTER
switch (g_settings.video.filter)
{
case FILTER_HQ2X:
scale = 4;
break;
case FILTER_HQ4X:
case FILTER_NTSC:
scale = 8;
break;
default:
break;
}
#endif
if (g_extern.filter.active)
scale = g_extern.filter.scale;
video_info_t video = {
.width = (g_settings.video.fullscreen) ? g_settings.video.fullscreen_x : (296 * g_settings.video.xscale),
@@ -243,6 +343,7 @@ void init_video_input(void)
.force_aspect = g_settings.video.force_aspect,
.smooth = g_settings.video.smooth,
.input_scale = scale,
.rgb32 = g_extern.filter.active
};
const input_driver_t *tmp = driver.input;
@@ -282,6 +383,12 @@ void uninit_video_input(void)
if ( driver.input_data != driver.video_data && driver.input )
driver.input->free(driver.input_data);
deinit_filter();
#ifdef HAVE_XML
deinit_shader_dir();
#endif
}
driver_t driver;
+9 -2
View File
@@ -39,8 +39,12 @@ enum
SSNES_REWIND,
SSNES_MOVIE_RECORD_TOGGLE,
SSNES_PAUSE_TOGGLE,
SSNES_RESET,
SSNES_SHADER_NEXT,
SSNES_SHADER_PREV,
};
struct snes_keybind
{
int id;
@@ -58,6 +62,7 @@ typedef struct video_info
bool force_aspect;
bool smooth;
int input_scale; // HQ2X => 2, HQ4X => 4, None => 1
bool rgb32; // Use 32-bit RGBA rather than native XBGR1555.
} video_info_t;
typedef struct audio_driver
@@ -78,7 +83,6 @@ typedef struct audio_driver
#define AXIS_NEG_GET(x) (((uint32_t)(x) >> 16) & 0xFFFFU)
#define AXIS_POS_GET(x) ((uint32_t)(x) & 0xFFFFU)
#define AXIS_NONE (0xFFFFFFFFU)
#define NO_BTN ((uint16_t)0xFFFFU) // I hope no joypad will ever have this many buttons ... ;)
@@ -106,11 +110,12 @@ typedef struct video_driver
{
void* (*init)(video_info_t *video, const input_driver_t **input, void **input_data);
// Should the video driver act as an input driver as well? :) The video init might preinitialize an input driver to override the settings in case the video driver relies on input driver for event handling, e.g.
bool (*frame)(void* data, const uint16_t* frame, unsigned width, unsigned height, unsigned pitch, const char *msg); // msg is for showing a message on the screen along with the video frame.
bool (*frame)(void* data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg); // msg is for showing a message on the screen along with the video frame.
void (*set_nonblock_state)(void* data, bool toggle); // Should we care about syncing to vblank? Fast forwarding.
// Is the window still active?
bool (*alive)(void *data);
bool (*focus)(void *data); // Does the window have focus?
bool (*xml_shader)(void *data, const char *path); // Sets XML-shader. Might not be implemented.
void (*free)(void* data);
const char *ident;
} video_driver_t;
@@ -147,7 +152,9 @@ extern const audio_driver_t audio_sdl;
extern const audio_driver_t audio_xa;
extern const audio_driver_t audio_pulse;
extern const video_driver_t video_gl;
extern const video_driver_t video_xvideo;
extern const input_driver_t input_sdl;
extern const input_driver_t input_x;
////////////////////////////////////////////////
#endif
+54 -29
View File
@@ -25,31 +25,28 @@
#include <libsnes.hpp>
#ifdef HAVE_DYNAMIC
#ifdef _WIN32
#include <windows.h>
#define SYM(x) do { \
p##x = ((void*)GetProcAddress(lib_handle, #x)); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#else
#include <dlfcn.h>
#define SYM(x) do { \
p##x = dlsym(lib_handle, #x); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#endif // _WIN32
#endif // HAVE_DYNAMIC
#endif
#ifdef HAVE_DYNAMIC
#ifdef _WIN32
static HMODULE lib_handle;
#else
static void *lib_handle = NULL;
#endif
#define DLSYM(lib, x) dylib_proc(lib, #x)
#define SYM(x) do { \
p##x = DLSYM(lib_handle, x); \
if (p##x == NULL) { SSNES_ERR("Failed to load symbol: \"%s\"\n", #x); exit(1); } \
} while(0)
#define OPT_SYM(x) do { \
p##x = DLSYM(lib_handle, x); \
} while(0)
static dylib_t lib_handle = NULL;
#endif
void (*psnes_init)(void);
void (*psnes_set_video_refresh)(snes_video_refresh_t);
@@ -57,8 +54,10 @@ void (*psnes_set_audio_sample)(snes_audio_sample_t);
void (*psnes_set_input_poll)(snes_input_poll_t);
void (*psnes_set_input_state)(snes_input_state_t);
void (*psnes_reset)(void);
void (*psnes_run)(void);
const char *(*psnes_library_id)(void) = NULL;
unsigned (*psnes_library_revision_minor)(void);
unsigned (*psnes_library_revision_major)(void);
@@ -97,11 +96,7 @@ void (*psnes_term)(void);
static void load_dynamic(void)
{
SSNES_LOG("Loading dynamic libsnes from: \"%s\"\n", g_settings.libsnes);
#ifdef _WIN32
lib_handle = LoadLibrary(g_settings.libsnes);
#else
lib_handle = dlopen(g_settings.libsnes, RTLD_LAZY);
#endif
lib_handle = dylib_load(g_settings.libsnes);
if (!lib_handle)
{
SSNES_ERR("Failed to open dynamic library: \"%s\"\n", g_settings.libsnes);
@@ -113,8 +108,10 @@ static void load_dynamic(void)
SYM(snes_set_audio_sample);
SYM(snes_set_input_poll);
SYM(snes_set_input_state);
OPT_SYM(snes_library_id);
SYM(snes_library_revision_minor);
SYM(snes_library_revision_major);
SYM(snes_reset);
SYM(snes_run);
SYM(snes_get_region);
SYM(snes_load_cartridge_normal);
@@ -148,6 +145,7 @@ static void set_statics(void)
SSYM(snes_set_input_state);
SSYM(snes_library_revision_minor);
SSYM(snes_library_revision_major);
SSYM(snes_reset);
SSYM(snes_run);
SSYM(snes_get_region);
SSYM(snes_load_cartridge_normal);
@@ -186,12 +184,39 @@ void uninit_dlsym(void)
{
#ifdef HAVE_DYNAMIC
if (lib_handle)
{
#ifdef _WIN32
FreeLibrary(lib_handle);
#else
dlclose(lib_handle);
#endif
}
dylib_close(lib_handle);
#endif
}
// Platform independent dylib loading.
dylib_t dylib_load(const char *path)
{
#ifdef _WIN32
return LoadLibrary(path);
#else
return dlopen(path, RTLD_LAZY);
#endif
}
void* dylib_proc(dylib_t lib, const char *proc)
{
#ifdef _WIN32
void *sym = (void*)GetProcAddress(lib, proc);
#else
void *sym = dlsym(lib, proc);
#endif
if (!sym)
SSNES_WARN("Failed to load symbol \"%s\"\n", proc);
return sym;
}
void dylib_close(dylib_t lib)
{
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif
}
+8 -1
View File
@@ -24,6 +24,12 @@
void init_dlsym(void);
void uninit_dlsym(void);
typedef void *dylib_t;
dylib_t dylib_load(const char *path);
void dylib_close(dylib_t lib);
void* dylib_proc(dylib_t lib, const char *proc);
extern void (*psnes_init)(void);
extern void (*psnes_set_video_refresh)(snes_video_refresh_t);
@@ -31,6 +37,7 @@ extern void (*psnes_set_audio_sample)(snes_audio_sample_t);
extern void (*psnes_set_input_poll)(snes_input_poll_t);
extern void (*psnes_set_input_state)(snes_input_state_t);
extern const char* (*psnes_library_id)(void);
extern unsigned (*psnes_library_revision_minor)(void);
extern unsigned (*psnes_library_revision_major)(void);
@@ -49,7 +56,6 @@ extern bool (*psnes_load_cartridge_sufami_turbo)(
const char*, const uint8_t*, unsigned,
const char*, const uint8_t*, unsigned);
extern void (*psnes_set_controller_port_device)(bool, unsigned);
extern bool (*psnes_get_region)(void);
@@ -58,6 +64,7 @@ extern unsigned (*psnes_serialize_size)(void);
extern bool (*psnes_serialize)(uint8_t*, unsigned);
extern bool (*psnes_unserialize)(const uint8_t*, unsigned);
extern void (*psnes_reset)(void);
extern void (*psnes_run)(void);
extern void (*psnes_set_cartridge_basename)(const char*);
+239 -80
View File
@@ -23,92 +23,17 @@
#include <string.h>
#include "dynamic.h"
#include "movie.h"
#include "ups.h"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif
// Load SNES rom only. Applies a hack for headered ROMs.
static ssize_t read_rom_file(FILE* file, void** buf)
{
ssize_t ret;
if (file == NULL) // stdin
{
#ifdef _WIN32
setmode(0, O_BINARY);
#endif
SSNES_LOG("Reading ROM from stdin ...\n");
size_t buf_size = 0xFFFFF; // Some initial guesstimate.
size_t buf_ptr = 0;
char *rom_buf = malloc(buf_size);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
for(;;)
{
size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin);
buf_ptr += ret;
// We've reached the end
if (buf_ptr < buf_size)
break;
rom_buf = realloc(rom_buf, buf_size * 2);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
buf_size *= 2;
}
if ((buf_ptr & 0x7fff) == 512)
{
memmove(rom_buf, rom_buf + 512, buf_ptr - 512);
buf_ptr -= 512;
}
*buf = rom_buf;
ret = buf_ptr;
}
else
{
fseek(file, 0, SEEK_END);
long length = ftell(file);
rewind(file);
if ((length & 0x7fff) == 512)
{
length -= 512;
fseek(file, 512, SEEK_SET);
}
void *rom_buf = malloc(length);
if ( rom_buf == NULL )
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
if ( fread(rom_buf, 1, length, file) < length )
{
SSNES_ERR("Didn't read whole file.\n");
free(rom_buf);
return -1;
}
*buf = rom_buf;
ret = length;
}
g_extern.cart_crc = crc32_calculate(*buf, ret);
return ret;
}
// Generic file loader.
static ssize_t read_file(const char *path, void **buf)
{
@@ -143,6 +68,117 @@ error:
return -1;
}
// Load SNES rom only. Applies a hack for headered ROMs.
static ssize_t read_rom_file(FILE* file, void** buf)
{
ssize_t ret = 0;
uint8_t *ret_buf = NULL;
if (file == NULL) // stdin
{
#ifdef _WIN32
setmode(0, O_BINARY);
#endif
SSNES_LOG("Reading ROM from stdin ...\n");
size_t buf_size = 0xFFFFF; // Some initial guesstimate.
size_t buf_ptr = 0;
uint8_t *rom_buf = malloc(buf_size);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
for(;;)
{
size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin);
buf_ptr += ret;
// We've reached the end
if (buf_ptr < buf_size)
break;
rom_buf = realloc(rom_buf, buf_size * 2);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
buf_size *= 2;
}
ret_buf = rom_buf;
ret = buf_ptr;
}
else
{
fseek(file, 0, SEEK_END);
ret = ftell(file);
rewind(file);
void *rom_buf = malloc(ret);
if (rom_buf == NULL)
{
SSNES_ERR("Couldn't allocate memory!\n");
return -1;
}
if (fread(rom_buf, 1, ret, file) < ret)
{
SSNES_ERR("Didn't read whole file.\n");
free(rom_buf);
return -1;
}
ret_buf = rom_buf;
}
// Patch with UPS.
ssize_t ups_patch_size;
void *ups_patch = NULL;
if (*g_extern.ups_name && (ups_patch_size = read_file(g_extern.ups_name, &ups_patch)) >= 0)
{
SSNES_LOG("Found UPS file in \"%s\", attempting to patch ...\n", g_extern.ups_name);
size_t target_size = ret * 4; // Just to be sure ...
uint8_t *patched_rom = malloc(target_size);
if (patched_rom)
{
ups_error_t err = ups_apply_patch(ups_patch, ups_patch_size, ret_buf, ret, patched_rom, &target_size);
if (err == UPS_SUCCESS)
{
free(ret_buf);
ret_buf = patched_rom;
ret = target_size;
SSNES_LOG("ROM patched successfully (UPS)!\n");
}
else
{
free(patched_rom);
SSNES_LOG("ROM failed to patch (UPS).\n");
}
free(ups_patch);
}
}
else if (*g_extern.ups_name)
SSNES_LOG("Could not find UPS patch.\n");
// Remove SMC header if present.
if ((ret & 0x7fff) == 512)
{
memmove(ret_buf, ret_buf + 512, ret - 512);
ret -= 512;
}
g_extern.cart_crc = crc32_calculate(ret_buf, ret);
*buf = ret_buf;
return ret;
}
// Dump stuff to file.
static bool dump_to_file(const char *path, const void *data, size_t size)
{
@@ -482,3 +518,126 @@ bool init_rom_file(enum ssnes_game_type type)
return true;
}
// Yep, this is C alright ;)
char** dir_list_new(const char *dir, const char *ext)
{
size_t path_len = strlen(dir);
size_t cur_ptr = 0;
size_t cur_size = 32;
char **dir_list = NULL;
#ifdef _WIN32
WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
size_t final_off = MAX_PATH + path_len + 2;
wchar_t wchar_buf[MAX_PATH + 1];
char utf8_buf[MAX_PATH + 3];
strncpy(utf8_buf, dir, MAX_PATH);
strcat(utf8_buf, "/*");
utf8_buf[MAX_PATH + 2] = '\0';
int ret = MultiByteToWideChar(CP_UTF8, 0, utf8_buf, strlen(utf8_buf), wchar_buf, MAX_PATH);
wchar_buf[ret] = 0;
hFind = FindFirstFileW(wchar_buf, &ffd);
if (hFind == INVALID_HANDLE_VALUE)
goto error;
#else
DIR *directory = NULL;
const struct dirent *entry = NULL;
size_t final_off = sizeof(entry->d_name) + path_len + 2;
directory = opendir(dir);
if (!directory)
goto error;
#endif
dir_list = calloc(cur_size, sizeof(char*));
if (!dir_list)
goto error;
#ifdef _WIN32 // Hard to read? Blame non-POSIX heathens!
do
#else
while ((entry = readdir(directory)))
#endif
{
// Not a perfect search of course, but hopefully good enough in practice.
#ifdef _WIN32
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
int ret = WideCharToMultiByte(CP_UTF8, 0, ffd.cFileName, wcslen(ffd.cFileName), utf8_buf, MAX_PATH, NULL, NULL);
utf8_buf[ret] = '\0';
if (ext && !strstr(utf8_buf, ext))
continue;
#else
if (ext && !strstr(entry->d_name, ext))
continue;
#endif
dir_list[cur_ptr] = malloc(final_off);
if (!dir_list[cur_ptr])
goto error;
strcpy(dir_list[cur_ptr], dir);
#ifdef _WIN32
dir_list[cur_ptr][path_len] = '\\';
strcpy(&dir_list[cur_ptr][path_len + 1], utf8_buf);
#else
dir_list[cur_ptr][path_len] = '/';
strcpy(&dir_list[cur_ptr][path_len + 1], entry->d_name);
#endif
dir_list[cur_ptr][final_off - 1] = '\0';
cur_ptr++;
if (cur_ptr + 1 == cur_size) // Need to reserve for NULL.
{
cur_size *= 2;
dir_list = realloc(dir_list, cur_size * sizeof(char*));
if (!dir_list)
goto error;
// Make sure it's all NULL'd out since we cannot rely on realloc to do this.
memset(dir_list + cur_ptr, 0, (cur_size - cur_ptr) * sizeof(char*));
}
}
#ifdef _WIN32
while (FindNextFileW(hFind, &ffd) != 0);
#endif
#ifdef _WIN32
FindClose(hFind);
#else
closedir(directory);
#endif
return dir_list;
error:
SSNES_ERR("Failed to open directory: \"%s\"\n", dir);
#ifdef _WIN32
if (hFind != INVALID_HANDLE_VALUE)
FindClose(hFind);
#else
if (directory)
closedir(directory);
#endif
dir_list_free(dir_list);
return NULL;
}
void dir_list_free(char **dir_list)
{
if (!dir_list)
return;
char **orig = dir_list;
while (*dir_list)
free(*dir_list++);
free(orig);
}
+4
View File
@@ -34,4 +34,8 @@ void save_ram_file(const char* path, int type);
bool init_rom_file(enum ssnes_game_type type);
// Returns a NULL-terminated list of files in a directory with full paths.
char** dir_list_new(const char *dir, const char *ext);
void dir_list_free(char **dir_list);
#endif
+58 -2
View File
@@ -28,6 +28,7 @@
#include "movie.h"
#include "autosave.h"
#include "netplay.h"
#include "dynamic.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -41,9 +42,17 @@
#define MAX_PLAYERS 5
#define MAX_BINDS 25 // Needs to be increased every time there are new binds added.
#define MAX_BINDS 28 // Needs to be increased every time there are new binds added.
#define SSNES_NO_JOYPAD 0xFFFF
enum ssnes_shader_type
{
SSNES_SHADER_CG,
SSNES_SHADER_BSNES,
SSNES_SHADER_AUTO,
SSNES_SHADER_NONE
};
// All config related settings go here.
struct settings
{
@@ -61,7 +70,15 @@ struct settings
float aspect_ratio;
char cg_shader_path[256];
char bsnes_shader_path[256];
unsigned filter;
char filter_path[256];
enum ssnes_shader_type shader_type;
bool render_to_texture;
double fbo_scale_x;
double fbo_scale_y;
char second_pass_shader[256];
bool second_pass_smooth;
char shader_dir[256];
char font_path[256];
unsigned font_size;
@@ -111,6 +128,7 @@ enum ssnes_game_type
SSNES_CART_SUFAMI,
};
// All run-time- / command line flag-related globals go here.
struct global
{
@@ -145,6 +163,7 @@ struct global
char savefile_name_asrm[512];
char savefile_name_bsrm[512];
char savestate_name[256];
char ups_name[512];
unsigned state_slot;
@@ -168,6 +187,20 @@ struct global
int16_t *conv_outsamples;
} audio_data;
struct
{
bool active;
uint32_t *buffer;
uint32_t *colormap;
unsigned pitch;
dylib_t lib;
unsigned scale;
void (*psize)(unsigned *width, unsigned *height);
void (*prender)(uint32_t *colormap, uint32_t *output, unsigned outpitch,
const uint16_t *input, unsigned pitch, unsigned width, unsigned height);
} filter;
msg_queue_t *msg_queue;
// Rewind support.
@@ -201,6 +234,15 @@ struct global
char record_path[256];
bool recording;
#endif
char title_buf[64];
struct
{
char** elems;
size_t size;
size_t ptr;
} shader_dir;
};
void parse_config(void);
@@ -224,4 +266,18 @@ extern struct global g_extern;
fflush(stderr); \
} while(0)
static inline uint32_t next_pow2(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
#endif
+58
View File
@@ -0,0 +1,58 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "gfx_common.h"
#include "general.h"
#include <sys/time.h>
static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, int frames)
{
float time = new_tv->tv_sec - tv->tv_sec + (new_tv->tv_usec - tv->tv_usec)/1000000.0;
return frames/time;
}
bool gfx_window_title(char *buf, size_t size)
{
static int frames = 0;
static struct timeval tv;
struct timeval new_tv;
bool ret = false;
if (frames == 0)
{
gettimeofday(&tv, NULL);
snprintf(buf, size, g_extern.title_buf);
ret = true;
}
if ((frames % 180) == 0 && frames > 0)
{
gettimeofday(&new_tv, NULL);
struct timeval tmp_tv = tv;
tv = new_tv;
float fps = tv_to_fps(&tmp_tv, &new_tv, 180);
snprintf(buf, size, "%s || FPS: %6.1f || Frames: %d", g_extern.title_buf, fps, frames);
ret = true;
}
frames++;
return ret;
}
+5 -25
View File
@@ -15,32 +15,12 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GFX_COMMON_H
#define __GFX_COMMON_H
#ifndef __FILTERS_H
#define __FILTERS_H
#include <stddef.h>
#include <stdbool.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_FILTER
#include "pastlib.h"
#include "grayscale.h"
#include "bleed.h"
#include "ntsc.h"
#define FILTER_NONE 0
#define FILTER_HQ2X 1
#define FILTER_HQ4X 2
#define FILTER_GRAYSCALE 3
#define FILTER_BLEED 4
#define FILTER_NTSC 5
#define FILTER_HQ2X_STR "hq2x"
#define FILTER_HQ4X_STR "hq4x"
#define FILTER_GRAYSCALE_STR "grayscale"
#define FILTER_BLEED_STR "bleed"
#define FILTER_NTSC_STR "ntsc"
#endif
bool gfx_window_title(char *buf, size_t size);
#endif
+693 -163
View File
File diff suppressed because it is too large. Load diff
+38 -1
View File
@@ -20,6 +20,15 @@
#include "general.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#endif
static inline bool gl_check_error(void)
{
int error = glGetError();
@@ -29,7 +38,7 @@ static inline bool gl_check_error(void)
SSNES_ERR("GL: Invalid enum.\n");
break;
case GL_INVALID_VALUE:
SSNES_ERR("GL: Invalid value.\n");
SSNES_ERR("GL: Invalid value. (You're not alone.)\n");
break;
case GL_INVALID_OPERATION:
SSNES_ERR("GL: Invalid operation.\n");
@@ -56,4 +65,32 @@ static inline bool gl_check_error(void)
return false;
}
struct gl_fbo_rect
{
unsigned img_width;
unsigned img_height;
unsigned max_img_width;
unsigned max_img_height;
unsigned width;
unsigned height;
};
enum gl_scale_type
{
SSNES_SCALE_ABSOLUTE,
SSNES_SCALE_INPUT,
SSNES_SCALE_VIEWPORT
};
struct gl_fbo_scale
{
enum gl_scale_type type_x;
enum gl_scale_type type_y;
float scale_x;
float scale_y;
unsigned abs_x;
unsigned abs_y;
bool valid;
};
#endif
+100 -47
View File
@@ -19,6 +19,7 @@
#include <Cg/cg.h>
#include <Cg/cgGL.h>
#include "general.h"
#include <string.h>
// Used when we call deactivate() since just unbinding the program didn't seem to work... :(
static const char* stock_cg_program =
@@ -55,21 +56,28 @@ static const char* stock_cg_program =
static CGcontext cgCtx;
static CGprogram cgFPrg;
static CGprogram cgVPrg;
static CGprogram cgSFPrg;
static CGprogram cgSVPrg;
static CGprofile cgFProf;
static CGprofile cgVProf;
static CGparameter cg_video_size, cg_texture_size, cg_output_size;
static CGparameter cg_Vvideo_size, cg_Vtexture_size, cg_Voutput_size; // Vertexes
static CGparameter cg_mvp_matrix;
struct cg_program
{
CGprogram vprg;
CGprogram fprg;
CGparameter vid_size_f;
CGparameter tex_size_f;
CGparameter out_size_f;
CGparameter vid_size_v;
CGparameter tex_size_v;
CGparameter out_size_v;
CGparameter mvp;
};
static struct cg_program prg[3];
static bool cg_active = false;
static CGprofile cgVProf, cgFProf;
static unsigned active_index = 0;
void gl_cg_set_proj_matrix(void)
{
if (cg_active)
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLSetStateMatrixParameter(prg[active_index].mvp, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
}
void gl_cg_set_params(unsigned width, unsigned height,
@@ -78,13 +86,13 @@ void gl_cg_set_params(unsigned width, unsigned height,
{
if (cg_active)
{
cgGLSetParameter2f(cg_video_size, width, height);
cgGLSetParameter2f(cg_texture_size, tex_width, tex_height);
cgGLSetParameter2f(cg_output_size, out_width, out_height);
cgGLSetParameter2f(prg[active_index].vid_size_f, width, height);
cgGLSetParameter2f(prg[active_index].tex_size_f, tex_width, tex_height);
cgGLSetParameter2f(prg[active_index].out_size_f, out_width, out_height);
cgGLSetParameter2f(cg_Vvideo_size, width, height);
cgGLSetParameter2f(cg_Vtexture_size, tex_width, tex_height);
cgGLSetParameter2f(cg_Voutput_size, out_width, out_height);
cgGLSetParameter2f(prg[active_index].vid_size_v, width, height);
cgGLSetParameter2f(prg[active_index].tex_size_v, tex_width, tex_height);
cgGLSetParameter2f(prg[active_index].out_size_v, out_width, out_height);
}
}
@@ -92,17 +100,22 @@ void gl_cg_deinit(void)
{
if (cg_active)
cgDestroyContext(cgCtx);
cg_active = false;
}
bool gl_cg_init(const char *path)
{
SSNES_LOG("Loading Cg file: %s\n", path);
if (strlen(g_settings.video.second_pass_shader) > 0)
SSNES_LOG("Loading 2nd pass: %s\n", g_settings.video.second_pass_shader);
cgCtx = cgCreateContext();
if (cgCtx == NULL)
{
SSNES_ERR("Failed to create Cg context\n");
return false;
}
cgFProf = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgVProf = cgGLGetLatestProfile(CG_GL_VERTEX);
if (cgFProf == CG_PROFILE_UNKNOWN || cgVProf == CG_PROFILE_UNKNOWN)
@@ -112,54 +125,94 @@ bool gl_cg_init(const char *path)
}
cgGLSetOptimalOptions(cgFProf);
cgGLSetOptimalOptions(cgVProf);
cgFPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgFProf, "main_fragment", 0);
cgVPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgVProf, "main_vertex", 0);
cgSFPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
cgSVPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
if (cgFPrg == NULL || cgVPrg == NULL || cgSFPrg == NULL || cgSVPrg == NULL)
prg[0].fprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgFProf, "main_fragment", 0);
prg[0].vprg = cgCreateProgram(cgCtx, CG_SOURCE, stock_cg_program, cgVProf, "main_vertex", 0);
prg[1].fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgFProf, "main_fragment", 0);
prg[1].vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, path, cgVProf, "main_vertex", 0);
if (strlen(g_settings.video.second_pass_shader) > 0)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
return false;
prg[2].fprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, g_settings.video.second_pass_shader, cgFProf, "main_fragment", 0);
prg[2].vprg = cgCreateProgramFromFile(cgCtx, CG_SOURCE, g_settings.video.second_pass_shader, cgVProf, "main_vertex", 0);
}
cgGLLoadProgram(cgFPrg);
cgGLLoadProgram(cgVPrg);
cgGLLoadProgram(cgSFPrg);
cgGLLoadProgram(cgSVPrg);
else
prg[2] = prg[0];
for (int i = 0; i < 3; i++)
{
if (prg[i].fprg == NULL || prg[i].vprg == NULL)
{
CGerror err = cgGetError();
SSNES_ERR("CG error: %s\n", cgGetErrorString(err));
return false;
}
cgGLLoadProgram(prg[i].fprg);
cgGLLoadProgram(prg[i].vprg);
}
cgGLEnableProfile(cgFProf);
cgGLEnableProfile(cgVProf);
cgGLBindProgram(cgFPrg);
cgGLBindProgram(cgVPrg);
cg_video_size = cgGetNamedParameter(cgFPrg, "IN.video_size");
cg_texture_size = cgGetNamedParameter(cgFPrg, "IN.texture_size");
cg_output_size = cgGetNamedParameter(cgFPrg, "IN.output_size");
cg_Vvideo_size = cgGetNamedParameter(cgVPrg, "IN.video_size");
cg_Vtexture_size = cgGetNamedParameter(cgVPrg, "IN.texture_size");
cg_Voutput_size = cgGetNamedParameter(cgVPrg, "IN.output_size");
cg_mvp_matrix = cgGetNamedParameter(cgVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cg_mvp_matrix = cgGetNamedParameter(cgSVPrg, "modelViewProj");
cgGLSetStateMatrixParameter(cg_mvp_matrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
for (int i = 0; i < 3; i++)
{
cgGLBindProgram(prg[i].fprg);
cgGLBindProgram(prg[i].vprg);
prg[i].vid_size_f = cgGetNamedParameter(prg[i].fprg, "IN.video_size");
prg[i].tex_size_f = cgGetNamedParameter(prg[i].fprg, "IN.texture_size");
prg[i].out_size_f = cgGetNamedParameter(prg[i].fprg, "IN.output_size");
prg[i].vid_size_v = cgGetNamedParameter(prg[i].vprg, "IN.video_size");
prg[i].tex_size_v = cgGetNamedParameter(prg[i].vprg, "IN.texture_size");
prg[i].out_size_v = cgGetNamedParameter(prg[i].vprg, "IN.output_size");
prg[i].mvp = cgGetNamedParameter(prg[i].vprg, "modelViewProj");
cgGLSetStateMatrixParameter(prg[i].mvp, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
}
cgGLBindProgram(prg[1].fprg);
cgGLBindProgram(prg[1].vprg);
cg_active = true;
return true;
}
void gl_cg_activate(void)
void gl_cg_use(unsigned index)
{
if (cg_active)
{
cgGLBindProgram(cgFPrg);
cgGLBindProgram(cgVPrg);
active_index = index;
cgGLBindProgram(prg[index].vprg);
cgGLBindProgram(prg[index].fprg);
}
}
// Just load a dummy shader.
void gl_cg_deactivate(void)
unsigned gl_cg_num(void)
{
if (cg_active)
{
cgGLBindProgram(cgSFPrg);
cgGLBindProgram(cgSVPrg);
if (prg[0].fprg == prg[2].fprg)
return 1;
else
return 2;
}
else
return 0;
}
bool gl_cg_filter_type(unsigned index, bool *smooth)
{
(void)index;
(void)smooth;
// We don't really care since .cg doesn't have those kinds of semantics by itself ...
return false;
}
void gl_cg_shader_scale(unsigned index, struct gl_fbo_scale *scale)
{
(void)index;
// We don't really care since .cg doesn't have those kinds of semantics by itself ...
scale->valid = false;
}
+7 -2
View File
@@ -20,6 +20,7 @@
#define __SSNES_CG_H
#include <stdbool.h>
#include "gl_common.h"
bool gl_cg_init(const char *path);
@@ -31,7 +32,11 @@ void gl_cg_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
void gl_cg_activate(void);
void gl_cg_deactivate(void);
void gl_cg_use(unsigned index);
unsigned gl_cg_num(void);
bool gl_cg_filter_type(unsigned index, bool *smooth);
void gl_cg_shader_scale(unsigned index, struct gl_fbo_scale *scale);
#endif
+451 -85
View File
@@ -15,17 +15,13 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
//
// GLSL code here is mostly copypasted from bSNES.
//
#include <stdbool.h>
#include <string.h>
#include "general.h"
#include "shader_glsl.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
@@ -42,6 +38,27 @@
#include "gl_common.h"
#ifdef __APPLE__
#define pglCreateProgram glCreateProgram
#define pglUseProgram glUseProgram
#define pglCreateShader glCreateShader
#define pglDeleteShader glDeleteShader
#define pglShaderSource glShaderSource
#define pglCompileShader glCompileShader
#define pglAttachShader glAttachShader
#define pglDetachShader glDetachShader
#define pglLinkProgram glLinkProgram
#define pglGetUniformLocation glGetUniformLocation
#define pglUniform1i glUniform1i
#define pglUniform2fv glUniform2fv
#define pglUniform4fv glUniform4fv
#define pglGetShaderiv glGetShaderiv
#define pglGetShaderInfoLog glGetShaderInfoLog
#define pglGetProgramiv glGetProgramiv
#define pglGetProgramInfoLog glGetProgramInfoLog
#define pglDeleteProgram glDeleteProgram
#define pglGetAttachedShaders glGetAttachedShaders
#else
static PFNGLCREATEPROGRAMPROC pglCreateProgram = NULL;
static PFNGLUSEPROGRAMPROC pglUseProgram = NULL;
static PFNGLCREATESHADERPROC pglCreateShader = NULL;
@@ -59,13 +76,204 @@ static PFNGLGETSHADERIVPROC pglGetShaderiv = NULL;
static PFNGLGETSHADERINFOLOGPROC pglGetShaderInfoLog = NULL;
static PFNGLGETPROGRAMIVPROC pglGetProgramiv = NULL;
static PFNGLGETPROGRAMINFOLOGPROC pglGetProgramInfoLog = NULL;
static PFNGLDELETEPROGRAMPROC pglDeleteProgram = NULL;
static PFNGLGETATTACHEDSHADERSPROC pglGetAttachedShaders = NULL;
#endif
#define MAX_PROGRAMS 16
enum filter_type
{
SSNES_GL_NOFORCE,
SSNES_GL_LINEAR,
SSNES_GL_NEAREST
};
static bool glsl_enable = false;
static GLuint gl_program;
static GLuint fragment_shader;
static GLuint vertex_shader;
static GLuint gl_program[MAX_PROGRAMS] = {0};
static enum filter_type gl_filter_type[MAX_PROGRAMS] = {SSNES_GL_NOFORCE};
static struct gl_fbo_scale gl_scale[MAX_PROGRAMS];
static unsigned gl_num_programs = 0;
static unsigned active_index = 0;
static bool get_xml_shaders(const char *path, char **vertex_shader, char **fragment_shader)
struct shader_program
{
char *vertex;
char *fragment;
enum filter_type filter;
float scale_x;
float scale_y;
unsigned abs_x;
unsigned abs_y;
enum gl_scale_type type_x;
enum gl_scale_type type_y;
bool valid_scale;
};
static bool get_xml_attrs(struct shader_program *prog, xmlNodePtr ptr)
{
prog->scale_x = 1.0;
prog->scale_y = 1.0;
prog->type_x = prog->type_y = SSNES_SCALE_INPUT;
prog->valid_scale = false;
// Check if shader forces a certain texture filtering.
xmlChar *attr = xmlGetProp(ptr, (const xmlChar*)"filter");
if (attr)
{
if (strcmp((const char*)attr, "nearest") == 0)
{
prog->filter = SSNES_GL_NEAREST;
SSNES_LOG("XML: Shader forces GL_NEAREST.\n");
}
else if (strcmp((const char*)attr, "linear") == 0)
{
prog->filter = SSNES_GL_LINEAR;
SSNES_LOG("XML: Shader forces GL_LINEAR.\n");
}
else
SSNES_WARN("XML: Invalid property for filter.\n");
xmlFree(attr);
}
else
prog->filter = SSNES_GL_NOFORCE;
// Check for scaling attributes *lots of code <_<*
xmlChar *attr_scale = xmlGetProp(ptr, (const xmlChar*)"scale");
xmlChar *attr_scale_x = xmlGetProp(ptr, (const xmlChar*)"scale_x");
xmlChar *attr_scale_y = xmlGetProp(ptr, (const xmlChar*)"scale_y");
xmlChar *attr_size = xmlGetProp(ptr, (const xmlChar*)"size");
xmlChar *attr_size_x = xmlGetProp(ptr, (const xmlChar*)"size_x");
xmlChar *attr_size_y = xmlGetProp(ptr, (const xmlChar*)"size_y");
xmlChar *attr_outscale = xmlGetProp(ptr, (const xmlChar*)"outscale");
xmlChar *attr_outscale_x = xmlGetProp(ptr, (const xmlChar*)"outscale_x");
xmlChar *attr_outscale_y = xmlGetProp(ptr, (const xmlChar*)"outscale_y");
unsigned x_attr_cnt = 0, y_attr_cnt = 0;
if (attr_scale)
{
float scale = strtod((const char*)attr_scale, NULL);
prog->scale_x = scale;
prog->scale_y = scale;
prog->valid_scale = true;
prog->type_x = prog->type_y = SSNES_SCALE_INPUT;
SSNES_LOG("Got scale attr: %.1f\n", scale);
x_attr_cnt++;
y_attr_cnt++;
}
if (attr_scale_x)
{
float scale = strtod((const char*)attr_scale_x, NULL);
prog->scale_x = scale;
prog->valid_scale = true;
prog->type_x = SSNES_SCALE_INPUT;
SSNES_LOG("Got scale_x attr: %.1f\n", scale);
x_attr_cnt++;
}
if (attr_scale_y)
{
float scale = strtod((const char*)attr_scale_y, NULL);
prog->scale_y = scale;
prog->valid_scale = true;
prog->type_y = SSNES_SCALE_INPUT;
SSNES_LOG("Got scale_y attr: %.1f\n", scale);
y_attr_cnt++;
}
if (attr_size)
{
prog->abs_x = prog->abs_y = strtoul((const char*)attr_size, NULL, 0);
prog->valid_scale = true;
prog->type_x = prog->type_y = SSNES_SCALE_ABSOLUTE;
SSNES_LOG("Got size attr: %u\n", prog->abs_x);
x_attr_cnt++;
y_attr_cnt++;
}
if (attr_size_x)
{
prog->abs_x = strtoul((const char*)attr_size_x, NULL, 0);
prog->valid_scale = true;
prog->type_x = SSNES_SCALE_ABSOLUTE;
SSNES_LOG("Got size_x attr: %u\n", prog->abs_x);
x_attr_cnt++;
}
if (attr_size_y)
{
prog->abs_y = strtoul((const char*)attr_size_y, NULL, 0);
prog->valid_scale = true;
prog->type_y = SSNES_SCALE_ABSOLUTE;
SSNES_LOG("Got size_y attr: %u\n", prog->abs_y);
y_attr_cnt++;
}
if (attr_outscale)
{
float scale = strtod((const char*)attr_outscale, NULL);
prog->scale_x = scale;
prog->scale_y = scale;
prog->valid_scale = true;
prog->type_x = prog->type_y = SSNES_SCALE_VIEWPORT;
SSNES_LOG("Got outscale attr: %.1f\n", scale);
x_attr_cnt++;
y_attr_cnt++;
}
if (attr_outscale_x)
{
float scale = strtod((const char*)attr_outscale_x, NULL);
prog->scale_x = scale;
prog->valid_scale = true;
prog->type_x = SSNES_SCALE_VIEWPORT;
SSNES_LOG("Got outscale_x attr: %.1f\n", scale);
x_attr_cnt++;
}
if (attr_outscale_y)
{
float scale = strtod((const char*)attr_outscale_y, NULL);
prog->scale_y = scale;
prog->valid_scale = true;
prog->type_y = SSNES_SCALE_VIEWPORT;
SSNES_LOG("Got outscale_y attr: %.1f\n", scale);
y_attr_cnt++;
}
if (attr_scale)
xmlFree(attr_scale);
if (attr_scale_x)
xmlFree(attr_scale_x);
if (attr_scale_y)
xmlFree(attr_scale_y);
if (attr_size)
xmlFree(attr_size);
if (attr_size_x)
xmlFree(attr_size_x);
if (attr_size_y)
xmlFree(attr_size_y);
if (attr_outscale)
xmlFree(attr_outscale);
if (attr_outscale_x)
xmlFree(attr_outscale_x);
if (attr_outscale_y)
xmlFree(attr_outscale_y);
if (x_attr_cnt > 1)
return false;
if (y_attr_cnt > 1)
return false;
return true;
}
static unsigned get_xml_shaders(const char *path, struct shader_program *prog, size_t size)
{
LIBXML_TEST_VERSION;
@@ -97,18 +305,26 @@ static bool get_xml_shaders(const char *path, char **vertex_shader, char **fragm
if (cur->type == XML_ELEMENT_NODE && strcmp((const char*)cur->name, "shader") == 0)
{
xmlChar *attr;
if ((attr = xmlGetProp(cur, (const xmlChar*)"language")) && strcmp((const char*)attr, "GLSL") == 0)
attr = xmlGetProp(cur, (const xmlChar*)"language");
if (attr && strcmp((const char*)attr, "GLSL") == 0)
{
xmlFree(attr);
break;
}
if (attr)
xmlFree(attr);
}
}
if (!cur) // We couldn't find any GLSL shader :(
goto error;
bool vertex_found = false;
bool fragment_found = false;
unsigned num = 0;
memset(prog, 0, sizeof(struct shader_program) * size);
// Iterate to check if we find fragment and/or vertex shaders.
for (cur = cur->children; cur; cur = cur->next)
for (cur = cur->children; cur && num < size; cur = cur->next)
{
if (cur->type != XML_ELEMENT_NODE)
continue;
@@ -117,37 +333,45 @@ static bool get_xml_shaders(const char *path, char **vertex_shader, char **fragm
if (!content)
continue;
if (strcmp((const char*)cur->name, "vertex") == 0 && !vertex_found)
if (strcmp((const char*)cur->name, "vertex") == 0)
{
*vertex_shader = malloc(xmlStrlen(content) + 1);
strcpy(*vertex_shader, (const char*)content);
vertex_found = true;
if (prog[num].vertex)
{
SSNES_ERR("Cannot have more than one vertex shader in a program.\n");
xmlFree(content);
goto error;
}
prog[num].vertex = (char*)content;
}
else if (strcmp((const char*)cur->name, "fragment") == 0 && !fragment_found)
else if (strcmp((const char*)cur->name, "fragment") == 0)
{
*fragment_shader = malloc(xmlStrlen(content) + 1);
strcpy(*fragment_shader, (const char*)content);
fragment_found = true;
prog[num].fragment = (char*)content;
if (!get_xml_attrs(&prog[num], cur))
{
SSNES_ERR("XML shader attributes do not comply with specifications.\n");
goto error;
}
num++;
}
}
if (!vertex_found && !fragment_found)
if (num == 0)
{
SSNES_ERR("Couldn't find vertex shader nor fragment shader in XML file.\n");
goto error;
}
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctx);
return true;
return num;
error:
SSNES_ERR("Failed to load XML shader ...\n");
if (doc)
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctx);
return false;
return 0;
}
static void print_shader_log(GLuint obj)
@@ -178,33 +402,114 @@ static void print_linker_log(GLuint obj)
SSNES_LOG("Linker log: %s\n", info_log);
}
static bool compile_programs(GLuint *gl_prog, struct shader_program *progs, size_t num)
{
for (unsigned i = 0; i < num; i++)
{
gl_prog[i] = pglCreateProgram();
if (!gl_check_error() || gl_prog[i] == 0)
{
SSNES_ERR("Failed to create GL program #%u.\n", i);
return false;
}
if (progs[i].vertex)
{
SSNES_LOG("Found GLSL vertex shader.\n");
GLuint shader = pglCreateShader(GL_VERTEX_SHADER);
pglShaderSource(shader, 1, (const char**)&progs[i].vertex, 0);
pglCompileShader(shader);
print_shader_log(shader);
pglAttachShader(gl_prog[i], shader);
free(progs[i].vertex);
}
if (!gl_check_error())
{
SSNES_ERR("Failed to compile vertex shader #%u\n", i);
return false;
}
if (progs[i].fragment)
{
SSNES_LOG("Found GLSL fragment shader.\n");
GLuint shader = pglCreateShader(GL_FRAGMENT_SHADER);
pglShaderSource(shader, 1, (const char**)&progs[i].fragment, 0);
pglCompileShader(shader);
print_shader_log(shader);
pglAttachShader(gl_prog[i], shader);
free(progs[i].fragment);
}
if (!gl_check_error())
{
SSNES_ERR("Failed to compile fragment shader #%u\n", i);
return false;
}
if (progs[i].vertex || progs[i].fragment)
{
SSNES_LOG("Linking GLSL program.\n");
pglLinkProgram(gl_prog[i]);
pglUseProgram(gl_prog[i]);
print_linker_log(gl_prog[i]);
GLint location = pglGetUniformLocation(gl_prog[i], "rubyTexture");
pglUniform1i(location, 0);
pglUseProgram(0);
}
if (!gl_check_error())
{
SSNES_ERR("Failed to link program #%u\n", i);
return false;
}
}
return true;
}
#define LOAD_GL_SYM(SYM) if (!(pgl##SYM)) pgl##SYM = SDL_GL_GetProcAddress("gl" #SYM)
bool gl_glsl_init(const char *path)
{
#ifndef __APPLE__
// Load shader functions.
pglCreateProgram = SDL_GL_GetProcAddress("glCreateProgram");
pglUseProgram = SDL_GL_GetProcAddress("glUseProgram");
pglCreateShader = SDL_GL_GetProcAddress("glCreateShader");
pglDeleteShader = SDL_GL_GetProcAddress("glDeleteShader");
pglShaderSource = SDL_GL_GetProcAddress("glShaderSource");
pglCompileShader = SDL_GL_GetProcAddress("glCompileShader");
pglAttachShader = SDL_GL_GetProcAddress("glAttachShader");
pglDetachShader = SDL_GL_GetProcAddress("glDetachShader");
pglLinkProgram = SDL_GL_GetProcAddress("glLinkProgram");
pglGetUniformLocation = SDL_GL_GetProcAddress("glGetUniformLocation");
pglUniform1i = SDL_GL_GetProcAddress("glUniform1i");
pglUniform2fv = SDL_GL_GetProcAddress("glUniform2fv");
pglUniform4fv = SDL_GL_GetProcAddress("glUniform4fv");
pglGetShaderiv = SDL_GL_GetProcAddress("glGetShaderiv");
pglGetShaderInfoLog = SDL_GL_GetProcAddress("glGetShaderInfoLog");
pglGetProgramiv = SDL_GL_GetProcAddress("glGetProgramiv");
pglGetProgramInfoLog = SDL_GL_GetProcAddress("glGetProgramInfoLog");
LOAD_GL_SYM(CreateProgram);
LOAD_GL_SYM(UseProgram);
LOAD_GL_SYM(CreateShader);
LOAD_GL_SYM(DeleteShader);
LOAD_GL_SYM(ShaderSource);
LOAD_GL_SYM(CompileShader);
LOAD_GL_SYM(AttachShader);
LOAD_GL_SYM(DetachShader);
LOAD_GL_SYM(LinkProgram);
LOAD_GL_SYM(GetUniformLocation);
LOAD_GL_SYM(Uniform1i);
LOAD_GL_SYM(Uniform2fv);
LOAD_GL_SYM(Uniform4fv);
LOAD_GL_SYM(GetShaderiv);
LOAD_GL_SYM(GetShaderInfoLog);
LOAD_GL_SYM(GetProgramiv);
LOAD_GL_SYM(GetProgramInfoLog);
LOAD_GL_SYM(DeleteProgram);
LOAD_GL_SYM(GetAttachedShaders);
#endif
SSNES_LOG("Checking GLSL shader support ...\n");
#ifdef __APPLE__
const bool shader_support = true;
#else
bool shader_support = pglCreateProgram && pglUseProgram && pglCreateShader
&& pglDeleteShader && pglShaderSource && pglCompileShader && pglAttachShader
&& pglDetachShader && pglLinkProgram && pglGetUniformLocation
&& pglUniform1i && pglUniform2fv && pglUniform4fv
&& pglGetShaderiv && pglGetShaderInfoLog && pglGetProgramiv && pglGetProgramInfoLog;
&& pglGetShaderiv && pglGetShaderInfoLog && pglGetProgramiv && pglGetProgramInfoLog
&& pglDeleteProgram && pglGetAttachedShaders;
#endif
if (!shader_support)
{
@@ -212,88 +517,149 @@ bool gl_glsl_init(const char *path)
return false;
}
gl_program = pglCreateProgram();
struct shader_program progs[MAX_PROGRAMS];
unsigned num_progs = get_xml_shaders(path, progs, MAX_PROGRAMS - 1);
char *vertex_prog = NULL;
char *fragment_prog = NULL;
if (!get_xml_shaders(path, &vertex_prog, &fragment_prog))
if (num_progs == 0)
{
SSNES_ERR("Couldn't find any valid shaders in XML file.\n");
return false;
}
for (unsigned i = 0; i < num_progs; i++)
{
gl_filter_type[i + 1] = progs[i].filter;
gl_scale[i + 1].type_x = progs[i].type_x;
gl_scale[i + 1].type_y = progs[i].type_y;
gl_scale[i + 1].scale_x = progs[i].scale_x;
gl_scale[i + 1].scale_y = progs[i].scale_y;
gl_scale[i + 1].abs_x = progs[i].abs_x;
gl_scale[i + 1].abs_y = progs[i].abs_y;
gl_scale[i + 1].valid = progs[i].valid_scale;
}
if (!compile_programs(&gl_program[1], progs, num_progs))
return false;
if (vertex_prog)
// SSNES custom two-pass with two different files.
if (num_progs == 1 && *g_settings.video.second_pass_shader)
{
vertex_shader = pglCreateShader(GL_VERTEX_SHADER);
pglShaderSource(vertex_shader, 1, (const char**)&vertex_prog, 0);
pglCompileShader(vertex_shader);
print_shader_log(vertex_shader);
pglAttachShader(gl_program, vertex_shader);
free(vertex_prog);
}
if (fragment_prog)
{
fragment_shader = pglCreateShader(GL_FRAGMENT_SHADER);
pglShaderSource(fragment_shader, 1, (const char**)&fragment_prog, 0);
pglCompileShader(fragment_shader);
print_shader_log(fragment_shader);
pglAttachShader(gl_program, fragment_shader);
free(fragment_prog);
}
if (vertex_prog || fragment_prog)
{
pglLinkProgram(gl_program);
pglUseProgram(gl_program);
print_linker_log(gl_program);
GLint location = pglGetUniformLocation(gl_program, "rubyTexture");
pglUniform1i(location, 0);
unsigned secondary_progs = get_xml_shaders(g_settings.video.second_pass_shader, progs, 1);
if (secondary_progs == 1)
{
compile_programs(&gl_program[2], progs, 1);
num_progs++;
}
else
{
SSNES_ERR("Did not find valid shader in secondary shader file.\n");
return false;
}
}
if (!gl_check_error())
return false;
glsl_enable = true;
gl_num_programs = num_progs;
return true;
}
void gl_glsl_deinit(void)
{}
{
if (glsl_enable)
{
pglUseProgram(0);
for (int i = 1; i < MAX_PROGRAMS; i++)
{
if (gl_program[i] == 0)
break;
GLsizei count;
GLuint shaders[2];
pglGetAttachedShaders(gl_program[i], 2, &count, shaders);
for (GLsizei j = 0; j < count; j++)
{
pglDetachShader(gl_program[i], shaders[j]);
pglDeleteShader(shaders[j]);
}
pglDeleteProgram(gl_program[i]);
}
}
memset(gl_program, 0, sizeof(gl_program));
glsl_enable = false;
active_index = 0;
}
void gl_glsl_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height)
{
if (glsl_enable)
if (glsl_enable && gl_program[active_index] > 0)
{
GLint location;
float inputSize[2] = {width, height};
location = pglGetUniformLocation(gl_program, "rubyInputSize");
location = pglGetUniformLocation(gl_program[active_index], "rubyInputSize");
pglUniform2fv(location, 1, inputSize);
float outputSize[2] = {out_width, out_height};
location = pglGetUniformLocation(gl_program, "rubyOutputSize");
location = pglGetUniformLocation(gl_program[active_index], "rubyOutputSize");
pglUniform2fv(location, 1, outputSize);
float textureSize[2] = {tex_width, tex_height};
location = pglGetUniformLocation(gl_program, "rubyTextureSize");
location = pglGetUniformLocation(gl_program[active_index], "rubyTextureSize");
pglUniform2fv(location, 1, textureSize);
}
}
void gl_glsl_set_proj_matrix(void)
{}
void gl_glsl_activate(void)
void gl_glsl_use(unsigned index)
{
if (glsl_enable)
pglUseProgram(gl_program);
{
active_index = index;
pglUseProgram(gl_program[index]);
}
}
void gl_glsl_deactivate(void)
unsigned gl_glsl_num(void)
{
return gl_num_programs;
}
bool gl_glsl_filter_type(unsigned index, bool *smooth)
{
if (!glsl_enable)
return false;
switch (gl_filter_type[index])
{
case SSNES_GL_NOFORCE:
return false;
case SSNES_GL_NEAREST:
*smooth = false;
return true;
case SSNES_GL_LINEAR:
*smooth = true;
return true;
default:
return false;
}
}
void gl_glsl_shader_scale(unsigned index, struct gl_fbo_scale *scale)
{
if (glsl_enable)
pglUseProgram(0);
*scale = gl_scale[index];
else
scale->valid = false;
}
+7 -2
View File
@@ -20,6 +20,7 @@
#define __SSNES_GLSL_H
#include <stdbool.h>
#include "gl_common.h"
bool gl_glsl_init(const char *path);
@@ -31,7 +32,11 @@ void gl_glsl_set_params(unsigned width, unsigned height,
unsigned tex_width, unsigned tex_height,
unsigned out_width, unsigned out_height);
void gl_glsl_activate(void);
void gl_glsl_deactivate(void);
void gl_glsl_use(unsigned index);
unsigned gl_glsl_num(void);
bool gl_glsl_filter_type(unsigned index, bool *smooth);
void gl_glsl_shader_scale(unsigned index, struct gl_fbo_scale *scale);
#endif
+724
View File
@@ -0,0 +1,724 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "general.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <math.h>
#ifdef HAVE_FREETYPE
#include "fonts.h"
#endif
#include "gfx_common.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#include <X11/extensions/Xv.h>
#include <X11/extensions/Xvlib.h>
// Adapted from bSNES and MPlayer source.
typedef struct xv xv_t;
struct xv
{
Display *display;
GC gc;
Window window;
Colormap colormap;
XShmSegmentInfo shminfo;
Atom quit_atom;
bool focus;
XvPortID port;
int depth;
int visualid;
XvImage *image;
uint32_t fourcc;
unsigned width;
unsigned height;
bool keep_aspect;
uint8_t *ytable;
uint8_t *utable;
uint8_t *vtable;
unsigned luma_i;
unsigned chroma_i;
#ifdef HAVE_FREETYPE
font_renderer_t *font;
#endif
void (*render_func)(xv_t*, const void *frame, unsigned width, unsigned height, unsigned pitch);
};
static void xv_set_nonblock_state(void *data, bool state)
{
xv_t *xv = data;
Atom atom = XInternAtom(xv->display, "XV_SYNC_TO_VBLANK", true);
if (atom != None && xv->port >= 0)
XvSetPortAttribute(xv->display, xv->port, atom, !state);
else
SSNES_WARN("Failed to set SYNC_TO_VBLANK attribute.\n");
}
static volatile sig_atomic_t g_quit = 0;
static void sighandler(int sig)
{
g_quit = 1;
}
static void init_yuv_tables(xv_t *xv)
{
xv->ytable = malloc(0x8000);
xv->utable = malloc(0x8000);
xv->vtable = malloc(0x8000);
for (unsigned i = 0; i < 0x8000; i++)
{
// Extract RGB555 color data from i
uint8_t r = (i >> 10) & 0x1F, g = (i >> 5) & 0x1F, b = (i) & 0x1F;
r = (r << 3) | (r >> 2); //R5->R8
g = (g << 3) | (g >> 2); //G5->G8
b = (b << 3) | (b >> 2); //B5->B8
int y = (int)(+((double)r * 0.257) + ((double)g * 0.504) + ((double)b * 0.098) + 16.0);
int u = (int)(-((double)r * 0.148) - ((double)g * 0.291) + ((double)b * 0.439) + 128.0);
int v = (int)(+((double)r * 0.439) - ((double)g * 0.368) - ((double)b * 0.071) + 128.0);
xv->ytable[i] = y < 0 ? 0 : y > 255 ? 255 : y;
xv->utable[i] = u < 0 ? 0 : u > 255 ? 255 : u;
xv->vtable[i] = v < 0 ? 0 : v > 255 ? 255 : v;
}
}
// Source: MPlayer
static void hide_mouse(xv_t *xv)
{
Cursor no_ptr;
Pixmap bm_no;
XColor black, dummy;
Colormap colormap;
static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
colormap = DefaultColormap(xv->display, DefaultScreen(xv->display));
if (!XAllocNamedColor(xv->display, colormap, "black", &black, &dummy))
return;
bm_no = XCreateBitmapFromData(xv->display, xv->window, bm_no_data, 8, 8);
no_ptr = XCreatePixmapCursor(xv->display, bm_no, bm_no, &black, &black, 0, 0);
XDefineCursor(xv->display, xv->window, no_ptr);
XFreeCursor(xv->display, no_ptr);
if (bm_no != None)
XFreePixmap(xv->display, bm_no);
XFreeColors(xv->display, colormap, &black.pixel, 1, 0);
}
static Atom XA_NET_WM_STATE;
static Atom XA_NET_WM_STATE_FULLSCREEN;
#define XA_INIT(x) XA##x = XInternAtom(xv->display, #x, False)
#define _NET_WM_STATE_ADD 1
// Source: MPlayer
static void set_fullscreen(xv_t *xv)
{
XA_INIT(_NET_WM_STATE);
XA_INIT(_NET_WM_STATE_FULLSCREEN);
if (!XA_NET_WM_STATE || !XA_NET_WM_STATE_FULLSCREEN)
{
SSNES_WARN("X11: Cannot set fullscreen.\n");
return;
}
XEvent xev;
xev.xclient.type = ClientMessage;
xev.xclient.serial = 0;
xev.xclient.send_event = True;
xev.xclient.message_type = XA_NET_WM_STATE;
xev.xclient.window = xv->window;
xev.xclient.format = 32;
xev.xclient.data.l[0] = _NET_WM_STATE_ADD;
xev.xclient.data.l[1] = XA_NET_WM_STATE_FULLSCREEN;
xev.xclient.data.l[2] = 0;
xev.xclient.data.l[3] = 0;
xev.xclient.data.l[4] = 0;
XSendEvent(xv->display, DefaultRootWindow(xv->display), False,
SubstructureRedirectMask | SubstructureNotifyMask,
&xev);
}
static void xv_init_font(xv_t *xv, const char *font_path, unsigned font_size)
{
#ifdef HAVE_FREETYPE
if (*font_path)
{
xv->font = font_renderer_new(font_path, font_size);
if (!xv->font)
SSNES_WARN("Failed to init font\n");
}
#endif
}
// We render @ 2x scale to combat chroma downsampling. Also makes fonts more bearable :)
static void render16_yuy2(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint16_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint16_t p = *input++;
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = y0;
output[1] = output[img_width + 1] = u;
output[2] = output[img_width + 2] = y0;
output[3] = output[img_width + 3] = v;
output += 4;
}
input += (pitch >> 1) - width;
output += (xv->width - width) << 2;
}
}
static void render16_uyvy(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint16_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint16_t p = *input++;
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = u;
output[1] = output[img_width + 1] = y0;
output[2] = output[img_width + 2] = v;
output[3] = output[img_width + 3] = y0;
output += 4;
}
input += (pitch >> 1) - width;
output += (xv->width - width) << 2;
}
}
static void render32_yuy2(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint32_t *input = input_;
uint8_t *output = (uint8_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint32_t p = *input++;
p = ((p >> 17) & 0x7c00) | ((p >> 14) & 0x03e0) | ((p >> 11) & 0x1f); // RGBA -> RGB15
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = y0;
output[1] = output[img_width + 1] = u;
output[2] = output[img_width + 2] = y0;
output[3] = output[img_width + 3] = v;
output += 4;
}
input += (pitch >> 2) - width;
output += (xv->width - width) << 2;
}
}
static void render32_uyvy(xv_t *xv, const void *input_, unsigned width, unsigned height, unsigned pitch)
{
const uint32_t *input = input_;
uint16_t *output = (uint16_t*)xv->image->data;
for (unsigned y = 0; y < height; y++)
{
for (unsigned x = 0; x < width; x++)
{
uint32_t p = *input++;
p = ((p >> 17) & 0x7c00) | ((p >> 14) & 0x03e0) | ((p >> 11) & 0x1f); // RGBA -> RGB15
uint8_t y0 = xv->ytable[p];
uint8_t u = xv->utable[p];
uint8_t v = xv->vtable[p];
unsigned img_width = xv->width << 1;
output[0] = output[img_width] = u;
output[1] = output[img_width + 1] = y0;
output[2] = output[img_width + 2] = v;
output[3] = output[img_width + 3] = y0;
output += 4;
}
input += (pitch >> 2) - width;
output += (xv->width - width) << 2;
}
}
static void* xv_init(video_info_t *video, const input_driver_t **input, void **input_data)
{
xv_t *xv = calloc(1, sizeof(*xv));
if (!xv)
return NULL;
xv->display = XOpenDisplay(NULL);
if (!XShmQueryExtension(xv->display))
{
SSNES_ERR("XVideo: XShm extension not found.\n");
goto error;
}
xv->keep_aspect = video->force_aspect;
// Find an appropriate Xv port.
xv->port = -1;
XvAdaptorInfo *adaptor_info;
unsigned adaptor_count = 0;
XvQueryAdaptors(xv->display, DefaultRootWindow(xv->display), &adaptor_count, &adaptor_info);
for (unsigned i = 0; i < adaptor_count; i++)
{
// Find adaptor that supports both input (memory->drawable) and image (drawable->screen) masks.
if (adaptor_info[i].num_formats < 1) continue;
if (!(adaptor_info[i].type & XvInputMask)) continue;
if (!(adaptor_info[i].type & XvImageMask)) continue;
xv->port = adaptor_info[i].base_id;
xv->depth = adaptor_info[i].formats->depth;
xv->visualid = adaptor_info[i].formats->visual_id;
break;
}
XvFreeAdaptorInfo(adaptor_info);
if (xv->port < 0)
{
SSNES_ERR("XVideo: Failed to find valid XvPort.\n");
goto error;
}
XVisualInfo visualtemplate;
visualtemplate.visualid = xv->visualid;
visualtemplate.screen = DefaultScreen(xv->display);
visualtemplate.depth = xv->depth;
visualtemplate.visual = 0;
int visualmatches = 0;
XVisualInfo *visualinfo = XGetVisualInfo(xv->display, VisualIDMask | VisualScreenMask | VisualDepthMask, &visualtemplate, &visualmatches);
if (visualmatches < 1 || !visualinfo->visual)
{
if (visualinfo) XFree(visualinfo);
SSNES_ERR("XVideo: Unable to find Xv-compatible visual.\n");
goto error;
}
xv->colormap = XCreateColormap(xv->display, DefaultRootWindow(xv->display), visualinfo->visual, AllocNone);
XSetWindowAttributes attributes;
attributes.colormap = xv->colormap;
attributes.border_pixel = 0;
attributes.event_mask = StructureNotifyMask | DestroyNotify | ClientMessage;
unsigned width = video->fullscreen ? ((video->width == 0) ? 256 : video->width) : video->width;
unsigned height = video->fullscreen ? ((video->height == 0) ? 224 : video->height) : video->height;
xv->window = XCreateWindow(xv->display, DefaultRootWindow(xv->display),
0, 0, width, height,
0, xv->depth, InputOutput, visualinfo->visual,
CWColormap | CWBorderPixel | CWEventMask, &attributes);
XFree(visualinfo);
XSetWindowBackground(xv->display, xv->window, 0);
XMapWindow(xv->display, xv->window);
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
XStoreName(xv->display, xv->window, buf);
if (video->fullscreen)
set_fullscreen(xv);
hide_mouse(xv);
xv->gc = XCreateGC(xv->display, xv->window, 0, 0);
// Set colorkey to auto paint, so that Xv video output is always visible
Atom atom = XInternAtom(xv->display, "XV_AUTOPAINT_COLORKEY", true);
if(atom != None) XvSetPortAttribute(xv->display, xv->port, atom, 1);
int format_count;
XvImageFormatValues *format = XvListImageFormats(xv->display, xv->port, &format_count);
bool has_format = false;
for (int i = 0; i < format_count; i++)
{
if (format[i].type == XvYUV && format[i].bits_per_pixel == 16 && format[i].format == XvPacked)
{
if (format[i].component_order[0] == 'Y' && format[i].component_order[1] == 'U'
&& format[i].component_order[2] == 'Y' && format[i].component_order[3] == 'V')
{
has_format = true;
xv->fourcc = format[i].id;
xv->render_func = video->rgb32 ? render32_yuy2 : render16_yuy2;
xv->luma_i = 0; xv->chroma_i = 1;
break;
}
}
}
if (!has_format) for (int i = 0; i < format_count; i++)
{
if (format[i].type == XvYUV && format[i].bits_per_pixel == 16 && format[i].format == XvPacked)
{
if (format[i].component_order[0] == 'U' && format[i].component_order[1] == 'Y'
&& format[i].component_order[2] == 'V' && format[i].component_order[3] == 'Y')
{
has_format = true;
xv->fourcc = format[i].id;
xv->render_func = video->rgb32 ? render32_uyvy : render16_uyvy;
xv->chroma_i = 0; xv->luma_i = 1;
break;
}
}
}
free(format);
if (!has_format)
{
SSNES_ERR("XVideo: unable to find a supported image format.\n");
goto error;
}
xv->width = 512;
xv->height = 512;
xv->image = XvShmCreateImage(xv->display, xv->port, xv->fourcc, NULL, xv->width, xv->height, &xv->shminfo);
if (!xv->image)
{
SSNES_ERR("XVideo: XShmCreateImage failed.\n");
goto error;
}
xv->width = xv->image->width;
xv->height = xv->image->height;
xv->shminfo.shmid = shmget(IPC_PRIVATE, xv->image->data_size, IPC_CREAT | 0777);
xv->shminfo.shmaddr = xv->image->data = shmat(xv->shminfo.shmid, NULL, 0);
xv->shminfo.readOnly = false;
if (!XShmAttach(xv->display, &xv->shminfo))
{
SSNES_ERR("XVideo: XShmAttach failed.\n");
goto error;
}
XSync(xv->display, False);
memset(xv->image->data, 128, xv->image->data_size);
xv->quit_atom = XInternAtom(xv->display, "WM_DELETE_WINDOW", False);
if (xv->quit_atom)
XSetWMProtocols(xv->display, xv->window, &xv->quit_atom, 1);
struct sigaction sa;
sa.sa_handler = sighandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
xv_set_nonblock_state(xv, !video->vsync);
xv->focus = true;
xv_init_font(xv, g_settings.video.font_path, g_settings.video.font_size);
void *xinput = input_x.init();
if (xinput)
{
*input = &input_x;
*input_data = xinput;
}
else
*input = NULL;
init_yuv_tables(xv);
return xv;
error:
free(xv);
return NULL;
}
static bool check_resize(xv_t *xv, unsigned width, unsigned height)
{
// We render @ 2x scale to combat chroma downsampling.
if (xv->width != (width << 1) || xv->height != (height << 1))
{
xv->width = width << 1;
xv->height = height << 1;
XShmDetach(xv->display, &xv->shminfo);
shmdt(xv->shminfo.shmaddr);
shmctl(xv->shminfo.shmid, IPC_RMID, NULL);
XFree(xv->image);
memset(&xv->shminfo, 0, sizeof(xv->shminfo));
xv->image = XvShmCreateImage(xv->display, xv->port, xv->fourcc, NULL, xv->width, xv->height, &xv->shminfo);
if (xv->image == None)
{
SSNES_ERR("Failed to create image.\n");
return false;
}
xv->width = xv->image->width;
xv->height = xv->image->height;
xv->shminfo.shmid = shmget(IPC_PRIVATE, xv->image->data_size, IPC_CREAT | 0777);
if (xv->shminfo.shmid < 0)
{
SSNES_ERR("Failed to init SHM.\n");
return false;
}
xv->shminfo.shmaddr = xv->image->data = shmat(xv->shminfo.shmid, NULL, 0);
xv->shminfo.readOnly = false;
if (!XShmAttach(xv->display, &xv->shminfo))
{
SSNES_ERR("Failed to reattch XvShm image.\n");
return false;
}
XSync(xv->display, False);
memset(xv->image->data, 128, xv->image->data_size);
}
return true;
}
static void calc_out_rect(bool keep_aspect, unsigned *x, unsigned *y, unsigned *width, unsigned *height, unsigned vp_width, unsigned vp_height)
{
if (!keep_aspect)
{
*x = 0; *y = 0; *width = vp_width; *height = vp_height;
}
else
{
float desired_aspect = g_settings.video.aspect_ratio;
float device_aspect = (float)vp_width / vp_height;
// If the aspect ratios of screen and desired aspect ratio are sufficiently equal (floating point stuff),
// assume they are actually equal.
if (fabs(device_aspect - desired_aspect) < 0.0001)
{
*x = 0; *y = 0; *width = vp_width; *height = vp_height;
}
else if (device_aspect > desired_aspect)
{
float delta = (desired_aspect / device_aspect - 1.0) / 2.0 + 0.5;
*x = vp_width * (0.5 - delta); *y = 0; *width = 2.0 * vp_width * delta; *height = vp_height;
}
else
{
float delta = (device_aspect / desired_aspect - 1.0) / 2.0 + 0.5;
*x = 0; *y = vp_height * (0.5 - delta); *width = vp_width; *height = 2.0 * vp_height * delta;
}
}
}
// TODO: Is there some way to render directly like GL? :(
static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned height)
{
#ifdef HAVE_FREETYPE
if (!xv->font)
return;
struct font_output_list out;
font_renderer_msg(xv->font, msg, &out);
struct font_output *head = out.head;
int _base_x = g_settings.video.msg_pos_x * width;
int _base_y = height - g_settings.video.msg_pos_y * height;
unsigned luma_i = xv->luma_i;
unsigned chroma_i = xv->chroma_i;
while (head)
{
int base_x = _base_x + head->off_x;
int base_y = _base_y - head->off_y;
if (base_y >= 0)
{
for (int y = 0; y < head->height && (base_y + y) < height; y++)
{
if (base_x < 0)
continue;
const uint8_t *a = head->output + head->pitch * y;
uint8_t *out = (uint8_t*)xv->image->data + (base_y - head->height + y) * (width << 1) + (base_x << 1);
for (int x = 0; x < (head->width << 1) && (base_x + x) < width; x += 2)
{
// Blend luma
uint8_t blend = a[x >> 1];
unsigned blended = blend + (((256 - blend) * (unsigned)out[x + luma_i]) >> 8);
blended = blended > 255 ? 255 : blended;
out[x + luma_i] = blended;
// Blend chroma
blended = (128 * blend + ((256 - blend) * (unsigned)out[x + chroma_i])) >> 8;
blended = blended > 255 ? 255 : blended;
out[x + chroma_i] = blended;
}
}
}
head = head->next;
}
font_renderer_free_output(&out);
#else
(void)xv;
(void)msg;
(void)width;
(void)height;
#endif
}
static bool xv_frame(void *data, const void* frame, unsigned width, unsigned height, unsigned pitch, const char *msg)
{
xv_t *xv = data;
if (!check_resize(xv, width, height))
return false;
XWindowAttributes target;
XGetWindowAttributes(xv->display, xv->window, &target);
xv->render_func(xv, frame, width, height, pitch);
if (msg)
xv_render_msg(xv, msg, xv->width, xv->height);
unsigned x, y, owidth, oheight;
calc_out_rect(xv->keep_aspect, &x, &y, &owidth, &oheight, target.width, target.height);
XvShmPutImage(xv->display, xv->port, xv->window, xv->gc, xv->image,
0, 0, width << 1, height << 1,
x, y, owidth, oheight,
true);
XSync(xv->display, False);
char buf[128];
if (gfx_window_title(buf, sizeof(buf)))
XStoreName(xv->display, xv->window, buf);
return true;
}
static bool xv_alive(void *data)
{
xv_t *xv = data;
XEvent event;
while (XPending(xv->display))
{
XNextEvent(xv->display, &event);
switch (event.type)
{
case ClientMessage:
if (event.xclient.data.l[0] == xv->quit_atom)
return false;
break;
case DestroyNotify:
return false;
case MapNotify: // Find something that works better.
xv->focus = true;
break;
case UnmapNotify:
xv->focus = false;
break;
default:
break;
}
}
return !g_quit;
}
static bool xv_focus(void *data)
{
xv_t *xv = data;
return xv->focus;
}
static void xv_free(void *data)
{
xv_t *xv = data;
XShmDetach(xv->display, &xv->shminfo);
shmdt(xv->shminfo.shmaddr);
shmctl(xv->shminfo.shmid, IPC_RMID, NULL);
XFree(xv->image);
if(xv->window)
XUnmapWindow(xv->display, xv->window);
if(xv->colormap)
XFreeColormap(xv->display, xv->colormap);
XCloseDisplay(xv->display);
free(xv->ytable);
free(xv->utable);
free(xv->vtable);
#ifdef HAVE_FREETYPE
if (xv->font)
font_renderer_free(xv->font);
#endif
free(xv);
}
const video_driver_t video_xvideo = {
.init = xv_init,
.frame = xv_frame,
.alive = xv_alive,
.set_nonblock_state = xv_set_nonblock_state,
.focus = xv_focus,
.free = xv_free,
.ident = "xvideo"
};
-45
View File
@@ -1,45 +0,0 @@
#include "bleed.h"
#include <stdlib.h>
#include <string.h>
void bleed_filter(uint16_t *data, int width, int height)
{
uint16_t tmp[4];
int ptr = 0;
uint16_t r[4];
uint16_t g[4];
uint16_t b[4];
uint16_t bleed_r;
uint16_t bleed_g;
uint16_t bleed_b;
float rand_map[4];
for (int h = 0; h < height; h++ )
{
memcpy(tmp, data, sizeof(tmp));
ptr = 3;
for (int i = 0; i < 4; i++)
{
rand_map[i] = (rand() % 20)/1000.0; // 0.02 * 4 = 0.08
}
for (int i = 3; i < width; i++, ptr++)
{
tmp[ptr & 0x3] = data[i];
for (int y = 0; y < 4; y++)
{
r[y] = (tmp[(ptr + y) & 0x3] >> 10) & 0x1F;
g[y] = (tmp[(ptr + y) & 0x3] >> 5) & 0x1F;
b[y] = (tmp[(ptr + y) & 0x3] >> 0) & 0x1F;
}
bleed_r = r[0] * (0.05 + rand_map[0] ) + r[1] * (0.10 + rand_map[1] ) + r[2] * (0.20 + rand_map[2]) + r[3] * (0.57 + rand_map[3] ); // 0.92
bleed_g = g[0] * (0.03 + rand_map[2]/3) + g[1] * (0.10 + rand_map[0]/3) + g[2] * (0.20 + rand_map[3]/3) + g[3] * (0.63 + rand_map[1]/3); // 0.96
bleed_b = b[0] * (0.05 + rand_map[3] ) + b[1] * (0.10 + rand_map[2] ) + b[2] * (0.20 + rand_map[1]) + b[3] * (0.57 + rand_map[0] ); // 0.92
data[i] = (bleed_r << 10) | (bleed_g << 5) | (bleed_b);
}
data += width;
}
}
-3
View File
@@ -1,3 +0,0 @@
#include <stdint.h>
void bleed_filter(uint16_t *data, int width, int height);
-94
View File
@@ -1,94 +0,0 @@
/*
Copyright (C) 2007 guest(r) - [email protected]
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
The 2xSaL shader processes a gfx. surface and redraws it 2x finer.
A linear post-resize can fit the image to any resolution.
Note: set scaler to normal2x.
*/
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
struct deltas
{
float2 UL, UR, DL, DR;
};
output main_fragment (float2 tex : TEXCOORD0, uniform input IN, uniform sampler2D s_p : TEXUNIT0)
{
float2 texsize = IN.texture_size;
float dx = pow(texsize.x, -1.0) * 0.25;
float dy = pow(texsize.y, -1.0) * 0.25;
float3 dt = float3(1.0, 1.0, 1.0);
deltas VAR = {
tex + float2(-dx, -dy),
tex + float2(dx, -dy),
tex + float2(-dx, dy),
tex + float2(dx, dy)
};
float3 c00 = tex2D(s_p, VAR.UL).xyz;
float3 c20 = tex2D(s_p, VAR.UR).xyz;
float3 c02 = tex2D(s_p, VAR.DL).xyz;
float3 c22 = tex2D(s_p, VAR.DR).xyz;
float m1=dot(abs(c00-c22),dt)+0.001;
float m2=dot(abs(c02-c20),dt)+0.001;
output OUT;
OUT.color = float4((m1*(c02+c20)+m2*(c22+c00))/(2.0*(m1+m2)),1.0);
return OUT;
}
-95
View File
@@ -1,95 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
float Luminance = 0.09f;
static const float fMiddleGray = 0.18f;
static const float fWhiteCutoff = 0.8f;
#define NUM 13
float2 PixelOffsets[NUM] =
{
{ -0.006, -0.006 },
{ -0.005, -0.005 },
{ -0.004, -0.004 },
{ -0.003, -0.003 },
{ -0.002, -0.002 },
{ -0.001, -0.001 },
{ 0.000, 0.000 },
{ 0.001, 0.001 },
{ 0.002, 0.002 },
{ 0.003, 0.003 },
{ 0.004, 0.004 },
{ 0.005, 0.005 },
{ 0.006, 0.006 },
};
static const float BlurWeights[NUM] =
{
0.002216,
0.008764,
0.026995,
0.064759,
0.120985,
0.176033,
0.199471,
0.176033,
0.120985,
0.064759,
0.026995,
0.008764,
0.002216,
};
struct output
{
float4 color : COLOR;
};
output main_fragment( float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float2 BloomSettings;
BloomSettings.x = 1;
BloomSettings.y = 0.3;
float3 pixel;
float3 Color = 0;
for(int i = 0; i < NUM; i++)
{
pixel = tex2D(s0, Tex + PixelOffsets[i] * 5.0f)+BloomSettings.y;
pixel *= fMiddleGray / (Luminance + 0.001f);
pixel *= (1.0f + (pixel / (fWhiteCutoff * fWhiteCutoff)));
pixel -= 5.0f;
pixel = max(pixel,0.0f);
pixel /= (10.0f + pixel);
Color += pixel * BlurWeights[i];
}
Color *= BloomSettings.x;
output OUT;
OUT.color = float4(Color,1.0) + tex2D(s0,Tex);
return OUT;
}
-55
View File
@@ -1,55 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
const float2 samples[12]
= {
-0.326212, -0.405805,
-0.840144, -0.073580,
-0.695914, 0.457137,
-0.203345, 0.620716,
0.962340, -0.194983,
0.473434, -0.480026,
0.519456, 0.767022,
0.185461, -0.893124,
0.507431, 0.064425,
0.896420, 0.412458,
-0.321940, -0.932615,
-0.791559, -0.597705
};
struct output
{
float4 col : COLOR;
};
output main_fragment(in float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float4 color = tex2D( s0, Tex.xy);
float BlurFactor = 0.0025; //set between 0.001 and 0.05
for (int i = 0; i < 12; i++)
{
color += tex2D(s0, Tex + BlurFactor * samples[i]);
}
output OUT;
OUT.col = color / 13;
return OUT;
}
-100
View File
@@ -1,100 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
#define TEX2D(c) tex2D(decal,(c))
#define PI 3.141592653589
#define phase 0.0
#define gamma 2.5
#define distortion 0.04
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
float2 barrelDistortion(float2 coord)
{
float2 cc = coord - 0.5;
float dist = dot(cc, cc);
return coord + cc * (dist + distortion * dist * dist) * distortion;
}
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
output OUT;
float2 rubyInputSize = IN.video_size;
float2 rubyOutputSize = IN.output_size;
float2 rubyTextureSize = IN.texture_size;
float2 xy = barrelDistortion(texCoord.xy);
float2 one = 1.0/rubyTextureSize;
xy = xy + float2(0.0 , -0.5 * (phase + (1-phase) * rubyInputSize.y/rubyOutputSize.y) * one.y);
float4 texels[8];
texels[0] = TEX2D(xy + float2(-one.x,0.0));
texels[1] = TEX2D(xy);
texels[2] = TEX2D(xy + float2(one.x, 0.0));
texels[3] = TEX2D(xy + float2(2 * one.x, 0.0));
texels[4] = TEX2D(xy + float2(-one.x,one.y));
texels[5] = TEX2D(xy + float2(0.0, one.y));
texels[6] = TEX2D(xy + one);
texels[7] = TEX2D(xy + float2(2 * one.x, one.y));
float2 uv_ratio = frac(xy*rubyTextureSize);
float4 col, col2;
float4 coeffs = float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
coeffs = (sin(PI * coeffs) * sin(PI * coeffs / 2.0)) / (coeffs * coeffs);
coeffs = coeffs / (coeffs.x+coeffs.y+coeffs.z+coeffs.w);
col = clamp(coeffs.x * texels[0] + coeffs.y * texels[1] + coeffs.z * texels[2] + coeffs.w * texels[3], 0.0, 1.0);
col2 = clamp(coeffs.x * texels[4] + coeffs.y * texels[5] + coeffs.z * texels[6] + coeffs.w * texels[7], 0.0, 1.0);
col = pow(col, gamma);
col2 = pow(col2, gamma);
float4 wid = 2 + 2 * pow(col, 4.0);
float4 weights = uv_ratio.y/0.3;
weights = 0.51*exp(-pow(weights*sqrt(2/wid),wid))/0.3/(0.6+0.2*wid);
wid = 2 + 4 * pow(col2,4.0);
float4 weights2 = (1.0-uv_ratio.y)/0.3;
weights2 = 0.51*exp(-pow(weights2*sqrt(2/wid),wid))/0.3/(0.6+0.2*wid);
float4 mcol = 1.0;
if ( fmod(xy.x*rubyOutputSize.x,2.0) < 1.0)
mcol.g = 0.7;
else
mcol.rb = 0.7;
//OUT.color = pow(mcol*(col * weights + col2 * weights2), 1.0/2.2);
OUT.color = 1.0;
return OUT;
}
-94
View File
@@ -1,94 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
const float mx = 0.325; // start smoothing wt.
const float k = -0.250; // wt. decrease factor
const float max_w = 0.25; // max filter weigth
const float min_w =-0.05; // min filter weigth
const float lum_add = 0.25; // effects smoothing
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
float x = 0.5 * (1.0 / IN.texture_size.x);
float y = 0.5 * (1.0 / IN.texture_size.y);
float2 dg1 = float2( x, y);
float2 dg2 = float2(-x, y);
float2 dx = float2(x, 0.0);
float2 dy = float2(0.0, y);
float4 TexCoord[5];
TexCoord[0] = float4(texCoord, 0.0, 0.0);
TexCoord[1].xy = TexCoord[0].xy - dg1;
TexCoord[1].zw = TexCoord[0].xy - dy;
TexCoord[2].xy = TexCoord[0].xy - dg2;
TexCoord[2].zw = TexCoord[0].xy + dx;
TexCoord[3].xy = TexCoord[0].xy + dg1;
TexCoord[3].zw = TexCoord[0].xy + dy;
TexCoord[4].xy = TexCoord[0].xy + dg2;
TexCoord[4].zw = TexCoord[0].xy - dx;
float3 c00 = tex2D(decal, TexCoord[1].xy).xyz;
float3 c10 = tex2D(decal, TexCoord[1].zw).xyz;
float3 c20 = tex2D(decal, TexCoord[2].xy).xyz;
float3 c01 = tex2D(decal, TexCoord[4].zw).xyz;
float3 c11 = tex2D(decal, TexCoord[0].xy).xyz;
float3 c21 = tex2D(decal, TexCoord[2].zw).xyz;
float3 c02 = tex2D(decal, TexCoord[4].xy).xyz;
float3 c12 = tex2D(decal, TexCoord[3].zw).xyz;
float3 c22 = tex2D(decal, TexCoord[3].xy).xyz;
float3 dt = float3(1.0, 1.0, 1.0);
float md1 = dot(abs(c00 - c22), dt);
float md2 = dot(abs(c02 - c20), dt);
float w1 = dot(abs(c22 - c11), dt) * md2;
float w2 = dot(abs(c02 - c11), dt) * md1;
float w3 = dot(abs(c00 - c11), dt) * md2;
float w4 = dot(abs(c20 - c11), dt) * md1;
float t1 = w1 + w3;
float t2 = w2 + w4;
float ww = max(t1, t2) + 0.0001;
c11 = (w1 * c00 + w2 * c20 + w3 * c22 + w4 * c02 + ww * c11) / (t1 + t2 + ww);
float lc1 = k / (0.12 * dot(c10 + c12 + c11, dt) + lum_add);
float lc2 = k / (0.12 * dot(c01 + c21 + c11, dt) + lum_add);
w1 = clamp(lc1 * dot(abs(c11 - c10), dt) + mx, min_w, max_w);
w2 = clamp(lc2 * dot(abs(c11 - c21), dt) + mx, min_w, max_w);
w3 = clamp(lc1 * dot(abs(c11 - c12), dt) + mx, min_w, max_w);
w4 = clamp(lc2 * dot(abs(c11 - c01), dt) + mx, min_w, max_w);
output OUT;
OUT.color = float4(w1 * c10 + w2 * c21 + w3 * c12 + w4 * c01 + (1.0 - w1 - w2 - w3 - w4) * c11, 1.0);
return OUT;
}
-87
View File
@@ -1,87 +0,0 @@
/*
Author: Themaister
License: Public domain
*/
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
struct deltas
{
float2 UL, UR, DL, DR;
};
float3 quad_inter(float3 x0, float3 x1, float3 x2, float x)
{
float3 poly[3];
poly[2] = 0.5*x0 - x1 + 0.5*x2;
poly[1] = -1.5*x0 + 2.0*x1 - 0.5*x2;
poly[0] = x0;
return poly[2] * x * x + poly[1] * x + poly[0];
}
output main_fragment (float2 tex : TEXCOORD0, uniform input IN, uniform sampler2D s0 : TEXUNIT0)
{
float2 texsize = IN.texture_size;
float sharpness = 2.0;
float dx = float(pow(sharpness * texsize.x, -1.0));
float dy = float(pow(sharpness * texsize.y, -1.0));
float3 c00 = tex2D(s0, tex + float2(-dx, -dy)).xyz;
float3 c01 = tex2D(s0, tex + float2(-dx, 0)).xyz;
float3 c02 = tex2D(s0, tex + float2(-dx, dy)).xyz;
float3 c10 = tex2D(s0, tex + float2(0, -dy)).xyz;
float3 c11 = tex2D(s0, tex + float2(0, 0)).xyz;
float3 c12 = tex2D(s0, tex + float2(0, dy)).xyz;
float3 c20 = tex2D(s0, tex + float2(dx, -dy)).xyz;
float3 c21 = tex2D(s0, tex + float2(dx, 0)).xyz;
float3 c22 = tex2D(s0, tex + float2(dx, dy)).xyz;
float frac_amt_x = frac(tex.x * texsize.x);
float frac_amt_y = frac(tex.y * texsize.y);
float3 loval = quad_inter(c00, c10, c20, frac_amt_x + 0.5);
float3 midval = quad_inter(c01, c11, c21, frac_amt_x + 0.5);
float3 hival = quad_inter(c02, c12, c22, frac_amt_x + 0.5);
float3 res = quad_inter(loval, midval, hival, frac_amt_y + 0.5);
output OUT;
// Bilinear!
// float3 first = lerp(c00, c20, frac(tex.x * texsize.x + 0.5));
// float3 second = lerp(c02, c22, frac(tex.x * texsize.x + 0.5));
// float3 res = lerp(first, second, frac(tex.y * texsize.y + 0.5));
// OUT.color = float4(res, 1.0);
OUT.color = float4(res, 1.0);
return OUT;
}
-46
View File
@@ -1,46 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
output OUT;
float winHeight = IN.output_size.y;
float4 tex_color = tex2D(decal, texCoord);
OUT.color = (1.0 - 0.60 * floor(fmod(texCoord.y * winHeight, 2.0))) * tex_color;
return OUT;
}
-38
View File
@@ -1,38 +0,0 @@
/* Default Vertex shader */
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
output main_fragment( in float2 Tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0 )
{
float4 Color = tex2D( s0, Tex.xy );
float Sharpenfactor = 30; //Make this between 10 and 50;
Color -= tex2D( s0, Tex.xy+0.0001)*Sharpenfactor;
Color += tex2D( s0, Tex.xy-0.0001)*Sharpenfactor;
Color.a = 1.0;
output OUT;
OUT.color = Color;
return OUT;
}
-21
View File
@@ -1,21 +0,0 @@
/* Very simple grayscale filter.
* Author: Hans-Kristian Arntzen
* License: Public domain
*/
#include "grayscale.h"
// Input format 0RRRRRGGGGGBBBBB. Colors themselves could be switched around.
void grayscale_filter(uint16_t *data, int width, int height)
{
for (int i = 0; i < width * height; i++ )
{
int r, g, b, color;
r = (data[i] >> 10) & 0x1F;
g = (data[i] >> 5) & 0x1F;
b = (data[i] >> 0) & 0x1F;
color = (int)(r * 0.3 + g * 0.59 + b * 0.11);
data[i] = (color << 10) | (color << 5) | color;
}
}
-7
View File
@@ -1,7 +0,0 @@
/* Very simple grayscale filter.
* Author: Hans-Kristian Arntzen
* License: Public domain
*/
#include <stdint.h>
void grayscale_filter(uint16_t *data, int width, int height);
-555
View File
@@ -1,555 +0,0 @@
/* This source code is Copyright (C) 2006-2009 by WolfWings. */
/* It is, however, released under the ISC license. */
/* en.wikipedia.org/wiki/ISC_Licence */
/* --------------------------------------------------------- */
/* It is a formula-level rederiviation of the HQ2x technique */
/* and is, thus, not a derivative work of the original code, */
/* only the original equations behind the code. */
#include <stdlib.h>
#include <string.h>
#include "pastlib.h"
static const uint8_t blends_2x[14*4] = {
/* 5 2 4 1 */
8, 4, 4, 0,
8, 4, 0, 4,
8, 0, 4, 4,
16, 0, 0, 0,
12, 4, 0, 0,
12, 0, 4, 0,
12, 0, 0, 4,
4, 6, 6, 0, /* Interp9 */
12, 2, 2, 0, /* Interp7 */
14, 1, 1, 0, /* Interp10*/
10, 4, 2, 0, /* Interp6 */
10, 2, 4, 0, /* Interp6 */
4, 6, 6, 0, /* Added for secondary blending used in HQ4x */
8, 4, 4, 0, /* Added for secondary blending used in HQ4x */
};
static const uint8_t blends_4x[14*16] = {
/* 5 2 4 1 5 2 4 1 5 2 4 1 5 2 4 1 */
8, 4, 4, 0, 10, 4, 2, 0, 10, 2, 4, 0, 12, 2, 2, 0, /* Needs to be split. B */
10, 0, 0, 6, 10, 4, 0, 2, 12, 0, 0, 4, 14, 0, 0, 2,
10, 0, 0, 6, 10, 0, 4, 2, 12, 0, 0, 4, 14, 0, 0, 2,
16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, /* Solid colors */
10, 6, 0, 0, 10, 6, 0, 0, 14, 2, 0, 0, 14, 2, 0, 0,
10, 0, 6, 0, 14, 0, 2, 0, 10, 0, 6, 0, 14, 0, 2, 0,
10, 0, 0, 6, 12, 0, 0, 4, 12, 0, 0, 4, 14, 0, 0, 2,
0, 8, 8, 0, 4, 8, 4, 0, 0, 6,10, 0, 12, 2, 2, 0, /* Needs to be split. A */
8, 4, 4, 0, 12, 4, 0, 0, 12, 0, 4, 0, 16, 0, 0, 0,
8, 4, 4, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0,
12, 4, 0, 0, 4,12, 0, 0, 10, 0, 4, 0, 14, 0, 2, 0,
12, 0, 4, 0, 10, 4, 0, 0, 4, 0,12, 0, 14, 2, 0, 0,
0, 8, 8, 0, 0,10, 6, 0, 4, 4, 8, 0, 12, 2, 2, 0, /* Needs to be split. A */
0, 8, 8, 0, 8, 8, 0, 0, 8, 0, 8, 0, 16, 0, 0, 0, /* Needs to be split. B */
};
static const uint8_t tree_hq[0x800] = {
/* 6 6 6 6 6 6 6 6
* 2 2 2 2 2 2 2 2
* 4 4 4 4 4 4 4 4
* 8 8 8 8 8 8 8 8
*/
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8, /* */
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8, /* 3 */
0, 0, 5,10, 4, 4,13,13, 0, 0, 5, 5,11, 4,13,13, /* 1 */
0, 0, 5,10, 4, 4,12,13, 0, 0, 5, 5,11, 4,13,13, /* 31 */
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8, /* 7 */
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6, /* 3 7 */
0, 0, 5,10, 4, 4, 7,13, 0, 0, 5, 5,11, 4,13,13, /* 17 */
0, 0, 5,10, 4, 4, 9, 9, 0, 0, 5,10,11,11, 9, 9, /* 317 */
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8, /* 9 */
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6, /* 3 9 */
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13, /* 1 9 */
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13, /* 31 9 */
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6, /* 79 */
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6, /* 3 79 */
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13, /* 179 */
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9, /* 3179 */
/* DIFF26 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7,13, 0, 0, 5, 5,11, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5,11,11, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF24 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5,10,11,11, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF26 DIFF24 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5,11,11, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF84 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5,10, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5,10, 4, 4,12,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 7,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5,10, 4, 4, 9, 9, 0, 0, 5,10, 4, 4, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF26 DIFF84 */
0, 0, 2, 2, 1, 1,13,13, 0, 0, 2, 2, 1, 1,13, 8,
0, 0, 2, 2, 1, 1,12, 6, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 6, 8,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
0, 0, 2, 2, 1, 1,13, 8, 0, 0, 2, 2, 1, 1, 8, 8,
0, 0, 2, 2, 1, 1,12, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 5, 5, 4, 4,13,13, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4,12,13, 0, 0, 5, 5, 4, 4,12,13,
0, 0, 2, 2, 1, 1, 7, 8, 0, 0, 2, 2, 1, 1, 8, 6,
0, 0, 2, 2, 1, 1, 8, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 7, 7, 0, 0, 5, 5, 4, 4,13,13,
0, 0, 5, 5, 4, 4, 9, 9, 0, 0, 5, 5, 4, 4, 9, 9,
/* DIFF24 DIFF84 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5,10, 4, 4, 3, 3, 0, 0, 5,10, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
/* DIFF26 DIFF24 DIFF84 */
0, 0, 2, 2, 1, 1, 6, 3, 0, 0, 2, 2, 1, 1, 3, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 2, 2, 1, 1, 6, 6, 0, 0, 2, 2, 1, 1, 6, 6,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3,
0, 0, 5, 5, 4, 4, 3, 3, 0, 0, 5, 5, 4, 4, 3, 3};
#define RB(y,u,v) (((y)&0x3FF)<<22)|(((u)&0x3FF)<<11)|((v)&0x3FF)
#define GO(y,v) ((((y)+64)&0x3FF)<<22)|(16<<11)|(((v)+16)&0x3FF)
static const uint32_t DiffTable[128] = {
RB(-85, 160, 80), RB(-83, 155, 77), RB(-80, 150, 75), RB(-77, 145, 72),
RB(-75, 140, 70), RB(-72, 135, 67), RB(-69, 130, 65), RB(-67, 125, 62),
RB(-64, 120, 60), RB(-61, 115, 57), RB(-59, 110, 55), RB(-56, 105, 52),
RB(-53, 100, 50), RB(-51, 95, 47), RB(-48, 90, 45), RB(-45, 85, 42),
RB(-43, 80, 40), RB(-40, 75, 37), RB(-37, 70, 35), RB(-35, 65, 32),
RB(-32, 60, 30), RB(-29, 55, 27), RB(-27, 50, 25), RB(-24, 45, 22),
RB(-21, 40, 20), RB(-19, 35, 17), RB(-16, 30, 15), RB(-13, 25, 12),
RB(-11, 20, 10), RB( -8, 15, 7), RB( -5, 10, 5), RB( -3, 5, 2),
RB( 0, 0, 0), RB( 2, -5, -2), RB( 5, -10, -5), RB( 7, -15, -7),
RB( 10, -20, -10), RB( 13, -25, -12), RB( 15, -30, -15), RB( 18, -35, -17),
RB( 21, -40, -20), RB( 23, -45, -22), RB( 26, -50, -25), RB( 29, -55, -27),
RB( 31, -60, -30), RB( 34, -65, -32), RB( 37, -70, -35), RB( 39, -75, -37),
RB( 42, -80, -40), RB( 45, -85, -42), RB( 47, -90, -45), RB( 50, -95, -47),
RB( 53,-100, -50), RB( 55,-105, -52), RB( 58,-110, -55), RB( 61,-115, -57),
RB( 63,-120, -60), RB( 66,-125, -62), RB( 69,-130, -65), RB( 71,-135, -67),
RB( 74,-140, -70), RB( 77,-145, -72), RB( 79,-150, -75), RB( 82,-155, -77),
GO(-84, -160), GO(-81, -155), GO(-78, -150), GO(-76, -145),
GO(-73, -140), GO(-70, -135), GO(-68, -130), GO(-65, -125),
GO(-63, -120), GO(-60, -115), GO(-57, -110), GO(-55, -105),
GO(-52, -100), GO(-49, -95), GO(-47, -90), GO(-44, -85),
GO(-42, -80), GO(-39, -75), GO(-36, -70), GO(-34, -65),
GO(-31, -60), GO(-28, -55), GO(-26, -50), GO(-23, -45),
GO(-21, -40), GO(-18, -35), GO(-15, -30), GO(-13, -25),
GO(-10, -20), GO( -7, -15), GO( -5, -10), GO( -2, -5),
GO( 0, 0), GO( 2, 4), GO( 5, 9), GO( 7, 14),
GO( 10, 19), GO( 13, 24), GO( 15, 29), GO( 18, 34),
GO( 21, 39), GO( 23, 44), GO( 26, 49), GO( 28, 54),
GO( 31, 59), GO( 34, 64), GO( 36, 69), GO( 39, 74),
GO( 42, 79), GO( 44, 84), GO( 47, 89), GO( 49, 94),
GO( 52, 99), GO( 55, 104), GO( 57, 109), GO( 60, 114),
GO( 63, 119), GO( 65, 124), GO( 68, 129), GO( 70, 134),
GO( 73, 139), GO( 76, 144), GO( 78, 149), GO( 81, 154)};
inline static int ExpandedDiff (uint32_t c0, uint32_t c1) {
uint32_t r, g;
g = c0;
g += 0x4008020;
g -= c1;
r = DiffTable[(int)((unsigned char)g) + 0];
r ^= (0x3FF << 11);
g >>= 10;
r += DiffTable[(int)((unsigned char)g) + 0];
g >>= 11;
r += DiffTable[(int)((unsigned char)g) + 64];
r &= 0xE01F03E0;
return r;
}
#define DIFF56 0x001
#define DIFF52 0x002
#define DIFF54 0x004
#define DIFF58 0x008
#define DIFF53 0x010
#define DIFF51 0x020
#define DIFF57 0x040
#define DIFF59 0x080
#define DIFF26 0x100
#define DIFF24 0x200
#define DIFF84 0x400
#define DIFF86 0x800
/* lastLineDiffs is previous line's 52, 53, 26, all >> 1 */
static uint8_t lastLineDiffs[__PAST_LIBRARY_WIDTH];
#define RotatePattern(x) ((((x) & 0x777) << 1) | (((x) & 0x888) >> 3))
void ProcessHQ2x(const pixel * restrict in, pixel * restrict out) {
signed int y, x;
unsigned int pattern, newpattern;
uint32_t pixels[9];
int prevline, nextline;
memset(lastLineDiffs, 0, sizeof(lastLineDiffs));
prevline = 1;
nextline = 1 + __PAST_LIBRARY_WIDTH;
y = __PAST_LIBRARY_HEIGHT - 1;
do {
pixels[1-1] =
pixels[2-1] = RGBUnpack(in[prevline-1]); /* Pixel 2 */
pixels[3-1] = RGBUnpack(in[prevline ]); /* Pixel 3 */
pixels[4-1] =
pixels[5-1] = RGBUnpack(in[ 0]); /* Pixel 5 */
pixels[6-1] = RGBUnpack(in[ 1]); /* Pixel 6 */
pixels[7-1] =
pixels[8-1] = RGBUnpack(in[nextline-1]); /* Pixel 8 */
pixels[9-1] = RGBUnpack(in[nextline ]); /* Pixel 9 */
pattern = 0;
x = __PAST_LIBRARY_WIDTH - 1;
do {
newpattern = 0;
if (pattern & DIFF26) newpattern |= DIFF51;
if (pattern & DIFF56) newpattern |= DIFF54;
if (pattern & DIFF86) newpattern |= DIFF57;
if (pattern & DIFF53) newpattern |= DIFF24;
if (pattern & DIFF59) newpattern |= DIFF84;
if (lastLineDiffs[x] & (DIFF52 >> 1)) newpattern |= DIFF58;
if (lastLineDiffs[x] & (DIFF26 >> 1)) newpattern |= DIFF59;
if (lastLineDiffs[x] & (DIFF53 >> 1)) newpattern |= DIFF86;
pattern = newpattern;
if (ExpandedDiff(pixels[5-1], pixels[2-1])) pattern |= DIFF52;
if (ExpandedDiff(pixels[5-1], pixels[3-1])) pattern |= DIFF53;
if (ExpandedDiff(pixels[5-1], pixels[6-1])) pattern |= DIFF56;
if (ExpandedDiff(pixels[2-1], pixels[6-1])) pattern |= DIFF26;
lastLineDiffs[x] = pattern >> 1;
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 0] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[2-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[4-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[1-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 1] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[6-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[2-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[3-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*2)+1] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[8-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[6-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[9-1] * blends_2x[(newpattern * 4) + 3])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*2) ] =
RGBPack(((pixels[5-1] * blends_2x[(newpattern * 4) + 0]) +
(pixels[4-1] * blends_2x[(newpattern * 4) + 1]) +
(pixels[8-1] * blends_2x[(newpattern * 4) + 2]) +
(pixels[7-1] * blends_2x[(newpattern * 4) + 3])) / 16);
out += 2;
in++;
x--;
if (x == 0) {
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
continue;
}
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[3-1] = RGBUnpack(in[prevline]); /* Pixel 3 */
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[6-1] = RGBUnpack(in[1 ]); /* Pixel 6 */
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
pixels[9-1] = RGBUnpack(in[nextline]); /* Pixel 9 */
} while (x >= 0);
prevline = 1 - __PAST_LIBRARY_WIDTH;
out += (__PAST_LIBRARY_WIDTH * 2);
y--;
if (y > 0) {
continue;
}
nextline = 0;
} while (y >= 0);
}
void ProcessHQ4x(const pixel * restrict in, pixel * restrict out) {
signed int y, x;
unsigned int pattern, newpattern;
uint32_t pixels[9];
int prevline, nextline;
memset(lastLineDiffs, 0, sizeof(lastLineDiffs));
prevline = 1;
nextline = 1 + __PAST_LIBRARY_WIDTH;
y = __PAST_LIBRARY_HEIGHT - 1;
do {
pixels[1-1] =
pixels[2-1] = RGBUnpack(in[prevline-1]); /* Pixel 2 */
pixels[3-1] = RGBUnpack(in[prevline ]); /* Pixel 3 */
pixels[4-1] =
pixels[5-1] = RGBUnpack(in[ 0]); /* Pixel 5 */
pixels[6-1] = RGBUnpack(in[ 1]); /* Pixel 6 */
pixels[7-1] =
pixels[8-1] = RGBUnpack(in[nextline-1]); /* Pixel 8 */
pixels[9-1] = RGBUnpack(in[nextline ]); /* Pixel 9 */
pattern = 0;
x = __PAST_LIBRARY_WIDTH - 1;
do {
newpattern = 0;
if (pattern & DIFF26) newpattern |= DIFF51;
if (pattern & DIFF56) newpattern |= DIFF54;
if (pattern & DIFF86) newpattern |= DIFF57;
if (pattern & DIFF53) newpattern |= DIFF24;
if (pattern & DIFF59) newpattern |= DIFF84;
if (lastLineDiffs[x] & (DIFF52 >> 1)) newpattern |= DIFF58;
if (lastLineDiffs[x] & (DIFF26 >> 1)) newpattern |= DIFF59;
if (lastLineDiffs[x] & (DIFF53 >> 1)) newpattern |= DIFF86;
pattern = newpattern;
if (ExpandedDiff(pixels[5-1], pixels[2-1])) pattern |= DIFF52;
if (ExpandedDiff(pixels[5-1], pixels[3-1])) pattern |= DIFF53;
if (ExpandedDiff(pixels[5-1], pixels[6-1])) pattern |= DIFF56;
if (ExpandedDiff(pixels[2-1], pixels[6-1])) pattern |= DIFF26;
lastLineDiffs[x] = pattern >> 1;
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 0] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[ 1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[1-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[ 3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[ 2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x4)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[2-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[3-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*0xC)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0xC)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+3] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+2] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[6-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[9-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
newpattern = tree_hq[pattern & 0x7FF];
pattern = RotatePattern(pattern);
out[(__PAST_LIBRARY_WIDTH*0xC) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x0]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x0]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x0]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x0])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8) ] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x4]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x4]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x4]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x4])) / 16);
out[(__PAST_LIBRARY_WIDTH*0xC)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0x8]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0x8]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0x8]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0x8])) / 16);
out[(__PAST_LIBRARY_WIDTH*0x8)+1] =
RGBPack(((pixels[5-1] * blends_4x[(newpattern * 16) + 0 + 0xC]) +
(pixels[4-1] * blends_4x[(newpattern * 16) + 1 + 0xC]) +
(pixels[8-1] * blends_4x[(newpattern * 16) + 2 + 0xC]) +
(pixels[7-1] * blends_4x[(newpattern * 16) + 3 + 0xC])) / 16);
out += 4;
in++;
x--;
if (x == 0) {
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
continue;
}
pixels[1-1] = pixels[2-1];
pixels[2-1] = pixels[3-1];
pixels[3-1] = RGBUnpack(in[prevline]); /* Pixel 3 */
pixels[4-1] = pixels[5-1];
pixels[5-1] = pixels[6-1];
pixels[6-1] = RGBUnpack(in[1 ]); /* Pixel 6 */
pixels[7-1] = pixels[8-1];
pixels[8-1] = pixels[9-1];
pixels[9-1] = RGBUnpack(in[nextline]); /* Pixel 9 */
} while (x >= 0);
prevline = 1 - __PAST_LIBRARY_WIDTH;
out += (__PAST_LIBRARY_WIDTH * 0xC);
y--;
if (y > 0) {
continue;
}
nextline = 0;
} while (y >= 0);
}
-19
View File
@@ -1,19 +0,0 @@
#include "ntsc.h"
#include <stdlib.h>
#include <stdbool.h>
void ntsc_filter(uint16_t * restrict out, const uint16_t * restrict in, int width, int height)
{
static int phase = 0;
static snes_ntsc_t ntsc;
static bool inited = false;
if (inited == false)
{
snes_ntsc_init(&ntsc, &snes_ntsc_composite);
inited = true;
}
snes_ntsc_blit(&ntsc, in, width, phase, width, height, out, SNES_NTSC_OUT_WIDTH(width) * sizeof(uint16_t));
phase ^= 1;
}
-9
View File
@@ -1,9 +0,0 @@
#ifndef _NTSC_FILTER_H
#define _NTSC_FILTER_H
#include <stdint.h>
#include "snes_ntsc/snes_ntsc.h"
void ntsc_filter(uint16_t * restrict out, const uint16_t * restrict in, int width, int height);
#endif
-54
View File
@@ -1,54 +0,0 @@
/* This source code is Copyright (C) 2006-2009 by WolfWings. */
/* It is, however, released under the ISC license. */
/* en.wikipedia.org/wiki/ISC_Licence */
/* --------------------------------------------------------- */
/* It is a formula-level rederiviation of the HQ2x technique */
/* and is, thus, not a derivative work of the original code, */
/* only the original equations behind the code. */
#ifndef __PAST_LIBRARY_H
#define __PAST_LIBRARY_H
#include <stdint.h>
#ifndef __PAST_LIBRARY_WIDTH
#define __PAST_LIBRARY_WIDTH 256
#endif
#ifndef __PAST_LIBRARY_HEIGHT
#define __PAST_LIBRARY_HEIGHT 224
#endif
typedef uint16_t pixel;
/* These two functions are what need to be modified to interface other
* pixel formats with the library. They need to return values inside a
* format like this: (Red/Blue can be reversed.)
*
* 0000-00GG-GGG0-0000-0RRR-RR00-000B-BBBB
*
* This is VERY fast to create from 15-bit (NOT 16-bit) RGB/BGR, as
* below.
*
* If you can't return data in this format, you'll have to rebuild the
* ExpandedDiff function and DiffTable array to compensate, and possibly
* change the blending functions in the Process* functions to handle any
* guard-bits and masking.
*/
static inline uint32_t RGBUnpack(pixel i) {
uint32_t o = i;
o = (o * 0x10001);
o = o & 0x03E07C1F;
return o;
}
static inline pixel RGBPack(uint32_t x) {
x &= 0x03E07C1F;
x |= (x >> 16);
return x;
}
void ProcessHQ2x(const pixel * restrict inbuffer, pixel * restrict outbuffer);
void ProcessHQ4x(const pixel * restrict inbuffer, pixel * restrict outbuffer);
#endif /* __PAST_LIBRARY_H */
-251
View File
@@ -1,251 +0,0 @@
/* snes_ntsc 0.2.2. http://www.slack.net/~ant/ */
#include "snes_ntsc.h"
/* Copyright (C) 2006-2007 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module 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 Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
snes_ntsc_setup_t const snes_ntsc_monochrome = { 0,-1, 0, 0,.2, 0,.2,-.2,-.2,-1, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_composite = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_svideo = { 0, 0, 0, 0,.2, 0,.2, -1, -1, 0, 1, 0, 0 };
snes_ntsc_setup_t const snes_ntsc_rgb = { 0, 0, 0, 0,.2, 0,.7, -1, -1,-1, 1, 0, 0 };
#define alignment_count 3
#define burst_count 3
#define rescale_in 8
#define rescale_out 7
#define artifacts_mid 1.0f
#define fringing_mid 1.0f
#define std_decoder_hue 0
#define rgb_bits 7 /* half normal range to allow for doubled hires pixels */
#define gamma_size 32
#include "snes_ntsc_impl.h"
/* 3 input pixels -> 8 composite samples */
pixel_info_t const snes_ntsc_pixels [alignment_count] = {
{ PIXEL_OFFSET( -4, -9 ), { 1, 1, .6667f, 0 } },
{ PIXEL_OFFSET( -2, -7 ), { .3333f, 1, 1, .3333f } },
{ PIXEL_OFFSET( 0, -5 ), { 0, .6667f, 1, 1 } },
};
static void merge_kernel_fields( snes_ntsc_rgb_t* io )
{
int n;
for ( n = burst_size; n; --n )
{
snes_ntsc_rgb_t p0 = io [burst_size * 0] + rgb_bias;
snes_ntsc_rgb_t p1 = io [burst_size * 1] + rgb_bias;
snes_ntsc_rgb_t p2 = io [burst_size * 2] + rgb_bias;
/* merge colors without losing precision */
io [burst_size * 0] =
((p0 + p1 - ((p0 ^ p1) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
io [burst_size * 1] =
((p1 + p2 - ((p1 ^ p2) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
io [burst_size * 2] =
((p2 + p0 - ((p2 ^ p0) & snes_ntsc_rgb_builder)) >> 1) - rgb_bias;
++io;
}
}
static void correct_errors( snes_ntsc_rgb_t color, snes_ntsc_rgb_t* out )
{
int n;
for ( n = burst_count; n; --n )
{
unsigned i;
for ( i = 0; i < rgb_kernel_size / 2; i++ )
{
snes_ntsc_rgb_t error = color -
out [i ] - out [(i+12)%14+14] - out [(i+10)%14+28] -
out [i + 7] - out [i + 5 +14] - out [i + 3 +28];
DISTRIBUTE_ERROR( i+3+28, i+5+14, i+7 );
}
out += alignment_count * rgb_kernel_size;
}
}
void snes_ntsc_init( snes_ntsc_t* ntsc, snes_ntsc_setup_t const* setup )
{
int merge_fields;
int entry;
init_t impl;
if ( !setup )
setup = &snes_ntsc_composite;
init( &impl, setup );
merge_fields = setup->merge_fields;
if ( setup->artifacts <= -1 && setup->fringing <= -1 )
merge_fields = 1;
for ( entry = 0; entry < snes_ntsc_palette_size; entry++ )
{
/* Reduce number of significant bits of source color. Clearing the
low bits of R and B were least notictable. Modifying green was too
noticeable. */
int ir = entry >> 8 & 0x1E;
int ig = entry >> 4 & 0x1F;
int ib = entry << 1 & 0x1E;
#if SNES_NTSC_BSNES_COLORTBL
if ( setup->bsnes_colortbl )
{
int bgr15 = (ib << 10) | (ig << 5) | ir;
unsigned long rgb16 = setup->bsnes_colortbl [bgr15];
ir = rgb16 >> 11 & 0x1E;
ig = rgb16 >> 6 & 0x1F;
ib = rgb16 & 0x1E;
}
#endif
{
float rr = impl.to_float [ir];
float gg = impl.to_float [ig];
float bb = impl.to_float [ib];
float y, i, q = RGB_TO_YIQ( rr, gg, bb, y, i );
int r, g, b = YIQ_TO_RGB( y, i, q, impl.to_rgb, int, r, g );
snes_ntsc_rgb_t rgb = PACK_RGB( r, g, b );
snes_ntsc_rgb_t* out = ntsc->table [entry];
gen_kernel( &impl, y, i, q, out );
if ( merge_fields )
merge_kernel_fields( out );
correct_errors( rgb, out );
}
}
}
#ifndef SNES_NTSC_NO_BLITTERS
void snes_ntsc_blit( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input, long in_row_width,
int burst_phase, int in_width, int in_height, void* rgb_out, long out_pitch )
{
int chunk_count = (in_width - 1) / snes_ntsc_in_chunk;
for ( ; in_height; --in_height )
{
SNES_NTSC_IN_T const* line_in = input;
SNES_NTSC_BEGIN_ROW( ntsc, burst_phase,
snes_ntsc_black, snes_ntsc_black, SNES_NTSC_ADJ_IN( *line_in ) );
snes_ntsc_out_t* restrict line_out = (snes_ntsc_out_t*) rgb_out;
int n;
++line_in;
for ( n = chunk_count; n; --n )
{
/* order of input and output pixels must not be altered */
SNES_NTSC_COLOR_IN( 0, SNES_NTSC_ADJ_IN( line_in [0] ) );
SNES_NTSC_RGB_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, SNES_NTSC_ADJ_IN( line_in [1] ) );
SNES_NTSC_RGB_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, SNES_NTSC_ADJ_IN( line_in [2] ) );
SNES_NTSC_RGB_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
line_in += 3;
line_out += 7;
}
/* finish final pixels */
SNES_NTSC_COLOR_IN( 0, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, snes_ntsc_black );
SNES_NTSC_RGB_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_RGB_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
burst_phase = (burst_phase + 1) % snes_ntsc_burst_count;
input += in_row_width;
rgb_out = (char*) rgb_out + out_pitch;
}
}
void snes_ntsc_blit_hires( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input, long in_row_width,
int burst_phase, int in_width, int in_height, void* rgb_out, long out_pitch )
{
int chunk_count = (in_width - 2) / (snes_ntsc_in_chunk * 2);
for ( ; in_height; --in_height )
{
SNES_NTSC_IN_T const* line_in = input;
SNES_NTSC_HIRES_ROW( ntsc, burst_phase,
snes_ntsc_black, snes_ntsc_black, snes_ntsc_black,
SNES_NTSC_ADJ_IN( line_in [0] ),
SNES_NTSC_ADJ_IN( line_in [1] ) );
snes_ntsc_out_t* restrict line_out = (snes_ntsc_out_t*) rgb_out;
int n;
line_in += 2;
for ( n = chunk_count; n; --n )
{
/* twice as many input pixels per chunk */
SNES_NTSC_COLOR_IN( 0, SNES_NTSC_ADJ_IN( line_in [0] ) );
SNES_NTSC_HIRES_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, SNES_NTSC_ADJ_IN( line_in [1] ) );
SNES_NTSC_HIRES_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, SNES_NTSC_ADJ_IN( line_in [2] ) );
SNES_NTSC_HIRES_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 3, SNES_NTSC_ADJ_IN( line_in [3] ) );
SNES_NTSC_HIRES_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 4, SNES_NTSC_ADJ_IN( line_in [4] ) );
SNES_NTSC_HIRES_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 5, SNES_NTSC_ADJ_IN( line_in [5] ) );
SNES_NTSC_HIRES_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_HIRES_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
line_in += 6;
line_out += 7;
}
SNES_NTSC_COLOR_IN( 0, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 0, line_out [0], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 1, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 1, line_out [1], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 2, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 2, line_out [2], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 3, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 3, line_out [3], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 4, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 4, line_out [4], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_COLOR_IN( 5, snes_ntsc_black );
SNES_NTSC_HIRES_OUT( 5, line_out [5], SNES_NTSC_OUT_DEPTH );
SNES_NTSC_HIRES_OUT( 6, line_out [6], SNES_NTSC_OUT_DEPTH );
burst_phase = (burst_phase + 1) % snes_ntsc_burst_count;
input += in_row_width;
rgb_out = (char*) rgb_out + out_pitch;
}
}
#endif
-236
View File
@@ -1,236 +0,0 @@
/* SNES NTSC video filter */
/* snes_ntsc 0.2.2 */
#ifndef SNES_NTSC_H
#define SNES_NTSC_H
#include "snes_ntsc_config.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Image parameters, ranging from -1.0 to 1.0. Actual internal values shown
in parenthesis and should remain fairly stable in future versions. */
typedef struct snes_ntsc_setup_t
{
/* Basic parameters */
double hue; /* -1 = -180 degrees +1 = +180 degrees */
double saturation; /* -1 = grayscale (0.0) +1 = oversaturated colors (2.0) */
double contrast; /* -1 = dark (0.5) +1 = light (1.5) */
double brightness; /* -1 = dark (0.5) +1 = light (1.5) */
double sharpness; /* edge contrast enhancement/blurring */
/* Advanced parameters */
double gamma; /* -1 = dark (1.5) +1 = light (0.5) */
double resolution; /* image resolution */
double artifacts; /* artifacts caused by color changes */
double fringing; /* color artifacts caused by brightness changes */
double bleed; /* color bleed (color resolution reduction) */
int merge_fields; /* if 1, merges even and odd fields together to reduce flicker */
float const* decoder_matrix; /* optional RGB decoder matrix, 6 elements */
unsigned long const* bsnes_colortbl; /* undocumented; set to 0 */
} snes_ntsc_setup_t;
/* Video format presets */
extern snes_ntsc_setup_t const snes_ntsc_composite; /* color bleeding + artifacts */
extern snes_ntsc_setup_t const snes_ntsc_svideo; /* color bleeding only */
extern snes_ntsc_setup_t const snes_ntsc_rgb; /* crisp image */
extern snes_ntsc_setup_t const snes_ntsc_monochrome;/* desaturated + artifacts */
/* Initializes and adjusts parameters. Can be called multiple times on the same
snes_ntsc_t object. Can pass NULL for either parameter. */
typedef struct snes_ntsc_t snes_ntsc_t;
void snes_ntsc_init( snes_ntsc_t* ntsc, snes_ntsc_setup_t const* setup );
/* Filters one or more rows of pixels. Input pixel format is set by SNES_NTSC_IN_FORMAT
and output RGB depth is set by SNES_NTSC_OUT_DEPTH. Both default to 16-bit RGB.
In_row_width is the number of pixels to get to the next input row. Out_pitch
is the number of *bytes* to get to the next output row. */
void snes_ntsc_blit( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input,
long in_row_width, int burst_phase, int in_width, int in_height,
void* rgb_out, long out_pitch );
void snes_ntsc_blit_hires( snes_ntsc_t const* ntsc, SNES_NTSC_IN_T const* input,
long in_row_width, int burst_phase, int in_width, int in_height,
void* rgb_out, long out_pitch );
/* Number of output pixels written by low-res blitter for given input width. Width
might be rounded down slightly; use SNES_NTSC_IN_WIDTH() on result to find rounded
value. Guaranteed not to round 256 down at all. */
#define SNES_NTSC_OUT_WIDTH( in_width ) \
((((in_width) - 1) / snes_ntsc_in_chunk + 1) * snes_ntsc_out_chunk)
/* Number of low-res input pixels that will fit within given output width. Might be
rounded down slightly; use SNES_NTSC_OUT_WIDTH() on result to find rounded
value. */
#define SNES_NTSC_IN_WIDTH( out_width ) \
(((out_width) / snes_ntsc_out_chunk - 1) * snes_ntsc_in_chunk + 1)
/* Interface for user-defined custom blitters */
enum { snes_ntsc_in_chunk = 3 }; /* number of input pixels read per chunk */
enum { snes_ntsc_out_chunk = 7 }; /* number of output pixels generated per chunk */
enum { snes_ntsc_black = 0 }; /* palette index for black */
enum { snes_ntsc_burst_count = 3 }; /* burst phase cycles through 0, 1, and 2 */
/* Begins outputting row and starts three pixels. First pixel will be cut off a bit.
Use snes_ntsc_black for unused pixels. Declares variables, so must be before first
statement in a block (unless you're using C++). */
#define SNES_NTSC_BEGIN_ROW( ntsc, burst, pixel0, pixel1, pixel2 ) \
char const* ktable = \
(char const*) (ntsc)->table + burst * (snes_ntsc_burst_size * sizeof (snes_ntsc_rgb_t));\
SNES_NTSC_BEGIN_ROW_6_( pixel0, pixel1, pixel2, SNES_NTSC_IN_FORMAT, ktable )
/* Begins input pixel */
#define SNES_NTSC_COLOR_IN( index, color ) \
SNES_NTSC_COLOR_IN_( index, color, SNES_NTSC_IN_FORMAT, ktable )
/* Generates output pixel. Bits can be 24, 16, 15, 14, 32 (treated as 24), or 0:
24: RRRRRRRR GGGGGGGG BBBBBBBB (8-8-8 RGB)
16: RRRRRGGG GGGBBBBB (5-6-5 RGB)
15: RRRRRGG GGGBBBBB (5-5-5 RGB)
14: BBBBBGG GGGRRRRR (5-5-5 BGR, native SNES format)
0: xxxRRRRR RRRxxGGG GGGGGxxB BBBBBBBx (native internal format; x = junk bits) */
#define SNES_NTSC_RGB_OUT( index, rgb_out, bits ) \
SNES_NTSC_RGB_OUT_14_( index, rgb_out, bits, 1 )
/* Hires equivalents */
#define SNES_NTSC_HIRES_ROW( ntsc, burst, pixel1, pixel2, pixel3, pixel4, pixel5 ) \
char const* ktable = \
(char const*) (ntsc)->table + burst * (snes_ntsc_burst_size * sizeof (snes_ntsc_rgb_t));\
unsigned const snes_ntsc_pixel1_ = (pixel1);\
snes_ntsc_rgb_t const* kernel1 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel1_ );\
unsigned const snes_ntsc_pixel2_ = (pixel2);\
snes_ntsc_rgb_t const* kernel2 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel2_ );\
unsigned const snes_ntsc_pixel3_ = (pixel3);\
snes_ntsc_rgb_t const* kernel3 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel3_ );\
unsigned const snes_ntsc_pixel4_ = (pixel4);\
snes_ntsc_rgb_t const* kernel4 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel4_ );\
unsigned const snes_ntsc_pixel5_ = (pixel5);\
snes_ntsc_rgb_t const* kernel5 = SNES_NTSC_IN_FORMAT( ktable, snes_ntsc_pixel5_ );\
snes_ntsc_rgb_t const* kernel0 = kernel1;\
snes_ntsc_rgb_t const* kernelx0;\
snes_ntsc_rgb_t const* kernelx1 = kernel1;\
snes_ntsc_rgb_t const* kernelx2 = kernel1;\
snes_ntsc_rgb_t const* kernelx3 = kernel1;\
snes_ntsc_rgb_t const* kernelx4 = kernel1;\
snes_ntsc_rgb_t const* kernelx5 = kernel1
#define SNES_NTSC_HIRES_OUT( x, rgb_out, bits ) {\
snes_ntsc_rgb_t raw_ =\
kernel0 [ x ] + kernel2 [(x+5)%7+14] + kernel4 [(x+3)%7+28] +\
kernelx0 [(x+7)%7+7] + kernelx2 [(x+5)%7+21] + kernelx4 [(x+3)%7+35] +\
kernel1 [(x+6)%7 ] + kernel3 [(x+4)%7+14] + kernel5 [(x+2)%7+28] +\
kernelx1 [(x+6)%7+7] + kernelx3 [(x+4)%7+21] + kernelx5 [(x+2)%7+35];\
SNES_NTSC_CLAMP_( raw_, 0 );\
SNES_NTSC_RGB_OUT_( rgb_out, (bits), 0 );\
}
/* private */
enum { snes_ntsc_entry_size = 128 };
enum { snes_ntsc_palette_size = 0x2000 };
typedef unsigned long snes_ntsc_rgb_t;
struct snes_ntsc_t {
snes_ntsc_rgb_t table [snes_ntsc_palette_size] [snes_ntsc_entry_size];
};
enum { snes_ntsc_burst_size = snes_ntsc_entry_size / snes_ntsc_burst_count };
#define SNES_NTSC_RGB16( ktable, n ) \
(snes_ntsc_rgb_t const*) (ktable + ((n & 0x001E) | (n >> 1 & 0x03E0) | (n >> 2 & 0x3C00)) * \
(snes_ntsc_entry_size / 2 * sizeof (snes_ntsc_rgb_t)))
#define SNES_NTSC_BGR15( ktable, n ) \
(snes_ntsc_rgb_t const*) (ktable + ((n << 9 & 0x3C00) | (n & 0x03E0) | (n >> 10 & 0x001E)) * \
(snes_ntsc_entry_size / 2 * sizeof (snes_ntsc_rgb_t)))
/* common 3->7 ntsc macros */
#define SNES_NTSC_BEGIN_ROW_6_( pixel0, pixel1, pixel2, ENTRY, table ) \
unsigned const snes_ntsc_pixel0_ = (pixel0);\
snes_ntsc_rgb_t const* kernel0 = ENTRY( table, snes_ntsc_pixel0_ );\
unsigned const snes_ntsc_pixel1_ = (pixel1);\
snes_ntsc_rgb_t const* kernel1 = ENTRY( table, snes_ntsc_pixel1_ );\
unsigned const snes_ntsc_pixel2_ = (pixel2);\
snes_ntsc_rgb_t const* kernel2 = ENTRY( table, snes_ntsc_pixel2_ );\
snes_ntsc_rgb_t const* kernelx0;\
snes_ntsc_rgb_t const* kernelx1 = kernel0;\
snes_ntsc_rgb_t const* kernelx2 = kernel0
#define SNES_NTSC_RGB_OUT_14_( x, rgb_out, bits, shift ) {\
snes_ntsc_rgb_t raw_ =\
kernel0 [x ] + kernel1 [(x+12)%7+14] + kernel2 [(x+10)%7+28] +\
kernelx0 [(x+7)%14] + kernelx1 [(x+ 5)%7+21] + kernelx2 [(x+ 3)%7+35];\
SNES_NTSC_CLAMP_( raw_, shift );\
SNES_NTSC_RGB_OUT_( rgb_out, bits, shift );\
}
/* common ntsc macros */
#define snes_ntsc_rgb_builder ((1L << 21) | (1 << 11) | (1 << 1))
#define snes_ntsc_clamp_mask (snes_ntsc_rgb_builder * 3 / 2)
#define snes_ntsc_clamp_add (snes_ntsc_rgb_builder * 0x101)
#define SNES_NTSC_CLAMP_( io, shift ) {\
snes_ntsc_rgb_t sub = (io) >> (9-(shift)) & snes_ntsc_clamp_mask;\
snes_ntsc_rgb_t clamp = snes_ntsc_clamp_add - sub;\
io |= clamp;\
clamp -= sub;\
io &= clamp;\
}
#define SNES_NTSC_COLOR_IN_( index, color, ENTRY, table ) {\
unsigned color_;\
kernelx##index = kernel##index;\
kernel##index = (color_ = (color), ENTRY( table, color_ ));\
}
/* x is always zero except in snes_ntsc library */
/* original routine */
#if 0
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
if ( bits == 16 )\
rgb_out = (raw_>>(13-x)& 0xF800)|(raw_>>(8-x)&0x07E0)|(raw_>>(4-x)&0x001F);\
if ( bits == 24 || bits == 32 )\
rgb_out = (raw_>>(5-x)&0xFF0000)|(raw_>>(3-x)&0xFF00)|(raw_>>(1-x)&0xFF);\
if ( bits == 15 )\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
if ( bits == 14 )\
rgb_out = (raw_>>(24-x)& 0x001F)|(raw_>>(9-x)&0x03E0)|(raw_<<(6+x)&0x7C00);\
if ( bits == 0 )\
rgb_out = raw_ << x;\
}
#endif
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
}
// Custom SSNES routine for 15 bit.
/* custom bsnes routine -- hooks into bsnes colortable */
#if 0
#define SNES_NTSC_RGB_OUT_( rgb_out, bits, x ) {\
if ( bits == 16 ) {\
rgb_out = (raw_>>(13-x)& 0xF800)|(raw_>>(8-x)&0x07E0)|(raw_>>(4-x)&0x001F);\
rgb_out = ((rgb_out&0xf800)>>11)|((rgb_out&0x07c0)>>1)|((rgb_out&0x001f)<<10);\
rgb_out = colortable[rgb_out];\
} else if ( bits == 24 || bits == 32 ) {\
rgb_out = (raw_>>(5-x)&0xFF0000)|(raw_>>(3-x)&0xFF00)|(raw_>>(1-x)&0xFF);\
rgb_out = ((rgb_out&0xf80000)>>19)|((rgb_out&0x00f800)>>6)|((rgb_out&0x0000f8)<<7);\
rgb_out = colortable[rgb_out];\
} else if ( bits == 15 ) {\
rgb_out = (raw_>>(14-x)& 0x7C00)|(raw_>>(9-x)&0x03E0)|(raw_>>(4-x)&0x001F);\
rgb_out = ((rgb_out&0x7c00)>>10)|((rgb_out&0x03e0))|((rgb_out&0x001f)<<10);\
rgb_out = colortable[rgb_out];\
} else {\
rgb_out = raw_ << x;\
}\
}
#endif
#ifdef __cplusplus
}
#endif
#endif
-27
View File
@@ -1,27 +0,0 @@
/* Configure library by modifying this file */
#ifndef SNES_NTSC_CONFIG_H
#define SNES_NTSC_CONFIG_H
/* Format of source pixels */
/* #define SNES_NTSC_IN_FORMAT SNES_NTSC_RGB16 */
#define SNES_NTSC_IN_FORMAT SNES_NTSC_BGR15
/* The following affect the built-in blitter only; a custom blitter can
handle things however it wants. */
/* Bits per pixel of output. Can be 15, 16, 32, or 24 (same as 32). */
#define SNES_NTSC_OUT_DEPTH 15
/* Type of input pixel values */
#define SNES_NTSC_IN_T unsigned short
/* Each raw pixel input value is passed through this. You might want to mask
the pixel index if you use the high bits as flags, etc. */
#define SNES_NTSC_ADJ_IN( in ) (((in >> 10) & 0x1F) | (in & 0x3E0) | ((in << 10) & 0x7C00))
/* For each pixel, this is the basic operation:
output_color = SNES_NTSC_ADJ_IN( SNES_NTSC_IN_T ) */
#endif
-439
View File
@@ -1,439 +0,0 @@
/* snes_ntsc 0.2.2. http://www.slack.net/~ant/ */
/* Common implementation of NTSC filters */
#include <assert.h>
#include <math.h>
/* Copyright (C) 2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module 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 Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#define DISABLE_CORRECTION 0
#undef PI
#define PI 3.14159265358979323846f
#ifndef LUMA_CUTOFF
#define LUMA_CUTOFF 0.20
#endif
#ifndef gamma_size
#define gamma_size 1
#endif
#ifndef rgb_bits
#define rgb_bits 8
#endif
#ifndef artifacts_max
#define artifacts_max (artifacts_mid * 1.5f)
#endif
#ifndef fringing_max
#define fringing_max (fringing_mid * 2)
#endif
#ifndef STD_HUE_CONDITION
#define STD_HUE_CONDITION( setup ) 1
#endif
#define ext_decoder_hue (std_decoder_hue + 15)
#define rgb_unit (1 << rgb_bits)
#define rgb_offset (rgb_unit * 2 + 0.5f)
enum { burst_size = snes_ntsc_entry_size / burst_count };
enum { kernel_half = 16 };
enum { kernel_size = kernel_half * 2 + 1 };
typedef struct init_t
{
float to_rgb [burst_count * 6];
float to_float [gamma_size];
float contrast;
float brightness;
float artifacts;
float fringing;
float kernel [rescale_out * kernel_size * 2];
} init_t;
#define ROTATE_IQ( i, q, sin_b, cos_b ) {\
float t;\
t = i * cos_b - q * sin_b;\
q = i * sin_b + q * cos_b;\
i = t;\
}
static void init_filters( init_t* impl, snes_ntsc_setup_t const* setup )
{
#if rescale_out > 1
float kernels [kernel_size * 2];
#else
float* const kernels = impl->kernel;
#endif
/* generate luma (y) filter using sinc kernel */
{
/* sinc with rolloff (dsf) */
float const rolloff = 1 + (float) setup->sharpness * (float) 0.032;
float const maxh = 32;
float const pow_a_n = (float) pow( rolloff, maxh );
float sum;
int i;
/* quadratic mapping to reduce negative (blurring) range */
float to_angle = (float) setup->resolution + 1;
to_angle = PI / maxh * (float) LUMA_CUTOFF * (to_angle * to_angle + 1);
kernels [kernel_size * 3 / 2] = maxh; /* default center value */
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
int x = i - kernel_half;
float angle = x * to_angle;
/* instability occurs at center point with rolloff very close to 1.0 */
if ( x || pow_a_n > (float) 1.056 || pow_a_n < (float) 0.981 )
{
float rolloff_cos_a = rolloff * (float) cos( angle );
float num = 1 - rolloff_cos_a -
pow_a_n * (float) cos( maxh * angle ) +
pow_a_n * rolloff * (float) cos( (maxh - 1) * angle );
float den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff;
float dsf = num / den;
kernels [kernel_size * 3 / 2 - kernel_half + i] = dsf - (float) 0.5;
}
}
/* apply blackman window and find sum */
sum = 0;
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
float x = PI * 2 / (kernel_half * 2) * i;
float blackman = 0.42f - 0.5f * (float) cos( x ) + 0.08f * (float) cos( x * 2 );
sum += (kernels [kernel_size * 3 / 2 - kernel_half + i] *= blackman);
}
/* normalize kernel */
sum = 1.0f / sum;
for ( i = 0; i < kernel_half * 2 + 1; i++ )
{
int x = kernel_size * 3 / 2 - kernel_half + i;
kernels [x] *= sum;
assert( kernels [x] == kernels [x] ); /* catch numerical instability */
}
}
/* generate chroma (iq) filter using gaussian kernel */
{
float const cutoff_factor = -0.03125f;
float cutoff = (float) setup->bleed;
int i;
if ( cutoff < 0 )
{
/* keep extreme value accessible only near upper end of scale (1.0) */
cutoff *= cutoff;
cutoff *= cutoff;
cutoff *= cutoff;
cutoff *= -30.0f / 0.65f;
}
cutoff = cutoff_factor - 0.65f * cutoff_factor * cutoff;
for ( i = -kernel_half; i <= kernel_half; i++ )
kernels [kernel_size / 2 + i] = (float) exp( i * i * cutoff );
/* normalize even and odd phases separately */
for ( i = 0; i < 2; i++ )
{
float sum = 0;
int x;
for ( x = i; x < kernel_size; x += 2 )
sum += kernels [x];
sum = 1.0f / sum;
for ( x = i; x < kernel_size; x += 2 )
{
kernels [x] *= sum;
assert( kernels [x] == kernels [x] ); /* catch numerical instability */
}
}
}
/*
printf( "luma:\n" );
for ( i = kernel_size; i < kernel_size * 2; i++ )
printf( "%f\n", kernels [i] );
printf( "chroma:\n" );
for ( i = 0; i < kernel_size; i++ )
printf( "%f\n", kernels [i] );
*/
/* generate linear rescale kernels */
#if rescale_out > 1
{
float weight = 1.0f;
float* out = impl->kernel;
int n = rescale_out;
do
{
float remain = 0;
int i;
weight -= 1.0f / rescale_in;
for ( i = 0; i < kernel_size * 2; i++ )
{
float cur = kernels [i];
float m = cur * weight;
*out++ = m + remain;
remain = cur - m;
}
}
while ( --n );
}
#endif
}
static float const default_decoder [6] =
{ 0.956f, 0.621f, -0.272f, -0.647f, -1.105f, 1.702f };
static void init( init_t* impl, snes_ntsc_setup_t const* setup )
{
impl->brightness = (float) setup->brightness * (0.5f * rgb_unit) + rgb_offset;
impl->contrast = (float) setup->contrast * (0.5f * rgb_unit) + rgb_unit;
#ifdef default_palette_contrast
if ( !setup->palette )
impl->contrast *= default_palette_contrast;
#endif
impl->artifacts = (float) setup->artifacts;
if ( impl->artifacts > 0 )
impl->artifacts *= artifacts_max - artifacts_mid;
impl->artifacts = impl->artifacts * artifacts_mid + artifacts_mid;
impl->fringing = (float) setup->fringing;
if ( impl->fringing > 0 )
impl->fringing *= fringing_max - fringing_mid;
impl->fringing = impl->fringing * fringing_mid + fringing_mid;
init_filters( impl, setup );
/* generate gamma table */
if ( gamma_size > 1 )
{
float const to_float = 1.0f / (gamma_size - (gamma_size > 1));
float const gamma = 1.1333f - (float) setup->gamma * 0.5f;
/* match common PC's 2.2 gamma to TV's 2.65 gamma */
int i;
for ( i = 0; i < gamma_size; i++ )
impl->to_float [i] =
(float) pow( i * to_float, gamma ) * impl->contrast + impl->brightness;
}
/* setup decoder matricies */
{
float hue = (float) setup->hue * PI + PI / 180 * ext_decoder_hue;
float sat = (float) setup->saturation + 1;
float const* decoder = setup->decoder_matrix;
if ( !decoder )
{
decoder = default_decoder;
if ( STD_HUE_CONDITION( setup ) )
hue += PI / 180 * (std_decoder_hue - ext_decoder_hue);
}
{
float s = (float) sin( hue ) * sat;
float c = (float) cos( hue ) * sat;
float* out = impl->to_rgb;
int n;
n = burst_count;
do
{
float const* in = decoder;
int n = 3;
do
{
float i = *in++;
float q = *in++;
*out++ = i * c - q * s;
*out++ = i * s + q * c;
}
while ( --n );
if ( burst_count <= 1 )
break;
ROTATE_IQ( s, c, 0.866025f, -0.5f ); /* +120 degrees */
}
while ( --n );
}
}
}
/* kernel generation */
#define RGB_TO_YIQ( r, g, b, y, i ) (\
(y = (r) * 0.299f + (g) * 0.587f + (b) * 0.114f),\
(i = (r) * 0.596f - (g) * 0.275f - (b) * 0.321f),\
((r) * 0.212f - (g) * 0.523f + (b) * 0.311f)\
)
#define YIQ_TO_RGB( y, i, q, to_rgb, type, r, g ) (\
r = (type) (y + to_rgb [0] * i + to_rgb [1] * q),\
g = (type) (y + to_rgb [2] * i + to_rgb [3] * q),\
(type) (y + to_rgb [4] * i + to_rgb [5] * q)\
)
#define PACK_RGB( r, g, b ) ((r) << 21 | (g) << 11 | (b) << 1)
enum { rgb_kernel_size = burst_size / alignment_count };
enum { rgb_bias = rgb_unit * 2 * snes_ntsc_rgb_builder };
typedef struct pixel_info_t
{
int offset;
float negate;
float kernel [4];
} pixel_info_t;
#if rescale_in > 1
#define PIXEL_OFFSET_( ntsc, scaled ) \
(kernel_size / 2 + ntsc + (scaled != 0) + (rescale_out - scaled) % rescale_out + \
(kernel_size * 2 * scaled))
#define PIXEL_OFFSET( ntsc, scaled ) \
PIXEL_OFFSET_( ((ntsc) - (scaled) / rescale_out * rescale_in),\
(((scaled) + rescale_out * 10) % rescale_out) ),\
(1.0f - (((ntsc) + 100) & 2))
#else
#define PIXEL_OFFSET( ntsc, scaled ) \
(kernel_size / 2 + (ntsc) - (scaled)),\
(1.0f - (((ntsc) + 100) & 2))
#endif
extern pixel_info_t const snes_ntsc_pixels [alignment_count];
/* Generate pixel at all burst phases and column alignments */
static void gen_kernel( init_t* impl, float y, float i, float q, snes_ntsc_rgb_t* out )
{
/* generate for each scanline burst phase */
float const* to_rgb = impl->to_rgb;
int burst_remain = burst_count;
y -= rgb_offset;
do
{
/* Encode yiq into *two* composite signals (to allow control over artifacting).
Convolve these with kernels which: filter respective components, apply
sharpening, and rescale horizontally. Convert resulting yiq to rgb and pack
into integer. Based on algorithm by NewRisingSun. */
pixel_info_t const* pixel = snes_ntsc_pixels;
int alignment_remain = alignment_count;
do
{
/* negate is -1 when composite starts at odd multiple of 2 */
float const yy = y * impl->fringing * pixel->negate;
float const ic0 = (i + yy) * pixel->kernel [0];
float const qc1 = (q + yy) * pixel->kernel [1];
float const ic2 = (i - yy) * pixel->kernel [2];
float const qc3 = (q - yy) * pixel->kernel [3];
float const factor = impl->artifacts * pixel->negate;
float const ii = i * factor;
float const yc0 = (y + ii) * pixel->kernel [0];
float const yc2 = (y - ii) * pixel->kernel [2];
float const qq = q * factor;
float const yc1 = (y + qq) * pixel->kernel [1];
float const yc3 = (y - qq) * pixel->kernel [3];
float const* k = &impl->kernel [pixel->offset];
int n;
++pixel;
for ( n = rgb_kernel_size; n; --n )
{
float i = k[0]*ic0 + k[2]*ic2;
float q = k[1]*qc1 + k[3]*qc3;
float y = k[kernel_size+0]*yc0 + k[kernel_size+1]*yc1 +
k[kernel_size+2]*yc2 + k[kernel_size+3]*yc3 + rgb_offset;
if ( rescale_out <= 1 )
k--;
else if ( k < &impl->kernel [kernel_size * 2 * (rescale_out - 1)] )
k += kernel_size * 2 - 1;
else
k -= kernel_size * 2 * (rescale_out - 1) + 2;
{
int r, g, b = YIQ_TO_RGB( y, i, q, to_rgb, int, r, g );
*out++ = PACK_RGB( r, g, b ) - rgb_bias;
}
}
}
while ( alignment_count > 1 && --alignment_remain );
if ( burst_count <= 1 )
break;
to_rgb += 6;
ROTATE_IQ( i, q, -0.866025f, -0.5f ); /* -120 degrees */
}
while ( --burst_remain );
}
static void correct_errors( snes_ntsc_rgb_t color, snes_ntsc_rgb_t* out );
#if DISABLE_CORRECTION
#define CORRECT_ERROR( a ) { out [i] += rgb_bias; }
#define DISTRIBUTE_ERROR( a, b, c ) { out [i] += rgb_bias; }
#else
#define CORRECT_ERROR( a ) { out [a] += error; }
#define DISTRIBUTE_ERROR( a, b, c ) {\
snes_ntsc_rgb_t fourth = (error + 2 * snes_ntsc_rgb_builder) >> 2;\
fourth &= (rgb_bias >> 1) - snes_ntsc_rgb_builder;\
fourth -= rgb_bias >> 2;\
out [a] += fourth;\
out [b] += fourth;\
out [c] += fourth;\
out [i] += error - (fourth * 3);\
}
#endif
#define RGB_PALETTE_OUT( rgb, out_ )\
{\
unsigned char* out = (out_);\
snes_ntsc_rgb_t clamped = (rgb);\
SNES_NTSC_CLAMP_( clamped, (8 - rgb_bits) );\
out [0] = (unsigned char) (clamped >> 21);\
out [1] = (unsigned char) (clamped >> 11);\
out [2] = (unsigned char) (clamped >> 1);\
}
/* blitter related */
#ifndef restrict
#if defined (__GNUC__)
#define restrict __restrict__
#elif defined (_MSC_VER) && _MSC_VER > 1300
#define restrict __restrict
#else
/* no support for restricted pointers */
#define restrict
#endif
#endif
#include <limits.h>
#if SNES_NTSC_OUT_DEPTH <= 16
#if USHRT_MAX == 0xFFFF
typedef unsigned short snes_ntsc_out_t;
#else
#error "Need 16-bit int type"
#endif
#else
#if UINT_MAX == 0xFFFFFFFF
typedef unsigned int snes_ntsc_out_t;
#elif ULONG_MAX == 0xFFFFFFFF
typedef unsigned long snes_ntsc_out_t;
#else
#error "Need 32-bit int type"
#endif
#endif
+4 -2
View File
@@ -61,6 +61,8 @@ static void* sdl_input_init(void)
}
}
sdl->use_keyboard = true;
return sdl;
}
@@ -141,7 +143,7 @@ static bool sdl_axis_pressed(sdl_input_t *sdl, int port_num, uint32_t joyaxis)
static bool sdl_is_pressed(sdl_input_t *sdl, int port_num, const struct snes_keybind *key)
{
if (sdl_key_pressed(key->key))
if (sdl->use_keyboard && sdl_key_pressed(key->key))
return true;
if (sdl->joysticks[port_num] == NULL)
return false;
@@ -246,7 +248,7 @@ static int16_t sdl_input_state(void *data, const struct snes_keybind **binds, bo
switch (device)
{
case SNES_DEVICE_JOYPAD:
return sdl_joypad_device_state(data, binds, port == SNES_PORT_1 ? 0 : 1, device, index, id);
return sdl_joypad_device_state(data, binds, (port == SNES_PORT_1) ? 0 : 1, device, index, id);
case SNES_DEVICE_MULTITAP:
return sdl_joypad_device_state(data, binds, (port == SNES_PORT_2) ? 1 + index : 0, device, index, id);
case SNES_DEVICE_MOUSE:
+1
View File
@@ -27,6 +27,7 @@ typedef struct sdl_input
unsigned num_buttons[MAX_PLAYERS];
unsigned num_hats[MAX_PLAYERS];
unsigned num_joysticks;
bool use_keyboard;
// A video driver could pre-init with the SDL driver and have it handle resizing events...
bool *quitting;
+210
View File
@@ -0,0 +1,210 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#include "SDL.h"
#include <stdbool.h>
#include "general.h"
#include <stdint.h>
#include <stdlib.h>
#include "ssnes_sdl_input.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
typedef struct x11_input
{
sdl_input_t *sdl;
Display *display;
char state[32];
} x11_input_t;
struct key_bind
{
int x;
int sdl;
};
static int keysym_lut[SDLK_LAST];
static const struct key_bind lut_binds[] = {
{ XK_Left, SDLK_LEFT },
{ XK_Right, SDLK_RIGHT },
{ XK_Up, SDLK_UP },
{ XK_Down, SDLK_DOWN },
{ XK_Return, SDLK_RETURN },
{ XK_Tab, SDLK_TAB },
{ XK_Insert, SDLK_INSERT },
{ XK_Delete, SDLK_DELETE },
{ XK_Shift_R, SDLK_RSHIFT },
{ XK_Shift_L, SDLK_LSHIFT },
{ XK_Control_L, SDLK_LCTRL },
{ XK_Alt_L, SDLK_LALT },
{ XK_space, SDLK_SPACE },
{ XK_Escape, SDLK_ESCAPE },
{ XK_KP_Add, SDLK_KP_PLUS },
{ XK_KP_Subtract, SDLK_KP_MINUS },
{ XK_F1, SDLK_F1 },
{ XK_F2, SDLK_F2 },
{ XK_F3, SDLK_F3 },
{ XK_F4, SDLK_F4 },
{ XK_F5, SDLK_F5 },
{ XK_F6, SDLK_F6 },
{ XK_F7, SDLK_F7 },
{ XK_F8, SDLK_F8 },
{ XK_F9, SDLK_F9 },
{ XK_F10, SDLK_F10 },
{ XK_F11, SDLK_F11 },
{ XK_F12, SDLK_F12 },
{ XK_a, SDLK_a },
{ XK_b, SDLK_b },
{ XK_c, SDLK_c },
{ XK_d, SDLK_d },
{ XK_e, SDLK_e },
{ XK_f, SDLK_f },
{ XK_g, SDLK_g },
{ XK_h, SDLK_h },
{ XK_i, SDLK_i },
{ XK_j, SDLK_j },
{ XK_k, SDLK_k },
{ XK_l, SDLK_l },
{ XK_m, SDLK_m },
{ XK_n, SDLK_n },
{ XK_o, SDLK_o },
{ XK_p, SDLK_p },
{ XK_q, SDLK_q },
{ XK_r, SDLK_r },
{ XK_s, SDLK_s },
{ XK_t, SDLK_t },
{ XK_u, SDLK_u },
{ XK_v, SDLK_v },
{ XK_w, SDLK_w },
{ XK_x, SDLK_x },
{ XK_y, SDLK_y },
{ XK_z, SDLK_z },
};
static void init_lut(void)
{
memset(keysym_lut, 0, sizeof(keysym_lut));
for (unsigned i = 0; i < sizeof(lut_binds) / sizeof(lut_binds[0]); i++)
keysym_lut[lut_binds[i].sdl] = lut_binds[i].x;
}
static void* x_input_init(void)
{
x11_input_t *x11 = calloc(1, sizeof(*x11));
if (!x11)
return NULL;
x11->display = XOpenDisplay(NULL);
if (!x11->display)
{
free(x11);
return NULL;
}
x11->sdl = input_sdl.init();
if (!x11->sdl)
{
free(x11);
return NULL;
}
init_lut();
x11->sdl->use_keyboard = false;
return x11;
}
static bool x_key_pressed(x11_input_t *x11, int key)
{
key = keysym_lut[key];
int keycode = XKeysymToKeycode(x11->display, key);
bool ret = x11->state[keycode >> 3] & (1 << (keycode & 7));
return ret;
}
static bool x_is_pressed(x11_input_t *x11, const struct snes_keybind *binds, unsigned id)
{
for (int i = 0; binds[i].id != -1; i++)
{
if (binds[i].id == id)
return x_key_pressed(x11, binds[i].key);
}
return false;
}
static bool x_bind_button_pressed(void *data, int key)
{
x11_input_t *x11 = data;
bool pressed = x_is_pressed(x11, g_settings.input.binds[0], key);
if (!pressed)
return input_sdl.key_pressed(x11->sdl, key);
return pressed;
}
static int16_t x_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id)
{
x11_input_t *x11 = data;
bool pressed = false;
switch (device)
{
case SNES_DEVICE_JOYPAD:
pressed = x_is_pressed(x11, binds[(port == SNES_PORT_1) ? 0 : 1], id);
if (!pressed)
pressed = input_sdl.input_state(x11->sdl, binds, port, device, index, id);
return pressed;
case SNES_DEVICE_MULTITAP:
pressed = x_is_pressed(x11, binds[(port == SNES_PORT_2) ? 1 + index : 0], id);
if (!pressed)
pressed = input_sdl.input_state(x11->sdl, binds, port, device, index, id);
return pressed;
default:
return 0;
}
}
static void x_input_free(void *data)
{
x11_input_t *x11 = data;
input_sdl.free(x11->sdl);
XCloseDisplay(x11->display);
free(data);
}
static void x_input_poll(void *data)
{
x11_input_t *x11 = data;
XQueryKeymap(x11->display, x11->state);
input_sdl.poll(x11->sdl);
}
const input_driver_t input_x = {
.init = x_input_init,
.poll = x_input_poll,
.input_state = x_input_state,
.key_pressed = x_bind_button_pressed,
.free = x_input_free,
.ident = "x"
};
+68 -2
View File
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include "general.h"
#include "dynamic.h"
#include <assert.h>
// CRC32 implementation taken from BSNES source :)
@@ -69,8 +70,7 @@ static const uint32_t crc32_table[256] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
static inline uint32_t crc32_adjust(uint32_t crc32, uint8_t input)
uint32_t crc32_adjust(uint32_t crc32, uint8_t input)
{
return ((crc32 >> 8) & 0x00ffffff) ^ crc32_table[(crc32 ^ input) & 0xff];
}
@@ -87,6 +87,17 @@ struct bsv_movie
{
FILE *file;
uint8_t *state;
long *frame_pos; // A ring buffer keeping track of many key presses were requested per frame.
size_t frame_mask;
uint16_t current_frame_count;
size_t frame_ptr;
bool playback;
unsigned min_file_pos;
bool first_rewind;
bool did_rewind;
};
#define BSV_MAGIC 0x42535631
@@ -127,6 +138,7 @@ static inline uint16_t swap_if_big16(uint16_t val)
static bool init_playback(bsv_movie_t *handle, const char *path)
{
handle->playback = true;
handle->file = fopen(path, "rb");
if (!handle->file)
{
@@ -158,6 +170,8 @@ static bool init_playback(bsv_movie_t *handle, const char *path)
if (!handle->state)
return false;
handle->min_file_pos = sizeof(header) + state_size;
if (fread(handle->state, 1, state_size, handle->file) != state_size)
{
SSNES_ERR("Couldn't read state from movie.\n");
@@ -196,6 +210,8 @@ static bool init_record(bsv_movie_t *handle, const char *path)
if (!handle->state)
return false;
handle->min_file_pos = sizeof(header) + state_size;
psnes_serialize(handle->state, state_size);
fwrite(handle->state, 1, state_size, handle->file);
return true;
@@ -208,6 +224,7 @@ void bsv_movie_free(bsv_movie_t *handle)
if (handle->file)
fclose(handle->file);
free(handle->state);
free(handle->frame_pos);
free(handle);
}
}
@@ -240,9 +257,58 @@ bsv_movie_t *bsv_movie_init(const char *path, enum ssnes_movie_type type)
else if (!init_record(handle, path))
goto error;
if (!(handle->frame_pos = calloc((1 << 20), sizeof(long)))) // Just pick something really large :D
goto error;
handle->frame_mask = (1 << 20) - 1;
return handle;
error:
bsv_movie_free(handle);
return NULL;
}
void bsv_movie_set_frame_start(bsv_movie_t *handle)
{
//fprintf(stderr, "Starting frame: %u, Pos: %ld\n", (unsigned)handle->frame_ptr, ftell(handle->file));
handle->frame_pos[handle->frame_ptr] = ftell(handle->file);
}
void bsv_movie_set_frame_end(bsv_movie_t *handle)
{
//fprintf(stderr, "Frame++\n");
handle->frame_ptr = (handle->frame_ptr + 1) & handle->frame_mask;
handle->first_rewind = !handle->did_rewind;
handle->did_rewind = false;
}
void bsv_movie_frame_rewind(bsv_movie_t *handle)
{
//fprintf(stderr, "Frame--\n");
handle->did_rewind = true;
if (handle->frame_ptr <= 1)
{
handle->frame_ptr = 0;
fseek(handle->file, handle->min_file_pos, SEEK_SET);
}
else
{
handle->frame_ptr = (handle->frame_ptr - (handle->first_rewind ? 1 : 2)) & handle->frame_mask;
fseek(handle->file, handle->frame_pos[handle->frame_ptr], SEEK_SET);
}
if (ftell(handle->file) <= (long)handle->min_file_pos)
{
if (!handle->playback)
{
fseek(handle->file, 4 * sizeof(uint32_t), SEEK_SET);
psnes_serialize(handle->state, psnes_serialize_size());
fwrite(handle->state, 1, psnes_serialize_size(), handle->file);
}
else
fseek(handle->file, handle->min_file_pos, SEEK_SET);
}
}
+6 -3
View File
@@ -37,12 +37,15 @@ bool bsv_movie_get_input(bsv_movie_t *handle, int16_t *input);
// Recording
void bsv_movie_set_input(bsv_movie_t *handle, int16_t input);
// Used for rewinding while playback/record.
void bsv_movie_set_frame_start(bsv_movie_t *handle); // Debugging purposes.
void bsv_movie_set_frame_end(bsv_movie_t *handle);
void bsv_movie_frame_rewind(bsv_movie_t *handle);
void bsv_movie_free(bsv_movie_t *handle);
uint32_t crc32_calculate(const uint8_t *data, unsigned length);
uint32_t crc32_adjust(uint32_t crc32, uint8_t input);
#endif
+23 -6
View File
@@ -17,8 +17,8 @@
#include "netplay.h"
#include "general.h"
#include "dynamic.h"
#include "autosave.h"
#include "dynamic.h"
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
@@ -47,6 +47,7 @@
#define NONCONST_CAST
#endif
#define PREV_PTR(x) ((x) == 0 ? handle->buffer_size - 1 : (x) - 1)
#define NEXT_PTR(x) ((x + 1) % handle->buffer_size)
@@ -408,25 +409,33 @@ static bool send_chunk(netplay_t *handle)
return true;
}
#define MAX_RETRIES 16
#define MAX_RETRIES 64
#define RETRY_MS 500
static int poll_input(netplay_t *handle, bool block)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(handle->udp_fd, &fds);
FD_SET(handle->fd, &fds);
int max_fd = (handle->fd > handle->udp_fd ? handle->fd : handle->udp_fd) + 1;
struct timeval tv = {
.tv_sec = 0,
.tv_usec = block ? 500000 : 0
.tv_usec = block ? (RETRY_MS * 1000) : 0
};
int i = 0;
do
{
if (select(handle->udp_fd + 1, &fds, NULL, NULL, &tv) < 0)
if (select(max_fd, &fds, NULL, NULL, &tv) < 0)
return -1;
// Somewhat hacky, but we aren't using the TCP connection for anything useful atm.
// Will probably add some proper messaging system here later.
if (FD_ISSET(handle->fd, &fds))
return -1;
if (FD_ISSET(handle->udp_fd, &fds))
return 1;
@@ -434,10 +443,18 @@ static int poll_input(netplay_t *handle, bool block)
{
SSNES_WARN("Netplay connection hung up. Will continue without netplay.\n");
handle->has_connection = false;
return false;
return -1;
}
} while (i++ < MAX_RETRIES && block);
if (block)
SSNES_LOG("Network lag of %d ms, resending packet... Attempt %d of %d ...\n", RETRY_MS, i, MAX_RETRIES);
// Seems to be necessary on Win32.
FD_ZERO(&fds);
FD_SET(handle->udp_fd, &fds);
FD_SET(handle->fd, &fds);
} while ((i++ < MAX_RETRIES) && block);
if (block)
return -1;
+12 -2
View File
@@ -15,6 +15,9 @@ if [ $HAVE_DYNAMIC != yes ]; then
add_define_make libsnes $LIBSNES
fi
check_lib FILTER -ldl dlopen
check_lib NETPLAY -lc socket
check_lib ALSA -lasound snd_pcm_open
check_header OSS sys/soundcard.h
@@ -24,9 +27,15 @@ else
check_lib AL -lopenal alcOpenDevice
fi
if [ "$OS" = "Darwin" ]; then
check_lib FBO "-framework OpenGL" glFramebufferTexture2D
else
check_lib FBO -lGL glFramebufferTexture2D
fi
check_pkgconf RSOUND rsound 1.1
check_lib ROAR -lroar roar_vs_new
check_lib JACK -ljack jack_client_open
check_pkgconf JACK jack 0.120.1
check_pkgconf PULSE libpulse
check_pkgconf SDL sdl 1.2.10
@@ -50,9 +59,10 @@ check_pkgconf SRC samplerate
check_lib DYNAMIC -ldl dlopen
check_pkgconf FREETYPE freetype2
check_lib XVIDEO -lXv XvShmCreateImage
# Creates config.mk and config.h.
VARS="ALSA OSS AL RSOUND ROAR JACK PULSE SDL FILTER CG XML DYNAMIC FFMPEG AVCODEC AVFORMAT AVCORE AVUTIL SWSCALE SRC CONFIGFILE FREETYPE"
VARS="ALSA OSS AL RSOUND ROAR JACK PULSE SDL FILTER CG XML DYNAMIC FFMPEG AVCODEC AVFORMAT AVCORE AVUTIL SWSCALE SRC CONFIGFILE FREETYPE XVIDEO NETPLAY FBO"
create_config_make config.mk $VARS
create_config_header config.h $VARS
+5 -2
View File
@@ -1,7 +1,7 @@
. qb/qb.params.sh
PACKAGE_NAME=ssnes
PACKAGE_VERSION=0.3
PACKAGE_VERSION=0.4-beta
# Adds a command line opt to ./configure --help
# $1: Variable (HAVE_ALSA, HAVE_OSS, etc)
@@ -10,11 +10,13 @@ PACKAGE_VERSION=0.3
add_command_line_enable DYNAMIC "Enable dynamic loading of libsnes library." no
add_command_line_string LIBSNES "libsnes library used" "-lsnes"
add_command_line_enable FFMPEG "Enable FFmpeg recording support" no
add_command_line_enable FILTER "Enable CPU filter support" no
add_command_line_enable FILTER "Enable CPU filter support" auto
add_command_line_enable NETPLAY "Enable netplay support" auto
add_command_line_enable SRC "Enable libsamplerate support" no
add_command_line_enable CONFIGFILE "Disable support for config file" yes
add_command_line_enable CG "Enable Cg shader support" auto
add_command_line_enable XML "Enable bSNES-style XML shader support" auto
add_command_line_enable FBO "Enable render-to-texture (FBO) support" auto
add_command_line_enable ALSA "Enable ALSA support" auto
add_command_line_enable OSS "Enable OSS support" auto
add_command_line_enable RSOUND "Enable RSound support" auto
@@ -23,3 +25,4 @@ add_command_line_enable AL "Enable OpenAL support" auto
add_command_line_enable JACK "Enable JACK support" auto
add_command_line_enable PULSE "Enable PulseAudio support" auto
add_command_line_enable FREETYPE "Enable FreeType support" auto
add_command_line_enable XVIDEO "Enable XVideo support" auto
+2 -2
View File
@@ -50,7 +50,7 @@ check_lib()
extralibs="$4"
$CC -o $TEMP_EXE $TEMP_C $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
$CC -o $TEMP_EXE $TEMP_C $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $CFLAGS $LDFLAGS $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
echo $answer
@@ -75,7 +75,7 @@ check_lib_cxx()
extralibs="$4"
$CXX -o $TEMP_EXE $TEMP_CXX $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
$CXX -o $TEMP_EXE $TEMP_CXX $INCLUDE_DIRS $LIBRARY_DIRS $extralibs $CFLAGS $LDFLAGS $2 2>/dev/null >/dev/null && answer=yes && eval HAVE_$1=yes
echo $answer
+8 -1
View File
@@ -33,6 +33,7 @@ struct state_manager
size_t top_ptr;
size_t bottom_ptr;
size_t state_size;
bool first_pop;
};
state_manager_t *state_manager_new(size_t state_size, size_t buffer_size, void *init_buffer)
@@ -77,6 +78,11 @@ void state_manager_free(state_manager_t *state)
bool state_manager_pop(state_manager_t *state, void **data)
{
*data = state->tmp_state;
if (state->first_pop)
{
state->first_pop = false;
return true;
}
if (state->top_ptr == 0)
state->top_ptr = state->buf_size - 1;
@@ -126,7 +132,7 @@ static void generate_delta(state_manager_t *state, const void *data)
const uint32_t *new_state = data;
state->buffer[state->top_ptr++] = 0; // For each separate delta, we have a 0 value sentinel in between.
if (state->top_ptr == state->bottom_ptr) // Check if top_ptr and bottom_ptr crossed eachother, which means we need to delete old cruft.
if (state->top_ptr == state->bottom_ptr) // Check if top_ptr and bottom_ptr crossed each other, which means we need to delete old cruft.
crossed = true;
for (uint64_t i = 0; i < state->state_size; i++)
@@ -156,6 +162,7 @@ bool state_manager_push(state_manager_t *state, const void *data)
{
generate_delta(state, data);
memcpy(state->tmp_state, data, state->state_size * sizeof(uint32_t));
state->first_pop = true;
return true;
}
+95 -54
View File
@@ -19,7 +19,6 @@
#include "conf/config_file.h"
#include <assert.h>
#include <string.h>
#include "hqflt/filters.h"
#include "config.def.h"
#ifdef HAVE_CONFIG_H
@@ -45,6 +44,9 @@ static void set_defaults(void)
case VIDEO_GL:
def_video = "gl";
break;
case VIDEO_XVIDEO:
def_video = "xvideo";
break;
default:
break;
}
@@ -84,6 +86,9 @@ static void set_defaults(void)
case INPUT_SDL:
def_input = "sdl";
break;
case INPUT_X:
def_input = "x";
break;
default:
break;
}
@@ -104,9 +109,7 @@ static void set_defaults(void)
g_settings.video.smooth = video_smooth;
g_settings.video.force_aspect = force_aspect;
g_settings.video.aspect_ratio = SNES_ASPECT_RATIO;
#ifdef HAVE_FILTER
g_settings.video.filter = FILTER_NONE;
#endif
g_settings.video.shader_type = SSNES_SHADER_AUTO;
#ifdef HAVE_FREETYPE
g_settings.video.font_size = font_size;
@@ -114,6 +117,13 @@ static void set_defaults(void)
g_settings.video.msg_pos_y = message_pos_offset_y;
#endif
#if defined(HAVE_CG) || defined(HAVE_XML)
g_settings.video.render_to_texture = render_to_texture;
g_settings.video.fbo_scale_x = fbo_scale_x;
g_settings.video.fbo_scale_y = fbo_scale_y;
g_settings.video.second_pass_smooth = second_pass_smooth;
#endif
g_settings.audio.enable = audio_enable;
g_settings.audio.out_rate = out_rate;
g_settings.audio.in_rate = in_rate;
@@ -279,6 +289,11 @@ static void parse_config_file(void)
CONFIG_GET_STRING(video.cg_shader_path, "video_cg_shader");
CONFIG_GET_STRING(video.bsnes_shader_path, "video_bsnes_shader");
CONFIG_GET_STRING(video.second_pass_shader, "video_second_pass_shader");
CONFIG_GET_BOOL(video.render_to_texture, "video_render_to_texture");
CONFIG_GET_DOUBLE(video.fbo_scale_x, "video_fbo_scale_x");
CONFIG_GET_DOUBLE(video.fbo_scale_y, "video_fbo_scale_y");
CONFIG_GET_BOOL(video.second_pass_smooth, "video_second_pass_smooth");
#ifdef HAVE_FREETYPE
CONFIG_GET_STRING(video.font_path, "video_font_path");
@@ -288,38 +303,29 @@ static void parse_config_file(void)
#endif
#ifdef HAVE_FILTER
if (config_get_string(conf, "video_filter", &tmp_str))
CONFIG_GET_STRING(video.filter_path, "video_filter");
#endif
#if defined(HAVE_CG) || defined(HAVE_XML)
if (config_get_string(conf, "video_shader_type", &tmp_str))
{
unsigned filter = 0;
if (strcasecmp(FILTER_HQ2X_STR, tmp_str) == 0)
filter = FILTER_HQ2X;
else if (strcasecmp(FILTER_HQ4X_STR, tmp_str) == 0)
filter = FILTER_HQ4X;
else if (strcasecmp(FILTER_GRAYSCALE_STR, tmp_str) == 0)
filter = FILTER_GRAYSCALE;
else if (strcasecmp(FILTER_BLEED_STR, tmp_str) == 0)
filter = FILTER_BLEED;
else if (strcasecmp(FILTER_NTSC_STR, tmp_str) == 0)
filter = FILTER_NTSC;
else
{
SSNES_ERR(
"Invalid filter... Valid filters are:\n"
"\t%s\n"
"\t%s\n"
"\t%s\n"
"\t%s\n"
"\t%s\n",
FILTER_HQ2X_STR, FILTER_HQ4X_STR, FILTER_GRAYSCALE_STR,
FILTER_BLEED_STR, FILTER_NTSC_STR);
exit(1);
}
if (strcmp("cg", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_CG;
else if (strcmp("bsnes", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_BSNES;
else if (strcmp("auto", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_AUTO;
else if (strcmp("none", tmp_str) == 0)
g_settings.video.shader_type = SSNES_SHADER_NONE;
free(tmp_str);
g_settings.video.filter = filter;
}
#endif
#if defined(HAVE_XML)
CONFIG_GET_STRING(video.shader_dir, "video_shader_dir");
#endif
CONFIG_GET_DOUBLE(input.axis_threshold, "input_axis_threshold");
CONFIG_GET_BOOL(input.netplay_client_swap_input, "netplay_client_swap_input");
CONFIG_GET_INT(input.joypad_map[0], "input_player1_joypad_index");
@@ -329,7 +335,6 @@ static void parse_config_file(void)
CONFIG_GET_INT(input.joypad_map[4], "input_player5_joypad_index");
// Audio settings.
CONFIG_GET_BOOL(audio.enable, "audio_enable");
CONFIG_GET_INT(audio.out_rate, "audio_out_rate");
CONFIG_GET_DOUBLE(audio.in_rate, "audio_in_rate");
@@ -405,6 +410,10 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player2_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -430,6 +439,10 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player3_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -455,6 +468,10 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player4_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -480,6 +497,10 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player5_a, SNES_DEVICE_ID_JOYPAD_A)
@@ -505,6 +526,10 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(rate_step_down, SSNES_AUDIO_INPUT_RATE_MINUS)
DECLARE_BIND(rewind, SSNES_REWIND)
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
};
@@ -545,6 +570,7 @@ static const struct key_map sdlk_map[] = {
{ "f10", SDLK_F10 },
{ "f11", SDLK_F11 },
{ "f12", SDLK_F12 },
{ "nul", SDLK_UNKNOWN },
};
static struct snes_keybind *find_snes_bind(unsigned port, int id)
@@ -578,6 +604,7 @@ static int find_sdlk_key(const char *str)
return find_sdlk_bind(str);
}
// Yes, this function needs a good refactor :)
static void read_keybinds(config_file_t *conf)
{
char *tmp_key = NULL;
@@ -592,6 +619,7 @@ static void read_keybinds(config_file_t *conf)
if (!bind)
continue;
// Check keybind
if (bind_maps[j][i].key && config_get_string(conf, bind_maps[j][i].key, &tmp_key))
{
int key = find_sdlk_key(tmp_key);
@@ -603,40 +631,53 @@ static void read_keybinds(config_file_t *conf)
tmp_key = NULL;
}
// Check joybutton bind (hats too)
if (bind_maps[j][i].btn && config_get_string(conf, bind_maps[j][i].btn, &tmp_btn))
{
const char *btn = tmp_btn;
if (*btn++ == 'h')
if (strcmp(tmp_btn, "nul") == 0)
{
if (isdigit(*btn))
{
char *dir = NULL;
int hat = strtol(btn, &dir, 0);
int hat_dir = 0;
if (dir)
{
if (strcasecmp(dir, "up") == 0)
hat_dir = HAT_UP_MASK;
else if (strcasecmp(dir, "down") == 0)
hat_dir = HAT_DOWN_MASK;
else if (strcasecmp(dir, "left") == 0)
hat_dir = HAT_LEFT_MASK;
else if (strcasecmp(dir, "right") == 0)
hat_dir = HAT_RIGHT_MASK;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
}
bind->joykey = NO_BTN;
}
else
bind->joykey = strtol(tmp_btn, NULL, 0);
{
if (*btn++ == 'h')
{
if (isdigit(*btn))
{
char *dir = NULL;
int hat = strtol(btn, &dir, 0);
int hat_dir = 0;
if (dir)
{
if (strcasecmp(dir, "up") == 0)
hat_dir = HAT_UP_MASK;
else if (strcasecmp(dir, "down") == 0)
hat_dir = HAT_DOWN_MASK;
else if (strcasecmp(dir, "left") == 0)
hat_dir = HAT_LEFT_MASK;
else if (strcasecmp(dir, "right") == 0)
hat_dir = HAT_RIGHT_MASK;
if (hat_dir)
bind->joykey = HAT_MAP(hat, hat_dir);
}
}
}
else
bind->joykey = strtol(tmp_btn, NULL, 0);
}
free(tmp_btn);
}
// Check joyaxis binds.
if (bind_maps[j][i].axis && config_get_string(conf, bind_maps[j][i].axis, &tmp_axis))
{
if (strlen(tmp_axis) >= 2 && (*tmp_axis == '+' || *tmp_axis == '-'))
if (strcmp(tmp_axis, "nul") == 0)
{
bind->joyaxis = AXIS_NONE;
}
else if (strlen(tmp_axis) >= 2 && (*tmp_axis == '+' || *tmp_axis == '-'))
{
int axis = strtol(tmp_axis + 1, NULL, 0);
if (*tmp_axis == '+')
Executable
+91
View File
@@ -0,0 +1,91 @@
#!/bin/sh
###
## Wrapper script that enables loading of compressed roms for SSNES.
## Author: Themaister
## License: Do whatever you want with it.
#
die()
{
echo "Error: $1"
rm "$TMPROM"
rm -r "$TMPDIR"
exit 1
}
if [ -z "$1" ]; then
die "Need at least one argument"
fi
COMP_FILE="$1"
TMPROM="`mktemp`"
TMPDIR="`mktemp -d`"
shift
extract_zip()
{
which unzip >/dev/null 2>&1 || die "Cannot find unzip"
which zipinfo >/dev/null 2>&1 || die "Cannot find zipinfo"
FILENAME="`zipinfo -1 \"$1\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
unzip -p "$1" "$FILENAME" > "$TMPROM"
}
extract_7z()
{
which 7z >/dev/null 2>&1 || die "Cannot find 7z"
7z x "$1" -o"$TMPDIR" >/dev/null 2>&1 || die "Couldn't extract from archive"
FILENAME="`ls \"$TMPDIR\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
mv "${TMPDIR}/${FILENAME}" "$TMPROM"
}
extract_rar()
{
which unrar >/dev/null 2>&1 || die "Cannot find unrar"
unrar x "$1" "$TMPDIR" >/dev/null 2>&1 || die "Couldn't extract from archive"
FILENAME="`ls \"$TMPDIR\" | grep '\.s[mf]c$' | head -n1`"
if [ -z "$FILENAME" ]; then
die "Cannot find .smc nor .sfc file in archive"
fi
mv "${TMPDIR}/${FILENAME}" "$TMPROM"
}
case "$COMP_FILE" in
*.zip )
extract_zip "$COMP_FILE"
EXTENSION=".zip"
;;
*.7z )
extract_7z "$COMP_FILE"
EXTENSION=".7z"
;;
*.rar )
extract_rar "$COMP_FILE"
EXTENSION=".rar"
;;
* )
die "Couldn't find valid zipped rom."
;;
esac
SAVENAME="`basename \"$COMP_FILE\" $EXTENSION`"
DIRNAME="`dirname \"$COMP_FILE\"`"
SAVENAME="${SAVENAME}.srm"
echo "Savename: ${DIRNAME}/${SAVENAME}"
SSNES_PATH="`which ssnes`"
if [ -z "$SSNES_PATH" ]; then
die "Cannot find SSNES in path."
fi
"$SSNES_PATH" "$TMPROM" -s "${DIRNAME}/${SAVENAME}" "$@"
rm "$TMPROM"
rm -r "$TMPDIR"
+145 -65
View File
@@ -25,7 +25,6 @@
#include <time.h>
#include "driver.h"
#include "file.h"
#include "hqflt/filters.h"
#include "general.h"
#include "dynamic.h"
#include "record/ffemu.h"
@@ -77,23 +76,6 @@ static void set_fast_forward_button(bool new_button_state)
old_button_state = new_button_state;
}
#ifdef HAVE_FILTER
static inline void process_frame (uint16_t * restrict out, const uint16_t * restrict in, unsigned width, unsigned height)
{
int pitch = 1024;
if (height == 448 || height == 478)
pitch = 512;
for (int y = 0; y < height; y++)
{
const uint16_t *src = in + y * pitch;
uint16_t *dst = out + y * width;
memcpy(dst, src, width * sizeof(uint16_t));
}
}
#endif
// libsnes: 0.065
// Format received is 16-bit 0RRRRRGGGGGBBBBB
static void video_frame(const uint16_t *data, unsigned width, unsigned height)
@@ -117,44 +99,18 @@ static void video_frame(const uint16_t *data, unsigned width, unsigned height)
const char *msg = msg_queue_pull(g_extern.msg_queue);
#ifdef HAVE_FILTER
uint16_t output_filter[width * height * 4 * 4];
uint16_t output[width * height];
if (g_settings.video.filter != FILTER_NONE)
process_frame(output, data, width, height);
switch (g_settings.video.filter)
if (g_extern.filter.active)
{
case FILTER_HQ2X:
ProcessHQ2x(output, output_filter);
if (!driver.video->frame(driver.video_data, output_filter, width << 1, height << 1, width << 2, msg))
g_extern.video_active = false;
break;
case FILTER_HQ4X:
ProcessHQ4x(output, output_filter);
if (!driver.video->frame(driver.video_data, output_filter, width << 2, height << 2, width << 3, msg))
g_extern.video_active = false;
break;
case FILTER_GRAYSCALE:
grayscale_filter(output, width, height);
if (!driver.video->frame(driver.video_data, output, width, height, width << 1, msg))
g_extern.video_active = false;
break;
case FILTER_BLEED:
bleed_filter(output, width, height);
if (!driver.video->frame(driver.video_data, output, width, height, width << 1, msg))
g_extern.video_active = false;
break;
case FILTER_NTSC:
ntsc_filter(output_filter, output, width, height);
if (!driver.video->frame(driver.video_data, output_filter, SNES_NTSC_OUT_WIDTH(width), height, SNES_NTSC_OUT_WIDTH(width) << 1, msg))
g_extern.video_active = false;
break;
default:
if (!driver.video->frame(driver.video_data, data, width, height, (height == 448 || height == 478) ? 1024 : 2048, msg))
g_extern.video_active = false;
unsigned owidth = width;
unsigned oheight = height;
g_extern.filter.psize(&owidth, &oheight);
g_extern.filter.prender(g_extern.filter.colormap, g_extern.filter.buffer,
g_extern.filter.pitch, data, (height == 448 || height == 478) ? 1024 : 2048, width, height);
if (!driver.video->frame(driver.video_data, g_extern.filter.buffer, owidth, oheight, g_extern.filter.pitch, msg))
g_extern.video_active = false;
}
else if (!driver.video->frame(driver.video_data, data, width, height, (height == 448 || height == 478) ? 1024 : 2048, msg))
g_extern.video_active = false;
#else
if (!driver.video->frame(driver.video_data, data, width, height, (height == 448 || height == 478) ? 1024 : 2048, msg))
g_extern.video_active = false;
@@ -285,7 +241,7 @@ static void fill_pathname(char *out_path, char *in_path, const char *replace)
#endif
#ifdef _WIN32
#define PACKAGE_VERSION "0.3"
#define PACKAGE_VERSION "0.4-beta"
#endif
#include "config.features.h"
@@ -307,11 +263,13 @@ static void print_features(void)
_PSUPP(filter, "Filter", "CPU based video filters");
_PSUPP(cg, "Cg", "Cg pixel shaders");
_PSUPP(xml, "XML", "bSNES XML pixel shaders");
_PSUPP(fbo, "FBO", "OpenGL render-to-texture (multi-pass shaders)");
_PSUPP(dynamic, "Dynamic", "Dynamic run-time loading of libsnes library");
_PSUPP(ffmpeg, "FFmpeg", "On-the-fly recording of gameplay with libavcodec");
_PSUPP(src, "SRC", "libsamplerate audio resampling");
_PSUPP(configfile, "Config file", "Configuration file support");
_PSUPP(freetype, "FreeType", "TTF font rendering with FreeType");
_PSUPP(netplay, "Netplay", "Peer-to-peer netplay");
}
#undef _PSUPP
@@ -339,15 +297,18 @@ static void print_help(void)
puts("\t-J/--justifiers: Daisy chain two virtual Konami Justifiers into port 2 of the SNES.");
puts("\t-4/--multitap: Connect a multitap to port 2 of the SNES.");
puts("\t-P/--bsvplay: Playback a BSV movie file.");
#ifdef HAVE_NETPLAY
puts("\t-H/--host: Host netplay as player 1.");
puts("\t-C/--connect: Connect to netplay as player 2.");
puts("\t--port: Port used to netplay. Default is 55435.");
puts("\t-F/--frames: Sync frames when using netplay.");
#endif
#ifdef HAVE_FFMPEG
puts("\t-r/--record: Path to record video file. Settings for video/audio codecs are found in config file.");
#endif
puts("\t-v/--verbose: Verbose logging");
puts("\t-U/--ups: Specifies path for UPS patch that will be applied to ROM.\n");
print_features();
}
@@ -398,6 +359,7 @@ static void parse_input(int argc, char *argv[])
{ "connect", 1, NULL, 'C' },
{ "frames", 1, NULL, 'F' },
{ "port", 1, &val, 'p' },
{ "ups", 1, NULL, 'U' },
{ NULL, 0, NULL, 0 }
};
@@ -415,7 +377,7 @@ static void parse_input(int argc, char *argv[])
#define CONFIG_FILE_ARG
#endif
char optstring[] = "hs:vS:m:p4jJg:b:B:Y:Z:P:HC:F:" FFMPEG_RECORD_ARG CONFIG_FILE_ARG;
char optstring[] = "hs:vS:m:p4jJg:b:B:Y:Z:P:HC:F:U:" FFMPEG_RECORD_ARG CONFIG_FILE_ARG;
for(;;)
{
val = 0;
@@ -530,6 +492,10 @@ static void parse_input(int argc, char *argv[])
g_extern.netplay_sync_frames = 32;
break;
case 'U':
strncpy(g_extern.ups_name, optarg, sizeof(g_extern.ups_name) - 1);
break;
case 0:
switch (val)
{
@@ -777,6 +743,7 @@ static void deinit_movie(void)
#define SSNES_DEFAULT_PORT 55435
#ifdef HAVE_NETPLAY
static void init_netplay(void)
{
if (g_extern.netplay_enable)
@@ -804,12 +771,15 @@ static void init_netplay(void)
}
}
}
#endif
#ifdef HAVE_NETPLAY
static void deinit_netplay(void)
{
if (g_extern.netplay)
netplay_free(g_extern.netplay);
}
#endif
static void init_autosave(void)
{
@@ -909,6 +879,9 @@ static void fill_pathnames(void)
if (!g_extern.bsv_movie_playback)
fill_pathname(g_extern.bsv_movie_path, g_extern.savefile_name_srm, "");
if (!(*g_extern.ups_name) && *g_extern.basename)
fill_pathname(g_extern.ups_name, g_extern.basename, ".ups");
}
// Save or load state here.
@@ -1037,6 +1010,13 @@ static void check_input_rate(void)
static void check_rewind(void)
{
g_extern.frame_is_reverse = false;
static bool first = true;
if (first)
{
first = false;
return;
}
if (!g_extern.state_manager)
return;
@@ -1046,9 +1026,15 @@ static void check_rewind(void)
void *buf;
if (state_manager_pop(g_extern.state_manager, &buf))
{
g_extern.frame_is_reverse = true;
msg_queue_push(g_extern.msg_queue, "Rewinding!", 0, 30);
psnes_unserialize(buf, psnes_serialize_size());
g_extern.frame_is_reverse = true;
if (g_extern.bsv_movie)
{
for (unsigned i = 0; i < (g_settings.rewind_granularity ? g_settings.rewind_granularity : 1); i++)
bsv_movie_frame_rewind(g_extern.bsv_movie);
}
}
else
msg_queue_push(g_extern.msg_queue, "Reached end of rewind buffer!", 0, 30);
@@ -1057,7 +1043,7 @@ static void check_rewind(void)
{
static unsigned cnt = 0;
cnt = (cnt + 1) % (g_settings.rewind_granularity ? g_settings.rewind_granularity : 1); // Avoid possible SIGFPE.
if (cnt == 0)
if (cnt == 0 || g_extern.bsv_movie)
{
psnes_serialize(g_extern.state_buf, psnes_serialize_size());
state_manager_push(g_extern.state_manager, g_extern.state_buf);
@@ -1160,10 +1146,72 @@ static void check_pause(void)
old_state = new_state;
}
static void check_reset(void)
{
if (driver.input->key_pressed(driver.input_data, SSNES_RESET))
{
SSNES_LOG("Resetting game!\n");
msg_queue_clear(g_extern.msg_queue);
msg_queue_push(g_extern.msg_queue, "Reset!", 1, 120);
psnes_reset();
init_controllers(); // bSNES since v073r01 resets controllers to JOYPAD after a reset, so just enforce it here.
}
}
#ifdef HAVE_XML
static void check_shader_dir(void)
{
static bool old_pressed_next = false;
static bool old_pressed_prev = true;
if (!g_extern.shader_dir.elems || !driver.video->xml_shader)
return;
bool should_apply = false;
bool pressed_next = driver.input->key_pressed(driver.input_data, SSNES_SHADER_NEXT);
bool pressed_prev = driver.input->key_pressed(driver.input_data, SSNES_SHADER_PREV);
if (pressed_next && !old_pressed_next)
{
should_apply = true;
g_extern.shader_dir.ptr = (g_extern.shader_dir.ptr + 1) % g_extern.shader_dir.size;
}
else if (pressed_prev && !old_pressed_prev)
{
should_apply = true;
if (g_extern.shader_dir.ptr == 0)
g_extern.shader_dir.ptr = g_extern.shader_dir.size - 1;
else
g_extern.shader_dir.ptr--;
}
if (should_apply)
{
const char *shader = g_extern.shader_dir.elems[g_extern.shader_dir.ptr];
strncpy(g_settings.video.bsnes_shader_path, shader, sizeof(g_settings.video.bsnes_shader_path) - 1);
g_settings.video.shader_type = SSNES_SHADER_BSNES;
msg_queue_clear(g_extern.msg_queue);
char msg[512];
snprintf(msg, sizeof(msg), "XML shader #%u: \"%s\"", (unsigned)g_extern.shader_dir.ptr, shader);
msg_queue_push(g_extern.msg_queue, msg, 1, 120);
SSNES_LOG("Applying shader \"%s\"\n", shader);
if (!driver.video->xml_shader(driver.video_data, shader))
SSNES_WARN("Failed to apply shader!\n");
}
old_pressed_next = pressed_next;
old_pressed_prev = pressed_prev;
}
#endif
static void do_state_checks(void)
{
if (!g_extern.netplay)
{
check_reset();
check_pause();
if (g_extern.is_paused)
return;
@@ -1174,23 +1222,36 @@ static void do_state_checks(void)
{
check_stateslots();
check_savestates();
check_rewind();
}
check_rewind();
if (!g_extern.bsv_movie_playback)
check_movie_record();
#ifdef HAVE_XML
check_shader_dir();
#endif
}
check_fullscreen();
check_input_rate();
}
static void fill_title_buf(void)
{
if (psnes_library_id)
snprintf(g_extern.title_buf, sizeof(g_extern.title_buf), "SSNES : %s", psnes_library_id());
else
snprintf(g_extern.title_buf, sizeof(g_extern.title_buf), "SSNES");
}
int main(int argc, char *argv[])
{
parse_input(argc, argv);
parse_config();
init_dlsym();
fill_title_buf();
psnes_init();
if (strlen(g_extern.basename) > 0)
@@ -1207,18 +1268,27 @@ int main(int argc, char *argv[])
init_movie();
if (!g_extern.bsv_movie)
{
load_save_files();
init_rewind();
}
#ifdef HAVE_NETPLAY
init_netplay();
#endif
init_drivers();
if (!g_extern.netplay)
init_rewind();
#ifdef HAVE_NETPLAY
psnes_set_video_refresh(g_extern.netplay ? video_frame_net : video_frame);
psnes_set_audio_sample(g_extern.netplay ? audio_sample_net : audio_sample);
psnes_set_input_poll(g_extern.netplay ? input_poll_net : input_poll);
psnes_set_input_state(g_extern.netplay ? input_state_net : input_state);
#else
psnes_set_video_refresh(video_frame);
psnes_set_audio_sample(audio_sample);
psnes_set_input_poll(input_poll);
psnes_set_input_state(input_state);
#endif
init_controllers();
@@ -1245,13 +1315,21 @@ int main(int argc, char *argv[])
{
lock_autosave();
#ifdef HAVE_NETPLAY
if (g_extern.netplay)
netplay_pre_frame(g_extern.netplay);
#endif
if (g_extern.bsv_movie)
bsv_movie_set_frame_start(g_extern.bsv_movie);
psnes_run();
if (g_extern.bsv_movie)
bsv_movie_set_frame_end(g_extern.bsv_movie);
#ifdef HAVE_NETPLAY
if (g_extern.netplay)
netplay_post_frame(g_extern.netplay);
#endif
unlock_autosave();
}
@@ -1270,7 +1348,9 @@ int main(int argc, char *argv[])
}
}
#ifdef HAVE_NETPLAY
deinit_netplay();
#endif
if (!g_extern.bsv_movie_playback && !g_extern.netplay_is_client)
deinit_autosave();
@@ -1280,10 +1360,10 @@ int main(int argc, char *argv[])
#endif
if (!g_extern.bsv_movie_playback && !g_extern.netplay_is_client)
{
deinit_rewind();
save_files();
}
if (!g_extern.netplay)
deinit_rewind();
deinit_movie();
deinit_msg_queue();
+37 -6
View File
@@ -9,9 +9,9 @@
# video_xscale = 3.0
# video_yscale = 3.0
# Fullscreen resolution
# video_fullscreen_x = 1920
# video_fullscreen_y = 1200
# Fullscreen resolution. Resolution of 0 uses the resolution of the desktop.
# video_fullscreen_x = 0
# video_fullscreen_y = 0
# Start in fullscreen. Can be changed at runtime.
# video_fullscreen = false
@@ -31,11 +31,32 @@
# Path to Cg shader.
# video_cg_shader = "/path/to/cg/shader.cg"
# Path to bSNES-style XML shader (GLSL only). If both Cg shader path and XML shader path are defined, Cg shader will take priority.
# Path to bSNES XML shader (GLSL only). If both Cg shader path and XML shader path are defined,
# Cg shader will take priority unless overridden in video_shader_type.
# video_bsnes_shader = "/path/to/bsnes/xml/shader.shader"
# CPU-based filter. Valid ones are: hq2x, hq4x, grayscale, bleed, ntsc.
# video_filter = ntsc
# Which shader type to use. Valid values are "cg", "bsnes", "none" and "auto"
# video_shader_type = auto
# Defines a directory where XML shaders are kept.
# video_shader_dir =
# Render to texture first. Useful when doing multi-pass shaders or control the output of shaders better.
# video_render_to_texture = false
# Defines the video scale of render-to-texture.
# The output FBO size is scaled by these amounts against the input size (typically 256 * 224 for SNES).
# video_fbo_scale_x = 2.0
# video_fbo_scale_y = 2.0
# Define shader to use for second pass (needs render-to-texture).
# video_second_pass_shader = "/path/to/second/shader.{cg,shader}"
# Defines if bilinear filtering is used during second pass (needs render-to-texture).
# video_second_pass_smooth = true
# CPU-based filter. Path to a bSNES CPU filter (*.filter)
# video_filter =
# Path to a TTF font used for rendering messages. This path must be defined to enable fonts.
# Do note that the _full_ path of the font is necessary!
@@ -90,6 +111,8 @@
# input_axis_threshold = 0.5
# Keyboard input. Will recognize normal keypresses and special keys like "left", "right", and so on.
# Keyboard input, Joypad and Joyaxis will all obey the "nul" bind, which disables the bind completely,
# rather than relying on a default.
# input_player1_a = x
# input_player1_b = z
# input_player1_y = a
@@ -192,6 +215,10 @@
# input_rate_step_up = kp_plus
# input_rate_step_down = kp_minus
# Applies next and previous XML shader in directory.
# input_shader_next = m
# input_shader_prev = n
# Enable rewinding. This will take a performance hit when playing, so it is disabled by default.
# Do note that rewinding will only work properly when using bSNES libsnes core atm.
# rewind_enable = false
@@ -212,6 +239,9 @@
# Toggle between paused and non-paused state
# input_pause_toggle = p
# Reset the emulated SNES.
# input_reset = h
# Pause gameplay when window focus is lost.
# pause_nonactive = true
@@ -221,3 +251,4 @@
# When being client over netplay, use keybinds for player 1.
# netplay_client_swap_input = false
+4 -1
View File
@@ -91,9 +91,12 @@ static struct bind binds[] = {
MISC_BIND("Rewind", rewind)
MISC_BIND("Movie recording toggle", movie_record_toggle)
MISC_BIND("Pause", pause_toggle)
MISC_BIND("Reset", reset)
MISC_BIND("Next shader", shader_next)
MISC_BIND("Previous shader", shader_prev)
};
void get_binds(config_file_t *conf, int player, int joypad)
static void get_binds(config_file_t *conf, int player, int joypad)
{
if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) < 0)
{
+165
View File
@@ -0,0 +1,165 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "ups.h"
#include "movie.h"
struct ups_data
{
const uint8_t *patch_data, *source_data;
uint8_t *target_data;
unsigned patch_length, source_length, target_length;
unsigned patch_offset, source_offset, target_offset;
unsigned patch_checksum, source_checksum, target_checksum;
};
static uint8_t patch_read(struct ups_data *data)
{
if (data->patch_offset < data->patch_length)
{
uint8_t n = data->patch_data[data->patch_offset++];
data->patch_checksum = crc32_adjust(data->patch_checksum, n);
return n;
}
return 0x00;
}
static uint8_t source_read(struct ups_data *data)
{
if (data->source_offset < data->source_length)
{
uint8_t n = data->source_data[data->source_offset++];
data->source_checksum = crc32_adjust(data->source_checksum, n);
return n;
}
return 0x00;
}
static void target_write(struct ups_data *data, uint8_t n)
{
if (data->target_offset < data->target_length)
{
data->target_data[data->target_offset] = n;
data->target_checksum = crc32_adjust(data->target_checksum, n);
}
data->target_offset++;
}
static uint64_t decode(struct ups_data *data)
{
uint64_t offset = 0, shift = 1;
while (true)
{
uint8_t x = patch_read(data);
offset += (x & 0x7f) * shift;
if (x & 0x80)
break;
shift <<= 7;
offset += shift;
}
return offset;
}
ups_error_t ups_apply_patch(
const uint8_t *patchdata, size_t patchlength,
const uint8_t *sourcedata, size_t sourcelength,
uint8_t *targetdata, size_t *targetlength)
{
struct ups_data data = {
.patch_data = patchdata,
.source_data = sourcedata,
.target_data = targetdata,
.patch_length = patchlength,
.source_length = sourcelength,
.target_length = *targetlength,
.patch_checksum = ~0,
.source_checksum = ~0,
.target_checksum = ~0
};
if (data.patch_length < 18)
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'U')
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'P')
return UPS_PATCH_INVALID;
if (patch_read(&data) != 'S')
return UPS_PATCH_INVALID;
if (patch_read(&data) != '1')
return UPS_PATCH_INVALID;
unsigned source_read_length = decode(&data);
unsigned target_read_length = decode(&data);
if (data.source_length != source_read_length && data.source_length != target_read_length)
return UPS_SOURCE_INVALID;
*targetlength = (data.source_length == source_read_length ? target_read_length : source_read_length);
if (data.target_length < *targetlength)
return UPS_TARGET_TOO_SMALL;
data.target_length = *targetlength;
while (data.patch_offset < data.patch_length - 12)
{
unsigned length = decode(&data);
while (length--)
target_write(&data, source_read(&data));
while (true)
{
uint8_t patch_xor = patch_read(&data);
target_write(&data, patch_xor ^ source_read(&data));
if (patch_xor == 0) break;
}
}
while (data.source_offset < data.source_length)
target_write(&data, source_read(&data));
while (data.target_offset < data.target_length)
target_write(&data, source_read(&data));
uint32_t patch_read_checksum = 0, source_read_checksum = 0, target_read_checksum = 0;
for (unsigned i = 0; i < 4; i++)
source_read_checksum |= patch_read(&data) << (i * 8);
for (unsigned i = 0; i < 4; i++)
target_read_checksum |= patch_read(&data) << (i * 8);
uint32_t patch_result_checksum = ~data.patch_checksum;
data.source_checksum = ~data.source_checksum;
data.target_checksum = ~data.target_checksum;
for (unsigned i = 0; i < 4; i++)
patch_read_checksum |= patch_read(&data) << (i * 8);
if (patch_result_checksum != patch_read_checksum)
return UPS_PATCH_INVALID;
if (data.source_checksum == source_read_checksum && data.source_length == source_read_length)
{
if (data.target_checksum == target_read_checksum && data.target_length == target_read_length)
return UPS_SUCCESS;
return UPS_TARGET_INVALID;
}
else if (data.source_checksum == target_read_checksum && data.source_length == target_read_length)
{
if (data.target_checksum == source_read_checksum && data.target_length == source_read_length)
return UPS_SUCCESS;
return UPS_TARGET_INVALID;
}
else
return UPS_SOURCE_INVALID;
}
+44
View File
@@ -0,0 +1,44 @@
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
*
* Some code herein may be based on code found in BSNES.
*
* SSNES 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* SSNES 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 SSNES.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __UPS_H
#define __UPS_H
#include <stdint.h>
#include <stddef.h>
// UPS implementation from bSNES.
typedef enum ups_error
{
UPS_UNKNOWN,
UPS_SUCCESS,
UPS_PATCH_INVALID,
UPS_SOURCE_INVALID,
UPS_TARGET_INVALID,
UPS_TARGET_TOO_SMALL,
UPS_PATCH_CHECKSUM_INVALID,
UPS_SOURCE_CHECKSUM_INVALID,
UPS_TARGET_CHECKSUM_INVALID
} ups_error_t;
ups_error_t ups_apply_patch(
const uint8_t *patch_data, size_t patch_length,
const uint8_t *source_data, size_t source_length,
uint8_t *target_data, size_t *target_length);
#endif