1 //
2 // Copyright (c) 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 // InputLayoutCache.h: Defines InputLayoutCache, a class that builds and caches
8 // D3D11 input layouts.
9 
10 #ifndef LIBANGLE_RENDERER_D3D_D3D11_INPUTLAYOUTCACHE_H_
11 #define LIBANGLE_RENDERER_D3D_D3D11_INPUTLAYOUTCACHE_H_
12 
13 #include <GLES2/gl2.h>
14 
15 #include <cstddef>
16 
17 #include <array>
18 #include <map>
19 
20 #include "common/angleutils.h"
21 #include "libANGLE/Constants.h"
22 #include "libANGLE/Error.h"
23 #include "libANGLE/formatutils.h"
24 #include "libANGLE/renderer/d3d/RendererD3D.h"
25 
26 namespace gl
27 {
28 class Program;
29 }
30 
31 namespace rx
32 {
33 struct TranslatedAttribute;
34 struct TranslatedIndexData;
35 struct SourceIndexData;
36 class ProgramD3D;
37 
38 class InputLayoutCache : angle::NonCopyable
39 {
40   public:
41     InputLayoutCache();
42     virtual ~InputLayoutCache();
43 
44     void initialize(ID3D11Device *device, ID3D11DeviceContext *context);
45     void clear();
46     void markDirty();
47 
48     gl::Error applyVertexBuffers(const gl::State &state,
49                                  const std::vector<TranslatedAttribute> &vertexArrayAttribs,
50                                  const std::vector<TranslatedAttribute> &currentValueAttribs,
51                                  GLenum mode,
52                                  GLint start,
53                                  TranslatedIndexData *indexInfo,
54                                  GLsizei numIndicesPerInstance);
55 
56     gl::Error updateVertexOffsetsForPointSpritesEmulation(GLint startVertex,
57                                                           GLsizei emulatedInstanceId);
58 
59     // Useful for testing
setCacheSize(unsigned int cacheSize)60     void setCacheSize(unsigned int cacheSize) { mCacheSize = cacheSize; }
61 
62   private:
63     struct PackedAttributeLayout
64     {
PackedAttributeLayoutPackedAttributeLayout65         PackedAttributeLayout()
66             : numAttributes(0),
67               flags(0)
68         {
69         }
70 
71         void addAttributeData(GLenum glType,
72                               UINT semanticIndex,
73                               gl::VertexFormatType vertexFormatType,
74                               unsigned int divisor);
75 
76         bool operator<(const PackedAttributeLayout &other) const;
77 
78         enum Flags
79         {
80             FLAG_USES_INSTANCED_SPRITES     = 0x1,
81             FLAG_INSTANCED_SPRITES_ACTIVE   = 0x2,
82             FLAG_INSTANCED_RENDERING_ACTIVE = 0x4,
83         };
84 
85         size_t numAttributes;
86         unsigned int flags;
87         uint32_t attributeData[gl::MAX_VERTEX_ATTRIBS];
88     };
89 
90     gl::Error updateInputLayout(const gl::State &state,
91                                 GLenum mode,
92                                 const AttribIndexArray &sortedSemanticIndices,
93                                 GLsizei numIndicesPerInstance);
94     gl::Error createInputLayout(const AttribIndexArray &sortedSemanticIndices,
95                                 GLenum mode,
96                                 gl::Program *program,
97                                 GLsizei numIndicesPerInstance,
98                                 ID3D11InputLayout **inputLayoutOut);
99 
100     std::map<PackedAttributeLayout, ID3D11InputLayout *> mLayoutMap;
101 
102     ID3D11InputLayout *mCurrentIL;
103     std::array<ID3D11Buffer *, gl::MAX_VERTEX_ATTRIBS> mCurrentBuffers;
104     std::array<UINT, gl::MAX_VERTEX_ATTRIBS> mCurrentVertexStrides;
105     std::array<UINT, gl::MAX_VERTEX_ATTRIBS> mCurrentVertexOffsets;
106     std::vector<const TranslatedAttribute *> mCurrentAttributes;
107 
108     ID3D11Buffer *mPointSpriteVertexBuffer;
109     ID3D11Buffer *mPointSpriteIndexBuffer;
110 
111     unsigned int mCacheSize;
112 
113     ID3D11Device *mDevice;
114     ID3D11DeviceContext *mDeviceContext;
115     D3D_FEATURE_LEVEL mFeatureLevel;
116 };
117 
118 }  // namespace rx
119 
120 #endif // LIBANGLE_RENDERER_D3D_D3D11_INPUTLAYOUTCACHE_H_
121