2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-01-23 09:20:20 -07:00
|
|
|
#include "VideoBackends/D3D/Render.h"
|
|
|
|
|
2017-04-09 14:15:57 -06:00
|
|
|
#include <array>
|
2013-10-26 03:55:41 -06:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cmath>
|
2017-04-09 15:27:20 -06:00
|
|
|
#include <cstring>
|
2015-12-22 16:47:20 -07:00
|
|
|
#include <memory>
|
2014-05-25 18:52:58 -06:00
|
|
|
#include <string>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <strsafe.h>
|
2017-04-09 13:05:24 -06:00
|
|
|
#include <tuple>
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-02-01 08:56:13 -07:00
|
|
|
#include "Common/Logging/Log.h"
|
2015-05-06 16:37:58 -06:00
|
|
|
#include "Common/MathUtil.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-02-19 04:14:09 -07:00
|
|
|
#include "Core/Core.h"
|
2011-01-25 09:43:08 -07:00
|
|
|
|
2014-12-04 19:01:20 -07:00
|
|
|
#include "VideoBackends/D3D/BoundingBox.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-06-23 00:11:07 -06:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DUtil.h"
|
2017-05-29 16:02:09 -06:00
|
|
|
#include "VideoBackends/D3D/DXTexture.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/FramebufferManager.h"
|
2014-11-14 17:53:08 -07:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/PixelShaderCache.h"
|
|
|
|
#include "VideoBackends/D3D/TextureCache.h"
|
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/BPFunctions.h"
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
#include "VideoCommon/PixelEngine.h"
|
2017-04-29 08:54:22 -06:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2017-01-23 09:20:20 -07:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2017-05-29 16:02:09 -06:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2017-09-02 20:30:34 -06:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2017-02-01 08:56:13 -07:00
|
|
|
#include "VideoCommon/XFMemory.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2011-01-29 13:16:51 -07:00
|
|
|
namespace DX11
|
|
|
|
{
|
2014-11-29 13:37:34 -07:00
|
|
|
// Nvidia stereo blitting struct defined in "nvstereo.h" from the Nvidia SDK
|
2014-11-13 16:24:53 -07:00
|
|
|
typedef struct _Nv_Stereo_Image_Header
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
unsigned int dwSignature;
|
|
|
|
unsigned int dwWidth;
|
|
|
|
unsigned int dwHeight;
|
|
|
|
unsigned int dwBPP;
|
|
|
|
unsigned int dwFlags;
|
2014-11-13 16:24:53 -07:00
|
|
|
} NVSTEREOIMAGEHEADER, *LPNVSTEREOIMAGEHEADER;
|
|
|
|
|
|
|
|
#define NVSTEREO_IMAGE_SIGNATURE 0x4433564e
|
2011-09-08 07:39:03 -06:00
|
|
|
|
2018-01-25 23:23:24 -07:00
|
|
|
Renderer::Renderer(int backbuffer_width, int backbuffer_height)
|
|
|
|
: ::Renderer(backbuffer_width, backbuffer_height)
|
2017-11-21 02:53:38 -07:00
|
|
|
{
|
|
|
|
m_last_multisamples = g_ActiveConfig.iMultisamples;
|
|
|
|
m_last_stereo_mode = g_ActiveConfig.stereo_mode != StereoMode::Off;
|
2018-01-25 23:23:24 -07:00
|
|
|
m_last_fullscreen_state = D3D::GetFullscreenState();
|
2017-11-21 02:53:38 -07:00
|
|
|
g_framebuffer_manager = std::make_unique<FramebufferManager>(m_target_width, m_target_height);
|
|
|
|
SetupDeviceObjects();
|
2017-04-09 13:51:15 -06:00
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
// Setup GX pipeline state
|
|
|
|
for (auto& sampler : m_gx_state.samplers)
|
|
|
|
sampler.hex = RenderState::GetPointSamplerState().hex;
|
|
|
|
|
|
|
|
m_gx_state.zmode.testenable = false;
|
|
|
|
m_gx_state.zmode.updateenable = false;
|
|
|
|
m_gx_state.zmode.func = ZMode::NEVER;
|
|
|
|
m_gx_state.raster.cullmode = GenMode::CULL_NONE;
|
2011-01-24 01:44:32 -07:00
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
// Clear EFB textures
|
|
|
|
constexpr std::array<float, 4> clear_color{{0.f, 0.f, 0.f, 1.f}};
|
|
|
|
D3D::context->ClearRenderTargetView(FramebufferManager::GetEFBColorTexture()->GetRTV(),
|
|
|
|
clear_color.data());
|
|
|
|
D3D::context->ClearDepthStencilView(FramebufferManager::GetEFBDepthTexture()->GetDSV(),
|
|
|
|
D3D11_CLEAR_DEPTH, 0.f, 0);
|
|
|
|
|
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)m_target_width, (float)m_target_height);
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
Renderer::~Renderer()
|
|
|
|
{
|
|
|
|
TeardownDeviceObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetupDeviceObjects()
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
D3D11_DEPTH_STENCIL_DESC ddesc;
|
|
|
|
ddesc.DepthEnable = FALSE;
|
|
|
|
ddesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
|
|
|
ddesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
|
|
|
|
ddesc.StencilEnable = FALSE;
|
|
|
|
ddesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
|
|
|
|
ddesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateDepthStencilState(&ddesc, &m_clear_depth_states[0]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create depth state for Renderer::ClearScreen");
|
|
|
|
ddesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
|
|
|
ddesc.DepthEnable = TRUE;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateDepthStencilState(&ddesc, &m_clear_depth_states[1]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create depth state for Renderer::ClearScreen");
|
|
|
|
ddesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateDepthStencilState(&ddesc, &m_clear_depth_states[2]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create depth state for Renderer::ClearScreen");
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::SetDebugObjectName(m_clear_depth_states[0],
|
2016-06-24 02:43:46 -06:00
|
|
|
"depth state for Renderer::ClearScreen (depth buffer disabled)");
|
|
|
|
D3D::SetDebugObjectName(
|
2017-11-21 02:53:38 -07:00
|
|
|
m_clear_depth_states[1],
|
2016-06-24 02:43:46 -06:00
|
|
|
"depth state for Renderer::ClearScreen (depth buffer enabled, writing enabled)");
|
|
|
|
D3D::SetDebugObjectName(
|
2017-11-21 02:53:38 -07:00
|
|
|
m_clear_depth_states[2],
|
2016-06-24 02:43:46 -06:00
|
|
|
"depth state for Renderer::ClearScreen (depth buffer enabled, writing disabled)");
|
|
|
|
|
|
|
|
D3D11_BLEND_DESC blenddesc;
|
|
|
|
blenddesc.AlphaToCoverageEnable = FALSE;
|
|
|
|
blenddesc.IndependentBlendEnable = FALSE;
|
|
|
|
blenddesc.RenderTarget[0].BlendEnable = FALSE;
|
|
|
|
blenddesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
|
|
|
|
blenddesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
|
|
|
|
blenddesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
|
|
|
|
blenddesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
|
|
|
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
|
|
|
|
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
|
|
|
|
blenddesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateBlendState(&blenddesc, &m_reset_blend_state);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create blend state for Renderer::ResetAPIState");
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::SetDebugObjectName(m_reset_blend_state, "blend state for Renderer::ResetAPIState");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
m_clear_blend_states[0] = m_reset_blend_state;
|
|
|
|
m_reset_blend_state->AddRef();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
blenddesc.RenderTarget[0].RenderTargetWriteMask =
|
|
|
|
D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN | D3D11_COLOR_WRITE_ENABLE_BLUE;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateBlendState(&blenddesc, &m_clear_blend_states[1]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create blend state for Renderer::ClearScreen");
|
|
|
|
|
|
|
|
blenddesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALPHA;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateBlendState(&blenddesc, &m_clear_blend_states[2]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create blend state for Renderer::ClearScreen");
|
|
|
|
|
|
|
|
blenddesc.RenderTarget[0].RenderTargetWriteMask = 0;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateBlendState(&blenddesc, &m_clear_blend_states[3]);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create blend state for Renderer::ClearScreen");
|
|
|
|
|
|
|
|
ddesc.DepthEnable = FALSE;
|
|
|
|
ddesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
|
|
|
|
ddesc.DepthFunc = D3D11_COMPARISON_LESS;
|
|
|
|
ddesc.StencilEnable = FALSE;
|
|
|
|
ddesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
|
|
|
|
ddesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateDepthStencilState(&ddesc, &m_reset_depth_state);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create depth state for Renderer::ResetAPIState");
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::SetDebugObjectName(m_reset_depth_state, "depth stencil state for Renderer::ResetAPIState");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
D3D11_RASTERIZER_DESC rastdesc = CD3D11_RASTERIZER_DESC(D3D11_FILL_SOLID, D3D11_CULL_NONE, false,
|
|
|
|
0, 0.f, 0.f, false, false, false, false);
|
2017-11-21 02:53:38 -07:00
|
|
|
hr = D3D::device->CreateRasterizerState(&rastdesc, &m_reset_rast_state);
|
2016-06-24 02:43:46 -06:00
|
|
|
CHECK(hr == S_OK, "Create rasterizer state for Renderer::ResetAPIState");
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::SetDebugObjectName(m_reset_rast_state, "rasterizer state for Renderer::ResetAPIState");
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
m_screenshot_texture = nullptr;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2011-09-08 07:39:03 -06:00
|
|
|
// Kill off all device objects
|
2017-11-21 02:53:38 -07:00
|
|
|
void Renderer::TeardownDeviceObjects()
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
g_framebuffer_manager.reset();
|
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
SAFE_RELEASE(m_clear_blend_states[0]);
|
|
|
|
SAFE_RELEASE(m_clear_blend_states[1]);
|
|
|
|
SAFE_RELEASE(m_clear_blend_states[2]);
|
|
|
|
SAFE_RELEASE(m_clear_blend_states[3]);
|
|
|
|
SAFE_RELEASE(m_clear_depth_states[0]);
|
|
|
|
SAFE_RELEASE(m_clear_depth_states[1]);
|
|
|
|
SAFE_RELEASE(m_clear_depth_states[2]);
|
|
|
|
SAFE_RELEASE(m_reset_blend_state);
|
|
|
|
SAFE_RELEASE(m_reset_depth_state);
|
|
|
|
SAFE_RELEASE(m_reset_rast_state);
|
|
|
|
SAFE_RELEASE(m_screenshot_texture);
|
|
|
|
SAFE_RELEASE(m_3d_vision_texture);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
void Renderer::Create3DVisionTexture(int width, int height)
|
2014-11-13 16:24:53 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Create a staging texture for 3D vision with signature information in the last row.
|
|
|
|
// Nvidia 3D Vision supports full SBS, so there is no loss in resolution during this process.
|
2017-04-09 15:27:20 -06:00
|
|
|
NVSTEREOIMAGEHEADER header;
|
|
|
|
header.dwSignature = NVSTEREO_IMAGE_SIGNATURE;
|
|
|
|
header.dwWidth = static_cast<u32>(width * 2);
|
|
|
|
header.dwHeight = static_cast<u32>(height + 1);
|
|
|
|
header.dwBPP = 32;
|
|
|
|
header.dwFlags = 0;
|
|
|
|
|
|
|
|
const u32 pitch = static_cast<u32>(4 * width * 2);
|
|
|
|
const auto memory = std::make_unique<u8[]>((height + 1) * pitch);
|
|
|
|
u8* image_header_location = &memory[height * pitch];
|
|
|
|
std::memcpy(image_header_location, &header, sizeof(header));
|
|
|
|
|
|
|
|
D3D11_SUBRESOURCE_DATA sys_data;
|
|
|
|
sys_data.SysMemPitch = pitch;
|
|
|
|
sys_data.pSysMem = memory.get();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-11-21 02:53:38 -07:00
|
|
|
m_3d_vision_texture =
|
2016-06-24 02:43:46 -06:00
|
|
|
D3DTexture2D::Create(width * 2, height + 1, D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT,
|
2017-04-09 15:27:20 -06:00
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, &sys_data);
|
2014-11-13 16:24:53 -07:00
|
|
|
}
|
|
|
|
|
2017-09-30 00:25:36 -06:00
|
|
|
std::unique_ptr<AbstractTexture> Renderer::CreateTexture(const TextureConfig& config)
|
|
|
|
{
|
|
|
|
return std::make_unique<DXTexture>(config);
|
|
|
|
}
|
|
|
|
|
2017-10-21 08:49:40 -06:00
|
|
|
std::unique_ptr<AbstractStagingTexture> Renderer::CreateStagingTexture(StagingTextureType type,
|
|
|
|
const TextureConfig& config)
|
|
|
|
{
|
|
|
|
return DXStagingTexture::Create(type, config);
|
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
void Renderer::RenderText(const std::string& text, int left, int top, u32 color)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-11-12 10:30:12 -07:00
|
|
|
D3D::DrawTextScaled(static_cast<float>(left + 1), static_cast<float>(top + 1), 20.f, 0.0f,
|
|
|
|
color & 0xFF000000, text);
|
|
|
|
D3D::DrawTextScaled(static_cast<float>(left), static_cast<float>(top), 20.f, 0.0f, color, text);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
TargetRectangle Renderer::ConvertEFBRectangle(const EFBRectangle& rc)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
TargetRectangle result;
|
|
|
|
result.left = EFBToScaledX(rc.left);
|
|
|
|
result.top = EFBToScaledY(rc.top);
|
|
|
|
result.right = EFBToScaledX(rc.right);
|
|
|
|
result.bottom = EFBToScaledY(rc.bottom);
|
|
|
|
return result;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-21 05:12:32 -07:00
|
|
|
void Renderer::SetScissorRect(const MathUtil::Rectangle<int>& rc)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2018-01-21 05:12:32 -07:00
|
|
|
const RECT rect = {rc.left, rc.top, rc.right, rc.bottom};
|
|
|
|
D3D::context->RSSetScissorRects(1, &rect);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2010-10-24 13:52:52 -06:00
|
|
|
// This function allows the CPU to directly access the EFB.
|
|
|
|
// There are EFB peeks (which will read the color or depth of a pixel)
|
|
|
|
// and EFB pokes (which will change the color or depth of a pixel).
|
|
|
|
//
|
|
|
|
// The behavior of EFB peeks can only be modified by:
|
2014-02-16 13:30:18 -07:00
|
|
|
// - GX_PokeAlphaRead
|
2010-10-24 13:52:52 -06:00
|
|
|
// The behavior of EFB pokes can be modified by:
|
2014-02-16 13:30:18 -07:00
|
|
|
// - GX_PokeAlphaMode (TODO)
|
|
|
|
// - GX_PokeAlphaUpdate (TODO)
|
|
|
|
// - GX_PokeBlendMode (TODO)
|
|
|
|
// - GX_PokeColorUpdate (TODO)
|
|
|
|
// - GX_PokeDither (TODO)
|
|
|
|
// - GX_PokeDstAlpha (TODO)
|
|
|
|
// - GX_PokeZMode (TODO)
|
2010-10-22 13:40:05 -06:00
|
|
|
u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Convert EFB dimensions to the ones of our render target
|
|
|
|
EFBRectangle efbPixelRc;
|
|
|
|
efbPixelRc.left = x;
|
|
|
|
efbPixelRc.top = y;
|
|
|
|
efbPixelRc.right = x + 1;
|
|
|
|
efbPixelRc.bottom = y + 1;
|
|
|
|
TargetRectangle targetPixelRc = Renderer::ConvertEFBRectangle(efbPixelRc);
|
|
|
|
|
|
|
|
// Take the mean of the resulting dimensions; TODO: Don't use the center pixel, compute the
|
|
|
|
// average color instead
|
|
|
|
D3D11_RECT RectToLock;
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PeekColor || type == EFBAccessType::PeekZ)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
RectToLock.left = (targetPixelRc.left + targetPixelRc.right) / 2;
|
|
|
|
RectToLock.top = (targetPixelRc.top + targetPixelRc.bottom) / 2;
|
|
|
|
RectToLock.right = RectToLock.left + 1;
|
|
|
|
RectToLock.bottom = RectToLock.top + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RectToLock.left = targetPixelRc.left;
|
|
|
|
RectToLock.right = targetPixelRc.right;
|
|
|
|
RectToLock.top = targetPixelRc.top;
|
|
|
|
RectToLock.bottom = targetPixelRc.bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset any game specific settings.
|
|
|
|
ResetAPIState();
|
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, 1.f, 1.f);
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
|
|
|
D3D::SetPointCopySampler();
|
|
|
|
|
|
|
|
// Select copy and read textures depending on if we are doing a color or depth read (since they
|
|
|
|
// are different formats).
|
|
|
|
D3DTexture2D* source_tex;
|
|
|
|
D3DTexture2D* read_tex;
|
|
|
|
ID3D11Texture2D* staging_tex;
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PeekColor)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
source_tex = FramebufferManager::GetEFBColorTexture();
|
|
|
|
read_tex = FramebufferManager::GetEFBColorReadTexture();
|
|
|
|
staging_tex = FramebufferManager::GetEFBColorStagingBuffer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source_tex = FramebufferManager::GetEFBDepthTexture();
|
|
|
|
read_tex = FramebufferManager::GetEFBDepthReadTexture();
|
|
|
|
staging_tex = FramebufferManager::GetEFBDepthStagingBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select pixel shader (we don't want to average depth samples, instead select the minimum).
|
|
|
|
ID3D11PixelShader* copy_pixel_shader;
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PeekZ && g_ActiveConfig.iMultisamples > 1)
|
2016-06-24 02:43:46 -06:00
|
|
|
copy_pixel_shader = PixelShaderCache::GetDepthResolveProgram();
|
|
|
|
else
|
|
|
|
copy_pixel_shader = PixelShaderCache::GetColorCopyProgram(true);
|
|
|
|
|
|
|
|
// Draw a quad to grab the texel we want to read.
|
|
|
|
D3D::context->OMSetRenderTargets(1, &read_tex->GetRTV(), nullptr);
|
|
|
|
D3D::drawShadedTexQuad(source_tex->GetSRV(), &RectToLock, Renderer::GetTargetWidth(),
|
|
|
|
Renderer::GetTargetHeight(), copy_pixel_shader,
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout());
|
|
|
|
|
|
|
|
// Restore expected game state.
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
2016-06-24 02:43:46 -06:00
|
|
|
RestoreAPIState();
|
|
|
|
|
|
|
|
// Copy the pixel from the renderable to cpu-readable buffer.
|
|
|
|
D3D11_BOX box = CD3D11_BOX(0, 0, 0, 1, 1, 1);
|
|
|
|
D3D::context->CopySubresourceRegion(staging_tex, 0, 0, 0, 0, read_tex->GetTex(), 0, &box);
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
CHECK(D3D::context->Map(staging_tex, 0, D3D11_MAP_READ, 0, &map) == S_OK,
|
|
|
|
"Map staging buffer failed");
|
|
|
|
|
|
|
|
// Convert the framebuffer data to the format the game is expecting to receive.
|
|
|
|
u32 ret;
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PeekColor)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
memcpy(&val, map.pData, sizeof(val));
|
|
|
|
|
|
|
|
// our buffers are RGBA, yet a BGRA value is expected
|
|
|
|
val = ((val & 0xFF00FF00) | ((val >> 16) & 0xFF) | ((val << 16) & 0xFF0000));
|
|
|
|
|
|
|
|
// check what to do with the alpha channel (GX_PokeAlphaRead)
|
|
|
|
PixelEngine::UPEAlphaReadReg alpha_read_mode = PixelEngine::GetAlphaReadMode();
|
|
|
|
|
|
|
|
if (bpmem.zcontrol.pixel_format == PEControl::RGBA6_Z24)
|
|
|
|
{
|
|
|
|
val = RGBA8ToRGBA6ToRGBA8(val);
|
|
|
|
}
|
|
|
|
else if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16)
|
|
|
|
{
|
|
|
|
val = RGBA8ToRGB565ToRGBA8(val);
|
|
|
|
}
|
|
|
|
if (bpmem.zcontrol.pixel_format != PEControl::RGBA6_Z24)
|
|
|
|
{
|
|
|
|
val |= 0xFF000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha_read_mode.ReadMode == 2)
|
|
|
|
ret = val; // GX_READ_NONE
|
|
|
|
else if (alpha_read_mode.ReadMode == 1)
|
|
|
|
ret = (val | 0xFF000000); // GX_READ_FF
|
|
|
|
else /*if(alpha_read_mode.ReadMode == 0)*/
|
|
|
|
ret = (val & 0x00FFFFFF); // GX_READ_00
|
|
|
|
}
|
2017-01-23 00:51:46 -07:00
|
|
|
else // type == EFBAccessType::PeekZ
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
float val;
|
|
|
|
memcpy(&val, map.pData, sizeof(val));
|
|
|
|
|
|
|
|
// depth buffer is inverted in the d3d backend
|
|
|
|
val = 1.0f - val;
|
|
|
|
|
|
|
|
if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16)
|
|
|
|
{
|
|
|
|
// if Z is in 16 bit format you must return a 16 bit integer
|
|
|
|
ret = MathUtil::Clamp<u32>(static_cast<u32>(val * 65536.0f), 0, 0xFFFF);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = MathUtil::Clamp<u32>(static_cast<u32>(val * 16777216.0f), 0, 0xFFFFFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D::context->Unmap(staging_tex, 0);
|
|
|
|
return ret;
|
2015-12-18 22:45:21 -07:00
|
|
|
}
|
2010-10-22 13:40:05 -06:00
|
|
|
|
2015-12-19 07:34:56 -07:00
|
|
|
void Renderer::PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num_points)
|
2015-12-18 22:45:21 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ResetAPIState();
|
|
|
|
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PokeColor)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
D3D11_VIEWPORT vp =
|
|
|
|
CD3D11_VIEWPORT(0.0f, 0.0f, (float)GetTargetWidth(), (float)GetTargetHeight());
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::BindEFBRenderTarget(false);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2017-01-23 00:51:46 -07:00
|
|
|
else // if (type == EFBAccessType::PokeZ)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_clear_blend_states[3]);
|
|
|
|
D3D::stateman->PushDepthState(m_clear_depth_states[1]);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
D3D11_VIEWPORT vp =
|
|
|
|
CD3D11_VIEWPORT(0.0f, 0.0f, (float)GetTargetWidth(), (float)GetTargetHeight());
|
|
|
|
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
D3D::DrawEFBPokeQuads(type, points, num_points);
|
|
|
|
|
2017-01-23 00:51:46 -07:00
|
|
|
if (type == EFBAccessType::PokeZ)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
D3D::stateman->PopDepthState();
|
|
|
|
D3D::stateman->PopBlendState();
|
|
|
|
}
|
|
|
|
|
|
|
|
RestoreAPIState();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-21 05:04:15 -07:00
|
|
|
void Renderer::SetViewport(float x, float y, float width, float height, float near_depth,
|
|
|
|
float far_depth)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// In D3D, the viewport rectangle must fit within the render target.
|
2018-01-21 05:04:15 -07:00
|
|
|
D3D11_VIEWPORT vp;
|
|
|
|
vp.TopLeftX = MathUtil::Clamp(x, 0.0f, static_cast<float>(m_target_width - 1));
|
|
|
|
vp.TopLeftY = MathUtil::Clamp(y, 0.0f, static_cast<float>(m_target_height - 1));
|
|
|
|
vp.Width = MathUtil::Clamp(width, 1.0f, static_cast<float>(m_target_width) - vp.TopLeftX);
|
|
|
|
vp.Height = MathUtil::Clamp(height, 1.0f, static_cast<float>(m_target_height) - vp.TopLeftY);
|
|
|
|
vp.MinDepth = near_depth;
|
|
|
|
vp.MaxDepth = far_depth;
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
2010-09-27 20:15:02 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void Renderer::ClearScreen(const EFBRectangle& rc, bool colorEnable, bool alphaEnable, bool zEnable,
|
|
|
|
u32 color, u32 z)
|
2010-06-16 04:12:57 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ResetAPIState();
|
|
|
|
|
|
|
|
if (colorEnable && alphaEnable)
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_clear_blend_states[0]);
|
2016-06-24 02:43:46 -06:00
|
|
|
else if (colorEnable)
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_clear_blend_states[1]);
|
2016-06-24 02:43:46 -06:00
|
|
|
else if (alphaEnable)
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_clear_blend_states[2]);
|
2016-06-24 02:43:46 -06:00
|
|
|
else
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_clear_blend_states[3]);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// TODO: Should we enable Z testing here?
|
2017-04-09 13:51:15 -06:00
|
|
|
// if (!bpmem.zmode.testenable) D3D::stateman->PushDepthState(s_clear_depth_states[0]);
|
|
|
|
// else
|
|
|
|
if (zEnable)
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushDepthState(m_clear_depth_states[1]);
|
2016-06-24 02:43:46 -06:00
|
|
|
else /*if (!zEnable)*/
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushDepthState(m_clear_depth_states[2]);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Update the view port for clearing the picture
|
|
|
|
TargetRectangle targetRc = Renderer::ConvertEFBRectangle(rc);
|
|
|
|
D3D11_VIEWPORT vp =
|
|
|
|
CD3D11_VIEWPORT((float)targetRc.left, (float)targetRc.top, (float)targetRc.GetWidth(),
|
|
|
|
(float)targetRc.GetHeight(), 0.f, 1.f);
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::SetIntegerEFBRenderTarget(false);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Color is passed in bgra mode so we need to convert it to rgba
|
|
|
|
u32 rgbaColor = (color & 0xFF00FF00) | ((color >> 16) & 0xFF) | ((color << 16) & 0xFF0000);
|
|
|
|
D3D::drawClearQuad(rgbaColor, 1.0f - (z & 0xFFFFFF) / 16777216.0f);
|
|
|
|
|
|
|
|
D3D::stateman->PopDepthState();
|
|
|
|
D3D::stateman->PopBlendState();
|
|
|
|
|
|
|
|
RestoreAPIState();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2010-12-27 14:56:20 -07:00
|
|
|
void Renderer::ReinterpretPixelData(unsigned int convtype)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// TODO: MSAA support..
|
2016-08-31 12:17:22 -06:00
|
|
|
D3D11_RECT source = CD3D11_RECT(0, 0, GetTargetWidth(), GetTargetHeight());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
ID3D11PixelShader* pixel_shader;
|
|
|
|
if (convtype == 0)
|
|
|
|
pixel_shader = PixelShaderCache::ReinterpRGB8ToRGBA6(true);
|
|
|
|
else if (convtype == 2)
|
|
|
|
pixel_shader = PixelShaderCache::ReinterpRGBA6ToRGB8(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Trying to reinterpret pixel data with unsupported conversion type %d",
|
|
|
|
convtype);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert data and set the target texture as our new EFB
|
2016-08-31 12:17:22 -06:00
|
|
|
ResetAPIState();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-08-31 12:17:22 -06:00
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, static_cast<float>(GetTargetWidth()),
|
|
|
|
static_cast<float>(GetTargetHeight()));
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
|
|
|
|
|
|
|
D3D::context->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTempTexture()->GetRTV(),
|
|
|
|
nullptr);
|
|
|
|
D3D::SetPointCopySampler();
|
|
|
|
D3D::drawShadedTexQuad(
|
2016-08-31 12:17:22 -06:00
|
|
|
FramebufferManager::GetEFBColorTexture()->GetSRV(), &source, GetTargetWidth(),
|
|
|
|
GetTargetHeight(), pixel_shader, VertexShaderCache::GetSimpleVertexShader(),
|
2016-06-24 02:43:46 -06:00
|
|
|
VertexShaderCache::GetSimpleInputLayout(), GeometryShaderCache::GetCopyGeometryShader());
|
|
|
|
|
2016-08-31 12:17:22 -06:00
|
|
|
RestoreAPIState();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
FramebufferManager::SwapReinterpretTexture();
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
2010-12-27 14:56:20 -07:00
|
|
|
}
|
|
|
|
|
2017-04-29 09:00:45 -06:00
|
|
|
void Renderer::SetBlendingState(const BlendingState& state)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-09-30 00:19:16 -06:00
|
|
|
m_gx_state.blend.hex = state.hex;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2010-09-27 20:15:02 -06:00
|
|
|
// This function has the final picture. We adjust the aspect ratio here.
|
2017-10-01 10:19:29 -06:00
|
|
|
void Renderer::SwapImpl(AbstractTexture* texture, const EFBRectangle& xfb_region, u64 ticks,
|
|
|
|
float Gamma)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ResetAPIState();
|
|
|
|
|
|
|
|
// Prepare to copy the XFBs to our backbuffer
|
2018-01-25 23:23:24 -07:00
|
|
|
CheckForSurfaceChange();
|
|
|
|
CheckForSurfaceResize();
|
2016-11-10 06:31:44 -07:00
|
|
|
UpdateDrawRectangle();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-01-25 23:23:24 -07:00
|
|
|
TargetRectangle targetRc = GetTargetRectangle();
|
|
|
|
static constexpr std::array<float, 4> clear_color{{0.f, 0.f, 0.f, 1.f}};
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->OMSetRenderTargets(1, &D3D::GetBackBuffer()->GetRTV(), nullptr);
|
2017-04-09 14:15:57 -06:00
|
|
|
D3D::context->ClearRenderTargetView(D3D::GetBackBuffer()->GetRTV(), clear_color.data());
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// activate linear filtering for the buffer copies
|
|
|
|
D3D::SetLinearCopySampler();
|
2017-05-29 16:02:09 -06:00
|
|
|
auto* xfb_texture = static_cast<DXTexture*>(texture);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-09-28 23:31:08 -06:00
|
|
|
BlitScreen(xfb_region, targetRc, xfb_texture->GetRawTexIdentifier(),
|
2017-09-02 20:30:34 -06:00
|
|
|
xfb_texture->GetConfig().width, xfb_texture->GetConfig().height, Gamma);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Reset viewport for drawing text
|
2018-01-25 23:23:24 -07:00
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.0f, 0.0f, static_cast<float>(m_backbuffer_width),
|
|
|
|
static_cast<float>(m_backbuffer_height));
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
|
|
|
|
|
|
|
Renderer::DrawDebugText();
|
|
|
|
|
|
|
|
OSD::DrawMessages();
|
|
|
|
|
2016-09-06 16:57:58 -06:00
|
|
|
g_texture_cache->Cleanup(frameCount);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Enable configuration changes
|
|
|
|
UpdateActiveConfig();
|
2016-09-06 16:57:58 -06:00
|
|
|
g_texture_cache->OnConfigChanged(g_ActiveConfig);
|
2017-07-19 23:25:31 -06:00
|
|
|
VertexShaderCache::RetreiveAsyncShaders();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Flip/present backbuffer to frontbuffer here
|
2018-01-25 23:23:24 -07:00
|
|
|
if (D3D::swapchain)
|
|
|
|
D3D::Present();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Resize the back buffers NOW to avoid flickering
|
2018-01-25 23:23:24 -07:00
|
|
|
if (CalculateTargetSize() || m_last_multisamples != g_ActiveConfig.iMultisamples ||
|
2017-11-21 02:53:38 -07:00
|
|
|
m_last_stereo_mode != (g_ActiveConfig.stereo_mode != StereoMode::Off))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-11-21 02:53:38 -07:00
|
|
|
m_last_multisamples = g_ActiveConfig.iMultisamples;
|
2018-01-25 23:23:24 -07:00
|
|
|
m_last_stereo_mode = g_ActiveConfig.stereo_mode != StereoMode::Off;
|
2016-06-24 02:43:46 -06:00
|
|
|
PixelShaderCache::InvalidateMSAAShaders();
|
2016-11-10 06:31:44 -07:00
|
|
|
UpdateDrawRectangle();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
g_framebuffer_manager.reset();
|
2017-03-03 23:42:21 -07:00
|
|
|
g_framebuffer_manager = std::make_unique<FramebufferManager>(m_target_width, m_target_height);
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->ClearRenderTargetView(FramebufferManager::GetEFBColorTexture()->GetRTV(),
|
2017-04-09 14:15:57 -06:00
|
|
|
clear_color.data());
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->ClearDepthStencilView(FramebufferManager::GetEFBDepthTexture()->GetDSV(),
|
|
|
|
D3D11_CLEAR_DEPTH, 0.f, 0);
|
|
|
|
}
|
|
|
|
|
2017-07-20 01:10:02 -06:00
|
|
|
if (CheckForHostConfigChanges())
|
2017-06-24 03:29:03 -06:00
|
|
|
{
|
|
|
|
VertexShaderCache::Reload();
|
|
|
|
GeometryShaderCache::Reload();
|
|
|
|
PixelShaderCache::Reload();
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// begin next frame
|
|
|
|
RestoreAPIState();
|
2017-09-03 00:34:01 -06:00
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-25 23:23:24 -07:00
|
|
|
void Renderer::CheckForSurfaceChange()
|
|
|
|
{
|
|
|
|
if (!m_surface_changed.TestAndClear())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_surface_handle = m_new_surface_handle;
|
|
|
|
m_new_surface_handle = nullptr;
|
|
|
|
|
|
|
|
SAFE_RELEASE(m_screenshot_texture);
|
|
|
|
SAFE_RELEASE(m_3d_vision_texture);
|
|
|
|
D3D::Reset(reinterpret_cast<HWND>(m_new_surface_handle));
|
|
|
|
UpdateBackbufferSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::CheckForSurfaceResize()
|
|
|
|
{
|
|
|
|
const bool fullscreen_state = D3D::GetFullscreenState();
|
|
|
|
const bool exclusive_fullscreen_changed = fullscreen_state != m_last_fullscreen_state;
|
|
|
|
if (!m_surface_resized.TestAndClear() && !exclusive_fullscreen_changed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_backbuffer_width = m_new_backbuffer_width;
|
|
|
|
m_backbuffer_height = m_new_backbuffer_height;
|
|
|
|
|
|
|
|
SAFE_RELEASE(m_screenshot_texture);
|
|
|
|
SAFE_RELEASE(m_3d_vision_texture);
|
|
|
|
m_last_fullscreen_state = fullscreen_state;
|
|
|
|
if (D3D::swapchain)
|
|
|
|
D3D::ResizeSwapChain();
|
|
|
|
UpdateBackbufferSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::UpdateBackbufferSize()
|
|
|
|
{
|
|
|
|
if (D3D::swapchain)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC1 desc = {};
|
|
|
|
D3D::swapchain->GetDesc1(&desc);
|
|
|
|
m_backbuffer_width = std::max(desc.Width, 1u);
|
|
|
|
m_backbuffer_height = std::max(desc.Height, 1u);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_backbuffer_width = 1;
|
|
|
|
m_backbuffer_height = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 11:54:13 -06:00
|
|
|
// ALWAYS call RestoreAPIState for each ResetAPIState call you're doing
|
2010-06-13 13:50:06 -06:00
|
|
|
void Renderer::ResetAPIState()
|
|
|
|
{
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::stateman->PushBlendState(m_reset_blend_state);
|
|
|
|
D3D::stateman->PushDepthState(m_reset_depth_state);
|
|
|
|
D3D::stateman->PushRasterizerState(m_reset_rast_state);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::RestoreAPIState()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Gets us back into a more game-like state.
|
|
|
|
D3D::stateman->PopBlendState();
|
|
|
|
D3D::stateman->PopDepthState();
|
|
|
|
D3D::stateman->PopRasterizerState();
|
2018-01-21 05:04:15 -07:00
|
|
|
BPFunctions::SetViewport();
|
2016-06-24 02:43:46 -06:00
|
|
|
BPFunctions::SetScissor();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:37:41 -07:00
|
|
|
void Renderer::ApplyState()
|
2011-01-24 01:44:32 -07:00
|
|
|
{
|
2017-09-30 00:19:16 -06:00
|
|
|
D3D::stateman->PushBlendState(m_state_cache.Get(m_gx_state.blend));
|
|
|
|
D3D::stateman->PushDepthState(m_state_cache.Get(m_gx_state.zmode));
|
|
|
|
D3D::stateman->PushRasterizerState(m_state_cache.Get(m_gx_state.raster));
|
2017-04-30 02:07:57 -06:00
|
|
|
D3D::stateman->SetPrimitiveTopology(
|
2017-09-30 00:19:16 -06:00
|
|
|
StateCache::GetPrimitiveTopology(m_gx_state.raster.primitive));
|
|
|
|
FramebufferManager::SetIntegerEFBRenderTarget(m_gx_state.blend.logicopenable);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-09-30 00:19:16 -06:00
|
|
|
for (u32 stage = 0; stage < static_cast<u32>(m_gx_state.samplers.size()); stage++)
|
|
|
|
D3D::stateman->SetSampler(stage, m_state_cache.Get(m_gx_state.samplers[stage]));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
ID3D11Buffer* vertexConstants = VertexShaderCache::GetConstantBuffer();
|
|
|
|
|
|
|
|
D3D::stateman->SetPixelConstants(PixelShaderCache::GetConstantBuffer(),
|
|
|
|
g_ActiveConfig.bEnablePixelLighting ? vertexConstants : nullptr);
|
|
|
|
D3D::stateman->SetVertexConstants(vertexConstants);
|
|
|
|
D3D::stateman->SetGeometryConstants(GeometryShaderCache::GetConstantBuffer());
|
2011-01-24 01:44:32 -07:00
|
|
|
}
|
|
|
|
|
2012-03-29 17:56:24 -06:00
|
|
|
void Renderer::RestoreState()
|
2011-01-24 01:44:32 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::stateman->PopBlendState();
|
|
|
|
D3D::stateman->PopDepthState();
|
|
|
|
D3D::stateman->PopRasterizerState();
|
2011-01-24 01:44:32 -07:00
|
|
|
}
|
|
|
|
|
2017-04-30 02:07:57 -06:00
|
|
|
void Renderer::SetRasterizationState(const RasterizationState& state)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-09-30 00:19:16 -06:00
|
|
|
m_gx_state.raster.hex = state.hex;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2017-04-29 23:54:45 -06:00
|
|
|
void Renderer::SetDepthState(const DepthState& state)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-09-30 00:19:16 -06:00
|
|
|
m_gx_state.zmode.hex = state.hex;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-21 06:13:25 -07:00
|
|
|
void Renderer::SetTexture(u32 index, const AbstractTexture* texture)
|
|
|
|
{
|
|
|
|
D3D::stateman->SetTexture(
|
|
|
|
index,
|
|
|
|
texture ? static_cast<const DXTexture*>(texture)->GetRawTexIdentifier()->GetSRV() : nullptr);
|
|
|
|
}
|
|
|
|
|
2017-09-09 02:30:15 -06:00
|
|
|
void Renderer::SetSamplerState(u32 index, const SamplerState& state)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-09-30 00:19:16 -06:00
|
|
|
m_gx_state.samplers[index].hex = state.hex;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2018-01-21 06:13:25 -07:00
|
|
|
void Renderer::UnbindTexture(const AbstractTexture* texture)
|
|
|
|
{
|
|
|
|
D3D::stateman->UnsetTexture(
|
|
|
|
static_cast<const DXTexture*>(texture)->GetRawTexIdentifier()->GetSRV());
|
|
|
|
}
|
|
|
|
|
2010-06-13 13:50:06 -06:00
|
|
|
void Renderer::SetInterlacingMode()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// TODO
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2014-12-04 19:01:20 -07:00
|
|
|
u16 Renderer::BBoxRead(int index)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// Here we get the min/max value of the truncated position of the upscaled framebuffer.
|
|
|
|
// So we have to correct them to the unscaled EFB sizes.
|
|
|
|
int value = BBox::Get(index);
|
|
|
|
|
|
|
|
if (index < 2)
|
|
|
|
{
|
|
|
|
// left/right
|
2017-03-03 23:42:21 -07:00
|
|
|
value = value * EFB_WIDTH / m_target_width;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// up/down
|
2017-03-03 23:42:21 -07:00
|
|
|
value = value * EFB_HEIGHT / m_target_height;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
if (index & 1)
|
|
|
|
value++; // fix max values to describe the outer border
|
|
|
|
|
|
|
|
return value;
|
2014-12-04 19:01:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::BBoxWrite(int index, u16 _value)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
int value = _value; // u16 isn't enough to multiply by the efb width
|
|
|
|
if (index & 1)
|
|
|
|
value--;
|
|
|
|
if (index < 2)
|
|
|
|
{
|
2017-03-03 23:42:21 -07:00
|
|
|
value = value * m_target_width / EFB_WIDTH;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-03 23:42:21 -07:00
|
|
|
value = value * m_target_height / EFB_HEIGHT;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
BBox::Set(index, value);
|
2014-12-04 19:01:20 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void Renderer::BlitScreen(TargetRectangle src, TargetRectangle dst, D3DTexture2D* src_texture,
|
|
|
|
u32 src_width, u32 src_height, float Gamma)
|
2014-12-20 10:20:49 -07:00
|
|
|
{
|
2017-11-10 20:55:00 -07:00
|
|
|
if (g_ActiveConfig.stereo_mode == StereoMode::SBS ||
|
|
|
|
g_ActiveConfig.stereo_mode == StereoMode::TAB)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
TargetRectangle leftRc, rightRc;
|
2017-04-09 13:05:24 -06:00
|
|
|
std::tie(leftRc, rightRc) = ConvertStereoRectangle(dst);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
D3D11_VIEWPORT leftVp = CD3D11_VIEWPORT((float)leftRc.left, (float)leftRc.top,
|
|
|
|
(float)leftRc.GetWidth(), (float)leftRc.GetHeight());
|
|
|
|
D3D11_VIEWPORT rightVp = CD3D11_VIEWPORT((float)rightRc.left, (float)rightRc.top,
|
|
|
|
(float)rightRc.GetWidth(), (float)rightRc.GetHeight());
|
|
|
|
|
|
|
|
D3D::context->RSSetViewports(1, &leftVp);
|
|
|
|
D3D::drawShadedTexQuad(src_texture->GetSRV(), src.AsRECT(), src_width, src_height,
|
|
|
|
PixelShaderCache::GetColorCopyProgram(false),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout(), nullptr, Gamma, 0);
|
|
|
|
|
|
|
|
D3D::context->RSSetViewports(1, &rightVp);
|
|
|
|
D3D::drawShadedTexQuad(src_texture->GetSRV(), src.AsRECT(), src_width, src_height,
|
|
|
|
PixelShaderCache::GetColorCopyProgram(false),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout(), nullptr, Gamma, 1);
|
|
|
|
}
|
2017-11-10 20:55:00 -07:00
|
|
|
else if (g_ActiveConfig.stereo_mode == StereoMode::Nvidia3DVision)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-11-21 02:53:38 -07:00
|
|
|
if (!m_3d_vision_texture)
|
2017-03-03 23:42:21 -07:00
|
|
|
Create3DVisionTexture(m_backbuffer_width, m_backbuffer_height);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
D3D11_VIEWPORT leftVp = CD3D11_VIEWPORT((float)dst.left, (float)dst.top, (float)dst.GetWidth(),
|
|
|
|
(float)dst.GetHeight());
|
2017-03-03 23:42:21 -07:00
|
|
|
D3D11_VIEWPORT rightVp = CD3D11_VIEWPORT((float)(dst.left + m_backbuffer_width), (float)dst.top,
|
2016-06-24 02:43:46 -06:00
|
|
|
(float)dst.GetWidth(), (float)dst.GetHeight());
|
|
|
|
|
|
|
|
// Render to staging texture which is double the width of the backbuffer
|
2017-11-21 02:53:38 -07:00
|
|
|
D3D::context->OMSetRenderTargets(1, &m_3d_vision_texture->GetRTV(), nullptr);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
D3D::context->RSSetViewports(1, &leftVp);
|
|
|
|
D3D::drawShadedTexQuad(src_texture->GetSRV(), src.AsRECT(), src_width, src_height,
|
|
|
|
PixelShaderCache::GetColorCopyProgram(false),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout(), nullptr, Gamma, 0);
|
|
|
|
|
|
|
|
D3D::context->RSSetViewports(1, &rightVp);
|
|
|
|
D3D::drawShadedTexQuad(src_texture->GetSRV(), src.AsRECT(), src_width, src_height,
|
|
|
|
PixelShaderCache::GetColorCopyProgram(false),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout(), nullptr, Gamma, 1);
|
|
|
|
|
|
|
|
// 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.
|
2017-03-03 23:42:21 -07:00
|
|
|
D3D11_BOX box = CD3D11_BOX(0, 0, 0, m_backbuffer_width, m_backbuffer_height, 1);
|
2016-06-24 02:43:46 -06:00
|
|
|
D3D::context->CopySubresourceRegion(D3D::GetBackBuffer()->GetTex(), 0, 0, 0, 0,
|
2017-11-21 02:53:38 -07:00
|
|
|
m_3d_vision_texture->GetTex(), 0, &box);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Restore render target to backbuffer
|
|
|
|
D3D::context->OMSetRenderTargets(1, &D3D::GetBackBuffer()->GetRTV(), nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT((float)dst.left, (float)dst.top, (float)dst.GetWidth(),
|
|
|
|
(float)dst.GetHeight());
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2017-06-25 13:08:17 -06:00
|
|
|
|
2017-11-10 20:55:00 -07:00
|
|
|
ID3D11PixelShader* pixelShader = (g_Config.stereo_mode == StereoMode::Anaglyph) ?
|
2017-06-25 13:08:17 -06:00
|
|
|
PixelShaderCache::GetAnaglyphProgram() :
|
|
|
|
PixelShaderCache::GetColorCopyProgram(false);
|
2017-11-10 20:55:00 -07:00
|
|
|
ID3D11GeometryShader* geomShader = (g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer) ?
|
2017-06-25 13:08:17 -06:00
|
|
|
GeometryShaderCache::GetCopyGeometryShader() :
|
|
|
|
nullptr;
|
|
|
|
D3D::drawShadedTexQuad(src_texture->GetSRV(), src.AsRECT(), src_width, src_height, pixelShader,
|
2016-06-24 02:43:46 -06:00
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
2017-06-25 13:08:17 -06:00
|
|
|
VertexShaderCache::GetSimpleInputLayout(), geomShader, Gamma);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2014-12-20 10:20:49 -07:00
|
|
|
}
|
|
|
|
|
2016-11-08 17:07:56 -07:00
|
|
|
void Renderer::SetFullscreen(bool enable_fullscreen)
|
|
|
|
{
|
|
|
|
D3D::SetFullscreenState(enable_fullscreen);
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:16:29 -07:00
|
|
|
bool Renderer::IsFullscreen() const
|
2016-11-08 17:41:38 -07:00
|
|
|
{
|
|
|
|
return D3D::GetFullscreenState();
|
|
|
|
}
|
|
|
|
|
2012-01-06 05:45:51 -07:00
|
|
|
} // namespace DX11
|