xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/d3d/d3d11/renderer11_utils.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 // renderer11_utils.cpp: Conversion functions and other utility routines
8 // specific to the D3D11 renderer.
9 
10 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
11 
12 #include <algorithm>
13 
14 #include "common/debug.h"
15 #include "libANGLE/Buffer.h"
16 #include "libANGLE/Context.h"
17 #include "libANGLE/Framebuffer.h"
18 #include "libANGLE/Program.h"
19 #include "libANGLE/State.h"
20 #include "libANGLE/VertexArray.h"
21 #include "libANGLE/formatutils.h"
22 #include "libANGLE/renderer/d3d/BufferD3D.h"
23 #include "libANGLE/renderer/d3d/FramebufferD3D.h"
24 #include "libANGLE/renderer/d3d/d3d11/Context11.h"
25 #include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
26 #include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
27 #include "libANGLE/renderer/d3d/d3d11/formatutils11.h"
28 #include "libANGLE/renderer/d3d/d3d11/texture_format_table.h"
29 #include "libANGLE/renderer/driver_utils.h"
30 #include "libANGLE/renderer/dxgi_support_table.h"
31 #include "platform/PlatformMethods.h"
32 #include "platform/autogen/FeaturesD3D_autogen.h"
33 
34 namespace rx
35 {
36 
37 namespace d3d11_gl
38 {
39 namespace
40 {
41 // TODO([email protected]): Get a more accurate limit.
42 static D3D_FEATURE_LEVEL kMinimumFeatureLevelForES31 = D3D_FEATURE_LEVEL_11_0;
43 
44 // Helper functor for querying DXGI support. Saves passing the parameters repeatedly.
45 class DXGISupportHelper : angle::NonCopyable
46 {
47   public:
DXGISupportHelper(ID3D11Device * device,D3D_FEATURE_LEVEL featureLevel)48     DXGISupportHelper(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel)
49         : mDevice(device), mFeatureLevel(featureLevel)
50     {}
51 
query(DXGI_FORMAT dxgiFormat,UINT supportMask)52     bool query(DXGI_FORMAT dxgiFormat, UINT supportMask)
53     {
54         if (dxgiFormat == DXGI_FORMAT_UNKNOWN)
55             return false;
56 
57         auto dxgiSupport = d3d11::GetDXGISupport(dxgiFormat, mFeatureLevel);
58 
59         UINT supportedBits = dxgiSupport.alwaysSupportedFlags;
60 
61         if ((dxgiSupport.optionallySupportedFlags & supportMask) != 0)
62         {
63             UINT formatSupport;
64             if (SUCCEEDED(mDevice->CheckFormatSupport(dxgiFormat, &formatSupport)))
65             {
66                 supportedBits |= (formatSupport & supportMask);
67             }
68             else
69             {
70                 // TODO(jmadill): find out why we fail this call sometimes in FL9_3
71                 // ERR() << "Error checking format support for format 0x" << std::hex << dxgiFormat;
72             }
73         }
74 
75         return ((supportedBits & supportMask) == supportMask);
76     }
77 
78   private:
79     ID3D11Device *mDevice;
80     D3D_FEATURE_LEVEL mFeatureLevel;
81 };
82 
GenerateTextureFormatCaps(gl::Version maxClientVersion,GLenum internalFormat,ID3D11Device * device,const Renderer11DeviceCaps & renderer11DeviceCaps)83 gl::TextureCaps GenerateTextureFormatCaps(gl::Version maxClientVersion,
84                                           GLenum internalFormat,
85                                           ID3D11Device *device,
86                                           const Renderer11DeviceCaps &renderer11DeviceCaps)
87 {
88     gl::TextureCaps textureCaps;
89 
90     DXGISupportHelper support(device, renderer11DeviceCaps.featureLevel);
91     const d3d11::Format &formatInfo = d3d11::Format::Get(internalFormat, renderer11DeviceCaps);
92 
93     const gl::InternalFormat &internalFormatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
94 
95     UINT texSupportMask = D3D11_FORMAT_SUPPORT_TEXTURE2D;
96     if (internalFormatInfo.depthBits == 0 && internalFormatInfo.stencilBits == 0)
97     {
98         texSupportMask |= D3D11_FORMAT_SUPPORT_TEXTURECUBE;
99         if (maxClientVersion.major > 2)
100         {
101             texSupportMask |= D3D11_FORMAT_SUPPORT_TEXTURE3D;
102         }
103     }
104 
105     textureCaps.texturable = support.query(formatInfo.texFormat, texSupportMask);
106     textureCaps.filterable =
107         support.query(formatInfo.srvFormat, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE);
108     textureCaps.textureAttachment =
109         (support.query(formatInfo.rtvFormat, D3D11_FORMAT_SUPPORT_RENDER_TARGET)) ||
110         (support.query(formatInfo.dsvFormat, D3D11_FORMAT_SUPPORT_DEPTH_STENCIL));
111     textureCaps.renderbuffer = textureCaps.textureAttachment;
112     textureCaps.blendable    = textureCaps.renderbuffer;
113 
114     DXGI_FORMAT renderFormat = DXGI_FORMAT_UNKNOWN;
115     if (formatInfo.dsvFormat != DXGI_FORMAT_UNKNOWN)
116     {
117         renderFormat = formatInfo.dsvFormat;
118     }
119     else if (formatInfo.rtvFormat != DXGI_FORMAT_UNKNOWN)
120     {
121         renderFormat = formatInfo.rtvFormat;
122     }
123     if (renderFormat != DXGI_FORMAT_UNKNOWN &&
124         support.query(renderFormat, D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
125     {
126         // Assume 1x
127         textureCaps.sampleCounts.insert(1);
128 
129         for (unsigned int sampleCount = 2; sampleCount <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT;
130              sampleCount *= 2)
131         {
132             UINT qualityCount = 0;
133             if (SUCCEEDED(device->CheckMultisampleQualityLevels(renderFormat, sampleCount,
134                                                                 &qualityCount)))
135             {
136                 // Assume we always support lower sample counts
137                 if (qualityCount == 0)
138                 {
139                     break;
140                 }
141                 textureCaps.sampleCounts.insert(sampleCount);
142             }
143         }
144     }
145 
146     return textureCaps;
147 }
148 
GetNPOTTextureSupport(D3D_FEATURE_LEVEL featureLevel)149 bool GetNPOTTextureSupport(D3D_FEATURE_LEVEL featureLevel)
150 {
151     switch (featureLevel)
152     {
153         case D3D_FEATURE_LEVEL_11_1:
154         case D3D_FEATURE_LEVEL_11_0:
155         case D3D_FEATURE_LEVEL_10_1:
156         case D3D_FEATURE_LEVEL_10_0:
157             return true;
158 
159         // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876.aspx
160         case D3D_FEATURE_LEVEL_9_3:
161         case D3D_FEATURE_LEVEL_9_2:
162         case D3D_FEATURE_LEVEL_9_1:
163             return false;
164 
165         default:
166             UNREACHABLE();
167             return false;
168     }
169 }
170 
GetMaximumAnisotropy(D3D_FEATURE_LEVEL featureLevel)171 float GetMaximumAnisotropy(D3D_FEATURE_LEVEL featureLevel)
172 {
173     switch (featureLevel)
174     {
175         case D3D_FEATURE_LEVEL_11_1:
176         case D3D_FEATURE_LEVEL_11_0:
177             return D3D11_MAX_MAXANISOTROPY;
178 
179         case D3D_FEATURE_LEVEL_10_1:
180         case D3D_FEATURE_LEVEL_10_0:
181             return D3D10_MAX_MAXANISOTROPY;
182 
183         // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876.aspx
184         case D3D_FEATURE_LEVEL_9_3:
185         case D3D_FEATURE_LEVEL_9_2:
186             return 16;
187 
188         case D3D_FEATURE_LEVEL_9_1:
189             return D3D_FL9_1_DEFAULT_MAX_ANISOTROPY;
190 
191         default:
192             UNREACHABLE();
193             return 0;
194     }
195 }
196 
GetOcclusionQuerySupport(D3D_FEATURE_LEVEL featureLevel)197 bool GetOcclusionQuerySupport(D3D_FEATURE_LEVEL featureLevel)
198 {
199     switch (featureLevel)
200     {
201         case D3D_FEATURE_LEVEL_11_1:
202         case D3D_FEATURE_LEVEL_11_0:
203         case D3D_FEATURE_LEVEL_10_1:
204         case D3D_FEATURE_LEVEL_10_0:
205             return true;
206 
207         // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476150.aspx
208         // ID3D11Device::CreateQuery
209         case D3D_FEATURE_LEVEL_9_3:
210         case D3D_FEATURE_LEVEL_9_2:
211             return true;
212         case D3D_FEATURE_LEVEL_9_1:
213             return false;
214 
215         default:
216             UNREACHABLE();
217             return false;
218     }
219 }
220 
GetEventQuerySupport(D3D_FEATURE_LEVEL featureLevel)221 bool GetEventQuerySupport(D3D_FEATURE_LEVEL featureLevel)
222 {
223     // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476150.aspx
224     // ID3D11Device::CreateQuery
225 
226     switch (featureLevel)
227     {
228         case D3D_FEATURE_LEVEL_11_1:
229         case D3D_FEATURE_LEVEL_11_0:
230         case D3D_FEATURE_LEVEL_10_1:
231         case D3D_FEATURE_LEVEL_10_0:
232         case D3D_FEATURE_LEVEL_9_3:
233         case D3D_FEATURE_LEVEL_9_2:
234         case D3D_FEATURE_LEVEL_9_1:
235             return true;
236 
237         default:
238             UNREACHABLE();
239             return false;
240     }
241 }
242 
GetInstancingSupport(D3D_FEATURE_LEVEL featureLevel)243 bool GetInstancingSupport(D3D_FEATURE_LEVEL featureLevel)
244 {
245     // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476150.aspx
246     // ID3D11Device::CreateInputLayout
247 
248     switch (featureLevel)
249     {
250         case D3D_FEATURE_LEVEL_11_1:
251         case D3D_FEATURE_LEVEL_11_0:
252         case D3D_FEATURE_LEVEL_10_1:
253         case D3D_FEATURE_LEVEL_10_0:
254             return true;
255 
256         // Feature Level 9_3 supports instancing, but slot 0 in the input layout must not be
257         // instanced.
258         // D3D9 has a similar restriction, where stream 0 must not be instanced.
259         // This restriction can be worked around by remapping any non-instanced slot to slot
260         // 0.
261         // This works because HLSL uses shader semantics to match the vertex inputs to the
262         // elements in the input layout, rather than the slots.
263         // Note that we only support instancing via ANGLE_instanced_array on 9_3, since 9_3
264         // doesn't support OpenGL ES 3.0
265         case D3D_FEATURE_LEVEL_9_3:
266             return true;
267 
268         case D3D_FEATURE_LEVEL_9_2:
269         case D3D_FEATURE_LEVEL_9_1:
270             return false;
271 
272         default:
273             UNREACHABLE();
274             return false;
275     }
276 }
277 
GetFramebufferMultisampleSupport(D3D_FEATURE_LEVEL featureLevel)278 bool GetFramebufferMultisampleSupport(D3D_FEATURE_LEVEL featureLevel)
279 {
280     switch (featureLevel)
281     {
282         case D3D_FEATURE_LEVEL_11_1:
283         case D3D_FEATURE_LEVEL_11_0:
284         case D3D_FEATURE_LEVEL_10_1:
285         case D3D_FEATURE_LEVEL_10_0:
286             return true;
287 
288         case D3D_FEATURE_LEVEL_9_3:
289         case D3D_FEATURE_LEVEL_9_2:
290         case D3D_FEATURE_LEVEL_9_1:
291             return false;
292 
293         default:
294             UNREACHABLE();
295             return false;
296     }
297 }
298 
GetFramebufferBlitSupport(D3D_FEATURE_LEVEL featureLevel)299 bool GetFramebufferBlitSupport(D3D_FEATURE_LEVEL featureLevel)
300 {
301     switch (featureLevel)
302     {
303         case D3D_FEATURE_LEVEL_11_1:
304         case D3D_FEATURE_LEVEL_11_0:
305         case D3D_FEATURE_LEVEL_10_1:
306         case D3D_FEATURE_LEVEL_10_0:
307             return true;
308 
309         case D3D_FEATURE_LEVEL_9_3:
310         case D3D_FEATURE_LEVEL_9_2:
311         case D3D_FEATURE_LEVEL_9_1:
312             return false;
313 
314         default:
315             UNREACHABLE();
316             return false;
317     }
318 }
319 
GetDerivativeInstructionSupport(D3D_FEATURE_LEVEL featureLevel)320 bool GetDerivativeInstructionSupport(D3D_FEATURE_LEVEL featureLevel)
321 {
322     // http://msdn.microsoft.com/en-us/library/windows/desktop/bb509588.aspx states that
323     // shader model
324     // ps_2_x is required for the ddx (and other derivative functions).
325 
326     // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876.aspx states that
327     // feature level
328     // 9.3 supports shader model ps_2_x.
329 
330     switch (featureLevel)
331     {
332         case D3D_FEATURE_LEVEL_11_1:
333         case D3D_FEATURE_LEVEL_11_0:
334         case D3D_FEATURE_LEVEL_10_1:
335         case D3D_FEATURE_LEVEL_10_0:
336         case D3D_FEATURE_LEVEL_9_3:
337             return true;
338         case D3D_FEATURE_LEVEL_9_2:
339         case D3D_FEATURE_LEVEL_9_1:
340             return false;
341 
342         default:
343             UNREACHABLE();
344             return false;
345     }
346 }
347 
GetShaderTextureLODSupport(D3D_FEATURE_LEVEL featureLevel)348 bool GetShaderTextureLODSupport(D3D_FEATURE_LEVEL featureLevel)
349 {
350     switch (featureLevel)
351     {
352         case D3D_FEATURE_LEVEL_11_1:
353         case D3D_FEATURE_LEVEL_11_0:
354         case D3D_FEATURE_LEVEL_10_1:
355         case D3D_FEATURE_LEVEL_10_0:
356             return true;
357 
358         case D3D_FEATURE_LEVEL_9_3:
359         case D3D_FEATURE_LEVEL_9_2:
360         case D3D_FEATURE_LEVEL_9_1:
361             return false;
362 
363         default:
364             UNREACHABLE();
365             return false;
366     }
367 }
368 
GetMaximumSimultaneousRenderTargets(D3D_FEATURE_LEVEL featureLevel)369 int GetMaximumSimultaneousRenderTargets(D3D_FEATURE_LEVEL featureLevel)
370 {
371     // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476150.aspx
372     // ID3D11Device::CreateInputLayout
373 
374     switch (featureLevel)
375     {
376         case D3D_FEATURE_LEVEL_11_1:
377         case D3D_FEATURE_LEVEL_11_0:
378             return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT;
379 
380         case D3D_FEATURE_LEVEL_10_1:
381         case D3D_FEATURE_LEVEL_10_0:
382             return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT;
383 
384         case D3D_FEATURE_LEVEL_9_3:
385             return D3D_FL9_3_SIMULTANEOUS_RENDER_TARGET_COUNT;
386         case D3D_FEATURE_LEVEL_9_2:
387         case D3D_FEATURE_LEVEL_9_1:
388             return D3D_FL9_1_SIMULTANEOUS_RENDER_TARGET_COUNT;
389 
390         default:
391             UNREACHABLE();
392             return 0;
393     }
394 }
395 
GetMaximum2DTextureSize(D3D_FEATURE_LEVEL featureLevel)396 int GetMaximum2DTextureSize(D3D_FEATURE_LEVEL featureLevel)
397 {
398     switch (featureLevel)
399     {
400         case D3D_FEATURE_LEVEL_11_1:
401         case D3D_FEATURE_LEVEL_11_0:
402             return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
403 
404         case D3D_FEATURE_LEVEL_10_1:
405         case D3D_FEATURE_LEVEL_10_0:
406             return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION;
407 
408         case D3D_FEATURE_LEVEL_9_3:
409             return D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION;
410         case D3D_FEATURE_LEVEL_9_2:
411         case D3D_FEATURE_LEVEL_9_1:
412             return D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION;
413 
414         default:
415             UNREACHABLE();
416             return 0;
417     }
418 }
419 
GetMaximumCubeMapTextureSize(D3D_FEATURE_LEVEL featureLevel)420 int GetMaximumCubeMapTextureSize(D3D_FEATURE_LEVEL featureLevel)
421 {
422     switch (featureLevel)
423     {
424         case D3D_FEATURE_LEVEL_11_1:
425         case D3D_FEATURE_LEVEL_11_0:
426             return D3D11_REQ_TEXTURECUBE_DIMENSION;
427 
428         case D3D_FEATURE_LEVEL_10_1:
429         case D3D_FEATURE_LEVEL_10_0:
430             return D3D10_REQ_TEXTURECUBE_DIMENSION;
431 
432         case D3D_FEATURE_LEVEL_9_3:
433             return D3D_FL9_3_REQ_TEXTURECUBE_DIMENSION;
434         case D3D_FEATURE_LEVEL_9_2:
435         case D3D_FEATURE_LEVEL_9_1:
436             return D3D_FL9_1_REQ_TEXTURECUBE_DIMENSION;
437 
438         default:
439             UNREACHABLE();
440             return 0;
441     }
442 }
443 
GetMaximum2DTextureArraySize(D3D_FEATURE_LEVEL featureLevel)444 int GetMaximum2DTextureArraySize(D3D_FEATURE_LEVEL featureLevel)
445 {
446     switch (featureLevel)
447     {
448         case D3D_FEATURE_LEVEL_11_1:
449         case D3D_FEATURE_LEVEL_11_0:
450             return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION;
451 
452         case D3D_FEATURE_LEVEL_10_1:
453         case D3D_FEATURE_LEVEL_10_0:
454             return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION;
455 
456         case D3D_FEATURE_LEVEL_9_3:
457         case D3D_FEATURE_LEVEL_9_2:
458         case D3D_FEATURE_LEVEL_9_1:
459             return 0;
460 
461         default:
462             UNREACHABLE();
463             return 0;
464     }
465 }
466 
GetMaximum3DTextureSize(D3D_FEATURE_LEVEL featureLevel)467 int GetMaximum3DTextureSize(D3D_FEATURE_LEVEL featureLevel)
468 {
469     switch (featureLevel)
470     {
471         case D3D_FEATURE_LEVEL_11_1:
472         case D3D_FEATURE_LEVEL_11_0:
473             return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION;
474 
475         case D3D_FEATURE_LEVEL_10_1:
476         case D3D_FEATURE_LEVEL_10_0:
477             return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION;
478 
479         case D3D_FEATURE_LEVEL_9_3:
480         case D3D_FEATURE_LEVEL_9_2:
481         case D3D_FEATURE_LEVEL_9_1:
482             return D3D_FL9_1_REQ_TEXTURE3D_U_V_OR_W_DIMENSION;
483 
484         default:
485             UNREACHABLE();
486             return 0;
487     }
488 }
489 
GetMaximumViewportSize(D3D_FEATURE_LEVEL featureLevel)490 int GetMaximumViewportSize(D3D_FEATURE_LEVEL featureLevel)
491 {
492     switch (featureLevel)
493     {
494         case D3D_FEATURE_LEVEL_11_1:
495         case D3D_FEATURE_LEVEL_11_0:
496             return D3D11_VIEWPORT_BOUNDS_MAX;
497 
498         case D3D_FEATURE_LEVEL_10_1:
499         case D3D_FEATURE_LEVEL_10_0:
500             return D3D10_VIEWPORT_BOUNDS_MAX;
501 
502         // No constants for D3D11 Feature Level 9 viewport size limits, use the maximum
503         // texture sizes
504         case D3D_FEATURE_LEVEL_9_3:
505             return D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION;
506         case D3D_FEATURE_LEVEL_9_2:
507         case D3D_FEATURE_LEVEL_9_1:
508             return D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION;
509 
510         default:
511             UNREACHABLE();
512             return 0;
513     }
514 }
515 
GetMaximumDrawIndexedIndexCount(D3D_FEATURE_LEVEL featureLevel)516 int GetMaximumDrawIndexedIndexCount(D3D_FEATURE_LEVEL featureLevel)
517 {
518     // D3D11 allows up to 2^32 elements, but we report max signed int for convenience since
519     // that's what's
520     // returned from glGetInteger
521     static_assert(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32,
522                   "Unexpected D3D11 constant value.");
523     static_assert(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32,
524                   "Unexpected D3D11 constant value.");
525 
526     switch (featureLevel)
527     {
528         case D3D_FEATURE_LEVEL_11_1:
529         case D3D_FEATURE_LEVEL_11_0:
530         case D3D_FEATURE_LEVEL_10_1:
531         case D3D_FEATURE_LEVEL_10_0:
532             return std::numeric_limits<GLint>::max();
533 
534         case D3D_FEATURE_LEVEL_9_3:
535         case D3D_FEATURE_LEVEL_9_2:
536             return D3D_FL9_2_IA_PRIMITIVE_MAX_COUNT;
537         case D3D_FEATURE_LEVEL_9_1:
538             return D3D_FL9_1_IA_PRIMITIVE_MAX_COUNT;
539 
540         default:
541             UNREACHABLE();
542             return 0;
543     }
544 }
545 
GetMaximumDrawVertexCount(D3D_FEATURE_LEVEL featureLevel)546 int GetMaximumDrawVertexCount(D3D_FEATURE_LEVEL featureLevel)
547 {
548     // D3D11 allows up to 2^32 elements, but we report max signed int for convenience since
549     // that's what's
550     // returned from glGetInteger
551     static_assert(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
552     static_assert(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32, "Unexpected D3D11 constant value.");
553 
554     switch (featureLevel)
555     {
556         case D3D_FEATURE_LEVEL_11_1:
557         case D3D_FEATURE_LEVEL_11_0:
558         case D3D_FEATURE_LEVEL_10_1:
559         case D3D_FEATURE_LEVEL_10_0:
560             return std::numeric_limits<GLint>::max();
561 
562         case D3D_FEATURE_LEVEL_9_3:
563         case D3D_FEATURE_LEVEL_9_2:
564             return D3D_FL9_2_IA_PRIMITIVE_MAX_COUNT;
565         case D3D_FEATURE_LEVEL_9_1:
566             return D3D_FL9_1_IA_PRIMITIVE_MAX_COUNT;
567 
568         default:
569             UNREACHABLE();
570             return 0;
571     }
572 }
573 
GetMaximumVertexInputSlots(D3D_FEATURE_LEVEL featureLevel)574 int GetMaximumVertexInputSlots(D3D_FEATURE_LEVEL featureLevel)
575 {
576     switch (featureLevel)
577     {
578         case D3D_FEATURE_LEVEL_11_1:
579         case D3D_FEATURE_LEVEL_11_0:
580             return D3D11_STANDARD_VERTEX_ELEMENT_COUNT;
581 
582         case D3D_FEATURE_LEVEL_10_1:
583             return D3D10_1_STANDARD_VERTEX_ELEMENT_COUNT;
584         case D3D_FEATURE_LEVEL_10_0:
585             return D3D10_STANDARD_VERTEX_ELEMENT_COUNT;
586 
587         // From http://http://msdn.microsoft.com/en-us/library/windows/desktop/ff476876.aspx
588         // "Max Input Slots"
589         case D3D_FEATURE_LEVEL_9_3:
590         case D3D_FEATURE_LEVEL_9_2:
591         case D3D_FEATURE_LEVEL_9_1:
592             return 16;
593 
594         default:
595             UNREACHABLE();
596             return 0;
597     }
598 }
599 
GetMaximumVertexUniformVectors(D3D_FEATURE_LEVEL featureLevel)600 int GetMaximumVertexUniformVectors(D3D_FEATURE_LEVEL featureLevel)
601 {
602     switch (featureLevel)
603     {
604         case D3D_FEATURE_LEVEL_11_1:
605         case D3D_FEATURE_LEVEL_11_0:
606             return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT;
607 
608         case D3D_FEATURE_LEVEL_10_1:
609         case D3D_FEATURE_LEVEL_10_0:
610             return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT;
611 
612         // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476149.aspx
613         // ID3D11DeviceContext::VSSetConstantBuffers
614         case D3D_FEATURE_LEVEL_9_3:
615         case D3D_FEATURE_LEVEL_9_2:
616         case D3D_FEATURE_LEVEL_9_1:
617             return 255 - d3d11_gl::GetReservedVertexUniformVectors(featureLevel);
618 
619         default:
620             UNREACHABLE();
621             return 0;
622     }
623 }
624 
GetMaximumVertexUniformBlocks(D3D_FEATURE_LEVEL featureLevel)625 int GetMaximumVertexUniformBlocks(D3D_FEATURE_LEVEL featureLevel)
626 {
627     switch (featureLevel)
628     {
629         case D3D_FEATURE_LEVEL_11_1:
630         case D3D_FEATURE_LEVEL_11_0:
631             return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT -
632                    d3d11::RESERVED_CONSTANT_BUFFER_SLOT_COUNT;
633 
634         case D3D_FEATURE_LEVEL_10_1:
635         case D3D_FEATURE_LEVEL_10_0:
636             return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT -
637                    d3d11::RESERVED_CONSTANT_BUFFER_SLOT_COUNT;
638 
639         // Uniform blocks not supported on D3D11 Feature Level 9
640         case D3D_FEATURE_LEVEL_9_3:
641         case D3D_FEATURE_LEVEL_9_2:
642         case D3D_FEATURE_LEVEL_9_1:
643             return 0;
644 
645         default:
646             UNREACHABLE();
647             return 0;
648     }
649 }
650 
GetReservedVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel)651 int GetReservedVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel)
652 {
653     // According to The OpenGL ES Shading Language specifications
654     // (Language Version 1.00 section 10.16, Language Version 3.10 section 12.21)
655     // built-in special variables (e.g. gl_FragCoord, or gl_PointCoord)
656     // which are statically used in the shader should be included in the variable packing
657     // algorithm.
658     // Therefore, we should not reserve output vectors for them.
659 
660     switch (featureLevel)
661     {
662         // We must reserve one output vector for dx_Position.
663         // We also reserve one for gl_Position, which we unconditionally output on Feature
664         // Levels 10_0+,
665         // even if it's unused in the shader (e.g. for transform feedback). TODO: This could
666         // be improved.
667         case D3D_FEATURE_LEVEL_11_1:
668         case D3D_FEATURE_LEVEL_11_0:
669         case D3D_FEATURE_LEVEL_10_1:
670         case D3D_FEATURE_LEVEL_10_0:
671             return 2;
672 
673         // Just reserve dx_Position on Feature Level 9, since we don't ever need to output
674         // gl_Position.
675         case D3D_FEATURE_LEVEL_9_3:
676         case D3D_FEATURE_LEVEL_9_2:
677         case D3D_FEATURE_LEVEL_9_1:
678             return 1;
679 
680         default:
681             UNREACHABLE();
682             return 0;
683     }
684 }
685 
GetMaximumVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel)686 int GetMaximumVertexOutputVectors(D3D_FEATURE_LEVEL featureLevel)
687 {
688     static_assert(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT,
689                   "Unexpected D3D11 constant value.");
690 
691     switch (featureLevel)
692     {
693         case D3D_FEATURE_LEVEL_11_1:
694         case D3D_FEATURE_LEVEL_11_0:
695             return D3D11_VS_OUTPUT_REGISTER_COUNT - GetReservedVertexOutputVectors(featureLevel);
696 
697         case D3D_FEATURE_LEVEL_10_1:
698             return D3D10_1_VS_OUTPUT_REGISTER_COUNT - GetReservedVertexOutputVectors(featureLevel);
699         case D3D_FEATURE_LEVEL_10_0:
700             return D3D10_VS_OUTPUT_REGISTER_COUNT - GetReservedVertexOutputVectors(featureLevel);
701 
702         // Use Shader Model 2.X limits
703         case D3D_FEATURE_LEVEL_9_3:
704         case D3D_FEATURE_LEVEL_9_2:
705         case D3D_FEATURE_LEVEL_9_1:
706             return 8 - GetReservedVertexOutputVectors(featureLevel);
707 
708         default:
709             UNREACHABLE();
710             return 0;
711     }
712 }
713 
GetMaximumVertexTextureUnits(D3D_FEATURE_LEVEL featureLevel)714 int GetMaximumVertexTextureUnits(D3D_FEATURE_LEVEL featureLevel)
715 {
716     switch (featureLevel)
717     {
718         case D3D_FEATURE_LEVEL_11_1:
719         case D3D_FEATURE_LEVEL_11_0:
720             return D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT;
721 
722         case D3D_FEATURE_LEVEL_10_1:
723         case D3D_FEATURE_LEVEL_10_0:
724             return D3D10_COMMONSHADER_SAMPLER_SLOT_COUNT;
725 
726         // Vertex textures not supported on D3D11 Feature Level 9 according to
727         // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476149.aspx
728         // ID3D11DeviceContext::VSSetSamplers and ID3D11DeviceContext::VSSetShaderResources
729         case D3D_FEATURE_LEVEL_9_3:
730         case D3D_FEATURE_LEVEL_9_2:
731         case D3D_FEATURE_LEVEL_9_1:
732             return 0;
733 
734         default:
735             UNREACHABLE();
736             return 0;
737     }
738 }
739 
GetMaximumPixelUniformVectors(D3D_FEATURE_LEVEL featureLevel)740 int GetMaximumPixelUniformVectors(D3D_FEATURE_LEVEL featureLevel)
741 {
742     // TODO(geofflang): Remove hard-coded limit once the gl-uniform-arrays test can pass
743     switch (featureLevel)
744     {
745         case D3D_FEATURE_LEVEL_11_1:
746         case D3D_FEATURE_LEVEL_11_0:
747             return 1024;  // D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT;
748 
749         case D3D_FEATURE_LEVEL_10_1:
750         case D3D_FEATURE_LEVEL_10_0:
751             return 1024;  // D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT;
752 
753         // From http://msdn.microsoft.com/en-us/library/windows/desktop/ff476149.aspx
754         // ID3D11DeviceContext::PSSetConstantBuffers
755         case D3D_FEATURE_LEVEL_9_3:
756         case D3D_FEATURE_LEVEL_9_2:
757         case D3D_FEATURE_LEVEL_9_1:
758             return 32 - d3d11_gl::GetReservedFragmentUniformVectors(featureLevel);
759 
760         default:
761             UNREACHABLE();
762             return 0;
763     }
764 }
765 
GetMaximumPixelUniformBlocks(D3D_FEATURE_LEVEL featureLevel)766 int GetMaximumPixelUniformBlocks(D3D_FEATURE_LEVEL featureLevel)
767 {
768     switch (featureLevel)
769     {
770         case D3D_FEATURE_LEVEL_11_1:
771         case D3D_FEATURE_LEVEL_11_0:
772             return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT -
773                    d3d11::RESERVED_CONSTANT_BUFFER_SLOT_COUNT;
774 
775         case D3D_FEATURE_LEVEL_10_1:
776         case D3D_FEATURE_LEVEL_10_0:
777             return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT -
778                    d3d11::RESERVED_CONSTANT_BUFFER_SLOT_COUNT;
779 
780         // Uniform blocks not supported on D3D11 Feature Level 9
781         case D3D_FEATURE_LEVEL_9_3:
782         case D3D_FEATURE_LEVEL_9_2:
783         case D3D_FEATURE_LEVEL_9_1:
784             return 0;
785 
786         default:
787             UNREACHABLE();
788             return 0;
789     }
790 }
791 
GetMaximumPixelInputVectors(D3D_FEATURE_LEVEL featureLevel)792 int GetMaximumPixelInputVectors(D3D_FEATURE_LEVEL featureLevel)
793 {
794     switch (featureLevel)
795     {
796         case D3D_FEATURE_LEVEL_11_1:
797         case D3D_FEATURE_LEVEL_11_0:
798             return D3D11_PS_INPUT_REGISTER_COUNT - GetReservedVertexOutputVectors(featureLevel);
799 
800         case D3D_FEATURE_LEVEL_10_1:
801         case D3D_FEATURE_LEVEL_10_0:
802             return D3D10_PS_INPUT_REGISTER_COUNT - GetReservedVertexOutputVectors(featureLevel);
803 
804         // Use Shader Model 2.X limits
805         case D3D_FEATURE_LEVEL_9_3:
806             return 8 - GetReservedVertexOutputVectors(featureLevel);
807         case D3D_FEATURE_LEVEL_9_2:
808         case D3D_FEATURE_LEVEL_9_1:
809             return 8 - GetReservedVertexOutputVectors(featureLevel);
810 
811         default:
812             UNREACHABLE();
813             return 0;
814     }
815 }
816 
GetMaximumPixelTextureUnits(D3D_FEATURE_LEVEL featureLevel)817 int GetMaximumPixelTextureUnits(D3D_FEATURE_LEVEL featureLevel)
818 {
819     switch (featureLevel)
820     {
821         case D3D_FEATURE_LEVEL_11_1:
822         case D3D_FEATURE_LEVEL_11_0:
823             return D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT;
824 
825         case D3D_FEATURE_LEVEL_10_1:
826         case D3D_FEATURE_LEVEL_10_0:
827             return D3D10_COMMONSHADER_SAMPLER_SLOT_COUNT;
828 
829         // http://msdn.microsoft.com/en-us/library/windows/desktop/ff476149.aspx
830         // ID3D11DeviceContext::PSSetShaderResources
831         case D3D_FEATURE_LEVEL_9_3:
832         case D3D_FEATURE_LEVEL_9_2:
833         case D3D_FEATURE_LEVEL_9_1:
834             return 16;
835 
836         default:
837             UNREACHABLE();
838             return 0;
839     }
840 }
841 
GetMaxComputeWorkGroupCount(D3D_FEATURE_LEVEL featureLevel)842 std::array<GLint, 3> GetMaxComputeWorkGroupCount(D3D_FEATURE_LEVEL featureLevel)
843 {
844     switch (featureLevel)
845     {
846         case D3D_FEATURE_LEVEL_11_1:
847         case D3D_FEATURE_LEVEL_11_0:
848             return {{D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION,
849                      D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION,
850                      D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION}};
851         default:
852             return {{0, 0, 0}};
853     }
854 }
855 
GetMaxComputeWorkGroupSize(D3D_FEATURE_LEVEL featureLevel)856 std::array<GLint, 3> GetMaxComputeWorkGroupSize(D3D_FEATURE_LEVEL featureLevel)
857 {
858     switch (featureLevel)
859     {
860         case D3D_FEATURE_LEVEL_11_1:
861         case D3D_FEATURE_LEVEL_11_0:
862             return {{D3D11_CS_THREAD_GROUP_MAX_X, D3D11_CS_THREAD_GROUP_MAX_Y,
863                      D3D11_CS_THREAD_GROUP_MAX_Z}};
864         default:
865             return {{0, 0, 0}};
866     }
867 }
868 
GetMaxComputeWorkGroupInvocations(D3D_FEATURE_LEVEL featureLevel)869 int GetMaxComputeWorkGroupInvocations(D3D_FEATURE_LEVEL featureLevel)
870 {
871     switch (featureLevel)
872     {
873         case D3D_FEATURE_LEVEL_11_1:
874         case D3D_FEATURE_LEVEL_11_0:
875             return D3D11_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP;
876         default:
877             return 0;
878     }
879 }
880 
GetMaxComputeSharedMemorySize(D3D_FEATURE_LEVEL featureLevel)881 int GetMaxComputeSharedMemorySize(D3D_FEATURE_LEVEL featureLevel)
882 {
883     switch (featureLevel)
884     {
885         // In D3D11 the maximum total size of all variables with the groupshared storage class is
886         // 32kb.
887         // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-variable-syntax
888         case D3D_FEATURE_LEVEL_11_1:
889         case D3D_FEATURE_LEVEL_11_0:
890             return 32768;
891         default:
892             return 0;
893     }
894 }
895 
GetMaximumComputeUniformVectors(D3D_FEATURE_LEVEL featureLevel)896 int GetMaximumComputeUniformVectors(D3D_FEATURE_LEVEL featureLevel)
897 {
898     switch (featureLevel)
899     {
900         case D3D_FEATURE_LEVEL_11_1:
901         case D3D_FEATURE_LEVEL_11_0:
902             return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT;
903         default:
904             return 0;
905     }
906 }
907 
GetMaximumComputeUniformBlocks(D3D_FEATURE_LEVEL featureLevel)908 int GetMaximumComputeUniformBlocks(D3D_FEATURE_LEVEL featureLevel)
909 {
910     switch (featureLevel)
911     {
912         case D3D_FEATURE_LEVEL_11_1:
913         case D3D_FEATURE_LEVEL_11_0:
914             return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT -
915                    d3d11::RESERVED_CONSTANT_BUFFER_SLOT_COUNT;
916         default:
917             return 0;
918     }
919 }
920 
GetMaximumComputeTextureUnits(D3D_FEATURE_LEVEL featureLevel)921 int GetMaximumComputeTextureUnits(D3D_FEATURE_LEVEL featureLevel)
922 {
923     switch (featureLevel)
924     {
925         case D3D_FEATURE_LEVEL_11_1:
926         case D3D_FEATURE_LEVEL_11_0:
927             return D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT;
928         default:
929             return 0;
930     }
931 }
932 
SetUAVRelatedResourceLimits(D3D_FEATURE_LEVEL featureLevel,gl::Caps * caps)933 void SetUAVRelatedResourceLimits(D3D_FEATURE_LEVEL featureLevel, gl::Caps *caps)
934 {
935     ASSERT(caps);
936 
937     GLuint reservedUAVsForAtomicCounterBuffers = 0u;
938 
939     // For pixel shaders, the render targets and unordered access views share the same resource
940     // slots when being written out.
941     // https://msdn.microsoft.com/en-us/library/windows/desktop/ff476465(v=vs.85).aspx
942     GLuint maxNumRTVsAndUAVs = 0u;
943 
944     switch (featureLevel)
945     {
946         case D3D_FEATURE_LEVEL_11_1:
947             // Currently we allocate 4 UAV slots for atomic counter buffers on feature level 11_1.
948             reservedUAVsForAtomicCounterBuffers = 4u;
949             maxNumRTVsAndUAVs                   = D3D11_1_UAV_SLOT_COUNT;
950             break;
951         case D3D_FEATURE_LEVEL_11_0:
952             // Currently we allocate 1 UAV slot for atomic counter buffers on feature level 11_0.
953             reservedUAVsForAtomicCounterBuffers = 1u;
954             maxNumRTVsAndUAVs                   = D3D11_PS_CS_UAV_REGISTER_COUNT;
955             break;
956         default:
957             return;
958     }
959 
960     // Set limits on atomic counter buffers in fragment shaders and compute shaders.
961     caps->maxCombinedAtomicCounterBuffers = reservedUAVsForAtomicCounterBuffers;
962     caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Compute] =
963         reservedUAVsForAtomicCounterBuffers;
964     caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Fragment] =
965         reservedUAVsForAtomicCounterBuffers;
966     caps->maxAtomicCounterBufferBindings = reservedUAVsForAtomicCounterBuffers;
967 
968     // Setting MAX_COMPUTE_ATOMIC_COUNTERS to a conservative number of 1024 * the number of UAV
969     // reserved for atomic counters. It could theoretically be set to max buffer size / 4 but that
970     // number could cause problems.
971     caps->maxCombinedAtomicCounters = reservedUAVsForAtomicCounterBuffers * 1024;
972     caps->maxShaderAtomicCounters[gl::ShaderType::Compute] = caps->maxCombinedAtomicCounters;
973 
974     // See
975     // https://docs.microsoft.com/en-us/windows/desktop/direct3d11/overviews-direct3d-11-resources-limits
976     // Resource size (in MB) for any of the preceding resources is min(max(128,0.25f * (amount of
977     // dedicated VRAM)), 2048) MB. So we set it to 128MB to keep same with GL backend.
978     GLint maxResourceSize = D3D11_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_A_TERM * 1024 * 1024;
979     caps->maxShaderStorageBlockSize = maxResourceSize;
980 
981     caps->maxAtomicCounterBufferSize = maxResourceSize;
982 
983     // Allocate the remaining slots for images and shader storage blocks.
984     // The maximum number of fragment shader outputs depends on the current context version, so we
985     // will not set it here. See comments in Context11::initialize().
986     caps->maxCombinedShaderOutputResources =
987         maxNumRTVsAndUAVs - reservedUAVsForAtomicCounterBuffers;
988 
989     // Set limits on images and shader storage blocks in fragment shaders and compute shaders.
990     caps->maxCombinedShaderStorageBlocks                   = caps->maxCombinedShaderOutputResources;
991     caps->maxShaderStorageBlocks[gl::ShaderType::Compute]  = caps->maxCombinedShaderOutputResources;
992     caps->maxShaderStorageBlocks[gl::ShaderType::Fragment] = caps->maxCombinedShaderOutputResources;
993     caps->maxShaderStorageBufferBindings                   = caps->maxCombinedShaderOutputResources;
994 
995     caps->maxImageUnits                                    = caps->maxCombinedShaderOutputResources;
996     caps->maxCombinedImageUniforms                         = caps->maxCombinedShaderOutputResources;
997     caps->maxShaderImageUniforms[gl::ShaderType::Compute]  = caps->maxCombinedShaderOutputResources;
998     caps->maxShaderImageUniforms[gl::ShaderType::Fragment] = caps->maxCombinedShaderOutputResources;
999 
1000     // On feature level 11_1, UAVs are also available in vertex shaders and geometry shaders.
1001     if (featureLevel == D3D_FEATURE_LEVEL_11_1)
1002     {
1003         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Vertex] =
1004             caps->maxCombinedAtomicCounterBuffers;
1005         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Geometry] =
1006             caps->maxCombinedAtomicCounterBuffers;
1007 
1008         caps->maxShaderImageUniforms[gl::ShaderType::Vertex] =
1009             caps->maxCombinedShaderOutputResources;
1010         caps->maxShaderStorageBlocks[gl::ShaderType::Vertex] =
1011             caps->maxCombinedShaderOutputResources;
1012         caps->maxShaderImageUniforms[gl::ShaderType::Geometry] =
1013             caps->maxCombinedShaderOutputResources;
1014         caps->maxShaderStorageBlocks[gl::ShaderType::Geometry] =
1015             caps->maxCombinedShaderOutputResources;
1016     }
1017 }
1018 
GetMinimumTexelOffset(D3D_FEATURE_LEVEL featureLevel)1019 int GetMinimumTexelOffset(D3D_FEATURE_LEVEL featureLevel)
1020 {
1021     switch (featureLevel)
1022     {
1023         case D3D_FEATURE_LEVEL_11_1:
1024         case D3D_FEATURE_LEVEL_11_0:
1025             return D3D11_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE;
1026 
1027         case D3D_FEATURE_LEVEL_10_1:
1028         case D3D_FEATURE_LEVEL_10_0:
1029             return D3D10_COMMONSHADER_TEXEL_OFFSET_MAX_NEGATIVE;
1030 
1031         // Sampling functions with offsets are not available below shader model 4.0.
1032         case D3D_FEATURE_LEVEL_9_3:
1033         case D3D_FEATURE_LEVEL_9_2:
1034         case D3D_FEATURE_LEVEL_9_1:
1035             return 0;
1036 
1037         default:
1038             UNREACHABLE();
1039             return 0;
1040     }
1041 }
1042 
GetMaximumTexelOffset(D3D_FEATURE_LEVEL featureLevel)1043 int GetMaximumTexelOffset(D3D_FEATURE_LEVEL featureLevel)
1044 {
1045     switch (featureLevel)
1046     {
1047         case D3D_FEATURE_LEVEL_11_1:
1048         case D3D_FEATURE_LEVEL_11_0:
1049             return D3D11_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE;
1050         case D3D_FEATURE_LEVEL_10_1:
1051         case D3D_FEATURE_LEVEL_10_0:
1052             return D3D11_COMMONSHADER_TEXEL_OFFSET_MAX_POSITIVE;
1053 
1054         // Sampling functions with offsets are not available below shader model 4.0.
1055         case D3D_FEATURE_LEVEL_9_3:
1056         case D3D_FEATURE_LEVEL_9_2:
1057         case D3D_FEATURE_LEVEL_9_1:
1058             return 0;
1059 
1060         default:
1061             UNREACHABLE();
1062             return 0;
1063     }
1064 }
1065 
GetMinimumTextureGatherOffset(D3D_FEATURE_LEVEL featureLevel)1066 int GetMinimumTextureGatherOffset(D3D_FEATURE_LEVEL featureLevel)
1067 {
1068     switch (featureLevel)
1069     {
1070         // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/gather4-po--sm5---asm-
1071         case D3D_FEATURE_LEVEL_11_1:
1072         case D3D_FEATURE_LEVEL_11_0:
1073             return -32;
1074 
1075         case D3D_FEATURE_LEVEL_10_1:
1076         case D3D_FEATURE_LEVEL_10_0:
1077         case D3D_FEATURE_LEVEL_9_3:
1078         case D3D_FEATURE_LEVEL_9_2:
1079         case D3D_FEATURE_LEVEL_9_1:
1080             return 0;
1081 
1082         default:
1083             UNREACHABLE();
1084             return 0;
1085     }
1086 }
1087 
GetMaximumTextureGatherOffset(D3D_FEATURE_LEVEL featureLevel)1088 int GetMaximumTextureGatherOffset(D3D_FEATURE_LEVEL featureLevel)
1089 {
1090     switch (featureLevel)
1091     {
1092         // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/gather4-po--sm5---asm-
1093         case D3D_FEATURE_LEVEL_11_1:
1094         case D3D_FEATURE_LEVEL_11_0:
1095             return 31;
1096 
1097         case D3D_FEATURE_LEVEL_10_1:
1098         case D3D_FEATURE_LEVEL_10_0:
1099         case D3D_FEATURE_LEVEL_9_3:
1100         case D3D_FEATURE_LEVEL_9_2:
1101         case D3D_FEATURE_LEVEL_9_1:
1102             return 0;
1103 
1104         default:
1105             UNREACHABLE();
1106             return 0;
1107     }
1108 }
1109 
GetMaximumConstantBufferSize(D3D_FEATURE_LEVEL featureLevel)1110 size_t GetMaximumConstantBufferSize(D3D_FEATURE_LEVEL featureLevel)
1111 {
1112     // Returns a size_t despite the limit being a GLuint64 because size_t is the maximum
1113     // size of
1114     // any buffer that could be allocated.
1115 
1116     const size_t bytesPerComponent = 4 * sizeof(float);
1117 
1118     switch (featureLevel)
1119     {
1120         case D3D_FEATURE_LEVEL_11_1:
1121         case D3D_FEATURE_LEVEL_11_0:
1122             return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1123 
1124         case D3D_FEATURE_LEVEL_10_1:
1125         case D3D_FEATURE_LEVEL_10_0:
1126             return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1127 
1128         // Limits from http://msdn.microsoft.com/en-us/library/windows/desktop/ff476501.aspx
1129         // remarks section
1130         case D3D_FEATURE_LEVEL_9_3:
1131         case D3D_FEATURE_LEVEL_9_2:
1132         case D3D_FEATURE_LEVEL_9_1:
1133             return 4096 * bytesPerComponent;
1134 
1135         default:
1136             UNREACHABLE();
1137             return 0;
1138     }
1139 }
1140 
GetMaximumStreamOutputBuffers(D3D_FEATURE_LEVEL featureLevel)1141 int GetMaximumStreamOutputBuffers(D3D_FEATURE_LEVEL featureLevel)
1142 {
1143     switch (featureLevel)
1144     {
1145         case D3D_FEATURE_LEVEL_11_1:
1146         case D3D_FEATURE_LEVEL_11_0:
1147             return D3D11_SO_BUFFER_SLOT_COUNT;
1148 
1149         case D3D_FEATURE_LEVEL_10_1:
1150             return D3D10_1_SO_BUFFER_SLOT_COUNT;
1151         case D3D_FEATURE_LEVEL_10_0:
1152             return D3D10_SO_BUFFER_SLOT_COUNT;
1153 
1154         case D3D_FEATURE_LEVEL_9_3:
1155         case D3D_FEATURE_LEVEL_9_2:
1156         case D3D_FEATURE_LEVEL_9_1:
1157             return 0;
1158 
1159         default:
1160             UNREACHABLE();
1161             return 0;
1162     }
1163 }
1164 
GetMaximumStreamOutputInterleavedComponents(D3D_FEATURE_LEVEL featureLevel)1165 int GetMaximumStreamOutputInterleavedComponents(D3D_FEATURE_LEVEL featureLevel)
1166 {
1167     switch (featureLevel)
1168     {
1169         case D3D_FEATURE_LEVEL_11_1:
1170         case D3D_FEATURE_LEVEL_11_0:
1171 
1172         case D3D_FEATURE_LEVEL_10_1:
1173         case D3D_FEATURE_LEVEL_10_0:
1174             return GetMaximumVertexOutputVectors(featureLevel) * 4;
1175 
1176         case D3D_FEATURE_LEVEL_9_3:
1177         case D3D_FEATURE_LEVEL_9_2:
1178         case D3D_FEATURE_LEVEL_9_1:
1179             return 0;
1180 
1181         default:
1182             UNREACHABLE();
1183             return 0;
1184     }
1185 }
1186 
GetMaximumStreamOutputSeparateComponents(D3D_FEATURE_LEVEL featureLevel)1187 int GetMaximumStreamOutputSeparateComponents(D3D_FEATURE_LEVEL featureLevel)
1188 {
1189     switch (featureLevel)
1190     {
1191         case D3D_FEATURE_LEVEL_11_1:
1192         case D3D_FEATURE_LEVEL_11_0:
1193             return GetMaximumStreamOutputInterleavedComponents(featureLevel) /
1194                    GetMaximumStreamOutputBuffers(featureLevel);
1195 
1196         // D3D 10 and 10.1 only allow one output per output slot if an output slot other
1197         // than zero is used.
1198         case D3D_FEATURE_LEVEL_10_1:
1199         case D3D_FEATURE_LEVEL_10_0:
1200             return 4;
1201 
1202         case D3D_FEATURE_LEVEL_9_3:
1203         case D3D_FEATURE_LEVEL_9_2:
1204         case D3D_FEATURE_LEVEL_9_1:
1205             return 0;
1206 
1207         default:
1208             UNREACHABLE();
1209             return 0;
1210     }
1211 }
1212 
GetMaximumRenderToBufferWindowSize(D3D_FEATURE_LEVEL featureLevel)1213 int GetMaximumRenderToBufferWindowSize(D3D_FEATURE_LEVEL featureLevel)
1214 {
1215     switch (featureLevel)
1216     {
1217         case D3D_FEATURE_LEVEL_11_1:
1218         case D3D_FEATURE_LEVEL_11_0:
1219             return D3D11_REQ_RENDER_TO_BUFFER_WINDOW_WIDTH;
1220         case D3D_FEATURE_LEVEL_10_1:
1221         case D3D_FEATURE_LEVEL_10_0:
1222             return D3D10_REQ_RENDER_TO_BUFFER_WINDOW_WIDTH;
1223 
1224         // REQ_RENDER_TO_BUFFER_WINDOW_WIDTH not supported on D3D11 Feature Level 9,
1225         // use the maximum texture sizes
1226         case D3D_FEATURE_LEVEL_9_3:
1227             return D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION;
1228         case D3D_FEATURE_LEVEL_9_2:
1229         case D3D_FEATURE_LEVEL_9_1:
1230             return D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION;
1231 
1232         default:
1233             UNREACHABLE();
1234             return 0;
1235     }
1236 }
1237 
GetIntelDriverVersion(const Optional<LARGE_INTEGER> driverVersion)1238 IntelDriverVersion GetIntelDriverVersion(const Optional<LARGE_INTEGER> driverVersion)
1239 {
1240     if (!driverVersion.valid())
1241         return IntelDriverVersion(0);
1242 
1243     DWORD lowPart = driverVersion.value().LowPart;
1244     return IntelDriverVersion(HIWORD(lowPart) * 10000 + LOWORD(lowPart));
1245 }
1246 
1247 }  // anonymous namespace
1248 
GetReservedVertexUniformVectors(D3D_FEATURE_LEVEL featureLevel)1249 unsigned int GetReservedVertexUniformVectors(D3D_FEATURE_LEVEL featureLevel)
1250 {
1251     switch (featureLevel)
1252     {
1253         case D3D_FEATURE_LEVEL_11_1:
1254         case D3D_FEATURE_LEVEL_11_0:
1255         case D3D_FEATURE_LEVEL_10_1:
1256         case D3D_FEATURE_LEVEL_10_0:
1257             return 0;
1258 
1259         case D3D_FEATURE_LEVEL_9_3:
1260         case D3D_FEATURE_LEVEL_9_2:
1261         case D3D_FEATURE_LEVEL_9_1:
1262             return 3;  // dx_ViewAdjust, dx_ViewCoords and dx_ViewScale
1263 
1264         default:
1265             UNREACHABLE();
1266             return 0;
1267     }
1268 }
1269 
GetReservedFragmentUniformVectors(D3D_FEATURE_LEVEL featureLevel)1270 unsigned int GetReservedFragmentUniformVectors(D3D_FEATURE_LEVEL featureLevel)
1271 {
1272     switch (featureLevel)
1273     {
1274         case D3D_FEATURE_LEVEL_11_1:
1275         case D3D_FEATURE_LEVEL_11_0:
1276         case D3D_FEATURE_LEVEL_10_1:
1277         case D3D_FEATURE_LEVEL_10_0:
1278             return 0;
1279 
1280         case D3D_FEATURE_LEVEL_9_3:
1281         case D3D_FEATURE_LEVEL_9_2:
1282         case D3D_FEATURE_LEVEL_9_1:
1283             return 4;  // dx_ViewCoords, dx_DepthFront, dx_DepthRange, dx_FragCoordOffset
1284 
1285         default:
1286             UNREACHABLE();
1287             return 0;
1288     }
1289 }
1290 
GetMaximumClientVersion(const Renderer11DeviceCaps & caps)1291 gl::Version GetMaximumClientVersion(const Renderer11DeviceCaps &caps)
1292 {
1293     switch (caps.featureLevel)
1294     {
1295         case D3D_FEATURE_LEVEL_11_1:
1296         case D3D_FEATURE_LEVEL_11_0:
1297             return gl::Version(3, 1);
1298         case D3D_FEATURE_LEVEL_10_1:
1299             return gl::Version(3, 0);
1300 
1301         case D3D_FEATURE_LEVEL_10_0:
1302             if (caps.allowES3OnFL10_0)
1303             {
1304                 return gl::Version(3, 0);
1305             }
1306             else
1307             {
1308                 return gl::Version(2, 0);
1309             }
1310         case D3D_FEATURE_LEVEL_9_3:
1311         case D3D_FEATURE_LEVEL_9_2:
1312         case D3D_FEATURE_LEVEL_9_1:
1313             return gl::Version(2, 0);
1314 
1315         default:
1316             UNREACHABLE();
1317             return gl::Version(0, 0);
1318     }
1319 }
1320 
GetMinimumFeatureLevelForES31()1321 D3D_FEATURE_LEVEL GetMinimumFeatureLevelForES31()
1322 {
1323     return kMinimumFeatureLevelForES31;
1324 }
1325 
GetMaxViewportAndScissorRectanglesPerPipeline(D3D_FEATURE_LEVEL featureLevel)1326 unsigned int GetMaxViewportAndScissorRectanglesPerPipeline(D3D_FEATURE_LEVEL featureLevel)
1327 {
1328     switch (featureLevel)
1329     {
1330         case D3D_FEATURE_LEVEL_11_1:
1331         case D3D_FEATURE_LEVEL_11_0:
1332             return D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
1333         case D3D_FEATURE_LEVEL_10_1:
1334         case D3D_FEATURE_LEVEL_10_0:
1335         case D3D_FEATURE_LEVEL_9_3:
1336         case D3D_FEATURE_LEVEL_9_2:
1337         case D3D_FEATURE_LEVEL_9_1:
1338             return 1;
1339         default:
1340             UNREACHABLE();
1341             return 0;
1342     }
1343 }
1344 
IsMultiviewSupported(D3D_FEATURE_LEVEL featureLevel)1345 bool IsMultiviewSupported(D3D_FEATURE_LEVEL featureLevel)
1346 {
1347     // The multiview extensions can always be supported in D3D11 through geometry shaders.
1348     switch (featureLevel)
1349     {
1350         case D3D_FEATURE_LEVEL_11_1:
1351         case D3D_FEATURE_LEVEL_11_0:
1352             return true;
1353         default:
1354             return false;
1355     }
1356 }
1357 
GetMaxSampleMaskWords(D3D_FEATURE_LEVEL featureLevel)1358 int GetMaxSampleMaskWords(D3D_FEATURE_LEVEL featureLevel)
1359 {
1360     switch (featureLevel)
1361     {
1362         // D3D10+ only allows 1 sample mask.
1363         case D3D_FEATURE_LEVEL_11_1:
1364         case D3D_FEATURE_LEVEL_11_0:
1365         case D3D_FEATURE_LEVEL_10_1:
1366         case D3D_FEATURE_LEVEL_10_0:
1367             return 1;
1368         case D3D_FEATURE_LEVEL_9_3:
1369         case D3D_FEATURE_LEVEL_9_2:
1370         case D3D_FEATURE_LEVEL_9_1:
1371             return 0;
1372         default:
1373             UNREACHABLE();
1374             return 0;
1375     }
1376 }
1377 
HasTextureBufferSupport(ID3D11Device * device,const Renderer11DeviceCaps & renderer11DeviceCaps)1378 bool HasTextureBufferSupport(ID3D11Device *device, const Renderer11DeviceCaps &renderer11DeviceCaps)
1379 {
1380     if (renderer11DeviceCaps.featureLevel < D3D_FEATURE_LEVEL_11_0)
1381         return false;
1382 
1383     if (!renderer11DeviceCaps.supportsTypedUAVLoadAdditionalFormats)
1384         return false;
1385 
1386     // https://docs.microsoft.com/en-us/windows/win32/direct3d12/typed-unordered-access-view-loads
1387     // we don't need to check the typed store. from the spec,
1388     // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#FormatList
1389     // all the following format support typed stored.
1390     // According to spec,
1391     // https://www.khronos.org/registry/OpenGL-Refpages/es3/html/glBindImageTexture.xhtml the
1392     // required image unit format are GL_RGBA32F, GL_RGBA32UI, GL_RGBA32I, GL_RGBA16F, GL_RGBA16UI,
1393     // GL_RGBA16I, GL_RGBA8, GL_RGBAUI, GL_RGBA8I, GL_RGBA8_SNORM, GL_R32F, GL_R32UI, GL_R32I,
1394     const std::array<DXGI_FORMAT, 2> &optionalFormats = {
1395         DXGI_FORMAT_R32G32B32A32_FLOAT,  // test for GL_RGBA32(UIF), GL_RGBA16(UIF),
1396                                          // GL_RGBA8(UIUnorm)
1397         DXGI_FORMAT_R8G8B8A8_SNORM,      // test for GL_RGBA8_SNORM,
1398     };
1399 
1400     for (DXGI_FORMAT dxgiFormat : optionalFormats)
1401     {
1402         D3D11_FEATURE_DATA_FORMAT_SUPPORT FormatSupport = {dxgiFormat, 0};
1403         if (!SUCCEEDED(device->CheckFeatureSupport(D3D11_FEATURE_FORMAT_SUPPORT, &FormatSupport,
1404                                                    sizeof(FormatSupport))))
1405         {
1406             WARN() << "Error checking typed load support for format 0x" << std::hex << dxgiFormat;
1407             return false;
1408         }
1409         if ((FormatSupport.OutFormatSupport & D3D11_FORMAT_SUPPORT2_UAV_TYPED_LOAD) == 0)
1410             return false;
1411     }
1412     return true;
1413 }
1414 
GenerateCaps(ID3D11Device * device,ID3D11DeviceContext * deviceContext,const Renderer11DeviceCaps & renderer11DeviceCaps,const angle::FeaturesD3D & features,const char * description,gl::Caps * caps,gl::TextureCapsMap * textureCapsMap,gl::Extensions * extensions,gl::Limitations * limitations,ShPixelLocalStorageOptions * plsOptions)1415 void GenerateCaps(ID3D11Device *device,
1416                   ID3D11DeviceContext *deviceContext,
1417                   const Renderer11DeviceCaps &renderer11DeviceCaps,
1418                   const angle::FeaturesD3D &features,
1419                   const char *description,
1420                   gl::Caps *caps,
1421                   gl::TextureCapsMap *textureCapsMap,
1422                   gl::Extensions *extensions,
1423                   gl::Limitations *limitations,
1424                   ShPixelLocalStorageOptions *plsOptions)
1425 {
1426     const D3D_FEATURE_LEVEL featureLevel = renderer11DeviceCaps.featureLevel;
1427     const gl::FormatSet &allFormats      = gl::GetAllSizedInternalFormats();
1428     for (GLenum internalFormat : allFormats)
1429     {
1430         gl::TextureCaps textureCaps =
1431             GenerateTextureFormatCaps(GetMaximumClientVersion(renderer11DeviceCaps), internalFormat,
1432                                       device, renderer11DeviceCaps);
1433         textureCapsMap->insert(internalFormat, textureCaps);
1434     }
1435 
1436     // GL core feature limits
1437     // Reserve MAX_UINT for D3D11's primitive restart.
1438     caps->maxElementIndex  = static_cast<GLint64>(std::numeric_limits<unsigned int>::max() - 1);
1439     caps->max3DTextureSize = GetMaximum3DTextureSize(featureLevel);
1440     caps->max2DTextureSize = GetMaximum2DTextureSize(featureLevel);
1441     caps->maxCubeMapTextureSize = GetMaximumCubeMapTextureSize(featureLevel);
1442     caps->maxArrayTextureLayers = GetMaximum2DTextureArraySize(featureLevel);
1443 
1444     // Unimplemented, set to minimum required
1445     caps->maxLODBias = 2.0f;
1446 
1447     // No specific limits on render target size, maximum 2D texture size is equivalent
1448     caps->maxRenderbufferSize = caps->max2DTextureSize;
1449 
1450     // Maximum draw buffers and color attachments are the same, max color attachments could
1451     // eventually be increased to 16
1452     caps->maxDrawBuffers      = GetMaximumSimultaneousRenderTargets(featureLevel);
1453     caps->maxColorAttachments = GetMaximumSimultaneousRenderTargets(featureLevel);
1454 
1455     // D3D11 has the same limit for viewport width and height
1456     caps->maxViewportWidth  = GetMaximumViewportSize(featureLevel);
1457     caps->maxViewportHeight = caps->maxViewportWidth;
1458 
1459     // Choose a reasonable maximum, enforced in the shader.
1460     caps->minAliasedPointSize = 1.0f;
1461     caps->maxAliasedPointSize = 1024.0f;
1462 
1463     // Wide lines not supported
1464     caps->minAliasedLineWidth = 1.0f;
1465     caps->maxAliasedLineWidth = 1.0f;
1466 
1467     // Primitive count limits
1468     caps->maxElementsIndices  = GetMaximumDrawIndexedIndexCount(featureLevel);
1469     caps->maxElementsVertices = GetMaximumDrawVertexCount(featureLevel);
1470 
1471     // Program and shader binary formats (no supported shader binary formats)
1472     caps->programBinaryFormats.push_back(GL_PROGRAM_BINARY_ANGLE);
1473 
1474     caps->vertexHighpFloat.setIEEEFloat();
1475     caps->vertexMediumpFloat.setIEEEFloat();
1476     caps->vertexLowpFloat.setIEEEFloat();
1477     caps->fragmentHighpFloat.setIEEEFloat();
1478     caps->fragmentMediumpFloat.setIEEEFloat();
1479     caps->fragmentLowpFloat.setIEEEFloat();
1480 
1481     // 32-bit integers are natively supported
1482     caps->vertexHighpInt.setTwosComplementInt(32);
1483     caps->vertexMediumpInt.setTwosComplementInt(32);
1484     caps->vertexLowpInt.setTwosComplementInt(32);
1485     caps->fragmentHighpInt.setTwosComplementInt(32);
1486     caps->fragmentMediumpInt.setTwosComplementInt(32);
1487     caps->fragmentLowpInt.setTwosComplementInt(32);
1488 
1489     // We do not wait for server fence objects internally, so report a max timeout of zero.
1490     caps->maxServerWaitTimeout = 0;
1491 
1492     // Vertex shader limits
1493     caps->maxVertexAttributes     = GetMaximumVertexInputSlots(featureLevel);
1494     caps->maxVertexUniformVectors = GetMaximumVertexUniformVectors(featureLevel);
1495     if (features.skipVSConstantRegisterZero.enabled)
1496     {
1497         caps->maxVertexUniformVectors -= 1;
1498     }
1499     caps->maxShaderUniformComponents[gl::ShaderType::Vertex] = caps->maxVertexUniformVectors * 4;
1500     caps->maxShaderUniformBlocks[gl::ShaderType::Vertex] =
1501         GetMaximumVertexUniformBlocks(featureLevel);
1502     caps->maxVertexOutputComponents = GetMaximumVertexOutputVectors(featureLevel) * 4;
1503     caps->maxShaderTextureImageUnits[gl::ShaderType::Vertex] =
1504         GetMaximumVertexTextureUnits(featureLevel);
1505 
1506     // Vertex Attribute Bindings are emulated on D3D11.
1507     caps->maxVertexAttribBindings = caps->maxVertexAttributes;
1508     // Experimental testing confirmed there is no explicit limit on maximum buffer offset in D3D11.
1509     caps->maxVertexAttribRelativeOffset = std::numeric_limits<GLint>::max();
1510     // Experimental testing confirmed 2048 is the maximum stride that D3D11 can support on all
1511     // platforms.
1512     caps->maxVertexAttribStride = 2048;
1513 
1514     // Fragment shader limits
1515     caps->maxFragmentUniformVectors = GetMaximumPixelUniformVectors(featureLevel);
1516     caps->maxShaderUniformComponents[gl::ShaderType::Fragment] =
1517         caps->maxFragmentUniformVectors * 4;
1518     caps->maxShaderUniformBlocks[gl::ShaderType::Fragment] =
1519         GetMaximumPixelUniformBlocks(featureLevel);
1520     caps->maxFragmentInputComponents = GetMaximumPixelInputVectors(featureLevel) * 4;
1521     caps->maxShaderTextureImageUnits[gl::ShaderType::Fragment] =
1522         GetMaximumPixelTextureUnits(featureLevel);
1523     caps->minProgramTexelOffset = GetMinimumTexelOffset(featureLevel);
1524     caps->maxProgramTexelOffset = GetMaximumTexelOffset(featureLevel);
1525 
1526     // Compute shader limits
1527     caps->maxComputeWorkGroupCount       = GetMaxComputeWorkGroupCount(featureLevel);
1528     caps->maxComputeWorkGroupSize        = GetMaxComputeWorkGroupSize(featureLevel);
1529     caps->maxComputeWorkGroupInvocations = GetMaxComputeWorkGroupInvocations(featureLevel);
1530     caps->maxComputeSharedMemorySize     = GetMaxComputeSharedMemorySize(featureLevel);
1531     caps->maxShaderUniformComponents[gl::ShaderType::Compute] =
1532         GetMaximumComputeUniformVectors(featureLevel) * 4;
1533     caps->maxShaderUniformBlocks[gl::ShaderType::Compute] =
1534         GetMaximumComputeUniformBlocks(featureLevel);
1535     caps->maxShaderTextureImageUnits[gl::ShaderType::Compute] =
1536         GetMaximumComputeTextureUnits(featureLevel);
1537 
1538     SetUAVRelatedResourceLimits(featureLevel, caps);
1539 
1540     // Aggregate shader limits
1541     caps->maxUniformBufferBindings = caps->maxShaderUniformBlocks[gl::ShaderType::Vertex] +
1542                                      caps->maxShaderUniformBlocks[gl::ShaderType::Fragment];
1543     caps->maxUniformBlockSize = static_cast<GLuint64>(GetMaximumConstantBufferSize(featureLevel));
1544 
1545     // TODO(oetuaho): Get a more accurate limit. For now using the minimum requirement for GLES 3.1.
1546     caps->maxUniformLocations = 1024;
1547 
1548     // With DirectX 11.1, constant buffer offset and size must be a multiple of 16 constants of 16
1549     // bytes each.
1550     // https://msdn.microsoft.com/en-us/library/windows/desktop/hh404649%28v=vs.85%29.aspx
1551     // With DirectX 11.0, we emulate UBO offsets using copies of ranges of the UBO however
1552     // we still keep the same alignment as 11.1 for consistency.
1553     caps->uniformBufferOffsetAlignment = 256;
1554 
1555     caps->maxCombinedUniformBlocks = caps->maxShaderUniformBlocks[gl::ShaderType::Vertex] +
1556                                      caps->maxShaderUniformBlocks[gl::ShaderType::Fragment];
1557 
1558     // A shader storage block will be translated to a structure in HLSL. So We reference the HLSL
1559     // structure packing rules
1560     // https://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx. The
1561     // resulting size of any structure will always be evenly divisible by sizeof(four-component
1562     // vector).
1563     caps->shaderStorageBufferOffsetAlignment = 16;
1564 
1565     for (gl::ShaderType shaderType : gl::AllShaderTypes())
1566     {
1567         caps->maxCombinedShaderUniformComponents[shaderType] =
1568             static_cast<GLint64>(caps->maxShaderUniformBlocks[shaderType]) *
1569                 static_cast<GLint64>(caps->maxUniformBlockSize / 4) +
1570             static_cast<GLint64>(caps->maxShaderUniformComponents[shaderType]);
1571     }
1572 
1573     caps->maxVaryingComponents         = GetMaximumVertexOutputVectors(featureLevel) * 4;
1574     caps->maxVaryingVectors            = GetMaximumVertexOutputVectors(featureLevel);
1575     caps->maxCombinedTextureImageUnits = caps->maxShaderTextureImageUnits[gl::ShaderType::Vertex] +
1576                                          caps->maxShaderTextureImageUnits[gl::ShaderType::Fragment];
1577 
1578     // Transform feedback limits
1579     caps->maxTransformFeedbackInterleavedComponents =
1580         GetMaximumStreamOutputInterleavedComponents(featureLevel);
1581     caps->maxTransformFeedbackSeparateAttributes = GetMaximumStreamOutputBuffers(featureLevel);
1582     caps->maxTransformFeedbackSeparateComponents =
1583         GetMaximumStreamOutputSeparateComponents(featureLevel);
1584 
1585     // Defer the computation of multisample limits to Context::updateCaps() where max*Samples values
1586     // are determined according to available sample counts for each individual format.
1587     caps->maxSamples             = std::numeric_limits<GLint>::max();
1588     caps->maxColorTextureSamples = std::numeric_limits<GLint>::max();
1589     caps->maxDepthTextureSamples = std::numeric_limits<GLint>::max();
1590     caps->maxIntegerSamples      = std::numeric_limits<GLint>::max();
1591 
1592     // Sample mask words limits
1593     caps->maxSampleMaskWords = GetMaxSampleMaskWords(featureLevel);
1594 
1595     // Framebuffer limits
1596     caps->maxFramebufferSamples = std::numeric_limits<GLint>::max();
1597     caps->maxFramebufferWidth   = GetMaximumRenderToBufferWindowSize(featureLevel);
1598     caps->maxFramebufferHeight  = caps->maxFramebufferWidth;
1599 
1600     // Texture gather offset limits
1601     caps->minProgramTextureGatherOffset = GetMinimumTextureGatherOffset(featureLevel);
1602     caps->maxProgramTextureGatherOffset = GetMaximumTextureGatherOffset(featureLevel);
1603 
1604     caps->maxTextureAnisotropy        = GetMaximumAnisotropy(featureLevel);
1605     caps->queryCounterBitsTimeElapsed = 64;
1606 
1607     caps->queryCounterBitsTimestamp = features.enableTimestampQueries.enabled ? 64 : 0;
1608 
1609     caps->maxDualSourceDrawBuffers = 1;
1610 
1611     // GL extension support
1612     extensions->setTextureExtensionSupport(*textureCapsMap);
1613 
1614     // Explicitly disable GL_OES_compressed_ETC1_RGB8_texture because it's emulated and never
1615     // becomes core. WebGL doesn't want to expose it unless there is native support.
1616     extensions->compressedETC1RGB8TextureOES    = false;
1617     extensions->compressedETC1RGB8SubTextureEXT = false;
1618 
1619     extensions->elementIndexUintOES         = true;
1620     extensions->getProgramBinaryOES         = true;
1621     extensions->rgb8Rgba8OES                = true;
1622     extensions->readFormatBgraEXT           = true;
1623     extensions->pixelBufferObjectNV         = true;
1624     extensions->mapbufferOES                = true;
1625     extensions->mapBufferRangeEXT           = true;
1626     extensions->textureNpotOES              = GetNPOTTextureSupport(featureLevel);
1627     extensions->drawBuffersEXT              = GetMaximumSimultaneousRenderTargets(featureLevel) > 1;
1628     extensions->drawBuffersIndexedEXT       = (featureLevel >= D3D_FEATURE_LEVEL_10_1);
1629     extensions->drawBuffersIndexedOES       = extensions->drawBuffersIndexedEXT;
1630     extensions->textureStorageEXT           = true;
1631     extensions->textureFilterAnisotropicEXT = true;
1632     extensions->occlusionQueryBooleanEXT    = GetOcclusionQuerySupport(featureLevel);
1633     extensions->fenceNV                     = GetEventQuerySupport(featureLevel);
1634     extensions->disjointTimerQueryEXT       = true;
1635     extensions->robustnessEXT               = true;
1636     extensions->robustnessKHR               = true;
1637     // Direct3D guarantees to return zero for any resource that is accessed out of bounds.
1638     // See https://msdn.microsoft.com/en-us/library/windows/desktop/ff476332(v=vs.85).aspx
1639     // and https://msdn.microsoft.com/en-us/library/windows/desktop/ff476900(v=vs.85).aspx
1640     extensions->robustBufferAccessBehaviorKHR = true;
1641     extensions->blendMinmaxEXT                = true;
1642     // https://docs.microsoft.com/en-us/windows/desktop/direct3ddxgi/format-support-for-direct3d-11-0-feature-level-hardware
1643     extensions->floatBlendEXT               = true;
1644     extensions->framebufferBlitANGLE        = GetFramebufferBlitSupport(featureLevel);
1645     extensions->framebufferBlitNV           = extensions->framebufferBlitANGLE;
1646     extensions->framebufferMultisampleANGLE = GetFramebufferMultisampleSupport(featureLevel);
1647     extensions->instancedArraysANGLE        = GetInstancingSupport(featureLevel);
1648     extensions->instancedArraysEXT          = GetInstancingSupport(featureLevel);
1649     extensions->packReverseRowOrderANGLE    = true;
1650     extensions->standardDerivativesOES      = GetDerivativeInstructionSupport(featureLevel);
1651     extensions->shaderTextureLodEXT         = GetShaderTextureLODSupport(featureLevel);
1652     extensions->fragDepthEXT                = true;
1653     extensions->conservativeDepthEXT        = (featureLevel >= D3D_FEATURE_LEVEL_11_0);
1654     extensions->polygonModeANGLE            = true;
1655     extensions->polygonOffsetClampEXT       = (featureLevel >= D3D_FEATURE_LEVEL_10_0);
1656     extensions->depthClampEXT               = true;
1657     extensions->stencilTexturingANGLE       = (featureLevel >= D3D_FEATURE_LEVEL_10_1);
1658     extensions->multiviewOVR                = IsMultiviewSupported(featureLevel);
1659     extensions->multiview2OVR               = IsMultiviewSupported(featureLevel);
1660     if (extensions->multiviewOVR || extensions->multiview2OVR)
1661     {
1662         caps->maxViews = std::min(static_cast<GLuint>(GetMaximum2DTextureArraySize(featureLevel)),
1663                                   GetMaxViewportAndScissorRectanglesPerPipeline(featureLevel));
1664     }
1665     extensions->textureUsageANGLE = true;  // This could be false since it has no effect in D3D11
1666     extensions->discardFramebufferEXT               = true;
1667     extensions->translatedShaderSourceANGLE         = true;
1668     extensions->fboRenderMipmapOES                  = true;
1669     extensions->debugMarkerEXT                      = true;
1670     extensions->EGLImageOES                         = true;
1671     extensions->EGLImageExternalOES                 = true;
1672     extensions->EGLImageExternalWrapModesEXT        = true;
1673     extensions->EGLImageExternalEssl3OES            = true;
1674     extensions->EGLStreamConsumerExternalNV         = true;
1675     extensions->unpackSubimageEXT                   = true;
1676     extensions->packSubimageNV                      = true;
1677     extensions->lossyEtcDecodeANGLE                 = true;
1678     extensions->syncQueryCHROMIUM                   = GetEventQuerySupport(featureLevel);
1679     extensions->copyTextureCHROMIUM                 = true;
1680     extensions->copyCompressedTextureCHROMIUM       = true;
1681     extensions->textureStorageMultisample2dArrayOES = true;
1682     extensions->textureMirrorClampToEdgeEXT         = true;
1683     extensions->shaderNoperspectiveInterpolationNV  = (featureLevel >= D3D_FEATURE_LEVEL_10_0);
1684     extensions->sampleVariablesOES                  = (featureLevel >= D3D_FEATURE_LEVEL_11_0);
1685     extensions->shaderMultisampleInterpolationOES   = (featureLevel >= D3D_FEATURE_LEVEL_11_0);
1686     if (extensions->shaderMultisampleInterpolationOES)
1687     {
1688         caps->subPixelInterpolationOffsetBits = 4;
1689         caps->minInterpolationOffset          = -0.5f;
1690         caps->maxInterpolationOffset          = +0.4375f;  // +0.5 - (2 ^ -4)
1691     }
1692     extensions->multiviewMultisampleANGLE =
1693         ((extensions->multiviewOVR || extensions->multiview2OVR) &&
1694          extensions->textureStorageMultisample2dArrayOES);
1695     extensions->copyTexture3dANGLE      = true;
1696     extensions->textureBorderClampEXT   = true;
1697     extensions->textureBorderClampOES   = true;
1698     extensions->multiDrawIndirectEXT    = true;
1699     extensions->textureMultisampleANGLE = true;
1700     extensions->provokingVertexANGLE    = true;
1701     extensions->blendFuncExtendedEXT    = true;
1702     // http://anglebug.com/42263500
1703     extensions->texture3DOES                             = false;
1704     extensions->baseInstanceEXT                          = true;
1705     extensions->baseVertexBaseInstanceANGLE              = true;
1706     extensions->baseVertexBaseInstanceShaderBuiltinANGLE = true;
1707     extensions->drawElementsBaseVertexOES                = true;
1708     extensions->drawElementsBaseVertexEXT                = true;
1709     if (!strstr(description, "Adreno"))
1710     {
1711         extensions->multisampledRenderToTextureEXT = true;
1712     }
1713     extensions->videoTextureWEBGL = true;
1714 
1715     // D3D11 cannot support reading depth texture as a luminance texture.
1716     // It treats it as a red-channel-only texture.
1717     extensions->depthTextureOES = false;
1718 
1719     // readPixels on depth & stencil not working with D3D11 backend.
1720     extensions->readDepthNV         = false;
1721     extensions->readStencilNV       = false;
1722     extensions->depthBufferFloat2NV = false;
1723 
1724     // GL_EXT_clip_control
1725     extensions->clipControlEXT = (featureLevel >= D3D_FEATURE_LEVEL_10_0);
1726 
1727     // GL_APPLE_clip_distance / GL_EXT_clip_cull_distance / GL_ANGLE_clip_cull_distance
1728     extensions->clipDistanceAPPLE         = true;
1729     extensions->clipCullDistanceEXT       = true;
1730     extensions->clipCullDistanceANGLE     = true;
1731     caps->maxClipDistances                = D3D11_CLIP_OR_CULL_DISTANCE_COUNT;
1732     caps->maxCullDistances                = D3D11_CLIP_OR_CULL_DISTANCE_COUNT;
1733     caps->maxCombinedClipAndCullDistances = D3D11_CLIP_OR_CULL_DISTANCE_COUNT;
1734 
1735     // GL_KHR_parallel_shader_compile
1736     extensions->parallelShaderCompileKHR = true;
1737 
1738     // GL_EXT_texture_buffer
1739     extensions->textureBufferEXT = HasTextureBufferSupport(device, renderer11DeviceCaps);
1740 
1741     // GL_OES_texture_buffer
1742     extensions->textureBufferOES = extensions->textureBufferEXT;
1743 
1744     // ANGLE_shader_pixel_local_storage -- fragment shader UAVs appear in D3D 11.0.
1745     if (featureLevel >= D3D_FEATURE_LEVEL_11_0)
1746     {
1747         extensions->shaderPixelLocalStorageANGLE = true;
1748         plsOptions->type                         = ShPixelLocalStorageType::ImageLoadStore;
1749         if (renderer11DeviceCaps.supportsRasterizerOrderViews)
1750         {
1751             extensions->shaderPixelLocalStorageCoherentANGLE = true;
1752             plsOptions->fragmentSyncType = ShFragmentSynchronizationType::RasterizerOrderViews_D3D;
1753         }
1754         // TODO(anglebug.com/40096838): If we add RG* support to pixel local storage, these are
1755         // *NOT* in the set of common formats, so we need to query support for each individualy:
1756         // https://learn.microsoft.com/en-us/windows/win32/direct3d11/typed-unordered-access-view-loads
1757         plsOptions->supportsNativeRGBA8ImageFormats =
1758             renderer11DeviceCaps.supportsUAVLoadStoreCommonFormats;
1759     }
1760 
1761     // D3D11 Feature Level 10_0+ uses SV_IsFrontFace in HLSL to emulate gl_FrontFacing.
1762     // D3D11 Feature Level 9_3 doesn't support SV_IsFrontFace, and has no equivalent, so can't
1763     // support gl_FrontFacing.
1764     limitations->noFrontFacingSupport = (featureLevel <= D3D_FEATURE_LEVEL_9_3);
1765 
1766     // D3D11 Feature Level 9_3 doesn't support alpha-to-coverage
1767     limitations->noSampleAlphaToCoverageSupport = (featureLevel <= D3D_FEATURE_LEVEL_9_3);
1768 
1769     // D3D11 has no concept of separate masks and refs for front and back faces in the depth stencil
1770     // state.
1771     limitations->noSeparateStencilRefsAndMasks = true;
1772 
1773     // D3D11 cannot support constant color and alpha blend funcs together
1774     limitations->noSimultaneousConstantColorAndAlphaBlendFunc = true;
1775 
1776     // D3D11 does not support multiple transform feedback outputs writing to the same buffer.
1777     limitations->noDoubleBoundTransformFeedbackBuffers = true;
1778 
1779     // D3D11 does not support vertex attribute aliasing
1780     limitations->noVertexAttributeAliasing = true;
1781 
1782     // D3D11 does not support compressed textures where the base mip level is not a multiple of 4
1783     limitations->compressedBaseMipLevelMultipleOfFour = true;
1784 
1785     if (extensions->textureBufferAny())
1786     {
1787         caps->maxTextureBufferSize = 1 << D3D11_REQ_BUFFER_RESOURCE_TEXEL_COUNT_2_TO_EXP;
1788         // this maybe touble for RGB32 format.
1789         caps->textureBufferOffsetAlignment = 16;
1790     }
1791 
1792 #ifdef ANGLE_ENABLE_WINDOWS_UWP
1793     // Setting a non-zero divisor on attribute zero doesn't work on certain Windows Phone 8-era
1794     // devices. We should prevent developers from doing this on ALL Windows Store devices. This will
1795     // maintain consistency across all Windows devices. We allow non-zero divisors on attribute zero
1796     // if the Client Version >= 3, since devices affected by this issue don't support ES3+.
1797     limitations->attributeZeroRequiresZeroDivisorInEXT = true;
1798 #endif
1799 }
1800 
1801 }  // namespace d3d11_gl
1802 
1803 namespace gl_d3d11
1804 {
1805 
ConvertBlendFunc(gl::BlendFactorType glBlend,bool isAlpha)1806 D3D11_BLEND ConvertBlendFunc(gl::BlendFactorType glBlend, bool isAlpha)
1807 {
1808     D3D11_BLEND d3dBlend = D3D11_BLEND_ZERO;
1809 
1810     switch (glBlend)
1811     {
1812         case gl::BlendFactorType::Zero:
1813             d3dBlend = D3D11_BLEND_ZERO;
1814             break;
1815         case gl::BlendFactorType::One:
1816             d3dBlend = D3D11_BLEND_ONE;
1817             break;
1818         case gl::BlendFactorType::SrcColor:
1819             d3dBlend = (isAlpha ? D3D11_BLEND_SRC_ALPHA : D3D11_BLEND_SRC_COLOR);
1820             break;
1821         case gl::BlendFactorType::OneMinusSrcColor:
1822             d3dBlend = (isAlpha ? D3D11_BLEND_INV_SRC_ALPHA : D3D11_BLEND_INV_SRC_COLOR);
1823             break;
1824         case gl::BlendFactorType::DstColor:
1825             d3dBlend = (isAlpha ? D3D11_BLEND_DEST_ALPHA : D3D11_BLEND_DEST_COLOR);
1826             break;
1827         case gl::BlendFactorType::OneMinusDstColor:
1828             d3dBlend = (isAlpha ? D3D11_BLEND_INV_DEST_ALPHA : D3D11_BLEND_INV_DEST_COLOR);
1829             break;
1830         case gl::BlendFactorType::SrcAlpha:
1831             d3dBlend = D3D11_BLEND_SRC_ALPHA;
1832             break;
1833         case gl::BlendFactorType::OneMinusSrcAlpha:
1834             d3dBlend = D3D11_BLEND_INV_SRC_ALPHA;
1835             break;
1836         case gl::BlendFactorType::DstAlpha:
1837             d3dBlend = D3D11_BLEND_DEST_ALPHA;
1838             break;
1839         case gl::BlendFactorType::OneMinusDstAlpha:
1840             d3dBlend = D3D11_BLEND_INV_DEST_ALPHA;
1841             break;
1842         case gl::BlendFactorType::ConstantColor:
1843             d3dBlend = D3D11_BLEND_BLEND_FACTOR;
1844             break;
1845         case gl::BlendFactorType::OneMinusConstantColor:
1846             d3dBlend = D3D11_BLEND_INV_BLEND_FACTOR;
1847             break;
1848         case gl::BlendFactorType::ConstantAlpha:
1849             d3dBlend = D3D11_BLEND_BLEND_FACTOR;
1850             break;
1851         case gl::BlendFactorType::OneMinusConstantAlpha:
1852             d3dBlend = D3D11_BLEND_INV_BLEND_FACTOR;
1853             break;
1854         case gl::BlendFactorType::SrcAlphaSaturate:
1855             d3dBlend = D3D11_BLEND_SRC_ALPHA_SAT;
1856             break;
1857         case gl::BlendFactorType::Src1Color:
1858             d3dBlend = (isAlpha ? D3D11_BLEND_SRC1_ALPHA : D3D11_BLEND_SRC1_COLOR);
1859             break;
1860         case gl::BlendFactorType::Src1Alpha:
1861             d3dBlend = D3D11_BLEND_SRC1_ALPHA;
1862             break;
1863         case gl::BlendFactorType::OneMinusSrc1Color:
1864             d3dBlend = (isAlpha ? D3D11_BLEND_INV_SRC1_ALPHA : D3D11_BLEND_INV_SRC1_COLOR);
1865             break;
1866         case gl::BlendFactorType::OneMinusSrc1Alpha:
1867             d3dBlend = D3D11_BLEND_INV_SRC1_ALPHA;
1868             break;
1869         default:
1870             UNREACHABLE();
1871     }
1872 
1873     return d3dBlend;
1874 }
1875 
ConvertBlendOp(gl::BlendEquationType glBlendOp)1876 D3D11_BLEND_OP ConvertBlendOp(gl::BlendEquationType glBlendOp)
1877 {
1878     D3D11_BLEND_OP d3dBlendOp = D3D11_BLEND_OP_ADD;
1879 
1880     switch (glBlendOp)
1881     {
1882         case gl::BlendEquationType::Add:
1883             d3dBlendOp = D3D11_BLEND_OP_ADD;
1884             break;
1885         case gl::BlendEquationType::Subtract:
1886             d3dBlendOp = D3D11_BLEND_OP_SUBTRACT;
1887             break;
1888         case gl::BlendEquationType::ReverseSubtract:
1889             d3dBlendOp = D3D11_BLEND_OP_REV_SUBTRACT;
1890             break;
1891         case gl::BlendEquationType::Min:
1892             d3dBlendOp = D3D11_BLEND_OP_MIN;
1893             break;
1894         case gl::BlendEquationType::Max:
1895             d3dBlendOp = D3D11_BLEND_OP_MAX;
1896             break;
1897         default:
1898             UNREACHABLE();
1899     }
1900 
1901     return d3dBlendOp;
1902 }
1903 
ConvertColorMask(bool red,bool green,bool blue,bool alpha)1904 UINT8 ConvertColorMask(bool red, bool green, bool blue, bool alpha)
1905 {
1906     UINT8 mask = 0;
1907     if (red)
1908     {
1909         mask |= D3D11_COLOR_WRITE_ENABLE_RED;
1910     }
1911     if (green)
1912     {
1913         mask |= D3D11_COLOR_WRITE_ENABLE_GREEN;
1914     }
1915     if (blue)
1916     {
1917         mask |= D3D11_COLOR_WRITE_ENABLE_BLUE;
1918     }
1919     if (alpha)
1920     {
1921         mask |= D3D11_COLOR_WRITE_ENABLE_ALPHA;
1922     }
1923     return mask;
1924 }
1925 
ConvertCullMode(bool cullEnabled,gl::CullFaceMode cullMode)1926 D3D11_CULL_MODE ConvertCullMode(bool cullEnabled, gl::CullFaceMode cullMode)
1927 {
1928     D3D11_CULL_MODE cull = D3D11_CULL_NONE;
1929 
1930     if (cullEnabled)
1931     {
1932         switch (cullMode)
1933         {
1934             case gl::CullFaceMode::Front:
1935                 cull = D3D11_CULL_FRONT;
1936                 break;
1937             case gl::CullFaceMode::Back:
1938                 cull = D3D11_CULL_BACK;
1939                 break;
1940             case gl::CullFaceMode::FrontAndBack:
1941                 cull = D3D11_CULL_NONE;
1942                 break;
1943             default:
1944                 UNREACHABLE();
1945         }
1946     }
1947     else
1948     {
1949         cull = D3D11_CULL_NONE;
1950     }
1951 
1952     return cull;
1953 }
1954 
ConvertComparison(GLenum comparison)1955 D3D11_COMPARISON_FUNC ConvertComparison(GLenum comparison)
1956 {
1957     D3D11_COMPARISON_FUNC d3dComp = D3D11_COMPARISON_NEVER;
1958     switch (comparison)
1959     {
1960         case GL_NEVER:
1961             d3dComp = D3D11_COMPARISON_NEVER;
1962             break;
1963         case GL_ALWAYS:
1964             d3dComp = D3D11_COMPARISON_ALWAYS;
1965             break;
1966         case GL_LESS:
1967             d3dComp = D3D11_COMPARISON_LESS;
1968             break;
1969         case GL_LEQUAL:
1970             d3dComp = D3D11_COMPARISON_LESS_EQUAL;
1971             break;
1972         case GL_EQUAL:
1973             d3dComp = D3D11_COMPARISON_EQUAL;
1974             break;
1975         case GL_GREATER:
1976             d3dComp = D3D11_COMPARISON_GREATER;
1977             break;
1978         case GL_GEQUAL:
1979             d3dComp = D3D11_COMPARISON_GREATER_EQUAL;
1980             break;
1981         case GL_NOTEQUAL:
1982             d3dComp = D3D11_COMPARISON_NOT_EQUAL;
1983             break;
1984         default:
1985             UNREACHABLE();
1986     }
1987 
1988     return d3dComp;
1989 }
1990 
ConvertDepthMask(bool depthWriteEnabled)1991 D3D11_DEPTH_WRITE_MASK ConvertDepthMask(bool depthWriteEnabled)
1992 {
1993     return depthWriteEnabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
1994 }
1995 
ConvertStencilMask(GLuint stencilmask)1996 UINT8 ConvertStencilMask(GLuint stencilmask)
1997 {
1998     return static_cast<UINT8>(stencilmask);
1999 }
2000 
ConvertStencilOp(GLenum stencilOp)2001 D3D11_STENCIL_OP ConvertStencilOp(GLenum stencilOp)
2002 {
2003     D3D11_STENCIL_OP d3dStencilOp = D3D11_STENCIL_OP_KEEP;
2004 
2005     switch (stencilOp)
2006     {
2007         case GL_ZERO:
2008             d3dStencilOp = D3D11_STENCIL_OP_ZERO;
2009             break;
2010         case GL_KEEP:
2011             d3dStencilOp = D3D11_STENCIL_OP_KEEP;
2012             break;
2013         case GL_REPLACE:
2014             d3dStencilOp = D3D11_STENCIL_OP_REPLACE;
2015             break;
2016         case GL_INCR:
2017             d3dStencilOp = D3D11_STENCIL_OP_INCR_SAT;
2018             break;
2019         case GL_DECR:
2020             d3dStencilOp = D3D11_STENCIL_OP_DECR_SAT;
2021             break;
2022         case GL_INVERT:
2023             d3dStencilOp = D3D11_STENCIL_OP_INVERT;
2024             break;
2025         case GL_INCR_WRAP:
2026             d3dStencilOp = D3D11_STENCIL_OP_INCR;
2027             break;
2028         case GL_DECR_WRAP:
2029             d3dStencilOp = D3D11_STENCIL_OP_DECR;
2030             break;
2031         default:
2032             UNREACHABLE();
2033     }
2034 
2035     return d3dStencilOp;
2036 }
2037 
ConvertFilter(GLenum minFilter,GLenum magFilter,float maxAnisotropy,GLenum comparisonMode)2038 D3D11_FILTER ConvertFilter(GLenum minFilter,
2039                            GLenum magFilter,
2040                            float maxAnisotropy,
2041                            GLenum comparisonMode)
2042 {
2043     bool comparison = comparisonMode != GL_NONE;
2044 
2045     if (maxAnisotropy > 1.0f)
2046     {
2047         return D3D11_ENCODE_ANISOTROPIC_FILTER(static_cast<D3D11_COMPARISON_FUNC>(comparison));
2048     }
2049     else
2050     {
2051         D3D11_FILTER_TYPE dxMin = D3D11_FILTER_TYPE_POINT;
2052         D3D11_FILTER_TYPE dxMip = D3D11_FILTER_TYPE_POINT;
2053         switch (minFilter)
2054         {
2055             case GL_NEAREST:
2056                 dxMin = D3D11_FILTER_TYPE_POINT;
2057                 dxMip = D3D11_FILTER_TYPE_POINT;
2058                 break;
2059             case GL_LINEAR:
2060                 dxMin = D3D11_FILTER_TYPE_LINEAR;
2061                 dxMip = D3D11_FILTER_TYPE_POINT;
2062                 break;
2063             case GL_NEAREST_MIPMAP_NEAREST:
2064                 dxMin = D3D11_FILTER_TYPE_POINT;
2065                 dxMip = D3D11_FILTER_TYPE_POINT;
2066                 break;
2067             case GL_LINEAR_MIPMAP_NEAREST:
2068                 dxMin = D3D11_FILTER_TYPE_LINEAR;
2069                 dxMip = D3D11_FILTER_TYPE_POINT;
2070                 break;
2071             case GL_NEAREST_MIPMAP_LINEAR:
2072                 dxMin = D3D11_FILTER_TYPE_POINT;
2073                 dxMip = D3D11_FILTER_TYPE_LINEAR;
2074                 break;
2075             case GL_LINEAR_MIPMAP_LINEAR:
2076                 dxMin = D3D11_FILTER_TYPE_LINEAR;
2077                 dxMip = D3D11_FILTER_TYPE_LINEAR;
2078                 break;
2079             default:
2080                 UNREACHABLE();
2081         }
2082 
2083         D3D11_FILTER_TYPE dxMag = D3D11_FILTER_TYPE_POINT;
2084         switch (magFilter)
2085         {
2086             case GL_NEAREST:
2087                 dxMag = D3D11_FILTER_TYPE_POINT;
2088                 break;
2089             case GL_LINEAR:
2090                 dxMag = D3D11_FILTER_TYPE_LINEAR;
2091                 break;
2092             default:
2093                 UNREACHABLE();
2094         }
2095 
2096         return D3D11_ENCODE_BASIC_FILTER(dxMin, dxMag, dxMip,
2097                                          static_cast<D3D11_COMPARISON_FUNC>(comparison));
2098     }
2099 }
2100 
ConvertTextureWrap(GLenum wrap)2101 D3D11_TEXTURE_ADDRESS_MODE ConvertTextureWrap(GLenum wrap)
2102 {
2103     switch (wrap)
2104     {
2105         case GL_REPEAT:
2106             return D3D11_TEXTURE_ADDRESS_WRAP;
2107         case GL_MIRRORED_REPEAT:
2108             return D3D11_TEXTURE_ADDRESS_MIRROR;
2109         case GL_CLAMP_TO_EDGE:
2110             return D3D11_TEXTURE_ADDRESS_CLAMP;
2111         case GL_CLAMP_TO_BORDER:
2112             return D3D11_TEXTURE_ADDRESS_BORDER;
2113         case GL_MIRROR_CLAMP_TO_EDGE_EXT:
2114             return D3D11_TEXTURE_ADDRESS_MIRROR_ONCE;
2115         default:
2116             UNREACHABLE();
2117     }
2118 
2119     return D3D11_TEXTURE_ADDRESS_WRAP;
2120 }
2121 
ConvertMaxAnisotropy(float maxAnisotropy,D3D_FEATURE_LEVEL featureLevel)2122 UINT ConvertMaxAnisotropy(float maxAnisotropy, D3D_FEATURE_LEVEL featureLevel)
2123 {
2124     return static_cast<UINT>(std::min(maxAnisotropy, d3d11_gl::GetMaximumAnisotropy(featureLevel)));
2125 }
2126 
ConvertQueryType(gl::QueryType type)2127 D3D11_QUERY ConvertQueryType(gl::QueryType type)
2128 {
2129     switch (type)
2130     {
2131         case gl::QueryType::AnySamples:
2132         case gl::QueryType::AnySamplesConservative:
2133             return D3D11_QUERY_OCCLUSION;
2134         case gl::QueryType::TransformFeedbackPrimitivesWritten:
2135             return D3D11_QUERY_SO_STATISTICS;
2136         case gl::QueryType::TimeElapsed:
2137             // Two internal queries are also created for begin/end timestamps
2138             return D3D11_QUERY_TIMESTAMP_DISJOINT;
2139         case gl::QueryType::Timestamp:
2140             // A disjoint query is also created for timestamp
2141             return D3D11_QUERY_TIMESTAMP_DISJOINT;
2142         case gl::QueryType::CommandsCompleted:
2143             return D3D11_QUERY_EVENT;
2144         default:
2145             UNREACHABLE();
2146             return D3D11_QUERY_EVENT;
2147     }
2148 }
2149 
2150 // Get the D3D11 write mask covering all color channels of a given format
GetColorMask(const gl::InternalFormat & format)2151 UINT8 GetColorMask(const gl::InternalFormat &format)
2152 {
2153     return ConvertColorMask(format.redBits > 0, format.greenBits > 0, format.blueBits > 0,
2154                             format.alphaBits > 0);
2155 }
2156 
2157 }  // namespace gl_d3d11
2158 
2159 namespace d3d11
2160 {
2161 
GetDeviceType(ID3D11Device * device)2162 ANGLED3D11DeviceType GetDeviceType(ID3D11Device *device)
2163 {
2164     // Note that this function returns an ANGLED3D11DeviceType rather than a D3D_DRIVER_TYPE value,
2165     // since it is difficult to tell Software and Reference devices apart
2166 
2167     IDXGIDevice *dxgiDevice     = nullptr;
2168     IDXGIAdapter *dxgiAdapter   = nullptr;
2169     IDXGIAdapter2 *dxgiAdapter2 = nullptr;
2170 
2171     ANGLED3D11DeviceType retDeviceType = ANGLE_D3D11_DEVICE_TYPE_UNKNOWN;
2172 
2173     HRESULT hr = device->QueryInterface(__uuidof(IDXGIDevice), (void **)&dxgiDevice);
2174     if (SUCCEEDED(hr))
2175     {
2176         hr = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&dxgiAdapter);
2177         if (SUCCEEDED(hr))
2178         {
2179             std::wstring adapterString;
2180             HRESULT adapter2hr =
2181                 dxgiAdapter->QueryInterface(__uuidof(dxgiAdapter2), (void **)&dxgiAdapter2);
2182             if (SUCCEEDED(adapter2hr))
2183             {
2184                 // On D3D_FEATURE_LEVEL_9_*, IDXGIAdapter::GetDesc returns "Software Adapter"
2185                 // for the description string. Try to use IDXGIAdapter2::GetDesc2 to get the
2186                 // actual hardware values if possible.
2187                 DXGI_ADAPTER_DESC2 adapterDesc2;
2188                 dxgiAdapter2->GetDesc2(&adapterDesc2);
2189                 adapterString = std::wstring(adapterDesc2.Description);
2190             }
2191             else
2192             {
2193                 DXGI_ADAPTER_DESC adapterDesc;
2194                 dxgiAdapter->GetDesc(&adapterDesc);
2195                 adapterString = std::wstring(adapterDesc.Description);
2196             }
2197 
2198             // Both Reference and Software adapters will be 'Software Adapter'
2199             const bool isSoftwareDevice =
2200                 (adapterString.find(std::wstring(L"Software Adapter")) != std::string::npos);
2201             const bool isNullDevice = (adapterString == L"");
2202             const bool isWARPDevice =
2203                 (adapterString.find(std::wstring(L"Basic Render")) != std::string::npos);
2204 
2205             if (isSoftwareDevice || isNullDevice)
2206             {
2207                 ASSERT(!isWARPDevice);
2208                 retDeviceType = ANGLE_D3D11_DEVICE_TYPE_SOFTWARE_REF_OR_NULL;
2209             }
2210             else if (isWARPDevice)
2211             {
2212                 retDeviceType = ANGLE_D3D11_DEVICE_TYPE_WARP;
2213             }
2214             else
2215             {
2216                 retDeviceType = ANGLE_D3D11_DEVICE_TYPE_HARDWARE;
2217             }
2218         }
2219     }
2220 
2221     SafeRelease(dxgiDevice);
2222     SafeRelease(dxgiAdapter);
2223     SafeRelease(dxgiAdapter2);
2224 
2225     return retDeviceType;
2226 }
2227 
MakeValidSize(bool isImage,DXGI_FORMAT format,GLsizei * requestWidth,GLsizei * requestHeight,int * levelOffset)2228 void MakeValidSize(bool isImage,
2229                    DXGI_FORMAT format,
2230                    GLsizei *requestWidth,
2231                    GLsizei *requestHeight,
2232                    int *levelOffset)
2233 {
2234     const DXGIFormatSize &dxgiFormatInfo = d3d11::GetDXGIFormatSizeInfo(format);
2235     bool validFormat                     = format != DXGI_FORMAT_UNKNOWN;
2236     bool validImage                      = isImage && validFormat;
2237 
2238     int upsampleCount = 0;
2239     // Don't expand the size of full textures that are at least (blockWidth x blockHeight) already.
2240     if (validImage || *requestWidth < static_cast<GLsizei>(dxgiFormatInfo.blockWidth) ||
2241         *requestHeight < static_cast<GLsizei>(dxgiFormatInfo.blockHeight))
2242     {
2243         while (*requestWidth % dxgiFormatInfo.blockWidth != 0 ||
2244                *requestHeight % dxgiFormatInfo.blockHeight != 0)
2245         {
2246             *requestWidth <<= 1;
2247             *requestHeight <<= 1;
2248             upsampleCount++;
2249         }
2250     }
2251     else if (validFormat)
2252     {
2253         if (*requestWidth % dxgiFormatInfo.blockWidth != 0)
2254         {
2255             *requestWidth = roundUp(*requestWidth, static_cast<GLsizei>(dxgiFormatInfo.blockWidth));
2256         }
2257 
2258         if (*requestHeight % dxgiFormatInfo.blockHeight != 0)
2259         {
2260             *requestHeight =
2261                 roundUp(*requestHeight, static_cast<GLsizei>(dxgiFormatInfo.blockHeight));
2262         }
2263     }
2264 
2265     if (levelOffset)
2266     {
2267         *levelOffset = upsampleCount;
2268     }
2269 }
2270 
GenerateInitialTextureData(const gl::Context * context,GLint internalFormat,const Renderer11DeviceCaps & renderer11DeviceCaps,GLuint width,GLuint height,GLuint depth,GLuint mipLevels,gl::TexLevelArray<D3D11_SUBRESOURCE_DATA> * outSubresourceData)2271 angle::Result GenerateInitialTextureData(
2272     const gl::Context *context,
2273     GLint internalFormat,
2274     const Renderer11DeviceCaps &renderer11DeviceCaps,
2275     GLuint width,
2276     GLuint height,
2277     GLuint depth,
2278     GLuint mipLevels,
2279     gl::TexLevelArray<D3D11_SUBRESOURCE_DATA> *outSubresourceData)
2280 {
2281     const d3d11::Format &d3dFormatInfo = d3d11::Format::Get(internalFormat, renderer11DeviceCaps);
2282     ASSERT(d3dFormatInfo.dataInitializerFunction != nullptr);
2283 
2284     const d3d11::DXGIFormatSize &dxgiFormatInfo =
2285         d3d11::GetDXGIFormatSizeInfo(d3dFormatInfo.texFormat);
2286 
2287     using CheckedSize        = angle::CheckedNumeric<size_t>;
2288     CheckedSize rowPitch     = CheckedSize(dxgiFormatInfo.pixelBytes) * CheckedSize(width);
2289     CheckedSize depthPitch   = rowPitch * CheckedSize(height);
2290     CheckedSize maxImageSize = depthPitch * CheckedSize(depth);
2291 
2292     Context11 *context11 = GetImplAs<Context11>(context);
2293     ANGLE_CHECK_GL_ALLOC(context11, maxImageSize.IsValid());
2294 
2295     angle::MemoryBuffer *scratchBuffer = nullptr;
2296     ANGLE_CHECK_GL_ALLOC(context11,
2297                          context->getScratchBuffer(maxImageSize.ValueOrDie(), &scratchBuffer));
2298 
2299     d3dFormatInfo.dataInitializerFunction(width, height, depth, scratchBuffer->data(),
2300                                           rowPitch.ValueOrDie(), depthPitch.ValueOrDie());
2301 
2302     for (unsigned int i = 0; i < mipLevels; i++)
2303     {
2304         unsigned int mipWidth  = std::max(width >> i, 1U);
2305         unsigned int mipHeight = std::max(height >> i, 1U);
2306 
2307         using CheckedUINT         = angle::CheckedNumeric<UINT>;
2308         CheckedUINT mipRowPitch   = CheckedUINT(dxgiFormatInfo.pixelBytes) * CheckedUINT(mipWidth);
2309         CheckedUINT mipDepthPitch = mipRowPitch * CheckedUINT(mipHeight);
2310 
2311         ANGLE_CHECK_GL_ALLOC(context11, mipRowPitch.IsValid() && mipDepthPitch.IsValid());
2312 
2313         outSubresourceData->at(i).pSysMem          = scratchBuffer->data();
2314         outSubresourceData->at(i).SysMemPitch      = mipRowPitch.ValueOrDie();
2315         outSubresourceData->at(i).SysMemSlicePitch = mipDepthPitch.ValueOrDie();
2316     }
2317 
2318     return angle::Result::Continue;
2319 }
2320 
GetPrimitiveRestartIndex()2321 UINT GetPrimitiveRestartIndex()
2322 {
2323     return std::numeric_limits<UINT>::max();
2324 }
2325 
SetPositionTexCoordVertex(PositionTexCoordVertex * vertex,float x,float y,float u,float v)2326 void SetPositionTexCoordVertex(PositionTexCoordVertex *vertex, float x, float y, float u, float v)
2327 {
2328     vertex->x = x;
2329     vertex->y = y;
2330     vertex->u = u;
2331     vertex->v = v;
2332 }
2333 
SetPositionLayerTexCoord3DVertex(PositionLayerTexCoord3DVertex * vertex,float x,float y,unsigned int layer,float u,float v,float s)2334 void SetPositionLayerTexCoord3DVertex(PositionLayerTexCoord3DVertex *vertex,
2335                                       float x,
2336                                       float y,
2337                                       unsigned int layer,
2338                                       float u,
2339                                       float v,
2340                                       float s)
2341 {
2342     vertex->x = x;
2343     vertex->y = y;
2344     vertex->l = layer;
2345     vertex->u = u;
2346     vertex->v = v;
2347     vertex->s = s;
2348 }
2349 
BlendStateKey()2350 BlendStateKey::BlendStateKey()
2351 {
2352     memset(this, 0, sizeof(BlendStateKey));
2353     blendStateExt = gl::BlendStateExt();
2354 }
2355 
BlendStateKey(const BlendStateKey & other)2356 BlendStateKey::BlendStateKey(const BlendStateKey &other)
2357 {
2358     memcpy(this, &other, sizeof(BlendStateKey));
2359 }
2360 
operator ==(const BlendStateKey & a,const BlendStateKey & b)2361 bool operator==(const BlendStateKey &a, const BlendStateKey &b)
2362 {
2363     return memcmp(&a, &b, sizeof(BlendStateKey)) == 0;
2364 }
2365 
operator !=(const BlendStateKey & a,const BlendStateKey & b)2366 bool operator!=(const BlendStateKey &a, const BlendStateKey &b)
2367 {
2368     return !(a == b);
2369 }
2370 
RasterizerStateKey()2371 RasterizerStateKey::RasterizerStateKey()
2372 {
2373     memset(this, 0, sizeof(RasterizerStateKey));
2374 }
2375 
operator ==(const RasterizerStateKey & a,const RasterizerStateKey & b)2376 bool operator==(const RasterizerStateKey &a, const RasterizerStateKey &b)
2377 {
2378     return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
2379 }
2380 
operator !=(const RasterizerStateKey & a,const RasterizerStateKey & b)2381 bool operator!=(const RasterizerStateKey &a, const RasterizerStateKey &b)
2382 {
2383     return !(a == b);
2384 }
2385 
SetDebugName(ID3D11DeviceChild * resource,const char * internalName,const std::string * khrDebugName)2386 HRESULT SetDebugName(ID3D11DeviceChild *resource,
2387                      const char *internalName,
2388                      const std::string *khrDebugName)
2389 {
2390     // Prepend ANGLE to separate names from other components in the same process.
2391     std::string d3dName = "ANGLE";
2392     bool sendNameToD3D  = false;
2393     if (internalName && internalName[0] != '\0')
2394     {
2395         d3dName += std::string("_") + internalName;
2396         sendNameToD3D = true;
2397     }
2398     if (khrDebugName && !khrDebugName->empty())
2399     {
2400         d3dName += std::string("_") + *khrDebugName;
2401         sendNameToD3D = true;
2402     }
2403     // If both internalName and khrDebugName are empty, avoid sending the string to d3d.
2404     if (sendNameToD3D)
2405     {
2406         return resource->SetPrivateData(WKPDID_D3DDebugObjectName,
2407                                         static_cast<UINT>(d3dName.size()), d3dName.c_str());
2408     }
2409     return S_OK;
2410 }
2411 
2412 // Keep this in cpp file where it has visibility of Renderer11.h, otherwise calling
2413 // allocateResource is only compatible with Clang and MSVS, which support calling a
2414 // method on a forward declared class in a template.
2415 template <ResourceType ResourceT>
resolveImpl(d3d::Context * context,Renderer11 * renderer,const GetDescType<ResourceT> & desc,GetInitDataType<ResourceT> * initData,const char * name)2416 angle::Result LazyResource<ResourceT>::resolveImpl(d3d::Context *context,
2417                                                    Renderer11 *renderer,
2418                                                    const GetDescType<ResourceT> &desc,
2419                                                    GetInitDataType<ResourceT> *initData,
2420                                                    const char *name)
2421 {
2422     if (!mResource.valid())
2423     {
2424         ANGLE_TRY(renderer->allocateResource(context, desc, initData, &mResource));
2425         mResource.setInternalName(name);
2426     }
2427     return angle::Result::Continue;
2428 }
2429 
2430 template angle::Result LazyResource<ResourceType::BlendState>::resolveImpl(
2431     d3d::Context *context,
2432     Renderer11 *renderer,
2433     const D3D11_BLEND_DESC &desc,
2434     void *initData,
2435     const char *name);
2436 template angle::Result LazyResource<ResourceType::ComputeShader>::resolveImpl(
2437     d3d::Context *context,
2438     Renderer11 *renderer,
2439     const ShaderData &desc,
2440     void *initData,
2441     const char *name);
2442 template angle::Result LazyResource<ResourceType::GeometryShader>::resolveImpl(
2443     d3d::Context *context,
2444     Renderer11 *renderer,
2445     const ShaderData &desc,
2446     const std::vector<D3D11_SO_DECLARATION_ENTRY> *initData,
2447     const char *name);
2448 template angle::Result LazyResource<ResourceType::InputLayout>::resolveImpl(
2449     d3d::Context *context,
2450     Renderer11 *renderer,
2451     const InputElementArray &desc,
2452     const ShaderData *initData,
2453     const char *name);
2454 template angle::Result LazyResource<ResourceType::PixelShader>::resolveImpl(d3d::Context *context,
2455                                                                             Renderer11 *renderer,
2456                                                                             const ShaderData &desc,
2457                                                                             void *initData,
2458                                                                             const char *name);
2459 template angle::Result LazyResource<ResourceType::VertexShader>::resolveImpl(d3d::Context *context,
2460                                                                              Renderer11 *renderer,
2461                                                                              const ShaderData &desc,
2462                                                                              void *initData,
2463                                                                              const char *name);
2464 
LazyInputLayout(const D3D11_INPUT_ELEMENT_DESC * inputDesc,size_t inputDescLen,const BYTE * byteCode,size_t byteCodeLen,const char * debugName)2465 LazyInputLayout::LazyInputLayout(const D3D11_INPUT_ELEMENT_DESC *inputDesc,
2466                                  size_t inputDescLen,
2467                                  const BYTE *byteCode,
2468                                  size_t byteCodeLen,
2469                                  const char *debugName)
2470     : mInputDesc(inputDesc, inputDescLen), mByteCode(byteCode, byteCodeLen), mDebugName(debugName)
2471 {}
2472 
~LazyInputLayout()2473 LazyInputLayout::~LazyInputLayout() {}
2474 
resolve(d3d::Context * context,Renderer11 * renderer)2475 angle::Result LazyInputLayout::resolve(d3d::Context *context, Renderer11 *renderer)
2476 {
2477     return resolveImpl(context, renderer, mInputDesc, &mByteCode, mDebugName);
2478 }
2479 
LazyBlendState(const D3D11_BLEND_DESC & desc,const char * debugName)2480 LazyBlendState::LazyBlendState(const D3D11_BLEND_DESC &desc, const char *debugName)
2481     : mDesc(desc), mDebugName(debugName)
2482 {}
2483 
resolve(d3d::Context * context,Renderer11 * renderer)2484 angle::Result LazyBlendState::resolve(d3d::Context *context, Renderer11 *renderer)
2485 {
2486     return resolveImpl(context, renderer, mDesc, nullptr, mDebugName);
2487 }
2488 
InitializeFeatures(const Renderer11DeviceCaps & deviceCaps,const DXGI_ADAPTER_DESC & adapterDesc,angle::FeaturesD3D * features)2489 void InitializeFeatures(const Renderer11DeviceCaps &deviceCaps,
2490                         const DXGI_ADAPTER_DESC &adapterDesc,
2491                         angle::FeaturesD3D *features)
2492 {
2493     bool isNvidia          = IsNvidia(adapterDesc.VendorId);
2494     bool isIntel           = IsIntel(adapterDesc.VendorId);
2495     bool isSkylake         = false;
2496     bool isBroadwell       = false;
2497     bool isHaswell         = false;
2498     bool isIvyBridge       = false;
2499     bool isAMD             = IsAMD(adapterDesc.VendorId);
2500     bool isFeatureLevel9_3 = deviceCaps.featureLevel <= D3D_FEATURE_LEVEL_9_3;
2501 
2502     IntelDriverVersion capsVersion = IntelDriverVersion(0);
2503     if (isIntel)
2504     {
2505         capsVersion = d3d11_gl::GetIntelDriverVersion(deviceCaps.driverVersion);
2506 
2507         isSkylake   = IsSkylake(adapterDesc.DeviceId);
2508         isBroadwell = IsBroadwell(adapterDesc.DeviceId);
2509         isHaswell   = IsHaswell(adapterDesc.DeviceId);
2510         isIvyBridge = IsIvyBridge(adapterDesc.DeviceId);
2511     }
2512 
2513     if (isNvidia)
2514     {
2515         // TODO(jmadill): Narrow problematic driver range.
2516         bool driverVersionValid = deviceCaps.driverVersion.valid();
2517         if (driverVersionValid)
2518         {
2519             WORD part1 = HIWORD(deviceCaps.driverVersion.value().LowPart);
2520             WORD part2 = LOWORD(deviceCaps.driverVersion.value().LowPart);
2521 
2522             // Disable the workaround to fix a second driver bug on newer NVIDIA.
2523             ANGLE_FEATURE_CONDITION(
2524                 features, depthStencilBlitExtraCopy,
2525                 (part1 <= 13u && part2 < 6881) && isNvidia && driverVersionValid);
2526         }
2527         else
2528         {
2529             ANGLE_FEATURE_CONDITION(features, depthStencilBlitExtraCopy,
2530                                     isNvidia && !driverVersionValid);
2531         }
2532     }
2533 
2534     ANGLE_FEATURE_CONDITION(features, mrtPerfWorkaround, true);
2535     ANGLE_FEATURE_CONDITION(features, zeroMaxLodWorkaround, isFeatureLevel9_3);
2536     ANGLE_FEATURE_CONDITION(features, allowES3OnFL100, false);
2537 
2538     // TODO(jmadill): Disable workaround when we have a fixed compiler DLL.
2539     ANGLE_FEATURE_CONDITION(features, expandIntegerPowExpressions, true);
2540 
2541     ANGLE_FEATURE_CONDITION(features, flushAfterEndingTransformFeedback, isNvidia);
2542     ANGLE_FEATURE_CONDITION(features, getDimensionsIgnoresBaseLevel, isNvidia);
2543     ANGLE_FEATURE_CONDITION(features, skipVSConstantRegisterZero, isNvidia);
2544     ANGLE_FEATURE_CONDITION(features, forceAtomicValueResolution, isNvidia);
2545 
2546     ANGLE_FEATURE_CONDITION(features, preAddTexelFetchOffsets, isIntel);
2547     ANGLE_FEATURE_CONDITION(features, useSystemMemoryForConstantBuffers, isIntel);
2548 
2549     ANGLE_FEATURE_CONDITION(features, callClearTwice,
2550                             isIntel && isSkylake && capsVersion >= IntelDriverVersion(160000) &&
2551                                 capsVersion < IntelDriverVersion(164771));
2552     ANGLE_FEATURE_CONDITION(features, emulateIsnanFloat,
2553                             isIntel && isSkylake && capsVersion >= IntelDriverVersion(160000) &&
2554                                 capsVersion < IntelDriverVersion(164542));
2555     ANGLE_FEATURE_CONDITION(features, rewriteUnaryMinusOperator,
2556                             isIntel && (isBroadwell || isHaswell) &&
2557                                 capsVersion >= IntelDriverVersion(150000) &&
2558                                 capsVersion < IntelDriverVersion(154624));
2559 
2560     ANGLE_FEATURE_CONDITION(features, addMockTextureNoRenderTarget,
2561                             isIntel && capsVersion >= IntelDriverVersion(160000) &&
2562                                 capsVersion < IntelDriverVersion(164815));
2563 
2564     // Haswell/Ivybridge drivers occasionally corrupt (small?) (vertex?) texture data uploads.
2565     ANGLE_FEATURE_CONDITION(features, setDataFasterThanImageUpload,
2566                             !(isIvyBridge || isBroadwell || isHaswell));
2567 
2568     ANGLE_FEATURE_CONDITION(features, disableB5G6R5Support,
2569                             (isIntel && capsVersion >= IntelDriverVersion(150000) &&
2570                              capsVersion < IntelDriverVersion(154539)) ||
2571                                 isAMD);
2572 
2573     // TODO(jmadill): Disable when we have a fixed driver version.
2574     // The tiny stencil texture workaround involves using CopySubresource or UpdateSubresource on a
2575     // depth stencil texture.  This is not allowed until feature level 10.1 but since it is not
2576     // possible to support ES3 on these devices, there is no need for the workaround to begin with
2577     // (anglebug.com/42260536).
2578     ANGLE_FEATURE_CONDITION(features, emulateTinyStencilTextures,
2579                             isAMD && !(deviceCaps.featureLevel < D3D_FEATURE_LEVEL_10_1));
2580 
2581     // If the VPAndRTArrayIndexFromAnyShaderFeedingRasterizer feature is not available, we have to
2582     // select the viewport / RT array index in the geometry shader.
2583     ANGLE_FEATURE_CONDITION(features, selectViewInGeometryShader,
2584                             !deviceCaps.supportsVpRtIndexWriteFromVertexShader);
2585 
2586     // NVidia drivers have no trouble clearing textures without showing corruption.
2587     // Intel and AMD drivers that have trouble have been blocklisted by Chromium. In the case of
2588     // Intel, they've been blocklisted to the DX9 runtime.
2589     ANGLE_FEATURE_CONDITION(features, allowClearForRobustResourceInit, true);
2590 
2591     // Allow translating uniform block to StructuredBuffer on Windows 10. This is targeted
2592     // to work around a slow fxc compile performance issue with dynamic uniform indexing.
2593     ANGLE_FEATURE_CONDITION(features, allowTranslateUniformBlockToStructuredBuffer,
2594                             IsWindows10OrLater());
2595 
2596     // D3D11 Feature Levels 9_3 and below do not support non-constant loop indexing and require
2597     // additional
2598     // pre-validation of the shader at compile time to produce a better error message.
2599     ANGLE_FEATURE_CONDITION(features, supportsNonConstantLoopIndexing, !isFeatureLevel9_3);
2600 }
2601 
InitializeFrontendFeatures(const DXGI_ADAPTER_DESC & adapterDesc,angle::FrontendFeatures * features)2602 void InitializeFrontendFeatures(const DXGI_ADAPTER_DESC &adapterDesc,
2603                                 angle::FrontendFeatures *features)
2604 {
2605     bool isAMD = IsAMD(adapterDesc.VendorId);
2606 
2607     ANGLE_FEATURE_CONDITION(features, forceDepthAttachmentInitOnClear, isAMD);
2608 
2609     // The D3D backend's handling of compile and link is thread-safe
2610     ANGLE_FEATURE_CONDITION(features, compileJobIsThreadSafe, true);
2611     ANGLE_FEATURE_CONDITION(features, linkJobIsThreadSafe, true);
2612 }
2613 
InitConstantBufferDesc(D3D11_BUFFER_DESC * constantBufferDescription,size_t byteWidth)2614 void InitConstantBufferDesc(D3D11_BUFFER_DESC *constantBufferDescription, size_t byteWidth)
2615 {
2616     constantBufferDescription->ByteWidth           = static_cast<UINT>(byteWidth);
2617     constantBufferDescription->Usage               = D3D11_USAGE_DYNAMIC;
2618     constantBufferDescription->BindFlags           = D3D11_BIND_CONSTANT_BUFFER;
2619     constantBufferDescription->CPUAccessFlags      = D3D11_CPU_ACCESS_WRITE;
2620     constantBufferDescription->MiscFlags           = 0;
2621     constantBufferDescription->StructureByteStride = 0;
2622 }
2623 
2624 }  // namespace d3d11
2625 
2626 // TextureHelper11 implementation.
TextureHelper11()2627 TextureHelper11::TextureHelper11() : mFormatSet(nullptr), mSampleCount(0) {}
2628 
TextureHelper11(TextureHelper11 && toCopy)2629 TextureHelper11::TextureHelper11(TextureHelper11 &&toCopy) : TextureHelper11()
2630 {
2631     *this = std::move(toCopy);
2632 }
2633 
TextureHelper11(const TextureHelper11 & other)2634 TextureHelper11::TextureHelper11(const TextureHelper11 &other)
2635     : mFormatSet(other.mFormatSet), mExtents(other.mExtents), mSampleCount(other.mSampleCount)
2636 {
2637     mData = other.mData;
2638 }
2639 
~TextureHelper11()2640 TextureHelper11::~TextureHelper11() {}
2641 
getDesc(D3D11_TEXTURE2D_DESC * desc) const2642 void TextureHelper11::getDesc(D3D11_TEXTURE2D_DESC *desc) const
2643 {
2644     static_cast<ID3D11Texture2D *>(mData->object)->GetDesc(desc);
2645 }
2646 
getDesc(D3D11_TEXTURE3D_DESC * desc) const2647 void TextureHelper11::getDesc(D3D11_TEXTURE3D_DESC *desc) const
2648 {
2649     static_cast<ID3D11Texture3D *>(mData->object)->GetDesc(desc);
2650 }
2651 
getDesc(D3D11_BUFFER_DESC * desc) const2652 void TextureHelper11::getDesc(D3D11_BUFFER_DESC *desc) const
2653 {
2654     static_cast<ID3D11Buffer *>(mData->object)->GetDesc(desc);
2655 }
2656 
initDesc(const D3D11_TEXTURE2D_DESC & desc2D)2657 void TextureHelper11::initDesc(const D3D11_TEXTURE2D_DESC &desc2D)
2658 {
2659     mData->resourceType = ResourceType::Texture2D;
2660     mExtents.width      = static_cast<int>(desc2D.Width);
2661     mExtents.height     = static_cast<int>(desc2D.Height);
2662     mExtents.depth      = 1;
2663     mSampleCount        = desc2D.SampleDesc.Count;
2664 }
2665 
initDesc(const D3D11_TEXTURE3D_DESC & desc3D)2666 void TextureHelper11::initDesc(const D3D11_TEXTURE3D_DESC &desc3D)
2667 {
2668     mData->resourceType = ResourceType::Texture3D;
2669     mExtents.width      = static_cast<int>(desc3D.Width);
2670     mExtents.height     = static_cast<int>(desc3D.Height);
2671     mExtents.depth      = static_cast<int>(desc3D.Depth);
2672     mSampleCount        = 1;
2673 }
2674 
initDesc(const D3D11_BUFFER_DESC & descBuffer)2675 void TextureHelper11::initDesc(const D3D11_BUFFER_DESC &descBuffer)
2676 {
2677     mData->resourceType = ResourceType::Buffer;
2678     mExtents.width      = static_cast<int>(descBuffer.ByteWidth);
2679     mExtents.height     = 1;
2680     mExtents.depth      = 1;
2681     mSampleCount        = 1;
2682 }
2683 
operator =(TextureHelper11 && other)2684 TextureHelper11 &TextureHelper11::operator=(TextureHelper11 &&other)
2685 {
2686     std::swap(mData, other.mData);
2687     std::swap(mExtents, other.mExtents);
2688     std::swap(mFormatSet, other.mFormatSet);
2689     std::swap(mSampleCount, other.mSampleCount);
2690     return *this;
2691 }
2692 
operator =(const TextureHelper11 & other)2693 TextureHelper11 &TextureHelper11::operator=(const TextureHelper11 &other)
2694 {
2695     mData        = other.mData;
2696     mExtents     = other.mExtents;
2697     mFormatSet   = other.mFormatSet;
2698     mSampleCount = other.mSampleCount;
2699     return *this;
2700 }
2701 
operator ==(const TextureHelper11 & other) const2702 bool TextureHelper11::operator==(const TextureHelper11 &other) const
2703 {
2704     return mData->object == other.mData->object;
2705 }
2706 
operator !=(const TextureHelper11 & other) const2707 bool TextureHelper11::operator!=(const TextureHelper11 &other) const
2708 {
2709     return mData->object != other.mData->object;
2710 }
2711 
UsePresentPathFast(const Renderer11 * renderer,const gl::FramebufferAttachment * framebufferAttachment)2712 bool UsePresentPathFast(const Renderer11 *renderer,
2713                         const gl::FramebufferAttachment *framebufferAttachment)
2714 {
2715     if (framebufferAttachment == nullptr)
2716     {
2717         return false;
2718     }
2719 
2720     return (framebufferAttachment->type() == GL_FRAMEBUFFER_DEFAULT &&
2721             renderer->presentPathFastEnabled());
2722 }
2723 
UsePrimitiveRestartWorkaround(bool primitiveRestartFixedIndexEnabled,gl::DrawElementsType type)2724 bool UsePrimitiveRestartWorkaround(bool primitiveRestartFixedIndexEnabled,
2725                                    gl::DrawElementsType type)
2726 {
2727     // We should never have to deal with primitive restart workaround issue with GL_UNSIGNED_INT
2728     // indices, since we restrict it via MAX_ELEMENT_INDEX.
2729     return (!primitiveRestartFixedIndexEnabled && type == gl::DrawElementsType::UnsignedShort);
2730 }
2731 
ClassifyIndexStorage(const gl::State & glState,const gl::Buffer * elementArrayBuffer,gl::DrawElementsType elementType,gl::DrawElementsType destElementType,unsigned int offset)2732 IndexStorageType ClassifyIndexStorage(const gl::State &glState,
2733                                       const gl::Buffer *elementArrayBuffer,
2734                                       gl::DrawElementsType elementType,
2735                                       gl::DrawElementsType destElementType,
2736                                       unsigned int offset)
2737 {
2738     // No buffer bound means we are streaming from a client pointer.
2739     if (!elementArrayBuffer || !IsOffsetAligned(elementType, offset))
2740     {
2741         return IndexStorageType::Dynamic;
2742     }
2743 
2744     // The buffer can be used directly if the storage supports it and no translation needed.
2745     BufferD3D *bufferD3D = GetImplAs<BufferD3D>(elementArrayBuffer);
2746     if (bufferD3D->supportsDirectBinding() && destElementType == elementType)
2747     {
2748         return IndexStorageType::Direct;
2749     }
2750 
2751     // Use a static copy when available.
2752     StaticIndexBufferInterface *staticBuffer = bufferD3D->getStaticIndexBuffer();
2753     if (staticBuffer != nullptr)
2754     {
2755         return IndexStorageType::Static;
2756     }
2757 
2758     // Static buffer not available, fall back to streaming.
2759     return IndexStorageType::Dynamic;
2760 }
2761 
SwizzleRequired(const gl::TextureState & textureState)2762 bool SwizzleRequired(const gl::TextureState &textureState)
2763 {
2764     // When sampling stencil, a swizzle is needed to move the stencil channel from G to R.
2765     return textureState.swizzleRequired() || textureState.isStencilMode();
2766 }
2767 
GetEffectiveSwizzle(const gl::TextureState & textureState)2768 gl::SwizzleState GetEffectiveSwizzle(const gl::TextureState &textureState)
2769 {
2770     const gl::SwizzleState &swizzle = textureState.getSwizzleState();
2771     if (textureState.isStencilMode())
2772     {
2773         // Per GL semantics, the stencil value should be in the red channel, while D3D11 formats
2774         // leave stencil in the green channel. So copy the stencil value from green to all
2775         // components requesting red. Green and blue become zero; alpha becomes one.
2776         std::unordered_map<GLenum, GLenum> map = {{GL_RED, GL_GREEN}, {GL_GREEN, GL_ZERO},
2777                                                   {GL_BLUE, GL_ZERO}, {GL_ALPHA, GL_ONE},
2778                                                   {GL_ZERO, GL_ZERO}, {GL_ONE, GL_ONE}};
2779 
2780         return gl::SwizzleState(map[swizzle.swizzleRed], map[swizzle.swizzleGreen],
2781                                 map[swizzle.swizzleBlue], map[swizzle.swizzleAlpha]);
2782     }
2783     return swizzle;
2784 }
2785 
2786 }  // namespace rx
2787