13
13
namespace nbl ::asset
14
14
{
15
15
16
- class ICPUBottomLevelAccelerationStructure final : public IAsset , public IBottomLevelAccelerationStructure// TODO: sort this out later, public IPreHashed
16
+ class ICPUBottomLevelAccelerationStructure final : public IPreHashed , public IBottomLevelAccelerationStructure
17
17
{
18
18
public:
19
19
static inline bool validBuildFlags (const core::bitflag<BUILD_FLAGS> flags) {return validBuildFlags (flags);}
@@ -152,80 +152,97 @@ class ICPUBottomLevelAccelerationStructure final : public IAsset, public IBottom
152
152
return 0 ;
153
153
}
154
154
155
- inline core::blake3_hash_t computeContentHash () const // TODO: sort this out later, override
155
+ inline core::blake3_hash_t computeContentHash () const override
156
156
{
157
- core::blake3_hasher hasher;
157
+ if (!m_geometryPrimitiveCount)
158
+ return INVALID_HASH;
158
159
const bool isAABB = m_buildFlags.hasFlags (BUILD_FLAGS::GEOMETRY_TYPE_IS_AABB_BIT);
160
+ core::blake3_hasher hasher;
159
161
hasher << isAABB;
160
- if (m_geometryPrimitiveCount)
162
+ auto countIt = m_geometryPrimitiveCount->begin ();
163
+ if (isAABB)
161
164
{
162
- auto countIt = m_geometryPrimitiveCount->begin ();
163
- if (isAABB)
165
+ for (const auto & aabb : *m_AABBGeoms)
164
166
{
165
- for ( const auto & aabb : *m_AABBGeoms)
166
- {
167
- const auto count = *(countIt++) ;
168
- hasher << count;
169
- hasher << aabb. geometryFlags ;
170
- hasher << aabb.stride ;
171
- const auto begin = reinterpret_cast < const uint8_t *>( aabb.data . buffer -> getPointer ())+aabb. data . offset ;
172
- const auto end = begin +aabb.stride *count ;
173
- for ( auto it=begin; it< end; it+= aabb.stride )
174
- hasher. update (it, sizeof (AABB_t));
175
- }
167
+ const void * data = aabb. data . isValid () ? aabb. data . buffer -> getPointer (): nullptr ;
168
+ if (!data)
169
+ return INVALID_HASH ;
170
+ const auto count = *(countIt++) ;
171
+ hasher << count ;
172
+ hasher << aabb.geometryFlags ;
173
+ hasher << aabb.stride ;
174
+ const auto begin = reinterpret_cast < const uint8_t *>(data) +aabb.data . offset ;
175
+ const auto end = begin+ aabb.stride *count;
176
+ for ( auto it=begin; it<end; it+=aabb. stride )
177
+ hasher. update (it, sizeof (AABB_t));
176
178
}
177
- else
179
+ }
180
+ else
181
+ {
182
+ for (const auto & triangles : *m_triangleGeoms)
178
183
{
179
- for (const auto & triangles : *m_triangleGeoms)
184
+ //
185
+ const size_t vertexSize = getTexelOrBlockBytesize (triangles.vertexFormat );
186
+ if (vertexSize==0 )
187
+ return INVALID_HASH;
188
+ //
189
+ const uint8_t * verticesA = triangles.vertexData [0 ].isValid () ? reinterpret_cast <const uint8_t *>(triangles.vertexData [0 ].buffer ->getPointer ()):nullptr ;
190
+ if (!verticesA)
191
+ return INVALID_HASH;
192
+ verticesA += triangles.vertexData [0 ].offset ;
193
+ //
194
+ const uint8_t * indices = nullptr ;
195
+ if (triangles.indexType !=E_INDEX_TYPE::EIT_UNKNOWN)
180
196
{
181
- const auto count = *(countIt++);
182
- const auto indexCount = count*3 ;
183
- hasher << count;
184
- hasher << triangles.transform ;
185
- hasher << triangles.maxVertex ;
186
- hasher << triangles.vertexStride ;
187
- hasher << triangles.vertexFormat ;
188
- hasher << triangles.indexType ;
189
- hasher << triangles.geometryFlags ;
190
- // now hash the triangle data
191
- const bool usesMotion = triangles.vertexData [1 ].isValid ();
192
- const size_t vertexSize = getTexelOrBlockBytesize (triangles.vertexFormat );
193
- const auto indices = reinterpret_cast <const uint8_t *>(triangles.indexData .buffer ->getPointer ())+triangles.indexData .offset ;
194
- const auto verticesA = reinterpret_cast <const uint8_t *>(triangles.vertexData [0 ].buffer ->getPointer ())+triangles.vertexData [0 ].offset ;
195
- const uint8_t * verticesB = nullptr ;
196
- if (usesMotion)
197
- verticesB = reinterpret_cast <const uint8_t *>(triangles.vertexData [1 ].buffer ->getPointer ())+triangles.vertexData [1 ].offset ;
198
- auto hashIndices = [&]<typename IndexType>() -> void
199
- {
200
- for (auto i=0 ; i<indexCount; i++)
201
- {
202
- uint32_t vertexIndex = i;
203
- if constexpr (std::is_integral_v<IndexType>)
204
- vertexIndex = reinterpret_cast <const IndexType*>(indices)[i];
205
- hasher.update (verticesA+vertexIndex*triangles.vertexStride ,vertexSize);
206
- if (usesMotion)
207
- hasher.update (verticesB+vertexIndex*triangles.vertexStride ,vertexSize);
208
- }
209
- };
210
- switch (triangles.indexType )
197
+ indices = triangles.indexData .isValid () ? reinterpret_cast <const uint8_t *>(triangles.indexData .buffer ->getPointer ()):nullptr ;
198
+ if (!indices)
199
+ return INVALID_HASH;
200
+ indices += triangles.indexData .offset ;
201
+ }
202
+ const auto count = *(countIt++);
203
+ const auto indexCount = count*3 ;
204
+ hasher << count;
205
+ hasher << triangles.transform ;
206
+ hasher << triangles.maxVertex ;
207
+ hasher << triangles.vertexStride ;
208
+ hasher << triangles.vertexFormat ;
209
+ hasher << triangles.indexType ;
210
+ hasher << triangles.geometryFlags ;
211
+ // now hash the triangle data
212
+ const bool usesMotion = triangles.vertexData [1 ].isValid ();
213
+ const uint8_t * verticesB = nullptr ;
214
+ if (usesMotion)
215
+ verticesB = reinterpret_cast <const uint8_t *>(triangles.vertexData [1 ].buffer ->getPointer ())+triangles.vertexData [1 ].offset ;
216
+ auto hashIndices = [&]<typename IndexType>() -> void
217
+ {
218
+ for (auto i=0 ; i<indexCount; i++)
211
219
{
212
- case E_INDEX_TYPE::EIT_16BIT:
213
- hashIndices.operator ()<uint16_t >();
214
- break ;
215
- case E_INDEX_TYPE::EIT_32BIT:
216
- hashIndices.operator ()<uint32_t >();
217
- break ;
218
- default :
219
- hashIndices.operator ()<void >();
220
- break ;
220
+ uint32_t vertexIndex = i;
221
+ if constexpr (std::is_integral_v<IndexType>)
222
+ vertexIndex = reinterpret_cast <const IndexType*>(indices)[i];
223
+ hasher.update (verticesA+vertexIndex*triangles.vertexStride ,vertexSize);
224
+ if (usesMotion)
225
+ hasher.update (verticesB+vertexIndex*triangles.vertexStride ,vertexSize);
221
226
}
227
+ };
228
+ switch (triangles.indexType )
229
+ {
230
+ case E_INDEX_TYPE::EIT_16BIT:
231
+ hashIndices.operator ()<uint16_t >();
232
+ break ;
233
+ case E_INDEX_TYPE::EIT_32BIT:
234
+ hashIndices.operator ()<uint32_t >();
235
+ break ;
236
+ default :
237
+ hashIndices.operator ()<void >();
238
+ break ;
222
239
}
223
240
}
224
241
}
225
242
return static_cast <core::blake3_hash_t >(hasher);
226
243
}
227
244
228
- inline bool missingContent () const // TODO: sort this out later, override
245
+ inline bool missingContent () const override
229
246
{
230
247
return !m_triangleGeoms && !m_AABBGeoms && !m_geometryPrimitiveCount;
231
248
}
@@ -236,29 +253,28 @@ class ICPUBottomLevelAccelerationStructure final : public IAsset, public IBottom
236
253
inline IAsset* getDependant_impl (const size_t ix) override
237
254
{
238
255
const ICPUBuffer* buffer = nullptr ;
239
- if (m_geometryPrimitiveCount)
256
+ // `ix` is always less than `getDependantCount()`
257
+ assert (m_geometryPrimitiveCount);
258
+ if (m_buildFlags.hasFlags (BUILD_FLAGS::GEOMETRY_TYPE_IS_AABB_BIT))
259
+ buffer = m_AABBGeoms ? m_AABBGeoms->operator [](ix).data .buffer .get ():nullptr ;
260
+ else if (m_triangleGeoms)
240
261
{
241
- if (m_buildFlags.hasFlags (BUILD_FLAGS::GEOMETRY_TYPE_IS_AABB_BIT))
242
- buffer = m_AABBGeoms ? m_AABBGeoms->operator [](ix).data .buffer .get ():nullptr ;
243
- else if (m_triangleGeoms)
262
+ const auto geomCount = m_triangleGeoms->size ();
263
+ const auto subResourceIx = ix/geomCount;
264
+ const auto & triangles = m_triangleGeoms->operator [](ix-subResourceIx*geomCount);
265
+ switch (subResourceIx)
244
266
{
245
- const auto geomCount = m_triangleGeoms->size ();
246
- const auto subResourceIx = ix/geomCount;
247
- const auto & triangles = m_triangleGeoms->operator [](ix-subResourceIx*geomCount);
248
- switch (subResourceIx)
249
- {
250
- case 0 :
251
- buffer = triangles.indexData .buffer .get ();
252
- break ;
253
- case 1 :
254
- buffer = triangles.vertexData [0 ].buffer .get ();
255
- break ;
256
- case 2 :
257
- buffer = triangles.vertexData [1 ].buffer .get ();
258
- break ;
259
- default :
260
- break ;
261
- }
267
+ case 0 :
268
+ buffer = triangles.indexData .buffer .get ();
269
+ break ;
270
+ case 1 :
271
+ buffer = triangles.vertexData [0 ].buffer .get ();
272
+ break ;
273
+ case 2 :
274
+ buffer = triangles.vertexData [1 ].buffer .get ();
275
+ break ;
276
+ default :
277
+ break ;
262
278
}
263
279
}
264
280
return const_cast <ICPUBuffer*>(buffer);
0 commit comments