Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtk: implement GPU scale factor feature #764

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions desmume/src/frontend/posix/gtk/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ void value<int>::save() {
g_key_file_set_integer(this->mKeyFile, this->mSection.c_str(), this->mKey.c_str(), this->mData);
}

/*class value<float> */

template<>
void value<float>::load() {
GError* err = NULL;
float val = g_key_file_get_double(this->mKeyFile, this->mSection.c_str(), this->mKey.c_str(), &err);
if (err != NULL) {
g_error_free(err);
} else {
this->mData = val;
}
}

template<>
void value<float>::save() {
g_key_file_set_double(this->mKeyFile, this->mSection.c_str(), this->mKey.c_str(), this->mData);
}

/* class value<string> */

template<>
Expand Down
2 changes: 1 addition & 1 deletion desmume/src/frontend/posix/gtk/config_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ OPT(core3D, int, 1, Config, Core3D)
OPT(textureDeposterize, bool, false, Config, 3DTextureDeposterization)
OPT(textureSmoothing, bool, false, Config, 3DTextureSmoothing)
OPT(textureUpscale, int, 1, Config, 3DTextureUpscaling)
OPT(gpuScaleFactor, int, 1, Config, GPUScaleFactor)
OPT(gpuScaleFactor, float, 1.0, Config, GPUScaleFactor)
OPT(highColorInterpolation, bool, true, Config, HighResolutionColorInterpolation)
OPT(multisampling, bool, false, Config, OpenGLMultisampling)
OPT(multisamplingSize, int, 0, Config, OpenGLMultisamplingSize)
Expand Down
22 changes: 3 additions & 19 deletions desmume/src/frontend/posix/gtk/graphics.ui
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,9 @@
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="gpuscale">
<items>
<item translatable="yes">×1</item>
<item translatable="yes">×2</item>
<item translatable="yes">×3</item>
<item translatable="yes">×4</item>
<item translatable="yes">×5</item>
<item translatable="yes">×6</item>
<item translatable="yes">×7</item>
<item translatable="yes">×8</item>
<item translatable="yes">×9</item>
<item translatable="yes">×10</item>
<item translatable="yes">×11</item>
<item translatable="yes">×12</item>
<item translatable="yes">×13</item>
<item translatable="yes">×14</item>
<item translatable="yes">×15</item>
<item translatable="yes">×16</item>
</items>
<object class="GtkSpinButton" id="gpuscale">
<property name="numeric">true</property>
<property name="digits">1</property>
</object>
<packing>
<property name="left_attach">1</property>
Expand Down
58 changes: 40 additions & 18 deletions desmume/src/frontend/posix/gtk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@
static int draw_count;
extern int _scanline_filter_a, _scanline_filter_b, _scanline_filter_c, _scanline_filter_d;
VideoFilter* video;
int gpu_scale_factor = 1;

#define GPU_SCALE_FACTOR_MIN 1.0f
#define GPU_SCALE_FACTOR_MAX 10.0f

float gpu_scale_factor = 1.0f;
int real_framebuffer_width = GPU_FRAMEBUFFER_NATIVE_WIDTH;
int real_framebuffer_height = GPU_FRAMEBUFFER_NATIVE_HEIGHT;


desmume::config::Config config;

Expand Down Expand Up @@ -1312,7 +1319,7 @@ static int ConfigureDrawingArea(GtkWidget *widget, GdkEventConfigure *event, gpo
static inline void gpu_screen_to_rgb(u32* dst)
{
ColorspaceConvertBuffer555To8888Opaque<false, false, BESwapDst>(GPU->GetDisplayInfo().masterNativeBuffer16, dst,
GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor * gpu_scale_factor);
real_framebuffer_width * real_framebuffer_height * 2);
}

static inline void drawScreen(cairo_t* cr, u32* buf, gint w, gint h) {
Expand Down Expand Up @@ -1439,9 +1446,9 @@ static gboolean ExposeDrawingArea (GtkWidget *widget, GdkEventExpose *event, gpo

static void RedrawScreen() {
ColorspaceConvertBuffer555To8888Opaque<true, false, BESwapDst>(GPU->GetDisplayInfo().masterNativeBuffer16, (uint32_t *)video->GetSrcBufferPtr(),
GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor * gpu_scale_factor);
real_framebuffer_width * real_framebuffer_height * 2);
#ifdef HAVE_LIBAGG
aggDraw.hud->attach((u8*)video->GetSrcBufferPtr(), GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor, 1024);
aggDraw.hud->attach((u8*)video->GetSrcBufferPtr(), real_framebuffer_width, real_framebuffer_height * 2, 1024);
osd->update();
DrawHUD();
osd->clear();
Expand Down Expand Up @@ -1990,15 +1997,18 @@ static void Edit_Joystick_Controls(GSimpleAction *action, GVariant *parameter, g
static void GraphicsSettingsDialog(GSimpleAction *action, GVariant *parameter, gpointer user_data) {
GtkDialog *dialog;
GtkGrid *wGrid;
GtkComboBox *coreCombo, *wScale, *wGPUScale, *wMultisample;
GtkComboBox *coreCombo, *wScale, *wMultisample;
GtkToggleButton *wPosterize, *wSmoothing, *wHCInterpolate;
GtkSpinButton *wGPUScale;

GtkBuilder *builder = gtk_builder_new_from_resource("/org/desmume/DeSmuME/graphics.ui");
dialog = GTK_DIALOG(gtk_builder_get_object(builder, "dialog"));
wGrid = GTK_GRID(gtk_builder_get_object(builder, "graphics_grid"));
coreCombo = GTK_COMBO_BOX(gtk_builder_get_object(builder, "core_combo"));
wScale = GTK_COMBO_BOX(gtk_builder_get_object(builder, "scale"));
wGPUScale = GTK_COMBO_BOX(gtk_builder_get_object(builder, "gpuscale"));
wGPUScale = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "gpuscale"));
gtk_spin_button_set_range(wGPUScale, GPU_SCALE_FACTOR_MIN, GPU_SCALE_FACTOR_MAX);
gtk_spin_button_set_increments(wGPUScale, 0.1, 1.0);
wMultisample = GTK_COMBO_BOX(gtk_builder_get_object(builder, "multisample"));
wPosterize = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "posterize"));
wSmoothing = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "smoothing"));
Expand All @@ -2015,7 +2025,7 @@ static void GraphicsSettingsDialog(GSimpleAction *action, GVariant *parameter, g
gtk_combo_box_set_active(wScale, CommonSettings.GFX3D_Renderer_TextureScalingFactor >> 1);

//GPU scaling factor
gtk_combo_box_set_active(wGPUScale, gpu_scale_factor-1);
gtk_spin_button_set_value(wGPUScale, gpu_scale_factor);

// 3D Texture Deposterization
gtk_toggle_button_set_active(wPosterize, CommonSettings.GFX3D_Renderer_TextureDeposterize);
Expand Down Expand Up @@ -2087,10 +2097,16 @@ static void GraphicsSettingsDialog(GSimpleAction *action, GVariant *parameter, g
default:
break;
}
gpu_scale_factor = gtk_combo_box_get_active(wGPUScale)+1;
gpu_scale_factor = gtk_spin_button_get_value(wGPUScale);
if(gpu_scale_factor < GPU_SCALE_FACTOR_MIN)
gpu_scale_factor = GPU_SCALE_FACTOR_MIN;
if(gpu_scale_factor > GPU_SCALE_FACTOR_MAX)
gpu_scale_factor = GPU_SCALE_FACTOR_MAX;
rofl0r marked this conversation as resolved.
Show resolved Hide resolved
config.gpuScaleFactor = gpu_scale_factor;
GPU->SetCustomFramebufferSize(GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * gpu_scale_factor);
video->SetSourceSize(GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor);
real_framebuffer_width = GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor;
real_framebuffer_height = GPU_FRAMEBUFFER_NATIVE_HEIGHT * gpu_scale_factor;
GPU->SetCustomFramebufferSize(real_framebuffer_width, real_framebuffer_height);
video->SetSourceSize(real_framebuffer_width, real_framebuffer_height * 2);
CommonSettings.GFX3D_Renderer_TextureDeposterize = config.textureDeposterize = gtk_toggle_button_get_active(wPosterize);
CommonSettings.GFX3D_Renderer_TextureSmoothing = config.textureSmoothing = gtk_toggle_button_get_active(wSmoothing);
CommonSettings.GFX3D_Renderer_TextureScalingFactor = config.textureUpscale = scale;
Expand Down Expand Up @@ -2151,7 +2167,7 @@ static void Printscreen(GSimpleAction *action, GVariant *parameter, gpointer use
const gchar *dir;
gchar *filename = NULL, *filen = NULL;
GError *error = NULL;
u8 rgb[256 * 384 * 4];
u8 *rgb = (u8*)malloc(real_framebuffer_width * real_framebuffer_height * 2 * 4);
static int seq = 0;
gint H, W;

Expand All @@ -2160,11 +2176,11 @@ static void Printscreen(GSimpleAction *action, GVariant *parameter, gpointer use
// return;

if (nds_screen.rotation_angle == 0 || nds_screen.rotation_angle == 180) {
W = screen_size[nds_screen.orientation].width;
H = screen_size[nds_screen.orientation].height;
W = real_framebuffer_width;
H = real_framebuffer_height * 2;
} else {
W = screen_size[nds_screen.orientation].height;
H = screen_size[nds_screen.orientation].width;
W = real_framebuffer_height * 2;
H = real_framebuffer_width;
}

gpu_screen_to_rgb((u32*)rgb);
Expand Down Expand Up @@ -2201,7 +2217,7 @@ static void Printscreen(GSimpleAction *action, GVariant *parameter, gpointer use
seq--;
}

//free(rgb);
free(rgb);
g_object_unref(screenshot);
g_free(filename);
g_free(filen);
Expand Down Expand Up @@ -3038,10 +3054,16 @@ common_gtk_main(GApplication *app, gpointer user_data)
nds_screen.orientation = ORIENT_VERTICAL;

gpu_scale_factor = config.gpuScaleFactor;
if(gpu_scale_factor < GPU_SCALE_FACTOR_MIN)
gpu_scale_factor = GPU_SCALE_FACTOR_MIN;
if(gpu_scale_factor > GPU_SCALE_FACTOR_MAX)
gpu_scale_factor = GPU_SCALE_FACTOR_MAX;
real_framebuffer_width = GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor;
real_framebuffer_height = GPU_FRAMEBUFFER_NATIVE_HEIGHT * gpu_scale_factor;

g_printerr("Using %d threads for video filter.\n", CommonSettings.num_cores);
GPU->SetCustomFramebufferSize(GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * gpu_scale_factor);
video = new VideoFilter(GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor, VideoFilterTypeID_None, CommonSettings.num_cores);
GPU->SetCustomFramebufferSize(real_framebuffer_width, real_framebuffer_height);
video = new VideoFilter(real_framebuffer_width, real_framebuffer_height * 2, VideoFilterTypeID_None, CommonSettings.num_cores);

/* Fetch the main elements from the window */
GtkBuilder *builder = gtk_builder_new_from_resource("/org/desmume/DeSmuME/main.ui");
Expand Down
7 changes: 4 additions & 3 deletions desmume/src/frontend/posix/gtk/sdl_3Demu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ static bool sdl_init(void) { return is_sdl_initialized(); }
static SDL_Window *win = NULL;
static SDL_GLContext ctx = NULL;

extern int gpu_scale_factor;
extern int real_framebuffer_width;
extern int real_framebuffer_height;

bool deinit_sdl_3Demu(void)
{
Expand Down Expand Up @@ -62,8 +63,8 @@ bool init_sdl_3Demu(void)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

win = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor,
GPU_FRAMEBUFFER_NATIVE_HEIGHT * gpu_scale_factor, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
win = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, real_framebuffer_width,
real_framebuffer_height, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
if (!win) {
fprintf(stderr, "SDL: Failed to create a window: %s\n", SDL_GetError());
return false;
Expand Down
Loading