| 50 | } |
| 51 | |
| 52 | void HitInfo::init(const Scene& scene, bool useCompression) |
| 53 | { |
| 54 | // Setup bit allocations for encoding the hit information. |
| 55 | // By default the shader code will use a 128-bit format. |
| 56 | // If compression is requested and the hit info is small enough, a 64-bit format is used instead. |
| 57 | |
| 58 | uint32_t typeCount = (uint32_t)HitType::Count; |
| 59 | mTypeBits = allocateBits(typeCount); |
| 60 | |
| 61 | mInstanceIDBits = allocateBits(scene.getGeometryInstanceCount()); |
| 62 | |
| 63 | uint32_t maxPrimitiveCount = 0; |
| 64 | |
| 65 | for (MeshID meshID{ 0 }; meshID.get() < scene.getMeshCount(); ++meshID) |
| 66 | { |
| 67 | uint32_t triangleCount = scene.getMesh(meshID).getTriangleCount(); |
| 68 | maxPrimitiveCount = std::max(maxPrimitiveCount, triangleCount); |
| 69 | } |
| 70 | for (CurveID curveID{ 0 }; curveID.get() < scene.getCurveCount(); ++curveID) |
| 71 | { |
| 72 | uint32_t curveSegmentCount = scene.getCurve(curveID).getSegmentCount(); |
| 73 | maxPrimitiveCount = std::max(maxPrimitiveCount, curveSegmentCount); |
| 74 | } |
| 75 | |
| 76 | mPrimitiveIndexBits = allocateBits(maxPrimitiveCount); |
| 77 | for (SdfGridID sdfID{ 0 } ; sdfID.get() < scene.getSDFGridCount(); ++sdfID) |
| 78 | { |
| 79 | uint32_t sdfGridMaxPrimitiveIDBits = scene.getSDFGrid(sdfID)->getMaxPrimitiveIDBits(); |
| 80 | mPrimitiveIndexBits = std::max(mPrimitiveIndexBits, sdfGridMaxPrimitiveIDBits); |
| 81 | } |
| 82 | |
| 83 | // Check that the final bit allocation fits. |
| 84 | if (mPrimitiveIndexBits > 32 || (mTypeBits + mInstanceIDBits) > 32) |
| 85 | { |
| 86 | FALCOR_THROW("Scene requires > 64 bits for encoding hit info header. This is currently not supported."); |
| 87 | } |
| 88 | |
| 89 | // Compute size of compressed header in bits. |
| 90 | const uint32_t compressedHeaderBits = kCompressedTypeBits + mInstanceIDBits + mPrimitiveIndexBits; |
| 91 | |
| 92 | // Check if compression is supported (small header and triangle meshes only). |
| 93 | const bool compressionSupported = compressedHeaderBits <= 32 && scene.getGeometryTypes() == Scene::GeometryTypeFlags::TriangleMesh; |
| 94 | |
| 95 | // Use compression if supported and requested. |
| 96 | mUseCompression = compressionSupported && useCompression; |
| 97 | |
| 98 | // Switch to using fewer bits for the type if compression is used. |
| 99 | if (mUseCompression) mTypeBits = kCompressedTypeBits; |
| 100 | |
| 101 | logInfo( |
| 102 | "HitInfo: Total size is {} bits (type: {} bits, instanceID: {} bits, primitiveIndex: {} bits)", |
| 103 | mUseCompression ? 64 : 128, mTypeBits, mInstanceIDBits, mPrimitiveIndexBits |
| 104 | ); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | DefineList HitInfo::getDefines() const |
| 109 | { |
no test coverage detected