• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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 gl::IndexRangeCache class which stores information about
8 // ranges of indices.
9 
10 #include "libANGLE/IndexRangeCache.h"
11 
12 #include "common/debug.h"
13 #include "libANGLE/formatutils.h"
14 
15 namespace gl
16 {
17 
IndexRangeCache()18 IndexRangeCache::IndexRangeCache() {}
19 
~IndexRangeCache()20 IndexRangeCache::~IndexRangeCache() {}
21 
addRange(DrawElementsType type,size_t offset,size_t count,bool primitiveRestartEnabled,const IndexRange & range)22 void IndexRangeCache::addRange(DrawElementsType type,
23                                size_t offset,
24                                size_t count,
25                                bool primitiveRestartEnabled,
26                                const IndexRange &range)
27 {
28     mIndexRangeCache[IndexRangeKey(type, offset, count, primitiveRestartEnabled)] = range;
29 }
30 
findRange(DrawElementsType type,size_t offset,size_t count,bool primitiveRestartEnabled,IndexRange * outRange) const31 bool IndexRangeCache::findRange(DrawElementsType type,
32                                 size_t offset,
33                                 size_t count,
34                                 bool primitiveRestartEnabled,
35                                 IndexRange *outRange) const
36 {
37     auto i = mIndexRangeCache.find(IndexRangeKey(type, offset, count, primitiveRestartEnabled));
38     if (i != mIndexRangeCache.end())
39     {
40         if (outRange)
41         {
42             *outRange = i->second;
43         }
44         return true;
45     }
46     else
47     {
48         if (outRange)
49         {
50             *outRange = IndexRange();
51         }
52         return false;
53     }
54 }
55 
invalidateRange(size_t offset,size_t size)56 void IndexRangeCache::invalidateRange(size_t offset, size_t size)
57 {
58     size_t invalidateStart = offset;
59     size_t invalidateEnd   = offset + size;
60 
61     auto i = mIndexRangeCache.begin();
62     while (i != mIndexRangeCache.end())
63     {
64         size_t rangeStart = i->first.offset;
65         size_t rangeEnd =
66             i->first.offset + (GetDrawElementsTypeSize(i->first.type) * i->first.count);
67 
68         if (invalidateEnd < rangeStart || invalidateStart > rangeEnd)
69         {
70             ++i;
71         }
72         else
73         {
74             mIndexRangeCache.erase(i++);
75         }
76     }
77 }
78 
clear()79 void IndexRangeCache::clear()
80 {
81     mIndexRangeCache.clear();
82 }
83 
IndexRangeKey()84 IndexRangeCache::IndexRangeKey::IndexRangeKey()
85     : IndexRangeCache::IndexRangeKey(DrawElementsType::InvalidEnum, 0, 0, false)
86 {}
87 
IndexRangeKey(DrawElementsType type_,size_t offset_,size_t count_,bool primitiveRestartEnabled_)88 IndexRangeCache::IndexRangeKey::IndexRangeKey(DrawElementsType type_,
89                                               size_t offset_,
90                                               size_t count_,
91                                               bool primitiveRestartEnabled_)
92     : type(type_), offset(offset_), count(count_), primitiveRestartEnabled(primitiveRestartEnabled_)
93 {}
94 
operator <(const IndexRangeKey & rhs) const95 bool IndexRangeCache::IndexRangeKey::operator<(const IndexRangeKey &rhs) const
96 {
97     if (type != rhs.type)
98     {
99         return type < rhs.type;
100     }
101     if (offset != rhs.offset)
102     {
103         return offset < rhs.offset;
104     }
105     if (count != rhs.count)
106     {
107         return count < rhs.count;
108     }
109     if (primitiveRestartEnabled != rhs.primitiveRestartEnabled)
110     {
111         return primitiveRestartEnabled;
112     }
113     return false;
114 }
115 
116 }  // namespace gl
117