Hash :
41d8dd85
Author :
Date :
2010-05-12T03:45:03
Add better error checking on buffer updates TRAC #12186 Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch Author: Andrew Lewycky git-svn-id: https://angleproject.googlecode.com/svn/trunk@266 736b8ea6-26fd-11df-bfd4-992fa37f6226
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
//
// 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/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())
{
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;
}
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);
translated->indices = output;
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;
}
}