yuzu-emu
/
yuzu
Archived
1
0
Fork 0

nvnflinger/gpu: implement applet capture

This commit is contained in:
Liam 2024-01-26 16:10:21 -05:00
parent 962c82540c
commit 0cb413c3d3
19 changed files with 288 additions and 55 deletions

View File

@ -68,8 +68,12 @@ void SystemBufferManager::SetWindowVisibility(bool visible) {
Result SystemBufferManager::WriteAppletCaptureBuffer(bool* out_was_written, Result SystemBufferManager::WriteAppletCaptureBuffer(bool* out_was_written,
s32* out_fbshare_layer_index) { s32* out_fbshare_layer_index) {
// TODO if (!m_buffer_sharing_enabled) {
R_SUCCEED(); return VI::ResultPermissionDenied;
}
return m_nvnflinger->GetSystemBufferManager().WriteAppletCaptureBuffer(out_was_written,
out_fbshare_layer_index);
} }
} // namespace Service::AM } // namespace Service::AM

View File

@ -15,6 +15,7 @@
#include "core/hle/service/vi/layer/vi_layer.h" #include "core/hle/service/vi/layer/vi_layer.h"
#include "core/hle/service/vi/vi_results.h" #include "core/hle/service/vi/vi_results.h"
#include "video_core/gpu.h" #include "video_core/gpu.h"
#include "video_core/host1x/host1x.h"
namespace Service::Nvnflinger { namespace Service::Nvnflinger {
@ -414,9 +415,30 @@ Result FbShareBufferManager::GetSharedFrameBufferAcquirableEvent(Kernel::KReadab
R_SUCCEED(); R_SUCCEED();
} }
Result FbShareBufferManager::WriteAppletCaptureBuffer(bool* out_was_written, Result FbShareBufferManager::WriteAppletCaptureBuffer(bool* out_was_written, s32* out_layer_index) {
s32* out_layer_index) { std::vector<u8> capture_buffer(m_system.GPU().GetAppletCaptureBuffer());
// TODO Common::ScratchBuffer<u32> scratch;
// TODO: this could be optimized
s64 e = -1280 * 768 * 4;
for (auto& block : *m_buffer_page_group) {
u8* start = m_system.DeviceMemory().GetPointer<u8>(block.GetAddress());
u8* end = m_system.DeviceMemory().GetPointer<u8>(block.GetAddress() + block.GetSize());
for (; start < end; start++) {
*start = 0;
if (e >= 0 && e < static_cast<s64>(capture_buffer.size())) {
*start = capture_buffer[e];
}
e++;
}
m_system.GPU().Host1x().MemoryManager().ApplyOpOnPointer(start, scratch, [&](DAddr addr) {
m_system.GPU().InvalidateRegion(addr, end - start);
});
}
*out_was_written = true; *out_was_written = true;
*out_layer_index = 1; *out_layer_index = 1;
R_SUCCEED(); R_SUCCEED();

View File

@ -18,6 +18,7 @@ add_library(video_core STATIC
buffer_cache/usage_tracker.h buffer_cache/usage_tracker.h
buffer_cache/word_manager.h buffer_cache/word_manager.h
cache_types.h cache_types.h
capture.h
cdma_pusher.cpp cdma_pusher.cpp
cdma_pusher.h cdma_pusher.h
compatible_formats.cpp compatible_formats.cpp

36
src/video_core/capture.h Normal file
View File

@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/alignment.h"
#include "common/bit_util.h"
#include "common/common_types.h"
#include "core/frontend/framebuffer_layout.h"
#include "video_core/surface.h"
namespace VideoCore::Capture {
constexpr u32 BlockHeight = 4;
constexpr u32 BlockDepth = 0;
constexpr u32 BppLog2 = 2;
constexpr auto PixelFormat = Surface::PixelFormat::B8G8R8A8_UNORM;
constexpr auto LinearWidth = Layout::ScreenUndocked::Width;
constexpr auto LinearHeight = Layout::ScreenUndocked::Height;
constexpr auto LinearDepth = 1U;
constexpr auto BytesPerPixel = 4U;
constexpr auto TiledWidth = LinearWidth;
constexpr auto TiledHeight = Common::AlignUpLog2(LinearHeight, BlockHeight + BlockDepth + BppLog2);
constexpr auto TiledSize = TiledWidth * TiledHeight * (1 << BppLog2);
constexpr Layout::FramebufferLayout Layout{
.width = LinearWidth,
.height = LinearHeight,
.screen = {0, 0, LinearWidth, LinearHeight},
.is_srgb = false,
};
} // namespace VideoCore::Capture

View File

@ -347,6 +347,17 @@ struct GPU::Impl {
WaitForSyncOperation(wait_fence); WaitForSyncOperation(wait_fence);
} }
std::vector<u8> GetAppletCaptureBuffer() {
std::vector<u8> out;
const auto wait_fence =
RequestSyncOperation([&] { out = renderer->GetAppletCaptureBuffer(); });
gpu_thread.TickGPU();
WaitForSyncOperation(wait_fence);
return out;
}
GPU& gpu; GPU& gpu;
Core::System& system; Core::System& system;
Host1x::Host1x& host1x; Host1x::Host1x& host1x;
@ -505,6 +516,10 @@ void GPU::RequestComposite(std::vector<Tegra::FramebufferConfig>&& layers,
impl->RequestComposite(std::move(layers), std::move(fences)); impl->RequestComposite(std::move(layers), std::move(fences));
} }
std::vector<u8> GPU::GetAppletCaptureBuffer() {
return impl->GetAppletCaptureBuffer();
}
u64 GPU::GetTicks() const { u64 GPU::GetTicks() const {
return impl->GetTicks(); return impl->GetTicks();
} }

View File

@ -215,6 +215,8 @@ public:
void RequestComposite(std::vector<Tegra::FramebufferConfig>&& layers, void RequestComposite(std::vector<Tegra::FramebufferConfig>&& layers,
std::vector<Service::Nvidia::NvFence>&& fences); std::vector<Service::Nvidia::NvFence>&& fences);
std::vector<u8> GetAppletCaptureBuffer();
/// Performs any additional setup necessary in order to begin GPU emulation. /// Performs any additional setup necessary in order to begin GPU emulation.
/// This can be used to launch any necessary threads and register any necessary /// This can be used to launch any necessary threads and register any necessary
/// core timing events. /// core timing events.

View File

@ -40,6 +40,9 @@ public:
/// Finalize rendering the guest frame and draw into the presentation texture /// Finalize rendering the guest frame and draw into the presentation texture
virtual void Composite(std::span<const Tegra::FramebufferConfig> layers) = 0; virtual void Composite(std::span<const Tegra::FramebufferConfig> layers) = 0;
/// Get the tiled applet layer capture buffer
virtual std::vector<u8> GetAppletCaptureBuffer() = 0;
[[nodiscard]] virtual RasterizerInterface* ReadRasterizer() = 0; [[nodiscard]] virtual RasterizerInterface* ReadRasterizer() = 0;
[[nodiscard]] virtual std::string GetDeviceVendor() const = 0; [[nodiscard]] virtual std::string GetDeviceVendor() const = 0;

View File

@ -3,6 +3,7 @@
#include "core/frontend/emu_window.h" #include "core/frontend/emu_window.h"
#include "core/frontend/graphics_context.h" #include "core/frontend/graphics_context.h"
#include "video_core/capture.h"
#include "video_core/renderer_null/renderer_null.h" #include "video_core/renderer_null/renderer_null.h"
namespace Null { namespace Null {
@ -22,4 +23,8 @@ void RendererNull::Composite(std::span<const Tegra::FramebufferConfig> framebuff
render_window.OnFrameDisplayed(); render_window.OnFrameDisplayed();
} }
std::vector<u8> RendererNull::GetAppletCaptureBuffer() {
return std::vector<u8>(VideoCore::Capture::TiledSize);
}
} // namespace Null } // namespace Null

View File

@ -19,6 +19,8 @@ public:
void Composite(std::span<const Tegra::FramebufferConfig> framebuffer) override; void Composite(std::span<const Tegra::FramebufferConfig> framebuffer) override;
std::vector<u8> GetAppletCaptureBuffer() override;
VideoCore::RasterizerInterface* ReadRasterizer() override { VideoCore::RasterizerInterface* ReadRasterizer() override {
return &m_rasterizer; return &m_rasterizer;
} }

View File

@ -20,7 +20,7 @@ BlitScreen::BlitScreen(RasterizerOpenGL& rasterizer_,
BlitScreen::~BlitScreen() = default; BlitScreen::~BlitScreen() = default;
void BlitScreen::DrawScreen(std::span<const Tegra::FramebufferConfig> framebuffers, void BlitScreen::DrawScreen(std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout) { const Layout::FramebufferLayout& layout, bool invert_y) {
// TODO: Signal state tracker about these changes // TODO: Signal state tracker about these changes
state_tracker.NotifyScreenDrawVertexArray(); state_tracker.NotifyScreenDrawVertexArray();
state_tracker.NotifyPolygonModes(); state_tracker.NotifyPolygonModes();
@ -60,7 +60,7 @@ void BlitScreen::DrawScreen(std::span<const Tegra::FramebufferConfig> framebuffe
} }
CreateWindowAdapt(); CreateWindowAdapt();
window_adapt->DrawToFramebuffer(program_manager, layers, framebuffers, layout); window_adapt->DrawToFramebuffer(program_manager, layers, framebuffers, layout, invert_y);
// TODO // TODO
// program_manager.RestoreGuestPipeline(); // program_manager.RestoreGuestPipeline();

View File

@ -51,7 +51,7 @@ public:
/// Draws the emulated screens to the emulator window. /// Draws the emulated screens to the emulator window.
void DrawScreen(std::span<const Tegra::FramebufferConfig> framebuffers, void DrawScreen(std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout); const Layout::FramebufferLayout& layout, bool invert_y);
private: private:
void CreateWindowAdapt(); void CreateWindowAdapt();

View File

@ -34,7 +34,7 @@ GLuint Layer::ConfigureDraw(std::array<GLfloat, 3 * 2>& out_matrix,
std::array<ScreenRectVertex, 4>& out_vertices, std::array<ScreenRectVertex, 4>& out_vertices,
ProgramManager& program_manager, ProgramManager& program_manager,
const Tegra::FramebufferConfig& framebuffer, const Tegra::FramebufferConfig& framebuffer,
const Layout::FramebufferLayout& layout) { const Layout::FramebufferLayout& layout, bool invert_y) {
FramebufferTextureInfo info = PrepareRenderTarget(framebuffer); FramebufferTextureInfo info = PrepareRenderTarget(framebuffer);
auto crop = Tegra::NormalizeCrop(framebuffer, info.width, info.height); auto crop = Tegra::NormalizeCrop(framebuffer, info.width, info.height);
GLuint texture = info.display_texture; GLuint texture = info.display_texture;
@ -83,10 +83,15 @@ GLuint Layer::ConfigureDraw(std::array<GLfloat, 3 * 2>& out_matrix,
const auto w = screen.GetWidth(); const auto w = screen.GetWidth();
const auto h = screen.GetHeight(); const auto h = screen.GetHeight();
out_vertices[0] = ScreenRectVertex(x, y, crop.left, crop.top); const auto left = crop.left;
out_vertices[1] = ScreenRectVertex(x + w, y, crop.right, crop.top); const auto right = crop.right;
out_vertices[2] = ScreenRectVertex(x, y + h, crop.left, crop.bottom); const auto top = invert_y ? crop.bottom : crop.top;
out_vertices[3] = ScreenRectVertex(x + w, y + h, crop.right, crop.bottom); const auto bottom = invert_y ? crop.top : crop.bottom;
out_vertices[0] = ScreenRectVertex(x, y, left, top);
out_vertices[1] = ScreenRectVertex(x + w, y, right, top);
out_vertices[2] = ScreenRectVertex(x, y + h, left, bottom);
out_vertices[3] = ScreenRectVertex(x + w, y + h, right, bottom);
return texture; return texture;
} }

View File

@ -51,7 +51,7 @@ public:
std::array<ScreenRectVertex, 4>& out_vertices, std::array<ScreenRectVertex, 4>& out_vertices,
ProgramManager& program_manager, ProgramManager& program_manager,
const Tegra::FramebufferConfig& framebuffer, const Tegra::FramebufferConfig& framebuffer,
const Layout::FramebufferLayout& layout); const Layout::FramebufferLayout& layout, bool invert_y);
private: private:
/// Loads framebuffer from emulated memory into the active OpenGL texture. /// Loads framebuffer from emulated memory into the active OpenGL texture.

View File

@ -37,7 +37,7 @@ WindowAdaptPass::~WindowAdaptPass() = default;
void WindowAdaptPass::DrawToFramebuffer(ProgramManager& program_manager, std::list<Layer>& layers, void WindowAdaptPass::DrawToFramebuffer(ProgramManager& program_manager, std::list<Layer>& layers,
std::span<const Tegra::FramebufferConfig> framebuffers, std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout) { const Layout::FramebufferLayout& layout, bool invert_y) {
GLint old_read_fb; GLint old_read_fb;
GLint old_draw_fb; GLint old_draw_fb;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb); glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb);
@ -51,7 +51,7 @@ void WindowAdaptPass::DrawToFramebuffer(ProgramManager& program_manager, std::li
auto layer_it = layers.begin(); auto layer_it = layers.begin();
for (size_t i = 0; i < layer_count; i++) { for (size_t i = 0; i < layer_count; i++) {
textures[i] = layer_it->ConfigureDraw(matrices[i], vertices[i], program_manager, textures[i] = layer_it->ConfigureDraw(matrices[i], vertices[i], program_manager,
framebuffers[i], layout); framebuffers[i], layout, invert_y);
layer_it++; layer_it++;
} }

View File

@ -31,7 +31,7 @@ public:
void DrawToFramebuffer(ProgramManager& program_manager, std::list<Layer>& layers, void DrawToFramebuffer(ProgramManager& program_manager, std::list<Layer>& layers,
std::span<const Tegra::FramebufferConfig> framebuffers, std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout); const Layout::FramebufferLayout& layout, bool invert_y);
private: private:
const Device& device; const Device& device;

View File

@ -16,6 +16,7 @@
#include "core/core_timing.h" #include "core/core_timing.h"
#include "core/frontend/emu_window.h" #include "core/frontend/emu_window.h"
#include "core/telemetry_session.h" #include "core/telemetry_session.h"
#include "video_core/capture.h"
#include "video_core/renderer_opengl/gl_blit_screen.h" #include "video_core/renderer_opengl/gl_blit_screen.h"
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_manager.h" #include "video_core/renderer_opengl/gl_shader_manager.h"
@ -121,6 +122,13 @@ RendererOpenGL::RendererOpenGL(Core::TelemetrySession& telemetry_session_,
} }
blit_screen = std::make_unique<BlitScreen>(rasterizer, device_memory, state_tracker, blit_screen = std::make_unique<BlitScreen>(rasterizer, device_memory, state_tracker,
program_manager, device); program_manager, device);
blit_applet = std::make_unique<BlitScreen>(rasterizer, device_memory, state_tracker,
program_manager, device);
capture_framebuffer.Create();
capture_renderbuffer.Create();
glBindRenderbuffer(GL_RENDERBUFFER, capture_renderbuffer.handle);
glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8, VideoCore::Capture::LinearWidth,
VideoCore::Capture::LinearHeight);
} }
RendererOpenGL::~RendererOpenGL() = default; RendererOpenGL::~RendererOpenGL() = default;
@ -130,10 +138,11 @@ void RendererOpenGL::Composite(std::span<const Tegra::FramebufferConfig> framebu
return; return;
} }
RenderAppletCaptureLayer(framebuffers);
RenderScreenshot(framebuffers); RenderScreenshot(framebuffers);
state_tracker.BindFramebuffer(0); state_tracker.BindFramebuffer(0);
blit_screen->DrawScreen(framebuffers, emu_window.GetFramebufferLayout()); blit_screen->DrawScreen(framebuffers, emu_window.GetFramebufferLayout(), false);
++m_current_frame; ++m_current_frame;
@ -159,11 +168,8 @@ void RendererOpenGL::AddTelemetryFields() {
telemetry_session.AddField(user_system, "GPU_OpenGL_Version", std::string(gl_version)); telemetry_session.AddField(user_system, "GPU_OpenGL_Version", std::string(gl_version));
} }
void RendererOpenGL::RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers) { void RendererOpenGL::RenderToBuffer(std::span<const Tegra::FramebufferConfig> framebuffers,
if (!renderer_settings.screenshot_requested) { const Layout::FramebufferLayout& layout, void* dst) {
return;
}
GLint old_read_fb; GLint old_read_fb;
GLint old_draw_fb; GLint old_draw_fb;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb); glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb);
@ -173,29 +179,86 @@ void RendererOpenGL::RenderScreenshot(std::span<const Tegra::FramebufferConfig>
screenshot_framebuffer.Create(); screenshot_framebuffer.Create();
glBindFramebuffer(GL_FRAMEBUFFER, screenshot_framebuffer.handle); glBindFramebuffer(GL_FRAMEBUFFER, screenshot_framebuffer.handle);
const Layout::FramebufferLayout layout{renderer_settings.screenshot_framebuffer_layout};
GLuint renderbuffer; GLuint renderbuffer;
glGenRenderbuffers(1, &renderbuffer); glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer); glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8, layout.width, layout.height); glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8, layout.width, layout.height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
blit_screen->DrawScreen(framebuffers, layout); blit_screen->DrawScreen(framebuffers, layout, false);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glReadPixels(0, 0, layout.width, layout.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, glReadPixels(0, 0, layout.width, layout.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, dst);
renderer_settings.screenshot_bits);
screenshot_framebuffer.Release(); screenshot_framebuffer.Release();
glDeleteRenderbuffers(1, &renderbuffer); glDeleteRenderbuffers(1, &renderbuffer);
glBindFramebuffer(GL_READ_FRAMEBUFFER, old_read_fb); glBindFramebuffer(GL_READ_FRAMEBUFFER, old_read_fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, old_draw_fb); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, old_draw_fb);
}
void RendererOpenGL::RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers) {
if (!renderer_settings.screenshot_requested) {
return;
}
RenderToBuffer(framebuffers, renderer_settings.screenshot_framebuffer_layout,
renderer_settings.screenshot_bits);
renderer_settings.screenshot_complete_callback(true); renderer_settings.screenshot_complete_callback(true);
renderer_settings.screenshot_requested = false; renderer_settings.screenshot_requested = false;
} }
void RendererOpenGL::RenderAppletCaptureLayer(
std::span<const Tegra::FramebufferConfig> framebuffers) {
GLint old_read_fb;
GLint old_draw_fb;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &old_draw_fb);
glBindFramebuffer(GL_FRAMEBUFFER, capture_framebuffer.handle);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
capture_renderbuffer.handle);
blit_applet->DrawScreen(framebuffers, VideoCore::Capture::Layout, true);
glBindFramebuffer(GL_READ_FRAMEBUFFER, old_read_fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, old_draw_fb);
}
std::vector<u8> RendererOpenGL::GetAppletCaptureBuffer() {
using namespace VideoCore::Capture;
std::vector<u8> linear(TiledSize);
std::vector<u8> out(TiledSize);
GLint old_read_fb;
GLint old_draw_fb;
GLint old_pixel_pack_buffer;
GLint old_pack_row_length;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &old_draw_fb);
glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &old_pixel_pack_buffer);
glGetIntegerv(GL_PACK_ROW_LENGTH, &old_pack_row_length);
glBindFramebuffer(GL_FRAMEBUFFER, capture_framebuffer.handle);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
capture_renderbuffer.handle);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glReadPixels(0, 0, LinearWidth, LinearHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV,
linear.data());
glBindFramebuffer(GL_READ_FRAMEBUFFER, old_read_fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, old_draw_fb);
glBindBuffer(GL_PIXEL_PACK_BUFFER, old_pixel_pack_buffer);
glPixelStorei(GL_PACK_ROW_LENGTH, old_pack_row_length);
Tegra::Texture::SwizzleTexture(out, linear, BytesPerPixel, LinearWidth, LinearHeight,
LinearDepth, BlockHeight, BlockDepth);
return out;
}
} // namespace OpenGL } // namespace OpenGL

View File

@ -42,6 +42,8 @@ public:
void Composite(std::span<const Tegra::FramebufferConfig> framebuffers) override; void Composite(std::span<const Tegra::FramebufferConfig> framebuffers) override;
std::vector<u8> GetAppletCaptureBuffer() override;
VideoCore::RasterizerInterface* ReadRasterizer() override { VideoCore::RasterizerInterface* ReadRasterizer() override {
return &rasterizer; return &rasterizer;
} }
@ -52,7 +54,11 @@ public:
private: private:
void AddTelemetryFields(); void AddTelemetryFields();
void RenderToBuffer(std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout, void* dst);
void RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers); void RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers);
void RenderAppletCaptureLayer(std::span<const Tegra::FramebufferConfig> framebuffers);
Core::TelemetrySession& telemetry_session; Core::TelemetrySession& telemetry_session;
Core::Frontend::EmuWindow& emu_window; Core::Frontend::EmuWindow& emu_window;
@ -64,8 +70,11 @@ private:
ProgramManager program_manager; ProgramManager program_manager;
RasterizerOpenGL rasterizer; RasterizerOpenGL rasterizer;
OGLFramebuffer screenshot_framebuffer; OGLFramebuffer screenshot_framebuffer;
OGLFramebuffer capture_framebuffer;
OGLRenderbuffer capture_renderbuffer;
std::unique_ptr<BlitScreen> blit_screen; std::unique_ptr<BlitScreen> blit_screen;
std::unique_ptr<BlitScreen> blit_applet;
}; };
} // namespace OpenGL } // namespace OpenGL

View File

@ -19,6 +19,7 @@
#include "core/core_timing.h" #include "core/core_timing.h"
#include "core/frontend/graphics_context.h" #include "core/frontend/graphics_context.h"
#include "core/telemetry_session.h" #include "core/telemetry_session.h"
#include "video_core/capture.h"
#include "video_core/gpu.h" #include "video_core/gpu.h"
#include "video_core/renderer_vulkan/present/util.h" #include "video_core/renderer_vulkan/present/util.h"
#include "video_core/renderer_vulkan/renderer_vulkan.h" #include "video_core/renderer_vulkan/renderer_vulkan.h"
@ -38,6 +39,20 @@
namespace Vulkan { namespace Vulkan {
namespace { namespace {
constexpr VkExtent2D CaptureImageSize{
.width = VideoCore::Capture::LinearWidth,
.height = VideoCore::Capture::LinearHeight,
};
constexpr VkExtent3D CaptureImageExtent{
.width = VideoCore::Capture::LinearWidth,
.height = VideoCore::Capture::LinearHeight,
.depth = VideoCore::Capture::LinearDepth,
};
constexpr VkFormat CaptureFormat = VK_FORMAT_A8B8G8R8_UNORM_PACK32;
std::string GetReadableVersion(u32 version) { std::string GetReadableVersion(u32 version) {
return fmt::format("{}.{}.{}", VK_VERSION_MAJOR(version), VK_VERSION_MINOR(version), return fmt::format("{}.{}.{}", VK_VERSION_MAJOR(version), VK_VERSION_MINOR(version),
VK_VERSION_PATCH(version)); VK_VERSION_PATCH(version));
@ -100,11 +115,11 @@ RendererVulkan::RendererVulkan(Core::TelemetrySession& telemetry_session_,
present_manager(instance, render_window, device, memory_allocator, scheduler, swapchain, present_manager(instance, render_window, device, memory_allocator, scheduler, swapchain,
surface), surface),
blit_swapchain(device_memory, device, memory_allocator, present_manager, scheduler), blit_swapchain(device_memory, device, memory_allocator, present_manager, scheduler),
blit_screenshot(device_memory, device, memory_allocator, present_manager, scheduler), blit_capture(device_memory, device, memory_allocator, present_manager, scheduler),
blit_application_layer(device_memory, device, memory_allocator, present_manager, scheduler), blit_applet(device_memory, device, memory_allocator, present_manager, scheduler),
rasterizer(render_window, gpu, device_memory, device, memory_allocator, state_tracker, rasterizer(render_window, gpu, device_memory, device, memory_allocator, state_tracker,
scheduler), scheduler),
application_frame() { applet_frame() {
if (Settings::values.renderer_force_max_clock.GetValue() && device.ShouldBoostClocks()) { if (Settings::values.renderer_force_max_clock.GetValue() && device.ShouldBoostClocks()) {
turbo_mode.emplace(instance, dld); turbo_mode.emplace(instance, dld);
scheduler.RegisterOnSubmit([this] { turbo_mode->QueueSubmitted(); }); scheduler.RegisterOnSubmit([this] { turbo_mode->QueueSubmitted(); });
@ -127,6 +142,8 @@ void RendererVulkan::Composite(std::span<const Tegra::FramebufferConfig> framebu
SCOPE_EXIT({ render_window.OnFrameDisplayed(); }); SCOPE_EXIT({ render_window.OnFrameDisplayed(); });
RenderAppletCaptureLayer(framebuffers);
if (!render_window.IsShown()) { if (!render_window.IsShown()) {
return; return;
} }
@ -169,30 +186,20 @@ void RendererVulkan::Report() const {
telemetry_session.AddField(field, "GPU_Vulkan_Extensions", extensions); telemetry_session.AddField(field, "GPU_Vulkan_Extensions", extensions);
} }
void Vulkan::RendererVulkan::RenderScreenshot( vk::Buffer RendererVulkan::RenderToBuffer(std::span<const Tegra::FramebufferConfig> framebuffers,
std::span<const Tegra::FramebufferConfig> framebuffers) { const Layout::FramebufferLayout& layout, VkFormat format,
if (!renderer_settings.screenshot_requested) { VkDeviceSize buffer_size) {
return;
}
constexpr VkFormat ScreenshotFormat{VK_FORMAT_B8G8R8A8_UNORM};
const Layout::FramebufferLayout layout{renderer_settings.screenshot_framebuffer_layout};
auto frame = [&]() { auto frame = [&]() {
Frame f{}; Frame f{};
f.image = CreateWrappedImage(memory_allocator, VkExtent2D{layout.width, layout.height}, f.image =
ScreenshotFormat); CreateWrappedImage(memory_allocator, VkExtent2D{layout.width, layout.height}, format);
f.image_view = CreateWrappedImageView(device, f.image, ScreenshotFormat); f.image_view = CreateWrappedImageView(device, f.image, format);
f.framebuffer = blit_screenshot.CreateFramebuffer(layout, *f.image_view, ScreenshotFormat); f.framebuffer = blit_capture.CreateFramebuffer(layout, *f.image_view, format);
return f; return f;
}(); }();
blit_screenshot.DrawToFrame(rasterizer, &frame, framebuffers, layout, 1, auto dst_buffer = CreateWrappedBuffer(memory_allocator, buffer_size, MemoryUsage::Download);
VK_FORMAT_B8G8R8A8_UNORM); blit_capture.DrawToFrame(rasterizer, &frame, framebuffers, layout, 1, format);
const auto dst_buffer = CreateWrappedBuffer(
memory_allocator, static_cast<VkDeviceSize>(layout.width * layout.height * 4),
MemoryUsage::Download);
scheduler.RequestOutsideRenderPassOperationContext(); scheduler.RequestOutsideRenderPassOperationContext();
scheduler.Record([&](vk::CommandBuffer cmdbuf) { scheduler.Record([&](vk::CommandBuffer cmdbuf) {
@ -200,15 +207,68 @@ void Vulkan::RendererVulkan::RenderScreenshot(
VkExtent3D{layout.width, layout.height, 1}); VkExtent3D{layout.width, layout.height, 1});
}); });
// Ensure the copy is fully completed before saving the screenshot // Ensure the copy is fully completed before saving the capture
scheduler.Finish(); scheduler.Finish();
// Copy backing image data to the QImage screenshot buffer // Copy backing image data to the capture buffer
dst_buffer.Invalidate(); dst_buffer.Invalidate();
return dst_buffer;
}
void RendererVulkan::RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers) {
if (!renderer_settings.screenshot_requested) {
return;
}
const auto& layout{renderer_settings.screenshot_framebuffer_layout};
const auto dst_buffer = RenderToBuffer(framebuffers, layout, VK_FORMAT_B8G8R8A8_UNORM,
layout.width * layout.height * 4);
std::memcpy(renderer_settings.screenshot_bits, dst_buffer.Mapped().data(), std::memcpy(renderer_settings.screenshot_bits, dst_buffer.Mapped().data(),
dst_buffer.Mapped().size()); dst_buffer.Mapped().size());
renderer_settings.screenshot_complete_callback(false); renderer_settings.screenshot_complete_callback(false);
renderer_settings.screenshot_requested = false; renderer_settings.screenshot_requested = false;
} }
std::vector<u8> RendererVulkan::GetAppletCaptureBuffer() {
using namespace VideoCore::Capture;
std::vector<u8> out(VideoCore::Capture::TiledSize);
if (!applet_frame.image) {
return out;
}
const auto dst_buffer =
CreateWrappedBuffer(memory_allocator, VideoCore::Capture::TiledSize, MemoryUsage::Download);
scheduler.RequestOutsideRenderPassOperationContext();
scheduler.Record([&](vk::CommandBuffer cmdbuf) {
DownloadColorImage(cmdbuf, *applet_frame.image, *dst_buffer, CaptureImageExtent);
});
// Ensure the copy is fully completed before writing the capture
scheduler.Finish();
// Swizzle image data to the capture buffer
dst_buffer.Invalidate();
Tegra::Texture::SwizzleTexture(out, dst_buffer.Mapped(), BytesPerPixel, LinearWidth,
LinearHeight, LinearDepth, BlockHeight, BlockDepth);
return out;
}
void RendererVulkan::RenderAppletCaptureLayer(
std::span<const Tegra::FramebufferConfig> framebuffers) {
if (!applet_frame.image) {
applet_frame.image = CreateWrappedImage(memory_allocator, CaptureImageSize, CaptureFormat);
applet_frame.image_view = CreateWrappedImageView(device, applet_frame.image, CaptureFormat);
applet_frame.framebuffer = blit_applet.CreateFramebuffer(
VideoCore::Capture::Layout, *applet_frame.image_view, CaptureFormat);
}
blit_applet.DrawToFrame(rasterizer, &applet_frame, framebuffers, VideoCore::Capture::Layout, 1,
CaptureFormat);
}
} // namespace Vulkan } // namespace Vulkan

View File

@ -48,6 +48,8 @@ public:
void Composite(std::span<const Tegra::FramebufferConfig> framebuffers) override; void Composite(std::span<const Tegra::FramebufferConfig> framebuffers) override;
std::vector<u8> GetAppletCaptureBuffer() override;
VideoCore::RasterizerInterface* ReadRasterizer() override { VideoCore::RasterizerInterface* ReadRasterizer() override {
return &rasterizer; return &rasterizer;
} }
@ -59,7 +61,11 @@ public:
private: private:
void Report() const; void Report() const;
vk::Buffer RenderToBuffer(std::span<const Tegra::FramebufferConfig> framebuffers,
const Layout::FramebufferLayout& layout, VkFormat format,
VkDeviceSize buffer_size);
void RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers); void RenderScreenshot(std::span<const Tegra::FramebufferConfig> framebuffers);
void RenderAppletCaptureLayer(std::span<const Tegra::FramebufferConfig> framebuffers);
Core::TelemetrySession& telemetry_session; Core::TelemetrySession& telemetry_session;
Tegra::MaxwellDeviceMemoryManager& device_memory; Tegra::MaxwellDeviceMemoryManager& device_memory;
@ -79,12 +85,12 @@ private:
Swapchain swapchain; Swapchain swapchain;
PresentManager present_manager; PresentManager present_manager;
BlitScreen blit_swapchain; BlitScreen blit_swapchain;
BlitScreen blit_screenshot; BlitScreen blit_capture;
BlitScreen blit_application_layer; BlitScreen blit_applet;
RasterizerVulkan rasterizer; RasterizerVulkan rasterizer;
std::optional<TurboMode> turbo_mode; std::optional<TurboMode> turbo_mode;
Frame application_frame; Frame applet_frame;
}; };
} // namespace Vulkan } // namespace Vulkan