Ejemplo n.º 1
0
        public TestSprite(Device device, DVector3 Position)
        {
            this.Position = Position;
            MMesh = ContentManager.LoadMesh("Content/Models/BaseSprite.mesh");
            EEEM = ContentManager.LoadEffect("Content/Shaders/BaseSprite");
            TexCont = ContentManager.LoadTexture2D("Content/Textures/Particl");
            // Подготовка константного буффера
            BufferDescription bd = new BufferDescription();
            bd.SizeInBytes = Marshal.SizeOf(typeof(ShaderConstants));
            bd.Usage = ResourceUsage.Dynamic;
            bd.BindFlags = BindFlags.ConstantBuffer;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.OptionFlags = ResourceOptionFlags.None;
            bd.StructureByteStride = 0;

            constantsBuffer = new Buffer(device, bd);
            constants = new ShaderConstants();
        }
Ejemplo n.º 2
0
        public void Initialize()
        {
            LoadSky(0);

            Texture2DDescription descTex = new Texture2DDescription();
            descTex.ArraySize = 6;
            descTex.Width = Size;
            descTex.Height = Size;
            descTex.Usage = ResourceUsage.Default;
            descTex.CpuAccessFlags = CpuAccessFlags.None;
            descTex.Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm;
            descTex.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
            descTex.MipLevels = MipLevel;
            descTex.BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget;
            descTex.OptionFlags = ResourceOptionFlags.GenerateMipMaps | ResourceOptionFlags.TextureCube;

            //Create the texture and shader view
            CubeTexture = new Texture2D(ModelViewer.Program.device, descTex);
            CubeSRV = new ShaderResourceView(ModelViewer.Program.device, CubeTexture);

            RenderTargetViewDescription RTVD = new RenderTargetViewDescription();
            RTVD.Format = descTex.Format;
            RTVD.Dimension = RenderTargetViewDimension.Texture2DArray;
            RTVD.Texture2DArray.FirstArraySlice = 0;
            RTVD.Texture2DArray.ArraySize = 1;
            RTVD.Texture2DArray.MipSlice = 0;

            CubeRenderTarget = new RenderTargetView[6];

            for (int i = 0; i < 6; i++)
            {
                RTVD.Texture2DArray.FirstArraySlice = i;
                CubeRenderTarget[i] = new RenderTargetView(ModelViewer.Program.device, CubeTexture, RTVD);
            }

            SamplerStateDescription a = new SamplerStateDescription();
            a.AddressU = TextureAddressMode.Clamp;
            a.AddressV = TextureAddressMode.Clamp;
            a.AddressW = TextureAddressMode.Clamp;
            a.Filter = Filter.MinMagMipLinear;
            SSWrapMipLinear = new SamplerState(ModelViewer.Program.device, a);

            MContains = new MeshContainer();
            MContains.BytesPerVertex = 20;
            MContains.FaceCount = 12;
            MContains.VertexsCount = 24;

            Vector3 vExtents = new Vector3(500, 500, 500);

            var vertices = new DataStream(MContains.BytesPerVertex * MContains.VertexsCount, true, true);

            //Back
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Front
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Bottom
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            //Top
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            //Left
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(-vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 0));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(-vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            //Right
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 1));
            vertices.Write(new Vector3(vExtents.X, -vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 1));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, vExtents.Z));
            vertices.Write(new Vector2(1, 0));
            vertices.Write(new Vector3(vExtents.X, vExtents.Y, -vExtents.Z));
            vertices.Write(new Vector2(0, 0));

            vertices.Position = 0;
            InputElement[] elements11 = new[] {
                new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0) ,
                new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 12, 0)};

            MContains.Vertexs = new Buffer(ModelViewer.Program.device, vertices, MContains.BytesPerVertex * MContains.VertexsCount, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            var indices = new DataStream(4 * MContains.FaceCount * 3, true, true);
            for (int x = 0; x < 6; x++)
            {
                indices.Write((int)(x * 4 + 0));
                indices.Write((int)(x * 4 + 1));
                indices.Write((int)(x * 4 + 2));

                indices.Write((int)(x * 4 + 2));
                indices.Write((int)(x * 4 + 3));
                indices.Write((int)(x * 4 + 0));
            }
            indices.Position = 0;

            MContains.Indices = new Buffer(ModelViewer.Program.device, indices, 4 * MContains.FaceCount * 3, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            MContains.binding = new VertexBufferBinding(MContains.Vertexs, MContains.BytesPerVertex, 0);

            EEEM = ContentManager.LoadEffect("Content/Shaders/SkyBox", elements11);

            // Подготовка константного буффера
            BufferDescription bd = new BufferDescription();
            bd.SizeInBytes = Marshal.SizeOf(typeof(SkyShaderConstants));
            bd.Usage = ResourceUsage.Dynamic;
            bd.BindFlags = BindFlags.ConstantBuffer;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.OptionFlags = ResourceOptionFlags.None;
            bd.StructureByteStride = 0;

            SkyConstantsBuffer = new Buffer(ModelViewer.Program.device, bd);
            SSC = new SkyShaderConstants();
        }
Ejemplo n.º 3
0
        public static void Init(Device device)
        {
            #region Sampler
            SamplerStateDescription desc = new SamplerStateDescription();
            desc.AddressU = TextureAddressMode.Wrap;
            desc.AddressV = TextureAddressMode.Wrap;
            desc.AddressW = TextureAddressMode.Wrap;
            desc.Filter = Filter.MinMagMipLinear;
            SamplLinWrap = new SamplerState(ModelViewer.Program.device, desc);
            desc.Filter = Filter.MinMagMipPoint;
            SamplPntWrap = new SamplerState(ModelViewer.Program.device, desc);
            desc.Filter = Filter.Anisotropic;
            if (Global.Settings.TextureFiltering > -1) desc.MaximumAnisotropy = System.Math.Min(Global.Settings.TextureFiltering, 16);
            else desc.MaximumAnisotropy = 0;
            SamplAnsWrap = new SamplerState(ModelViewer.Program.device, desc);
            desc.MaximumAnisotropy = 0;
            desc.MinimumLod = 0;
            desc.MaximumLod = 7;
            desc.Filter = Filter.MinMagMipLinear;
            SamplLinWrapMip = new SamplerState(ModelViewer.Program.device, desc);
            desc.Filter = Filter.MinMagMipPoint;
            SamplPntWrapMip = new SamplerState(ModelViewer.Program.device, desc);
            desc.Filter = Filter.Anisotropic;
            if (Global.Settings.TextureFiltering > -1) desc.MaximumAnisotropy = System.Math.Min(Global.Settings.TextureFiltering, 16);
            else desc.MaximumAnisotropy = 0;
            SamplAnsWrapMip = new SamplerState(ModelViewer.Program.device, desc);
            #endregion

            OcclViewport = new Viewport(0, 0, OcclPixelInTex, 1);

            OcclTexture = new Texture2D[1];
            OcclSRV = new ShaderResourceView[1];
            OcclRTV = new RenderTargetView[1];
            InitOcclInd(0);

            MConteints = new MeshContainer();

            MConteints.BytesPerVertex = 12;
            MConteints.FaceCount = 2;
            MConteints.VertexsCount = 4;

            var vertices = new DataStream(MConteints.BytesPerVertex * MConteints.VertexsCount, true, true);
            vertices.Write(new Vector3(0.0f, 0.0f, 0.5f));
            vertices.Write(new Vector3(1.0f, 0.0f, 0.5f));
            vertices.Write(new Vector3(1.0f, 1.0f, 0.5f));
            vertices.Write(new Vector3(0.0f, 1.0f, 0.5f));
            vertices.Position = 0;

            MConteints.Vertexs = new Buffer(device, vertices, MConteints.BytesPerVertex * MConteints.VertexsCount, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            var indices = new DataStream(4 * MConteints.FaceCount * 3, true, true);
            indices.Write(0);
            indices.Write(1);
            indices.Write(2);
            indices.Write(2);
            indices.Write(3);
            indices.Write(0);
            indices.Position = 0;

            MConteints.Indices = new Buffer(device, indices, 4 * MConteints.FaceCount * 3, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            MConteints.binding = new VertexBufferBinding(MConteints.Vertexs, MConteints.BytesPerVertex, 0);

            InputElement[] elements11 = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0) };
            EEEM = ContentManager.LoadEffect("Content/Shaders/OcclusionTest", elements11);

            // Подготовка константного буффера
            BufferDescription bd = new BufferDescription();
            bd.SizeInBytes = Marshal.SizeOf(typeof(ShaderConstants));
            bd.Usage = ResourceUsage.Dynamic;
            bd.BindFlags = BindFlags.ConstantBuffer;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.OptionFlags = ResourceOptionFlags.None;
            bd.StructureByteStride = 0;

            constantsBuffer = new Buffer(device, bd);
            constants = new ShaderConstants();

            constants.Resolution = new Vector4(Global.Settings.Width, Global.Settings.Height, OcclPixelInTex, 1);
            constants.WM = Matrix.Transpose(Matrix.OrthoLH(constants.Resolution.X, constants.Resolution.Y, 0.1f, 1000));

            LensBatching.Init(device);
            PSSMsHelper.Init(device);
        }
Ejemplo n.º 4
0
        public static void Init(Device device, int W, int H)
        {
            #region Mesh
            _MConteints = new MeshContainer();

            _MConteints.BytesPerVertex = 12;
            _MConteints.FaceCount = 2;
            _MConteints.VertexsCount = 4;

            var vertices = new DataStream(_MConteints.BytesPerVertex * _MConteints.VertexsCount, true, true);
            vertices.Write(new Vector3(0.0f, 0.0f, 0.5f));
            vertices.Write(new Vector3(1.0f, 0.0f, 0.5f));
            vertices.Write(new Vector3(1.0f, 1.0f, 0.5f));
            vertices.Write(new Vector3(0.0f, 1.0f, 0.5f));
            vertices.Position = 0;

            _MConteints.Vertexs = new Buffer(device, vertices, _MConteints.BytesPerVertex * _MConteints.VertexsCount, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            var indices = new DataStream(4 * _MConteints.FaceCount * 3, true, true);
            indices.Write(0);
            indices.Write(1);
            indices.Write(2);
            indices.Write(2);
            indices.Write(3);
            indices.Write(0);
            indices.Position = 0;

            _MConteints.Indices = new Buffer(device, indices, 4 * _MConteints.FaceCount * 3, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            _MConteints.binding = new VertexBufferBinding(_MConteints.Vertexs, _MConteints.BytesPerVertex, 0);

            InputElement[] elements11 = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0) };
            EEEM = ContentManager.LoadEffect("Content/Shaders/QuadShader", elements11);

            // Подготовка константного буффера
            BufferDescription bd = new BufferDescription();
            bd.SizeInBytes = Marshal.SizeOf(typeof(ShaderConstants));
            bd.Usage = ResourceUsage.Dynamic;
            bd.BindFlags = BindFlags.ConstantBuffer;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.OptionFlags = ResourceOptionFlags.None;
            bd.StructureByteStride = 0;

            constantsBuffer = new Buffer(device, bd);
            constants = new ShaderConstants();
            constants.Resolution = new Vector4(W, H, 0, 0);
            constants.WM = Matrix.OrthoLH(constants.Resolution.X, constants.Resolution.Y, 0.1f, 1000);
            #endregion
            //RenderTargets
            Texture2DDescription BufDescription = new Texture2DDescription();
            BufDescription.MipLevels = 1;
            BufDescription.ArraySize = 1;
            BufDescription.Width = W/4;
            BufDescription.Height = H/4;
            BufDescription.Format = Format.R8G8B8A8_UNorm;
            BufDescription.SampleDescription = new SampleDescription(1, 0);
            BufDescription.Usage = ResourceUsage.Default;
            BufDescription.BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource;
            BufDescription.CpuAccessFlags = CpuAccessFlags.None;
            BufDescription.OptionFlags = ResourceOptionFlags.None;

            BufTexture_1 = new Texture2D(device, BufDescription);
            BufTexture_2 = new Texture2D(device, BufDescription);

            BufSRV_1 = new ShaderResourceView(device, BufTexture_1);
            BufSRV_2 = new ShaderResourceView(device, BufTexture_2);

            RenderTargetViewDescription RTVD = new RenderTargetViewDescription();
            RTVD.Format = BufDescription.Format;
            RTVD.Dimension = RenderTargetViewDimension.Texture2D;
            RTVD.Texture2DArray.FirstArraySlice = 0;
            RTVD.Texture2DArray.ArraySize = 1;
            RTVD.Texture2D.MipSlice = 0;

            BufRTV_1 = new RenderTargetView(device, BufTexture_1, RTVD);
            BufRTV_2 = new RenderTargetView(device, BufTexture_2, RTVD);

            BufDescription.Width = W / 2;
            BufDescription.Height = H / 2;
            BufDescription.MipLevels = 0;

            BufDescription.OptionFlags = ResourceOptionFlags.GenerateMipMaps;
            BufDescription.Format = Format.R32G32B32A32_Float;
            RTVD.Format = BufDescription.Format;
            BufTexture_3 = new Texture2D(device, BufDescription);
            BufSRV_3 = new ShaderResourceView(device, BufTexture_3);
            BufRTV_3 = new RenderTargetView(device, BufTexture_3, RTVD);

            SamplerStateDescription sd = new SamplerStateDescription();
            sd.AddressU = TextureAddressMode.Clamp;
            sd.AddressV = TextureAddressMode.Clamp;
            sd.AddressW = TextureAddressMode.Clamp;
            sd.Filter = Filter.MinMagMipLinear;
            sd.MinimumLod = 0;
            sd.MaximumLod = 16;

            BufSS = new SamplerState(device, ref sd);

            HDRViewport = new Viewport(0, 0, W / 4, H / 4);
            DoubleHDRViewport = new Viewport(0, 0, W / 2, H / 2);

            BufDescription = new Texture2DDescription();
            BufDescription.MipLevels = 1;
            BufDescription.ArraySize = 1;
            BufDescription.Width = 1;
            BufDescription.Height = 1;
            BufDescription.Format = Format.R32G32B32A32_Float;
            BufDescription.SampleDescription = new SampleDescription(1, 0);
            BufDescription.Usage = ResourceUsage.Default;
            BufDescription.BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource;
            BufDescription.CpuAccessFlags = CpuAccessFlags.None;
            BufDescription.OptionFlags = ResourceOptionFlags.None;

            RTVD = new RenderTargetViewDescription();
            RTVD.Format = BufDescription.Format;
            RTVD.Dimension = RenderTargetViewDimension.Texture2D;
            RTVD.Texture2DArray.FirstArraySlice = 0;
            RTVD.Texture2DArray.ArraySize = 1;
            RTVD.Texture2D.MipSlice = 0;

            T1x1 = new Texture2D(device, BufDescription);
            V1x1 = new Viewport(0, 0, 1, 1);
            SRV1x1 = new ShaderResourceView(device, T1x1);
            RTV1x1 = new RenderTargetView(device, T1x1, RTVD);

            device.ImmediateContext.ClearRenderTargetView(BufRTV_3, new Color4(0.0f, 0.0f, 0.0f, 1f));
        }
        /// <summary>
        /// Получить отдельный мэш
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static MeshContainer LoadMesh(string FileName)
        {
            string Origin = FileName;
            if (Meshes.ContainsKey(Origin) && Meshes[Origin].IsAlive)
            {
                //Уже есть
                return (MeshContainer)Meshes[Origin].Target;
            }
            else
            {
                //нужно загрузить
                MeshContainer _MConteints = null;

                if (File.Exists(FileName + ".mesh"))
                {
                    FileName += ".mesh";
                }

                string extension = System.IO.Path.GetExtension(FileName);

                if (extension.ToLower() == ".mesh")
                {
                    try
                    {
                        System.IO.FileStream fs = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        Serialize.Mesh M = Serializer.Deserialize<Serialize.Mesh>(fs);
                        fs.Close();
                        if (M != null) _MConteints = new MeshContainer(M);
                        else return null;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show("Error! Ошибкак чтения модели: " + FileName);
                        new FileNotFoundException("Файл", FileName);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Error! Неверный тип файла: " + FileName);
                    new FileNotFoundException("Неверный тип файла", FileName);
                }

                WeakReference wr = new WeakReference(_MConteints, false);
                Meshes.Add(Origin, wr);
                _MConteints.Name = Origin;
                return _MConteints;
                //TODO проверка на валидность загрузки
            }
        }