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();
}
......
......@@ -26,7 +26,7 @@ namespace rx
Image11::Image11(Renderer11 *renderer)
: mRenderer(renderer),
mDXGIFormat(DXGI_FORMAT_UNKNOWN),
mStagingTexture(nullptr),
mStagingTexture(),
mStagingSubresource(0),
mRecoverFromStorage(false),
mAssociatedStorage(nullptr),
......@@ -82,7 +82,7 @@ bool Image11::isDirty() const
// If mDirty is true AND mStagingTexture doesn't exist AND mStagingTexture doesn't need to be
// recovered from TextureStorage AND the texture doesn't require init data (i.e. a blank new
// texture will suffice) then isDirty should still return false.
if (mDirty && !mStagingTexture && !mRecoverFromStorage)
if (mDirty && !mStagingTexture.valid() && !mRecoverFromStorage)
{
const Renderer11DeviceCaps &deviceCaps = mRenderer->getRenderer11DeviceCaps();
const auto &formatInfo = d3d11::Format::Get(mInternalFormat, deviceCaps);
......@@ -114,11 +114,11 @@ gl::Error Image11::copyToStorage(TextureStorage *storage,
ANGLE_TRY(storage11->releaseAssociatedImage(index, this));
}
ID3D11Resource *stagingTexture = nullptr;
const TextureHelper11 *stagingTexture = nullptr;
unsigned int stagingSubresourceIndex = 0;
ANGLE_TRY(getStagingTexture(&stagingTexture, &stagingSubresourceIndex));
ANGLE_TRY(
storage11->updateSubresourceLevel(stagingTexture, stagingSubresourceIndex, index, region));
storage11->updateSubresourceLevel(*stagingTexture, stagingSubresourceIndex, index, region));
// Once the image data has been copied into the Storage, we can release it locally.
if (attemptToReleaseStagingTexture)
......@@ -301,15 +301,13 @@ gl::Error Image11::copyFromTexStorage(const gl::ImageIndex &imageIndex, TextureS
{
TextureStorage11 *storage11 = GetAs<TextureStorage11>(source);
ID3D11Resource *resource = nullptr;
ANGLE_TRY(storage11->getResource(&resource));
const TextureHelper11 *textureHelper = nullptr;
ANGLE_TRY(storage11->getResource(&textureHelper));
UINT subresourceIndex = storage11->getSubresourceIndex(imageIndex);
TextureHelper11 textureHelper =
TextureHelper11::MakeAndReference(resource, storage11->getFormatSet());
gl::Box sourceBox(0, 0, 0, mWidth, mHeight, mDepth);
return copyWithoutConversion(gl::Offset(), sourceBox, textureHelper, subresourceIndex);
return copyWithoutConversion(gl::Offset(), sourceBox, *textureHelper, subresourceIndex);
}
gl::Error Image11::copyFromFramebuffer(const gl::Offset &destOffset,
......@@ -325,14 +323,11 @@ gl::Error Image11::copyFromFramebuffer(const gl::Offset &destOffset,
if (d3d11Format.texFormat == mDXGIFormat && sourceInternalFormat == mInternalFormat)
{
RenderTargetD3D *renderTarget = nullptr;
ANGLE_TRY(srcAttachment->getRenderTarget(&renderTarget));
RenderTarget11 *rt11 = nullptr;
ANGLE_TRY(srcAttachment->getRenderTarget(&rt11));
ASSERT(rt11->getTexture().get());
RenderTarget11 *rt11 = GetAs<RenderTarget11>(renderTarget);
ASSERT(rt11->getTexture());
TextureHelper11 textureHelper =
TextureHelper11::MakeAndReference(rt11->getTexture(), rt11->getFormatSet());
TextureHelper11 textureHelper = rt11->getTexture();
unsigned int sourceSubResource = rt11->getSubresourceIndex();
gl::Box sourceBox(sourceArea.x, sourceArea.y, 0, sourceArea.width, sourceArea.height, 1);
......@@ -392,22 +387,23 @@ gl::Error Image11::copyWithoutConversion(const gl::Offset &destOffset,
UINT sourceSubResource)
{
// No conversion needed-- use copyback fastpath
ID3D11Resource *stagingTexture = nullptr;
const TextureHelper11 *stagingTexture = nullptr;
unsigned int stagingSubresourceIndex = 0;
ANGLE_TRY(getStagingTexture(&stagingTexture, &stagingSubresourceIndex));
ID3D11Device *device = mRenderer->getDevice();
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
UINT subresourceAfterResolve = sourceSubResource;
ID3D11Resource *srcTex = nullptr;
const gl::Extents &extents = textureHelper.getExtents();
bool needResolve =
(textureHelper.getTextureType() == GL_TEXTURE_2D && textureHelper.getSampleCount() > 1);
D3D11_BOX srcBox;
srcBox.left = sourceArea.x;
srcBox.right = sourceArea.x + sourceArea.width;
srcBox.top = sourceArea.y;
srcBox.bottom = sourceArea.y + sourceArea.height;
srcBox.front = sourceArea.z;
srcBox.back = sourceArea.z + sourceArea.depth;
if (needResolve)
if (textureHelper.is2D() && textureHelper.getSampleCount() > 1)
{
D3D11_TEXTURE2D_DESC resolveDesc;
resolveDesc.Width = extents.width;
......@@ -422,64 +418,45 @@ gl::Error Image11::copyWithoutConversion(const gl::Offset &destOffset,
resolveDesc.CPUAccessFlags = 0;
resolveDesc.MiscFlags = 0;
ID3D11Texture2D *srcTex2D = nullptr;
HRESULT result = device->CreateTexture2D(&resolveDesc, nullptr, &srcTex2D);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.",
result);
}
srcTex = srcTex2D;
d3d11::Texture2D resolveTex;
ANGLE_TRY(mRenderer->allocateResource(resolveDesc, &resolveTex));
deviceContext->ResolveSubresource(srcTex, 0, textureHelper.getTexture2D(),
deviceContext->ResolveSubresource(resolveTex.get(), 0, textureHelper.get(),
sourceSubResource, textureHelper.getFormat());
subresourceAfterResolve = 0;
deviceContext->CopySubresourceRegion(stagingTexture->get(), stagingSubresourceIndex,
destOffset.x, destOffset.y, destOffset.z,
resolveTex.get(), 0, &srcBox);
}
else
{
srcTex = textureHelper.getResource();
}
D3D11_BOX srcBox;
srcBox.left = sourceArea.x;
srcBox.right = sourceArea.x + sourceArea.width;
srcBox.top = sourceArea.y;
srcBox.bottom = sourceArea.y + sourceArea.height;
srcBox.front = sourceArea.z;
srcBox.back = sourceArea.z + sourceArea.depth;
deviceContext->CopySubresourceRegion(stagingTexture, stagingSubresourceIndex, destOffset.x,
destOffset.y, destOffset.z, srcTex,
subresourceAfterResolve, &srcBox);
if (needResolve)
{
SafeRelease(srcTex);
deviceContext->CopySubresourceRegion(stagingTexture->get(), stagingSubresourceIndex,
destOffset.x, destOffset.y, destOffset.z,
textureHelper.get(), sourceSubResource, &srcBox);
}
mDirty = true;
return gl::NoError();
}
gl::Error Image11::getStagingTexture(ID3D11Resource **outStagingTexture,
gl::Error Image11::getStagingTexture(const TextureHelper11 **outStagingTexture,
unsigned int *outSubresourceIndex)
{
ANGLE_TRY(createStagingTexture());
*outStagingTexture = mStagingTexture;
*outStagingTexture = &mStagingTexture;
*outSubresourceIndex = mStagingSubresource;
return gl::NoError();
}
void Image11::releaseStagingTexture()
{
SafeRelease(mStagingTexture);
mStagingTexture.reset();
}
gl::Error Image11::createStagingTexture()
{
if (mStagingTexture)
if (mStagingTexture.valid())
{
return gl::NoError();
}
......@@ -487,9 +464,8 @@ gl::Error Image11::createStagingTexture()
ASSERT(mWidth > 0 && mHeight > 0 && mDepth > 0);
const DXGI_FORMAT dxgiFormat = getDXGIFormat();
ID3D11Device *device = mRenderer->getDevice();
HRESULT result;
const auto &formatInfo =
d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps());
int lodOffset = 1;
GLsizei width = mWidth;
......@@ -500,8 +476,6 @@ gl::Error Image11::createStagingTexture()
if (mTarget == GL_TEXTURE_3D)
{
ID3D11Texture3D *newTexture = nullptr;
D3D11_TEXTURE3D_DESC desc;
desc.Width = width;
desc.Height = height;
......@@ -513,8 +487,7 @@ gl::Error Image11::createStagingTexture()
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;
if (d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps())
.dataInitializerFunction != nullptr)
if (formatInfo.dataInitializerFunction != nullptr)
{
std::vector<D3D11_SUBRESOURCE_DATA> initialData;
std::vector<std::vector<BYTE>> textureData;
......@@ -522,28 +495,19 @@ gl::Error Image11::createStagingTexture()
width, height, mDepth, lodOffset + 1, &initialData,
&textureData);
result = device->CreateTexture3D(&desc, initialData.data(), &newTexture);
ANGLE_TRY(
mRenderer->allocateTexture(desc, formatInfo, initialData.data(), &mStagingTexture));
}
else
{
result = device->CreateTexture3D(&desc, nullptr, &newTexture);
}
if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create staging texture, result: 0x%X.",
result);
ANGLE_TRY(mRenderer->allocateTexture(desc, formatInfo, &mStagingTexture));
}
mStagingTexture = newTexture;
mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
}
else if (mTarget == GL_TEXTURE_2D || mTarget == GL_TEXTURE_2D_ARRAY ||
mTarget == GL_TEXTURE_CUBE_MAP)
{
ID3D11Texture2D *newTexture = nullptr;
D3D11_TEXTURE2D_DESC desc;
desc.Width = width;
desc.Height = height;
......@@ -557,8 +521,7 @@ gl::Error Image11::createStagingTexture()
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;
if (d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps())
.dataInitializerFunction != nullptr)
if (formatInfo.dataInitializerFunction != nullptr)
{
std::vector<D3D11_SUBRESOURCE_DATA> initialData;
std::vector<std::vector<BYTE>> textureData;
......@@ -566,21 +529,14 @@ gl::Error Image11::createStagingTexture()
width, height, 1, lodOffset + 1, &initialData,
&textureData);
result = device->CreateTexture2D(&desc, initialData.data(), &newTexture);
ANGLE_TRY(
mRenderer->allocateTexture(desc, formatInfo, initialData.data(), &mStagingTexture));
}
else
{
result = device->CreateTexture2D(&desc, nullptr, &newTexture);
}
if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create staging texture, result: 0x%X.",
result);
ANGLE_TRY(mRenderer->allocateTexture(desc, formatInfo, &mStagingTexture));
}
mStagingTexture = newTexture;
mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
}
else
......@@ -597,14 +553,14 @@ gl::Error Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
// We must recover from the TextureStorage if necessary, even for D3D11_MAP_WRITE.
ANGLE_TRY(recoverFromAssociatedStorage());
ID3D11Resource *stagingTexture = nullptr;
const TextureHelper11 *stagingTexture = nullptr;
unsigned int subresourceIndex = 0;
ANGLE_TRY(getStagingTexture(&stagingTexture, &subresourceIndex));
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
ASSERT(mStagingTexture);
HRESULT result = deviceContext->Map(stagingTexture, subresourceIndex, mapType, 0, map);
ASSERT(stagingTexture && stagingTexture->valid());
HRESULT result = deviceContext->Map(stagingTexture->get(), subresourceIndex, mapType, 0, map);
if (FAILED(result))
{
......@@ -623,10 +579,10 @@ gl::Error Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
void Image11::unmap()
{
if (mStagingTexture)
if (mStagingTexture.valid())
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
deviceContext->Unmap(mStagingTexture, mStagingSubresource);
deviceContext->Unmap(mStagingTexture.get(), mStagingSubresource);
}
}
......
......@@ -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;
......
......@@ -3404,13 +3404,13 @@ gl::Error Renderer11::copyTexture(const gl::Texture *source,
source->getFormat(GL_TEXTURE_2D, sourceLevel).info->format == destFormat &&
sourceStorage11->getFormatSet().texFormat == destStorage11->getFormatSet().texFormat)
{
ID3D11Resource *sourceResource = nullptr;
const TextureHelper11 *sourceResource = nullptr;
ANGLE_TRY(sourceStorage11->getResource(&sourceResource));
gl::ImageIndex sourceIndex = gl::ImageIndex::Make2D(sourceLevel);
UINT sourceSubresource = sourceStorage11->getSubresourceIndex(sourceIndex);
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(destStorage11->getResource(&destResource));
gl::ImageIndex destIndex = gl::ImageIndex::MakeGeneric(destTarget, destLevel);
......@@ -3425,8 +3425,8 @@ gl::Error Renderer11::copyTexture(const gl::Texture *source,
1u,
};
mDeviceContext->CopySubresourceRegion(destResource, destSubresource, destOffset.x,
destOffset.y, destOffset.z, sourceResource,
mDeviceContext->CopySubresourceRegion(destResource->get(), destSubresource, destOffset.x,
destOffset.y, destOffset.z, sourceResource->get(),
sourceSubresource, &sourceBox);
}
else
......@@ -3477,7 +3477,7 @@ gl::Error Renderer11::copyCompressedTexture(const gl::Texture *source,
TextureStorage11_2D *destStorage11 = GetAs<TextureStorage11_2D>(storage);
ASSERT(destStorage11);
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(destStorage11->getResource(&destResource));
gl::ImageIndex destIndex = gl::ImageIndex::Make2D(destLevel);
......@@ -3492,14 +3492,14 @@ gl::Error Renderer11::copyCompressedTexture(const gl::Texture *source,
TextureStorage11_2D *sourceStorage11 = GetAs<TextureStorage11_2D>(sourceStorage);
ASSERT(sourceStorage11);
ID3D11Resource *sourceResource = nullptr;
const TextureHelper11 *sourceResource = nullptr;
ANGLE_TRY(sourceStorage11->getResource(&sourceResource));
gl::ImageIndex sourceIndex = gl::ImageIndex::Make2D(sourceLevel);
UINT sourceSubresource = sourceStorage11->getSubresourceIndex(sourceIndex);
mDeviceContext->CopySubresourceRegion(destResource, destSubresource, 0, 0, 0, sourceResource,
sourceSubresource, nullptr);
mDeviceContext->CopySubresourceRegion(destResource->get(), destSubresource, 0, 0, 0,
sourceResource->get(), sourceSubresource, nullptr);
return gl::NoError();
}
......@@ -3555,14 +3555,8 @@ gl::Error Renderer11::createRenderTarget(int width,
// The format must be either an RTV or a DSV
ASSERT(bindRTV != bindDSV);
ID3D11Texture2D *texture = nullptr;
HRESULT result = mDevice->CreateTexture2D(&desc, nullptr, &texture);
if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create render target texture, result: 0x%X.", result);
}
TextureHelper11 texture;
ANGLE_TRY(allocateTexture(desc, formatInfo, &texture));
d3d11::SharedSRV srv;
d3d11::SharedSRV blitSRV;
......@@ -3575,12 +3569,7 @@ gl::Error Renderer11::createRenderTarget(int width,
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
gl::Error err = allocateResource(srvDesc, texture, &srv);
if (err.isError())
{
SafeRelease(texture);
return err;
}
ANGLE_TRY(allocateResource(srvDesc, texture.get(), &srv));
if (formatInfo.blitSRVFormat != formatInfo.srvFormat)
{
......@@ -3592,12 +3581,7 @@ gl::Error Renderer11::createRenderTarget(int width,
blitSRVDesc.Texture2D.MostDetailedMip = 0;
blitSRVDesc.Texture2D.MipLevels = 1;
err = allocateResource(blitSRVDesc, texture, &blitSRV);
if (err.isError())
{
SafeRelease(texture);
return err;
}
ANGLE_TRY(allocateResource(blitSRVDesc, texture.get(), &blitSRV));
}
else
{
......@@ -3615,12 +3599,7 @@ gl::Error Renderer11::createRenderTarget(int width,
dsvDesc.Flags = 0;
d3d11::DepthStencilView dsv;
gl::Error err = allocateResource(dsvDesc, texture, &dsv);
if (err.isError())
{
SafeRelease(texture);
return err;
}
ANGLE_TRY(allocateResource(dsvDesc, texture.get(), &dsv));
*outRT = new TextureRenderTarget11(std::move(dsv), texture, srv, format, formatInfo,
width, height, 1, supportedSamples);
......@@ -3634,12 +3613,7 @@ gl::Error Renderer11::createRenderTarget(int width,
rtvDesc.Texture2D.MipSlice = 0;
d3d11::RenderTargetView rtv;
gl::Error err = allocateResource(rtvDesc, texture, &rtv);
if (err.isError())
{
SafeRelease(texture);
return err;
}
ANGLE_TRY(allocateResource(rtvDesc, texture.get(), &rtv));
if (formatInfo.dataInitializerFunction != nullptr)
{
......@@ -3654,14 +3628,13 @@ gl::Error Renderer11::createRenderTarget(int width,
{
UNREACHABLE();
}
SafeRelease(texture);
}
else
{
*outRT = new TextureRenderTarget11(
d3d11::RenderTargetView(), nullptr, d3d11::SharedSRV(), d3d11::SharedSRV(), format,
d3d11::Format::Get(GL_NONE, mRenderer11DeviceCaps), width, height, 1, supportedSamples);
*outRT = new TextureRenderTarget11(d3d11::RenderTargetView(), TextureHelper11(),
d3d11::SharedSRV(), d3d11::SharedSRV(), format,
d3d11::Format::Get(GL_NONE, mRenderer11DeviceCaps),
width, height, 1, supportedSamples);
}
return gl::NoError();
......@@ -3678,8 +3651,8 @@ gl::Error Renderer11::createRenderTargetCopy(RenderTargetD3D *source, RenderTarg
RenderTarget11 *source11 = GetAs<RenderTarget11>(source);
RenderTarget11 *dest11 = GetAs<RenderTarget11>(newRT);
mDeviceContext->CopySubresourceRegion(dest11->getTexture(), dest11->getSubresourceIndex(), 0, 0,
0, source11->getTexture(),
mDeviceContext->CopySubresourceRegion(dest11->getTexture().get(), dest11->getSubresourceIndex(),
0, 0, 0, source11->getTexture().get(),
source11->getSubresourceIndex(), nullptr);
*outRT = newRT;
return gl::NoError();
......@@ -4078,14 +4051,11 @@ gl::Error Renderer11::readFromAttachment(const gl::FramebufferAttachment &srcAtt
const bool invertTexture = UsePresentPathFast(this, &srcAttachment);
RenderTargetD3D *renderTarget = nullptr;
ANGLE_TRY(srcAttachment.getRenderTarget(&renderTarget));
RenderTarget11 *rt11 = GetAs<RenderTarget11>(renderTarget);
ASSERT(rt11->getTexture());
RenderTarget11 *rt11 = nullptr;
ANGLE_TRY(srcAttachment.getRenderTarget(&rt11));
ASSERT(rt11->getTexture().valid());
TextureHelper11 textureHelper =
TextureHelper11::MakeAndReference(rt11->getTexture(), rt11->getFormatSet());
const TextureHelper11 &textureHelper = rt11->getTexture();
unsigned int sourceSubResource = rt11->getSubresourceIndex();
const gl::Extents &texSize = textureHelper.getExtents();
......@@ -4129,7 +4099,7 @@ gl::Error Renderer11::readFromAttachment(const gl::FramebufferAttachment &srcAtt
// For 2D multisampled textures, it points to the multisampled resolve texture.
const TextureHelper11 *srcTexture = &textureHelper;
if (textureHelper.getTextureType() == GL_TEXTURE_2D && textureHelper.getSampleCount() > 1)
if (textureHelper.is2D() && textureHelper.getSampleCount() > 1)
{
D3D11_TEXTURE2D_DESC resolveDesc;
resolveDesc.Width = static_cast<UINT>(texSize.width);
......@@ -4144,20 +4114,11 @@ gl::Error Renderer11::readFromAttachment(const gl::FramebufferAttachment &srcAtt
resolveDesc.CPUAccessFlags = 0;
resolveDesc.MiscFlags = 0;
ID3D11Texture2D *resolveTex2D = nullptr;
HRESULT result = mDevice->CreateTexture2D(&resolveDesc, nullptr, &resolveTex2D);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Renderer11::readTextureData failed to create internal resolve "
"texture for ReadPixels, HRESULT: 0x%X.",
result);
}
ANGLE_TRY(
allocateTexture(resolveDesc, textureHelper.getFormatSet(), &resolvedTextureHelper));
mDeviceContext->ResolveSubresource(resolveTex2D, 0, textureHelper.getTexture2D(),
mDeviceContext->ResolveSubresource(resolvedTextureHelper.get(), 0, textureHelper.get(),
sourceSubResource, textureHelper.getFormat());
resolvedTextureHelper =
TextureHelper11::MakeAndReference(resolveTex2D, textureHelper.getFormatSet());
sourceSubResource = 0;
srcTexture = &resolvedTextureHelper;
......@@ -4171,14 +4132,14 @@ gl::Error Renderer11::readFromAttachment(const gl::FramebufferAttachment &srcAtt
// Select the correct layer from a 3D attachment
srcBox.front = 0;
if (textureHelper.getTextureType() == GL_TEXTURE_3D)
if (textureHelper.is3D())
{
srcBox.front = static_cast<UINT>(srcAttachment.layer());
}
srcBox.back = srcBox.front + 1;
mDeviceContext->CopySubresourceRegion(stagingHelper.getResource(), 0, 0, 0, 0,
srcTexture->getResource(), sourceSubResource, &srcBox);
mDeviceContext->CopySubresourceRegion(stagingHelper.get(), 0, 0, 0, 0, srcTexture->get(),
sourceSubResource, &srcBox);
if (!invertTexture)
{
......@@ -4207,7 +4168,7 @@ gl::Error Renderer11::packPixels(const TextureHelper11 &textureHelper,
const PackPixelsParams &params,
uint8_t *pixelsOut)
{
ID3D11Resource *readResource = textureHelper.getResource();
ID3D11Resource *readResource = textureHelper.get();
D3D11_MAPPED_SUBRESOURCE mapping;
HRESULT hr = mDeviceContext->Map(readResource, 0, D3D11_MAP_READ, 0, &mapping);
......@@ -4253,8 +4214,7 @@ gl::Error Renderer11::blitRenderbufferRect(const gl::Rectangle &readRectIn,
<< "Failed to retrieve the internal draw render target from the draw framebuffer.";
}
TextureHelper11 drawTexture = TextureHelper11::MakeAndReference(
drawRenderTarget11->getTexture(), drawRenderTarget11->getFormatSet());
const TextureHelper11 &drawTexture = drawRenderTarget11->getTexture();
unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
RenderTarget11 *readRenderTarget11 = GetAs<RenderTarget11>(readRenderTarget);
......@@ -4283,14 +4243,13 @@ gl::Error Renderer11::blitRenderbufferRect(const gl::Rectangle &readRectIn,
viewDesc.Texture2D.MipLevels = 1;
viewDesc.Texture2D.MostDetailedMip = 0;
ANGLE_TRY(allocateResource(viewDesc, readTexture.getResource(), &readSRV));
ANGLE_TRY(allocateResource(viewDesc, readTexture.get(), &readSRV));
}
}
else
{
ASSERT(readRenderTarget11);
readTexture = TextureHelper11::MakeAndReference(readRenderTarget11->getTexture(),
readRenderTarget11->getFormatSet());
readTexture = readRenderTarget11->getTexture();
readSubresource = readRenderTarget11->getSubresourceIndex();
readSRV = readRenderTarget11->getBlitShaderResourceView();
if (!readSRV.valid())
......@@ -4456,9 +4415,8 @@ gl::Error Renderer11::blitRenderbufferRect(const gl::Rectangle &readRectIn,
// We also require complete framebuffer copies for depth-stencil blit.
D3D11_BOX *pSrcBox = wholeBufferCopy ? nullptr : &readBox;
mDeviceContext->CopySubresourceRegion(drawTexture.getResource(), drawSubresource, dstX,
dstY, 0, readTexture.getResource(), readSubresource,
pSrcBox);
mDeviceContext->CopySubresourceRegion(drawTexture.get(), drawSubresource, dstX, dstY, 0,
readTexture.get(), readSubresource, pSrcBox);
}
else
{
......@@ -4576,17 +4534,12 @@ Renderer11::resolveMultisampledTexture(RenderTarget11 *renderTarget, bool depth,
resolveDesc.CPUAccessFlags = 0;
resolveDesc.MiscFlags = 0;
ID3D11Texture2D *resolveTexture = nullptr;
HRESULT result = mDevice->CreateTexture2D(&resolveDesc, nullptr, &resolveTexture);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
}
TextureHelper11 resolveTexture;
ANGLE_TRY(allocateTexture(resolveDesc, formatSet, &resolveTexture));
mDeviceContext->ResolveSubresource(resolveTexture, 0, renderTarget->getTexture(),
mDeviceContext->ResolveSubresource(resolveTexture.get(), 0, renderTarget->getTexture().get(),
renderTarget->getSubresourceIndex(), formatSet.texFormat);
return TextureHelper11::MakeAndPossess2D(resolveTexture, renderTarget->getFormatSet());
return resolveTexture;
}
bool Renderer11::getLUID(LUID *adapterLuid) const
......@@ -4992,12 +4945,12 @@ gl::Error Renderer11::applyComputeUniforms(const ProgramD3D &programD3D,
}
gl::ErrorOrResult<TextureHelper11> Renderer11::createStagingTexture(
GLenum textureType,
ResourceType textureType,
const d3d11::Format &formatSet,
const gl::Extents &size,
StagingAccess readAndWriteAccess)
{
if (textureType == GL_TEXTURE_2D)
if (textureType == ResourceType::Texture2D)
{
D3D11_TEXTURE2D_DESC stagingDesc;
stagingDesc.Width = size.width;
......@@ -5017,16 +4970,11 @@ gl::ErrorOrResult<TextureHelper11> Renderer11::createStagingTexture(
stagingDesc.CPUAccessFlags |= D3D11_CPU_ACCESS_WRITE;
}
ID3D11Texture2D *stagingTex = nullptr;
HRESULT result = mDevice->CreateTexture2D(&stagingDesc, nullptr, &stagingTex);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "createStagingTexture failed, HRESULT: 0x%X.",
result);
}
return TextureHelper11::MakeAndPossess2D(stagingTex, formatSet);
TextureHelper11 stagingTex;
ANGLE_TRY(allocateTexture(stagingDesc, formatSet, &stagingTex));
return stagingTex;
}
ASSERT(textureType == GL_TEXTURE_3D);
ASSERT(textureType == ResourceType::Texture3D);
D3D11_TEXTURE3D_DESC stagingDesc;
stagingDesc.Width = size.width;
......@@ -5039,14 +4987,31 @@ gl::ErrorOrResult<TextureHelper11> Renderer11::createStagingTexture(
stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
stagingDesc.MiscFlags = 0;
ID3D11Texture3D *stagingTex = nullptr;
HRESULT result = mDevice->CreateTexture3D(&stagingDesc, nullptr, &stagingTex);
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "createStagingTexture failed, HRESULT: 0x%X.", result);
}
TextureHelper11 stagingTex;
ANGLE_TRY(allocateTexture(stagingDesc, formatSet, &stagingTex));
return stagingTex;
}
return TextureHelper11::MakeAndPossess3D(stagingTex, formatSet);
gl::Error Renderer11::allocateTexture(const D3D11_TEXTURE2D_DESC &desc,
const d3d11::Format &format,
const D3D11_SUBRESOURCE_DATA *initData,
TextureHelper11 *textureOut)
{
d3d11::Texture2D texture;
ANGLE_TRY(mResourceManager11.allocate(this, &desc, initData, &texture));
textureOut->init(std::move(texture), desc, format);
return gl::NoError();
}
gl::Error Renderer11::allocateTexture(const D3D11_TEXTURE3D_DESC &desc,
const d3d11::Format &format,
const D3D11_SUBRESOURCE_DATA *initData,
TextureHelper11 *textureOut)
{
d3d11::Texture3D texture;
ANGLE_TRY(mResourceManager11.allocate(this, &desc, initData, &texture));
textureOut->init(std::move(texture), desc, format);
return gl::NoError();
}
} // namespace rx
......@@ -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?
......
......@@ -70,14 +70,14 @@ SwapChain11::SwapChain11(Renderer11 *renderer,
mSwapChain(nullptr),
mSwapChain1(nullptr),
mKeyedMutex(nullptr),
mBackBufferTexture(nullptr),
mBackBufferTexture(),
mBackBufferRTView(),
mBackBufferSRView(),
mNeedsOffscreenTexture(NeedsOffscreenTexture(renderer, nativeWindow, orientation)),
mOffscreenTexture(nullptr),
mOffscreenTexture(),
mOffscreenRTView(),
mOffscreenSRView(),
mDepthStencilTexture(nullptr),
mDepthStencilTexture(),
mDepthStencilDSView(),
mDepthStencilSRView(),
mQuadVB(nullptr),
......@@ -111,13 +111,13 @@ void SwapChain11::release()
SafeRelease(mSwapChain1);
SafeRelease(mSwapChain);
SafeRelease(mKeyedMutex);
SafeRelease(mBackBufferTexture);
mBackBufferTexture.reset();
mBackBufferRTView.reset();
mBackBufferSRView.reset();
SafeRelease(mOffscreenTexture);
mOffscreenTexture.reset();
mOffscreenRTView.reset();
mOffscreenSRView.reset();
SafeRelease(mDepthStencilTexture);
mDepthStencilTexture.reset();
mDepthStencilDSView.reset();
mDepthStencilSRView.reset();
SafeRelease(mQuadVB);
......@@ -135,14 +135,14 @@ void SwapChain11::release()
void SwapChain11::releaseOffscreenColorBuffer()
{
SafeRelease(mOffscreenTexture);
mOffscreenTexture.reset();
mOffscreenRTView.reset();
mOffscreenSRView.reset();
}
void SwapChain11::releaseOffscreenDepthBuffer()
{
SafeRelease(mDepthStencilTexture);
mDepthStencilTexture.reset();
mDepthStencilDSView.reset();
mDepthStencilSRView.reset();
}
......@@ -184,11 +184,7 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
ASSERT(backbufferHeight >= 1);
// Preserve the render target content
ID3D11Texture2D *previousOffscreenTexture = mOffscreenTexture;
if (previousOffscreenTexture)
{
previousOffscreenTexture->AddRef();
}
TextureHelper11 previousOffscreenTexture(std::move(mOffscreenTexture));
const int previousWidth = mWidth;
const int previousHeight = mHeight;
......@@ -209,19 +205,21 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
(void **)&tempResource11);
ASSERT(SUCCEEDED(result));
mOffscreenTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(tempResource11);
mOffscreenTexture.set(d3d11::DynamicCastComObject<ID3D11Texture2D>(tempResource11),
backbufferFormatInfo);
SafeRelease(tempResource11);
}
else if (mD3DTexture != nullptr)
{
mOffscreenTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(mD3DTexture);
mOffscreenTexture.set(d3d11::DynamicCastComObject<ID3D11Texture2D>(mD3DTexture),
backbufferFormatInfo);
}
else
{
UNREACHABLE();
}
ASSERT(mOffscreenTexture != nullptr);
mOffscreenTexture->GetDesc(&offscreenTextureDesc);
ASSERT(mOffscreenTexture.valid());
mOffscreenTexture.getDesc(&offscreenTextureDesc);
}
else
{
......@@ -240,31 +238,23 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
offscreenTextureDesc.CPUAccessFlags = 0;
offscreenTextureDesc.MiscFlags = useSharedResource ? ANGLE_RESOURCE_SHARE_TYPE : 0;
HRESULT result =
device->CreateTexture2D(&offscreenTextureDesc, nullptr, &mOffscreenTexture);
if (FAILED(result))
gl::Error err = mRenderer->allocateTexture(offscreenTextureDesc, backbufferFormatInfo,
&mOffscreenTexture);
if (err.isError())
{
ERR() << "Could not create offscreen texture, " << gl::FmtHR(result);
ERR() << "Could not create offscreen texture, " << err;
release();
if (d3d11::isDeviceLostError(result))
{
return EGL_CONTEXT_LOST;
}
else
{
return EGL_BAD_ALLOC;
}
return EGL_BAD_ALLOC;
}
d3d11::SetDebugName(mOffscreenTexture, "Offscreen back buffer texture");
mOffscreenTexture.setDebugName("Offscreen back buffer texture");
// EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client
if (useSharedResource)
{
IDXGIResource *offscreenTextureResource = nullptr;
result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource);
HRESULT result = mOffscreenTexture.get()->QueryInterface(
__uuidof(IDXGIResource), (void **)&offscreenTextureResource);
// Fall back to no share handle on failure
if (FAILED(result))
......@@ -286,7 +276,7 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
}
// This may return null if the original texture was created without a keyed mutex.
mKeyedMutex = d3d11::DynamicCastComObject<IDXGIKeyedMutex>(mOffscreenTexture);
mKeyedMutex = d3d11::DynamicCastComObject<IDXGIKeyedMutex>(mOffscreenTexture.get());
D3D11_RENDER_TARGET_VIEW_DESC offscreenRTVDesc;
offscreenRTVDesc.Format = backbufferFormatInfo.rtvFormat;
......@@ -295,7 +285,7 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
offscreenRTVDesc.Texture2D.MipSlice = 0;
gl::Error err =
mRenderer->allocateResource(offscreenRTVDesc, mOffscreenTexture, &mOffscreenRTView);
mRenderer->allocateResource(offscreenRTVDesc, mOffscreenTexture.get(), &mOffscreenRTView);
ASSERT(!err.isError());
mOffscreenRTView.setDebugName("Offscreen back buffer render target");
......@@ -308,12 +298,13 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
if (offscreenTextureDesc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
{
err = mRenderer->allocateResource(offscreenSRVDesc, mOffscreenTexture, &mOffscreenSRView);
err = mRenderer->allocateResource(offscreenSRVDesc, mOffscreenTexture.get(),
&mOffscreenSRView);
ASSERT(!err.isError());
mOffscreenSRView.setDebugName("Offscreen back buffer shader resource");
}
if (previousOffscreenTexture != nullptr)
if (previousOffscreenTexture.valid())
{
D3D11_BOX sourceBox = {0};
sourceBox.left = 0;
......@@ -325,10 +316,8 @@ EGLint SwapChain11::resetOffscreenColorBuffer(int backbufferWidth, int backbuffe
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
const int yoffset = std::max(backbufferHeight - previousHeight, 0);
deviceContext->CopySubresourceRegion(mOffscreenTexture, 0, 0, yoffset, 0,
previousOffscreenTexture, 0, &sourceBox);
SafeRelease(previousOffscreenTexture);
deviceContext->CopySubresourceRegion(mOffscreenTexture.get(), 0, 0, yoffset, 0,
previousOffscreenTexture.get(), 0, &sourceBox);
if (mSwapChain)
{
......@@ -367,25 +356,15 @@ EGLint SwapChain11::resetOffscreenDepthBuffer(int backbufferWidth, int backbuffe
depthStencilTextureDesc.CPUAccessFlags = 0;
depthStencilTextureDesc.MiscFlags = 0;
ID3D11Device *device = mRenderer->getDevice();
HRESULT result =
device->CreateTexture2D(&depthStencilTextureDesc, nullptr, &mDepthStencilTexture);
if (FAILED(result))
gl::Error err = mRenderer->allocateTexture(depthStencilTextureDesc, depthBufferFormatInfo,
&mDepthStencilTexture);
if (err.isError())
{
ERR() << "Could not create depthstencil surface for new swap chain, "
<< gl::FmtHR(result);
ERR() << "Could not create depthstencil surface for new swap chain, " << err;
release();
if (d3d11::isDeviceLostError(result))
{
return EGL_CONTEXT_LOST;
}
else
{
return EGL_BAD_ALLOC;
}
return EGL_BAD_ALLOC;
}
d3d11::SetDebugName(mDepthStencilTexture, "Offscreen depth stencil texture");
mDepthStencilTexture.setDebugName("Offscreen depth stencil texture");
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilDesc;
depthStencilDesc.Format = depthBufferFormatInfo.dsvFormat;
......@@ -394,8 +373,8 @@ EGLint SwapChain11::resetOffscreenDepthBuffer(int backbufferWidth, int backbuffe
depthStencilDesc.Flags = 0;
depthStencilDesc.Texture2D.MipSlice = 0;
gl::Error err = mRenderer->allocateResource(depthStencilDesc, mDepthStencilTexture,
&mDepthStencilDSView);
err = mRenderer->allocateResource(depthStencilDesc, mDepthStencilTexture.get(),
&mDepthStencilDSView);
ASSERT(!err.isError());
mDepthStencilDSView.setDebugName("Offscreen depth stencil view");
......@@ -409,7 +388,7 @@ EGLint SwapChain11::resetOffscreenDepthBuffer(int backbufferWidth, int backbuffe
depthStencilSRVDesc.Texture2D.MostDetailedMip = 0;
depthStencilSRVDesc.Texture2D.MipLevels = static_cast<UINT>(-1);
err = mRenderer->allocateResource(depthStencilSRVDesc, mDepthStencilTexture,
err = mRenderer->allocateResource(depthStencilSRVDesc, mDepthStencilTexture.get(),
&mDepthStencilSRView);
ASSERT(!err.isError());
mDepthStencilSRView.setDebugName("Offscreen depth stencil shader resource");
......@@ -442,10 +421,10 @@ EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
}
// Can only call resize if we have already created our swap buffer and resources
ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView.valid() &&
ASSERT(mSwapChain && mBackBufferTexture.valid() && mBackBufferRTView.valid() &&
mBackBufferSRView.valid());
SafeRelease(mBackBufferTexture);
mBackBufferTexture.reset();
mBackBufferRTView.reset();
mBackBufferSRView.reset();
......@@ -476,17 +455,23 @@ EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight)
}
}
result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
ID3D11Texture2D *backbufferTexture = nullptr;
result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
reinterpret_cast<void **>(&backbufferTexture));
ASSERT(SUCCEEDED(result));
if (SUCCEEDED(result))
{
d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
const auto &format =
d3d11::Format::Get(mOffscreenRenderTargetFormat, mRenderer->getRenderer11DeviceCaps());
mBackBufferTexture.set(backbufferTexture, format);
mBackBufferTexture.setDebugName("Back buffer texture");
gl::Error err = mRenderer->allocateResourceNoDesc(mBackBufferTexture, &mBackBufferRTView);
gl::Error err =
mRenderer->allocateResourceNoDesc(mBackBufferTexture.get(), &mBackBufferRTView);
ASSERT(!err.isError());
mBackBufferRTView.setDebugName("Back buffer render target");
err = mRenderer->allocateResourceNoDesc(mBackBufferTexture, &mBackBufferSRView);
err = mRenderer->allocateResourceNoDesc(mBackBufferTexture.get(), &mBackBufferSRView);
ASSERT(!err.isError());
mBackBufferSRView.setDebugName("Back buffer shader resource");
}
......@@ -552,7 +537,7 @@ EGLint SwapChain11::reset(EGLint backbufferWidth, EGLint backbufferHeight, EGLin
// old render target still exists for the purpose of preserving its contents.
SafeRelease(mSwapChain1);
SafeRelease(mSwapChain);
SafeRelease(mBackBufferTexture);
mBackBufferTexture.reset();
mBackBufferRTView.reset();
// EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains
......@@ -589,15 +574,21 @@ EGLint SwapChain11::reset(EGLint backbufferWidth, EGLint backbufferHeight, EGLin
mSwapChain1 = d3d11::DynamicCastComObject<IDXGISwapChain1>(mSwapChain);
}
result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture);
ID3D11Texture2D *backbufferTex = nullptr;
result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D),
reinterpret_cast<LPVOID *>(&backbufferTex));
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture");
const auto &format =
d3d11::Format::Get(mOffscreenRenderTargetFormat, mRenderer->getRenderer11DeviceCaps());
mBackBufferTexture.set(backbufferTex, format);
mBackBufferTexture.setDebugName("Back buffer texture");
gl::Error err = mRenderer->allocateResourceNoDesc(mBackBufferTexture, &mBackBufferRTView);
gl::Error err =
mRenderer->allocateResourceNoDesc(mBackBufferTexture.get(), &mBackBufferRTView);
ASSERT(!err.isError());
mBackBufferRTView.setDebugName("Back buffer render target");
err = mRenderer->allocateResourceNoDesc(mBackBufferTexture, &mBackBufferSRView);
err = mRenderer->allocateResourceNoDesc(mBackBufferTexture.get(), &mBackBufferSRView);
ASSERT(!err.isError());
mBackBufferSRView.setDebugName("Back buffer shader resource view");
}
......@@ -889,7 +880,7 @@ EGLint SwapChain11::present(EGLint x, EGLint y, EGLint width, EGLint height)
return EGL_SUCCESS;
}
ID3D11Texture2D *SwapChain11::getOffscreenTexture()
const TextureHelper11 &SwapChain11::getOffscreenTexture()
{
return mNeedsOffscreenTexture ? mOffscreenTexture : mBackBufferTexture;
}
......@@ -914,7 +905,7 @@ const d3d11::SharedSRV &SwapChain11::getDepthStencilShaderResource()
return mDepthStencilSRView;
}
ID3D11Texture2D *SwapChain11::getDepthStencilTexture()
const TextureHelper11 &SwapChain11::getDepthStencilTexture()
{
return mDepthStencilTexture;
}
......
......@@ -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;
......
......@@ -53,7 +53,7 @@ TextureStorage11::TextureStorage11(Renderer11 *renderer,
mTextureWidth(0),
mTextureHeight(0),
mTextureDepth(0),
mDropStencilTexture(nullptr),
mDropStencilTexture(),
mBindFlags(bindFlags),
mMiscFlags(miscFlags)
{
......@@ -62,7 +62,6 @@ TextureStorage11::TextureStorage11(Renderer11 *renderer,
TextureStorage11::~TextureStorage11()
{
mSrvCache.clear();
SafeRelease(mDropStencilTexture);
}
DWORD TextureStorage11::GetTextureBindFlags(GLenum internalFormat,
......@@ -239,7 +238,7 @@ gl::Error TextureStorage11::getCachedOrCreateSRV(const SRVKey &key, d3d11::Share
return gl::NoError();
}
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
if (key.swizzle)
......@@ -252,8 +251,8 @@ gl::Error TextureStorage11::getCachedOrCreateSRV(const SRVKey &key, d3d11::Share
}
else if (key.dropStencil)
{
ASSERT(mDropStencilTexture);
texture = mDropStencilTexture;
ASSERT(mDropStencilTexture.valid());
texture = &mDropStencilTexture;
format = DXGI_FORMAT_R32_FLOAT;
}
else
......@@ -264,7 +263,7 @@ gl::Error TextureStorage11::getCachedOrCreateSRV(const SRVKey &key, d3d11::Share
d3d11::SharedSRV srv;
ANGLE_TRY(createSRV(key.baseLevel, key.mipLevels, format, texture, &srv));
ANGLE_TRY(createSRV(key.baseLevel, key.mipLevels, format, *texture, &srv));
mSrvCache.insert(std::make_pair(key, srv));
*outSRV = srv;
......@@ -288,12 +287,12 @@ gl::Error TextureStorage11::getSRVLevel(int mipLevel, bool blitSRV, const d3d11:
}
else
{
ID3D11Resource *resource = nullptr;
const TextureHelper11 *resource = nullptr;
ANGLE_TRY(getResource(&resource));
DXGI_FORMAT resourceFormat =
blitSRV ? mFormatInfo.blitSRVFormat : mFormatInfo.srvFormat;
ANGLE_TRY(createSRV(mipLevel, 1, resourceFormat, resource, &levelSRVs[mipLevel]));
ANGLE_TRY(createSRV(mipLevel, 1, resourceFormat, *resource, &levelSRVs[mipLevel]));
}
}
......@@ -370,7 +369,7 @@ void TextureStorage11::markLevelDirty(int mipLevel)
mSwizzleCache[mipLevel] = gl::SwizzleState();
}
SafeRelease(mDropStencilTexture);
mDropStencilTexture.reset();
}
void TextureStorage11::markDirty()
......@@ -381,12 +380,12 @@ void TextureStorage11::markDirty()
}
}
gl::Error TextureStorage11::updateSubresourceLevel(ID3D11Resource *srcTexture,
gl::Error TextureStorage11::updateSubresourceLevel(const TextureHelper11 &srcTexture,
unsigned int sourceSubresource,
const gl::ImageIndex &index,
const gl::Box &copyArea)
{
ASSERT(srcTexture);
ASSERT(srcTexture.valid());
const GLint level = index.mipIndex;
......@@ -398,7 +397,7 @@ gl::Error TextureStorage11::updateSubresourceLevel(ID3D11Resource *srcTexture,
copyArea.width == texSize.width && copyArea.height == texSize.height &&
copyArea.depth == texSize.depth;
ID3D11Resource *dstTexture = nullptr;
const TextureHelper11 *dstTexture = nullptr;
// If the zero-LOD workaround is active and we want to update a level greater than zero, then we
// should update the mipmapped texture, even if mapmaps are currently disabled.
......@@ -413,7 +412,7 @@ gl::Error TextureStorage11::updateSubresourceLevel(ID3D11Resource *srcTexture,
unsigned int dstSubresource = getSubresourceIndex(index);
ASSERT(dstTexture);
ASSERT(dstTexture->valid());
const d3d11::DXGIFormatSize &dxgiFormatSizeInfo =
d3d11::GetDXGIFormatSizeInfo(mFormatInfo.texFormat);
......@@ -421,10 +420,8 @@ gl::Error TextureStorage11::updateSubresourceLevel(ID3D11Resource *srcTexture,
{
// CopySubresourceRegion cannot copy partial depth stencils, use the blitter instead
Blit11 *blitter = mRenderer->getBlitter();
TextureHelper11 source = TextureHelper11::MakeAndReference(srcTexture, getFormatSet());
TextureHelper11 dest = TextureHelper11::MakeAndReference(dstTexture, getFormatSet());
return blitter->copyDepthStencil(source, sourceSubresource, copyArea, texSize, dest,
dstSubresource, copyArea, texSize, nullptr);
return blitter->copyDepthStencil(srcTexture, sourceSubresource, copyArea, texSize,
*dstTexture, dstSubresource, copyArea, texSize, nullptr);
}
D3D11_BOX srcBox;
......@@ -439,19 +436,20 @@ gl::Error TextureStorage11::updateSubresourceLevel(ID3D11Resource *srcTexture,
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
context->CopySubresourceRegion(dstTexture, dstSubresource, copyArea.x, copyArea.y, copyArea.z,
srcTexture, sourceSubresource, fullCopy ? nullptr : &srcBox);
context->CopySubresourceRegion(dstTexture->get(), dstSubresource, copyArea.x, copyArea.y,
copyArea.z, srcTexture.get(), sourceSubresource,
fullCopy ? nullptr : &srcBox);
return gl::NoError();
}
gl::Error TextureStorage11::copySubresourceLevel(ID3D11Resource *dstTexture,
gl::Error TextureStorage11::copySubresourceLevel(const TextureHelper11 &dstTexture,
unsigned int dstSubresource,
const gl::ImageIndex &index,
const gl::Box &region)
{
ASSERT(dstTexture);
ASSERT(dstTexture.valid());
ID3D11Resource *srcTexture = nullptr;
const TextureHelper11 *srcTexture = nullptr;
// If the zero-LOD workaround is active and we want to update a level greater than zero, then we
// should update the mipmapped texture, even if mapmaps are currently disabled.
......@@ -464,7 +462,7 @@ gl::Error TextureStorage11::copySubresourceLevel(ID3D11Resource *dstTexture,
ANGLE_TRY(getResource(&srcTexture));
}
ASSERT(srcTexture);
ASSERT(srcTexture->valid());
unsigned int srcSubresource = getSubresourceIndex(index);
......@@ -489,8 +487,8 @@ gl::Error TextureStorage11::copySubresourceLevel(ID3D11Resource *dstTexture,
pSrcBox = &srcBox;
}
context->CopySubresourceRegion(dstTexture, dstSubresource, region.x, region.y, region.z,
srcTexture, srcSubresource, pSrcBox);
context->CopySubresourceRegion(dstTexture.get(), dstSubresource, region.x, region.y, region.z,
srcTexture->get(), srcSubresource, pSrcBox);
return gl::NoError();
}
......@@ -548,15 +546,15 @@ gl::Error TextureStorage11::copyToStorage(TextureStorage *destStorage)
{
ASSERT(destStorage);
ID3D11Resource *sourceResouce = nullptr;
const TextureHelper11 *sourceResouce = nullptr;
ANGLE_TRY(getResource(&sourceResouce));
TextureStorage11 *dest11 = GetAs<TextureStorage11>(destStorage);
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
ID3D11DeviceContext *immediateContext = mRenderer->getDeviceContext();
immediateContext->CopyResource(destResource, sourceResouce);
immediateContext->CopyResource(destResource->get(), sourceResouce->get());
dest11->markDirty();
......@@ -574,9 +572,9 @@ gl::Error TextureStorage11::setData(const gl::ImageIndex &index,
markLevelDirty(index.mipIndex);
ID3D11Resource *resource = nullptr;
const TextureHelper11 *resource = nullptr;
ANGLE_TRY(getResource(&resource));
ASSERT(resource);
ASSERT(resource && resource->valid());
UINT destSubresource = getSubresourceIndex(index);
......@@ -653,12 +651,12 @@ gl::Error TextureStorage11::setData(const gl::ImageIndex &index,
destD3DBox.front = destBox->z;
destD3DBox.back = destBox->z + destBox->depth;
immediateContext->UpdateSubresource(resource, destSubresource, &destD3DBox, data,
immediateContext->UpdateSubresource(resource->get(), destSubresource, &destD3DBox, data,
bufferRowPitch, bufferDepthPitch);
}
else
{
immediateContext->UpdateSubresource(resource, destSubresource, nullptr, data,
immediateContext->UpdateSubresource(resource->get(), destSubresource, nullptr, data,
bufferRowPitch, bufferDepthPitch);
}
......@@ -677,13 +675,11 @@ TextureStorage11_2D::TextureStorage11_2D(Renderer11 *renderer, SwapChain11 *swap
0,
swapchain->getRenderTargetInternalFormat()),
mTexture(swapchain->getOffscreenTexture()),
mLevelZeroTexture(nullptr),
mLevelZeroTexture(),
mLevelZeroRenderTarget(nullptr),
mUseLevelZeroTexture(false),
mSwizzleTexture(nullptr)
mSwizzleTexture()
{
mTexture->AddRef();
for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
mAssociatedImages[i] = nullptr;
......@@ -691,7 +687,7 @@ TextureStorage11_2D::TextureStorage11_2D(Renderer11 *renderer, SwapChain11 *swap
}
D3D11_TEXTURE2D_DESC texDesc;
mTexture->GetDesc(&texDesc);
mTexture.getDesc(&texDesc);
mMipLevels = texDesc.MipLevels;
mTextureWidth = texDesc.Width;
mTextureHeight = texDesc.Height;
......@@ -714,12 +710,12 @@ TextureStorage11_2D::TextureStorage11_2D(Renderer11 *renderer,
renderTarget,
levels),
internalformat),
mTexture(nullptr),
mTexture(),
mHasKeyedMutex(false),
mLevelZeroTexture(nullptr),
mLevelZeroTexture(),
mLevelZeroRenderTarget(nullptr),
mUseLevelZeroTexture(hintLevelZeroOnly && levels > 1),
mSwizzleTexture(nullptr)
mSwizzleTexture()
{
for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
......@@ -756,10 +752,6 @@ TextureStorage11_2D::~TextureStorage11_2D()
}
}
SafeRelease(mTexture);
SafeRelease(mSwizzleTexture);
SafeRelease(mLevelZeroTexture);
SafeDelete(mLevelZeroRenderTarget);
for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
......@@ -786,36 +778,36 @@ gl::Error TextureStorage11_2D::copyToStorage(TextureStorage *destStorage)
{
// If either mTexture or mLevelZeroTexture exist, then we need to copy them into the
// corresponding textures in destStorage.
if (mTexture)
if (mTexture.valid())
{
ANGLE_TRY(dest11->useLevelZeroWorkaroundTexture(false));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
immediateContext->CopyResource(destResource, mTexture);
immediateContext->CopyResource(destResource->get(), mTexture.get());
}
if (mLevelZeroTexture)
if (mLevelZeroTexture.valid())
{
ANGLE_TRY(dest11->useLevelZeroWorkaroundTexture(true));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
immediateContext->CopyResource(destResource, mLevelZeroTexture);
immediateContext->CopyResource(destResource->get(), mLevelZeroTexture.get());
}
return gl::NoError();
}
ID3D11Resource *sourceResouce = nullptr;
const TextureHelper11 *sourceResouce = nullptr;
ANGLE_TRY(getResource(&sourceResouce));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
immediateContext->CopyResource(destResource, sourceResouce);
immediateContext->CopyResource(destResource->get(), sourceResouce->get());
dest11->markDirty();
return gl::NoError();
......@@ -827,28 +819,30 @@ gl::Error TextureStorage11_2D::useLevelZeroWorkaroundTexture(bool useLevelZeroTe
if (useLevelZeroTexture && mMipLevels > 1)
{
if (!mUseLevelZeroTexture && mTexture)
if (!mUseLevelZeroTexture && mTexture.valid())
{
ANGLE_TRY(ensureTextureExists(1));
// Pull data back from the mipped texture if necessary.
ASSERT(mLevelZeroTexture);
ASSERT(mLevelZeroTexture.valid());
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
context->CopySubresourceRegion(mLevelZeroTexture, 0, 0, 0, 0, mTexture, 0, nullptr);
context->CopySubresourceRegion(mLevelZeroTexture.get(), 0, 0, 0, 0, mTexture.get(), 0,
nullptr);
}
mUseLevelZeroTexture = true;
}
else
{
if (mUseLevelZeroTexture && mLevelZeroTexture)
if (mUseLevelZeroTexture && mLevelZeroTexture.valid())
{
ANGLE_TRY(ensureTextureExists(mMipLevels));
// Pull data back from the level zero texture if necessary.
ASSERT(mTexture);
ASSERT(mTexture.valid());
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
context->CopySubresourceRegion(mTexture, 0, 0, 0, 0, mLevelZeroTexture, 0, nullptr);
context->CopySubresourceRegion(mTexture.get(), 0, 0, 0, 0, mLevelZeroTexture.get(), 0,
nullptr);
}
mUseLevelZeroTexture = false;
......@@ -932,30 +926,30 @@ gl::Error TextureStorage11_2D::releaseAssociatedImage(const gl::ImageIndex &inde
return gl::NoError();
}
gl::Error TextureStorage11_2D::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_2D::getResource(const TextureHelper11 **outResource)
{
if (mUseLevelZeroTexture && mMipLevels > 1)
{
ANGLE_TRY(ensureTextureExists(1));
*outResource = mLevelZeroTexture;
*outResource = &mLevelZeroTexture;
return gl::NoError();
}
ANGLE_TRY(ensureTextureExists(mMipLevels));
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
gl::Error TextureStorage11_2D::getMippedResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_2D::getMippedResource(const TextureHelper11 **outResource)
{
// This shouldn't be called unless the zero max LOD workaround is active.
ASSERT(mRenderer->getWorkarounds().zeroMaxLodWorkaround);
ANGLE_TRY(ensureTextureExists(mMipLevels));
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
......@@ -965,16 +959,14 @@ gl::Error TextureStorage11_2D::ensureTextureExists(int mipLevels)
bool useLevelZeroTexture = mRenderer->getWorkarounds().zeroMaxLodWorkaround
? (mipLevels == 1) && (mMipLevels > 1)
: false;
ID3D11Texture2D **outputTexture = useLevelZeroTexture ? &mLevelZeroTexture : &mTexture;
TextureHelper11 *outputTexture = useLevelZeroTexture ? &mLevelZeroTexture : &mTexture;
// if the width or height is not positive this should be treated as an incomplete texture
// we handle that here by skipping the d3d texture creation
if (*outputTexture == nullptr && mTextureWidth > 0 && mTextureHeight > 0)
if (!outputTexture->valid() && mTextureWidth > 0 && mTextureHeight > 0)
{
ASSERT(mipLevels > 0);
ID3D11Device *device = mRenderer->getDevice();
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth; // Compressed texture size constraints?
desc.Height = mTextureHeight;
......@@ -988,23 +980,8 @@ gl::Error TextureStorage11_2D::ensureTextureExists(int mipLevels)
desc.CPUAccessFlags = 0;
desc.MiscFlags = getMiscFlags();
HRESULT result = device->CreateTexture2D(&desc, nullptr, outputTexture);
// this can happen from windows TDR
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create 2D texture storage, result: 0x%X.",
result);
}
else if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create 2D texture storage, result: 0x%X.",
result);
}
d3d11::SetDebugName(*outputTexture, "TexStorage2D.Texture");
ANGLE_TRY(mRenderer->allocateTexture(desc, mFormatInfo, outputTexture));
outputTexture->setDebugName("TexStorage2D.Texture");
}
return gl::NoError();
......@@ -1033,7 +1010,7 @@ gl::Error TextureStorage11_2D::getRenderTarget(const gl::ImageIndex &index, Rend
return gl::NoError();
}
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
ANGLE_TRY(getResource(&texture));
const d3d11::SharedSRV *srv = nullptr;
......@@ -1052,7 +1029,7 @@ gl::Error TextureStorage11_2D::getRenderTarget(const gl::ImageIndex &index, Rend
rtvDesc.Texture2D.MipSlice = mTopLevel + level;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mLevelZeroTexture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mLevelZeroTexture.get(), &rtv));
mLevelZeroRenderTarget = new TextureRenderTarget11(
std::move(rtv), mLevelZeroTexture, d3d11::SharedSRV(), d3d11::SharedSRV(),
......@@ -1072,10 +1049,10 @@ gl::Error TextureStorage11_2D::getRenderTarget(const gl::ImageIndex &index, Rend
rtvDesc.Texture2D.MipSlice = mTopLevel + level;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture->get(), &rtv));
mRenderTarget[level] = new TextureRenderTarget11(
std::move(rtv), texture, *srv, *blitSRV, mFormatInfo.internalFormat, getFormatSet(),
std::move(rtv), *texture, *srv, *blitSRV, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(level), getLevelHeight(level), 1, 0);
*outRT = mRenderTarget[level];
......@@ -1091,10 +1068,10 @@ gl::Error TextureStorage11_2D::getRenderTarget(const gl::ImageIndex &index, Rend
dsvDesc.Flags = 0;
d3d11::DepthStencilView dsv;
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture, &dsv));
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture->get(), &dsv));
mRenderTarget[level] = new TextureRenderTarget11(
std::move(dsv), texture, *srv, mFormatInfo.internalFormat, getFormatSet(),
std::move(dsv), *texture, *srv, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(level), getLevelHeight(level), 1, 0);
*outRT = mRenderTarget[level];
......@@ -1104,7 +1081,7 @@ gl::Error TextureStorage11_2D::getRenderTarget(const gl::ImageIndex &index, Rend
gl::Error TextureStorage11_2D::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
ASSERT(outSRV);
......@@ -1115,7 +1092,7 @@ gl::Error TextureStorage11_2D::createSRV(int baseLevel,
srvDesc.Texture2D.MostDetailedMip = mTopLevel + baseLevel;
srvDesc.Texture2D.MipLevels = mipLevels;
ID3D11Resource *srvTexture = texture;
const TextureHelper11 *srvTexture = &texture;
if (mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
......@@ -1127,37 +1104,37 @@ gl::Error TextureStorage11_2D::createSRV(int baseLevel,
if (mipLevels == 1 && mMipLevels > 1)
{
// We must use a SRV on the level-zero-only texture.
ASSERT(mLevelZeroTexture != nullptr && texture == mLevelZeroTexture);
srvTexture = mLevelZeroTexture;
ASSERT(mLevelZeroTexture.valid() && texture == mLevelZeroTexture);
srvTexture = &mLevelZeroTexture;
}
else
{
ASSERT(mipLevels == static_cast<int>(mMipLevels));
ASSERT(mTexture != nullptr && texture == mTexture);
srvTexture = mTexture;
ASSERT(mTexture.valid() && texture == mTexture);
srvTexture = &mTexture;
}
}
ANGLE_TRY(mRenderer->allocateResource(srvDesc, srvTexture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, srvTexture->get(), outSRV));
outSRV->setDebugName("TexStorage2D.SRV");
return gl::NoError();
}
gl::Error TextureStorage11_2D::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_2D::getSwizzleTexture(const TextureHelper11 **outTexture)
{
ASSERT(outTexture);
if (!mSwizzleTexture)
if (!mSwizzleTexture.valid())
{
ID3D11Device *device = mRenderer->getDevice();
const auto &format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps());
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
desc.MipLevels = mMipLevels;
desc.ArraySize = 1;
desc.Format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps()).texFormat;
desc.Format = format.texFormat;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
......@@ -1165,19 +1142,11 @@ gl::Error TextureStorage11_2D::getSwizzleTexture(ID3D11Resource **outTexture)
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
HRESULT result = device->CreateTexture2D(&desc, nullptr, &mSwizzleTexture);
ASSERT(result == E_OUTOFMEMORY || SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create internal swizzle texture, result: 0x%X.", result);
}
d3d11::SetDebugName(mSwizzleTexture, "TexStorage2D.SwizzleTexture");
ANGLE_TRY(mRenderer->allocateTexture(desc, format, &mSwizzleTexture));
mSwizzleTexture.setDebugName("TexStorage2D.SwizzleTexture");
}
*outTexture = mSwizzleTexture;
*outTexture = &mSwizzleTexture;
return gl::NoError();
}
......@@ -1189,7 +1158,7 @@ gl::Error TextureStorage11_2D::getSwizzleRenderTarget(int mipLevel,
if (!mSwizzleRenderTargets[mipLevel].valid())
{
ID3D11Resource *swizzleTexture = nullptr;
const TextureHelper11 *swizzleTexture = nullptr;
ANGLE_TRY(getSwizzleTexture(&swizzleTexture));
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
......@@ -1198,7 +1167,7 @@ gl::Error TextureStorage11_2D::getSwizzleRenderTarget(int mipLevel,
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvDesc.Texture2D.MipSlice = mTopLevel + mipLevel;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture,
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture.get(),
&mSwizzleRenderTargets[mipLevel]));
}
......@@ -1208,7 +1177,7 @@ gl::Error TextureStorage11_2D::getSwizzleRenderTarget(int mipLevel,
gl::ErrorOrResult<TextureStorage11::DropStencil> TextureStorage11_2D::ensureDropStencilTexture()
{
if (mDropStencilTexture)
if (mDropStencilTexture.valid())
{
return DropStencil::ALREADY_EXISTS;
}
......@@ -1226,14 +1195,10 @@ gl::ErrorOrResult<TextureStorage11::DropStencil> TextureStorage11_2D::ensureDrop
dropDesc.Usage = D3D11_USAGE_DEFAULT;
dropDesc.Width = mTextureWidth;
ID3D11Device *device = mRenderer->getDevice();
HRESULT hr = device->CreateTexture2D(&dropDesc, nullptr, &mDropStencilTexture);
if (FAILED(hr))
{
return gl::InternalError() << "Error creating drop stencil texture.";
}
d3d11::SetDebugName(mDropStencilTexture, "TexStorage2D.DropStencil");
const auto &format =
d3d11::Format::Get(GL_DEPTH_COMPONENT32F, mRenderer->getRenderer11DeviceCaps());
ANGLE_TRY(mRenderer->allocateTexture(dropDesc, format, &mDropStencilTexture));
mDropStencilTexture.setDebugName("TexStorage2D.DropStencil");
ANGLE_TRY(initDropStencilTexture(gl::ImageIndexIterator::Make2D(0, mMipLevels)));
......@@ -1248,13 +1213,13 @@ TextureStorage11_External::TextureStorage11_External(
{
ASSERT(stream->getProducerType() == egl::Stream::ProducerType::D3D11TextureNV12);
StreamProducerNV12 *producer = static_cast<StreamProducerNV12 *>(stream->getImplementation());
mTexture = producer->getD3DTexture();
mTexture.set(producer->getD3DTexture(), mFormatInfo);
mSubresourceIndex = producer->getArraySlice();
mTexture->AddRef();
mTexture.get()->AddRef();
mMipLevels = 1;
D3D11_TEXTURE2D_DESC desc;
mTexture->GetDesc(&desc);
mTexture.getDesc(&desc);
mTextureWidth = desc.Width;
mTextureHeight = desc.Height;
mTextureDepth = 1;
......@@ -1263,7 +1228,6 @@ TextureStorage11_External::TextureStorage11_External(
TextureStorage11_External::~TextureStorage11_External()
{
SafeRelease(mTexture);
if (mHasKeyedMutex)
{
// If the keyed mutex is released that will unbind it and cause the state cache to become
......@@ -1313,15 +1277,15 @@ gl::Error TextureStorage11_External::releaseAssociatedImage(const gl::ImageIndex
return gl::NoError();
}
gl::Error TextureStorage11_External::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_External::getResource(const TextureHelper11 **outResource)
{
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
gl::Error TextureStorage11_External::getMippedResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_External::getMippedResource(const TextureHelper11 **outResource)
{
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
......@@ -1336,7 +1300,7 @@ gl::Error TextureStorage11_External::getRenderTarget(const gl::ImageIndex &index
gl::Error TextureStorage11_External::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
// Since external textures are treates as non-mipmapped textures, we ignore mipmap levels and
......@@ -1353,15 +1317,13 @@ gl::Error TextureStorage11_External::createSRV(int baseLevel,
srvDesc.Texture2DArray.FirstArraySlice = mSubresourceIndex;
srvDesc.Texture2DArray.ArraySize = 1;
ID3D11Resource *srvTexture = texture;
ANGLE_TRY(mRenderer->allocateResource(srvDesc, srvTexture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), outSRV));
outSRV->setDebugName("TexStorage2D.SRV");
return gl::NoError();
}
gl::Error TextureStorage11_External::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_External::getSwizzleTexture(const TextureHelper11 **outTexture)
{
UNIMPLEMENTED();
return gl::Error(GL_INVALID_OPERATION);
......@@ -1383,7 +1345,7 @@ TextureStorage11_EGLImage::TextureStorage11_EGLImage(Renderer11 *renderer,
renderTarget11->getInternalFormat()),
mImage(eglImage),
mCurrentRenderTarget(0),
mSwizzleTexture(nullptr),
mSwizzleTexture(),
mSwizzleRenderTargets(gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
{
mCurrentRenderTarget = reinterpret_cast<uintptr_t>(renderTarget11);
......@@ -1396,16 +1358,15 @@ TextureStorage11_EGLImage::TextureStorage11_EGLImage(Renderer11 *renderer,
TextureStorage11_EGLImage::~TextureStorage11_EGLImage()
{
SafeRelease(mSwizzleTexture);
}
gl::Error TextureStorage11_EGLImage::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_EGLImage::getResource(const TextureHelper11 **outResource)
{
ANGLE_TRY(checkForUpdatedRenderTarget());
RenderTarget11 *renderTarget11 = nullptr;
ANGLE_TRY(getImageRenderTarget(&renderTarget11));
*outResource = renderTarget11->getTexture();
*outResource = &renderTarget11->getTexture();
return gl::NoError();
}
......@@ -1416,7 +1377,7 @@ gl::Error TextureStorage11_EGLImage::getSRV(const gl::TextureState &textureState
return TextureStorage11::getSRV(textureState, outSRV);
}
gl::Error TextureStorage11_EGLImage::getMippedResource(ID3D11Resource **)
gl::Error TextureStorage11_EGLImage::getMippedResource(const TextureHelper11 **)
{
// This shouldn't be called unless the zero max LOD workaround is active.
// EGL images are unavailable in this configuration.
......@@ -1437,16 +1398,16 @@ gl::Error TextureStorage11_EGLImage::getRenderTarget(const gl::ImageIndex &index
gl::Error TextureStorage11_EGLImage::copyToStorage(TextureStorage *destStorage)
{
ID3D11Resource *sourceResouce = nullptr;
const TextureHelper11 *sourceResouce = nullptr;
ANGLE_TRY(getResource(&sourceResouce));
ASSERT(destStorage);
TextureStorage11_2D *dest11 = GetAs<TextureStorage11_2D>(destStorage);
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
ID3D11DeviceContext *immediateContext = mRenderer->getDeviceContext();
immediateContext->CopyResource(destResource, sourceResouce);
immediateContext->CopyResource(destResource->get(), sourceResouce->get());
dest11->markDirty();
......@@ -1476,20 +1437,20 @@ gl::Error TextureStorage11_EGLImage::useLevelZeroWorkaroundTexture(bool)
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureStorage11_EGLImage::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_EGLImage::getSwizzleTexture(const TextureHelper11 **outTexture)
{
ASSERT(outTexture);
if (!mSwizzleTexture)
if (!mSwizzleTexture.valid())
{
ID3D11Device *device = mRenderer->getDevice();
const auto &format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps());
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
desc.MipLevels = mMipLevels;
desc.ArraySize = 1;
desc.Format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps()).texFormat;
desc.Format = format.texFormat;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
......@@ -1497,19 +1458,11 @@ gl::Error TextureStorage11_EGLImage::getSwizzleTexture(ID3D11Resource **outTextu
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
HRESULT result = device->CreateTexture2D(&desc, nullptr, &mSwizzleTexture);
ASSERT(result == E_OUTOFMEMORY || SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create internal swizzle texture, result: 0x%X.", result);
}
d3d11::SetDebugName(mSwizzleTexture, "TexStorageEGLImage.SwizzleTexture");
ANGLE_TRY(mRenderer->allocateTexture(desc, format, &mSwizzleTexture));
mSwizzleTexture.setDebugName("TexStorageEGLImage.SwizzleTexture");
}
*outTexture = mSwizzleTexture;
*outTexture = &mSwizzleTexture;
return gl::NoError();
}
......@@ -1521,7 +1474,7 @@ gl::Error TextureStorage11_EGLImage::getSwizzleRenderTarget(int mipLevel,
if (!mSwizzleRenderTargets[mipLevel].valid())
{
ID3D11Resource *swizzleTexture = nullptr;
const TextureHelper11 *swizzleTexture = nullptr;
ANGLE_TRY(getSwizzleTexture(&swizzleTexture));
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
......@@ -1530,7 +1483,7 @@ gl::Error TextureStorage11_EGLImage::getSwizzleRenderTarget(int mipLevel,
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
rtvDesc.Texture2D.MipSlice = mTopLevel + mipLevel;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture,
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture.get(),
&mSwizzleRenderTargets[mipLevel]));
}
......@@ -1555,7 +1508,7 @@ gl::Error TextureStorage11_EGLImage::checkForUpdatedRenderTarget()
gl::Error TextureStorage11_EGLImage::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
ASSERT(baseLevel == 0);
......@@ -1572,7 +1525,7 @@ gl::Error TextureStorage11_EGLImage::createSRV(int baseLevel,
srvDesc.Texture2D.MostDetailedMip = mTopLevel + baseLevel;
srvDesc.Texture2D.MipLevels = mipLevels;
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), outSRV));
outSRV->setDebugName("TexStorageEGLImage.SRV");
}
else
......@@ -1610,10 +1563,10 @@ TextureStorage11_Cube::TextureStorage11_Cube(Renderer11 *renderer,
renderTarget,
levels),
internalformat),
mTexture(nullptr),
mLevelZeroTexture(nullptr),
mTexture(),
mLevelZeroTexture(),
mUseLevelZeroTexture(hintLevelZeroOnly && levels > 1),
mSwizzleTexture(nullptr)
mSwizzleTexture()
{
for (unsigned int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++)
{
......@@ -1659,10 +1612,6 @@ TextureStorage11_Cube::~TextureStorage11_Cube()
}
}
SafeRelease(mTexture);
SafeRelease(mSwizzleTexture);
SafeRelease(mLevelZeroTexture);
for (unsigned int face = 0; face < CUBE_FACE_COUNT; face++)
{
SafeDelete(mLevelZeroRenderTarget[face]);
......@@ -1709,36 +1658,36 @@ gl::Error TextureStorage11_Cube::copyToStorage(TextureStorage *destStorage)
// If either mTexture or mLevelZeroTexture exist, then we need to copy them into the
// corresponding textures in destStorage.
if (mTexture)
if (mTexture.valid())
{
ANGLE_TRY(dest11->useLevelZeroWorkaroundTexture(false));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
immediateContext->CopyResource(destResource, mTexture);
immediateContext->CopyResource(destResource->get(), mTexture.get());
}
if (mLevelZeroTexture)
if (mLevelZeroTexture.valid())
{
ANGLE_TRY(dest11->useLevelZeroWorkaroundTexture(true));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
immediateContext->CopyResource(destResource, mLevelZeroTexture);
immediateContext->CopyResource(destResource->get(), mLevelZeroTexture.get());
}
}
else
{
ID3D11Resource *sourceResouce = nullptr;
const TextureHelper11 *sourceResouce = nullptr;
ANGLE_TRY(getResource(&sourceResouce));
ID3D11Resource *destResource = nullptr;
const TextureHelper11 *destResource = nullptr;
ANGLE_TRY(dest11->getResource(&destResource));
ID3D11DeviceContext *immediateContext = mRenderer->getDeviceContext();
immediateContext->CopyResource(destResource, sourceResouce);
immediateContext->CopyResource(destResource->get(), sourceResouce->get());
}
dest11->markDirty();
......@@ -1750,18 +1699,19 @@ gl::Error TextureStorage11_Cube::useLevelZeroWorkaroundTexture(bool useLevelZero
{
if (useLevelZeroTexture && mMipLevels > 1)
{
if (!mUseLevelZeroTexture && mTexture)
if (!mUseLevelZeroTexture && mTexture.valid())
{
ANGLE_TRY(ensureTextureExists(1));
// Pull data back from the mipped texture if necessary.
ASSERT(mLevelZeroTexture);
ASSERT(mLevelZeroTexture.valid());
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
for (int face = 0; face < 6; face++)
{
context->CopySubresourceRegion(mLevelZeroTexture, D3D11CalcSubresource(0, face, 1),
0, 0, 0, mTexture, face * mMipLevels, nullptr);
context->CopySubresourceRegion(mLevelZeroTexture.get(),
D3D11CalcSubresource(0, face, 1), 0, 0, 0,
mTexture.get(), face * mMipLevels, nullptr);
}
}
......@@ -1769,18 +1719,19 @@ gl::Error TextureStorage11_Cube::useLevelZeroWorkaroundTexture(bool useLevelZero
}
else
{
if (mUseLevelZeroTexture && mLevelZeroTexture)
if (mUseLevelZeroTexture && mLevelZeroTexture.valid())
{
ANGLE_TRY(ensureTextureExists(mMipLevels));
// Pull data back from the level zero texture if necessary.
ASSERT(mTexture);
ASSERT(mTexture.valid());
ID3D11DeviceContext *context = mRenderer->getDeviceContext();
for (int face = 0; face < 6; face++)
{
context->CopySubresourceRegion(mTexture, D3D11CalcSubresource(0, face, mMipLevels),
0, 0, 0, mLevelZeroTexture, face, nullptr);
context->CopySubresourceRegion(mTexture.get(),
D3D11CalcSubresource(0, face, mMipLevels), 0, 0, 0,
mLevelZeroTexture.get(), face, nullptr);
}
}
......@@ -1864,29 +1815,29 @@ gl::Error TextureStorage11_Cube::releaseAssociatedImage(const gl::ImageIndex &in
return gl::NoError();
}
gl::Error TextureStorage11_Cube::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_Cube::getResource(const TextureHelper11 **outResource)
{
if (mUseLevelZeroTexture && mMipLevels > 1)
{
ANGLE_TRY(ensureTextureExists(1));
*outResource = mLevelZeroTexture;
*outResource = &mLevelZeroTexture;
return gl::NoError();
}
else
{
ANGLE_TRY(ensureTextureExists(mMipLevels));
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
}
gl::Error TextureStorage11_Cube::getMippedResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_Cube::getMippedResource(const TextureHelper11 **outResource)
{
// This shouldn't be called unless the zero max LOD workaround is active.
ASSERT(mRenderer->getWorkarounds().zeroMaxLodWorkaround);
ANGLE_TRY(ensureTextureExists(mMipLevels));
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
......@@ -1896,16 +1847,14 @@ gl::Error TextureStorage11_Cube::ensureTextureExists(int mipLevels)
bool useLevelZeroTexture = mRenderer->getWorkarounds().zeroMaxLodWorkaround
? (mipLevels == 1) && (mMipLevels > 1)
: false;
ID3D11Texture2D **outputTexture = useLevelZeroTexture ? &mLevelZeroTexture : &mTexture;
TextureHelper11 *outputTexture = useLevelZeroTexture ? &mLevelZeroTexture : &mTexture;
// if the size is not positive this should be treated as an incomplete texture
// we handle that here by skipping the d3d texture creation
if (*outputTexture == nullptr && mTextureWidth > 0 && mTextureHeight > 0)
if (!outputTexture->valid() && mTextureWidth > 0 && mTextureHeight > 0)
{
ASSERT(mMipLevels > 0);
ID3D11Device *device = mRenderer->getDevice();
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
......@@ -1919,29 +1868,14 @@ gl::Error TextureStorage11_Cube::ensureTextureExists(int mipLevels)
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE | getMiscFlags();
HRESULT result = device->CreateTexture2D(&desc, nullptr, outputTexture);
// this can happen from windows TDR
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create cube texture storage, result: 0x%X.", result);
}
else if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create cube texture storage, result: 0x%X.", result);
}
d3d11::SetDebugName(*outputTexture, "TexStorageCube.Texture");
ANGLE_TRY(mRenderer->allocateTexture(desc, mFormatInfo, outputTexture));
outputTexture->setDebugName("TexStorageCube.Texture");
}
return gl::NoError();
}
gl::Error TextureStorage11_Cube::createRenderTargetSRV(ID3D11Resource *texture,
gl::Error TextureStorage11_Cube::createRenderTargetSRV(const TextureHelper11 &texture,
const gl::ImageIndex &index,
DXGI_FORMAT resourceFormat,
d3d11::SharedSRV *srv) const
......@@ -1963,7 +1897,7 @@ gl::Error TextureStorage11_Cube::createRenderTargetSRV(ID3D11Resource *texture,
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
}
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture, srv));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), srv));
return gl::NoError();
}
......@@ -1978,7 +1912,7 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
if (!mRenderTarget[faceIndex][level])
{
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
ANGLE_TRY(getResource(&texture));
if (mUseLevelZeroTexture)
......@@ -1993,7 +1927,7 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
rtvDesc.Texture2DArray.ArraySize = 1;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mLevelZeroTexture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mLevelZeroTexture.get(), &rtv));
mLevelZeroRenderTarget[faceIndex] = new TextureRenderTarget11(
std::move(rtv), mLevelZeroTexture, d3d11::SharedSRV(), d3d11::SharedSRV(),
......@@ -2007,11 +1941,11 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
}
d3d11::SharedSRV srv;
ANGLE_TRY(createRenderTargetSRV(texture, index, mFormatInfo.srvFormat, &srv));
ANGLE_TRY(createRenderTargetSRV(*texture, index, mFormatInfo.srvFormat, &srv));
d3d11::SharedSRV blitSRV;
if (mFormatInfo.blitSRVFormat != mFormatInfo.srvFormat)
{
ANGLE_TRY(createRenderTargetSRV(texture, index, mFormatInfo.blitSRVFormat, &blitSRV));
ANGLE_TRY(createRenderTargetSRV(*texture, index, mFormatInfo.blitSRVFormat, &blitSRV));
}
else
{
......@@ -2030,11 +1964,11 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
rtvDesc.Texture2DArray.ArraySize = 1;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture->get(), &rtv));
rtv.setDebugName("TexStorageCube.RenderTargetRTV");
mRenderTarget[faceIndex][level] = new TextureRenderTarget11(
std::move(rtv), texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
std::move(rtv), *texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(level), getLevelHeight(level), 1, 0);
}
else if (mFormatInfo.dsvFormat != DXGI_FORMAT_UNKNOWN)
......@@ -2048,11 +1982,11 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
dsvDesc.Texture2DArray.ArraySize = 1;
d3d11::DepthStencilView dsv;
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture, &dsv));
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture->get(), &dsv));
dsv.setDebugName("TexStorageCube.RenderTargetDSV");
mRenderTarget[faceIndex][level] = new TextureRenderTarget11(
std::move(dsv), texture, srv, mFormatInfo.internalFormat, getFormatSet(),
std::move(dsv), *texture, srv, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(level), getLevelHeight(level), 1, 0);
}
else
......@@ -2069,7 +2003,7 @@ gl::Error TextureStorage11_Cube::getRenderTarget(const gl::ImageIndex &index,
gl::Error TextureStorage11_Cube::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
ASSERT(outSRV);
......@@ -2095,7 +2029,7 @@ gl::Error TextureStorage11_Cube::createSRV(int baseLevel,
srvDesc.TextureCube.MostDetailedMip = mTopLevel + baseLevel;
}
ID3D11Resource *srvTexture = texture;
const TextureHelper11 *srvTexture = &texture;
if (mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
......@@ -2107,37 +2041,37 @@ gl::Error TextureStorage11_Cube::createSRV(int baseLevel,
if (mipLevels == 1 && mMipLevels > 1)
{
// We must use a SRV on the level-zero-only texture.
ASSERT(mLevelZeroTexture != nullptr && texture == mLevelZeroTexture);
srvTexture = mLevelZeroTexture;
ASSERT(mLevelZeroTexture.valid() && texture == mLevelZeroTexture);
srvTexture = &mLevelZeroTexture;
}
else
{
ASSERT(mipLevels == static_cast<int>(mMipLevels));
ASSERT(mTexture != nullptr && texture == mTexture);
srvTexture = mTexture;
ASSERT(mTexture.valid() && texture == mTexture);
srvTexture = &mTexture;
}
}
ANGLE_TRY(mRenderer->allocateResource(srvDesc, srvTexture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, srvTexture->get(), outSRV));
outSRV->setDebugName("TexStorageCube.SRV");
return gl::NoError();
}
gl::Error TextureStorage11_Cube::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_Cube::getSwizzleTexture(const TextureHelper11 **outTexture)
{
ASSERT(outTexture);
if (!mSwizzleTexture)
if (!mSwizzleTexture.valid())
{
ID3D11Device *device = mRenderer->getDevice();
const auto &format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps());
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
desc.MipLevels = mMipLevels;
desc.ArraySize = CUBE_FACE_COUNT;
desc.Format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps()).texFormat;
desc.Format = format.texFormat;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
......@@ -2145,19 +2079,11 @@ gl::Error TextureStorage11_Cube::getSwizzleTexture(ID3D11Resource **outTexture)
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
HRESULT result = device->CreateTexture2D(&desc, nullptr, &mSwizzleTexture);
ASSERT(result == E_OUTOFMEMORY || SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create internal swizzle texture, result: 0x%X.", result);
}
d3d11::SetDebugName(*outTexture, "TexStorageCube.SwizzleTexture");
ANGLE_TRY(mRenderer->allocateTexture(desc, format, &mSwizzleTexture));
mSwizzleTexture.setDebugName("TexStorageCube.SwizzleTexture");
}
*outTexture = mSwizzleTexture;
*outTexture = &mSwizzleTexture;
return gl::NoError();
}
......@@ -2169,7 +2095,7 @@ gl::Error TextureStorage11_Cube::getSwizzleRenderTarget(int mipLevel,
if (!mSwizzleRenderTargets[mipLevel].valid())
{
ID3D11Resource *swizzleTexture = nullptr;
const TextureHelper11 *swizzleTexture = nullptr;
ANGLE_TRY(getSwizzleTexture(&swizzleTexture));
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
......@@ -2180,7 +2106,7 @@ gl::Error TextureStorage11_Cube::getSwizzleRenderTarget(int mipLevel,
rtvDesc.Texture2DArray.FirstArraySlice = 0;
rtvDesc.Texture2DArray.ArraySize = CUBE_FACE_COUNT;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture,
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture.get(),
&mSwizzleRenderTargets[mipLevel]));
}
......@@ -2190,12 +2116,8 @@ gl::Error TextureStorage11_Cube::getSwizzleRenderTarget(int mipLevel,
gl::Error TextureStorage11::initDropStencilTexture(const gl::ImageIndexIterator &it)
{
ID3D11Resource *resource = nullptr;
ANGLE_TRY(getResource(&resource));
TextureHelper11 sourceTexture = TextureHelper11::MakeAndReference(resource, mFormatInfo);
TextureHelper11 destTexture = TextureHelper11::MakeAndReference(
mDropStencilTexture,
d3d11::Format::Get(GL_DEPTH_COMPONENT32F, mRenderer->getRenderer11DeviceCaps()));
const TextureHelper11 *sourceTexture = nullptr;
ANGLE_TRY(getResource(&sourceTexture));
gl::ImageIndexIterator itCopy = it;
......@@ -2206,9 +2128,9 @@ gl::Error TextureStorage11::initDropStencilTexture(const gl::ImageIndexIterator
1);
gl::Extents wholeSize(wholeArea.width, wholeArea.height, 1);
UINT subresource = getSubresourceIndex(index);
ANGLE_TRY(mRenderer->getBlitter()->copyDepthStencil(sourceTexture, subresource, wholeArea,
wholeSize, destTexture, subresource,
wholeArea, wholeSize, nullptr));
ANGLE_TRY(mRenderer->getBlitter()->copyDepthStencil(
*sourceTexture, subresource, wholeArea, wholeSize, mDropStencilTexture, subresource,
wholeArea, wholeSize, nullptr));
}
return gl::NoError();
......@@ -2216,7 +2138,7 @@ gl::Error TextureStorage11::initDropStencilTexture(const gl::ImageIndexIterator
gl::ErrorOrResult<TextureStorage11::DropStencil> TextureStorage11_Cube::ensureDropStencilTexture()
{
if (mDropStencilTexture)
if (mDropStencilTexture.valid())
{
return DropStencil::ALREADY_EXISTS;
}
......@@ -2234,14 +2156,10 @@ gl::ErrorOrResult<TextureStorage11::DropStencil> TextureStorage11_Cube::ensureDr
dropDesc.Usage = D3D11_USAGE_DEFAULT;
dropDesc.Width = mTextureWidth;
ID3D11Device *device = mRenderer->getDevice();
HRESULT hr = device->CreateTexture2D(&dropDesc, nullptr, &mDropStencilTexture);
if (FAILED(hr))
{
return gl::InternalError() << "Error creating drop stencil texture.";
}
d3d11::SetDebugName(mDropStencilTexture, "TexStorageCube.DropStencil");
const auto &format =
d3d11::Format::Get(GL_DEPTH_COMPONENT32F, mRenderer->getRenderer11DeviceCaps());
ANGLE_TRY(mRenderer->allocateTexture(dropDesc, format, &mDropStencilTexture));
mDropStencilTexture.setDebugName("TexStorageCube.DropStencil");
ANGLE_TRY(initDropStencilTexture(gl::ImageIndexIterator::MakeCube(0, mMipLevels)));
......@@ -2264,9 +2182,6 @@ TextureStorage11_3D::TextureStorage11_3D(Renderer11 *renderer,
levels),
internalformat)
{
mTexture = nullptr;
mSwizzleTexture = nullptr;
for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
mAssociatedImages[i] = nullptr;
......@@ -2296,9 +2211,6 @@ TextureStorage11_3D::~TextureStorage11_3D()
}
}
SafeRelease(mTexture);
SafeRelease(mSwizzleTexture);
for (RenderTargetMap::iterator i = mLevelLayerRenderTargets.begin();
i != mLevelLayerRenderTargets.end(); i++)
{
......@@ -2371,16 +2283,14 @@ gl::Error TextureStorage11_3D::releaseAssociatedImage(const gl::ImageIndex &inde
return gl::NoError();
}
gl::Error TextureStorage11_3D::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_3D::getResource(const TextureHelper11 **outResource)
{
// If the width, height or depth are not positive this should be treated as an incomplete
// texture. We handle that here by skipping the d3d texture creation.
if (mTexture == nullptr && mTextureWidth > 0 && mTextureHeight > 0 && mTextureDepth > 0)
if (!mTexture.valid() && mTextureWidth > 0 && mTextureHeight > 0 && mTextureDepth > 0)
{
ASSERT(mMipLevels > 0);
ID3D11Device *device = mRenderer->getDevice();
D3D11_TEXTURE3D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
......@@ -2392,33 +2302,18 @@ gl::Error TextureStorage11_3D::getResource(ID3D11Resource **outResource)
desc.CPUAccessFlags = 0;
desc.MiscFlags = getMiscFlags();
HRESULT result = device->CreateTexture3D(&desc, nullptr, &mTexture);
// this can happen from windows TDR
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create 3D texture storage, result: 0x%X.",
result);
}
else if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create 3D texture storage, result: 0x%X.",
result);
}
d3d11::SetDebugName(mTexture, "TexStorage3D.Texture");
ANGLE_TRY(mRenderer->allocateTexture(desc, mFormatInfo, &mTexture));
mTexture.setDebugName("TexStorage3D.Texture");
}
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
gl::Error TextureStorage11_3D::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
ASSERT(outSRV);
......@@ -2429,7 +2324,7 @@ gl::Error TextureStorage11_3D::createSRV(int baseLevel,
srvDesc.Texture3D.MostDetailedMip = baseLevel;
srvDesc.Texture3D.MipLevels = mipLevels;
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), outSRV));
outSRV->setDebugName("TexStorage3D.SRV");
return gl::NoError();
......@@ -2446,7 +2341,7 @@ gl::Error TextureStorage11_3D::getRenderTarget(const gl::ImageIndex &index, Rend
{
if (!mLevelRenderTargets[mipLevel])
{
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
ANGLE_TRY(getResource(&texture));
const d3d11::SharedSRV *srv = nullptr;
......@@ -2463,12 +2358,13 @@ gl::Error TextureStorage11_3D::getRenderTarget(const gl::ImageIndex &index, Rend
rtvDesc.Texture3D.WSize = static_cast<UINT>(-1);
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture->get(), &rtv));
rtv.setDebugName("TexStorage3D.RTV");
mLevelRenderTargets[mipLevel] = new TextureRenderTarget11(
std::move(rtv), texture, *srv, *blitSRV, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(mipLevel), getLevelHeight(mipLevel), getLevelDepth(mipLevel), 0);
std::move(rtv), *texture, *srv, *blitSRV, mFormatInfo.internalFormat,
getFormatSet(), getLevelWidth(mipLevel), getLevelHeight(mipLevel),
getLevelDepth(mipLevel), 0);
}
ASSERT(outRT);
......@@ -2481,7 +2377,7 @@ gl::Error TextureStorage11_3D::getRenderTarget(const gl::ImageIndex &index, Rend
LevelLayerKey key(mipLevel, layer);
if (mLevelLayerRenderTargets.find(key) == mLevelLayerRenderTargets.end())
{
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
ANGLE_TRY(getResource(&texture));
// TODO, what kind of SRV is expected here?
......@@ -2496,11 +2392,11 @@ gl::Error TextureStorage11_3D::getRenderTarget(const gl::ImageIndex &index, Rend
rtvDesc.Texture3D.WSize = 1;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture->get(), &rtv));
rtv.setDebugName("TexStorage3D.LayerRTV");
mLevelLayerRenderTargets[key] = new TextureRenderTarget11(
std::move(rtv), texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
std::move(rtv), *texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(mipLevel), getLevelHeight(mipLevel), 1, 0);
}
......@@ -2509,38 +2405,30 @@ gl::Error TextureStorage11_3D::getRenderTarget(const gl::ImageIndex &index, Rend
return gl::NoError();
}
gl::Error TextureStorage11_3D::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_3D::getSwizzleTexture(const TextureHelper11 **outTexture)
{
ASSERT(outTexture);
if (!mSwizzleTexture)
if (!mSwizzleTexture.valid())
{
ID3D11Device *device = mRenderer->getDevice();
const auto &format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps());
D3D11_TEXTURE3D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
desc.Depth = mTextureDepth;
desc.MipLevels = mMipLevels;
desc.Format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps()).texFormat;
desc.Format = format.texFormat;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
HRESULT result = device->CreateTexture3D(&desc, nullptr, &mSwizzleTexture);
ASSERT(result == E_OUTOFMEMORY || SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create internal swizzle texture, result: 0x%X.", result);
}
d3d11::SetDebugName(mSwizzleTexture, "TexStorage3D.SwizzleTexture");
ANGLE_TRY(mRenderer->allocateTexture(desc, format, &mSwizzleTexture));
mSwizzleTexture.setDebugName("TexStorage3D.SwizzleTexture");
}
*outTexture = mSwizzleTexture;
*outTexture = &mSwizzleTexture;
return gl::NoError();
}
......@@ -2552,7 +2440,7 @@ gl::Error TextureStorage11_3D::getSwizzleRenderTarget(int mipLevel,
if (!mSwizzleRenderTargets[mipLevel].valid())
{
ID3D11Resource *swizzleTexture = nullptr;
const TextureHelper11 *swizzleTexture = nullptr;
ANGLE_TRY(getSwizzleTexture(&swizzleTexture));
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
......@@ -2563,7 +2451,7 @@ gl::Error TextureStorage11_3D::getSwizzleRenderTarget(int mipLevel,
rtvDesc.Texture3D.FirstWSlice = 0;
rtvDesc.Texture3D.WSize = static_cast<UINT>(-1);
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture,
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture.get(),
&mSwizzleRenderTargets[mipLevel]));
mSwizzleRenderTargets[mipLevel].setDebugName("TexStorage3D.SwizzleRTV");
}
......@@ -2588,9 +2476,6 @@ TextureStorage11_2DArray::TextureStorage11_2DArray(Renderer11 *renderer,
levels),
internalformat)
{
mTexture = nullptr;
mSwizzleTexture = nullptr;
// adjust size if needed for compressed textures
d3d11::MakeValidSize(false, mFormatInfo.texFormat, &width, &height, &mTopLevel);
......@@ -2615,9 +2500,6 @@ TextureStorage11_2DArray::~TextureStorage11_2DArray()
}
mAssociatedImages.clear();
SafeRelease(mTexture);
SafeRelease(mSwizzleTexture);
for (RenderTargetMap::iterator i = mRenderTargets.begin(); i != mRenderTargets.end(); i++)
{
SafeDelete(i->second);
......@@ -2695,16 +2577,14 @@ gl::Error TextureStorage11_2DArray::releaseAssociatedImage(const gl::ImageIndex
return gl::NoError();
}
gl::Error TextureStorage11_2DArray::getResource(ID3D11Resource **outResource)
gl::Error TextureStorage11_2DArray::getResource(const TextureHelper11 **outResource)
{
// if the width, height or depth is not positive this should be treated as an incomplete texture
// we handle that here by skipping the d3d texture creation
if (mTexture == nullptr && mTextureWidth > 0 && mTextureHeight > 0 && mTextureDepth > 0)
if (!mTexture.valid() && mTextureWidth > 0 && mTextureHeight > 0 && mTextureDepth > 0)
{
ASSERT(mMipLevels > 0);
ID3D11Device *device = mRenderer->getDevice();
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
......@@ -2718,33 +2598,18 @@ gl::Error TextureStorage11_2DArray::getResource(ID3D11Resource **outResource)
desc.CPUAccessFlags = 0;
desc.MiscFlags = getMiscFlags();
HRESULT result = device->CreateTexture2D(&desc, nullptr, &mTexture);
// this can happen from windows TDR
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create 2D array texture storage, result: 0x%X.", result);
}
else if (FAILED(result))
{
ASSERT(result == E_OUTOFMEMORY);
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create 2D array texture storage, result: 0x%X.", result);
}
d3d11::SetDebugName(mTexture, "TexStorage2DArray.Texture");
ANGLE_TRY(mRenderer->allocateTexture(desc, mFormatInfo, &mTexture));
mTexture.setDebugName("TexStorage2DArray.Texture");
}
*outResource = mTexture;
*outResource = &mTexture;
return gl::NoError();
}
gl::Error TextureStorage11_2DArray::createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const
{
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
......@@ -2755,13 +2620,13 @@ gl::Error TextureStorage11_2DArray::createSRV(int baseLevel,
srvDesc.Texture2DArray.FirstArraySlice = 0;
srvDesc.Texture2DArray.ArraySize = mTextureDepth;
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture, outSRV));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), outSRV));
outSRV->setDebugName("TexStorage2DArray.SRV");
return gl::NoError();
}
gl::Error TextureStorage11_2DArray::createRenderTargetSRV(ID3D11Resource *texture,
gl::Error TextureStorage11_2DArray::createRenderTargetSRV(const TextureHelper11 &texture,
const gl::ImageIndex &index,
DXGI_FORMAT resourceFormat,
d3d11::SharedSRV *srv) const
......@@ -2774,7 +2639,7 @@ gl::Error TextureStorage11_2DArray::createRenderTargetSRV(ID3D11Resource *textur
srvDesc.Texture2DArray.FirstArraySlice = index.layerIndex;
srvDesc.Texture2DArray.ArraySize = 1;
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture, srv));
ANGLE_TRY(mRenderer->allocateResource(srvDesc, texture.get(), srv));
return gl::NoError();
}
......@@ -2792,14 +2657,14 @@ gl::Error TextureStorage11_2DArray::getRenderTarget(const gl::ImageIndex &index,
LevelLayerKey key(mipLevel, layer);
if (mRenderTargets.find(key) == mRenderTargets.end())
{
ID3D11Resource *texture = nullptr;
const TextureHelper11 *texture = nullptr;
ANGLE_TRY(getResource(&texture));
d3d11::SharedSRV srv;
ANGLE_TRY(createRenderTargetSRV(texture, index, mFormatInfo.srvFormat, &srv));
ANGLE_TRY(createRenderTargetSRV(*texture, index, mFormatInfo.srvFormat, &srv));
d3d11::SharedSRV blitSRV;
if (mFormatInfo.blitSRVFormat != mFormatInfo.srvFormat)
{
ANGLE_TRY(createRenderTargetSRV(texture, index, mFormatInfo.blitSRVFormat, &blitSRV));
ANGLE_TRY(createRenderTargetSRV(*texture, index, mFormatInfo.blitSRVFormat, &blitSRV));
}
else
{
......@@ -2818,11 +2683,11 @@ gl::Error TextureStorage11_2DArray::getRenderTarget(const gl::ImageIndex &index,
rtvDesc.Texture2DArray.ArraySize = 1;
d3d11::RenderTargetView rtv;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture, &rtv));
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, texture->get(), &rtv));
rtv.setDebugName("TexStorage2DArray.RenderTargetRTV");
mRenderTargets[key] = new TextureRenderTarget11(
std::move(rtv), texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
std::move(rtv), *texture, srv, blitSRV, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(mipLevel), getLevelHeight(mipLevel), 1, 0);
}
else
......@@ -2838,11 +2703,11 @@ gl::Error TextureStorage11_2DArray::getRenderTarget(const gl::ImageIndex &index,
dsvDesc.Flags = 0;
d3d11::DepthStencilView dsv;
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture, &dsv));
ANGLE_TRY(mRenderer->allocateResource(dsvDesc, texture->get(), &dsv));
dsv.setDebugName("TexStorage2DArray.RenderTargetDSV");
mRenderTargets[key] = new TextureRenderTarget11(
std::move(dsv), texture, srv, mFormatInfo.internalFormat, getFormatSet(),
std::move(dsv), *texture, srv, mFormatInfo.internalFormat, getFormatSet(),
getLevelWidth(mipLevel), getLevelHeight(mipLevel), 1, 0);
}
}
......@@ -2852,18 +2717,18 @@ gl::Error TextureStorage11_2DArray::getRenderTarget(const gl::ImageIndex &index,
return gl::NoError();
}
gl::Error TextureStorage11_2DArray::getSwizzleTexture(ID3D11Resource **outTexture)
gl::Error TextureStorage11_2DArray::getSwizzleTexture(const TextureHelper11 **outTexture)
{
if (!mSwizzleTexture)
if (!mSwizzleTexture.valid())
{
ID3D11Device *device = mRenderer->getDevice();
const auto &format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps());
D3D11_TEXTURE2D_DESC desc;
desc.Width = mTextureWidth;
desc.Height = mTextureHeight;
desc.MipLevels = mMipLevels;
desc.ArraySize = mTextureDepth;
desc.Format = mFormatInfo.getSwizzleFormat(mRenderer->getRenderer11DeviceCaps()).texFormat;
desc.Format = format.texFormat;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
......@@ -2871,19 +2736,11 @@ gl::Error TextureStorage11_2DArray::getSwizzleTexture(ID3D11Resource **outTextur
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
HRESULT result = device->CreateTexture2D(&desc, nullptr, &mSwizzleTexture);
ASSERT(result == E_OUTOFMEMORY || SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY,
"Failed to create internal swizzle texture, result: 0x%X.", result);
}
d3d11::SetDebugName(*outTexture, "TexStorage2DArray.SwizzleTexture");
ANGLE_TRY(mRenderer->allocateTexture(desc, format, &mSwizzleTexture));
mSwizzleTexture.setDebugName("TexStorage2DArray.SwizzleTexture");
}
*outTexture = mSwizzleTexture;
*outTexture = &mSwizzleTexture;
return gl::NoError();
}
......@@ -2895,7 +2752,7 @@ gl::Error TextureStorage11_2DArray::getSwizzleRenderTarget(int mipLevel,
if (!mSwizzleRenderTargets[mipLevel].valid())
{
ID3D11Resource *swizzleTexture = nullptr;
const TextureHelper11 *swizzleTexture = nullptr;
ANGLE_TRY(getSwizzleTexture(&swizzleTexture));
D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
......@@ -2906,7 +2763,7 @@ gl::Error TextureStorage11_2DArray::getSwizzleRenderTarget(int mipLevel,
rtvDesc.Texture2DArray.FirstArraySlice = 0;
rtvDesc.Texture2DArray.ArraySize = mTextureDepth;
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture,
ANGLE_TRY(mRenderer->allocateResource(rtvDesc, mSwizzleTexture.get(),
&mSwizzleRenderTargets[mipLevel]));
}
......@@ -2917,7 +2774,7 @@ gl::Error TextureStorage11_2DArray::getSwizzleRenderTarget(int mipLevel,
gl::ErrorOrResult<TextureStorage11::DropStencil>
TextureStorage11_2DArray::ensureDropStencilTexture()
{
if (mDropStencilTexture)
if (mDropStencilTexture.valid())
{
return DropStencil::ALREADY_EXISTS;
}
......@@ -2935,14 +2792,10 @@ TextureStorage11_2DArray::ensureDropStencilTexture()
dropDesc.Usage = D3D11_USAGE_DEFAULT;
dropDesc.Width = mTextureWidth;
ID3D11Device *device = mRenderer->getDevice();
HRESULT hr = device->CreateTexture2D(&dropDesc, nullptr, &mDropStencilTexture);
if (FAILED(hr))
{
return gl::InternalError() << "Error creating drop stencil texture.";
}
d3d11::SetDebugName(mDropStencilTexture, "TexStorage2DArray.DropStencil");
const auto &format =
d3d11::Format::Get(GL_DEPTH_COMPONENT32F, mRenderer->getRenderer11DeviceCaps());
ANGLE_TRY(mRenderer->allocateTexture(dropDesc, format, &mDropStencilTexture));
mDropStencilTexture.setDebugName("TexStorage2DArray.DropStencil");
std::vector<GLsizei> layerCounts(mMipLevels, mTextureDepth);
......
......@@ -13,7 +13,7 @@
#include "libANGLE/Error.h"
#include "libANGLE/Texture.h"
#include "libANGLE/renderer/d3d/TextureStorage.h"
#include "libANGLE/renderer/d3d/d3d11/ResourceManager11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
#include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
#include <array>
......@@ -50,11 +50,15 @@ class TextureStorage11 : public TextureStorage
void markLevelDirty(int mipLevel);
void markDirty();
gl::Error updateSubresourceLevel(ID3D11Resource *texture, unsigned int sourceSubresource,
const gl::ImageIndex &index, const gl::Box &copyArea);
gl::Error updateSubresourceLevel(const TextureHelper11 &texture,
unsigned int sourceSubresource,
const gl::ImageIndex &index,
const gl::Box &copyArea);
gl::Error copySubresourceLevel(ID3D11Resource* dstTexture, unsigned int dstSubresource,
const gl::ImageIndex &index, const gl::Box &region);
gl::Error copySubresourceLevel(const TextureHelper11 &dstTexture,
unsigned int dstSubresource,
const gl::ImageIndex &index,
const gl::Box &region);
// TextureStorage virtual functions
int getTopLevel() const override;
......@@ -74,7 +78,7 @@ class TextureStorage11 : public TextureStorage
virtual gl::Error getSRV(const gl::TextureState &textureState, d3d11::SharedSRV *outSRV);
virtual UINT getSubresourceIndex(const gl::ImageIndex &index) const;
virtual gl::Error getResource(ID3D11Resource **outResource) = 0;
virtual gl::Error getResource(const TextureHelper11 **outResource) = 0;
virtual void associateImage(Image11* image, const gl::ImageIndex &index) = 0;
virtual void disassociateImage(const gl::ImageIndex &index, Image11* expectedImage) = 0;
virtual void verifyAssociatedImageValid(const gl::ImageIndex &index,
......@@ -88,9 +92,12 @@ class TextureStorage11 : public TextureStorage
int getLevelDepth(int mipLevel) const;
// Some classes (e.g. TextureStorage11_2D) will override getMippedResource.
virtual gl::Error getMippedResource(ID3D11Resource **outResource) { return getResource(outResource); }
virtual gl::Error getMippedResource(const TextureHelper11 **outResource)
{
return getResource(outResource);
}
virtual gl::Error getSwizzleTexture(ID3D11Resource **outTexture) = 0;
virtual gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) = 0;
virtual gl::Error getSwizzleRenderTarget(int mipLevel,
const d3d11::RenderTargetView **outRTV) = 0;
gl::Error getSRVLevel(int mipLevel, bool blitSRV, const d3d11::SharedSRV **outSRV);
......@@ -108,7 +115,7 @@ class TextureStorage11 : public TextureStorage
virtual gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const = 0;
void verifySwizzleExists(const gl::SwizzleState &swizzleState);
......@@ -126,7 +133,7 @@ class TextureStorage11 : public TextureStorage
unsigned int mTextureDepth;
gl::SwizzleState mSwizzleCache[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
ID3D11Texture2D *mDropStencilTexture;
TextureHelper11 mDropStencilTexture;
private:
const UINT mBindFlags;
......@@ -159,8 +166,8 @@ class TextureStorage11_2D : public TextureStorage11
TextureStorage11_2D(Renderer11 *renderer, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels, bool hintLevelZeroOnly = false);
~TextureStorage11_2D() override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getMippedResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
gl::Error getMippedResource(const TextureHelper11 **outResource) override;
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
gl::Error copyToStorage(TextureStorage *destStorage) override;
......@@ -173,7 +180,7 @@ class TextureStorage11_2D : public TextureStorage11
gl::Error useLevelZeroWorkaroundTexture(bool useLevelZeroTexture) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
gl::ErrorOrResult<DropStencil> ensureDropStencilTexture() override;
......@@ -184,10 +191,10 @@ class TextureStorage11_2D : public TextureStorage11
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
ID3D11Texture2D *mTexture;
TextureHelper11 mTexture;
RenderTarget11 *mRenderTarget[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
bool mHasKeyedMutex;
......@@ -200,12 +207,12 @@ class TextureStorage11_2D : public TextureStorage11
// One example of this is an application that creates a texture, calls glGenerateMipmap, and then disables mipmaps on the texture.
// A more likely example is an app that creates an empty texture, renders to it, and then calls glGenerateMipmap
// TODO: In this rendering scenario, release the mLevelZeroTexture after mTexture has been created to save memory.
ID3D11Texture2D *mLevelZeroTexture;
TextureHelper11 mLevelZeroTexture;
RenderTarget11 *mLevelZeroRenderTarget;
bool mUseLevelZeroTexture;
// Swizzle-related variables
ID3D11Texture2D *mSwizzleTexture;
TextureHelper11 mSwizzleTexture;
d3d11::RenderTargetView mSwizzleRenderTargets[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
Image11 *mAssociatedImages[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
......@@ -219,8 +226,8 @@ class TextureStorage11_External : public TextureStorage11
const egl::Stream::GLTextureDescription &glDesc);
~TextureStorage11_External() override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getMippedResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
gl::Error getMippedResource(const TextureHelper11 **outResource) override;
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
gl::Error copyToStorage(TextureStorage *destStorage) override;
......@@ -231,17 +238,17 @@ class TextureStorage11_External : public TextureStorage11
gl::Error releaseAssociatedImage(const gl::ImageIndex &index, Image11 *incomingImage) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
private:
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
ID3D11Texture2D *mTexture;
TextureHelper11 mTexture;
int mSubresourceIndex;
bool mHasKeyedMutex;
......@@ -256,9 +263,9 @@ class TextureStorage11_EGLImage final : public TextureStorage11
RenderTarget11 *renderTarget11);
~TextureStorage11_EGLImage() override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
gl::Error getSRV(const gl::TextureState &textureState, d3d11::SharedSRV *outSRV) override;
gl::Error getMippedResource(ID3D11Resource **outResource) override;
gl::Error getMippedResource(const TextureHelper11 **outResource) override;
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
gl::Error copyToStorage(TextureStorage *destStorage) override;
......@@ -271,7 +278,7 @@ class TextureStorage11_EGLImage final : public TextureStorage11
gl::Error useLevelZeroWorkaroundTexture(bool useLevelZeroTexture) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
private:
......@@ -282,7 +289,7 @@ class TextureStorage11_EGLImage final : public TextureStorage11
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
gl::Error getImageRenderTarget(RenderTarget11 **outRT) const;
......@@ -291,7 +298,7 @@ class TextureStorage11_EGLImage final : public TextureStorage11
uintptr_t mCurrentRenderTarget;
// Swizzle-related variables
ID3D11Texture2D *mSwizzleTexture;
TextureHelper11 mSwizzleTexture;
std::vector<d3d11::RenderTargetView> mSwizzleRenderTargets;
};
......@@ -303,8 +310,8 @@ class TextureStorage11_Cube : public TextureStorage11
UINT getSubresourceIndex(const gl::ImageIndex &index) const override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getMippedResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
gl::Error getMippedResource(const TextureHelper11 **outResource) override;
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
gl::Error copyToStorage(TextureStorage *destStorage) override;
......@@ -317,7 +324,7 @@ class TextureStorage11_Cube : public TextureStorage11
gl::Error useLevelZeroWorkaroundTexture(bool useLevelZeroTexture) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
gl::ErrorOrResult<DropStencil> ensureDropStencilTexture() override;
......@@ -328,24 +335,24 @@ class TextureStorage11_Cube : public TextureStorage11
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
gl::Error createRenderTargetSRV(ID3D11Resource *texture,
gl::Error createRenderTargetSRV(const TextureHelper11 &texture,
const gl::ImageIndex &index,
DXGI_FORMAT resourceFormat,
d3d11::SharedSRV *srv) const;
static const size_t CUBE_FACE_COUNT = 6;
ID3D11Texture2D *mTexture;
TextureHelper11 mTexture;
RenderTarget11 *mRenderTarget[CUBE_FACE_COUNT][gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
// Level-zero workaround members. See TextureStorage11_2D's workaround members for a description.
ID3D11Texture2D *mLevelZeroTexture;
TextureHelper11 mLevelZeroTexture;
RenderTarget11 *mLevelZeroRenderTarget[CUBE_FACE_COUNT];
bool mUseLevelZeroTexture;
ID3D11Texture2D *mSwizzleTexture;
TextureHelper11 mSwizzleTexture;
d3d11::RenderTargetView mSwizzleRenderTargets[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
Image11 *mAssociatedImages[CUBE_FACE_COUNT][gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
......@@ -358,7 +365,7 @@ class TextureStorage11_3D : public TextureStorage11
GLsizei width, GLsizei height, GLsizei depth, int levels);
~TextureStorage11_3D() override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
// Handles both layer and non-layer RTs
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
......@@ -369,14 +376,14 @@ class TextureStorage11_3D : public TextureStorage11
gl::Error releaseAssociatedImage(const gl::ImageIndex &index, Image11 *incomingImage) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
private:
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
typedef std::pair<int, int> LevelLayerKey;
......@@ -385,8 +392,8 @@ class TextureStorage11_3D : public TextureStorage11
RenderTarget11 *mLevelRenderTargets[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
ID3D11Texture3D *mTexture;
ID3D11Texture3D *mSwizzleTexture;
TextureHelper11 mTexture;
TextureHelper11 mSwizzleTexture;
d3d11::RenderTargetView mSwizzleRenderTargets[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
Image11 *mAssociatedImages[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
......@@ -399,7 +406,7 @@ class TextureStorage11_2DArray : public TextureStorage11
GLsizei width, GLsizei height, GLsizei depth, int levels);
~TextureStorage11_2DArray() override;
gl::Error getResource(ID3D11Resource **outResource) override;
gl::Error getResource(const TextureHelper11 **outResource) override;
gl::Error getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT) override;
void associateImage(Image11 *image, const gl::ImageIndex &index) override;
......@@ -408,7 +415,7 @@ class TextureStorage11_2DArray : public TextureStorage11
gl::Error releaseAssociatedImage(const gl::ImageIndex &index, Image11 *incomingImage) override;
protected:
gl::Error getSwizzleTexture(ID3D11Resource **outTexture) override;
gl::Error getSwizzleTexture(const TextureHelper11 **outTexture) override;
gl::Error getSwizzleRenderTarget(int mipLevel, const d3d11::RenderTargetView **outRTV) override;
gl::ErrorOrResult<DropStencil> ensureDropStencilTexture() override;
......@@ -417,9 +424,9 @@ class TextureStorage11_2DArray : public TextureStorage11
gl::Error createSRV(int baseLevel,
int mipLevels,
DXGI_FORMAT format,
ID3D11Resource *texture,
const TextureHelper11 &texture,
d3d11::SharedSRV *outSRV) const override;
gl::Error createRenderTargetSRV(ID3D11Resource *texture,
gl::Error createRenderTargetSRV(const TextureHelper11 &texture,
const gl::ImageIndex &index,
DXGI_FORMAT resourceFormat,
d3d11::SharedSRV *srv) const;
......@@ -428,9 +435,9 @@ class TextureStorage11_2DArray : public TextureStorage11
typedef std::map<LevelLayerKey, RenderTarget11*> RenderTargetMap;
RenderTargetMap mRenderTargets;
ID3D11Texture2D *mTexture;
TextureHelper11 mTexture;
ID3D11Texture2D *mSwizzleTexture;
TextureHelper11 mSwizzleTexture;
d3d11::RenderTargetView mSwizzleRenderTargets[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS];
typedef std::map<LevelLayerKey, Image11*> ImageMap;
......
......@@ -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