2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 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.
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "VideoBackends/D3D/PSTextureEncoder.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
2014-06-23 06:11:07 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2015-01-25 23:49:35 +00:00
|
|
|
#include "VideoBackends/D3D/D3DUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/FramebufferManager.h"
|
|
|
|
#include "VideoBackends/D3D/Render.h"
|
|
|
|
#include "VideoBackends/D3D/TextureCache.h"
|
2015-01-25 23:49:35 +00:00
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
#include "VideoCommon/TextureConversionShader.h"
|
2016-07-21 23:04:57 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2015-01-25 23:49:35 +00:00
|
|
|
struct EFBEncodeParams
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2017-10-30 15:27:02 +00:00
|
|
|
s32 SrcLeft;
|
|
|
|
s32 SrcTop;
|
|
|
|
u32 DestWidth;
|
|
|
|
u32 ScaleFactor;
|
2017-07-03 02:24:20 +00:00
|
|
|
float y_scale;
|
|
|
|
u32 padding[3];
|
2011-03-08 23:25:37 +00:00
|
|
|
};
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
PSTextureEncoder::PSTextureEncoder()
|
2016-06-24 08:43:46 +00:00
|
|
|
: m_ready(false), m_out(nullptr), m_outRTV(nullptr), m_outStage(nullptr),
|
|
|
|
m_encodeParams(nullptr)
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSTextureEncoder::Init()
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_ready = false;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
// Create output texture RGBA format
|
2017-05-29 22:02:09 +00:00
|
|
|
// TODO: This Texture is overly large and parts of it are unused
|
|
|
|
// EFB2RAM copies use max (EFB_WIDTH * 4) by (EFB_HEIGHT / 4)
|
|
|
|
// XFB2RAM copies use max (EFB_WIDTH / 2) by (EFB_HEIGHT)
|
2017-09-03 02:30:34 +00:00
|
|
|
D3D11_TEXTURE2D_DESC t2dd = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_B8G8R8A8_UNORM, EFB_WIDTH * 4, 1024,
|
|
|
|
1, 1, D3D11_BIND_RENDER_TARGET);
|
2016-06-24 08:43:46 +00:00
|
|
|
hr = D3D::device->CreateTexture2D(&t2dd, nullptr, &m_out);
|
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode output texture");
|
|
|
|
D3D::SetDebugObjectName(m_out, "efb encoder output texture");
|
|
|
|
|
|
|
|
// Create output render target view
|
|
|
|
D3D11_RENDER_TARGET_VIEW_DESC rtvd = CD3D11_RENDER_TARGET_VIEW_DESC(
|
|
|
|
m_out, D3D11_RTV_DIMENSION_TEXTURE2D, DXGI_FORMAT_B8G8R8A8_UNORM);
|
|
|
|
hr = D3D::device->CreateRenderTargetView(m_out, &rtvd, &m_outRTV);
|
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode output render target view");
|
|
|
|
D3D::SetDebugObjectName(m_outRTV, "efb encoder output rtv");
|
|
|
|
|
|
|
|
// Create output staging buffer
|
|
|
|
t2dd.Usage = D3D11_USAGE_STAGING;
|
|
|
|
t2dd.BindFlags = 0;
|
|
|
|
t2dd.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
|
|
|
hr = D3D::device->CreateTexture2D(&t2dd, nullptr, &m_outStage);
|
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode output staging buffer");
|
|
|
|
D3D::SetDebugObjectName(m_outStage, "efb encoder output staging buffer");
|
|
|
|
|
|
|
|
// Create constant buffer for uploading data to shaders
|
|
|
|
D3D11_BUFFER_DESC bd = CD3D11_BUFFER_DESC(sizeof(EFBEncodeParams), D3D11_BIND_CONSTANT_BUFFER);
|
|
|
|
hr = D3D::device->CreateBuffer(&bd, nullptr, &m_encodeParams);
|
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode params buffer");
|
|
|
|
D3D::SetDebugObjectName(m_encodeParams, "efb encoder params buffer");
|
|
|
|
|
|
|
|
m_ready = true;
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
void PSTextureEncoder::Shutdown()
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_ready = false;
|
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
for (auto& it : m_encoding_shaders)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
SAFE_RELEASE(it.second);
|
|
|
|
}
|
2017-04-04 13:55:36 +00:00
|
|
|
m_encoding_shaders.clear();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
SAFE_RELEASE(m_encodeParams);
|
|
|
|
SAFE_RELEASE(m_outStage);
|
|
|
|
SAFE_RELEASE(m_outRTV);
|
|
|
|
SAFE_RELEASE(m_out);
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
void PSTextureEncoder::Encode(u8* dst, const EFBCopyParams& params, u32 native_width,
|
2017-04-04 13:55:36 +00:00
|
|
|
u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride,
|
2017-07-30 19:45:55 +00:00
|
|
|
const EFBRectangle& src_rect, bool scale_by_half)
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_ready) // Make sure we initialized OK
|
|
|
|
return;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
// Resolve MSAA targets before copying.
|
2016-12-26 19:54:37 +00:00
|
|
|
// FIXME: Instead of resolving EFB, it would be better to pick out a
|
|
|
|
// single sample from each pixel. The game may break if it isn't
|
|
|
|
// expecting the blurred edges around multisampled shapes.
|
2017-07-30 19:45:55 +00:00
|
|
|
ID3D11ShaderResourceView* pEFB = params.depth ?
|
2016-12-26 19:54:37 +00:00
|
|
|
FramebufferManager::GetResolvedEFBDepthTexture()->GetSRV() :
|
|
|
|
FramebufferManager::GetResolvedEFBColorTexture()->GetSRV();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Reset API
|
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
|
|
|
|
// Set up all the state for EFB encoding
|
|
|
|
{
|
|
|
|
const u32 words_per_row = bytes_per_row / sizeof(u32);
|
|
|
|
|
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, FLOAT(words_per_row), FLOAT(num_blocks_y));
|
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
|
|
|
|
|
|
|
constexpr EFBRectangle fullSrcRect(0, 0, EFB_WIDTH, EFB_HEIGHT);
|
|
|
|
TargetRectangle targetRect = g_renderer->ConvertEFBRectangle(fullSrcRect);
|
|
|
|
|
|
|
|
D3D::context->OMSetRenderTargets(1, &m_outRTV, nullptr);
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
EFBEncodeParams encode_params;
|
|
|
|
encode_params.SrcLeft = src_rect.left;
|
|
|
|
encode_params.SrcTop = src_rect.top;
|
|
|
|
encode_params.DestWidth = native_width;
|
|
|
|
encode_params.ScaleFactor = scale_by_half ? 2 : 1;
|
2017-07-03 02:24:20 +00:00
|
|
|
encode_params.y_scale = params.y_scale;
|
2017-07-30 19:45:55 +00:00
|
|
|
D3D::context->UpdateSubresource(m_encodeParams, 0, nullptr, &encode_params, 0, 0);
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D::stateman->SetPixelConstants(m_encodeParams);
|
|
|
|
|
2016-09-06 23:17:32 +00:00
|
|
|
// We also linear filtering for both box filtering and downsampling higher resolutions to 1x
|
2017-07-03 14:32:02 +00:00
|
|
|
// TODO: This only produces perfect downsampling for 2x IR, other resolutions will need more
|
|
|
|
// complex down filtering to average all pixels and produce the correct result.
|
2016-09-06 23:17:32 +00:00
|
|
|
// Also, box filtering won't be correct for anything other than 1x IR
|
2017-07-03 02:24:20 +00:00
|
|
|
if (scale_by_half || g_renderer->GetEFBScale() != 1 || params.y_scale > 1.0f)
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D::SetLinearCopySampler();
|
|
|
|
else
|
|
|
|
D3D::SetPointCopySampler();
|
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
D3D::drawShadedTexQuad(pEFB, targetRect.AsRECT(), g_renderer->GetTargetWidth(),
|
2017-07-30 19:45:55 +00:00
|
|
|
g_renderer->GetTargetHeight(), GetEncodingPixelShader(params),
|
2017-04-04 13:55:36 +00:00
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Copy to staging buffer
|
|
|
|
D3D11_BOX srcBox = CD3D11_BOX(0, 0, 0, words_per_row, num_blocks_y, 1);
|
|
|
|
D3D::context->CopySubresourceRegion(m_outStage, 0, 0, 0, 0, m_out, 0, &srcBox);
|
|
|
|
|
|
|
|
// Transfer staging buffer to GameCube/Wii RAM
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map = {0};
|
|
|
|
hr = D3D::context->Map(m_outStage, 0, D3D11_MAP_READ, 0, &map);
|
|
|
|
CHECK(SUCCEEDED(hr), "map staging buffer (0x%x)", hr);
|
|
|
|
|
|
|
|
u8* src = (u8*)map.pData;
|
|
|
|
u32 readStride = std::min(bytes_per_row, map.RowPitch);
|
|
|
|
for (unsigned int y = 0; y < num_blocks_y; ++y)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, readStride);
|
|
|
|
dst += memory_stride;
|
|
|
|
src += map.RowPitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D::context->Unmap(m_outStage, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore API
|
2017-09-03 06:34:01 +00:00
|
|
|
FramebufferManager::BindEFBRenderTarget();
|
2016-06-24 08:43:46 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
ID3D11PixelShader* PSTextureEncoder::GetEncodingPixelShader(const EFBCopyParams& params)
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2017-07-30 19:45:55 +00:00
|
|
|
auto iter = m_encoding_shaders.find(params);
|
2017-04-04 13:55:36 +00:00
|
|
|
if (iter != m_encoding_shaders.end())
|
|
|
|
return iter->second;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
D3DBlob* bytecode = nullptr;
|
2017-07-30 19:45:55 +00:00
|
|
|
const char* shader = TextureConversionShader::GenerateEncodingShader(params, APIType::D3D);
|
2017-04-04 13:55:36 +00:00
|
|
|
if (!D3D::CompilePixelShader(shader, &bytecode))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-04-04 13:55:36 +00:00
|
|
|
PanicAlert("Failed to compile texture encoding shader.");
|
2017-07-30 19:45:55 +00:00
|
|
|
m_encoding_shaders[params] = nullptr;
|
2017-04-04 13:55:36 +00:00
|
|
|
return nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 13:55:36 +00:00
|
|
|
ID3D11PixelShader* newShader;
|
|
|
|
HRESULT hr =
|
|
|
|
D3D::device->CreatePixelShader(bytecode->Data(), bytecode->Size(), nullptr, &newShader);
|
|
|
|
CHECK(SUCCEEDED(hr), "create efb encoder pixel shader");
|
|
|
|
|
2017-07-30 19:45:55 +00:00
|
|
|
m_encoding_shaders.emplace(params, newShader);
|
2017-04-04 13:55:36 +00:00
|
|
|
return newShader;
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
}
|