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
|
|
|
|
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/PSTextureEncoder.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"
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
struct EFBEncodeParams
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2015-01-25 23:49:35 +00:00
|
|
|
DWORD SrcLeft;
|
2015-01-26 05:20:25 +00:00
|
|
|
DWORD SrcTop;
|
2015-01-25 23:49:35 +00:00
|
|
|
DWORD DestWidth;
|
|
|
|
DWORD ScaleFactor;
|
2011-03-08 23:25:37 +00:00
|
|
|
};
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
PSTextureEncoder::PSTextureEncoder()
|
2014-03-09 20:14:26 +00:00
|
|
|
: m_ready(false), m_out(nullptr), m_outRTV(nullptr), m_outStage(nullptr),
|
2015-01-25 23:49:35 +00:00
|
|
|
m_encodeParams(nullptr)
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSTextureEncoder::Init()
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
|
|
|
m_ready = false;
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
HRESULT hr;
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
// Create output texture RGBA format
|
|
|
|
D3D11_TEXTURE2D_DESC t2dd = CD3D11_TEXTURE2D_DESC(
|
2015-01-26 01:22:29 +00:00
|
|
|
DXGI_FORMAT_B8G8R8A8_UNORM,
|
|
|
|
EFB_WIDTH * 4, EFB_HEIGHT / 4, 1, 1, D3D11_BIND_RENDER_TARGET);
|
2014-03-09 20:14:26 +00:00
|
|
|
hr = D3D::device->CreateTexture2D(&t2dd, nullptr, &m_out);
|
2011-06-11 19:37:21 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode output texture");
|
2011-03-08 23:25:37 +00:00
|
|
|
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,
|
2015-01-26 01:22:29 +00:00
|
|
|
D3D11_RTV_DIMENSION_TEXTURE2D, DXGI_FORMAT_B8G8R8A8_UNORM);
|
2011-06-11 19:37:21 +00:00
|
|
|
hr = D3D::device->CreateRenderTargetView(m_out, &rtvd, &m_outRTV);
|
2011-03-08 23:25:37 +00:00
|
|
|
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;
|
2014-03-09 20:14:26 +00:00
|
|
|
hr = D3D::device->CreateTexture2D(&t2dd, nullptr, &m_outStage);
|
2011-06-11 19:37:21 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode output staging buffer");
|
2011-03-08 23:25:37 +00:00
|
|
|
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);
|
2014-03-09 20:14:26 +00:00
|
|
|
hr = D3D::device->CreateBuffer(&bd, nullptr, &m_encodeParams);
|
2011-06-11 19:37:21 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "create efb encode params buffer");
|
2011-03-08 23:25:37 +00:00
|
|
|
D3D::SetDebugObjectName(m_encodeParams, "efb encoder params buffer");
|
|
|
|
|
|
|
|
m_ready = true;
|
|
|
|
}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
void PSTextureEncoder::Shutdown()
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
m_ready = false;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-02-12 15:00:34 +00:00
|
|
|
for (auto& it : m_staticShaders)
|
2011-06-11 19:37:21 +00:00
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
SAFE_RELEASE(it.second);
|
2011-06-11 19:37:21 +00:00
|
|
|
}
|
|
|
|
m_staticShaders.clear();
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
SAFE_RELEASE(m_encodeParams);
|
|
|
|
SAFE_RELEASE(m_outStage);
|
2011-03-08 23:25:37 +00:00
|
|
|
SAFE_RELEASE(m_outRTV);
|
2011-06-11 19:37:21 +00:00
|
|
|
SAFE_RELEASE(m_out);
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PSTextureEncoder::Encode(u8* dst, unsigned int dstFormat,
|
2014-03-23 20:44:23 +00:00
|
|
|
PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
|
|
|
|
bool isIntensity, bool scaleByHalf)
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
|
|
|
if (!m_ready) // Make sure we initialized OK
|
|
|
|
return 0;
|
|
|
|
|
2011-03-12 18:13:54 +00:00
|
|
|
// Clamp srcRect to 640x528. BPS: The Strike tries to encode an 800x600
|
|
|
|
// texture, which is invalid.
|
|
|
|
EFBRectangle correctSrc = srcRect;
|
|
|
|
correctSrc.ClampUL(0, 0, EFB_WIDTH, EFB_HEIGHT);
|
|
|
|
|
|
|
|
// Validate source rect size
|
|
|
|
if (correctSrc.GetWidth() <= 0 || correctSrc.GetHeight() <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-03-08 23:25:37 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
unsigned int blockW = BLOCK_WIDTHS[dstFormat];
|
|
|
|
unsigned int blockH = BLOCK_HEIGHTS[dstFormat];
|
|
|
|
|
|
|
|
// Round up source dims to multiple of block size
|
2011-03-12 18:13:54 +00:00
|
|
|
unsigned int actualWidth = correctSrc.GetWidth() / (scaleByHalf ? 2 : 1);
|
2011-03-08 23:25:37 +00:00
|
|
|
actualWidth = (actualWidth + blockW-1) & ~(blockW-1);
|
2011-03-12 18:13:54 +00:00
|
|
|
unsigned int actualHeight = correctSrc.GetHeight() / (scaleByHalf ? 2 : 1);
|
2011-03-08 23:25:37 +00:00
|
|
|
actualHeight = (actualHeight + blockH-1) & ~(blockH-1);
|
|
|
|
|
|
|
|
unsigned int numBlocksX = actualWidth/blockW;
|
|
|
|
unsigned int numBlocksY = actualHeight/blockH;
|
|
|
|
|
|
|
|
unsigned int cacheLinesPerRow;
|
|
|
|
if (dstFormat == 0x6) // RGBA takes two cache lines per block; all others take one
|
|
|
|
cacheLinesPerRow = numBlocksX*2;
|
|
|
|
else
|
|
|
|
cacheLinesPerRow = numBlocksX;
|
2011-03-12 18:13:54 +00:00
|
|
|
_assert_msg_(VIDEO, cacheLinesPerRow*32 <= MAX_BYTES_PER_BLOCK_ROW, "cache lines per row sanity check");
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
unsigned int totalCacheLines = cacheLinesPerRow * numBlocksY;
|
2011-03-12 18:13:54 +00:00
|
|
|
_assert_msg_(VIDEO, totalCacheLines*32 <= MAX_BYTES_PER_ENCODE, "total encode size sanity check");
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
size_t encodeSize = 0;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-03-08 23:25:37 +00:00
|
|
|
// Reset API
|
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
|
|
|
|
// Set up all the state for EFB encoding
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
2015-01-26 01:22:29 +00:00
|
|
|
D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, FLOAT(cacheLinesPerRow * 8), FLOAT(numBlocksY));
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
EFBRectangle fullSrcRect;
|
|
|
|
fullSrcRect.left = 0;
|
|
|
|
fullSrcRect.top = 0;
|
|
|
|
fullSrcRect.right = EFB_WIDTH;
|
|
|
|
fullSrcRect.bottom = EFB_HEIGHT;
|
|
|
|
TargetRectangle targetRect = g_renderer->ConvertEFBRectangle(fullSrcRect);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
D3D::context->OMSetRenderTargets(1, &m_outRTV, nullptr);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2014-03-23 20:44:23 +00:00
|
|
|
ID3D11ShaderResourceView* pEFB = (srcFormat == PEControl::Z24) ?
|
2015-02-08 15:58:04 +00:00
|
|
|
FramebufferManager::GetResolvedEFBDepthTexture()->GetSRV() :
|
2011-06-11 19:37:21 +00:00
|
|
|
// FIXME: Instead of resolving EFB, it would be better to pick out a
|
2013-04-15 20:28:55 +00:00
|
|
|
// single sample from each pixel. The game may break if it isn't
|
|
|
|
// expecting the blurred edges around multisampled shapes.
|
|
|
|
FramebufferManager::GetResolvedEFBColorTexture()->GetSRV();
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
EFBEncodeParams params;
|
|
|
|
params.SrcLeft = correctSrc.left;
|
2015-01-26 01:22:29 +00:00
|
|
|
params.SrcTop = correctSrc.top;
|
2015-01-25 23:49:35 +00:00
|
|
|
params.DestWidth = actualWidth;
|
|
|
|
params.ScaleFactor = scaleByHalf ? 2 : 1;
|
|
|
|
D3D::context->UpdateSubresource(m_encodeParams, 0, nullptr, ¶ms, 0, 0);
|
2015-01-26 01:22:29 +00:00
|
|
|
D3D::stateman->SetPixelConstants(m_encodeParams);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2015-01-26 07:05:23 +00:00
|
|
|
// Use linear filtering if (bScaleByHalf), use point filtering otherwise
|
|
|
|
if (scaleByHalf)
|
|
|
|
D3D::SetLinearCopySampler();
|
|
|
|
else
|
|
|
|
D3D::SetPointCopySampler();
|
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
D3D::drawShadedTexQuad(pEFB,
|
|
|
|
targetRect.AsRECT(),
|
|
|
|
Renderer::GetTargetWidth(),
|
|
|
|
Renderer::GetTargetHeight(),
|
|
|
|
SetStaticShader(dstFormat, srcFormat, isIntensity, scaleByHalf),
|
|
|
|
VertexShaderCache::GetSimpleVertexShader(),
|
|
|
|
VertexShaderCache::GetSimpleInputLayout());
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
// Copy to staging buffer
|
2015-01-26 01:22:29 +00:00
|
|
|
D3D11_BOX srcBox = CD3D11_BOX(0, 0, 0, cacheLinesPerRow * 8, numBlocksY, 1);
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->CopySubresourceRegion(m_outStage, 0, 0, 0, 0, m_out, 0, &srcBox);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
// Transfer staging buffer to GameCube/Wii RAM
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map = { 0 };
|
2011-06-11 19:37:21 +00:00
|
|
|
hr = D3D::context->Map(m_outStage, 0, D3D11_MAP_READ, 0, &map);
|
2014-01-06 10:31:09 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "map staging buffer (0x%x)", hr);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
u8* src = (u8*)map.pData;
|
|
|
|
for (unsigned int y = 0; y < numBlocksY; ++y)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, cacheLinesPerRow*32);
|
|
|
|
dst += bpmem.copyMipMapStrideChannels*32;
|
|
|
|
src += map.RowPitch;
|
|
|
|
}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->Unmap(m_outStage, 0);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
|
|
|
encodeSize = bpmem.copyMipMapStrideChannels*32 * numBlocksY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore API
|
|
|
|
g_renderer->RestoreAPIState();
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->OMSetRenderTargets(1,
|
|
|
|
&FramebufferManager::GetEFBColorTexture()->GetRTV(),
|
2011-03-08 23:25:37 +00:00
|
|
|
FramebufferManager::GetEFBDepthTexture()->GetDSV());
|
|
|
|
|
|
|
|
return encodeSize;
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
ID3D11PixelShader* PSTextureEncoder::SetStaticShader(unsigned int dstFormat, PEControl::PixelFormat srcFormat,
|
2011-03-08 23:25:37 +00:00
|
|
|
bool isIntensity, bool scaleByHalf)
|
|
|
|
{
|
2014-03-23 20:44:23 +00:00
|
|
|
size_t fetchNum = static_cast<size_t>(srcFormat);
|
2011-03-08 23:25:37 +00:00
|
|
|
size_t scaledFetchNum = scaleByHalf ? 1 : 0;
|
|
|
|
size_t intensityNum = isIntensity ? 1 : 0;
|
|
|
|
size_t generatorNum = dstFormat;
|
|
|
|
|
|
|
|
ComboKey key = MakeComboKey(dstFormat, srcFormat, isIntensity, scaleByHalf);
|
|
|
|
|
|
|
|
ComboMap::iterator it = m_staticShaders.find(key);
|
|
|
|
if (it == m_staticShaders.end())
|
|
|
|
{
|
|
|
|
INFO_LOG(VIDEO, "Compiling efb encoding shader for dstFormat 0x%X, srcFormat %d, isIntensity %d, scaleByHalf %d",
|
2014-03-23 20:44:23 +00:00
|
|
|
dstFormat, static_cast<int>(srcFormat), isIntensity ? 1 : 0, scaleByHalf ? 1 : 0);
|
2011-03-08 23:25:37 +00:00
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
u32 format = dstFormat;
|
|
|
|
|
|
|
|
if (srcFormat == PEControl::Z24)
|
|
|
|
{
|
|
|
|
format |= _GX_TF_ZTF;
|
|
|
|
if (dstFormat == 11)
|
|
|
|
format = GX_TF_Z16;
|
|
|
|
else if (format < GX_TF_Z8 || format > GX_TF_Z24X8)
|
|
|
|
format |= _GX_TF_CTF;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dstFormat > GX_TF_RGBA8 || (dstFormat < GX_TF_RGB565 && !isIntensity))
|
|
|
|
format |= _GX_TF_CTF;
|
|
|
|
}
|
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
D3DBlob* bytecode = nullptr;
|
2015-01-25 23:49:35 +00:00
|
|
|
const char* shader = TextureConversionShader::GenerateEncodingShader(format, API_D3D);
|
|
|
|
if (!D3D::CompilePixelShader(shader, &bytecode))
|
2011-03-08 23:25:37 +00:00
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "EFB encoder shader for dstFormat 0x%X, srcFormat %d, isIntensity %d, scaleByHalf %d failed to compile",
|
2014-03-23 20:44:23 +00:00
|
|
|
dstFormat, static_cast<int>(srcFormat), isIntensity ? 1 : 0, scaleByHalf ? 1 : 0);
|
2014-03-09 20:14:26 +00:00
|
|
|
m_staticShaders[key] = nullptr;
|
2015-01-25 23:49:35 +00:00
|
|
|
return nullptr;
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
ID3D11PixelShader* newShader;
|
2014-03-09 20:14:26 +00:00
|
|
|
HRESULT hr = D3D::device->CreatePixelShader(bytecode->Data(), bytecode->Size(), nullptr, &newShader);
|
2011-03-08 23:25:37 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "create efb encoder pixel shader");
|
|
|
|
|
2015-02-09 12:43:28 +00:00
|
|
|
char debugName[255] = {};
|
|
|
|
sprintf_s(debugName, "efb encoder pixel shader (dst:%d, src:%d, intensity:%d, scale:%d)",
|
|
|
|
dstFormat, srcFormat, isIntensity, scaleByHalf);
|
|
|
|
D3D::SetDebugObjectName(newShader, debugName);
|
|
|
|
|
2015-06-28 23:08:28 +00:00
|
|
|
it = m_staticShaders.emplace(key, newShader).first;
|
2011-06-11 19:37:21 +00:00
|
|
|
bytecode->Release();
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 23:49:35 +00:00
|
|
|
return it->second;
|
2011-03-08 23:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|