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 | |
Olli Etuaho | 3d5f268 | 2015-03-25 17:05:59 +0200 | [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 | { |
Olli Etuaho | 3d5f268 | 2015-03-25 17:05:59 +0200 | [diff] [blame] | 51 | mIndexRangeCache[IndexRange(type, offset, count)] = range; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void IndexRangeCache::invalidateRange(unsigned int offset, unsigned int size) |
| 55 | { |
| 56 | unsigned int invalidateStart = offset; |
| 57 | unsigned int invalidateEnd = offset + size; |
| 58 | |
| 59 | IndexRangeMap::iterator i = mIndexRangeCache.begin(); |
| 60 | while (i != mIndexRangeCache.end()) |
| 61 | { |
Olli Etuaho | 3d5f268 | 2015-03-25 17:05:59 +0200 | [diff] [blame] | 62 | unsigned int rangeStart = i->first.offset; |
| 63 | unsigned int rangeEnd = i->first.offset + (gl::GetTypeInfo(i->first.type).bytes * i->first.count); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 64 | |
| 65 | if (invalidateEnd < rangeStart || invalidateStart > rangeEnd) |
| 66 | { |
| 67 | ++i; |
| 68 | } |
| 69 | else |
| 70 | { |
Jamie Madill | 2bd2a42 | 2015-01-05 13:26:05 -0500 | [diff] [blame] | 71 | mIndexRangeCache.erase(i++); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 76 | bool IndexRangeCache::findRange(GLenum type, unsigned int offset, GLsizei count, |
Olli Etuaho | 3d5f268 | 2015-03-25 17:05:59 +0200 | [diff] [blame] | 77 | RangeUI *outRange) const |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 78 | { |
| 79 | IndexRangeMap::const_iterator i = mIndexRangeCache.find(IndexRange(type, offset, count)); |
| 80 | if (i != mIndexRangeCache.end()) |
| 81 | { |
Olli Etuaho | 3d5f268 | 2015-03-25 17:05:59 +0200 | [diff] [blame] | 82 | if (outRange) *outRange = i->second; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | else |
| 86 | { |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 87 | if (outRange) *outRange = RangeUI(0, 0); |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void IndexRangeCache::clear() |
| 93 | { |
| 94 | mIndexRangeCache.clear(); |
| 95 | } |
| 96 | |
| 97 | IndexRangeCache::IndexRange::IndexRange() |
| 98 | : type(GL_NONE), offset(0), count(0) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | IndexRangeCache::IndexRange::IndexRange(GLenum typ, intptr_t off, GLsizei c) |
Jamie Madill | b3584fb | 2015-04-09 17:34:21 +0000 | [diff] [blame^] | 103 | : type(typ), offset(off), count(c) |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
| 107 | bool IndexRangeCache::IndexRange::operator<(const IndexRange& rhs) const |
| 108 | { |
Jamie Madill | 3897cd1 | 2015-01-05 13:15:15 -0500 | [diff] [blame] | 109 | if (type != rhs.type) return type < rhs.type; |
| 110 | if (offset != rhs.offset) return offset < rhs.offset; |
| 111 | return count < rhs.count; |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 114 | } |