2010-04-25 00:31:27 +00:00
|
|
|
/*
|
2010-04-24 21:37:39 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2010-04-24 21:37:39 +00:00
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2010-04-24 21:37:39 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2010-04-24 21:37:39 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSTexture11.h"
|
2016-02-19 18:05:11 +00:00
|
|
|
#include "GSPng.h"
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
GSTexture11::GSTexture11(ID3D11Texture2D* texture)
|
2018-09-20 17:14:59 +00:00
|
|
|
: m_texture(texture), m_layer(0)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_texture);
|
|
|
|
|
|
|
|
m_texture->GetDevice(&m_dev);
|
|
|
|
m_texture->GetDesc(&m_desc);
|
|
|
|
|
|
|
|
m_dev->GetImmediateContext(&m_ctx);
|
|
|
|
|
|
|
|
m_size.x = (int)m_desc.Width;
|
|
|
|
m_size.y = (int)m_desc.Height;
|
|
|
|
|
|
|
|
if(m_desc.BindFlags & D3D11_BIND_RENDER_TARGET) m_type = RenderTarget;
|
|
|
|
else if(m_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL) m_type = DepthStencil;
|
|
|
|
else if(m_desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) m_type = Texture;
|
|
|
|
else if(m_desc.Usage == D3D11_USAGE_STAGING) m_type = Offscreen;
|
|
|
|
|
|
|
|
m_format = (int)m_desc.Format;
|
|
|
|
|
2018-09-20 17:14:59 +00:00
|
|
|
m_max_layer = m_desc.MipLevels;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 19:36:52 +00:00
|
|
|
bool GSTexture11::Update(const GSVector4i& r, const void* data, int pitch, int layer)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2018-09-20 17:14:59 +00:00
|
|
|
if(layer >= m_max_layer)
|
|
|
|
return true;
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
if(m_dev && m_texture)
|
|
|
|
{
|
2016-04-14 10:00:58 +00:00
|
|
|
D3D11_BOX box = { (UINT)r.left, (UINT)r.top, 0U, (UINT)r.right, (UINT)r.bottom, 1U };
|
2018-09-20 17:14:59 +00:00
|
|
|
UINT subresource = layer; // MipSlice + (ArraySlice * MipLevels).
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2018-09-20 17:14:59 +00:00
|
|
|
m_ctx->UpdateSubresource(m_texture, subresource, &box, data, pitch, 0);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-30 19:36:52 +00:00
|
|
|
bool GSTexture11::Map(GSMap& m, const GSVector4i* r, int layer)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
|
|
|
if(r != NULL)
|
|
|
|
{
|
|
|
|
// ASSERT(0); // not implemented
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:14:59 +00:00
|
|
|
if(layer >= m_max_layer)
|
|
|
|
return false;
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
if(m_texture && m_desc.Usage == D3D11_USAGE_STAGING)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
2018-09-20 17:14:59 +00:00
|
|
|
UINT subresource = layer;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2018-09-20 17:14:59 +00:00
|
|
|
if(SUCCEEDED(m_ctx->Map(m_texture, subresource, D3D11_MAP_READ_WRITE, 0, &map)))
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
|
|
|
m.bits = (uint8*)map.pData;
|
|
|
|
m.pitch = (int)map.RowPitch;
|
|
|
|
|
2018-09-20 17:14:59 +00:00
|
|
|
m_layer = layer;
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSTexture11::Unmap()
|
|
|
|
{
|
|
|
|
if(m_texture)
|
|
|
|
{
|
2018-09-20 17:14:59 +00:00
|
|
|
UINT subresource = m_layer;
|
|
|
|
m_ctx->Unmap(m_texture, subresource);
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 20:16:14 +00:00
|
|
|
bool GSTexture11::Save(const std::string& fn)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-02-19 18:05:11 +00:00
|
|
|
CComPtr<ID3D11Texture2D> res;
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
m_texture->GetDesc(&desc);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
desc.Usage = D3D11_USAGE_STAGING;
|
|
|
|
desc.BindFlags = 0;
|
|
|
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
HRESULT hr = m_dev->CreateTexture2D(&desc, nullptr, &res);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
m_ctx->CopyResource(res, m_texture);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
if (m_desc.BindFlags & D3D11_BIND_DEPTH_STENCIL)
|
|
|
|
{
|
|
|
|
CComPtr<ID3D11Texture2D> dst;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2016-02-19 18:05:11 +00:00
|
|
|
desc.CPUAccessFlags |= D3D11_CPU_ACCESS_WRITE;
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
hr = m_dev->CreateTexture2D(&desc, nullptr, &dst);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
|
|
|
D3D11_MAPPED_SUBRESOURCE sm, dm;
|
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
hr = m_ctx->Map(res, 0, D3D11_MAP_READ, 0, &sm);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
hr = m_ctx->Map(dst, 0, D3D11_MAP_WRITE, 0, &dm);
|
2016-02-19 18:05:11 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
m_ctx->Unmap(res, 0);
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
uint8* s = static_cast<uint8*>(sm.pData);
|
|
|
|
uint8* d = static_cast<uint8*>(dm.pData);
|
2010-04-24 21:37:39 +00:00
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
for (uint32 y = 0; y < desc.Height; y++, s += sm.RowPitch, d += dm.RowPitch)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-02-19 18:05:11 +00:00
|
|
|
for (uint32 x = 0; x < desc.Width; x++)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-02-19 18:05:11 +00:00
|
|
|
reinterpret_cast<uint32*>(d)[x] = static_cast<uint32>(ldexpf(reinterpret_cast<float*>(s)[x*2], 32));
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
m_ctx->Unmap(res, 0);
|
2010-04-24 21:37:39 +00:00
|
|
|
m_ctx->Unmap(dst, 0);
|
|
|
|
|
|
|
|
res = dst;
|
|
|
|
}
|
2016-02-19 18:05:11 +00:00
|
|
|
|
|
|
|
res->GetDesc(&desc);
|
|
|
|
|
2018-12-22 11:32:34 +00:00
|
|
|
#ifdef ENABLE_OGL_DEBUG
|
|
|
|
GSPng::Format format = GSPng::RGB_A_PNG;
|
|
|
|
#else
|
|
|
|
GSPng::Format format = GSPng::RGB_PNG;
|
|
|
|
#endif
|
2016-02-19 18:05:11 +00:00
|
|
|
switch (desc.Format)
|
2010-04-24 21:37:39 +00:00
|
|
|
{
|
2016-02-19 18:05:11 +00:00
|
|
|
case DXGI_FORMAT_A8_UNORM:
|
|
|
|
format = GSPng::R8I_PNG;
|
|
|
|
break;
|
|
|
|
case DXGI_FORMAT_R8G8B8A8_UNORM:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "DXGI_FORMAT %d not saved to image\n", desc.Format);
|
|
|
|
return false;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 18:05:11 +00:00
|
|
|
D3D11_MAPPED_SUBRESOURCE sm;
|
|
|
|
hr = m_ctx->Map(res, 0, D3D11_MAP_READ, 0, &sm);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-20 12:05:53 +00:00
|
|
|
int compression = theApp.GetConfigI("png_compression_level");
|
2016-02-24 21:52:17 +00:00
|
|
|
bool success = GSPng::Save(format, fn, static_cast<uint8*>(sm.pData), desc.Width, desc.Height, sm.RowPitch, compression);
|
2016-02-19 18:05:11 +00:00
|
|
|
|
|
|
|
m_ctx->Unmap(res, 0);
|
|
|
|
|
|
|
|
return success;
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSTexture11::operator ID3D11Texture2D*()
|
|
|
|
{
|
|
|
|
return m_texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTexture11::operator ID3D11ShaderResourceView*()
|
|
|
|
{
|
|
|
|
if(!m_srv && m_dev && m_texture)
|
|
|
|
{
|
2018-12-04 05:20:41 +00:00
|
|
|
if(m_desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS)
|
|
|
|
{
|
|
|
|
D3D11_SHADER_RESOURCE_VIEW_DESC srvd = {};
|
|
|
|
|
|
|
|
srvd.Format = DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
|
|
|
|
srvd.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
srvd.Texture2D.MipLevels = 1;
|
|
|
|
|
|
|
|
m_dev->CreateShaderResourceView(m_texture, &srvd, &m_srv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_dev->CreateShaderResourceView(m_texture, NULL, &m_srv);
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_srv;
|
|
|
|
}
|
|
|
|
|
2012-01-18 11:47:31 +00:00
|
|
|
GSTexture11::operator ID3D11UnorderedAccessView*()
|
|
|
|
{
|
|
|
|
if(!m_uav && m_dev && m_texture)
|
|
|
|
{
|
|
|
|
m_dev->CreateUnorderedAccessView(m_texture, NULL, &m_uav);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_uav;
|
|
|
|
}
|
|
|
|
|
2010-04-24 21:37:39 +00:00
|
|
|
GSTexture11::operator ID3D11RenderTargetView*()
|
|
|
|
{
|
|
|
|
ASSERT(m_dev);
|
|
|
|
|
|
|
|
if(!m_rtv && m_dev && m_texture)
|
|
|
|
{
|
|
|
|
m_dev->CreateRenderTargetView(m_texture, NULL, &m_rtv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_rtv;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSTexture11::operator ID3D11DepthStencilView*()
|
|
|
|
{
|
|
|
|
if(!m_dsv && m_dev && m_texture)
|
|
|
|
{
|
2018-12-04 05:20:41 +00:00
|
|
|
if(m_desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS)
|
|
|
|
{
|
|
|
|
D3D11_DEPTH_STENCIL_VIEW_DESC dsvd = {};
|
|
|
|
|
|
|
|
dsvd.Format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
|
|
|
|
dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
|
|
|
|
|
|
|
|
m_dev->CreateDepthStencilView(m_texture, &dsvd, &m_dsv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_dev->CreateDepthStencilView(m_texture, NULL, &m_dsv);
|
|
|
|
}
|
2010-04-24 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_dsv;
|
|
|
|
}
|
2018-12-15 18:40:03 +00:00
|
|
|
|
|
|
|
bool GSTexture11::Equal(GSTexture11* tex)
|
|
|
|
{
|
|
|
|
return tex && m_texture == tex->m_texture;
|
|
|
|
}
|