protected override async void InternalLoad()
        {
            var swapChainDescription = this.swapChain?.SwapChainDescription;

            this.width  = swapChainDescription.Value.Width;
            this.height = swapChainDescription.Value.Height;

            // Graphics Resources
            var vertexShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "VertexShader", ShaderStages.Vertex, "VS");

            var pixelShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "FragmentShader", ShaderStages.Pixel, "PS");

            var vertexShader = this.graphicsContext.Factory.CreateShader(ref vertexShaderDescription);
            var pixelShader  = this.graphicsContext.Factory.CreateShader(ref pixelShaderDescription);

            Texture textureCube = null;

            using (var stream = System.IO.File.OpenRead("Content/TextureCubeToSpherical/TextureCube.ktx"))
            {
                if (stream != null)
                {
                    VisualTests.LowLevel.Images.Image image = VisualTests.LowLevel.Images.Image.Load(stream);
                    var textureDescription = image.TextureDescription;
                    textureCube = graphicsContext.Factory.CreateTexture(image.DataBoxes, ref textureDescription);
                }
            }

            var samplerDescription = SamplerStates.LinearClamp;
            var samplerState       = this.graphicsContext.Factory.CreateSamplerState(ref samplerDescription);

            ResourceLayoutDescription layoutDescription = new ResourceLayoutDescription(
                new LayoutElementDescription(0, ResourceType.Texture, ShaderStages.Pixel),
                new LayoutElementDescription(0, ResourceType.Sampler, ShaderStages.Pixel));
            ResourceLayout resourceLayout = this.graphicsContext.Factory.CreateResourceLayout(ref layoutDescription);

            ResourceSetDescription resourceSetDescription = new ResourceSetDescription(resourceLayout, textureCube, samplerState);

            this.resourceSet = this.graphicsContext.Factory.CreateResourceSet(ref resourceSetDescription);

            var pipelineDescription = new GraphicsPipelineDescription()
            {
                PrimitiveTopology = PrimitiveTopology.TriangleList,
                InputLayouts      = null,
                ResourceLayouts   = new[] { resourceLayout },
                Shaders           = new ShaderStateDescription()
                {
                    VertexShader = vertexShader,
                    PixelShader  = pixelShader,
                },
                RenderStates = new RenderStateDescription()
                {
                    RasterizerState   = RasterizerStates.CullBack,
                    BlendState        = BlendStates.Opaque,
                    DepthStencilState = DepthStencilStates.Read,
                },
                Outputs = this.frameBuffer.OutputDescription,
            };

            this.graphicsPipelineState = this.graphicsContext.Factory.CreateGraphicsPipeline(ref pipelineDescription);
            this.graphicsCommandQueue  = this.graphicsContext.Factory.CreateCommandQueue(CommandQueueType.Graphics);

            this.viewports    = new Viewport[1];
            this.viewports[0] = new Viewport(0, 0, this.width, this.height);
            this.scissors     = new Rectangle[1];
            this.scissors[0]  = new Rectangle(0, 0, (int)this.width, (int)this.height);
        }
Пример #2
0
        protected override async void InternalLoad()
        {
            // Compile Vertex and Pixel shaders
            var vertexShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "VertexShader", ShaderStages.Vertex, "VS");

            var pixelShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "HLSL", "FragmentShader", ShaderStages.Pixel, "PS");

            var vertexShader = this.graphicsContext.Factory.CreateShader(ref vertexShaderDescription);
            var pixelShader  = this.graphicsContext.Factory.CreateShader(ref pixelShaderDescription);


            var vertexBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <VertexPositionNormalTexture>() * (uint)vertexData.Length, BufferFlags.VertexBuffer, ResourceUsage.Default);
            var vertexBuffer            = this.graphicsContext.Factory.CreateBuffer(vertexData, ref vertexBufferDescription);

            this.view = Matrix4x4.CreateLookAt(new Vector3(0, 0, 4f), new Vector3(0, 0, 0), Vector3.UnitY);
            this.proj = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)this.frameBuffer.Width / (float)this.frameBuffer.Height, 0.1f, 100f);

            // Parameters
            float density       = 0.4f;
            float minHairLength = 0.5f;

            this.parameters = new Parameters();
            this.parameters.displacement     = new Vector3(0, -0.05f, 0);
            this.parameters.numLayers        = 50f;
            this.parameters.startShadowValue = 0.2f;
            this.parameters.MaxHairLength    = 0.2f;
            this.parameters.viewProj         = Matrix4x4.Multiply(this.view, this.proj);

            // Constant Buffer
            var constantBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <Parameters>(), BufferFlags.ConstantBuffer, ResourceUsage.Default);

            this.constantBuffer = this.graphicsContext.Factory.CreateBuffer(ref this.parameters, ref constantBufferDescription);

            // Create FurTexture
            uint size        = 1024;
            var  description = new TextureDescription()
            {
                Type        = TextureType.Texture2D,
                Width       = size,
                Height      = size,
                Depth       = 1,
                ArraySize   = 1,
                Faces       = 1,
                Usage       = ResourceUsage.Default,
                CpuAccess   = ResourceCpuAccess.None,
                Flags       = TextureFlags.ShaderResource,
                Format      = PixelFormat.R8_UNorm,
                MipLevels   = 1,
                SampleCount = TextureSampleCount.None,
            };
            var textureFur = this.graphicsContext.Factory.CreateTexture(ref description);

            uint totalPixels = size * size;

            byte[] data = new byte[totalPixels];

            int strands  = (int)(density * totalPixels);
            int minValue = (int)(minHairLength * 255f);

            for (int i = 0; i < strands; i++)
            {
                int x = rand.Next((int)size);
                int y = rand.Next((int)size);
                data[(x * size) + y] = (byte)rand.Next(minValue, 255);
            }

            this.graphicsContext.UpdateTextureData(textureFur, data);

            // Color Texture
            Texture texture2D = null;

            ////using (var stream = this.assetsDirectory.Open("Cat.ktx"))
            using (var stream = this.assetsDirectory.Open("Leopard.ktx"))
            ////using (var stream = this.assetsDirectory.Open("Cheetah.ktx"))
            ////using (var stream = this.assetsDirectory.Open("GrayLeopard.ktx"))
            ////using (var stream = this.assetsDirectory.Open("Tiger.ktx"))
            ////using (var stream = this.assetsDirectory.Open("Zebra.ktx"))
            {
                if (stream != null)
                {
                    VisualTests.LowLevel.Images.Image image = VisualTests.LowLevel.Images.Image.Load(stream);
                    var textureDescription = image.TextureDescription;
                    texture2D = graphicsContext.Factory.CreateTexture(image.DataBoxes, ref textureDescription);
                }
            }

            SamplerStateDescription sampler1Description = SamplerStates.LinearClamp;
            var sampler1 = this.graphicsContext.Factory.CreateSamplerState(ref sampler1Description);

            SamplerStateDescription sampler2Description = SamplerStates.PointClamp;
            var sampler2 = this.graphicsContext.Factory.CreateSamplerState(ref sampler2Description);

            // Prepare Pipeline
            var vertexLayouts = new InputLayouts()
                                .Add(VertexPositionNormalTexture.VertexFormat);

            ResourceLayoutDescription layoutDescription = new ResourceLayoutDescription(
                new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Vertex),
                new LayoutElementDescription(0, ResourceType.Texture, ShaderStages.Pixel),
                new LayoutElementDescription(1, ResourceType.Texture, ShaderStages.Pixel),
                new LayoutElementDescription(0, ResourceType.Sampler, ShaderStages.Pixel),
                new LayoutElementDescription(1, ResourceType.Sampler, ShaderStages.Pixel));

            ResourceLayout resourcesLayout = this.graphicsContext.Factory.CreateResourceLayout(ref layoutDescription);

            ResourceSetDescription resourceSetDescription = new ResourceSetDescription(resourcesLayout, this.constantBuffer, texture2D, textureFur, sampler1, sampler2);

            this.resourceSet = this.graphicsContext.Factory.CreateResourceSet(ref resourceSetDescription);

            var pipelineDescription = new GraphicsPipelineDescription()
            {
                PrimitiveTopology = PrimitiveTopology.TriangleList,
                InputLayouts      = vertexLayouts,
                ResourceLayouts   = new[] { resourcesLayout },
                Shaders           = new ShaderStateDescription()
                {
                    VertexShader = vertexShader,
                    PixelShader  = pixelShader,
                },
                RenderStates = new RenderStateDescription()
                {
                    RasterizerState   = RasterizerStates.None,
                    BlendState        = BlendStates.Opaque,
                    DepthStencilState = DepthStencilStates.None,
                },
                Outputs = this.frameBuffer.OutputDescription,
            };

            this.graphicsPipelineState = this.graphicsContext.Factory.CreateGraphicsPipeline(ref pipelineDescription);
            this.graphicsCommandQueue  = this.graphicsContext.Factory.CreateCommandQueue();

            var swapChainDescription = this.swapChain?.SwapChainDescription;
            var width  = swapChainDescription.HasValue ? swapChainDescription.Value.Width : this.surface.Width;
            var height = swapChainDescription.HasValue ? swapChainDescription.Value.Height : this.surface.Height;

            this.viewports    = new Viewport[1];
            this.viewports[0] = new Viewport(0, 0, width, height);
            this.scissors     = new Rectangle[1];
            this.scissors[0]  = new Rectangle(0, 0, (int)width, (int)height);

            this.vertexBuffers    = new Buffer[1];
            this.vertexBuffers[0] = vertexBuffer;
        }