2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-10-29 00:19:09 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DUtil.h"
|
|
|
|
#include "VideoBackends/D3D/FramebufferManager.h"
|
2014-11-29 20:31:30 +00:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
2014-10-29 00:19:09 +00:00
|
|
|
#include "VideoBackends/D3D/PixelShaderCache.h"
|
2014-10-29 08:52:08 +00:00
|
|
|
#include "VideoBackends/D3D/PSTextureEncoder.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/TextureCache.h"
|
|
|
|
#include "VideoBackends/D3D/TextureEncoder.h"
|
2014-02-19 11:14:09 +00:00
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
static TextureEncoder* g_encoder = nullptr;
|
2013-11-24 03:43:54 +00:00
|
|
|
const size_t MAX_COPY_BUFFERS = 32;
|
2011-06-11 19:37:21 +00:00
|
|
|
ID3D11Buffer* efbcopycbuf[MAX_COPY_BUFFERS] = { 0 };
|
|
|
|
|
|
|
|
TextureCache::TCacheEntry::~TCacheEntry()
|
|
|
|
{
|
|
|
|
texture->Release();
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
void TextureCache::TCacheEntry::Bind(unsigned int stage)
|
|
|
|
{
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetTexture(stage, texture->GetSRV());
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:33:19 +00:00
|
|
|
bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int level)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2012-05-12 11:50:03 +00:00
|
|
|
// TODO: Somehow implement this (D3DX11 doesn't support dumping individual LODs)
|
|
|
|
static bool warn_once = true;
|
|
|
|
if (level && warn_once)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Dumping individual LOD not supported by D3D11 backend!");
|
|
|
|
warn_once = false;
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-13 11:48:02 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
ID3D11Texture2D* pNewTexture = nullptr;
|
2013-11-13 11:48:02 +00:00
|
|
|
ID3D11Texture2D* pSurface = texture->GetTex();
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
pSurface->GetDesc(&desc);
|
|
|
|
|
|
|
|
desc.BindFlags = 0;
|
|
|
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
|
|
|
|
desc.Usage = D3D11_USAGE_STAGING;
|
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, &pNewTexture);
|
2013-11-13 11:48:02 +00:00
|
|
|
|
2013-11-15 00:00:38 +00:00
|
|
|
bool saved_png = false;
|
|
|
|
|
2013-11-13 11:48:02 +00:00
|
|
|
if (SUCCEEDED(hr) && pNewTexture)
|
|
|
|
{
|
|
|
|
D3D::context->CopyResource(pNewTexture, pSurface);
|
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
HRESULT hr = D3D::context->Map(pNewTexture, 0, D3D11_MAP_READ_WRITE, 0, &map);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2013-11-16 22:12:07 +00:00
|
|
|
saved_png = TextureToPng((u8*)map.pData, map.RowPitch, filename, desc.Width, desc.Height);
|
2013-11-13 11:48:02 +00:00
|
|
|
D3D::context->Unmap(pNewTexture, 0);
|
|
|
|
}
|
|
|
|
SAFE_RELEASE(pNewTexture);
|
|
|
|
}
|
|
|
|
|
2013-11-15 00:00:38 +00:00
|
|
|
return saved_png;
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,
|
2012-08-10 11:13:51 +00:00
|
|
|
unsigned int expanded_width, unsigned int level)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
|
|
|
D3D::ReplaceRGBATexture2D(texture->GetTex(), TextureCache::temp, width, height, expanded_width, level, usage);
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2015-01-17 08:46:31 +00:00
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateTexture(unsigned int width, unsigned int height, unsigned int tex_levels)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
D3D11_USAGE usage = D3D11_USAGE_DEFAULT;
|
|
|
|
D3D11_CPU_ACCESS_FLAG cpu_access = (D3D11_CPU_ACCESS_FLAG)0;
|
|
|
|
|
2010-11-24 19:13:19 +00:00
|
|
|
if (tex_levels == 1)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-10-19 22:24:27 +00:00
|
|
|
usage = D3D11_USAGE_DYNAMIC;
|
|
|
|
cpu_access = D3D11_CPU_ACCESS_WRITE;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
|
|
|
|
const D3D11_TEXTURE2D_DESC texdesc = CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
|
|
width, height, 1, tex_levels, D3D11_BIND_SHADER_RESOURCE, usage, cpu_access);
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
ID3D11Texture2D *pTexture;
|
2014-05-19 20:35:53 +00:00
|
|
|
const HRESULT hr = D3D::device->CreateTexture2D(&texdesc, nullptr, &pTexture);
|
2011-06-11 19:37:21 +00:00
|
|
|
CHECK(SUCCEEDED(hr), "Create texture of the TextureCache");
|
2010-10-19 22:24:27 +00:00
|
|
|
|
2015-01-11 14:03:41 +00:00
|
|
|
TCacheEntryConfig config;
|
|
|
|
config.width = width;
|
|
|
|
config.height = height;
|
|
|
|
config.levels = tex_levels;
|
|
|
|
|
|
|
|
TCacheEntry* const entry = new TCacheEntry(config, new D3DTexture2D(pTexture, D3D11_BIND_SHADER_RESOURCE));
|
2010-10-19 22:24:27 +00:00
|
|
|
entry->usage = usage;
|
|
|
|
|
|
|
|
// TODO: better debug names
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetTex(), "a texture of the TextureCache");
|
2013-10-29 05:23:17 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)entry->texture->GetSRV(), "shader resource view of a texture of the TextureCache");
|
2011-06-11 19:37:21 +00:00
|
|
|
|
|
|
|
SAFE_RELEASE(pTexture);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
return entry;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
void TextureCache::TCacheEntry::FromRenderTarget(u32 dstAddr, unsigned int dstFormat,
|
2014-03-23 20:44:23 +00:00
|
|
|
PEControl::PixelFormat srcFormat, const EFBRectangle& srcRect,
|
2011-02-26 23:41:02 +00:00
|
|
|
bool isIntensity, bool scaleByHalf, unsigned int cbufid,
|
|
|
|
const float *colmat)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2012-01-29 19:24:23 +00:00
|
|
|
if (type != TCET_EC_DYNAMIC || g_ActiveConfig.bCopyEFBToTexture)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2011-02-26 23:41:02 +00:00
|
|
|
g_renderer->ResetAPIState();
|
|
|
|
|
|
|
|
// stretch picture with increased internal resolution
|
2015-01-11 14:03:41 +00:00
|
|
|
const D3D11_VIEWPORT vp = CD3D11_VIEWPORT(0.f, 0.f, (float)config.width, (float)config.height);
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->RSSetViewports(1, &vp);
|
2011-02-26 23:41:02 +00:00
|
|
|
|
|
|
|
// set transformation
|
2014-03-09 20:14:26 +00:00
|
|
|
if (nullptr == efbcopycbuf[cbufid])
|
2011-02-26 23:41:02 +00:00
|
|
|
{
|
|
|
|
const D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(28 * sizeof(float), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DEFAULT);
|
|
|
|
D3D11_SUBRESOURCE_DATA data;
|
|
|
|
data.pSysMem = colmat;
|
2011-06-11 19:37:21 +00:00
|
|
|
HRESULT hr = D3D::device->CreateBuffer(&cbdesc, &data, &efbcopycbuf[cbufid]);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create efb copy constant buffer %d", cbufid);
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)efbcopycbuf[cbufid], "a constant buffer used in TextureCache::CopyRenderTargetToTexture");
|
2011-02-26 23:41:02 +00:00
|
|
|
}
|
2014-12-06 13:54:06 +00:00
|
|
|
D3D::stateman->SetPixelConstants(efbcopycbuf[cbufid]);
|
2011-02-26 23:41:02 +00:00
|
|
|
|
|
|
|
const TargetRectangle targetSource = g_renderer->ConvertEFBRectangle(srcRect);
|
|
|
|
// TODO: try targetSource.asRECT();
|
|
|
|
const D3D11_RECT sourcerect = CD3D11_RECT(targetSource.left, targetSource.top, targetSource.right, targetSource.bottom);
|
|
|
|
|
|
|
|
// Use linear filtering if (bScaleByHalf), use point filtering otherwise
|
|
|
|
if (scaleByHalf)
|
|
|
|
D3D::SetLinearCopySampler();
|
|
|
|
else
|
|
|
|
D3D::SetPointCopySampler();
|
|
|
|
|
2014-12-12 23:32:08 +00:00
|
|
|
// if texture is currently in use, it needs to be temporarily unset
|
|
|
|
u32 textureSlotMask = D3D::stateman->UnsetTexture(texture->GetSRV());
|
|
|
|
D3D::stateman->Apply();
|
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
D3D::context->OMSetRenderTargets(1, &texture->GetRTV(), nullptr);
|
2011-02-26 23:41:02 +00:00
|
|
|
|
|
|
|
// Create texture copy
|
|
|
|
D3D::drawShadedTexQuad(
|
2014-03-23 20:44:23 +00:00
|
|
|
(srcFormat == PEControl::Z24) ? FramebufferManager::GetEFBDepthTexture()->GetSRV() : FramebufferManager::GetEFBColorTexture()->GetSRV(),
|
2011-05-12 02:14:45 +00:00
|
|
|
&sourcerect, Renderer::GetTargetWidth(), Renderer::GetTargetHeight(),
|
2014-03-23 20:44:23 +00:00
|
|
|
(srcFormat == PEControl::Z24) ? PixelShaderCache::GetDepthMatrixProgram(true) : PixelShaderCache::GetColorMatrixProgram(true),
|
2014-12-21 14:37:05 +00:00
|
|
|
VertexShaderCache::GetSimpleVertexShader(), VertexShaderCache::GetSimpleInputLayout(), GeometryShaderCache::GetCopyGeometryShader());
|
2011-02-26 23:41:02 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
D3D::context->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV(), FramebufferManager::GetEFBDepthTexture()->GetDSV());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
g_renderer->RestoreAPIState();
|
2014-12-12 23:32:08 +00:00
|
|
|
|
|
|
|
// Restore old texture in all previously used slots, if any
|
2014-12-17 19:31:08 +00:00
|
|
|
D3D::stateman->SetTextureByMask(textureSlotMask, texture->GetSRV());
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
2010-11-27 11:11:05 +00:00
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
if (!g_ActiveConfig.bCopyEFBToTexture)
|
|
|
|
{
|
|
|
|
u8* dst = Memory::GetPointer(dstAddr);
|
2011-12-30 00:00:34 +00:00
|
|
|
size_t encoded_size = g_encoder->Encode(dst, dstFormat, srcFormat, srcRect, isIntensity, scaleByHalf);
|
|
|
|
|
2012-06-20 14:43:13 +00:00
|
|
|
u64 hash = GetHash64(dst, (int)encoded_size, g_ActiveConfig.iSafeTextureCache_ColorSamples);
|
2011-02-26 23:41:02 +00:00
|
|
|
|
2015-01-10 12:47:52 +00:00
|
|
|
size_in_bytes = (u32)encoded_size;
|
|
|
|
|
2011-12-30 00:00:34 +00:00
|
|
|
// Mark texture entries in destination address range dynamic unless caching is enabled and the texture entry is up to date
|
|
|
|
if (!g_ActiveConfig.bEFBCopyCacheEnable)
|
|
|
|
TextureCache::MakeRangeDynamic(addr, (u32)encoded_size);
|
|
|
|
else if (!TextureCache::Find(addr, hash))
|
|
|
|
TextureCache::MakeRangeDynamic(addr, (u32)encoded_size);
|
2012-06-20 14:43:13 +00:00
|
|
|
|
|
|
|
this->hash = hash;
|
2011-02-26 23:41:02 +00:00
|
|
|
}
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureCache::TCacheEntryBase* TextureCache::CreateRenderTargetTexture(
|
2015-01-11 14:03:41 +00:00
|
|
|
unsigned int scaled_tex_w, unsigned int scaled_tex_h, unsigned int layers)
|
2010-10-19 22:24:27 +00:00
|
|
|
{
|
2015-01-11 14:03:41 +00:00
|
|
|
TCacheEntryConfig config;
|
|
|
|
config.width = scaled_tex_w;
|
|
|
|
config.height = scaled_tex_h;
|
|
|
|
config.layers = layers;
|
|
|
|
config.rendertarget = true;
|
|
|
|
|
|
|
|
return new TCacheEntry(config, D3DTexture2D::Create(scaled_tex_w, scaled_tex_h,
|
2010-10-19 22:24:27 +00:00
|
|
|
(D3D11_BIND_FLAG)((int)D3D11_BIND_RENDER_TARGET | (int)D3D11_BIND_SHADER_RESOURCE),
|
2015-01-11 14:03:41 +00:00
|
|
|
D3D11_USAGE_DEFAULT, DXGI_FORMAT_R8G8B8A8_UNORM, 1, layers));
|
2010-10-19 22:24:27 +00:00
|
|
|
}
|
2010-06-18 18:40:58 +00:00
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::TextureCache()
|
|
|
|
{
|
2011-02-26 23:41:02 +00:00
|
|
|
// FIXME: Is it safe here?
|
2011-06-11 19:37:21 +00:00
|
|
|
g_encoder = new PSTextureEncoder;
|
|
|
|
g_encoder->Init();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 22:24:27 +00:00
|
|
|
TextureCache::~TextureCache()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
for (unsigned int k = 0; k < MAX_COPY_BUFFERS; ++k)
|
|
|
|
SAFE_RELEASE(efbcopycbuf[k]);
|
|
|
|
|
|
|
|
g_encoder->Shutdown();
|
|
|
|
delete g_encoder;
|
2014-03-09 20:14:26 +00:00
|
|
|
g_encoder = nullptr;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|