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
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
#include "Common/Assert.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"
|
2017-10-30 13:11:46 +00:00
|
|
|
#include "VideoBackends/D3D/DXTexture.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
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
#include "VideoCommon/AbstractStagingTexture.h"
|
|
|
|
#include "VideoCommon/AbstractTexture.h"
|
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;
|
2018-04-29 08:52:30 +00:00
|
|
|
float gamma_rcp;
|
|
|
|
float clamp_top;
|
|
|
|
float clamp_bottom;
|
|
|
|
s32 filter_coefficients[3];
|
|
|
|
u32 padding;
|
2011-03-08 23:25:37 +00:00
|
|
|
};
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
PSTextureEncoder::PSTextureEncoder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
PSTextureEncoder::~PSTextureEncoder() = default;
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
void PSTextureEncoder::Init()
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2017-10-30 13:11:46 +00:00
|
|
|
// TODO: Move this to a constant somewhere in common.
|
2018-01-21 05:03:06 +00:00
|
|
|
TextureConfig encoding_texture_config(EFB_WIDTH * 4, 1024, 1, 1, 1, AbstractTextureFormat::BGRA8,
|
2017-10-30 13:11:46 +00:00
|
|
|
true);
|
|
|
|
m_encoding_render_texture = g_renderer->CreateTexture(encoding_texture_config);
|
|
|
|
m_encoding_readback_texture =
|
|
|
|
g_renderer->CreateStagingTexture(StagingTextureType::Readback, encoding_texture_config);
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT(m_encoding_render_texture && m_encoding_readback_texture);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Create constant buffer for uploading data to shaders
|
|
|
|
D3D11_BUFFER_DESC bd = CD3D11_BUFFER_DESC(sizeof(EFBEncodeParams), D3D11_BIND_CONSTANT_BUFFER);
|
2017-10-30 13:11:46 +00:00
|
|
|
HRESULT hr = D3D::device->CreateBuffer(&bd, nullptr, &m_encode_params);
|
2016-06-24 08:43:46 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode params buffer");
|
2017-10-30 13:11:46 +00:00
|
|
|
D3D::SetDebugObjectName(m_encode_params, "efb encoder params buffer");
|
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
|
|
|
{
|
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
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
SAFE_RELEASE(m_encode_params);
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-29 08:52:30 +00:00
|
|
|
void PSTextureEncoder::Encode(
|
|
|
|
u8* dst, const EFBCopyParams& params, u32 native_width, u32 bytes_per_row, u32 num_blocks_y,
|
|
|
|
u32 memory_stride, const EFBRectangle& src_rect, bool scale_by_half, float y_scale, float gamma,
|
|
|
|
bool clamp_top, bool clamp_bottom,
|
|
|
|
const TextureCacheBase::CopyFilterCoefficientArray& filter_coefficients)
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// 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);
|
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
D3D::context->OMSetRenderTargets(
|
|
|
|
1,
|
|
|
|
&static_cast<DXTexture*>(m_encoding_render_texture.get())->GetRawTexIdentifier()->GetRTV(),
|
|
|
|
nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
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;
|
2018-04-29 08:52:30 +00:00
|
|
|
encode_params.y_scale = y_scale;
|
|
|
|
encode_params.gamma_rcp = 1.0f / gamma;
|
|
|
|
encode_params.clamp_top = clamp_top ? src_rect.top / float(EFB_HEIGHT) : 0.0f;
|
2018-05-10 14:32:39 +00:00
|
|
|
encode_params.clamp_bottom = clamp_bottom ? src_rect.bottom / float(EFB_HEIGHT) : 1.0f;
|
2018-04-29 08:52:30 +00:00
|
|
|
for (size_t i = 0; i < filter_coefficients.size(); i++)
|
|
|
|
encode_params.filter_coefficients[i] = filter_coefficients[i];
|
|
|
|
|
2017-10-30 13:11:46 +00:00
|
|
|
D3D::context->UpdateSubresource(m_encode_params, 0, nullptr, &encode_params, 0, 0);
|
|
|
|
D3D::stateman->SetPixelConstants(m_encode_params);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
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
|
2018-04-29 08:52:30 +00:00
|
|
|
if (scale_by_half || g_renderer->GetEFBScale() != 1 || 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
|
2017-10-30 13:11:46 +00:00
|
|
|
MathUtil::Rectangle<int> copy_rect(0, 0, words_per_row, num_blocks_y);
|
|
|
|
m_encoding_readback_texture->CopyFromTexture(m_encoding_render_texture.get(), copy_rect, 0, 0,
|
|
|
|
copy_rect);
|
|
|
|
m_encoding_readback_texture->Flush();
|
|
|
|
if (m_encoding_readback_texture->Map())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-10-30 13:11:46 +00:00
|
|
|
m_encoding_readback_texture->ReadTexels(copy_rect, dst, memory_stride);
|
|
|
|
m_encoding_readback_texture->Unmap();
|
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-11-25 10:13:22 +00:00
|
|
|
const char* shader = TextureConversionShaderTiled::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
|
|
|
}
|
|
|
|
}
|