1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#ifndef VBO_H
#define VBO_H
#include <vector>
#include "../Utilities/UtilMacros.h"
#include "../Shaders/ShaderChannel.h"
#include "OpenGL.h"
#include "GPUDataBuffer.h"
// 默认的顶点属性,够用了
enum EVertexAttr
{
VertexAttr_Position = 0,
VertexAttr_Normal,
VertexAttr_Tangent,
VertexAttr_Color,
VertexAttr_UV,
VertexAttr_UV2,
VertexAttr_UV3,
VertexAttr_UV4,
VertexAttr_Count
};
enum EPrimitive
{
Primitive_Triangle = 1,
Primitive_Line = 2,
Primitive_Point = 3,
};
struct VertexAttributeDescriptor
{
const void* pointer; // 内存或显存
uint componentType;
uint componentNum;
uint16 stride;
};
// GPU侧的顶点属性
struct VertexDataInfo
{
uint32 enableMask;
GLuint buffer;
VertexAttributeDescriptor attributes[VertexAttr_Count];
};
class VertexBuffer
{
public:
enum VertexBufferType
{
VertexBufferType_Static, // device
VertexBufferType_Stream, // pinned (best performance)
VertexBufferType_Dynamic,// device
};
VertexBuffer(VertexBufferType type);
~VertexBuffer();
void Draw();
private:
VertexBufferType m_Type;
GPU::DataBuffer *m_VB;
GPU::DataBuffer *m_IB;// vertex buffer and index buffer
};
class SharedVertexBuffer
{
public:
SharedVertexBuffer();
~SharedVertexBuffer();
void GetChunk(uint attrs, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib);
void ReleaseChunk(int actualVerts, int actualIndices);
void DrawChunk();
private:
void FillVertexArrayInfo(VertexDataInfo& dst);
void Clean();
// if vertex databuffer size beyond this value, use pinned memory mapping, instead of glBufferData.
enum { DataBufferThreshold = 1024 };
// shared vbo and ibo
GPU::DataBuffer *m_CurVB;
GPU::DataBuffer *m_CurIB;
// restore vbo and ibo data if not using pinned memory mapping
std::vector<byte> m_CurVBData;
std::vector<uint16> m_CurIBData;
EPrimitive m_CurPrimitive;
uint m_CurAttrMask;
uint m_CurStride;
uint m_CurVertexCount;
uint m_CurIndexCount;
};
#endif
|