shocker-0x15 / OptiX_Utility

OptiX 8 Lightweight Wrapper Library
Other
102 stars 9 forks source link

latest version does not compile #9

Closed Hurleyworks closed 4 years ago

Hurleyworks commented 4 years ago

Hi again :)

The latest GitHub version does not compile because of an error on line 785 of optix_util.cpp. Looks like getName() returns a const char* instead of a std::string;

I see you have a new RTX 3080 now. I am jealous. :)

I also had a question about object replacement. I'm trying to do a fluid simulation where a new fluid mesh is constantly generated. Right now I am just creating new vertex buffers and triangle buffers and rebuilding the GAS, the hitgroupSBT and the top level IAS. Can you tell me if that's the best way to do object replacement? I am getting okay results but I'm wondering if performance can be improved. https://www.youtube.com/watch?v=dXsj5O6w0no&ab_channel=HurleyworksInc

  OptixVertices newVertices;
    uint32_t vertexCount = addVertices (newMesh, newVertices);

    OptixTriangles newTriangles;
    uint32_t triangleCount = addAllTriangles (newMesh, newTriangles);

    uint32_t geoGroupID = node->getClientID();
    GeometryGroupRef& geomGroup = state->geomGroups.at (geoGroupID);

    VertexBufferRef vertexBuffer = make_shared_with_deleter<cudau::TypedBuffer<Shared::Vertex>> (
        [] (cudau::TypedBuffer<Shared::Vertex>* p) {
            p->finalize();
        });
    vertexBuffer->initialize (state->cuContext, g_bufferType, newVertices);

    GeometryInstanceRef& geoInst = geomGroup->geomInsts[0];
    geoInst->vertexBuffer->finalize();
    geoInst->vertexBuffer = vertexBuffer;

    geoInst->triangleBuffer.finalize();
    geoInst->triangleBuffer.initialize (state->cuContext, g_bufferType, newTriangles, state->cuStream);

    geoInst->optixGeomInst.setVertexBuffer (*vertexBuffer);
    geoInst->optixGeomInst.setTriangleBuffer (geoInst->triangleBuffer);

    Shared::GeometryData geomData = {};
    geomData.vertexBuffer = vertexBuffer->getDevicePointer();
    geomData.triangleBuffer = geoInst->triangleBuffer.getDevicePointer();
    geoInst->optixGeomInst.setUserData (geomData);

    geomGroup->optixGAS.markDirty();
shocker-0x15 commented 4 years ago

Oh, thanks for the report! It failed in asserts which are removed in Release build. Maybe it's time to write a test for this library.

RTX 3080 also helps to warm my room :D

I'll check your question later.

shocker-0x15 commented 4 years ago
shocker-0x15 commented 4 years ago

Hit group SBT layout/size is determined by

So SBT layout invalidation is not required in theory even in the case where you change both vertex/triangle buffer and need to call markDirty() to rebuild a GAS. However the current implementation of the utility always invalidates SBT layout when markDirty() is called for simplicity. It is maybe worth exposing a variant of markDirty() without invalidating the SBT layout.

But if rebuilding SBT is not a heavy task, this is not a problem in the first place.

Hurleyworks commented 4 years ago

Thanks for the help! The vertex/triangle data is generated on the CPU using OpenVDB. I'm emitting fluids so the vertex and triangle counts are usually different in each incoming mesh.

I tried pre-allocating vertex and triangle buffers and the updating them with the new data when a new fluid mesh arrives but I am getting rendering errors. Do you know what might cause this.? I have tried updating the fluid buffers only after rendering and cuStreamSynchronize () but still get the rendering artefacts..

const uint32_t DEFAULT_FLUID_VERTICES = 60000;
const uint32_t DEFAULT_FLUID_TRIANGLES = 120000;

//------------- create fluid mesh
    VertexBufferRef vertexBuffer = make_shared_with_deleter<cudau::TypedBuffer<Shared::Vertex>> (
        [] (cudau::TypedBuffer<Shared::Vertex>* p) {
            p->finalize();
        });

    OptixVertices maxVertices (DEFAULT_FLUID_VERTICES, Shared::Vertex{});
    std::memcpy (maxVertices.data(), vertices.data(), vertices.size() * sizeof (Shared::Vertex));

    vertexBuffer->initialize (state->cuContext, cudau::BufferType::Device, maxVertices);

    OptixTriangles maxTriangle (DEFAULT_FLUID_TRIANGLES, Shared::Triangle{});
    std::memcpy (maxTriangle.data(), triangles.data(), triangles.size() * sizeof (Shared::Triangle));

    GeometryInstanceRef geomInst = make_shared_with_deleter<GeometryInstance> (GeometryInstance::finalize);
    geomInst->vertexBuffer = vertexBuffer;

    geomInst->triangleBuffer.initialize (state->cuContext, cudau::BufferType::Device, maxTriangle, state->cuStream);
    geomInst->optixGeomInst.setVertexBuffer (*vertexBuffer);
    geomInst->optixGeomInst.setTriangleBuffer (geomInst->triangleBuffer);

    Shared::GeometryData geomData = {};
    geomData.vertexBuffer = vertexBuffer->getDevicePointer();
    geomData.triangleBuffer = geomInst->triangleBuffer.getDevicePointer();
    geomInst->optixGeomInst.setUserData (geomData);

// -----------update fluid mesh
    GeometryInstanceRef& geoInst = geomGroup->geomInsts[0];

    Shared::Vertex* const verts = geoInst->vertexBuffer->map();
    std::memcpy (verts, newVertices.data(), newVertices.size() * sizeof (Shared::Vertex));
    geoInst->vertexBuffer->unmap();

    Shared::Triangle* const tris = geoInst->triangleBuffer.map();
    std::memcpy (tris, newTriangles.data(), newTriangles.size() * sizeof (Shared::Triangle));
    geoInst->triangleBuffer.unmap();

    geomGroup->optixGAS.markDirty();

render_error

shocker-0x15 commented 4 years ago

GeometryInstance takes BufferView, not cudau::Buffer (There is implicit conversion defined in optixu_on_cudau you might be using). When updating vertices/indices, you need to update the number of vertices/indices as well. In this case, you need to set vertex/index buffer again with the current number of vertices/indices.

Hurleyworks commented 4 years ago

Excellent. That fixed the rendering errors. Thanks very much for the help!