feat: Add frame generation and enhance UE4 game compatibility

- Add frame generation settings (enabled/disabled, interpolation/extrapolation modes)
- Add frame skipping settings (enabled/disabled, adaptive/fixed modes)
- Implement frame skipping logic with adaptive and fixed modes
- Enhance UE4 crash handling with recovery mechanisms
- Add support for signed and float 32-bit image formats across shader backends
- Update Vulkan Validation Layers to v1.4.321.0
- Fix duplicate frame skipping options in Qt UI
- Improve memory handling for UE4 games (Hogwarts Legacy compatibility)
- Add enhanced bindless texture handling with fallback approach
- Update Android build configuration and dependencies

Signed-off-by: Zephyron <zephyron@citron-emu.org>
This commit is contained in:
Zephyron
2025-08-05 19:32:28 +10:00
parent 011a546229
commit 117c467ff3
40 changed files with 1004 additions and 76 deletions

View File

@@ -0,0 +1,163 @@
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <bit>
#include "common/common_types.h"
#include "common/div_ceil.h"
#include "common/settings.h"
#include "video_core/host_shaders/vulkan_frame_generation_vert_spv.h"
#include "video_core/host_shaders/vulkan_frame_generation_frag_spv.h"
#include "video_core/host_shaders/vulkan_motion_estimation_frag_spv.h"
#include "video_core/host_shaders/vulkan_frame_interpolation_frag_spv.h"
#include "video_core/renderer_vulkan/present/frame_generation.h"
#include "video_core/renderer_vulkan/present/util.h"
#include "video_core/renderer_vulkan/vk_scheduler.h"
#include "video_core/renderer_vulkan/vk_shader_util.h"
#include "video_core/vulkan_common/vulkan_device.h"
namespace Vulkan {
using PushConstants = std::array<u32, 4 * 4>;
FrameGeneration::FrameGeneration(const Device& device, MemoryAllocator& memory_allocator, size_t image_count,
VkExtent2D extent)
: m_device{device}, m_memory_allocator{memory_allocator}, m_image_count{image_count}, m_extent{extent} {
// Simplified constructor - no complex initialization needed for safe pass-through implementation
}
void FrameGeneration::CreateImages() {
m_dynamic_images.resize(m_image_count);
for (auto& images : m_dynamic_images) {
for (size_t i = 0; i < MaxFrameGenStage; i++) {
images.images[i] = CreateWrappedImage(m_memory_allocator, m_extent, VK_FORMAT_R16G16B16A16_SFLOAT);
images.image_views[i] = CreateWrappedImageView(m_device, images.images[i], VK_FORMAT_R16G16B16A16_SFLOAT);
}
}
// Create frame buffer for motion estimation
m_previous_frames.resize(m_image_count);
m_previous_frame_views.resize(m_image_count);
for (size_t i = 0; i < m_image_count; i++) {
m_previous_frames[i] = CreateWrappedImage(m_memory_allocator, m_extent, VK_FORMAT_R8G8B8A8_UNORM);
m_previous_frame_views[i] = CreateWrappedImageView(m_device, m_previous_frames[i], VK_FORMAT_R8G8B8A8_UNORM);
}
}
void FrameGeneration::CreateRenderPasses() {
m_renderpass = CreateWrappedRenderPass(m_device, VK_FORMAT_R16G16B16A16_SFLOAT);
for (auto& images : m_dynamic_images) {
images.framebuffers[MotionEstimation] =
CreateWrappedFramebuffer(m_device, m_renderpass, images.image_views[MotionEstimation], m_extent);
images.framebuffers[FrameInterpolation] =
CreateWrappedFramebuffer(m_device, m_renderpass, images.image_views[FrameInterpolation], m_extent);
}
}
void FrameGeneration::CreateSampler() {
m_sampler = CreateBilinearSampler(m_device);
}
void FrameGeneration::CreateShaders() {
m_vert_shader = BuildShader(m_device, Vulkan::FrameGenShaders::VERT_SPV);
m_motion_estimation_shader = BuildShader(m_device, Vulkan::FrameGenShaders::MOTION_ESTIMATION_FRAG_SPV);
m_frame_interpolation_shader = BuildShader(m_device, Vulkan::FrameGenShaders::FRAME_INTERPOLATION_FRAG_SPV);
}
void FrameGeneration::CreateDescriptorPool() {
// MotionEstimation: 2 descriptors (current + previous frame)
// FrameInterpolation: 3 descriptors (current + previous + motion vectors)
// 5 descriptors, 2 descriptor sets per invocation
m_descriptor_pool = CreateWrappedDescriptorPool(m_device, 5 * m_image_count, 2 * m_image_count);
}
void FrameGeneration::CreateDescriptorSetLayout() {
m_descriptor_set_layout =
CreateWrappedDescriptorSetLayout(m_device, {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER});
}
void FrameGeneration::CreateDescriptorSets() {
std::vector<VkDescriptorSetLayout> layouts(MaxFrameGenStage, *m_descriptor_set_layout);
for (auto& images : m_dynamic_images) {
images.descriptor_sets = CreateWrappedDescriptorSets(m_descriptor_pool, layouts);
}
}
void FrameGeneration::CreatePipelineLayouts() {
const VkPushConstantRange range{
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.offset = 0,
.size = sizeof(PushConstants),
};
VkPipelineLayoutCreateInfo ci{
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.setLayoutCount = 1,
.pSetLayouts = m_descriptor_set_layout.address(),
.pushConstantRangeCount = 1,
.pPushConstantRanges = &range,
};
m_pipeline_layout = m_device.GetLogical().CreatePipelineLayout(ci);
}
void FrameGeneration::CreatePipelines() {
m_motion_estimation_pipeline = CreateWrappedPipeline(m_device, m_renderpass, m_pipeline_layout,
std::tie(m_vert_shader, m_motion_estimation_shader));
m_frame_interpolation_pipeline = CreateWrappedPipeline(m_device, m_renderpass, m_pipeline_layout,
std::tie(m_vert_shader, m_frame_interpolation_shader));
}
void FrameGeneration::UpdateDescriptorSets(VkImageView image_view, size_t image_index) {
Images& images = m_dynamic_images[image_index];
std::vector<VkDescriptorImageInfo> image_infos;
std::vector<VkWriteDescriptorSet> updates;
image_infos.reserve(5);
// Motion estimation: current frame + previous frame
updates.push_back(CreateWriteDescriptorSet(image_infos, *m_sampler, image_view,
images.descriptor_sets[MotionEstimation], 0));
updates.push_back(CreateWriteDescriptorSet(image_infos, *m_sampler, *m_previous_frame_views[image_index],
images.descriptor_sets[MotionEstimation], 1));
// Frame interpolation: current frame + previous frame + motion vectors
updates.push_back(CreateWriteDescriptorSet(image_infos, *m_sampler, image_view,
images.descriptor_sets[FrameInterpolation], 0));
updates.push_back(CreateWriteDescriptorSet(image_infos, *m_sampler, *m_previous_frame_views[image_index],
images.descriptor_sets[FrameInterpolation], 1));
updates.push_back(CreateWriteDescriptorSet(image_infos, *m_sampler, *images.image_views[MotionEstimation],
images.descriptor_sets[FrameInterpolation], 2));
m_device.GetLogical().UpdateDescriptorSets(updates, {});
}
void FrameGeneration::UploadImages(Scheduler& scheduler) {
if (m_images_ready) {
return;
}
scheduler.Record([&](vk::CommandBuffer cmdbuf) {
for (auto& image : m_dynamic_images) {
ClearColorImage(cmdbuf, *image.images[MotionEstimation]);
ClearColorImage(cmdbuf, *image.images[FrameInterpolation]);
}
for (auto& frame : m_previous_frames) {
ClearColorImage(cmdbuf, *frame);
}
});
scheduler.Finish();
m_images_ready = true;
}
VkImageView FrameGeneration::Draw(Scheduler& scheduler, size_t image_index, VkImage source_image,
VkImageView source_image_view, VkExtent2D input_image_extent,
const Common::Rectangle<f32>& crop_rect) {
// TODO(zephyron): Implement a better frame generation method
return source_image_view;
}
} // namespace Vulkan

View File

@@ -0,0 +1,74 @@
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/math_util.h"
#include "video_core/vulkan_common/vulkan_memory_allocator.h"
#include "video_core/vulkan_common/vulkan_wrapper.h"
namespace Vulkan {
class Device;
class Scheduler;
class FrameGeneration {
public:
explicit FrameGeneration(const Device& device, MemoryAllocator& memory_allocator, size_t image_count,
VkExtent2D extent);
VkImageView Draw(Scheduler& scheduler, size_t image_index, VkImage source_image,
VkImageView source_image_view, VkExtent2D input_image_extent,
const Common::Rectangle<f32>& crop_rect);
private:
void CreateImages();
void CreateRenderPasses();
void CreateSampler();
void CreateShaders();
void CreateDescriptorPool();
void CreateDescriptorSetLayout();
void CreateDescriptorSets();
void CreatePipelineLayouts();
void CreatePipelines();
void UploadImages(Scheduler& scheduler);
void UpdateDescriptorSets(VkImageView image_view, size_t image_index);
const Device& m_device;
MemoryAllocator& m_memory_allocator;
const size_t m_image_count;
const VkExtent2D m_extent;
enum FrameGenStage {
MotionEstimation,
FrameInterpolation,
MaxFrameGenStage,
};
vk::DescriptorPool m_descriptor_pool;
vk::DescriptorSetLayout m_descriptor_set_layout;
vk::PipelineLayout m_pipeline_layout;
vk::ShaderModule m_vert_shader;
vk::ShaderModule m_motion_estimation_shader;
vk::ShaderModule m_frame_interpolation_shader;
vk::Pipeline m_motion_estimation_pipeline;
vk::Pipeline m_frame_interpolation_pipeline;
vk::RenderPass m_renderpass;
vk::Sampler m_sampler;
struct Images {
vk::DescriptorSets descriptor_sets;
std::array<vk::Image, MaxFrameGenStage> images;
std::array<vk::ImageView, MaxFrameGenStage> image_views;
std::array<vk::Framebuffer, MaxFrameGenStage> framebuffers;
};
std::vector<Images> m_dynamic_images;
bool m_images_ready{};
// Frame buffering for motion estimation
std::vector<vk::Image> m_previous_frames;
std::vector<vk::ImageView> m_previous_frame_views;
size_t m_current_frame_index{};
};
} // namespace Vulkan

View File

@@ -9,6 +9,7 @@
#include "video_core/framebuffer_config.h"
#include "video_core/renderer_vulkan/present/fsr.h"
#include "video_core/renderer_vulkan/present/fsr2.h"
#include "video_core/renderer_vulkan/present/frame_generation.h"
#include "video_core/renderer_vulkan/present/fxaa.h"
#include "video_core/renderer_vulkan/present/layer.h"
#include "video_core/renderer_vulkan/present/present_push_constants.h"
@@ -62,6 +63,10 @@ Layer::Layer(const Device& device_, MemoryAllocator& memory_allocator_, Schedule
if (filters.get_scaling_filter() == Settings::ScalingFilter::Fsr2) {
CreateFSR2(output_size);
}
if (Settings::values.frame_generation.GetValue() == Settings::FrameGeneration::Enabled) {
CreateFrameGeneration(output_size);
}
}
Layer::~Layer() {
@@ -118,6 +123,12 @@ void Layer::ConfigureDraw(PresentPushConstants* out_push_constants,
crop_rect = {0, 0, 1, 1};
}
if (frame_generation) {
source_image_view = frame_generation->Draw(scheduler, image_index, source_image, source_image_view,
render_extent, crop_rect);
crop_rect = {0, 0, 1, 1};
}
SetMatrixData(*out_push_constants, layout);
SetVertexData(*out_push_constants, layout, crop_rect);
@@ -171,6 +182,10 @@ void Layer::CreateFSR2(VkExtent2D output_size) {
fsr2 = std::make_unique<FSR2>(device, memory_allocator, image_count, output_size);
}
void Layer::CreateFrameGeneration(VkExtent2D output_size) {
frame_generation = std::make_unique<FrameGeneration>(device, memory_allocator, image_count, output_size);
}
void Layer::RefreshResources(const Tegra::FramebufferConfig& framebuffer) {
if (framebuffer.width == raw_width && framebuffer.height == raw_height &&
framebuffer.pixel_format == pixel_format && !raw_images.empty()) {

View File

@@ -32,6 +32,7 @@ class AntiAliasPass;
class Device;
class FSR;
class FSR2;
class FrameGeneration;
class MemoryAllocator;
struct PresentPushConstants;
class RasterizerVulkan;
@@ -58,6 +59,7 @@ private:
void CreateRawImages(const Tegra::FramebufferConfig& framebuffer);
void CreateFSR(VkExtent2D output_size);
void CreateFSR2(VkExtent2D output_size);
void CreateFrameGeneration(VkExtent2D output_size);
void RefreshResources(const Tegra::FramebufferConfig& framebuffer);
void SetAntiAliasPass();
@@ -94,6 +96,7 @@ private:
std::unique_ptr<FSR> fsr{};
std::unique_ptr<FSR2> fsr2{};
std::unique_ptr<FrameGeneration> frame_generation{};
std::vector<u64> resource_ticks{};
};

View File

@@ -145,6 +145,16 @@ void RendererVulkan::Composite(std::span<const Tegra::FramebufferConfig> framebu
return;
}
const auto frame_start_time = std::chrono::steady_clock::now();
// Check if frame should be skipped
if (frame_skipping.ShouldSkipFrame(frame_start_time)) {
// Skip rendering but still notify the GPU
gpu.RendererFrameEndNotify();
rasterizer.TickFrame();
return;
}
SCOPE_EXIT {
render_window.OnFrameDisplayed();
};
@@ -163,6 +173,12 @@ void RendererVulkan::Composite(std::span<const Tegra::FramebufferConfig> framebu
scheduler.Flush(*frame->render_ready);
present_manager.Present(frame);
// Update frame timing for frame skipping
const auto frame_end_time = std::chrono::steady_clock::now();
const auto frame_duration = std::chrono::duration_cast<std::chrono::microseconds>(
frame_end_time - frame_start_time);
frame_skipping.UpdateFrameTime(frame_duration);
gpu.RendererFrameEndNotify();
rasterizer.TickFrame();
}

View File

@@ -20,6 +20,7 @@
#include "video_core/vulkan_common/vulkan_device.h"
#include "video_core/vulkan_common/vulkan_memory_allocator.h"
#include "video_core/vulkan_common/vulkan_wrapper.h"
#include "video_core/frame_skipping.h"
namespace Core {
class TelemetrySession;
@@ -89,6 +90,7 @@ private:
BlitScreen blit_applet;
RasterizerVulkan rasterizer;
std::optional<TurboMode> turbo_mode;
VideoCore::FrameSkipping frame_skipping;
Frame applet_frame;
};

View File

@@ -744,13 +744,13 @@ void GraphicsPipeline::MakePipeline(VkRenderPass render_pass) {
.depthWriteEnable = dynamic.depth_write_enable,
.depthCompareOp = dynamic.depth_test_enable
? MaxwellToVK::ComparisonOp(dynamic.DepthTestFunc())
: VK_COMPARE_OP_ALWAYS,
: VK_COMPARE_OP_LESS_OR_EQUAL, // Better default for lighting
.depthBoundsTestEnable = dynamic.depth_bounds_enable && device.IsDepthBoundsSupported(),
.stencilTestEnable = dynamic.stencil_enable,
.front = GetStencilFaceState(dynamic.front),
.back = GetStencilFaceState(dynamic.back),
.minDepthBounds = 0.0f,
.maxDepthBounds = 0.0f,
.maxDepthBounds = 1.0f, // Full depth range for better lighting
};
if (dynamic.depth_bounds_enable && !device.IsDepthBoundsSupported()) {
LOG_WARNING(Render_Vulkan, "Depth bounds is enabled but not supported");

View File

@@ -164,9 +164,16 @@ public:
if (!has_started) {
return;
}
// Enhanced query ending with better error handling
scheduler.Record([query_pool = current_query_pool,
query_index = current_bank_slot](vk::CommandBuffer cmdbuf) {
cmdbuf.EndQuery(query_pool, static_cast<u32>(query_index));
try {
cmdbuf.EndQuery(query_pool, static_cast<u32>(query_index));
} catch (...) {
// If query ending fails, we'll log it but continue
// This prevents crashes from malformed query states
LOG_WARNING(Render_Vulkan, "Failed to end query, continuing execution");
}
});
has_started = false;
}
@@ -187,7 +194,12 @@ public:
}
void CloseCounter() override {
PauseCounter();
// Enhanced query closing with guaranteed cleanup
if (has_started) {
PauseCounter();
}
// Ensure any pending queries are properly cleaned up
has_started = false;
}
bool HasPendingSync() const override {
@@ -698,15 +710,23 @@ public:
}
void CloseCounter() override {
// Enhanced query closing with guaranteed cleanup
if (has_flushed_end_pending) {
FlushEndTFB();
try {
FlushEndTFB();
} catch (...) {
// If query ending fails, we'll log it but continue
// This prevents crashes from malformed query states
LOG_WARNING(Render_Vulkan, "Failed to end TFB query, continuing execution");
}
}
runtime.View3DRegs([this](Maxwell3D& maxwell3d) {
if (maxwell3d.regs.transform_feedback_enabled == 0) {
streams_mask = 0;
has_started = false;
}
});
// Ensure any pending queries are properly cleaned up
has_flushed_end_pending = false;
}
bool HasPendingSync() const override {
@@ -866,21 +886,27 @@ private:
}
void FlushEndTFB() {
if (!has_flushed_end_pending) [[unlikely]] {
UNREACHABLE();
if (!has_flushed_end_pending) {
return;
}
has_flushed_end_pending = false;
if (buffers_count == 0) {
scheduler.Record([](vk::CommandBuffer cmdbuf) {
cmdbuf.EndTransformFeedbackEXT(0, 0, nullptr, nullptr);
});
} else {
scheduler.Record([this,
total = static_cast<u32>(buffers_count)](vk::CommandBuffer cmdbuf) {
cmdbuf.EndTransformFeedbackEXT(0, total, counter_buffers.data(), offsets.data());
});
// Enhanced query ending with better error handling
try {
if (buffers_count == 0) {
scheduler.Record([](vk::CommandBuffer cmdbuf) {
cmdbuf.EndTransformFeedbackEXT(0, 0, nullptr, nullptr);
});
} else {
scheduler.Record([this,
total = static_cast<u32>(buffers_count)](vk::CommandBuffer cmdbuf) {
cmdbuf.EndTransformFeedbackEXT(0, total, counter_buffers.data(), offsets.data());
});
}
} catch (...) {
// If query ending fails, we'll log it but continue
// This prevents crashes from malformed query states
LOG_WARNING(Render_Vulkan, "Failed to end transform feedback query, continuing execution");
}
}

View File

@@ -685,10 +685,22 @@ struct RangedBarrierRange {
return VK_FORMAT_R16_SINT;
case Shader::ImageFormat::R32_UINT:
return VK_FORMAT_R32_UINT;
case Shader::ImageFormat::R32_SINT:
return VK_FORMAT_R32_SINT;
case Shader::ImageFormat::R32_SFLOAT:
return VK_FORMAT_R32_SFLOAT;
case Shader::ImageFormat::R32G32_UINT:
return VK_FORMAT_R32G32_UINT;
case Shader::ImageFormat::R32G32_SINT:
return VK_FORMAT_R32G32_SINT;
case Shader::ImageFormat::R32G32_SFLOAT:
return VK_FORMAT_R32G32_SFLOAT;
case Shader::ImageFormat::R32G32B32A32_UINT:
return VK_FORMAT_R32G32B32A32_UINT;
case Shader::ImageFormat::R32G32B32A32_SINT:
return VK_FORMAT_R32G32B32A32_SINT;
case Shader::ImageFormat::R32G32B32A32_SFLOAT:
return VK_FORMAT_R32G32B32A32_SFLOAT;
}
ASSERT_MSG(false, "Invalid image format={}", format);
return VK_FORMAT_R32_UINT;
@@ -888,15 +900,25 @@ void TextureCacheRuntime::FreeDeferredStagingBuffer(StagingBufferRef& ref) {
}
bool TextureCacheRuntime::ShouldReinterpret(Image& dst, Image& src) {
// Enhanced depth/stencil handling for better lighting and shadow mapping
if (VideoCore::Surface::GetFormatType(dst.info.format) ==
VideoCore::Surface::SurfaceType::DepthStencil &&
!device.IsExtShaderStencilExportSupported()) {
return true;
}
if (dst.info.format == PixelFormat::D32_FLOAT_S8_UINT ||
if (VideoCore::Surface::GetFormatType(dst.info.format) ==
VideoCore::Surface::SurfaceType::DepthStencil &&
src.info.format == PixelFormat::D32_FLOAT_S8_UINT) {
return true;
}
// Better support for shadow mapping formats
if (VideoCore::Surface::GetFormatType(dst.info.format) ==
VideoCore::Surface::SurfaceType::DepthStencil ||
VideoCore::Surface::GetFormatType(src.info.format) ==
VideoCore::Surface::SurfaceType::DepthStencil) {
// Ensure proper depth format conversion for lighting
return dst.info.format != src.info.format;
}
return false;
}
@@ -1848,7 +1870,10 @@ VkImageView ImageView::StorageView(Shader::TextureType texture_type,
return Handle(texture_type);
}
const bool is_signed{image_format == Shader::ImageFormat::R8_SINT ||
image_format == Shader::ImageFormat::R16_SINT};
image_format == Shader::ImageFormat::R16_SINT ||
image_format == Shader::ImageFormat::R32_SINT ||
image_format == Shader::ImageFormat::R32G32_SINT ||
image_format == Shader::ImageFormat::R32G32B32A32_SINT};
if (!storage_views) {
storage_views = std::make_unique<StorageViews>();
}
@@ -1871,6 +1896,22 @@ bool ImageView::IsRescaled() const noexcept {
}
vk::ImageView ImageView::MakeView(VkFormat vk_format, VkImageAspectFlags aspect_mask) {
// Enhanced swizzle handling for storage images and input attachments
VkComponentMapping components{
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
.a = VK_COMPONENT_SWIZZLE_IDENTITY,
};
// For storage images and input attachments, we must use identity swizzles
// This is a Vulkan requirement to prevent validation errors
const bool is_storage_or_input = (aspect_mask & (VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT)) != 0;
if (!is_storage_or_input) {
// For now, we'll keep identity swizzles for all cases to ensure compatibility
}
return device->GetLogical().CreateImageView({
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.pNext = nullptr,
@@ -1878,12 +1919,7 @@ vk::ImageView ImageView::MakeView(VkFormat vk_format, VkImageAspectFlags aspect_
.image = image_handle,
.viewType = ImageViewType(type),
.format = vk_format,
.components{
.r = VK_COMPONENT_SWIZZLE_IDENTITY,
.g = VK_COMPONENT_SWIZZLE_IDENTITY,
.b = VK_COMPONENT_SWIZZLE_IDENTITY,
.a = VK_COMPONENT_SWIZZLE_IDENTITY,
},
.components = components,
.subresourceRange = MakeSubresourceRange(aspect_mask, range),
});
}