/// <summary> /// Handles input for quitting or changing settings. /// </summary> void HandleInput() { lastKeyboardState = currentKeyboardState; lastGamePadState = currentGamePadState; currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); // Check for exit. if (currentKeyboardState.IsKeyDown(Keys.Escape) || currentGamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } // Change the number of instances more quickly if there are // already lots of them. This avoids you having to sit there // for hours with your finger on the "increase" button! int instanceChangeRate = Math.Max(instances.Count / 100, 1); // Increase the number of instances? if (currentKeyboardState.IsKeyDown(Keys.X) || currentGamePadState.Buttons.X == ButtonState.Pressed) { for (int i = 0; i < instanceChangeRate; i++) { instances.Add(new SpinningInstance()); } } // Decrease the number of instances? if (currentKeyboardState.IsKeyDown(Keys.Y) || currentGamePadState.Buttons.Y == ButtonState.Pressed) { for (int i = 0; i < instanceChangeRate; i++) { if (instances.Count == 0) { break; } instances.RemoveAt(instances.Count - 1); } } // Change which instancing technique we are using? if ((currentKeyboardState.IsKeyDown(Keys.A) && lastKeyboardState.IsKeyUp(Keys.A)) || (currentGamePadState.Buttons.A == ButtonState.Pressed && lastGamePadState.Buttons.A == ButtonState.Released)) { instancingTechnique++; // Wrap if we reach the end of the possible techniques. if (instancingTechnique > InstancingTechnique.NoInstancingOrStateBatching) { instancingTechnique = 0; } } }
public bool IsTechniqueSupported( InstancingTechnique technique ) { if ( technique == InstancingTechnique.HardwareInstancing ) { GraphicsDeviceCapabilities caps = graphicsDevice.GraphicsDeviceCapabilities; return caps.PixelShaderVersion.Major >= 3; } return true; }
/// <summary> /// Draw a batch of instanced model geometry. /// </summary> public void Draw(InstancingTechnique technique, Matrix[] instanceTransforms, Matrix view, Matrix projection) { SetRenderStates(technique, view, projection); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); switch (technique) { case InstancingTechnique.HardwareInstancing: DrawHardwareInstancing(instanceTransforms); break; } pass.End(); } effect.End(); }
/// <summary> /// Helper function sets up the graphics device and /// effect ready for drawing instanced geometry. /// </summary> void SetRenderStates( InstancingTechnique instancingTechnique, Matrix view, Matrix projection, Vector3 eye) { // Set the graphics device to use our vertex data. graphicsDevice.VertexDeclaration = vertexDeclaration; graphicsDevice.Vertices[0].SetSource( vertexBuffer, 0, vertexStride ); graphicsDevice.Indices = indexBuffer; // Make sure our effect is set to use the right technique. if ( techniqueChanged ) { string techniqueName = instancingTechnique.ToString(); effect.CurrentTechnique = effect.Techniques[techniqueName]; techniqueChanged = false; } effectParameterView.SetValue( view ); effectParameterProjection.SetValue( projection ); effectParameterEye.SetValue( eye ); effectParameterVertexCount.SetValue( vertexCount ); }
/// <summary> /// Initializes a model part to use the specified instancing /// technique. This is called once at startup, and then again /// whenever the instancing technique is changed. /// </summary> internal void Initialize( InstancingTechnique instancingTechnique ) { techniqueChanged = true; }
/// <summary> /// Draws a batch of instanced model geometry, /// using the specified technique and camera matrices. /// </summary> public void Draw( InstancingTechnique instancingTechnique, Matrix[] instanceTransforms, int nTransforms, Matrix view, Matrix projection, Vector3 eye) { SetRenderStates( instancingTechnique, view, projection, eye ); // Begin the effect, then loop over all the effect passes. effect.Begin(); EffectPassCollection passes = effect.CurrentTechnique.Passes; int nPasses = passes.Count; for ( int i = 0; i < nPasses; ++i ) { EffectPass pass = passes[i]; pass.Begin(); DrawShaderInstancing( instanceTransforms, nTransforms ); pass.End(); } effect.End(); }
/// <summary> /// Changes which instancing technique we are using. /// </summary> public void SetInstancingTechnique( InstancingTechnique technique ) { instancingTechnique = technique; foreach ( InstancedModelPart modelPart in modelParts ) { modelPart.Initialize( technique ); } }
public bool SetInstancingTechnique( InstancingTechnique technique, VertexElement[] instanceVertexElements) { if ( !IsTechniqueSupported( technique ) ) return false; InstancingTechnique = technique; foreach ( InstancedModelPart modelPart in modelParts ) { modelPart.Initialize( technique, instanceVertexElements ); } return true; }
internal void Initialize( InstancingTechnique instancingTechnique, VertexElement[] instanceVertexElements) { switch ( instancingTechnique ) { case InstancingTechnique.ShaderInstancing: InitializeShaderInstancing( false ); Effect.CurrentTechnique = Effect.Techniques["ShaderInstancing"]; break; case InstancingTechnique.HardwareInstancing: InitializeHardwareInstancing( instanceVertexElements ); Effect.CurrentTechnique = Effect.Techniques["HardwareInstancing"]; break; } }
public RenderPipeline(GraphicsDevice dev, Camera c) { _device = dev; camera = c; _visualizationEffect = new BasicEffect(this._device) { VertexColorEnabled = true }; /// <summary> /// added /// </summary> sprite = new SpriteBatch(this._device); frustumForShadow = new BoundingFrustum(Matrix.Identity); debugRenderer = new DebugRender.DebugRenderer(_device, _visualizationEffect); debugRenderArray = new MyContainer<PivotObject>(10, 3); EnableShadows = Config.Instance["_enableShadows"]; SmoothShadows = Config.Instance["_smoothShadows"]; EnableDebugRender = Config.Instance["_enableDebugRender"]; int size = Config.Instance["_shadowMapSize"]; if (size != 0) shadowMapWidthHeight = size; instancingTechnique = Config.Instance["_useHardwareInstancing"] ? InstancingTechnique.HardwareInstancing : InstancingTechnique.NoInstancing; if (EnableShadows) { if (dev.GraphicsProfile == GraphicsProfile.HiDef) { shadowRenderTarget = new RenderTarget2D(_device, shadowMapWidthHeight, shadowMapWidthHeight, false, SurfaceFormat.Single, DepthFormat.Depth16); } else { if (!Config.Instance["_ultraLowRender"]) { shadowRenderTarget = new RenderTarget2D(_device, shadowMapWidthHeight, shadowMapWidthHeight, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.DiscardContents); } else { shadowRenderTarget = new RenderTarget2D(_device, dev.PresentationParameters.BackBufferWidth, dev.PresentationParameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None); } SmoothShadows = false; } } arrays = new List<string>(); {//основные arrays.Add(Shader.AnimRenderNoSM); arrays.Add(Shader.NotAnimRenderNoSM); arrays.Add(Shader.AnimRenderSM); arrays.Add(Shader.NotAnimRenderSM); } {//шмапа arrays.Add(Shader.CreateStaticShadowMap); arrays.Add(Shader.CreateAnimShadowMap); } {//прозрачные arrays.Add(Shader.TransparentSM); arrays.Add(Shader.TransparentSelfIlmnNoSM); arrays.Add(Shader.TransparentNoSM); } {//инстансные //инст с шм arrays.Add(Shader.InstancedNoSM); arrays.Add(Shader.InstancedSM); //инстанс прозр с шм arrays.Add(Shader.InstancedTransparentNoSM); arrays.Add(Shader.InstancedTransparentSM); //инстанс прозр самосвет arrays.Add(Shader.InstancedTransparentSelfIlmnNoSM); } //если хайдеф, то и сгладенная шмапа if (dev.GraphicsProfile == GraphicsProfile.HiDef) { arrays.Add(Shader.AnimRenderSMSmooth); arrays.Add(Shader.NotAnimRenderSMSmooth); arrays.Add(Shader.TransparentSMSmooth); arrays.Add(Shader.InstancedTransparentSMSmooth); arrays.Add(Shader.InstancedSMSmooth); } foreach (string s in arrays) ArraysPerTehnique.Add(s, new RenderArray(s)); }
void SetRenderStates(InstancingTechnique technique, Matrix view, Matrix projection) { device.VertexDeclaration = vertexDeclaration; device.Vertices[0].SetSource(vertexBuffer, 0, vertexStride); device.Indices = indexBuffer; if (techniqueChanged) { string techniqueName = technique.ToString(); effect.CurrentTechnique = effect.Techniques[techniqueName]; techniqueChanged = false; } effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); }
internal void SetInstancingTechnique(InstancingTechnique technique) { switch (technique) { case InstancingTechnique.ShaderInstancing: InitializeShaderInstancing(); break; case InstancingTechnique.HardwareInstancing: InitializeHardwareInstancing(); break; } techniqueChanged = true; }