Pārlūkot izejas kodu

Updated glsl-optimizer.

Branimir Karadžić 9 gadi atpakaļ
vecāks
revīzija
d3221c3404

+ 17 - 0
3rdparty/glsl-optimizer/Changelog.md

@@ -1,6 +1,23 @@
 GLSL optimizer Change Log
 =========================
 
+2016 06
+-------
+
+Fixed:
+
+* Fixed Metal translation in some cases having wrong precision on constants or constant arrays.
+
+
+2016 05
+-------
+
+Fixed:
+
+* Fixed Metal translation in some cases having wrong precision on struct members.
+* Fixed Metal translation in some cases emitting struct declarations vs. constant initializers in wrong order.
+
+
 2016 03
 -------
 

+ 6 - 0
3rdparty/glsl-optimizer/src/glsl/ast_to_hir.cpp

@@ -3042,6 +3042,12 @@ process_initializer(ir_variable *var, ast_declaration *decl,
    ir_dereference *const lhs = new(state) ir_dereference_variable(var);
    ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
 
+   /* Propagate precision qualifier for constant value */
+   if (type->qualifier.flags.q.constant) {
+      ir_constant *constant_value = rhs->constant_expression_value();
+      constant_value->set_precision((glsl_precision)type->qualifier.precision);
+   }
+	
    /* Calculate the constant value if this is a const or uniform
     * declaration.
     */

+ 2 - 2
3rdparty/glsl-optimizer/src/glsl/ir.cpp

@@ -647,8 +647,8 @@ ir_constant::ir_constant()
 }
 
 ir_constant::ir_constant(const struct glsl_type *type,
-			 const ir_constant_data *data)
-   : ir_rvalue(ir_type_constant, glsl_precision_undefined)
+			 const ir_constant_data *data, glsl_precision precision)
+   : ir_rvalue(ir_type_constant, precision)
 {
    assert((type->base_type >= GLSL_TYPE_UINT)
 	  && (type->base_type <= GLSL_TYPE_BOOL));

+ 1 - 1
3rdparty/glsl-optimizer/src/glsl/ir.h

@@ -2161,7 +2161,7 @@ union ir_constant_data {
 
 class ir_constant : public ir_rvalue {
 public:
-   ir_constant(const struct glsl_type *type, const ir_constant_data *data);
+   ir_constant(const struct glsl_type *type, const ir_constant_data *data, glsl_precision precision = glsl_precision_undefined);
    ir_constant(bool b, unsigned vector_elements=1);
    ir_constant(unsigned int u, unsigned vector_elements=1);
    ir_constant(int i, unsigned vector_elements=1);

+ 2 - 1
3rdparty/glsl-optimizer/src/glsl/ir_clone.cpp

@@ -330,7 +330,7 @@ ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
    case GLSL_TYPE_INT:
    case GLSL_TYPE_FLOAT:
    case GLSL_TYPE_BOOL:
-      return new(mem_ctx) ir_constant(this->type, &this->value);
+      return new(mem_ctx) ir_constant(this->type, &this->value, this->precision);
 
    case GLSL_TYPE_STRUCT: {
       ir_constant *c = new(mem_ctx) ir_constant;
@@ -351,6 +351,7 @@ ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
       ir_constant *c = new(mem_ctx) ir_constant;
 
       c->type = this->type;
+      c->set_precision(this->get_precision());
       c->array_elements = ralloc_array(c, ir_constant *, this->type->length);
       for (unsigned i = 0; i < this->type->length; i++) {
 	 c->array_elements[i] = this->array_elements[i]->clone(mem_ctx, NULL);

+ 9 - 2
3rdparty/glsl-optimizer/src/glsl/ir_print_metal_visitor.cpp

@@ -99,6 +99,7 @@ struct metal_print_context
 	, inoutStr(ralloc_strdup(buffer, ""))
 	, uniformStr(ralloc_strdup(buffer, ""))
 	, paramsStr(ralloc_strdup(buffer, ""))
+	, typedeclStr(ralloc_strdup(buffer, ""))
 	, writingParams(false)
 	, matrixCastsDone(false)
 	, matrixConstructorsDone(false)
@@ -117,6 +118,7 @@ struct metal_print_context
 	string_buffer inoutStr;
 	string_buffer uniformStr;
 	string_buffer paramsStr;
+	string_buffer typedeclStr;
 	bool writingParams;
 	bool matrixCastsDone;
 	bool matrixConstructorsDone;
@@ -267,7 +269,10 @@ _mesa_print_ir_metal(exec_list *instructions,
 			if (var->data.mode == ir_var_shader_inout)
 				strOut = &ctx.inoutStr;
 		}
-
+		
+		if (ir->ir_type == ir_type_typedecl) {
+			strOut = &ctx.typedeclStr;
+		}
 
 		ir_print_metal_visitor v (ctx, *strOut, &gtracker, mode, state);
 		v.loopstate = ls;
@@ -293,6 +298,8 @@ _mesa_print_ir_metal(exec_list *instructions,
 	ctx.uniformStr.asprintf_append("};\n");
 
 	// emit global array/struct constants
+	
+	ctx.prefixStr.asprintf_append("%s", ctx.typedeclStr.c_str());
 	foreach_in_list_safe(gconst_entry_metal, node, &gtracker.global_constants)
 	{
 		ir_constant* c = node->ir;
@@ -1968,7 +1975,7 @@ ir_print_metal_visitor::visit(ir_typedecl_statement *ir)
 		buffer.asprintf_append ("  ");
 		//if (state->es_shader)
 		//	buffer.asprintf_append ("%s", get_precision_string(s->fields.structure[j].precision)); //@TODO
-		print_type(buffer, ir, s->fields.structure[j].type, false);
+		print_type_precision(buffer, s->fields.structure[j].type, s->fields.structure[j].precision, false);
 		buffer.asprintf_append (" %s", s->fields.structure[j].name);
 		print_type_post(buffer, s->fields.structure[j].type, false);
 		buffer.asprintf_append (";\n");

+ 40 - 0
3rdparty/glsl-optimizer/tests/fragment/const-precision-inES3.txt

@@ -0,0 +1,40 @@
+#version 300 es
+#define gl_FragData _glesFragData
+layout(location = 0) out mediump vec4 _glesFragData[1];
+
+struct v2f {
+    highp vec4 pos;
+    mediump vec2 uv;
+};
+
+struct u2v {
+    highp vec4 vertex;
+    mediump vec2 texcoord;
+};
+
+const mediump vec3[3] ha = vec3[3]( vec3( 1.0, 2.0, 3.0), vec3( 4.0, 5.0, 6.0), vec3( 7.0, 8.0, 9.0));
+const highp vec3[3] fa = vec3[3]( vec3( 11.0, 12.0, 13.0), vec3( 14.0, 15.0, 16.0), vec3( 17.0, 18.0, 19.0));
+
+mediump vec4 frag( in v2f i ) {
+    mediump vec3 h = vec3( 0.0);
+    highp vec3 f = vec3( 0.0);
+    highp vec3 p = vec3( i.uv.xy, 1.0);
+    highp int j = 0;
+    for ( ; (j < int((i.uv.x * 3.0))); (j++)) {
+        h += ha[j];
+        f += fa[j];
+        f += (p * ha[0]);
+    }
+    return vec4( h.xy, f.xy);
+}
+
+in mediump vec2 xlv_TEXCOORD0;
+void main() {
+    mediump vec4 xl_retval;
+    v2f xlt_i;
+    xlt_i.pos = vec4(0.0);
+    xlt_i.uv = vec2(xlv_TEXCOORD0);
+    xl_retval = frag( xlt_i);
+    gl_FragData[0] = vec4(xl_retval);
+}
+

+ 33 - 0
3rdparty/glsl-optimizer/tests/fragment/const-precision-outES3.txt

@@ -0,0 +1,33 @@
+#version 300 es
+layout(location=0) out mediump vec4 _glesFragData[1];
+in mediump vec2 xlv_TEXCOORD0;
+void main ()
+{
+  mediump vec4 tmpvar_1;
+  mediump vec2 tmpvar_2;
+  tmpvar_2 = xlv_TEXCOORD0;
+  highp vec3 p_4;
+  highp vec3 f_5;
+  mediump vec3 h_6;
+  h_6 = vec3(0.0, 0.0, 0.0);
+  f_5 = vec3(0.0, 0.0, 0.0);
+  mediump vec3 tmpvar_7;
+  tmpvar_7.z = 1.0;
+  tmpvar_7.xy = xlv_TEXCOORD0;
+  p_4 = tmpvar_7;
+  for (highp int j_3 = 0; j_3 < int((tmpvar_2.x * 3.0)); j_3++) {
+    h_6 = (h_6 + vec3[3](vec3(1.0, 2.0, 3.0), vec3(4.0, 5.0, 6.0), vec3(7.0, 8.0, 9.0))[j_3]);
+    f_5 = (f_5 + vec3[3](vec3(11.0, 12.0, 13.0), vec3(14.0, 15.0, 16.0), vec3(17.0, 18.0, 19.0))[j_3]);
+    f_5 = (f_5 + (p_4 * vec3(1.0, 2.0, 3.0)));
+  };
+  highp vec4 tmpvar_8;
+  tmpvar_8.xy = h_6.xy;
+  tmpvar_8.zw = f_5.xy;
+  tmpvar_1 = tmpvar_8;
+  _glesFragData[0] = tmpvar_1;
+}
+
+
+// stats: 12 alu 0 tex 2 flow
+// inputs: 1
+//  #0: xlv_TEXCOORD0 (medium float) 2x1 [-1]

+ 45 - 0
3rdparty/glsl-optimizer/tests/fragment/const-precision-outES3Metal.txt

@@ -0,0 +1,45 @@
+#include <metal_stdlib>
+#pragma clang diagnostic ignored "-Wparentheses-equality"
+using namespace metal;
+constant half3 _xlat_mtl_const1[3] = {float3(1.0, 2.0, 3.0), float3(4.0, 5.0, 6.0), float3(7.0, 8.0, 9.0)};
+constant float3 _xlat_mtl_const2[3] = {float3(11.0, 12.0, 13.0), float3(14.0, 15.0, 16.0), float3(17.0, 18.0, 19.0)};
+struct xlatMtlShaderInput {
+  half2 xlv_TEXCOORD0;
+};
+struct xlatMtlShaderOutput {
+  half4 _glesFragData_0 [[color(0)]];
+};
+struct xlatMtlShaderUniform {
+};
+fragment xlatMtlShaderOutput xlatMtlMain (xlatMtlShaderInput _mtl_i [[stage_in]], constant xlatMtlShaderUniform& _mtl_u [[buffer(0)]])
+{
+  xlatMtlShaderOutput _mtl_o;
+  half4 tmpvar_1;
+  half2 tmpvar_2;
+  tmpvar_2 = _mtl_i.xlv_TEXCOORD0;
+  float3 p_4;
+  float3 f_5;
+  half3 h_6;
+  h_6 = half3(float3(0.0, 0.0, 0.0));
+  f_5 = float3(0.0, 0.0, 0.0);
+  half3 tmpvar_7;
+  tmpvar_7.z = half(1.0);
+  tmpvar_7.xy = _mtl_i.xlv_TEXCOORD0;
+  p_4 = float3(tmpvar_7);
+  for (int j_3 = 0; j_3 < short((tmpvar_2.x * (half)3.0)); j_3++) {
+    h_6 = (h_6 + _xlat_mtl_const1[j_3]);
+    f_5 = (f_5 + _xlat_mtl_const2[j_3]);
+    f_5 = (f_5 + (p_4 * float3(1.0, 2.0, 3.0)));
+  };
+  float4 tmpvar_8;
+  tmpvar_8.xy = float2(h_6.xy);
+  tmpvar_8.zw = f_5.xy;
+  tmpvar_1 = half4(tmpvar_8);
+  _mtl_o._glesFragData_0 = tmpvar_1;
+  return _mtl_o;
+}
+
+
+// stats: 12 alu 0 tex 2 flow
+// inputs: 1
+//  #0: xlv_TEXCOORD0 (medium float) 2x1 [-1]

+ 685 - 0
3rdparty/glsl-optimizer/tests/fragment/global-struct-constant-init-metal-inES3.txt

@@ -0,0 +1,685 @@
+#version 300 es
+vec4 xll_texCUBElod(samplerCube s, vec4 coord) {
+  return textureLod( s, coord.xyz, coord.w);
+}
+float xll_shadow2D(mediump sampler2DShadow s, vec3 coord) { return texture (s, coord); }
+float xll_saturate_f( float x) {
+  return clamp( x, 0.0, 1.0);
+}
+vec2 xll_saturate_vf2( vec2 x) {
+  return clamp( x, 0.0, 1.0);
+}
+vec3 xll_saturate_vf3( vec3 x) {
+  return clamp( x, 0.0, 1.0); 
+}
+vec4 xll_saturate_vf4( vec4 x) {
+  return clamp( x, 0.0, 1.0);
+}
+mat2 xll_saturate_mf2x2(mat2 m) {
+  return mat2( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0));
+}
+mat3 xll_saturate_mf3x3(mat3 m) {
+  return mat3( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0), clamp(m[2], 0.0, 1.0));
+}
+mat4 xll_saturate_mf4x4(mat4 m) {
+  return mat4( clamp(m[0], 0.0, 1.0), clamp(m[1], 0.0, 1.0), clamp(m[2], 0.0, 1.0), clamp(m[3], 0.0, 1.0));
+}
+
+struct v2f_vertex_lit {
+    highp vec2 uv;
+    lowp vec4 diff;
+    lowp vec4 spec;
+};
+
+struct v2f_img {
+    highp vec4 pos;
+    mediump vec2 uv;
+};
+
+struct appdata_img {
+    highp vec4 vertex;
+    mediump vec2 texcoord;
+};
+
+struct Unity_GlossyEnvironmentData {
+    mediump float roughness;
+    mediump vec3 reflUVW;
+};
+
+struct UnityLight {
+    mediump vec3 color;
+    mediump vec3 dir;
+    mediump float ndotl;
+};
+
+struct UnityIndirect {
+    mediump vec3 diffuse;
+    mediump vec3 specular;
+};
+
+struct UnityGI {
+    UnityLight light;
+    UnityIndirect indirect;
+};
+
+struct UnityGIInput {
+    UnityLight light;
+    highp vec3 worldPos;
+    mediump vec3 worldViewDir;
+    mediump float atten;
+    mediump vec3 ambient;
+    mediump vec4 lightmapUV;
+    highp vec4 boxMax[2];
+    highp vec4 boxMin[2];
+    highp vec4 probePosition[2];
+    highp vec4 probeHDR[2];
+};
+
+struct SurfaceOutputStandard {
+    lowp vec3 Albedo;
+    lowp vec3 Normal;
+    mediump vec3 Emission;
+    mediump float Metallic;
+    mediump float Smoothness;
+    mediump float Occlusion;
+    lowp float Alpha;
+};
+
+struct SurfaceOutputStandardSpecular {
+    lowp vec3 Albedo;
+    lowp vec3 Specular;
+    lowp vec3 Normal;
+    mediump vec3 Emission;
+    mediump float Smoothness;
+    mediump float Occlusion;
+    lowp float Alpha;
+};
+
+struct VertexInput {
+    highp vec4 vertex;
+    mediump vec3 normal;
+    highp vec2 uv0;
+    highp vec2 uv1;
+};
+
+struct FragmentCommonData {
+    mediump vec3 diffColor;
+    mediump vec3 specColor;
+    mediump float oneMinusReflectivity;
+    mediump float oneMinusRoughness;
+    mediump vec3 normalWorld;
+    mediump vec3 eyeVec;
+    mediump vec3 posWorld;
+    mediump float alpha;
+};
+
+struct VertexOutputForwardBase {
+    highp vec4 pos;
+    highp vec4 tex;
+    mediump vec3 eyeVec;
+    mediump vec4 tangentToWorldAndParallax[3];
+    mediump vec4 ambientOrLightmapUV;
+    mediump vec4 _ShadowCoord;
+};
+
+struct VertexOutputForwardAdd {
+    highp vec4 pos;
+    highp vec4 tex;
+    mediump vec3 eyeVec;
+    mediump vec4 tangentToWorldAndLightDir[3];
+    mediump vec4 _ShadowCoord;
+};
+
+struct VertexOutputDeferred {
+    highp vec4 pos;
+    highp vec4 tex;
+    mediump vec3 eyeVec;
+    mediump vec4 tangentToWorldAndParallax[3];
+    mediump vec4 ambientOrLightmapUV;
+};
+
+struct VertexInput_VC {
+    highp vec4 vertex;
+    lowp vec4 color;
+    mediump vec3 normal;
+    highp vec2 uv0;
+    highp vec2 uv1;
+};
+
+struct VertexOutputForwardBase_VC {
+    highp vec4 pos;
+    highp vec4 tex;
+    mediump vec3 eyeVec;
+    mediump vec4 tangentToWorldAndParallax[3];
+    mediump vec4 ambientOrLightmapUV;
+    mediump vec4 _ShadowCoord;
+    lowp vec4 color;
+};
+
+struct VertexOutputDeferred_VC {
+    highp vec4 pos;
+    lowp vec4 color;
+    highp vec4 tex;
+    mediump vec3 eyeVec;
+    mediump vec4 tangentToWorldAndParallax[3];
+    mediump vec4 ambientOrLightmapUV;
+};
+
+uniform highp vec4 _Time;
+uniform highp vec4 _SinTime;
+uniform highp vec4 _CosTime;
+uniform highp vec4 unity_DeltaTime;
+
+uniform highp vec3 _WorldSpaceCameraPos;
+
+uniform highp vec4 _ProjectionParams;
+
+uniform highp vec4 _ScreenParams;
+
+uniform highp vec4 _ZBufferParams;
+
+uniform highp vec4 unity_OrthoParams;
+
+uniform highp vec4 unity_CameraWorldClipPlanes[6];
+
+uniform highp mat4 unity_CameraProjection;
+uniform highp mat4 unity_CameraInvProjection;
+
+uniform mediump vec4 _WorldSpaceLightPos0;
+
+uniform highp vec4 _LightPositionRange;
+uniform highp vec4 unity_4LightPosX0;
+uniform highp vec4 unity_4LightPosY0;
+
+uniform highp vec4 unity_4LightPosZ0;
+uniform mediump vec4 unity_4LightAtten0;
+uniform mediump vec4 unity_LightColor[8];
+
+uniform highp vec4 unity_LightPosition[8];
+
+uniform mediump vec4 unity_LightAtten[8];
+uniform highp vec4 unity_SpotDirection[8];
+
+uniform mediump vec4 unity_SHAr;
+uniform mediump vec4 unity_SHAg;
+uniform mediump vec4 unity_SHAb;
+uniform mediump vec4 unity_SHBr;
+
+uniform mediump vec4 unity_SHBg;
+uniform mediump vec4 unity_SHBb;
+uniform mediump vec4 unity_SHC;
+
+uniform mediump vec3 unity_LightColor0;
+uniform mediump vec3 unity_LightColor1;
+uniform mediump vec3 unity_LightColor2;
+uniform mediump vec3 unity_LightColor3;
+
+uniform highp vec4 unity_ShadowSplitSpheres[4];
+uniform highp vec4 unity_ShadowSplitSqRadii;
+uniform highp vec4 unity_LightShadowBias;
+uniform highp vec4 _LightSplitsNear;
+
+uniform highp vec4 _LightSplitsFar;
+uniform highp mat4 unity_World2Shadow[4];
+uniform mediump vec4 _LightShadowData;
+uniform highp vec4 unity_ShadowFadeCenterAndType;
+
+uniform highp mat4 glstate_matrix_mvp;
+uniform highp mat4 glstate_matrix_modelview0;
+uniform highp mat4 glstate_matrix_invtrans_modelview0;
+
+uniform highp mat4 _Object2World;
+uniform highp mat4 _World2Object;
+uniform highp vec4 unity_LODFade;
+uniform highp vec4 unity_WorldTransformParams;
+
+uniform highp mat4 glstate_matrix_transpose_modelview0;
+
+uniform highp mat4 glstate_matrix_projection;
+uniform lowp vec4 glstate_lightmodel_ambient;
+
+uniform highp mat4 unity_MatrixV;
+uniform highp mat4 unity_MatrixVP;
+
+uniform lowp vec4 unity_AmbientSky;
+uniform lowp vec4 unity_AmbientEquator;
+uniform lowp vec4 unity_AmbientGround;
+
+uniform lowp vec4 unity_FogColor;
+
+uniform highp vec4 unity_FogParams;
+
+uniform sampler2D unity_Lightmap;
+uniform sampler2D unity_LightmapInd;
+
+uniform sampler2D unity_DynamicLightmap;
+uniform sampler2D unity_DynamicDirectionality;
+uniform sampler2D unity_DynamicNormal;
+
+uniform highp vec4 unity_LightmapST;
+uniform highp vec4 unity_DynamicLightmapST;
+
+uniform samplerCube unity_SpecCube0;
+uniform samplerCube unity_SpecCube1;
+
+uniform highp vec4 unity_SpecCube0_BoxMax;
+uniform highp vec4 unity_SpecCube0_BoxMin;
+uniform highp vec4 unity_SpecCube0_ProbePosition;
+uniform mediump vec4 unity_SpecCube0_HDR;
+
+uniform highp vec4 unity_SpecCube1_BoxMax;
+uniform highp vec4 unity_SpecCube1_BoxMin;
+uniform highp vec4 unity_SpecCube1_ProbePosition;
+uniform mediump vec4 unity_SpecCube1_HDR;
+
+uniform lowp vec4 unity_ColorSpaceGrey;
+uniform lowp vec4 unity_ColorSpaceDouble;
+uniform mediump vec4 unity_ColorSpaceDielectricSpec;
+uniform mediump vec4 unity_ColorSpaceLuminance;
+
+uniform mediump vec4 unity_Lightmap_HDR;
+
+uniform mediump vec4 unity_DynamicLightmap_HDR;
+
+uniform lowp vec4 _LightColor0;
+uniform lowp vec4 _SpecColor;
+
+uniform sampler2D unity_NHxRoughness;
+
+uniform mediump vec4 _Color;
+uniform mediump float _Cutoff;
+uniform sampler2D _MainTex;
+
+uniform highp vec4 _MainTex_ST;
+uniform sampler2D _DetailAlbedoMap;
+uniform highp vec4 _DetailAlbedoMap_ST;
+
+uniform sampler2D _BumpMap;
+uniform mediump float _BumpScale;
+uniform sampler2D _DetailMask;
+
+uniform sampler2D _DetailNormalMap;
+uniform mediump float _DetailNormalMapScale;
+uniform sampler2D _SpecGlossMap;
+
+uniform sampler2D _MetallicGlossMap;
+uniform mediump float _Metallic;
+uniform mediump float _Glossiness;
+
+uniform sampler2D _OcclusionMap;
+uniform mediump float _OcclusionStrength;
+uniform sampler2D _ParallaxMap;
+
+uniform mediump float _Parallax;
+uniform mediump float _UVSec;
+uniform mediump vec4 _EmissionColor;
+
+uniform sampler2D _EmissionMap;
+
+uniform lowp sampler2DShadow _ShadowMapTexture;
+
+mediump float DotClamped( in mediump vec3 a, in mediump vec3 b ) {    
+    return max( 0.0, dot( a, b));
+}
+
+mediump float BlinnTerm( in mediump vec3 normal, in mediump vec3 halfDir ) {
+    return DotClamped( normal, halfDir);
+}
+
+mediump float Pow4( in mediump float x ) {
+    return (((x * x) * x) * x);
+}
+
+mediump vec3 FresnelLerpFast( in mediump vec3 F0, in mediump vec3 F90, in mediump float cosA ) {
+    mediump float t = Pow4( (1.0 - cosA));
+    return mix( F0, F90, vec3( t));
+}
+
+bool IsGammaSpace(  ) {
+    return true;
+}
+
+mediump float RoughnessToSpecPower( in mediump float roughness ) {
+    
+    mediump float m = max( 0.0001, (roughness * roughness));
+    mediump float n = ((2.0 / (m * m)) - 2.0);
+    n = max( n, 0.0001);
+    
+    return n;
+}
+
+mediump vec3 Unity_SafeNormalize( in mediump vec3 inVec ) {
+    mediump float dp3 = max( 0.001, dot( inVec, inVec));
+    return (inVec * inversesqrt(dp3));
+}
+
+mediump vec4 BRDF2_Unity_PBS( in mediump vec3 diffColor, in mediump vec3 specColor, in mediump float oneMinusReflectivity, in mediump float oneMinusRoughness, in mediump vec3 normal, in mediump vec3 viewDir, in UnityLight light, in UnityIndirect gi ) {
+    mediump vec3 halfDir = Unity_SafeNormalize( (light.dir + viewDir));
+    
+    mediump float nl = light.ndotl;
+    mediump float nh = BlinnTerm( normal, halfDir);
+    mediump float nv = DotClamped( normal, viewDir);
+    mediump float lh = DotClamped( light.dir, halfDir);
+    
+    mediump float roughness = (1.0 - oneMinusRoughness);
+    mediump float specularPower = RoughnessToSpecPower( roughness);
+    
+    mediump float invV = (((lh * lh) * oneMinusRoughness) + (roughness * roughness));
+    mediump float invF = lh;
+    mediump float specular = (((specularPower + 1.0) * pow( nh, specularPower)) / (((8.0 * invV) * invF) + 0.0001));
+    if (IsGammaSpace( )){
+        specular = sqrt(max( 0.0001, specular));
+    }
+    
+    mediump float realRoughness = (roughness * roughness);
+    mediump float surfaceReduction = (( IsGammaSpace( ) ) ? ( 0.28 ) : ( (0.6 - (0.08 * roughness)) ));
+    
+    surfaceReduction = (1.0 - ((realRoughness * roughness) * surfaceReduction));
+    
+    mediump float grazingTerm = xll_saturate_f((oneMinusRoughness + (1.0 - oneMinusReflectivity)));
+    mediump vec3 color = (((((diffColor + (specular * specColor)) * light.color) * nl) + (gi.diffuse * diffColor)) + ((surfaceReduction * gi.specular) * FresnelLerpFast( specColor, vec3( grazingTerm), nv)));
+    
+    return vec4( color, 1.0);
+}
+
+mediump vec3 BRDF_Unity_Indirect( in mediump vec3 baseColor, in mediump vec3 specColor, in mediump float oneMinusReflectivity, in mediump float oneMinusRoughness, in mediump vec3 normal, in mediump vec3 viewDir, in mediump float occlusion, in UnityGI gi ) {
+    mediump vec3 c = vec3( 0.0);
+    
+    return c;
+}
+
+mediump vec3 Emission( in highp vec2 uv ) {
+    return vec3( 0.0);
+}
+
+void ResetUnityLight( out UnityLight outLight ) {
+    
+    outLight.color = vec3( 0.0);
+    outLight.dir = vec3( 0.0);
+    outLight.ndotl = 0.0;
+}
+
+void ResetUnityGI( out UnityGI outGI ) {
+    ResetUnityLight( outGI.light);
+    
+    outGI.indirect.diffuse = vec3( 0.0);
+    outGI.indirect.specular = vec3( 0.0);
+}
+
+mediump vec3 LinearToGammaSpace( in mediump vec3 linRGB ) {
+    linRGB = max( linRGB, vec3( 0.0, 0.0, 0.0));
+    
+    return max( ((1.055 * pow( linRGB, vec3( 0.4166667))) - 0.055), vec3( 0.0));
+}
+
+mediump vec3 SHEvalLinearL0L1( in mediump vec4 normal ) {
+    mediump vec3 x;
+    
+    x.x = dot( unity_SHAr, normal);
+    x.y = dot( unity_SHAg, normal);
+    x.z = dot( unity_SHAb, normal);
+    
+    return x;
+}
+
+mediump vec3 ShadeSHPerPixel( in mediump vec3 normal, in mediump vec3 ambient ) {
+    
+    mediump vec3 ambient_contrib = vec3( 0.0);
+    
+    ambient_contrib = SHEvalLinearL0L1( vec4( normal, 1.0));
+    ambient = max( vec3( 0.0, 0.0, 0.0), (ambient + ambient_contrib));
+    if (IsGammaSpace( )){
+        ambient = LinearToGammaSpace( ambient);
+    }
+    
+    return ambient;
+}
+
+UnityGI UnityGI_Base( in UnityGIInput data, in mediump float occlusion, in mediump vec3 normalWorld ) {
+    UnityGI o_gi;
+    ResetUnityGI( o_gi);
+    
+    o_gi.light = data.light;
+    o_gi.light.color *= data.atten;
+    
+    o_gi.indirect.diffuse = ShadeSHPerPixel( normalWorld, data.ambient);
+    
+    o_gi.indirect.diffuse *= occlusion;
+    return o_gi;
+}
+
+UnityGI UnityGlobalIllumination( in UnityGIInput data, in mediump float occlusion, in mediump vec3 normalWorld ) {
+    return UnityGI_Base( data, occlusion, normalWorld);
+}
+
+mediump vec3 DecodeHDR( in mediump vec4 data, in mediump vec4 decodeInstructions ) {
+    return ((decodeInstructions.x * data.w) * data.xyz);
+}
+
+mediump vec3 DecodeHDR_NoLinearSupportInSM2( in mediump vec4 data, in mediump vec4 decodeInstructions ) {    
+    return DecodeHDR( data, decodeInstructions);
+}
+
+mediump vec3 Unity_GlossyEnvironment( in samplerCube tex, in mediump vec4 hdr, in Unity_GlossyEnvironmentData glossIn ) {
+    
+    mediump float roughness = glossIn.roughness;
+    
+    roughness = (roughness * (1.7 - (0.7 * roughness)));
+    
+    mediump float mip = (roughness * 6.0);
+    mediump vec4 rgbm = xll_texCUBElod( tex, vec4( glossIn.reflUVW, mip));
+    return DecodeHDR_NoLinearSupportInSM2( rgbm, hdr);
+}
+
+mediump vec3 UnityGI_IndirectSpecular( in UnityGIInput data, in mediump float occlusion, in mediump vec3 normalWorld, in Unity_GlossyEnvironmentData glossIn ) {
+    mediump vec3 specular;
+    mediump vec3 env0 = Unity_GlossyEnvironment( unity_SpecCube0, data.probeHDR[0], glossIn);
+    specular = env0;
+    return (specular * occlusion);
+}
+
+UnityGI UnityGlobalIllumination( in UnityGIInput data, in mediump float occlusion, in mediump vec3 normalWorld, in Unity_GlossyEnvironmentData glossIn ) {
+    UnityGI o_gi = UnityGI_Base( data, occlusion, normalWorld);
+    o_gi.indirect.specular = UnityGI_IndirectSpecular( data, occlusion, normalWorld, glossIn);    
+    return o_gi;
+}
+
+UnityGI FragmentGI( in FragmentCommonData s, in mediump float occlusion, in mediump vec4 i_ambientOrLightmapUV, in mediump float atten, in UnityLight light, in bool reflections ) {
+    UnityGIInput d;
+    d.light = light;
+    
+    d.worldPos = s.posWorld;
+    d.worldViewDir = (-s.eyeVec);
+    d.atten = atten;
+    
+    d.ambient = i_ambientOrLightmapUV.xyz;
+    d.lightmapUV = vec4( 0.0);
+    d.boxMax[0] = unity_SpecCube0_BoxMax;
+    
+    d.boxMin[0] = unity_SpecCube0_BoxMin;
+    d.probePosition[0] = unity_SpecCube0_ProbePosition;
+    d.probeHDR[0] = unity_SpecCube0_HDR;
+    
+    d.boxMax[1] = unity_SpecCube1_BoxMax;
+    d.boxMin[1] = unity_SpecCube1_BoxMin;
+    d.probePosition[1] = unity_SpecCube1_ProbePosition;
+    d.probeHDR[1] = unity_SpecCube1_HDR;
+    
+    if (reflections){
+        Unity_GlossyEnvironmentData g;
+        g.roughness = (1.0 - s.oneMinusRoughness);
+        
+        g.reflUVW = reflect( s.eyeVec, s.normalWorld);
+        return UnityGlobalIllumination( d, occlusion, s.normalWorld, g);
+    }
+    else{
+        return UnityGlobalIllumination( d, occlusion, s.normalWorld);
+    }
+}
+
+UnityGI FragmentGI( in highp vec3 posWorld, in mediump float occlusion, in mediump vec4 i_ambientOrLightmapUV, in mediump float atten, in mediump float oneMinusRoughness, in mediump vec3 normalWorld, in mediump vec3 eyeVec, in UnityLight light, in bool reflections ) {
+    FragmentCommonData s = FragmentCommonData(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0, 0.0, vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0);
+    s.oneMinusRoughness = oneMinusRoughness;
+    s.normalWorld = normalWorld;
+    s.eyeVec = eyeVec;
+    s.posWorld = posWorld;
+    return FragmentGI( s, occlusion, i_ambientOrLightmapUV, atten, light, reflections);
+}
+
+UnityGI FragmentGI( in highp vec3 posWorld, in mediump float occlusion, in mediump vec4 i_ambientOrLightmapUV, in mediump float atten, in mediump float oneMinusRoughness, in mediump vec3 normalWorld, in mediump vec3 eyeVec, in UnityLight light ) {
+    return FragmentGI( posWorld, occlusion, i_ambientOrLightmapUV, atten, oneMinusRoughness, normalWorld, eyeVec, light, true);
+}
+
+mediump float Alpha( in highp vec2 uv ) {
+    return (texture( _MainTex, uv).w * _Color.w);
+}
+
+mediump vec3 Albedo( in highp vec4 texcoords ) {
+    mediump vec3 albedo = (_Color.xyz * texture( _MainTex, texcoords.xy).xyz);
+    return albedo;
+}
+
+mediump float OneMinusReflectivityFromMetallic( in mediump float metallic ) {
+    mediump float oneMinusDielectricSpec = unity_ColorSpaceDielectricSpec.w;
+    return (oneMinusDielectricSpec - (metallic * oneMinusDielectricSpec));
+}
+
+mediump vec3 DiffuseAndSpecularFromMetallic( in mediump vec3 albedo, in mediump float metallic, out mediump vec3 specColor, out mediump float oneMinusReflectivity ) {
+    specColor = mix( unity_ColorSpaceDielectricSpec.xyz, albedo, vec3( metallic));
+    oneMinusReflectivity = OneMinusReflectivityFromMetallic( metallic);
+    
+    return (albedo * oneMinusReflectivity);
+}
+
+mediump vec2 MetallicGloss( in highp vec2 uv ) {
+    mediump vec2 mg;
+    
+    mg = vec2( _Metallic, _Glossiness);
+    return mg;
+}
+
+FragmentCommonData MetallicSetup( in highp vec4 i_tex ) {
+    mediump vec2 metallicGloss = MetallicGloss( i_tex.xy);
+    mediump float metallic = metallicGloss.x;
+    
+    mediump float oneMinusRoughness = metallicGloss.y;
+    mediump float oneMinusReflectivity;
+    mediump vec3 specColor;
+    
+    mediump vec3 diffColor = DiffuseAndSpecularFromMetallic( Albedo( i_tex), metallic, specColor, oneMinusReflectivity);
+    FragmentCommonData o = FragmentCommonData(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0, 0.0, vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0);
+    o.diffColor = diffColor;
+    
+    o.specColor = specColor;
+    o.oneMinusReflectivity = oneMinusReflectivity;
+    o.oneMinusRoughness = oneMinusRoughness;
+    return o;
+}
+
+mediump vec3 NormalizePerPixelNormal( in mediump vec3 n ) {
+    return normalize(n);
+}
+
+highp vec4 Parallax( in highp vec4 texcoords, in mediump vec3 viewDir ) {
+    return texcoords;
+}
+
+mediump vec3 PerPixelWorldNormal( in highp vec4 i_tex, in mediump vec4 tangentToWorld[3] ) {
+    mediump vec3 normalWorld = normalize(tangentToWorld[2].xyz);
+    return normalWorld;
+}
+
+mediump vec3 PreMultiplyAlpha( in mediump vec3 diffColor, in mediump float alpha, in mediump float oneMinusReflectivity, out mediump float outModifiedAlpha ) {
+    outModifiedAlpha = alpha;
+    return diffColor;
+}
+
+FragmentCommonData FragmentSetup( in highp vec4 i_tex, in mediump vec3 i_eyeVec, in mediump vec3 i_viewDirForParallax, in mediump vec4 tangentToWorld[3], in mediump vec3 i_posWorld ) {
+    i_tex = Parallax( i_tex, i_viewDirForParallax);
+    
+    mediump float alpha = Alpha( i_tex.xy);
+    
+    FragmentCommonData o = MetallicSetup( i_tex);
+    o.normalWorld = PerPixelWorldNormal( i_tex, tangentToWorld);
+    o.eyeVec = NormalizePerPixelNormal( i_eyeVec);
+    o.posWorld = i_posWorld;
+    
+    o.diffColor = PreMultiplyAlpha( o.diffColor, alpha, o.oneMinusReflectivity, o.alpha);
+    return o;
+}
+
+mediump float LambertTerm( in mediump vec3 normal, in mediump vec3 lightDir ) {
+    return DotClamped( normal, lightDir);
+}
+
+UnityLight MainLight( in mediump vec3 normalWorld ) {
+    UnityLight l;
+    
+    l.color = _LightColor0.xyz;
+    l.dir = _WorldSpaceLightPos0.xyz;
+    l.ndotl = LambertTerm( normalWorld, l.dir);
+    
+    return l;
+}
+
+mediump float LerpOneTo( in mediump float b, in mediump float t ) {
+    mediump float oneMinusT = (1.0 - t);
+    return (oneMinusT + (b * t));
+}
+
+mediump float Occlusion( in highp vec2 uv ) {    
+    mediump float occ = texture( _OcclusionMap, uv).y;
+    return LerpOneTo( occ, _OcclusionStrength);
+}
+
+mediump vec4 OutputForward( in mediump vec4 xlat_varoutput, in mediump float alphaFromSurface ) {
+    xlat_varoutput.w = 1.0;
+    return xlat_varoutput;
+}
+
+lowp float unitySampleShadow( in mediump vec4 shadowCoord ) {    
+    lowp float shadow = xll_shadow2D( _ShadowMapTexture, shadowCoord.xyz.xyz);
+    shadow = (_LightShadowData.x + (shadow * (1.0 - _LightShadowData.x)));
+    return shadow;
+}
+
+mediump vec4 fragForwardBase_VC( in VertexOutputForwardBase_VC i ) {
+    FragmentCommonData s = FragmentSetup( i.tex, i.eyeVec, vec3( 0.0, 0.0, 0.0), i.tangentToWorldAndParallax, vec3( 0.0, 0.0, 0.0));
+    UnityLight mainLight = MainLight( s.normalWorld);
+    
+    mediump float atten = unitySampleShadow( i._ShadowCoord);
+    mediump float occlusion = Occlusion( i.tex.xy);
+    UnityGI gi = FragmentGI( s.posWorld, occlusion, i.ambientOrLightmapUV, atten, s.oneMinusRoughness, s.normalWorld, s.eyeVec, mainLight);
+    
+    mediump vec4 c = BRDF2_Unity_PBS( s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, (-s.eyeVec), gi.light, gi.indirect);
+    c *= i.color;
+    
+    c.xyz += BRDF_Unity_Indirect( s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, (-s.eyeVec), occlusion, gi);
+    c.xyz += Emission( i.tex.xy);
+    
+    return OutputForward( c, (s.alpha * i.color.w));
+}
+in highp vec4 xlv_TEXCOORD0;
+in mediump vec3 xlv_TEXCOORD1;
+in mediump vec4 xlv_TEXCOORD2;
+in mediump vec4 xlv_TEXCOORD2_1;
+in mediump vec4 xlv_TEXCOORD2_2;
+in mediump vec4 xlv_TEXCOORD5;
+in mediump vec4 xlv_TEXCOORD6;
+in lowp vec4 xlv_COLOR;
+out lowp vec4 FragData [1];
+void main() {
+    mediump vec4 xl_retval;
+    VertexOutputForwardBase_VC xlt_i;
+    xlt_i.pos = vec4(0.0);
+    xlt_i.tex = vec4(xlv_TEXCOORD0);
+    xlt_i.eyeVec = vec3(xlv_TEXCOORD1);
+    xlt_i.tangentToWorldAndParallax[0] = vec4(xlv_TEXCOORD2);
+    xlt_i.tangentToWorldAndParallax[1] = vec4(xlv_TEXCOORD2_1);
+    xlt_i.tangentToWorldAndParallax[2] = vec4(xlv_TEXCOORD2_2);
+    xlt_i.ambientOrLightmapUV = vec4(xlv_TEXCOORD5);
+    xlt_i._ShadowCoord = vec4(xlv_TEXCOORD6);
+    xlt_i.color = vec4(xlv_COLOR);
+    xl_retval = fragForwardBase_VC( xlt_i);
+    FragData[0] = vec4(xl_retval);
+}

+ 187 - 0
3rdparty/glsl-optimizer/tests/fragment/global-struct-constant-init-metal-outES3.txt

@@ -0,0 +1,187 @@
+#version 300 es
+struct FragmentCommonData {
+  mediump vec3 diffColor;
+  mediump vec3 specColor;
+  mediump float oneMinusReflectivity;
+  mediump float oneMinusRoughness;
+  mediump vec3 normalWorld;
+  mediump vec3 eyeVec;
+  mediump vec3 posWorld;
+  mediump float alpha;
+};
+uniform mediump vec4 _WorldSpaceLightPos0;
+uniform mediump vec4 unity_SHAr;
+uniform mediump vec4 unity_SHAg;
+uniform mediump vec4 unity_SHAb;
+uniform mediump vec4 _LightShadowData;
+uniform lowp samplerCube unity_SpecCube0;
+uniform mediump vec4 unity_SpecCube0_HDR;
+uniform mediump vec4 unity_ColorSpaceDielectricSpec;
+uniform lowp vec4 _LightColor0;
+uniform mediump vec4 _Color;
+uniform sampler2D _MainTex;
+uniform mediump float _Metallic;
+uniform mediump float _Glossiness;
+uniform sampler2D _OcclusionMap;
+uniform mediump float _OcclusionStrength;
+uniform lowp sampler2DShadow _ShadowMapTexture;
+in highp vec4 xlv_TEXCOORD0;
+in mediump vec3 xlv_TEXCOORD1;
+in mediump vec4 xlv_TEXCOORD2_2;
+in mediump vec4 xlv_TEXCOORD5;
+in mediump vec4 xlv_TEXCOORD6;
+in lowp vec4 xlv_COLOR;
+out lowp vec4 FragData[1];
+void main ()
+{
+  mediump vec4 c_1;
+  mediump float atten_2;
+  lowp vec4 tmpvar_3;
+  tmpvar_3 = texture (_MainTex, xlv_TEXCOORD0.xy);
+  mediump vec2 tmpvar_4;
+  tmpvar_4.x = _Metallic;
+  tmpvar_4.y = _Glossiness;
+  mediump vec3 tmpvar_5;
+  tmpvar_5 = (_Color.xyz * tmpvar_3.xyz);
+  mediump vec3 tmpvar_6;
+  mediump vec3 tmpvar_7;
+  tmpvar_7 = mix (unity_ColorSpaceDielectricSpec.xyz, tmpvar_5, vec3(_Metallic));
+  mediump float tmpvar_8;
+  tmpvar_8 = (unity_ColorSpaceDielectricSpec.w - (_Metallic * unity_ColorSpaceDielectricSpec.w));
+  tmpvar_6 = (tmpvar_5 * tmpvar_8);
+  mediump vec3 tmpvar_9;
+  tmpvar_9 = normalize(xlv_TEXCOORD2_2.xyz);
+  mediump vec3 tmpvar_10;
+  tmpvar_10 = normalize(xlv_TEXCOORD1);
+  mediump vec3 tmpvar_11;
+  tmpvar_11 = _LightColor0.xyz;
+  lowp float shadow_12;
+  mediump float tmpvar_13;
+  tmpvar_13 = texture (_ShadowMapTexture, xlv_TEXCOORD6.xyz);
+  lowp float tmpvar_14;
+  tmpvar_14 = tmpvar_13;
+  shadow_12 = (_LightShadowData.x + (tmpvar_14 * (1.0 - _LightShadowData.x)));
+  atten_2 = shadow_12;
+  mediump float occ_15;
+  lowp float tmpvar_16;
+  tmpvar_16 = texture (_OcclusionMap, xlv_TEXCOORD0.xy).y;
+  occ_15 = tmpvar_16;
+  mediump float tmpvar_17;
+  tmpvar_17 = ((1.0 - _OcclusionStrength) + (occ_15 * _OcclusionStrength));
+  FragmentCommonData s_18;
+  s_18 = FragmentCommonData(vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0, 0.0, vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), 0.0);
+  s_18.oneMinusRoughness = tmpvar_4.y;
+  s_18.normalWorld = tmpvar_9;
+  s_18.eyeVec = tmpvar_10;
+  s_18.posWorld = vec3(0.0, 0.0, 0.0);
+  mediump vec3 tmpvar_19;
+  mediump vec3 tmpvar_20;
+  tmpvar_19 = s_18.normalWorld;
+  tmpvar_20 = s_18.eyeVec;
+  highp vec4 tmpvar_21;
+  tmpvar_21 = unity_SpecCube0_HDR;
+  mediump float tmpvar_22;
+  tmpvar_22 = (1.0 - s_18.oneMinusRoughness);
+  mediump vec3 tmpvar_23;
+  tmpvar_23 = (tmpvar_20 - (2.0 * (
+    dot (tmpvar_19, tmpvar_20)
+   * tmpvar_19)));
+  mediump vec4 tmpvar_24;
+  tmpvar_24.w = 1.0;
+  tmpvar_24.xyz = tmpvar_19;
+  mediump vec3 x_25;
+  x_25.x = dot (unity_SHAr, tmpvar_24);
+  x_25.y = dot (unity_SHAg, tmpvar_24);
+  x_25.z = dot (unity_SHAb, tmpvar_24);
+  mediump vec4 hdr_26;
+  hdr_26 = tmpvar_21;
+  mediump vec4 tmpvar_27;
+  tmpvar_27.xyz = tmpvar_23;
+  tmpvar_27.w = ((tmpvar_22 * (1.7 - 
+    (0.7 * tmpvar_22)
+  )) * 6.0);
+  lowp vec4 tmpvar_28;
+  tmpvar_28 = textureLod (unity_SpecCube0, tmpvar_23, tmpvar_27.w);
+  mediump vec4 tmpvar_29;
+  tmpvar_29 = tmpvar_28;
+  mediump vec3 viewDir_30;
+  viewDir_30 = -(tmpvar_10);
+  mediump vec3 tmpvar_31;
+  mediump vec3 inVec_32;
+  inVec_32 = (_WorldSpaceLightPos0.xyz + viewDir_30);
+  tmpvar_31 = (inVec_32 * inversesqrt(max (0.001, 
+    dot (inVec_32, inVec_32)
+  )));
+  mediump float tmpvar_33;
+  tmpvar_33 = max (0.0, dot (_WorldSpaceLightPos0.xyz, tmpvar_31));
+  mediump float tmpvar_34;
+  tmpvar_34 = (1.0 - _Glossiness);
+  mediump float tmpvar_35;
+  tmpvar_35 = max (0.0001, (tmpvar_34 * tmpvar_34));
+  mediump float tmpvar_36;
+  tmpvar_36 = max (((2.0 / 
+    (tmpvar_35 * tmpvar_35)
+  ) - 2.0), 0.0001);
+  mediump float x_37;
+  x_37 = (1.0 - max (0.0, dot (tmpvar_9, viewDir_30)));
+  mediump vec4 tmpvar_38;
+  tmpvar_38.w = 1.0;
+  tmpvar_38.xyz = (((
+    ((tmpvar_6 + (sqrt(
+      max (0.0001, (((tmpvar_36 + 1.0) * pow (
+        max (0.0, dot (tmpvar_9, tmpvar_31))
+      , tmpvar_36)) / ((
+        (8.0 * (((tmpvar_33 * tmpvar_33) * _Glossiness) + (tmpvar_34 * tmpvar_34)))
+       * tmpvar_33) + 0.0001)))
+    ) * tmpvar_7)) * (tmpvar_11 * atten_2))
+   * 
+    max (0.0, dot (tmpvar_9, _WorldSpaceLightPos0.xyz))
+  ) + (
+    (max (((1.055 * 
+      pow (max (vec3(0.0, 0.0, 0.0), (xlv_TEXCOORD5.xyz + x_25)), vec3(0.4166667, 0.4166667, 0.4166667))
+    ) - 0.055), vec3(0.0, 0.0, 0.0)) * tmpvar_17)
+   * tmpvar_6)) + ((
+    (1.0 - ((tmpvar_34 * tmpvar_34) * (tmpvar_34 * 0.28)))
+   * 
+    (((hdr_26.x * tmpvar_29.w) * tmpvar_29.xyz) * tmpvar_17)
+  ) * mix (tmpvar_7, vec3(
+    clamp ((_Glossiness + (1.0 - tmpvar_8)), 0.0, 1.0)
+  ), vec3(
+    ((x_37 * x_37) * (x_37 * x_37))
+  ))));
+  c_1 = (tmpvar_38 * xlv_COLOR);
+  c_1.xyz = c_1.xyz;
+  c_1.xyz = c_1.xyz;
+  mediump vec4 xlat_varoutput_39;
+  xlat_varoutput_39.xyz = c_1.xyz;
+  xlat_varoutput_39.w = 1.0;
+  FragData[0] = xlat_varoutput_39;
+}
+
+
+// stats: 97 alu 4 tex 0 flow
+// inputs: 6
+//  #0: xlv_TEXCOORD0 (high float) 4x1 [-1]
+//  #1: xlv_TEXCOORD1 (medium float) 3x1 [-1]
+//  #2: xlv_TEXCOORD2_2 (medium float) 4x1 [-1]
+//  #3: xlv_TEXCOORD5 (medium float) 4x1 [-1]
+//  #4: xlv_TEXCOORD6 (medium float) 4x1 [-1]
+//  #5: xlv_COLOR (low float) 4x1 [-1]
+// uniforms: 12 (total size: 0)
+//  #0: _WorldSpaceLightPos0 (medium float) 4x1 [-1]
+//  #1: unity_SHAr (medium float) 4x1 [-1]
+//  #2: unity_SHAg (medium float) 4x1 [-1]
+//  #3: unity_SHAb (medium float) 4x1 [-1]
+//  #4: _LightShadowData (medium float) 4x1 [-1]
+//  #5: unity_SpecCube0_HDR (medium float) 4x1 [-1]
+//  #6: unity_ColorSpaceDielectricSpec (medium float) 4x1 [-1]
+//  #7: _LightColor0 (low float) 4x1 [-1]
+//  #8: _Color (medium float) 4x1 [-1]
+//  #9: _Metallic (medium float) 1x1 [-1]
+//  #10: _Glossiness (medium float) 1x1 [-1]
+//  #11: _OcclusionStrength (medium float) 1x1 [-1]
+// textures: 4
+//  #0: unity_SpecCube0 (low cube) 0x0 [-1]
+//  #1: _MainTex (low 2d) 0x0 [-1]
+//  #2: _OcclusionMap (low 2d) 0x0 [-1]
+//  #3: _ShadowMapTexture (low 2dshadow) 0x0 [-1]

+ 199 - 0
3rdparty/glsl-optimizer/tests/fragment/global-struct-constant-init-metal-outES3Metal.txt

@@ -0,0 +1,199 @@
+#include <metal_stdlib>
+#pragma clang diagnostic ignored "-Wparentheses-equality"
+using namespace metal;
+constexpr sampler _mtl_xl_shadow_sampler(address::clamp_to_edge, filter::linear, compare_func::less);
+struct FragmentCommonData {
+  half3 diffColor;
+  half3 specColor;
+  half oneMinusReflectivity;
+  half oneMinusRoughness;
+  half3 normalWorld;
+  half3 eyeVec;
+  half3 posWorld;
+  half alpha;
+};
+constant FragmentCommonData _xlat_mtl_const1 = {float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), 0.0, 0.0, float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), 0.0};
+struct xlatMtlShaderInput {
+  float4 xlv_TEXCOORD0;
+  half3 xlv_TEXCOORD1;
+  half4 xlv_TEXCOORD2_2;
+  half4 xlv_TEXCOORD5;
+  half4 xlv_TEXCOORD6;
+  half4 xlv_COLOR;
+};
+struct xlatMtlShaderOutput {
+  half4 FragData_0 [[color(0)]];
+};
+struct xlatMtlShaderUniform {
+  half4 _WorldSpaceLightPos0;
+  half4 unity_SHAr;
+  half4 unity_SHAg;
+  half4 unity_SHAb;
+  half4 _LightShadowData;
+  half4 unity_SpecCube0_HDR;
+  half4 unity_ColorSpaceDielectricSpec;
+  half4 _LightColor0;
+  half4 _Color;
+  half _Metallic;
+  half _Glossiness;
+  half _OcclusionStrength;
+};
+fragment xlatMtlShaderOutput xlatMtlMain (xlatMtlShaderInput _mtl_i [[stage_in]], constant xlatMtlShaderUniform& _mtl_u [[buffer(0)]]
+  ,   texturecube<half> unity_SpecCube0 [[texture(0)]], sampler _mtlsmp_unity_SpecCube0 [[sampler(0)]]
+  ,   texture2d<half> _MainTex [[texture(1)]], sampler _mtlsmp__MainTex [[sampler(1)]]
+  ,   texture2d<half> _OcclusionMap [[texture(2)]], sampler _mtlsmp__OcclusionMap [[sampler(2)]]
+  ,   depth2d<float> _ShadowMapTexture [[texture(3)]], sampler _mtlsmp__ShadowMapTexture [[sampler(3)]])
+{
+  xlatMtlShaderOutput _mtl_o;
+  half4 c_1;
+  half atten_2;
+  half4 tmpvar_3;
+  tmpvar_3 = _MainTex.sample(_mtlsmp__MainTex, (float2)(_mtl_i.xlv_TEXCOORD0.xy));
+  half2 tmpvar_4;
+  tmpvar_4.x = _mtl_u._Metallic;
+  tmpvar_4.y = _mtl_u._Glossiness;
+  half3 tmpvar_5;
+  tmpvar_5 = (_mtl_u._Color.xyz * tmpvar_3.xyz);
+  half3 tmpvar_6;
+  half3 tmpvar_7;
+  tmpvar_7 = mix (_mtl_u.unity_ColorSpaceDielectricSpec.xyz, tmpvar_5, half3(_mtl_u._Metallic));
+  half tmpvar_8;
+  tmpvar_8 = (_mtl_u.unity_ColorSpaceDielectricSpec.w - (_mtl_u._Metallic * _mtl_u.unity_ColorSpaceDielectricSpec.w));
+  tmpvar_6 = (tmpvar_5 * tmpvar_8);
+  half3 tmpvar_9;
+  tmpvar_9 = normalize(_mtl_i.xlv_TEXCOORD2_2.xyz);
+  half3 tmpvar_10;
+  tmpvar_10 = normalize(_mtl_i.xlv_TEXCOORD1);
+  half3 tmpvar_11;
+  tmpvar_11 = _mtl_u._LightColor0.xyz;
+  half shadow_12;
+  half tmpvar_13;
+  tmpvar_13 = _ShadowMapTexture.sample_compare(_mtl_xl_shadow_sampler, (float2)(_mtl_i.xlv_TEXCOORD6.xyz).xy, (float)(_mtl_i.xlv_TEXCOORD6.xyz).z);
+  half tmpvar_14;
+  tmpvar_14 = tmpvar_13;
+  shadow_12 = (_mtl_u._LightShadowData.x + (tmpvar_14 * ((half)1.0 - _mtl_u._LightShadowData.x)));
+  atten_2 = shadow_12;
+  half occ_15;
+  half tmpvar_16;
+  tmpvar_16 = _OcclusionMap.sample(_mtlsmp__OcclusionMap, (float2)(_mtl_i.xlv_TEXCOORD0.xy)).y;
+  occ_15 = tmpvar_16;
+  half tmpvar_17;
+  tmpvar_17 = (((half)1.0 - _mtl_u._OcclusionStrength) + (occ_15 * _mtl_u._OcclusionStrength));
+  FragmentCommonData s_18;
+  s_18 = _xlat_mtl_const1;
+  s_18.oneMinusRoughness = tmpvar_4.y;
+  s_18.normalWorld = tmpvar_9;
+  s_18.eyeVec = tmpvar_10;
+  s_18.posWorld = half3(float3(0.0, 0.0, 0.0));
+  half3 tmpvar_19;
+  half3 tmpvar_20;
+  tmpvar_19 = s_18.normalWorld;
+  tmpvar_20 = s_18.eyeVec;
+  float4 tmpvar_21;
+  tmpvar_21 = float4(_mtl_u.unity_SpecCube0_HDR);
+  half tmpvar_22;
+  tmpvar_22 = ((half)1.0 - s_18.oneMinusRoughness);
+  half3 tmpvar_23;
+  tmpvar_23 = (tmpvar_20 - ((half)2.0 * (
+    dot (tmpvar_19, tmpvar_20)
+   * tmpvar_19)));
+  half4 tmpvar_24;
+  tmpvar_24.w = half(1.0);
+  tmpvar_24.xyz = tmpvar_19;
+  half3 x_25;
+  x_25.x = dot (_mtl_u.unity_SHAr, tmpvar_24);
+  x_25.y = dot (_mtl_u.unity_SHAg, tmpvar_24);
+  x_25.z = dot (_mtl_u.unity_SHAb, tmpvar_24);
+  half4 hdr_26;
+  hdr_26 = half4(tmpvar_21);
+  half4 tmpvar_27;
+  tmpvar_27.xyz = tmpvar_23;
+  tmpvar_27.w = ((tmpvar_22 * ((half)1.7 - 
+    ((half)0.7 * tmpvar_22)
+  )) * (half)6.0);
+  half4 tmpvar_28;
+  tmpvar_28 = unity_SpecCube0.sample(_mtlsmp_unity_SpecCube0, (float3)(tmpvar_23), level(tmpvar_27.w));
+  half4 tmpvar_29;
+  tmpvar_29 = tmpvar_28;
+  half3 viewDir_30;
+  viewDir_30 = -(tmpvar_10);
+  half3 tmpvar_31;
+  half3 inVec_32;
+  inVec_32 = (_mtl_u._WorldSpaceLightPos0.xyz + viewDir_30);
+  tmpvar_31 = (inVec_32 * rsqrt(max ((half)0.001, 
+    dot (inVec_32, inVec_32)
+  )));
+  half tmpvar_33;
+  tmpvar_33 = max ((half)0.0, dot (_mtl_u._WorldSpaceLightPos0.xyz, tmpvar_31));
+  half tmpvar_34;
+  tmpvar_34 = ((half)1.0 - _mtl_u._Glossiness);
+  half tmpvar_35;
+  tmpvar_35 = max ((half)0.0001, (tmpvar_34 * tmpvar_34));
+  half tmpvar_36;
+  tmpvar_36 = max ((((half)2.0 / 
+    (tmpvar_35 * tmpvar_35)
+  ) - (half)2.0), (half)0.0001);
+  half x_37;
+  x_37 = ((half)1.0 - max ((half)0.0, dot (tmpvar_9, viewDir_30)));
+  half4 tmpvar_38;
+  tmpvar_38.w = half(1.0);
+  tmpvar_38.xyz = (((
+    ((tmpvar_6 + (sqrt(
+      max ((half)0.0001, (((tmpvar_36 + (half)1.0) * pow (
+        max ((half)0.0, dot (tmpvar_9, tmpvar_31))
+      , tmpvar_36)) / ((
+        ((half)8.0 * (((tmpvar_33 * tmpvar_33) * _mtl_u._Glossiness) + (tmpvar_34 * tmpvar_34)))
+       * tmpvar_33) + (half)0.0001)))
+    ) * tmpvar_7)) * (tmpvar_11 * atten_2))
+   * 
+    max ((half)0.0, dot (tmpvar_9, _mtl_u._WorldSpaceLightPos0.xyz))
+  ) + (
+    (max ((((half)1.055 * 
+      pow (max ((half3)float3(0.0, 0.0, 0.0), (_mtl_i.xlv_TEXCOORD5.xyz + x_25)), (half3)float3(0.4166667, 0.4166667, 0.4166667))
+    ) - (half)0.055), (half3)float3(0.0, 0.0, 0.0)) * tmpvar_17)
+   * tmpvar_6)) + ((
+    ((half)1.0 - ((tmpvar_34 * tmpvar_34) * (tmpvar_34 * (half)0.28)))
+   * 
+    (((hdr_26.x * tmpvar_29.w) * tmpvar_29.xyz) * tmpvar_17)
+  ) * mix (tmpvar_7, half3(
+    clamp ((_mtl_u._Glossiness + ((half)1.0 - tmpvar_8)), (half)0.0, (half)1.0)
+  ), half3(
+    ((x_37 * x_37) * (x_37 * x_37))
+  ))));
+  c_1 = (tmpvar_38 * _mtl_i.xlv_COLOR);
+  c_1.xyz = c_1.xyz;
+  c_1.xyz = c_1.xyz;
+  half4 xlat_varoutput_39;
+  xlat_varoutput_39.xyz = c_1.xyz;
+  xlat_varoutput_39.w = half(1.0);
+  _mtl_o.FragData_0 = xlat_varoutput_39;
+  return _mtl_o;
+}
+
+
+// stats: 97 alu 4 tex 0 flow
+// inputs: 6
+//  #0: xlv_TEXCOORD0 (high float) 4x1 [-1]
+//  #1: xlv_TEXCOORD1 (medium float) 3x1 [-1]
+//  #2: xlv_TEXCOORD2_2 (medium float) 4x1 [-1]
+//  #3: xlv_TEXCOORD5 (medium float) 4x1 [-1]
+//  #4: xlv_TEXCOORD6 (medium float) 4x1 [-1]
+//  #5: xlv_COLOR (low float) 4x1 [-1]
+// uniforms: 12 (total size: 78)
+//  #0: _WorldSpaceLightPos0 (medium float) 4x1 [-1] loc 0
+//  #1: unity_SHAr (medium float) 4x1 [-1] loc 8
+//  #2: unity_SHAg (medium float) 4x1 [-1] loc 16
+//  #3: unity_SHAb (medium float) 4x1 [-1] loc 24
+//  #4: _LightShadowData (medium float) 4x1 [-1] loc 32
+//  #5: unity_SpecCube0_HDR (medium float) 4x1 [-1] loc 40
+//  #6: unity_ColorSpaceDielectricSpec (medium float) 4x1 [-1] loc 48
+//  #7: _LightColor0 (low float) 4x1 [-1] loc 56
+//  #8: _Color (medium float) 4x1 [-1] loc 64
+//  #9: _Metallic (medium float) 1x1 [-1] loc 72
+//  #10: _Glossiness (medium float) 1x1 [-1] loc 74
+//  #11: _OcclusionStrength (medium float) 1x1 [-1] loc 76
+// textures: 4
+//  #0: unity_SpecCube0 (low cube) 0x0 [-1] loc 0
+//  #1: _MainTex (low 2d) 0x0 [-1] loc 1
+//  #2: _OcclusionMap (low 2d) 0x0 [-1] loc 2
+//  #3: _ShadowMapTexture (low 2dshadow) 0x0 [-1] loc 3