Skip to content

Commit

Permalink
refactor: clamping, update graphics functions
Browse files Browse the repository at this point in the history
- Updated various functions to use `std::max` and `std::min` for clamping values.
- Removed `#ifdef PICASSO96` block in `updatepicasso96`.
- Changed surface format in `setupcursor` to `SDL_PIXELFORMAT_BGRA32`.
  • Loading branch information
midwan committed Jan 5, 2025
1 parent 1800911 commit 0c182d2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 79 deletions.
25 changes: 10 additions & 15 deletions src/gfxutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "gfxfilter.h"
#include "machdep/maccess.h"

#include <math.h>
#include <cmath>

#include <algorithm>

float getvsyncrate(int monid, float hz, int *mult)
{
Expand Down Expand Up @@ -128,8 +130,7 @@ static float video_gamma (float value, float gamma, float bri, float con)
factor = (float)pow(255.0f, 1.0f - gamma);
ret = (float)(factor * pow(value, gamma));

if (ret < 0.0f)
ret = 0.0f;
ret = std::max(ret, 0.0f);

return ret;
}
Expand Down Expand Up @@ -181,10 +182,8 @@ static void video_calc_gammatable(int monid)
v = video_gamma(val, gams[j], bri, con);
}

if (v < 0.0)
v = 0.0;
if (v > max)
v = max;
v = std::max<double>(v, 0.0);
v = std::min(v, max);

uae_gamma[i][j] = (uae_u32)(v + 0.5);
}
Expand All @@ -197,10 +196,8 @@ static uae_u32 limit256(int monid, float v)
if (!gfx_hdr) {
v = v * (float)(currprefs.gf[ad->gf_index].gfx_filter_contrast + 1000) / 1000.0f + currprefs.gf[ad->gf_index].gfx_filter_luminance / 10.0f;
}
if (v < 0)
v = 0;
if (v > 255)
v = 255;
v = std::max<float>(v, 0);
v = std::min<float>(v, 255);
return (uae_u32)v;
}
static uae_s32 limit256rb(int monid, float v)
Expand All @@ -209,10 +206,8 @@ static uae_s32 limit256rb(int monid, float v)
if (!gfx_hdr) {
v *= (float)(currprefs.gf[ad->gf_index].gfx_filter_saturation + 1000) / 1000.0f;
}
if (v < -128)
v = -128;
if (v > 127)
v = 127;
v = std::max<float>(v, -128);
v = std::min<float>(v, 127);
return (uae_s32)v;
}
static float get_y(int r, int g, int b)
Expand Down
20 changes: 11 additions & 9 deletions src/osdep/amiberry_gfx.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
Expand Down Expand Up @@ -455,8 +456,7 @@ static struct MultiDisplay* getdisplay2(struct uae_prefs* p, int index)
return nullptr;
if (display >= max)
display = 0;
if (display < 0)
display = 0;
display = std::max(display, 0);
return &Displays[display];
}

Expand Down Expand Up @@ -800,8 +800,9 @@ static void modesList(struct MultiDisplay* md)

void reenumeratemonitors(void)
{
for (int i = 0; i < MAX_DISPLAYS; i++) {
struct MultiDisplay* md = &Displays[i];
for (auto& Display : Displays)
{
struct MultiDisplay* md = &Display;
memcpy(&md->workrect, &md->rect, sizeof(SDL_Rect));
}
enumeratedisplays();
Expand Down Expand Up @@ -872,8 +873,7 @@ void sortdisplays()
{
struct PicassoResolution* pr = &md->DisplayModes[idx2];
if (dm.w == w && dm.h == h && SDL_BITSPERPIXEL(dm.format) == b) {
if (dm.refresh_rate > deskhz)
deskhz = dm.refresh_rate;
deskhz = std::max(dm.refresh_rate, deskhz);
}
if (pr->res.width == dm.w && pr->res.height == dm.h && pr->depth == SDL_BITSPERPIXEL(dm.format) / 8) {
for (i = 0; pr->refresh[i]; i++) {
Expand Down Expand Up @@ -2228,7 +2228,6 @@ void gfx_set_picasso_state(int monid, int on)

static void updatepicasso96(struct AmigaMonitor* mon)
{
#ifdef PICASSO96
struct picasso_vidbuf_description* vidinfo = &picasso_vidinfo[mon->monitor_id];
vidinfo->rowbytes = 0;
vidinfo->pixbytes = amiga_surface->format->BytesPerPixel;
Expand All @@ -2239,7 +2238,6 @@ static void updatepicasso96(struct AmigaMonitor* mon)
vidinfo->depth = amiga_surface->format->BytesPerPixel * 8;
vidinfo->offset = 0;
vidinfo->splitypos = -1;
#endif
}

void gfx_set_picasso_modeinfo(int monid, RGBFTYPE rgbfmt)
Expand Down Expand Up @@ -2437,7 +2435,11 @@ bool target_graphics_buffer_update(int monid, bool force)
if (mon->screen_is_picasso) {
w = state->Width;
h = state->Height;
depth = state->RGBFormat == RGBFB_R5G6B5 || state->RGBFormat == RGBFB_R5G6B5PC || state->RGBFormat == RGBFB_CLUT ? 16 : 32;
depth = state->RGBFormat == RGBFB_R5G6B5
|| state->RGBFormat == RGBFB_R5G6B5PC
|| state->RGBFormat == RGBFB_B5G6R5PC
|| state->RGBFormat == RGBFB_CLUT
? 16 : 32;
} else {
struct vidbuffer* vb = avidinfo->drawbuffer.tempbufferinuse ? &avidinfo->tempbuffer : &avidinfo->drawbuffer;
avidinfo->outbuffer = vb;
Expand Down
80 changes: 27 additions & 53 deletions src/osdep/picasso96.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "sysconfig.h"
#include "sysdeps.h"

#include <algorithm>
#include <cstdlib>

#include "uae.h"
Expand Down Expand Up @@ -603,8 +604,7 @@ static int CopyBitMapStructureA2U(TrapContext *ctx, uaecptr amigamemptr, struct
bm->Depth = md[3].params[0];

/* ARGH - why is THIS happening? */
if(bm->Depth > 8)
bm->Depth = 8;
bm->Depth = std::min<int>(bm->Depth, 8);

for (int i = 0; i < bm->Depth; i++) {
const uaecptr plane = md[4 + i].params[0];
Expand Down Expand Up @@ -828,7 +828,7 @@ static void setupcursor()
}
}

auto* p96_formatted_cursor_surface = SDL_ConvertSurfaceFormat(p96_cursor_surface, SDL_PIXELFORMAT_RGBA32, 0);
auto* p96_formatted_cursor_surface = SDL_ConvertSurfaceFormat(p96_cursor_surface, SDL_PIXELFORMAT_BGRA32, 0);
if (p96_formatted_cursor_surface != nullptr) {
SDL_FreeSurface(p96_cursor_surface);
if (p96_cursor != nullptr) {
Expand Down Expand Up @@ -985,8 +985,7 @@ static void rtg_render()
picasso_flushpixels(0, gfxmem_banks[uaegfx_index]->start + natmem_offset, static_cast<int>(state->XYOffset - gfxmem_banks[uaegfx_index]->start), true);
}
} else {
if (vidinfo->full_refresh < 0)
vidinfo->full_refresh = 0;
vidinfo->full_refresh = std::max(vidinfo->full_refresh, 0);
if (vidinfo->full_refresh > 0)
vidinfo->full_refresh--;
}
Expand Down Expand Up @@ -1176,10 +1175,8 @@ static void setconvert(int monid)
vidinfo->host_mode = picasso_vidinfo[monid].pixbytes == 4 ? RGBFB_B8G8R8A8 : RGBFB_B5G6R5PC;
if (picasso_vidinfo[monid].pixbytes == 4)
alloc_colors_rgb(8, 8, 8, 16, 8, 0, 0, 0, 0, 0, p96rc, p96gc, p96bc); // BGRA
//alloc_colors_rgb(8, 8, 8, 0, 8, 16, 0, 0, 0, 0, p96rc, p96gc, p96bc); // RGBA
else
alloc_colors_rgb(5, 6, 5, 11, 5, 0, 0, 0, 0, 0, p96rc, p96gc, p96bc); // BGR
//alloc_colors_rgb(5, 6, 5, 0, 5, 11, 0, 0, 0, 0, p96rc, p96gc, p96bc); // RGB
gfx_set_picasso_colors(monid, state->RGBFormat);
picasso_palette(state->CLUT, vidinfo->clut);
if (vidinfo->host_mode != vidinfo->ohost_mode || state->RGBFormat != vidinfo->orgbformat) {
Expand Down Expand Up @@ -2038,9 +2035,7 @@ static int createwindowscursor(int monid, int set, int chipset)
int bits;
int maxbits = w - x;

if (maxbits > 16 * hiressprite) {
maxbits = 16 * hiressprite;
}
maxbits = std::min(maxbits, 16 * hiressprite);
for (bits = 0; bits < maxbits && x < w; bits++) {
uae_u8 c = ((d2 & 0x80000000) ? 2 : 0) + ((d1 & 0x80000000) ? 1 : 0);
d1 <<= 1;
Expand Down Expand Up @@ -2401,8 +2396,7 @@ static uae_u32 setspriteimage(TrapContext *ctx, uaecptr bi)
uae_u32 d1 = trap_get_long(ctx, img);
uae_u32 d2 = trap_get_long(ctx, img + 2 * hiressprite);
int maxbits = w - x;
if (maxbits > 16 * hiressprite)
maxbits = 16 * hiressprite;
maxbits = std::min(maxbits, 16 * hiressprite);
for (bits = 0; bits < maxbits && x < w; bits++) {
const uae_u8 c = ((d2 & 0x80000000) ? 2 : 0) + ((d1 & 0x80000000) ? 1 : 0);
d1 <<= 1;
Expand Down Expand Up @@ -2935,28 +2929,20 @@ static void picasso96_alloc2 (TrapContext *ctx)
for (depth = 1; depth <= 4; depth++) {
switch (depth) {
case 1:
if (newmodes[i].res.width > chunky.width)
chunky.width = newmodes[i].res.width;
if (newmodes[i].res.height > chunky.height)
chunky.height = newmodes[i].res.height;
chunky.width = std::max(newmodes[i].res.width, chunky.width);
chunky.height = std::max(newmodes[i].res.height, chunky.height);
break;
case 2:
if (newmodes[i].res.width > hicolour.width)
hicolour.width = newmodes[i].res.width;
if (newmodes[i].res.height > hicolour.height)
hicolour.height = newmodes[i].res.height;
hicolour.width = std::max(newmodes[i].res.width, hicolour.width);
hicolour.height = std::max(newmodes[i].res.height, hicolour.height);
break;
case 3:
if (newmodes[i].res.width > truecolour.width)
truecolour.width = newmodes[i].res.width;
if (newmodes[i].res.height > truecolour.height)
truecolour.height = newmodes[i].res.height;
truecolour.width = std::max(newmodes[i].res.width, truecolour.width);
truecolour.height = std::max(newmodes[i].res.height, truecolour.height);
break;
case 4:
if (newmodes[i].res.width > alphacolour.width)
alphacolour.width = newmodes[i].res.width;
if (newmodes[i].res.height > alphacolour.height)
alphacolour.height = newmodes[i].res.height;
alphacolour.width = std::max(newmodes[i].res.width, alphacolour.width);
alphacolour.height = std::max(newmodes[i].res.height, alphacolour.height);
break;
default: // never
break;
Expand Down Expand Up @@ -4064,8 +4050,7 @@ static uae_u32 REGPARAM2 picasso_BlitPattern(TrapContext *ctx)
int max = static_cast<int>(W) - cols;
uae_u32 data = d;

if (max > 16)
max = 16;
max = std::min(max, 16);

switch (pattern.DrawMode)
{
Expand Down Expand Up @@ -4233,8 +4218,7 @@ static uae_u32 REGPARAM2 picasso_BlitTemplate(TrapContext *ctx)
uae_u32 byte;
int max = W - cols;

if (max > 8)
max = 8;
max = std::min(max, 8);

data <<= 8;
data |= *++tmpl_mem;
Expand Down Expand Up @@ -4368,8 +4352,7 @@ void init_hz_p96(int monid)
}
if (p96vblank <= 0)
p96vblank = 60;
if (p96vblank >= 300)
p96vblank = 300;
p96vblank = std::min<float>(p96vblank, 300);
p96syncrate = static_cast<int>(static_cast<float>(maxvpos_nom) * vblank_hz / p96vblank);
write_log(_T("RTGFREQ: %d*%.4f = %.4f / %.1f = %d\n"), maxvpos_nom, vblank_hz, static_cast<float>(maxvpos_nom) * vblank_hz, p96vblank, p96syncrate);
}
Expand Down Expand Up @@ -4906,11 +4889,9 @@ void picasso_statusline(int monid, uae_u8 *dst)
int dst_height, dst_width, pitch;

dst_height = state->Height;
if (dst_height > vidinfo->height)
dst_height = vidinfo->height;
dst_height = std::min(dst_height, vidinfo->height);
dst_width = state->Width;
if (dst_width > vidinfo->width)
dst_width = vidinfo->width;
dst_width = std::min(dst_width, vidinfo->width);
pitch = vidinfo->rowbytes;
statusline_getpos(monid, &slx, &sly, state->Width, dst_height);
statusline_render(monid, dst + sly * pitch, vidinfo->pixbytes, pitch, dst_width, dst_height, p96rc, p96gc, p96bc, nullptr);
Expand Down Expand Up @@ -5223,18 +5204,12 @@ static uae_u16 yuvtorgb(uae_u8 yx, uae_u8 ux, uae_u8 vx)
int r = (298 * y + 409 * v + 128) >> (8 + 3);
int g = (298 * y - 100 * u - 208 * v + 128) >> (8 + 3);
int b = (298 * y + 516 * u + 128) >> (8 + 3);
if (r < 0)
r = 0;
if (r > 31)
r = 31;
if (g < 0)
g = 0;
if (g > 31)
g = 31;
if (b < 0)
b = 0;
if (b > 31)
b = 31;
r = std::max(r, 0);
r = std::min(r, 31);
g = std::max(g, 0);
g = std::min(g, 31);
b = std::max(b, 0);
b = std::min(b, 31);
return (r << 10) | (g << 5) | b;
}

Expand Down Expand Up @@ -5831,9 +5806,8 @@ uae_u8 *uaegfx_getrtgbuffer(const int monid, int *widthp, int *heightp, int *pit
return nullptr;
convert[0] = getconvert (state->RGBFormat, pixbytes);
convert[1] = convert[0];
//alloc_colors_picasso(8, 8, 8, 16, 8, 0, state->RGBFormat, p96_rgbx16); // BGR
alloc_colors_picasso(8, 8, 8, 0, 8, 16, state->RGBFormat, p96_rgbx16); // RGB

alloc_colors_picasso(8, 8, 8, 16, 8, 0, state->RGBFormat, p96_rgbx16); // BGR

copyall (monid, src + off, dst, width, height, state->BytesPerRow, state->BytesPerPixel, width * pixbytes, pixbytes, convert);
if (pixbytes == 1) {
for (int i = 0; i < 256; i++) {
Expand Down
2 changes: 0 additions & 2 deletions src/statusline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <cctype>
#include <cassert>

#include <algorithm>

#include "options.h"
#include "uae.h"
#include "xwin.h"
Expand Down

0 comments on commit 0c182d2

Please sign in to comment.