| 132 | } |
| 133 | |
| 134 | void Screenshot::Capture(GPUTexture* target, const StringView& path) |
| 135 | { |
| 136 | // Validate |
| 137 | if (target == nullptr) |
| 138 | { |
| 139 | LOG(Warning, "Cannot take screenshot. Render target texture is not allocated."); |
| 140 | return; |
| 141 | } |
| 142 | if (target->Depth() != 1) |
| 143 | { |
| 144 | LOG(Warning, "Cannot take screenshot. 3D textures are not supported."); |
| 145 | return; |
| 146 | } |
| 147 | if (GPUDevice::Instance == nullptr || GPUDevice::Instance->GetState() != GPUDevice::DeviceState::Ready) |
| 148 | { |
| 149 | LOG(Warning, "Cannot take screenshot. Graphics device is not ready."); |
| 150 | return; |
| 151 | } |
| 152 | PROFILE_MEM(Graphics); |
| 153 | |
| 154 | // Faster path for staging textures that contents are ready to access on a CPU |
| 155 | if (target->IsStaging()) |
| 156 | { |
| 157 | CaptureScreenshot screenshot(target, path); |
| 158 | TextureData& data = screenshot.GetData(); |
| 159 | data.Width = target->Width(); |
| 160 | data.Height = target->Height(); |
| 161 | data.Format = target->Format(); |
| 162 | data.Depth = target->Depth(); |
| 163 | data.Items.Resize(target->ArraySize()); |
| 164 | for (int32 arrayIndex = 0; arrayIndex < target->ArraySize(); arrayIndex++) |
| 165 | { |
| 166 | auto& slice = data.Items[arrayIndex]; |
| 167 | slice.Mips.Resize(target->MipLevels()); |
| 168 | for (int32 mipIndex = 0; mipIndex < target->MipLevels(); mipIndex++) |
| 169 | { |
| 170 | auto& mip = slice.Mips[mipIndex]; |
| 171 | if (target->GetData(arrayIndex, mipIndex, mip)) |
| 172 | { |
| 173 | LOG(Warning, "Cannot take screenshot. Failed to get texture data."); |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | screenshot.Run(); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // Create tasks |
| 183 | auto saveTask = New<CaptureScreenshot>(target, path); |
| 184 | auto downloadTask = target->DownloadDataAsync(saveTask->GetData()); |
| 185 | if (downloadTask == nullptr) |
| 186 | { |
| 187 | LOG(Warning, "Cannot capture screenshot. Cannot create download async task."); |
| 188 | Delete(saveTask); |
| 189 | return; |
| 190 | } |
| 191 |
no test coverage detected