Skip to content

Commit

Permalink
Merge pull request #4999 from stenzek/renderer-statics
Browse files Browse the repository at this point in the history
VideoCommon: Eliminate static state in Renderer
  • Loading branch information
degasus committed Mar 8, 2017
2 parents 9c1eac1 + 459a5ab commit 489d90b
Show file tree
Hide file tree
Showing 37 changed files with 304 additions and 382 deletions.
3 changes: 1 addition & 2 deletions Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulatio
jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SaveScreenShot("thumb");
Renderer::s_screenshotCompleted.WaitFor(std::chrono::seconds(2));
Core::SaveScreenShot("thumb", true);
Core::Stop();
updateMainFrameEvent.Set(); // Kick the waiting event
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,27 +732,27 @@ static std::string GenerateScreenshotName()
return name;
}

void SaveScreenShot()
void SaveScreenShot(bool wait_for_completion)
{
const bool bPaused = GetState() == State::Paused;

SetState(State::Paused);

Renderer::SetScreenshot(GenerateScreenshotName());
g_renderer->SaveScreenshot(GenerateScreenshotName(), wait_for_completion);

if (!bPaused)
SetState(State::Running);
}

void SaveScreenShot(const std::string& name)
void SaveScreenShot(const std::string& name, bool wait_for_completion)
{
const bool bPaused = GetState() == State::Paused;

SetState(State::Paused);

std::string filePath = GenerateScreenshotFolderPath() + name + ".png";

Renderer::SetScreenshot(filePath);
g_renderer->SaveScreenshot(filePath, wait_for_completion);

if (!bPaused)
SetState(State::Running);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ bool IsGPUThread();
void SetState(State state);
State GetState();

void SaveScreenShot();
void SaveScreenShot(const std::string& name);
void SaveScreenShot(bool wait_for_completion = false);
void SaveScreenShot(const std::string& name, bool wait_for_completion = false);

void Callback_WiimoteInterruptChannel(int _number, u16 _channelID, const void* _pData, u32 _Size);

Expand Down
18 changes: 5 additions & 13 deletions Source/Core/VideoBackends/D3D/FramebufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,10 @@ D3DTexture2D*& FramebufferManager::GetResolvedEFBDepthTexture()
}
}

FramebufferManager::FramebufferManager()
FramebufferManager::FramebufferManager(int target_width, int target_height)
{
m_target_width = Renderer::GetTargetWidth();
m_target_height = Renderer::GetTargetHeight();
if (m_target_height < 1)
{
m_target_height = 1;
}
if (m_target_width < 1)
{
m_target_width = 1;
}
m_target_width = static_cast<unsigned int>(std::max(target_width, 1));
m_target_height = static_cast<unsigned int>(std::max(target_height, 1));
DXGI_SAMPLE_DESC sample_desc;
sample_desc.Count = g_ActiveConfig.iMultisamples;
sample_desc.Quality = 0;
Expand Down Expand Up @@ -318,8 +310,8 @@ void XFBSource::CopyEFB(float Gamma)
D3D::SetPointCopySampler();

D3D::drawShadedTexQuad(
FramebufferManager::GetEFBColorTexture()->GetSRV(), &rect, Renderer::GetTargetWidth(),
Renderer::GetTargetHeight(), PixelShaderCache::GetColorCopyProgram(true),
FramebufferManager::GetEFBColorTexture()->GetSRV(), &rect, g_renderer->GetTargetWidth(),
g_renderer->GetTargetHeight(), PixelShaderCache::GetColorCopyProgram(true),
VertexShaderCache::GetSimpleVertexShader(), VertexShaderCache::GetSimpleInputLayout(),
GeometryShaderCache::GetCopyGeometryShader(), Gamma);

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D/FramebufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct XFBSource : public XFBSourceBase
class FramebufferManager : public FramebufferManagerBase
{
public:
FramebufferManager();
FramebufferManager(int target_width, int target_height);
~FramebufferManager();

static D3DTexture2D*& GetEFBColorTexture();
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D/PSTextureEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void PSTextureEncoder::Encode(u8* dst, u32 format, u32 native_width, u32 bytes_p
D3D::SetPointCopySampler();

D3D::drawShadedTexQuad(
pEFB, targetRect.AsRECT(), Renderer::GetTargetWidth(), Renderer::GetTargetHeight(),
pEFB, targetRect.AsRECT(), g_renderer->GetTargetWidth(), g_renderer->GetTargetHeight(),
SetStaticShader(format, is_depth_copy, isIntensity, scaleByHalf),
VertexShaderCache::GetSimpleVertexShader(), VertexShaderCache::GetSimpleInputLayout());

Expand Down
49 changes: 15 additions & 34 deletions Source/Core/VideoBackends/D3D/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ static void SetupDeviceObjects()
{
s_television.Init();

g_framebuffer_manager = std::make_unique<FramebufferManager>();

HRESULT hr;

D3D11_DEPTH_STENCIL_DESC ddesc;
Expand Down Expand Up @@ -235,25 +233,13 @@ static void Create3DVisionTexture(int width, int height)
delete[] sysData.pSysMem;
}

Renderer::Renderer(void*& window_handle)
Renderer::Renderer() : ::Renderer(D3D::GetBackBufferWidth(), D3D::GetBackBufferHeight())
{
D3D::Create((HWND)window_handle);

s_backbuffer_width = D3D::GetBackBufferWidth();
s_backbuffer_height = D3D::GetBackBufferHeight();

FramebufferManagerBase::SetLastXfbWidth(MAX_XFB_WIDTH);
FramebufferManagerBase::SetLastXfbHeight(MAX_XFB_HEIGHT);

UpdateDrawRectangle();

s_last_multisamples = g_ActiveConfig.iMultisamples;
s_last_efb_scale = g_ActiveConfig.iEFBScale;
s_last_stereo_mode = g_ActiveConfig.iStereoMode > 0;
s_last_xfb_mode = g_ActiveConfig.bUseRealXFB;
CalculateTargetSize();
PixelShaderManager::SetEfbScaleChanged();

g_framebuffer_manager = std::make_unique<FramebufferManager>(m_target_width, m_target_height);
SetupDeviceObjects();

// Setup GX pipeline state
Expand Down Expand Up @@ -282,7 +268,7 @@ Renderer::Renderer(void*& window_handle)
D3D::context->ClearDepthStencilView(FramebufferManager::GetEFBDepthTexture()->GetDSV(),
D3D11_CLEAR_DEPTH, 0.f, 0);

D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)s_target_width, (float)s_target_height);
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)m_target_width, (float)m_target_height);
D3D::context->RSSetViewports(1, &vp);
D3D::context->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV(),
FramebufferManager::GetEFBDepthTexture()->GetDSV());
Expand Down Expand Up @@ -324,8 +310,7 @@ bool Renderer::CheckForResize()
int client_height = rcWindow.bottom - rcWindow.top;

// Sanity check
if ((client_width != Renderer::GetBackbufferWidth() ||
client_height != Renderer::GetBackbufferHeight()) &&
if ((client_width != GetBackbufferWidth() || client_height != GetBackbufferHeight()) &&
client_width >= 4 && client_height >= 4)
{
return true;
Expand Down Expand Up @@ -726,7 +711,7 @@ void Renderer::SetBlendMode(bool forceUpdate)
void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
const EFBRectangle& rc, u64 ticks, float Gamma)
{
if ((!XFBWrited && !g_ActiveConfig.RealXFBEnabled()) || !fbWidth || !fbHeight)
if ((!m_xfb_written && !g_ActiveConfig.RealXFBEnabled()) || !fbWidth || !fbHeight)
{
Core::Callback_VideoCopiedToXFB(false);
return;
Expand Down Expand Up @@ -880,7 +865,6 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,

// Resize the back buffers NOW to avoid flickering
if (CalculateTargetSize() || xfbchanged || windowResized ||
s_last_efb_scale != g_ActiveConfig.iEFBScale ||
s_last_multisamples != g_ActiveConfig.iMultisamples ||
s_last_stereo_mode != (g_ActiveConfig.iStereoMode > 0))
{
Expand All @@ -894,21 +878,18 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
D3D::Reset();
SAFE_RELEASE(s_screenshot_texture);
SAFE_RELEASE(s_3d_vision_texture);
s_backbuffer_width = D3D::GetBackBufferWidth();
s_backbuffer_height = D3D::GetBackBufferHeight();
m_backbuffer_width = D3D::GetBackBufferWidth();
m_backbuffer_height = D3D::GetBackBufferHeight();
}

UpdateDrawRectangle();

s_last_efb_scale = g_ActiveConfig.iEFBScale;
s_last_stereo_mode = g_ActiveConfig.iStereoMode > 0;

PixelShaderManager::SetEfbScaleChanged();

D3D::context->OMSetRenderTargets(1, &D3D::GetBackBuffer()->GetRTV(), nullptr);

g_framebuffer_manager.reset();
g_framebuffer_manager = std::make_unique<FramebufferManager>();
g_framebuffer_manager = std::make_unique<FramebufferManager>(m_target_width, m_target_height);
float clear_col[4] = {0.f, 0.f, 0.f, 1.f};
D3D::context->ClearRenderTargetView(FramebufferManager::GetEFBColorTexture()->GetRTV(),
clear_col);
Expand Down Expand Up @@ -1162,12 +1143,12 @@ u16 Renderer::BBoxRead(int index)
if (index < 2)
{
// left/right
value = value * EFB_WIDTH / s_target_width;
value = value * EFB_WIDTH / m_target_width;
}
else
{
// up/down
value = value * EFB_HEIGHT / s_target_height;
value = value * EFB_HEIGHT / m_target_height;
}
if (index & 1)
value++; // fix max values to describe the outer border
Expand All @@ -1182,11 +1163,11 @@ void Renderer::BBoxWrite(int index, u16 _value)
value--;
if (index < 2)
{
value = value * s_target_width / EFB_WIDTH;
value = value * m_target_width / EFB_WIDTH;
}
else
{
value = value * s_target_height / EFB_HEIGHT;
value = value * m_target_height / EFB_HEIGHT;
}

BBox::Set(index, value);
Expand Down Expand Up @@ -1220,11 +1201,11 @@ void Renderer::BlitScreen(TargetRectangle src, TargetRectangle dst, D3DTexture2D
else if (g_ActiveConfig.iStereoMode == STEREO_3DVISION)
{
if (!s_3d_vision_texture)
Create3DVisionTexture(s_backbuffer_width, s_backbuffer_height);
Create3DVisionTexture(m_backbuffer_width, m_backbuffer_height);

D3D11_VIEWPORT leftVp = CD3D11_VIEWPORT((float)dst.left, (float)dst.top, (float)dst.GetWidth(),
(float)dst.GetHeight());
D3D11_VIEWPORT rightVp = CD3D11_VIEWPORT((float)(dst.left + s_backbuffer_width), (float)dst.top,
D3D11_VIEWPORT rightVp = CD3D11_VIEWPORT((float)(dst.left + m_backbuffer_width), (float)dst.top,
(float)dst.GetWidth(), (float)dst.GetHeight());

// Render to staging texture which is double the width of the backbuffer
Expand All @@ -1244,7 +1225,7 @@ void Renderer::BlitScreen(TargetRectangle src, TargetRectangle dst, D3DTexture2D

// Copy the left eye to the backbuffer, if Nvidia 3D Vision is enabled it should
// recognize the signature and automatically include the right eye frame.
D3D11_BOX box = CD3D11_BOX(0, 0, 0, s_backbuffer_width, s_backbuffer_height, 1);
D3D11_BOX box = CD3D11_BOX(0, 0, 0, m_backbuffer_width, m_backbuffer_height, 1);
D3D::context->CopySubresourceRegion(D3D::GetBackBuffer()->GetTex(), 0, 0, 0, 0,
s_3d_vision_texture->GetTex(), 0, &box);

Expand Down
6 changes: 3 additions & 3 deletions Source/Core/VideoBackends/D3D/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class D3DTexture2D;
class Renderer : public ::Renderer
{
public:
Renderer(void*& window_handle);
~Renderer();
Renderer();
~Renderer() override;

void SetColorMask() override;
void SetBlendMode(bool forceUpdate) override;
Expand Down Expand Up @@ -60,7 +60,7 @@ class Renderer : public ::Renderer

void ReinterpretPixelData(unsigned int convtype) override;

static bool CheckForResize();
bool CheckForResize();

u32 GetMaxTextureSize() override;

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void TextureCache::TCacheEntry::FromRenderTarget(bool is_depth_copy, const EFBRe

// Create texture copy
D3D::drawShadedTexQuad(
efbTexSRV, &sourcerect, Renderer::GetTargetWidth(), Renderer::GetTargetHeight(),
efbTexSRV, &sourcerect, g_renderer->GetTargetWidth(), g_renderer->GetTargetHeight(),
is_depth_copy ? PixelShaderCache::GetDepthMatrixProgram(multisampled) :
PixelShaderCache::GetColorMatrixProgram(multisampled),
VertexShaderCache::GetSimpleVertexShader(), VertexShaderCache::GetSimpleInputLayout(),
Expand Down
6 changes: 5 additions & 1 deletion Source/Core/VideoBackends/D3D/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>

#include "Common/CommonTypes.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"

#include "VideoBackends/D3D/BoundingBox.h"
Expand Down Expand Up @@ -144,8 +145,11 @@ bool VideoBackend::Initialize(void* window_handle)

void VideoBackend::Video_Prepare()
{
if (FAILED(D3D::Create(reinterpret_cast<HWND>(m_window_handle))))
PanicAlert("Failed to create D3D device.");

// internal interfaces
g_renderer = std::make_unique<Renderer>(m_window_handle);
g_renderer = std::make_unique<Renderer>();
g_texture_cache = std::make_unique<TextureCache>();
g_vertex_manager = std::make_unique<VertexManager>();
g_perf_query = std::make_unique<PerfQuery>();
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/VideoBackends/D3D12/FramebufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ D3DTexture2D*& FramebufferManager::GetResolvedEFBDepthTexture()
}
}

FramebufferManager::FramebufferManager()
FramebufferManager::FramebufferManager(int target_width, int target_height)
{
m_target_width = std::max(Renderer::GetTargetWidth(), 1);
m_target_height = std::max(Renderer::GetTargetHeight(), 1);
m_target_width = static_cast<unsigned int>(std::max(target_width, 1));
m_target_height = static_cast<unsigned int>(std::max(target_height, 1));

DXGI_SAMPLE_DESC sample_desc;
sample_desc.Count = g_ActiveConfig.iMultisamples;
Expand Down Expand Up @@ -525,7 +525,7 @@ void XFBSource::CopyEFB(float gamma)
D3D::SetPointCopySampler();

D3D::DrawShadedTexQuad(FramebufferManager::GetEFBColorTexture(), &rect,
Renderer::GetTargetWidth(), Renderer::GetTargetHeight(),
g_renderer->GetTargetWidth(), g_renderer->GetTargetHeight(),
StaticShaderCache::GetColorCopyPixelShader(true),
StaticShaderCache::GetSimpleVertexShader(),
StaticShaderCache::GetSimpleVertexShaderInputLayout(),
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D12/FramebufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct XFBSource final : public XFBSourceBase
class FramebufferManager final : public FramebufferManagerBase
{
public:
FramebufferManager();
FramebufferManager(int target_width, int target_height);
~FramebufferManager();

static D3DTexture2D*& GetEFBColorTexture();
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void PSTextureEncoder::Encode(u8* dst, u32 format, u32 native_width, u32 bytes_p
D3D::SetPointCopySampler();

D3D::DrawShadedTexQuad(
efb_source, target_rect.AsRECT(), Renderer::GetTargetWidth(), Renderer::GetTargetHeight(),
efb_source, target_rect.AsRECT(), g_renderer->GetTargetWidth(), g_renderer->GetTargetHeight(),
SetStaticShader(format, is_depth_copy, is_intensity, scale_by_half),
StaticShaderCache::GetSimpleVertexShader(),
StaticShaderCache::GetSimpleVertexShaderInputLayout(), D3D12_SHADER_BYTECODE(), 1.0f, 0,
Expand Down

0 comments on commit 489d90b

Please sign in to comment.