/src/mozilla-central/gfx/angle/checkout/src/common/PackedEnums.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2017 The ANGLE Project Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | // |
5 | | // PackedGLEnums_autogen.h: |
6 | | // Declares ANGLE-specific enums classes for GLEnum and functions operating |
7 | | // on them. |
8 | | |
9 | | #ifndef COMMON_PACKEDGLENUMS_H_ |
10 | | #define COMMON_PACKEDGLENUMS_H_ |
11 | | |
12 | | #include "common/PackedEGLEnums_autogen.h" |
13 | | #include "common/PackedGLEnums_autogen.h" |
14 | | |
15 | | #include <array> |
16 | | #include <bitset> |
17 | | #include <cstddef> |
18 | | |
19 | | #include <EGL/egl.h> |
20 | | |
21 | | #include "common/bitset_utils.h" |
22 | | |
23 | | namespace angle |
24 | | { |
25 | | |
26 | | // Return the number of elements of a packed enum, including the InvalidEnum element. |
27 | | template <typename E> |
28 | | constexpr size_t EnumSize() |
29 | 0 | { |
30 | 0 | using UnderlyingType = typename std::underlying_type<E>::type; |
31 | 0 | return static_cast<UnderlyingType>(E::EnumCount); |
32 | 0 | } |
33 | | |
34 | | // Implementation of AllEnums which allows iterating over all the possible values for a packed enums |
35 | | // like so: |
36 | | // for (auto value : AllEnums<MyPackedEnum>()) { |
37 | | // // Do something with the enum. |
38 | | // } |
39 | | |
40 | | template <typename E> |
41 | | class EnumIterator final |
42 | | { |
43 | | private: |
44 | | using UnderlyingType = typename std::underlying_type<E>::type; |
45 | | |
46 | | public: |
47 | | EnumIterator(E value) : mValue(static_cast<UnderlyingType>(value)) {} |
48 | | EnumIterator &operator++() |
49 | | { |
50 | | mValue++; |
51 | | return *this; |
52 | | } |
53 | | bool operator==(const EnumIterator &other) const { return mValue == other.mValue; } |
54 | | bool operator!=(const EnumIterator &other) const { return mValue != other.mValue; } |
55 | | E operator*() const { return static_cast<E>(mValue); } |
56 | | |
57 | | private: |
58 | | UnderlyingType mValue; |
59 | | }; |
60 | | |
61 | | template <typename E> |
62 | | struct AllEnums |
63 | | { |
64 | | EnumIterator<E> begin() const { return {static_cast<E>(0)}; } |
65 | | EnumIterator<E> end() const { return {E::InvalidEnum}; } |
66 | | }; |
67 | | |
68 | | // PackedEnumMap<E, T> is like an std::array<T, E::EnumCount> but is indexed with enum values. It |
69 | | // implements all of the std::array interface except with enum values instead of indices. |
70 | | template <typename E, typename T> |
71 | | class PackedEnumMap |
72 | | { |
73 | | using UnderlyingType = typename std::underlying_type<E>::type; |
74 | | using Storage = std::array<T, EnumSize<E>()>; |
75 | | |
76 | | public: |
77 | | // types: |
78 | | using value_type = T; |
79 | | using pointer = T *; |
80 | | using const_pointer = const T *; |
81 | | using reference = T &; |
82 | | using const_reference = const T &; |
83 | | |
84 | | using size_type = size_t; |
85 | | using difference_type = ptrdiff_t; |
86 | | |
87 | | using iterator = typename Storage::iterator; |
88 | | using const_iterator = typename Storage::const_iterator; |
89 | | using reverse_iterator = std::reverse_iterator<iterator>; |
90 | | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
91 | | |
92 | | // No explicit construct/copy/destroy for aggregate type |
93 | | void fill(const T &u) { mPrivateData.fill(u); } |
94 | | void swap(PackedEnumMap<E, T> &a) noexcept { mPrivateData.swap(a.mPrivateData); } |
95 | | |
96 | | // iterators: |
97 | | iterator begin() noexcept { return mPrivateData.begin(); } |
98 | | const_iterator begin() const noexcept { return mPrivateData.begin(); } |
99 | | iterator end() noexcept { return mPrivateData.end(); } |
100 | | const_iterator end() const noexcept { return mPrivateData.end(); } |
101 | | |
102 | | reverse_iterator rbegin() noexcept { return mPrivateData.rbegin(); } |
103 | | const_reverse_iterator rbegin() const noexcept { return mPrivateData.rbegin(); } |
104 | | reverse_iterator rend() noexcept { return mPrivateData.rend(); } |
105 | | const_reverse_iterator rend() const noexcept { return mPrivateData.rend(); } |
106 | | |
107 | | // capacity: |
108 | | constexpr size_type size() const noexcept { return mPrivateData.size(); } |
109 | | constexpr size_type max_size() const noexcept { return mPrivateData.max_size(); } |
110 | | constexpr bool empty() const noexcept { return mPrivateData.empty(); } |
111 | | |
112 | | // element access: |
113 | | reference operator[](E n) { return mPrivateData[static_cast<UnderlyingType>(n)]; } |
114 | | const_reference operator[](E n) const { return mPrivateData[static_cast<UnderlyingType>(n)]; } |
115 | | const_reference at(E n) const { return mPrivateData.at(static_cast<UnderlyingType>(n)); } |
116 | | reference at(E n) { return mPrivateData.at(static_cast<UnderlyingType>(n)); } |
117 | | |
118 | | reference front() { return mPrivateData.front(); } |
119 | | const_reference front() const { return mPrivateData.front(); } |
120 | | reference back() { return mPrivateData.back(); } |
121 | | const_reference back() const { return mPrivateData.back(); } |
122 | | |
123 | | T *data() noexcept { return mPrivateData.data(); } |
124 | | const T *data() const noexcept { return mPrivateData.data(); } |
125 | | |
126 | | // Do not access this variable directly. It unfortunately must be public to use aggregate init. |
127 | | /* private: */ Storage mPrivateData; |
128 | | }; |
129 | | |
130 | | // PackedEnumBitSetE> is like an std::bitset<E::EnumCount> but is indexed with enum values. It |
131 | | // implements the std::bitset interface except with enum values instead of indices. |
132 | | template <typename E, typename DataT = uint32_t> |
133 | | using PackedEnumBitSet = BitSetT<EnumSize<E>(), DataT, E>; |
134 | | |
135 | | } // namespace angle |
136 | | |
137 | | namespace gl |
138 | | { |
139 | | |
140 | | TextureType TextureTargetToType(TextureTarget target); |
141 | | TextureTarget NonCubeTextureTypeToTarget(TextureType type); |
142 | | |
143 | | TextureTarget CubeFaceIndexToTextureTarget(size_t face); |
144 | | size_t CubeMapTextureTargetToFaceIndex(TextureTarget target); |
145 | | bool IsCubeMapFaceTarget(TextureTarget target); |
146 | | |
147 | | constexpr TextureTarget kCubeMapTextureTargetMin = TextureTarget::CubeMapPositiveX; |
148 | | constexpr TextureTarget kCubeMapTextureTargetMax = TextureTarget::CubeMapNegativeZ; |
149 | | constexpr TextureTarget kAfterCubeMapTextureTargetMax = |
150 | | static_cast<TextureTarget>(static_cast<uint8_t>(kCubeMapTextureTargetMax) + 1); |
151 | | struct AllCubeFaceTextureTargets |
152 | | { |
153 | 0 | angle::EnumIterator<TextureTarget> begin() const { return kCubeMapTextureTargetMin; } |
154 | 0 | angle::EnumIterator<TextureTarget> end() const { return kAfterCubeMapTextureTargetMax; } |
155 | | }; |
156 | | |
157 | | constexpr ShaderType kGLES2ShaderTypeMin = ShaderType::Vertex; |
158 | | constexpr ShaderType kGLES2ShaderTypeMax = ShaderType::Fragment; |
159 | | constexpr ShaderType kAfterGLES2ShaderTypeMax = |
160 | | static_cast<ShaderType>(static_cast<uint8_t>(kGLES2ShaderTypeMax) + 1); |
161 | | struct AllGLES2ShaderTypes |
162 | | { |
163 | 0 | angle::EnumIterator<ShaderType> begin() const { return kGLES2ShaderTypeMin; } |
164 | 0 | angle::EnumIterator<ShaderType> end() const { return kAfterGLES2ShaderTypeMax; } |
165 | | }; |
166 | | |
167 | | constexpr ShaderType kShaderTypeMin = ShaderType::Vertex; |
168 | | constexpr ShaderType kShaderTypeMax = ShaderType::Compute; |
169 | | constexpr ShaderType kAfterShaderTypeMax = |
170 | | static_cast<ShaderType>(static_cast<uint8_t>(kShaderTypeMax) + 1); |
171 | | struct AllShaderTypes |
172 | | { |
173 | 0 | angle::EnumIterator<ShaderType> begin() const { return kShaderTypeMin; } |
174 | 0 | angle::EnumIterator<ShaderType> end() const { return kAfterShaderTypeMax; } |
175 | | }; |
176 | | |
177 | | constexpr size_t kGraphicsShaderCount = static_cast<size_t>(ShaderType::EnumCount) - 1u; |
178 | | // Arrange the shader types in the order of rendering pipeline |
179 | | constexpr std::array<ShaderType, kGraphicsShaderCount> kAllGraphicsShaderTypes = { |
180 | | ShaderType::Vertex, ShaderType::Geometry, ShaderType::Fragment}; |
181 | | |
182 | | using ShaderBitSet = angle::PackedEnumBitSet<ShaderType, uint8_t>; |
183 | | static_assert(sizeof(ShaderBitSet) == sizeof(uint8_t), "Unexpected size"); |
184 | | |
185 | | template <typename T> |
186 | | using ShaderMap = angle::PackedEnumMap<ShaderType, T>; |
187 | | |
188 | | TextureType SamplerTypeToTextureType(GLenum samplerType); |
189 | | |
190 | | } // namespace gl |
191 | | |
192 | | namespace egl |
193 | | { |
194 | | MessageType ErrorCodeToMessageType(EGLint errorCode); |
195 | | } // namespace egl |
196 | | |
197 | | namespace egl_gl |
198 | | { |
199 | | gl::TextureTarget EGLCubeMapTargetToCubeMapTarget(EGLenum eglTarget); |
200 | | gl::TextureTarget EGLImageTargetToTextureTarget(EGLenum eglTarget); |
201 | | gl::TextureType EGLTextureTargetToTextureType(EGLenum eglTarget); |
202 | | } // namespace egl_gl |
203 | | |
204 | | #endif // COMMON_PACKEDGLENUMS_H_ |