2 // Copyright (c) 2013 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.
7 // IndexRangeCache.cpp: Defines the rx::IndexRangeCache class which stores information about
10 #include "libGLESv2/renderer/IndexRangeCache.h"
11 #include "libGLESv2/formatutils.h"
13 #include "common/debug.h"
20 template <class IndexType>
21 static RangeUI ComputeTypedRange(const IndexType *indices, GLsizei count)
23 unsigned int minIndex = indices[0];
24 unsigned int maxIndex = indices[0];
26 for (GLsizei i = 1; i < count; i++)
28 if (minIndex > indices[i]) minIndex = indices[i];
29 if (maxIndex < indices[i]) maxIndex = indices[i];
32 return RangeUI(minIndex, maxIndex);
35 RangeUI IndexRangeCache::ComputeRange(GLenum type, const GLvoid *indices, GLsizei count)
39 case GL_UNSIGNED_BYTE:
40 return ComputeTypedRange(static_cast<const GLubyte*>(indices), count);
42 return ComputeTypedRange(static_cast<const GLuint*>(indices), count);
43 case GL_UNSIGNED_SHORT:
44 return ComputeTypedRange(static_cast<const GLushort*>(indices), count);
51 void IndexRangeCache::addRange(GLenum type, unsigned int offset, GLsizei count, const RangeUI &range,
52 unsigned int streamOffset)
54 mIndexRangeCache[IndexRange(type, offset, count)] = IndexBounds(range, streamOffset);
57 void IndexRangeCache::invalidateRange(unsigned int offset, unsigned int size)
59 unsigned int invalidateStart = offset;
60 unsigned int invalidateEnd = offset + size;
62 IndexRangeMap::iterator i = mIndexRangeCache.begin();
63 while (i != mIndexRangeCache.end())
65 unsigned int rangeStart = i->second.streamOffset;
66 unsigned int rangeEnd = i->second.streamOffset + (gl::GetTypeInfo(i->first.type).bytes * i->first.count);
68 if (invalidateEnd < rangeStart || invalidateStart > rangeEnd)
74 i = mIndexRangeCache.erase(i);
79 bool IndexRangeCache::findRange(GLenum type, unsigned int offset, GLsizei count,
80 RangeUI *outRange, unsigned int *outStreamOffset) const
82 IndexRangeMap::const_iterator i = mIndexRangeCache.find(IndexRange(type, offset, count));
83 if (i != mIndexRangeCache.end())
85 if (outRange) *outRange = i->second.range;
86 if (outStreamOffset) *outStreamOffset = i->second.streamOffset;
91 if (outRange) *outRange = RangeUI(0, 0);
92 if (outStreamOffset) *outStreamOffset = 0;
97 void IndexRangeCache::clear()
99 mIndexRangeCache.clear();
102 IndexRangeCache::IndexRange::IndexRange()
103 : type(GL_NONE), offset(0), count(0)
107 IndexRangeCache::IndexRange::IndexRange(GLenum typ, intptr_t off, GLsizei c)
108 : type(typ), offset(off), count(c)
112 bool IndexRangeCache::IndexRange::operator<(const IndexRange& rhs) const
114 return std::make_tuple(type, offset, count) < std::make_tuple(rhs.type, rhs.offset, rhs.count);
117 IndexRangeCache::IndexBounds::IndexBounds()
123 IndexRangeCache::IndexBounds::IndexBounds(const RangeUI &rangeIn, unsigned int offset)
124 : range(rangeIn), streamOffset(offset)