- Home /
Custom shader works in editor, displays in all pink on Android. Fragment shader shows in logcat
Here's the shader I wrote:
Shader "Custom/InstancedContourShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_LineWidthInverse ("LineWidthInverse", Float) = 0.98
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags {"RenderType"="Opaque" "LightMode"="ForwardBase"}
LOD 100
Pass
{
CGPROGRAM
#pragma debug
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float2 texcoord: TEXCOORD;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float2 uv : TEXCOORD0;
fixed4 diff : COLOR0;
float4 vertex : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
sampler2D _MainTex;
float4 _MainTex_ST;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
#pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_CBUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
UNITY_DEFINE_INSTANCED_PROP(float1, _LineWidthInverse)
// fixed4 _Color;
UNITY_INSTANCING_CBUFFER_END
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half nl = abs(dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.diff = nl * _LightColor0;
// the only difference from previous shader:
// in addition to the diffuse lighting from the main light,
// add illumination from ambient or light probes
// ShadeSH9 function from UnityCG.cginc evaluates it,
// using world space normal
o.diff.rgb += ShadeSH9(half4(worldNormal,1));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
// sample the texture
fixed4 col = tex2D (_MainTex, i.uv) * UNITY_ACCESS_INSTANCED_PROP(_Color);
col *= i.diff;
float t = max (abs(i.uv.x), abs(i.uv.y));
t = min(t - UNITY_ACCESS_INSTANCED_PROP(_LineWidthInverse), 0);
t = -sign(t);
col *= t;
return col;
}
ENDCG
}
}
}
The shader is used in several materials, all are directly attached to gameobjects. I tried applying fixes suggested for similar questions like adding the shader to always include in the project settings and disabling stripping for instanced variants. But to no avail.
Here's the adb logcat output regarding shaders:
09-05 11:38:19.470 19632 19648 D Unity : GL_EXT_debug_marker GL_ARM_rgba8 GL_ARM_mali_shader_binary GL_OES_depth24 GL_OES_depth_texture GL_OES_depth_texture_cube_map GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_EXT_read_format_bgra GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_standard_derivatives GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_EGL_image_external_essl3 GL_OES_EGL_sync GL_OES_texture_npot GL_OES_vertex_half_float GL_OES_required_internalformat GL_OES_vertex_array_object GL_OES_mapbuffer GL_EXT_texture_format_BGRA8888 GL_EXT_texture_rg GL_EXT_texture_type_2_10_10_10_REV GL_OES_fbo_render_mipmap GL_OES_element_index_uint GL_EXT_shadow_samplers GL_OES_texture_compression_astc GL_KHR_texture_compression_astc_ldr GL_KHR_texture_compression_astc_hdr GL_KHR_texture_compression_astc_sliced_3d GL_KHR_debug GL_EXT_occlusion_query_boolean GL_EXT_disjoint_timer_query GL_EXT_blend_minmax GL_EXT_discard_framebuffer GL_OES_get_program_binary GL_OES_texture_3D GL_EXT_texture_storage GL_EXT_multisampled_render_09-05 11:38:19.470 19632 19648 D Unity : to_texture GL_OES_surfaceless_context GL_OES_texture_stencil8 GL_EXT_shader_pixel_local_storage GL_ARM_shader_framebuffer_fetch GL_ARM_shader_framebuffer_fetch_depth_stencil GL_ARM_mali_program_binary GL_EXT_sRGB GL_EXT_sRGB_write_control GL_EXT_texture_sRGB_decode GL_KHR_blend_equation_advanced GL_KHR_blend_equation_advanced_coherent GL_OES_texture_storage_multisample_2d_array GL_OES_shader_image_atomic GL_EXT_robustness GL_EXT_draw_buffers_indexed GL_OES_draw_buffers_indexed GL_EXT_texture_border_clamp GL_OES_texture_border_clamp GL_EXT_texture_cube_map_array GL_OES_texture_cube_map_array GL_OES_sample_variables GL_OES_sample_shading GL_OES_shader_multisample_interpolation GL_EXT_shader_io_blocks GL_OES_shader_io_blocks GL_EXT_tessellation_shader GL_OES_tessellation_shader GL_EXT_primitive_bounding_box GL_OES_primitive_bounding_box GL_EXT_geometry_shader GL_OES_geometry_shader GL_ANDROID_extension_pack_es31a GL_EXT_gpu_shader5 GL_OES_gpu_shader5 GL_EXT_texture_buffer GL_OES_texture_buffer GL_EXT_copy_image09-05 11:38:19.470 19632 19648 D Unity : GL_OES_copy_image GL_EXT_color_buffer_half_float GL_EXT_color_buffer_float GL_EXT_YUV_target GL_OVR_multiview GL_OVR_multiview2 GL_OVR_multiview_multisampled_render_to_texture GL_KHR_robustness GL_KHR_robust_buffer_access_behavior GL_EXT_draw_elements_base_vertex GL_OES_draw_elements_base_vertex GL_EXT_protected_textures
09-05 11:38:19.959 19632 19648 D Unity : #version 300 es
09-05 11:38:19.959 19632 19648 D Unity : precision highp float;
09-05 11:38:19.959 19632 19648 D Unity : precision highp int;
09-05 11:38:19.959 19632 19648 D Unity : uniform mediump vec4 _Color;
09-05 11:38:19.959 19632 19648 D Unity : uniform float _LineWidthInverse;
09-05 11:38:19.959 19632 19648 D Unity : uniform lowp sampler2D _MainTex;
09-05 11:38:19.959 19632 19648 D Unity : in highp vec2 vs_TEXCOORD0;
09-05 11:38:19.959 19632 19648 D Unity : in mediump vec4 vs_COLOR0;
09-05 11:38:19.959 19632 19648 D Unity : layout(location = 0) out mediump vec4 SV_Target0;
09-05 11:38:19.959 19632 19648 D Unity : vec4 u_xlat0;
09-05 11:38:19.959 19632 19648 D Unity : int u_xlati0;
09-05 11:38:19.959 19632 19648 D Unity : mediump vec4 u_xlat16_1;
09-05 11:38:19.959 19632 19648 D Unity : lowp vec4 u_xlat10_1;
09-05 11:38:19.959 19632 19648 D Unity : void main()
09-05 11:38:19.959 19632 19648 D Unity : {
09-05 11:38:19.959 19632 19648 D Unity : u_xlat0.x = max(abs(vs_TEXCOORD0.y), abs(vs_TEXCOORD0.x));
09-05 11:38:19.959 19632 19648 D Unity : u_xlat0.x = u_xlat0.x + (-_LineWidthInverse.x);
09-05 11:38:19.959 19632 19648 D Unity : u_xlat0.x = min(u_xlat0.x, 0.0);
09-05 11:38:19.959 19632 19648 D Unity : #ifdef UNITY_ADRENO_ES3
09-05 11:38:19.959 19632 19648 D Unity : { bool cond = u_xlat0.x<0.0; u_xlati0 = int(!!cond ? 0xFFFFFFFFu : uint(0
u)); }
09-05 11:38:19.959 19632 19648 D Unity : #else
09-05 11:38:19.959 19632 19648 D Unity : u_xlati0 = int((u_xlat0.x<0.0) ? 0xFFFFFFFFu : uint(0u));
09-05 11:38:19.959 19632 19648 D Unity : #endif
09-05 11:38:19.959 19632 19648 D Unity : u_xlat0.x = float((-u_xlati0));
09-05 11:38:19.959 19632 19648 D Unity : u_xlat10_1 = texture(_MainTex, vs_TEXCOORD0.xy);
09-05 11:38:19.959 19632 19648 D Unity : u_xlat16_1 = u_xlat10_1 * _Color;
09-05 11:38:19.959 19632 19648 D Unity : u_xlat16_1 = u_xlat16_1 * vs_COLOR0;
09-05 11:38:19.959 19632 19648 D Unity : u_xlat0 = u_xlat0.xxxx * u_xlat16_1;
09-05 11:38:19.959 19632 19648 D Unity : SV_Target0 = u_xlat0;
09-05 11:38:19.959 19632 19648 D Unity : return;
09-05 11:38:19.959 19632 19648 D Unity : }
09-05 11:38:19.959 19632 19648 D Unity : fragment evaluation shader
09-05 11:38:19.965 19632 19648 D Unity : #version 300 es
09-05 11:38:19.965 19632 19648 D Unity : precision highp float;
09-05 11:38:19.965 19632 19648 D Unity : precision highp int;
09-05 11:38:19.965 19632 19648 D Unity : uniform mediump vec4 _Color;
09-05 11:38:19.965 19632 19648 D Unity : uniform float _LineWidthInverse;
09-05 11:38:19.965 19632 19648 D Unity : uniform lowp sampler2D _MainTex;
09-05 11:38:19.965 19632 19648 D Unity : in highp vec2 vs_TEXCOORD0;
09-05 11:38:19.965 19632 19648 D Unity : in mediump vec4 vs_COLOR0;
09-05 11:38:19.965 19632 19648 D Unity : layout(location = 0) out mediump vec4 SV_Target0;
09-05 11:38:19.965 19632 19648 D Unity : vec4 u_xlat0;
09-05 11:38:19.965 19632 19648 D Unity : int u_xlati0;
09-05 11:38:19.965 19632 19648 D Unity : mediump vec4 u_xlat16_1;
09-05 11:38:19.965 19632 19648 D Unity : lowp vec4 u_xlat10_1;
09-05 11:38:19.965 19632 19648 D Unity : void main()
09-05 11:38:19.965 19632 19648 D Unity : {
09-05 11:38:19.965 19632 19648 D Unity : u_xlat0.x = max(abs(vs_TEXCOORD0.y), abs(vs_TEXCOORD0.x));
09-05 11:38:19.965 19632 19648 D Unity : u_xlat0.x = u_xlat0.x + (-_LineWidthInverse.x);
09-05 11:38:19.965 19632 19648 D Unity : u_xlat0.x = min(u_xlat0.x, 0.0);
09-05 11:38:19.965 19632 19648 D Unity : #ifdef UNITY_ADRENO_ES3
09-05 11:38:19.965 19632 19648 D Unity : { bool cond = u_xlat0.x<0.0; u_xlati0 = int(!!cond ? 0xFFFFFFFFu : uint(0
u)); }
09-05 11:38:19.965 19632 19648 D Unity : #else
09-05 11:38:19.965 19632 19648 D Unity : u_xlati0 = int((u_xlat0.x<0.0) ? 0xFFFFFFFFu : uint(0u));
09-05 11:38:19.965 19632 19648 D Unity : #endif
09-05 11:38:19.965 19632 19648 D Unity : u_xlat0.x = float((-u_xlati0));
09-05 11:38:19.965 19632 19648 D Unity : u_xlat10_1 = texture(_MainTex, vs_TEXCOORD0.xy);
09-05 11:38:19.965 19632 19648 D Unity : u_xlat16_1 = u_xlat10_1 * _Color;
09-05 11:38:19.965 19632 19648 D Unity : u_xlat16_1 = u_xlat16_1 * vs_COLOR0;
09-05 11:38:19.965 19632 19648 D Unity : u_xlat0 = u_xlat0.xxxx * u_xlat16_1;
09-05 11:38:19.965 19632 19648 D Unity : SV_Target0 = u_xlat0;
09-05 11:38:19.965 19632 19648 D Unity : return;
09-05 11:38:19.965 19632 19648 D Unity : }
09-05 11:38:19.965 19632 19648 D Unity : fragment evaluation shader
09-05 11:38:28.459 19632 19648 D Unity : #version 300 es
09-05 11:38:28.459 19632 19648 D Unity : precision highp float;
09-05 11:38:28.459 19632 19648 D Unity : precision highp int;
09-05 11:38:28.459 19632 19648 D Unity : uniform int unity_BaseInstanceID;
09-05 11:38:28.459 19632 19648 D Unity : layout(std140) uniform UnityInstancing_Props {
09-05 11:38:28.459 19632 19648 D Unity : mediump vec4 _Color[125];
09-05 11:38:28.459 19632 19648 D Unity : float _LineWidthInverse[125];
09-05 11:38:28.459 19632 19648 D Unity : };
09-05 11:38:28.459 19632 19648 D Unity : uniform lowp sampler2D _MainTex;
09-05 11:38:28.459 19632 19648 D Unity : in highp vec2 vs_TEXCOORD0;
09-05 11:38:28.459 19632 19648 D Unity : in mediump vec4 vs_COLOR0;
09-05 11:38:28.459 19632 19648 D Unity : flat in highp uint vs_SV_InstanceID0;
09-05 11:38:28.459 19632 19648 D Unity : layout(location = 0) out mediump vec4 SV_Target0;
09-05 11:38:28.459 19632 19648 D Unity : vec4 u_xlat0;
09-05 11:38:28.459 19632 19648 D Unity : int u_xlati0;
09-05 11:38:28.459 19632 19648 D Unity : mediump vec4 u_xlat16_1;
09-05 11:38:28.459 19632 19648 D Unity : lowp vec4 u_xlat10_1;
09-05 11:38:28.459 19632 19648 D Unity : int u_xlati2;
09-05 11:38:28.459 19632 19648 D Unity : void main()
09-05 11:38:28.459 19632 19648 D Unity : {
09-05 11:38:28.459 19632 19648 D Unity : u_xlat0.x = max(abs(vs_TEXCOORD0.y), abs(vs_TEXCOORD0.x));
09-05 11:38:28.459 19632 19648 D Unity : u_xlati2 = int(vs_SV_InstanceID0) + unity_BaseInstanceID;
09-05 11:38:28.459 19632 19648 D Unity : u_xlat0.x = u_xlat0.x + (-_LineWidthInverse[u_xlati2].x);
09-05 11:38:28.459 19632 19648 D Unity : u_xlat0.x = min(u_xlat0.x, 0.0);
09-05 11:38:28.459 19632 19648 D Unity : #ifdef UNITY_ADRENO_ES3
09-05 11:38:28.459 19632 19648 D Unity : { bool cond = u_xlat0.x<0.0; u_xlati0 = int(!!cond ? 0xFFFFFFFFu : uint(0
u)); }
09-05 11:38:28.459 19632 19648 D Unity : #else
09-05 11:38:28.459 19632 19648 D Unity : u_xlati0 = int((u_xlat0.x<0.0) ? 0xFFFFFFFFu : uint(0u));
09-05 11:38:28.459 19632 19648 D Unity : #endif
09-05 11:38:28.459 19632 19648 D Unity : u_xlat0.x = float((-u_xlati0));
09-05 11:38:28.459 19632 19648 D Unity : u_xlat10_1 = texture(_MainTex, vs_TEXCOORD0.xy);
09-05 11:38:28.459 19632 19648 D Unity : u_xlat16_1 = u_xlat10_1 * _Color[u_xlati2];
09-05 11:38:28.459 19632 19648 D Unity : u_xlat16_1 = u_xlat16_1 * vs_COLOR0;
09-05 11:38:28.459 19632 19648 D Unity : u_xlat0 = u_xlat0.xxxx * u_xlat16_1;
09-05 11:38:28.459 19632 19648 D Unity : SV_Target0 = u_xlat0;
09-05 11:38:28.459 19632 19648 D Unity : return;
09-05 11:38:28.459 19632 19648 D Unity : }
09-05 11:38:28.459 19632 19648 D Unity : fragment evaluation shader
09-05 11:38:28.461 19632 19648 D Unity : #version 300 es
09-05 11:38:28.461 19632 19648 D Unity : precision highp float;
09-05 11:38:28.461 19632 19648 D Unity : precision highp int;
09-05 11:38:28.461 19632 19648 D Unity : uniform int unity_BaseInstanceID;
09-05 11:38:28.461 19632 19648 D Unity : layout(std140) uniform UnityInstancing_Props {
09-05 11:38:28.461 19632 19648 D Unity : mediump vec4 _Color[125];
09-05 11:38:28.461 19632 19648 D Unity : float _LineWidthInverse[125];
09-05 11:38:28.461 19632 19648 D Unity : };
09-05 11:38:28.461 19632 19648 D Unity : uniform lowp sampler2D _MainTex;
09-05 11:38:28.461 19632 19648 D Unity : in highp vec2 vs_TEXCOORD0;
09-05 11:38:28.461 19632 19648 D Unity : in mediump vec4 vs_COLOR0;
09-05 11:38:28.461 19632 19648 D Unity : flat in highp uint vs_SV_InstanceID0;
09-05 11:38:28.461 19632 19648 D Unity : layout(location = 0) out mediump vec4 SV_Target0;
09-05 11:38:28.461 19632 19648 D Unity : vec4 u_xlat0;
09-05 11:38:28.461 19632 19648 D Unity : int u_xlati0;
09-05 11:38:28.461 19632 19648 D Unity : mediump vec4 u_xlat16_1;
09-05 11:38:28.461 19632 19648 D Unity : lowp vec4 u_xlat10_1;
09-05 11:38:28.461 19632 19648 D Unity : int u_xlati2;
09-05 11:38:28.461 19632 19648 D Unity : void main()
09-05 11:38:28.461 19632 19648 D Unity : {
09-05 11:38:28.461 19632 19648 D Unity : u_xlat0.x = max(abs(vs_TEXCOORD0.y), abs(vs_TEXCOORD0.x));
09-05 11:38:28.461 19632 19648 D Unity : u_xlati2 = int(vs_SV_InstanceID0) + unity_BaseInstanceID;
09-05 11:38:28.461 19632 19648 D Unity : u_xlat0.x = u_xlat0.x + (-_LineWidthInverse[u_xlati2].x);
09-05 11:38:28.461 19632 19648 D Unity : u_xlat0.x = min(u_xlat0.x, 0.0);
09-05 11:38:28.461 19632 19648 D Unity : #ifdef UNITY_ADRENO_ES3
09-05 11:38:28.461 19632 19648 D Unity : { bool cond = u_xlat0.x<0.0; u_xlati0 = int(!!cond ? 0xFFFFFFFFu : uint(0
u)); }
09-05 11:38:28.461 19632 19648 D Unity : #else
09-05 11:38:28.461 19632 19648 D Unity : u_xlati0 = int((u_xlat0.x<0.0) ? 0xFFFFFFFFu : uint(0u));
09-05 11:38:28.461 19632 19648 D Unity : #endif
09-05 11:38:28.461 19632 19648 D Unity : u_xlat0.x = float((-u_xlati0));
09-05 11:38:28.461 19632 19648 D Unity : u_xlat10_1 = texture(_MainTex, vs_TEXCOORD0.xy);
09-05 11:38:28.461 19632 19648 D Unity : u_xlat16_1 = u_xlat10_1 * _Color[u_xlati2];
09-05 11:38:28.461 19632 19648 D Unity : u_xlat16_1 = u_xlat16_1 * vs_COLOR0;
09-05 11:38:28.461 19632 19648 D Unity : u_xlat0 = u_xlat0.xxxx * u_xlat16_1;
09-05 11:38:28.461 19632 19648 D Unity : SV_Target0 = u_xlat0;
09-05 11:38:28.461 19632 19648 D Unity : return;
09-05 11:38:28.461 19632 19648 D Unity : }
09-05 11:38:28.461 19632 19648 D Unity : fragment evaluation shader
Since the compiled fragment shader part is output several times (once for each material I'm guessing) there should be a problem with the fragment shader specifically but it doesn't tell me what the problem is.
It's a Gear VR app running on an S7.
Answer by akillingbeck · Sep 06, 2017 at 08:27 AM
What graphics API is in use? Instancing won't work on all devices.
If I uncheck Auto Graphics API in the player settings, these three are listed: OpenGLES3, OpenGLES2, Vulkan. I don't know which one is actually used on the device.
Instancing should not be the problem. I previously used a custom surface shader with instancing enabled and that worked without problem. To get pixel contour shading I had to implement instancing again but this time with vertex and fragment shaders. It works in the editor, not on the device.
So if instancing is the problem, it would be specific to the vert&frag implementation. It is more likely though that the pixel shading in the fragment shader is to blame, since that part is output in logcat for some reason.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134235im_/https://answers.unity.com/themes/thub/images/avi.jpg)