mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
* feat(framegen): add spatial post-upscale per presentation * Fix framegen post-upscale expectations * test: run framegen post-upscale assertions * test: verify framegen postprocessing image handles * test: capture framegen screen blit source
1039 lines
53 KiB
C
1039 lines
53 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include "effects_chain.h"
|
|
#include "blend_pass.h"
|
|
#include "vk_loader.h"
|
|
#include "vk_present.h"
|
|
#include "framegen_bridge.h"
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "upscale_vert.h"
|
|
#include "sgsr_frag.h"
|
|
#include "sgsr_quality_frag.h"
|
|
#include "nis_frag.h"
|
|
#include "fsr_easu_frag.h"
|
|
#include "fsr_rcas_frag.h"
|
|
#include "cas_frag.h"
|
|
#include "color_frag.h"
|
|
#include "fxaa_frag.h"
|
|
#include "fxaa_toon_frag.h"
|
|
#include "smaa_edge_frag.h"
|
|
#include "smaa_weights_frag.h"
|
|
#include "smaa_blend_frag.h"
|
|
#include "toon_frag.h"
|
|
#include "toon_color_frag.h"
|
|
#include "hdr_frag.h"
|
|
#include "ntsc_frag.h"
|
|
#include "crt_frag.h"
|
|
#include "deband_frag.h"
|
|
/* Half-precision upscaler/CAS set (shaders/gen_shaders.sh -DDD_HALF=1), ported from Bannerlator. */
|
|
#include "sgsr_half_frag.h"
|
|
#include "sgsr_quality_half_frag.h"
|
|
#include "nis_luma_half_frag.h"
|
|
#include "nis3_half_frag.h"
|
|
#include "nis_coef.h"
|
|
#include "fsr_easu_half_frag.h"
|
|
#include "fsr_rcas_half_frag.h"
|
|
#include "cas_ffx_half_frag.h"
|
|
#ifdef __ANDROID__
|
|
#include <sys/system_properties.h>
|
|
#endif
|
|
|
|
#define FX_FORMAT VK_FORMAT_R8G8B8A8_UNORM
|
|
|
|
|
|
|
|
|
|
static VkFormat g_fx_fmt = FX_FORMAT, g_scene_fmt = FX_FORMAT;
|
|
static int g_scene_is_pq;
|
|
#define FX_MAX_DIM 4096
|
|
|
|
|
|
struct fx_settings {
|
|
int scaling;
|
|
int sharp_pct;
|
|
int cas_on, cas_pct;
|
|
int hdr_on;
|
|
int deband_on, deband_pct;
|
|
int color_on;
|
|
float brightness, contrast, gamma, saturation;
|
|
float b_raw, c_raw, s_raw;
|
|
int aa_mode, toon, crt, ntsc;
|
|
char look[48];
|
|
};
|
|
|
|
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
static struct fx_settings g_set = {
|
|
.scaling = 0, .sharp_pct = 75, .cas_on = 0, .cas_pct = 60, .deband_pct = 100,
|
|
.gamma = 1.0f, .saturation = 1.0f, .s_raw = 100.0f, .look = "Off"};
|
|
static volatile int g_changed;
|
|
static struct fx_settings g_cur;
|
|
static struct fx_settings g_generated_settings;
|
|
static int g_cur_valid;
|
|
|
|
static void set_begin(void) { pthread_mutex_lock(&g_lock); }
|
|
static void set_end(void) { g_changed = 1; pthread_mutex_unlock(&g_lock); }
|
|
|
|
void vkp_effects_set_scaling(int mode) {
|
|
if (mode < 0 || mode > 8) mode = 0;
|
|
set_begin(); g_set.scaling = mode; set_end();
|
|
}
|
|
void vkp_effects_set_upscale_sharpness(int pct) {
|
|
if (pct < 0) pct = 0;
|
|
if (pct > 100) pct = 100;
|
|
set_begin(); g_set.sharp_pct = pct; set_end();
|
|
}
|
|
void vkp_effects_set_cas(int enabled, int pct) {
|
|
if (pct < 0) pct = 0;
|
|
if (pct > 100) pct = 100;
|
|
set_begin(); g_set.cas_on = enabled ? 1 : 0; g_set.cas_pct = pct; set_end();
|
|
}
|
|
void vkp_effects_set_hdr(int enabled) { set_begin(); g_set.hdr_on = enabled ? 1 : 0; set_end(); }
|
|
void vkp_effects_set_deband(int enabled, int pct) {
|
|
if (pct < 0) pct = 0;
|
|
if (pct > 200) pct = 200;
|
|
set_begin(); g_set.deband_on = enabled ? 1 : 0; g_set.deband_pct = pct; set_end();
|
|
}
|
|
static float clampf(float v, float lo, float hi) { return v < lo ? lo : v > hi ? hi : v; }
|
|
void vkp_effects_set_screen(float brightness, float contrast, float gamma, float saturation,
|
|
int aa_mode, int toon, int crt, int ntsc) {
|
|
int on = !(brightness == 0.0f && contrast == 0.0f && gamma == 1.0f && saturation == 100.0f);
|
|
set_begin();
|
|
g_set.color_on = on;
|
|
g_set.b_raw = brightness; g_set.c_raw = contrast; g_set.s_raw = saturation;
|
|
g_set.brightness = clampf(brightness / 100.0f, -1.0f, 1.0f);
|
|
g_set.contrast = clampf(contrast / 100.0f, 0.0f, 2.0f);
|
|
g_set.saturation = clampf(saturation / 100.0f, 0.0f, 2.0f);
|
|
g_set.gamma = clampf(gamma, 0.1f, 5.0f);
|
|
g_set.aa_mode = (aa_mode >= 0 && aa_mode <= 2) ? aa_mode : 0;
|
|
g_set.toon = toon ? 1 : 0; g_set.crt = crt ? 1 : 0; g_set.ntsc = ntsc ? 1 : 0;
|
|
set_end();
|
|
}
|
|
void vkp_effects_set_look(const char *name) {
|
|
set_begin();
|
|
snprintf(g_set.look, sizeof(g_set.look), "%s", name && *name ? name : "Custom");
|
|
set_end();
|
|
}
|
|
|
|
static const char *scaling_name(int m) {
|
|
static const char *n[] = {"None", "Linear", "Nearest", "SGSR", "FSR", "FSR Fit", "Sharpen", "NIS", "SGSR HQ"};
|
|
return (m >= 0 && m <= 8) ? n[m] : "?";
|
|
}
|
|
static int spatial_mode(int m) { return m >= 3 && m <= 8; }
|
|
static int cas_active(const struct fx_settings *s) { return s->cas_on && s->cas_pct > 0; }
|
|
static int deband_active(const struct fx_settings *s) { return s->deband_on && s->deband_pct > 0; }
|
|
static int effect_count(const struct fx_settings *s) {
|
|
return (s->aa_mode != 0) + s->toon + s->color_on + cas_active(s) + s->hdr_on + s->ntsc + s->crt + deband_active(s);
|
|
}
|
|
|
|
|
|
static void log_settings(const struct fx_settings *s) {
|
|
char line[400];
|
|
int n;
|
|
if (!spatial_mode(s->scaling) && !effect_count(s)) {
|
|
droiddeck_log("effects", "all off: scaling=%s (plain blit)", scaling_name(s->scaling));
|
|
return;
|
|
}
|
|
n = snprintf(line, sizeof(line), "scaling=%s", scaling_name(s->scaling));
|
|
if (spatial_mode(s->scaling)) n += snprintf(line + n, sizeof(line) - n, " %d%%", s->sharp_pct);
|
|
if (cas_active(s)) n += snprintf(line + n, sizeof(line) - n, ", CAS on %d%%", s->cas_pct);
|
|
else n += snprintf(line + n, sizeof(line) - n, ", CAS off");
|
|
n += snprintf(line + n, sizeof(line) - n, ", Look=\"%s\"", s->look);
|
|
if (s->color_on)
|
|
n += snprintf(line + n, sizeof(line) - n, ", colour b=%+.0f c=%+.0f g=%.2f s=%.0f%%",
|
|
s->b_raw, s->c_raw, s->gamma, s->s_raw);
|
|
if (s->aa_mode == 1) n += snprintf(line + n, sizeof(line) - n, ", FXAA on");
|
|
else if (s->aa_mode == 2) n += snprintf(line + n, sizeof(line) - n, ", SMAA on");
|
|
if (s->toon) n += snprintf(line + n, sizeof(line) - n, ", Toon on");
|
|
if (s->hdr_on) n += snprintf(line + n, sizeof(line) - n, ", HDR on");
|
|
if (s->ntsc) n += snprintf(line + n, sizeof(line) - n, ", NTSC on");
|
|
if (s->crt) n += snprintf(line + n, sizeof(line) - n, ", CRT on");
|
|
if (deband_active(s)) n += snprintf(line + n, sizeof(line) - n, ", Deband on %d%%", s->deband_pct);
|
|
(void)n;
|
|
droiddeck_log("effects", "%s", line);
|
|
}
|
|
|
|
int vkp_effects_sync(void) {
|
|
if (!g_changed && g_cur_valid) return 0;
|
|
pthread_mutex_lock(&g_lock);
|
|
g_changed = 0;
|
|
g_cur = g_set;
|
|
pthread_mutex_unlock(&g_lock);
|
|
g_cur_valid = 1;
|
|
log_settings(&g_cur);
|
|
return 1;
|
|
}
|
|
|
|
int vkp_effects_active(void) {
|
|
vkp_effects_sync();
|
|
return spatial_mode(g_cur.scaling) || effect_count(&g_cur) > 0;
|
|
}
|
|
|
|
int vkp_effects_upscaling(void) {
|
|
vkp_effects_sync();
|
|
return spatial_mode(g_cur.scaling) && g_cur.scaling != 6;
|
|
}
|
|
|
|
int vkp_effects_sharpening(void) {
|
|
return vkp_effects_upscaling() || cas_active(&g_cur);
|
|
}
|
|
|
|
int vkp_effects_postprocessing(void) {
|
|
vkp_effects_sync();
|
|
return effect_count(&g_cur) > 0;
|
|
}
|
|
|
|
VkFilter vkp_effects_blit_filter(void) {
|
|
vkp_effects_sync();
|
|
return g_cur.scaling == 2 ? VK_FILTER_NEAREST : VK_FILTER_LINEAR;
|
|
}
|
|
|
|
|
|
|
|
struct pc_sgsr { float ndc[4]; float vp[4]; float edge; };
|
|
struct pc_easu { float ndc[4]; uint32_t con0[4], con1[4], con2[4], con3[4]; float outW, outH; };
|
|
struct pc_rcas { float ndc[4]; uint32_t con[4]; float outW, outH; };
|
|
struct pc_res { float ndc[4]; float res[2]; float extra; float pad; };
|
|
struct pc_color { float ndc[4]; float b, c, g, s; };
|
|
struct pc_toon_color {
|
|
float ndc[4]; float res[2]; float b, c, g, s;
|
|
};
|
|
_Static_assert(sizeof(struct pc_toon_color) == 40,
|
|
"toon_color push constants layout mismatch");
|
|
#define PC_MAX_SIZE sizeof(struct pc_easu)
|
|
|
|
static uint32_t packf(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
|
|
|
|
static void easu_con(struct pc_easu *e, float inW, float inH, float outW, float outH) {
|
|
e->con0[0] = packf(inW / outW); e->con0[1] = packf(inH / outH);
|
|
e->con0[2] = packf(0.5f * inW / outW - 0.5f); e->con0[3] = packf(0.5f * inH / outH - 0.5f);
|
|
e->con1[0] = packf(1.0f / inW); e->con1[1] = packf(1.0f / inH);
|
|
e->con1[2] = packf(1.0f / inW); e->con1[3] = packf(-1.0f / inH);
|
|
e->con2[0] = packf(-1.0f / inW); e->con2[1] = packf(2.0f / inH);
|
|
e->con2[2] = packf(1.0f / inW); e->con2[3] = packf(2.0f / inH);
|
|
e->con3[0] = packf(0.0f); e->con3[1] = packf(4.0f / inH);
|
|
e->con3[2] = 0; e->con3[3] = 0;
|
|
}
|
|
static void rcas_con(struct pc_rcas *r, float scale01) {
|
|
r->con[0] = packf(scale01); r->con[1] = 0; r->con[2] = 0; r->con[3] = 0;
|
|
}
|
|
|
|
|
|
struct fx_target {
|
|
VkImage img; VkDeviceMemory mem; VkImageView view; VkFramebuffer fb; VkDescriptorSet ds;
|
|
int w, h;
|
|
};
|
|
|
|
static VkDevice g_dev;
|
|
static VkPhysicalDevice g_pd;
|
|
static VkPhysicalDeviceMemoryProperties g_mp;
|
|
static int g_ready;
|
|
static VkRenderPass g_rp, g_smaa_rp;
|
|
static VkDescriptorSetLayout g_dsl;
|
|
static VkPipelineLayout g_pl;
|
|
static VkDescriptorPool g_dpool;
|
|
static VkSampler g_sampler;
|
|
enum { P_SGSR, P_SGSR_HQ, P_NIS, P_EASU, P_RCAS, P_CAS, P_COLOR, P_FXAA, P_FXAA_TOON, P_SMAA_EDGE, P_SMAA_WEIGHTS, P_SMAA_BLEND, P_TOON, P_TOON_COLOR, P_HDR, P_NTSC, P_CRT, P_DEBAND, P_NIS_LUMA, P_COUNT };
|
|
static VkPipeline g_pipe[P_COUNT];
|
|
struct fx_context {
|
|
struct fx_target targets[2], mid, nissrc;
|
|
struct { VkImage img; VkImageView view; VkDescriptorSet ds; int w, h; } scene;
|
|
};
|
|
#define FX_CONTEXTS (VKP_FG_MAX_GENERATIONS + 2)
|
|
static struct fx_context g_contexts[FX_CONTEXTS];
|
|
static int g_context;
|
|
#define g_t (g_contexts[g_context].targets)
|
|
#define g_mid (g_contexts[g_context].mid)
|
|
#define g_nissrc (g_contexts[g_context].nissrc)
|
|
static struct fx_target g_smaa_edges, g_smaa_weights;
|
|
/* Half set: NIS = nis_luma prepass (scene rgb + luma in alpha, scene size) into g_nissrc, then
|
|
* nis3 gathers its luma from that alpha, its coefficient banks (nis_coef.h) in a uniform buffer
|
|
* at descriptor set 1 (g_pl_nis = {g_dsl, g_coef_dsl}, same push-constant range as g_pl).
|
|
* Old set (or no buffer): nis.frag on the scene, P_NIS_LUMA unused. */
|
|
static int g_nis_fast;
|
|
static int g_nis_attempted;
|
|
static int g_upscale_half;
|
|
static VkDescriptorSetLayout g_coef_dsl;
|
|
static VkPipelineLayout g_pl_nis;
|
|
static VkDescriptorPool g_coef_pool;
|
|
static VkDescriptorSet g_coef_ds;
|
|
static VkBuffer g_coef_buf;
|
|
static VkDeviceMemory g_coef_mem;
|
|
|
|
#define g_scene (g_contexts[g_context].scene)
|
|
|
|
void vkp_effects_bind_device(VkDevice dev, VkPhysicalDevice pd,
|
|
const VkPhysicalDeviceMemoryProperties *memprops) {
|
|
g_dev = dev; g_pd = pd; g_mp = *memprops;
|
|
}
|
|
|
|
static int memtype(uint32_t bits, VkMemoryPropertyFlags want) {
|
|
for (uint32_t i = 0; i < g_mp.memoryTypeCount; i++)
|
|
if ((bits & (1u << i)) && (g_mp.memoryTypes[i].propertyFlags & want) == want) return (int)i;
|
|
return -1;
|
|
}
|
|
|
|
static VkDescriptorSet alloc_ds(VkImageView view) {
|
|
VkDescriptorSet ds = VK_NULL_HANDLE;
|
|
VkDescriptorSetAllocateInfo ai = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
|
.descriptorPool = g_dpool, .descriptorSetCount = 1, .pSetLayouts = &g_dsl};
|
|
if (g_vk.AllocateDescriptorSets(g_dev, &ai, &ds) != VK_SUCCESS) return VK_NULL_HANDLE;
|
|
VkDescriptorImageInfo ii = {.sampler = g_sampler, .imageView = view,
|
|
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
|
|
VkWriteDescriptorSet wr = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = ds, .dstBinding = 0,
|
|
.descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
.pImageInfo = &ii};
|
|
g_vk.UpdateDescriptorSets(g_dev, 1, &wr, 0, NULL);
|
|
return ds;
|
|
}
|
|
|
|
static VkImageView make_view(VkImage img, VkFormat fmt) {
|
|
VkImageView v = VK_NULL_HANDLE;
|
|
VkImageViewCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img,
|
|
.viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt,
|
|
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
|
if (g_vk.CreateImageView(g_dev, &ci, NULL, &v) != VK_SUCCESS) return VK_NULL_HANDLE;
|
|
return v;
|
|
}
|
|
|
|
static void target_destroy(struct fx_target *t) {
|
|
if (t->img) blendp_forget_image(t->img);
|
|
if (t->ds) g_vk.FreeDescriptorSets(g_dev, g_dpool, 1, &t->ds);
|
|
if (t->fb) g_vk.DestroyFramebuffer(g_dev, t->fb, NULL);
|
|
if (t->view) g_vk.DestroyImageView(g_dev, t->view, NULL);
|
|
if (t->img) g_vk.DestroyImage(g_dev, t->img, NULL);
|
|
if (t->mem) g_vk.FreeMemory(g_dev, t->mem, NULL);
|
|
memset(t, 0, sizeof(*t));
|
|
}
|
|
|
|
#define FX_TARGET_USAGE (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | \
|
|
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)
|
|
#define FX_SCRATCH_USAGE (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
|
|
|
|
static int target_ensure_format(struct fx_target *t, int w, int h,
|
|
VkImageUsageFlags usage, const char *what,
|
|
VkFormat format, VkRenderPass render_pass) {
|
|
if (t->img && t->w == w && t->h == h) return 0;
|
|
target_destroy(t);
|
|
VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
|
.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, .imageType = VK_IMAGE_TYPE_2D,
|
|
.format = format, .extent = {(uint32_t)w, (uint32_t)h, 1}, .mipLevels = 1,
|
|
.arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL,
|
|
.usage = usage,
|
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
|
|
VkResult r = g_vk.CreateImage(g_dev, &ii, NULL, &t->img);
|
|
if (r != VK_SUCCESS) { droiddeck_log("error", "effects: %s image %dx%d: vkCreateImage %d", what, w, h, (int)r); return -1; }
|
|
VkMemoryRequirements req;
|
|
g_vk.GetImageMemoryRequirements(g_dev, t->img, &req);
|
|
int idx = memtype(req.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
if (idx < 0) idx = memtype(req.memoryTypeBits, 0);
|
|
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = req.size,
|
|
.memoryTypeIndex = (uint32_t)(idx < 0 ? 0 : idx)};
|
|
r = g_vk.AllocateMemory(g_dev, &mai, NULL, &t->mem);
|
|
if (r != VK_SUCCESS) {
|
|
droiddeck_log("error", "effects: %s image %dx%d (%llu bytes): vkAllocateMemory %d", what, w, h,
|
|
(unsigned long long)req.size, (int)r);
|
|
target_destroy(t); return -1;
|
|
}
|
|
g_vk.BindImageMemory(g_dev, t->img, t->mem, 0);
|
|
if (!(t->view = make_view(t->img, format))) { target_destroy(t); return -1; }
|
|
VkFramebufferCreateInfo fi = {.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, .renderPass = render_pass,
|
|
.attachmentCount = 1, .pAttachments = &t->view, .width = (uint32_t)w,
|
|
.height = (uint32_t)h, .layers = 1};
|
|
if (g_vk.CreateFramebuffer(g_dev, &fi, NULL, &t->fb) != VK_SUCCESS) { target_destroy(t); return -1; }
|
|
if (!(t->ds = alloc_ds(t->view))) { target_destroy(t); return -1; }
|
|
t->w = w; t->h = h;
|
|
return 0;
|
|
}
|
|
|
|
static int target_ensure(struct fx_target *t, int w, int h,
|
|
VkImageUsageFlags usage, const char *what) {
|
|
return target_ensure_format(t, w, h, usage, what, g_fx_fmt, g_rp);
|
|
}
|
|
|
|
static void scene_release(void) {
|
|
if (g_scene.ds) g_vk.FreeDescriptorSets(g_dev, g_dpool, 1, &g_scene.ds);
|
|
if (g_scene.view) g_vk.DestroyImageView(g_dev, g_scene.view, NULL);
|
|
memset(&g_scene, 0, sizeof(g_scene));
|
|
}
|
|
|
|
void vkp_effects_forget_image(VkImage img) {
|
|
int saved = g_context;
|
|
for (g_context = 0; g_context < FX_CONTEXTS; g_context++)
|
|
if (img && g_scene.img == img) scene_release();
|
|
g_context = saved;
|
|
}
|
|
|
|
void vkp_effects_release_generated_sources(void) {
|
|
for (g_context = 1; g_context < FX_CONTEXTS; g_context++) scene_release();
|
|
g_context = 0;
|
|
}
|
|
|
|
static int scene_ensure(VkImage img, int w, int h) {
|
|
if (g_scene.img == img && g_scene.w == w && g_scene.h == h && g_scene.ds) return 0;
|
|
scene_release();
|
|
if (!(g_scene.view = make_view(img, g_scene_fmt))) return -1;
|
|
if (!(g_scene.ds = alloc_ds(g_scene.view))) { scene_release(); return -1; }
|
|
g_scene.img = img; g_scene.w = w; g_scene.h = h;
|
|
return 0;
|
|
}
|
|
|
|
static VkShaderModule shader(const uint32_t *code, size_t size) {
|
|
VkShaderModule m = VK_NULL_HANDLE;
|
|
VkShaderModuleCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, .pCode = code};
|
|
if (g_vk.CreateShaderModule(g_dev, &ci, NULL, &m) != VK_SUCCESS) return VK_NULL_HANDLE;
|
|
return m;
|
|
}
|
|
|
|
static VkPipeline post_pipeline_for_renderpass(VkShaderModule vert,
|
|
const uint32_t *frag_code, size_t frag_size, VkRenderPass render_pass,
|
|
const char *name, VkPipelineLayout layout) {
|
|
VkShaderModule frag = shader(frag_code, frag_size);
|
|
if (!frag) { droiddeck_log("error", "effects: %s: vkCreateShaderModule failed", name); return VK_NULL_HANDLE; }
|
|
VkPipelineShaderStageCreateInfo st[2] = {
|
|
{.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vert, .pName = "main"},
|
|
{.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = frag, .pName = "main"}};
|
|
VkPipelineVertexInputStateCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
|
|
VkPipelineInputAssemblyStateCreateInfo ia = {.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
|
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP};
|
|
VkDynamicState dyn[2] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
|
|
VkPipelineDynamicStateCreateInfo ds = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
|
|
.dynamicStateCount = 2, .pDynamicStates = dyn};
|
|
VkPipelineViewportStateCreateInfo vp = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
|
.viewportCount = 1, .scissorCount = 1};
|
|
VkPipelineRasterizationStateCreateInfo rs = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
|
.polygonMode = VK_POLYGON_MODE_FILL, .cullMode = VK_CULL_MODE_NONE,
|
|
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE, .lineWidth = 1.0f};
|
|
VkPipelineMultisampleStateCreateInfo ms = {.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
|
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT};
|
|
VkPipelineColorBlendAttachmentState ba = {.blendEnable = VK_FALSE, .colorWriteMask = 0xF};
|
|
VkPipelineColorBlendStateCreateInfo cb = {.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
|
.attachmentCount = 1, .pAttachments = &ba};
|
|
VkGraphicsPipelineCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .stageCount = 2,
|
|
.pStages = st, .pVertexInputState = &vi, .pInputAssemblyState = &ia,
|
|
.pViewportState = &vp, .pRasterizationState = &rs, .pMultisampleState = &ms,
|
|
.pColorBlendState = &cb, .pDynamicState = &ds, .layout = layout,
|
|
.renderPass = render_pass, .subpass = 0};
|
|
VkPipeline p = VK_NULL_HANDLE;
|
|
VkResult r = g_vk.CreateGraphicsPipelines(g_dev, VK_NULL_HANDLE, 1, &pi, NULL, &p);
|
|
g_vk.DestroyShaderModule(g_dev, frag, NULL);
|
|
if (r != VK_SUCCESS) { droiddeck_log("error", "effects: %s: vkCreateGraphicsPipelines %d", name, (int)r); return VK_NULL_HANDLE; }
|
|
return p;
|
|
}
|
|
|
|
static void coef_destroy(void) {
|
|
if (g_coef_pool) g_vk.DestroyDescriptorPool(g_dev, g_coef_pool, NULL); /* frees g_coef_ds */
|
|
if (g_coef_buf) g_vk.DestroyBuffer(g_dev, g_coef_buf, NULL);
|
|
if (g_coef_mem) g_vk.FreeMemory(g_dev, g_coef_mem, NULL);
|
|
if (g_pl_nis) g_vk.DestroyPipelineLayout(g_dev, g_pl_nis, NULL);
|
|
if (g_coef_dsl) g_vk.DestroyDescriptorSetLayout(g_dev, g_coef_dsl, NULL);
|
|
g_coef_pool = VK_NULL_HANDLE; g_coef_ds = VK_NULL_HANDLE; g_coef_buf = VK_NULL_HANDLE;
|
|
g_coef_mem = VK_NULL_HANDLE; g_pl_nis = VK_NULL_HANDLE; g_coef_dsl = VK_NULL_HANDLE;
|
|
}
|
|
|
|
/* nis3's coefficient banks: a 4 KB host-visible uniform buffer (scale then usm), filled once. 0 = ok. */
|
|
static int coef_create(void) {
|
|
VkDescriptorSetLayoutBinding b = {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
.descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT};
|
|
VkDescriptorSetLayoutCreateInfo dli = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = 1, .pBindings = &b};
|
|
if (g_vk.CreateDescriptorSetLayout(g_dev, &dli, NULL, &g_coef_dsl) != VK_SUCCESS) goto fail;
|
|
VkDescriptorSetLayout sets[2] = {g_dsl, g_coef_dsl};
|
|
VkPushConstantRange pcr = {.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, .offset = 0, .size = PC_MAX_SIZE};
|
|
VkPipelineLayoutCreateInfo pli = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 2, .pSetLayouts = sets,
|
|
.pushConstantRangeCount = 1, .pPushConstantRanges = &pcr};
|
|
if (g_vk.CreatePipelineLayout(g_dev, &pli, NULL, &g_pl_nis) != VK_SUCCESS) goto fail;
|
|
VkBufferCreateInfo bci = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 4096,
|
|
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE};
|
|
if (g_vk.CreateBuffer(g_dev, &bci, NULL, &g_coef_buf) != VK_SUCCESS) goto fail;
|
|
VkMemoryRequirements req;
|
|
g_vk.GetBufferMemoryRequirements(g_dev, g_coef_buf, &req);
|
|
int idx = memtype(req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
if (idx < 0) goto fail;
|
|
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = req.size, .memoryTypeIndex = (uint32_t)idx};
|
|
if (g_vk.AllocateMemory(g_dev, &mai, NULL, &g_coef_mem) != VK_SUCCESS) goto fail;
|
|
if (g_vk.BindBufferMemory(g_dev, g_coef_buf, g_coef_mem, 0) != VK_SUCCESS) goto fail;
|
|
void *p = NULL;
|
|
if (g_vk.MapMemory(g_dev, g_coef_mem, 0, 4096, 0, &p) != VK_SUCCESS || !p) goto fail;
|
|
memcpy(p, kNisCoefScale, sizeof(kNisCoefScale));
|
|
memcpy((char *)p + sizeof(kNisCoefScale), kNisCoefUsm, sizeof(kNisCoefUsm));
|
|
g_vk.UnmapMemory(g_dev, g_coef_mem);
|
|
VkDescriptorPoolSize ps = {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1};
|
|
VkDescriptorPoolCreateInfo dpi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, .poolSizeCount = 1, .pPoolSizes = &ps};
|
|
if (g_vk.CreateDescriptorPool(g_dev, &dpi, NULL, &g_coef_pool) != VK_SUCCESS) goto fail;
|
|
VkDescriptorSetAllocateInfo ai = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, .descriptorPool = g_coef_pool,
|
|
.descriptorSetCount = 1, .pSetLayouts = &g_coef_dsl};
|
|
if (g_vk.AllocateDescriptorSets(g_dev, &ai, &g_coef_ds) != VK_SUCCESS) goto fail;
|
|
VkDescriptorBufferInfo bi = {.buffer = g_coef_buf, .offset = 0, .range = 4096};
|
|
VkWriteDescriptorSet wr = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = g_coef_ds, .dstBinding = 0,
|
|
.descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &bi};
|
|
g_vk.UpdateDescriptorSets(g_dev, 1, &wr, 0, NULL);
|
|
return 0;
|
|
fail:
|
|
coef_destroy();
|
|
return -1;
|
|
}
|
|
|
|
/* nis_luma keeps luma in the target's alpha: only formats with at least 8 alpha bits (not the
|
|
* 2-bit alpha of the 10-bit HDR composition format) can hold it. */
|
|
static int fx_alpha_holds_luma(VkFormat f) {
|
|
return f == VK_FORMAT_R8G8B8A8_UNORM || f == VK_FORMAT_B8G8R8A8_UNORM ||
|
|
f == VK_FORMAT_R16G16B16A16_SFLOAT || f == VK_FORMAT_R16G16B16A16_UNORM;
|
|
}
|
|
|
|
/* The half-precision upscaler set unless debug.droiddeck.upscale.shaders is "old" (the fp32
|
|
* shaders, for A/B comparisons). */
|
|
static int upscale_half_set(void) {
|
|
#ifdef __ANDROID__
|
|
char v[PROP_VALUE_MAX] = {0};
|
|
if (__system_property_get("debug.droiddeck.upscale.shaders", v) > 0 && strcmp(v, "old") == 0) return 0;
|
|
#endif
|
|
return 1;
|
|
}
|
|
|
|
static void objects_destroy(void) {
|
|
if (!g_dev) return;
|
|
int saved = g_context;
|
|
for (g_context = 0; g_context < FX_CONTEXTS; g_context++) {
|
|
scene_release();
|
|
target_destroy(&g_t[0]); target_destroy(&g_t[1]); target_destroy(&g_mid);
|
|
target_destroy(&g_nissrc);
|
|
}
|
|
g_context = saved;
|
|
target_destroy(&g_smaa_edges); target_destroy(&g_smaa_weights);
|
|
for (int i = 0; i < P_COUNT; i++) { if (g_pipe[i]) g_vk.DestroyPipeline(g_dev, g_pipe[i], NULL); g_pipe[i] = VK_NULL_HANDLE; }
|
|
if (g_dpool) g_vk.DestroyDescriptorPool(g_dev, g_dpool, NULL);
|
|
if (g_pl) g_vk.DestroyPipelineLayout(g_dev, g_pl, NULL);
|
|
coef_destroy();
|
|
if (g_dsl) g_vk.DestroyDescriptorSetLayout(g_dev, g_dsl, NULL);
|
|
if (g_sampler) g_vk.DestroySampler(g_dev, g_sampler, NULL);
|
|
if (g_smaa_rp) g_vk.DestroyRenderPass(g_dev, g_smaa_rp, NULL);
|
|
if (g_rp) g_vk.DestroyRenderPass(g_dev, g_rp, NULL);
|
|
g_dpool = VK_NULL_HANDLE; g_pl = VK_NULL_HANDLE; g_dsl = VK_NULL_HANDLE;
|
|
g_sampler = VK_NULL_HANDLE; g_rp = VK_NULL_HANDLE; g_smaa_rp = VK_NULL_HANDLE;
|
|
g_nis_fast = 0; g_nis_attempted = 0; g_upscale_half = 0;
|
|
g_ready = 0;
|
|
}
|
|
|
|
void vkp_effects_destroy(void) { objects_destroy(); g_dev = VK_NULL_HANDLE; }
|
|
|
|
void vkp_effects_set_formats(VkFormat scene_fmt, VkFormat target_fmt, int scene_is_pq) {
|
|
g_scene_is_pq = scene_is_pq != 0;
|
|
if (target_fmt != g_fx_fmt) {
|
|
if (g_ready) objects_destroy();
|
|
g_fx_fmt = target_fmt;
|
|
g_scene_fmt = scene_fmt;
|
|
if (g_dev)
|
|
droiddeck_log("effects", "chain now works in %s (%s)",
|
|
target_fmt == FX_FORMAT ? "8-bit RGBA"
|
|
: target_fmt == VK_FORMAT_R16G16B16A16_SFLOAT ? "FP16 RGBA" : "10-bit RGB",
|
|
target_fmt == FX_FORMAT ? "SDR frames"
|
|
: target_fmt == VK_FORMAT_R16G16B16A16_SFLOAT ? "the HDR picture for frame generation"
|
|
: "the HDR composition's picture: PQ, or tone-mapped SDR with HDR output off");
|
|
} else if (scene_fmt != g_scene_fmt) {
|
|
int saved = g_context;
|
|
for (g_context = 0; g_context < FX_CONTEXTS; g_context++) scene_release();
|
|
g_context = saved;
|
|
g_scene_fmt = scene_fmt;
|
|
}
|
|
}
|
|
|
|
static int objects_ensure(void) {
|
|
if (g_ready) return g_ready == 1 ? 0 : -1;
|
|
if (!g_dev || !g_vk.CreateGraphicsPipelines) { g_ready = -1; droiddeck_log("error", "effects: no device for the chain"); return -1; }
|
|
VkAttachmentDescription att = {.format = g_fx_fmt, .samples = VK_SAMPLE_COUNT_1_BIT,
|
|
.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
|
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, .finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
|
|
VkAttachmentReference ref = {0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
|
|
VkSubpassDescription sub = {.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, .colorAttachmentCount = 1, .pColorAttachments = &ref};
|
|
VkSubpassDependency deps[2] = {
|
|
{.srcSubpass = VK_SUBPASS_EXTERNAL, .dstSubpass = 0,
|
|
.srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT,
|
|
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT},
|
|
{.srcSubpass = 0, .dstSubpass = VK_SUBPASS_EXTERNAL,
|
|
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT}};
|
|
VkRenderPassCreateInfo rci = {.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, .attachmentCount = 1, .pAttachments = &att,
|
|
.subpassCount = 1, .pSubpasses = &sub, .dependencyCount = 2, .pDependencies = deps};
|
|
if (g_vk.CreateRenderPass(g_dev, &rci, NULL, &g_rp) != VK_SUCCESS) goto fail;
|
|
|
|
VkAttachmentDescription smaa_att = att;
|
|
smaa_att.format = FX_FORMAT;
|
|
VkRenderPassCreateInfo smaa_rci = rci;
|
|
smaa_rci.pAttachments = &smaa_att;
|
|
if (g_vk.CreateRenderPass(g_dev, &smaa_rci, NULL, &g_smaa_rp) != VK_SUCCESS) goto fail;
|
|
|
|
VkSamplerCreateInfo sci = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_LINEAR,
|
|
.minFilter = VK_FILTER_LINEAR, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
|
|
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
|
|
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE};
|
|
if (g_vk.CreateSampler(g_dev, &sci, NULL, &g_sampler) != VK_SUCCESS) goto fail;
|
|
|
|
VkDescriptorSetLayoutBinding b = {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
.descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT};
|
|
VkDescriptorSetLayoutCreateInfo dli = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = 1, .pBindings = &b};
|
|
if (g_vk.CreateDescriptorSetLayout(g_dev, &dli, NULL, &g_dsl) != VK_SUCCESS) goto fail;
|
|
VkPushConstantRange pcr = {.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, .offset = 0, .size = PC_MAX_SIZE};
|
|
VkDescriptorSetLayout set_layouts[2] = {g_dsl, g_dsl};
|
|
VkPipelineLayoutCreateInfo pli = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 2, .pSetLayouts = set_layouts,
|
|
.pushConstantRangeCount = 1, .pPushConstantRanges = &pcr};
|
|
if (g_vk.CreatePipelineLayout(g_dev, &pli, NULL, &g_pl) != VK_SUCCESS) goto fail;
|
|
VkDescriptorPoolSize ps = {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, FX_CONTEXTS * 5 + 2};
|
|
VkDescriptorPoolCreateInfo dpi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
|
.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, .maxSets = FX_CONTEXTS * 5 + 2,
|
|
.poolSizeCount = 1, .pPoolSizes = &ps};
|
|
if (g_vk.CreateDescriptorPool(g_dev, &dpi, NULL, &g_dpool) != VK_SUCCESS) goto fail;
|
|
|
|
g_upscale_half = upscale_half_set();
|
|
g_nis_fast = 0;
|
|
g_nis_attempted = 0;
|
|
g_ready = 1;
|
|
droiddeck_log("effects", "shared resources ready on %s; effect pipelines are created when needed", vkp_gpu_name());
|
|
return 0;
|
|
fail:
|
|
objects_destroy();
|
|
g_ready = -1;
|
|
droiddeck_log("error", "effects: the screen-effect chain could not be built on this driver; effects are off for this session");
|
|
return -1;
|
|
}
|
|
|
|
static int chain_failed(void) {
|
|
objects_destroy();
|
|
g_ready = -1;
|
|
droiddeck_log("error", "effects: the screen-effect chain could not be built on this driver; effects are off for this session");
|
|
return -1;
|
|
}
|
|
|
|
static int pipelines_ensure(int spatial, const struct fx_settings *s) {
|
|
VkShaderModule vert = VK_NULL_HANDLE;
|
|
#define MKL(needed, slot, code, name, render_pass, layout) do { \
|
|
if ((needed) && !g_pipe[slot]) { \
|
|
if (!vert && !(vert = shader(upscale_vert_code, sizeof(upscale_vert_code)))) goto fail; \
|
|
g_pipe[slot] = post_pipeline_for_renderpass(vert, code, sizeof(code), render_pass, name, layout); \
|
|
if (!g_pipe[slot]) goto fail; \
|
|
} \
|
|
} while (0)
|
|
#define MK(needed, slot, code, name) MKL(needed, slot, code, name, g_rp, g_pl)
|
|
if (spatial == 3) {
|
|
if (g_upscale_half) MK(1, P_SGSR, sgsr_half_code, "sgsr_half");
|
|
else MK(1, P_SGSR, sgsr_code, "sgsr");
|
|
}
|
|
if (spatial == 8) {
|
|
if (g_upscale_half) MK(1, P_SGSR_HQ, sgsr_quality_half_code, "sgsr_quality_half");
|
|
else MK(1, P_SGSR_HQ, sgsr_quality_code, "sgsr_quality");
|
|
}
|
|
if (spatial == 4 || spatial == 5) {
|
|
if (g_upscale_half) MK(1, P_EASU, fsr_easu_half_code, "fsr_easu_half");
|
|
else MK(1, P_EASU, fsr_easu_code, "fsr_easu");
|
|
if (s->sharp_pct != 0) {
|
|
if (g_upscale_half) MK(1, P_RCAS, fsr_rcas_half_code, "fsr_rcas_half");
|
|
else MK(1, P_RCAS, fsr_rcas_code, "fsr_rcas");
|
|
}
|
|
}
|
|
if (spatial == 6) {
|
|
if (g_upscale_half) MK(1, P_RCAS, fsr_rcas_half_code, "fsr_rcas_half");
|
|
else MK(1, P_RCAS, fsr_rcas_code, "fsr_rcas");
|
|
}
|
|
if (spatial == 7) {
|
|
if (g_upscale_half && !g_nis_attempted) {
|
|
g_nis_attempted = 1;
|
|
if (!fx_alpha_holds_luma(g_fx_fmt))
|
|
droiddeck_log("effects", "NIS uses nis.frag: the chain format has no 8-bit alpha for the luma prepass");
|
|
else if (coef_create() != 0)
|
|
droiddeck_log("effects", "NIS coefficient buffer unavailable; NIS uses nis.frag");
|
|
else {
|
|
if (!vert && !(vert = shader(upscale_vert_code, sizeof(upscale_vert_code)))) goto fail;
|
|
g_pipe[P_NIS] = post_pipeline_for_renderpass(vert, nis3_half_code, sizeof(nis3_half_code), g_rp, "nis3_half", g_pl_nis);
|
|
if (g_pipe[P_NIS])
|
|
g_pipe[P_NIS_LUMA] = post_pipeline_for_renderpass(vert, nis_luma_half_code, sizeof(nis_luma_half_code), g_rp, "nis_luma_half", g_pl);
|
|
if (g_pipe[P_NIS] && g_pipe[P_NIS_LUMA]) g_nis_fast = 1;
|
|
else {
|
|
if (g_pipe[P_NIS]) g_vk.DestroyPipeline(g_dev, g_pipe[P_NIS], NULL);
|
|
if (g_pipe[P_NIS_LUMA]) g_vk.DestroyPipeline(g_dev, g_pipe[P_NIS_LUMA], NULL);
|
|
g_pipe[P_NIS] = VK_NULL_HANDLE;
|
|
g_pipe[P_NIS_LUMA] = VK_NULL_HANDLE;
|
|
coef_destroy();
|
|
droiddeck_log("effects", "fast NIS pipelines failed to build; NIS uses nis.frag");
|
|
}
|
|
}
|
|
}
|
|
if (!g_nis_fast) MK(1, P_NIS, nis_code, "nis");
|
|
else if (g_upscale_half) MK(1, P_SGSR, sgsr_half_code, "sgsr_half");
|
|
}
|
|
if (cas_active(s)) {
|
|
if (g_upscale_half) MK(1, P_CAS, cas_ffx_half_code, "cas_ffx_half");
|
|
else MK(1, P_CAS, cas_code, "cas");
|
|
}
|
|
if (s->color_on && !(s->toon && g_fx_fmt == FX_FORMAT)) MK(1, P_COLOR, color_code, "color");
|
|
if (s->aa_mode == 1 || s->aa_mode == 2) MK(1, P_FXAA, fxaa_code, "fxaa");
|
|
if (s->aa_mode == 2) {
|
|
MKL(1, P_SMAA_EDGE, smaa_edge_code, "smaa_edge", g_smaa_rp, g_pl);
|
|
MKL(1, P_SMAA_WEIGHTS, smaa_weights_code, "smaa_weights", g_smaa_rp, g_pl);
|
|
MK(1, P_SMAA_BLEND, smaa_blend_code, "smaa_blend");
|
|
}
|
|
if (s->toon && s->color_on && g_fx_fmt == FX_FORMAT) MK(1, P_TOON_COLOR, toon_color_code, "toon_color");
|
|
else if (s->toon) MK(1, P_TOON, toon_code, "toon");
|
|
if (s->hdr_on) MK(1, P_HDR, hdr_code, "hdr");
|
|
if (s->ntsc) MK(1, P_NTSC, ntsc_code, "ntsc");
|
|
if (s->crt) MK(1, P_CRT, crt_code, "crt");
|
|
if (deband_active(s)) MK(1, P_DEBAND, deband_code, "deband");
|
|
#undef MK
|
|
#undef MKL
|
|
if (vert) g_vk.DestroyShaderModule(g_dev, vert, NULL);
|
|
return 0;
|
|
fail:
|
|
if (vert) g_vk.DestroyShaderModule(g_dev, vert, NULL);
|
|
return chain_failed();
|
|
}
|
|
|
|
|
|
static void barrier(VkCommandBuffer cmd, VkImage img, VkImageLayout from, VkImageLayout to,
|
|
VkPipelineStageFlags src_stage, VkAccessFlags src_access,
|
|
VkPipelineStageFlags dst_stage, VkAccessFlags dst_access) {
|
|
VkImageMemoryBarrier b = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .oldLayout = from, .newLayout = to,
|
|
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
.image = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
|
|
.srcAccessMask = src_access, .dstAccessMask = dst_access};
|
|
g_vk.CmdPipelineBarrier(cmd, src_stage, dst_stage, 0, 0, NULL, 0, NULL, 1, &b);
|
|
}
|
|
|
|
static void pass_renderpass(VkCommandBuffer cmd, VkRenderPass render_pass,
|
|
VkPipeline p, struct fx_target *dst, VkDescriptorSet src,
|
|
const void *pc, uint32_t pcsz) {
|
|
VkRenderPassBeginInfo rpi = {.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = render_pass, .framebuffer = dst->fb,
|
|
.renderArea = {{0, 0}, {(uint32_t)dst->w, (uint32_t)dst->h}}};
|
|
g_vk.CmdBeginRenderPass(cmd, &rpi, VK_SUBPASS_CONTENTS_INLINE);
|
|
g_vk.CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p);
|
|
g_vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, g_pl, 0, 1, &src, 0, NULL);
|
|
if (g_nis_fast && p == g_pipe[P_NIS] && g_coef_ds) /* nis3: coefficient banks at set 1 */
|
|
g_vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, g_pl_nis, 1, 1, &g_coef_ds, 0, NULL);
|
|
g_vk.CmdPushConstants(cmd, g_pl, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, pcsz, pc);
|
|
g_vk.CmdDraw(cmd, 4, 1, 0, 0);
|
|
g_vk.CmdEndRenderPass(cmd);
|
|
}
|
|
|
|
static void pass(VkCommandBuffer cmd, VkPipeline p, struct fx_target *dst,
|
|
VkDescriptorSet src, const void *pc, uint32_t pcsz) {
|
|
pass_renderpass(cmd, g_rp, p, dst, src, pc, pcsz);
|
|
}
|
|
|
|
static void pass_dual(VkCommandBuffer cmd, VkPipeline p, struct fx_target *dst,
|
|
VkDescriptorSet src, VkDescriptorSet second,
|
|
const void *pc, uint32_t pcsz) {
|
|
VkRenderPassBeginInfo rpi = {.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = g_rp, .framebuffer = dst->fb,
|
|
.renderArea = {{0, 0}, {(uint32_t)dst->w, (uint32_t)dst->h}}};
|
|
VkDescriptorSet sets[2] = {src, second};
|
|
g_vk.CmdBeginRenderPass(cmd, &rpi, VK_SUBPASS_CONTENTS_INLINE);
|
|
g_vk.CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, p);
|
|
g_vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, g_pl, 0, 2, sets, 0, NULL);
|
|
g_vk.CmdPushConstants(cmd, g_pl, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, pcsz, pc);
|
|
g_vk.CmdDraw(cmd, 4, 1, 0, 0);
|
|
g_vk.CmdEndRenderPass(cmd);
|
|
}
|
|
|
|
static const float FULL_NDC[4] = {-1.0f, -1.0f, 1.0f, 1.0f};
|
|
#define SET_NDC(pc) memcpy((pc).ndc, FULL_NDC, sizeof(FULL_NDC))
|
|
|
|
struct fx_source_state {
|
|
VkImageLayout layout;
|
|
VkPipelineStageFlags stage;
|
|
VkAccessFlags access;
|
|
};
|
|
|
|
static VkImage effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int scene_h,
|
|
int out_w, int out_h, int *res_w, int *res_h,
|
|
VkImageLayout input_layout, const struct fx_settings *frozen,
|
|
struct fx_source_state *source_state) {
|
|
struct fx_settings snapshot;
|
|
const struct fx_settings *s;
|
|
if (frozen) {
|
|
snapshot = *frozen;
|
|
} else {
|
|
vkp_effects_sync();
|
|
pthread_mutex_lock(&g_lock);
|
|
snapshot = g_cur;
|
|
pthread_mutex_unlock(&g_lock);
|
|
}
|
|
s = &snapshot;
|
|
*res_w = scene_w; *res_h = scene_h;
|
|
|
|
int cw = out_w > 0 ? out_w : scene_w, ch = out_h > 0 ? out_h : scene_h;
|
|
if (cw > FX_MAX_DIM) cw = FX_MAX_DIM;
|
|
if (ch > FX_MAX_DIM) ch = FX_MAX_DIM;
|
|
if (cw < 1) cw = 1;
|
|
if (ch < 1) ch = 1;
|
|
const int upscaling = cw > scene_w || ch > scene_h;
|
|
int spatial = 0;
|
|
if (spatial_mode(s->scaling) && (s->scaling == 6 || upscaling)) spatial = s->scaling;
|
|
/* NIS here is the SDR implementation and is documented for <=2x upscales. */
|
|
const int nis_fallback = spatial == 7 &&
|
|
(g_scene_is_pq || cw < scene_w || ch < scene_h ||
|
|
(double)cw > (double)scene_w * 2.0 ||
|
|
(double)ch > (double)scene_h * 2.0);
|
|
static int last_nis_fallback = 0;
|
|
const int was_nis_fallback =
|
|
__atomic_exchange_n(&last_nis_fallback, nis_fallback, __ATOMIC_RELAXED);
|
|
if (nis_fallback) {
|
|
spatial = 4; /* FSR fallback; keep the saved NIS selection unchanged. */
|
|
if (!was_nis_fallback)
|
|
droiddeck_log("effects", "NIS unsupported for this output; FSR fallback active");
|
|
} else if (was_nis_fallback && s->scaling == 7) {
|
|
droiddeck_log("effects", "NIS supported again; NIS active");
|
|
}
|
|
const int nfx = effect_count(s);
|
|
const int resize = cw != scene_w || ch != scene_h;
|
|
const int fuse_toon_color = s->toon && s->color_on && g_fx_fmt == FX_FORMAT;
|
|
const int fuse_fxaa_toon = 0;
|
|
const int chain_passes = ((spatial || resize) ? 1 : 0) +
|
|
effect_count(s) - (fuse_toon_color ? 1 : 0) -
|
|
(fuse_fxaa_toon ? 1 : 0);
|
|
|
|
if ((spatial || nfx) && objects_ensure() == 0 && pipelines_ensure(spatial, s) == 0 && scene_ensure(scene, scene_w, scene_h) == 0 &&
|
|
target_ensure(&g_t[0], cw, ch, FX_TARGET_USAGE, "chain") == 0 && (chain_passes < 2 || target_ensure(&g_t[1], cw, ch, FX_TARGET_USAGE, "chain") == 0) &&
|
|
(!(spatial == 4 || spatial == 5) || s->sharp_pct == 0 ||
|
|
target_ensure(&g_mid, cw, ch, FX_SCRATCH_USAGE, "EASU") == 0)) {
|
|
if (s->aa_mode == 2 &&
|
|
(target_ensure_format(&g_smaa_edges, cw, ch, FX_SCRATCH_USAGE,
|
|
"SMAA edges", FX_FORMAT, g_smaa_rp) != 0 ||
|
|
target_ensure_format(&g_smaa_weights, cw, ch, FX_SCRATCH_USAGE,
|
|
"SMAA weights", FX_FORMAT, g_smaa_rp) != 0)) {
|
|
droiddeck_log("error", "effects: SMAA scratch allocation failed; falling back to FXAA");
|
|
snapshot.aa_mode = 1;
|
|
s = &snapshot;
|
|
}
|
|
|
|
VkDescriptorSet cur_ds;
|
|
struct fx_target *cur = NULL;
|
|
#define FREE_TARGET() ((cur == &g_t[0]) ? &g_t[1] : &g_t[0])
|
|
const float sharp01 = (float)s->sharp_pct / 100.0f;
|
|
|
|
VkViewport chain_vp = {0, 0, (float)cw, (float)ch, 0, 1};
|
|
VkRect2D chain_sc = {{0, 0}, {(uint32_t)cw, (uint32_t)ch}};
|
|
g_vk.CmdSetViewport(cmd, 0, 1, &chain_vp);
|
|
g_vk.CmdSetScissor(cmd, 0, 1, &chain_sc);
|
|
|
|
if (spatial) {
|
|
if (source_state) *source_state = (struct fx_source_state){VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT};
|
|
barrier(cmd, scene, input_layout, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
|
|
struct fx_target *dst = FREE_TARGET();
|
|
if (spatial == 3 || spatial == 8 || spatial == 7) {
|
|
struct pc_sgsr pc = {0};
|
|
SET_NDC(pc);
|
|
pc.vp[0] = 1.0f / (float)scene_w; pc.vp[1] = 1.0f / (float)scene_h;
|
|
pc.vp[2] = (float)scene_w; pc.vp[3] = (float)scene_h;
|
|
pc.edge = spatial == 7 ? sharp01 : 0.5f + sharp01 * 4.0f;
|
|
VkDescriptorSet src = g_scene.ds;
|
|
int nis_pipe = P_NIS;
|
|
if (spatial == 7 && g_nis_fast &&
|
|
target_ensure(&g_nissrc, scene_w, scene_h, FX_SCRATCH_USAGE, "NIS luma") != 0) {
|
|
/* nis3 needs the prepass image; without it this frame takes SGSR from the scene
|
|
* instead of dropping every effect */
|
|
static int warned;
|
|
if (!__atomic_exchange_n(&warned, 1, __ATOMIC_RELAXED))
|
|
droiddeck_log("error", "effects: NIS luma target allocation failed; SGSR instead");
|
|
nis_pipe = P_SGSR;
|
|
pc.edge = 0.5f + sharp01 * 4.0f;
|
|
} else if (spatial == 7 && g_nis_fast) {
|
|
/* NIS prepass: scene -> g_nissrc 1:1 (rgb + luma in alpha); only ndc is read. */
|
|
struct pc_res pl = {0};
|
|
SET_NDC(pl);
|
|
VkViewport scene_vp = {0, 0, (float)scene_w, (float)scene_h, 0, 1};
|
|
VkRect2D scene_sc = {{0, 0}, {(uint32_t)scene_w, (uint32_t)scene_h}};
|
|
g_vk.CmdSetViewport(cmd, 0, 1, &scene_vp);
|
|
g_vk.CmdSetScissor(cmd, 0, 1, &scene_sc);
|
|
pass(cmd, g_pipe[P_NIS_LUMA], &g_nissrc, g_scene.ds, &pl, sizeof(pl.ndc));
|
|
g_vk.CmdSetViewport(cmd, 0, 1, &chain_vp);
|
|
g_vk.CmdSetScissor(cmd, 0, 1, &chain_sc);
|
|
src = g_nissrc.ds;
|
|
}
|
|
pass(cmd, g_pipe[spatial == 3 ? P_SGSR : spatial == 8 ? P_SGSR_HQ : nis_pipe], dst, src, &pc, sizeof(pc));
|
|
} else if (spatial == 6) {
|
|
struct pc_rcas pc = {0};
|
|
SET_NDC(pc);
|
|
rcas_con(&pc, sharp01);
|
|
pc.outW = (float)scene_w; pc.outH = (float)scene_h;
|
|
pass(cmd, g_pipe[P_RCAS], dst, g_scene.ds, &pc, sizeof(pc));
|
|
} else {
|
|
|
|
|
|
struct pc_easu e = {0};
|
|
struct pc_rcas r = {0};
|
|
SET_NDC(e);
|
|
easu_con(&e, (float)scene_w, (float)scene_h, (float)cw, (float)ch);
|
|
e.outW = (float)cw; e.outH = (float)ch;
|
|
struct fx_target *easu_dst = sharp01 == 0.0f ? dst : &g_mid;
|
|
pass(cmd, g_pipe[P_EASU], easu_dst, g_scene.ds, &e, sizeof(e));
|
|
if (sharp01 != 0.0f) {
|
|
SET_NDC(r);
|
|
rcas_con(&r, sharp01);
|
|
r.outW = (float)cw; r.outH = (float)ch;
|
|
pass(cmd, g_pipe[P_RCAS], dst, g_mid.ds, &r, sizeof(r));
|
|
}
|
|
}
|
|
cur = dst; cur_ds = dst->ds;
|
|
} else if (resize) {
|
|
struct fx_target *dst = FREE_TARGET();
|
|
if (source_state) *source_state = (struct fx_source_state){VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT};
|
|
barrier(cmd, scene, input_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
|
barrier(cmd, dst->img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT);
|
|
VkImageBlit bl = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .srcOffsets = {{0, 0, 0}, {scene_w, scene_h, 1}},
|
|
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .dstOffsets = {{0, 0, 0}, {cw, ch, 1}}};
|
|
g_vk.CmdBlitImage(cmd, scene, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst->img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
1, &bl, s->scaling == 2 ? VK_FILTER_NEAREST : VK_FILTER_LINEAR);
|
|
barrier(cmd, dst->img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
|
|
cur = dst; cur_ds = dst->ds;
|
|
} else {
|
|
if (source_state) *source_state = (struct fx_source_state){VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT};
|
|
barrier(cmd, scene, input_layout, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT);
|
|
cur = NULL; cur_ds = g_scene.ds;
|
|
}
|
|
|
|
enum { FX_FXAA, FX_FXAA_TOON, FX_SMAA, FX_TOON, FX_COLOR, FX_TOON_COLOR, FX_CAS, FX_HDR, FX_NTSC, FX_CRT, FX_DEBAND };
|
|
int order[8], n = 0;
|
|
if (s->aa_mode == 1)
|
|
order[n++] = fuse_fxaa_toon ? FX_FXAA_TOON : FX_FXAA;
|
|
else if (s->aa_mode == 2) order[n++] = FX_SMAA;
|
|
if (s->toon && s->color_on && g_fx_fmt == FX_FORMAT) {
|
|
order[n++] = FX_TOON_COLOR;
|
|
} else {
|
|
if (s->toon && !fuse_fxaa_toon) order[n++] = FX_TOON;
|
|
if (s->color_on) order[n++] = FX_COLOR;
|
|
}
|
|
if (cas_active(s)) order[n++] = FX_CAS;
|
|
if (s->hdr_on) order[n++] = FX_HDR;
|
|
if (s->ntsc) order[n++] = FX_NTSC;
|
|
if (s->crt) order[n++] = FX_CRT;
|
|
if (deband_active(s)) order[n++] = FX_DEBAND;
|
|
for (int i = 0; i < n; i++) {
|
|
struct fx_target *dst = FREE_TARGET();
|
|
struct pc_res pr = {0};
|
|
SET_NDC(pr);
|
|
pr.res[0] = (float)cw; pr.res[1] = (float)ch;
|
|
switch (order[i]) {
|
|
case FX_FXAA: pass(cmd, g_pipe[P_FXAA], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_FXAA_TOON: pass(cmd, g_pipe[P_FXAA_TOON], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_SMAA:
|
|
pass_renderpass(cmd, g_smaa_rp, g_pipe[P_SMAA_EDGE],
|
|
&g_smaa_edges, cur_ds, &pr, sizeof(pr));
|
|
pass_renderpass(cmd, g_smaa_rp, g_pipe[P_SMAA_WEIGHTS],
|
|
&g_smaa_weights, g_smaa_edges.ds, &pr, sizeof(pr));
|
|
pass_dual(cmd, g_pipe[P_SMAA_BLEND], dst, cur_ds,
|
|
g_smaa_weights.ds, &pr, sizeof(pr));
|
|
break;
|
|
case FX_TOON: pass(cmd, g_pipe[P_TOON], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_TOON_COLOR: {
|
|
struct pc_toon_color pc = {0};
|
|
SET_NDC(pc);
|
|
pc.res[0] = (float)cw;
|
|
pc.res[1] = (float)ch;
|
|
pc.b = s->brightness;
|
|
pc.c = s->contrast;
|
|
pc.g = s->gamma;
|
|
pc.s = s->saturation;
|
|
pass(cmd, g_pipe[P_TOON_COLOR], dst, cur_ds,
|
|
&pc, sizeof(pc));
|
|
break;
|
|
}
|
|
case FX_COLOR: {
|
|
struct pc_color pc = {0};
|
|
SET_NDC(pc);
|
|
pc.b = s->brightness; pc.c = s->contrast; pc.g = s->gamma; pc.s = s->saturation;
|
|
pass(cmd, g_pipe[P_COLOR], dst, cur_ds, &pc, sizeof(pc));
|
|
break;
|
|
}
|
|
case FX_CAS: pr.extra = (float)s->cas_pct / 100.0f; pass(cmd, g_pipe[P_CAS], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_HDR: pass(cmd, g_pipe[P_HDR], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_NTSC:
|
|
pass(cmd, g_pipe[P_NTSC], dst, cur_ds, &pr, sizeof(pr));
|
|
break;
|
|
case FX_CRT: pass(cmd, g_pipe[P_CRT], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
case FX_DEBAND: pr.extra = (float)s->deband_pct / 100.0f; pass(cmd, g_pipe[P_DEBAND], dst, cur_ds, &pr, sizeof(pr)); break;
|
|
}
|
|
cur = dst; cur_ds = dst->ds;
|
|
}
|
|
|
|
#undef FREE_TARGET
|
|
if (cur) {
|
|
barrier(cmd, cur->img, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
|
*res_w = cur->w; *res_h = cur->h;
|
|
return cur->img;
|
|
}
|
|
barrier(cmd, scene, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
|
return scene;
|
|
}
|
|
|
|
if (source_state) *source_state = (struct fx_source_state){VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT};
|
|
barrier(cmd, scene, input_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
|
return scene;
|
|
}
|
|
|
|
VkImage vkp_effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int scene_h,
|
|
int out_w, int out_h, int *res_w, int *res_h) {
|
|
return effects_run(cmd, scene, scene_w, scene_h, out_w, out_h, res_w, res_h,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, NULL, NULL);
|
|
}
|
|
|
|
int vkp_effects_prepare_generated(void) {
|
|
vkp_effects_sync();
|
|
pthread_mutex_lock(&g_lock);
|
|
g_generated_settings = g_cur;
|
|
pthread_mutex_unlock(&g_lock);
|
|
return spatial_mode(g_generated_settings.scaling) && g_generated_settings.scaling != 6 &&
|
|
effect_count(&g_generated_settings) == 0;
|
|
}
|
|
|
|
VkImage vkp_effects_run_before_generated(VkCommandBuffer cmd, VkImage scene, int w, int h,
|
|
int *res_w, int *res_h) {
|
|
struct fx_settings settings = g_generated_settings;
|
|
settings.scaling = 0;
|
|
return effects_run(cmd, scene, w, h, w, h, res_w, res_h,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &settings, NULL);
|
|
}
|
|
|
|
VkImage vkp_effects_run_generated(VkCommandBuffer cmd, VkImage scene, int scene_w, int scene_h,
|
|
int out_w, int out_h, int slot, int *res_w, int *res_h) {
|
|
if (slot < 0 || slot > VKP_FG_MAX_GENERATIONS) {
|
|
*res_w = scene_w; *res_h = scene_h;
|
|
return scene;
|
|
}
|
|
g_context = slot + 1;
|
|
struct fx_source_state source_state = {0};
|
|
VkImage result = effects_run(cmd, scene, scene_w, scene_h, out_w, out_h, res_w, res_h,
|
|
VK_IMAGE_LAYOUT_GENERAL, &g_generated_settings, &source_state);
|
|
barrier(cmd, result, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT);
|
|
if (result != scene)
|
|
barrier(cmd, scene, source_state.layout, VK_IMAGE_LAYOUT_GENERAL,
|
|
source_state.stage, source_state.access,
|
|
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT);
|
|
g_context = 0;
|
|
return result;
|
|
}
|