RenderKit / embree

Embree ray tracing kernels repository.
Apache License 2.0
2.32k stars 383 forks source link

embree for emscripten fails to detect instersection #451

Open jjcasmar opened 1 year ago

jjcasmar commented 1 year ago

I am testing embree 4.1.0 with emscripten

I have created a basic test, with one triangle and a single ray

TEST(embree, basic)
{
    RTCDevice device{rtcNewDevice("threads=1")};
    RTCScene scene = rtcNewScene(device);
    RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);

    float *vb = reinterpret_cast<float *>(
        rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), 4));
    vb[0] = 0.f;
    vb[1] = 0.f;
    vb[2] = 0.f;  // 1st vertex
    vb[3] = 1.f;
    vb[4] = 0.f;
    vb[5] = 0.f;  // 2nd vertex
    vb[6] = 0.f;
    vb[7] = 1.f;
    vb[8] = 0.f;  // 3rd vertex

    unsigned *ib = reinterpret_cast<unsigned int *>(
        rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(unsigned), 1));
    ib[0] = 0;
    ib[1] = 1;
    ib[2] = 2;

    rtcCommitGeometry(geom);
    rtcAttachGeometry(scene, geom);
    rtcReleaseGeometry(geom);
    rtcCommitScene(scene);

    RTCRayHit rayhit;
    rayhit.ray.org_x = 0.2f;
    rayhit.ray.org_y = 0.2f;
    rayhit.ray.org_z = -1.f;
    rayhit.ray.dir_x = 0.f;
    rayhit.ray.dir_y = 0.f;
    rayhit.ray.dir_z = 1.f;
    rayhit.ray.tnear = 0.f;
    rayhit.ray.tfar = std::numeric_limits<float>::infinity();
    rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;

    rtcIntersect1(scene, &rayhit);

    if (rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID) {
        std::cout << "Intersection at t = " << rayhit.ray.tfar << std::endl;
    } else {
        std::cout << "No Intersection" << std::endl;
    }

    rtcReleaseScene(scene);
    rtcReleaseDevice(device);
}

For x86_64, this code runs fine and produces the correct result

[==========] Running 1 test from 1 test suite.                                                                                                                                               
[----------] Global test environment set-up.                                                                                                                                                 
[----------] 1 test from embree                                                                                                                                                              
[ RUN      ] embree.basic                                                                                                                                                                    
Intersection at t = 1                                                                                                                                                                        
[       OK ] embree.basic (0 ms)                                                                                                                                                             
[----------] 1 test from embree (0 ms total)                                                                                                                                                 

[----------] Global test environment tear-down                                                                                                                                               
[==========] 1 test from 1 test suite ran. (0 ms total)                                                                                                                                      
[  PASSED  ] 1 test. 

However, when I compile embree and this test with emscripten 3.1.20, it doesn't run fine.

[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from embree
[ RUN      ] embree.basic
warning: unsupported syscall: __syscall_madvise

No Intersection
[       OK ] embree.basic (32 ms)
[----------] 1 test from embree (32 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (34 ms total)
[  PASSED  ] 1 test.

I think the error has something to do with the alignment of the vertex and index buffer, but I am not sure how I should fix it. Any suggestion?

jjcasmar commented 1 year ago

As an update, the code doesn't run fine in x86_64. Sometimes it reports the correct intersection, but other time reports no intersection.