blob: c076b9017bae7189d92742a9f3ab95c53296bed1 [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;
31};
32
33class IndexBuffer
34{
35 public:
36 IndexBuffer(IDirect3DDevice9 *device, UINT size, D3DFORMAT format);
37 virtual ~IndexBuffer();
38
39 UINT size() const { return mBufferSize; }
40 virtual void *map(UINT requiredSpace, UINT *offset) = 0;
41 void unmap();
42 virtual void reserveSpace(UINT requiredSpace, GLenum type) = 0;
43
44 IDirect3DIndexBuffer9 *getBuffer() const;
45
46 protected:
47 IDirect3DDevice9 *const mDevice;
48
49 IDirect3DIndexBuffer9 *mIndexBuffer;
50 UINT mBufferSize;
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(IndexBuffer);
54};
55
56class StreamingIndexBuffer : public IndexBuffer
57{
58 public:
59 StreamingIndexBuffer(IDirect3DDevice9 *device, UINT initialSize, D3DFORMAT format);
60 ~StreamingIndexBuffer();
61
daniel@transgaming.comd976b582011-03-29 00:56:20 +000062 virtual void *map(UINT requiredSpace, UINT *offset);
63 virtual void reserveSpace(UINT requiredSpace, GLenum type);
daniel@transgaming.com83921382011-01-08 05:46:00 +000064
65 private:
66 UINT mWritePosition;
67};
68
69class StaticIndexBuffer : public IndexBuffer
70{
71 public:
72 explicit StaticIndexBuffer(IDirect3DDevice9 *device);
73 ~StaticIndexBuffer();
74
daniel@transgaming.comd976b582011-03-29 00:56:20 +000075 virtual void *map(UINT requiredSpace, UINT *offset);
76 virtual void reserveSpace(UINT requiredSpace, GLenum type);
daniel@transgaming.com83921382011-01-08 05:46:00 +000077
78 bool lookupType(GLenum type);
79 UINT lookupRange(intptr_t offset, GLsizei count, UINT *minIndex, UINT *maxIndex); // Returns the offset into the index buffer, or -1 if not found
80 void addRange(intptr_t offset, GLsizei count, UINT minIndex, UINT maxIndex, UINT streamOffset);
81
82 private:
83 GLenum mCacheType;
84
85 struct IndexRange
86 {
87 intptr_t offset;
88 GLsizei count;
89
jbauman@chromium.org43cbe742011-09-01 22:09:40 +000090 bool operator<(const IndexRange& rhs) const
91 {
92 if (offset != rhs.offset)
93 {
94 return offset < rhs.offset;
95 }
96 if (count != rhs.count)
97 {
98 return count < rhs.count;
99 }
100 return false;
101 }
102 };
103
104 struct IndexResult
105 {
daniel@transgaming.com83921382011-01-08 05:46:00 +0000106 UINT minIndex;
107 UINT maxIndex;
108 UINT streamOffset;
109 };
110
jbauman@chromium.org43cbe742011-09-01 22:09:40 +0000111 std::map<IndexRange, IndexResult> mCache;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000112};
113
114class IndexDataManager
115{
116 public:
daniel@transgaming.com83921382011-01-08 05:46:00 +0000117 IndexDataManager(Context *context, IDirect3DDevice9 *evice);
118 virtual ~IndexDataManager();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000119
daniel@transgaming.com83921382011-01-08 05:46:00 +0000120 GLenum prepareIndexData(GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices, TranslatedIndexData *translated);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000121
122 private:
daniel@transgaming.com83921382011-01-08 05:46:00 +0000123 DISALLOW_COPY_AND_ASSIGN(IndexDataManager);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000124
daniel@transgaming.com83921382011-01-08 05:46:00 +0000125 std::size_t typeSize(GLenum type) const;
126 std::size_t indexSize(D3DFORMAT format) const;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000127
daniel@transgaming.com83921382011-01-08 05:46:00 +0000128 IDirect3DDevice9 *const mDevice;
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +0000129
daniel@transgaming.com83921382011-01-08 05:46:00 +0000130 StreamingIndexBuffer *mStreamingBufferShort;
131 StreamingIndexBuffer *mStreamingBufferInt;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000132};
133
134}
135
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +0000136#endif // LIBGLESV2_INDEXDATAMANAGER_H_