I wanted to use this as a toon shader base for my game, but when I build for iOS the materials with the shader appear in magenta color when game is run on the device. This is the log for the compilation error:
Compilation failed:
program_source:104:29: error: as_type cast from 'half' to 'int' is not allowed
u_xlati18 = u_xlatb18 ? as_type<int>(u_xlat16_1.x) : int(0);
^~~~~~~~~~~~~~~~~~~~~~~~~~
I'm total noob when it comes to shaders, and I couldn't figure out what part of code exactly compiles to the one above. Can you please help me with it?
#include <metal_stdlib>
#include <metal_texture>
using namespace metal;
#if !(__HAVE_FMA__)
#define fma(a,b,c) ((a) * (b) + (c))
#endif
#ifndef XLT_REMAP_O
#define XLT_REMAP_O {0, 1, 2, 3, 4, 5, 6, 7}
#endif
constexpr constant uint xlt_remap_o[] = XLT_REMAP_O;
struct FGlobals_Type
{
float4 _MainLightPosition;
half4 _MainLightColor;
half4 _MainLightShadowParams;
};
struct UnityPerDraw_Type
{
float4 hlslcc_mtx4x4unity_ObjectToWorld[4];
float4 hlslcc_mtx4x4unity_WorldToObject[4];
float4 unity_LODFade;
half4 unity_WorldTransformParams;
half4 unity_LightData;
half4 unity_LightIndices[2];
float4 unity_ProbesOcclusion;
half4 unity_SpecCube0_HDR;
float4 unity_LightmapST;
float4 unity_DynamicLightmapST;
half4 unity_SHAr;
half4 unity_SHAg;
half4 unity_SHAb;
half4 unity_SHBr;
half4 unity_SHBg;
half4 unity_SHBb;
half4 unity_SHC;
};
struct UnityPerMaterial_Type
{
float4 _BaseMap_ST;
half4 _BaseColor;
float _UseAlphaClipping;
half _Cutoff;
half3 _IndirectLightConstColor;
half _IndirectLightMultiplier;
half _DirectLightMultiplier;
half _CelShadeMidPoint;
half _CelShadeSoftness;
half _ReceiveShadowMappingAmount;
float _UseEmission;
half3 _EmissionColor;
half3 _EmissionMapChannelMask;
float _OutlineWidth;
half3 _OutlineColor;
};
struct Mtl_FragmentIn
{
float2 TEXCOORD0 [[ user(TEXCOORD0) ]] ;
half3 TEXCOORD3 [[ user(TEXCOORD3) ]] ;
float4 TEXCOORD6 [[ user(TEXCOORD6) ]] ;
};
struct Mtl_FragmentOut
{
half4 SV_TARGET0 [[ color(xlt_remap_o[0]) ]];
};
constexpr sampler _mtl_xl_shadow_sampler(address::clamp_to_edge, filter::linear, compare_func::greater_equal);
fragment Mtl_FragmentOut xlatMtlMain(
constant FGlobals_Type& FGlobals [[ buffer(0) ]],
constant UnityPerDraw_Type& UnityPerDraw [[ buffer(1) ]],
constant UnityPerMaterial_Type& UnityPerMaterial [[ buffer(2) ]],
sampler sampler_MainLightShadowmapTexture [[ sampler (0) ]],
sampler sampler_BaseMap [[ sampler (1) ]],
sampler sampler_EmissionMap [[ sampler (2) ]],
texture2d<half, access::sample > _BaseMap [[ texture(0) ]] ,
texture2d<half, access::sample > _EmissionMap [[ texture(1) ]] ,
depth2d<float, access::sample > _MainLightShadowmapTexture [[ texture(2) ]] ,
Mtl_FragmentIn input [[ stage_in ]])
{
Mtl_FragmentOut output;
half4 u_xlat16_0;
half3 u_xlat16_1;
half3 u_xlat16_2;
half3 u_xlat16_3;
half3 u_xlat16_4;
float u_xlat5;
half u_xlat16_5;
bool u_xlatb5;
half u_xlat16_9;
half u_xlat16_18;
int u_xlati18;
bool u_xlatb18;
half u_xlat16_20;
u_xlat16_0 = _BaseMap.sample(sampler_BaseMap, input.TEXCOORD0.xy);
u_xlat16_0.xyz = u_xlat16_0.xyz * UnityPerMaterial._BaseColor.xyz;
u_xlat16_1.x = half((float(0.0)!=UnityPerMaterial._UseAlphaClipping) ? 0xFFFFFFFFu : uint(0));
u_xlat16_2.x = fma(u_xlat16_0.w, UnityPerMaterial._BaseColor.w, (-UnityPerMaterial._Cutoff));
u_xlatb18 = u_xlat16_2.x<half(0.0);
u_xlati18 = u_xlatb18 ? as_type<int>(u_xlat16_1.x) : int(0);
if((u_xlati18)!=0){discard_fragment();}
u_xlatb18 = float(0.0)!=UnityPerMaterial._UseEmission;
if(u_xlatb18){
u_xlat16_1.xyz = _EmissionMap.sample(sampler_EmissionMap, input.TEXCOORD0.xy).xyz;
u_xlat16_1.xyz = u_xlat16_1.xyz * UnityPerMaterial._EmissionColor.xyzx.xyz;
u_xlat16_1.xyz = u_xlat16_1.xyz * UnityPerMaterial._EmissionMapChannelMask.xyzx.xyz;
u_xlat16_1.xyz = u_xlat16_1.xyz;
}
u_xlat16_2.xyz = (bool(u_xlatb18)) ? u_xlat16_1.xyz : half3(0.0, 0.0, 0.0);
u_xlat16_20 = dot(input.TEXCOORD3.xyz, input.TEXCOORD3.xyz);
u_xlat16_20 = rsqrt(u_xlat16_20);
u_xlat16_3.xyz = half3(u_xlat16_20) * input.TEXCOORD3.xyz;
u_xlat16_4.x = max(UnityPerDraw.unity_SHAr.w, half(0.0));
u_xlat16_4.y = max(UnityPerDraw.unity_SHAg.w, half(0.0));
u_xlat16_4.z = max(UnityPerDraw.unity_SHAb.w, half(0.0));
u_xlat16_4.xyz = fma(u_xlat16_4.xyz, half3(UnityPerMaterial._IndirectLightMultiplier), UnityPerMaterial._IndirectLightConstColor.xxyz.yzw);
u_xlat16_18 = _MainLightShadowmapTexture.sample_compare(_mtl_xl_shadow_sampler, input.TEXCOORD6.xy, saturate(input.TEXCOORD6.z), level(0.0));
u_xlat16_20 = (-FGlobals._MainLightShadowParams.x) + half(1.0);
u_xlat16_20 = fma(u_xlat16_18, FGlobals._MainLightShadowParams.x, u_xlat16_20);
u_xlatb18 = 0.0>=input.TEXCOORD6.z;
u_xlatb5 = input.TEXCOORD6.z>=1.0;
u_xlatb18 = u_xlatb18 || u_xlatb5;
u_xlat16_3.x = dot(float3(u_xlat16_3.xyz), FGlobals._MainLightPosition.xyz);
u_xlat16_5 = u_xlat16_20 + half(-1.0);
u_xlat5 = fma(float(UnityPerMaterial._ReceiveShadowMappingAmount), float(u_xlat16_5), 1.0);
u_xlat16_20 = (u_xlatb18) ? half(1.0) : half(u_xlat5);
u_xlat16_9 = min(UnityPerDraw.unity_LightData.z, half(2.0));
u_xlat16_20 = u_xlat16_20 * u_xlat16_9;
u_xlat16_9 = (-UnityPerMaterial._CelShadeSoftness) + UnityPerMaterial._CelShadeMidPoint;
u_xlat16_3.z = UnityPerMaterial._CelShadeSoftness + UnityPerMaterial._CelShadeMidPoint;
u_xlat16_3.xz = (-half2(u_xlat16_9)) + u_xlat16_3.xz;
u_xlat16_9 = half(1.0) / u_xlat16_3.z;
u_xlat16_3.x = u_xlat16_9 * u_xlat16_3.x;
u_xlat16_3.x = clamp(u_xlat16_3.x, 0.0h, 1.0h);
u_xlat16_9 = fma(u_xlat16_3.x, half(-2.0), half(3.0));
u_xlat16_3.x = u_xlat16_3.x * u_xlat16_3.x;
u_xlat16_3.x = u_xlat16_3.x * u_xlat16_9;
u_xlat16_20 = u_xlat16_20 * u_xlat16_3.x;
u_xlat16_20 = u_xlat16_20 * UnityPerMaterial._DirectLightMultiplier;
u_xlat16_3.xyz = u_xlat16_0.xyz * FGlobals._MainLightColor.xyz;
u_xlat16_3.xyz = half3(u_xlat16_20) * u_xlat16_3.xyz;
u_xlat16_3.xyz = fma(u_xlat16_0.xyz, u_xlat16_4.xyz, u_xlat16_3.xyz);
u_xlat16_2.xyz = u_xlat16_2.xyz + u_xlat16_3.xyz;
output.SV_TARGET0.xyz = u_xlat16_2.xyz * UnityPerMaterial._OutlineColor.xyzx.xyz;
output.SV_TARGET0.w = half(1.0);
return output;
}