1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsGLPrerequisites.h"
6#include "Utility/BsModule.h"
7
8namespace bs { namespace ct
9{
10 /** @addtogroup GL
11 * @{
12 */
13
14 /**
15 * Vertex array object that contains vertex buffer object bindings and vertex attribute pointers for a specific set of
16 * vertex buffers and a vertex declaration.
17 */
18 class GLVertexArrayObject
19 {
20 private:
21 /** Generates hash value for the VAO object. */
22 class Hash
23 {
24 public:
25 ::std::size_t operator()(const GLVertexArrayObject& vao) const;
26 };
27
28 /** Checks if two VAO objects are equal. */
29 class Equal
30 {
31 public:
32 bool operator()(const GLVertexArrayObject &a, const GLVertexArrayObject &b) const;
33 };
34
35 public:
36 bool operator== (const GLVertexArrayObject& obj);
37 bool operator!= (const GLVertexArrayObject& obj);
38
39 /** Returns internal OpenGL VBO handle. */
40 GLuint getGLHandle() const { return mHandle; }
41
42 private:
43 friend class GLVertexArrayObjectManager;
44
45 GLVertexArrayObject() = default;
46 GLVertexArrayObject(GLuint handle, UINT64 vertexProgramId, GLVertexBuffer** attachedBuffers, UINT32 numBuffers);
47
48 GLuint mHandle = 0;
49 UINT64 mVertProgId = 0;
50 GLVertexBuffer** mAttachedBuffers = nullptr;
51 UINT32 mNumBuffers = 0;
52 };
53
54 /** Manager that handles creation and destruction of vertex array objects. */
55 class GLVertexArrayObjectManager : public Module<GLVertexArrayObjectManager>
56 {
57 public:
58 ~GLVertexArrayObjectManager();
59
60 /**
61 * Attempts to find an existing vertex array object matching the provided set of vertex buffers, vertex declaration,
62 * and vertex shader input parameters. If one cannot be found new one is created and returned.
63 *
64 * Lifetime of returned VAO is managed by the vertex buffers that it binds.
65 */
66 const GLVertexArrayObject& getVAO(const SPtr<GLSLGpuProgram>& vertexProgram,
67 const SPtr<VertexDeclaration>& vertexDecl, const std::array<SPtr<VertexBuffer>, 32>& boundBuffers);
68
69 /** Called when a vertex buffer containing the provided VAO is destroyed. */
70 void notifyBufferDestroyed(GLVertexArrayObject vao);
71 private:
72 typedef UnorderedSet<GLVertexArrayObject, GLVertexArrayObject::Hash, GLVertexArrayObject::Equal> VAOMap;
73
74 VAOMap mVAObjects;
75 };
76
77 /** @} */
78}}
79