Commit 20805650 by Jamie Madill Committed by Commit Bot

D3D11: Consolidate state allocation.

This cleans up the allocation and deallocation of Blend, DepthStencil, Rasterizer, and Sampler states. This patch introduces a LazyResource2 class, basically a replacement for LazyResource, which will be removed once the refactor is done. BUG=angleproject:2034 Change-Id: I4fa759ae479807ff69a629f89a08b01800ba3f66 Reviewed-on: https://chromium-review.googlesource.com/503627 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent 857c09db
......@@ -557,11 +557,11 @@ Blit11::Blit11(Renderer11 *renderer)
: mRenderer(renderer),
mResourcesInitialized(false),
mVertexBuffer(),
mPointSampler(nullptr),
mLinearSampler(nullptr),
mScissorEnabledRasterizerState(nullptr),
mScissorDisabledRasterizerState(nullptr),
mDepthStencilState(nullptr),
mPointSampler(),
mLinearSampler(),
mScissorEnabledRasterizerState(),
mScissorDisabledRasterizerState(),
mDepthStencilState(),
mQuad2DIL(quad2DLayout,
ArraySize(quad2DLayout),
g_VS_Passthrough2D,
......@@ -618,9 +618,6 @@ gl::Error Blit11::initResources()
TRACE_EVENT0("gpu.angle", "Blit11::initResources");
HRESULT result;
ID3D11Device *device = mRenderer->getDevice();
D3D11_BUFFER_DESC vbDesc;
vbDesc.ByteWidth =
static_cast<unsigned int>(std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex),
......@@ -650,13 +647,8 @@ gl::Error Blit11::initResources()
pointSamplerDesc.MinLOD = 0.0f;
pointSamplerDesc.MaxLOD = FLT_MAX;
result = device->CreateSamplerState(&pointSamplerDesc, mPointSampler.GetAddressOf());
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit point sampler state, " << result;
}
d3d11::SetDebugName(mPointSampler, "Blit11 point sampler");
ANGLE_TRY(mRenderer->allocateResource(pointSamplerDesc, &mPointSampler));
mPointSampler.setDebugName("Blit11 point sampler");
D3D11_SAMPLER_DESC linearSamplerDesc;
linearSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
......@@ -673,13 +665,8 @@ gl::Error Blit11::initResources()
linearSamplerDesc.MinLOD = 0.0f;
linearSamplerDesc.MaxLOD = FLT_MAX;
result = device->CreateSamplerState(&linearSamplerDesc, mLinearSampler.GetAddressOf());
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit linear sampler state, " << result;
}
d3d11::SetDebugName(mLinearSampler, "Blit11 linear sampler");
ANGLE_TRY(mRenderer->allocateResource(linearSamplerDesc, &mLinearSampler));
mLinearSampler.setDebugName("Blit11 linear sampler");
// Use a rasterizer state that will not cull so that inverted quads will not be culled
D3D11_RASTERIZER_DESC rasterDesc;
......@@ -694,25 +681,12 @@ gl::Error Blit11::initResources()
rasterDesc.AntialiasedLineEnable = FALSE;
rasterDesc.ScissorEnable = TRUE;
result =
device->CreateRasterizerState(&rasterDesc, mScissorEnabledRasterizerState.GetAddressOf());
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit scissoring rasterizer state, " << result;
}
d3d11::SetDebugName(mScissorEnabledRasterizerState, "Blit11 scissoring rasterizer state");
ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &mScissorEnabledRasterizerState));
mScissorEnabledRasterizerState.setDebugName("Blit11 scissoring rasterizer state");
rasterDesc.ScissorEnable = FALSE;
result =
device->CreateRasterizerState(&rasterDesc, mScissorDisabledRasterizerState.GetAddressOf());
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit no scissoring rasterizer state, "
<< result;
}
d3d11::SetDebugName(mScissorDisabledRasterizerState, "Blit11 no scissoring rasterizer state");
ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &mScissorDisabledRasterizerState));
mScissorDisabledRasterizerState.setDebugName("Blit11 no scissoring rasterizer state");
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = TRUE;
......@@ -730,13 +704,8 @@ gl::Error Blit11::initResources()
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
result = device->CreateDepthStencilState(&depthStencilDesc, mDepthStencilState.GetAddressOf());
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit depth stencil state, " << result;
}
d3d11::SetDebugName(mDepthStencilState, "Blit11 depth stencil state");
ANGLE_TRY(mRenderer->allocateResource(depthStencilDesc, &mDepthStencilState));
mDepthStencilState.setDebugName("Blit11 depth stencil state");
D3D11_BUFFER_DESC swizzleBufferDesc;
swizzleBufferDesc.ByteWidth = sizeof(unsigned int) * 4;
......@@ -1112,7 +1081,7 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source,
// Apply state
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(nullptr, 0xFFFFFFFF);
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get());
deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
// Apply shaders
deviceContext->IASetInputLayout(support.inputLayout);
......@@ -1143,7 +1112,8 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source,
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get());
// Apply samplers
deviceContext->PSSetSamplers(0, 1, mPointSampler.GetAddressOf());
ID3D11SamplerState *samplerState = mPointSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw the quad
deviceContext->Draw(drawCount, 0);
......@@ -1226,8 +1196,8 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
// Apply state
if (maskOffAlpha)
{
ID3D11BlendState *blendState = mAlphaMaskBlendState.resolve(mRenderer->getDevice());
ASSERT(blendState);
ANGLE_TRY(mAlphaMaskBlendState.resolve(mRenderer));
ID3D11BlendState *blendState = mAlphaMaskBlendState.get();
deviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFF);
}
else
......@@ -1245,11 +1215,11 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
scissorRect.bottom = scissor->y + scissor->height;
deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get());
deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
}
else
{
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get());
deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
}
// Apply shaders
......@@ -1285,10 +1255,10 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
switch (filter)
{
case GL_NEAREST:
sampler = mPointSampler.Get();
sampler = mPointSampler.get();
break;
case GL_LINEAR:
sampler = mLinearSampler.Get();
sampler = mLinearSampler.get();
break;
default:
......@@ -1365,7 +1335,7 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
// Apply state
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(mDepthStencilState.Get(), 0xFFFFFFFF);
deviceContext->OMSetDepthStencilState(mDepthStencilState.get(), 0xFFFFFFFF);
if (scissor)
{
......@@ -1376,11 +1346,11 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
scissorRect.bottom = scissor->y + scissor->height;
deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get());
deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
}
else
{
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get());
deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
}
ID3D11Device *device = mRenderer->getDevice();
......@@ -1419,7 +1389,8 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get());
// Apply samplers
deviceContext->PSSetSamplers(0, 1, mPointSampler.GetAddressOf());
ID3D11SamplerState *samplerState = mPointSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw the quad
deviceContext->Draw(drawCount, 0);
......@@ -2050,7 +2021,7 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveDepth(RenderTarget11 *depth)
context->VSSetShader(mResolveDepthStencilVS.resolve(device), nullptr, 0);
context->GSSetShader(nullptr, nullptr, 0);
context->RSSetState(nullptr);
context->OMSetDepthStencilState(mDepthStencilState.Get(), 0xFFFFFFFF);
context->OMSetDepthStencilState(mDepthStencilState.get(), 0xFFFFFFFF);
context->OMSetRenderTargets(0, nullptr, mResolvedDepthDSView.get());
context->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
......
......@@ -278,11 +278,11 @@ class Blit11 : angle::NonCopyable
bool mResourcesInitialized;
d3d11::Buffer mVertexBuffer;
angle::ComPtr<ID3D11SamplerState> mPointSampler;
angle::ComPtr<ID3D11SamplerState> mLinearSampler;
angle::ComPtr<ID3D11RasterizerState> mScissorEnabledRasterizerState;
angle::ComPtr<ID3D11RasterizerState> mScissorDisabledRasterizerState;
angle::ComPtr<ID3D11DepthStencilState> mDepthStencilState;
d3d11::SamplerState mPointSampler;
d3d11::SamplerState mLinearSampler;
d3d11::RasterizerState mScissorEnabledRasterizerState;
d3d11::RasterizerState mScissorDisabledRasterizerState;
d3d11::DepthStencilState mDepthStencilState;
d3d11::LazyInputLayout mQuad2DIL;
d3d11::LazyShader<ID3D11VertexShader> mQuad2DVS;
......
......@@ -151,17 +151,28 @@ void Clear11::ShaderManager::getShadersAndLayout(ID3D11Device *device,
Clear11::Clear11(Renderer11 *renderer)
: mRenderer(renderer),
mScissorEnabledRasterizerState(nullptr),
mScissorDisabledRasterizerState(nullptr),
mResourcesInitialized(false),
mScissorEnabledRasterizerState(),
mScissorDisabledRasterizerState(),
mShaderManager(),
mConstantBuffer(),
mVertexBuffer(),
mShaderData({})
{
TRACE_EVENT0("gpu.angle", "Clear11::Clear11");
}
HRESULT result;
ID3D11Device *device = renderer->getDevice();
Clear11::~Clear11()
{
}
gl::Error Clear11::ensureResourcesInitialized()
{
if (mResourcesInitialized)
{
return gl::NoError();
}
TRACE_EVENT0("gpu.angle", "Clear11::ensureResourcesInitialized");
static_assert((sizeof(RtvDsvClearInfo<float>) == sizeof(RtvDsvClearInfo<int>)),
"Size of rx::RtvDsvClearInfo<float> is not equal to rx::RtvDsvClearInfo<int>");
......@@ -186,16 +197,12 @@ Clear11::Clear11(Renderer11 *renderer)
rsDesc.MultisampleEnable = FALSE;
rsDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rsDesc, mScissorDisabledRasterizerState.GetAddressOf());
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mScissorDisabledRasterizerState,
"Clear11 Rasterizer State with scissor disabled");
ANGLE_TRY(mRenderer->allocateResource(rsDesc, &mScissorDisabledRasterizerState));
mScissorDisabledRasterizerState.setDebugName("Clear11 Rasterizer State with scissor disabled");
rsDesc.ScissorEnable = TRUE;
result = device->CreateRasterizerState(&rsDesc, mScissorEnabledRasterizerState.GetAddressOf());
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mScissorEnabledRasterizerState,
"Clear11 Rasterizer State with scissor enabled");
ANGLE_TRY(mRenderer->allocateResource(rsDesc, &mScissorEnabledRasterizerState));
mScissorEnabledRasterizerState.setDebugName("Clear11 Rasterizer State with scissor enabled");
// Initialize Depthstencil state with defaults
mDepthStencilStateKey.depthTest = false;
......@@ -227,10 +234,9 @@ Clear11::Clear11(Renderer11 *renderer)
mBlendStateKey.blendState.dither = true;
mBlendStateKey.mrt = false;
memset(mBlendStateKey.rtvMasks, 0, sizeof(mBlendStateKey.rtvMasks));
}
Clear11::~Clear11()
{
mResourcesInitialized = true;
return gl::NoError();
}
bool Clear11::useVertexBuffer() const
......@@ -305,6 +311,8 @@ gl::Error Clear11::ensureVertexBufferCreated()
gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams,
const gl::FramebufferState &fboData)
{
ANGLE_TRY(ensureResourcesInitialized());
const auto &colorAttachments = fboData.getColorAttachments();
const auto &drawBufferStates = fboData.getDrawBufferStates();
const gl::FramebufferAttachment *depthStencilAttachment = fboData.getDepthOrStencilAttachment();
......@@ -676,11 +684,11 @@ gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams,
const D3D11_RECT scissorRect = {clearParams.scissor.x, clearParams.scissor.y,
clearParams.scissor.x1(), clearParams.scissor.y1()};
deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get());
deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
}
else
{
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get());
deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
}
// Get Shaders
......
......@@ -67,12 +67,14 @@ class Clear11 : angle::NonCopyable
bool useVertexBuffer() const;
gl::Error ensureConstantBufferCreated();
gl::Error ensureVertexBufferCreated();
gl::Error ensureResourcesInitialized();
Renderer11 *mRenderer;
bool mResourcesInitialized;
// States
angle::ComPtr<ID3D11RasterizerState> mScissorEnabledRasterizerState;
angle::ComPtr<ID3D11RasterizerState> mScissorDisabledRasterizerState;
d3d11::RasterizerState mScissorEnabledRasterizerState;
d3d11::RasterizerState mScissorDisabledRasterizerState;
gl::DepthStencilState mDepthStencilStateKey;
d3d11::BlendStateKey mBlendStateKey;
......
......@@ -39,8 +39,8 @@ PixelTransfer11::PixelTransfer11(Renderer11 *renderer)
mBufferToTextureVS(nullptr),
mBufferToTextureGS(nullptr),
mParamsConstantBuffer(),
mCopyRasterizerState(nullptr),
mCopyDepthStencilState(nullptr)
mCopyRasterizerState(),
mCopyDepthStencilState()
{
}
......@@ -55,8 +55,6 @@ PixelTransfer11::~PixelTransfer11()
SafeRelease(mBufferToTextureVS);
SafeRelease(mBufferToTextureGS);
SafeRelease(mCopyRasterizerState);
SafeRelease(mCopyDepthStencilState);
}
gl::Error PixelTransfer11::loadResources()
......@@ -66,9 +64,6 @@ gl::Error PixelTransfer11::loadResources()
return gl::NoError();
}
HRESULT result = S_OK;
ID3D11Device *device = mRenderer->getDevice();
D3D11_RASTERIZER_DESC rasterDesc;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_NONE;
......@@ -81,12 +76,7 @@ gl::Error PixelTransfer11::loadResources()
rasterDesc.MultisampleEnable = FALSE;
rasterDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rasterDesc, &mCopyRasterizerState);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create internal pixel transfer rasterizer state, result: 0x%X.", result);
}
ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &mCopyRasterizerState));
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = true;
......@@ -104,12 +94,7 @@ gl::Error PixelTransfer11::loadResources()
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
result = device->CreateDepthStencilState(&depthStencilDesc, &mCopyDepthStencilState);
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::Error(GL_OUT_OF_MEMORY, "Failed to create internal pixel transfer depth stencil state, result: 0x%X.", result);
}
ANGLE_TRY(mRenderer->allocateResource(depthStencilDesc, &mCopyDepthStencilState));
D3D11_BUFFER_DESC constantBufferDesc = { 0 };
constantBufferDesc.ByteWidth = roundUp<UINT>(sizeof(CopyShaderParams), 32u);
......@@ -123,6 +108,7 @@ gl::Error PixelTransfer11::loadResources()
mParamsConstantBuffer.setDebugName("PixelTransfer11 constant buffer");
// init shaders
ID3D11Device *device = mRenderer->getDevice();
mBufferToTextureVS = d3d11::CompileVS(device, g_VS_BufferToTexture, "BufferToTexture VS");
if (!mBufferToTextureVS)
{
......@@ -226,8 +212,8 @@ gl::Error PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpac
deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(mCopyDepthStencilState, 0xFFFFFFFF);
deviceContext->RSSetState(mCopyRasterizerState);
deviceContext->OMSetDepthStencilState(mCopyDepthStencilState.get(), 0xFFFFFFFF);
deviceContext->RSSetState(mCopyRasterizerState.get());
stateManager->setOneTimeRenderTarget(textureRTV.get(), nullptr);
......
......@@ -79,11 +79,10 @@ class PixelTransfer11
d3d11::Buffer mParamsConstantBuffer;
CopyShaderParams mParamsData;
ID3D11RasterizerState *mCopyRasterizerState;
ID3D11DepthStencilState *mCopyDepthStencilState;
d3d11::RasterizerState mCopyRasterizerState;
d3d11::DepthStencilState mCopyDepthStencilState;
};
}
} // namespace rx
#endif // LIBANGLE_RENDERER_D3D_D3D11_PIXELTRANSFER11_H_
......@@ -32,7 +32,6 @@ class RenderStateCache : angle::NonCopyable
RenderStateCache(Renderer11 *renderer);
virtual ~RenderStateCache();
void initialize(ID3D11Device *device);
void clear();
static d3d11::BlendStateKey GetBlendStateKey(const gl::Framebuffer *framebuffer,
......@@ -55,7 +54,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*BlendStateHashFunction)(const d3d11::BlendStateKey &);
typedef bool (*BlendStateEqualityFunction)(const d3d11::BlendStateKey &,
const d3d11::BlendStateKey &);
typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
typedef std::pair<d3d11::BlendState, unsigned long long> BlendStateCounterPair;
typedef std::unordered_map<d3d11::BlendStateKey,
BlendStateCounterPair,
BlendStateHashFunction,
......@@ -75,7 +74,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &);
typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, const RasterizerStateKey &);
typedef std::pair<ID3D11RasterizerState*, unsigned long long> RasterizerStateCounterPair;
typedef std::pair<d3d11::RasterizerState, unsigned long long> RasterizerStateCounterPair;
typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap;
RasterizerStateMap mRasterizerStateCache;
......@@ -86,7 +85,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &);
typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, const gl::DepthStencilState &);
typedef std::pair<ID3D11DepthStencilState*, unsigned long long> DepthStencilStateCounterPair;
typedef std::pair<d3d11::DepthStencilState, unsigned long long> DepthStencilStateCounterPair;
typedef std::unordered_map<gl::DepthStencilState,
DepthStencilStateCounterPair,
DepthStencilStateHashFunction,
......@@ -100,16 +99,14 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &);
typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, const gl::SamplerState &);
typedef std::pair<ID3D11SamplerState*, unsigned long long> SamplerStateCounterPair;
typedef std::pair<d3d11::SamplerState, unsigned long long> SamplerStateCounterPair;
typedef std::unordered_map<gl::SamplerState,
SamplerStateCounterPair,
SamplerStateHashFunction,
SamplerStateEqualityFunction> SamplerStateMap;
SamplerStateMap mSamplerStateCache;
ID3D11Device *mDevice;
};
}
} // namespace rx
#endif // LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
......@@ -791,7 +791,7 @@ void Renderer11::initializeDevice()
populateRenderer11DeviceCaps();
mStateCache.initialize(mDevice);
mStateCache.clear();
mInputLayoutCache.initialize();
ASSERT(!mVertexDataManager && !mIndexDataManager);
......
......@@ -89,6 +89,14 @@ size_t ComputeGenericMemoryUsage(ResourceType resourceType, ID3D11DeviceChild *r
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_BLEND_DESC *desc,
void * /*initData*/,
ID3D11BlendState **blendState)
{
return device->CreateBlendState(desc, blendState);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_BUFFER_DESC *desc,
const D3D11_SUBRESOURCE_DATA *initData,
ID3D11Buffer **buffer)
......@@ -97,6 +105,14 @@ HRESULT CreateResource(ID3D11Device *device,
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_DEPTH_STENCIL_DESC *desc,
void * /*initData*/,
ID3D11DepthStencilState **resourceOut)
{
return device->CreateDepthStencilState(desc, resourceOut);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_DEPTH_STENCIL_VIEW_DESC *desc,
ID3D11Resource *resource,
ID3D11DepthStencilView **resourceOut)
......@@ -105,6 +121,14 @@ HRESULT CreateResource(ID3D11Device *device,
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_RASTERIZER_DESC *desc,
void * /*initData*/,
ID3D11RasterizerState **rasterizerState)
{
return device->CreateRasterizerState(desc, rasterizerState);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_RENDER_TARGET_VIEW_DESC *desc,
ID3D11Resource *resource,
ID3D11RenderTargetView **renderTargetView)
......@@ -113,6 +137,14 @@ HRESULT CreateResource(ID3D11Device *device,
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_SAMPLER_DESC *desc,
void * /*initData*/,
ID3D11SamplerState **resourceOut)
{
return device->CreateSamplerState(desc, resourceOut);
}
HRESULT CreateResource(ID3D11Device *device,
const D3D11_SHADER_RESOURCE_VIEW_DESC *desc,
ID3D11Resource *resource,
ID3D11ShaderResourceView **resourceOut)
......
......@@ -26,11 +26,15 @@ class TextureHelper11;
// Format: ResourceType, D3D11 type, DESC type, init data type.
#define ANGLE_RESOURCE_TYPE_OP(NAME, OP) \
OP(NAME, BlendState, ID3D11BlendState, D3D11_BLEND_DESC, void) \
OP(NAME, Buffer, ID3D11Buffer, D3D11_BUFFER_DESC, const D3D11_SUBRESOURCE_DATA) \
OP(NAME, DepthStencilState, ID3D11DepthStencilState, D3D11_DEPTH_STENCIL_DESC, void) \
OP(NAME, DepthStencilView, ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, RasterizerState, ID3D11RasterizerState, D3D11_RASTERIZER_DESC, void) \
OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, SamplerState, ID3D11SamplerState, D3D11_SAMPLER_DESC, void) \
OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \
ID3D11Resource) \
OP(NAME, Texture2D, ID3D11Texture2D, D3D11_TEXTURE2D_DESC, const D3D11_SUBRESOURCE_DATA) \
......
......@@ -81,11 +81,11 @@ SwapChain11::SwapChain11(Renderer11 *renderer,
mDepthStencilDSView(),
mDepthStencilSRView(),
mQuadVB(),
mPassThroughSampler(nullptr),
mPassThroughSampler(),
mPassThroughIL(nullptr),
mPassThroughVS(nullptr),
mPassThroughPS(nullptr),
mPassThroughRS(nullptr),
mPassThroughRS(),
mColorRenderTarget(this, renderer, false),
mDepthStencilRenderTarget(this, renderer, true),
mEGLSamples(samples)
......@@ -121,11 +121,11 @@ void SwapChain11::release()
mDepthStencilDSView.reset();
mDepthStencilSRView.reset();
mQuadVB.reset();
SafeRelease(mPassThroughSampler);
mPassThroughSampler.reset();
SafeRelease(mPassThroughIL);
SafeRelease(mPassThroughVS);
SafeRelease(mPassThroughPS);
SafeRelease(mPassThroughRS);
mPassThroughRS.reset();
if (!mAppCreatedShareHandle)
{
......@@ -611,7 +611,7 @@ void SwapChain11::initPassThroughResources()
ASSERT(device != nullptr);
// Make sure our resources are all not allocated, when we create
ASSERT(!mQuadVB.valid() && mPassThroughSampler == nullptr);
ASSERT(!mQuadVB.valid() && !mPassThroughSampler.valid());
ASSERT(mPassThroughIL == nullptr && mPassThroughVS == nullptr && mPassThroughPS == nullptr);
D3D11_BUFFER_DESC vbDesc;
......@@ -641,9 +641,9 @@ void SwapChain11::initPassThroughResources()
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
HRESULT result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler);
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler");
err = mRenderer->allocateResource(samplerDesc, &mPassThroughSampler);
ASSERT(!err.isError());
mPassThroughSampler.setDebugName("Swap chain pass through sampler");
D3D11_INPUT_ELEMENT_DESC quadLayout[] =
{
......@@ -651,7 +651,8 @@ void SwapChain11::initPassThroughResources()
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), &mPassThroughIL);
HRESULT result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough2D,
sizeof(g_VS_Passthrough2D), &mPassThroughIL);
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout");
......@@ -686,9 +687,10 @@ void SwapChain11::initPassThroughResources()
rasterizerDesc.ScissorEnable = FALSE;
rasterizerDesc.MultisampleEnable = FALSE;
rasterizerDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rasterizerDesc, &mPassThroughRS);
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mPassThroughRS, "Swap chain pass through rasterizer state");
err = mRenderer->allocateResource(rasterizerDesc, &mPassThroughRS);
ASSERT(!err.isError());
mPassThroughRS.setDebugName("Swap chain pass through rasterizer state");
mPassThroughResourcesInit = true;
}
......@@ -777,7 +779,7 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
deviceContext->OMSetBlendState(nullptr, blendFactor, 0xFFFFFFF);
deviceContext->RSSetState(mPassThroughRS);
deviceContext->RSSetState(mPassThroughRS.get());
// Apply shaders
deviceContext->IASetInputLayout(mPassThroughIL);
......@@ -803,7 +805,9 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
// Apply textures
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, mOffscreenSRView.get());
deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler);
ID3D11SamplerState *samplerState = mPassThroughSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw
deviceContext->Draw(4, 0);
......
......@@ -99,11 +99,11 @@ class SwapChain11 final : public SwapChainD3D
d3d11::SharedSRV mDepthStencilSRView;
d3d11::Buffer mQuadVB;
ID3D11SamplerState *mPassThroughSampler;
d3d11::SamplerState mPassThroughSampler;
ID3D11InputLayout *mPassThroughIL;
ID3D11VertexShader *mPassThroughVS;
ID3D11PixelShader *mPassThroughPS;
ID3D11RasterizerState *mPassThroughRS;
d3d11::RasterizerState mPassThroughRS;
SurfaceRenderTarget11 mColorRenderTarget;
SurfaceRenderTarget11 mDepthStencilRenderTarget;
......
......@@ -1947,18 +1947,9 @@ LazyBlendState::LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugNa
{
}
ID3D11BlendState *LazyBlendState::resolve(ID3D11Device *device)
gl::Error LazyBlendState::resolve(Renderer11 *renderer)
{
checkAssociatedDevice(device);
if (mResource == nullptr)
{
HRESULT result = device->CreateBlendState(&mDesc, &mResource);
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mResource, mDebugName);
}
return mResource;
return resolveImpl(renderer, mDesc, mDebugName);
}
angle::WorkaroundsD3D GenerateWorkarounds(const Renderer11DeviceCaps &deviceCaps,
......
......@@ -240,6 +240,37 @@ class LazyResource : angle::NonCopyable
ID3D11Device *mAssociatedDevice;
};
template <ResourceType ResourceT>
class LazyResource2 : angle::NonCopyable
{
public:
LazyResource2() : mResource() {}
virtual ~LazyResource2() {}
virtual gl::Error resolve(Renderer11 *renderer) = 0;
void reset() { mResource.reset(); }
GetD3D11Type<ResourceT> *get() const
{
ASSERT(mResource.valid());
return mResource.get();
}
protected:
gl::Error resolveImpl(Renderer11 *renderer,
const GetDescType<ResourceT> &desc,
const char *name)
{
if (!mResource.valid())
{
ANGLE_TRY(renderer->allocateResource(desc, &mResource));
mResource.setDebugName(name);
}
return gl::NoError();
}
Resource11<GetD3D11Type<ResourceT>> mResource;
};
template <typename ResourceType>
void LazyResource<ResourceType>::checkAssociatedDevice(ID3D11Device *device)
{
......@@ -320,12 +351,12 @@ class LazyInputLayout final : public LazyResource<ID3D11InputLayout>
const char *mDebugName;
};
class LazyBlendState final : public LazyResource<ID3D11BlendState>
class LazyBlendState final : public LazyResource2<ResourceType::BlendState>
{
public:
LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugName);
ID3D11BlendState *resolve(ID3D11Device *device) override;
gl::Error resolve(Renderer11 *renderer);
private:
D3D11_BLEND_DESC mDesc;
......
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