• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render
8 // state objects.
9 
10 #include "libANGLE/renderer/d3d/d3d11/RenderStateCache.h"
11 
12 #include <float.h>
13 
14 #include "common/Color.h"
15 #include "common/debug.h"
16 #include "libANGLE/Context.h"
17 #include "libANGLE/Framebuffer.h"
18 #include "libANGLE/FramebufferAttachment.h"
19 #include "libANGLE/renderer/d3d/d3d11/Context11.h"
20 #include "libANGLE/renderer/d3d/d3d11/Framebuffer11.h"
21 #include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
22 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
23 
24 namespace rx
25 {
26 using namespace gl_d3d11;
27 
RenderStateCache()28 RenderStateCache::RenderStateCache()
29     : mBlendStateCache(kMaxStates),
30       mRasterizerStateCache(kMaxStates),
31       mDepthStencilStateCache(kMaxStates),
32       mSamplerStateCache(kMaxStates)
33 {}
34 
~RenderStateCache()35 RenderStateCache::~RenderStateCache() {}
36 
clear()37 void RenderStateCache::clear()
38 {
39     mBlendStateCache.Clear();
40     mRasterizerStateCache.Clear();
41     mDepthStencilStateCache.Clear();
42     mSamplerStateCache.Clear();
43 }
44 
45 // static
GetBlendStateKey(const gl::Context * context,Framebuffer11 * framebuffer11,const gl::BlendStateExt & blendStateExt,bool sampleAlphaToCoverage)46 d3d11::BlendStateKey RenderStateCache::GetBlendStateKey(const gl::Context *context,
47                                                         Framebuffer11 *framebuffer11,
48                                                         const gl::BlendStateExt &blendStateExt,
49                                                         bool sampleAlphaToCoverage)
50 {
51     d3d11::BlendStateKey key;
52     // All fields of the BlendStateExt inside the key should be initialized for the caching to
53     // work correctly. Due to mrt_perf_workaround, the actual indices of active draw buffers may be
54     // different, so both arrays should be tracked.
55     key.blendStateExt                      = gl::BlendStateExt(blendStateExt.getDrawBufferCount());
56     const gl::AttachmentList &colorbuffers = framebuffer11->getColorAttachmentsForRender(context);
57     const gl::DrawBufferMask colorAttachmentsForRenderMask =
58         framebuffer11->getLastColorAttachmentsForRenderMask();
59 
60     ASSERT(blendStateExt.getDrawBufferCount() <= colorAttachmentsForRenderMask.size());
61     ASSERT(colorbuffers.size() == colorAttachmentsForRenderMask.count());
62 
63     size_t keyBlendIndex = 0;
64 
65     // With blending disabled, factors and equations are ignored when building
66     // D3D11_RENDER_TARGET_BLEND_DESC, so we can reduce the amount of unique keys by
67     // enforcing default values.
68     for (size_t sourceIndex : colorAttachmentsForRenderMask)
69     {
70         ASSERT(keyBlendIndex < colorbuffers.size());
71         const gl::FramebufferAttachment *attachment = colorbuffers[keyBlendIndex];
72 
73         // Do not set blend state for null attachments that may be present when
74         // mrt_perf_workaround is disabled.
75         if (attachment == nullptr)
76         {
77             keyBlendIndex++;
78             continue;
79         }
80 
81         const uint8_t colorMask = blendStateExt.getColorMaskIndexed(sourceIndex);
82 
83         const gl::InternalFormat &internalFormat = *attachment->getFormat().info;
84 
85         key.blendStateExt.setColorMaskIndexed(keyBlendIndex,
86                                               gl_d3d11::GetColorMask(internalFormat) & colorMask);
87         key.rtvMax = static_cast<uint16_t>(keyBlendIndex) + 1;
88 
89         // Some D3D11 drivers produce unexpected results when blending is enabled for integer
90         // attachments. Per OpenGL ES spec, it must be ignored anyway. When blending is disabled,
91         // the state remains default to reduce the number of unique keys.
92         if (blendStateExt.getEnabledMask().test(sourceIndex) && !internalFormat.isInt())
93         {
94             key.blendStateExt.setEnabledIndexed(keyBlendIndex, true);
95             key.blendStateExt.setEquationsIndexed(keyBlendIndex, sourceIndex, blendStateExt);
96             key.blendStateExt.setFactorsIndexed(keyBlendIndex, sourceIndex, blendStateExt);
97         }
98         keyBlendIndex++;
99     }
100 
101     key.sampleAlphaToCoverage = sampleAlphaToCoverage ? 1 : 0;
102     return key;
103 }
104 
getBlendState(const gl::Context * context,Renderer11 * renderer,const d3d11::BlendStateKey & key,const d3d11::BlendState ** outBlendState)105 angle::Result RenderStateCache::getBlendState(const gl::Context *context,
106                                               Renderer11 *renderer,
107                                               const d3d11::BlendStateKey &key,
108                                               const d3d11::BlendState **outBlendState)
109 {
110     auto keyIter = mBlendStateCache.Get(key);
111     if (keyIter != mBlendStateCache.end())
112     {
113         *outBlendState = &keyIter->second;
114         return angle::Result::Continue;
115     }
116 
117     TrimCache(kMaxStates, kGCLimit, "blend state", &mBlendStateCache);
118 
119     // Create a new blend state and insert it into the cache
120     D3D11_BLEND_DESC blendDesc             = {};  // avoid undefined fields
121     const gl::BlendStateExt &blendStateExt = key.blendStateExt;
122 
123     blendDesc.AlphaToCoverageEnable  = key.sampleAlphaToCoverage != 0 ? TRUE : FALSE;
124     blendDesc.IndependentBlendEnable = key.rtvMax > 1 ? TRUE : FALSE;
125 
126     // D3D11 API always accepts an array of blend states. Its validity depends on the hardware
127     // feature level. Given that we do not expose GL entrypoints that set per-buffer blend states on
128     // systems lower than FL10_1, this array will be always valid.
129 
130     for (size_t i = 0; i < blendStateExt.getDrawBufferCount(); i++)
131     {
132         D3D11_RENDER_TARGET_BLEND_DESC &rtDesc = blendDesc.RenderTarget[i];
133 
134         if (blendStateExt.getEnabledMask().test(i))
135         {
136             rtDesc.BlendEnable = true;
137             rtDesc.SrcBlend =
138                 gl_d3d11::ConvertBlendFunc(blendStateExt.getSrcColorIndexed(i), false);
139             rtDesc.DestBlend =
140                 gl_d3d11::ConvertBlendFunc(blendStateExt.getDstColorIndexed(i), false);
141             rtDesc.BlendOp = gl_d3d11::ConvertBlendOp(blendStateExt.getEquationColorIndexed(i));
142             rtDesc.SrcBlendAlpha =
143                 gl_d3d11::ConvertBlendFunc(blendStateExt.getSrcAlphaIndexed(i), true);
144             rtDesc.DestBlendAlpha =
145                 gl_d3d11::ConvertBlendFunc(blendStateExt.getDstAlphaIndexed(i), true);
146             rtDesc.BlendOpAlpha =
147                 gl_d3d11::ConvertBlendOp(blendStateExt.getEquationAlphaIndexed(i));
148         }
149 
150         // blendStateExt.colorMask follows the same packing scheme as
151         // D3D11_RENDER_TARGET_BLEND_DESC.RenderTargetWriteMask
152         rtDesc.RenderTargetWriteMask = blendStateExt.getColorMaskIndexed(i);
153     }
154 
155     d3d11::BlendState d3dBlendState;
156     ANGLE_TRY(renderer->allocateResource(GetImplAs<Context11>(context), blendDesc, &d3dBlendState));
157     const auto &iter = mBlendStateCache.Put(key, std::move(d3dBlendState));
158 
159     *outBlendState = &iter->second;
160 
161     return angle::Result::Continue;
162 }
163 
getRasterizerState(const gl::Context * context,Renderer11 * renderer,const gl::RasterizerState & rasterState,bool scissorEnabled,ID3D11RasterizerState ** outRasterizerState)164 angle::Result RenderStateCache::getRasterizerState(const gl::Context *context,
165                                                    Renderer11 *renderer,
166                                                    const gl::RasterizerState &rasterState,
167                                                    bool scissorEnabled,
168                                                    ID3D11RasterizerState **outRasterizerState)
169 {
170     d3d11::RasterizerStateKey key;
171     key.rasterizerState = rasterState;
172     key.scissorEnabled  = scissorEnabled ? 1 : 0;
173 
174     auto keyIter = mRasterizerStateCache.Get(key);
175     if (keyIter != mRasterizerStateCache.end())
176     {
177         *outRasterizerState = keyIter->second.get();
178         return angle::Result::Continue;
179     }
180 
181     TrimCache(kMaxStates, kGCLimit, "rasterizer state", &mRasterizerStateCache);
182 
183     D3D11_CULL_MODE cullMode =
184         gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
185 
186     // Disable culling if drawing points
187     if (rasterState.pointDrawMode)
188     {
189         cullMode = D3D11_CULL_NONE;
190     }
191 
192     D3D11_RASTERIZER_DESC rasterDesc;
193     rasterDesc.FillMode =
194         rasterState.polygonMode == gl::PolygonMode::Fill ? D3D11_FILL_SOLID : D3D11_FILL_WIREFRAME;
195     rasterDesc.CullMode              = cullMode;
196     rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE : TRUE;
197     rasterDesc.DepthClipEnable       = !rasterState.depthClamp;
198     rasterDesc.ScissorEnable         = scissorEnabled ? TRUE : FALSE;
199     rasterDesc.MultisampleEnable     = rasterState.multiSample;
200     rasterDesc.AntialiasedLineEnable = FALSE;
201 
202     if (rasterState.isPolygonOffsetEnabled())
203     {
204         rasterDesc.DepthBias            = (INT)rasterState.polygonOffsetUnits;
205         rasterDesc.DepthBiasClamp       = rasterState.polygonOffsetClamp;
206         rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
207     }
208     else
209     {
210         rasterDesc.DepthBias            = 0;
211         rasterDesc.DepthBiasClamp       = 0.0f;
212         rasterDesc.SlopeScaledDepthBias = 0.0f;
213     }
214 
215     d3d11::RasterizerState dx11RasterizerState;
216     ANGLE_TRY(renderer->allocateResource(GetImplAs<Context11>(context), rasterDesc,
217                                          &dx11RasterizerState));
218     *outRasterizerState = dx11RasterizerState.get();
219     mRasterizerStateCache.Put(key, std::move(dx11RasterizerState));
220 
221     return angle::Result::Continue;
222 }
223 
getDepthStencilState(const gl::Context * context,Renderer11 * renderer,const gl::DepthStencilState & glState,const d3d11::DepthStencilState ** outDSState)224 angle::Result RenderStateCache::getDepthStencilState(const gl::Context *context,
225                                                      Renderer11 *renderer,
226                                                      const gl::DepthStencilState &glState,
227                                                      const d3d11::DepthStencilState **outDSState)
228 {
229     auto keyIter = mDepthStencilStateCache.Get(glState);
230     if (keyIter != mDepthStencilStateCache.end())
231     {
232         *outDSState = &keyIter->second;
233         return angle::Result::Continue;
234     }
235 
236     TrimCache(kMaxStates, kGCLimit, "depth stencil state", &mDepthStencilStateCache);
237 
238     D3D11_DEPTH_STENCIL_DESC dsDesc     = {};
239     dsDesc.DepthEnable                  = glState.depthTest ? TRUE : FALSE;
240     dsDesc.DepthWriteMask               = ConvertDepthMask(glState.depthMask);
241     dsDesc.DepthFunc                    = ConvertComparison(glState.depthFunc);
242     dsDesc.StencilEnable                = glState.stencilTest ? TRUE : FALSE;
243     dsDesc.StencilReadMask              = ConvertStencilMask(glState.stencilMask);
244     dsDesc.StencilWriteMask             = ConvertStencilMask(glState.stencilWritemask);
245     dsDesc.FrontFace.StencilFailOp      = ConvertStencilOp(glState.stencilFail);
246     dsDesc.FrontFace.StencilDepthFailOp = ConvertStencilOp(glState.stencilPassDepthFail);
247     dsDesc.FrontFace.StencilPassOp      = ConvertStencilOp(glState.stencilPassDepthPass);
248     dsDesc.FrontFace.StencilFunc        = ConvertComparison(glState.stencilFunc);
249     dsDesc.BackFace.StencilFailOp       = ConvertStencilOp(glState.stencilBackFail);
250     dsDesc.BackFace.StencilDepthFailOp  = ConvertStencilOp(glState.stencilBackPassDepthFail);
251     dsDesc.BackFace.StencilPassOp       = ConvertStencilOp(glState.stencilBackPassDepthPass);
252     dsDesc.BackFace.StencilFunc         = ConvertComparison(glState.stencilBackFunc);
253 
254     d3d11::DepthStencilState dx11DepthStencilState;
255     ANGLE_TRY(
256         renderer->allocateResource(GetImplAs<Context11>(context), dsDesc, &dx11DepthStencilState));
257     const auto &iter = mDepthStencilStateCache.Put(glState, std::move(dx11DepthStencilState));
258 
259     *outDSState = &iter->second;
260 
261     return angle::Result::Continue;
262 }
263 
getSamplerState(const gl::Context * context,Renderer11 * renderer,const gl::SamplerState & samplerState,ID3D11SamplerState ** outSamplerState)264 angle::Result RenderStateCache::getSamplerState(const gl::Context *context,
265                                                 Renderer11 *renderer,
266                                                 const gl::SamplerState &samplerState,
267                                                 ID3D11SamplerState **outSamplerState)
268 {
269     auto keyIter = mSamplerStateCache.Get(samplerState);
270     if (keyIter != mSamplerStateCache.end())
271     {
272         *outSamplerState = keyIter->second.get();
273         return angle::Result::Continue;
274     }
275 
276     TrimCache(kMaxStates, kGCLimit, "sampler state", &mSamplerStateCache);
277 
278     const auto &featureLevel = renderer->getRenderer11DeviceCaps().featureLevel;
279 
280     D3D11_SAMPLER_DESC samplerDesc;
281     samplerDesc.Filter =
282         gl_d3d11::ConvertFilter(samplerState.getMinFilter(), samplerState.getMagFilter(),
283                                 samplerState.getMaxAnisotropy(), samplerState.getCompareMode());
284     samplerDesc.AddressU   = gl_d3d11::ConvertTextureWrap(samplerState.getWrapS());
285     samplerDesc.AddressV   = gl_d3d11::ConvertTextureWrap(samplerState.getWrapT());
286     samplerDesc.AddressW   = gl_d3d11::ConvertTextureWrap(samplerState.getWrapR());
287     samplerDesc.MipLODBias = 0;
288     samplerDesc.MaxAnisotropy =
289         gl_d3d11::ConvertMaxAnisotropy(samplerState.getMaxAnisotropy(), featureLevel);
290     samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.getCompareFunc());
291     samplerDesc.BorderColor[0] = samplerState.getBorderColor().colorF.red;
292     samplerDesc.BorderColor[1] = samplerState.getBorderColor().colorF.green;
293     samplerDesc.BorderColor[2] = samplerState.getBorderColor().colorF.blue;
294     samplerDesc.BorderColor[3] = samplerState.getBorderColor().colorF.alpha;
295     samplerDesc.MinLOD         = samplerState.getMinLod();
296     samplerDesc.MaxLOD         = samplerState.getMaxLod();
297 
298     if (featureLevel <= D3D_FEATURE_LEVEL_9_3)
299     {
300         // Check that maxLOD is nearly FLT_MAX (1000.0f is the default), since 9_3 doesn't support
301         // anything other than FLT_MAX. Note that Feature Level 9_* only supports GL ES 2.0, so the
302         // consumer of ANGLE can't modify the Max LOD themselves.
303         ASSERT(samplerState.getMaxLod() >= 999.9f);
304 
305         // Now just set MaxLOD to FLT_MAX. Other parts of the renderer (e.g. the non-zero max LOD
306         // workaround) should take account of this.
307         samplerDesc.MaxLOD = FLT_MAX;
308     }
309 
310     d3d11::SamplerState dx11SamplerState;
311     ANGLE_TRY(
312         renderer->allocateResource(GetImplAs<Context11>(context), samplerDesc, &dx11SamplerState));
313     *outSamplerState = dx11SamplerState.get();
314     mSamplerStateCache.Put(samplerState, std::move(dx11SamplerState));
315 
316     return angle::Result::Continue;
317 }
318 
319 }  // namespace rx
320