| 663 | } |
| 664 | |
| 665 | void GPUDevice::DrawEnd() |
| 666 | { |
| 667 | PROFILE_CPU_NAMED("Present"); |
| 668 | |
| 669 | // Check if use VSync |
| 670 | bool useVSync = Graphics::UseVSync; |
| 671 | if (CommandLine::Options.NoVSync.HasValue()) |
| 672 | useVSync = !CommandLine::Options.NoVSync.GetValue(); |
| 673 | else if (CommandLine::Options.VSync.HasValue()) |
| 674 | useVSync = CommandLine::Options.VSync.GetValue(); |
| 675 | |
| 676 | // Find index of the last rendered window task (use vsync only on the last window) |
| 677 | int32 lastWindowIndex = -1; |
| 678 | for (int32 i = RenderTask::Tasks.Count() - 1; i >= 0; i--) |
| 679 | { |
| 680 | const auto task = RenderTask::Tasks[i]; |
| 681 | if (task && task->LastUsedFrame == Engine::FrameCount && task->SwapChain && task->SwapChain->IsReady()) |
| 682 | { |
| 683 | lastWindowIndex = i; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // Call present on all used tasks |
| 689 | int32 presentCount = 0; |
| 690 | bool anyVSync = false; |
| 691 | #if COMPILE_WITH_PROFILER |
| 692 | const double presentStart = Platform::GetTimeSeconds(); |
| 693 | #endif |
| 694 | for (int32 i = 0; i < RenderTask::Tasks.Count(); i++) |
| 695 | { |
| 696 | const auto task = RenderTask::Tasks[i]; |
| 697 | if (task && task->LastUsedFrame == Engine::FrameCount && task->SwapChain && task->SwapChain->IsReady()) |
| 698 | { |
| 699 | bool vsync = useVSync; |
| 700 | if (lastWindowIndex != i) |
| 701 | { |
| 702 | // Perform VSync only on the last window |
| 703 | vsync = false; |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | // End profiler timer queries |
| 708 | #if COMPILE_WITH_PROFILER |
| 709 | ProfilerGPU::OnPresent(); |
| 710 | #endif |
| 711 | } |
| 712 | |
| 713 | anyVSync |= vsync; |
| 714 | task->OnPresent(vsync); |
| 715 | presentCount++; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // If no `Present` calls has been performed just execute GPU commands |
| 720 | if (presentCount == 0) |
| 721 | { |
| 722 | // End profiler timer queries |