public bool DrawFrame(Action <int, int> draw, [CallerMemberName] string frameName = null)
        {
            int rtvIndex = BeginFrame();

            // Refit the top-level acceleration structure
            this.acs.CreateTopLevelAS(mpDevice, mpCmdList, this.bottomLevelBuffers, ref mTlasSize, this.mRotation, true, ref topLevelBuffers);
            this.mRotation += 0.005f;

            // Let's raytrace
            context.ResourceBarrier(mpCmdList, mpOutputResource, ResourceStates.CopySource, ResourceStates.UnorderedAccess);
            DispatchRaysDescription raytraceDesc = new DispatchRaysDescription();

            raytraceDesc.Width  = mSwapChainRect.Width;
            raytraceDesc.Height = mSwapChainRect.Height;
            raytraceDesc.Depth  = 1;

            // RayGen is the first entry in the shader-table
            raytraceDesc.RayGenerationShaderRecord.StartAddress = mpShaderTable.GPUVirtualAddress + 0 * mShaderTableEntrySize;
            raytraceDesc.RayGenerationShaderRecord.SizeInBytes  = mShaderTableEntrySize;

            // Miss is the second entry in the shader-table
            uint missOffset = 1 * mShaderTableEntrySize;

            raytraceDesc.MissShaderTable.StartAddress  = mpShaderTable.GPUVirtualAddress + missOffset;
            raytraceDesc.MissShaderTable.StrideInBytes = mShaderTableEntrySize;
            raytraceDesc.MissShaderTable.SizeInBytes   = mShaderTableEntrySize * 2; // Only a s single miss-entry

            // Hit is the third entry in the shader-table
            uint hitOffset = 3 * mShaderTableEntrySize;

            raytraceDesc.HitGroupTable.StartAddress  = mpShaderTable.GPUVirtualAddress + hitOffset;
            raytraceDesc.HitGroupTable.StrideInBytes = mShaderTableEntrySize;
            raytraceDesc.HitGroupTable.SizeInBytes   = mShaderTableEntrySize;

            // Bind the empty root signature
            mpCmdList.SetComputeRootSignature(mpEmptyRootSig);

            // Dispatch
            mpCmdList.SetPipelineState1(mpPipelineState);
            mpCmdList.DispatchRays(raytraceDesc);

            // Copy the results to the back-buffer
            context.ResourceBarrier(mpCmdList, mpOutputResource, ResourceStates.UnorderedAccess, ResourceStates.CopySource);
            context.ResourceBarrier(mpCmdList, mFrameObjects[rtvIndex].pSwapChainBuffer, ResourceStates.Present, ResourceStates.CopyDestination);
            mpCmdList.CopyResource(mFrameObjects[rtvIndex].pSwapChainBuffer, mpOutputResource);

            EndFrame(rtvIndex);

            return(true);
        }
示例#2
0
        public bool DrawFrame(Action <int, int> draw, [CallerMemberName] string frameName = null)
        {
            int rtvIndex = BeginFrame();

            CreateTopLevelAS(device, commandList, bottomLevelAS, ref tlasSize, true, rotation);
            rotation += 0.0005f;

            // Let's raytrace
            InsertTransitionResourceBarrier(outputResource, ResourceStates.CopySource, ResourceStates.UnorderedAccess);
            DispatchRaysDescription raytraceDesc = new DispatchRaysDescription();

            raytraceDesc.Width  = Window.Width;
            raytraceDesc.Height = Window.Height;
            raytraceDesc.Depth  = 1;

            // RayGen is the first entry in the shader-table
            raytraceDesc.RayGenerationShaderRecord = new GpuVirtualAddressRange();
            raytraceDesc.RayGenerationShaderRecord.StartAddress = shaderTable.GPUVirtualAddress + 0 * shaderTableEntrySize;
            raytraceDesc.RayGenerationShaderRecord.SizeInBytes  = shaderTableEntrySize;

            // Miss is the second entry in the shader-table
            uint missOffset = 1 * shaderTableEntrySize;

            raytraceDesc.MissShaderTable = new GpuVirtualAddressRangeAndStride();
            raytraceDesc.MissShaderTable.StartAddress  = shaderTable.GPUVirtualAddress + missOffset;
            raytraceDesc.MissShaderTable.StrideInBytes = shaderTableEntrySize;
            raytraceDesc.MissShaderTable.SizeInBytes   = shaderTableEntrySize * 2; // Only a s single miss-entry

            // Hit is the third entry in the shader-table
            uint hitOffset = 3 * shaderTableEntrySize;

            raytraceDesc.HitGroupTable = new GpuVirtualAddressRangeAndStride();
            raytraceDesc.HitGroupTable.StartAddress  = shaderTable.GPUVirtualAddress + hitOffset;
            raytraceDesc.HitGroupTable.StrideInBytes = shaderTableEntrySize;
            raytraceDesc.HitGroupTable.SizeInBytes   = shaderTableEntrySize * ((triangleConstantBuffers.Length + 1) * 2);

            // Bind the empty root signature
            commandList.SetComputeRootSignature(emptyRootSig);

            // Dispatch
            commandList.SetPipelineState1(pipelineState);
            commandList.DispatchRays(raytraceDesc);

            // Copy the results to the back-buffer
            InsertTransitionResourceBarriers(outputResource,
                                             new ResourceStates[]
            {
                ResourceStates.UnorderedAccess,
                ResourceStates.Present
            },
                                             new ResourceStates[]
            {
                ResourceStates.CopySource,
                ResourceStates.CopyDestination
            });
            commandList.CopyResource(frameObjects[rtvIndex].swapChainBuffer, outputResource);

            EndFrame(rtvIndex);

            frameCounter++;

            if (stopwatch.ElapsedMilliseconds >= 1000)
            {
                Console.WriteLine("FPS: " + frameCounter);

                long primaryRayCount      = Window.Width * Window.Height;
                long primaryRaysPerSecond = primaryRayCount * frameCounter;
                Console.WriteLine((primaryRaysPerSecond / 1000000) + " MRays/second");

                frameCounter = 0;
                stopwatch.Restart();
            }

            return(true);
        }