Example #1
0
File: Game.cs Project: sq/FNATest
        protected override void LoadContent()
        {
            base.LoadContent();

            Rt = new RenderTarget2D(Graphics.GraphicsDevice, 512, 512);

            var provider = new EmbeddedFreeTypeFontProvider(RenderCoordinator);

            Font             = provider.Load("FiraSans-Medium");
            Font.SizePoints  = 20;
            Font.GlyphMargin = 4;

            Texture = Texture2D.FromStream(Graphics.GraphicsDevice, Assembly.GetExecutingAssembly().GetManifestResourceStream("bunny"));

            try {
                Materials = new DefaultMaterialSet(RenderCoordinator)
                {
                    ViewTransform = ViewTransform.CreateOrthographic(Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight)
                };
            } catch (Exception exc) {
                Console.WriteLine("Shader load failed.");
                Console.WriteLine(exc);
                Thread.Sleep(5000);
                Environment.Exit(1);
                return;
            }

            EffectProvider = new EmbeddedEffectProvider(RenderCoordinator);
            Vpos           = new Material(EffectProvider.Load("vpos"), "vposShader");
            Materials.Add(Vpos);
        }
        public LightingRenderer(ContentManager content, RenderCoordinator coordinator, DefaultMaterialSet materials, LightingEnvironment environment)
        {
            Materials   = materials;
            Coordinator = coordinator;

            IlluminantMaterials = new IlluminantMaterials(materials);

            StoreScissorRect       = _StoreScissorRect;
            RestoreScissorRect     = _RestoreScissorRect;
            ShadowBatchSetup       = _ShadowBatchSetup;
            IlluminationBatchSetup = _IlluminationBatchSetup;

            IlluminantMaterials.ClearStencil = materials.Get(
                materials.Clear,
                rasterizerState: RasterizerState.CullNone,
                depthStencilState: new DepthStencilState {
                StencilEnable    = true,
                StencilMask      = StencilTrue,
                StencilWriteMask = StencilTrue,
                ReferenceStencil = StencilFalse,
                StencilFunction  = CompareFunction.Always,
                StencilPass      = StencilOperation.Replace,
                StencilFail      = StencilOperation.Replace,
            },
                blendState: BlendState.Opaque
                );

            materials.Add(
                IlluminantMaterials.DebugOutlines = materials.WorldSpaceGeometry.SetStates(
                    blendState: BlendState.AlphaBlend
                    )
                );

            // If stencil == false, paint point light at this location
            PointLightStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilFalse,
                StencilFunction   = CompareFunction.Equal,
                StencilPass       = StencilOperation.Keep,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilFalse
            };

            {
                var dBegin = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RenderStates.ScissorOnly, depthStencilState: PointLightStencil
                        )
                };
                var dEnd = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                        )
                };

#if SDL2
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponential"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinear"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponentialRampTexture"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinearRampTexture"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#else
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#endif
            }

            // If stencil == false: set stencil to true.
            // If stencil == true: leave stencil alone, don't paint this pixel
            ShadowStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilTrue,
                StencilFunction   = CompareFunction.NotEqual,
                StencilPass       = StencilOperation.Replace,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilTrue
            };

            materials.Add(IlluminantMaterials.Shadow = new DelegateMaterial(
                              ShadowMaterialInner    = new Squared.Render.EffectMaterial(
#if SDL2
                                  content.Load <Effect>("Shadow"), "Shadow"
#else
                                  content.Load <Effect>("Illumination"), "Shadow"
#endif
                                  ),
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RenderStates.ScissorOnly,
                    depthStencilState: ShadowStencil,
                    blendState: RenderStates.DrawNone
                    )
            },
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                    )
            }
                              ));

#if SDL2
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceGammaCompressedBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceGammaCompressedBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceToneMappedBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceToneMappedBitmap"), "WorldSpaceToneMappedBitmap"
                              ));
#else
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "ScreenSpaceRampBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "WorldSpaceRampBitmap"
                              ));
#endif

            Environment = environment;

            // Reduce garbage created by BufferPool<>.Allocate when creating cached sectors
            BufferPool <ShadowVertex> .MaxBufferSize = 1024 * 16;
            BufferPool <short> .MaxBufferSize        = 1024 * 32;
        }