blob: 84017b69319637ef503fd2eee6772cc93e50dc40 [file] [log] [blame]
Geoff Langf23eb282013-07-22 10:52:19 -04001//
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 Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/renderer/IndexRangeCache.h"
11#include "libANGLE/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012
Geoff Langf23eb282013-07-22 10:52:19 -040013#include "common/debug.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040014
Geoff Langf23eb282013-07-22 10:52:19 -040015namespace rx
16{
17
Jamie Madill2b976812014-08-25 15:47:49 -040018template <class IndexType>
19static 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
33RangeUI 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 Etuaho3d5f2682015-03-25 17:05:59 +020049void IndexRangeCache::addRange(GLenum type, unsigned int offset, GLsizei count, const RangeUI &range)
Geoff Langf23eb282013-07-22 10:52:19 -040050{
Olli Etuaho3d5f2682015-03-25 17:05:59 +020051 mIndexRangeCache[IndexRange(type, offset, count)] = range;
Geoff Langf23eb282013-07-22 10:52:19 -040052}
53
54void 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 Etuaho3d5f2682015-03-25 17:05:59 +020062 unsigned int rangeStart = i->first.offset;
63 unsigned int rangeEnd = i->first.offset + (gl::GetTypeInfo(i->first.type).bytes * i->first.count);
Geoff Langf23eb282013-07-22 10:52:19 -040064
65 if (invalidateEnd < rangeStart || invalidateStart > rangeEnd)
66 {
67 ++i;
68 }
69 else
70 {
Jamie Madill2bd2a422015-01-05 13:26:05 -050071 mIndexRangeCache.erase(i++);
Geoff Langf23eb282013-07-22 10:52:19 -040072 }
73 }
74}
75
Jamie Madill39b43462014-08-18 16:39:50 -040076bool IndexRangeCache::findRange(GLenum type, unsigned int offset, GLsizei count,
Olli Etuaho3d5f2682015-03-25 17:05:59 +020077 RangeUI *outRange) const
Geoff Langf23eb282013-07-22 10:52:19 -040078{
79 IndexRangeMap::const_iterator i = mIndexRangeCache.find(IndexRange(type, offset, count));
80 if (i != mIndexRangeCache.end())
81 {
Olli Etuaho3d5f2682015-03-25 17:05:59 +020082 if (outRange) *outRange = i->second;
Geoff Langf23eb282013-07-22 10:52:19 -040083 return true;
84 }
85 else
86 {
Jamie Madill39b43462014-08-18 16:39:50 -040087 if (outRange) *outRange = RangeUI(0, 0);
Geoff Langf23eb282013-07-22 10:52:19 -040088 return false;
89 }
90}
91
92void IndexRangeCache::clear()
93{
94 mIndexRangeCache.clear();
95}
96
97IndexRangeCache::IndexRange::IndexRange()
98 : type(GL_NONE), offset(0), count(0)
99{
100}
101
102IndexRangeCache::IndexRange::IndexRange(GLenum typ, intptr_t off, GLsizei c)
Minmin Gong3b26e232015-04-07 18:31:54 -0700103 : type(typ), offset(static_cast<unsigned int>(off)), count(c)
Geoff Langf23eb282013-07-22 10:52:19 -0400104{
105}
106
107bool IndexRangeCache::IndexRange::operator<(const IndexRange& rhs) const
108{
Jamie Madill3897cd12015-01-05 13:15:15 -0500109 if (type != rhs.type) return type < rhs.type;
110 if (offset != rhs.offset) return offset < rhs.offset;
111 return count < rhs.count;
Geoff Langf23eb282013-07-22 10:52:19 -0400112}
113
Geoff Langf23eb282013-07-22 10:52:19 -0400114}