2021-08-11 08:53:36 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
|
|
|
* Copyright © 2017-2021 The Looking Glass Authors
|
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postprocess.h"
|
|
|
|
#include "filters.h"
|
2021-08-11 23:04:45 +00:00
|
|
|
#include "app.h"
|
|
|
|
#include "cimgui.h"
|
|
|
|
|
|
|
|
#include <stdatomic.h>
|
2021-08-11 08:53:36 +00:00
|
|
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/array.h"
|
2021-08-22 06:22:27 +00:00
|
|
|
#include "common/vector.h"
|
2021-08-11 08:53:36 +00:00
|
|
|
|
|
|
|
static const EGL_FilterOps * EGL_Filters[] =
|
|
|
|
{
|
2021-08-12 05:53:35 +00:00
|
|
|
&egl_filterDownscaleOps,
|
2021-08-11 08:53:36 +00:00
|
|
|
&egl_filterFFXFSR1Ops,
|
|
|
|
&egl_filterFFXCASOps
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EGL_PostProcess
|
|
|
|
{
|
2021-08-22 06:22:27 +00:00
|
|
|
Vector * filters;
|
2021-08-11 08:53:36 +00:00
|
|
|
GLuint output;
|
|
|
|
unsigned int outputX, outputY;
|
2021-08-11 23:04:45 +00:00
|
|
|
_Atomic(bool) modified;
|
2021-08-11 08:53:36 +00:00
|
|
|
|
|
|
|
EGL_Model * model;
|
|
|
|
};
|
|
|
|
|
|
|
|
void egl_postProcessEarlyInit(void)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < ARRAY_LENGTH(EGL_Filters); ++i)
|
|
|
|
EGL_Filters[i]->earlyInit();
|
|
|
|
}
|
|
|
|
|
2021-08-24 12:05:46 +00:00
|
|
|
static void drawDropTarget(void)
|
|
|
|
{
|
|
|
|
igPushStyleColorVec4(ImGuiCol_Separator, (ImVec4) { 1.0f, 1.0f, 0.0f, 1.0f });
|
|
|
|
igSeparator();
|
|
|
|
igPopStyleColor(1);
|
|
|
|
}
|
|
|
|
|
2021-08-11 23:04:45 +00:00
|
|
|
static void configUI(void * opaque, int * id)
|
|
|
|
{
|
|
|
|
struct EGL_PostProcess * this = opaque;
|
|
|
|
|
|
|
|
bool redraw = false;
|
2021-08-22 06:22:27 +00:00
|
|
|
|
2021-08-24 12:05:46 +00:00
|
|
|
static size_t mouseIdx = -1;
|
|
|
|
static bool moving = false;
|
|
|
|
static size_t moveIdx = 0;
|
|
|
|
bool doMove = false;
|
|
|
|
|
|
|
|
ImVec2 window, pos;
|
2021-08-22 07:52:28 +00:00
|
|
|
igGetWindowPos(&window);
|
2021-08-24 12:05:46 +00:00
|
|
|
igGetMousePos(&pos);
|
2021-08-22 07:52:28 +00:00
|
|
|
|
|
|
|
EGL_Filter ** filters = vector_data(this->filters);
|
|
|
|
size_t count = vector_size(this->filters);
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
2021-08-11 23:04:45 +00:00
|
|
|
{
|
2021-08-22 07:52:28 +00:00
|
|
|
EGL_Filter * filter = filters[i];
|
2021-08-24 12:05:46 +00:00
|
|
|
|
|
|
|
if (moving && mouseIdx < moveIdx && i == mouseIdx)
|
|
|
|
drawDropTarget();
|
2021-08-22 07:52:28 +00:00
|
|
|
|
|
|
|
igPushIDPtr(filter);
|
|
|
|
bool draw = igCollapsingHeaderBoolPtr(filter->ops.name, NULL, 0);
|
2021-08-24 12:05:46 +00:00
|
|
|
if (igIsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
|
|
|
|
mouseIdx = i;
|
|
|
|
|
2021-08-22 07:52:28 +00:00
|
|
|
bool active = igIsItemActive();
|
|
|
|
if (draw)
|
2021-08-11 23:04:45 +00:00
|
|
|
redraw |= egl_filterImguiConfig(filter);
|
|
|
|
igPopID();
|
2021-08-22 07:52:28 +00:00
|
|
|
|
2021-08-24 12:05:46 +00:00
|
|
|
if (moving)
|
2021-08-22 07:52:28 +00:00
|
|
|
{
|
2021-08-24 12:05:46 +00:00
|
|
|
if (!igIsMouseDragging(ImGuiMouseButton_Left, -1.0f))
|
|
|
|
{
|
|
|
|
moving = false;
|
|
|
|
doMove = true;
|
|
|
|
}
|
2021-08-22 07:52:28 +00:00
|
|
|
}
|
2021-08-24 12:05:46 +00:00
|
|
|
else
|
|
|
|
if (active && igIsMouseDragging(ImGuiMouseButton_Left, -1.0f))
|
|
|
|
{
|
|
|
|
moveIdx = mouseIdx;
|
|
|
|
moving = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moving && mouseIdx > moveIdx && i == mouseIdx)
|
|
|
|
drawDropTarget();
|
|
|
|
}
|
2021-08-22 07:52:28 +00:00
|
|
|
|
2021-08-24 12:05:46 +00:00
|
|
|
if (moving)
|
|
|
|
{
|
|
|
|
igSetMouseCursor(ImGuiMouseCursor_Hand);
|
|
|
|
igSetTooltip(filters[moveIdx]->ops.name);
|
2021-08-22 07:52:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 12:05:46 +00:00
|
|
|
if (doMove)
|
2021-08-22 07:52:28 +00:00
|
|
|
{
|
2021-08-24 12:05:46 +00:00
|
|
|
EGL_Filter * tmp[count];
|
|
|
|
memcpy(tmp, filters, sizeof(*tmp) * count);
|
|
|
|
|
|
|
|
size_t s = 0, d = 0;
|
|
|
|
for(size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
if (i == mouseIdx)
|
|
|
|
{
|
|
|
|
filters[d++] = tmp[moveIdx];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == moveIdx)
|
|
|
|
++s;
|
|
|
|
|
|
|
|
filters[d++] = tmp[s++];
|
|
|
|
}
|
2021-08-11 23:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (redraw)
|
|
|
|
{
|
|
|
|
atomic_store(&this->modified, true);
|
|
|
|
app_invalidateWindow(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
bool egl_postProcessInit(EGL_PostProcess ** pp)
|
|
|
|
{
|
|
|
|
EGL_PostProcess * this = calloc(1, sizeof(*this));
|
|
|
|
if (!this)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to allocate memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-22 06:22:27 +00:00
|
|
|
this->filters = vector_create(sizeof(EGL_Filter *), ARRAY_LENGTH(EGL_Filters));
|
2021-08-11 08:53:36 +00:00
|
|
|
if (!this->filters)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to allocate the filter list");
|
|
|
|
goto error_this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_modelInit(&this->model))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the model");
|
|
|
|
goto error_filters;
|
|
|
|
}
|
|
|
|
egl_modelSetDefault(this->model, false);
|
|
|
|
|
2021-08-11 23:04:45 +00:00
|
|
|
app_overlayConfigRegisterTab("EGL Filters", configUI, this);
|
|
|
|
|
2021-08-11 08:53:36 +00:00
|
|
|
*pp = this;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error_filters:
|
2021-08-22 06:22:27 +00:00
|
|
|
vector_free(this->filters);
|
2021-08-11 08:53:36 +00:00
|
|
|
|
|
|
|
error_this:
|
|
|
|
free(this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_postProcessFree(EGL_PostProcess ** pp)
|
|
|
|
{
|
|
|
|
if (!*pp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EGL_PostProcess * this = *pp;
|
|
|
|
|
|
|
|
if (this->filters)
|
|
|
|
{
|
2021-08-22 06:22:27 +00:00
|
|
|
EGL_Filter ** filter;
|
|
|
|
vector_forEachRef(filter, this->filters)
|
|
|
|
egl_filterFree(filter);
|
|
|
|
vector_free(this->filters);
|
2021-08-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
egl_modelFree(&this->model);
|
|
|
|
free(this);
|
|
|
|
*pp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_postProcessAdd(EGL_PostProcess * this, const EGL_FilterOps * ops)
|
|
|
|
{
|
|
|
|
EGL_Filter * filter;
|
|
|
|
if (!egl_filterInit(ops, &filter))
|
|
|
|
return false;
|
|
|
|
|
2021-08-22 06:22:27 +00:00
|
|
|
vector_push(this->filters, &filter);
|
2021-08-11 08:53:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-11 23:04:45 +00:00
|
|
|
bool egl_postProcessConfigModified(EGL_PostProcess * this)
|
2021-08-11 08:53:36 +00:00
|
|
|
{
|
2021-08-11 23:04:45 +00:00
|
|
|
return atomic_load(&this->modified);
|
2021-08-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_postProcessRun(EGL_PostProcess * this, EGL_Texture * tex,
|
|
|
|
unsigned int targetX, unsigned int targetY)
|
|
|
|
{
|
2021-08-22 06:22:27 +00:00
|
|
|
EGL_Filter * lastFilter = NULL;
|
2021-08-11 08:53:36 +00:00
|
|
|
unsigned int sizeX, sizeY;
|
|
|
|
|
|
|
|
GLuint texture;
|
|
|
|
if (egl_textureGet(tex, &texture, &sizeX, &sizeY) != EGL_TEX_STATUS_OK)
|
|
|
|
return false;
|
|
|
|
|
2021-08-11 23:04:45 +00:00
|
|
|
atomic_store(&this->modified, false);
|
2021-08-22 06:22:27 +00:00
|
|
|
|
|
|
|
EGL_Filter * filter;
|
|
|
|
vector_forEach(filter, this->filters)
|
2021-08-11 08:53:36 +00:00
|
|
|
{
|
|
|
|
egl_filterSetOutputResHint(filter, targetX, targetY);
|
|
|
|
egl_filterSetup(filter, tex->format.pixFmt, sizeX, sizeY);
|
|
|
|
|
|
|
|
if (!egl_filterPrepare(filter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
texture = egl_filterRun(filter, this->model, texture);
|
|
|
|
egl_filterGetOutputRes(filter, &sizeX, &sizeY);
|
|
|
|
|
|
|
|
if (lastFilter)
|
|
|
|
egl_filterRelease(lastFilter);
|
|
|
|
|
|
|
|
lastFilter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->output = texture;
|
|
|
|
this->outputX = sizeX;
|
|
|
|
this->outputY = sizeY;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint egl_postProcessGetOutput(EGL_PostProcess * this,
|
|
|
|
unsigned int * outputX, unsigned int * outputY)
|
|
|
|
{
|
|
|
|
*outputX = this->outputX;
|
|
|
|
*outputY = this->outputY;
|
|
|
|
return this->output;
|
|
|
|
}
|