Commit 2c479d6e by Jamie Madill Committed by Commit Bot

D3D11: Use TextureHelper11 everywhere.

This consolidates all texture allocation into ResourceManager11. It removes a lot of error checking and resource management code. In a few places we're storing some redundant information, like in TextureStorage11, we might store the Format pointer in two places, or the Texture's size. BUG=angleproject:2034 Change-Id: I9369e76925a67632c444c662e5667c5ed7875547 Reviewed-on: https://chromium-review.googlesource.com/503252Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent a22aa4ed
......@@ -1536,16 +1536,15 @@ gl::Error Blit11::copyAndConvertImpl(const TextureHelper11 &source,
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
TextureHelper11 sourceStaging;
ANGLE_TRY_RESULT(mRenderer->createStagingTexture(GL_TEXTURE_2D, source.getFormatSet(),
ANGLE_TRY_RESULT(mRenderer->createStagingTexture(ResourceType::Texture2D, source.getFormatSet(),
sourceSize, StagingAccess::READ),
sourceStaging);
deviceContext->CopySubresourceRegion(sourceStaging.getResource(), 0, 0, 0, 0,
source.getResource(), sourceSubresource, nullptr);
deviceContext->CopySubresourceRegion(sourceStaging.get(), 0, 0, 0, 0, source.get(),
sourceSubresource, nullptr);
D3D11_MAPPED_SUBRESOURCE sourceMapping;
HRESULT result =
deviceContext->Map(sourceStaging.getResource(), 0, D3D11_MAP_READ, 0, &sourceMapping);
HRESULT result = deviceContext->Map(sourceStaging.get(), 0, D3D11_MAP_READ, 0, &sourceMapping);
if (FAILED(result))
{
return gl::OutOfMemory()
......@@ -1554,10 +1553,10 @@ gl::Error Blit11::copyAndConvertImpl(const TextureHelper11 &source,
}
D3D11_MAPPED_SUBRESOURCE destMapping;
result = deviceContext->Map(destStaging.getResource(), 0, D3D11_MAP_WRITE, 0, &destMapping);
result = deviceContext->Map(destStaging.get(), 0, D3D11_MAP_WRITE, 0, &destMapping);
if (FAILED(result))
{
deviceContext->Unmap(sourceStaging.getResource(), 0);
deviceContext->Unmap(sourceStaging.get(), 0);
return gl::OutOfMemory()
<< "Failed to map internal destination staging texture for depth stencil blit, "
<< result;
......@@ -1577,8 +1576,8 @@ gl::Error Blit11::copyAndConvertImpl(const TextureHelper11 &source,
destPixelStride, static_cast<const uint8_t *>(sourceMapping.pData),
static_cast<uint8_t *>(destMapping.pData));
deviceContext->Unmap(sourceStaging.getResource(), 0);
deviceContext->Unmap(destStaging.getResource(), 0);
deviceContext->Unmap(sourceStaging.get(), 0);
deviceContext->Unmap(destStaging.get(), 0);
return gl::NoError();
}
......@@ -1607,12 +1606,12 @@ gl::Error Blit11::copyAndConvert(const TextureHelper11 &source,
// ID3D11DevicContext::UpdateSubresource can be called
// using it's mapped data as a source
TextureHelper11 destStaging;
ANGLE_TRY_RESULT(mRenderer->createStagingTexture(GL_TEXTURE_2D, dest.getFormatSet(), destSize,
StagingAccess::READ_WRITE),
ANGLE_TRY_RESULT(mRenderer->createStagingTexture(ResourceType::Texture2D, dest.getFormatSet(),
destSize, StagingAccess::READ_WRITE),
destStaging);
deviceContext->CopySubresourceRegion(destStaging.getResource(), 0, 0, 0, 0, dest.getResource(),
destSubresource, nullptr);
deviceContext->CopySubresourceRegion(destStaging.get(), 0, 0, 0, 0, dest.get(), destSubresource,
nullptr);
copyAndConvertImpl(source, sourceSubresource, sourceArea, sourceSize, destStaging, destArea,
destSize, scissor, readOffset, writeOffset, copySize, srcPixelStride,
......@@ -1622,15 +1621,15 @@ gl::Error Blit11::copyAndConvert(const TextureHelper11 &source,
if (mRenderer->getWorkarounds().depthStencilBlitExtraCopy)
{
D3D11_MAPPED_SUBRESOURCE mapped;
deviceContext->Map(destStaging.getResource(), 0, D3D11_MAP_READ, 0, &mapped);
deviceContext->UpdateSubresource(dest.getResource(), destSubresource, nullptr, mapped.pData,
deviceContext->Map(destStaging.get(), 0, D3D11_MAP_READ, 0, &mapped);
deviceContext->UpdateSubresource(dest.get(), destSubresource, nullptr, mapped.pData,
mapped.RowPitch, mapped.DepthPitch);
deviceContext->Unmap(destStaging.getResource(), 0);
deviceContext->Unmap(destStaging.get(), 0);
}
else
{
deviceContext->CopySubresourceRegion(dest.getResource(), destSubresource, 0, 0, 0,
destStaging.getResource(), 0, nullptr);
deviceContext->CopySubresourceRegion(dest.get(), destSubresource, 0, 0, 0,
destStaging.get(), 0, nullptr);
}
return gl::NoError();
......@@ -2080,8 +2079,7 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveDepth(RenderTarget11 *depth)
// Trigger the blit on the GPU.
context->Draw(6, 0);
return TextureHelper11::MakeAndReference(mResolvedDepth.getResource(),
mResolvedDepth.getFormatSet());
return mResolvedDepth;
}
gl::Error Blit11::initResolveDepthOnly(const d3d11::Format &format, const gl::Extents &extents)
......@@ -2092,8 +2090,6 @@ gl::Error Blit11::initResolveDepthOnly(const d3d11::Format &format, const gl::Ex
return gl::NoError();
}
ID3D11Device *device = mRenderer->getDevice();
D3D11_TEXTURE2D_DESC textureDesc;
textureDesc.Width = extents.width;
textureDesc.Height = extents.height;
......@@ -2107,15 +2103,8 @@ gl::Error Blit11::initResolveDepthOnly(const d3d11::Format &format, const gl::Ex
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
ID3D11Texture2D *resolvedDepth = nullptr;
HRESULT hr = device->CreateTexture2D(&textureDesc, nullptr, &resolvedDepth);
if (FAILED(hr))
{
return gl::OutOfMemory() << "Failed to allocate resolved depth texture, " << hr;
}
d3d11::SetDebugName(resolvedDepth, "Blit11::mResolvedDepth");
mResolvedDepth = TextureHelper11::MakeAndPossess2D(resolvedDepth, format);
ANGLE_TRY(mRenderer->allocateTexture(textureDesc, format, &mResolvedDepth));
mResolvedDepth.setDebugName("Blit11::mResolvedDepth");
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
dsvDesc.Flags = 0;
......@@ -2123,8 +2112,7 @@ gl::Error Blit11::initResolveDepthOnly(const d3d11::Format &format, const gl::Ex
dsvDesc.Texture2D.MipSlice = 0;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
ANGLE_TRY(
mRenderer->allocateResource(dsvDesc, mResolvedDepth.getResource(), &mResolvedDepthDSView));
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, mResolvedDepth.get(), &mResolvedDepthDSView));
mResolvedDepthDSView.setDebugName("Blit11::mResolvedDepthDSView");
// Possibly D3D11 bug or undefined behaviour: Clear the DSV so that our first render
......@@ -2164,22 +2152,13 @@ gl::Error Blit11::initResolveDepthStencil(const gl::Extents &extents)
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
ID3D11Device *device = mRenderer->getDevice();
ANGLE_TRY(mRenderer->allocateTexture(textureDesc, formatSet, &mResolvedDepthStencil));
mResolvedDepthStencil.setDebugName("Blit11::mResolvedDepthStencil");
ID3D11Texture2D *resolvedDepthStencil = nullptr;
HRESULT hr = device->CreateTexture2D(&textureDesc, nullptr, &resolvedDepthStencil);
if (FAILED(hr))
{
return gl::OutOfMemory() << "Failed to allocate resolved depth stencil texture, " << hr;
}
d3d11::SetDebugName(resolvedDepthStencil, "Blit11::mResolvedDepthStencil");
ANGLE_TRY(
mRenderer->allocateResourceNoDesc(resolvedDepthStencil, &mResolvedDepthStencilRTView));
ANGLE_TRY(mRenderer->allocateResourceNoDesc(mResolvedDepthStencil.get(),
&mResolvedDepthStencilRTView));
mResolvedDepthStencilRTView.setDebugName("Blit11::mResolvedDepthStencilRTView");
mResolvedDepthStencil = TextureHelper11::MakeAndPossess2D(resolvedDepthStencil, formatSet);
return gl::NoError();
}
......@@ -2196,7 +2175,7 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveStencil(RenderTarget11 *depthS
ID3D11Device *device = mRenderer->getDevice();
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
ID3D11Resource *stencilResource = depthStencil->getTexture();
ID3D11Resource *stencilResource = depthStencil->getTexture().get();
// Check if we need to re-create the stencil SRV.
if (mStencilSRV.valid())
......@@ -2271,9 +2250,10 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveStencil(RenderTarget11 *depthS
gl::Box copyBox(0, 0, 0, extents.width, extents.height, 1);
TextureHelper11 dest;
ANGLE_TRY_RESULT(mRenderer->createStagingTexture(GL_TEXTURE_2D, depthStencil->getFormatSet(),
extents, StagingAccess::READ_WRITE),
dest);
ANGLE_TRY_RESULT(
mRenderer->createStagingTexture(ResourceType::Texture2D, depthStencil->getFormatSet(),
extents, StagingAccess::READ_WRITE),
dest);
const auto &copyFunction = GetCopyDepthStencilFunction(depthStencil->getInternalFormat());
const auto &dsFormatSet = depthStencil->getFormatSet();
......
......@@ -1451,17 +1451,14 @@ gl::Error Buffer11::PackStorage::packPixels(const gl::FramebufferAttachment &rea
RenderTarget11 *renderTarget = nullptr;
ANGLE_TRY(readAttachment.getRenderTarget(&renderTarget));
ID3D11Resource *renderTargetResource = renderTarget->getTexture();
ASSERT(renderTargetResource);
const TextureHelper11 &srcTexture = renderTarget->getTexture();
ASSERT(srcTexture.valid());
unsigned int srcSubresource = renderTarget->getSubresourceIndex();
TextureHelper11 srcTexture =
TextureHelper11::MakeAndReference(renderTargetResource, renderTarget->getFormatSet());
mQueuedPackCommand.reset(new PackPixelsParams(params));
gl::Extents srcTextureSize(params.area.width, params.area.height, 1);
if (!mStagingTexture.getResource() || mStagingTexture.getFormat() != srcTexture.getFormat() ||
if (!mStagingTexture.get() || mStagingTexture.getFormat() != srcTexture.getFormat() ||
mStagingTexture.getExtents() != srcTextureSize)
{
ANGLE_TRY_RESULT(
......@@ -1482,15 +1479,15 @@ gl::Error Buffer11::PackStorage::packPixels(const gl::FramebufferAttachment &rea
// Select the correct layer from a 3D attachment
srcBox.front = 0;
if (mStagingTexture.getTextureType() == GL_TEXTURE_3D)
if (mStagingTexture.is3D())
{
srcBox.front = static_cast<UINT>(readAttachment.layer());
}
srcBox.back = srcBox.front + 1;
// Asynchronous copy
immediateContext->CopySubresourceRegion(mStagingTexture.getResource(), 0, 0, 0, 0,
srcTexture.getResource(), srcSubresource, &srcBox);
immediateContext->CopySubresourceRegion(mStagingTexture.get(), 0, 0, 0, 0, srcTexture.get(),
srcSubresource, &srcBox);
return gl::NoError();
}
......
......@@ -10,10 +10,10 @@
#ifndef LIBANGLE_RENDERER_D3D_D3D11_IMAGE11_H_
#define LIBANGLE_RENDERER_D3D_D3D11_IMAGE11_H_
#include "libANGLE/renderer/d3d/ImageD3D.h"
#include "libANGLE/ImageIndex.h"
#include "common/debug.h"
#include "libANGLE/ImageIndex.h"
#include "libANGLE/renderer/d3d/ImageD3D.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
namespace gl
{
......@@ -71,14 +71,15 @@ class Image11 : public ImageD3D
const TextureHelper11 &textureHelper,
UINT sourceSubResource);
gl::Error getStagingTexture(ID3D11Resource **outStagingTexture, unsigned int *outSubresourceIndex);
gl::Error getStagingTexture(const TextureHelper11 **outStagingTexture,
unsigned int *outSubresourceIndex);
gl::Error createStagingTexture();
void releaseStagingTexture();
Renderer11 *mRenderer;
DXGI_FORMAT mDXGIFormat;
ID3D11Resource *mStagingTexture;
TextureHelper11 mStagingTexture;
unsigned int mStagingSubresource;
bool mRecoverFromStorage;
......
......@@ -212,7 +212,7 @@ void RenderTarget11::signalDirty()
}
TextureRenderTarget11::TextureRenderTarget11(d3d11::RenderTargetView &&rtv,
ID3D11Resource *resource,
const TextureHelper11 &resource,
const d3d11::SharedSRV &srv,
const d3d11::SharedSRV &blitSRV,
GLenum internalFormat,
......@@ -234,20 +234,15 @@ TextureRenderTarget11::TextureRenderTarget11(d3d11::RenderTargetView &&rtv,
mShaderResource(srv),
mBlitShaderResource(blitSRV)
{
if (mTexture)
if (mRenderTarget.valid() && mTexture.valid())
{
mTexture->AddRef();
}
if (mRenderTarget.valid() && mTexture)
{
mSubresourceIndex = GetRTVSubresourceIndex(mTexture, mRenderTarget.get());
mSubresourceIndex = GetRTVSubresourceIndex(mTexture.get(), mRenderTarget.get());
}
ASSERT(mFormatSet.formatID != angle::Format::ID::NONE || mWidth == 0 || mHeight == 0);
}
TextureRenderTarget11::TextureRenderTarget11(d3d11::DepthStencilView &&dsv,
ID3D11Resource *resource,
const TextureHelper11 &resource,
const d3d11::SharedSRV &srv,
GLenum internalFormat,
const d3d11::Format &formatSet,
......@@ -268,24 +263,18 @@ TextureRenderTarget11::TextureRenderTarget11(d3d11::DepthStencilView &&dsv,
mShaderResource(srv),
mBlitShaderResource()
{
if (mTexture)
{
mTexture->AddRef();
}
if (mDepthStencil.valid() && mTexture)
if (mDepthStencil.valid() && mTexture.valid())
{
mSubresourceIndex = GetDSVSubresourceIndex(mTexture, mDepthStencil.get());
mSubresourceIndex = GetDSVSubresourceIndex(mTexture.get(), mDepthStencil.get());
}
ASSERT(mFormatSet.formatID != angle::Format::ID::NONE || mWidth == 0 || mHeight == 0);
}
TextureRenderTarget11::~TextureRenderTarget11()
{
SafeRelease(mTexture);
}
ID3D11Resource *TextureRenderTarget11::getTexture() const
const TextureHelper11 &TextureRenderTarget11::getTexture() const
{
return mTexture;
}
......@@ -379,7 +368,7 @@ GLsizei SurfaceRenderTarget11::getSamples() const
return mSwapChain->getSamples();
}
ID3D11Resource *SurfaceRenderTarget11::getTexture() const
const TextureHelper11 &SurfaceRenderTarget11::getTexture() const
{
return (mDepth ? mSwapChain->getDepthStencilTexture() : mSwapChain->getOffscreenTexture());
}
......
......@@ -26,7 +26,7 @@ class RenderTarget11 : public RenderTargetD3D
RenderTarget11(const d3d11::Format &formatSet);
virtual ~RenderTarget11();
virtual ID3D11Resource *getTexture() const = 0;
virtual const TextureHelper11 &getTexture() const = 0;
virtual const d3d11::RenderTargetView &getRenderTargetView() const = 0;
virtual const d3d11::DepthStencilView &getDepthStencilView() const = 0;
virtual const d3d11::SharedSRV &getShaderResourceView() const = 0;
......@@ -49,7 +49,7 @@ class TextureRenderTarget11 : public RenderTarget11
public:
// TextureRenderTarget11 takes ownership of any D3D11 resources it is given and will AddRef them
TextureRenderTarget11(d3d11::RenderTargetView &&rtv,
ID3D11Resource *resource,
const TextureHelper11 &resource,
const d3d11::SharedSRV &srv,
const d3d11::SharedSRV &blitSRV,
GLenum internalFormat,
......@@ -59,7 +59,7 @@ class TextureRenderTarget11 : public RenderTarget11
GLsizei depth,
GLsizei samples);
TextureRenderTarget11(d3d11::DepthStencilView &&dsv,
ID3D11Resource *resource,
const TextureHelper11 &resource,
const d3d11::SharedSRV &srv,
GLenum internalFormat,
const d3d11::Format &formatSet,
......@@ -75,7 +75,7 @@ class TextureRenderTarget11 : public RenderTarget11
GLenum getInternalFormat() const override;
GLsizei getSamples() const override;
ID3D11Resource *getTexture() const override;
const TextureHelper11 &getTexture() const override;
const d3d11::RenderTargetView &getRenderTargetView() const override;
const d3d11::DepthStencilView &getDepthStencilView() const override;
const d3d11::SharedSRV &getShaderResourceView() const override;
......@@ -91,7 +91,7 @@ class TextureRenderTarget11 : public RenderTarget11
GLsizei mSamples;
unsigned int mSubresourceIndex;
ID3D11Resource *mTexture;
TextureHelper11 mTexture;
d3d11::RenderTargetView mRenderTarget;
d3d11::DepthStencilView mDepthStencil;
d3d11::SharedSRV mShaderResource;
......@@ -113,7 +113,7 @@ class SurfaceRenderTarget11 : public RenderTarget11
GLenum getInternalFormat() const override;
GLsizei getSamples() const override;
ID3D11Resource *getTexture() const override;
const TextureHelper11 &getTexture() const override;
const d3d11::RenderTargetView &getRenderTargetView() const override;
const d3d11::DepthStencilView &getDepthStencilView() const override;
const d3d11::SharedSRV &getShaderResourceView() const override;
......
......@@ -404,11 +404,17 @@ class Renderer11 : public RendererD3D
const std::vector<D3DUniform *> &uniformArray) override;
gl::Error applyComputeShader(const gl::ContextState &data);
gl::ErrorOrResult<TextureHelper11> createStagingTexture(GLenum textureType,
gl::ErrorOrResult<TextureHelper11> createStagingTexture(ResourceType textureType,
const d3d11::Format &formatSet,
const gl::Extents &size,
StagingAccess readAndWriteAccess);
template <typename DescT, typename ResourceT>
gl::Error allocateResource(const DescT &desc, ResourceT *resourceOut)
{
return mResourceManager11.allocate(this, &desc, nullptr, resourceOut);
}
template <typename DescT, typename InitDataT, typename ResourceT>
gl::Error allocateResource(const DescT &desc, InitDataT *initData, ResourceT *resourceOut)
{
......@@ -421,6 +427,24 @@ class Renderer11 : public RendererD3D
return mResourceManager11.allocate(this, nullptr, initData, resourceOut);
}
template <typename DescT>
gl::Error allocateTexture(const DescT &desc,
const d3d11::Format &format,
TextureHelper11 *textureOut)
{
return allocateTexture(desc, format, nullptr, textureOut);
}
gl::Error allocateTexture(const D3D11_TEXTURE2D_DESC &desc,
const d3d11::Format &format,
const D3D11_SUBRESOURCE_DATA *initData,
TextureHelper11 *textureOut);
gl::Error allocateTexture(const D3D11_TEXTURE3D_DESC &desc,
const d3d11::Format &format,
const D3D11_SUBRESOURCE_DATA *initData,
TextureHelper11 *textureOut);
protected:
gl::Error clearTextures(gl::SamplerType samplerType,
size_t rangeStart,
......
......@@ -17,12 +17,69 @@ namespace rx
namespace
{
size_t ComputeMippedMemoryUsage(unsigned int width,
unsigned int height,
unsigned int depth,
size_t pixelSize,
unsigned int mipLevels)
{
size_t sizeSum = 0;
for (unsigned int level = 0; level < mipLevels; ++level)
{
unsigned int mipWidth = std::max(width >> level, 1u);
unsigned int mipHeight = std::max(height >> level, 1u);
unsigned int mipDepth = std::max(depth >> level, 1u);
sizeSum += static_cast<size_t>(mipWidth * mipHeight * mipDepth) * pixelSize;
}
return sizeSum;
}
size_t ComputeMemoryUsage(const D3D11_TEXTURE2D_DESC *desc)
{
ASSERT(desc);
size_t pixelBytes = static_cast<size_t>(d3d11::GetDXGIFormatSizeInfo(desc->Format).pixelBytes);
return ComputeMippedMemoryUsage(desc->Width, desc->Height, 1, pixelBytes, desc->MipLevels);
}
size_t ComputeMemoryUsage(const D3D11_TEXTURE3D_DESC *desc)
{
ASSERT(desc);
size_t pixelBytes = static_cast<size_t>(d3d11::GetDXGIFormatSizeInfo(desc->Format).pixelBytes);
return ComputeMippedMemoryUsage(desc->Width, desc->Height, desc->Depth, pixelBytes,
desc->MipLevels);
}
template <typename T>
size_t ComputeMemoryUsage(const T *desc)
{
return 0;
}
template <ResourceType ResourceT>
size_t ComputeGenericMemoryUsage(ID3D11DeviceChild *genericResource)
{
auto *typedResource = static_cast<GetD3D11Type<ResourceT> *>(genericResource);
GetDescType<ResourceT> desc;
typedResource->GetDesc(&desc);
return ComputeMemoryUsage(&desc);
}
size_t ComputeGenericMemoryUsage(ResourceType resourceType, ID3D11DeviceChild *resource)
{
switch (resourceType)
{
case ResourceType::Texture2D:
return ComputeGenericMemoryUsage<ResourceType::Texture2D>(resource);
case ResourceType::Texture3D:
return ComputeGenericMemoryUsage<ResourceType::Texture3D>(resource);
default:
return 0;
}
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_DEPTH_STENCIL_VIEW_DESC *desc,
ID3D11Resource *resource,
......@@ -47,6 +104,22 @@ HRESULT CreateResource(ID3D11Device *device,
return device->CreateShaderResourceView(resource, desc, resourceOut);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_TEXTURE2D_DESC *desc,
const D3D11_SUBRESOURCE_DATA *initData,
ID3D11Texture2D **texture)
{
return device->CreateTexture2D(desc, initData, texture);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_TEXTURE3D_DESC *desc,
const D3D11_SUBRESOURCE_DATA *initData,
ID3D11Texture3D **texture)
{
return device->CreateTexture3D(desc, initData, texture);
}
#define ANGLE_RESOURCE_STRINGIFY_OP(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) #RESTYPE
constexpr std::array<const char *, NumResourceTypes> kResourceTypeNames = {
......@@ -115,6 +188,19 @@ void ResourceManager11::decrResource(ResourceType resourceType, size_t memorySiz
mAllocatedResourceDeviceMemory[ResourceTypeIndex(resourceType)] -= memorySize;
}
void ResourceManager11::onReleaseResource(ResourceType resourceType, ID3D11Resource *resource)
{
ASSERT(resource);
decrResource(resourceType, ComputeGenericMemoryUsage(resourceType, resource));
}
template <>
void ResourceManager11::onRelease(ID3D11Resource *resource)
{
// For untyped ID3D11Resource, they must call onReleaseResource.
UNREACHABLE();
}
template <typename T>
void ResourceManager11::onRelease(T *resource)
{
......
......@@ -22,15 +22,18 @@ class Renderer11;
class ResourceManager11;
template <typename T>
class SharedResource11;
class TextureHelper11;
// Format: ResourceType, D3D11 type, DESC type, init data type.
#define ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
OP(NAME, DepthStencilView, ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \
ID3D11Resource)
#define ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
OP(NAME, DepthStencilView, ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, Texture2D, ID3D11Texture2D, D3D11_TEXTURE2D_DESC, const D3D11_SUBRESOURCE_DATA) \
OP(NAME, Texture3D, ID3D11Texture3D, D3D11_TEXTURE3D_DESC, const D3D11_SUBRESOURCE_DATA)
#define ANGLE_RESOURCE_TYPE_LIST(NAME, RESTYPE, D3D11TYPE, DESCTYPE, INITDATATYPE) RESTYPE,
......@@ -157,6 +160,8 @@ class Resource11Base : angle::NonCopyable
void reset() { mData.reset(new DataT()); }
protected:
friend class TextureHelper11;
Resource11Base() : mData(new DataT()) {}
Resource11Base(Resource11Base &&movedObj) : mData(new DataT())
......@@ -268,6 +273,7 @@ class ResourceManager11 final : angle::NonCopyable
template <typename T>
void onRelease(T *resource);
void onReleaseResource(ResourceType resourceType, ID3D11Resource *resource);
private:
void incrResource(ResourceType resourceType, size_t memorySize);
......
......@@ -1061,7 +1061,7 @@ gl::Error StateManager11::syncFramebuffer(ContextImpl *contextImpl, gl::Framebuf
// Unset conflicting texture SRVs
const auto *attachment = framebuffer->getColorbuffer(rtIndex);
ASSERT(attachment);
unsetConflictingAttachmentResources(attachment, renderTarget->getTexture());
unsetConflictingAttachmentResources(attachment, renderTarget->getTexture().get());
appliedRTIndex++;
}
......@@ -1085,7 +1085,8 @@ gl::Error StateManager11::syncFramebuffer(ContextImpl *contextImpl, gl::Framebuf
// Unset conflicting texture SRVs
const auto *attachment = framebuffer->getDepthOrStencilbuffer();
ASSERT(attachment);
unsetConflictingAttachmentResources(attachment, depthStencilRenderTarget->getTexture());
unsetConflictingAttachmentResources(attachment,
depthStencilRenderTarget->getTexture().get());
}
// TODO(jmadill): Use context caps?
......
......@@ -39,11 +39,11 @@ class SwapChain11 final : public SwapChainD3D
RenderTargetD3D *getColorRenderTarget() override { return &mColorRenderTarget; }
RenderTargetD3D *getDepthStencilRenderTarget() override { return &mDepthStencilRenderTarget; }
ID3D11Texture2D *getOffscreenTexture();
const TextureHelper11 &getOffscreenTexture();
const d3d11::RenderTargetView &getRenderTarget();
const d3d11::SharedSRV &getRenderTargetShaderResource();
ID3D11Texture2D *getDepthStencilTexture();
const TextureHelper11 &getDepthStencilTexture();
const d3d11::DepthStencilView &getDepthStencil();
const d3d11::SharedSRV &getDepthStencilShaderResource();
......@@ -85,16 +85,16 @@ class SwapChain11 final : public SwapChainD3D
IDXGISwapChain1 *mSwapChain1;
IDXGIKeyedMutex *mKeyedMutex;
ID3D11Texture2D *mBackBufferTexture;
TextureHelper11 mBackBufferTexture;
d3d11::RenderTargetView mBackBufferRTView;
d3d11::SharedSRV mBackBufferSRView;
const bool mNeedsOffscreenTexture;
ID3D11Texture2D *mOffscreenTexture;
TextureHelper11 mOffscreenTexture;
d3d11::RenderTargetView mOffscreenRTView;
d3d11::SharedSRV mOffscreenSRView;
ID3D11Texture2D *mDepthStencilTexture;
TextureHelper11 mDepthStencilTexture;
d3d11::DepthStencilView mDepthStencilDSView;
d3d11::SharedSRV mDepthStencilSRView;
......
......@@ -2047,136 +2047,80 @@ void InitConstantBufferDesc(D3D11_BUFFER_DESC *constantBufferDescription, size_t
} // namespace d3d11
TextureHelper11::TextureHelper11()
: mTextureType(GL_NONE),
mFormat(DXGI_FORMAT_UNKNOWN),
mFormatSet(nullptr),
mSampleCount(0),
mTexture2D(nullptr),
mTexture3D(nullptr)
// TextureHelper11 implementation.
TextureHelper11::TextureHelper11() : mFormatSet(nullptr), mSampleCount(0)
{
}
TextureHelper11::TextureHelper11(TextureHelper11 &&toCopy)
: mTextureType(toCopy.mTextureType),
mExtents(toCopy.mExtents),
mFormat(toCopy.mFormat),
mFormatSet(toCopy.mFormatSet),
mSampleCount(toCopy.mSampleCount),
mTexture2D(toCopy.mTexture2D),
mTexture3D(toCopy.mTexture3D)
TextureHelper11::TextureHelper11(TextureHelper11 &&toCopy) : TextureHelper11()
{
toCopy.reset();
*this = std::move(toCopy);
}
// static
TextureHelper11 TextureHelper11::MakeAndReference(ID3D11Resource *genericResource,
const d3d11::Format &formatSet)
TextureHelper11::TextureHelper11(const TextureHelper11 &other)
: mFormatSet(other.mFormatSet), mExtents(other.mExtents), mSampleCount(other.mSampleCount)
{
TextureHelper11 newHelper;
newHelper.mFormatSet = &formatSet;
newHelper.mTexture2D = d3d11::DynamicCastComObject<ID3D11Texture2D>(genericResource);
newHelper.mTexture3D = d3d11::DynamicCastComObject<ID3D11Texture3D>(genericResource);
newHelper.mTextureType = newHelper.mTexture2D ? GL_TEXTURE_2D : GL_TEXTURE_3D;
newHelper.initDesc();
return newHelper;
mData = other.mData;
}
// static
TextureHelper11 TextureHelper11::MakeAndPossess2D(ID3D11Texture2D *texToOwn,
const d3d11::Format &formatSet)
TextureHelper11::~TextureHelper11()
{
TextureHelper11 newHelper;
newHelper.mFormatSet = &formatSet;
newHelper.mTexture2D = texToOwn;
newHelper.mTextureType = GL_TEXTURE_2D;
newHelper.initDesc();
return newHelper;
}
// static
TextureHelper11 TextureHelper11::MakeAndPossess3D(ID3D11Texture3D *texToOwn,
const d3d11::Format &formatSet)
void TextureHelper11::getDesc(D3D11_TEXTURE2D_DESC *desc) const
{
TextureHelper11 newHelper;
newHelper.mFormatSet = &formatSet;
newHelper.mTexture3D = texToOwn;
newHelper.mTextureType = GL_TEXTURE_3D;
newHelper.initDesc();
return newHelper;
static_cast<ID3D11Texture2D *>(mData->object)->GetDesc(desc);
}
void TextureHelper11::initDesc()
void TextureHelper11::getDesc(D3D11_TEXTURE3D_DESC *desc) const
{
if (mTextureType == GL_TEXTURE_2D)
{
ASSERT(!mTexture3D);
D3D11_TEXTURE2D_DESC desc2D;
mTexture2D->GetDesc(&desc2D);
mExtents.width = static_cast<int>(desc2D.Width);
mExtents.height = static_cast<int>(desc2D.Height);
mExtents.depth = 1;
mFormat = desc2D.Format;
mSampleCount = desc2D.SampleDesc.Count;
}
else
{
ASSERT(mTexture3D && mTextureType == GL_TEXTURE_3D);
D3D11_TEXTURE3D_DESC desc3D;
mTexture3D->GetDesc(&desc3D);
mExtents.width = static_cast<int>(desc3D.Width);
mExtents.height = static_cast<int>(desc3D.Height);
mExtents.depth = static_cast<int>(desc3D.Depth);
mFormat = desc3D.Format;
mSampleCount = 1;
}
ASSERT(mFormatSet && mFormat == mFormatSet->texFormat);
static_cast<ID3D11Texture3D *>(mData->object)->GetDesc(desc);
}
TextureHelper11::~TextureHelper11()
void TextureHelper11::initDesc(const D3D11_TEXTURE2D_DESC &desc2D)
{
SafeRelease(mTexture2D);
SafeRelease(mTexture3D);
mData->resourceType = ResourceType::Texture2D;
mExtents.width = static_cast<int>(desc2D.Width);
mExtents.height = static_cast<int>(desc2D.Height);
mExtents.depth = 1;
mSampleCount = desc2D.SampleDesc.Count;
}
ID3D11Resource *TextureHelper11::getResource() const
void TextureHelper11::initDesc(const D3D11_TEXTURE3D_DESC &desc3D)
{
return mTexture2D ? static_cast<ID3D11Resource *>(mTexture2D)
: static_cast<ID3D11Resource *>(mTexture3D);
mData->resourceType = ResourceType::Texture3D;
mExtents.width = static_cast<int>(desc3D.Width);
mExtents.height = static_cast<int>(desc3D.Height);
mExtents.depth = static_cast<int>(desc3D.Depth);
mSampleCount = 1;
}
TextureHelper11 &TextureHelper11::operator=(TextureHelper11 &&texture)
TextureHelper11 &TextureHelper11::operator=(TextureHelper11 &&other)
{
SafeRelease(mTexture2D);
SafeRelease(mTexture3D);
std::swap(mData, other.mData);
std::swap(mExtents, other.mExtents);
std::swap(mFormatSet, other.mFormatSet);
std::swap(mSampleCount, other.mSampleCount);
return *this;
}
mTextureType = texture.mTextureType;
mExtents = texture.mExtents;
mFormat = texture.mFormat;
mFormatSet = texture.mFormatSet;
mSampleCount = texture.mSampleCount;
mTexture2D = texture.mTexture2D;
mTexture3D = texture.mTexture3D;
texture.reset();
TextureHelper11 &TextureHelper11::operator=(const TextureHelper11 &other)
{
mData = other.mData;
mExtents = other.mExtents;
mFormatSet = other.mFormatSet;
mSampleCount = other.mSampleCount;
return *this;
}
void TextureHelper11::reset()
bool TextureHelper11::operator==(const TextureHelper11 &other) const
{
mTextureType = GL_NONE;
mExtents = gl::Extents();
mFormat = DXGI_FORMAT_UNKNOWN;
mFormatSet = nullptr;
mSampleCount = 0;
mTexture2D = nullptr;
mTexture3D = nullptr;
return mData->object == other.mData->object;
}
bool TextureHelper11::valid() const
bool TextureHelper11::operator!=(const TextureHelper11 &other) const
{
return (mTextureType != GL_NONE);
return mData->object != other.mData->object;
}
bool UsePresentPathFast(const Renderer11 *renderer,
......
......@@ -18,8 +18,9 @@
#include "libANGLE/Caps.h"
#include "libANGLE/Error.h"
#include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
#include "libANGLE/renderer/d3d/RendererD3D.h"
#include "libANGLE/renderer/d3d/d3d11/ResourceManager11.h"
#include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
namespace gl
{
......@@ -360,43 +361,88 @@ enum ReservedConstantBufferSlot
void InitConstantBufferDesc(D3D11_BUFFER_DESC *constantBufferDescription, size_t byteWidth);
} // namespace d3d11
struct GenericData
{
GenericData() {}
~GenericData()
{
if (object)
{
// We can have a nullptr factory when holding passed-in resources.
if (manager)
{
manager->onReleaseResource(resourceType, object);
manager = nullptr;
}
object->Release();
object = nullptr;
}
}
ResourceType resourceType = ResourceType::Last;
ID3D11Resource *object = nullptr;
ResourceManager11 *manager = nullptr;
};
// A helper class which wraps a 2D or 3D texture.
class TextureHelper11 : angle::NonCopyable
class TextureHelper11 : public Resource11Base<ID3D11Resource, std::shared_ptr, GenericData>
{
public:
TextureHelper11();
TextureHelper11(TextureHelper11 &&toCopy);
TextureHelper11(TextureHelper11 &&other);
TextureHelper11(const TextureHelper11 &other);
~TextureHelper11();
TextureHelper11 &operator=(TextureHelper11 &&texture);
static TextureHelper11 MakeAndReference(ID3D11Resource *genericResource,
const d3d11::Format &formatSet);
static TextureHelper11 MakeAndPossess2D(ID3D11Texture2D *texToOwn,
const d3d11::Format &formatSet);
static TextureHelper11 MakeAndPossess3D(ID3D11Texture3D *texToOwn,
const d3d11::Format &formatSet);
TextureHelper11 &operator=(TextureHelper11 &&other);
TextureHelper11 &operator=(const TextureHelper11 &other);
GLenum getTextureType() const { return mTextureType; }
bool is2D() const { return mData->resourceType == ResourceType::Texture2D; }
bool is3D() const { return mData->resourceType == ResourceType::Texture3D; }
ResourceType getTextureType() const { return mData->resourceType; }
gl::Extents getExtents() const { return mExtents; }
DXGI_FORMAT getFormat() const { return mFormat; }
DXGI_FORMAT getFormat() const { return mFormatSet->texFormat; }
const d3d11::Format &getFormatSet() const { return *mFormatSet; }
int getSampleCount() const { return mSampleCount; }
ID3D11Texture2D *getTexture2D() const { return mTexture2D; }
ID3D11Texture3D *getTexture3D() const { return mTexture3D; }
ID3D11Resource *getResource() const;
bool valid() const;
template <typename DescT, typename ResourceT>
void init(Resource11<ResourceT> &&texture, const DescT &desc, const d3d11::Format &format)
{
std::swap(mData->manager, texture.mData->manager);
// Can't use std::swap because texture is typed, and here we use ID3D11Resource.
auto temp = mData->object;
mData->object = texture.mData->object;
texture.mData->object = static_cast<ResourceT *>(temp);
mFormatSet = &format;
initDesc(desc);
}
template <typename ResourceT>
void set(ResourceT *object, const d3d11::Format &format)
{
ASSERT(!valid());
mFormatSet = &format;
mData->object = object;
mData->manager = nullptr;
GetDescFromD3D11<ResourceT> desc;
getDesc(&desc);
initDesc(desc);
}
bool operator==(const TextureHelper11 &other) const;
bool operator!=(const TextureHelper11 &other) const;
void getDesc(D3D11_TEXTURE2D_DESC *desc) const;
void getDesc(D3D11_TEXTURE3D_DESC *desc) const;
private:
void reset();
void initDesc();
void initDesc(const D3D11_TEXTURE2D_DESC &desc2D);
void initDesc(const D3D11_TEXTURE3D_DESC &desc3D);
GLenum mTextureType;
gl::Extents mExtents;
DXGI_FORMAT mFormat;
const d3d11::Format *mFormatSet;
gl::Extents mExtents;
int mSampleCount;
ID3D11Texture2D *mTexture2D;
ID3D11Texture3D *mTexture3D;
};
enum class StagingAccess
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment