blob: 04db4be62593233df04852653653f2a0d4179e02 [file] [log] [blame]
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001//
2// Copyright (c) 2002-2010 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
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +00007// IndexDataManager.h: Defines the IndexDataManager, a class that
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00008// runs the Buffer translation process for index buffers.
9
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000010#ifndef LIBGLESV2_INDEXDATAMANAGER_H_
11#define LIBGLESV2_INDEXDATAMANAGER_H_
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000012
daniel@transgaming.com83921382011-01-08 05:46:00 +000013#include <vector>
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000014#include <cstddef>
15
16#define GL_APICALL
17#include <GLES2/gl2.h>
18
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000019#include "libGLESv2/Context.h"
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000020
21namespace gl
22{
23
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000024struct TranslatedIndexData
25{
daniel@transgaming.com83921382011-01-08 05:46:00 +000026 UINT minIndex;
27 UINT maxIndex;
28 UINT startIndex;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000029
daniel@transgaming.com83921382011-01-08 05:46:00 +000030 IDirect3DIndexBuffer9 *indexBuffer;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000031 unsigned int serial;
daniel@transgaming.com83921382011-01-08 05:46:00 +000032};
33
34class IndexBuffer
35{
36 public:
37 IndexBuffer(IDirect3DDevice9 *device, UINT size, D3DFORMAT format);
38 virtual ~IndexBuffer();
39
40 UINT size() const { return mBufferSize; }
41 virtual void *map(UINT requiredSpace, UINT *offset) = 0;
42 void unmap();
43 virtual void reserveSpace(UINT requiredSpace, GLenum type) = 0;
44
45 IDirect3DIndexBuffer9 *getBuffer() const;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000046 unsigned int getSerial() const;
daniel@transgaming.com83921382011-01-08 05:46:00 +000047
48 protected:
49 IDirect3DDevice9 *const mDevice;
50
51 IDirect3DIndexBuffer9 *mIndexBuffer;
52 UINT mBufferSize;
53
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +000054 unsigned int mSerial;
55 static unsigned int issueSerial();
56 static unsigned int mCurrentSerial;
57
daniel@transgaming.com83921382011-01-08 05:46:00 +000058 private:
59 DISALLOW_COPY_AND_ASSIGN(IndexBuffer);
60};
61
62class StreamingIndexBuffer : public IndexBuffer
63{
64 public:
65 StreamingIndexBuffer(IDirect3DDevice9 *device, UINT initialSize, D3DFORMAT format);
66 ~StreamingIndexBuffer();
67
daniel@transgaming.comd976b582011-03-29 00:56:20 +000068 virtual void *map(UINT requiredSpace, UINT *offset);
69 virtual void reserveSpace(UINT requiredSpace, GLenum type);
daniel@transgaming.com83921382011-01-08 05:46:00 +000070
71 private:
72 UINT mWritePosition;
73};
74
75class StaticIndexBuffer : public IndexBuffer
76{
77 public:
78 explicit StaticIndexBuffer(IDirect3DDevice9 *device);
79 ~StaticIndexBuffer();
80
daniel@transgaming.comd976b582011-03-29 00:56:20 +000081 virtual void *map(UINT requiredSpace, UINT *offset);
82 virtual void reserveSpace(UINT requiredSpace, GLenum type);
daniel@transgaming.com83921382011-01-08 05:46:00 +000083
84 bool lookupType(GLenum type);
85 UINT lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex); // Returns the offset into the index buffer, or -1 if not found
86 void addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset);
87
88 private:
89 GLenum mCacheType;
90
91 struct IndexRange
92 {
93 intptr_t offset;
94 GLsizei count;
95
jbauman@chromium.org43cbe742011-09-01 22:09:40 +000096 bool operator<(const IndexRange& rhs) const
97 {
98 if (offset != rhs.offset)
99 {
100 return offset < rhs.offset;
101 }
102 if (count != rhs.count)
103 {
104 return count < rhs.count;
105 }
106 return false;
107 }
108 };
109
110 struct IndexResult
111 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000112 UINT minIndex;
113 UINT maxIndex;
114 UINT streamOffset;
115 };
116
jbauman@chromium.org43cbe742011-09-01 22:09:40 +0000117 std::map<IndexRange, IndexResult> mCache;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000118};
119
120class IndexDataManager
121{
122 public:
daniel@transgaming.com83921382011-01-08 05:46:00 +0000123 IndexDataManager(Context *context, IDirect3DDevice9 *evice);
124 virtual ~IndexDataManager();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000125
daniel@transgaming.com83921382011-01-08 05:46:00 +0000126 GLenum prepareIndexData(GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices, TranslatedIndexData *translated);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000127
128 private:
daniel@transgaming.com83921382011-01-08 05:46:00 +0000129 DISALLOW_COPY_AND_ASSIGN(IndexDataManager);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000130
daniel@transgaming.com83921382011-01-08 05:46:00 +0000131 std::size_t typeSize(GLenum type) const;
132 std::size_t indexSize(D3DFORMAT format) const;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000133
daniel@transgaming.com83921382011-01-08 05:46:00 +0000134 IDirect3DDevice9 *const mDevice;
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000135
daniel@transgaming.com83921382011-01-08 05:46:00 +0000136 StreamingIndexBuffer *mStreamingBufferShort;
137 StreamingIndexBuffer *mStreamingBufferInt;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000138};
139
140}
141
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +0000142#endif // LIBGLESV2_INDEXDATAMANAGER_H_