From d8509b3367c31be8397feaa18b55262ea40504fb Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Tue, 12 Jul 2016 01:26:43 -0600 Subject: [PATCH] Front-end: Implement compile-time constant folding for any() and all(). --- Test/baseResults/constFold.frag.out | 24 ++++++++++++++++++++++++ Test/constFold.frag | 9 ++++++++- glslang/MachineIndependent/Constant.cpp | 26 ++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/Test/baseResults/constFold.frag.out b/Test/baseResults/constFold.frag.out index f5c3e9c..2e78468 100644 --- a/Test/baseResults/constFold.frag.out +++ b/Test/baseResults/constFold.frag.out @@ -374,6 +374,18 @@ ERROR: node is still EOpNull! 0:? 1 (const int) 0:? 9.000000 0:? false (const bool) +0:? 'cval1' (const bool) +0:? true (const bool) +0:? 'cval2' (const bool) +0:? false (const bool) +0:? 'cval3' (const bool) +0:? false (const bool) +0:? 'cval4' (const bool) +0:? true (const bool) +0:? 'cval5' (const bool) +0:? false (const bool) +0:? 'cval6' (const bool) +0:? true (const bool) Linked fragment stage: @@ -744,4 +756,16 @@ ERROR: node is still EOpNull! 0:? 1 (const int) 0:? 9.000000 0:? false (const bool) +0:? 'cval1' (const bool) +0:? true (const bool) +0:? 'cval2' (const bool) +0:? false (const bool) +0:? 'cval3' (const bool) +0:? false (const bool) +0:? 'cval4' (const bool) +0:? true (const bool) +0:? 'cval5' (const bool) +0:? false (const bool) +0:? 'cval6' (const bool) +0:? true (const bool) diff --git a/Test/constFold.frag b/Test/constFold.frag index 97c3b2b..7306078 100644 --- a/Test/constFold.frag +++ b/Test/constFold.frag @@ -138,4 +138,11 @@ const cag a0[3] = cag[3](cag(3, 2.0, true), cag(1, 5.0, true), cag(1, 9.0, false void foo4() { int a = int(a0[2].f); -} \ No newline at end of file +} + +const bool cval1 = all(bvec4(true, true, true, true)); +const bool cval2 = all(bvec4(false, false, false, false)); +const bool cval3 = all(bvec4(true, true, false, true)); +const bool cval4 = any(bvec4(true, true, true, true)); +const bool cval5 = any(bvec4(false, false, false, false)); +const bool cval6 = any(bvec4(false, true, false, false)); diff --git a/glslang/MachineIndependent/Constant.cpp b/glslang/MachineIndependent/Constant.cpp index 03033bc..4adfd47 100644 --- a/glslang/MachineIndependent/Constant.cpp +++ b/glslang/MachineIndependent/Constant.cpp @@ -399,6 +399,27 @@ TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType) break; } + case EOpAny: + { + bool result = false; + for (int i = 0; i < objectSize; i++) { + if (unionArray[i].getBConst()) + result = true; + } + newConstArray[0].setBConst(result); + break; + } + case EOpAll: + { + bool result = true; + for (int i = 0; i < objectSize; i++) { + if (! unionArray[i].getBConst()) + result = false; + } + newConstArray[0].setBConst(result); + break; + } + // TODO: 3.0 Functionality: unary constant folding: the rest of the ops have to be fleshed out case EOpPackSnorm2x16: @@ -412,11 +433,8 @@ TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType) case EOpDeterminant: case EOpMatrixInverse: case EOpTranspose: - - case EOpAny: - case EOpAll: return 0; - + default: assert(componentWise); break; -- 2.7.4