Skip to content

Commit

Permalink
Merge pull request dolphin-emu#4326 from degasus/framedump
Browse files Browse the repository at this point in the history
Framedump: Merge screenshot code with framedumping.
  • Loading branch information
degasus authored Oct 10, 2016
2 parents 562bc01 + 64927a2 commit a583d36
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 271 deletions.
49 changes: 2 additions & 47 deletions Source/Core/VideoBackends/D3D/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "VideoCommon/AVIDump.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
Expand Down Expand Up @@ -712,39 +711,6 @@ void Renderer::SetBlendMode(bool forceUpdate)
}
}

bool Renderer::SaveScreenshot(const std::string& filename, const TargetRectangle& rc)
{
if (!s_screenshot_texture)
CreateScreenshotTexture();

// copy back buffer to system memory
D3D11_BOX source_box = GetScreenshotSourceBox(rc);
D3D::context->CopySubresourceRegion(s_screenshot_texture, 0, 0, 0, 0,
(ID3D11Resource*)D3D::GetBackBuffer()->GetTex(), 0,
&source_box);

D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(s_screenshot_texture, 0, D3D11_MAP_READ_WRITE, 0, &map);

bool saved_png =
TextureToPng((u8*)map.pData, map.RowPitch, filename, source_box.right - source_box.left,
source_box.bottom - source_box.top, false);

D3D::context->Unmap(s_screenshot_texture, 0);

if (saved_png)
{
OSD::AddMessage(
StringFromFormat("Saved %i x %i %s", rc.GetWidth(), rc.GetHeight(), filename.c_str()));
}
else
{
OSD::AddMessage(StringFromFormat("Error saving %s", filename.c_str()));
}

return saved_png;
}

// This function has the final picture. We adjust the aspect ratio here.
void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
const EFBRectangle& rc, float Gamma)
Expand Down Expand Up @@ -842,17 +808,6 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
BlitScreen(sourceRc, targetRc, read_texture, GetTargetWidth(), GetTargetHeight(), Gamma);
}

// done with drawing the game stuff, good moment to save a screenshot
if (s_bScreenshot)
{
std::lock_guard<std::mutex> guard(s_criticalScreenshot);

SaveScreenshot(s_sScreenshotName, GetTargetRectangle());
s_sScreenshotName.clear();
s_bScreenshot = false;
s_screenshotCompleted.Set();
}

// Dump frames
if (IsFrameDumping())
{
Expand All @@ -869,8 +824,8 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(s_screenshot_texture, 0, D3D11_MAP_READ, 0, &map);

DumpFrameData(reinterpret_cast<const u8*>(map.pData), source_width, source_height, map.RowPitch,
AVIDump::DumpFormat::FORMAT_RGBA);
DumpFrameData(reinterpret_cast<const u8*>(map.pData), source_width, source_height,
map.RowPitch);
FinishFrameData();

D3D::context->Unmap(s_screenshot_texture, 0);
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/VideoBackends/D3D/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype) override;

bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override;

static bool CheckForResize();

u32 GetMaxTextureSize() override;
Expand Down
72 changes: 1 addition & 71 deletions Source/Core/VideoBackends/D3D12/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "VideoCommon/AVIDump.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
Expand Down Expand Up @@ -631,63 +630,6 @@ void Renderer::SetBlendMode(bool force_update)
D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_PSO, true);
}

bool Renderer::SaveScreenshot(const std::string& filename, const TargetRectangle& rc)
{
if (!s_screenshot_texture)
CreateScreenshotTexture();

// copy back buffer to system memory
bool saved_png = false;

D3D12_BOX source_box = GetScreenshotSourceBox(rc);

D3D12_TEXTURE_COPY_LOCATION dst_location = {};
dst_location.pResource = s_screenshot_texture;
dst_location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
dst_location.PlacedFootprint.Offset = 0;
dst_location.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dst_location.PlacedFootprint.Footprint.Width = D3D::GetBackBufferWidth();
dst_location.PlacedFootprint.Footprint.Height = D3D::GetBackBufferHeight();
dst_location.PlacedFootprint.Footprint.Depth = 1;
dst_location.PlacedFootprint.Footprint.RowPitch = D3D::AlignValue(
dst_location.PlacedFootprint.Footprint.Width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);

D3D12_TEXTURE_COPY_LOCATION src_location = {};
src_location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
src_location.SubresourceIndex = 0;
src_location.pResource = D3D::GetBackBuffer()->GetTex12();

D3D::GetBackBuffer()->TransitionToResourceState(D3D::current_command_list,
D3D12_RESOURCE_STATE_COPY_SOURCE);
D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, &source_box);

D3D::command_list_mgr->ExecuteQueuedWork(true);

void* screenshot_texture_map;
D3D12_RANGE read_range = {0, dst_location.PlacedFootprint.Footprint.RowPitch *
(source_box.bottom - source_box.top)};
CheckHR(s_screenshot_texture->Map(0, &read_range, &screenshot_texture_map));

saved_png = TextureToPng(
static_cast<u8*>(screenshot_texture_map), dst_location.PlacedFootprint.Footprint.RowPitch,
filename, source_box.right - source_box.left, source_box.bottom - source_box.top, false);

D3D12_RANGE write_range = {};
s_screenshot_texture->Unmap(0, &write_range);

if (saved_png)
{
OSD::AddMessage(
StringFromFormat("Saved %i x %i %s", rc.GetWidth(), rc.GetHeight(), filename.c_str()));
}
else
{
OSD::AddMessage(StringFromFormat("Error saving %s", filename.c_str()));
}

return saved_png;
}

// This function has the final picture. We adjust the aspect ratio here.
void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height,
const EFBRectangle& rc, float gamma)
Expand Down Expand Up @@ -797,17 +739,6 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height
BlitScreen(source_rc, target_rc, read_texture, GetTargetWidth(), GetTargetHeight(), gamma);
}

// done with drawing the game stuff, good moment to save a screenshot
if (s_bScreenshot)
{
std::lock_guard<std::mutex> guard(s_criticalScreenshot);

SaveScreenshot(s_sScreenshotName, GetTargetRectangle());
s_sScreenshotName.clear();
s_bScreenshot = false;
s_screenshotCompleted.Set();
}

// Dump frames
if (IsFrameDumping())
{
Expand Down Expand Up @@ -847,8 +778,7 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height
CheckHR(s_screenshot_texture->Map(0, &read_range, &screenshot_texture_map));

DumpFrameData(reinterpret_cast<const u8*>(screenshot_texture_map), source_width, source_height,
dst_location.PlacedFootprint.Footprint.RowPitch,
AVIDump::DumpFormat::FORMAT_RGBA);
dst_location.PlacedFootprint.Footprint.RowPitch);
FinishFrameData();

D3D12_RANGE write_range = {};
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/VideoBackends/D3D12/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class Renderer final : public ::Renderer

void ReinterpretPixelData(unsigned int conv_type) override;

bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override;

static bool CheckForResize();

u32 GetMaxTextureSize() override;
Expand Down
4 changes: 0 additions & 4 deletions Source/Core/VideoBackends/Null/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,5 @@ class Renderer : public ::Renderer
}

void ReinterpretPixelData(unsigned int convtype) override {}
bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override
{
return false;
}
};
}
33 changes: 1 addition & 32 deletions Source/Core/VideoBackends/OGL/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PixelEngine.h"
Expand Down Expand Up @@ -1444,31 +1443,16 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,

glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

// Save screenshot
if (s_bScreenshot)
{
std::lock_guard<std::mutex> lk(s_criticalScreenshot);

if (SaveScreenshot(s_sScreenshotName, flipped_trc))
OSD::AddMessage("Screenshot saved to " + s_sScreenshotName);

// Reset settings
s_sScreenshotName.clear();
s_bScreenshot = false;
s_screenshotCompleted.Set();
}

if (IsFrameDumping())
{
std::lock_guard<std::mutex> lk(s_criticalScreenshot);
std::vector<u8> image(flipped_trc.GetWidth() * flipped_trc.GetHeight() * 4);

glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(flipped_trc.left, flipped_trc.bottom, flipped_trc.GetWidth(),
flipped_trc.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.data());

DumpFrameData(image.data(), flipped_trc.GetWidth(), flipped_trc.GetHeight(),
flipped_trc.GetWidth() * 4, AVIDump::DumpFormat::FORMAT_RGBA, true);
flipped_trc.GetWidth() * 4, true);
FinishFrameData();
}
// Finish up the current frame, print some stats
Expand Down Expand Up @@ -1726,21 +1710,6 @@ void Renderer::SetInterlacingMode()

namespace OGL
{
bool Renderer::SaveScreenshot(const std::string& filename, const TargetRectangle& back_rc)
{
u32 W = back_rc.GetWidth();
u32 H = back_rc.GetHeight();
std::unique_ptr<u8[]> data(new u8[W * 4 * H]);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

glReadPixels(back_rc.left, back_rc.bottom, W, H, GL_RGBA, GL_UNSIGNED_BYTE, data.get());

// Turn image upside down
FlipImageData(data.get(), W, H, 4);

return TextureToPng(data.get(), W * 4, filename, W, H, false);
}

u32 Renderer::GetMaxTextureSize()
{
// Right now nvidia seems to do something very weird if we try to cache GL_MAX_TEXTURE_SIZE in
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/VideoBackends/OGL/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype) override;

bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override;

u32 GetMaxTextureSize() override;

void ChangeSurface(void* new_surface_handle) override;
Expand Down
13 changes: 0 additions & 13 deletions Source/Core/VideoBackends/OGL/VertexManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ void VertexManager::vFlush(bool useDstAlpha)
OpenFStream(fvs, filename, std::ios_base::out);
fvs << prog.shader.strvprog;
}

if (g_ActiveConfig.iLog & CONF_SAVETARGETS)
{
std::string filename =
StringFromFormat("%starg%.3d.png", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
g_ActiveConfig.iSaveTargetId);
TargetRectangle tr;
tr.left = 0;
tr.right = Renderer::GetTargetWidth();
tr.top = 0;
tr.bottom = Renderer::GetTargetHeight();
g_renderer->SaveScreenshot(filename, tr);
}
#endif
g_Config.iSaveTargetId++;

Expand Down
4 changes: 0 additions & 4 deletions Source/Core/VideoBackends/Software/SWRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,4 @@ class SWRenderer : public Renderer
u32 color, u32 z) override;

void ReinterpretPixelData(unsigned int convtype) override {}
bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override
{
return true;
};
};
41 changes: 6 additions & 35 deletions Source/Core/VideoBackends/Vulkan/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "VideoCommon/AVIDump.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
Expand Down Expand Up @@ -482,24 +481,13 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

// Draw to the screenshot buffer if needed.
bool needs_framedump = IsFrameDumping();
bool needs_screenshot = s_bScreenshot || needs_framedump;
if (needs_screenshot && DrawScreenshot(source_rc, efb_color_texture))
if (IsFrameDumping() && DrawScreenshot(source_rc, efb_color_texture))
{
if (s_bScreenshot)
{
WriteScreenshot();
}

if (needs_framedump)
{
DumpFrameData(reinterpret_cast<const u8*>(m_screenshot_readback_texture->GetMapPointer()),
static_cast<int>(m_screenshot_render_texture->GetWidth()),
static_cast<int>(m_screenshot_render_texture->GetHeight()),
static_cast<int>(m_screenshot_readback_texture->GetRowStride()),
AVIDump::DumpFormat::FORMAT_RGBA);
FinishFrameData();
}
DumpFrameData(reinterpret_cast<const u8*>(m_screenshot_readback_texture->GetMapPointer()),
static_cast<int>(m_screenshot_render_texture->GetWidth()),
static_cast<int>(m_screenshot_render_texture->GetHeight()),
static_cast<int>(m_screenshot_readback_texture->GetRowStride()));
FinishFrameData();
}

// Restore the EFB color texture to color attachment ready for rendering the next frame.
Expand Down Expand Up @@ -760,23 +748,6 @@ void Renderer::DestroyScreenshotResources()
m_screenshot_readback_texture.reset();
}

void Renderer::WriteScreenshot()
{
std::lock_guard<std::mutex> guard(s_criticalScreenshot);

if (!TextureToPng(reinterpret_cast<u8*>(m_screenshot_readback_texture->GetMapPointer()),
static_cast<int>(m_screenshot_readback_texture->GetRowStride()),
s_sScreenshotName, static_cast<int>(m_screenshot_render_texture->GetWidth()),
static_cast<int>(m_screenshot_render_texture->GetHeight()), false))
{
WARN_LOG(VIDEO, "Failed to write screenshot to %s", s_sScreenshotName.c_str());
}

s_sScreenshotName.clear();
s_bScreenshot = false;
s_screenshotCompleted.Set();
}

void Renderer::CheckForTargetResize(u32 fb_width, u32 fb_stride, u32 fb_height)
{
if (FramebufferManagerBase::LastXfbWidth() == fb_stride &&
Expand Down
6 changes: 0 additions & 6 deletions Source/Core/VideoBackends/Vulkan/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype) override;

bool SaveScreenshot(const std::string& filename, const TargetRectangle& rc) override
{
return false;
}

void ApplyState(bool bUseDstAlpha) override;

void ResetAPIState() override;
Expand Down Expand Up @@ -99,7 +94,6 @@ class Renderer : public ::Renderer
const TargetRectangle& src_rect, const Texture2D* src_tex, bool linear_filter);
bool ResizeScreenshotBuffer(u32 new_width, u32 new_height);
void DestroyScreenshotResources();
void WriteScreenshot();
FramebufferManager* m_framebuffer_mgr = nullptr;

VkSemaphore m_image_available_semaphore = VK_NULL_HANDLE;
Expand Down
Loading

0 comments on commit a583d36

Please sign in to comment.