glsBuiltinPrecisionTests.cpp revision d6148171f88da1301f053e2e0236afc69416137c
1/*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL (ES) Module 3 * ----------------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 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 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 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. 18 * 19 *//*! 20 * \file 21 * \brief Precision and range tests for GLSL builtins and types. 22 * 23 *//*--------------------------------------------------------------------*/ 24 25#include "glsBuiltinPrecisionTests.hpp" 26 27#include "deMath.h" 28#include "deMemory.h" 29#include "deDefs.hpp" 30#include "deRandom.hpp" 31#include "deSTLUtil.hpp" 32#include "deStringUtil.hpp" 33#include "deUniquePtr.hpp" 34#include "deSharedPtr.hpp" 35#include "deArrayUtil.hpp" 36 37#include "tcuCommandLine.hpp" 38#include "tcuFloatFormat.hpp" 39#include "tcuInterval.hpp" 40#include "tcuTestCase.hpp" 41#include "tcuTestLog.hpp" 42#include "tcuVector.hpp" 43#include "tcuMatrix.hpp" 44#include "tcuResultCollector.hpp" 45 46#include "gluContextInfo.hpp" 47#include "gluVarType.hpp" 48#include "gluRenderContext.hpp" 49#include "glwDefs.hpp" 50 51#include "glsShaderExecUtil.hpp" 52 53#include <cmath> 54#include <string> 55#include <sstream> 56#include <iostream> 57#include <map> 58#include <utility> 59 60// Uncomment this to get evaluation trace dumps to std::cerr 61// #define GLS_ENABLE_TRACE 62 63// set this to true to dump even passing results 64#define GLS_LOG_ALL_RESULTS false 65 66namespace deqp 67{ 68namespace gls 69{ 70namespace BuiltinPrecisionTests 71{ 72 73using std::string; 74using std::map; 75using std::ostream; 76using std::ostringstream; 77using std::pair; 78using std::vector; 79using std::set; 80 81using de::MovePtr; 82using de::Random; 83using de::SharedPtr; 84using de::UniquePtr; 85using tcu::Interval; 86using tcu::FloatFormat; 87using tcu::MessageBuilder; 88using tcu::TestCase; 89using tcu::TestLog; 90using tcu::Vector; 91using tcu::Matrix; 92namespace matrix = tcu::matrix; 93using glu::Precision; 94using glu::RenderContext; 95using glu::VarType; 96using glu::DataType; 97using glu::ShaderType; 98using glu::ContextInfo; 99using gls::ShaderExecUtil::Symbol; 100 101typedef TestCase::IterateResult IterateResult; 102 103using namespace glw; 104using namespace tcu; 105 106/*--------------------------------------------------------------------*//*! 107 * \brief Generic singleton creator. 108 * 109 * instance<T>() returns a reference to a unique default-constructed instance 110 * of T. This is mainly used for our GLSL function implementations: each 111 * function is implemented by an object, and each of the objects has a 112 * distinct class. It would be extremely toilsome to maintain a separate 113 * context object that contained individual instances of the function classes, 114 * so we have to resort to global singleton instances. 115 * 116 *//*--------------------------------------------------------------------*/ 117template <typename T> 118const T& instance (void) 119{ 120 static const T s_instance = T(); 121 return s_instance; 122} 123 124/*--------------------------------------------------------------------*//*! 125 * \brief Dummy placeholder type for unused template parameters. 126 * 127 * In the precision tests we are dealing with functions of different arities. 128 * To minimize code duplication, we only define templates with the maximum 129 * number of arguments, currently four. If a function's arity is less than the 130 * maximum, Void us used as the type for unused arguments. 131 * 132 * Although Voids are not used at run-time, they still must be compilable, so 133 * they must support all operations that other types do. 134 * 135 *//*--------------------------------------------------------------------*/ 136struct Void 137{ 138 typedef Void Element; 139 enum 140 { 141 SIZE = 0, 142 }; 143 144 template <typename T> 145 explicit Void (const T&) {} 146 Void (void) {} 147 operator double (void) const { return TCU_NAN; } 148 149 // These are used to make Voids usable as containers in container-generic code. 150 Void& operator[] (int) { return *this; } 151 const Void& operator[] (int) const { return *this; } 152}; 153 154ostream& operator<< (ostream& os, Void) { return os << "()"; } 155 156//! Returns true for all other types except Void 157template <typename T> bool isTypeValid (void) { return true; } 158template <> bool isTypeValid<Void> (void) { return false; } 159 160//! Utility function for getting the name of a data type. 161//! This is used in vector and matrix constructors. 162template <typename T> 163const char* dataTypeNameOf (void) 164{ 165 return glu::getDataTypeName(glu::dataTypeOf<T>()); 166} 167 168template <> 169const char* dataTypeNameOf<Void> (void) 170{ 171 DE_ASSERT(!"Impossible"); 172 return DE_NULL; 173} 174 175//! A hack to get Void support for VarType. 176template <typename T> 177VarType getVarTypeOf (Precision prec = glu::PRECISION_LAST) 178{ 179 return glu::varTypeOf<T>(prec); 180} 181 182template <> 183VarType getVarTypeOf<Void> (Precision) 184{ 185 DE_ASSERT(!"Impossible"); 186 return VarType(); 187} 188 189/*--------------------------------------------------------------------*//*! 190 * \brief Type traits for generalized interval types. 191 * 192 * We are trying to compute sets of acceptable values not only for 193 * float-valued expressions but also for compound values: vectors and 194 * matrices. We approximate a set of vectors as a vector of intervals and 195 * likewise for matrices. 196 * 197 * We now need generalized operations for each type and its interval 198 * approximation. These are given in the type Traits<T>. 199 * 200 * The type Traits<T>::IVal is the approximation of T: it is `Interval` for 201 * scalar types, and a vector or matrix of intervals for container types. 202 * 203 * To allow template inference to take place, there are function wrappers for 204 * the actual operations in Traits<T>. Hence we can just use: 205 * 206 * makeIVal(someFloat) 207 * 208 * instead of: 209 * 210 * Traits<float>::doMakeIVal(value) 211 * 212 *//*--------------------------------------------------------------------*/ 213 214template <typename T> struct Traits; 215 216//! Create container from elementwise singleton values. 217template <typename T> 218typename Traits<T>::IVal makeIVal (const T& value) 219{ 220 return Traits<T>::doMakeIVal(value); 221} 222 223//! Elementwise union of intervals. 224template <typename T> 225typename Traits<T>::IVal unionIVal (const typename Traits<T>::IVal& a, 226 const typename Traits<T>::IVal& b) 227{ 228 return Traits<T>::doUnion(a, b); 229} 230 231//! Returns true iff every element of `ival` contains the corresponding element of `value`. 232template <typename T> 233bool contains (const typename Traits<T>::IVal& ival, const T& value) 234{ 235 return Traits<T>::doContains(ival, value); 236} 237 238//! Print out an interval with the precision of `fmt`. 239template <typename T> 240void printIVal (const FloatFormat& fmt, const typename Traits<T>::IVal& ival, ostream& os) 241{ 242 Traits<T>::doPrintIVal(fmt, ival, os); 243} 244 245template <typename T> 246string intervalToString (const FloatFormat& fmt, const typename Traits<T>::IVal& ival) 247{ 248 ostringstream oss; 249 printIVal<T>(fmt, ival, oss); 250 return oss.str(); 251} 252 253//! Print out a value with the precision of `fmt`. 254template <typename T> 255void printValue (const FloatFormat& fmt, const T& value, ostream& os) 256{ 257 Traits<T>::doPrintValue(fmt, value, os); 258} 259 260template <typename T> 261string valueToString (const FloatFormat& fmt, const T& val) 262{ 263 ostringstream oss; 264 printValue(fmt, val, oss); 265 return oss.str(); 266} 267 268//! Approximate `value` elementwise to the float precision defined in `fmt`. 269//! The resulting interval might not be a singleton if rounding in both 270//! directions is allowed. 271template <typename T> 272typename Traits<T>::IVal round (const FloatFormat& fmt, const T& value) 273{ 274 return Traits<T>::doRound(fmt, value); 275} 276 277template <typename T> 278typename Traits<T>::IVal convert (const FloatFormat& fmt, 279 const typename Traits<T>::IVal& value) 280{ 281 return Traits<T>::doConvert(fmt, value); 282} 283 284//! Common traits for scalar types. 285template <typename T> 286struct ScalarTraits 287{ 288 typedef Interval IVal; 289 290 static Interval doMakeIVal (const T& value) 291 { 292 // Thankfully all scalar types have a well-defined conversion to `double`, 293 // hence Interval can represent their ranges without problems. 294 return Interval(double(value)); 295 } 296 297 static Interval doUnion (const Interval& a, const Interval& b) 298 { 299 return a | b; 300 } 301 302 static bool doContains (const Interval& a, T value) 303 { 304 return a.contains(double(value)); 305 } 306 307 static Interval doConvert (const FloatFormat& fmt, const IVal& ival) 308 { 309 return fmt.convert(ival); 310 } 311 312 static Interval doRound (const FloatFormat& fmt, T value) 313 { 314 return fmt.roundOut(double(value), false); 315 } 316}; 317 318template<> 319struct Traits<float> : ScalarTraits<float> 320{ 321 static void doPrintIVal (const FloatFormat& fmt, 322 const Interval& ival, 323 ostream& os) 324 { 325 os << fmt.intervalToHex(ival); 326 } 327 328 static void doPrintValue (const FloatFormat& fmt, 329 const float& value, 330 ostream& os) 331 { 332 os << fmt.floatToHex(value); 333 } 334}; 335 336template<> 337struct Traits<bool> : ScalarTraits<bool> 338{ 339 static void doPrintValue (const FloatFormat&, 340 const float& value, 341 ostream& os) 342 { 343 os << (value ? "true" : "false"); 344 } 345 346 static void doPrintIVal (const FloatFormat&, 347 const Interval& ival, 348 ostream& os) 349 { 350 os << "{"; 351 if (ival.contains(false)) 352 os << "false"; 353 if (ival.contains(false) && ival.contains(true)) 354 os << ", "; 355 if (ival.contains(true)) 356 os << "true"; 357 os << "}"; 358 } 359}; 360 361template<> 362struct Traits<int> : ScalarTraits<int> 363{ 364 static void doPrintValue (const FloatFormat&, 365 const int& value, 366 ostream& os) 367 { 368 os << value; 369 } 370 371 static void doPrintIVal (const FloatFormat&, 372 const Interval& ival, 373 ostream& os) 374 { 375 os << "[" << int(ival.lo()) << ", " << int(ival.hi()) << "]"; 376 } 377}; 378 379//! Common traits for containers, i.e. vectors and matrices. 380//! T is the container type itself, I is the same type with interval elements. 381template <typename T, typename I> 382struct ContainerTraits 383{ 384 typedef typename T::Element Element; 385 typedef I IVal; 386 387 static IVal doMakeIVal (const T& value) 388 { 389 IVal ret; 390 391 for (int ndx = 0; ndx < T::SIZE; ++ndx) 392 ret[ndx] = makeIVal(value[ndx]); 393 394 return ret; 395 } 396 397 static IVal doUnion (const IVal& a, const IVal& b) 398 { 399 IVal ret; 400 401 for (int ndx = 0; ndx < T::SIZE; ++ndx) 402 ret[ndx] = unionIVal<Element>(a[ndx], b[ndx]); 403 404 return ret; 405 } 406 407 static bool doContains (const IVal& ival, const T& value) 408 { 409 for (int ndx = 0; ndx < T::SIZE; ++ndx) 410 if (!contains(ival[ndx], value[ndx])) 411 return false; 412 413 return true; 414 } 415 416 static void doPrintIVal (const FloatFormat& fmt, const IVal ival, ostream& os) 417 { 418 os << "("; 419 420 for (int ndx = 0; ndx < T::SIZE; ++ndx) 421 { 422 if (ndx > 0) 423 os << ", "; 424 425 printIVal<Element>(fmt, ival[ndx], os); 426 } 427 428 os << ")"; 429 } 430 431 static void doPrintValue (const FloatFormat& fmt, const T& value, ostream& os) 432 { 433 os << dataTypeNameOf<T>() << "("; 434 435 for (int ndx = 0; ndx < T::SIZE; ++ndx) 436 { 437 if (ndx > 0) 438 os << ", "; 439 440 printValue<Element>(fmt, value[ndx], os); 441 } 442 443 os << ")"; 444 } 445 446 static IVal doConvert (const FloatFormat& fmt, const IVal& value) 447 { 448 IVal ret; 449 450 for (int ndx = 0; ndx < T::SIZE; ++ndx) 451 ret[ndx] = convert<Element>(fmt, value[ndx]); 452 453 return ret; 454 } 455 456 static IVal doRound (const FloatFormat& fmt, T value) 457 { 458 IVal ret; 459 460 for (int ndx = 0; ndx < T::SIZE; ++ndx) 461 ret[ndx] = round(fmt, value[ndx]); 462 463 return ret; 464 } 465}; 466 467template <typename T, int Size> 468struct Traits<Vector<T, Size> > : 469 ContainerTraits<Vector<T, Size>, Vector<typename Traits<T>::IVal, Size> > 470{ 471}; 472 473template <typename T, int Rows, int Cols> 474struct Traits<Matrix<T, Rows, Cols> > : 475 ContainerTraits<Matrix<T, Rows, Cols>, Matrix<typename Traits<T>::IVal, Rows, Cols> > 476{ 477}; 478 479//! Void traits. These are just dummies, but technically valid: a Void is a 480//! unit type with a single possible value. 481template<> 482struct Traits<Void> 483{ 484 typedef Void IVal; 485 486 static Void doMakeIVal (const Void& value) { return value; } 487 static Void doUnion (const Void&, const Void&) { return Void(); } 488 static bool doContains (const Void&, Void) { return true; } 489 static Void doRound (const FloatFormat&, const Void& value) { return value; } 490 static Void doConvert (const FloatFormat&, const Void& value) { return value; } 491 492 static void doPrintValue (const FloatFormat&, const Void&, ostream& os) 493 { 494 os << "()"; 495 } 496 497 static void doPrintIVal (const FloatFormat&, const Void&, ostream& os) 498 { 499 os << "()"; 500 } 501}; 502 503//! This is needed for container-generic operations. 504//! We want a scalar type T to be its own "one-element vector". 505template <typename T, int Size> struct ContainerOf { typedef Vector<T, Size> Container; }; 506 507template <typename T> struct ContainerOf<T, 1> { typedef T Container; }; 508template <int Size> struct ContainerOf<Void, Size> { typedef Void Container; }; 509 510// This is a kludge that is only needed to get the ExprP::operator[] syntactic sugar to work. 511template <typename T> struct ElementOf { typedef typename T::Element Element; }; 512template <> struct ElementOf<float> { typedef void Element; }; 513template <> struct ElementOf<bool> { typedef void Element; }; 514template <> struct ElementOf<int> { typedef void Element; }; 515 516/*--------------------------------------------------------------------*//*! 517 * 518 * \name Abstract syntax for expressions and statements. 519 * 520 * We represent GLSL programs as syntax objects: an Expr<T> represents an 521 * expression whose GLSL type corresponds to the C++ type T, and a Statement 522 * represents a statement. 523 * 524 * To ease memory management, we use shared pointers to refer to expressions 525 * and statements. ExprP<T> is a shared pointer to an Expr<T>, and StatementP 526 * is a shared pointer to a Statement. 527 * 528 * \{ 529 * 530 *//*--------------------------------------------------------------------*/ 531 532class ExprBase; 533class ExpandContext; 534class Statement; 535class StatementP; 536class FuncBase; 537template <typename T> class ExprP; 538template <typename T> class Variable; 539template <typename T> class VariableP; 540template <typename T> class DefaultSampling; 541 542typedef set<const FuncBase*> FuncSet; 543 544template <typename T> 545VariableP<T> variable (const string& name); 546StatementP compoundStatement (const vector<StatementP>& statements); 547 548/*--------------------------------------------------------------------*//*! 549 * \brief A variable environment. 550 * 551 * An Environment object maintains the mapping between variables of the 552 * abstract syntax tree and their values. 553 * 554 * \todo [2014-03-28 lauri] At least run-time type safety. 555 * 556 *//*--------------------------------------------------------------------*/ 557class Environment 558{ 559public: 560 template<typename T> 561 void bind (const Variable<T>& variable, 562 const typename Traits<T>::IVal& value) 563 { 564 deUint8* const data = new deUint8[sizeof(value)]; 565 566 deMemcpy(data, &value, sizeof(value)); 567 de::insert(m_map, variable.getName(), SharedPtr<deUint8>(data)); 568 } 569 570 template<typename T> 571 typename Traits<T>::IVal& lookup (const Variable<T>& variable) const 572 { 573 deUint8* const data = de::lookup(m_map, variable.getName()).get(); 574 575 return *reinterpret_cast<typename Traits<T>::IVal*>(data); 576 } 577 578private: 579 map<string, SharedPtr<deUint8, de::ArrayDeleter<deUint8> > > m_map; 580}; 581 582/*--------------------------------------------------------------------*//*! 583 * \brief Evaluation context. 584 * 585 * The evaluation context contains everything that separates one execution of 586 * an expression from the next. Currently this means the desired floating 587 * point precision and the current variable environment. 588 * 589 *//*--------------------------------------------------------------------*/ 590struct EvalContext 591{ 592 EvalContext (const FloatFormat& format_, 593 Precision floatPrecision_, 594 Environment& env_, 595 int callDepth_ = 0) 596 : format (format_) 597 , floatPrecision (floatPrecision_) 598 , env (env_) 599 , callDepth (callDepth_) {} 600 601 FloatFormat format; 602 Precision floatPrecision; 603 Environment& env; 604 int callDepth; 605}; 606 607/*--------------------------------------------------------------------*//*! 608 * \brief Simple incremental counter. 609 * 610 * This is used to make sure that different ExpandContexts will not produce 611 * overlapping temporary names. 612 * 613 *//*--------------------------------------------------------------------*/ 614class Counter 615{ 616public: 617 Counter (int count = 0) : m_count(count) {} 618 int operator() (void) { return m_count++; } 619 620private: 621 int m_count; 622}; 623 624class ExpandContext 625{ 626public: 627 ExpandContext (Counter& symCounter) : m_symCounter(symCounter) {} 628 ExpandContext (const ExpandContext& parent) 629 : m_symCounter(parent.m_symCounter) {} 630 631 template<typename T> 632 VariableP<T> genSym (const string& baseName) 633 { 634 return variable<T>(baseName + de::toString(m_symCounter())); 635 } 636 637 void addStatement (const StatementP& stmt) 638 { 639 m_statements.push_back(stmt); 640 } 641 642 vector<StatementP> getStatements (void) const 643 { 644 return m_statements; 645 } 646private: 647 Counter& m_symCounter; 648 vector<StatementP> m_statements; 649}; 650 651/*--------------------------------------------------------------------*//*! 652 * \brief A statement or declaration. 653 * 654 * Statements have no values. Instead, they are executed for their side 655 * effects only: the execute() method should modify at least one variable in 656 * the environment. 657 * 658 * As a bit of a kludge, a Statement object can also represent a declaration: 659 * when it is evaluated, it can add a variable binding to the environment 660 * instead of modifying a current one. 661 * 662 *//*--------------------------------------------------------------------*/ 663class Statement 664{ 665public: 666 virtual ~Statement (void) { } 667 //! Execute the statement, modifying the environment of `ctx` 668 void execute (EvalContext& ctx) const { this->doExecute(ctx); } 669 void print (ostream& os) const { this->doPrint(os); } 670 //! Add the functions used in this statement to `dst`. 671 void getUsedFuncs (FuncSet& dst) const { this->doGetUsedFuncs(dst); } 672 673protected: 674 virtual void doPrint (ostream& os) const = 0; 675 virtual void doExecute (EvalContext& ctx) const = 0; 676 virtual void doGetUsedFuncs (FuncSet& dst) const = 0; 677}; 678 679ostream& operator<<(ostream& os, const Statement& stmt) 680{ 681 stmt.print(os); 682 return os; 683} 684 685/*--------------------------------------------------------------------*//*! 686 * \brief Smart pointer for statements (and declarations) 687 * 688 *//*--------------------------------------------------------------------*/ 689class StatementP : public SharedPtr<const Statement> 690{ 691public: 692 typedef SharedPtr<const Statement> Super; 693 694 StatementP (void) {} 695 explicit StatementP (const Statement* ptr) : Super(ptr) {} 696 StatementP (const Super& ptr) : Super(ptr) {} 697}; 698 699/*--------------------------------------------------------------------*//*! 700 * \brief 701 * 702 * A statement that modifies a variable or a declaration that binds a variable. 703 * 704 *//*--------------------------------------------------------------------*/ 705template <typename T> 706class VariableStatement : public Statement 707{ 708public: 709 VariableStatement (const VariableP<T>& variable, const ExprP<T>& value, 710 bool isDeclaration) 711 : m_variable (variable) 712 , m_value (value) 713 , m_isDeclaration (isDeclaration) {} 714 715protected: 716 void doPrint (ostream& os) const 717 { 718 if (m_isDeclaration) 719 os << glu::declare(getVarTypeOf<T>(), m_variable->getName()); 720 else 721 os << m_variable->getName(); 722 723 os << " = " << *m_value << ";\n"; 724 } 725 726 void doExecute (EvalContext& ctx) const 727 { 728 if (m_isDeclaration) 729 ctx.env.bind(*m_variable, m_value->evaluate(ctx)); 730 else 731 ctx.env.lookup(*m_variable) = m_value->evaluate(ctx); 732 } 733 734 void doGetUsedFuncs (FuncSet& dst) const 735 { 736 m_value->getUsedFuncs(dst); 737 } 738 739 VariableP<T> m_variable; 740 ExprP<T> m_value; 741 bool m_isDeclaration; 742}; 743 744template <typename T> 745StatementP variableStatement (const VariableP<T>& variable, 746 const ExprP<T>& value, 747 bool isDeclaration) 748{ 749 return StatementP(new VariableStatement<T>(variable, value, isDeclaration)); 750} 751 752template <typename T> 753StatementP variableDeclaration (const VariableP<T>& variable, const ExprP<T>& definiens) 754{ 755 return variableStatement(variable, definiens, true); 756} 757 758template <typename T> 759StatementP variableAssignment (const VariableP<T>& variable, const ExprP<T>& value) 760{ 761 return variableStatement(variable, value, false); 762} 763 764/*--------------------------------------------------------------------*//*! 765 * \brief A compound statement, i.e. a block. 766 * 767 * A compound statement is executed by executing its constituent statements in 768 * sequence. 769 * 770 *//*--------------------------------------------------------------------*/ 771class CompoundStatement : public Statement 772{ 773public: 774 CompoundStatement (const vector<StatementP>& statements) 775 : m_statements (statements) {} 776 777protected: 778 void doPrint (ostream& os) const 779 { 780 os << "{\n"; 781 782 for (size_t ndx = 0; ndx < m_statements.size(); ++ndx) 783 os << *m_statements[ndx]; 784 785 os << "}\n"; 786 } 787 788 void doExecute (EvalContext& ctx) const 789 { 790 for (size_t ndx = 0; ndx < m_statements.size(); ++ndx) 791 m_statements[ndx]->execute(ctx); 792 } 793 794 void doGetUsedFuncs (FuncSet& dst) const 795 { 796 for (size_t ndx = 0; ndx < m_statements.size(); ++ndx) 797 m_statements[ndx]->getUsedFuncs(dst); 798 } 799 800 vector<StatementP> m_statements; 801}; 802 803StatementP compoundStatement(const vector<StatementP>& statements) 804{ 805 return StatementP(new CompoundStatement(statements)); 806} 807 808//! Common base class for all expressions regardless of their type. 809class ExprBase 810{ 811public: 812 virtual ~ExprBase (void) {} 813 void printExpr (ostream& os) const { this->doPrintExpr(os); } 814 815 //! Output the functions that this expression refers to 816 void getUsedFuncs (FuncSet& dst) const 817 { 818 this->doGetUsedFuncs(dst); 819 } 820 821protected: 822 virtual void doPrintExpr (ostream&) const {} 823 virtual void doGetUsedFuncs (FuncSet&) const {} 824}; 825 826//! Type-specific operations for an expression representing type T. 827template <typename T> 828class Expr : public ExprBase 829{ 830public: 831 typedef T Val; 832 typedef typename Traits<T>::IVal IVal; 833 834 IVal evaluate (const EvalContext& ctx) const; 835 836protected: 837 virtual IVal doEvaluate (const EvalContext& ctx) const = 0; 838}; 839 840//! Evaluate an expression with the given context, optionally tracing the calls to stderr. 841template <typename T> 842typename Traits<T>::IVal Expr<T>::evaluate (const EvalContext& ctx) const 843{ 844#ifdef GLS_ENABLE_TRACE 845 static const FloatFormat highpFmt (-126, 127, 23, true, 846 tcu::MAYBE, 847 tcu::YES, 848 tcu::MAYBE); 849 EvalContext newCtx (ctx.format, ctx.floatPrecision, 850 ctx.env, ctx.callDepth + 1); 851 const IVal ret = this->doEvaluate(newCtx); 852 853 if (isTypeValid<T>()) 854 { 855 std::cerr << string(ctx.callDepth, ' '); 856 this->printExpr(std::cerr); 857 std::cerr << " -> " << intervalToString<T>(highpFmt, ret) << std::endl; 858 } 859 return ret; 860#else 861 return this->doEvaluate(ctx); 862#endif 863} 864 865template <typename T> 866class ExprPBase : public SharedPtr<const Expr<T> > 867{ 868public: 869}; 870 871ostream& operator<< (ostream& os, const ExprBase& expr) 872{ 873 expr.printExpr(os); 874 return os; 875} 876 877/*--------------------------------------------------------------------*//*! 878 * \brief Shared pointer to an expression of a container type. 879 * 880 * Container types (i.e. vectors and matrices) support the subscription 881 * operator. This class provides a bit of syntactic sugar to allow us to use 882 * the C++ subscription operator to create a subscription expression. 883 *//*--------------------------------------------------------------------*/ 884template <typename T> 885class ContainerExprPBase : public ExprPBase<T> 886{ 887public: 888 ExprP<typename T::Element> operator[] (int i) const; 889}; 890 891template <typename T> 892class ExprP : public ExprPBase<T> {}; 893 894// We treat Voids as containers since the dummy parameters in generalized 895// vector functions are represented as Voids. 896template <> 897class ExprP<Void> : public ContainerExprPBase<Void> {}; 898 899template <typename T, int Size> 900class ExprP<Vector<T, Size> > : public ContainerExprPBase<Vector<T, Size> > {}; 901 902template <typename T, int Rows, int Cols> 903class ExprP<Matrix<T, Rows, Cols> > : public ContainerExprPBase<Matrix<T, Rows, Cols> > {}; 904 905template <typename T> ExprP<T> exprP (void) 906{ 907 return ExprP<T>(); 908} 909 910template <typename T> 911ExprP<T> exprP (const SharedPtr<const Expr<T> >& ptr) 912{ 913 ExprP<T> ret; 914 static_cast<SharedPtr<const Expr<T> >&>(ret) = ptr; 915 return ret; 916} 917 918template <typename T> 919ExprP<T> exprP (const Expr<T>* ptr) 920{ 921 return exprP(SharedPtr<const Expr<T> >(ptr)); 922} 923 924/*--------------------------------------------------------------------*//*! 925 * \brief A shared pointer to a variable expression. 926 * 927 * This is just a narrowing of ExprP for the operations that require a variable 928 * instead of an arbitrary expression. 929 * 930 *//*--------------------------------------------------------------------*/ 931template <typename T> 932class VariableP : public SharedPtr<const Variable<T> > 933{ 934public: 935 typedef SharedPtr<const Variable<T> > Super; 936 explicit VariableP (const Variable<T>* ptr) : Super(ptr) {} 937 VariableP (void) {} 938 VariableP (const Super& ptr) : Super(ptr) {} 939 940 operator ExprP<T> (void) const { return exprP(SharedPtr<const Expr<T> >(*this)); } 941}; 942 943/*--------------------------------------------------------------------*//*! 944 * \name Syntactic sugar operators for expressions. 945 * 946 * @{ 947 * 948 * These operators allow the use of C++ syntax to construct GLSL expressions 949 * containing operators: e.g. "a+b" creates an addition expression with 950 * operands a and b, and so on. 951 * 952 *//*--------------------------------------------------------------------*/ 953ExprP<float> operator-(const ExprP<float>& arg0); 954ExprP<float> operator+(const ExprP<float>& arg0, 955 const ExprP<float>& arg1); 956ExprP<float> operator-(const ExprP<float>& arg0, 957 const ExprP<float>& arg1); 958ExprP<float> operator*(const ExprP<float>& arg0, 959 const ExprP<float>& arg1); 960ExprP<float> operator/(const ExprP<float>& arg0, 961 const ExprP<float>& arg1); 962template<int Size> 963ExprP<Vector<float, Size> > operator-(const ExprP<Vector<float, Size> >& arg0); 964template<int Size> 965ExprP<Vector<float, Size> > operator*(const ExprP<Vector<float, Size> >& arg0, 966 const ExprP<float>& arg1); 967template<int Size> 968ExprP<Vector<float, Size> > operator*(const ExprP<Vector<float, Size> >& arg0, 969 const ExprP<Vector<float, Size> >& arg1); 970template<int Size> 971ExprP<Vector<float, Size> > operator-(const ExprP<Vector<float, Size> >& arg0, 972 const ExprP<Vector<float, Size> >& arg1); 973template<int Left, int Mid, int Right> 974ExprP<Matrix<float, Left, Right> > operator* (const ExprP<Matrix<float, Left, Mid> >& left, 975 const ExprP<Matrix<float, Mid, Right> >& right); 976template<int Rows, int Cols> 977ExprP<Vector<float, Rows> > operator* (const ExprP<Vector<float, Cols> >& left, 978 const ExprP<Matrix<float, Rows, Cols> >& right); 979template<int Rows, int Cols> 980ExprP<Vector<float, Cols> > operator* (const ExprP<Matrix<float, Rows, Cols> >& left, 981 const ExprP<Vector<float, Rows> >& right); 982template<int Rows, int Cols> 983ExprP<Matrix<float, Rows, Cols> > operator* (const ExprP<Matrix<float, Rows, Cols> >& left, 984 const ExprP<float>& right); 985template<int Rows, int Cols> 986ExprP<Matrix<float, Rows, Cols> > operator+ (const ExprP<Matrix<float, Rows, Cols> >& left, 987 const ExprP<Matrix<float, Rows, Cols> >& right); 988template<int Rows, int Cols> 989ExprP<Matrix<float, Rows, Cols> > operator- (const ExprP<Matrix<float, Rows, Cols> >& mat); 990 991//! @} 992 993/*--------------------------------------------------------------------*//*! 994 * \brief Variable expression. 995 * 996 * A variable is evaluated by looking up its range of possible values from an 997 * environment. 998 *//*--------------------------------------------------------------------*/ 999template <typename T> 1000class Variable : public Expr<T> 1001{ 1002public: 1003 typedef typename Expr<T>::IVal IVal; 1004 1005 Variable (const string& name) : m_name (name) {} 1006 string getName (void) const { return m_name; } 1007 1008protected: 1009 void doPrintExpr (ostream& os) const { os << m_name; } 1010 IVal doEvaluate (const EvalContext& ctx) const 1011 { 1012 return ctx.env.lookup<T>(*this); 1013 } 1014 1015private: 1016 string m_name; 1017}; 1018 1019template <typename T> 1020VariableP<T> variable (const string& name) 1021{ 1022 return VariableP<T>(new Variable<T>(name)); 1023} 1024 1025template <typename T> 1026VariableP<T> bindExpression (const string& name, ExpandContext& ctx, const ExprP<T>& expr) 1027{ 1028 VariableP<T> var = ctx.genSym<T>(name); 1029 ctx.addStatement(variableDeclaration(var, expr)); 1030 return var; 1031} 1032 1033/*--------------------------------------------------------------------*//*! 1034 * \brief Constant expression. 1035 * 1036 * A constant is evaluated by rounding it to a set of possible values allowed 1037 * by the current floating point precision. 1038 *//*--------------------------------------------------------------------*/ 1039template <typename T> 1040class Constant : public Expr<T> 1041{ 1042public: 1043 typedef typename Expr<T>::IVal IVal; 1044 1045 Constant (const T& value) : m_value(value) {} 1046 1047protected: 1048 void doPrintExpr (ostream& os) const { os << m_value; } 1049 IVal doEvaluate (const EvalContext&) const { return makeIVal(m_value); } 1050 1051private: 1052 T m_value; 1053}; 1054 1055template <typename T> 1056ExprP<T> constant (const T& value) 1057{ 1058 return exprP(new Constant<T>(value)); 1059} 1060 1061//! Return a reference to a singleton void constant. 1062const ExprP<Void>& voidP (void) 1063{ 1064 static const ExprP<Void> singleton = constant(Void()); 1065 1066 return singleton; 1067} 1068 1069/*--------------------------------------------------------------------*//*! 1070 * \brief Four-element tuple. 1071 * 1072 * This is used for various things where we need one thing for each possible 1073 * function parameter. Currently the maximum supported number of parameters is 1074 * four. 1075 *//*--------------------------------------------------------------------*/ 1076template <typename T0 = Void, typename T1 = Void, typename T2 = Void, typename T3 = Void> 1077struct Tuple4 1078{ 1079 explicit Tuple4 (const T0& e0 = T0(), 1080 const T1& e1 = T1(), 1081 const T2& e2 = T2(), 1082 const T3& e3 = T3()) 1083 : a (e0) 1084 , b (e1) 1085 , c (e2) 1086 , d (e3) 1087 { 1088 } 1089 1090 T0 a; 1091 T1 b; 1092 T2 c; 1093 T3 d; 1094}; 1095 1096/*--------------------------------------------------------------------*//*! 1097 * \brief Function signature. 1098 * 1099 * This is a purely compile-time structure used to bundle all types in a 1100 * function signature together. This makes passing the signature around in 1101 * templates easier, since we only need to take and pass a single Sig instead 1102 * of a bunch of parameter types and a return type. 1103 * 1104 *//*--------------------------------------------------------------------*/ 1105template <typename R, 1106 typename P0 = Void, typename P1 = Void, 1107 typename P2 = Void, typename P3 = Void> 1108struct Signature 1109{ 1110 typedef R Ret; 1111 typedef P0 Arg0; 1112 typedef P1 Arg1; 1113 typedef P2 Arg2; 1114 typedef P3 Arg3; 1115 typedef typename Traits<Ret>::IVal IRet; 1116 typedef typename Traits<Arg0>::IVal IArg0; 1117 typedef typename Traits<Arg1>::IVal IArg1; 1118 typedef typename Traits<Arg2>::IVal IArg2; 1119 typedef typename Traits<Arg3>::IVal IArg3; 1120 1121 typedef Tuple4< const Arg0&, const Arg1&, const Arg2&, const Arg3&> Args; 1122 typedef Tuple4< const IArg0&, const IArg1&, const IArg2&, const IArg3&> IArgs; 1123 typedef Tuple4< ExprP<Arg0>, ExprP<Arg1>, ExprP<Arg2>, ExprP<Arg3> > ArgExprs; 1124}; 1125 1126typedef vector<const ExprBase*> BaseArgExprs; 1127 1128/*--------------------------------------------------------------------*//*! 1129 * \brief Type-independent operations for function objects. 1130 * 1131 *//*--------------------------------------------------------------------*/ 1132class FuncBase 1133{ 1134public: 1135 virtual ~FuncBase (void) {} 1136 virtual string getName (void) const = 0; 1137 //! Name of extension that this function requires, or empty. 1138 virtual string getRequiredExtension (void) const { return ""; } 1139 virtual void print (ostream&, 1140 const BaseArgExprs&) const = 0; 1141 //! Index of output parameter, or -1 if none of the parameters is output. 1142 virtual int getOutParamIndex (void) const { return -1; } 1143 1144 void printDefinition (ostream& os) const 1145 { 1146 doPrintDefinition(os); 1147 } 1148 1149 void getUsedFuncs (FuncSet& dst) const 1150 { 1151 this->doGetUsedFuncs(dst); 1152 } 1153 1154protected: 1155 virtual void doPrintDefinition (ostream& os) const = 0; 1156 virtual void doGetUsedFuncs (FuncSet& dst) const = 0; 1157}; 1158 1159typedef Tuple4<string, string, string, string> ParamNames; 1160 1161/*--------------------------------------------------------------------*//*! 1162 * \brief Function objects. 1163 * 1164 * Each Func object represents a GLSL function. It can be applied to interval 1165 * arguments, and it returns the an interval that is a conservative 1166 * approximation of the image of the GLSL function over the argument 1167 * intervals. That is, it is given a set of possible arguments and it returns 1168 * the set of possible values. 1169 * 1170 *//*--------------------------------------------------------------------*/ 1171template <typename Sig_> 1172class Func : public FuncBase 1173{ 1174public: 1175 typedef Sig_ Sig; 1176 typedef typename Sig::Ret Ret; 1177 typedef typename Sig::Arg0 Arg0; 1178 typedef typename Sig::Arg1 Arg1; 1179 typedef typename Sig::Arg2 Arg2; 1180 typedef typename Sig::Arg3 Arg3; 1181 typedef typename Sig::IRet IRet; 1182 typedef typename Sig::IArg0 IArg0; 1183 typedef typename Sig::IArg1 IArg1; 1184 typedef typename Sig::IArg2 IArg2; 1185 typedef typename Sig::IArg3 IArg3; 1186 typedef typename Sig::Args Args; 1187 typedef typename Sig::IArgs IArgs; 1188 typedef typename Sig::ArgExprs ArgExprs; 1189 1190 void print (ostream& os, 1191 const BaseArgExprs& args) const 1192 { 1193 this->doPrint(os, args); 1194 } 1195 1196 IRet apply (const EvalContext& ctx, 1197 const IArg0& arg0 = IArg0(), 1198 const IArg1& arg1 = IArg1(), 1199 const IArg2& arg2 = IArg2(), 1200 const IArg3& arg3 = IArg3()) const 1201 { 1202 return this->applyArgs(ctx, IArgs(arg0, arg1, arg2, arg3)); 1203 } 1204 IRet applyArgs (const EvalContext& ctx, 1205 const IArgs& args) const 1206 { 1207 return this->doApply(ctx, args); 1208 } 1209 ExprP<Ret> operator() (const ExprP<Arg0>& arg0 = voidP(), 1210 const ExprP<Arg1>& arg1 = voidP(), 1211 const ExprP<Arg2>& arg2 = voidP(), 1212 const ExprP<Arg3>& arg3 = voidP()) const; 1213 1214 const ParamNames& getParamNames (void) const 1215 { 1216 return this->doGetParamNames(); 1217 } 1218 1219protected: 1220 virtual IRet doApply (const EvalContext&, 1221 const IArgs&) const = 0; 1222 virtual void doPrint (ostream& os, const BaseArgExprs& args) const 1223 { 1224 os << getName() << "("; 1225 1226 if (isTypeValid<Arg0>()) 1227 os << *args[0]; 1228 1229 if (isTypeValid<Arg1>()) 1230 os << ", " << *args[1]; 1231 1232 if (isTypeValid<Arg2>()) 1233 os << ", " << *args[2]; 1234 1235 if (isTypeValid<Arg3>()) 1236 os << ", " << *args[3]; 1237 1238 os << ")"; 1239 } 1240 1241 virtual const ParamNames& doGetParamNames (void) const 1242 { 1243 static ParamNames names ("a", "b", "c", "d"); 1244 return names; 1245 } 1246}; 1247 1248template <typename Sig> 1249class Apply : public Expr<typename Sig::Ret> 1250{ 1251public: 1252 typedef typename Sig::Ret Ret; 1253 typedef typename Sig::Arg0 Arg0; 1254 typedef typename Sig::Arg1 Arg1; 1255 typedef typename Sig::Arg2 Arg2; 1256 typedef typename Sig::Arg3 Arg3; 1257 typedef typename Expr<Ret>::Val Val; 1258 typedef typename Expr<Ret>::IVal IVal; 1259 typedef Func<Sig> ApplyFunc; 1260 typedef typename ApplyFunc::ArgExprs ArgExprs; 1261 1262 Apply (const ApplyFunc& func, 1263 const ExprP<Arg0>& arg0 = voidP(), 1264 const ExprP<Arg1>& arg1 = voidP(), 1265 const ExprP<Arg2>& arg2 = voidP(), 1266 const ExprP<Arg3>& arg3 = voidP()) 1267 : m_func (func), 1268 m_args (arg0, arg1, arg2, arg3) {} 1269 1270 Apply (const ApplyFunc& func, 1271 const ArgExprs& args) 1272 : m_func (func), 1273 m_args (args) {} 1274protected: 1275 void doPrintExpr (ostream& os) const 1276 { 1277 BaseArgExprs args; 1278 args.push_back(m_args.a.get()); 1279 args.push_back(m_args.b.get()); 1280 args.push_back(m_args.c.get()); 1281 args.push_back(m_args.d.get()); 1282 m_func.print(os, args); 1283 } 1284 1285 IVal doEvaluate (const EvalContext& ctx) const 1286 { 1287 return m_func.apply(ctx, 1288 m_args.a->evaluate(ctx), m_args.b->evaluate(ctx), 1289 m_args.c->evaluate(ctx), m_args.d->evaluate(ctx)); 1290 } 1291 1292 void doGetUsedFuncs (FuncSet& dst) const 1293 { 1294 m_func.getUsedFuncs(dst); 1295 m_args.a->getUsedFuncs(dst); 1296 m_args.b->getUsedFuncs(dst); 1297 m_args.c->getUsedFuncs(dst); 1298 m_args.d->getUsedFuncs(dst); 1299 } 1300 1301 const ApplyFunc& m_func; 1302 ArgExprs m_args; 1303}; 1304 1305template <typename Sig> 1306ExprP<typename Sig::Ret> createApply (const Func<Sig>& func, 1307 const typename Func<Sig>::ArgExprs& args) 1308{ 1309 return exprP(new Apply<Sig>(func, args)); 1310} 1311 1312template <typename Sig> 1313ExprP<typename Sig::Ret> createApply ( 1314 const Func<Sig>& func, 1315 const ExprP<typename Sig::Arg0>& arg0 = voidP(), 1316 const ExprP<typename Sig::Arg1>& arg1 = voidP(), 1317 const ExprP<typename Sig::Arg2>& arg2 = voidP(), 1318 const ExprP<typename Sig::Arg3>& arg3 = voidP()) 1319{ 1320 return exprP(new Apply<Sig>(func, arg0, arg1, arg2, arg3)); 1321} 1322 1323template <typename Sig> 1324ExprP<typename Sig::Ret> Func<Sig>::operator() (const ExprP<typename Sig::Arg0>& arg0, 1325 const ExprP<typename Sig::Arg1>& arg1, 1326 const ExprP<typename Sig::Arg2>& arg2, 1327 const ExprP<typename Sig::Arg3>& arg3) const 1328{ 1329 return createApply(*this, arg0, arg1, arg2, arg3); 1330} 1331 1332template <typename F> 1333ExprP<typename F::Ret> app (const ExprP<typename F::Arg0>& arg0 = voidP(), 1334 const ExprP<typename F::Arg1>& arg1 = voidP(), 1335 const ExprP<typename F::Arg2>& arg2 = voidP(), 1336 const ExprP<typename F::Arg3>& arg3 = voidP()) 1337{ 1338 return createApply(instance<F>(), arg0, arg1, arg2, arg3); 1339} 1340 1341template <typename F> 1342typename F::IRet call (const EvalContext& ctx, 1343 const typename F::IArg0& arg0 = Void(), 1344 const typename F::IArg1& arg1 = Void(), 1345 const typename F::IArg2& arg2 = Void(), 1346 const typename F::IArg3& arg3 = Void()) 1347{ 1348 return instance<F>().apply(ctx, arg0, arg1, arg2, arg3); 1349} 1350 1351template <typename Sig> 1352class ApplyVar : public Apply<Sig> 1353{ 1354public: 1355 typedef typename Sig::Ret Ret; 1356 typedef typename Sig::Arg0 Arg0; 1357 typedef typename Sig::Arg1 Arg1; 1358 typedef typename Sig::Arg2 Arg2; 1359 typedef typename Sig::Arg3 Arg3; 1360 typedef typename Expr<Ret>::Val Val; 1361 typedef typename Expr<Ret>::IVal IVal; 1362 typedef Func<Sig> ApplyFunc; 1363 typedef typename ApplyFunc::ArgExprs ArgExprs; 1364 1365 ApplyVar (const ApplyFunc& func, 1366 const VariableP<Arg0>& arg0, 1367 const VariableP<Arg1>& arg1, 1368 const VariableP<Arg2>& arg2, 1369 const VariableP<Arg3>& arg3) 1370 : Apply<Sig> (func, arg0, arg1, arg2, arg3) {} 1371protected: 1372 IVal doEvaluate (const EvalContext& ctx) const 1373 { 1374 const Variable<Arg0>& var0 = static_cast<const Variable<Arg0>&>(*this->m_args.a); 1375 const Variable<Arg1>& var1 = static_cast<const Variable<Arg1>&>(*this->m_args.b); 1376 const Variable<Arg2>& var2 = static_cast<const Variable<Arg2>&>(*this->m_args.c); 1377 const Variable<Arg3>& var3 = static_cast<const Variable<Arg3>&>(*this->m_args.d); 1378 return this->m_func.apply(ctx, 1379 ctx.env.lookup(var0), ctx.env.lookup(var1), 1380 ctx.env.lookup(var2), ctx.env.lookup(var3)); 1381 } 1382}; 1383 1384template <typename Sig> 1385ExprP<typename Sig::Ret> applyVar (const Func<Sig>& func, 1386 const VariableP<typename Sig::Arg0>& arg0, 1387 const VariableP<typename Sig::Arg1>& arg1, 1388 const VariableP<typename Sig::Arg2>& arg2, 1389 const VariableP<typename Sig::Arg3>& arg3) 1390{ 1391 return exprP(new ApplyVar<Sig>(func, arg0, arg1, arg2, arg3)); 1392} 1393 1394template <typename Sig_> 1395class DerivedFunc : public Func<Sig_> 1396{ 1397public: 1398 typedef typename DerivedFunc::ArgExprs ArgExprs; 1399 typedef typename DerivedFunc::IRet IRet; 1400 typedef typename DerivedFunc::IArgs IArgs; 1401 typedef typename DerivedFunc::Ret Ret; 1402 typedef typename DerivedFunc::Arg0 Arg0; 1403 typedef typename DerivedFunc::Arg1 Arg1; 1404 typedef typename DerivedFunc::Arg2 Arg2; 1405 typedef typename DerivedFunc::Arg3 Arg3; 1406 typedef typename DerivedFunc::IArg0 IArg0; 1407 typedef typename DerivedFunc::IArg1 IArg1; 1408 typedef typename DerivedFunc::IArg2 IArg2; 1409 typedef typename DerivedFunc::IArg3 IArg3; 1410 1411protected: 1412 void doPrintDefinition (ostream& os) const 1413 { 1414 const ParamNames& paramNames = this->getParamNames(); 1415 1416 initialize(); 1417 1418 os << dataTypeNameOf<Ret>() << " " << this->getName() 1419 << "("; 1420 if (isTypeValid<Arg0>()) 1421 os << dataTypeNameOf<Arg0>() << " " << paramNames.a; 1422 if (isTypeValid<Arg1>()) 1423 os << ", " << dataTypeNameOf<Arg1>() << " " << paramNames.b; 1424 if (isTypeValid<Arg2>()) 1425 os << ", " << dataTypeNameOf<Arg2>() << " " << paramNames.c; 1426 if (isTypeValid<Arg3>()) 1427 os << ", " << dataTypeNameOf<Arg3>() << " " << paramNames.d; 1428 os << ")\n{\n"; 1429 1430 for (size_t ndx = 0; ndx < m_body.size(); ++ndx) 1431 os << *m_body[ndx]; 1432 os << "return " << *m_ret << ";\n"; 1433 os << "}\n"; 1434 } 1435 1436 IRet doApply (const EvalContext& ctx, 1437 const IArgs& args) const 1438 { 1439 Environment funEnv; 1440 IArgs& mutArgs = const_cast<IArgs&>(args); 1441 IRet ret; 1442 1443 initialize(); 1444 1445 funEnv.bind(*m_var0, args.a); 1446 funEnv.bind(*m_var1, args.b); 1447 funEnv.bind(*m_var2, args.c); 1448 funEnv.bind(*m_var3, args.d); 1449 1450 { 1451 EvalContext funCtx(ctx.format, ctx.floatPrecision, funEnv, ctx.callDepth); 1452 1453 for (size_t ndx = 0; ndx < m_body.size(); ++ndx) 1454 m_body[ndx]->execute(funCtx); 1455 1456 ret = m_ret->evaluate(funCtx); 1457 } 1458 1459 // \todo [lauri] Store references instead of values in environment 1460 const_cast<IArg0&>(mutArgs.a) = funEnv.lookup(*m_var0); 1461 const_cast<IArg1&>(mutArgs.b) = funEnv.lookup(*m_var1); 1462 const_cast<IArg2&>(mutArgs.c) = funEnv.lookup(*m_var2); 1463 const_cast<IArg3&>(mutArgs.d) = funEnv.lookup(*m_var3); 1464 1465 return ret; 1466 } 1467 1468 void doGetUsedFuncs (FuncSet& dst) const 1469 { 1470 initialize(); 1471 if (dst.insert(this).second) 1472 { 1473 for (size_t ndx = 0; ndx < m_body.size(); ++ndx) 1474 m_body[ndx]->getUsedFuncs(dst); 1475 m_ret->getUsedFuncs(dst); 1476 } 1477 } 1478 1479 virtual ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args_) const = 0; 1480 1481 // These are transparently initialized when first needed. They cannot be 1482 // initialized in the constructor because they depend on the doExpand 1483 // method of the subclass. 1484 1485 mutable VariableP<Arg0> m_var0; 1486 mutable VariableP<Arg1> m_var1; 1487 mutable VariableP<Arg2> m_var2; 1488 mutable VariableP<Arg3> m_var3; 1489 mutable vector<StatementP> m_body; 1490 mutable ExprP<Ret> m_ret; 1491 1492private: 1493 1494 void initialize (void) const 1495 { 1496 if (!m_ret) 1497 { 1498 const ParamNames& paramNames = this->getParamNames(); 1499 Counter symCounter; 1500 ExpandContext ctx (symCounter); 1501 ArgExprs args; 1502 1503 args.a = m_var0 = variable<Arg0>(paramNames.a); 1504 args.b = m_var1 = variable<Arg1>(paramNames.b); 1505 args.c = m_var2 = variable<Arg2>(paramNames.c); 1506 args.d = m_var3 = variable<Arg3>(paramNames.d); 1507 1508 m_ret = this->doExpand(ctx, args); 1509 m_body = ctx.getStatements(); 1510 } 1511 } 1512}; 1513 1514template <typename Sig> 1515class PrimitiveFunc : public Func<Sig> 1516{ 1517public: 1518 typedef typename PrimitiveFunc::Ret Ret; 1519 typedef typename PrimitiveFunc::ArgExprs ArgExprs; 1520 1521protected: 1522 void doPrintDefinition (ostream&) const {} 1523 void doGetUsedFuncs (FuncSet&) const {} 1524}; 1525 1526template <typename T> 1527class Cond : public PrimitiveFunc<Signature<T, bool, T, T> > 1528{ 1529public: 1530 typedef typename Cond::IArgs IArgs; 1531 typedef typename Cond::IRet IRet; 1532 1533 string getName (void) const 1534 { 1535 return "_cond"; 1536 } 1537 1538protected: 1539 1540 void doPrint (ostream& os, const BaseArgExprs& args) const 1541 { 1542 os << "(" << *args[0] << " ? " << *args[1] << " : " << *args[2] << ")"; 1543 } 1544 1545 IRet doApply (const EvalContext&, const IArgs& iargs)const 1546 { 1547 IRet ret; 1548 1549 if (iargs.a.contains(true)) 1550 ret = unionIVal<T>(ret, iargs.b); 1551 1552 if (iargs.a.contains(false)) 1553 ret = unionIVal<T>(ret, iargs.c); 1554 1555 return ret; 1556 } 1557}; 1558 1559template <typename T> 1560class CompareOperator : public PrimitiveFunc<Signature<bool, T, T> > 1561{ 1562public: 1563 typedef typename CompareOperator::IArgs IArgs; 1564 typedef typename CompareOperator::IArg0 IArg0; 1565 typedef typename CompareOperator::IArg1 IArg1; 1566 typedef typename CompareOperator::IRet IRet; 1567 1568protected: 1569 void doPrint (ostream& os, const BaseArgExprs& args) const 1570 { 1571 os << "(" << *args[0] << getSymbol() << *args[1] << ")"; 1572 } 1573 1574 Interval doApply (const EvalContext&, const IArgs& iargs) const 1575 { 1576 const IArg0& arg0 = iargs.a; 1577 const IArg1& arg1 = iargs.b; 1578 IRet ret; 1579 1580 if (canSucceed(arg0, arg1)) 1581 ret |= true; 1582 if (canFail(arg0, arg1)) 1583 ret |= false; 1584 1585 return ret; 1586 } 1587 1588 virtual string getSymbol (void) const = 0; 1589 virtual bool canSucceed (const IArg0&, const IArg1&) const = 0; 1590 virtual bool canFail (const IArg0&, const IArg1&) const = 0; 1591}; 1592 1593template <typename T> 1594class LessThan : public CompareOperator<T> 1595{ 1596public: 1597 string getName (void) const { return "lessThan"; } 1598 1599protected: 1600 string getSymbol (void) const { return "<"; } 1601 1602 bool canSucceed (const Interval& a, const Interval& b) const 1603 { 1604 return (a.lo() < b.hi()); 1605 } 1606 1607 bool canFail (const Interval& a, const Interval& b) const 1608 { 1609 return !(a.hi() < b.lo()); 1610 } 1611}; 1612 1613template <typename T> 1614ExprP<bool> operator< (const ExprP<T>& a, const ExprP<T>& b) 1615{ 1616 return app<LessThan<T> >(a, b); 1617} 1618 1619template <typename T> 1620ExprP<T> cond (const ExprP<bool>& test, 1621 const ExprP<T>& consequent, 1622 const ExprP<T>& alternative) 1623{ 1624 return app<Cond<T> >(test, consequent, alternative); 1625} 1626 1627/*--------------------------------------------------------------------*//*! 1628 * 1629 * @} 1630 * 1631 *//*--------------------------------------------------------------------*/ 1632 1633class FloatFunc1 : public PrimitiveFunc<Signature<float, float> > 1634{ 1635protected: 1636 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 1637 { 1638 return this->applyMonotone(ctx, iargs.a); 1639 } 1640 1641 Interval applyMonotone (const EvalContext& ctx, const Interval& iarg0) const 1642 { 1643 Interval ret; 1644 1645 TCU_INTERVAL_APPLY_MONOTONE1(ret, arg0, iarg0, val, 1646 TCU_SET_INTERVAL(val, point, 1647 point = this->applyPoint(ctx, arg0))); 1648 1649 ret |= innerExtrema(ctx, iarg0); 1650 ret &= (this->getCodomain() | TCU_NAN); 1651 1652 return ctx.format.convert(ret); 1653 } 1654 1655 virtual Interval innerExtrema (const EvalContext&, const Interval&) const 1656 { 1657 return Interval(); // empty interval, i.e. no extrema 1658 } 1659 1660 virtual Interval applyPoint (const EvalContext& ctx, double arg0) const 1661 { 1662 const double exact = this->applyExact(arg0); 1663 const double prec = this->precision(ctx, exact, arg0); 1664 1665 return exact + Interval(-prec, prec); 1666 } 1667 1668 virtual double applyExact (double) const 1669 { 1670 TCU_THROW(InternalError, "Cannot apply"); 1671 } 1672 1673 virtual Interval getCodomain (void) const 1674 { 1675 return Interval::unbounded(true); 1676 } 1677 1678 virtual double precision (const EvalContext& ctx, double, double) const = 0; 1679}; 1680 1681class CFloatFunc1 : public FloatFunc1 1682{ 1683public: 1684 CFloatFunc1 (const string& name, DoubleFunc1& func) 1685 : m_name(name), m_func(func) {} 1686 1687 string getName (void) const { return m_name; } 1688 1689protected: 1690 double applyExact (double x) const { return m_func(x); } 1691 1692 const string m_name; 1693 DoubleFunc1& m_func; 1694}; 1695 1696class FloatFunc2 : public PrimitiveFunc<Signature<float, float, float> > 1697{ 1698protected: 1699 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 1700 { 1701 return this->applyMonotone(ctx, iargs.a, iargs.b); 1702 } 1703 1704 Interval applyMonotone (const EvalContext& ctx, 1705 const Interval& xi, 1706 const Interval& yi) const 1707 { 1708 Interval reti; 1709 1710 TCU_INTERVAL_APPLY_MONOTONE2(reti, x, xi, y, yi, ret, 1711 TCU_SET_INTERVAL(ret, point, 1712 point = this->applyPoint(ctx, x, y))); 1713 reti |= innerExtrema(ctx, xi, yi); 1714 reti &= (this->getCodomain() | TCU_NAN); 1715 1716 return ctx.format.convert(reti); 1717 } 1718 1719 virtual Interval innerExtrema (const EvalContext&, 1720 const Interval&, 1721 const Interval&) const 1722 { 1723 return Interval(); // empty interval, i.e. no extrema 1724 } 1725 1726 virtual Interval applyPoint (const EvalContext& ctx, 1727 double x, 1728 double y) const 1729 { 1730 const double exact = this->applyExact(x, y); 1731 const double prec = this->precision(ctx, exact, x, y); 1732 1733 return exact + Interval(-prec, prec); 1734 } 1735 1736 virtual double applyExact (double, double) const 1737 { 1738 TCU_THROW(InternalError, "Cannot apply"); 1739 } 1740 1741 virtual Interval getCodomain (void) const 1742 { 1743 return Interval::unbounded(true); 1744 } 1745 1746 virtual double precision (const EvalContext& ctx, 1747 double ret, 1748 double x, 1749 double y) const = 0; 1750}; 1751 1752class CFloatFunc2 : public FloatFunc2 1753{ 1754public: 1755 CFloatFunc2 (const string& name, 1756 DoubleFunc2& func) 1757 : m_name(name) 1758 , m_func(func) 1759 { 1760 } 1761 1762 string getName (void) const { return m_name; } 1763 1764protected: 1765 double applyExact (double x, double y) const { return m_func(x, y); } 1766 1767 const string m_name; 1768 DoubleFunc2& m_func; 1769}; 1770 1771class InfixOperator : public FloatFunc2 1772{ 1773protected: 1774 virtual string getSymbol (void) const = 0; 1775 1776 void doPrint (ostream& os, const BaseArgExprs& args) const 1777 { 1778 os << "(" << *args[0] << " " << getSymbol() << " " << *args[1] << ")"; 1779 } 1780 1781 Interval applyPoint (const EvalContext& ctx, 1782 double x, 1783 double y) const 1784 { 1785 const double exact = this->applyExact(x, y); 1786 1787 // Allow either representable number on both sides of the exact value, 1788 // but require exactly representable values to be preserved. 1789 return ctx.format.roundOut(exact, !deIsInf(x) && !deIsInf(y)); 1790 } 1791 1792 double precision (const EvalContext&, double, double, double) const 1793 { 1794 return 0.0; 1795 } 1796}; 1797 1798class FloatFunc3 : public PrimitiveFunc<Signature<float, float, float, float> > 1799{ 1800protected: 1801 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 1802 { 1803 return this->applyMonotone(ctx, iargs.a, iargs.b, iargs.c); 1804 } 1805 1806 Interval applyMonotone (const EvalContext& ctx, 1807 const Interval& xi, 1808 const Interval& yi, 1809 const Interval& zi) const 1810 { 1811 Interval reti; 1812 TCU_INTERVAL_APPLY_MONOTONE3(reti, x, xi, y, yi, z, zi, ret, 1813 TCU_SET_INTERVAL(ret, point, 1814 point = this->applyPoint(ctx, x, y, z))); 1815 return ctx.format.convert(reti); 1816 } 1817 1818 virtual Interval applyPoint (const EvalContext& ctx, 1819 double x, 1820 double y, 1821 double z) const 1822 { 1823 const double exact = this->applyExact(x, y, z); 1824 const double prec = this->precision(ctx, exact, x, y, z); 1825 return exact + Interval(-prec, prec); 1826 } 1827 1828 virtual double applyExact (double, double, double) const 1829 { 1830 TCU_THROW(InternalError, "Cannot apply"); 1831 } 1832 1833 virtual double precision (const EvalContext& ctx, 1834 double result, 1835 double x, 1836 double y, 1837 double z) const = 0; 1838}; 1839 1840// We define syntactic sugar functions for expression constructors. Since 1841// these have the same names as ordinary mathematical operations (sin, log 1842// etc.), it's better to give them a dedicated namespace. 1843namespace Functions 1844{ 1845 1846using namespace tcu; 1847 1848class Add : public InfixOperator 1849{ 1850public: 1851 string getName (void) const { return "add"; } 1852 string getSymbol (void) const { return "+"; } 1853 1854 Interval doApply (const EvalContext& ctx, 1855 const IArgs& iargs) const 1856 { 1857 // Fast-path for common case 1858 if (iargs.a.isOrdinary() && iargs.b.isOrdinary()) 1859 { 1860 Interval ret; 1861 TCU_SET_INTERVAL_BOUNDS(ret, sum, 1862 sum = iargs.a.lo() + iargs.b.lo(), 1863 sum = iargs.a.hi() + iargs.b.hi()); 1864 return ctx.format.convert(ctx.format.roundOut(ret, true)); 1865 } 1866 return this->applyMonotone(ctx, iargs.a, iargs.b); 1867 } 1868 1869protected: 1870 double applyExact (double x, double y) const { return x + y; } 1871}; 1872 1873class Mul : public InfixOperator 1874{ 1875public: 1876 string getName (void) const { return "mul"; } 1877 string getSymbol (void) const { return "*"; } 1878 1879 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 1880 { 1881 Interval a = iargs.a; 1882 Interval b = iargs.b; 1883 1884 // Fast-path for common case 1885 if (a.isOrdinary() && b.isOrdinary()) 1886 { 1887 Interval ret; 1888 if (a.hi() < 0) 1889 { 1890 a = -a; 1891 b = -b; 1892 } 1893 if (a.lo() >= 0 && b.lo() >= 0) 1894 { 1895 TCU_SET_INTERVAL_BOUNDS(ret, prod, 1896 prod = iargs.a.lo() * iargs.b.lo(), 1897 prod = iargs.a.hi() * iargs.b.hi()); 1898 return ctx.format.convert(ctx.format.roundOut(ret, true)); 1899 } 1900 if (a.lo() >= 0 && b.hi() <= 0) 1901 { 1902 TCU_SET_INTERVAL_BOUNDS(ret, prod, 1903 prod = iargs.a.hi() * iargs.b.lo(), 1904 prod = iargs.a.lo() * iargs.b.hi()); 1905 return ctx.format.convert(ctx.format.roundOut(ret, true)); 1906 } 1907 } 1908 return this->applyMonotone(ctx, iargs.a, iargs.b); 1909 } 1910 1911protected: 1912 double applyExact (double x, double y) const { return x * y; } 1913 1914 Interval innerExtrema(const EvalContext&, const Interval& xi, const Interval& yi) const 1915 { 1916 if (((xi.contains(-TCU_INFINITY) || xi.contains(TCU_INFINITY)) && yi.contains(0.0)) || 1917 ((yi.contains(-TCU_INFINITY) || yi.contains(TCU_INFINITY)) && xi.contains(0.0))) 1918 return Interval(TCU_NAN); 1919 1920 return Interval(); 1921 } 1922}; 1923 1924class Sub : public InfixOperator 1925{ 1926public: 1927 string getName (void) const { return "sub"; } 1928 string getSymbol (void) const { return "-"; } 1929 1930 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 1931 { 1932 // Fast-path for common case 1933 if (iargs.a.isOrdinary() && iargs.b.isOrdinary()) 1934 { 1935 Interval ret; 1936 1937 TCU_SET_INTERVAL_BOUNDS(ret, diff, 1938 diff = iargs.a.lo() - iargs.b.hi(), 1939 diff = iargs.a.hi() - iargs.b.lo()); 1940 return ctx.format.convert(ctx.format.roundOut(ret, true)); 1941 1942 } 1943 else 1944 { 1945 return this->applyMonotone(ctx, iargs.a, iargs.b); 1946 } 1947 } 1948 1949protected: 1950 double applyExact (double x, double y) const { return x - y; } 1951}; 1952 1953class Negate : public FloatFunc1 1954{ 1955public: 1956 string getName (void) const { return "_negate"; } 1957 void doPrint (ostream& os, const BaseArgExprs& args) const { os << "-" << *args[0]; } 1958 1959protected: 1960 double precision (const EvalContext&, double, double) const { return 0.0; } 1961 double applyExact (double x) const { return -x; } 1962}; 1963 1964class Div : public InfixOperator 1965{ 1966public: 1967 string getName (void) const { return "div"; } 1968 1969protected: 1970 string getSymbol (void) const { return "/"; } 1971 1972 Interval innerExtrema (const EvalContext&, 1973 const Interval& nom, 1974 const Interval& den) const 1975 { 1976 Interval ret; 1977 1978 if (den.contains(0.0)) 1979 { 1980 if (nom.contains(0.0)) 1981 ret |= TCU_NAN; 1982 1983 if (nom.lo() < 0.0 || nom.hi() > 0.0) 1984 ret |= Interval::unbounded(); 1985 } 1986 1987 return ret; 1988 } 1989 1990protected: 1991 1992 double applyExact (double x, double y) const { return x / y; } 1993 1994 Interval applyPoint (const EvalContext& ctx, double x, double y) const 1995 { 1996 return FloatFunc2::applyPoint(ctx, x, y); 1997 } 1998 1999 double precision (const EvalContext& ctx, double ret, double, double den) const 2000 { 2001 const FloatFormat& fmt = ctx.format; 2002 2003 // \todo [2014-03-05 lauri] Check that the limits in GLSL 3.10 are actually correct. 2004 // For now, we assume that division's precision is 2.5 ULP when the value is within 2005 // [2^MINEXP, 2^MAXEXP-1] 2006 2007 if (den == 0.0) 2008 return 0.0; // Result must be exactly inf 2009 else if (de::inBounds(deAbs(den), 2010 deLdExp(1.0, fmt.getMinExp()), 2011 deLdExp(1.0, fmt.getMaxExp() - 1))) 2012 return fmt.ulp(ret, 2.5); 2013 else 2014 return TCU_INFINITY; // Can be any number, but must be a number. 2015 } 2016}; 2017 2018class InverseSqrt : public FloatFunc1 2019{ 2020public: 2021 string getName (void) const { return "inversesqrt"; } 2022 2023protected: 2024 double applyExact (double x) const { return 1.0 / deSqrt(x); } 2025 2026 double precision (const EvalContext& ctx, double ret, double x) const 2027 { 2028 return x <= 0 ? TCU_NAN : ctx.format.ulp(ret, 2.0); 2029 } 2030 2031 Interval getCodomain (void) const 2032 { 2033 return Interval(0.0, TCU_INFINITY); 2034 } 2035}; 2036 2037class ExpFunc : public CFloatFunc1 2038{ 2039public: 2040 ExpFunc (const string& name, DoubleFunc1& func) 2041 : CFloatFunc1(name, func) {} 2042protected: 2043 double precision (const EvalContext& ctx, double ret, double x) const 2044 { 2045 switch (ctx.floatPrecision) 2046 { 2047 case glu::PRECISION_HIGHP: 2048 return ctx.format.ulp(ret, 3.0 + 2.0 * deAbs(x)); 2049 case glu::PRECISION_MEDIUMP: 2050 return ctx.format.ulp(ret, 2.0 + 2.0 * deAbs(x)); 2051 case glu::PRECISION_LOWP: 2052 return ctx.format.ulp(ret, 2.0); 2053 default: 2054 DE_ASSERT(!"Impossible"); 2055 } 2056 return 0; 2057 } 2058 2059 Interval getCodomain (void) const 2060 { 2061 return Interval(0.0, TCU_INFINITY); 2062 } 2063}; 2064 2065class Exp2 : public ExpFunc { public: Exp2 (void) : ExpFunc("exp2", deExp2) {} }; 2066class Exp : public ExpFunc { public: Exp (void) : ExpFunc("exp", deExp) {} }; 2067 2068ExprP<float> exp2 (const ExprP<float>& x) { return app<Exp2>(x); } 2069ExprP<float> exp (const ExprP<float>& x) { return app<Exp>(x); } 2070 2071class LogFunc : public CFloatFunc1 2072{ 2073public: 2074 LogFunc (const string& name, DoubleFunc1& func) 2075 : CFloatFunc1(name, func) {} 2076 2077protected: 2078 double precision (const EvalContext& ctx, double ret, double x) const 2079 { 2080 if (x <= 0) 2081 return TCU_NAN; 2082 2083 switch (ctx.floatPrecision) 2084 { 2085 case glu::PRECISION_HIGHP: 2086 return (0.5 <= x && x <= 2.0) ? deLdExp(1.0, -21) : ctx.format.ulp(ret, 3.0); 2087 case glu::PRECISION_MEDIUMP: 2088 return (0.5 <= x && x <= 2.0) ? deLdExp(1.0, -7) : ctx.format.ulp(ret, 2.0); 2089 case glu::PRECISION_LOWP: 2090 return ctx.format.ulp(ret, 2.0); 2091 default: 2092 DE_ASSERT(!"Impossible"); 2093 } 2094 2095 return 0; 2096 } 2097}; 2098 2099class Log2 : public LogFunc { public: Log2 (void) : LogFunc("log2", deLog2) {} }; 2100class Log : public LogFunc { public: Log (void) : LogFunc("log", deLog) {} }; 2101 2102ExprP<float> log2 (const ExprP<float>& x) { return app<Log2>(x); } 2103ExprP<float> log (const ExprP<float>& x) { return app<Log>(x); } 2104 2105#define DEFINE_CONSTRUCTOR1(CLASS, TRET, NAME, T0) \ 2106ExprP<TRET> NAME (const ExprP<T0>& arg0) { return app<CLASS>(arg0); } 2107 2108#define DEFINE_DERIVED1(CLASS, TRET, NAME, T0, ARG0, EXPANSION) \ 2109class CLASS : public DerivedFunc<Signature<TRET, T0> > \ 2110{ \ 2111public: \ 2112 string getName (void) const { return #NAME; } \ 2113 \ 2114protected: \ 2115 ExprP<TRET> doExpand (ExpandContext&, \ 2116 const CLASS::ArgExprs& args_) const \ 2117 { \ 2118 const ExprP<float>& ARG0 = args_.a; \ 2119 return EXPANSION; \ 2120 } \ 2121}; \ 2122DEFINE_CONSTRUCTOR1(CLASS, TRET, NAME, T0) 2123 2124#define DEFINE_DERIVED_FLOAT1(CLASS, NAME, ARG0, EXPANSION) \ 2125 DEFINE_DERIVED1(CLASS, float, NAME, float, ARG0, EXPANSION) 2126 2127#define DEFINE_CONSTRUCTOR2(CLASS, TRET, NAME, T0, T1) \ 2128ExprP<TRET> NAME (const ExprP<T0>& arg0, const ExprP<T1>& arg1) \ 2129{ \ 2130 return app<CLASS>(arg0, arg1); \ 2131} 2132 2133#define DEFINE_DERIVED2(CLASS, TRET, NAME, T0, Arg0, T1, Arg1, EXPANSION) \ 2134class CLASS : public DerivedFunc<Signature<TRET, T0, T1> > \ 2135{ \ 2136public: \ 2137 string getName (void) const { return #NAME; } \ 2138 \ 2139protected: \ 2140 ExprP<TRET> doExpand (ExpandContext&, const ArgExprs& args_) const \ 2141 { \ 2142 const ExprP<T0>& Arg0 = args_.a; \ 2143 const ExprP<T1>& Arg1 = args_.b; \ 2144 return EXPANSION; \ 2145 } \ 2146}; \ 2147DEFINE_CONSTRUCTOR2(CLASS, TRET, NAME, T0, T1) 2148 2149#define DEFINE_DERIVED_FLOAT2(CLASS, NAME, Arg0, Arg1, EXPANSION) \ 2150 DEFINE_DERIVED2(CLASS, float, NAME, float, Arg0, float, Arg1, EXPANSION) 2151 2152#define DEFINE_CONSTRUCTOR3(CLASS, TRET, NAME, T0, T1, T2) \ 2153ExprP<TRET> NAME (const ExprP<T0>& arg0, const ExprP<T1>& arg1, const ExprP<T2>& arg2) \ 2154{ \ 2155 return app<CLASS>(arg0, arg1, arg2); \ 2156} 2157 2158#define DEFINE_DERIVED3(CLASS, TRET, NAME, T0, ARG0, T1, ARG1, T2, ARG2, EXPANSION) \ 2159class CLASS : public DerivedFunc<Signature<TRET, T0, T1, T2> > \ 2160{ \ 2161public: \ 2162 string getName (void) const { return #NAME; } \ 2163 \ 2164protected: \ 2165 ExprP<TRET> doExpand (ExpandContext&, const ArgExprs& args_) const \ 2166 { \ 2167 const ExprP<T0>& ARG0 = args_.a; \ 2168 const ExprP<T1>& ARG1 = args_.b; \ 2169 const ExprP<T2>& ARG2 = args_.c; \ 2170 return EXPANSION; \ 2171 } \ 2172}; \ 2173DEFINE_CONSTRUCTOR3(CLASS, TRET, NAME, T0, T1, T2) 2174 2175#define DEFINE_DERIVED_FLOAT3(CLASS, NAME, ARG0, ARG1, ARG2, EXPANSION) \ 2176 DEFINE_DERIVED3(CLASS, float, NAME, float, ARG0, float, ARG1, float, ARG2, EXPANSION) 2177 2178#define DEFINE_CONSTRUCTOR4(CLASS, TRET, NAME, T0, T1, T2, T3) \ 2179ExprP<TRET> NAME (const ExprP<T0>& arg0, const ExprP<T1>& arg1, \ 2180 const ExprP<T2>& arg2, const ExprP<T3>& arg3) \ 2181{ \ 2182 return app<CLASS>(arg0, arg1, arg2, arg3); \ 2183} 2184 2185DEFINE_DERIVED_FLOAT1(Sqrt, sqrt, x, constant(1.0f) / app<InverseSqrt>(x)); 2186DEFINE_DERIVED_FLOAT2(Pow, pow, x, y, exp2(y * log2(x))); 2187DEFINE_DERIVED_FLOAT1(Radians, radians, d, (constant(DE_PI) / constant(180.0f)) * d); 2188DEFINE_DERIVED_FLOAT1(Degrees, degrees, r, (constant(180.0f) / constant(DE_PI)) * r); 2189 2190class TrigFunc : public CFloatFunc1 2191{ 2192public: 2193 TrigFunc (const string& name, 2194 DoubleFunc1& func, 2195 const Interval& loEx, 2196 const Interval& hiEx) 2197 : CFloatFunc1 (name, func) 2198 , m_loExtremum (loEx) 2199 , m_hiExtremum (hiEx) {} 2200 2201protected: 2202 Interval innerExtrema (const EvalContext&, const Interval& angle) const 2203 { 2204 const double lo = angle.lo(); 2205 const double hi = angle.hi(); 2206 const int loSlope = doGetSlope(lo); 2207 const int hiSlope = doGetSlope(hi); 2208 2209 // Detect the high and low values the function can take between the 2210 // interval endpoints. 2211 if (angle.length() >= 2.0 * DE_PI_DOUBLE) 2212 { 2213 // The interval is longer than a full cycle, so it must get all possible values. 2214 return m_hiExtremum | m_loExtremum; 2215 } 2216 else if (loSlope == 1 && hiSlope == -1) 2217 { 2218 // The slope can change from positive to negative only at the maximum value. 2219 return m_hiExtremum; 2220 } 2221 else if (loSlope == -1 && hiSlope == 1) 2222 { 2223 // The slope can change from negative to positive only at the maximum value. 2224 return m_loExtremum; 2225 } 2226 else if (loSlope == hiSlope && 2227 deIntSign(applyExact(hi) - applyExact(lo)) * loSlope == -1) 2228 { 2229 // The slope has changed twice between the endpoints, so both extrema are included. 2230 return m_hiExtremum | m_loExtremum; 2231 } 2232 2233 return Interval(); 2234 } 2235 2236 Interval getCodomain (void) const 2237 { 2238 // Ensure that result is always within [-1, 1], or NaN (for +-inf) 2239 return Interval(-1.0, 1.0) | TCU_NAN; 2240 } 2241 2242 double precision (const EvalContext& ctx, double ret, double arg) const 2243 { 2244 if (ctx.floatPrecision == glu::PRECISION_HIGHP) 2245 { 2246 // Use precision from OpenCL fast relaxed math 2247 if (-DE_PI_DOUBLE <= arg && arg <= DE_PI_DOUBLE) 2248 { 2249 return deLdExp(1.0, -11); 2250 } 2251 else 2252 { 2253 // "larger otherwise", let's pick |x| * 2^-12 , which is slightly over 2254 // 2^-11 at x == pi. 2255 return deLdExp(deAbs(arg), -12); 2256 } 2257 } 2258 else 2259 { 2260 // from OpenCL half-float extension specification 2261 return ctx.format.ulp(ret, 2.0); 2262 } 2263 } 2264 2265 virtual int doGetSlope (double angle) const = 0; 2266 2267 Interval m_loExtremum; 2268 Interval m_hiExtremum; 2269}; 2270 2271class Sin : public TrigFunc 2272{ 2273public: 2274 Sin (void) : TrigFunc("sin", deSin, -1.0, 1.0) {} 2275 2276protected: 2277 int doGetSlope (double angle) const { return deIntSign(deCos(angle)); } 2278}; 2279 2280ExprP<float> sin (const ExprP<float>& x) { return app<Sin>(x); } 2281 2282class Cos : public TrigFunc 2283{ 2284public: 2285 Cos (void) : TrigFunc("cos", deCos, -1.0, 1.0) {} 2286 2287protected: 2288 int doGetSlope (double angle) const { return -deIntSign(deSin(angle)); } 2289}; 2290 2291ExprP<float> cos (const ExprP<float>& x) { return app<Cos>(x); } 2292 2293DEFINE_DERIVED_FLOAT1(Tan, tan, x, sin(x) * (constant(1.0f) / cos(x))); 2294 2295class ArcTrigFunc : public CFloatFunc1 2296{ 2297public: 2298 ArcTrigFunc (const string& name, 2299 DoubleFunc1& func, 2300 double precisionULPs, 2301 const Interval& domain, 2302 const Interval& codomain) 2303 : CFloatFunc1 (name, func) 2304 , m_precision (precisionULPs) 2305 , m_domain (domain) 2306 , m_codomain (codomain) {} 2307 2308protected: 2309 double precision (const EvalContext& ctx, double ret, double x) const 2310 { 2311 if (!m_domain.contains(x)) 2312 return TCU_NAN; 2313 2314 if (ctx.floatPrecision == glu::PRECISION_HIGHP) 2315 { 2316 // Use OpenCL's precision 2317 return ctx.format.ulp(ret, m_precision); 2318 } 2319 else 2320 { 2321 // Use OpenCL half-float spec 2322 return ctx.format.ulp(ret, 2.0); 2323 } 2324 } 2325 2326 // We could implement getCodomain with m_codomain, but choose not to, 2327 // because it seems too strict with trascendental constants like pi. 2328 2329 const double m_precision; 2330 const Interval m_domain; 2331 const Interval m_codomain; 2332}; 2333 2334class ASin : public ArcTrigFunc 2335{ 2336public: 2337 ASin (void) : ArcTrigFunc("asin", deAsin, 4.0, 2338 Interval(-1.0, 1.0), 2339 Interval(-DE_PI_DOUBLE * 0.5, DE_PI_DOUBLE * 0.5)) {} 2340}; 2341 2342class ACos : public ArcTrigFunc 2343{ 2344public: 2345 ACos (void) : ArcTrigFunc("acos", deAcos, 4.0, 2346 Interval(-1.0, 1.0), 2347 Interval(0.0, DE_PI_DOUBLE)) {} 2348}; 2349 2350class ATan : public ArcTrigFunc 2351{ 2352public: 2353 ATan (void) : ArcTrigFunc("atan", deAtanOver, 5.0, 2354 Interval::unbounded(), 2355 Interval(-DE_PI_DOUBLE * 0.5, DE_PI_DOUBLE * 0.5)) {} 2356}; 2357 2358class ATan2 : public CFloatFunc2 2359{ 2360public: 2361 ATan2 (void) : CFloatFunc2 ("atan", deAtan2) {} 2362 2363protected: 2364 Interval innerExtrema (const EvalContext&, 2365 const Interval& yi, 2366 const Interval& xi) const 2367 { 2368 Interval ret; 2369 2370 if (yi.contains(0.0)) 2371 { 2372 if (xi.contains(0.0)) 2373 ret |= TCU_NAN; 2374 if (xi.intersects(Interval(-TCU_INFINITY, 0.0))) 2375 ret |= Interval(-DE_PI_DOUBLE, DE_PI_DOUBLE); 2376 } 2377 2378 return ret; 2379 } 2380 2381 double precision (const EvalContext& ctx, double ret, double, double) const 2382 { 2383 if (ctx.floatPrecision == glu::PRECISION_HIGHP) 2384 return ctx.format.ulp(ret, 6.0); 2385 else 2386 return ctx.format.ulp(ret, 2.0); 2387 } 2388 2389 // Codomain could be [-pi, pi], but that would probably be too strict. 2390}; 2391 2392DEFINE_DERIVED_FLOAT1(Sinh, sinh, x, (exp(x) - exp(-x)) / constant(2.0f)); 2393DEFINE_DERIVED_FLOAT1(Cosh, cosh, x, (exp(x) + exp(-x)) / constant(2.0f)); 2394DEFINE_DERIVED_FLOAT1(Tanh, tanh, x, sinh(x) / cosh(x)); 2395 2396// These are not defined as derived forms in the GLSL ES spec, but 2397// that gives us a reasonable precision. 2398DEFINE_DERIVED_FLOAT1(ASinh, asinh, x, log(x + sqrt(x * x + constant(1.0f)))); 2399DEFINE_DERIVED_FLOAT1(ACosh, acosh, x, log(x + sqrt((x + constant(1.0f)) * 2400 (x - constant(1.0f))))); 2401DEFINE_DERIVED_FLOAT1(ATanh, atanh, x, constant(0.5f) * log((constant(1.0f) + x) / 2402 (constant(1.0f) - x))); 2403 2404template <typename T> 2405class GetComponent : public PrimitiveFunc<Signature<typename T::Element, T, int> > 2406{ 2407public: 2408 typedef typename GetComponent::IRet IRet; 2409 2410 string getName (void) const { return "_getComponent"; } 2411 2412 void print (ostream& os, 2413 const BaseArgExprs& args) const 2414 { 2415 os << *args[0] << "[" << *args[1] << "]"; 2416 } 2417 2418protected: 2419 IRet doApply (const EvalContext&, 2420 const typename GetComponent::IArgs& iargs) const 2421 { 2422 IRet ret; 2423 2424 for (int compNdx = 0; compNdx < T::SIZE; ++compNdx) 2425 { 2426 if (iargs.b.contains(compNdx)) 2427 ret = unionIVal<typename T::Element>(ret, iargs.a[compNdx]); 2428 } 2429 2430 return ret; 2431 } 2432 2433}; 2434 2435template <typename T> 2436ExprP<typename T::Element> getComponent (const ExprP<T>& container, int ndx) 2437{ 2438 DE_ASSERT(0 <= ndx && ndx < T::SIZE); 2439 return app<GetComponent<T> >(container, constant(ndx)); 2440} 2441 2442template <typename T> string vecNamePrefix (void); 2443template <> string vecNamePrefix<float> (void) { return ""; } 2444template <> string vecNamePrefix<int> (void) { return "i"; } 2445template <> string vecNamePrefix<bool> (void) { return "b"; } 2446 2447template <typename T, int Size> 2448string vecName (void) { return vecNamePrefix<T>() + "vec" + de::toString(Size); } 2449 2450template <typename T, int Size> class GenVec; 2451 2452template <typename T> 2453class GenVec<T, 1> : public DerivedFunc<Signature<T, T> > 2454{ 2455public: 2456 typedef typename GenVec<T, 1>::ArgExprs ArgExprs; 2457 2458 string getName (void) const 2459 { 2460 return "_" + vecName<T, 1>(); 2461 } 2462 2463protected: 2464 2465 ExprP<T> doExpand (ExpandContext&, const ArgExprs& args) const { return args.a; } 2466}; 2467 2468template <typename T> 2469class GenVec<T, 2> : public PrimitiveFunc<Signature<Vector<T, 2>, T, T> > 2470{ 2471public: 2472 typedef typename GenVec::IRet IRet; 2473 typedef typename GenVec::IArgs IArgs; 2474 2475 string getName (void) const 2476 { 2477 return vecName<T, 2>(); 2478 } 2479 2480protected: 2481 IRet doApply (const EvalContext&, const IArgs& iargs) const 2482 { 2483 return IRet(iargs.a, iargs.b); 2484 } 2485}; 2486 2487template <typename T> 2488class GenVec<T, 3> : public PrimitiveFunc<Signature<Vector<T, 3>, T, T, T> > 2489{ 2490public: 2491 typedef typename GenVec::IRet IRet; 2492 typedef typename GenVec::IArgs IArgs; 2493 2494 string getName (void) const 2495 { 2496 return vecName<T, 3>(); 2497 } 2498 2499protected: 2500 IRet doApply (const EvalContext&, const IArgs& iargs) const 2501 { 2502 return IRet(iargs.a, iargs.b, iargs.c); 2503 } 2504}; 2505 2506template <typename T> 2507class GenVec<T, 4> : public PrimitiveFunc<Signature<Vector<T, 4>, T, T, T, T> > 2508{ 2509public: 2510 typedef typename GenVec::IRet IRet; 2511 typedef typename GenVec::IArgs IArgs; 2512 2513 string getName (void) const { return vecName<T, 4>(); } 2514 2515protected: 2516 IRet doApply (const EvalContext&, const IArgs& iargs) const 2517 { 2518 return IRet(iargs.a, iargs.b, iargs.c, iargs.d); 2519 } 2520}; 2521 2522 2523 2524template <typename T, int Rows, int Columns> 2525class GenMat; 2526 2527template <typename T, int Rows> 2528class GenMat<T, Rows, 2> : public PrimitiveFunc< 2529 Signature<Matrix<T, Rows, 2>, Vector<T, Rows>, Vector<T, Rows> > > 2530{ 2531public: 2532 typedef typename GenMat::Ret Ret; 2533 typedef typename GenMat::IRet IRet; 2534 typedef typename GenMat::IArgs IArgs; 2535 2536 string getName (void) const 2537 { 2538 return dataTypeNameOf<Ret>(); 2539 } 2540 2541protected: 2542 2543 IRet doApply (const EvalContext&, const IArgs& iargs) const 2544 { 2545 IRet ret; 2546 ret[0] = iargs.a; 2547 ret[1] = iargs.b; 2548 return ret; 2549 } 2550}; 2551 2552template <typename T, int Rows> 2553class GenMat<T, Rows, 3> : public PrimitiveFunc< 2554 Signature<Matrix<T, Rows, 3>, Vector<T, Rows>, Vector<T, Rows>, Vector<T, Rows> > > 2555{ 2556public: 2557 typedef typename GenMat::Ret Ret; 2558 typedef typename GenMat::IRet IRet; 2559 typedef typename GenMat::IArgs IArgs; 2560 2561 string getName (void) const 2562 { 2563 return dataTypeNameOf<Ret>(); 2564 } 2565 2566protected: 2567 2568 IRet doApply (const EvalContext&, const IArgs& iargs) const 2569 { 2570 IRet ret; 2571 ret[0] = iargs.a; 2572 ret[1] = iargs.b; 2573 ret[2] = iargs.c; 2574 return ret; 2575 } 2576}; 2577 2578template <typename T, int Rows> 2579class GenMat<T, Rows, 4> : public PrimitiveFunc< 2580 Signature<Matrix<T, Rows, 4>, 2581 Vector<T, Rows>, Vector<T, Rows>, Vector<T, Rows>, Vector<T, Rows> > > 2582{ 2583public: 2584 typedef typename GenMat::Ret Ret; 2585 typedef typename GenMat::IRet IRet; 2586 typedef typename GenMat::IArgs IArgs; 2587 2588 string getName (void) const 2589 { 2590 return dataTypeNameOf<Ret>(); 2591 } 2592 2593protected: 2594 IRet doApply (const EvalContext&, const IArgs& iargs) const 2595 { 2596 IRet ret; 2597 ret[0] = iargs.a; 2598 ret[1] = iargs.b; 2599 ret[2] = iargs.c; 2600 ret[3] = iargs.d; 2601 return ret; 2602 } 2603}; 2604 2605template <typename T, int Rows> 2606ExprP<Matrix<T, Rows, 2> > mat2 (const ExprP<Vector<T, Rows> >& arg0, 2607 const ExprP<Vector<T, Rows> >& arg1) 2608{ 2609 return app<GenMat<T, Rows, 2> >(arg0, arg1); 2610} 2611 2612template <typename T, int Rows> 2613ExprP<Matrix<T, Rows, 3> > mat3 (const ExprP<Vector<T, Rows> >& arg0, 2614 const ExprP<Vector<T, Rows> >& arg1, 2615 const ExprP<Vector<T, Rows> >& arg2) 2616{ 2617 return app<GenMat<T, Rows, 3> >(arg0, arg1, arg2); 2618} 2619 2620template <typename T, int Rows> 2621ExprP<Matrix<T, Rows, 4> > mat4 (const ExprP<Vector<T, Rows> >& arg0, 2622 const ExprP<Vector<T, Rows> >& arg1, 2623 const ExprP<Vector<T, Rows> >& arg2, 2624 const ExprP<Vector<T, Rows> >& arg3) 2625{ 2626 return app<GenMat<T, Rows, 4> >(arg0, arg1, arg2, arg3); 2627} 2628 2629 2630template <int Rows, int Cols> 2631class MatNeg : public PrimitiveFunc<Signature<Matrix<float, Rows, Cols>, 2632 Matrix<float, Rows, Cols> > > 2633{ 2634public: 2635 typedef typename MatNeg::IRet IRet; 2636 typedef typename MatNeg::IArgs IArgs; 2637 2638 string getName (void) const 2639 { 2640 return "_matNeg"; 2641 } 2642 2643protected: 2644 void doPrint (ostream& os, const BaseArgExprs& args) const 2645 { 2646 os << "-(" << *args[0] << ")"; 2647 } 2648 2649 IRet doApply (const EvalContext&, const IArgs& iargs) const 2650 { 2651 IRet ret; 2652 2653 for (int col = 0; col < Cols; ++col) 2654 { 2655 for (int row = 0; row < Rows; ++row) 2656 ret[col][row] = -iargs.a[col][row]; 2657 } 2658 2659 return ret; 2660 } 2661}; 2662 2663template <typename T, typename Sig> 2664class CompWiseFunc : public PrimitiveFunc<Sig> 2665{ 2666public: 2667 typedef Func<Signature<T, T, T> > ScalarFunc; 2668 2669 string getName (void) const 2670 { 2671 return doGetScalarFunc().getName(); 2672 } 2673protected: 2674 void doPrint (ostream& os, 2675 const BaseArgExprs& args) const 2676 { 2677 doGetScalarFunc().print(os, args); 2678 } 2679 2680 virtual 2681 const ScalarFunc& doGetScalarFunc (void) const = 0; 2682}; 2683 2684template <int Rows, int Cols> 2685class CompMatFuncBase : public CompWiseFunc<float, Signature<Matrix<float, Rows, Cols>, 2686 Matrix<float, Rows, Cols>, 2687 Matrix<float, Rows, Cols> > > 2688{ 2689public: 2690 typedef typename CompMatFuncBase::IRet IRet; 2691 typedef typename CompMatFuncBase::IArgs IArgs; 2692 2693protected: 2694 2695 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 2696 { 2697 IRet ret; 2698 2699 for (int col = 0; col < Cols; ++col) 2700 { 2701 for (int row = 0; row < Rows; ++row) 2702 ret[col][row] = this->doGetScalarFunc().apply(ctx, 2703 iargs.a[col][row], 2704 iargs.b[col][row]); 2705 } 2706 2707 return ret; 2708 } 2709}; 2710 2711template <typename F, int Rows, int Cols> 2712class CompMatFunc : public CompMatFuncBase<Rows, Cols> 2713{ 2714protected: 2715 const typename CompMatFunc::ScalarFunc& doGetScalarFunc (void) const 2716 { 2717 return instance<F>(); 2718 } 2719}; 2720 2721class ScalarMatrixCompMult : public Mul 2722{ 2723public: 2724 string getName (void) const 2725 { 2726 return "matrixCompMult"; 2727 } 2728 2729 void doPrint (ostream& os, const BaseArgExprs& args) const 2730 { 2731 Func<Sig>::doPrint(os, args); 2732 } 2733}; 2734 2735template <int Rows, int Cols> 2736class MatrixCompMult : public CompMatFunc<ScalarMatrixCompMult, Rows, Cols> 2737{ 2738}; 2739 2740template <int Rows, int Cols> 2741class ScalarMatFuncBase : public CompWiseFunc<float, Signature<Matrix<float, Rows, Cols>, 2742 Matrix<float, Rows, Cols>, 2743 float> > 2744{ 2745public: 2746 typedef typename ScalarMatFuncBase::IRet IRet; 2747 typedef typename ScalarMatFuncBase::IArgs IArgs; 2748 2749protected: 2750 2751 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 2752 { 2753 IRet ret; 2754 2755 for (int col = 0; col < Cols; ++col) 2756 { 2757 for (int row = 0; row < Rows; ++row) 2758 ret[col][row] = this->doGetScalarFunc().apply(ctx, iargs.a[col][row], iargs.b); 2759 } 2760 2761 return ret; 2762 } 2763}; 2764 2765template <typename F, int Rows, int Cols> 2766class ScalarMatFunc : public ScalarMatFuncBase<Rows, Cols> 2767{ 2768protected: 2769 const typename ScalarMatFunc::ScalarFunc& doGetScalarFunc (void) const 2770 { 2771 return instance<F>(); 2772 } 2773}; 2774 2775template<typename T, int Size> struct GenXType; 2776 2777template<typename T> 2778struct GenXType<T, 1> 2779{ 2780 static ExprP<T> genXType (const ExprP<T>& x) { return x; } 2781}; 2782 2783template<typename T> 2784struct GenXType<T, 2> 2785{ 2786 static ExprP<Vector<T, 2> > genXType (const ExprP<T>& x) 2787 { 2788 return app<GenVec<T, 2> >(x, x); 2789 } 2790}; 2791 2792template<typename T> 2793struct GenXType<T, 3> 2794{ 2795 static ExprP<Vector<T, 3> > genXType (const ExprP<T>& x) 2796 { 2797 return app<GenVec<T, 3> >(x, x, x); 2798 } 2799}; 2800 2801template<typename T> 2802struct GenXType<T, 4> 2803{ 2804 static ExprP<Vector<T, 4> > genXType (const ExprP<T>& x) 2805 { 2806 return app<GenVec<T, 4> >(x, x, x, x); 2807 } 2808}; 2809 2810//! Returns an expression of vector of size `Size` (or scalar if Size == 1), 2811//! with each element initialized with the expression `x`. 2812template<typename T, int Size> 2813ExprP<typename ContainerOf<T, Size>::Container> genXType (const ExprP<T>& x) 2814{ 2815 return GenXType<T, Size>::genXType(x); 2816} 2817 2818typedef GenVec<float, 2> FloatVec2; 2819DEFINE_CONSTRUCTOR2(FloatVec2, Vec2, vec2, float, float) 2820 2821typedef GenVec<float, 3> FloatVec3; 2822DEFINE_CONSTRUCTOR3(FloatVec3, Vec3, vec3, float, float, float) 2823 2824typedef GenVec<float, 4> FloatVec4; 2825DEFINE_CONSTRUCTOR4(FloatVec4, Vec4, vec4, float, float, float, float) 2826 2827template <int Size> 2828class Dot : public DerivedFunc<Signature<float, Vector<float, Size>, Vector<float, Size> > > 2829{ 2830public: 2831 typedef typename Dot::ArgExprs ArgExprs; 2832 2833 string getName (void) const 2834 { 2835 return "dot"; 2836 } 2837 2838protected: 2839 ExprP<float> doExpand (ExpandContext&, const ArgExprs& args) const 2840 { 2841 ExprP<float> val = args.a[0] * args.b[0]; 2842 2843 for (int ndx = 1; ndx < Size; ++ndx) 2844 val = val + args.a[ndx] * args.b[ndx]; 2845 2846 return val; 2847 } 2848}; 2849 2850template <> 2851class Dot<1> : public DerivedFunc<Signature<float, float, float> > 2852{ 2853public: 2854 string getName (void) const 2855 { 2856 return "dot"; 2857 } 2858 2859 ExprP<float> doExpand (ExpandContext&, const ArgExprs& args) const 2860 { 2861 return args.a * args.b; 2862 } 2863}; 2864 2865template <int Size> 2866ExprP<float> dot (const ExprP<Vector<float, Size> >& x, const ExprP<Vector<float, Size> >& y) 2867{ 2868 return app<Dot<Size> >(x, y); 2869} 2870 2871ExprP<float> dot (const ExprP<float>& x, const ExprP<float>& y) 2872{ 2873 return app<Dot<1> >(x, y); 2874} 2875 2876template <int Size> 2877class Length : public DerivedFunc< 2878 Signature<float, typename ContainerOf<float, Size>::Container> > 2879{ 2880public: 2881 typedef typename Length::ArgExprs ArgExprs; 2882 2883 string getName (void) const 2884 { 2885 return "length"; 2886 } 2887 2888protected: 2889 ExprP<float> doExpand (ExpandContext&, const ArgExprs& args) const 2890 { 2891 return sqrt(dot(args.a, args.a)); 2892 } 2893}; 2894 2895template <int Size> 2896ExprP<float> length (const ExprP<typename ContainerOf<float, Size>::Container>& x) 2897{ 2898 return app<Length<Size> >(x); 2899} 2900 2901template <int Size> 2902class Distance : public DerivedFunc< 2903 Signature<float, 2904 typename ContainerOf<float, Size>::Container, 2905 typename ContainerOf<float, Size>::Container> > 2906{ 2907public: 2908 typedef typename Distance::Ret Ret; 2909 typedef typename Distance::ArgExprs ArgExprs; 2910 2911 string getName (void) const 2912 { 2913 return "distance"; 2914 } 2915 2916protected: 2917 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 2918 { 2919 return length<Size>(args.a - args.b); 2920 } 2921}; 2922 2923// cross 2924 2925class Cross : public DerivedFunc<Signature<Vec3, Vec3, Vec3> > 2926{ 2927public: 2928 string getName (void) const 2929 { 2930 return "cross"; 2931 } 2932 2933protected: 2934 ExprP<Vec3> doExpand (ExpandContext&, const ArgExprs& x) const 2935 { 2936 return vec3(x.a[1] * x.b[2] - x.b[1] * x.a[2], 2937 x.a[2] * x.b[0] - x.b[2] * x.a[0], 2938 x.a[0] * x.b[1] - x.b[0] * x.a[1]); 2939 } 2940}; 2941 2942DEFINE_CONSTRUCTOR2(Cross, Vec3, cross, Vec3, Vec3) 2943 2944template<int Size> 2945class Normalize : public DerivedFunc< 2946 Signature<typename ContainerOf<float, Size>::Container, 2947 typename ContainerOf<float, Size>::Container> > 2948{ 2949public: 2950 typedef typename Normalize::Ret Ret; 2951 typedef typename Normalize::ArgExprs ArgExprs; 2952 2953 string getName (void) const 2954 { 2955 return "normalize"; 2956 } 2957 2958protected: 2959 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 2960 { 2961 return args.a / length<Size>(args.a); 2962 } 2963}; 2964 2965template <int Size> 2966class FaceForward : public DerivedFunc< 2967 Signature<typename ContainerOf<float, Size>::Container, 2968 typename ContainerOf<float, Size>::Container, 2969 typename ContainerOf<float, Size>::Container, 2970 typename ContainerOf<float, Size>::Container> > 2971{ 2972public: 2973 typedef typename FaceForward::Ret Ret; 2974 typedef typename FaceForward::ArgExprs ArgExprs; 2975 2976 string getName (void) const 2977 { 2978 return "faceforward"; 2979 } 2980 2981protected: 2982 2983 2984 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 2985 { 2986 return cond(dot(args.c, args.b) < constant(0.0f), args.a, -args.a); 2987 } 2988}; 2989 2990template <int Size> 2991class Reflect : public DerivedFunc< 2992 Signature<typename ContainerOf<float, Size>::Container, 2993 typename ContainerOf<float, Size>::Container, 2994 typename ContainerOf<float, Size>::Container> > 2995{ 2996public: 2997 typedef typename Reflect::Ret Ret; 2998 typedef typename Reflect::ArgExprs ArgExprs; 2999 3000 string getName (void) const 3001 { 3002 return "reflect"; 3003 } 3004 3005protected: 3006 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 3007 { 3008 return args.a - (args.b * dot(args.b, args.a) * constant(2.0f)); 3009 } 3010}; 3011 3012template <int Size> 3013class Refract : public DerivedFunc< 3014 Signature<typename ContainerOf<float, Size>::Container, 3015 typename ContainerOf<float, Size>::Container, 3016 typename ContainerOf<float, Size>::Container, 3017 float> > 3018{ 3019public: 3020 typedef typename Refract::Ret Ret; 3021 typedef typename Refract::Arg0 Arg0; 3022 typedef typename Refract::Arg1 Arg1; 3023 typedef typename Refract::ArgExprs ArgExprs; 3024 3025 string getName (void) const 3026 { 3027 return "refract"; 3028 } 3029 3030protected: 3031 ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args) const 3032 { 3033 const ExprP<Arg0>& i = args.a; 3034 const ExprP<Arg1>& n = args.b; 3035 const ExprP<float>& eta = args.c; 3036 const ExprP<float> dotNI = bindExpression("dotNI", ctx, dot(n, i)); 3037 const ExprP<float> k = bindExpression("k", ctx, constant(1.0f) - eta * eta * 3038 (constant(1.0f) - dotNI * dotNI)); 3039 3040 return cond(k < constant(0.0f), 3041 genXType<float, Size>(constant(0.0f)), 3042 i * eta - n * (eta * dotNI + sqrt(k))); 3043 } 3044}; 3045 3046class PreciseFunc1 : public CFloatFunc1 3047{ 3048public: 3049 PreciseFunc1 (const string& name, DoubleFunc1& func) : CFloatFunc1(name, func) {} 3050protected: 3051 double precision (const EvalContext&, double, double) const { return 0.0; } 3052}; 3053 3054class Abs : public PreciseFunc1 3055{ 3056public: 3057 Abs (void) : PreciseFunc1("abs", deAbs) {} 3058}; 3059 3060class Sign : public PreciseFunc1 3061{ 3062public: 3063 Sign (void) : PreciseFunc1("sign", deSign) {} 3064}; 3065 3066class Floor : public PreciseFunc1 3067{ 3068public: 3069 Floor (void) : PreciseFunc1("floor", deFloor) {} 3070}; 3071 3072class Trunc : public PreciseFunc1 3073{ 3074public: 3075 Trunc (void) : PreciseFunc1("trunc", deTrunc) {} 3076}; 3077 3078class Round : public FloatFunc1 3079{ 3080public: 3081 string getName (void) const { return "round"; } 3082 3083protected: 3084 Interval applyPoint (const EvalContext&, double x) const 3085 { 3086 double truncated = 0.0; 3087 const double fract = deModf(x, &truncated); 3088 Interval ret; 3089 3090 if (fabs(fract) <= 0.5) 3091 ret |= truncated; 3092 if (fabs(fract) >= 0.5) 3093 ret |= truncated + deSign(fract); 3094 3095 return ret; 3096 } 3097 3098 double precision (const EvalContext&, double, double) const { return 0.0; } 3099}; 3100 3101class RoundEven : public PreciseFunc1 3102{ 3103public: 3104 RoundEven (void) : PreciseFunc1("roundEven", deRoundEven) {} 3105}; 3106 3107class Ceil : public PreciseFunc1 3108{ 3109public: 3110 Ceil (void) : PreciseFunc1("ceil", deCeil) {} 3111}; 3112 3113DEFINE_DERIVED_FLOAT1(Fract, fract, x, x - app<Floor>(x)); 3114 3115class PreciseFunc2 : public CFloatFunc2 3116{ 3117public: 3118 PreciseFunc2 (const string& name, DoubleFunc2& func) : CFloatFunc2(name, func) {} 3119protected: 3120 double precision (const EvalContext&, double, double, double) const { return 0.0; } 3121}; 3122 3123DEFINE_DERIVED_FLOAT2(Mod, mod, x, y, x - y * app<Floor>(x / y)); 3124 3125class Modf : public PrimitiveFunc<Signature<float, float, float> > 3126{ 3127public: 3128 string getName (void) const 3129 { 3130 return "modf"; 3131 } 3132 3133protected: 3134 IRet doApply (const EvalContext&, const IArgs& iargs) const 3135 { 3136 Interval fracIV; 3137 Interval& wholeIV = const_cast<Interval&>(iargs.b); 3138 double intPart = 0; 3139 3140 TCU_INTERVAL_APPLY_MONOTONE1(fracIV, x, iargs.a, frac, frac = deModf(x, &intPart)); 3141 TCU_INTERVAL_APPLY_MONOTONE1(wholeIV, x, iargs.a, whole, 3142 deModf(x, &intPart); whole = intPart); 3143 return fracIV; 3144 } 3145 3146 int getOutParamIndex (void) const 3147 { 3148 return 1; 3149 } 3150}; 3151 3152class Min : public PreciseFunc2 { public: Min (void) : PreciseFunc2("min", deMin) {} }; 3153class Max : public PreciseFunc2 { public: Max (void) : PreciseFunc2("max", deMax) {} }; 3154 3155class Clamp : public FloatFunc3 3156{ 3157public: 3158 string getName (void) const { return "clamp"; } 3159 3160 double applyExact (double x, double minVal, double maxVal) const 3161 { 3162 return de::min(de::max(x, minVal), maxVal); 3163 } 3164 3165 double precision (const EvalContext&, double, double, double minVal, double maxVal) const 3166 { 3167 return minVal > maxVal ? TCU_NAN : 0.0; 3168 } 3169}; 3170 3171ExprP<float> clamp(const ExprP<float>& x, const ExprP<float>& minVal, const ExprP<float>& maxVal) 3172{ 3173 return app<Clamp>(x, minVal, maxVal); 3174} 3175 3176DEFINE_DERIVED_FLOAT3(Mix, mix, x, y, a, (x * (constant(1.0f) - a)) + y * a); 3177 3178static double step (double edge, double x) 3179{ 3180 return x < edge ? 0.0 : 1.0; 3181} 3182 3183class Step : public PreciseFunc2 { public: Step (void) : PreciseFunc2("step", step) {} }; 3184 3185class SmoothStep : public DerivedFunc<Signature<float, float, float, float> > 3186{ 3187public: 3188 string getName (void) const 3189 { 3190 return "smoothstep"; 3191 } 3192 3193protected: 3194 3195 ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args) const 3196 { 3197 const ExprP<float>& edge0 = args.a; 3198 const ExprP<float>& edge1 = args.b; 3199 const ExprP<float>& x = args.c; 3200 const ExprP<float> tExpr = clamp((x - edge0) / (edge1 - edge0), 3201 constant(0.0f), constant(1.0f)); 3202 const ExprP<float> t = bindExpression("t", ctx, tExpr); 3203 3204 return (t * t * (constant(3.0f) - constant(2.0f) * t)); 3205 } 3206}; 3207 3208class FrExp : public PrimitiveFunc<Signature<float, float, int> > 3209{ 3210public: 3211 string getName (void) const 3212 { 3213 return "frexp"; 3214 } 3215 3216protected: 3217 IRet doApply (const EvalContext&, const IArgs& iargs) const 3218 { 3219 IRet ret; 3220 const IArg0& x = iargs.a; 3221 IArg1& exponent = const_cast<IArg1&>(iargs.b); 3222 3223 if (x.hasNaN() || x.contains(TCU_INFINITY) || x.contains(-TCU_INFINITY)) 3224 { 3225 // GLSL (in contrast to IEEE) says that result of applying frexp 3226 // to infinity is undefined 3227 ret = Interval::unbounded() | TCU_NAN; 3228 exponent = Interval(-deLdExp(1.0, 31), deLdExp(1.0, 31)-1); 3229 } 3230 else if (!x.empty()) 3231 { 3232 int loExp = 0; 3233 const double loFrac = deFrExp(x.lo(), &loExp); 3234 int hiExp = 0; 3235 const double hiFrac = deFrExp(x.hi(), &hiExp); 3236 3237 if (deSign(loFrac) != deSign(hiFrac)) 3238 { 3239 exponent = Interval(-TCU_INFINITY, de::max(loExp, hiExp)); 3240 ret = Interval(); 3241 if (deSign(loFrac) < 0) 3242 ret |= Interval(-1.0 + DBL_EPSILON*0.5, 0.0); 3243 if (deSign(hiFrac) > 0) 3244 ret |= Interval(0.0, 1.0 - DBL_EPSILON*0.5); 3245 } 3246 else 3247 { 3248 exponent = Interval(loExp, hiExp); 3249 if (loExp == hiExp) 3250 ret = Interval(loFrac, hiFrac); 3251 else 3252 ret = deSign(loFrac) * Interval(0.5, 1.0 - DBL_EPSILON*0.5); 3253 } 3254 } 3255 3256 return ret; 3257 } 3258 3259 int getOutParamIndex (void) const 3260 { 3261 return 1; 3262 } 3263}; 3264 3265class LdExp : public PrimitiveFunc<Signature<float, float, int> > 3266{ 3267public: 3268 string getName (void) const 3269 { 3270 return "ldexp"; 3271 } 3272 3273protected: 3274 Interval doApply (const EvalContext& ctx, const IArgs& iargs) const 3275 { 3276 Interval ret = call<Exp2>(ctx, iargs.b); 3277 // Khronos bug 11180 consensus: if exp2(exponent) cannot be represented, 3278 // the result is undefined. 3279 3280 if (ret.contains(TCU_INFINITY) | ret.contains(-TCU_INFINITY)) 3281 ret |= TCU_NAN; 3282 3283 return call<Mul>(ctx, iargs.a, ret); 3284 } 3285}; 3286 3287template<int Rows, int Columns> 3288class Transpose : public PrimitiveFunc<Signature<Matrix<float, Rows, Columns>, 3289 Matrix<float, Columns, Rows> > > 3290{ 3291public: 3292 typedef typename Transpose::IRet IRet; 3293 typedef typename Transpose::IArgs IArgs; 3294 3295 string getName (void) const 3296 { 3297 return "transpose"; 3298 } 3299 3300protected: 3301 IRet doApply (const EvalContext&, const IArgs& iargs) const 3302 { 3303 IRet ret; 3304 3305 for (int rowNdx = 0; rowNdx < Rows; ++rowNdx) 3306 { 3307 for (int colNdx = 0; colNdx < Columns; ++colNdx) 3308 ret(rowNdx, colNdx) = iargs.a(colNdx, rowNdx); 3309 } 3310 3311 return ret; 3312 } 3313}; 3314 3315template<typename Ret, typename Arg0, typename Arg1> 3316class MulFunc : public PrimitiveFunc<Signature<Ret, Arg0, Arg1> > 3317{ 3318public: 3319 string getName (void) const { return "mul"; } 3320 3321protected: 3322 void doPrint (ostream& os, const BaseArgExprs& args) const 3323 { 3324 os << "(" << *args[0] << " * " << *args[1] << ")"; 3325 } 3326}; 3327 3328template<int LeftRows, int Middle, int RightCols> 3329class MatMul : public MulFunc<Matrix<float, LeftRows, RightCols>, 3330 Matrix<float, LeftRows, Middle>, 3331 Matrix<float, Middle, RightCols> > 3332{ 3333protected: 3334 typedef typename MatMul::IRet IRet; 3335 typedef typename MatMul::IArgs IArgs; 3336 typedef typename MatMul::IArg0 IArg0; 3337 typedef typename MatMul::IArg1 IArg1; 3338 3339 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 3340 { 3341 const IArg0& left = iargs.a; 3342 const IArg1& right = iargs.b; 3343 IRet ret; 3344 3345 for (int row = 0; row < LeftRows; ++row) 3346 { 3347 for (int col = 0; col < RightCols; ++col) 3348 { 3349 Interval element (0.0); 3350 3351 for (int ndx = 0; ndx < Middle; ++ndx) 3352 element = call<Add>(ctx, element, 3353 call<Mul>(ctx, left[ndx][row], right[col][ndx])); 3354 3355 ret[col][row] = element; 3356 } 3357 } 3358 3359 return ret; 3360 } 3361}; 3362 3363template<int Rows, int Cols> 3364class VecMatMul : public MulFunc<Vector<float, Cols>, 3365 Vector<float, Rows>, 3366 Matrix<float, Rows, Cols> > 3367{ 3368public: 3369 typedef typename VecMatMul::IRet IRet; 3370 typedef typename VecMatMul::IArgs IArgs; 3371 typedef typename VecMatMul::IArg0 IArg0; 3372 typedef typename VecMatMul::IArg1 IArg1; 3373 3374protected: 3375 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 3376 { 3377 const IArg0& left = iargs.a; 3378 const IArg1& right = iargs.b; 3379 IRet ret; 3380 3381 for (int col = 0; col < Cols; ++col) 3382 { 3383 Interval element (0.0); 3384 3385 for (int row = 0; row < Rows; ++row) 3386 element = call<Add>(ctx, element, call<Mul>(ctx, left[row], right[col][row])); 3387 3388 ret[col] = element; 3389 } 3390 3391 return ret; 3392 } 3393}; 3394 3395template<int Rows, int Cols> 3396class MatVecMul : public MulFunc<Vector<float, Rows>, 3397 Matrix<float, Rows, Cols>, 3398 Vector<float, Cols> > 3399{ 3400public: 3401 typedef typename MatVecMul::IRet IRet; 3402 typedef typename MatVecMul::IArgs IArgs; 3403 typedef typename MatVecMul::IArg0 IArg0; 3404 typedef typename MatVecMul::IArg1 IArg1; 3405 3406protected: 3407 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 3408 { 3409 const IArg0& left = iargs.a; 3410 const IArg1& right = iargs.b; 3411 3412 return call<VecMatMul<Cols, Rows> >(ctx, right, 3413 call<Transpose<Rows, Cols> >(ctx, left)); 3414 } 3415}; 3416 3417template<int Rows, int Cols> 3418class OuterProduct : public PrimitiveFunc<Signature<Matrix<float, Rows, Cols>, 3419 Vector<float, Rows>, 3420 Vector<float, Cols> > > 3421{ 3422public: 3423 typedef typename OuterProduct::IRet IRet; 3424 typedef typename OuterProduct::IArgs IArgs; 3425 3426 string getName (void) const 3427 { 3428 return "outerProduct"; 3429 } 3430 3431protected: 3432 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 3433 { 3434 IRet ret; 3435 3436 for (int row = 0; row < Rows; ++row) 3437 { 3438 for (int col = 0; col < Cols; ++col) 3439 ret[col][row] = call<Mul>(ctx, iargs.a[row], iargs.b[col]); 3440 } 3441 3442 return ret; 3443 } 3444}; 3445 3446template<int Rows, int Cols> 3447ExprP<Matrix<float, Rows, Cols> > outerProduct (const ExprP<Vector<float, Rows> >& left, 3448 const ExprP<Vector<float, Cols> >& right) 3449{ 3450 return app<OuterProduct<Rows, Cols> >(left, right); 3451} 3452 3453template<int Size> 3454class DeterminantBase : public DerivedFunc<Signature<float, Matrix<float, Size, Size> > > 3455{ 3456public: 3457 string getName (void) const { return "determinant"; } 3458}; 3459 3460template<int Size> 3461class Determinant; 3462 3463template<int Size> 3464ExprP<float> determinant (ExprP<Matrix<float, Size, Size> > mat) 3465{ 3466 return app<Determinant<Size> >(mat); 3467} 3468 3469template<> 3470class Determinant<2> : public DeterminantBase<2> 3471{ 3472protected: 3473 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 3474 { 3475 ExprP<Mat2> mat = args.a; 3476 3477 return mat[0][0] * mat[1][1] - mat[1][0] * mat[0][1]; 3478 } 3479}; 3480 3481template<> 3482class Determinant<3> : public DeterminantBase<3> 3483{ 3484protected: 3485 ExprP<Ret> doExpand (ExpandContext&, const ArgExprs& args) const 3486 { 3487 ExprP<Mat3> mat = args.a; 3488 3489 return (mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) + 3490 mat[0][1] * (mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2]) + 3491 mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0])); 3492 } 3493}; 3494 3495template<> 3496class Determinant<4> : public DeterminantBase<4> 3497{ 3498protected: 3499 ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args) const 3500 { 3501 ExprP<Mat4> mat = args.a; 3502 ExprP<Mat3> minors[4]; 3503 3504 for (int ndx = 0; ndx < 4; ++ndx) 3505 { 3506 ExprP<Vec4> minorColumns[3]; 3507 ExprP<Vec3> columns[3]; 3508 3509 for (int col = 0; col < 3; ++col) 3510 minorColumns[col] = mat[col < ndx ? col : col + 1]; 3511 3512 for (int col = 0; col < 3; ++col) 3513 columns[col] = vec3(minorColumns[0][col+1], 3514 minorColumns[1][col+1], 3515 minorColumns[2][col+1]); 3516 3517 minors[ndx] = bindExpression("minor", ctx, 3518 mat3(columns[0], columns[1], columns[2])); 3519 } 3520 3521 return (mat[0][0] * determinant(minors[0]) - 3522 mat[1][0] * determinant(minors[1]) + 3523 mat[2][0] * determinant(minors[2]) - 3524 mat[3][0] * determinant(minors[3])); 3525 } 3526}; 3527 3528template<int Size> class Inverse; 3529 3530template <int Size> 3531ExprP<Matrix<float, Size, Size> > inverse (ExprP<Matrix<float, Size, Size> > mat) 3532{ 3533 return app<Inverse<Size> >(mat); 3534} 3535 3536template<> 3537class Inverse<2> : public DerivedFunc<Signature<Mat2, Mat2> > 3538{ 3539public: 3540 string getName (void) const 3541 { 3542 return "inverse"; 3543 } 3544 3545protected: 3546 ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args) const 3547 { 3548 ExprP<Mat2> mat = args.a; 3549 ExprP<float> det = bindExpression("det", ctx, determinant(mat)); 3550 3551 return mat2(vec2(mat[1][1] / det, -mat[0][1] / det), 3552 vec2(-mat[1][0] / det, mat[0][0] / det)); 3553 } 3554}; 3555 3556template<> 3557class Inverse<3> : public DerivedFunc<Signature<Mat3, Mat3> > 3558{ 3559public: 3560 string getName (void) const 3561 { 3562 return "inverse"; 3563 } 3564 3565protected: 3566 ExprP<Ret> doExpand (ExpandContext& ctx, const ArgExprs& args) const 3567 { 3568 ExprP<Mat3> mat = args.a; 3569 ExprP<Mat2> invA = bindExpression("invA", ctx, 3570 inverse(mat2(vec2(mat[0][0], mat[0][1]), 3571 vec2(mat[1][0], mat[1][1])))); 3572 3573 ExprP<Vec2> matB = bindExpression("matB", ctx, vec2(mat[2][0], mat[2][1])); 3574 ExprP<Vec2> matC = bindExpression("matC", ctx, vec2(mat[0][2], mat[1][2])); 3575 ExprP<float> matD = bindExpression("matD", ctx, mat[2][2]); 3576 3577 ExprP<float> schur = bindExpression("schur", ctx, 3578 constant(1.0f) / 3579 (matD - dot(matC * invA, matB))); 3580 3581 ExprP<Vec2> t1 = invA * matB; 3582 ExprP<Vec2> t2 = t1 * schur; 3583 ExprP<Mat2> t3 = outerProduct(t2, matC); 3584 ExprP<Mat2> t4 = t3 * invA; 3585 ExprP<Mat2> t5 = invA + t4; 3586 ExprP<Mat2> blockA = bindExpression("blockA", ctx, t5); 3587 ExprP<Vec2> blockB = bindExpression("blockB", ctx, 3588 (invA * matB) * -schur); 3589 ExprP<Vec2> blockC = bindExpression("blockC", ctx, 3590 (matC * invA) * -schur); 3591 3592 return mat3(vec3(blockA[0][0], blockA[0][1], blockC[0]), 3593 vec3(blockA[1][0], blockA[1][1], blockC[1]), 3594 vec3(blockB[0], blockB[1], schur)); 3595 } 3596}; 3597 3598template<> 3599class Inverse<4> : public DerivedFunc<Signature<Mat4, Mat4> > 3600{ 3601public: 3602 string getName (void) const { return "inverse"; } 3603 3604protected: 3605 ExprP<Ret> doExpand (ExpandContext& ctx, 3606 const ArgExprs& args) const 3607 { 3608 ExprP<Mat4> mat = args.a; 3609 ExprP<Mat2> invA = bindExpression("invA", ctx, 3610 inverse(mat2(vec2(mat[0][0], mat[0][1]), 3611 vec2(mat[1][0], mat[1][1])))); 3612 ExprP<Mat2> matB = bindExpression("matB", ctx, 3613 mat2(vec2(mat[2][0], mat[2][1]), 3614 vec2(mat[3][0], mat[3][1]))); 3615 ExprP<Mat2> matC = bindExpression("matC", ctx, 3616 mat2(vec2(mat[0][2], mat[0][3]), 3617 vec2(mat[1][2], mat[1][3]))); 3618 ExprP<Mat2> matD = bindExpression("matD", ctx, 3619 mat2(vec2(mat[2][2], mat[2][3]), 3620 vec2(mat[3][2], mat[3][3]))); 3621 ExprP<Mat2> schur = bindExpression("schur", ctx, 3622 inverse(matD + -(matC * invA * matB))); 3623 ExprP<Mat2> blockA = bindExpression("blockA", ctx, 3624 invA + (invA * matB * schur * matC * invA)); 3625 ExprP<Mat2> blockB = bindExpression("blockB", ctx, 3626 (-invA) * matB * schur); 3627 ExprP<Mat2> blockC = bindExpression("blockC", ctx, 3628 (-schur) * matC * invA); 3629 3630 return mat4(vec4(blockA[0][0], blockA[0][1], blockC[0][0], blockC[0][1]), 3631 vec4(blockA[1][0], blockA[1][1], blockC[1][0], blockC[1][1]), 3632 vec4(blockB[0][0], blockB[0][1], schur[0][0], schur[0][1]), 3633 vec4(blockB[1][0], blockB[1][1], schur[1][0], schur[1][1])); 3634 } 3635}; 3636 3637class Fma : public DerivedFunc<Signature<float, float, float, float> > 3638{ 3639public: 3640 string getName (void) const 3641 { 3642 return "fma"; 3643 } 3644 3645 string getRequiredExtension (void) const 3646 { 3647 return "GL_EXT_gpu_shader5"; 3648 } 3649 3650protected: 3651 ExprP<float> doExpand (ExpandContext&, const ArgExprs& x) const 3652 { 3653 return x.a * x.b + x.c; 3654 } 3655}; 3656 3657} // Functions 3658 3659using namespace Functions; 3660 3661template <typename T> 3662ExprP<typename T::Element> ContainerExprPBase<T>::operator[] (int i) const 3663{ 3664 return Functions::getComponent(exprP<T>(*this), i); 3665} 3666 3667ExprP<float> operator+ (const ExprP<float>& arg0, const ExprP<float>& arg1) 3668{ 3669 return app<Add>(arg0, arg1); 3670} 3671 3672ExprP<float> operator- (const ExprP<float>& arg0, const ExprP<float>& arg1) 3673{ 3674 return app<Sub>(arg0, arg1); 3675} 3676 3677ExprP<float> operator- (const ExprP<float>& arg0) 3678{ 3679 return app<Negate>(arg0); 3680} 3681 3682ExprP<float> operator* (const ExprP<float>& arg0, const ExprP<float>& arg1) 3683{ 3684 return app<Mul>(arg0, arg1); 3685} 3686 3687ExprP<float> operator/ (const ExprP<float>& arg0, const ExprP<float>& arg1) 3688{ 3689 return app<Div>(arg0, arg1); 3690} 3691 3692template <typename Sig_, int Size> 3693class GenFunc : public PrimitiveFunc<Signature< 3694 typename ContainerOf<typename Sig_::Ret, Size>::Container, 3695 typename ContainerOf<typename Sig_::Arg0, Size>::Container, 3696 typename ContainerOf<typename Sig_::Arg1, Size>::Container, 3697 typename ContainerOf<typename Sig_::Arg2, Size>::Container, 3698 typename ContainerOf<typename Sig_::Arg3, Size>::Container> > 3699{ 3700public: 3701 typedef typename GenFunc::IArgs IArgs; 3702 typedef typename GenFunc::IRet IRet; 3703 3704 GenFunc (const Func<Sig_>& scalarFunc) : m_func (scalarFunc) {} 3705 3706 string getName (void) const 3707 { 3708 return m_func.getName(); 3709 } 3710 3711 int getOutParamIndex (void) const 3712 { 3713 return m_func.getOutParamIndex(); 3714 } 3715 3716 string getRequiredExtension (void) const 3717 { 3718 return m_func.getRequiredExtension(); 3719 } 3720 3721protected: 3722 void doPrint (ostream& os, const BaseArgExprs& args) const 3723 { 3724 m_func.print(os, args); 3725 } 3726 3727 IRet doApply (const EvalContext& ctx, const IArgs& iargs) const 3728 { 3729 IRet ret; 3730 3731 for (int ndx = 0; ndx < Size; ++ndx) 3732 { 3733 ret[ndx] = 3734 m_func.apply(ctx, iargs.a[ndx], iargs.b[ndx], iargs.c[ndx], iargs.d[ndx]); 3735 } 3736 3737 return ret; 3738 } 3739 3740 void doGetUsedFuncs (FuncSet& dst) const 3741 { 3742 m_func.getUsedFuncs(dst); 3743 } 3744 3745 const Func<Sig_>& m_func; 3746}; 3747 3748template <typename F, int Size> 3749class VectorizedFunc : public GenFunc<typename F::Sig, Size> 3750{ 3751public: 3752 VectorizedFunc (void) : GenFunc<typename F::Sig, Size>(instance<F>()) {} 3753}; 3754 3755 3756 3757template <typename Sig_, int Size> 3758class FixedGenFunc : public PrimitiveFunc <Signature< 3759 typename ContainerOf<typename Sig_::Ret, Size>::Container, 3760 typename ContainerOf<typename Sig_::Arg0, Size>::Container, 3761 typename Sig_::Arg1, 3762 typename ContainerOf<typename Sig_::Arg2, Size>::Container, 3763 typename ContainerOf<typename Sig_::Arg3, Size>::Container> > 3764{ 3765public: 3766 typedef typename FixedGenFunc::IArgs IArgs; 3767 typedef typename FixedGenFunc::IRet IRet; 3768 3769 string getName (void) const 3770 { 3771 return this->doGetScalarFunc().getName(); 3772 } 3773 3774protected: 3775 void doPrint (ostream& os, const BaseArgExprs& args) const 3776 { 3777 this->doGetScalarFunc().print(os, args); 3778 } 3779 3780 IRet doApply (const EvalContext& ctx, 3781 const IArgs& iargs) const 3782 { 3783 IRet ret; 3784 const Func<Sig_>& func = this->doGetScalarFunc(); 3785 3786 for (int ndx = 0; ndx < Size; ++ndx) 3787 ret[ndx] = func.apply(ctx, iargs.a[ndx], iargs.b, iargs.c[ndx], iargs.d[ndx]); 3788 3789 return ret; 3790 } 3791 3792 virtual const Func<Sig_>& doGetScalarFunc (void) const = 0; 3793}; 3794 3795template <typename F, int Size> 3796class FixedVecFunc : public FixedGenFunc<typename F::Sig, Size> 3797{ 3798protected: 3799 const Func<typename F::Sig>& doGetScalarFunc (void) const { return instance<F>(); } 3800}; 3801 3802template<typename Sig> 3803struct GenFuncs 3804{ 3805 GenFuncs (const Func<Sig>& func_, 3806 const GenFunc<Sig, 2>& func2_, 3807 const GenFunc<Sig, 3>& func3_, 3808 const GenFunc<Sig, 4>& func4_) 3809 : func (func_) 3810 , func2 (func2_) 3811 , func3 (func3_) 3812 , func4 (func4_) 3813 {} 3814 3815 const Func<Sig>& func; 3816 const GenFunc<Sig, 2>& func2; 3817 const GenFunc<Sig, 3>& func3; 3818 const GenFunc<Sig, 4>& func4; 3819}; 3820 3821template<typename F> 3822GenFuncs<typename F::Sig> makeVectorizedFuncs (void) 3823{ 3824 return GenFuncs<typename F::Sig>(instance<F>(), 3825 instance<VectorizedFunc<F, 2> >(), 3826 instance<VectorizedFunc<F, 3> >(), 3827 instance<VectorizedFunc<F, 4> >()); 3828} 3829 3830template<int Size> 3831ExprP<Vector<float, Size> > operator*(const ExprP<Vector<float, Size> >& arg0, 3832 const ExprP<Vector<float, Size> >& arg1) 3833{ 3834 return app<VectorizedFunc<Mul, Size> >(arg0, arg1); 3835} 3836 3837template<int Size> 3838ExprP<Vector<float, Size> > operator*(const ExprP<Vector<float, Size> >& arg0, 3839 const ExprP<float>& arg1) 3840{ 3841 return app<FixedVecFunc<Mul, Size> >(arg0, arg1); 3842} 3843 3844template<int Size> 3845ExprP<Vector<float, Size> > operator/(const ExprP<Vector<float, Size> >& arg0, 3846 const ExprP<float>& arg1) 3847{ 3848 return app<FixedVecFunc<Div, Size> >(arg0, arg1); 3849} 3850 3851template<int Size> 3852ExprP<Vector<float, Size> > operator-(const ExprP<Vector<float, Size> >& arg0) 3853{ 3854 return app<VectorizedFunc<Negate, Size> >(arg0); 3855} 3856 3857template<int Size> 3858ExprP<Vector<float, Size> > operator-(const ExprP<Vector<float, Size> >& arg0, 3859 const ExprP<Vector<float, Size> >& arg1) 3860{ 3861 return app<VectorizedFunc<Sub, Size> >(arg0, arg1); 3862} 3863 3864template<int LeftRows, int Middle, int RightCols> 3865ExprP<Matrix<float, LeftRows, RightCols> > 3866operator* (const ExprP<Matrix<float, LeftRows, Middle> >& left, 3867 const ExprP<Matrix<float, Middle, RightCols> >& right) 3868{ 3869 return app<MatMul<LeftRows, Middle, RightCols> >(left, right); 3870} 3871 3872template<int Rows, int Cols> 3873ExprP<Vector<float, Rows> > operator* (const ExprP<Vector<float, Cols> >& left, 3874 const ExprP<Matrix<float, Rows, Cols> >& right) 3875{ 3876 return app<VecMatMul<Rows, Cols> >(left, right); 3877} 3878 3879template<int Rows, int Cols> 3880ExprP<Vector<float, Cols> > operator* (const ExprP<Matrix<float, Rows, Cols> >& left, 3881 const ExprP<Vector<float, Rows> >& right) 3882{ 3883 return app<MatVecMul<Rows, Cols> >(left, right); 3884} 3885 3886template<int Rows, int Cols> 3887ExprP<Matrix<float, Rows, Cols> > operator* (const ExprP<Matrix<float, Rows, Cols> >& left, 3888 const ExprP<float>& right) 3889{ 3890 return app<ScalarMatFunc<Mul, Rows, Cols> >(left, right); 3891} 3892 3893template<int Rows, int Cols> 3894ExprP<Matrix<float, Rows, Cols> > operator+ (const ExprP<Matrix<float, Rows, Cols> >& left, 3895 const ExprP<Matrix<float, Rows, Cols> >& right) 3896{ 3897 return app<CompMatFunc<Add, Rows, Cols> >(left, right); 3898} 3899 3900template<int Rows, int Cols> 3901ExprP<Matrix<float, Rows, Cols> > operator- (const ExprP<Matrix<float, Rows, Cols> >& mat) 3902{ 3903 return app<MatNeg<Rows, Cols> >(mat); 3904} 3905 3906template <typename T> 3907class Sampling 3908{ 3909public: 3910 virtual void genFixeds (const FloatFormat&, vector<T>&) const {} 3911 virtual T genRandom (const FloatFormat&, Precision, Random&) const { return T(); } 3912 virtual double getWeight (void) const { return 0.0; } 3913}; 3914 3915template <> 3916class DefaultSampling<Void> : public Sampling<Void> 3917{ 3918public: 3919 void genFixeds (const FloatFormat&, vector<Void>& dst) const { dst.push_back(Void()); } 3920}; 3921 3922template <> 3923class DefaultSampling<bool> : public Sampling<bool> 3924{ 3925public: 3926 void genFixeds (const FloatFormat&, vector<bool>& dst) const 3927 { 3928 dst.push_back(true); 3929 dst.push_back(false); 3930 } 3931}; 3932 3933template <> 3934class DefaultSampling<int> : public Sampling<int> 3935{ 3936public: 3937 int genRandom (const FloatFormat&, Precision prec, Random& rnd) const 3938 { 3939 const int exp = rnd.getInt(0, getNumBits(prec)-2); 3940 const int sign = rnd.getBool() ? -1 : 1; 3941 3942 return sign * rnd.getInt(0, 1L << exp); 3943 } 3944 3945 void genFixeds (const FloatFormat&, vector<int>& dst) const 3946 { 3947 dst.push_back(0); 3948 dst.push_back(-1); 3949 dst.push_back(1); 3950 } 3951 double getWeight (void) const { return 1.0; } 3952 3953private: 3954 static inline int getNumBits (Precision prec) 3955 { 3956 switch (prec) 3957 { 3958 case glu::PRECISION_LOWP: return 8; 3959 case glu::PRECISION_MEDIUMP: return 16; 3960 case glu::PRECISION_HIGHP: return 32; 3961 default: 3962 DE_ASSERT(false); 3963 return 0; 3964 } 3965 } 3966}; 3967 3968template <> 3969class DefaultSampling<float> : public Sampling<float> 3970{ 3971public: 3972 float genRandom (const FloatFormat& format, Precision prec, Random& rnd) const; 3973 void genFixeds (const FloatFormat& format, vector<float>& dst) const; 3974 double getWeight (void) const { return 1.0; } 3975}; 3976 3977//! Generate a random float from a reasonable general-purpose distribution. 3978float DefaultSampling<float>::genRandom (const FloatFormat& format, 3979 Precision, 3980 Random& rnd) const 3981{ 3982 const int minExp = format.getMinExp(); 3983 const int maxExp = format.getMaxExp(); 3984 const bool haveSubnormal = format.hasSubnormal() != tcu::NO; 3985 3986 // Choose exponent so that the cumulative distribution is cubic. 3987 // This makes the probability distribution quadratic, with the peak centered on zero. 3988 const double minRoot = deCbrt(minExp - 0.5 - (haveSubnormal ? 1.0 : 0.0)); 3989 const double maxRoot = deCbrt(maxExp + 0.5); 3990 const int fractionBits = format.getFractionBits(); 3991 const int exp = int(deRoundEven(dePow(rnd.getDouble(minRoot, maxRoot), 3992 3.0))); 3993 float base = 0.0f; // integral power of two 3994 float quantum = 0.0f; // smallest representable difference in the binade 3995 float significand = 0.0f; // Significand. 3996 3997 DE_ASSERT(fractionBits < std::numeric_limits<float>::digits); 3998 3999 // Generate some occasional special numbers 4000 switch (rnd.getInt(0, 64)) 4001 { 4002 case 0: return 0; 4003 case 1: return TCU_INFINITY; 4004 case 2: return -TCU_INFINITY; 4005 case 3: return TCU_NAN; 4006 default: break; 4007 } 4008 4009 if (exp >= minExp) 4010 { 4011 // Normal number 4012 base = deFloatLdExp(1.0f, exp); 4013 quantum = deFloatLdExp(1.0f, exp - fractionBits); 4014 } 4015 else 4016 { 4017 // Subnormal 4018 base = 0.0f; 4019 quantum = deFloatLdExp(1.0f, minExp - fractionBits); 4020 } 4021 4022 switch (rnd.getInt(0, 16)) 4023 { 4024 case 0: // The highest number in this binade, significand is all bits one. 4025 significand = base - quantum; 4026 break; 4027 case 1: // Significand is one. 4028 significand = quantum; 4029 break; 4030 case 2: // Significand is zero. 4031 significand = 0.0; 4032 break; 4033 default: // Random (evenly distributed) significand. 4034 { 4035 deUint64 intFraction = rnd.getUint64() & ((1 << fractionBits) - 1); 4036 significand = float(intFraction) * quantum; 4037 } 4038 } 4039 4040 // Produce positive numbers more often than negative. 4041 return (rnd.getInt(0,3) == 0 ? -1.0f : 1.0f) * (base + significand); 4042} 4043 4044//! Generate a standard set of floats that should always be tested. 4045void DefaultSampling<float>::genFixeds (const FloatFormat& format, vector<float>& dst) const 4046{ 4047 const int minExp = format.getMinExp(); 4048 const int maxExp = format.getMaxExp(); 4049 const int fractionBits = format.getFractionBits(); 4050 const float minQuantum = deFloatLdExp(1.0f, minExp - fractionBits); 4051 const float minNormalized = deFloatLdExp(1.0f, minExp); 4052 const float maxQuantum = deFloatLdExp(1.0f, maxExp - fractionBits); 4053 4054 // NaN 4055 dst.push_back(TCU_NAN); 4056 // Zero 4057 dst.push_back(0.0f); 4058 4059 for (int sign = -1; sign <= 1; sign += 2) 4060 { 4061 // Smallest subnormal 4062 dst.push_back(sign * minQuantum); 4063 4064 // Largest subnormal 4065 dst.push_back(sign * (minNormalized - minQuantum)); 4066 4067 // Smallest normalized 4068 dst.push_back(sign * minNormalized); 4069 4070 // Next smallest normalized 4071 dst.push_back(sign * (minNormalized + minQuantum)); 4072 4073 dst.push_back(sign * 0.5f); 4074 dst.push_back(sign * 1.0f); 4075 dst.push_back(sign * 2.0f); 4076 4077 // Largest number 4078 dst.push_back(sign * (deFloatLdExp(1.0f, maxExp) + 4079 (deFloatLdExp(1.0f, maxExp) - maxQuantum))); 4080 4081 dst.push_back(sign * TCU_INFINITY); 4082 } 4083} 4084 4085template <typename T, int Size> 4086class DefaultSampling<Vector<T, Size> > : public Sampling<Vector<T, Size> > 4087{ 4088public: 4089 typedef Vector<T, Size> Value; 4090 4091 Value genRandom (const FloatFormat& fmt, Precision prec, Random& rnd) const 4092 { 4093 Value ret; 4094 4095 for (int ndx = 0; ndx < Size; ++ndx) 4096 ret[ndx] = instance<DefaultSampling<T> >().genRandom(fmt, prec, rnd); 4097 4098 return ret; 4099 } 4100 4101 void genFixeds (const FloatFormat& fmt, vector<Value>& dst) const 4102 { 4103 vector<T> scalars; 4104 4105 instance<DefaultSampling<T> >().genFixeds(fmt, scalars); 4106 4107 for (size_t scalarNdx = 0; scalarNdx < scalars.size(); ++scalarNdx) 4108 dst.push_back(Value(scalars[scalarNdx])); 4109 } 4110 4111 double getWeight (void) const 4112 { 4113 return dePow(instance<DefaultSampling<T> >().getWeight(), Size); 4114 } 4115}; 4116 4117template <typename T, int Rows, int Columns> 4118class DefaultSampling<Matrix<T, Rows, Columns> > : public Sampling<Matrix<T, Rows, Columns> > 4119{ 4120public: 4121 typedef Matrix<T, Rows, Columns> Value; 4122 4123 Value genRandom (const FloatFormat& fmt, Precision prec, Random& rnd) const 4124 { 4125 Value ret; 4126 4127 for (int rowNdx = 0; rowNdx < Rows; ++rowNdx) 4128 for (int colNdx = 0; colNdx < Columns; ++colNdx) 4129 ret(rowNdx, colNdx) = instance<DefaultSampling<T> >().genRandom(fmt, prec, rnd); 4130 4131 return ret; 4132 } 4133 4134 void genFixeds (const FloatFormat& fmt, vector<Value>& dst) const 4135 { 4136 vector<T> scalars; 4137 4138 instance<DefaultSampling<T> >().genFixeds(fmt, scalars); 4139 4140 for (size_t scalarNdx = 0; scalarNdx < scalars.size(); ++scalarNdx) 4141 dst.push_back(Value(scalars[scalarNdx])); 4142 4143 if (Columns == Rows) 4144 { 4145 Value mat (0.0); 4146 T x = T(1.0f); 4147 mat[0][0] = x; 4148 for (int ndx = 0; ndx < Columns; ++ndx) 4149 { 4150 mat[Columns-1-ndx][ndx] = x; 4151 x *= T(2.0f); 4152 } 4153 dst.push_back(mat); 4154 } 4155 } 4156 4157 double getWeight (void) const 4158 { 4159 return dePow(instance<DefaultSampling<T> >().getWeight(), Rows * Columns); 4160 } 4161}; 4162 4163struct Context 4164{ 4165 Context (const string& name_, 4166 TestContext& testContext_, 4167 RenderContext& renderContext_, 4168 const FloatFormat& floatFormat_, 4169 const FloatFormat& highpFormat_, 4170 Precision precision_, 4171 ShaderType shaderType_, 4172 size_t numRandoms_) 4173 : name (name_) 4174 , testContext (testContext_) 4175 , renderContext (renderContext_) 4176 , floatFormat (floatFormat_) 4177 , highpFormat (highpFormat_) 4178 , precision (precision_) 4179 , shaderType (shaderType_) 4180 , numRandoms (numRandoms_) {} 4181 4182 string name; 4183 TestContext& testContext; 4184 RenderContext& renderContext; 4185 FloatFormat floatFormat; 4186 FloatFormat highpFormat; 4187 Precision precision; 4188 ShaderType shaderType; 4189 size_t numRandoms; 4190}; 4191 4192template<typename In0_ = Void, typename In1_ = Void, typename In2_ = Void, typename In3_ = Void> 4193struct InTypes 4194{ 4195 typedef In0_ In0; 4196 typedef In1_ In1; 4197 typedef In2_ In2; 4198 typedef In3_ In3; 4199}; 4200 4201template <typename In> 4202int numInputs (void) 4203{ 4204 return (!isTypeValid<typename In::In0>() ? 0 : 4205 !isTypeValid<typename In::In1>() ? 1 : 4206 !isTypeValid<typename In::In2>() ? 2 : 4207 !isTypeValid<typename In::In3>() ? 3 : 4208 4); 4209} 4210 4211template<typename Out0_, typename Out1_ = Void> 4212struct OutTypes 4213{ 4214 typedef Out0_ Out0; 4215 typedef Out1_ Out1; 4216}; 4217 4218template <typename Out> 4219int numOutputs (void) 4220{ 4221 return (!isTypeValid<typename Out::Out0>() ? 0 : 4222 !isTypeValid<typename Out::Out1>() ? 1 : 4223 2); 4224} 4225 4226template<typename In> 4227struct Inputs 4228{ 4229 vector<typename In::In0> in0; 4230 vector<typename In::In1> in1; 4231 vector<typename In::In2> in2; 4232 vector<typename In::In3> in3; 4233}; 4234 4235template<typename Out> 4236struct Outputs 4237{ 4238 Outputs (size_t size) : out0(size), out1(size) {} 4239 4240 vector<typename Out::Out0> out0; 4241 vector<typename Out::Out1> out1; 4242}; 4243 4244template<typename In, typename Out> 4245struct Variables 4246{ 4247 VariableP<typename In::In0> in0; 4248 VariableP<typename In::In1> in1; 4249 VariableP<typename In::In2> in2; 4250 VariableP<typename In::In3> in3; 4251 VariableP<typename Out::Out0> out0; 4252 VariableP<typename Out::Out1> out1; 4253}; 4254 4255template<typename In> 4256struct Samplings 4257{ 4258 Samplings (const Sampling<typename In::In0>& in0_, 4259 const Sampling<typename In::In1>& in1_, 4260 const Sampling<typename In::In2>& in2_, 4261 const Sampling<typename In::In3>& in3_) 4262 : in0 (in0_), in1 (in1_), in2 (in2_), in3 (in3_) {} 4263 4264 const Sampling<typename In::In0>& in0; 4265 const Sampling<typename In::In1>& in1; 4266 const Sampling<typename In::In2>& in2; 4267 const Sampling<typename In::In3>& in3; 4268}; 4269 4270template<typename In> 4271struct DefaultSamplings : Samplings<In> 4272{ 4273 DefaultSamplings (void) 4274 : Samplings<In>(instance<DefaultSampling<typename In::In0> >(), 4275 instance<DefaultSampling<typename In::In1> >(), 4276 instance<DefaultSampling<typename In::In2> >(), 4277 instance<DefaultSampling<typename In::In3> >()) {} 4278}; 4279 4280class PrecisionCase : public TestCase 4281{ 4282public: 4283 IterateResult iterate (void); 4284 4285protected: 4286 PrecisionCase (const Context& context, 4287 const string& name, 4288 const string& extension = "") 4289 : TestCase (context.testContext, 4290 name.c_str(), 4291 name.c_str()) 4292 , m_ctx (context) 4293 , m_status () 4294 , m_rnd (0xdeadbeefu + 4295 context.testContext.getCommandLine().getBaseSeed()) 4296 , m_extension (extension) 4297 { 4298 } 4299 4300 RenderContext& getRenderContext(void) const { return m_ctx.renderContext; } 4301 4302 const FloatFormat& getFormat (void) const { return m_ctx.floatFormat; } 4303 4304 TestLog& log (void) const { return m_testCtx.getLog(); } 4305 4306 virtual void runTest (void) = 0; 4307 4308 template <typename In, typename Out> 4309 void testStatement (const Variables<In, Out>& variables, 4310 const Inputs<In>& inputs, 4311 const Statement& stmt); 4312 4313 template<typename T> 4314 Symbol makeSymbol (const Variable<T>& variable) 4315 { 4316 return Symbol(variable.getName(), getVarTypeOf<T>(m_ctx.precision)); 4317 } 4318 4319 Context m_ctx; 4320 ResultCollector m_status; 4321 Random m_rnd; 4322 const string m_extension; 4323}; 4324 4325IterateResult PrecisionCase::iterate (void) 4326{ 4327 runTest(); 4328 m_status.setTestContextResult(m_testCtx); 4329 return STOP; 4330} 4331 4332template <typename In, typename Out> 4333void PrecisionCase::testStatement (const Variables<In, Out>& variables, 4334 const Inputs<In>& inputs, 4335 const Statement& stmt) 4336{ 4337 using namespace ShaderExecUtil; 4338 4339 typedef typename In::In0 In0; 4340 typedef typename In::In1 In1; 4341 typedef typename In::In2 In2; 4342 typedef typename In::In3 In3; 4343 typedef typename Out::Out0 Out0; 4344 typedef typename Out::Out1 Out1; 4345 4346 const FloatFormat& fmt = getFormat(); 4347 const int inCount = numInputs<In>(); 4348 const int outCount = numOutputs<Out>(); 4349 const size_t numValues = (inCount > 0) ? inputs.in0.size() : 1; 4350 Outputs<Out> outputs (numValues); 4351 ShaderSpec spec; 4352 const FloatFormat highpFmt = m_ctx.highpFormat; 4353 const int maxMsgs = 100; 4354 int numErrors = 0; 4355 Environment env; // Hoisted out of the inner loop for optimization. 4356 4357 switch (inCount) 4358 { 4359 case 4: DE_ASSERT(inputs.in3.size() == numValues); 4360 case 3: DE_ASSERT(inputs.in2.size() == numValues); 4361 case 2: DE_ASSERT(inputs.in1.size() == numValues); 4362 case 1: DE_ASSERT(inputs.in0.size() == numValues); 4363 default: break; 4364 } 4365 4366 // Print out the statement and its definitions 4367 log() << TestLog::Message << "Statement: " << stmt << TestLog::EndMessage; 4368 { 4369 ostringstream oss; 4370 FuncSet funcs; 4371 4372 stmt.getUsedFuncs(funcs); 4373 for (FuncSet::const_iterator it = funcs.begin(); it != funcs.end(); ++it) 4374 { 4375 (*it)->printDefinition(oss); 4376 } 4377 if (!funcs.empty()) 4378 log() << TestLog::Message << "Reference definitions:\n" << oss.str() 4379 << TestLog::EndMessage; 4380 } 4381 4382 // Initialize ShaderSpec from precision, variables and statement. 4383 { 4384 ostringstream os; 4385 os << "precision " << glu::getPrecisionName(m_ctx.precision) << " float;\n"; 4386 spec.globalDeclarations = os.str(); 4387 } 4388 4389 spec.version = getContextTypeGLSLVersion(getRenderContext().getType()); 4390 4391 if (!m_extension.empty()) 4392 spec.globalDeclarations = "#extension " + m_extension + " : require\n"; 4393 4394 spec.inputs.resize(inCount); 4395 4396 switch (inCount) 4397 { 4398 case 4: spec.inputs[3] = makeSymbol(*variables.in3); 4399 case 3: spec.inputs[2] = makeSymbol(*variables.in2); 4400 case 2: spec.inputs[1] = makeSymbol(*variables.in1); 4401 case 1: spec.inputs[0] = makeSymbol(*variables.in0); 4402 default: break; 4403 } 4404 4405 spec.outputs.resize(outCount); 4406 4407 switch (outCount) 4408 { 4409 case 2: spec.outputs[1] = makeSymbol(*variables.out1); 4410 case 1: spec.outputs[0] = makeSymbol(*variables.out0); 4411 default: break; 4412 } 4413 4414 spec.source = de::toString(stmt); 4415 4416 // Run the shader with inputs. 4417 { 4418 UniquePtr<ShaderExecutor> executor (createExecutor(getRenderContext(), 4419 m_ctx.shaderType, 4420 spec)); 4421 const void* inputArr[] = 4422 { 4423 &inputs.in0.front(), &inputs.in1.front(), &inputs.in2.front(), &inputs.in3.front(), 4424 }; 4425 void* outputArr[] = 4426 { 4427 &outputs.out0.front(), &outputs.out1.front(), 4428 }; 4429 4430 executor->log(log()); 4431 if (!executor->isOk()) 4432 TCU_FAIL("Shader compilation failed"); 4433 4434 executor->useProgram(); 4435 executor->execute(int(numValues), inputArr, outputArr); 4436 } 4437 4438 // Initialize environment with dummy values so we don't need to bind in inner loop. 4439 { 4440 const typename Traits<In0>::IVal in0; 4441 const typename Traits<In1>::IVal in1; 4442 const typename Traits<In2>::IVal in2; 4443 const typename Traits<In3>::IVal in3; 4444 const typename Traits<Out0>::IVal reference0; 4445 const typename Traits<Out1>::IVal reference1; 4446 4447 env.bind(*variables.in0, in0); 4448 env.bind(*variables.in1, in1); 4449 env.bind(*variables.in2, in2); 4450 env.bind(*variables.in3, in3); 4451 env.bind(*variables.out0, reference0); 4452 env.bind(*variables.out1, reference1); 4453 } 4454 4455 // For each input tuple, compute output reference interval and compare 4456 // shader output to the reference. 4457 for (size_t valueNdx = 0; valueNdx < numValues; valueNdx++) 4458 { 4459 bool result = true; 4460 typename Traits<Out0>::IVal reference0; 4461 typename Traits<Out1>::IVal reference1; 4462 4463 env.lookup(*variables.in0) = convert<In0>(fmt, round(fmt, inputs.in0[valueNdx])); 4464 env.lookup(*variables.in1) = convert<In1>(fmt, round(fmt, inputs.in1[valueNdx])); 4465 env.lookup(*variables.in2) = convert<In2>(fmt, round(fmt, inputs.in2[valueNdx])); 4466 env.lookup(*variables.in3) = convert<In3>(fmt, round(fmt, inputs.in3[valueNdx])); 4467 4468 { 4469 EvalContext ctx (fmt, m_ctx.precision, env); 4470 stmt.execute(ctx); 4471 } 4472 4473 switch (outCount) 4474 { 4475 case 2: 4476 reference1 = convert<Out1>(highpFmt, env.lookup(*variables.out1)); 4477 if (!m_status.check(contains(reference1, outputs.out1[valueNdx]), 4478 "Shader output 1 is outside acceptable range")) 4479 result = false; 4480 case 1: 4481 reference0 = convert<Out0>(highpFmt, env.lookup(*variables.out0)); 4482 if (!m_status.check(contains(reference0, outputs.out0[valueNdx]), 4483 "Shader output 0 is outside acceptable range")) 4484 result = false; 4485 default: break; 4486 } 4487 4488 if (!result) 4489 ++numErrors; 4490 4491 if ((!result && numErrors <= maxMsgs) || GLS_LOG_ALL_RESULTS) 4492 { 4493 MessageBuilder builder = log().message(); 4494 4495 builder << (result ? "Passed" : "Failed") << " sample:\n"; 4496 4497 if (inCount > 0) 4498 { 4499 builder << "\t" << variables.in0->getName() << " = " 4500 << valueToString(highpFmt, inputs.in0[valueNdx]) << "\n"; 4501 } 4502 4503 if (inCount > 1) 4504 { 4505 builder << "\t" << variables.in1->getName() << " = " 4506 << valueToString(highpFmt, inputs.in1[valueNdx]) << "\n"; 4507 } 4508 4509 if (inCount > 2) 4510 { 4511 builder << "\t" << variables.in2->getName() << " = " 4512 << valueToString(highpFmt, inputs.in2[valueNdx]) << "\n"; 4513 } 4514 4515 if (inCount > 3) 4516 { 4517 builder << "\t" << variables.in3->getName() << " = " 4518 << valueToString(highpFmt, inputs.in3[valueNdx]) << "\n"; 4519 } 4520 4521 if (outCount > 0) 4522 { 4523 builder << "\t" << variables.out0->getName() << " = " 4524 << valueToString(highpFmt, outputs.out0[valueNdx]) << "\n" 4525 << "\tExpected range: " 4526 << intervalToString<typename Out::Out0>(highpFmt, reference0) << "\n"; 4527 } 4528 4529 if (outCount > 1) 4530 { 4531 builder << "\t" << variables.out1->getName() << " = " 4532 << valueToString(highpFmt, outputs.out1[valueNdx]) << "\n" 4533 << "\tExpected range: " 4534 << intervalToString<typename Out::Out1>(highpFmt, reference1) << "\n"; 4535 } 4536 4537 builder << TestLog::EndMessage; 4538 } 4539 } 4540 4541 if (numErrors > maxMsgs) 4542 { 4543 log() << TestLog::Message << "(Skipped " << (numErrors - maxMsgs) << " messages.)" 4544 << TestLog::EndMessage; 4545 } 4546 4547 if (numErrors == 0) 4548 { 4549 log() << TestLog::Message << "All " << numValues << " inputs passed." 4550 << TestLog::EndMessage; 4551 } 4552 else 4553 { 4554 log() << TestLog::Message << numErrors << "/" << numValues << " inputs failed." 4555 << TestLog::EndMessage; 4556 } 4557} 4558 4559 4560 4561template <typename T> 4562struct InputLess 4563{ 4564 bool operator() (const T& val1, const T& val2) const 4565 { 4566 return val1 < val2; 4567 } 4568}; 4569 4570template <typename T> 4571bool inputLess (const T& val1, const T& val2) 4572{ 4573 return InputLess<T>()(val1, val2); 4574} 4575 4576template <> 4577struct InputLess<float> 4578{ 4579 bool operator() (const float& val1, const float& val2) const 4580 { 4581 if (deIsNaN(val1)) 4582 return false; 4583 if (deIsNaN(val2)) 4584 return true; 4585 return val1 < val2; 4586 } 4587}; 4588 4589template <typename T, int Size> 4590struct InputLess<Vector<T, Size> > 4591{ 4592 bool operator() (const Vector<T, Size>& vec1, const Vector<T, Size>& vec2) const 4593 { 4594 for (int ndx = 0; ndx < Size; ++ndx) 4595 { 4596 if (inputLess(vec1[ndx], vec2[ndx])) 4597 return true; 4598 if (inputLess(vec2[ndx], vec1[ndx])) 4599 return false; 4600 } 4601 4602 return false; 4603 } 4604}; 4605 4606template <typename T, int Rows, int Cols> 4607struct InputLess<Matrix<T, Rows, Cols> > 4608{ 4609 bool operator() (const Matrix<T, Rows, Cols>& mat1, 4610 const Matrix<T, Rows, Cols>& mat2) const 4611 { 4612 for (int col = 0; col < Cols; ++col) 4613 { 4614 if (inputLess(mat1[col], mat2[col])) 4615 return true; 4616 if (inputLess(mat2[col], mat1[col])) 4617 return false; 4618 } 4619 4620 return false; 4621 } 4622}; 4623 4624template <typename In> 4625struct InTuple : 4626 public Tuple4<typename In::In0, typename In::In1, typename In::In2, typename In::In3> 4627{ 4628 InTuple (const typename In::In0& in0, 4629 const typename In::In1& in1, 4630 const typename In::In2& in2, 4631 const typename In::In3& in3) 4632 : Tuple4<typename In::In0, typename In::In1, typename In::In2, typename In::In3> 4633 (in0, in1, in2, in3) {} 4634}; 4635 4636template <typename In> 4637struct InputLess<InTuple<In> > 4638{ 4639 bool operator() (const InTuple<In>& in1, const InTuple<In>& in2) const 4640 { 4641 if (inputLess(in1.a, in2.a)) 4642 return true; 4643 if (inputLess(in2.a, in1.a)) 4644 return false; 4645 if (inputLess(in1.b, in2.b)) 4646 return true; 4647 if (inputLess(in2.b, in1.b)) 4648 return false; 4649 if (inputLess(in1.c, in2.c)) 4650 return true; 4651 if (inputLess(in2.c, in1.c)) 4652 return false; 4653 if (inputLess(in1.d, in2.d)) 4654 return true; 4655 return false; 4656 }; 4657}; 4658 4659template<typename In> 4660Inputs<In> generateInputs (const Samplings<In>& samplings, 4661 const FloatFormat& floatFormat, 4662 Precision intPrecision, 4663 size_t numSamples, 4664 Random& rnd) 4665{ 4666 Inputs<In> ret; 4667 Inputs<In> fixedInputs; 4668 set<InTuple<In>, InputLess<InTuple<In> > > seenInputs; 4669 4670 samplings.in0.genFixeds(floatFormat, fixedInputs.in0); 4671 samplings.in1.genFixeds(floatFormat, fixedInputs.in1); 4672 samplings.in2.genFixeds(floatFormat, fixedInputs.in2); 4673 samplings.in3.genFixeds(floatFormat, fixedInputs.in3); 4674 4675 for (size_t ndx0 = 0; ndx0 < fixedInputs.in0.size(); ++ndx0) 4676 { 4677 for (size_t ndx1 = 0; ndx1 < fixedInputs.in1.size(); ++ndx1) 4678 { 4679 for (size_t ndx2 = 0; ndx2 < fixedInputs.in2.size(); ++ndx2) 4680 { 4681 for (size_t ndx3 = 0; ndx3 < fixedInputs.in3.size(); ++ndx3) 4682 { 4683 const InTuple<In> tuple (fixedInputs.in0[ndx0], 4684 fixedInputs.in1[ndx1], 4685 fixedInputs.in2[ndx2], 4686 fixedInputs.in3[ndx3]); 4687 4688 seenInputs.insert(tuple); 4689 ret.in0.push_back(tuple.a); 4690 ret.in1.push_back(tuple.b); 4691 ret.in2.push_back(tuple.c); 4692 ret.in3.push_back(tuple.d); 4693 } 4694 } 4695 } 4696 } 4697 4698 for (size_t ndx = 0; ndx < numSamples; ++ndx) 4699 { 4700 const typename In::In0 in0 = samplings.in0.genRandom(floatFormat, intPrecision, rnd); 4701 const typename In::In1 in1 = samplings.in1.genRandom(floatFormat, intPrecision, rnd); 4702 const typename In::In2 in2 = samplings.in2.genRandom(floatFormat, intPrecision, rnd); 4703 const typename In::In3 in3 = samplings.in3.genRandom(floatFormat, intPrecision, rnd); 4704 const InTuple<In> tuple (in0, in1, in2, in3); 4705 4706 if (de::contains(seenInputs, tuple)) 4707 continue; 4708 4709 seenInputs.insert(tuple); 4710 ret.in0.push_back(in0); 4711 ret.in1.push_back(in1); 4712 ret.in2.push_back(in2); 4713 ret.in3.push_back(in3); 4714 } 4715 4716 return ret; 4717} 4718 4719class FuncCaseBase : public PrecisionCase 4720{ 4721public: 4722 IterateResult iterate (void); 4723 4724protected: 4725 FuncCaseBase (const Context& context, 4726 const string& name, 4727 const FuncBase& func) 4728 : PrecisionCase (context, name, func.getRequiredExtension()) {} 4729}; 4730 4731IterateResult FuncCaseBase::iterate (void) 4732{ 4733 MovePtr<ContextInfo> info (ContextInfo::create(getRenderContext())); 4734 4735 if (!m_extension.empty() && !info->isExtensionSupported(m_extension.c_str())) 4736 throw NotSupportedError("Unsupported extension: " + m_extension); 4737 4738 runTest(); 4739 4740 m_status.setTestContextResult(m_testCtx); 4741 return STOP; 4742} 4743 4744template <typename Sig> 4745class FuncCase : public FuncCaseBase 4746{ 4747public: 4748 typedef Func<Sig> CaseFunc; 4749 typedef typename Sig::Ret Ret; 4750 typedef typename Sig::Arg0 Arg0; 4751 typedef typename Sig::Arg1 Arg1; 4752 typedef typename Sig::Arg2 Arg2; 4753 typedef typename Sig::Arg3 Arg3; 4754 typedef InTypes<Arg0, Arg1, Arg2, Arg3> In; 4755 typedef OutTypes<Ret> Out; 4756 4757 FuncCase (const Context& context, 4758 const string& name, 4759 const CaseFunc& func) 4760 : FuncCaseBase (context, name, func) 4761 , m_func (func) {} 4762 4763protected: 4764 void runTest (void); 4765 4766 virtual const Samplings<In>& getSamplings (void) 4767 { 4768 return instance<DefaultSamplings<In> >(); 4769 } 4770 4771private: 4772 const CaseFunc& m_func; 4773}; 4774 4775template <typename Sig> 4776void FuncCase<Sig>::runTest (void) 4777{ 4778 const Inputs<In> inputs (generateInputs(getSamplings(), 4779 m_ctx.floatFormat, 4780 m_ctx.precision, 4781 m_ctx.numRandoms, 4782 m_rnd)); 4783 Variables<In, Out> variables; 4784 4785 variables.out0 = variable<Ret>("out0"); 4786 variables.out1 = variable<Void>("out1"); 4787 variables.in0 = variable<Arg0>("in0"); 4788 variables.in1 = variable<Arg1>("in1"); 4789 variables.in2 = variable<Arg2>("in2"); 4790 variables.in3 = variable<Arg3>("in3"); 4791 4792 { 4793 ExprP<Ret> expr = applyVar(m_func, 4794 variables.in0, variables.in1, 4795 variables.in2, variables.in3); 4796 StatementP stmt = variableAssignment(variables.out0, expr); 4797 4798 this->testStatement(variables, inputs, *stmt); 4799 } 4800} 4801 4802template <typename Sig> 4803class InOutFuncCase : public FuncCaseBase 4804{ 4805public: 4806 typedef Func<Sig> CaseFunc; 4807 typedef typename Sig::Ret Ret; 4808 typedef typename Sig::Arg0 Arg0; 4809 typedef typename Sig::Arg1 Arg1; 4810 typedef typename Sig::Arg2 Arg2; 4811 typedef typename Sig::Arg3 Arg3; 4812 typedef InTypes<Arg0, Arg2, Arg3> In; 4813 typedef OutTypes<Ret, Arg1> Out; 4814 4815 InOutFuncCase (const Context& context, 4816 const string& name, 4817 const CaseFunc& func) 4818 : FuncCaseBase (context, name, func) 4819 , m_func (func) {} 4820 4821protected: 4822 void runTest (void); 4823 4824 virtual const Samplings<In>& getSamplings (void) 4825 { 4826 return instance<DefaultSamplings<In> >(); 4827 } 4828 4829private: 4830 const CaseFunc& m_func; 4831}; 4832 4833template <typename Sig> 4834void InOutFuncCase<Sig>::runTest (void) 4835{ 4836 const Inputs<In> inputs (generateInputs(getSamplings(), 4837 m_ctx.floatFormat, 4838 m_ctx.precision, 4839 m_ctx.numRandoms, 4840 m_rnd)); 4841 Variables<In, Out> variables; 4842 4843 variables.out0 = variable<Ret>("out0"); 4844 variables.out1 = variable<Arg1>("out1"); 4845 variables.in0 = variable<Arg0>("in0"); 4846 variables.in1 = variable<Arg2>("in1"); 4847 variables.in2 = variable<Arg3>("in2"); 4848 variables.in3 = variable<Void>("in3"); 4849 4850 { 4851 ExprP<Ret> expr = applyVar(m_func, 4852 variables.in0, variables.out1, 4853 variables.in1, variables.in2); 4854 StatementP stmt = variableAssignment(variables.out0, expr); 4855 4856 this->testStatement(variables, inputs, *stmt); 4857 } 4858} 4859 4860template <typename Sig> 4861PrecisionCase* createFuncCase (const Context& context, 4862 const string& name, 4863 const Func<Sig>& func) 4864{ 4865 switch (func.getOutParamIndex()) 4866 { 4867 case -1: 4868 return new FuncCase<Sig>(context, name, func); 4869 case 1: 4870 return new InOutFuncCase<Sig>(context, name, func); 4871 default: 4872 DE_ASSERT(!"Impossible"); 4873 } 4874 return DE_NULL; 4875} 4876 4877class CaseFactory 4878{ 4879public: 4880 virtual ~CaseFactory (void) {} 4881 virtual MovePtr<TestNode> createCase (const Context& ctx) const = 0; 4882 virtual string getName (void) const = 0; 4883 virtual string getDesc (void) const = 0; 4884}; 4885 4886class FuncCaseFactory : public CaseFactory 4887{ 4888public: 4889 virtual const FuncBase& getFunc (void) const = 0; 4890 4891 string getName (void) const 4892 { 4893 return de::toLower(getFunc().getName()); 4894 } 4895 4896 string getDesc (void) const 4897 { 4898 return "Function '" + getFunc().getName() + "'"; 4899 } 4900}; 4901 4902template <typename Sig> 4903class GenFuncCaseFactory : public CaseFactory 4904{ 4905public: 4906 4907 GenFuncCaseFactory (const GenFuncs<Sig>& funcs, 4908 const string& name) 4909 : m_funcs (funcs) 4910 , m_name (de::toLower(name)) {} 4911 4912 MovePtr<TestNode> createCase (const Context& ctx) const 4913 { 4914 TestCaseGroup* group = new TestCaseGroup(ctx.testContext, 4915 ctx.name.c_str(), ctx.name.c_str()); 4916 4917 group->addChild(createFuncCase(ctx, "scalar", m_funcs.func)); 4918 group->addChild(createFuncCase(ctx, "vec2", m_funcs.func2)); 4919 group->addChild(createFuncCase(ctx, "vec3", m_funcs.func3)); 4920 group->addChild(createFuncCase(ctx, "vec4", m_funcs.func4)); 4921 4922 return MovePtr<TestNode>(group); 4923 } 4924 4925 string getName (void) const 4926 { 4927 return m_name; 4928 } 4929 4930 string getDesc (void) const 4931 { 4932 return "Function '" + m_funcs.func.getName() + "'"; 4933 } 4934 4935private: 4936 const GenFuncs<Sig> m_funcs; 4937 string m_name; 4938}; 4939 4940template <template <int> class GenF> 4941class TemplateFuncCaseFactory : public FuncCaseFactory 4942{ 4943public: 4944 MovePtr<TestNode> createCase (const Context& ctx) const 4945 { 4946 TestCaseGroup* group = new TestCaseGroup(ctx.testContext, 4947 ctx.name.c_str(), ctx.name.c_str()); 4948 group->addChild(createFuncCase(ctx, "scalar", instance<GenF<1> >())); 4949 group->addChild(createFuncCase(ctx, "vec2", instance<GenF<2> >())); 4950 group->addChild(createFuncCase(ctx, "vec3", instance<GenF<3> >())); 4951 group->addChild(createFuncCase(ctx, "vec4", instance<GenF<4> >())); 4952 4953 return MovePtr<TestNode>(group); 4954 } 4955 4956 const FuncBase& getFunc (void) const { return instance<GenF<1> >(); } 4957}; 4958 4959template <template <int> class GenF> 4960class SquareMatrixFuncCaseFactory : public FuncCaseFactory 4961{ 4962public: 4963 MovePtr<TestNode> createCase (const Context& ctx) const 4964 { 4965 TestCaseGroup* group = new TestCaseGroup(ctx.testContext, 4966 ctx.name.c_str(), ctx.name.c_str()); 4967 group->addChild(createFuncCase(ctx, "mat2", instance<GenF<2> >())); 4968#if 0 4969 // disabled until we get reasonable results 4970 group->addChild(createFuncCase(ctx, "mat3", instance<GenF<3> >())); 4971 group->addChild(createFuncCase(ctx, "mat4", instance<GenF<4> >())); 4972#endif 4973 4974 return MovePtr<TestNode>(group); 4975 } 4976 4977 const FuncBase& getFunc (void) const { return instance<GenF<2> >(); } 4978}; 4979 4980template <template <int, int> class GenF> 4981class MatrixFuncCaseFactory : public FuncCaseFactory 4982{ 4983public: 4984 MovePtr<TestNode> createCase (const Context& ctx) const 4985 { 4986 TestCaseGroup* const group = new TestCaseGroup(ctx.testContext, 4987 ctx.name.c_str(), ctx.name.c_str()); 4988 4989 this->addCase<2, 2>(ctx, group); 4990 this->addCase<3, 2>(ctx, group); 4991 this->addCase<4, 2>(ctx, group); 4992 this->addCase<2, 3>(ctx, group); 4993 this->addCase<3, 3>(ctx, group); 4994 this->addCase<4, 3>(ctx, group); 4995 this->addCase<2, 4>(ctx, group); 4996 this->addCase<3, 4>(ctx, group); 4997 this->addCase<4, 4>(ctx, group); 4998 4999 return MovePtr<TestNode>(group); 5000 } 5001 5002 const FuncBase& getFunc (void) const { return instance<GenF<2,2> >(); } 5003 5004private: 5005 template <int Rows, int Cols> 5006 void addCase (const Context& ctx, TestCaseGroup* group) const 5007 { 5008 const char* const name = dataTypeNameOf<Matrix<float, Rows, Cols> >(); 5009 5010 group->addChild(createFuncCase(ctx, name, instance<GenF<Rows, Cols> >())); 5011 } 5012}; 5013 5014template <typename Sig> 5015class SimpleFuncCaseFactory : public CaseFactory 5016{ 5017public: 5018 SimpleFuncCaseFactory (const Func<Sig>& func) : m_func(func) {} 5019 5020 MovePtr<TestNode> createCase (const Context& ctx) const 5021 { 5022 return MovePtr<TestNode>(createFuncCase(ctx, ctx.name.c_str(), m_func)); 5023 } 5024 5025 string getName (void) const 5026 { 5027 return de::toLower(m_func.getName()); 5028 } 5029 5030 string getDesc (void) const 5031 { 5032 return "Function '" + getName() + "'"; 5033 } 5034 5035private: 5036 const Func<Sig>& m_func; 5037}; 5038 5039template <typename F> 5040SharedPtr<SimpleFuncCaseFactory<typename F::Sig> > createSimpleFuncCaseFactory (void) 5041{ 5042 return SharedPtr<SimpleFuncCaseFactory<typename F::Sig> >( 5043 new SimpleFuncCaseFactory<typename F::Sig>(instance<F>())); 5044} 5045 5046class BuiltinFuncs : public CaseFactories 5047{ 5048public: 5049 const vector<const CaseFactory*> getFactories (void) const 5050 { 5051 vector<const CaseFactory*> ret; 5052 5053 for (size_t ndx = 0; ndx < m_factories.size(); ++ndx) 5054 ret.push_back(m_factories[ndx].get()); 5055 5056 return ret; 5057 } 5058 5059 void addFactory (SharedPtr<const CaseFactory> fact) 5060 { 5061 m_factories.push_back(fact); 5062 } 5063 5064private: 5065 vector<SharedPtr<const CaseFactory> > m_factories; 5066}; 5067 5068template <typename F> 5069void addScalarFactory(BuiltinFuncs& funcs, string name = "") 5070{ 5071 if (name.empty()) 5072 name = instance<F>().getName(); 5073 5074 funcs.addFactory(SharedPtr<const CaseFactory>(new GenFuncCaseFactory<typename F::Sig>( 5075 makeVectorizedFuncs<F>(), name))); 5076} 5077 5078MovePtr<const CaseFactories> createES3BuiltinCases (void) 5079{ 5080 MovePtr<BuiltinFuncs> funcs (new BuiltinFuncs()); 5081 5082 addScalarFactory<Add>(*funcs); 5083 addScalarFactory<Sub>(*funcs); 5084 addScalarFactory<Mul>(*funcs); 5085 addScalarFactory<Div>(*funcs); 5086 5087 addScalarFactory<Radians>(*funcs); 5088 addScalarFactory<Degrees>(*funcs); 5089 addScalarFactory<Sin>(*funcs); 5090 addScalarFactory<Cos>(*funcs); 5091 addScalarFactory<Tan>(*funcs); 5092 addScalarFactory<ASin>(*funcs); 5093 addScalarFactory<ACos>(*funcs); 5094 addScalarFactory<ATan2>(*funcs, "atan2"); 5095 addScalarFactory<ATan>(*funcs); 5096 addScalarFactory<Sinh>(*funcs); 5097 addScalarFactory<Cosh>(*funcs); 5098 addScalarFactory<Tanh>(*funcs); 5099 addScalarFactory<ASinh>(*funcs); 5100 addScalarFactory<ACosh>(*funcs); 5101 addScalarFactory<ATanh>(*funcs); 5102 5103 addScalarFactory<Pow>(*funcs); 5104 addScalarFactory<Exp>(*funcs); 5105 addScalarFactory<Log>(*funcs); 5106 addScalarFactory<Exp2>(*funcs); 5107 addScalarFactory<Log2>(*funcs); 5108 addScalarFactory<Sqrt>(*funcs); 5109 addScalarFactory<InverseSqrt>(*funcs); 5110 5111 addScalarFactory<Abs>(*funcs); 5112 addScalarFactory<Sign>(*funcs); 5113 addScalarFactory<Floor>(*funcs); 5114 addScalarFactory<Trunc>(*funcs); 5115 addScalarFactory<Round>(*funcs); 5116 addScalarFactory<RoundEven>(*funcs); 5117 addScalarFactory<Ceil>(*funcs); 5118 addScalarFactory<Fract>(*funcs); 5119 addScalarFactory<Mod>(*funcs); 5120 funcs->addFactory(createSimpleFuncCaseFactory<Modf>()); 5121 addScalarFactory<Min>(*funcs); 5122 addScalarFactory<Max>(*funcs); 5123 addScalarFactory<Clamp>(*funcs); 5124 addScalarFactory<Mix>(*funcs); 5125 addScalarFactory<Step>(*funcs); 5126 addScalarFactory<SmoothStep>(*funcs); 5127 5128 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Length>())); 5129 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Distance>())); 5130 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Dot>())); 5131 funcs->addFactory(createSimpleFuncCaseFactory<Cross>()); 5132 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Normalize>())); 5133 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<FaceForward>())); 5134 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Reflect>())); 5135 funcs->addFactory(SharedPtr<const CaseFactory>(new TemplateFuncCaseFactory<Refract>())); 5136 5137 5138 funcs->addFactory(SharedPtr<const CaseFactory>(new MatrixFuncCaseFactory<MatrixCompMult>())); 5139 funcs->addFactory(SharedPtr<const CaseFactory>(new MatrixFuncCaseFactory<OuterProduct>())); 5140 funcs->addFactory(SharedPtr<const CaseFactory>(new MatrixFuncCaseFactory<Transpose>())); 5141 funcs->addFactory(SharedPtr<const CaseFactory>(new SquareMatrixFuncCaseFactory<Determinant>())); 5142 funcs->addFactory(SharedPtr<const CaseFactory>(new SquareMatrixFuncCaseFactory<Inverse>())); 5143 5144 return MovePtr<const CaseFactories>(funcs.release()); 5145} 5146 5147MovePtr<const CaseFactories> createES31BuiltinCases (void) 5148{ 5149 MovePtr<BuiltinFuncs> funcs (new BuiltinFuncs()); 5150 5151 addScalarFactory<FrExp>(*funcs); 5152 addScalarFactory<LdExp>(*funcs); 5153 addScalarFactory<Fma>(*funcs); 5154 5155 return MovePtr<const CaseFactories>(funcs.release()); 5156} 5157 5158struct PrecisionTestContext 5159{ 5160 PrecisionTestContext (TestContext& testCtx_, 5161 RenderContext& renderCtx_, 5162 const FloatFormat& highp_, 5163 const FloatFormat& mediump_, 5164 const FloatFormat& lowp_, 5165 const vector<ShaderType>& shaderTypes_, 5166 int numRandoms_) 5167 : testCtx (testCtx_) 5168 , renderCtx (renderCtx_) 5169 , shaderTypes (shaderTypes_) 5170 , numRandoms (numRandoms_) 5171 { 5172 formats[glu::PRECISION_HIGHP] = &highp_; 5173 formats[glu::PRECISION_MEDIUMP] = &mediump_; 5174 formats[glu::PRECISION_LOWP] = &lowp_; 5175 } 5176 5177 TestContext& testCtx; 5178 RenderContext& renderCtx; 5179 const FloatFormat* formats[glu::PRECISION_LAST]; 5180 vector<ShaderType> shaderTypes; 5181 int numRandoms; 5182}; 5183 5184TestCaseGroup* createFuncGroup (const PrecisionTestContext& ctx, 5185 const CaseFactory& factory) 5186{ 5187 TestCaseGroup* const group = new TestCaseGroup(ctx.testCtx, 5188 factory.getName().c_str(), 5189 factory.getDesc().c_str()); 5190 5191 for (int precNdx = 0; precNdx < glu::PRECISION_LAST; ++precNdx) 5192 { 5193 const Precision precision = Precision(precNdx); 5194 const string precName (glu::getPrecisionName(precision)); 5195 const FloatFormat& fmt = *de::getSizedArrayElement<glu::PRECISION_LAST>(ctx.formats, precNdx); 5196 const FloatFormat& highpFmt = *de::getSizedArrayElement<glu::PRECISION_LAST>(ctx.formats, 5197 glu::PRECISION_HIGHP); 5198 5199 for (size_t shaderNdx = 0; shaderNdx < ctx.shaderTypes.size(); ++shaderNdx) 5200 { 5201 const ShaderType shaderType = ctx.shaderTypes[shaderNdx]; 5202 const string shaderName (glu::getShaderTypeName(shaderType)); 5203 const string name = precName + "_" + shaderName; 5204 const Context caseCtx (name, ctx.testCtx, ctx.renderCtx, fmt, highpFmt, 5205 precision, shaderType, ctx.numRandoms); 5206 5207 group->addChild(factory.createCase(caseCtx).release()); 5208 } 5209 } 5210 5211 return group; 5212} 5213 5214void addBuiltinPrecisionTests (TestContext& testCtx, 5215 RenderContext& renderCtx, 5216 const CaseFactories& cases, 5217 const vector<ShaderType>& shaderTypes, 5218 TestCaseGroup& dstGroup) 5219{ 5220 const FloatFormat highp (-126, 127, 23, true, 5221 tcu::MAYBE, // subnormals 5222 tcu::YES, // infinities 5223 tcu::MAYBE); // NaN 5224 // \todo [2014-04-01 lauri] Check these once Khronos bug 11840 is resolved. 5225 const FloatFormat mediump (-13, 13, 9, false); 5226 // A fixed-point format is just a floating point format with a fixed 5227 // exponent and support for subnormals. 5228 const FloatFormat lowp (0, 0, 7, false, tcu::YES); 5229 const PrecisionTestContext ctx (testCtx, renderCtx, highp, mediump, lowp, 5230 shaderTypes, 16384); 5231 5232 for (size_t ndx = 0; ndx < cases.getFactories().size(); ++ndx) 5233 dstGroup.addChild(createFuncGroup(ctx, *cases.getFactories()[ndx])); 5234} 5235 5236} // BuiltinPrecisionTests 5237} // gls 5238} // deqp 5239