<?xml version="1.0" encoding="utf-8" ?> 

<post_processor>	
	<filters>
		<filter name="identity">
			<render_template name="identity"/>
		</filter>
		<filter name="9_tap_filter">
			<render_template name="9_tap_filter"/>
		</filter>
	</filters>
	
	<!-- TODO : Get rid of any material that we don't need -->
	
	<materials config_name="deferred_processor">		
		<material name="shadow">
			<variable name="fadeout_blend" type="scalar" value="0.5"/>			
			
			<variable name="shadow_slice_depths" type="mirror" material="shadow" material_config="shadow_processor" variable="shadow_slice_depths"/>
			<variable name="shadow_slice_overlap" type="mirror" material="shadow" material_config="shadow_processor" variable="shadow_slice_overlap"/>		
			<variable name="shadow_fadeout" type="mirror" material="shadow" material_config="shadow_processor" variable="shadow_fadeout"/>			
			
			<variable name="slice0" type="mirror" material="shadow" material_config="shadow_processor" variable="slice0"/>			
			<variable name="slice1" type="mirror" material="shadow" material_config="shadow_processor" variable="slice1"/>			
			<variable name="slice2" type="mirror" material="shadow" material_config="shadow_processor" variable="slice2"/>			
			<variable name="slice3" type="mirror" material="shadow" material_config="shadow_processor" variable="slice3"/>			
			
		<!-- Composite shadow globlaight channels -->
			<variable name="dome_occ_pos" type="vector3" value="0 0 -900"/> <!-- start end amount -->
			<variable name="dome_occ_size" type="vector3" value="15000 15000 0"/> <!-- start end amount -->
			
			<filter_color_texture file="core/textures/dome_occ_test"/>
			
			<variable name="sky_top_color" type="vector3" value="0.15 0.125 0.0"/>
			<variable name="sky_bottom_color" type="vector3" value="0.15 0.125 0.0"/>
			<variable name="ambient_color" type="vector3" value="0.5 0.7 0.8"/>
			<variable name="fog_min_range" type="scalar" value="1000"/>
			<variable name="fg_max_range" type="scalar" value="25000"/>
						
			<variable name="fov_ratio" type="mirror" material="shadow" material_config="shadow_processor" variable="fov_ratio"/>
		</material>
		
		<material name="move_global_occ">
			<filter_color_texture file="core/textures/dome_occ_test"/>
		</material>
		
		<material name="global_lighting">
		<!-- TODO : These two are only used for debug, remove for release -->
			<variable name="ambient_scale" type="scalar" value="1"/>
			<variable name="ambient_falloff_scale" type="scalar" value="0.5"/>			
			
		<!-- Light -->
			<variable name="sun_specular_color" type="vector3" value="1 1 1"/>
			<variable name="sky_top_color" type="vector3" value="0.15 0.125 0.0"/>		
			<variable name="sky_bottom_color" type="vector3" value="0.15 0.125 0.0"/>		
			<variable name="ambient_color" type="vector3" value="0.5 0.7 0.8"/>
			
		<!-- TODO : Old params, kill these -->
			<variable name="height_fade" type="vector3" value="-1000 10000 0"/>
			<variable name="height_fade_intesity_clamp" type="vector3" value="0.1 1 0"/>
			<variable name="sky_reflection_top_color" type="vector3" value="0.15 0.125 0.0"/>		
			<variable name="sky_reflection_bottom_color" type="vector3" value="0.15 0.125 0.0"/>					
			<variable name="environment_map_intensity" type="scalar" value="5.0"/>
			<variable name="environment_map_intensity_shadow" type="scalar" value="3.0"/>
			<variable name="effect_light_scale" type="scalar" value="1.0"/>
			<variable name="ambient_view_falloff_sharpness" type="scalar" value="0.5"/>
			
		<!-- Dome occlusion -->
			<variable name="dome_occ_pos" type="vector3" value="0 0 -900"/> <!-- start end amount -->
			<variable name="dome_occ_size" type="vector3" value="15000 15000 0"/> <!-- start end amount -->
			
			<filter_color_texture file="core/textures/dome_occ_test"/>
			
		<!-- Dome reflection -->
			<reflection_texture type="cubemap" file="core/textures/skydome_cube_test"/>
				<!-- TODO : Use global cubemap texture here -->

        <!-- SSAO -->
            <variable name="use_ssao" type="scalar" value="1"/>
            
        <!-- Bloom -->
        	<variable name="bloom_threshold" type="scalar" value="0.55"/>

		<!-- Fog settings -->
			<!-- Colors -->
			<variable name="fog_start_color" type="vector3" value="0 0 1"/>
			<variable name="fog_far_low_color" type="vector3" value="1 0 0"/>
			<variable name="fog_far_high_color" type="vector3" value="0 1 0"/>
			<variable name="fog_sun_scatter_color" type="vector3" value="1 0.5 0"/>
			
			<!-- Alphas -->
			<variable name="start" type="scalar" value="0"/>
			<variable name="alpha0" type="scalar" value="1"/>
			<variable name="alpha1" type="scalar" value="1"/>
			
			<!-- Ranges -->
			<variable name="fog_sun_range" type="scalar" value="14000"/>
			<variable name="fog_min_range" type="scalar" value="1000"/>
			<variable name="fog_max_range" type="scalar" value="25000"/>
			<variable name="fog_height_range" type="scalar" value="25000"/>
			<variable name="fog_max_density" type="scalar" value="1"/>
			
			<!-- Curve -->
			<variable name="fog_curve" type="scalar" value="0.5"/>
			
		</material>
		
		<material name="local_ssao">		
			<variable name="occluder_radius" type="scalar" value="2"/>
			<variable name="falloff_radius" type="scalar" value="10"/>
			<variable name="intensity" type="scalar" value="2.5"/>
		</material>
		
		<material name="global_ssao">
			<variable name="occluder_radius" type="scalar" value="8"/>
			<variable name="falloff_radius" type="scalar" value="35"/>
			<variable name="intensity" type="scalar" value="2.5"/>
		</material>
		
		<material name="ssao_apply">		
			<variable name="intensity" type="scalar" value="0.93"/>
		</material>
				
	</materials>
	
	<!-- DEBUGGING -->
	<effect name="albedo_visualization">
		<texture_filter src="albedo" dst="bb" filter="identity" render_template="debug_visualization:ALBEDO"/>
	</effect>
	<effect name="specular_visualization">
		<texture_filter src="normal" dst="bb" filter="identity" render_template="debug_visualization:ALPHA_MASK"/>
	</effect>
	<effect name="glossiness_visualization">
		<texture_filter src="albedo" dst="bb" filter="identity" render_template="debug_visualization:ALPHA_MASK"/>
	</effect>
	<effect name="reflection_visualization">
		<texture_filter src="reflections" dst="bb" filter="identity"/>
	</effect>
	<effect name="normal_visualization">
		<texture_filter src="normal" dst="bb" filter="identity"/>
	</effect>
	<effect name="depth_visualization">
		<texture_filter src="depth" dst="bb" filter="identity" render_template="depth_visualization"/>		
	</effect>
	<effect name="ambient_visualization">
		<texture_filter src="ambient" dst="bb" filter="identity"/>
	</effect>		
	<effect name="test">
		<combiner diffuse_texture="albedo" dst="bb" render_template="spass_directional"/>
	</effect>	
	<effect name="debug_shadow">
		<shadow_mapper dst="shadow_depth"/>
		<texture_filter src="shadow_depth" dst="bb" filter="identity"/>
	</effect>		
	
	
	<render_targets>
		<!-- <render_target name="lowres_depth" src="albedo" w_scale="0.5" h_scale="0.5" format="R32" msaa="false" hint_needs_clearing="false"/> -->
	</render_targets>
	
	<effect name="no_light">
	</effect>
	
	<effect name="deferred_lighting">
		<defined platform="X360">
			<x360_renderer_command command="gpr_allocation" flags="0" vertex_count="26" pixel_count="102"/>
			<!-- <combiner dst="temp" depth_stencil_target="scene_depth_stencil" render_template="stencil_mark_skydome"/> -->
			<x360_renderer_command command="flush_hi_z_stencil" flush_type="ASYNCH"/>
		</defined>
		
	<!-- DEPTH/STENCIL FIX -->
		<!--
		<defined platform="X360">
			<combiner dst="temp" depth_stencil_target="scene_depth_stencil" render_template="stencil_mark_skydome"/>
			<x360_renderer_command command="flush_hi_z_stencil" flush_type="ASYNCH"/>
		</defined>
		-->
		
	<!-- DYNAMIC LIGHTS -->
		<!-- TODO : GENERATE_PROJ move this to another pass, to support spec on of later -->
		<!-- Console or low end PC -->
		
		<defined platform="PS3 X360">
			<deferred_shading
				dst = "temp"
				depth_stencil_target="scene_depth_stencil"
				
				diffuse_texture="low_target_6"			
				
				omni_rt="omni"
				omni_specular_rt="omni:SPECULAR" 
				omni_cube_projection_rt="omni:PROJECTION"
				omni_specular_cube_projection_rt="omni:PROJECTION:SPECULAR"
				
				spot_rt="spot"  
				spot_specular_rt="spot:SPECULAR" 
				spot_projection_rt="spot:PROJECTION"
				spot_specular_projection_rt="spot:PROJECTION:SPECULAR"
	
				directional_rt="deferred_directional"
				directional_specular_rt="deferred_directional_specular"
				geometry_file="core/shaders/light_volumes"
				omni_geometry="omni"
				spot_geometry="spot"
				omni_attenuation_lookup="core/shaders/light_lookup_linear"
				spot_attenuation_lookup="core/shaders/light_lookup_hard"
			/>
		<else/>
			<deferred_shading 
				dst = "temp"
				depth_stencil_target="scene_depth_stencil"
				
				diffuse_texture="low_target_6"			
				
				omni_rt="omni:HQ"
				omni_specular_rt="omni:HQ:SPECULAR" 
				omni_cube_projection_rt="omni:HQ:PROJECTION"
				omni_specular_cube_projection_rt="omni:HQ:PROJECTION:SPECULAR"
				
				spot_rt="spot"  
				spot_specular_rt="spot:HQ:SPECULAR" 
				spot_projection_rt="spot:HQ:PROJECTION"
				spot_specular_projection_rt="spot:HQ:PROJECTION:SPECULAR" 

				spot_falloff_rt="spot:INVSQ"
				spot_falloff_specular_rt="spot:HQ:INVSQ:SPECULAR"
				spot_falloff_projection_rt="spot:HQ:INVSQ:PROJECTION"
				spot_falloff_specular_projection_rt="spot:HQ:INVSQ:PROJECTION:SPECULAR"

				directional_rt="deferred_directional"
				directional_specular_rt="deferred_directional_specular"
				geometry_file="core/shaders/light_volumes"
				omni_geometry="omni"
				spot_geometry="spot"
				omni_attenuation_lookup="core/shaders/light_lookup_linear"
				spot_attenuation_lookup="core/shaders/light_lookup_hard"
			/>
		</defined>
		
		<!-- LOWRES DEPTH GENERATION AND SUN LIGHT OPTIMIZATION 
		<combiner diffuse_texture="depth" dst="depth_lowres" render_template="depth_downsample"/>	
		<combiner diffuse_texture="depth_lowres" displacement_texture="shadow_depth" dst="low_target_3_b" depth="slice2.y" render_template="global_shadow_mask_pregen" material="shadow" scriptable="shadow" render_settings="shadow_quality=high"/>
		<texture_filter src="low_target_3_b" dst="low_target_3" filter="9_tap_filter" bake_filter_taps="false"/>
		<texture_filter src="low_target_3" dst="low_target_3_b" filter="9_tap_filter" bake_filter_taps="false"/>
		<texture_filter src="low_target_3_b" dst="low_target_4" filter="9_tap_filter" bake_filter_taps="false"/>
		<texture_filter src="low_target_4" dst="low_target_5" filter="9_tap_filter" bake_filter_taps="false"/>
		-->
		
		<!-- Replace with post_downsample, quicker and more stable -->
		<!-- Also, we should make a new one, that only cares about one channel, to cut down on fillrate and instructions etc -->
		
		<!-- NOTE : This one is hacked right now to output borders everywhere, until decisions are made about when and what to stencil -->
			<!-- Lowres depth is going to be there anyways for the cheaper particles. So thats no argument -->
			<!-- The one pass optimization has also been written, so the lowres shadow can use that -->
		
		<!-- <combiner diffuse_texture="low_target_5" dst="low_target_3" render_template="filter_shadow_areas"/>-->
		
		<!-- CLEAR STENCIL WITH 0 -->
		<!--<combiner diffuse_texture="low_target_3" dst="bb" render_template="filter_mark_shadow_areas:CLEAR"/>-->
		
		<!-- STENCIL MARK SHADOW BORDER INTO 1 -->
		<!--<combiner diffuse_texture="low_target_3" dst="bb" render_template="filter_mark_shadow_areas:BORDER"/>-->
		
			<!-- TODO : Remove this, sort out how the stencil is handled -->
			<!-- Right now, we get clipping in the shadow borders, sample several times instead, do one pass. for PC -->
				<!-- For console, use the multi render layer version, double sample in the closest step -->
		
		<!-- PROJECT A ONE SAMPLE SHADOW ON DOWNSIZED BUFFER -->
		<!-- BLUR THE SMALL SHADOW BUFFER ONE STEP, MIGHT NOT BE NEEDED -->
		<!-- CREATE A STENCIL MASK SHADER THAN CAN OUTPUT DARK/BRIGHT/SHADOW -->
		
	<!-- SHADOWS -->
		<defined platform="PS3"> <!-- NVBDT -->
			<!-- Dynamic branching in the fragment program is extremly expensive on PS3 so we fall back to a multiplass approach which utilizes DBT to cull pixels. -->
			<!-- <combiner diffuse_texture="depth" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask:SLICE0" depth_s="0" depth_e="slice0.y" material="shadow" scriptable="shadow"/> -->
		 	
			<combiner diffuse_texture="low_target_6" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask:SLICE0" depth_s="0" depth_e="slice0.y" material="shadow" scriptable="shadow"/>
			<combiner diffuse_texture="low_target_6" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask:SLICE1" depth_s="slice0.y" depth_e="slice1.y" material="shadow"/>	
			<combiner diffuse_texture="low_target_6" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask:SLICE2" depth_s="slice1.y" depth_e="slice2.y" material="shadow"/>
			<!-- depth_stencil_target="scene_depth_stencil" -->
			
			<!-- render_target="albedo normal" -->
			
			<!-- TODO :  Change this to albedo -->
			<!-- <combiner dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask_fill" depth="slice2.y" render_settings="shadow_quality=high" material="shadow"/> -->
		<else/>
			<defined platform="X360"> <!-- NVBDT -->
				<combiner diffuse_texture="low_target_6" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask" depth="slice3.y" material="shadow" scriptable="shadow" render_settings="shadow_quality=high"/>		
				
				<!-- depth_stencil_target="scene_depth_stencil" -->
				
				<!-- <combiner diffuse_texture="low_target_6" dst="albedo" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask_fill" depth="slice2.y" render_settings="shadow_quality=high" material="shadow"/> -->
			<else/>
				<combiner diffuse_texture="low_target_6" displacement_texture="shadow_depth" dst="temp" depth_stencil_target="scene_depth_stencil" depth="slice2.y" render_template="global_shadow_mask" material="shadow" scriptable="shadow" render_settings="shadow_quality=high"/>
				<combiner diffuse_texture="low_target_6" dst="temp" depth_stencil_target="scene_depth_stencil" render_template="global_shadow_mask_fill" depth="slice2.y" render_settings="shadow_quality=high" material="shadow"/>
			</defined>
		</defined>
		
		<!-- TODO : Placing the AO in the back_buffer_copy is TEMP, this shader should be baked with the global light stuff -->
		<defined platform="PS3 X360"> <!-- Console or low end PC -->
			<!-- <combiner diffuse_texture="low_target_6" dst="bb" depth_stencil_target="scene_depth_stencil" render_template="global_lighting" material="global_lighting" scriptable="apply_ambient"/> -->
			
			<defined platform="PS3">
				<combiner diffuse_texture="low_target_6" dst="albedo" render_template="filter_shadow_areas" material="move_global_occ" scriptable="move_global_occ"/>
				
				<combiner diffuse_texture="normal" dst="bb" render_template="global_lighting" material="global_lighting" scriptable="apply_ambient" depth_stencil_target="scene_depth_stencil"/>
			<else/>
				<combiner diffuse_texture="normal" dst="bb" render_template="global_lighting" material="global_lighting" scriptable="apply_ambient" depth_stencil_target="scene_depth_stencil"/>
			</defined>
			
			<!-- Clear normal target for contour use -->
			<combiner diffuse_texture="low_target_6" dst="normal" render_template="post_clear_target"/>
		<else/>
			<combiner diffuse_texture="normal" self_illumination_texture="temp_ao" dst="bb" depth_stencil_target="scene_depth_stencil" render_template="global_lighting:HQ" material="global_lighting" scriptable="apply_ambient"/>
		</defined>
	</effect>
	
<!-- PROJECTION LIGHT RENDERING -->
	<!-- TODO : USE THIS WHEN RENDERING PROJECTION LIGHTS -->
	<!-- TODO : CREATE ON FOR AMBIENT SAMPLING TOO -->
	<effect name="projection_generation">
		<combiner diffuse_texture="depth" displacement_texture="shadow_depth" dst="bb" depth_stencil_target="scene_depth_stencil" depth="slice2.y" render_template="global_shadow_mask" material="shadow" scriptable="shadow" render_settings="shadow_quality=high"/>
		
		<combiner diffuse_texture="temp" dst="low_target_1" render_template="post_downscale"/>
		<combiner diffuse_texture="low_target_1" dst="low_target_1_b" render_template="post_downscale"/>
		<combiner diffuse_texture="low_target_1_b" dst="low_target_1" render_template="post_downscale"/>
		<combiner diffuse_texture="low_target_1" dst="temp" render_template="post_upscale"/>
		
		<deferred_shading
			dst = "temp"
			depth_stencil_target="scene_depth_stencil"
			
			diffuse_texture="temp"
			
			omni_rt="omni:SHADOW_GEN"
			omni_specular_rt="omni:SPECULAR" 
			omni_cube_projection_rt="omni:PROJECTION"
			omni_specular_cube_projection_rt="omni:PROJECTION:SPECULAR"
			
			spot_rt="spot"  
			spot_specular_rt="spot:SPECULAR" 
			spot_projection_rt="spot:PROJECTION"
			spot_specular_projection_rt="spot:PROJECTION:SPECULAR" 

			spot_falloff_rt="spot:INVSQ"
			spot_falloff_specular_rt="spot:INVSQ:SPECULAR"
			spot_falloff_projection_rt="spot:INVSQ:PROJECTION"
			spot_falloff_specular_projection_rt="spot:INVSQ:PROJECTION:SPECULAR"

			directional_rt="deferred_directional"
			directional_specular_rt="deferred_directional_specular"
			geometry_file="core/shaders/light_volumes"
			omni_geometry="omni"
			spot_geometry="spot"
			omni_attenuation_lookup="core/shaders/light_lookup_linear"
			spot_attenuation_lookup="core/shaders/light_lookup_hard"
		/>
	</effect>
</post_processor>
