includes = ["core/stingray_renderer/shader_libraries/common.shader_source" ]
render_states = { 	
	
	ribbon_opacity = {
		inherits = "opacity"
		states = {
			blend_op = "blend_op_add"
			dest_blend = "blend_inv_src_alpha"
			src_blend = "blend_one"
			
			defined_WIREFRAME = {
				fill_mode = "fill_wireframe"
			}
			defined_SCREEN_SPACE = {
				cull_mode = "cull_none"
				z_enable = "false"
			}
			ndefined_SCREEN_SPACE = {
				z_enable = "true"
			}
			defined_LOW_RES = {
				write_mask0 = "red|green|blue|alpha"
				write_mask1 = "red|green"
			}
			ndefined_LOW_RES = {
				write_mask0 = "red|green|blue"
			}
		}
	}	
}

hlsl_shaders = {

	ribbon = {
		includes = [ "common" ]
		
		samplers = {
			defined_DIFFUSE_MAP = {
				diffuse_map = { sampler_states = "wrap_anisotropic_srgb" }
			}
		}			
		
		code = """
			
			#if defined(DIFFUSE_MAP)
				#define UV0
				DECLARE_SAMPLER_2D(diffuse_map);
			#endif
			
			CBUFFER_START(c_billboard)
					float4x4 view;
					float4x4 view_proj;
			CBUFFER_END
			
			
			struct VS_INPUT {
				float4 position : POSITION;
				float strip_info : POSITION1;
				float2 size : TEXCOORD7;
				#if defined(VERTEX_COLOR)
					float4 color : COLOR;
				#endif
				#if defined(TANGENT_TO_NEXT)
					float3 tangent : TANGENT;
					float ribbon_distance : TEXCOORD6;
				#endif

			};
			
			struct PS_INPUT {
				float4 position : SV_POSITION;
				#if defined(VERTEX_COLOR)
					float4 color : COLOR;
				#endif
				#if defined(UV0)
					float2 uv : TEXCOORD0;
				#endif
				
			};
			
			DEFAULT_ROOT_SIGNATURE_ATTRIBUTE
			PS_INPUT vs_main(VS_INPUT input) {
				PS_INPUT o;
				
				#if defined(TANGENT_TO_NEXT)
					float3 y = input.tangent;
					float3 x = normalize(cross(normalize(input.position.xyz - camera_pos), y));
				#else
					float3 x = view._m00_m10_m20;
					float3 y = view._m02_m12_m22;
				#endif
				
				float3 x_axis = x;
				float3 y_axis = y;

				float corner = input.strip_info * (input.size.x * 0.5);
				
				float3 wp = input.position.xyz + (x_axis * corner);
				
				float4 p = mul(float4(wp, 1), view_proj);
				o.position = p;
				
				#if defined(VERTEX_COLOR)
					//#if defined(TANGENT_TO_NEXT)
					//	float4 dist = fmod(input.ribbon_distance, 2) / 2; // (input.ribbon_distance * 0.05f).xxxx;
					//	o.color = dist;
					//#else
						o.color = decode_vertex_color(input.color);
					//#endif
				#endif
				
				#if defined(UV0)
					float2 uv = float2(input.ribbon_distance, input.strip_info * 0.5 + 0.5);
					o.uv = uv;
				#endif
				
				
				return o;
			}
			
			float4 fast_gamma_to_linear(float4 c) {
				return c * c;
			}
			
			DEFAULT_ROOT_SIGNATURE_ATTRIBUTE
			float4 ps_main(PS_INPUT input) : SV_TARGET0 {
				
				#if defined(DIFFUSE_MAP)
					float4 c = TEX2D(diffuse_map, input.uv);
				#else
					float4 c = float4(1,1,1,1);
				#endif
				
				#if defined(VERTEX_COLOR)
					float4 vc = input.color;
					vc = fast_gamma_to_linear(vc);
					c *= vc;
				#endif
					
				float alpha = c.a;
				c.rgb *= alpha;
				#ifdef BLEND_ADDITIVE 
					c.a = 0.0;
				#endif
				
				return c;
			}
			
		"""
	}
}

shaders = {
	ribbon = {

		contexts = {
			default = {
				passes = [
					{ layer="hdr_transparent" hlsl_shader="ribbon" render_states="ribbon_opacity" }
				]
			}
		}
		
		compile = {
			default = [
				{ defines=[] }
			]
		}
		
	}
}