IndexDataManager.cpp 7.02 KB
Newer Older
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// geometry/IndexDataManager.cpp: Defines the IndexDataManager, a class that
// runs the Buffer translation process for index buffers.

#include "libGLESv2/geometry/IndexDataManager.h"

#include "common/debug.h"

#include "libGLESv2/Buffer.h"
#include "libGLESv2/mathutil.h"
#include "libGLESv2/geometry/backend.h"

namespace
{
    enum { INITIAL_INDEX_BUFFER_SIZE = 4096 * sizeof(GLuint) };
}

namespace gl
{

IndexDataManager::IndexDataManager(Context *context, BufferBackEnd *backend)
  : mContext(context), mBackend(backend), mIntIndicesSupported(backend->supportIntIndices())
{
    mCountingBuffer = NULL;
    mCountingBufferSize = 0;

    mLineLoopBuffer = NULL;

    mStreamBufferShort = mBackend->createIndexBuffer(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT);

    if (mIntIndicesSupported)
    {
        mStreamBufferInt = mBackend->createIndexBuffer(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT);
    }
    else
    {
        mStreamBufferInt = NULL;
    }
}

IndexDataManager::~IndexDataManager()
{
    delete mStreamBufferShort;
    delete mStreamBufferInt;
    delete mCountingBuffer;
    delete mLineLoopBuffer;
}

namespace
{

template <class InputIndexType, class OutputIndexType>
void copyIndices(const InputIndexType *in, GLsizei count, OutputIndexType *out, GLuint *minIndex, GLuint *maxIndex)
{
    InputIndexType first = *in;
    GLuint minIndexSoFar = first;
    GLuint maxIndexSoFar = first;

    for (GLsizei i = 0; i < count; i++)
    {
        if (minIndexSoFar > *in) minIndexSoFar = *in;
        if (maxIndexSoFar < *in) maxIndexSoFar = *in;

        *out++ = *in++;
    }

    // It might be a line loop, so copy the loop index.
    *out = first;

    *minIndex = minIndexSoFar;
    *maxIndex = maxIndexSoFar;
}

}

GLenum IndexDataManager::preRenderValidate(GLenum mode, GLenum type, GLsizei count, Buffer *arrayElementBuffer, const void *indices, TranslatedIndexData *translated)
{
    ASSERT(type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT);
    ASSERT(count > 0);

    if (arrayElementBuffer != NULL)
    {
        GLsizei offset = reinterpret_cast<GLsizei>(indices);

        if (typeSize(type) * count + offset > static_cast<std::size_t>(arrayElementBuffer->size()))
        {
            return GL_INVALID_OPERATION;
        }

        indices = static_cast<const GLubyte*>(arrayElementBuffer->data()) + offset;
    }

    translated->count = count;

    std::size_t requiredSpace = spaceRequired(type, count);

    TranslatedIndexBuffer *streamIb = prepareIndexBuffer(type, requiredSpace);

    size_t offset;
    void *output = streamIb->map(requiredSpace, &offset);

    translated->buffer = streamIb;
    translated->offset = offset;
    translated->indexSize = indexSize(type);

    if (type == GL_UNSIGNED_BYTE)
    {
        const GLubyte *in = static_cast<const GLubyte*>(indices);
        GLushort *out = static_cast<GLushort*>(output);

        copyIndices(in, count, out, &translated->minIndex, &translated->maxIndex);
    }
    else if (type == GL_UNSIGNED_INT)
    {
        const GLuint *in = static_cast<const GLuint*>(indices);

        if (mIntIndicesSupported)
        {
            GLuint *out = static_cast<GLuint*>(output);

            copyIndices(in, count, out, &translated->minIndex, &translated->maxIndex);
        }
        else
        {
            // When 32-bit indices are unsupported, fake them by truncating to 16-bit.

            GLushort *out = static_cast<GLushort*>(output);

            copyIndices(in, count, out, &translated->minIndex, &translated->maxIndex);
        }
    }
    else
    {
        const GLushort *in = static_cast<const GLushort*>(indices);
        GLushort *out = static_cast<GLushort*>(output);

        copyIndices(in, count, out, &translated->minIndex, &translated->maxIndex);
    }

    streamIb->unmap();

    return GL_NO_ERROR;
}

std::size_t IndexDataManager::indexSize(GLenum type) const
{
    return (type == GL_UNSIGNED_INT && mIntIndicesSupported) ? sizeof(GLuint) : sizeof(GLushort);
}

std::size_t IndexDataManager::typeSize(GLenum type) const
{
    switch (type)
    {
      case GL_UNSIGNED_INT: return sizeof(GLuint);
      case GL_UNSIGNED_SHORT: return sizeof(GLushort);
      default: UNREACHABLE();
      case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
    }
}

std::size_t IndexDataManager::spaceRequired(GLenum type, GLsizei count) const
{
    return (count + 1) * indexSize(type); // +1 because we always leave an extra for line loops
}

TranslatedIndexBuffer *IndexDataManager::prepareIndexBuffer(GLenum type, std::size_t requiredSpace)
{
    bool use32 = (type == GL_UNSIGNED_INT && mIntIndicesSupported);

    TranslatedIndexBuffer *streamIb = use32 ? mStreamBufferInt : mStreamBufferShort;

    if (requiredSpace > streamIb->size())
    {
        std::size_t newSize = std::max(requiredSpace, 2 * streamIb->size());

        TranslatedIndexBuffer *newStreamBuffer = mBackend->createIndexBuffer(newSize, use32 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT);

        delete streamIb;

        streamIb = newStreamBuffer;

        if (use32)
        {
            mStreamBufferInt = streamIb;
        }
        else
        {
            mStreamBufferShort = streamIb;
        }
    }

    streamIb->reserveSpace(requiredSpace);

    return streamIb;
}

GLenum IndexDataManager::preRenderValidateUnindexed(GLenum mode, GLsizei count, TranslatedIndexData *indexInfo)
{
    if (count >= 65535) return GL_OUT_OF_MEMORY;

    if (mode == GL_LINE_LOOP)
    {
        // For line loops, create a single-use buffer that runs 0 - count-1, 0.
        delete mLineLoopBuffer;
        mLineLoopBuffer = mBackend->createIndexBuffer((count+1) * sizeof(unsigned short), GL_UNSIGNED_SHORT);

        unsigned short *indices = static_cast<unsigned short *>(mLineLoopBuffer->map());

        for (int i = 0; i < count; i++)
        {
            indices[i] = i;
        }

        indices[count] = 0;

        mLineLoopBuffer->unmap();

        indexInfo->buffer = mLineLoopBuffer;
        indexInfo->count = count + 1;
        indexInfo->maxIndex = count - 1;
    }
    else if (mCountingBufferSize < count)
    {
        mCountingBufferSize = std::max(static_cast<GLsizei>(ceilPow2(count)), mCountingBufferSize*2);

        delete mCountingBuffer;
        mCountingBuffer = mBackend->createIndexBuffer(count * sizeof(unsigned short), GL_UNSIGNED_SHORT);

        unsigned short *indices = static_cast<unsigned short *>(mCountingBuffer->map());

        for (int i = 0; i < count; i++)
        {
            indices[i] = i;
        }

        mCountingBuffer->unmap();

        indexInfo->buffer = mCountingBuffer;
        indexInfo->count = count;
        indexInfo->maxIndex = count - 1;
    }
    else
    {
        indexInfo->buffer = mCountingBuffer;
        indexInfo->count = count;
        indexInfo->maxIndex = count - 1;
    }

    indexInfo->indexSize = sizeof(unsigned short);
    indexInfo->minIndex = 0;
    indexInfo->offset = 0;

    return GL_NO_ERROR;
}

}