Skip to content

Commit

Permalink
beta7
Browse files Browse the repository at this point in the history
  • Loading branch information
DerKoun committed Jun 12, 2019
1 parent ee7b978 commit 8533686
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 110 deletions.
106 changes: 99 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,99 @@
bsnes-hd
========
bsnes-hd (formally "HD Mode 7 mod, for bsnes") is a fork of bsnes (currently 107r3) that adds HD video features, such as:
- HD Mode 7, rendering the rotated or pseudo perspective backgrounds at higher resolutions
- Widescreen Mode 7, as well as widescreen for other backgrounds and sprites with significant limitations
- more to come
Further documentation coming soon
# bsnes-hd



## What is it?

bsnes-hd (formally "*HD Mode 7 mod, for bsnes*") is a fork of bsnes (currently 107r3) that adds HD video features, such as:

### HD Mode 7

Rendering the rotated, scaled or pseudo perspective backgrounds at higher resolutions. This does not involve new custom imagery or upscaling algorithms. It is a higher resolution version of the process the SNES uses.

### Widescreen

Mostly for Mode 7 scenes, but also works for some other scenes/games here and there. Some setting tweaking required...

### more to come

For example higher color depth and interpolation for the colors blending that help the sense of depth.

### plus some none-HD related features

like the ability to disable background layers, sprites and window effects for screenshots for wallpapers or soft crop to zoom in, leaving maps or static art off the sides of the screen.



## Settings

all in Settings / Emulator

### Scale

The resolution Mode 7 is rendered at. Higher setting drastically increase CPU usage. "*disable*" uses the classic Mode 7 code, disabling some of the following features.

### Perspective correction

Whether and how pseudo 3D perspective are optimized, avoiding limitations of SNES integer math.
- **off**: disable correction
- **on**: enable correction
- **auto**(default): enable correction, but try to detect when it breaks the image and automatically disable it
- **wide**(default)/**medium**/**narrow**: the distance between the lines used for the interpolation (and also for the detection, if enabled). Use "*wide*" unless it causes issues. Only currently known game that requires this is "*Tales of Phantasia*", requiring "*narrow*".

### Supersampling

Allows basing every resulting pixel on the average of multiple pixels. At 1x scale it allows using the higher precision at the original resolution, e.g. for use with CRT-shaders. At higher resolutions it is basically expensive anti-aliasing. Keep in mind that to estimate the performance impact you have to multiply(!) this value with the "*scale*" value.

### Keep Mosaics non-HD

When unchecked mosaic effects are ignored on Mode 7 backgrounds. When checked the affected lines fall back to classic Mode 7.

### Widescreen

Enables experimental widescreen (works best for Mode 7 scenes, rest is hit and miss) and sets its AR. The actual AR are slightly wider than the selection, use some "*soft crop*" "*left*" and "*right*" for the ideal fullscreen experience. "*HDcrop*" at 5x scale results in a width of 1920, which (combined with "*soft crop*" "*top*" and "*bottom*" at 10, output/center and shader/none) results in perfect 1080p fullscreen.

### BG1/BG2/BG2/BG4

Settings for the background layers:
- **off**: no widescreen (e.g. for HUDs)
- **on**: widescreen enabled
- **< xy**/**> xy**: widescreen enabled above/below a certain scanline (for backgrounds that contain HUDs and backgrounds).
- **crop**: do not draw the left- and right-most 8 pixels (next to the widescreen areas) of the background (for backgrounds blanking some edge pixels, leaving black lines in widescreen, e.g. "*Top Gear 2*").
- **cAuto**: same as "*crop*", except for 2 differences. Only black pixel are not rendered. And lines of black pixel starting in the left crop area are removed entirely, e.g. for "*Super Mario Kart*".
- **dis.**: Disable the background entirely, e.g. to remove HUDs for screenshots for wallpapers.

### Sprites

- **normal**: Sprites are rendered if they are at least partially in the classic content area. So they can only partially reach into the widescreen areas.
- **unsafe**: Sprites are rendered, even if they are entirely in a widescreen area. Works fine for a view games, like "*Super Mario World*", causes artifacts in many.
- **disab.**: Disable sprites entirely, e.g. to remove characters or HUD elements for screenshots for wallpapers.

### Ignore window

- **none**: Apply window effects normally. Widescreen pixels are treated like the nearest non-widescreen pixel.
- **outside**: "*outside*" window effects are ignored, i.e. all pixels are treated like they are at the "*fallback x-coordinate*" for this purpose. This allows Mode 7 widescreen in "*Final Fantasy III*"
- **outside&alw**: Same as outside, but also for "*always*" window effects.
- **all**: Same as outside, but for all window effects. Can for example be used to remove the shadow and meter from "*F-Zero*" for screenshots for wallpapers (requires a "*fallback x-coordinate*" other than 128).

### Fallback x-coordinate

The x-coordinate used as fallback for "*ignore window*"

### Soft crop

Allows widths at the four edges of the image to be declared uninteresting. So when sizing and placing the image, especially in fullscreen, they can overflow the screen, allowing the significant parts of the image to be larger. So to focus on the top part of "*Super Mario Kart*" set "*bottom*" to 130 and "*top*" to 0. Further tweaking based on your monitor AR and liking may be required.
- **none**: disable soft crop
- **center**: enable soft crop for output/center
- **scale**: enable soft crop for output/center and output/scale

### Top/Bottom/Left/Right

The soft crop widths for all four edges.



## Help Wanted

### Converting C++ Rendering Code to Shaders for Optimization

Currently all the HD rendering is running on the CPU cores. This will have to be moved to the GPU to achieve usable performance. I'd really appreciate any help with that, even if it's just to get started. Please open an issue if you want to contribute, or contact me on Reddit.
1 change: 1 addition & 0 deletions bsnes/obj/DO_NOT_DELETE_THIS_FOLDER
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DO_NOT_DELETE_THIS_FOLDER
1 change: 1 addition & 0 deletions bsnes/out/DO_NOT_DELETE_THIS_FOLDER
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DO_NOT_DELETE_THIS_FOLDER
12 changes: 9 additions & 3 deletions bsnes/sfc/interface/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ auto Configuration::process(Markup::Node document, bool load) -> void {
bind(boolean, "Hacks/PPU/Fast", hacks.ppu.fast);
bind(boolean, "Hacks/PPU/NoSpriteLimit", hacks.ppu.noSpriteLimit);
bind(natural, "Hacks/PPU/Mode7/Scale", hacks.ppu.mode7.scale);
bind(boolean, "Hacks/PPU/Mode7/Perspective", hacks.ppu.mode7.perspective);
bind(natural, "Hacks/PPU/Mode7/Perspective", hacks.ppu.mode7.perspective);
bind(natural, "Hacks/PPU/Mode7/Widescreen", hacks.ppu.mode7.widescreen);
bind(natural, "Hacks/PPU/Mode7/Wsbg1", hacks.ppu.mode7.wsbg1);
bind(natural, "Hacks/PPU/Mode7/Wsbg2", hacks.ppu.mode7.wsbg2);
bind(natural, "Hacks/PPU/Mode7/Wsbg3", hacks.ppu.mode7.wsbg3);
bind(natural, "Hacks/PPU/Mode7/Wsbg4", hacks.ppu.mode7.wsbg4);
bind(boolean, "Hacks/PPU/Mode7/Wsobj", hacks.ppu.mode7.wsobj);
bind(boolean, "Hacks/PPU/Mode7/Igwin", hacks.ppu.mode7.igwin);
bind(natural, "Hacks/PPU/Mode7/Wsobj", hacks.ppu.mode7.wsobj);
bind(natural, "Hacks/PPU/Mode7/Igwin", hacks.ppu.mode7.igwin);
bind(natural, "Hacks/PPU/Mode7/Igwinx", hacks.ppu.mode7.igwinx);
bind(natural, "Hacks/PPU/Mode7/UnintrMode", hacks.ppu.mode7.unintrMode);
bind(natural, "Hacks/PPU/Mode7/UnintrTop", hacks.ppu.mode7.unintrTop);
bind(natural, "Hacks/PPU/Mode7/UnintrBottom", hacks.ppu.mode7.unintrBottom);
bind(natural, "Hacks/PPU/Mode7/UnintrLeft", hacks.ppu.mode7.unintrLeft);
bind(natural, "Hacks/PPU/Mode7/UnintrRight", hacks.ppu.mode7.unintrRight);
bind(natural, "Hacks/PPU/Mode7/Supersample", hacks.ppu.mode7.supersample);
bind(boolean, "Hacks/PPU/Mode7/Mosaic", hacks.ppu.mode7.mosaic);
bind(boolean, "Hacks/DSP/Fast", hacks.dsp.fast);
Expand Down
12 changes: 9 additions & 3 deletions bsnes/sfc/interface/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ struct Configuration {
bool noSpriteLimit = true;
struct Mode7 {
uint scale = 2;
bool perspective = true;
uint perspective = 1;
uint widescreen = 72;
uint wsbg1 = 1;
uint wsbg2 = 1;
uint wsbg3 = 1;
uint wsbg4 = 1;
bool wsobj = false;
bool igwin = false;
uint wsobj = 0;
uint igwin = 0;
uint igwinx = 128;
uint unintrMode = 1;
uint unintrTop = 10;
uint unintrBottom = 10;
uint unintrLeft = 20;
uint unintrRight = 20;
uint supersample = 1;
bool mosaic = false;
} mode7;
Expand Down
29 changes: 20 additions & 9 deletions bsnes/sfc/ppu-fast/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ auto PPUfast::Line::renderBackground(PPUfast::IO::Background& self, uint source)
if(self.tileMode == TileMode::Mode7) return renderMode7(self, source);
if(self.tileMode == TileMode::Inactive) return;

uint wsConf = ppufast.wsbg(source);
if(wsConf == 14) {
return;
}
bool autoCrop = false;
int ws = (int)ppufast.widescreen();
if(ws > 0) {
if(wsConf == 12) {
ws = -8;
} else if(wsConf == 13) {
ws = 0;
autoCrop = true;
} else if(wsConf == 0 || (wsConf != 1 && (((wsConf % 2) != 0) == (y < (((int)(wsConf / 2)) * 40))))) {
ws = 0;
}
}

array<bool[256]> windowAbove;
array<bool[256]> windowBelow;
renderWindow(self.window, self.window.aboveEnable, windowAbove);
Expand Down Expand Up @@ -37,14 +54,7 @@ auto PPUfast::Line::renderBackground(PPUfast::IO::Background& self, uint source)
uint mosaicPriority = 0;
uint mosaicColor = 0;

int ws = (int)ppufast.widescreen();
if(ws > 0) {
uint wsConf = ppufast.wsbg(source);
if(wsConf == 0 || (wsConf != 1 && (((wsConf % 2) != 0) == (y < (((int)(wsConf / 2)) * 40))))) {
ws = 0;
}
}

bool lastCropped = false;
int x = 0 - (hscroll & 7) - ws;
while(x < (width + ws)) {
uint hoffset = x + hscroll;
Expand Down Expand Up @@ -102,7 +112,8 @@ auto PPUfast::Line::renderBackground(PPUfast::IO::Background& self, uint source)
mosaicColor = cgram[paletteIndex + mosaicPalette];
}
}
if(!mosaicPalette) continue;
if(!mosaicPalette) { lastCropped = false; continue; };
if(autoCrop && (lastCropped || x < 8 || x > 255-8) && mosaicColor == 0) { lastCropped = true; continue; } else { lastCropped = false; }

if(!hires) {
if(self.aboveEnable && !windowAbove[ppufast.winXad(x, false)]) plotAbove(x, source, mosaicPriority, mosaicColor);
Expand Down
85 changes: 80 additions & 5 deletions bsnes/sfc/ppu-fast/line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ uint PPUfast::Line::start = 0;
uint PPUfast::Line::count = 0;

auto PPUfast::Line::flush() -> void {
if(ppufast.hdPerspective()) {
uint perspCorMode = ppufast.hdPerspective();
if(perspCorMode > 0) {
#define isLineMode7(l) (l.io.bg1.tileMode == TileMode::Mode7 \
&& !l.io.displayDisable && (l.io.bg1.aboveEnable || l.io.bg1.belowEnable))
ppufast.ind = 0;
bool state = false;
uint y;
int offsPart = 8;
if(perspCorMode == 2 || perspCorMode == 5) {
offsPart = 6;
} else if(perspCorMode == 3 || perspCorMode == 6) {
offsPart = 4;
}
for(y = 0; y < Line::count; y++) {
if(state != isLineMode7(ppufast.lines[Line::start + y])) {
state = !state;
if(state) {
ppufast.starts[ppufast.ind] = ppufast.lines[Line::start + y].y;
} else {
ppufast.ends[ppufast.ind] = ppufast.lines[Line::start + y].y - 1;
int offs = (ppufast.ends[ppufast.ind] - ppufast.starts[ppufast.ind]) / 8;
int offs = (ppufast.ends[ppufast.ind] - ppufast.starts[ppufast.ind]) / offsPart;
ppufast.startsp[ppufast.ind] = ppufast.starts[ppufast.ind] + offs;
ppufast.endsp[ppufast.ind] = ppufast.ends[ppufast.ind] - offs;
ppufast.ind++;
Expand All @@ -25,12 +32,79 @@ auto PPUfast::Line::flush() -> void {
#undef isLineMode7
if(state) {
ppufast.ends[ppufast.ind] = ppufast.lines[Line::start + y].y - 1;
int offs = (ppufast.ends[ppufast.ind] - ppufast.starts[ppufast.ind]) / 8;
int offs = (ppufast.ends[ppufast.ind] - ppufast.starts[ppufast.ind]) / offsPart;
ppufast.startsp[ppufast.ind] = ppufast.starts[ppufast.ind] + offs;
ppufast.endsp[ppufast.ind] = ppufast.ends[ppufast.ind] - offs;
ppufast.ind++;
}
}

if(perspCorMode < 4) {
for(int i = 0; i < ppufast.ind; i++) {
int la = -1;
int lb = -1;
int lc = -1;
int ld = -1;
bool abd= false;
bool bbd= false;
bool cbd= false;
bool dbd= false;
bool ab= false;
bool bb= false;
bool cb= false;
bool db= false;
for(y = ppufast.startsp[i]; y <= ppufast.endsp[i]; y++) {
int a = ((int)((int16)(ppufast.lines[y].io.mode7.a)));
int b = ((int)((int16)(ppufast.lines[y].io.mode7.b)));
int c = ((int)((int16)(ppufast.lines[y].io.mode7.c)));
int d = ((int)((int16)(ppufast.lines[y].io.mode7.d)));
if(la > 0 && a > 0 && a != la) {
if(!abd) {
abd = true;
ab = a > la;
} else if(ab != a > la) {
ppufast.startsp[i] = -1;
ppufast.endsp[i] = -1;
break;
}
}
if(lb > 0 && b > 0 && b != lb) {
if(!bbd) {
bbd = true;
bb = b > lb;
} else if(bb != b > lb) {
ppufast.startsp[i] = -1;
ppufast.endsp[i] = -1;
break;
}
}
if(lc > 0 && c > 0 && c != lc) {
if(!cbd) {
cbd = true;
cb = c > lc;
} else if(cb != c > lc) {
ppufast.startsp[i] = -1;
ppufast.endsp[i] = -1;
break;
}
}
if(ld > 0 && d > 0 && d != ld) {
if(!dbd) {
dbd = true;
db = d > ld;
} else if(db != d > ld) {
ppufast.startsp[i] = -1;
ppufast.endsp[i] = -1;
break;
}
}
la = a;
lb = b;
lc = c;
ld = d;
}
}
}
}

if(Line::count) {
#pragma omp parallel for if(Line::count >= 8)
Expand Down Expand Up @@ -70,10 +144,11 @@ auto PPUfast::Line::render() -> void {
}

renderBackground(io.bg1, Source::BG1);
renderBackground(io.bg2, Source::BG2);
if(!io.extbg) renderBackground(io.bg2, Source::BG2);
renderBackground(io.bg3, Source::BG3);
renderBackground(io.bg4, Source::BG4);
renderObject(io.obj);
if(io.extbg) renderBackground(io.bg2, Source::BG2);
renderWindow(io.col.window, io.col.window.aboveMask, windowAbove);
renderWindow(io.col.window, io.col.window.belowMask, windowBelow);

Expand Down
7 changes: 5 additions & 2 deletions bsnes/sfc/ppu-fast/mode7hd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ auto PPUfast::Line::renderMode7HD(PPUfast::IO::Background& self, uint source) ->
const uint sampScale = ppufast.hdSupersample();
const uint scale = ppufast.hdScale() * sampScale;

uint sampTmp[(256+2*ppufast.widescreen()) * 4 * scale/sampScale] = {};
int sampSize = sampScale < 2 ? 0 : (256+2*ppufast.widescreen()) * 4 * scale/sampScale;
uint *sampTmp = new uint[sampSize];
memory::fill<uint>(sampTmp, sampSize);

Pixel pixel;
Pixel* above = &this->above[0];
Expand All @@ -15,7 +17,7 @@ auto PPUfast::Line::renderMode7HD(PPUfast::IO::Background& self, uint source) ->
#define isLineMode7(n) (ppufast.lines[n].io.bg1.tileMode == TileMode::Mode7 && !ppufast.lines[n].io.displayDisable && ( \
(ppufast.lines[n].io.bg1.aboveEnable || ppufast.lines[n].io.bg1.belowEnable) \
))
if(ppufast.hdPerspective()) {
if(ppufast.hdPerspective() > 0) {
for(int i = 0; i < ppufast.ind; i++) {
if(y >= ppufast.starts[i] && y <= ppufast.ends[i]) {
y_a = ppufast.startsp[i];
Expand Down Expand Up @@ -145,6 +147,7 @@ auto PPUfast::Line::renderMode7HD(PPUfast::IO::Background& self, uint source) ->
}
}
}
delete[] sampTmp;
}

//interpolation and extrapolation
Expand Down
3 changes: 2 additions & 1 deletion bsnes/sfc/ppu-fast/object.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
auto PPUfast::Line::renderObject(PPUfast::IO::Object& self) -> void {
if(!self.aboveEnable && !self.belowEnable) return;
if(ppufast.wsobj() == 2) return;

array<bool[256]> windowAbove;
array<bool[256]> windowBelow;
Expand Down Expand Up @@ -28,7 +29,7 @@ auto PPUfast::Line::renderObject(PPUfast::IO::Object& self) -> void {
item.height = heights[self.baseSize];
}

if(!ppufast.wsobj()
if(ppufast.wsobj() == 0
&& object.x > 256 && object.x + item.width - 1 < 512) continue;
uint height = item.height >> self.interlace;
if((y >= object.y && y < object.y + height)
Expand Down
Loading

0 comments on commit 8533686

Please sign in to comment.