RenderKit / embree

Embree ray tracing kernels repository.
Apache License 2.0
2.37k stars 389 forks source link

Explanation on ISA selector #425

Closed jjcasmar closed 1 year ago

jjcasmar commented 1 year ago

I have a test application where I create a 3D mesh and submitted as an embree geometry. My vertex buffer is 16 byte aligned.

  data.geometry = rtcNewGeometry(data.device, RTC_GEOMETRY_TYPE_TRIANGLE);
  rtcSetGeometryBuildQuality(data.geometry, RTC_BUILD_QUALITY_HIGH);

  auto geomId = rtcAttachGeometry(data.scene, data.geometry);

  auto *vb = static_cast<float *>(rtcSetNewGeometryBuffer(
      data.geometry, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3,
      4 * sizeof(float), points.size()));

  for (int i = 0; i < points.size(); ++i) {
    vb[4 * i + 0] = points[i].x();
    vb[4 * i + 1] = points[i].y();
    vb[4 * i + 2] = points[i].z();
  }

  auto *ib = static_cast<unsigned int *>(rtcSetNewGeometryBuffer(
      data.geometry, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3,
      3 * sizeof(unsigned int), triangles.size()));
  for (int i = 0; i < triangles.size(); ++i) {
    ib[3 * i + 0] = triangles[i][0];
    ib[3 * i + 1] = triangles[i][1];
    ib[3 * i + 2] = triangles[i][2];
  }

I have set the verbosity of the device to 2 to see which backends is calling, I am having a surprised. When I enable SSE2, SSE42, AVX, AVX2 and AVX512, the BVH construction is selecting AVX backend.

 building BVH8<triangle4v> using avx::BVH8BuilderSAH ...

However, the intersectors select the avx2 backend.

created scene intersector
  accels[0]
    intersector1  = avx2::BVH8Triangle4vIntersector1Pluecker
    intersector4  = avx2::BVH8Triangle4vIntersector4HybridPluecker
    intersector8  = avx2::BVH8Triangle4vIntersector8HybridPluecker
    intersectorN = avx2::BVH8Triangle4vIntersectorStreamPluecker

On the other hand, if I only allow AVX2 backend, then both the BVH builder and the intersectors select AVX2.

I dont understand the logic of selecting AVX for the BVH construction when there is a working AVX2 backend. What is the heuristic for selecting one backend or another?

svenwoop commented 1 year ago

This is expected behavior. We instantiate the BVH builders just up to AVX if you have multiple ISAs selected. There is no performance benefit of using AVX2 for the builder. However, if you just specify one ISA than all code is compiled with that ISA, that is why you see builder using AVX2 in that case.