From 44e842cd9c6376054a68bba391a239731151889c Mon Sep 17 00:00:00 2001 From: Chow Date: Mon, 23 Dec 2019 11:29:52 +0800 Subject: [PATCH] Add constant expression with mod Purpose: glsl spec allows to define array with the length of the result of constant expression, the arithmetic operation of "mod(float (7.1), float (4.0))" should be generate the array length at the shader compile time, but glslang didn't support mod operation for the constant expression in previous implementation; An example is as following: ########### #version 460 flat out highp int vtx_out_out0; void main (void) { float array[int(mod(float (7.1), float (4.0)))]; vtx_out_out0 = array.length(); } ########### --- Test/460.vert | 1 + Test/baseResults/460.vert.out | 24 ++++++++++++------------ glslang/MachineIndependent/Constant.cpp | 10 ++++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Test/460.vert b/Test/460.vert index fd87d8b11..cf4f4adaf 100644 --- a/Test/460.vert +++ b/Test/460.vert @@ -7,6 +7,7 @@ float f;;; void main() { bool b1; + float array[int(mod(float (7.1), float (4.0)))]; b1 = anyInvocation(b1); b1 = allInvocations(b1); b1 = allInvocationsEqual(b1); diff --git a/Test/baseResults/460.vert.out b/Test/baseResults/460.vert.out index 8fa659b30..7f4093bf7 100644 --- a/Test/baseResults/460.vert.out +++ b/Test/baseResults/460.vert.out @@ -4,18 +4,18 @@ Shader version: 460 0:7 Function Definition: main( ( global void) 0:7 Function Parameters: 0:? Sequence -0:10 move second child to first child ( temp bool) -0:10 'b1' ( temp bool) -0:10 anyInvocation ( global bool) -0:10 'b1' ( temp bool) 0:11 move second child to first child ( temp bool) 0:11 'b1' ( temp bool) -0:11 allInvocations ( global bool) +0:11 anyInvocation ( global bool) 0:11 'b1' ( temp bool) 0:12 move second child to first child ( temp bool) 0:12 'b1' ( temp bool) -0:12 allInvocationsEqual ( global bool) +0:12 allInvocations ( global bool) 0:12 'b1' ( temp bool) +0:13 move second child to first child ( temp bool) +0:13 'b1' ( temp bool) +0:13 allInvocationsEqual ( global bool) +0:13 'b1' ( temp bool) 0:? Linker Objects 0:? 'i' ( global int) 0:? 'f' ( global float) @@ -31,18 +31,18 @@ Shader version: 460 0:7 Function Definition: main( ( global void) 0:7 Function Parameters: 0:? Sequence -0:10 move second child to first child ( temp bool) -0:10 'b1' ( temp bool) -0:10 anyInvocation ( global bool) -0:10 'b1' ( temp bool) 0:11 move second child to first child ( temp bool) 0:11 'b1' ( temp bool) -0:11 allInvocations ( global bool) +0:11 anyInvocation ( global bool) 0:11 'b1' ( temp bool) 0:12 move second child to first child ( temp bool) 0:12 'b1' ( temp bool) -0:12 allInvocationsEqual ( global bool) +0:12 allInvocations ( global bool) 0:12 'b1' ( temp bool) +0:13 move second child to first child ( temp bool) +0:13 'b1' ( temp bool) +0:13 allInvocationsEqual ( global bool) +0:13 'b1' ( temp bool) 0:? Linker Objects 0:? 'i' ( global int) 0:? 'f' ( global float) diff --git a/glslang/MachineIndependent/Constant.cpp b/glslang/MachineIndependent/Constant.cpp index 98c2666fb..0002d3609 100644 --- a/glslang/MachineIndependent/Constant.cpp +++ b/glslang/MachineIndependent/Constant.cpp @@ -1012,6 +1012,7 @@ TIntermTyped* TIntermediate::fold(TIntermAggregate* aggrNode) case EOpMin: case EOpMax: case EOpMix: + case EOpMod: case EOpClamp: case EOpLessThan: case EOpGreaterThan: @@ -1074,6 +1075,15 @@ TIntermTyped* TIntermediate::fold(TIntermAggregate* aggrNode) case EOpPow: newConstArray[comp].setDConst(pow(childConstUnions[0][arg0comp].getDConst(), childConstUnions[1][arg1comp].getDConst())); break; + case EOpMod: + { + double arg0 = childConstUnions[0][arg0comp].getDConst(); + double arg1 = childConstUnions[1][arg1comp].getDConst(); + assert(arg1 != 0.0); + double result = arg0 - arg1 * floor(arg0 / arg1); + newConstArray[comp].setDConst(result); + break; + } case EOpMin: switch(children[0]->getAsTyped()->getBasicType()) { case EbtFloat16: