abstract target cubemap_target
{
	pass render_scene
	{
		load
		{
			all				clear
			clear_colour	0 0 0 1
		}
		store
		{
			//We only care about the contents of the colour target
			depth			dont_care
			stencil			dont_care
		}

		expose cubemap

		//Filter stuff that shouldn't be reflected (vampires?)
		visibility_mask		0x00000005
		overlays			off
		camera_cubemap_reorient true
	}

	//Render sky (see TutorialSky_Postprocess)
	pass render_quad
	{
		store
		{
			//We only care about the contents of the colour target
			depth			dont_care
			stencil			dont_care
		}

		quad_normals	camera_direction
		material SkyPostprocess
		camera_cubemap_reorient true
	}
}

compositor_node CubemapRendererNode
{
	in 0 cubemap

	texture tmpCubemap target_width target_height target_format cubemap mipmaps 0

	target tmpCubemap +X : cubemap_target { }
	target tmpCubemap -X : cubemap_target { }
	target tmpCubemap +Y : cubemap_target { }
	target tmpCubemap -Y : cubemap_target { }
	target tmpCubemap +Z : cubemap_target { }
	target tmpCubemap -Z : cubemap_target
	{
		pass ibl_specular
		{
			input tmpCubemap
			output cubemap
		}
	}
}

// MSAA Cubemaps do not exist. Therefore we have to manually define the RTV (RenderTextureView)
// To render to a temporary MSAA surface and tell Ogre to resolve the MSAA contents into a cubemap
compositor_node CubemapRendererNodeMsaa
{
	in 0 cubemap

	// Note: IF you wish to preserve the MSAA contents of all 6 faces, then use
	//	'texture tmpMsaa target_width target_height 6 target_format 2d_array msaa 4 explicit_resolve'
	// instead
	texture tmpMsaa		target_width target_height target_format msaa 4 explicit_resolve
	texture tmpCubemap	target_width target_height target_format cubemap mipmaps 0

	rtv mixedCubemapRtv
	{
		// Specify we want to render to tmpMsaa at slot[0] but we want to resolve to tmpCubemap
		colour	0 tmpMsaa resolve tmpCubemap
	}

	target mixedCubemapRtv +X : cubemap_target { }
	target mixedCubemapRtv -X : cubemap_target { }
	target mixedCubemapRtv +Y : cubemap_target { }
	target mixedCubemapRtv -Y : cubemap_target { }
	target mixedCubemapRtv +Z : cubemap_target { }
	target mixedCubemapRtv -Z : cubemap_target
	{
		pass ibl_specular
		{
			input tmpCubemap
			output cubemap
		}
	}
}

compositor_node Tutorial_DynamicCubemapRenderingNode
{
	in 0 rt_renderwindow
	in 1 demo_dynamic_cubemap

	target rt_renderwindow
	{
		pass render_scene
		{
			load
			{
				all				clear
				clear_colour	0 0 0 1
			}
			store
			{
				depth			dont_care
				stencil			dont_care
			}

			//Our materials in this pass will be using this cubemap,
			//so we need to expose it to the pass.
			//Note: Even if it "just works" without exposing, the reason for
			//exposing is to ensure compatibility with Vulkan & D3D12.
			expose demo_dynamic_cubemap

			shadows		Tutorial_DynamicCubemapShadowNode
			overlays	off
			rq_first	0
			rq_last		200
		}

		//Render sky (see TutorialSky_Postprocess)
		pass render_quad
		{
			store
			{
				depth			dont_care
				stencil			dont_care
			}
			quad_normals	camera_direction
			material SkyPostprocess
		}

		//Render transparent stuff after sky
		pass render_scene
		{
			store
			{
				depth			dont_care
				stencil			dont_care
			}
			overlays	on
			rq_first	200
		}
	}
}

abstract target cubemap_target_shadow
{
	pass render_scene
	{
		load
		{
			all									clear
			clear_colour_reverse_depth_aware	1 1 1 1
		}
		store
		{
			//We only care about the contents of the colour target with point shadows
			depth			dont_care
			stencil			dont_care
		}
		camera_cubemap_reorient true
	}
}

compositor_node_shadow Tutorial_DynamicCubemapShadowNode
{
	technique pssm

	texture atlas		2048 7168 PFG_D32_FLOAT
	texture tmpCubemap	1024 1024 PFG_R32_FLOAT cubemap depth_format PFG_D32_FLOAT

	num_splits		3
	pssm_lambda		0.95
	shadow_map 0 atlas uv 0.0 0.000000000000000 1.0 0.285714285714286 light 0 split 0
	shadow_map 1 atlas uv 0.0 0.285714285714286 0.5 0.142857142857143 light 0 split 1
	shadow_map 2 atlas uv 0.5 0.285714285714286 0.5 0.142857142857143 light 0 split 2

	technique focused
	shadow_map 3 atlas uv 0.0 0.428571428571429 1.0 0.285714285714286 light 1
	shadow_map 4 atlas uv 0.0 0.714285714285714 1.0 0.285714285714286 light 2

	target atlas
	{
		pass clear
		{
			colour_value 1 1 1 1
		}
	}

	shadow_map_target_type directional
	{
		shadow_map 0 1 2
		{
			pass render_scene
			{
			}
		}
	}

	shadow_map_target_type directional spot
	{
		shadow_map 3 4
		{
			pass render_scene
			{
			}
		}
	}

	shadow_map_target_type point
	{
		shadow_map_repeat 3 4
		{
			target tmpCubemap +X : cubemap_target_shadow {}
			target tmpCubemap -X : cubemap_target_shadow {}
			target tmpCubemap +Y : cubemap_target_shadow {}
			target tmpCubemap -Y : cubemap_target_shadow {}
			target tmpCubemap +Z : cubemap_target_shadow {}
			target tmpCubemap -Z : cubemap_target_shadow {}

			shadow_map
			{
				pass render_quad
				{
					material Ogre/DPSM/CubeToDpsm
					input 0 tmpCubemap
				}
			}
		}
	}
}

workspace Tutorial_DynamicCubemapWorkspace
{
	//Render Window on channel #0
	connect_external 0 Tutorial_DynamicCubemapRenderingNode 0
	//Cubemap on channel #1
	connect_external 1 Tutorial_DynamicCubemapRenderingNode 1
}
