Below is a simplified version of the current shader system I'm working on, it pulls data from a one time dispatched compute shader into vertex program: v2f, and then parses this data to v2fc - this struct stores the static data with an added rend float. This float value, currently set manually, will be linked with an external input, properties or perhaps another compute kernel.
As seen in the frag program, this is used to effectively decided wither that instance is rendered or not.
I have also attempted using the 'discard' function, but i'm told this method is inefficient. Additionally the same if else I attempted with rend being an int, and including a switch statement.
All of these methods have given me the same result: the else , or default case option is always performed regardless of the switch/if condition.
I do have some experience with c++ but mainly c#, and am relatively new to complex shader systems, so forgive me if incorrect syntax.
I appreciate any advice or explanation to this problem.
//TODO Rendfiltermethods
Shader "Custom/StarShaderTemp"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color(" Color", Color) = (1, 0.5, 0.5, 1)
_Scale(" Scale ", Float) = 1
_Damping(" Damping Value", Float) = 500
}
SubShader
{
Pass
{
Tags { "RenderType" = "Transparent"}
ZTest Off
ZWrite Off
Cull Off
Blend SrcAlpha One
CGPROGRAM
#pragma target 5.0
#pragma vertex star_vertex
#pragma vertex star_cull
#pragma fragment frag
#pragma exclude_renderers gles
#include "UnityCG.cginc"
#include "StarStructInc.cginc"
StructuredBuffer<Star> stars;
StructuredBuffer<float3> quadPoints;
StructuredBuffer<int> cullint; //Input from cull compute shader for bypassing gifmain compute
sampler2D _MainTex;
sampler2D _O;
sampler2D _B;
sampler2D _A;
sampler2D _F;
sampler2D _G;
sampler2D _K;
sampler2D _M;
sampler2D _Blackhole;
float4 _Color;
float _Scale;
float _CamDist;
float _Damping;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2fc
{
v2f vetwof : v2f;
float rendfloat: float;
};
// vertex shader with no inputs
// uses the system values SV_VertexID and SV_InstanceID to read from compute buffers
v2f star_vertex(uint id : SV_VertexID, uint inst : SV_InstanceID)
{
v2f o;
float3 worldPosition = stars[inst].position;
float3 Qpoint = quadPoints[id] * _Scale * sqrt(_CamDist / _Damping);
o.pos = mul (UNITY_MATRIX_P, mul (UNITY_MATRIX_V, float4(worldPosition, 1.0f)) + float4(Qpoint, 0.0f));
o.uv = quadPoints[id] + 0.5f; //setting UV texture coord center
return o;
}
v2fc star_cull(v2f i)
{
v2fc c;
c.vetwof = i;
c.rendfloat = 6.0;
return c;
}
float4 frag (v2fc i) : COLOR
{
if(i.rendfloat < 5.0)
{
float4 texColn = tex2D (_MainTex, i.vetwof.uv);
return texColn * _Color;
}
else
{
i.vetwof.pos =0;
float4 texColn = tex2D (_MainTex, i.vetwof.uv);
return texColn * _Color;
}
}
ENDCG
}
}
FallBack "Diffuse"
}
The else behaviour is corrected by creating void functions instead of success frag ETC. After viewing the compiled code it seems there is an issue with the branch prediction used when optimising assembly in that frag function reference can be combined. Another fix is to use multiple passes with render targets.