blob: e5366f3d88e41042dc674b8bbc2d38444cfb4974 [file] [log] [blame]
Geoff Langf23eb282013-07-22 10:52:19 -04001#include "precompiled.h"
2//
3// Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// IndexRangeCache.cpp: Defines the rx::IndexRangeCache class which stores information about
9// ranges of indices.
10
11#include "libGLESv2/renderer/IndexRangeCache.h"
12#include "libGLESv2/formatutils.h"
13#include "common/debug.h"
14#include <tuple>
15
16namespace rx
17{
18
Jamie Madill2b976812014-08-25 15:47:49 -040019template <class IndexType>
20static RangeUI ComputeTypedRange(const IndexType *indices, GLsizei count)
21{
22 unsigned int minIndex = indices[0];
23 unsigned int maxIndex = indices[0];
24
25 for (GLsizei i = 1; i < count; i++)
26 {
27 if (minIndex > indices[i]) minIndex = indices[i];
28 if (maxIndex < indices[i]) maxIndex = indices[i];
29 }
30
31 return RangeUI(minIndex, maxIndex);
32}
33
34RangeUI IndexRangeCache::ComputeRange(GLenum type, const GLvoid *indices, GLsizei count)
35{
36 switch (type)
37 {
38 case GL_UNSIGNED_BYTE:
39 return ComputeTypedRange(static_cast<const GLubyte*>(indices), count);
40 case GL_UNSIGNED_INT:
41 return ComputeTypedRange(static_cast<const GLuint*>(indices), count);
42 case GL_UNSIGNED_SHORT:
43 return ComputeTypedRange(static_cast<const GLushort*>(indices), count);
44 default:
45 UNREACHABLE();
46 return RangeUI();
47 }
48}
49
Jamie Madill39b43462014-08-18 16:39:50 -040050void IndexRangeCache::addRange(GLenum type, unsigned int offset, GLsizei count, const RangeUI &range,
Geoff Langf23eb282013-07-22 10:52:19 -040051 unsigned int streamOffset)
52{
Jamie Madill39b43462014-08-18 16:39:50 -040053 mIndexRangeCache[IndexRange(type, offset, count)] = IndexBounds(range, streamOffset);
Geoff Langf23eb282013-07-22 10:52:19 -040054}
55
56void IndexRangeCache::invalidateRange(unsigned int offset, unsigned int size)
57{
58 unsigned int invalidateStart = offset;
59 unsigned int invalidateEnd = offset + size;
60
61 IndexRangeMap::iterator i = mIndexRangeCache.begin();
62 while (i != mIndexRangeCache.end())
63 {
64 unsigned int rangeStart = i->second.streamOffset;
Geoff Lang5d601382014-07-22 15:14:06 -040065 unsigned int rangeEnd = i->second.streamOffset + (gl::GetTypeInfo(i->first.type).bytes * i->first.count);
Geoff Langf23eb282013-07-22 10:52:19 -040066
67 if (invalidateEnd < rangeStart || invalidateStart > rangeEnd)
68 {
69 ++i;
70 }
71 else
72 {
73 i = mIndexRangeCache.erase(i);
74 }
75 }
76}
77
Jamie Madill39b43462014-08-18 16:39:50 -040078bool IndexRangeCache::findRange(GLenum type, unsigned int offset, GLsizei count,
79 RangeUI *outRange, unsigned int *outStreamOffset) const
Geoff Langf23eb282013-07-22 10:52:19 -040080{
81 IndexRangeMap::const_iterator i = mIndexRangeCache.find(IndexRange(type, offset, count));
82 if (i != mIndexRangeCache.end())
83 {
Jamie Madill39b43462014-08-18 16:39:50 -040084 if (outRange) *outRange = i->second.range;
Geoff Langf23eb282013-07-22 10:52:19 -040085 if (outStreamOffset) *outStreamOffset = i->second.streamOffset;
86 return true;
87 }
88 else
89 {
Jamie Madill39b43462014-08-18 16:39:50 -040090 if (outRange) *outRange = RangeUI(0, 0);
Geoff Langf23eb282013-07-22 10:52:19 -040091 if (outStreamOffset) *outStreamOffset = 0;
92 return false;
93 }
94}
95
96void IndexRangeCache::clear()
97{
98 mIndexRangeCache.clear();
99}
100
101IndexRangeCache::IndexRange::IndexRange()
102 : type(GL_NONE), offset(0), count(0)
103{
104}
105
106IndexRangeCache::IndexRange::IndexRange(GLenum typ, intptr_t off, GLsizei c)
107 : type(typ), offset(off), count(c)
108{
109}
110
111bool IndexRangeCache::IndexRange::operator<(const IndexRange& rhs) const
112{
113 return std::make_tuple(type, offset, count) < std::make_tuple(rhs.type, rhs.offset, rhs.count);
114}
115
116IndexRangeCache::IndexBounds::IndexBounds()
Jamie Madill39b43462014-08-18 16:39:50 -0400117 : range(0, 0),
118 streamOffset(0)
Geoff Langf23eb282013-07-22 10:52:19 -0400119{
120}
121
Jamie Madill39b43462014-08-18 16:39:50 -0400122IndexRangeCache::IndexBounds::IndexBounds(const RangeUI &rangeIn, unsigned int offset)
123 : range(rangeIn), streamOffset(offset)
Geoff Langf23eb282013-07-22 10:52:19 -0400124{
125}
126
127}