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) ...@@ -557,11 +557,11 @@ Blit11::Blit11(Renderer11 *renderer)
: mRenderer(renderer), : mRenderer(renderer),
mResourcesInitialized(false), mResourcesInitialized(false),
mVertexBuffer(), mVertexBuffer(),
mPointSampler(nullptr), mPointSampler(),
mLinearSampler(nullptr), mLinearSampler(),
mScissorEnabledRasterizerState(nullptr), mScissorEnabledRasterizerState(),
mScissorDisabledRasterizerState(nullptr), mScissorDisabledRasterizerState(),
mDepthStencilState(nullptr), mDepthStencilState(),
mQuad2DIL(quad2DLayout, mQuad2DIL(quad2DLayout,
ArraySize(quad2DLayout), ArraySize(quad2DLayout),
g_VS_Passthrough2D, g_VS_Passthrough2D,
...@@ -618,9 +618,6 @@ gl::Error Blit11::initResources() ...@@ -618,9 +618,6 @@ gl::Error Blit11::initResources()
TRACE_EVENT0("gpu.angle", "Blit11::initResources"); TRACE_EVENT0("gpu.angle", "Blit11::initResources");
HRESULT result;
ID3D11Device *device = mRenderer->getDevice();
D3D11_BUFFER_DESC vbDesc; D3D11_BUFFER_DESC vbDesc;
vbDesc.ByteWidth = vbDesc.ByteWidth =
static_cast<unsigned int>(std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex), static_cast<unsigned int>(std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex),
...@@ -650,13 +647,8 @@ gl::Error Blit11::initResources() ...@@ -650,13 +647,8 @@ gl::Error Blit11::initResources()
pointSamplerDesc.MinLOD = 0.0f; pointSamplerDesc.MinLOD = 0.0f;
pointSamplerDesc.MaxLOD = FLT_MAX; pointSamplerDesc.MaxLOD = FLT_MAX;
result = device->CreateSamplerState(&pointSamplerDesc, mPointSampler.GetAddressOf()); ANGLE_TRY(mRenderer->allocateResource(pointSamplerDesc, &mPointSampler));
ASSERT(SUCCEEDED(result)); mPointSampler.setDebugName("Blit11 point sampler");
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit point sampler state, " << result;
}
d3d11::SetDebugName(mPointSampler, "Blit11 point sampler");
D3D11_SAMPLER_DESC linearSamplerDesc; D3D11_SAMPLER_DESC linearSamplerDesc;
linearSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; linearSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
...@@ -673,13 +665,8 @@ gl::Error Blit11::initResources() ...@@ -673,13 +665,8 @@ gl::Error Blit11::initResources()
linearSamplerDesc.MinLOD = 0.0f; linearSamplerDesc.MinLOD = 0.0f;
linearSamplerDesc.MaxLOD = FLT_MAX; linearSamplerDesc.MaxLOD = FLT_MAX;
result = device->CreateSamplerState(&linearSamplerDesc, mLinearSampler.GetAddressOf()); ANGLE_TRY(mRenderer->allocateResource(linearSamplerDesc, &mLinearSampler));
ASSERT(SUCCEEDED(result)); mLinearSampler.setDebugName("Blit11 linear sampler");
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit linear sampler state, " << result;
}
d3d11::SetDebugName(mLinearSampler, "Blit11 linear sampler");
// Use a rasterizer state that will not cull so that inverted quads will not be culled // Use a rasterizer state that will not cull so that inverted quads will not be culled
D3D11_RASTERIZER_DESC rasterDesc; D3D11_RASTERIZER_DESC rasterDesc;
...@@ -694,25 +681,12 @@ gl::Error Blit11::initResources() ...@@ -694,25 +681,12 @@ gl::Error Blit11::initResources()
rasterDesc.AntialiasedLineEnable = FALSE; rasterDesc.AntialiasedLineEnable = FALSE;
rasterDesc.ScissorEnable = TRUE; rasterDesc.ScissorEnable = TRUE;
result = ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &mScissorEnabledRasterizerState));
device->CreateRasterizerState(&rasterDesc, mScissorEnabledRasterizerState.GetAddressOf()); mScissorEnabledRasterizerState.setDebugName("Blit11 scissoring rasterizer state");
ASSERT(SUCCEEDED(result));
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit scissoring rasterizer state, " << result;
}
d3d11::SetDebugName(mScissorEnabledRasterizerState, "Blit11 scissoring rasterizer state");
rasterDesc.ScissorEnable = FALSE; rasterDesc.ScissorEnable = FALSE;
result = ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &mScissorDisabledRasterizerState));
device->CreateRasterizerState(&rasterDesc, mScissorDisabledRasterizerState.GetAddressOf()); mScissorDisabledRasterizerState.setDebugName("Blit11 no scissoring rasterizer state");
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");
D3D11_DEPTH_STENCIL_DESC depthStencilDesc; D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = TRUE; depthStencilDesc.DepthEnable = TRUE;
...@@ -730,13 +704,8 @@ gl::Error Blit11::initResources() ...@@ -730,13 +704,8 @@ gl::Error Blit11::initResources()
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
result = device->CreateDepthStencilState(&depthStencilDesc, mDepthStencilState.GetAddressOf()); ANGLE_TRY(mRenderer->allocateResource(depthStencilDesc, &mDepthStencilState));
ASSERT(SUCCEEDED(result)); mDepthStencilState.setDebugName("Blit11 depth stencil state");
if (FAILED(result))
{
return gl::OutOfMemory() << "Failed to create blit depth stencil state, " << result;
}
d3d11::SetDebugName(mDepthStencilState, "Blit11 depth stencil state");
D3D11_BUFFER_DESC swizzleBufferDesc; D3D11_BUFFER_DESC swizzleBufferDesc;
swizzleBufferDesc.ByteWidth = sizeof(unsigned int) * 4; swizzleBufferDesc.ByteWidth = sizeof(unsigned int) * 4;
...@@ -1112,7 +1081,7 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source, ...@@ -1112,7 +1081,7 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source,
// Apply state // Apply state
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF); deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(nullptr, 0xFFFFFFFF); deviceContext->OMSetDepthStencilState(nullptr, 0xFFFFFFFF);
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get()); deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
// Apply shaders // Apply shaders
deviceContext->IASetInputLayout(support.inputLayout); deviceContext->IASetInputLayout(support.inputLayout);
...@@ -1143,7 +1112,8 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source, ...@@ -1143,7 +1112,8 @@ gl::Error Blit11::swizzleTexture(const d3d11::SharedSRV &source,
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get()); stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get());
// Apply samplers // Apply samplers
deviceContext->PSSetSamplers(0, 1, mPointSampler.GetAddressOf()); ID3D11SamplerState *samplerState = mPointSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw the quad // Draw the quad
deviceContext->Draw(drawCount, 0); deviceContext->Draw(drawCount, 0);
...@@ -1226,8 +1196,8 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source, ...@@ -1226,8 +1196,8 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
// Apply state // Apply state
if (maskOffAlpha) if (maskOffAlpha)
{ {
ID3D11BlendState *blendState = mAlphaMaskBlendState.resolve(mRenderer->getDevice()); ANGLE_TRY(mAlphaMaskBlendState.resolve(mRenderer));
ASSERT(blendState); ID3D11BlendState *blendState = mAlphaMaskBlendState.get();
deviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFF); deviceContext->OMSetBlendState(blendState, nullptr, 0xFFFFFFF);
} }
else else
...@@ -1245,11 +1215,11 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source, ...@@ -1245,11 +1215,11 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
scissorRect.bottom = scissor->y + scissor->height; scissorRect.bottom = scissor->y + scissor->height;
deviceContext->RSSetScissorRects(1, &scissorRect); deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get()); deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
} }
else else
{ {
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get()); deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
} }
// Apply shaders // Apply shaders
...@@ -1285,10 +1255,10 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source, ...@@ -1285,10 +1255,10 @@ gl::Error Blit11::copyTexture(const d3d11::SharedSRV &source,
switch (filter) switch (filter)
{ {
case GL_NEAREST: case GL_NEAREST:
sampler = mPointSampler.Get(); sampler = mPointSampler.get();
break; break;
case GL_LINEAR: case GL_LINEAR:
sampler = mLinearSampler.Get(); sampler = mLinearSampler.get();
break; break;
default: default:
...@@ -1365,7 +1335,7 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source, ...@@ -1365,7 +1335,7 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
// Apply state // Apply state
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF); deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(mDepthStencilState.Get(), 0xFFFFFFFF); deviceContext->OMSetDepthStencilState(mDepthStencilState.get(), 0xFFFFFFFF);
if (scissor) if (scissor)
{ {
...@@ -1376,11 +1346,11 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source, ...@@ -1376,11 +1346,11 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
scissorRect.bottom = scissor->y + scissor->height; scissorRect.bottom = scissor->y + scissor->height;
deviceContext->RSSetScissorRects(1, &scissorRect); deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get()); deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
} }
else else
{ {
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get()); deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
} }
ID3D11Device *device = mRenderer->getDevice(); ID3D11Device *device = mRenderer->getDevice();
...@@ -1419,7 +1389,8 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source, ...@@ -1419,7 +1389,8 @@ gl::Error Blit11::copyDepth(const d3d11::SharedSRV &source,
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get()); stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, source.get());
// Apply samplers // Apply samplers
deviceContext->PSSetSamplers(0, 1, mPointSampler.GetAddressOf()); ID3D11SamplerState *samplerState = mPointSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw the quad // Draw the quad
deviceContext->Draw(drawCount, 0); deviceContext->Draw(drawCount, 0);
...@@ -2050,7 +2021,7 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveDepth(RenderTarget11 *depth) ...@@ -2050,7 +2021,7 @@ gl::ErrorOrResult<TextureHelper11> Blit11::resolveDepth(RenderTarget11 *depth)
context->VSSetShader(mResolveDepthStencilVS.resolve(device), nullptr, 0); context->VSSetShader(mResolveDepthStencilVS.resolve(device), nullptr, 0);
context->GSSetShader(nullptr, nullptr, 0); context->GSSetShader(nullptr, nullptr, 0);
context->RSSetState(nullptr); context->RSSetState(nullptr);
context->OMSetDepthStencilState(mDepthStencilState.Get(), 0xFFFFFFFF); context->OMSetDepthStencilState(mDepthStencilState.get(), 0xFFFFFFFF);
context->OMSetRenderTargets(0, nullptr, mResolvedDepthDSView.get()); context->OMSetRenderTargets(0, nullptr, mResolvedDepthDSView.get());
context->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF); context->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
......
...@@ -278,11 +278,11 @@ class Blit11 : angle::NonCopyable ...@@ -278,11 +278,11 @@ class Blit11 : angle::NonCopyable
bool mResourcesInitialized; bool mResourcesInitialized;
d3d11::Buffer mVertexBuffer; d3d11::Buffer mVertexBuffer;
angle::ComPtr<ID3D11SamplerState> mPointSampler; d3d11::SamplerState mPointSampler;
angle::ComPtr<ID3D11SamplerState> mLinearSampler; d3d11::SamplerState mLinearSampler;
angle::ComPtr<ID3D11RasterizerState> mScissorEnabledRasterizerState; d3d11::RasterizerState mScissorEnabledRasterizerState;
angle::ComPtr<ID3D11RasterizerState> mScissorDisabledRasterizerState; d3d11::RasterizerState mScissorDisabledRasterizerState;
angle::ComPtr<ID3D11DepthStencilState> mDepthStencilState; d3d11::DepthStencilState mDepthStencilState;
d3d11::LazyInputLayout mQuad2DIL; d3d11::LazyInputLayout mQuad2DIL;
d3d11::LazyShader<ID3D11VertexShader> mQuad2DVS; d3d11::LazyShader<ID3D11VertexShader> mQuad2DVS;
......
...@@ -151,17 +151,28 @@ void Clear11::ShaderManager::getShadersAndLayout(ID3D11Device *device, ...@@ -151,17 +151,28 @@ void Clear11::ShaderManager::getShadersAndLayout(ID3D11Device *device,
Clear11::Clear11(Renderer11 *renderer) Clear11::Clear11(Renderer11 *renderer)
: mRenderer(renderer), : mRenderer(renderer),
mScissorEnabledRasterizerState(nullptr), mResourcesInitialized(false),
mScissorDisabledRasterizerState(nullptr), mScissorEnabledRasterizerState(),
mScissorDisabledRasterizerState(),
mShaderManager(), mShaderManager(),
mConstantBuffer(), mConstantBuffer(),
mVertexBuffer(), mVertexBuffer(),
mShaderData({}) mShaderData({})
{ {
TRACE_EVENT0("gpu.angle", "Clear11::Clear11"); }
HRESULT result; Clear11::~Clear11()
ID3D11Device *device = renderer->getDevice(); {
}
gl::Error Clear11::ensureResourcesInitialized()
{
if (mResourcesInitialized)
{
return gl::NoError();
}
TRACE_EVENT0("gpu.angle", "Clear11::ensureResourcesInitialized");
static_assert((sizeof(RtvDsvClearInfo<float>) == sizeof(RtvDsvClearInfo<int>)), static_assert((sizeof(RtvDsvClearInfo<float>) == sizeof(RtvDsvClearInfo<int>)),
"Size of rx::RtvDsvClearInfo<float> is not equal to rx::RtvDsvClearInfo<int>"); "Size of rx::RtvDsvClearInfo<float> is not equal to rx::RtvDsvClearInfo<int>");
...@@ -186,16 +197,12 @@ Clear11::Clear11(Renderer11 *renderer) ...@@ -186,16 +197,12 @@ Clear11::Clear11(Renderer11 *renderer)
rsDesc.MultisampleEnable = FALSE; rsDesc.MultisampleEnable = FALSE;
rsDesc.AntialiasedLineEnable = FALSE; rsDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rsDesc, mScissorDisabledRasterizerState.GetAddressOf()); ANGLE_TRY(mRenderer->allocateResource(rsDesc, &mScissorDisabledRasterizerState));
ASSERT(SUCCEEDED(result)); mScissorDisabledRasterizerState.setDebugName("Clear11 Rasterizer State with scissor disabled");
d3d11::SetDebugName(mScissorDisabledRasterizerState,
"Clear11 Rasterizer State with scissor disabled");
rsDesc.ScissorEnable = TRUE; rsDesc.ScissorEnable = TRUE;
result = device->CreateRasterizerState(&rsDesc, mScissorEnabledRasterizerState.GetAddressOf()); ANGLE_TRY(mRenderer->allocateResource(rsDesc, &mScissorEnabledRasterizerState));
ASSERT(SUCCEEDED(result)); mScissorEnabledRasterizerState.setDebugName("Clear11 Rasterizer State with scissor enabled");
d3d11::SetDebugName(mScissorEnabledRasterizerState,
"Clear11 Rasterizer State with scissor enabled");
// Initialize Depthstencil state with defaults // Initialize Depthstencil state with defaults
mDepthStencilStateKey.depthTest = false; mDepthStencilStateKey.depthTest = false;
...@@ -227,10 +234,9 @@ Clear11::Clear11(Renderer11 *renderer) ...@@ -227,10 +234,9 @@ Clear11::Clear11(Renderer11 *renderer)
mBlendStateKey.blendState.dither = true; mBlendStateKey.blendState.dither = true;
mBlendStateKey.mrt = false; mBlendStateKey.mrt = false;
memset(mBlendStateKey.rtvMasks, 0, sizeof(mBlendStateKey.rtvMasks)); memset(mBlendStateKey.rtvMasks, 0, sizeof(mBlendStateKey.rtvMasks));
}
Clear11::~Clear11() mResourcesInitialized = true;
{ return gl::NoError();
} }
bool Clear11::useVertexBuffer() const bool Clear11::useVertexBuffer() const
...@@ -305,6 +311,8 @@ gl::Error Clear11::ensureVertexBufferCreated() ...@@ -305,6 +311,8 @@ gl::Error Clear11::ensureVertexBufferCreated()
gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams, gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams,
const gl::FramebufferState &fboData) const gl::FramebufferState &fboData)
{ {
ANGLE_TRY(ensureResourcesInitialized());
const auto &colorAttachments = fboData.getColorAttachments(); const auto &colorAttachments = fboData.getColorAttachments();
const auto &drawBufferStates = fboData.getDrawBufferStates(); const auto &drawBufferStates = fboData.getDrawBufferStates();
const gl::FramebufferAttachment *depthStencilAttachment = fboData.getDepthOrStencilAttachment(); const gl::FramebufferAttachment *depthStencilAttachment = fboData.getDepthOrStencilAttachment();
...@@ -676,11 +684,11 @@ gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams, ...@@ -676,11 +684,11 @@ gl::Error Clear11::clearFramebuffer(const ClearParameters &clearParams,
const D3D11_RECT scissorRect = {clearParams.scissor.x, clearParams.scissor.y, const D3D11_RECT scissorRect = {clearParams.scissor.x, clearParams.scissor.y,
clearParams.scissor.x1(), clearParams.scissor.y1()}; clearParams.scissor.x1(), clearParams.scissor.y1()};
deviceContext->RSSetScissorRects(1, &scissorRect); deviceContext->RSSetScissorRects(1, &scissorRect);
deviceContext->RSSetState(mScissorEnabledRasterizerState.Get()); deviceContext->RSSetState(mScissorEnabledRasterizerState.get());
} }
else else
{ {
deviceContext->RSSetState(mScissorDisabledRasterizerState.Get()); deviceContext->RSSetState(mScissorDisabledRasterizerState.get());
} }
// Get Shaders // Get Shaders
......
...@@ -67,12 +67,14 @@ class Clear11 : angle::NonCopyable ...@@ -67,12 +67,14 @@ class Clear11 : angle::NonCopyable
bool useVertexBuffer() const; bool useVertexBuffer() const;
gl::Error ensureConstantBufferCreated(); gl::Error ensureConstantBufferCreated();
gl::Error ensureVertexBufferCreated(); gl::Error ensureVertexBufferCreated();
gl::Error ensureResourcesInitialized();
Renderer11 *mRenderer; Renderer11 *mRenderer;
bool mResourcesInitialized;
// States // States
angle::ComPtr<ID3D11RasterizerState> mScissorEnabledRasterizerState; d3d11::RasterizerState mScissorEnabledRasterizerState;
angle::ComPtr<ID3D11RasterizerState> mScissorDisabledRasterizerState; d3d11::RasterizerState mScissorDisabledRasterizerState;
gl::DepthStencilState mDepthStencilStateKey; gl::DepthStencilState mDepthStencilStateKey;
d3d11::BlendStateKey mBlendStateKey; d3d11::BlendStateKey mBlendStateKey;
......
...@@ -39,8 +39,8 @@ PixelTransfer11::PixelTransfer11(Renderer11 *renderer) ...@@ -39,8 +39,8 @@ PixelTransfer11::PixelTransfer11(Renderer11 *renderer)
mBufferToTextureVS(nullptr), mBufferToTextureVS(nullptr),
mBufferToTextureGS(nullptr), mBufferToTextureGS(nullptr),
mParamsConstantBuffer(), mParamsConstantBuffer(),
mCopyRasterizerState(nullptr), mCopyRasterizerState(),
mCopyDepthStencilState(nullptr) mCopyDepthStencilState()
{ {
} }
...@@ -55,8 +55,6 @@ PixelTransfer11::~PixelTransfer11() ...@@ -55,8 +55,6 @@ PixelTransfer11::~PixelTransfer11()
SafeRelease(mBufferToTextureVS); SafeRelease(mBufferToTextureVS);
SafeRelease(mBufferToTextureGS); SafeRelease(mBufferToTextureGS);
SafeRelease(mCopyRasterizerState);
SafeRelease(mCopyDepthStencilState);
} }
gl::Error PixelTransfer11::loadResources() gl::Error PixelTransfer11::loadResources()
...@@ -66,9 +64,6 @@ gl::Error PixelTransfer11::loadResources() ...@@ -66,9 +64,6 @@ gl::Error PixelTransfer11::loadResources()
return gl::NoError(); return gl::NoError();
} }
HRESULT result = S_OK;
ID3D11Device *device = mRenderer->getDevice();
D3D11_RASTERIZER_DESC rasterDesc; D3D11_RASTERIZER_DESC rasterDesc;
rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = D3D11_CULL_NONE; rasterDesc.CullMode = D3D11_CULL_NONE;
...@@ -81,12 +76,7 @@ gl::Error PixelTransfer11::loadResources() ...@@ -81,12 +76,7 @@ gl::Error PixelTransfer11::loadResources()
rasterDesc.MultisampleEnable = FALSE; rasterDesc.MultisampleEnable = FALSE;
rasterDesc.AntialiasedLineEnable = FALSE; rasterDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rasterDesc, &mCopyRasterizerState); ANGLE_TRY(mRenderer->allocateResource(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);
}
D3D11_DEPTH_STENCIL_DESC depthStencilDesc; D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = true; depthStencilDesc.DepthEnable = true;
...@@ -104,12 +94,7 @@ gl::Error PixelTransfer11::loadResources() ...@@ -104,12 +94,7 @@ gl::Error PixelTransfer11::loadResources()
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
result = device->CreateDepthStencilState(&depthStencilDesc, &mCopyDepthStencilState); ANGLE_TRY(mRenderer->allocateResource(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);
}
D3D11_BUFFER_DESC constantBufferDesc = { 0 }; D3D11_BUFFER_DESC constantBufferDesc = { 0 };
constantBufferDesc.ByteWidth = roundUp<UINT>(sizeof(CopyShaderParams), 32u); constantBufferDesc.ByteWidth = roundUp<UINT>(sizeof(CopyShaderParams), 32u);
...@@ -123,6 +108,7 @@ gl::Error PixelTransfer11::loadResources() ...@@ -123,6 +108,7 @@ gl::Error PixelTransfer11::loadResources()
mParamsConstantBuffer.setDebugName("PixelTransfer11 constant buffer"); mParamsConstantBuffer.setDebugName("PixelTransfer11 constant buffer");
// init shaders // init shaders
ID3D11Device *device = mRenderer->getDevice();
mBufferToTextureVS = d3d11::CompileVS(device, g_VS_BufferToTexture, "BufferToTexture VS"); mBufferToTextureVS = d3d11::CompileVS(device, g_VS_BufferToTexture, "BufferToTexture VS");
if (!mBufferToTextureVS) if (!mBufferToTextureVS)
{ {
...@@ -226,8 +212,8 @@ gl::Error PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpac ...@@ -226,8 +212,8 @@ gl::Error PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpac
deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF); deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFF);
deviceContext->OMSetDepthStencilState(mCopyDepthStencilState, 0xFFFFFFFF); deviceContext->OMSetDepthStencilState(mCopyDepthStencilState.get(), 0xFFFFFFFF);
deviceContext->RSSetState(mCopyRasterizerState); deviceContext->RSSetState(mCopyRasterizerState.get());
stateManager->setOneTimeRenderTarget(textureRTV.get(), nullptr); stateManager->setOneTimeRenderTarget(textureRTV.get(), nullptr);
......
...@@ -79,11 +79,10 @@ class PixelTransfer11 ...@@ -79,11 +79,10 @@ class PixelTransfer11
d3d11::Buffer mParamsConstantBuffer; d3d11::Buffer mParamsConstantBuffer;
CopyShaderParams mParamsData; CopyShaderParams mParamsData;
ID3D11RasterizerState *mCopyRasterizerState; d3d11::RasterizerState mCopyRasterizerState;
ID3D11DepthStencilState *mCopyDepthStencilState; d3d11::DepthStencilState mCopyDepthStencilState;
}; };
} } // namespace rx
#endif // LIBANGLE_RENDERER_D3D_D3D11_PIXELTRANSFER11_H_ #endif // LIBANGLE_RENDERER_D3D_D3D11_PIXELTRANSFER11_H_
...@@ -23,16 +23,6 @@ namespace rx ...@@ -23,16 +23,6 @@ namespace rx
{ {
using namespace gl_d3d11; using namespace gl_d3d11;
template <typename mapType>
static void ClearStateMap(mapType &map)
{
for (typename mapType::iterator i = map.begin(); i != map.end(); i++)
{
SafeRelease(i->second.first);
}
map.clear();
}
// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState, // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
// ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
// number of unique states of each type an application can create is 4096 // number of unique states of each type an application can create is 4096
...@@ -51,28 +41,20 @@ RenderStateCache::RenderStateCache(Renderer11 *renderer) ...@@ -51,28 +41,20 @@ RenderStateCache::RenderStateCache(Renderer11 *renderer)
mDepthStencilStateCache(kMaxDepthStencilStates, mDepthStencilStateCache(kMaxDepthStencilStates,
HashDepthStencilState, HashDepthStencilState,
CompareDepthStencilStates), CompareDepthStencilStates),
mSamplerStateCache(kMaxSamplerStates, HashSamplerState, CompareSamplerStates), mSamplerStateCache(kMaxSamplerStates, HashSamplerState, CompareSamplerStates)
mDevice(nullptr)
{ {
} }
RenderStateCache::~RenderStateCache() RenderStateCache::~RenderStateCache()
{ {
clear();
}
void RenderStateCache::initialize(ID3D11Device *device)
{
clear();
mDevice = device;
} }
void RenderStateCache::clear() void RenderStateCache::clear()
{ {
ClearStateMap(mBlendStateCache); mBlendStateCache.clear();
ClearStateMap(mRasterizerStateCache); mRasterizerStateCache.clear();
ClearStateMap(mDepthStencilStateCache); mDepthStencilStateCache.clear();
ClearStateMap(mSamplerStateCache); mSamplerStateCache.clear();
} }
// static // static
...@@ -137,17 +119,12 @@ d3d11::BlendStateKey RenderStateCache::GetBlendStateKey(const gl::Framebuffer *f ...@@ -137,17 +119,12 @@ d3d11::BlendStateKey RenderStateCache::GetBlendStateKey(const gl::Framebuffer *f
gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key, gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key,
ID3D11BlendState **outBlendState) ID3D11BlendState **outBlendState)
{ {
if (!mDevice)
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal error, RenderStateCache is not initialized.");
}
BlendStateMap::iterator keyIter = mBlendStateCache.find(key); BlendStateMap::iterator keyIter = mBlendStateCache.find(key);
if (keyIter != mBlendStateCache.end()) if (keyIter != mBlendStateCache.end())
{ {
BlendStateCounterPair &state = keyIter->second; BlendStateCounterPair &state = keyIter->second;
state.second = mCounter++; state.second = mCounter++;
*outBlendState = state.first; *outBlendState = state.first.get();
return gl::NoError(); return gl::NoError();
} }
else else
...@@ -165,7 +142,6 @@ gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key, ...@@ -165,7 +142,6 @@ gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key,
leastRecentlyUsed = i; leastRecentlyUsed = i;
} }
} }
SafeRelease(leastRecentlyUsed->second.first);
mBlendStateCache.erase(leastRecentlyUsed); mBlendStateCache.erase(leastRecentlyUsed);
} }
...@@ -198,13 +174,10 @@ gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key, ...@@ -198,13 +174,10 @@ gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key,
blendDesc.RenderTarget[i].RenderTargetWriteMask = key.rtvMasks[i]; blendDesc.RenderTarget[i].RenderTargetWriteMask = key.rtvMasks[i];
} }
HRESULT result = mDevice->CreateBlendState(&blendDesc, outBlendState); d3d11::BlendState d3dBlendState;
if (FAILED(result) || !(*outBlendState)) ANGLE_TRY(mRenderer->allocateResource(blendDesc, &d3dBlendState));
{ *outBlendState = d3dBlendState.get();
return gl::Error(GL_OUT_OF_MEMORY, "Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result); mBlendStateCache[key] = std::make_pair(std::move(d3dBlendState), mCounter++);
}
mBlendStateCache.insert(std::make_pair(key, std::make_pair(*outBlendState, mCounter++)));
return gl::NoError(); return gl::NoError();
} }
...@@ -229,11 +202,6 @@ bool RenderStateCache::CompareRasterizerStates(const RasterizerStateKey &a, cons ...@@ -229,11 +202,6 @@ bool RenderStateCache::CompareRasterizerStates(const RasterizerStateKey &a, cons
gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled, gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled,
ID3D11RasterizerState **outRasterizerState) ID3D11RasterizerState **outRasterizerState)
{ {
if (!mDevice)
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal error, RenderStateCache is not initialized.");
}
RasterizerStateKey key = {}; RasterizerStateKey key = {};
key.rasterizerState = rasterState; key.rasterizerState = rasterState;
key.scissorEnabled = scissorEnabled; key.scissorEnabled = scissorEnabled;
...@@ -243,7 +211,7 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster ...@@ -243,7 +211,7 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster
{ {
RasterizerStateCounterPair &state = keyIter->second; RasterizerStateCounterPair &state = keyIter->second;
state.second = mCounter++; state.second = mCounter++;
*outRasterizerState = state.first; *outRasterizerState = state.first.get();
return gl::NoError(); return gl::NoError();
} }
else else
...@@ -261,7 +229,6 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster ...@@ -261,7 +229,6 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster
leastRecentlyUsed = i; leastRecentlyUsed = i;
} }
} }
SafeRelease(leastRecentlyUsed->second.first);
mRasterizerStateCache.erase(leastRecentlyUsed); mRasterizerStateCache.erase(leastRecentlyUsed);
} }
...@@ -294,16 +261,12 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster ...@@ -294,16 +261,12 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster
rasterDesc.DepthBias = 0; rasterDesc.DepthBias = 0;
} }
ID3D11RasterizerState *dx11RasterizerState = nullptr; d3d11::RasterizerState dx11RasterizerState;
HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState); ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &dx11RasterizerState));
if (FAILED(result) || !dx11RasterizerState) *outRasterizerState = dx11RasterizerState.get();
{ mRasterizerStateCache.insert(
return gl::Error(GL_OUT_OF_MEMORY, "Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result); std::make_pair(key, std::make_pair(std::move(dx11RasterizerState), mCounter++)));
}
mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
*outRasterizerState = dx11RasterizerState;
return gl::NoError(); return gl::NoError();
} }
} }
...@@ -327,17 +290,12 @@ bool RenderStateCache::CompareDepthStencilStates(const gl::DepthStencilState &a, ...@@ -327,17 +290,12 @@ bool RenderStateCache::CompareDepthStencilStates(const gl::DepthStencilState &a,
gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &glState, gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &glState,
ID3D11DepthStencilState **outDSState) ID3D11DepthStencilState **outDSState)
{ {
if (!mDevice)
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal error, RenderStateCache is not initialized.");
}
auto keyIter = mDepthStencilStateCache.find(glState); auto keyIter = mDepthStencilStateCache.find(glState);
if (keyIter != mDepthStencilStateCache.end()) if (keyIter != mDepthStencilStateCache.end())
{ {
DepthStencilStateCounterPair &state = keyIter->second; DepthStencilStateCounterPair &state = keyIter->second;
state.second = mCounter++; state.second = mCounter++;
*outDSState = state.first; *outDSState = state.first.get();
return gl::NoError(); return gl::NoError();
} }
...@@ -354,7 +312,6 @@ gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &gl ...@@ -354,7 +312,6 @@ gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &gl
leastRecentlyUsed = i; leastRecentlyUsed = i;
} }
} }
SafeRelease(leastRecentlyUsed->second.first);
mDepthStencilStateCache.erase(leastRecentlyUsed); mDepthStencilStateCache.erase(leastRecentlyUsed);
} }
...@@ -374,18 +331,12 @@ gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &gl ...@@ -374,18 +331,12 @@ gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &gl
dsDesc.BackFace.StencilPassOp = ConvertStencilOp(glState.stencilBackPassDepthPass); dsDesc.BackFace.StencilPassOp = ConvertStencilOp(glState.stencilBackPassDepthPass);
dsDesc.BackFace.StencilFunc = ConvertComparison(glState.stencilBackFunc); dsDesc.BackFace.StencilFunc = ConvertComparison(glState.stencilBackFunc);
ID3D11DepthStencilState *dx11DepthStencilState = nullptr; d3d11::DepthStencilState dx11DepthStencilState;
HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState); ANGLE_TRY(mRenderer->allocateResource(dsDesc, &dx11DepthStencilState));
if (FAILED(result) || !dx11DepthStencilState) *outDSState = dx11DepthStencilState.get();
{
return gl::Error(GL_OUT_OF_MEMORY,
"Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
}
mDepthStencilStateCache.insert( mDepthStencilStateCache.insert(
std::make_pair(glState, std::make_pair(dx11DepthStencilState, mCounter++))); std::make_pair(glState, std::make_pair(std::move(dx11DepthStencilState), mCounter++)));
*outDSState = dx11DepthStencilState;
return gl::NoError(); return gl::NoError();
} }
...@@ -407,17 +358,12 @@ bool RenderStateCache::CompareSamplerStates(const gl::SamplerState &a, const gl: ...@@ -407,17 +358,12 @@ bool RenderStateCache::CompareSamplerStates(const gl::SamplerState &a, const gl:
gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState, ID3D11SamplerState **outSamplerState) gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState, ID3D11SamplerState **outSamplerState)
{ {
if (!mDevice)
{
return gl::Error(GL_OUT_OF_MEMORY, "Internal error, RenderStateCache is not initialized.");
}
SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState); SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState);
if (keyIter != mSamplerStateCache.end()) if (keyIter != mSamplerStateCache.end())
{ {
SamplerStateCounterPair &state = keyIter->second; SamplerStateCounterPair &state = keyIter->second;
state.second = mCounter++; state.second = mCounter++;
*outSamplerState = state.first; *outSamplerState = state.first.get();
return gl::NoError(); return gl::NoError();
} }
else else
...@@ -435,10 +381,11 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState ...@@ -435,10 +381,11 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState
leastRecentlyUsed = i; leastRecentlyUsed = i;
} }
} }
SafeRelease(leastRecentlyUsed->second.first);
mSamplerStateCache.erase(leastRecentlyUsed); mSamplerStateCache.erase(leastRecentlyUsed);
} }
const auto &featureLevel = mRenderer->getRenderer11DeviceCaps().featureLevel;
D3D11_SAMPLER_DESC samplerDesc; D3D11_SAMPLER_DESC samplerDesc;
samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
samplerState.maxAnisotropy, samplerState.compareMode); samplerState.maxAnisotropy, samplerState.compareMode);
...@@ -447,7 +394,7 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState ...@@ -447,7 +394,7 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState
samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR); samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
samplerDesc.MipLODBias = 0; samplerDesc.MipLODBias = 0;
samplerDesc.MaxAnisotropy = samplerDesc.MaxAnisotropy =
gl_d3d11::ConvertMaxAnisotropy(samplerState.maxAnisotropy, mDevice->GetFeatureLevel()); gl_d3d11::ConvertMaxAnisotropy(samplerState.maxAnisotropy, featureLevel);
samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc); samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[0] = 0.0f;
samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[1] = 0.0f;
...@@ -466,18 +413,14 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState ...@@ -466,18 +413,14 @@ gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState
samplerDesc.MaxLOD = FLT_MAX; samplerDesc.MaxLOD = FLT_MAX;
} }
ID3D11SamplerState *dx11SamplerState = nullptr; d3d11::SamplerState dx11SamplerState;
HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState); ANGLE_TRY(mRenderer->allocateResource(samplerDesc, &dx11SamplerState));
if (FAILED(result) || !dx11SamplerState) *outSamplerState = dx11SamplerState.get();
{ mSamplerStateCache.insert(
return gl::Error(GL_OUT_OF_MEMORY, "Unable to create a ID3D11SamplerState, HRESULT: 0x%X.", result); std::make_pair(samplerState, std::make_pair(std::move(dx11SamplerState), mCounter++)));
}
mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
*outSamplerState = dx11SamplerState;
return gl::NoError(); return gl::NoError();
} }
} }
} } // namespace rx
...@@ -32,7 +32,6 @@ class RenderStateCache : angle::NonCopyable ...@@ -32,7 +32,6 @@ class RenderStateCache : angle::NonCopyable
RenderStateCache(Renderer11 *renderer); RenderStateCache(Renderer11 *renderer);
virtual ~RenderStateCache(); virtual ~RenderStateCache();
void initialize(ID3D11Device *device);
void clear(); void clear();
static d3d11::BlendStateKey GetBlendStateKey(const gl::Framebuffer *framebuffer, static d3d11::BlendStateKey GetBlendStateKey(const gl::Framebuffer *framebuffer,
...@@ -55,7 +54,7 @@ class RenderStateCache : angle::NonCopyable ...@@ -55,7 +54,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*BlendStateHashFunction)(const d3d11::BlendStateKey &); typedef std::size_t (*BlendStateHashFunction)(const d3d11::BlendStateKey &);
typedef bool (*BlendStateEqualityFunction)(const d3d11::BlendStateKey &, typedef bool (*BlendStateEqualityFunction)(const d3d11::BlendStateKey &,
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, typedef std::unordered_map<d3d11::BlendStateKey,
BlendStateCounterPair, BlendStateCounterPair,
BlendStateHashFunction, BlendStateHashFunction,
...@@ -75,7 +74,7 @@ class RenderStateCache : angle::NonCopyable ...@@ -75,7 +74,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &); typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &);
typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, 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; typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap;
RasterizerStateMap mRasterizerStateCache; RasterizerStateMap mRasterizerStateCache;
...@@ -86,7 +85,7 @@ class RenderStateCache : angle::NonCopyable ...@@ -86,7 +85,7 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &); typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &);
typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, 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, typedef std::unordered_map<gl::DepthStencilState,
DepthStencilStateCounterPair, DepthStencilStateCounterPair,
DepthStencilStateHashFunction, DepthStencilStateHashFunction,
...@@ -100,16 +99,14 @@ class RenderStateCache : angle::NonCopyable ...@@ -100,16 +99,14 @@ class RenderStateCache : angle::NonCopyable
typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &); typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &);
typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, 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, typedef std::unordered_map<gl::SamplerState,
SamplerStateCounterPair, SamplerStateCounterPair,
SamplerStateHashFunction, SamplerStateHashFunction,
SamplerStateEqualityFunction> SamplerStateMap; SamplerStateEqualityFunction> SamplerStateMap;
SamplerStateMap mSamplerStateCache; SamplerStateMap mSamplerStateCache;
ID3D11Device *mDevice;
}; };
} } // namespace rx
#endif // LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_ #endif // LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
...@@ -791,7 +791,7 @@ void Renderer11::initializeDevice() ...@@ -791,7 +791,7 @@ void Renderer11::initializeDevice()
populateRenderer11DeviceCaps(); populateRenderer11DeviceCaps();
mStateCache.initialize(mDevice); mStateCache.clear();
mInputLayoutCache.initialize(); mInputLayoutCache.initialize();
ASSERT(!mVertexDataManager && !mIndexDataManager); ASSERT(!mVertexDataManager && !mIndexDataManager);
......
...@@ -89,6 +89,14 @@ size_t ComputeGenericMemoryUsage(ResourceType resourceType, ID3D11DeviceChild *r ...@@ -89,6 +89,14 @@ size_t ComputeGenericMemoryUsage(ResourceType resourceType, ID3D11DeviceChild *r
} }
HRESULT CreateResource(ID3D11Device *device, 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_BUFFER_DESC *desc,
const D3D11_SUBRESOURCE_DATA *initData, const D3D11_SUBRESOURCE_DATA *initData,
ID3D11Buffer **buffer) ID3D11Buffer **buffer)
...@@ -97,6 +105,14 @@ HRESULT CreateResource(ID3D11Device *device, ...@@ -97,6 +105,14 @@ HRESULT CreateResource(ID3D11Device *device,
} }
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, const D3D11_DEPTH_STENCIL_VIEW_DESC *desc,
ID3D11Resource *resource, ID3D11Resource *resource,
ID3D11DepthStencilView **resourceOut) ID3D11DepthStencilView **resourceOut)
...@@ -105,6 +121,14 @@ HRESULT CreateResource(ID3D11Device *device, ...@@ -105,6 +121,14 @@ HRESULT CreateResource(ID3D11Device *device,
} }
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, const D3D11_RENDER_TARGET_VIEW_DESC *desc,
ID3D11Resource *resource, ID3D11Resource *resource,
ID3D11RenderTargetView **renderTargetView) ID3D11RenderTargetView **renderTargetView)
...@@ -113,6 +137,14 @@ HRESULT CreateResource(ID3D11Device *device, ...@@ -113,6 +137,14 @@ HRESULT CreateResource(ID3D11Device *device,
} }
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, const D3D11_SHADER_RESOURCE_VIEW_DESC *desc,
ID3D11Resource *resource, ID3D11Resource *resource,
ID3D11ShaderResourceView **resourceOut) ID3D11ShaderResourceView **resourceOut)
......
...@@ -26,11 +26,15 @@ class TextureHelper11; ...@@ -26,11 +26,15 @@ class TextureHelper11;
// Format: ResourceType, D3D11 type, DESC type, init data type. // Format: ResourceType, D3D11 type, DESC type, init data type.
#define ANGLE_RESOURCE_TYPE_OP(NAME, OP) \ #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, 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, \ OP(NAME, DepthStencilView, ID3D11DepthStencilView, D3D11_DEPTH_STENCIL_VIEW_DESC, \
ID3D11Resource) \ ID3D11Resource) \
OP(NAME, RasterizerState, ID3D11RasterizerState, D3D11_RASTERIZER_DESC, void) \
OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \ OP(NAME, RenderTargetView, ID3D11RenderTargetView, D3D11_RENDER_TARGET_VIEW_DESC, \
ID3D11Resource) \ ID3D11Resource) \
OP(NAME, SamplerState, ID3D11SamplerState, D3D11_SAMPLER_DESC, void) \
OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \ OP(NAME, ShaderResourceView, ID3D11ShaderResourceView, D3D11_SHADER_RESOURCE_VIEW_DESC, \
ID3D11Resource) \ ID3D11Resource) \
OP(NAME, Texture2D, ID3D11Texture2D, D3D11_TEXTURE2D_DESC, const D3D11_SUBRESOURCE_DATA) \ OP(NAME, Texture2D, ID3D11Texture2D, D3D11_TEXTURE2D_DESC, const D3D11_SUBRESOURCE_DATA) \
......
...@@ -81,11 +81,11 @@ SwapChain11::SwapChain11(Renderer11 *renderer, ...@@ -81,11 +81,11 @@ SwapChain11::SwapChain11(Renderer11 *renderer,
mDepthStencilDSView(), mDepthStencilDSView(),
mDepthStencilSRView(), mDepthStencilSRView(),
mQuadVB(), mQuadVB(),
mPassThroughSampler(nullptr), mPassThroughSampler(),
mPassThroughIL(nullptr), mPassThroughIL(nullptr),
mPassThroughVS(nullptr), mPassThroughVS(nullptr),
mPassThroughPS(nullptr), mPassThroughPS(nullptr),
mPassThroughRS(nullptr), mPassThroughRS(),
mColorRenderTarget(this, renderer, false), mColorRenderTarget(this, renderer, false),
mDepthStencilRenderTarget(this, renderer, true), mDepthStencilRenderTarget(this, renderer, true),
mEGLSamples(samples) mEGLSamples(samples)
...@@ -121,11 +121,11 @@ void SwapChain11::release() ...@@ -121,11 +121,11 @@ void SwapChain11::release()
mDepthStencilDSView.reset(); mDepthStencilDSView.reset();
mDepthStencilSRView.reset(); mDepthStencilSRView.reset();
mQuadVB.reset(); mQuadVB.reset();
SafeRelease(mPassThroughSampler); mPassThroughSampler.reset();
SafeRelease(mPassThroughIL); SafeRelease(mPassThroughIL);
SafeRelease(mPassThroughVS); SafeRelease(mPassThroughVS);
SafeRelease(mPassThroughPS); SafeRelease(mPassThroughPS);
SafeRelease(mPassThroughRS); mPassThroughRS.reset();
if (!mAppCreatedShareHandle) if (!mAppCreatedShareHandle)
{ {
...@@ -611,7 +611,7 @@ void SwapChain11::initPassThroughResources() ...@@ -611,7 +611,7 @@ void SwapChain11::initPassThroughResources()
ASSERT(device != nullptr); ASSERT(device != nullptr);
// Make sure our resources are all not allocated, when we create // 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); ASSERT(mPassThroughIL == nullptr && mPassThroughVS == nullptr && mPassThroughPS == nullptr);
D3D11_BUFFER_DESC vbDesc; D3D11_BUFFER_DESC vbDesc;
...@@ -641,9 +641,9 @@ void SwapChain11::initPassThroughResources() ...@@ -641,9 +641,9 @@ void SwapChain11::initPassThroughResources()
samplerDesc.MinLOD = 0; samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
HRESULT result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); err = mRenderer->allocateResource(samplerDesc, &mPassThroughSampler);
ASSERT(SUCCEEDED(result)); ASSERT(!err.isError());
d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); mPassThroughSampler.setDebugName("Swap chain pass through sampler");
D3D11_INPUT_ELEMENT_DESC quadLayout[] = D3D11_INPUT_ELEMENT_DESC quadLayout[] =
{ {
...@@ -651,7 +651,8 @@ void SwapChain11::initPassThroughResources() ...@@ -651,7 +651,8 @@ void SwapChain11::initPassThroughResources()
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "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)); ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout");
...@@ -686,9 +687,10 @@ void SwapChain11::initPassThroughResources() ...@@ -686,9 +687,10 @@ void SwapChain11::initPassThroughResources()
rasterizerDesc.ScissorEnable = FALSE; rasterizerDesc.ScissorEnable = FALSE;
rasterizerDesc.MultisampleEnable = FALSE; rasterizerDesc.MultisampleEnable = FALSE;
rasterizerDesc.AntialiasedLineEnable = FALSE; rasterizerDesc.AntialiasedLineEnable = FALSE;
result = device->CreateRasterizerState(&rasterizerDesc, &mPassThroughRS);
ASSERT(SUCCEEDED(result)); err = mRenderer->allocateResource(rasterizerDesc, &mPassThroughRS);
d3d11::SetDebugName(mPassThroughRS, "Swap chain pass through rasterizer state"); ASSERT(!err.isError());
mPassThroughRS.setDebugName("Swap chain pass through rasterizer state");
mPassThroughResourcesInit = true; mPassThroughResourcesInit = true;
} }
...@@ -777,7 +779,7 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width, ...@@ -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 }; static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
deviceContext->OMSetBlendState(nullptr, blendFactor, 0xFFFFFFF); deviceContext->OMSetBlendState(nullptr, blendFactor, 0xFFFFFFF);
deviceContext->RSSetState(mPassThroughRS); deviceContext->RSSetState(mPassThroughRS.get());
// Apply shaders // Apply shaders
deviceContext->IASetInputLayout(mPassThroughIL); deviceContext->IASetInputLayout(mPassThroughIL);
...@@ -803,7 +805,9 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width, ...@@ -803,7 +805,9 @@ EGLint SwapChain11::copyOffscreenToBackbuffer(EGLint x, EGLint y, EGLint width,
// Apply textures // Apply textures
stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, mOffscreenSRView.get()); stateManager->setShaderResource(gl::SAMPLER_PIXEL, 0, mOffscreenSRView.get());
deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler);
ID3D11SamplerState *samplerState = mPassThroughSampler.get();
deviceContext->PSSetSamplers(0, 1, &samplerState);
// Draw // Draw
deviceContext->Draw(4, 0); deviceContext->Draw(4, 0);
......
...@@ -99,11 +99,11 @@ class SwapChain11 final : public SwapChainD3D ...@@ -99,11 +99,11 @@ class SwapChain11 final : public SwapChainD3D
d3d11::SharedSRV mDepthStencilSRView; d3d11::SharedSRV mDepthStencilSRView;
d3d11::Buffer mQuadVB; d3d11::Buffer mQuadVB;
ID3D11SamplerState *mPassThroughSampler; d3d11::SamplerState mPassThroughSampler;
ID3D11InputLayout *mPassThroughIL; ID3D11InputLayout *mPassThroughIL;
ID3D11VertexShader *mPassThroughVS; ID3D11VertexShader *mPassThroughVS;
ID3D11PixelShader *mPassThroughPS; ID3D11PixelShader *mPassThroughPS;
ID3D11RasterizerState *mPassThroughRS; d3d11::RasterizerState mPassThroughRS;
SurfaceRenderTarget11 mColorRenderTarget; SurfaceRenderTarget11 mColorRenderTarget;
SurfaceRenderTarget11 mDepthStencilRenderTarget; SurfaceRenderTarget11 mDepthStencilRenderTarget;
......
...@@ -1947,18 +1947,9 @@ LazyBlendState::LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugNa ...@@ -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); return resolveImpl(renderer, mDesc, mDebugName);
if (mResource == nullptr)
{
HRESULT result = device->CreateBlendState(&mDesc, &mResource);
ASSERT(SUCCEEDED(result));
d3d11::SetDebugName(mResource, mDebugName);
}
return mResource;
} }
angle::WorkaroundsD3D GenerateWorkarounds(const Renderer11DeviceCaps &deviceCaps, angle::WorkaroundsD3D GenerateWorkarounds(const Renderer11DeviceCaps &deviceCaps,
......
...@@ -240,6 +240,37 @@ class LazyResource : angle::NonCopyable ...@@ -240,6 +240,37 @@ class LazyResource : angle::NonCopyable
ID3D11Device *mAssociatedDevice; 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> template <typename ResourceType>
void LazyResource<ResourceType>::checkAssociatedDevice(ID3D11Device *device) void LazyResource<ResourceType>::checkAssociatedDevice(ID3D11Device *device)
{ {
...@@ -320,12 +351,12 @@ class LazyInputLayout final : public LazyResource<ID3D11InputLayout> ...@@ -320,12 +351,12 @@ class LazyInputLayout final : public LazyResource<ID3D11InputLayout>
const char *mDebugName; const char *mDebugName;
}; };
class LazyBlendState final : public LazyResource<ID3D11BlendState> class LazyBlendState final : public LazyResource2<ResourceType::BlendState>
{ {
public: public:
LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugName); LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugName);
ID3D11BlendState *resolve(ID3D11Device *device) override; gl::Error resolve(Renderer11 *renderer);
private: private:
D3D11_BLEND_DESC mDesc; 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