Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new material from the specified descriptor.
        /// </summary>
        /// <param name="device"></param>
        /// <param name="descriptor">The material descriptor.</param>
        /// <returns>An instance of a <see cref="Material"/>.</returns>
        /// <exception cref="System.ArgumentNullException">descriptor</exception>
        /// <exception cref="System.InvalidOperationException">If an error occurs with the material description</exception>
        public static Material New(GraphicsDevice device, MaterialDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            var context = new MaterialGeneratorContext(new Material());
            var result  = MaterialGenerator.Generate(descriptor, context, string.Format("{0}:RuntimeMaterial", descriptor.MaterialId));

            if (result.HasErrors)
            {
                throw new InvalidOperationException(string.Format("Error when creating the material [{0}]", result.ToText()));
            }

            var material   = result.Material;
            var blendState = material.Parameters.Get(Graphics.Effect.BlendStateKey);

            if (blendState != null && blendState.GraphicsDevice == null)
            {
                var newState = BlendState.New(device, blendState.Description);
                material.Parameters.Set(Effect.BlendStateKey, newState);
            }
            // TODO: Add other states?

            return(material);
        }
        public override void SetupResources(EffectMesh effectMesh)
        {
            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.SetDefaults();
            blendStateDesc.AlphaToCoverageEnable        = false;
            blendStateDesc.IndependentBlendEnable       = false;
            blendStateDesc.RenderTargets[0].BlendEnable = true;

            blendStateDesc.RenderTargets[0].AlphaBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].AlphaSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].AlphaDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].ColorSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].ColorDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorWriteChannels = ColorWriteChannels.All;

            // "LightPrePassAdditiveBlend"
            Effect.Parameters.Set(BlendStateKey, BlendState.New(GraphicsDevice, blendStateDesc));

            if (Debug)
            {
                var rasterizer = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription()
                {
                    FillMode = FillMode.Wireframe
                });
                rasterizer.Name = "LightPrePassWireFrame";
                Effect.Parameters.Set(RasterizerStateKey, rasterizer);
            }

            Effect.PrepareMesh    += SetupMeshResources;
            Effect.UpdateMeshData += UpdateMeshResources;
        }
Ejemplo n.º 3
0
        protected override void Initialize()
        {
            base.Initialize();



            Window.Title = "Kaibo Crawler";

            // Setup states
            var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default();

            m_backfaceCullingState = RasterizerState.New(GraphicsDevice, "CullModeBack", rasterizerStateDesc);

            var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();

            m_depthStencilStateState = DepthStencilState.New(GraphicsDevice, "NormalZBufferUse", depthStencilStateDesc);

            depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();
            depthStencilStateDesc.IsDepthEnabled = false;


            m_noDepthStencil = DepthStencilState.New(GraphicsDevice, "NoZBufferUse", depthStencilStateDesc);


            var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default();

            samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            samplerStateDesc.Filter   = SharpDX.Direct3D11.Filter.MinMagMipPoint;
            m_linearSamplerState      = SamplerState.New(GraphicsDevice, "LinearSampler", samplerStateDesc);


            var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default();

            m_blendStateOpaque = BlendState.New(GraphicsDevice, "Opaque", blendStateDesc);


            var blendStateDesc1 = SharpDX.Direct3D11.BlendStateDescription.Default();

            blendStateDesc1.IndependentBlendEnable = false;
            blendStateDesc1.AlphaToCoverageEnable  = false;


            m_alphaBlendState = BlendState.New(GraphicsDevice, SharpDX.Direct3D11.BlendOption.SourceAlpha,     //sourceBlend
                                               SharpDX.Direct3D11.BlendOption.InverseSourceAlpha,              //destinationBlend
                                               SharpDX.Direct3D11.BlendOperation.Add,                          //blendoperation
                                               SharpDX.Direct3D11.BlendOption.SourceAlpha,                     //source alphaBlend
                                               SharpDX.Direct3D11.BlendOption.InverseSourceAlpha,              //destination alpha blend
                                               SharpDX.Direct3D11.BlendOperation.Add,                          //alphablend operation
                                               SharpDX.Direct3D11.ColorWriteMaskFlags.All,                     //rendertarget mask
                                               -1);                                                            //mask



            Input.init(this);
        }
Ejemplo n.º 4
0
        private void InitializeIconsRenderer()
        {
            var graphicsDevice = graphicsDeviceService.GraphicsDevice;

            var lightIconEffectBuilder = this.effectSystemOld.BuildEffect("LightIcon")
                                         .Using(new BasicShaderPlugin(
                                                    new ShaderMixinSource
            {
                Mixins = new List <ShaderClassSource>()
                {
                    "ShaderBase",
                    "TransformationZero",
                    "TransformationWVP",
                    "EditorIcon",
                },
                Compositions = new Dictionary <string, ShaderSource>()
                {
                    { "color", new ShaderClassSource("ComputeColorTexture", TexturingKeys.Texture0.Name, "TEXCOORD") }
                }
            })
            {
                RenderPassPlugin = renderTargetsPlugin
            })
                                         .Using(new StateShaderPlugin {
                UseBlendState = true, UseDepthStencilState = true, RenderPassPlugin = renderTargetsPlugin
            });

            lightIconEffectBuilder.PickingPassMainPlugin = renderTargetsPlugin;
            lightIconEffect = lightIconEffectBuilder;

            lightIconEffect.Parameters.Set(EffectPlugin.BlendStateKey, BlendState.New(graphicsDevice, new BlendStateDescription(Blend.SourceAlpha, Blend.InverseSourceAlpha)));
            lightIconEffect.Parameters.Set(EffectPlugin.DepthStencilStateKey, graphicsDevice.DepthStencilStates.Default);

            throw new NotImplementedException();
            //lightIconEffect.Passes[0].UpdatePasses += (RenderPass currentRenderPass, ref FastList<RenderPass> currentPasses) =>
            //    {
            //        if (currentRenderPass.Passes != null)
            //        {
            //            var meshComparer = new MeshComparer(renderTargetsPlugin.Parameters);
            //            currentRenderPass.Passes.Sort(meshComparer);
            //        }
            //    };

            // Generates a quad for post effect rendering (should be utility function)
            var vertices = new[]
            {
                0.0f, 1.0f,
                1.0f, 1.0f,
                0.0f, 0.0f,
                1.0f, 0.0f,
            };

            lightBulbTexture = (Texture2D)assetManager.Load <Texture>("/global_data/editor/light_bulb.png");
        }
Ejemplo n.º 5
0
        public override void Load()
        {
            var desc = new BlendStateDescription(Blend.One, Blend.SourceAlpha);

            desc.RenderTargets[0].AlphaSourceBlend      = Blend.Zero;
            desc.RenderTargets[0].AlphaDestinationBlend = Blend.One;
            blendState = BlendState.New(GraphicsDevice, desc);

            effect          = EffectSystem.LoadEffect("AtmosphereLighting");
            Pass.StartPass += Render;
        }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        public override void Load()
        {
            base.Load();

            // Create necessary objects
            if (IBLRenderTarget == null)
            {
                IBLRenderTarget = Texture.New2D(GraphicsDevice, readOnlyDepthBuffer.Width, readOnlyDepthBuffer.Height, PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
            }

            cubemapMesh = GeometricPrimitive.Cube.New(GraphicsDevice);

            var blendStateDescr = new BlendStateDescription()
            {
                RenderTargets = new[]
                {
                    new BlendStateRenderTargetDescription()
                    {
                        BlendEnable           = true,
                        ColorSourceBlend      = Blend.SourceAlpha,
                        ColorDestinationBlend = Blend.One,
                        ColorBlendFunction    = BlendFunction.Add,
                        AlphaSourceBlend      = Blend.One,
                        AlphaDestinationBlend = Blend.One,
                        AlphaBlendFunction    = BlendFunction.Add,
                        ColorWriteChannels    = ColorWriteChannels.All
                    }
                }
            };

            IBLBlendState = BlendState.New(GraphicsDevice, blendStateDescr);

            // depth state to test z-fail of backfaces
            IBLDepthStencilState = DepthStencilState.New(GraphicsDevice, new DepthStencilStateDescription(true, false)
            {
                StencilEnable       = false,
                DepthBufferFunction = CompareFunction.GreaterEqual,
            });

            // effect
            IBLEffect = EffectSystem.LoadEffect(specularEffectName);

            parameters = new ParameterCollection();
            parameters.Set(RenderTargetKeys.DepthStencilSource, readOnlyDepthBuffer);
        }
Ejemplo n.º 7
0
        public AtmosphereBuilder(GraphicsDevice device, EffectSystem effectSystem)
        {
            blendState = BlendState.New(device, new BlendStateDescription(Blend.One, Blend.One)).DisposeBy(this);

            Data = new AtmosphereData(device, new AtmosphereSettings());

            // TODO: Use max precision temporary textures

            /*
             *          var intermediateTransmittanceDesc = Data.Transmittance.Description;
             *          intermediateTransmittanceDesc.Format = PixelFormat.R32G32B32A32_Float;
             *          var intermediateIrradianceDesc = Data.Irradiance.Description;
             *          intermediateIrradianceDesc.Format = PixelFormat.R32G32B32A32_Float;
             *          var intermediateInscatterDesc = Data.Inscatter.Description;
             *          intermediateInscatterDesc.Format = PixelFormat.R32G32B32A32_Float;
             *
             *          var intermediateTransmittance = Texture2D.New(device, intermediateTransmittanceDesc).DisposeBy(this);
             *          var intermediateIrradiance = Texture3D.New(device, intermediateIrradianceDesc).DisposeBy(this);
             *          var intermediateInscatter = Texture3D.New(device, intermediateInscatterDesc).DisposeBy(this);
             */

            deltaE  = Texture.New(device, Data.Irradiance.Description).DisposeBy(this);
            deltaSM = Texture.New(device, Data.Inscatter.Description).DisposeBy(this);
            deltaSR = Texture.New(device, Data.Inscatter.Description).DisposeBy(this);
            deltaJ  = Texture.New(device, Data.Inscatter.Description).DisposeBy(this);

            computeTransmittance      = effectSystem.LoadEffect("ComputeTransmittance");
            computeSingleIrradiance   = effectSystem.LoadEffect("SingleIrradiance");
            copySingleInscatter       = effectSystem.LoadEffect("CopySingleInscatter");
            computeSingleInscatter    = effectSystem.LoadEffect("SingleInscatter");
            computeOutscatter         = effectSystem.LoadEffect("Outscatter");
            computeMultipleIrradiance = effectSystem.LoadEffect("MultipleIrradiance");
            computeMultipleInscatter  = effectSystem.LoadEffect("MultipleInscatter");
            copyMultipleInscatter     = effectSystem.LoadEffect("CopyMultipleInscatter");
            copySlice = effectSystem.LoadEffect("CopySlice");

            parameters.Set(AtmospherePrecomputationKeys.DeltaSR, deltaSR);
            parameters.Set(AtmospherePrecomputationKeys.DeltaSM, deltaSM);
            parameters.Set(AtmospherePrecomputationKeys.DeltaE, deltaE);
            parameters.Set(AtmospherePrecomputationKeys.DeltaJ, deltaJ);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Sets the blending state.
        /// </summary>
        private void SetUpBlendState()
        {
            var blendStateDesc = new BlendStateDescription
            {
                AlphaToCoverageEnable = true,
                IndependentBlendEnable = true
            };

            blendStateDesc.RenderTarget[0].IsBlendEnabled = true;
            blendStateDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend = BlendOption.DestinationAlpha;
            blendStateDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;

            blendStateDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.DestinationAlpha;
            blendStateDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.ReverseSubtract;

            BlendState blendState = BlendState.New(this.GraphicsDevice, blendStateDesc);
            this.GraphicsDevice.SetBlendState(blendState);
        }
Ejemplo n.º 9
0
        public override void SetupResources(EffectMesh effectMesh)
        {
            base.SetupResources(effectMesh);

            Effect.Parameters.RegisterParameter(BlendStateKey);
            Effect.Parameters.RegisterParameter(RasterizerStateKey);

            // Create blendstate for min calculation
            var bbBlendDesc = new BlendStateDescription();

            bbBlendDesc.SetDefaults();
            bbBlendDesc.AlphaToCoverageEnable                  = false;
            bbBlendDesc.IndependentBlendEnable                 = false;
            bbBlendDesc.RenderTargets[0].BlendEnable           = true;
            bbBlendDesc.RenderTargets[0].ColorSourceBlend      = Blend.One;
            bbBlendDesc.RenderTargets[0].ColorDestinationBlend = Blend.One;

            bbBlendDesc.RenderTargets[0].ColorBlendFunction = BlendFunction.Max;
            bbBlendDesc.RenderTargets[0].ColorWriteChannels = ColorWriteChannels.Red;
            var blendMinState = BlendState.New(GraphicsDevice, bbBlendDesc);

            blendMinState.Name = "MinBlend";

            // Create blendstate for max calculation
            bbBlendDesc.RenderTargets[0].ColorBlendFunction = BlendFunction.Max;
            bbBlendDesc.RenderTargets[0].ColorWriteChannels = ColorWriteChannels.Green;
            var blendMaxState = BlendState.New(GraphicsDevice, bbBlendDesc);

            blendMaxState.Name = "MaxBlend";

            maxPass.Parameters.Set(RasterizerStateKey, GraphicsDevice.RasterizerStates.CullFront);
            maxPass.Parameters.Set(BlendStateKey, blendMaxState);

            minPass.Parameters.Set(RasterizerStateKey, GraphicsDevice.RasterizerStates.CullBack);
            minPass.Parameters.Set(BlendStateKey, blendMinState);
        }
Ejemplo n.º 10
0
        public override void Initialize()
        {
            base.Initialize();

            boundingBoxPass   = new RenderPass("BoundingBoxPass");
            minMaxPass        = new RenderPass("MinmaxPass");
            lightShaftPass    = new RenderPass("LightShaftPass");
            filterUpscalePass = new RenderPass("UpscalePass");
            RenderPass.AddPass(boundingBoxPass, minMaxPass, lightShaftPass, filterUpscalePass);

            var useUpScaling = false;

            var bbRenderTargetPlugin = new RenderTargetsPlugin("BoundingBoxRenderTargetPlugin")
            {
                EnableSetTargets  = true,
                EnableClearTarget = true,
                RenderPass        = boundingBoxPass,
                Services          = Services,
            };

            var minMaxEffectBuilder = this.EffectSystemOld.BuildEffect("MinMax")
                                      .Using(new MinMaxShaderPlugin("MinMaxShaderPlugin")
            {
                RenderPassPlugin = bbRenderTargetPlugin
            })
                                      .Using(new BasicShaderPlugin("TransformationWVP")
            {
                RenderPassPlugin = bbRenderTargetPlugin
            });


            var minmaxEffectBuilder = this.EffectSystemOld.BuildEffect("LightShaftsMinMax")
                                      .Using(new PostEffectSeparateShaderPlugin()
            {
                RenderPass = minMaxPass
            })
                                      .Using(new BasicShaderPlugin("ForwardShadowMapBase")
            {
                RenderPass = minMaxPass
            })
                                      .Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectMinMax", "ShadowMapUtils.shadowMapTexture", "PointSampler", 4, 4, 0.0, 1.0))
            {
                RenderPass = minMaxPass
            });

            var lightShaftsEffectBuilder = this.EffectSystemOld.BuildEffect("LightShafts")
                                           .Using(new PostEffectSeparateShaderPlugin()
            {
                RenderPass = lightShaftPass
            })
                                           .Using(new StateShaderPlugin()
            {
                UseBlendState = !useUpScaling, RenderPass = lightShaftPass
            })
                                           //.Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectLightShafts", Debug ? 1 : 0, RenderContext.IsZReverse ? 1 : 0, StepCount)) { RenderPass = lightShaftPass });
                                           .Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectLightShafts", Debug ? 1 : 0, false ? 1 : 0, StepCount))
            {
                RenderPass = lightShaftPass
            });

            if (OfflineCompilation)
            {
                minMaxEffectBuilder.InstantiatePermutation();
                minmaxEffectBuilder.InstantiatePermutation();
                lightShaftsEffectBuilder.InstantiatePermutation();
                return;
            }

            Parameters.AddSources(ViewParameters);
            Parameters.Set(RenderTargetKeys.DepthStencilSource, DepthStencil.Texture);
            Parameters.Set(TexturingKeys.Sampler, GraphicsDevice.SamplerStates.PointClamp);

            // BoundingBox prepass
            var gbufferDesc    = RenderTarget.Description;
            var bbRenderTarget = Texture2D.New(GraphicsDevice, gbufferDesc.Width / 8, gbufferDesc.Height / 8, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget);

            // Use MinMax Plugin
            bbRenderTargetPlugin.RenderTarget = bbRenderTarget.ToRenderTarget();
            bbRenderTargetPlugin.Parameters.AddSources(Parameters);
            bbRenderTargetPlugin.Apply();

            EffectOld minMaxEffect = minMaxEffectBuilder.InstantiatePermutation();

            // Add meshes
            foreach (var bbMeshData in BoundingBoxes)
            {
                // Mesh for MinPass
                var bbMesh = new EffectMesh(minMaxEffect, bbMeshData).KeepAliveBy(this);
                // Add mesh
                // boundingBoxPass.AddPass(bbMesh.EffectMeshPasses[0].EffectPass);
                RenderSystem.GlobalMeshes.AddMesh(bbMesh);
            }

            // MinMax render target
            var minMaxRenderTarget = Texture2D.New(GraphicsDevice, 256, 256, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget);

            minMaxPass.Parameters = new ParameterCollection(null);
            minMaxPass.Parameters.AddSources(ShadowMap.Parameters);
            minMaxPass.Parameters.AddSources(Parameters);
            minMaxPass.Parameters.AddDynamic(PostEffectMinMaxKeys.MinMaxCoords, ParameterDynamicValue.New(LightingPlugin.CascadeTextureCoords, (ref Vector4[] cascadeTextureCoords, ref Vector4 output) =>
            {
                output = cascadeTextureCoords[0];
            }, autoCheckDependencies: false));

            EffectOld minmaxEffect = minmaxEffectBuilder.InstantiatePermutation();

            var minMaxMesh = new EffectMesh(minmaxEffect).KeepAliveBy(this);

            minMaxMesh.Parameters.Set(RenderTargetKeys.RenderTarget, minMaxRenderTarget.ToRenderTarget());
            RenderSystem.GlobalMeshes.AddMesh(minMaxMesh);

            // Light Shafts effect
            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.SetDefaults();
            blendStateDesc.AlphaToCoverageEnable        = false;
            blendStateDesc.IndependentBlendEnable       = false;
            blendStateDesc.RenderTargets[0].BlendEnable = true;

            blendStateDesc.RenderTargets[0].AlphaBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].AlphaSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].AlphaDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].ColorSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].ColorDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorWriteChannels = ColorWriteChannels.All;

            var additiveBlending = BlendState.New(GraphicsDevice, blendStateDesc);

            additiveBlending.Name = "LightShaftAdditiveBlend";

            var shaftRenderTarget = useUpScaling ? Texture2D.New(GraphicsDevice, gbufferDesc.Width / 2, gbufferDesc.Height / 2, PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget).ToRenderTarget() : RenderTarget;


            lightShaftPass.Parameters = new ParameterCollection();
            lightShaftPass.Parameters.AddSources(ShadowMap.Parameters);
            lightShaftPass.Parameters.AddSources(Parameters);

            this.lightShaftsEffect = lightShaftsEffectBuilder.InstantiatePermutation();


            var mesh = new EffectMesh(lightShaftsEffect).KeepAliveBy(this);

            mesh.Parameters.Set(TexturingKeys.Texture0, minMaxRenderTarget);
            mesh.Parameters.Set(TexturingKeys.Texture1, bbRenderTarget);
            mesh.Parameters.Set(RenderTargetKeys.RenderTarget, shaftRenderTarget);

            if (!useUpScaling)
            {
                mesh.Parameters.Set(EffectPlugin.BlendStateKey, additiveBlending);
            }
            RenderSystem.GlobalMeshes.AddMesh(mesh);

            // Bilateral Gaussian filtering for up-sampling
            if (useUpScaling)
            {
                var bbRenderTargetUpScaleH = Texture2D.New(GraphicsDevice, gbufferDesc.Width, gbufferDesc.Height / 2, PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
                var bbRenderTargetUpScaleV = RenderTarget;
                //var bbRenderTargetUpScaleV = GraphicsDevice.RenderTarget2D.New(gbufferDesc.Width, gbufferDesc.Height, PixelFormat.HalfVector4);

                var blurEffects = new EffectOld[] {
                    this.EffectSystemOld.BuildEffect("BilateralGaussianFiltering")
                    .Using(new PostEffectSeparateShaderPlugin())
                    .Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectBilateralGaussian", 0))),
                    this.EffectSystemOld.BuildEffect("BilateralGaussianFiltering")
                    .Using(new StateShaderPlugin()
                    {
                        UseBlendState = true
                    })
                    .Using(new PostEffectSeparateShaderPlugin())
                    .Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectBilateralGaussian", 1))),
                };

                Texture2D    textureSourceH = (Texture2D)shaftRenderTarget.Texture;
                Texture2D    textureSourceV = bbRenderTargetUpScaleH;
                RenderTarget renderTargetH  = bbRenderTargetUpScaleH.ToRenderTarget();
                RenderTarget renderTargetV  = bbRenderTargetUpScaleV;

                var blurQuadMesh = new EffectMesh[2];
                for (int i = 0; i < 2; ++i)
                {
                    blurQuadMesh[i] = new EffectMesh(blurEffects[i]).KeepAliveBy(this);
                    filterUpscalePass.AddPass(blurQuadMesh[i].EffectPass);
                    RenderSystem.GlobalMeshes.AddMesh(blurQuadMesh[i]);
                }

                blurQuadMesh[0].Parameters.Set(TexturingKeys.Texture0, textureSourceH);
                blurQuadMesh[1].Parameters.Set(TexturingKeys.Texture0, textureSourceV);
                blurQuadMesh[0].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetH);
                blurQuadMesh[1].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetV);

                // Additive blending for 2nd render target
                blurQuadMesh[1].Parameters.Set(EffectPlugin.BlendStateKey, additiveBlending);
            }
        }
Ejemplo n.º 11
0
        public VoxelRenderer(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            _cubeVertexBuffer = Buffer.Vertex.New(
                graphicsDevice,
                new[]
            {
                // 3D coordinates              UV Texture coordinates
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                     // Front
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                    // BACK
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                    // Top
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                     // Bottom
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                     // Left
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                    // Right
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                }
            }, SharpDX.Direct3D11.ResourceUsage.Immutable);

            // Create an input layout from the vertices
            _vertexInputLayout = VertexInputLayout.New(
                VertexBufferLayout.New(0, new VertexElement[] { new VertexElement("POSITION_CUBE", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0),
                                                                new VertexElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, sizeof(float) * 3),
                                                                new VertexElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, sizeof(float) * 6) }, 0),
                VertexBufferLayout.New(1, new VertexElement[] { new VertexElement("POSITION_INSTANCE", SharpDX.DXGI.Format.R32_SInt) }, 1));

            // Create instance buffer for every VoxelInfo
            _voxelTypeRenderingData = new VoxelTypeInstanceData[TypeInformation.GetNumTypes() - 1];
            for (int i = 0; i < _voxelTypeRenderingData.Length; ++i)
            {
                _voxelTypeRenderingData[i] = new VoxelTypeInstanceData(graphicsDevice, (VoxelType)(i + 1));
            }
            LoadTextures(contentManager);


            // load shader
            EffectCompilerFlags compilerFlags = EffectCompilerFlags.None;

#if DEBUG
            compilerFlags |= EffectCompilerFlags.Debug;
#endif
            var voxelShaderCompileResult = EffectCompiler.CompileFromFile("Content/voxel.fx", compilerFlags);
            if (voxelShaderCompileResult.HasErrors)
            {
                System.Console.WriteLine(voxelShaderCompileResult.Logger.Messages);
                System.Diagnostics.Debugger.Break();
            }
            _voxelEffect = new SharpDX.Toolkit.Graphics.Effect(graphicsDevice, voxelShaderCompileResult.EffectData);

            // setup states
            var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default();
            rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.Back;
            _backfaceCullingState        = RasterizerState.New(graphicsDevice, "CullModeBack", rasterizerStateDesc);
            rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None;
            _noneCullingState            = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc);

            var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();
            depthStencilStateDesc.IsDepthEnabled = true;
            _depthStencilStateState = DepthStencilState.New(graphicsDevice, "NormalZBufferUse", depthStencilStateDesc);

            var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default();
            samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Mirror;
            samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Mirror;
            samplerStateDesc.Filter   = SharpDX.Direct3D11.Filter.MinMagMipPoint;
            _pointSamplerState        = SamplerState.New(graphicsDevice, "PointSampler", samplerStateDesc);
            _voxelEffect.Parameters["PointSampler"].SetResource(_pointSamplerState);

            var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default();
            _blendStateOpaque = BlendState.New(graphicsDevice, "Opaque", blendStateDesc);
            blendStateDesc.RenderTarget[0].IsBlendEnabled   = true;
            blendStateDesc.RenderTarget[0].SourceBlend      = SharpDX.Direct3D11.BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha;
            blendStateDesc.RenderTarget[0].BlendOperation   = SharpDX.Direct3D11.BlendOperation.Add;
            _blendStateTransparent = BlendState.New(graphicsDevice, "AlphaBlend", blendStateDesc);

            // vertexbuffer for a single instance
            _singleInstanceBuffer = Buffer.Vertex.New <Int32>(graphicsDevice, 1, SharpDX.Direct3D11.ResourceUsage.Dynamic);
        }