2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-05-03 02:47:04 +00:00
|
|
|
#include <algorithm>
|
2015-05-14 16:33:19 +00:00
|
|
|
#include <atomic>
|
2015-05-27 03:44:51 +00:00
|
|
|
#include <mutex>
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
2014-05-03 02:47:04 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-06-03 05:08:54 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/Core.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/SWCommandProcessor.h"
|
2015-09-26 08:07:48 +00:00
|
|
|
#include "VideoBackends/Software/SWOGLWindow.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/SWRenderer.h"
|
|
|
|
#include "VideoBackends/Software/SWStatistics.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2013-04-13 05:48:53 +00:00
|
|
|
|
2013-11-23 11:52:17 +00:00
|
|
|
static u8 *s_xfbColorTexture[2];
|
2013-08-20 11:51:39 +00:00
|
|
|
static int s_currentColorTexture = 0;
|
|
|
|
|
2015-05-14 16:33:19 +00:00
|
|
|
static std::atomic<bool> s_bScreenshot;
|
2013-11-16 04:07:08 +00:00
|
|
|
static std::mutex s_criticalScreenshot;
|
|
|
|
static std::string s_sScreenshotName;
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Init()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2015-05-14 16:33:19 +00:00
|
|
|
s_bScreenshot.store(false);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Shutdown()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
delete[] s_xfbColorTexture[0];
|
|
|
|
delete[] s_xfbColorTexture[1];
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::Prepare()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
s_xfbColorTexture[0] = new u8[MAX_XFB_WIDTH * MAX_XFB_HEIGHT * 4];
|
|
|
|
s_xfbColorTexture[1] = new u8[MAX_XFB_WIDTH * MAX_XFB_HEIGHT * 4];
|
2013-11-23 11:52:17 +00:00
|
|
|
|
2013-08-20 11:51:39 +00:00
|
|
|
s_currentColorTexture = 0;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 04:07:08 +00:00
|
|
|
void SWRenderer::SetScreenshot(const char *_szFilename)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(s_criticalScreenshot);
|
|
|
|
s_sScreenshotName = _szFilename;
|
2015-05-14 16:33:19 +00:00
|
|
|
s_bScreenshot.store(true);
|
2013-11-16 04:07:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::RenderText(const char* pstr, int left, int top, u32 color)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2015-09-26 08:07:48 +00:00
|
|
|
SWOGLWindow::s_instance->PrintText(pstr, left, top, color);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWRenderer::DrawDebugText()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2014-06-03 05:08:54 +00:00
|
|
|
std::string debugtext;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
if (g_SWVideoConfig.bShowStats)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2014-06-03 05:08:54 +00:00
|
|
|
debugtext += StringFromFormat("Objects: %i\n", swstats.thisFrame.numDrawnObjects);
|
|
|
|
debugtext += StringFromFormat("Primitives: %i\n", swstats.thisFrame.numPrimatives);
|
|
|
|
debugtext += StringFromFormat("Vertices Loaded: %i\n", swstats.thisFrame.numVerticesLoaded);
|
|
|
|
|
|
|
|
debugtext += StringFromFormat("Triangles Input: %i\n", swstats.thisFrame.numTrianglesIn);
|
|
|
|
debugtext += StringFromFormat("Triangles Rejected: %i\n", swstats.thisFrame.numTrianglesRejected);
|
|
|
|
debugtext += StringFromFormat("Triangles Culled: %i\n", swstats.thisFrame.numTrianglesCulled);
|
|
|
|
debugtext += StringFromFormat("Triangles Clipped: %i\n", swstats.thisFrame.numTrianglesClipped);
|
|
|
|
debugtext += StringFromFormat("Triangles Drawn: %i\n", swstats.thisFrame.numTrianglesDrawn);
|
|
|
|
|
|
|
|
debugtext += StringFromFormat("Rasterized Pix: %i\n", swstats.thisFrame.rasterizedPixels);
|
|
|
|
debugtext += StringFromFormat("TEV Pix In: %i\n", swstats.thisFrame.tevPixelsIn);
|
|
|
|
debugtext += StringFromFormat("TEV Pix Out: %i\n", swstats.thisFrame.tevPixelsOut);
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
|
|
|
// Render a shadow, and then the text.
|
2014-06-03 05:08:54 +00:00
|
|
|
SWRenderer::RenderText(debugtext.c_str(), 21, 21, 0xDD000000);
|
|
|
|
SWRenderer::RenderText(debugtext.c_str(), 20, 20, 0xFFFFFF00);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
2013-09-02 06:40:05 +00:00
|
|
|
|
2014-08-11 02:01:42 +00:00
|
|
|
u8* SWRenderer::GetNextColorTexture()
|
2014-08-11 01:18:38 +00:00
|
|
|
{
|
2013-11-23 07:04:37 +00:00
|
|
|
return s_xfbColorTexture[!s_currentColorTexture];
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:01:42 +00:00
|
|
|
u8* SWRenderer::GetCurrentColorTexture()
|
2014-08-11 01:18:38 +00:00
|
|
|
{
|
2014-07-14 11:51:55 +00:00
|
|
|
return s_xfbColorTexture[s_currentColorTexture];
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:01:42 +00:00
|
|
|
void SWRenderer::SwapColorTexture()
|
2014-08-11 01:18:38 +00:00
|
|
|
{
|
2013-11-23 07:04:37 +00:00
|
|
|
s_currentColorTexture = !s_currentColorTexture;
|
|
|
|
}
|
|
|
|
|
2013-11-23 05:38:15 +00:00
|
|
|
void SWRenderer::UpdateColorTexture(EfbInterface::yuv422_packed *xfb, u32 fbWidth, u32 fbHeight)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
if (fbWidth * fbHeight > MAX_XFB_WIDTH * MAX_XFB_HEIGHT)
|
2014-08-11 01:18:38 +00:00
|
|
|
{
|
2013-11-23 11:15:08 +00:00
|
|
|
ERROR_LOG(VIDEO, "Framebuffer is too large: %ix%i", fbWidth, fbHeight);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:51:39 +00:00
|
|
|
u32 offset = 0;
|
2014-08-11 02:01:42 +00:00
|
|
|
u8 *TexturePointer = GetNextColorTexture();
|
2013-08-20 11:51:39 +00:00
|
|
|
|
2013-11-23 05:38:15 +00:00
|
|
|
for (u16 y = 0; y < fbHeight; y++)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
2013-11-23 05:38:15 +00:00
|
|
|
for (u16 x = 0; x < fbWidth; x+=2)
|
2013-08-20 11:51:39 +00:00
|
|
|
{
|
|
|
|
// We do this one color sample (aka 2 RGB pixles) at a time
|
|
|
|
int Y1 = xfb[x].Y - 16;
|
2015-02-15 19:43:31 +00:00
|
|
|
int Y2 = xfb[x + 1].Y - 16;
|
2013-08-20 11:51:39 +00:00
|
|
|
int U = int(xfb[x].UV) - 128;
|
2015-02-15 19:43:31 +00:00
|
|
|
int V = int(xfb[x + 1].UV) - 128;
|
2013-08-20 11:51:39 +00:00
|
|
|
|
|
|
|
// We do the inverse BT.601 conversion for YCbCr to RGB
|
|
|
|
// http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion
|
2014-07-14 12:42:33 +00:00
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y1 + 1.596f * V), 0, 255);
|
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y1 - 0.392f * U - 0.813f * V), 0, 255);
|
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y1 + 2.017f * U ), 0, 255);
|
2013-08-20 11:51:39 +00:00
|
|
|
TexturePointer[offset++] = 255;
|
|
|
|
|
2014-07-14 12:42:33 +00:00
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y2 + 1.596f * V), 0, 255);
|
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y2 - 0.392f * U - 0.813f * V), 0, 255);
|
|
|
|
TexturePointer[offset++] = MathUtil::Clamp(int(1.164f * Y2 + 2.017f * U ), 0, 255);
|
2013-08-20 11:51:39 +00:00
|
|
|
TexturePointer[offset++] = 255;
|
|
|
|
}
|
2013-11-23 05:38:15 +00:00
|
|
|
xfb += fbWidth;
|
2013-08-20 11:51:39 +00:00
|
|
|
}
|
2014-08-11 02:01:42 +00:00
|
|
|
SwapColorTexture();
|
2013-08-20 11:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called on the GPU thread
|
|
|
|
void SWRenderer::Swap(u32 fbWidth, u32 fbHeight)
|
|
|
|
{
|
2013-11-16 04:07:08 +00:00
|
|
|
// Save screenshot
|
2015-05-14 16:33:19 +00:00
|
|
|
if (s_bScreenshot.load())
|
2013-11-16 04:07:08 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(s_criticalScreenshot);
|
2015-09-26 08:07:48 +00:00
|
|
|
TextureToPng(GetCurrentColorTexture(), fbWidth * 4, s_sScreenshotName, fbWidth, fbHeight, false);
|
2013-11-16 04:07:08 +00:00
|
|
|
// Reset settings
|
|
|
|
s_sScreenshotName.clear();
|
2015-05-14 16:33:19 +00:00
|
|
|
s_bScreenshot.store(false);
|
2013-11-16 04:07:08 +00:00
|
|
|
}
|
2013-11-23 10:20:45 +00:00
|
|
|
|
2013-04-13 05:48:53 +00:00
|
|
|
OSD::DoCallbacks(OSD::OSD_ONFRAME);
|
|
|
|
|
2012-12-26 16:33:45 +00:00
|
|
|
DrawDebugText();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2015-09-26 08:07:48 +00:00
|
|
|
SWOGLWindow::s_instance->ShowImage(GetCurrentColorTexture(), fbWidth * 4, fbWidth, fbHeight, 1.0);
|
2013-04-14 03:54:02 +00:00
|
|
|
|
2015-09-26 08:07:48 +00:00
|
|
|
swstats.frameCount++;
|
2013-01-19 06:51:00 +00:00
|
|
|
swstats.ResetFrame();
|
2015-09-26 08:07:48 +00:00
|
|
|
Core::Callback_VideoCopiedToXFB(true); // FIXME: should this function be called FrameRendered?
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|