summaryrefslogtreecommitdiff
path: root/Runtime/GfxDevice/opengles20/VBOGLES20.h
blob: 62ce55d06bbd2528f843a558a1d4119be43072fa (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef VBO_GLES20_H
#define VBO_GLES20_H

#include "Runtime/Shaders/BufferedVBO.h"
#include "IncludesGLES20.h"
#include <map>

class GLES2VBO : public BufferedVBO {
public:
	GLES2VBO();
	virtual ~GLES2VBO ();

	virtual void UpdateVertexData( const VertexBufferData& buffer );
	virtual void UpdateIndexData (const IndexBufferData& buffer);
	virtual void DrawVBO (const ChannelAssigns& channels, UInt32 firstIndexByte, UInt32 indexCount,
					  GfxPrimitiveType topology, UInt32 firstVertex, UInt32 vertexCount );
	virtual void DrawCustomIndexed( const ChannelAssigns& channels, void* indices, UInt32 indexCount,
								   GfxPrimitiveType topology, UInt32 vertexRangeBegin, UInt32 vertexRangeEnd, UInt32 drawVertexCount );

	virtual bool MapVertexStream( VertexStreamData& outData, unsigned stream );
	virtual void UnmapVertexStream( unsigned stream );

	virtual void Cleanup();
	virtual void Recreate();
	virtual bool IsVertexBufferLost() const;
	virtual bool IsIndexBufferLost() const;
	virtual void MarkBuffersLost();

	virtual bool IsUsingSourceVertices() const { return m_VertexBufferID[0] == 0; }
	virtual bool IsUsingSourceIndices() const { return m_IndexBufferID == 0; }

	virtual int GetRuntimeMemorySize() const;

private:

	void DrawInternal(int vertexBufferID, int indexBufferID, const ChannelAssigns& channels, void* indices, UInt32 indexCount, GfxPrimitiveType topology, UInt32 vertexRangeBegin, UInt32 vertexRangeEnd, UInt32 drawVertexCount);

	UInt32 GetUnavailableChannels(const ChannelAssigns& channels) const;

	ChannelInfoArray	m_Channels;
	IndexBufferData		m_IBData;
	enum { DynamicVertexBufferCount = 3 };
	int					m_VertexBufferID[DynamicVertexBufferCount];
	int					m_CurrentBufferIndex;
	int					m_IndexBufferID;
	int					m_IBSize;
	void*				m_ReadableIndices;

	int					m_VBOUsage;
	int					m_IBOUsage;


	bool				m_UsesVBO;
	bool				m_UsesIBO;

	// for now these are only called on UpdateVertexData/UpdateIndexData
	// so they are extracted only for convinience
	bool				ShouldUseVBO();
	bool				ShouldUseIBO();

	void				EnsureVerticesInited(bool newBuffers);
	void				EnsureIndicesInited();
};

class SharedBuffer;

// Usage:
// 1) GetChunk
//    if this returns false, don't use data pointers, bail out
// 2) fill with data
// 3) ReleaseChunk
// 4) DrawChunk (possibly multiple times)
//
// The key is that drawing must happen immediately after filling the chunk, because the next
// GetChunk might destroy the previous chunk's data. So never count on chunks being persistent.
class DynamicGLES2VBO : public DynamicVBO
{
public:
	DynamicGLES2VBO();
	~DynamicGLES2VBO();

	virtual bool GetChunk( UInt32 shaderChannelMask, UInt32 maxVertices, UInt32 maxIndices, RenderMode renderMode, void** outVB, void** outIB );
	virtual void ReleaseChunk( UInt32 actualVertices, UInt32 actualIndices );
	virtual void DrawChunk (const ChannelAssigns& channels);

	virtual void Recreate();

	SharedBuffer*	GetSharedVB (size_t bytes);
	SharedBuffer*	GetSharedIB (size_t bytes);

	void*			GetVertexMemory(size_t bytes);
	void*			GetIndexMemory(size_t bytes);

private:
	void InitializeQuadsIB();

private:
	//chai: 记录channel在m_VtxSysMemStorage中的各个channel的首地址
	void*			m_BufferChannel[kShaderChannelCount];
	RenderMode		m_LastRenderMode;

	//chai: 记录开启的channel
  //UInt32 m_LastChunkShaderChannelMask;


	SharedBuffer*	m_VB;
	SharedBuffer*	m_LargeVB;
	SharedBuffer*	m_ActiveVB;
	SharedBuffer*	m_IB;
	SharedBuffer*	m_LargeIB;
	SharedBuffer*	m_ActiveIB;

	UInt16*			m_QuadsIB;
	int				m_IndexBufferQuadsID;

	UInt8*			m_VtxSysMemStorage;
	unsigned		m_VtxSysMemStorageSize;

	UInt16*			m_IdxSysMemStorage;
	unsigned		m_IdxSysMemStorageSize;
};

void InvalidateVertexInputCacheGLES20();
// WARNING: forceBufferObject is just a temp solution, as we really need to clean up all this mess
// we need it for bigger dynamic vbos
void* LockSharedBufferGLES20 (int bufferType, size_t bytes, bool forceBufferObject=false);
int UnlockSharedBufferGLES20 (size_t actualBytes = 0, bool forceBufferObject=false);

#define GL_VERTEX_ARRAY		0
#define GL_COLOR_ARRAY		1
#define GL_NORMAL_ARRAY		2
#define GL_TEXTURE_ARRAY0	3
#define GL_TEXTURE_ARRAY1	GL_TEXTURE_ARRAY0 + 1
#define GL_TEXTURE_ARRAY2	GL_TEXTURE_ARRAY0 + 2
#define GL_TEXTURE_ARRAY3	GL_TEXTURE_ARRAY0 + 3
#define GL_TEXTURE_ARRAY4	GL_TEXTURE_ARRAY0 + 4
#define GL_TEXTURE_ARRAY5	GL_TEXTURE_ARRAY0 + 5
#define GL_TEXTURE_ARRAY6	GL_TEXTURE_ARRAY0 + 6
#define GL_TEXTURE_ARRAY7	GL_TEXTURE_ARRAY0 + 7

#if NV_STATE_FILTERING
void filteredBindBufferGLES20(GLenum target, GLuint buffer,bool isImmediate=false);
void filteredVertexAttribPointerGLES20(GLuint  index,  GLint  size,  GLenum  type,  GLboolean  normalized,  GLsizei  stride,  const GLvoid *  pointer);
void StateFiltering_InvalidateVBOCacheGLES20();
#endif

#endif