1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Random Shader Generator
3 * ----------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Variable Value class.
22 *//*--------------------------------------------------------------------*/
24 #include "rsgVariableValue.hpp"
32 template <class CompareOp>
33 bool compareValueRangesAllTrue (const ConstValueRangeAccess& a, const ConstValueRangeAccess& b)
35 DE_ASSERT(a.getType() == b.getType());
37 if (a.getType().isStruct())
39 int numMembers = (int)a.getType().getMembers().size();
40 for (int ndx = 0; ndx < numMembers; ndx++)
42 if (!compareValueRangesAllTrue<CompareOp>(a.member(ndx), b.member(ndx)))
46 else if (a.getType().isArray())
48 int numElements = (int)a.getType().getNumElements();
49 for (int ndx = 0; ndx < numElements; ndx++)
51 if (!compareValueRangesAllTrue<CompareOp>(a.arrayElement(ndx), b.arrayElement(ndx)))
57 int numElements = (int)a.getType().getNumElements();
58 switch (a.getType().getBaseType())
60 case VariableType::TYPE_FLOAT:
61 for (int ndx = 0; ndx < numElements; ndx++)
63 float aMin = a.component(ndx).getMin().asFloat();
64 float aMax = a.component(ndx).getMax().asFloat();
65 float bMin = b.component(ndx).getMin().asFloat();
66 float bMax = b.component(ndx).getMax().asFloat();
68 if (!CompareOp()(aMin, aMax, bMin, bMax))
73 case VariableType::TYPE_INT:
74 case VariableType::TYPE_SAMPLER_2D:
75 case VariableType::TYPE_SAMPLER_CUBE:
76 for (int ndx = 0; ndx < numElements; ndx++)
78 int aMin = a.component(ndx).getMin().asInt();
79 int aMax = a.component(ndx).getMax().asInt();
80 int bMin = b.component(ndx).getMin().asInt();
81 int bMax = b.component(ndx).getMax().asInt();
83 if (!CompareOp()(aMin, aMax, bMin, bMax))
88 case VariableType::TYPE_BOOL:
89 for (int ndx = 0; ndx < numElements; ndx++)
91 bool aMin = a.component(ndx).getMin().asBool();
92 bool aMax = a.component(ndx).getMax().asBool();
93 bool bMin = b.component(ndx).getMin().asBool();
94 bool bMax = b.component(ndx).getMax().asBool();
96 if (!CompareOp()(aMin, aMax, bMin, bMax))
110 inline int toInt (bool boolVal) { return boolVal ? 1 : 0; }
112 struct CompareIntersection
114 inline bool operator() (float aMin, float aMax, float bMin, float bMax) const { return (aMin <= bMax && bMin <= aMax); }
115 inline bool operator() (int aMin, int aMax, int bMin, int bMax) const { return (aMin <= bMax && bMin <= aMax); }
117 inline bool operator() (bool aMin, bool aMax, bool bMin, bool bMax) const
119 return CompareIntersection()(toInt(aMin), toInt(aMax), toInt(bMin), toInt(bMax));
123 struct CompareIsSubsetOf
125 inline bool operator() (float aMin, float aMax, float bMin, float bMax) const
127 return de::inRange(aMin, bMin, bMax) && de::inRange(aMax, bMin, bMax);
130 inline bool operator() (int aMin, int aMax, int bMin, int bMax) const
132 return de::inRange(aMin, bMin, bMax) && de::inRange(aMax, bMin, bMax);
135 inline bool operator() (bool aMin, bool aMax, bool bMin, bool bMax) const
137 return CompareIsSubsetOf()(toInt(aMin), toInt(aMax), toInt(bMin), toInt(bMax));
143 bool ConstValueRangeAccess::intersects (const ConstValueRangeAccess& other) const
145 return compareValueRangesAllTrue<CompareIntersection>(*this, other);
148 bool ConstValueRangeAccess::isSubsetOf (const ConstValueRangeAccess& other) const
150 return compareValueRangesAllTrue<CompareIsSubsetOf>(*this, other);
153 bool ConstValueRangeAccess::isSupersetOf (const ConstValueRangeAccess& other) const
155 return other.isSubsetOf(*this);
158 ValueRange::ValueRange (const VariableType& type)
160 , m_min (type.getScalarSize())
161 , m_max (type.getScalarSize())
165 ValueRange::ValueRange (const VariableType& type, const ConstValueAccess& minVal, const ConstValueAccess& maxVal)
167 , m_min (type.getScalarSize())
168 , m_max (type.getScalarSize())
170 getMin() = minVal.value();
171 getMax() = maxVal.value();
174 ValueRange::ValueRange (const VariableType& type, const Scalar* minVal, const Scalar* maxVal)
176 , m_min (type.getScalarSize())
177 , m_max (type.getScalarSize())
179 getMin() = ConstValueAccess(type, minVal).value();
180 getMax() = ConstValueAccess(type, maxVal).value();
183 ValueRange::ValueRange (ConstValueRangeAccess other)
184 : m_type (other.getType())
185 , m_min (other.getType().getScalarSize())
186 , m_max (other.getType().getScalarSize())
188 getMin() = other.getMin().value();
189 getMax() = other.getMax().value();
192 ValueRange::~ValueRange (void)
196 void ValueRange::computeIntersection (ValueRange& dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b)
198 computeIntersection(dst.asAccess(), a, b);
201 void ValueRange::computeIntersection (ValueRangeAccess dst, const ConstValueRangeAccess& a, const ConstValueRangeAccess& b)
203 DE_ASSERT(dst.getType() == a.getType() && dst.getType() == b.getType());
205 if (a.getType().isStruct())
207 int numMembers = (int)a.getType().getMembers().size();
208 for (int ndx = 0; ndx < numMembers; ndx++)
209 computeIntersection(dst.member(ndx), a.member(ndx), b.member(ndx));
211 else if (a.getType().isArray())
213 int numElements = (int)a.getType().getNumElements();
214 for (int ndx = 0; ndx < numElements; ndx++)
215 computeIntersection(dst.arrayElement(ndx), a.arrayElement(ndx), b.arrayElement(ndx));
219 int numElements = (int)a.getType().getNumElements();
220 switch (a.getType().getBaseType())
222 case VariableType::TYPE_FLOAT:
223 for (int ndx = 0; ndx < numElements; ndx++)
225 float aMin = a.component(ndx).getMin().asFloat();
226 float aMax = a.component(ndx).getMax().asFloat();
227 float bMin = b.component(ndx).getMin().asFloat();
228 float bMax = b.component(ndx).getMax().asFloat();
230 dst.component(ndx).getMin() = de::max(aMin, bMin);
231 dst.component(ndx).getMax() = de::min(aMax, bMax);
235 case VariableType::TYPE_INT:
236 case VariableType::TYPE_SAMPLER_2D:
237 case VariableType::TYPE_SAMPLER_CUBE:
238 for (int ndx = 0; ndx < numElements; ndx++)
240 int aMin = a.component(ndx).getMin().asInt();
241 int aMax = a.component(ndx).getMax().asInt();
242 int bMin = b.component(ndx).getMin().asInt();
243 int bMax = b.component(ndx).getMax().asInt();
245 dst.component(ndx).getMin() = de::max(aMin, bMin);
246 dst.component(ndx).getMax() = de::min(aMax, bMax);
250 case VariableType::TYPE_BOOL:
251 for (int ndx = 0; ndx < numElements; ndx++)
253 bool aMin = a.component(ndx).getMin().asBool();
254 bool aMax = a.component(ndx).getMax().asBool();
255 bool bMin = b.component(ndx).getMin().asBool();
256 bool bMax = b.component(ndx).getMax().asBool();
258 dst.component(ndx).getMin() = aMin || bMin;
259 dst.component(ndx).getMax() = aMax && bMax;
269 VariableValue::VariableValue (const VariableValue& other)
270 : m_variable(other.m_variable)
271 , m_storage(other.m_variable->getType())
273 m_storage.getValue(getType()) = other.getValue().value();
276 VariableValue& VariableValue::operator= (const VariableValue& other)
278 m_variable = other.m_variable;
279 m_storage.setStorage(getType());
280 m_storage.getValue(getType()) = other.getValue().value();