GPU/D3D11: Purge D3D11::StagingTexture

This commit is contained in:
Connor McLaughlin
2022-09-26 22:11:36 +10:00
parent 6bafcea94f
commit 8aa1c8827c
11 changed files with 87 additions and 394 deletions

View File

@ -82,8 +82,6 @@ if(WIN32)
d3d11/shader_cache.h
d3d11/shader_compiler.cpp
d3d11/shader_compiler.h
d3d11/staging_texture.cpp
d3d11/staging_texture.h
d3d11/stream_buffer.cpp
d3d11/stream_buffer.h
d3d11/texture.cpp

View File

@ -11,7 +11,6 @@
<ClInclude Include="crash_handler.h" />
<ClInclude Include="d3d11\shader_cache.h" />
<ClInclude Include="d3d11\shader_compiler.h" />
<ClInclude Include="d3d11\staging_texture.h" />
<ClInclude Include="d3d11\stream_buffer.h" />
<ClInclude Include="d3d11\texture.h" />
<ClInclude Include="d3d12\context.h" />
@ -114,7 +113,6 @@
<ClCompile Include="crash_handler.cpp" />
<ClCompile Include="d3d11\shader_cache.cpp" />
<ClCompile Include="d3d11\shader_compiler.cpp" />
<ClCompile Include="d3d11\staging_texture.cpp" />
<ClCompile Include="d3d11\stream_buffer.cpp" />
<ClCompile Include="d3d11\texture.cpp" />
<ClCompile Include="error.cpp" />

View File

@ -20,9 +20,6 @@
<ClInclude Include="d3d11\texture.h">
<Filter>d3d11</Filter>
</ClInclude>
<ClInclude Include="d3d11\staging_texture.h">
<Filter>d3d11</Filter>
</ClInclude>
<ClInclude Include="d3d11\shader_compiler.h">
<Filter>d3d11</Filter>
</ClInclude>
@ -146,9 +143,6 @@
<ClCompile Include="d3d11\texture.cpp">
<Filter>d3d11</Filter>
</ClCompile>
<ClCompile Include="d3d11\staging_texture.cpp">
<Filter>d3d11</Filter>
</ClCompile>
<ClCompile Include="d3d11\stream_buffer.cpp">
<Filter>d3d11</Filter>
</ClCompile>

View File

@ -1,117 +0,0 @@
#include "staging_texture.h"
#include "../assert.h"
#include "../log.h"
Log_SetChannel(D3D11);
namespace D3D11 {
StagingTexture::StagingTexture() : m_width(0), m_height(0) {}
StagingTexture::~StagingTexture()
{
Destroy();
}
bool StagingTexture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT format, bool for_uploading)
{
CD3D11_TEXTURE2D_DESC desc(format, width, height, 1, 1, 0, for_uploading ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_STAGING,
for_uploading ? D3D11_CPU_ACCESS_WRITE : D3D11_CPU_ACCESS_READ, 1, 0, 0);
ComPtr<ID3D11Texture2D> texture;
const HRESULT tex_hr = device->CreateTexture2D(&desc, nullptr, texture.GetAddressOf());
if (FAILED(tex_hr))
{
Log_ErrorPrintf("Create texture failed: 0x%08X", tex_hr);
return false;
}
m_texture = std::move(texture);
m_width = desc.Width;
m_height = desc.Height;
m_format = desc.Format;
return true;
}
void StagingTexture::Destroy()
{
Assert(!IsMapped());
m_texture.Reset();
}
bool StagingTexture::Map(ID3D11DeviceContext* context, bool writing)
{
Assert(!IsMapped());
const HRESULT hr = context->Map(m_texture.Get(), 0, writing ? D3D11_MAP_WRITE : D3D11_MAP_READ, 0, &m_map);
if (FAILED(hr))
{
Log_ErrorPrintf("Map staging texture failed: 0x%08X", hr);
return false;
}
return true;
}
void StagingTexture::Unmap(ID3D11DeviceContext* context)
{
Assert(IsMapped());
context->Unmap(m_texture.Get(), 0);
m_map = {};
}
void StagingTexture::CopyToTexture(ID3D11DeviceContext* context, u32 src_x, u32 src_y, ID3D11Resource* dst_texture,
u32 dst_subresource, u32 dst_x, u32 dst_y, u32 width, u32 height)
{
DebugAssert((src_x + width) <= m_width && (src_y + height) <= m_height);
const CD3D11_BOX box(static_cast<LONG>(src_x), static_cast<LONG>(src_y), 0, static_cast<LONG>(src_x + width),
static_cast<LONG>(src_y + height), 1);
context->CopySubresourceRegion(dst_texture, dst_subresource, dst_x, dst_y, 0, m_texture.Get(), 0, &box);
}
void StagingTexture::CopyFromTexture(ID3D11DeviceContext* context, ID3D11Resource* src_texture, u32 src_subresource,
u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height)
{
DebugAssert((dst_x + width) <= m_width && (dst_y + height) <= m_height);
const CD3D11_BOX box(static_cast<LONG>(src_x), static_cast<LONG>(src_y), 0, static_cast<LONG>(src_x + width),
static_cast<LONG>(src_y + height), 1);
context->CopySubresourceRegion(m_texture.Get(), 0, dst_x, dst_y, 0, src_texture, src_subresource, &box);
}
bool AutoStagingTexture::EnsureSize(ID3D11DeviceContext* context, u32 width, u32 height, DXGI_FORMAT format,
bool for_uploading)
{
if (m_texture && m_width >= width && m_height >= height && m_format == format)
return true;
ComPtr<ID3D11Device> device;
context->GetDevice(device.GetAddressOf());
CD3D11_TEXTURE2D_DESC new_desc(format, width, height, 1, 1, 0,
for_uploading ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_STAGING,
for_uploading ? D3D11_CPU_ACCESS_WRITE : D3D11_CPU_ACCESS_READ, 1, 0, 0);
ComPtr<ID3D11Texture2D> new_texture;
HRESULT hr = device->CreateTexture2D(&new_desc, nullptr, new_texture.GetAddressOf());
if (FAILED(hr))
{
Log_ErrorPrintf("Create texture failed: 0x%08X", hr);
return false;
}
m_texture = std::move(new_texture);
m_width = width;
m_height = height;
m_format = format;
return true;
}
void AutoStagingTexture::CopyFromTexture(ID3D11DeviceContext* context, ID3D11Resource* src_texture, u32 src_subresource,
u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height)
{
if (!EnsureSize(context, width, height, m_format, false))
return;
StagingTexture::CopyFromTexture(context, src_texture, src_subresource, src_x, src_y, dst_x, dst_y, width, height);
}
} // namespace D3D11

View File

@ -1,156 +0,0 @@
#pragma once
#include "../types.h"
#include "../windows_headers.h"
#include <cstring>
#include <d3d11.h>
#include <wrl/client.h>
namespace D3D11 {
class StagingTexture
{
public:
template<typename T>
using ComPtr = Microsoft::WRL::ComPtr<T>;
StagingTexture();
~StagingTexture();
ALWAYS_INLINE ID3D11Texture2D* GetD3DTexture() const { return m_texture.Get(); }
ALWAYS_INLINE u32 GetWidth() const { return m_width; }
ALWAYS_INLINE u32 GetHeight() const { return m_height; }
ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return m_format; }
ALWAYS_INLINE bool IsMapped() const { return m_map.pData != nullptr; }
ALWAYS_INLINE const D3D11_MAPPED_SUBRESOURCE& GetMappedSubresource() const { return m_map; }
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); }
bool Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT format, bool for_uploading);
void Destroy();
bool Map(ID3D11DeviceContext* context, bool writing);
void Unmap(ID3D11DeviceContext* context);
void CopyToTexture(ID3D11DeviceContext* context, u32 src_x, u32 src_y, ID3D11Resource* dst_texture,
u32 dst_subresource, u32 dst_x, u32 dst_y, u32 width, u32 height);
void CopyFromTexture(ID3D11DeviceContext* context, ID3D11Resource* src_texture, u32 src_subresource, u32 src_x,
u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height);
template<typename T>
T ReadPixel(u32 x, u32 y)
{
T pixel;
std::memcpy(&pixel, static_cast<u8*>(m_map.pData) + (y * m_map.RowPitch) + x, sizeof(T));
return pixel;
}
template<typename T>
void WritePixel(u32 x, u32 y, T pixel)
{
std::memcpy(static_cast<u8*>(m_map.pData) + (y * m_map.RowPitch) + x, &pixel, sizeof(T));
}
template<typename T>
void ReadPixels(u32 x, u32 y, u32 width, u32 height, u32 stride, T* data)
{
const u8* src_ptr = static_cast<u8*>(m_map.pData) + (y * m_map.RowPitch) + (x * sizeof(T));
u8* dst_ptr = reinterpret_cast<u8*>(data);
if (m_map.RowPitch != stride || width != m_width || x != 0)
{
for (u32 row = 0; row < height; row++)
{
std::memcpy(dst_ptr, src_ptr, sizeof(T) * width);
src_ptr += m_map.RowPitch;
dst_ptr += stride;
}
}
else
{
std::memcpy(dst_ptr, src_ptr, stride * height);
}
}
template<typename T>
bool ReadPixels(ID3D11DeviceContext* context, u32 x, u32 y, u32 width, u32 height, u32 stride, T* data)
{
const bool was_mapped = IsMapped();
if (!was_mapped && !Map(context, false))
return false;
ReadPixels<T>(x, y, width, height, stride, data);
if (!was_mapped)
Unmap(context);
return true;
}
template<typename T>
void WritePixels(u32 x, u32 y, u32 width, u32 height, u32 stride, const T* data)
{
const u8* src_ptr = reinterpret_cast<const u8*>(data);
u8* dst_ptr = static_cast<u8*>(m_map.pData) + (y * m_map.RowPitch) + (x * sizeof(T));
if (m_map.RowPitch != stride || width != m_width || x != 0)
{
for (u32 row = 0; row < height; row++)
{
std::memcpy(dst_ptr, src_ptr, sizeof(T) * width);
src_ptr += stride;
dst_ptr += m_map.RowPitch;
}
}
else
{
std::memcpy(dst_ptr, src_ptr, stride * height);
}
}
template<typename T>
bool WritePixels(ID3D11DeviceContext* context, u32 x, u32 y, u32 width, u32 height, u32 stride, const T* data)
{
const bool was_mapped = IsMapped();
if (!was_mapped && !Map(context, true))
return false;
WritePixels<T>(context, x, y, width, height, stride, data);
if (!was_mapped)
Unmap(context);
return true;
}
protected:
ComPtr<ID3D11Texture2D> m_texture;
u32 m_width;
u32 m_height;
DXGI_FORMAT m_format;
D3D11_MAPPED_SUBRESOURCE m_map = {};
};
class AutoStagingTexture : public StagingTexture
{
public:
bool EnsureSize(ID3D11DeviceContext* context, u32 width, u32 height, DXGI_FORMAT format, bool for_uploading);
void CopyFromTexture(ID3D11DeviceContext* context, ID3D11Resource* src_texture, u32 src_subresource, u32 src_x,
u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height);
template<typename T>
bool WritePixels(ID3D11DeviceContext* context, u32 x, u32 y, u32 width, u32 height, u32 stride, const T* data)
{
if (!EnsureSize(context, width, height, m_format, true))
return false;
const bool was_mapped = IsMapped();
if (!was_mapped && !Map(context, true))
return false;
WritePixels<T>(context, x, y, width, height, stride, data);
if (!was_mapped)
Unmap(context);
return true;
}
};
} // namespace D3D11