Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Graphics/GraphicsEngineOpenGL/include/VAOCache.h Source File

Diligent Engine API Reference

VAOCache.h
1 /* Copyright 2015-2018 Egor Yusov
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12 *
13 * In no event and under no legal theory, whether in tort (including negligence),
14 * contract, or otherwise, unless required by applicable law (such as deliberate
15 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16 * liable for any damages, including any direct, indirect, special, incidental,
17 * or consequential damages of any character arising as a result of this License or
18 * out of the use or inability to use the software (including but not limited to damages
19 * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20 * all other commercial damages or losses), even if such Contributor has been advised
21 * of the possibility of such damages.
22 */
23
24 #pragma once
25
26 #include <cstring>
27 #include "GraphicsTypes.h"
28 #include "Buffer.h"
29 #include "InputLayout.h"
30 #include "LockHelper.h"
31 #include "HashUtils.h"
32 #include "DeviceContextBase.h"
33 #include "BaseInterfacesGL.h"
34
35 namespace Diligent
36 {
37
38 class VAOCache
39 {
40 public:
41 VAOCache();
42 ~VAOCache();
43
44 VAOCache(const VAOCache&) = delete;
45 VAOCache( VAOCache&&) = delete;
46 VAOCache& operator = (const VAOCache&) = delete;
47 VAOCache& operator = ( VAOCache&&) = delete;
48
49
50
51 const GLObjectWrappers::GLVertexArrayObj& GetVAO( IPipelineState *pPSO,
52 IBuffer *pIndexBuffer,
53 VertexStreamInfo VertexStreams[],
54 Uint32 NumVertexStreams,
55 class GLContextState &GLContextState);
56 const GLObjectWrappers::GLVertexArrayObj& GetEmptyVAO();
57
58 void OnDestroyBuffer(IBuffer *pBuffer);
59 void OnDestroyPSO(IPipelineState *pPSO);
60
61 private:
62 // This structure is used as the key to find VAO
63 struct VAOCacheKey
64 {
65 VAOCacheKey(const IPipelineState* pso, const IBuffer* indBuffer) :
66 pPSO(pso),
67 pIndexBuffer(indBuffer),
68 NumUsedSlots(0)
69 {}
70
71 // Note that the the pointers are used for ordering only
72 // They are not used to access the objects
73
74 // VAO encapsulates both input layout and all bound buffers
75 // PSO uniqly defines the layout (attrib pointers, divisors, etc.),
76 // so we do not need to add individual layout elements to the key
77 // The keey needs to contain all bound buffers
78 const IPipelineState* const pPSO;
79 const IBuffer* const pIndexBuffer;
80 Uint32 NumUsedSlots;
81 struct StreamAttribs
82 {
83 const IBuffer* pBuffer;
84 Uint32 Stride;
85 Uint32 Offset;
86 }Streams[MaxBufferSlots];
87
88 mutable size_t Hash = 0;
89
90 bool operator == (const VAOCacheKey &Key)const
91 {
92 return pPSO == Key.pPSO &&
93 pIndexBuffer == Key.pIndexBuffer &&
94 NumUsedSlots == Key.NumUsedSlots &&
95 std::memcmp(Streams, Key.Streams, sizeof(StreamAttribs) * NumUsedSlots) == 0;
96 }
97 };
98
99 struct VAOCacheKeyHashFunc
100 {
101 std::size_t operator() ( const VAOCacheKey& Key )const
102 {
103 if (Key.Hash == 0)
104 {
105 std::size_t Seed = 0;
106 HashCombine(Seed, Key.pPSO, Key.NumUsedSlots);
107 if (Key.pIndexBuffer)
108 HashCombine(Seed, Key.pIndexBuffer);
109 for (Uint32 slot = 0; slot < Key.NumUsedSlots; ++slot)
110 {
111 auto &CurrStream = Key.Streams[slot];
112 if (CurrStream.pBuffer)
113 {
114 HashCombine(Seed, CurrStream.pBuffer);
115 HashCombine(Seed, CurrStream.Offset);
116 HashCombine(Seed, CurrStream.Stride);
117 }
118 }
119 Key.Hash = Seed;
120 }
121 return Key.Hash;
122 }
123 };
124
125
126 friend class RenderDeviceGLImpl;
127 ThreadingTools::LockFlag m_CacheLockFlag;
128 std::unordered_map<VAOCacheKey, GLObjectWrappers::GLVertexArrayObj, VAOCacheKeyHashFunc> m_Cache;
129 std::unordered_multimap<const IPipelineState*, VAOCacheKey> m_PSOToKey;
130 std::unordered_multimap<const IBuffer*, VAOCacheKey> m_BuffToKey;
131
132 // Any draw command fails if no VAO is bound. We will use this empty
133 // VAO for draw commands with null input layout, such as these that
134 // only use VertexID as input.
135 GLObjectWrappers::GLVertexArrayObj m_EmptyVAO;
136 };
137
138 }
Diligent::MaxBufferSlots
static constexpr Uint32 MaxBufferSlots
Maximum number of input buffer slots. D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT == 32...
Definition: Constants.h:33
Diligent
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34