1//
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
7// geometry/IndexDataManager.h: Defines the IndexDataManager, a class that
8// runs the Buffer translation process for index buffers.
9
10#ifndef LIBGLESV2_GEOMETRY_INDEXDATAMANAGER_H_
11#define LIBGLESV2_GEOMETRY_INDEXDATAMANAGER_H_
12
13#include <vector>
14#include <cstddef>
15
16#define GL_APICALL
17#include <GLES2/gl2.h>
18
19#include "libGLESv2/Context.h"
20
21namespace gl
22{
23
24struct TranslatedIndexData
25{
26    UINT minIndex;
27    UINT maxIndex;
28    UINT startIndex;
29
30    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
62    void *map(UINT requiredSpace, UINT *offset);
63    void reserveSpace(UINT requiredSpace, GLenum type);
64
65  private:
66    UINT mWritePosition;
67};
68
69class StaticIndexBuffer : public IndexBuffer
70{
71  public:
72    explicit StaticIndexBuffer(IDirect3DDevice9 *device);
73    ~StaticIndexBuffer();
74
75    void *map(UINT requiredSpace, UINT *offset);
76    void reserveSpace(UINT requiredSpace, GLenum type);
77
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
90        UINT minIndex;
91        UINT maxIndex;
92        UINT streamOffset;
93    };
94
95    std::vector<IndexRange> mCache;
96};
97
98class IndexDataManager
99{
100  public:
101    IndexDataManager(Context *context, IDirect3DDevice9 *evice);
102    virtual ~IndexDataManager();
103
104    GLenum prepareIndexData(GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices, TranslatedIndexData *translated);
105
106  private:
107    DISALLOW_COPY_AND_ASSIGN(IndexDataManager);
108
109    std::size_t typeSize(GLenum type) const;
110    std::size_t indexSize(D3DFORMAT format) const;
111
112    IDirect3DDevice9 *const mDevice;
113
114    StreamingIndexBuffer *mStreamingBufferShort;
115    StreamingIndexBuffer *mStreamingBufferInt;
116};
117
118}
119
120#endif   // LIBGLESV2_GEOMETRY_INDEXDATAMANAGER_H_
121