When I want to update my acceleration structure per frame like this:
I use buildAccel() in initialization and use updateAccel() in a render loop, but it will error the last sentence of code CUDA_SYNC_CHECK();
0x0000029f62ea68f0 "CUDA error on synchronize with error ‘an illegal memory access was encountered’ "
I checked the values of parameters of two optixAccelBuild() should be the same.
So I’m wondering what went wrong with my understanding that caused this error
Thank you for helping me!!
void USRenderer::buildAccel() {
PING;
numMeshes = scene->worldmodel.size();
PRINT(numMeshes);
vertexBuffer.resize(numMeshes);
indexBuffer.resize(numMeshes);
normalBuffer.resize(numMeshes);
triangleInput.resize(numMeshes);
d_vertices.resize(numMeshes);
d_indices.resize(numMeshes);
triangleInputFlags.resize(numMeshes);
for (int meshID = 0; meshID < numMeshes; meshID++) {
TriangleMesh& mesh = *scene->worldmodel[meshID];
vertexBuffer[meshID].alloc_and_upload(mesh.vertex);
indexBuffer[meshID].alloc_and_upload(mesh.index);
if (!mesh.normal.empty())
normalBuffer[meshID].alloc_and_upload(mesh.normal);
triangleInput[meshID] = {};
triangleInput[meshID].type
= OPTIX_BUILD_INPUT_TYPE_TRIANGLES;
d_vertices[meshID] = vertexBuffer[meshID].d_pointer();
d_indices[meshID] = indexBuffer[meshID].d_pointer();
triangleInput[meshID].triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3;
triangleInput[meshID].triangleArray.vertexStrideInBytes = sizeof(vec3f);
triangleInput[meshID].triangleArray.numVertices = (int)mesh.vertex.size();
triangleInput[meshID].triangleArray.vertexBuffers = &d_vertices[meshID];
triangleInput[meshID].triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3;
triangleInput[meshID].triangleArray.indexStrideInBytes = sizeof(vec3i);
triangleInput[meshID].triangleArray.numIndexTriplets = (int)mesh.index.size();
triangleInput[meshID].triangleArray.indexBuffer = d_indices[meshID];
triangleInputFlags[meshID] = 0;
triangleInput[meshID].triangleArray.flags = &triangleInputFlags[meshID];
triangleInput[meshID].triangleArray.numSbtRecords = 1;
triangleInput[meshID].triangleArray.sbtIndexOffsetBuffer = 0;
triangleInput[meshID].triangleArray.sbtIndexOffsetSizeInBytes = 0;
triangleInput[meshID].triangleArray.sbtIndexOffsetStrideInBytes = 0;
}
accelOptions.buildFlags = OPTIX_BUILD_FLAG_ALLOW_COMPACTION | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
accelOptions.motionOptions.numKeys = 1;
accelOptions.operation = OPTIX_BUILD_OPERATION_BUILD;
OptixAccelBufferSizes blasBufferSizes;
OPTIX_CHECK(optixAccelComputeMemoryUsage
(optixContext,
&accelOptions,
triangleInput.data(),
(int)numMeshes, // num_build_inputs
&blasBufferSizes
));
CUDABuffer compactedSizeBuffer;
compactedSizeBuffer.alloc(sizeof(uint64_t));
emitDesc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
emitDesc.result = compactedSizeBuffer.d_pointer();
tempBuffer.alloc(blasBufferSizes.tempSizeInBytes);
outputBuffer.alloc(blasBufferSizes.outputSizeInBytes);
OPTIX_CHECK(optixAccelBuild(
optixContext,
stream,
&accelOptions,
triangleInput.data(),
(int)numMeshes,
tempBuffer.d_pointer(),
tempBuffer.sizeInBytes,
outputBuffer.d_pointer(),
outputBuffer.sizeInBytes,
&asHandle,
&emitDesc, 1
));
CUDA_SYNC_CHECK();
compactedSizeBuffer.download(&compactedSize, 1);
asBuffer.alloc(compactedSize);
OPTIX_CHECK(optixAccelCompact(optixContext,
stream,
asHandle,
asBuffer.d_pointer(),
asBuffer.sizeInBytes,
&asHandle));
CUDA_SYNC_CHECK();
outputBuffer.free();
tempBuffer.free();
compactedSizeBuffer.free();
tempBuffer.alloc(blasBufferSizes.tempSizeInBytes);
outputBuffer.alloc(blasBufferSizes.outputSizeInBytes);
CUDA_SYNC_CHECK();
}
void USRenderer::updateAccel() {
accelOptionsUpdate.buildFlags = OPTIX_BUILD_FLAG_ALLOW_COMPACTION | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
accelOptionsUpdate.operation = OPTIX_BUILD_OPERATION_UPDATE;
std::cout << "vertexBuffers4: " << int(triangleInput.data()->triangleArray.vertexBuffers[0]) << std::endl;
OPTIX_CHECK(optixAccelBuild(
optixContext,
stream,
&accelOptionsUpdate,
triangleInput.data(),
(int)numMeshes,
tempBuffer.d_pointer(),
tempBuffer.sizeInBytes,
outputBuffer.d_pointer(),
outputBuffer.sizeInBytes,
&asHandle,
&emitDesc, 1
));
CUDA_SYNC_CHECK();
std::cout << "update success" << std::endl;
}