Repository: google/filament
File: filament/src/details/Scene.cpp (C++)

Code before:
            //        we need a better way to handle this
            const mat4f& transform = tcm.getTransform(ti);
            float const scale = (length(transform[0].xyz) + length(transform[1].xyz) +
                                 length(transform[2].xyz)) / 3.0f;

            size_t const index = std::distance(first, p) + i;
            assert_invariant(index < sceneData.size());

            sceneData.elementAt<RENDERABLE_INSTANCE>(index) = ri;
            sceneData.elementAt<WORLD_TRANSFORM>(index)     = shaderWorldTransform;
            sceneData.elementAt<VISIBILITY_STATE>(index)    = visibility;
            sceneData.elementAt<SKINNING_BUFFER>(index)     = rcm.getSkinningBufferInfo(ri);
            sceneData.elementAt<MORPHING_BUFFER>(index)     = rcm.getMorphingBufferInfo(ri);
            sceneData.elementAt<INSTANCES>(index)           = rcm.getInstancesInfo(ri);
            sceneData.elementAt<WORLD_AABB_CENTER>(index)   = worldAABB.center;
            sceneData.elementAt<VISIBLE_MASK>(index)        = 0;
            sceneData.elementAt<CHANNELS>(index)            = rcm.getChannels(ri);
            sceneData.elementAt<LAYERS>(index)              = rcm.getLayerMask(ri);
            sceneData.elementAt<WORLD_AABB_EXTENT>(index)   = worldAABB.halfExtent;
            //sceneData.elementAt<PRIMITIVES>(index)          = {}; // already initialized, Slice<>
            sceneData.elementAt<SUMMED_PRIMITIVE_COUNT>(index) = 0;
            //sceneData.elementAt<UBO>(index)                 = {}; // not needed here
            sceneData.elementAt<USER_DATA>(index)           = scale;

            auto skinning = rcm.getSkinning(ri);
            sceneData.elementAt<SKINNING_STATE>(index) = skinning;
        }
    };

    auto lightWork = [first = lightInstances.data(), &lcm, &tcm, &worldTransform,
            &lightData](auto* p, auto c) {
        FILAMENT_TRACING_NAME(FILAMENT_TRACING_CATEGORY_FILAMENT, "lightWork");
        for (size_t i = 0; i < c; i++) {
            auto [li, ti] = p[i];
            // this is where we go from double to float for our transforms
            mat4f const shaderWorldTransform{
                    worldTransform * tcm.getWorldTransformAccurate(ti) };
            float4 const position = shaderWorldTransform * float4{ lcm.getLocalPosition(li), 1 };
            float3 d = 0;
            if (!lcm.isPointLight(li) || lcm.isIESLight(li)) {
                d = lcm.getLocalDirection(li);
                // using mat3f::getTransformForNormals handles non-uniform scaling
                d = normalize(mat3f::getTransformForNormals(shaderWorldTransform.upperLeft()) * d);
            }
            size_t const index = DIRECTIONAL_LIGHTS_COUNT + std::distance(first, p) + i;
            assert_invariant(index < lightData.size());
            lightData.elementAt<POSITION_RADIUS>(index) = float4{ position.xyz, lcm.getRadius(li) };
            lightData.elementAt<DIRECTION>(index) = d;
            lightData.elementAt<LIGHT_INSTANCE>(index) = li;
        }
    };

Review comment (suggestion):
it should work to write:
```
sceneData.elementAt<SKINNING_STATE>(index) = rcm.getSkinning(ri);
```


Code after:
            //        we need a better way to handle this
            const mat4f& transform = tcm.getTransform(ti);
            float const scale = (length(transform[0].xyz) + length(transform[1].xyz) +
                                 length(transform[2].xyz)) / 3.0f;

            size_t const index = std::distance(first, p) + i;
            assert_invariant(index < sceneData.size());

            sceneData.elementAt<RENDERABLE_INSTANCE>(index) = ri;
            sceneData.elementAt<WORLD_TRANSFORM>(index)     = shaderWorldTransform;
            sceneData.elementAt<VISIBILITY_STATE>(index)    = visibility;
            sceneData.elementAt<SKINNING_BUFFER>(index)     = rcm.getSkinningBufferInfo(ri);
            sceneData.elementAt<MORPHING_BUFFER>(index)     = rcm.getMorphingBufferInfo(ri);
            sceneData.elementAt<INSTANCES>(index)           = rcm.getInstancesInfo(ri);
            sceneData.elementAt<WORLD_AABB_CENTER>(index)   = worldAABB.center;
            sceneData.elementAt<VISIBLE_MASK>(index)        = 0;
            sceneData.elementAt<CHANNELS>(index)            = rcm.getChannels(ri);
            sceneData.elementAt<LAYERS>(index)              = rcm.getLayerMask(ri);
            sceneData.elementAt<WORLD_AABB_EXTENT>(index)   = worldAABB.halfExtent;
            //sceneData.elementAt<PRIMITIVES>(index)          = {}; // already initialized, Slice<>
            sceneData.elementAt<SUMMED_PRIMITIVE_COUNT>(index) = 0;
            //sceneData.elementAt<UBO>(index)                 = {}; // not needed here
            sceneData.elementAt<USER_DATA>(index)           = scale;


            sceneData.elementAt<SKINNING_STATE>(index) = rcm.getSkinning(ri);
        }
    };

    auto lightWork = [first = lightInstances.data(), &lcm, &tcm, &worldTransform,
            &lightData](auto* p, auto c) {
        FILAMENT_TRACING_NAME(FILAMENT_TRACING_CATEGORY_FILAMENT, "lightWork");
        for (size_t i = 0; i < c; i++) {
            auto [li, ti] = p[i];
            // this is where we go from double to float for our transforms
            mat4f const shaderWorldTransform{
                    worldTransform * tcm.getWorldTransformAccurate(ti) };
            float4 const position = shaderWorldTransform * float4{ lcm.getLocalPosition(li), 1 };
            float3 d = 0;
            if (!lcm.isPointLight(li) || lcm.isIESLight(li)) {
                d = lcm.getLocalDirection(li);
                // using mat3f::getTransformForNormals handles non-uniform scaling
                d = normalize(mat3f::getTransformForNormals(shaderWorldTransform.upperLeft()) * d);
            }
            size_t const index = DIRECTIONAL_LIGHTS_COUNT + std::distance(first, p) + i;
            assert_invariant(index < lightData.size());
            lightData.elementAt<POSITION_RADIUS>(index) = float4{ position.xyz, lcm.getRadius(li) };
            lightData.elementAt<DIRECTION>(index) = d;
            lightData.elementAt<LIGHT_INSTANCE>(index) = li;
        }
    };