arb_program_interface_query: set vs_input2[1][0] as valid name
[piglit.git] / tests / llvmpipe / glsl / value.h
blob2c870043d1a5106e29209af3decf906b73d6072e
1 /*
2 * Copyright © 2012-2021 VMware, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 #pragma once
26 #include <string>
27 #include <vector>
28 #include <sstream>
30 #include "../utils/colour.h"
32 namespace glsl
34 class Value
36 public:
37 static const Value none;
38 static const Value vec2;
39 static const Value vec3;
40 static const Value vec4;
42 public:
43 Value()
44 : mSize(0)
48 Value(int components, float value)
49 : mSize(0)
51 for (int i = 0; i < components; ++i)
52 add(value);
55 Value(float x)
56 : mSize(0)
58 add(x);
61 Value(float x, float y)
62 : mSize(0)
64 add(x);
65 add(y);
68 Value(float x, float y, float z)
69 : mSize(0)
71 add(x);
72 add(y);
73 add(z);
76 Value(float x, float y, float z, float w)
77 : mSize(0)
79 add(x);
80 add(y);
81 add(z);
82 add(w);
85 Value(const Colour& colour)
86 : mSize(4)
88 memcpy(mValue, (const float*)colour, sizeof(float) * 4);
91 Value(const Value& other)
92 : mSize(other.mSize)
94 memcpy(mValue, other.mValue, sizeof(float) * 4);
97 template<typename T>
98 static Value create(const T& val)
100 Value value;
101 *(value.valuePtr<T>()) = val;
102 return value;
105 float& x()
107 assert(mSize >= 1);
108 return mValue[0];
111 float& y()
113 assert(mSize >= 2);
114 return mValue[1];
117 float& z()
119 assert(mSize >= 3);
120 return mValue[2];
123 float& w()
125 assert(mSize == 4);
126 return mValue[3];
129 float& operator[](int i)
131 return mValue[i];
134 const float& operator[](int i) const
136 return mValue[i];
139 operator const GLfloat*() const
141 return mValue;
144 size_t size() const
146 return mSize;
149 void add(float value)
151 assert(mSize < 4);
152 mValue[mSize++] = value;
155 template<typename T>
156 T* valuePtr() const
158 return (T*)&mValue;
161 template<typename T>
162 T& value() const
164 return *(T*)&mValue;
167 std::string toTypeString() const
169 if (mSize == 1)
170 return "float";
172 std::string type = "vec";
173 type.push_back((char)('0' + mSize));
174 return type;
177 std::string toString() const
179 std::stringstream ss;
180 ss.precision(9);
182 ss << std::fixed;
184 if (mSize > 1)
185 ss << "vec" << mSize << "(";
187 for (size_t i = 0; i < mSize; ++i) {
188 if (i > 0)
189 ss << ", ";
191 ss << mValue[i];
194 if (mSize > 1)
195 ss << ")";
197 return ss.str();
200 bool operator >(const Value& rhs) const
202 assert(mSize == rhs.mSize);
204 for (size_t i = 0; i < mSize; ++i) {
205 if (! (mValue[i] > rhs[i]) )
206 return false;
209 return true;
212 bool operator <(const Value& rhs) const
214 assert(mSize == rhs.mSize);
216 for (size_t i = 0; i < mSize; ++i) {
217 if (! (mValue[i] < rhs[i]) )
218 return false;
221 return true;
224 bool operator >=(const Value& rhs) const
226 assert(mSize == rhs.mSize);
228 for (size_t i = 0; i < mSize; ++i) {
229 if (! (mValue[i] >= rhs[i]) )
230 return false;
233 return true;
236 bool operator <=(const Value& rhs) const
238 assert(mSize == rhs.mSize);
240 for (size_t i = 0; i < mSize; ++i) {
241 if (! (mValue[i] <= rhs[i]) )
242 return false;
245 return true;
248 Value& operator =(const Value& rhs)
250 mSize = rhs.mSize;
251 memcpy(mValue, rhs.mValue, sizeof(float) * 4);
252 return *this;
255 Value& operator =(const Colour& rhs)
257 mSize = 4;
258 memcpy(mValue, (const float*)rhs, sizeof(float) * 4);
259 return *this;
262 Value& operator +=(const Value& rhs)
264 *this = *this + rhs;
265 return *this;
268 Value& operator -=(const Value& rhs)
270 *this = *this- rhs;
271 return *this;
274 Value& operator *=(const Value& rhs)
276 *this = *this * rhs;
277 return *this;
280 Value& operator /=(const Value& rhs)
282 *this = *this / rhs;
283 return *this;
286 Value operator +(const Value& rhs) const
288 assert(mSize == rhs.mSize);
290 Value result;
291 result.mSize = mSize;
293 for (size_t i = 0; i < mSize; ++i)
294 result.mValue[i] = mValue[i] + rhs.mValue[i];
296 return result;
299 Value operator -(const Value& rhs) const
301 assert(mSize == rhs.mSize);
303 Value result;
304 result.mSize = mSize;
306 for (size_t i = 0; i < mSize; ++i)
307 result.mValue[i] = mValue[i] - rhs.mValue[i];
309 return result;
312 Value operator *(const Value& rhs) const
314 assert(mSize == rhs.mSize || rhs.mSize == 1);
316 Value result;
317 result.mSize = mSize;
319 if (rhs.mSize == 1) {
320 for (size_t i = 0; i < rhs.mSize; ++i) {
321 result.mValue[i] = mValue[i] * rhs.mValue[0];
323 } else {
324 for (size_t i = 0; i < mSize; ++i) {
325 result.mValue[i] = mValue[i] * rhs.mValue[i];
329 return result;
332 Value operator /(const Value& rhs) const
334 assert(mSize == rhs.mSize || rhs.mSize == 1);
336 Value result;
337 result.mSize = mSize;
339 if (rhs.mSize == 1) {
340 for (size_t i = 0; i < rhs.mSize; ++i) {
341 result.mValue[i] = mValue[i] / rhs.mValue[0];
343 } else {
344 for (size_t i = 0; i < mSize; ++i) {
345 result.mValue[i] = mValue[i] / rhs.mValue[i];
349 return result;
352 protected:
353 size_t mSize;
354 float mValue[4];
357 Value vec2(float x, float y);
358 Value vec3(float x, float y, float z);
359 Value vec4(float x, float y, float z, float w);