Exemplo n.º 1
0
        public MainPage()
        {
            this.InitializeComponent();

            m_ctx       = new ResourceCreateContext();
            m_swapChain = new CompositionSwapChainResources(m_ctx, m_swapChainPanel);

            var display = DisplayInformation.GetForCurrentView();

            display.DpiChanged += new TypedEventHandler <DisplayInformation, object>(OnDpiChanged);
            m_triangle          = makeTriangle(m_ctx);
            m_compute           = makeCompute(m_ctx);

            m_buffer  = m_ctx.CreateByteAddressBuffer(4096, ResourceState.CopyDestination);
            m_texture = m_ctx.CreateTexture2D(256, 256, 1, GraphicsFormat.R8_UNORM, ResourceState.CopyDestination);

            var ctx = m_swapChain.CreateGraphicsComputeCommandContext();

            {
                float[] positions =
                {
                    0.0f,  0.5f, 0.5f, 1.0f,
                    -0.5f, 0.0f, 0.5f, 1.0f,
                    0.5f,  0.0f, 0.5f, 1.0f
                };

                byte[] b0 = new byte[positions.Length * 4];
                Buffer.BlockCopy(positions, 0, b0, 0, b0.Length);

                float[] colors =
                {
                    1.0f, 0.0f, 0.0f,
                    0.0f, 1.0f, 0.0f,
                    0.0f, 0.0f, 1.0f
                };

                byte[] b1 = new byte[colors.Length * 4];
                Buffer.BlockCopy(colors, 0, b1, 0, b1.Length);

                ctx.UpdateBuffer(m_buffer, 0, b0);
                ctx.UpdateBuffer(m_buffer, (uint)b0.Length, b1);
            }

            {
                var subResourceData = new SubresourceData();

                subResourceData.Data       = GenerateTextureData();
                subResourceData.RowPitch   = 256;
                subResourceData.SlicePitch = 256 * 256;

                SubresourceData[] datas = { subResourceData };

                ctx.UploadResource(m_texture, 0, 1, datas);
            }

            ctx.TransitionResource(m_texture, ResourceState.CopyDestination, ResourceState.PixelShaderResource | ResourceState.NonPixelShaderResource);
            ctx.TransitionResource(m_buffer, ResourceState.CopyDestination, ResourceState.NonPixelShaderResource);

            ctx.SubmitAndWaitToExecute();
        }
        public MainPage()
        {
            this.InitializeComponent();

            m_ctx       = new ResourceCreateContext();
            m_swapChain = new CompositionSwapChainResources(m_ctx, m_swapChainPanel);

            var display = DisplayInformation.GetForCurrentView();

            display.DpiChanged += new TypedEventHandler <DisplayInformation, object>(OnDpiChanged);

            var ctx = m_swapChain.CreateGraphicsComputeCommandContext();

            m_mechanicMaterial = new DerivativesSkinnedMaterial(m_ctx, ctx, "");
            m_mechanicModel    = new DerivativesSkinnedModel(m_mechanicMaterial, m_ctx, ctx, @"Assets\\models\\military_mechanic.derivatives_skinned_model.model");
            m_mechanicInstance = new DerivativesSkinnedModelInstance(m_mechanicModel, identity());

            ctx.SubmitAndWaitToExecute();

            m_lighting = makeLighting(m_ctx);
        }
Exemplo n.º 3
0
        protected override async void InternalLoad()
        {
            var swapChainDescription = this.swapChain?.SwapChainDescription;

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

            // Create Scene
            BasicScene();

            // Compute Resources
            var spheresBufferDescription = new BufferDescription(
                (uint)(Unsafe.SizeOf <Sphere>() * spheres.Length),
                BufferFlags.UnorderedAccess | BufferFlags.ShaderResource | BufferFlags.BufferStructured,
                ResourceUsage.Default,
                ResourceCpuAccess.None,
                Unsafe.SizeOf <Sphere>());

            spheresBuffer = this.graphicsContext.Factory.CreateBuffer(spheres, ref spheresBufferDescription);

            var materialsBufferDescription = new BufferDescription(
                (uint)(Unsafe.SizeOf <Material>() * spheres.Length),
                BufferFlags.UnorderedAccess | BufferFlags.ShaderResource | BufferFlags.BufferStructured,
                ResourceUsage.Default,
                ResourceCpuAccess.None,
                Unsafe.SizeOf <Material>());

            materialsBuffer = this.graphicsContext.Factory.CreateBuffer(materials, ref materialsBufferDescription);

            this.threadGroupX = this.width / 16u;
            this.threadGroupY = this.height / 16u;

            CompilerParameters parameters = new CompilerParameters()
            {
                CompilationMode = CompilationMode.None,
                Profile         = GraphicsProfile.Level_11_0,
            };

            var computeShaderDescription = await this.assetsDirectory.ReadAndCompileShader(this.graphicsContext, "CS", "ComputeShader", ShaderStages.Compute, "CS", parameters);

            var computeShader = this.graphicsContext.Factory.CreateShader(ref computeShaderDescription);

            var textureDescription = new TextureDescription()
            {
                Type        = TextureType.Texture2D,
                Usage       = ResourceUsage.Default,
                Flags       = TextureFlags.UnorderedAccess | TextureFlags.ShaderResource,
                Format      = PixelFormat.R8G8B8A8_UNorm,
                Width       = this.width,
                Height      = this.height,
                Depth       = 1,
                MipLevels   = 1,
                ArraySize   = 1,
                CpuAccess   = ResourceCpuAccess.None,
                SampleCount = TextureSampleCount.None,
            };

            Texture texture2D = this.graphicsContext.Factory.CreateTexture(ref textureDescription);

            this.computeData = new ComputeData()
            {
                time        = 0,
                width       = this.width,
                height      = this.height,
                framecount  = 0,
                samples     = 25,
                recursion   = 5,
                spherecount = (uint)spheres.Length,
                cam         = this.cam
            };

            var constantBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <ComputeData>(), BufferFlags.ConstantBuffer, ResourceUsage.Default);

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

            ResourceLayoutDescription computeLayoutDescription = new ResourceLayoutDescription(
                new LayoutElementDescription(0, ResourceType.StructuredBuffer, ShaderStages.Compute),
                new LayoutElementDescription(1, ResourceType.StructuredBuffer, ShaderStages.Compute),
                new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Compute),
                new LayoutElementDescription(0, ResourceType.TextureReadWrite, ShaderStages.Compute));

            ResourceLayout computeResourceLayout = this.graphicsContext.Factory.CreateResourceLayout(ref computeLayoutDescription);

            ResourceSetDescription computeResourceSetDescription = new ResourceSetDescription(computeResourceLayout, this.spheresBuffer, this.materialsBuffer, this.constantBuffer, texture2D);

            this.computeResourceSet = this.graphicsContext.Factory.CreateResourceSet(ref computeResourceSetDescription);

            var computePipelineDescription = new ComputePipelineDescription()
            {
                ComputeShader    = computeShader,
                ResourceLayouts  = new[] { computeResourceLayout },
                ThreadGroupSizeX = 16,
                ThreadGroupSizeY = 16,
                ThreadGroupSizeZ = 1,
            };

            this.computePipelineState = this.graphicsContext.Factory.CreateComputePipeline(ref computePipelineDescription);

            // 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);

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

            // Set Params
            this.paramsData.Samples       = 1;
            this.paramsData.IsPathTracing = false;

            var paramsBufferDescription = new BufferDescription((uint)Unsafe.SizeOf <Params>(), BufferFlags.ConstantBuffer, ResourceUsage.Default);

            this.paramsBuffer = this.graphicsContext.Factory.CreateBuffer(ref paramsBufferDescription);

            ResourceLayoutDescription layoutDescription = new ResourceLayoutDescription(
                new LayoutElementDescription(0, ResourceType.ConstantBuffer, ShaderStages.Pixel),
                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, this.paramsBuffer, texture2D, samplerState);

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

            BlendStateDescription blend = BlendStateDescription.Default;

            if (this.paramsData.IsPathTracing)
            {
                blend.RenderTarget0.BlendEnable           = true;
                blend.RenderTarget0.SourceBlendColor      = Blend.SourceAlpha;
                blend.RenderTarget0.DestinationBlendColor = Blend.InverseSourceAlpha;
            }
            else
            {
                blend = BlendStates.Opaque;
            }

            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        = blend,
                    DepthStencilState = DepthStencilStates.Read,
                },
                Outputs = this.frameBuffer.OutputDescription,
            };

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

            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);
        }