| 1389 | }; |
| 1390 | |
| 1391 | bool CompressTextureSetWithTargetPSNR(ntc::IContext* context, ntc::ITextureSet* textureSet) |
| 1392 | { |
| 1393 | ntc::Status ntcStatus; |
| 1394 | |
| 1395 | ntc::AdaptiveCompressionSessionWrapper session(context); |
| 1396 | ntcStatus = context->CreateAdaptiveCompressionSession(session.ptr()); |
| 1397 | CHECK_NTC_RESULT("CreateAdaptiveCompressionSession") |
| 1398 | |
| 1399 | float const targetPsnr = g_options.targetPsnr; |
| 1400 | float const maxBitsPerPixel = std::isnan(g_options.maxBitsPerPixel) ? 0.f : g_options.maxBitsPerPixel; |
| 1401 | ntcStatus = session->Reset(targetPsnr, maxBitsPerPixel); |
| 1402 | CHECK_NTC_RESULT("Reset") |
| 1403 | |
| 1404 | printf("Starting search for optimal BPP to achieve %.2f dB PSNR.\n", g_options.targetPsnr); |
| 1405 | |
| 1406 | int experimentCount = 0; |
| 1407 | std::vector<AdaptiveSearchResult> results; |
| 1408 | |
| 1409 | while (!session->Finished()) |
| 1410 | { |
| 1411 | float bitsPerPixel; |
| 1412 | ntc::LatentShape latentShape; |
| 1413 | session->GetCurrentPreset(&bitsPerPixel, &latentShape); |
| 1414 | |
| 1415 | printf("Experiment %d: %.2f bpp...\n", experimentCount + 1, bitsPerPixel); |
| 1416 | |
| 1417 | ntcStatus = textureSet->SetLatentShape(latentShape); |
| 1418 | CHECK_NTC_RESULT(SetLatentShape) |
| 1419 | |
| 1420 | float psnr = NAN; |
| 1421 | if (!CompressTextureSet(context, textureSet, &psnr)) |
| 1422 | return false; |
| 1423 | |
| 1424 | // Store the compression result |
| 1425 | AdaptiveSearchResult result; |
| 1426 | result.bitsPerPixel = bitsPerPixel; |
| 1427 | result.latentShape = latentShape; |
| 1428 | result.psnr = psnr; |
| 1429 | |
| 1430 | // Save the compressed data to an in-memory vector |
| 1431 | size_t bufferSize = textureSet->GetOutputStreamSize(); |
| 1432 | result.compressedData.resize(bufferSize); |
| 1433 | ntcStatus = textureSet->SaveToMemory(result.compressedData.data(), &bufferSize); |
| 1434 | CHECK_NTC_RESULT(SaveToMemory) |
| 1435 | |
| 1436 | // Trim the buffer to the actual size of the saved data |
| 1437 | result.compressedData.resize(bufferSize); |
| 1438 | |
| 1439 | results.push_back(std::move(result)); |
| 1440 | |
| 1441 | session->Next(psnr); |
| 1442 | ++experimentCount; |
| 1443 | } |
| 1444 | |
| 1445 | // Get and validate the index of the final result |
| 1446 | int finalIndex = session->GetIndexOfFinalRun(); |
| 1447 | if (finalIndex < 0 || finalIndex >= int(results.size())) |
| 1448 | { |
no test coverage detected