Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 1 | // |
| 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. |
| 5 | // |
| 6 | |
| 7 | // IndexRangeCache.cpp: Defines the rx::IndexRangeCache class which stores information about |
| 8 | // ranges of indices. |
| 9 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/renderer/IndexRangeCache.h" |
| 11 | #include "libANGLE/formatutils.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 12 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 13 | #include "common/debug.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 14 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 15 | namespace rx |
| 16 | { |
| 17 | |
Jamie Madill | 2b97681 | 2014-08-25 15:47:49 -0400 | [diff] [blame] | 18 | template <class IndexType> |
| 19 | static RangeUI ComputeTypedRange(const IndexType *indices, GLsizei count) |
| 20 | { |
| 21 | unsigned int minIndex = indices[0]; |
| 22 | unsigned int maxIndex = indices[0]; |
| 23 | |
| 24 | for (GLsizei i = 1; i < count; i++) |
| 25 | { |
| 26 | if (minIndex > indices[i]) minIndex = indices[i]; |
| 27 | if (maxIndex < indices[i]) maxIndex = indices[i]; |
| 28 | } |
| 29 | |
| 30 | return RangeUI(minIndex, maxIndex); |
| 31 | } |
| 32 | |
| 33 | RangeUI IndexRangeCache::ComputeRange(GLenum type, const GLvoid *indices, GLsizei count) |
| 34 | { |
| 35 | switch (type) |
| 36 | { |
| 37 | case GL_UNSIGNED_BYTE: |
| 38 | return ComputeTypedRange(static_cast<const GLubyte*>(indices), count); |
| 39 | case GL_UNSIGNED_INT: |
| 40 | return ComputeTypedRange(static_cast<const GLuint*>(indices), count); |
| 41 | case GL_UNSIGNED_SHORT: |
| 42 | return ComputeTypedRange(static_cast<const GLushort*>(indices), count); |
| 43 | default: |
| 44 | UNREACHABLE(); |
| 45 | return RangeUI(); |
| 46 | } |
| 47 | } |
| 48 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 49 | void IndexRangeCache::addRange(GLenum type, unsigned int offset, GLsizei count, const RangeUI &range, |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 50 | unsigned int streamOffset) |
| 51 | { |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 52 | mIndexRangeCache[IndexRange(type, offset, count)] = IndexBounds(range, streamOffset); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void IndexRangeCache::invalidateRange(unsigned int offset, unsigned int size) |
| 56 | { |
| 57 | unsigned int invalidateStart = offset; |
| 58 | unsigned int invalidateEnd = offset + size; |
| 59 | |
| 60 | IndexRangeMap::iterator i = mIndexRangeCache.begin(); |
| 61 | while (i != mIndexRangeCache.end()) |
| 62 | { |
| 63 | unsigned int rangeStart = i->second.streamOffset; |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 64 | unsigned int rangeEnd = i->second.streamOffset + (gl::GetTypeInfo(i->first.type).bytes * i->first.count); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 65 | |
| 66 | if (invalidateEnd < rangeStart || invalidateStart > rangeEnd) |
| 67 | { |
| 68 | ++i; |
| 69 | } |
| 70 | else |
| 71 | { |
Jamie Madill | 2bd2a42 | 2015-01-05 13:26:05 -0500 | [diff] [blame] | 72 | mIndexRangeCache.erase(i++); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 77 | bool IndexRangeCache::findRange(GLenum type, unsigned int offset, GLsizei count, |
| 78 | RangeUI *outRange, unsigned int *outStreamOffset) const |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 79 | { |
| 80 | IndexRangeMap::const_iterator i = mIndexRangeCache.find(IndexRange(type, offset, count)); |
| 81 | if (i != mIndexRangeCache.end()) |
| 82 | { |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 83 | if (outRange) *outRange = i->second.range; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 84 | if (outStreamOffset) *outStreamOffset = i->second.streamOffset; |
| 85 | return true; |
| 86 | } |
| 87 | else |
| 88 | { |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 89 | if (outRange) *outRange = RangeUI(0, 0); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 90 | if (outStreamOffset) *outStreamOffset = 0; |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void IndexRangeCache::clear() |
| 96 | { |
| 97 | mIndexRangeCache.clear(); |
| 98 | } |
| 99 | |
| 100 | IndexRangeCache::IndexRange::IndexRange() |
| 101 | : type(GL_NONE), offset(0), count(0) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | IndexRangeCache::IndexRange::IndexRange(GLenum typ, intptr_t off, GLsizei c) |
| 106 | : type(typ), offset(off), count(c) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | bool IndexRangeCache::IndexRange::operator<(const IndexRange& rhs) const |
| 111 | { |
Jamie Madill | 3897cd1 | 2015-01-05 13:15:15 -0500 | [diff] [blame^] | 112 | if (type != rhs.type) return type < rhs.type; |
| 113 | if (offset != rhs.offset) return offset < rhs.offset; |
| 114 | return count < rhs.count; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | IndexRangeCache::IndexBounds::IndexBounds() |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 118 | : range(0, 0), |
| 119 | streamOffset(0) |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 123 | IndexRangeCache::IndexBounds::IndexBounds(const RangeUI &rangeIn, unsigned int offset) |
| 124 | : range(rangeIn), streamOffset(offset) |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 125 | { |
| 126 | } |
| 127 | |
| 128 | } |