Skip to content

Commit

Permalink
port: implement motion blur and zoom blur
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Oct 16, 2023
1 parent ed3bd97 commit b78a5ee
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 36 deletions.
3 changes: 2 additions & 1 deletion port/fast3d/gfx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void gfx_set_target_fps(int);
void gfx_set_maximum_frame_latency(int latency);
void gfx_texture_cache_clear(void);
void gfx_texture_cache_delete(const uint8_t *orig_addr);
int gfx_create_framebuffer(uint32_t width, uint32_t height);
int gfx_create_framebuffer(uint32_t width, uint32_t height, int upscale, int autoresize);
void gfx_resize_framebuffer(int fb, uint32_t width, uint32_t height, int upscale, int autoresize);
void gfx_set_framebuffer(int fb, float noise_scale) ;
void gfx_reset_framebuffer(void);
void gfx_copy_framebuffer(int fb_dst, int fb_src, int left, int top);
Expand Down
13 changes: 8 additions & 5 deletions port/fast3d/gfx_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,16 +1139,19 @@ void gfx_opengl_copy_framebuffer(int fb_dst, int fb_src, int left, int top) {
srcY1 = src.height;
}

if (fb_src == 0) {
// flip the dst rect to mirror the image vertically
std::swap(dstY0, dstY1);
}

glDisable(GL_SCISSOR_TEST);

glBindFramebuffer(GL_READ_FRAMEBUFFER, src.fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst.fbo);

if (fb_src == 0) {
// flip the dst rect to mirror the image vertically
std::swap(dstY0, dstY1);
glReadBuffer(GL_FRONT);
} else {
glReadBuffer(GL_COLOR_ATTACHMENT0);
}

glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, GL_COLOR_BUFFER_BIT, GL_NEAREST);

glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[current_framebuffer].fbo);
Expand Down
132 changes: 116 additions & 16 deletions port/fast3d/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ static uintptr_t segmentPointers[16];
struct FBInfo {
uint32_t orig_width, orig_height;
uint32_t applied_width, applied_height;
bool upscale, autoresize;
};

static bool fbActive = 0;
Expand Down Expand Up @@ -2152,10 +2153,10 @@ static void gfx_dp_texture_rectangle(int32_t ulx, int32_t uly, int32_t lrx, int3
// lrx, lry, ulx, uly are U10.2
// lrs, lrt are S10.5

int16_t width = !flip ? lrx - ulx : lry - uly;
int16_t height = !flip ? lry - uly : lrx - ulx;
float lrs = ((uls << 7) + dsdx * width) >> 7;
float lrt = ((ult << 7) + dtdy * height) >> 7;
const int16_t width = flip ? lry - uly : lrx - ulx;
const int16_t height = flip ? lrx - ulx : lry - uly;
const float lrs = ((uls << 7) + dsdx * width) >> 7;
const float lrt = ((ult << 7) + dtdy * height) >> 7;

struct LoadedVertex* ul = &rsp.loaded_vertices[MAX_VERTICES + 0];
struct LoadedVertex* ll = &rsp.loaded_vertices[MAX_VERTICES + 1];
Expand Down Expand Up @@ -2193,6 +2194,53 @@ static void gfx_dp_texture_rectangle(int32_t ulx, int32_t uly, int32_t lrx, int3
rdp.combine_mode = saved_combine_mode;
}

static void gfx_dp_image_rectangle(int32_t tile, int32_t w, int32_t h,
int32_t ulx, int32_t uly, int16_t uls, int16_t ult,
int32_t lrx, int32_t lry, int16_t lrs, int16_t lrt) {
uint64_t saved_combine_mode = rdp.combine_mode;

struct LoadedVertex* ul = &rsp.loaded_vertices[MAX_VERTICES + 0];
struct LoadedVertex* ll = &rsp.loaded_vertices[MAX_VERTICES + 1];
struct LoadedVertex* lr = &rsp.loaded_vertices[MAX_VERTICES + 2];
struct LoadedVertex* ur = &rsp.loaded_vertices[MAX_VERTICES + 3];
ul->u = uls * 32;
ul->v = ult * 32;
lr->u = lrs * 32;
lr->v = lrt * 32;
ll->u = uls * 32;
ll->v = lrt * 32;
ur->u = lrs * 32;
ur->v = ult * 32;

// ensure we have the correct texture size
rdp.texture_tile[tile].line_size_bytes = w << rdp.texture_tile[tile].siz >> 1;
rdp.texture_tile[tile].width = w;
rdp.texture_tile[tile].height = h;
rdp.texture_tile[tile].cms = 0;
rdp.texture_tile[tile].cmt = 0;
rdp.texture_tile[tile].shifts = 0;
rdp.texture_tile[tile].shiftt = 0;
auto& loadtex = rdp.loaded_texture[rdp.texture_tile[tile].tmem];
loadtex.full_image_line_size_bytes = loadtex.line_size_bytes = rdp.texture_tile[tile].line_size_bytes;
loadtex.size_bytes = loadtex.orig_size_bytes = loadtex.full_size_bytes = loadtex.line_size_bytes * h;

uint8_t saved_tile = rdp.first_tile_index;
if (saved_tile != tile) {
rdp.textures_changed[0] = true;
rdp.textures_changed[1] = true;
}
rdp.first_tile_index = tile;

gfx_draw_rectangle(ulx, uly, lrx, lry);
if (saved_tile != tile) {
rdp.textures_changed[0] = true;
rdp.textures_changed[1] = true;
}
rdp.first_tile_index = saved_tile;

rdp.combine_mode = saved_combine_mode;
}

static void gfx_dp_fill_rectangle(int32_t ulx, int32_t uly, int32_t lrx, int32_t lry) {
if (rdp.color_image_address == rdp.z_buf_address) {
// Don't clear Z buffer here since we already did it with glClear
Expand Down Expand Up @@ -2529,9 +2577,11 @@ static void gfx_run_dl(Gfx* cmd) {
case G_TEXRECT_WIDE_EXT: {
int32_t lrx, lry, tile, ulx, uly;
uint32_t uls, ult, dsdx, dtdy;
bool flip;
lrx = (int32_t)((C0(0, 24) << 8)) >> 8;
lry = (int32_t)((C1(0, 24) << 8)) >> 8;
tile = C1(24, 3);
flip = C1(27, 1);
++cmd;
ulx = (int32_t)((C0(0, 24) << 8)) >> 8;
uly = (int32_t)((C1(0, 24) << 8)) >> 8;
Expand All @@ -2540,7 +2590,27 @@ static void gfx_run_dl(Gfx* cmd) {
ult = C0(0, 16);
dsdx = C1(16, 16);
dtdy = C1(0, 16);
gfx_dp_texture_rectangle(ulx, uly, lrx, lry, tile, uls, ult, dsdx, dtdy, opcode == G_TEXRECTFLIP);
gfx_dp_texture_rectangle(ulx, uly, lrx, lry, tile, uls, ult, dsdx, dtdy, flip);
break;
}
case G_IMAGERECT_EXT: {
int16_t tile, iw, ih;
int16_t x0, y0, s0, t0;
int16_t x1, y1, s1, t1;
tile = C0(0, 3);
iw = C1(16, 16);
ih = C1(0, 16);
++cmd;
x0 = C0(16, 16);
y0 = C0(0, 16);
s0 = C1(16, 16);
t0 = C1(0, 16);
++cmd;
x1 = C0(16, 16);
y1 = C0(0, 16);
s1 = C1(16, 16);
t1 = C1(0, 16);
gfx_dp_image_rectangle(tile, iw, ih, x0, y0, s0, t0, x1, y1, s1, t1);
break;
}
case G_SETSCISSOR:
Expand Down Expand Up @@ -2665,12 +2735,22 @@ extern "C" void gfx_start_frame(void) {

if (gfx_current_dimensions.height != gfx_prev_dimensions.height) {
for (auto& fb : framebuffers) {
uint32_t width = fb.second.orig_width, height = fb.second.orig_height;
gfx_adjust_width_height_for_scale(width, height);
if (width != fb.second.applied_width || height != fb.second.applied_height) {
gfx_rapi->update_framebuffer_parameters(fb.first, width, height, 1, true, true, true, true);
fb.second.applied_width = width;
fb.second.applied_height = height;
uint32_t width, height, msaa;
if (fb.second.autoresize) {
if (fb.second.upscale) {
width = fb.second.orig_width;
height = fb.second.orig_height;
gfx_adjust_width_height_for_scale(width, height);
} else {
// assume this is a fullscreen fb
width = gfx_current_dimensions.width;
height = gfx_current_dimensions.height;
}
if (width != fb.second.applied_width || height != fb.second.applied_height) {
gfx_rapi->update_framebuffer_parameters(fb.first, width, height, 1, true, true, true, true);
fb.second.applied_width = width;
fb.second.applied_height = height;
}
}
}
}
Expand Down Expand Up @@ -2768,15 +2848,35 @@ extern "C" void gfx_set_target_fps(int fps) {
gfx_wapi->set_target_fps(fps);
}

extern "C" int gfx_create_framebuffer(uint32_t width, uint32_t height) {
uint32_t orig_width = width, orig_height = height;
gfx_adjust_width_height_for_scale(width, height);
extern "C" int gfx_create_framebuffer(uint32_t width, uint32_t height, int upscale, int autoresize) {
int fb = gfx_rapi->create_framebuffer();
gfx_rapi->update_framebuffer_parameters(fb, width, height, 1, true, true, true, true);
framebuffers[fb] = { orig_width, orig_height, width, height };
gfx_resize_framebuffer(fb, width, height, upscale, autoresize);
return fb;
}

extern "C" void gfx_resize_framebuffer(int fb, uint32_t width, uint32_t height, int upscale, int autoresize) {
uint32_t orig_width, orig_height;

if (width && height) {
// user-specified size
orig_width = width;
orig_height = height;
if (upscale) {
gfx_adjust_width_height_for_scale(width, height);
}
gfx_rapi->update_framebuffer_parameters(fb, width, height, 1, true, true, true, true);
} else {
// same size as main fb
orig_width = width = gfx_current_dimensions.width;
orig_height = height = gfx_current_dimensions.height;
upscale = false;
autoresize = true;
gfx_rapi->update_framebuffer_parameters(fb, width, height, 1, true, true, true, true);
}

framebuffers[fb] = { orig_width, orig_height, width, height, (bool)upscale, (bool)autoresize };
}

extern "C" void gfx_set_framebuffer(int fb, float noise_scale) {
gfx_rapi->start_draw_to_framebuffer(fb, noise_scale);
gfx_rapi->clear_framebuffer();
Expand Down
3 changes: 2 additions & 1 deletion port/include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ u32 videoGetTextureFilter2D(void);

void videoSetWindowOffset(s32 x, s32 y);

s32 videoCreateFramebuffer(u32 w, u32 h);
s32 videoCreateFramebuffer(u32 w, u32 h, s32 upscale, s32 autoresize);
void videoSetFramebuffer(s32 target);
void videoResetFramebuffer(void);
void videoCopyFramebuffer(s32 dst, s32 src, s32 left, s32 top);
void videoResizeFramebuffer(s32 target, u32 w, u32 h, s32 upscale, s32 autoresize);

void videoResetTextureCache(void);
void videoFreeCachedTexture(const void *texptr);
Expand Down
14 changes: 14 additions & 0 deletions port/src/pdsched.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ u32 var8005cea4 = 0;
OSScMsg g_SchedRspMsg = {OS_SC_RSP_MSG};
bool g_SchedIsFirstTask = true;

s32 g_PrevFrameFb = -1;
s32 g_PrevFrameCapTimer = -1;

void schedSetCrashEnable1(bool enable)
{
g_SchedCrashEnable1 = enable;
Expand Down Expand Up @@ -380,6 +383,17 @@ void schedConsiderScreenshot(void)
g_MenuData.screenshottimer = 0;
}

if (g_PrevFrameFb < 0) {
g_PrevFrameFb = videoCreateFramebuffer(0, 0, false, true);
}

if (g_PrevFrameCapTimer == 0) {
videoCopyFramebuffer(g_PrevFrameFb, 0, -1, -1);
g_PrevFrameCapTimer = -1;
} else if (g_PrevFrameCapTimer > 0) {
--g_PrevFrameCapTimer;
}

if (g_MenuData.screenshottimer >= 2) {
g_MenuData.screenshottimer--;
}
Expand Down
9 changes: 7 additions & 2 deletions port/src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ void videoSetWindowOffset(s32 x, s32 y)
gfx_current_game_window_viewport.y = y;
}

s32 videoCreateFramebuffer(u32 w, u32 h)
s32 videoCreateFramebuffer(u32 w, u32 h, s32 upscale, s32 autoresize)
{
return gfx_create_framebuffer(w, h);
return gfx_create_framebuffer(w, h, upscale, autoresize);
}

void videoSetFramebuffer(s32 target)
Expand All @@ -163,6 +163,11 @@ void videoResetFramebuffer(void)
return gfx_reset_framebuffer();
}

void videoResizeFramebuffer(s32 target, u32 w, u32 h, s32 upscale, s32 autoresize)
{
gfx_resize_framebuffer(target, w, h, upscale, autoresize);
}

void videoCopyFramebuffer(s32 dst, s32 src, s32 left, s32 top)
{
gfx_copy_framebuffer(dst, src, left, top);
Expand Down
29 changes: 29 additions & 0 deletions src/game/bondview.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "data.h"
#include "types.h"
#include "gbiex.h"
#ifndef PLATFORM_N64
#include "video.h"
#endif

#ifdef AVOID_UB
char var800a41c0[26];
Expand Down Expand Up @@ -291,10 +294,19 @@ Gfx *bviewDrawMotionBlur(Gfx *gdl, u32 colour, u32 alpha)
somefloat = (viewheight - viewheight / fyyy) * 0.5f;
gdl = bviewPrepareStaticRgba16(gdl, colour, newalpha);

#ifdef PLATFORM_N64
for (i = viewtop; i < viewtop + viewheight; i++) {
gdl = bviewCopyPixels(gdl, fb, viewtop + (s32)somefloat, 5, i, fxxx, viewleft, viewwidth);
somefloat += 1.0f / fyyy;
}
#else
gDPSetFramebufferTextureEXT(gdl++, 0, 0, 0, g_PrevFrameFb);
gSPImageRectangleEXT(gdl++,
viewleft << 2, viewtop << 2, viewleft, viewtop,
(viewleft + viewwidth) << 2, (viewtop + viewheight) << 2, viewleft + viewwidth, viewtop + viewheight,
0, videoGetNativeWidth(), videoGetNativeHeight());
g_PrevFrameCapTimer = 0;
#endif

return gdl;
}
Expand Down Expand Up @@ -506,10 +518,27 @@ Gfx *bviewDrawZoomBlur(Gfx *gdl, u32 colour, s32 alpha, f32 arg3, f32 arg4)

gdl = bviewPrepareStaticRgba16(gdl, colour, alpha);

#ifdef PLATFORM_N64
for (i = viewtop; i < viewtop + viewheight; i++) {
gdl = bviewCopyPixels(gdl, fb, (s32)somefloat + viewtop, 5, i, arg3, viewleft, viewwidth);
somefloat += 1.0f / arg4;
}
#else
const f32 xcenter = viewleft + viewwidth * 0.5f;
const f32 ycenter = viewtop + viewheight * 0.5f;
const f32 halfw = viewwidth * 0.5f * arg3;
const f32 halfh = viewheight * 0.5f * arg4;
const s32 left = xcenter - halfw;
const s32 top = ycenter - halfh;
const s32 right = xcenter + halfw;
const s32 bottom = ycenter + halfh;
gDPSetFramebufferTextureEXT(gdl++, 0, 0, 0, g_PrevFrameFb);
gSPImageRectangleEXT(gdl++,
left << 2, top << 2, viewleft, viewtop,
right << 2, bottom << 2, viewleft + viewwidth, viewtop + viewheight,
0, videoGetNativeWidth(), videoGetNativeHeight());
g_PrevFrameCapTimer = 0;
#endif

return gdl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/chrmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void chrmgrReset(void)
var8009ccc0[i] = (void *)ALIGN64(mempAlloc(16 * 16 * sizeof(u16) + 0x40, MEMPOOL_STAGE));
#else
if (!var8009ccc0[i]) {
var8009ccc0[i] = videoCreateFramebuffer(16, 16);
var8009ccc0[i] = videoCreateFramebuffer(16, 16, false, false);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/menugfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void menugfxCreateBlur(void)
g_ScaleX = 1;
#else
if (g_MenuBlurFb == 0) {
g_MenuBlurFb = videoCreateFramebuffer(BLURIMG_WIDTH, BLURIMG_HEIGHT);
g_MenuBlurFb = videoCreateFramebuffer(BLURIMG_WIDTH, BLURIMG_HEIGHT, true, false);
}
// copy full viewport and downscale to 40x30
videoCopyFramebuffer(g_MenuBlurFb, 0, -1, -1);
Expand Down
2 changes: 2 additions & 0 deletions src/include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ extern f32 g_ViShakeIntensityMult;
extern u32 g_TexFilter2D;
extern u32 g_HudAlignModeL;
extern u32 g_HudAlignModeR;
extern s32 g_PrevFrameFb;
extern s32 g_PrevFrameCapTimer;

#define TEX_FILTER_2D g_TexFilter2D
#define ADJUST_ZOOM_FOV(x) ((x) * g_PlayerFovZoomMultiplier)
Expand Down
Loading

0 comments on commit b78a5ee

Please sign in to comment.