Skip to content

Commit

Permalink
[WIP] gtk: implement GPU scale factor feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thesourcehim committed Jan 20, 2024
1 parent b06537c commit dc507c3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions desmume/src/frontend/posix/gtk/config_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +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(highColorInterpolation, bool, true, Config, HighResolutionColorInterpolation)
OPT(multisampling, bool, false, Config, OpenGLMultisampling)
OPT(multisamplingSize, int, 0, Config, OpenGLMultisamplingSize)
Expand Down
45 changes: 40 additions & 5 deletions desmume/src/frontend/posix/gtk/graphics.ui
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,49 @@
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="label" translatable="yes">GPU scale factor:</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</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>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="posterize">
<property name="label" translatable="yes">3D Texture Deposterization</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="top_attach">3</property>
</packing>
</child>
<child>
Expand All @@ -111,7 +146,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="top_attach">4</property>
</packing>
</child>
<child>
Expand All @@ -121,7 +156,7 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">3</property>
<property name="top_attach">4</property>
</packing>
</child>
<child>
Expand All @@ -130,7 +165,7 @@
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
<property name="top_attach">5</property>
</packing>
</child>
<child>
Expand All @@ -146,7 +181,7 @@
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">4</property>
<property name="top_attach">5</property>
</packing>
</child>
</object>
Expand Down
26 changes: 20 additions & 6 deletions desmume/src/frontend/posix/gtk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
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;

desmume::config::Config config;

Expand Down Expand Up @@ -1310,7 +1311,8 @@ 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);
ColorspaceConvertBuffer555To8888Opaque<false, false, BESwapDst>(GPU->GetDisplayInfo().masterNativeBuffer16, dst,
GPU_FRAMEBUFFER_NATIVE_WIDTH * GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor * gpu_scale_factor);
}

static inline void drawScreen(cairo_t* cr, u32* buf, gint w, gint h) {
Expand Down Expand Up @@ -1384,7 +1386,7 @@ static gboolean ExposeDrawingArea (GtkWidget *widget, GdkEventExpose *event, gpo
gint dstW = video->GetDstWidth();
gint dstH = video->GetDstHeight();

gint dstScale = dstW * 2 / 256; // Actual scale * 2 to handle 1.5x filters
gint dstScale = dstW * 2 / GPU_FRAMEBUFFER_NATIVE_WIDTH; // Actual scale * 2 to handle 1.5x filters

gint gap = nds_screen.orientation == ORIENT_VERTICAL ? nds_screen.gap_size * dstScale / 2 : 0;
gint imgW, imgH;
Expand Down Expand Up @@ -1436,9 +1438,10 @@ 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);
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);
#ifdef HAVE_LIBAGG
aggDraw.hud->attach((u8*)video->GetSrcBufferPtr(), 256, 384, 1024);
aggDraw.hud->attach((u8*)video->GetSrcBufferPtr(), GPU_FRAMEBUFFER_NATIVE_WIDTH * gpu_scale_factor, GPU_FRAMEBUFFER_NATIVE_HEIGHT * 2 * gpu_scale_factor, 1024);
osd->update();
DrawHUD();
osd->clear();
Expand Down Expand Up @@ -1987,14 +1990,15 @@ 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, *wMultisample;
GtkComboBox *coreCombo, *wScale, *wGPUScale, *wMultisample;
GtkToggleButton *wPosterize, *wSmoothing, *wHCInterpolate;

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"));
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 @@ -2010,6 +2014,9 @@ static void GraphicsSettingsDialog(GSimpleAction *action, GVariant *parameter, g
// The shift it work for scale up to 4. For scaling more than 4, a mapping function is required
gtk_combo_box_set_active(wScale, CommonSettings.GFX3D_Renderer_TextureScalingFactor >> 1);

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

// 3D Texture Deposterization
gtk_toggle_button_set_active(wPosterize, CommonSettings.GFX3D_Renderer_TextureDeposterize);

Expand Down Expand Up @@ -2080,6 +2087,10 @@ static void GraphicsSettingsDialog(GSimpleAction *action, GVariant *parameter, g
default:
break;
}
gpu_scale_factor = gtk_combo_box_get_active(wGPUScale)+1;
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);
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 @@ -3026,8 +3037,11 @@ common_gtk_main(GApplication *app, gpointer user_data)
memset(&nds_screen, 0, sizeof(nds_screen));
nds_screen.orientation = ORIENT_VERTICAL;

gpu_scale_factor = config.gpuScaleFactor;

g_printerr("Using %d threads for video filter.\n", CommonSettings.num_cores);
video = new VideoFilter(256, 384, VideoFilterTypeID_None, 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);

/* Fetch the main elements from the window */
GtkBuilder *builder = gtk_builder_new_from_resource("/org/desmume/DeSmuME/main.ui");
Expand Down
5 changes: 4 additions & 1 deletion desmume/src/frontend/posix/gtk/sdl_3Demu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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;

bool deinit_sdl_3Demu(void)
{
bool ret = false;
Expand Down Expand Up @@ -60,7 +62,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, 256, 192, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
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);
if (!win) {
fprintf(stderr, "SDL: Failed to create a window: %s\n", SDL_GetError());
return false;
Expand Down

0 comments on commit dc507c3

Please sign in to comment.