//construct the displayer.
		//Because this class needs to load content, a ContentRegister is a required
		//constructor parameter. This makes sure it will always load content
		public ImageDisplayer(IContentRegister contentRegister)
		{
			if (contentRegister == null)
				throw new ArgumentNullException();

			//create the element that will display the texture
			var sizeInPixels = new Vector2(768,384);

			//create an instance of the shader
			//The texture will be assigned to the shader in LoadContent
			this.shader = new Shader.Tutorial09Technique();

			//create the element which will display the shader
			this.element = new ShaderElement(this.shader, sizeInPixels);

			//place the element in the centre of the screen
			this.element.HorizontalAlignment = HorizontalAlignment.Centre;
			this.element.VerticalAlignment = VerticalAlignment.Centre;

			//add this object to the content register
			//items added to a content register do not need to be removed, as they
			//are tracked by a weak reference.
			//LoadContent() will be called whenever the device is created/reset.
			contentRegister.Add(this);

			//At this point, since the device is being created, LoadContent() will have
			//been called already. If the device hadn't been created yet, then
			//LoadContent() would be called at a later time.

			//LoadContent() will always be called before the first time Draw() is called.
		}
		/// <summary>
		/// Construct the texture downsampler
		/// </summary>
		/// <param name="source">Source texture to read</param>
		/// <param name="target">Target texture to write to</param>
		/// <param name="intermediate">Intermediate texture (if null, will be created as required)</param>
		/// <param name="intermediate2">Second intermediate texture (if null, will be created as required)</param>
		/// <param name="targetWidth">target width to downsample to</param>
		/// <param name="targetHeight">target height to downsample to</param>
		/// <param name="shaderProvider">Optional provider for downsample shaders</param>
		public TextureDownsample(DrawTargetTexture2D source, DrawTargetTexture2D target, ref DrawTargetTexture2D intermediate, ref DrawTargetTexture2D intermediate2, int targetWidth, int targetHeight,
					IDownsampleShaderProvider shaderProvider)
		{
			if (source == null ||
				target == null)
				throw new ArgumentNullException();

			if (targetWidth <= 0 ||
				targetHeight <= 0)
				throw new ArgumentException("Invalid target size");

			if (DrawTarget.FormatChannels(source.SurfaceFormat) != DrawTarget.FormatChannels(target.SurfaceFormat))
			    throw new ArgumentException("source.SurfaceFormat has a different number of channels than target.SurfaceFormat");

			if (targetWidth > target.Width ||
				targetHeight > target.Height)
				throw new ArgumentException("Size is larger than target");

			if (targetWidth > source.Width ||
				targetHeight > source.Height)
				throw new ArgumentException("Size is larger than source");

			if (intermediate != null)
			{
				if (target.SurfaceFormat != intermediate.SurfaceFormat)
					throw new ArgumentException("target.SurfaceFormat != intermediate.SurfaceFormat");
				if (intermediate == intermediate2)
					throw new ArgumentException("intermediate == intermediate2");
			}
			if (intermediate2 != null)
			{
				if (target.SurfaceFormat != intermediate2.SurfaceFormat)
					throw new ArgumentException("target.SurfaceFormat != intermediate2.SurfaceFormat");
			}

			int w = source.Width;
			int h = source.Height;

			int targetMultipleWidth = targetWidth;
			int targetMultipleHeight = targetHeight;

			while (targetMultipleWidth * 2 <= w)
				targetMultipleWidth *= 2;
			while (targetMultipleHeight * 2 <= h)
				targetMultipleHeight *= 2;

			DrawTargetTexture2D current = null;
			Rectangle sRegion = new Rectangle(0,0,0,0);

			//first pass may require that the source is sized down to a multiple of the target size

			if ((double)targetWidth / (double)w <= 0.5 &&
				(double)targetHeight / (double)h <= 0.5 &&
				(targetMultipleWidth != w || targetMultipleHeight != h))
			{
				DrawTargetTexture2D go = this.PickRT(ref intermediate, ref intermediate2, source, targetMultipleWidth, targetMultipleHeight,target.SurfaceFormat);

				Vector2 size = new Vector2((float)targetMultipleWidth, (float)targetMultipleHeight);

				TexturedElement te = new TexturedElement(source, size, false);
				te.TextureCrop = new Rectangle(0,0,w,h);

				go.Add(te);
				passes.Add(go);
				current = go;
				w = targetMultipleWidth;
				h = targetMultipleHeight;
			}

			//downsample on the larger axis, either 2x, 4x or 8x downsampling, until reached the target size

			while (target.Equals(current) == false)
			{
				DrawTargetTexture2D localSource = current ?? source;

				double difW = (double)targetWidth / (double)w;
				double difH = (double)targetHeight / (double)h;

				sRegion.Width = w;
				sRegion.Height = h;
				sRegion.Y = localSource.Height - h;

				//both width/height difference are less than 50% smaller, so a linear interpolation will do fine
				if (difW > 0.5 &&
					difH > 0.5)
				{
					//write directly to the target
					DrawTargetTexture2D go = target.Clone(false, false, false);
					Vector2 te_size = new Vector2((float)targetWidth, (float)targetHeight);
					TexturedElement te = new TexturedElement(localSource, te_size, false);

					go.AddModifier(new ScissorModifier(0, go.Height - targetHeight, targetWidth, go.Height, go));
					te.TextureCrop = sRegion;

					go.Add(te);
					passes.Add(go);
					current = go;

					continue;
				}

				bool horizontal = difW < difH;
				double dif = Math.Min(difW, difH);
				int size = horizontal ? w : h;

				Vector2 dir = new Vector2(0,0);
				if (horizontal)
					dir.X = 1.0f / localSource.Width;
				else
					dir.Y = 1.0f / localSource.Height;

				if (dif > 0.25) // cutoff for using 2 samples
				{
					DrawTargetTexture2D go;
					int new_width = w;
					int new_height = h;
					if (horizontal)
						new_width /= 2;
					else
						new_height /= 2;

					if (new_width == targetWidth && new_height == targetHeight)
						go = target.Clone(false, false, false);
					else
						go = PickRT(ref intermediate, ref intermediate2, localSource, new_width, new_height, target.SurfaceFormat);

					Vector2 se_size = new Vector2((float)new_width, (float)new_height);
					ShaderElement se = new ShaderElement((shaderProvider ?? this).DownsampleShader2, se_size, false);

					go.AddModifier(new ScissorModifier(0, go.Height - new_height, new_width, go.Height, go));

					se.TextureCrop = sRegion;

					go.Add(new Drawer(dir,se,localSource));
					passes.Add(go);

					w = new_width;
					h = new_height;

					current = go;
					continue;
				}

				if (dif > 0.125) // cutoff for using 4 samples
				{
					DrawTargetTexture2D go;
					int new_width = w;
					int new_height = h;
					if (horizontal)
						new_width /= 4;
					else
						new_height /= 4;

					if (new_width == targetWidth && new_height == targetHeight)
						go = target.Clone(false, false, false);
					else
						go = PickRT(ref intermediate, ref intermediate2, localSource, new_width, new_height, target.SurfaceFormat);

					Vector2 se_size = new Vector2((float)new_width, (float)new_height);
					ShaderElement se = new ShaderElement((shaderProvider ?? this).DownsampleShader4, se_size, false);

					go.AddModifier(new ScissorModifier(0, go.Height - new_height, new_width, go.Height, go));

					se.TextureCrop = sRegion;

					go.Add(new Drawer(dir, se, localSource));
					passes.Add(go);

					w = new_width;
					h = new_height;

					current = go;
					continue;
				}

				// cutoff for using 8 samples
				{
					DrawTargetTexture2D go;
					int new_width = w;
					int new_height = h;
					if (horizontal)
						new_width /= 8;
					else
						new_height /= 8;
					
					if (new_width == targetWidth && new_height == targetHeight)
						go = target.Clone(false, false, false);
					else
						go = PickRT(ref intermediate, ref intermediate2, localSource, new_width, new_height, target.SurfaceFormat);

					Vector2 se_size = new Vector2((float)new_width, (float)new_height);
					ShaderElement se = new ShaderElement((shaderProvider ?? this).DownsampleShader8, se_size, false);

					go.AddModifier(new ScissorModifier(0, go.Height - new_height, new_width, go.Height, go));

					se.TextureCrop = sRegion;

					go.Add(new Drawer(dir, se, localSource));
					passes.Add(go);

					w = new_width;
					h = new_height;

					current = go;
					continue;
				}
			}
		}
		internal SinglePassTextureFilter(DrawTargetTexture2D source, DrawTargetTexture2D target)
		{
			if (source == null || target == null)
				throw new ArgumentNullException();
			if (source.SurfaceFormat != target.SurfaceFormat)
				throw new ArgumentException("source.SurfaceFormat != target.SurfaceFormat");
			if (source == target)
				throw new ArgumentException("source == target is invalid");
			if (source.Width > target.Width ||
				source.Height > target.Height)
				throw new ArgumentException("source is larger than target");

			this.source = source;
			this.targetClone = target.Clone(false,false,false);

			this.targetClone.ClearBuffer.Enabled = false;

			this.element = new ShaderElement(null, source.Width, source.Height,new Vector2(1,1),true);
			this.targetClone.Add(element);

			if (target.Width != source.Width ||
				target.Height != source.Height)
			{
				this.targetClone.AddModifier(new Xen.Graphics.Modifier.ScissorModifier(0, 0, Math.Min(1, (source.Width) / target.Width), Math.Min(1, (source.Height) / target.Height)));
				this.element.TextureCrop = new Rectangle(0, 0, Math.Min(this.source.Width, this.targetClone.Width), Math.Min(this.source.Height, this.targetClone.Height));
			}
		}
			public Drawer(Vector2 direction, ShaderElement drawable, DrawTargetTexture2D source)
			{
				drawable.SetTextureSize(source.Width, source.Height);
				this.drawable = drawable;
				this.source = source;
				this.direction = direction;
			}
		protected override void Initialise()
		{
			//setup the view camera first
			//--------------------------------------

			viewCamera = new Xen.Camera.FirstPersonControlledCamera3D(this.UpdateManager);
			viewCamera.Projection.FieldOfView *= 0.65f;
			viewCamera.MovementSensitivity *= 0.05f;
			viewCamera.LookAt(new Vector3(-3, 4, 2), new Vector3(6, 6, 2), new Vector3(0, 1, 0));
			viewCamera.Projection.NearClip = 0.1f;

			//shadow map setup:
			//--------------------------------------

			const float shadowArea = 4;
			const int shadowMapResolution = 1024;

			//setup the shadow map rendering camera
			shadowCamera = new Camera3D();

			//setup the shadow map projection to roughly cover the character
			shadowCamera.Projection.Orthographic = true;
			shadowCamera.Projection.NearClip = shadowArea * 2;
			shadowCamera.Projection.FarClip = -shadowArea * 2;
			shadowCamera.Projection.Region = new Vector4(1, -1.8f, -1, 0.2f) * shadowArea;

			//setup the shadow map draw target

			//create the shadow map
			shadowMap = new DrawTargetTexture2D(shadowCamera, shadowMapResolution, shadowMapResolution, SurfaceFormat.HalfSingle, DepthFormat.Depth24);
			shadowMap.ClearBuffer.ClearColour = Color.White;

			//setup the shadow map drawer..
			shadowDrawer = new Tutorial_25.ShadowMapDrawer(null, new Tutorial_25.ShadowOutputShaderProvider());
			this.shadowMap.Add(shadowDrawer);



			//create the main draw targets.
			//--------------------------------------

			drawToScreen = new DrawTargetScreen(new Camera2D());
			drawToScreen.ClearBuffer.ClearColourEnabled = false;

			drawToRenderTarget = new DrawTargetTexture2D(viewCamera, this.WindowWidth, this.WindowHeight, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, false, PreferredMultiSampleLevel.FourSamples, RenderTargetUsage.PlatformContents);
			drawToRenderTarget.ClearBuffer.ClearColourEnabled = false;

			//setup the bloom draw targets
			//--------------------------------------

			//scale to reduce the size of the bloom target, compared to main render target
			const int bloomDownsample = 8;	//eight times smaller

			bloomRenderTarget = new DrawTargetTexture2D(new Camera2D(), Math.Max(1, drawToRenderTarget.Width / bloomDownsample), Math.Max(1, drawToRenderTarget.Height / bloomDownsample), SurfaceFormat.Color, DepthFormat.None);
			bloomRenderTarget.ClearBuffer.ClearColourEnabled = false;

			bloomIntermediateRenderTarget = null;

			//the bloom intermediate target is not needed on the xbox, as the full bloom target fits in EDRAM
			bloomIntermediateRenderTarget = new DrawTargetTexture2D(viewCamera, bloomRenderTarget.Width, bloomRenderTarget.Height, SurfaceFormat.Color, DepthFormat.None);
			bloomIntermediateRenderTarget.ClearBuffer.ClearColourEnabled = false;

			//setup the blur filter, with a large 31 sample radius.
			bloomBlurPass = new Xen.Ex.Filters.BlurFilter(Xen.Ex.Filters.BlurFilterFormat.ThirtyOneSampleBlur_FilteredTextureFormat, 1.0f, bloomRenderTarget, bloomIntermediateRenderTarget);


			//setup the character model
			this.model = new ModelInstance();	//(the model is setup in LoadContent)
			this.modelRotation = new DrawRotated(model);
			this.modelRotation.RotationAngle = 3;

			//add the model to be drawn
			drawToRenderTarget.Add(modelRotation);

			//setup the shaders
			this.characterRenderShader = new Shaders.Character();

			//setup the output and bloom shaders
			outputShader = new Shaders.RgbmDecode();
			drawToScreen.Add(new ShaderElement(outputShader, new Vector2(1, 1), true));

			bloomPassShader = new Shaders.RgbmDecodeBloomPass();
			bloomRenderTarget.Add(new ShaderElement(bloomPassShader, new Vector2(1, 1), true));

			//add a background to be drawn
			drawToRenderTarget.Add(new BackgroundDrawer());


			//setup the debug image displays
			//--------------------------------------

			this.rgmbTextureAlphaShader = new Shaders.AlphaWrite();
			this.bloomTextureDisplay = new TexturedElement(this.bloomRenderTarget, new Vector2(0.2f, 0.2f), true);
			this.rgbmTextureDisplay = new TexturedElement(this.drawToRenderTarget, new Vector2(0.2f, 0.2f), true);
			this.rgbmTextureAlphaDisplay = new ShaderElement(this.rgmbTextureAlphaShader, new Vector2(0.2f, 0.2f), true);

			this.rgbmTextureAlphaDisplay.Position = new Vector2(0.7f, 0.2f);
			this.rgbmTextureDisplay.Position = new Vector2(0.7f, 0.4f);
			this.bloomTextureDisplay.Position = new Vector2(0.7f, 0.6f);

			this.drawToScreen.Add(this.rgbmTextureDisplay);
			this.drawToScreen.Add(this.rgbmTextureAlphaDisplay);
			this.drawToScreen.Add(this.bloomTextureDisplay);

			//setup the render config
			this.configEditor = new RenderConfigEditor(this.Content);

			this.drawToScreen.Add(configEditor);
			this.UpdateManager.Add(configEditor);


			//add a statistics overlay.
			drawStats = new Xen.Ex.Graphics2D.Statistics.DrawStatisticsDisplay(this.UpdateManager);
			drawToScreen.Add(drawStats);
			
		}