Przeglądaj źródła

Batched blocks for projects with live controllers: two task-graph rounds per block (round 190lv)

Round 190kz batched a block cut in pieces into one task-graph round but kept projects with live
controllers (Peak / Formula / envelope controllers, RackState::liveAutomations) piece by piece:
piece k+1's updateAutomation reads each controller's output after piece k. Now such a block runs
every piece's serial part first, with a placeholder for the live values of pieces >= 1
(deferLive, Piece::liveDeferred), then two rounds: round A = the sources and their upstream closure
(liveInA), each source writing mapLive (controlOutput) into piece k+1's slot right after rendering
piece k (unless clearOverriddenValues left NaN there); round B = the rest (pending counts without
round A's predecessors, fused chains into round B cut). The serial fallback (A7) writes the values
after each piece's Master, as before.

Exact: every unit gets the same calls, values and order; the controller value is read at the same
point of the source's own calls. buildLivePlan refuses (counted as "live controllers") clips, zone,
lanes, links, modulators, pattern events, tempo, crossfader or VCA on a live slot; a live pitch
bend; a source that is the Master, an empty slot or no task; a round-A unit reading a live value
other than its own source's or an upstream source's. planPieces keeps a block with fewer than 3
pieces (two rounds would save nothing) or a later continuing piece on the old path.
LYNX_POOL live=0 (WorkerPool::Tuning::liveRounds) = the old path for these blocks; rounds=chunk
still means the old path for everything. perfbench: two-round block count and the refusal reason.
Ka1-S 2 tygodni temu
rodzic
commit
a46b7e802d

+ 314 - 12
src/core/RackRenderer.cpp

@@ -2674,6 +2674,13 @@ void RackRenderer::prepare (double sr, int maxBlockSize)
     unitParamStart.assign ((std::size_t) maxChannels + maxMixerTracks + 1, 0);
     unitParams.assign (4096, 0);
     unitParamsState = nullptr;
+    // round 190lv: the live controllers' two rounds (built with the bindings, same rule)
+    liveInA.assign ((std::size_t) graphCapacity, 0);
+    liveSourceStart.assign ((std::size_t) graphCapacity + 1, 0);
+    liveSources.assign ((std::size_t) RackState::maxAutomations, 0);
+    liveRootsA.assign ((std::size_t) graphCapacity, 0);
+    liveRootsB.assign ((std::size_t) graphCapacity, 0);
+    livePendingB.assign ((std::size_t) graphCapacity, 0);
     zeroBlock.assign ((std::size_t) blockCapacity, 0.0f);
     hwScratch.assign (2 * (std::size_t) blockCapacity, 0.0f);
     layerPick.assign ((std::size_t) maxChannels * 128, (std::int16_t) -1);
@@ -2897,15 +2904,19 @@ void RackRenderer::updateAutomation (const RackState& state, double startTick) n
         const auto& src = automation.source;
         if (src.channel >= 0 ? foreignChannel (src.channel) : foreignTrack (src.track))
             continue; // (render-ahead: that unit's renderer works it out; what it drives is that renderer's too)
+        // (round 190lv: a batched block's piece >= 1 holds a placeholder here, the source writes the value into the piece's
+        // slot right after it rendered the piece before: runGraphRound / processBatched)
         if (src.channel >= 0)
         {
             if (src.channel < (int) state.instruments.size() && state.instruments[(std::size_t) src.channel] != nullptr)
-                automationValues[(std::size_t) a] = automation.mapLive (state.instruments[(std::size_t) src.channel]->controlOutput (src.output));
+                automationValues[(std::size_t) a] = deferLive ? 0.0f : automation.mapLive (state.instruments[(std::size_t) src.channel]->controlOutput (src.output));
         }
         else if (src.track < (int) state.effects.size() && src.slot >= 0 && src.slot < (int) state.effects[(std::size_t) src.track].size()
             && state.effects[(std::size_t) src.track][(std::size_t) src.slot] != nullptr)
-            automationValues[(std::size_t) a] = automation.mapLive (state.effects[(std::size_t) src.track][(std::size_t) src.slot]->controlOutput (src.output));
+            automationValues[(std::size_t) a] = deferLive ? 0.0f : automation.mapLive (state.effects[(std::size_t) src.track][(std::size_t) src.slot]->controlOutput (src.output));
     }
+    if (deferLive)
+        pieces[(std::size_t) pieceSlot()].liveDeferred = true;
 
     // pattern events: in pattern mode the current pattern's (looped); in song mode those of the pattern clip
     // that covers the position. Elsewhere the value holds.
@@ -4641,8 +4652,10 @@ void RackRenderer::process (const RackState* state,
     const auto batch = batchable (*state, numSamples);
     if (batch && planPieces (*state, playing, startTick, numSamples, tps, chunk, grid))
         processBatched (*state, playing, numSamples, left, right, meters);
-    else if (batch && pieceCount < 0) // (more than maxPieces: pattern events close together)
+    else if (batch && pieceCount == -1) // (more than maxPieces: pattern events close together)
         ++batchRefused[(std::size_t) BatchRefusal::manyPieces];
+    else if (batch && pieceCount == -2) // (round 190lv: a later piece goes on without its live values: piece by piece)
+        ++batchRefused[(std::size_t) BatchRefusal::liveControllers];
     else
     for (int done = 0; done < numSamples;)
     {
@@ -6137,12 +6150,243 @@ void RackRenderer::applyUnitParams (const RackState& state, int unit, const Piec
         applyPluginParam (state, (std::size_t) unitParams[(std::size_t) k], piece.paramsFirst, piece.paramsLive);
 }
 
+// Round 190lv (sched2): can this state's live controllers be batched (two rounds per block, processBatched)? In the
+// per-piece path piece k+1's updateAutomation reads each controller's output after every unit rendered piece k. Batched,
+// the source writes it into piece k+1's slot right after it rendered piece k, in round A; round B and the Master read it
+// later. That is the same value read at the same point of the source's own calls. It is exact only when nothing else
+// reads or writes the slot in a later piece's serial part (the placeholder must stay unseen), and when a round-A unit reads
+// only the live values of its own source or of one upstream of it (any other source's pieces can run after the unit's).
+// Anything else: piece by piece.
+bool RackRenderer::buildLivePlan (const RackState& state) noexcept
+{
+    const auto& song = state.song;
+    const auto channels = std::min<int> ({ (int) state.instruments.size(), (int) song.channels.size(), maxChannels });
+    const auto tracks = std::max<int> (1, (int) song.mixer.size());
+    const auto tasks = channels + tracks - 1;
+    const auto no = [this] (const char* why) noexcept {
+        livePlanWhy = why;
+        return false;
+    };
+    if (state.liveAutomations.empty() || tasks <= 0 || tasks > graphCapacity || (int) liveInA.size() < tasks
+        || (int) liveSourceStart.size() < tasks + 1 || state.liveAutomations.size() > liveSources.size()
+        || (int) state.graphNextStart.size() < tasks + 1 || (int) state.graphFused.size() < tasks)
+        return no ("no graph");
+    std::array<char, RackState::maxAutomations> live {};
+    for (const auto a : state.liveAutomations)
+    {
+        if (a < 0 || a >= RackState::maxAutomations || (std::size_t) a >= song.automations.size())
+            return no ("automation index");
+        live[(std::size_t) a] = 1;
+    }
+    const auto isLive = [&live] (int slot) noexcept { return slot >= 0 && slot < RackState::maxAutomations && live[(std::size_t) slot] != 0; };
+
+    // a slot a later piece's serial part reads or writes besides the live loop: clips, Performance zone, lanes, links,
+    // modulators, pattern events; the tempo, VCAs, the crossfader (their readers are not one unit)
+    for (const auto a : state.liveAutomations)
+    {
+        if (((std::size_t) a < state.automationClips.size() && ! state.automationClips[(std::size_t) a].empty())
+            || ((std::size_t) a < state.zoneAutomations.size() && state.zoneAutomations[(std::size_t) a] != 0)
+            || a == state.tempoAutomation || a == state.crossfaderAutomation)
+            return no ("clips, zone, tempo or crossfader on a live automation");
+        if (std::find (state.vcaLevelAuto.begin(), state.vcaLevelAuto.end(), a) != state.vcaLevelAuto.end())
+            return no ("a live VCA level");
+    }
+    for (const auto& lane : state.laneAutomations)
+    {
+        if (isLive (lane.automation))
+            return no ("a live automation's lane");
+        for (const auto peer : lane.clipPeers)
+            if (isLive (peer))
+                return no ("a live lane peer");
+        for (const auto& held : lane.clipSlots)
+            if (isLive (held.first) || isLive (held.second))
+                return no ("a live lane peer");
+    }
+    for (const auto& link : state.linkSlots)
+        if (isLive (link.automation) || isLive (link.slot))
+            return no ("a link on a live slot");
+    for (const auto& r : state.modRoutes)
+        if (isLive (r.source) || isLive (r.slot))
+            return no ("a modulator on a live slot");
+    for (const auto& lane : state.eventLanes)
+        if (isLive (lane.slot))
+            return no ("pattern events on a live slot");
+
+    const auto trackTask = [channels] (int t) noexcept { return channels + t - 1; };
+    // the sources: a channel's controller instrument or an insert's effect (not the Master: it runs after the rounds)
+    std::fill (liveInA.begin(), liveInA.begin() + tasks, 0);
+    std::fill (liveSourceStart.begin(), liveSourceStart.begin() + tasks + 1, 0);
+    const auto sourceOf = [&] (int a) noexcept -> int {
+        const auto& src = song.automations[(std::size_t) a].source;
+        if (src.channel >= 0)
+            return src.channel < channels && state.instruments[(std::size_t) src.channel] != nullptr ? src.channel : -1;
+        if (src.track <= 0 || src.track >= tracks || src.track >= maxMixerTracks || (std::size_t) src.track >= state.effects.size()
+            || src.slot < 0 || (std::size_t) src.slot >= state.effects[(std::size_t) src.track].size()
+            || state.effects[(std::size_t) src.track][(std::size_t) src.slot] == nullptr)
+            return -1;
+        return trackTask (src.track);
+    };
+    for (const auto a : state.liveAutomations)
+    {
+        const auto node = sourceOf (a);
+        if (node < 0)
+            return no ("a source that is not a task (the Master, an empty slot, a channel past the graph)");
+        liveInA[(std::size_t) node] = 1;
+        ++liveSourceStart[(std::size_t) node + 1];
+    }
+    for (int i = 0; i < tasks; ++i)
+        liveSourceStart[(std::size_t) i + 1] += liveSourceStart[(std::size_t) i];
+    for (const auto a : state.liveAutomations) // (in liveAutomations' order per source; the cursor moves to its end, then back)
+        liveSources[(std::size_t) liveSourceStart[(std::size_t) sourceOf (a)]++] = a;
+    for (int i = tasks; i > 0; --i)
+        liveSourceStart[(std::size_t) i] = liveSourceStart[(std::size_t) i - 1];
+    liveSourceStart[0] = 0;
+
+    // round A: every unit that reaches a source (the upstream closure: its units wait only for each other)
+    for (auto changed = true; changed;)
+    {
+        changed = false;
+        for (int i = 0; i < tasks; ++i)
+            if (liveInA[(std::size_t) i] == 0)
+                for (auto k = state.graphNextStart[(std::size_t) i]; k < state.graphNextStart[(std::size_t) i + 1]; ++k)
+                    if (liveInA[(std::size_t) state.graphNext[(std::size_t) k]] != 0)
+                    {
+                        liveInA[(std::size_t) i] = 1;
+                        changed = true;
+                        break;
+                    }
+    }
+    // the units that read a live value (automated / automatedLevel / applyPluginParam): a channel's volume, pan and
+    // levels adjustment; an insert's fader, pan, effect parameters and mix, the routes into it; plugin bindings. Round B
+    // reads them after round A. A round-A unit reads them right: its own (it writes piece k+1's value after its piece k,
+    // then works on piece k+1) and an upstream source's (that one rendered every piece before the unit starts); any
+    // other source may not have written the value yet.
+    std::array<char, graphCapacity> seen {};
+    std::array<int, graphCapacity> stack {};
+    const auto reaches = [&] (int from, int to) noexcept {
+        std::fill (seen.begin(), seen.begin() + tasks, 0);
+        int depth = 0;
+        stack[(std::size_t) depth++] = from;
+        seen[(std::size_t) from] = 1;
+        while (depth > 0)
+        {
+            const auto at = stack[(std::size_t) --depth];
+            if (at == to)
+                return true;
+            for (auto k = state.graphNextStart[(std::size_t) at]; k < state.graphNextStart[(std::size_t) at + 1]; ++k)
+                if (const auto j = state.graphNext[(std::size_t) k]; seen[(std::size_t) j] == 0)
+                {
+                    seen[(std::size_t) j] = 1;
+                    stack[(std::size_t) depth++] = j;
+                }
+        }
+        return false;
+    };
+    const auto readOk = [&] (int node, int a) noexcept {
+        if (! isLive (a) || liveInA[(std::size_t) node] == 0)
+            return true;
+        const auto source = sourceOf (a);
+        return source == node || reaches (source, node);
+    };
+    const auto lateRead = [&] (int node) noexcept {
+        return no (node < channels ? "a round-A channel reads a live value its source may not have written yet"
+                                   : "a round-A insert reads a live value its source may not have written yet");
+    };
+    for (int c = 0; c < channels; ++c)
+    {
+        const auto at = (std::size_t) c;
+        if ((at < state.channelVolumeAuto.size() && ! readOk (c, state.channelVolumeAuto[at]))
+            || (at < state.channelPanAuto.size() && ! readOk (c, state.channelPanAuto[at]))
+            || (at < state.channelAdjustAuto.size() && (! readOk (c, state.channelAdjustAuto[at][0]) || ! readOk (c, state.channelAdjustAuto[at][1]))))
+            return lateRead (c);
+    }
+    for (int t = 1; t < tracks; ++t)
+    {
+        const auto at = (std::size_t) t;
+        const auto node = trackTask (t);
+        auto ok = (at >= state.mixerVolumeAuto.size() || readOk (node, state.mixerVolumeAuto[at]))
+                  && (at >= state.mixerPanAuto.size() || readOk (node, state.mixerPanAuto[at]));
+        if (at < state.effectParamAuto.size())
+            for (const auto& slot : state.effectParamAuto[at])
+                for (const auto a : slot)
+                    ok = ok && readOk (node, a);
+        if (at < state.effectMixAuto.size())
+            for (const auto a : state.effectMixAuto[at])
+                ok = ok && readOk (node, a);
+        if (at < state.mixerInputs.size())
+            for (const auto& in : state.mixerInputs[at])
+                ok = ok && readOk (node, in.automation);
+        if (! ok)
+            return lateRead (node);
+    }
+    for (const auto& b : state.pluginParams)
+    {
+        if (! isLive (b.automation))
+            continue;
+        if (b.channel >= 0)
+        {
+            if (midictl::isPitchBend (b.param)) // (applyLiveControl's bend: kept piece by piece)
+                return no ("a live pitch bend");
+            if (b.channel < channels && ! readOk (b.channel, b.automation))
+                return lateRead (b.channel);
+        }
+        else if (b.track > 0 && b.track < tracks && ! readOk (trackTask (b.track), b.automation))
+            return lateRead (trackTask (b.track));
+    }
+
+    // the rounds' roots, pending counts and positions (round B's units count only their round-B predecessors; a fused
+    // chain from round A into round B starts round B)
+    liveRootCountA = liveRootCountB = liveCountA = liveCountB = 0;
+    for (int i = 0; i < tasks; ++i)
+        livePendingB[(std::size_t) i] = state.graphIndegree[(std::size_t) i];
+    for (int i = 0; i < tasks; ++i)
+        if (liveInA[(std::size_t) i] != 0)
+            for (auto k = state.graphNextStart[(std::size_t) i]; k < state.graphNextStart[(std::size_t) i + 1]; ++k)
+                if (const auto j = state.graphNext[(std::size_t) k]; liveInA[(std::size_t) j] == 0)
+                    --livePendingB[(std::size_t) j];
+    for (int i = 0; i < tasks; ++i)
+    {
+        const auto inA = liveInA[(std::size_t) i] != 0;
+        const auto fused = state.graphFused[(std::size_t) i];
+        const auto fusedSameRound = fused >= 0 && (liveInA[(std::size_t) fused] != 0) == inA;
+        if (inA)
+        {
+            if (state.graphIndegree[(std::size_t) i] == 0)
+                liveRootsA[(std::size_t) liveRootCountA++] = i;
+            liveCountA += fusedSameRound ? 0 : 1;
+        }
+        else
+        {
+            if (livePendingB[(std::size_t) i] == 0)
+                liveRootsB[(std::size_t) liveRootCountB++] = i;
+            liveCountB += fusedSameRound ? 0 : 1;
+        }
+    }
+    // (each position is one unit handed out: the nodes of the round minus those run inline by their fused predecessor)
+    livePlanWhy = liveCountA > 0 ? "" : "no round A";
+    return liveCountA > 0;
+}
+
+// a controller's value for piece `slot`, as that piece's updateAutomation would have read it (unless the control's own
+// value plays there: clearOverriddenValues left NaN)
+void RackRenderer::writeLiveValue (const RackState& state, int automation, int slot) noexcept
+{
+    auto& value = automationValues.v[(std::size_t) slot][(std::size_t) automation];
+    if (std::isnan (value))
+        return;
+    const auto& a = state.song.automations[(std::size_t) automation];
+    const auto& src = a.source;
+    value = src.channel >= 0 ? a.mapLive (state.instruments[(std::size_t) src.channel]->controlOutput (src.output))
+                             : a.mapLive (state.effects[(std::size_t) src.track][(std::size_t) src.slot]->controlOutput (src.output));
+}
+
 bool RackRenderer::batchable (const RackState& state, int numSamples) noexcept
 {
     const auto refuse = [this] (BatchRefusal why) noexcept {
         ++batchRefused[(std::size_t) why];
         return false;
     };
+    liveBatch = false;
     if (WorkerPool::tuning().blockRounds.load (std::memory_order_relaxed) == 0)
         return refuse (BatchRefusal::switchedOff);
     if (workers == nullptr || workers->workers() == 0 || split != nullptr || ownTimeline != nullptr || buffers == nullptr || ! taskGraphEnabled() || ! state.graphOk)
@@ -6151,8 +6395,8 @@ bool RackRenderer::batchable (const RackState& state, int numSamples) noexcept
         return refuse (BatchRefusal::noGraph);
     if (numSamples > chunkCapacity || numSamples > buffers->frames || numSamples > (int) zeroBlock.size())
         return refuse (BatchRefusal::bigBlock);
-    if (! state.liveAutomations.empty())
-        return refuse (BatchRefusal::liveControllers);
+    if (! state.liveAutomations.empty() && WorkerPool::tuning().liveRounds.load (std::memory_order_relaxed) == 0)
+        return refuse (BatchRefusal::liveControllers); // (round 190lv: LYNX_POOL live=0, piece by piece as before)
     if (state.hasMidiOutputs || ! state.portEventChannels.empty() || ! state.dashboardChannels.empty() || ! state.vfxChannels.empty()
         || state.hasEffectMidiRoutes || ! state.hardwareMidiChannels.empty())
         return refuse (BatchRefusal::midiBetweenUnits);
@@ -6172,6 +6416,7 @@ bool RackRenderer::batchable (const RackState& state, int numSamples) noexcept
     if (&state != unitParamsState || state.serial != unitParamsSerial)
     {
         unitParamsOk = buildUnitParams (state);
+        livePlanOk = unitParamsOk && ! state.liveAutomations.empty() && buildLivePlan (state); // (round 190lv)
         unitParamsState = &state;
         unitParamsSerial = state.serial;
     }
@@ -6187,6 +6432,13 @@ bool RackRenderer::batchable (const RackState& state, int numSamples) noexcept
         return refuse (BatchRefusal::heldNotes);
     if (unitParamsTimed && sampleAccurateAutomation())
         return refuse (BatchRefusal::timedParams);
+    // round 190lv: live controllers, in two rounds where buildLivePlan proved that exact
+    if (! state.liveAutomations.empty())
+    {
+        if (! livePlanOk)
+            return refuse (BatchRefusal::liveControllers);
+        liveBatch = true;
+    }
     return true;
 }
 
@@ -6246,6 +6498,23 @@ bool RackRenderer::planPieces (const RackState& state, bool playing, double star
         pieceCount = 0;
         return false;
     }
+    // (round 190lv: with live controllers a block costs two rounds: two pieces cost no more piece by piece, where A7 can
+    // still keep a light piece on this thread — measured at 256 samples, 1.26 rounds per block against 1.59)
+    if (liveBatch && pieceCount < 3)
+    {
+        pieceStart = pieceStartBefore;
+        pieceCount = 0;
+        return false;
+    }
+    // (round 190lv: a later piece that goes on from the one before skips updateAutomation, it would keep the placeholder)
+    if (liveBatch)
+        for (int k = 1; k < pieceCount; ++k)
+            if (pieces[(std::size_t) k].continuing)
+            {
+                pieceStart = pieceStartBefore;
+                pieceCount = -2; // (counted as a refusal by process())
+                return false;
+            }
     return true;
 }
 
@@ -6288,7 +6557,9 @@ void RackRenderer::processBatched (const RackState& state, bool playing, int num
         liveInputOffset = piece.offset;
         timedGridPos = piece.gridPos;
         deferParams = true;
+        deferLive = liveBatch && k > 0;
         chunkPrelude (state, playing, piece.startTick, piece.samples, piece.continuing);
+        deferLive = false;
         deferParams = false;
         eventCount += events.size();
         piece.live = liveInput != nullptr && liveInput->numChannels > 0;
@@ -6310,6 +6581,8 @@ void RackRenderer::processBatched (const RackState& state, bool playing, int num
     if (runBatchedGraph (state, playing, channels, meters))
     {
         ++batchedRounds;
+        if (liveBatch)
+            ++batchedLiveRounds;
         for (int k = 0; k < pieceCount; ++k)
         {
             const auto& piece = pieces[(std::size_t) k];
@@ -6335,6 +6608,10 @@ void RackRenderer::processBatched (const RackState& state, bool playing, int num
             workers = chosen;
             chunkUnits (state, playing, piece.samples, meters);
             chunkMaster (state, piece.samples, left + piece.offset, right + piece.offset, meters, pool, chunkStartNs);
+            // (round 190lv: the controllers' values after this piece, read as the next piece's updateAutomation read them)
+            if (liveBatch && k + 1 < pieceCount && pieces[(std::size_t) k + 1].liveDeferred)
+                for (const auto a : state.liveAutomations)
+                    writeLiveValue (state, a, k + 1);
         }
     workers = pool;
 
@@ -6357,8 +6634,6 @@ bool RackRenderer::runBatchedGraph (const RackState& state, bool playing, int ch
     // (runTaskGraph's conditions; batchable checked the rest for the whole block)
     if (workers == nullptr || ! parallelChunk)
         return false;
-    const auto tracks = std::max<int> (1, (int) state.song.mixer.size());
-    const auto tasks = channels + tracks - 1;
 
     for (int k = 0; k < pieceCount; ++k)
     {
@@ -6366,14 +6641,34 @@ bool RackRenderer::runBatchedGraph (const RackState& state, bool playing, int ch
         trackActive.fill (0);
         tapActive.fill (0);
     }
+    if (! liveBatch)
+        return runGraphRound (state, playing, channels, meters, 0);
+    // round 190lv: the controllers' sources and what feeds them, then the rest (each unit still renders all its pieces
+    // after every unit it depends on: the graph's order, cut in two)
+    runGraphRound (state, playing, channels, meters, 1);
+    if (liveCountB > 0)
+        runGraphRound (state, playing, channels, meters, 2);
+    return true;
+}
+
+// one round of the batched task graph: round 0 = every unit, 1 = round A of a live block (liveInA), 2 = its round B
+bool RackRenderer::runGraphRound (const RackState& state, bool playing, int channels, MeterBank* meters, int round) noexcept
+{
+    const auto tracks = std::max<int> (1, (int) state.song.mixer.size());
+    const auto tasks = channels + tracks - 1;
+    const auto liveA = round == 1;
     for (int i = 0; i < tasks; ++i)
     {
-        graphPending[(std::size_t) i].store (state.graphIndegree[(std::size_t) i], std::memory_order_relaxed);
+        graphPending[(std::size_t) i].store (round == 2 ? livePendingB[(std::size_t) i] : state.graphIndegree[(std::size_t) i], std::memory_order_relaxed);
         graphSlots[(std::size_t) i].store (0, std::memory_order_relaxed);
     }
     auto pushed = 0;
-    for (const auto r : state.graphRoots)
-        graphSlots[(std::size_t) pushed++].store (r + 1, std::memory_order_relaxed);
+    if (round == 0)
+        for (const auto r : state.graphRoots)
+            graphSlots[(std::size_t) pushed++].store (r + 1, std::memory_order_relaxed);
+    else
+        for (int k = 0, n = round == 1 ? liveRootCountA : liveRootCountB; k < n; ++k)
+            graphSlots[(std::size_t) pushed++].store ((round == 1 ? liveRootsA : liveRootsB)[(std::size_t) k] + 1, std::memory_order_relaxed);
     graphPushed.store (pushed, std::memory_order_release);
 
     const auto timing = times->enabled.load (std::memory_order_relaxed);
@@ -6443,6 +6738,11 @@ bool RackRenderer::runBatchedGraph (const RackState& state, bool playing, int ch
                         mixSideInput (state, t, n);
                         processTrack (state, t, n, meters);
                     }
+                    // (round 190lv: a controller's source: its values for the next piece, as that piece's
+                    // updateAutomation read them after this one)
+                    if (liveA && k + 1 < pieceCount && pieces[(std::size_t) k + 1].liveDeferred)
+                        for (auto s = liveSourceStart[(std::size_t) at]; s < liveSourceStart[(std::size_t) at + 1]; ++s)
+                            writeLiveValue (state, liveSources[(std::size_t) s], k + 1);
                 }
                 const auto spent = ProcessTimes::now() - before;
                 // (A7 keeps one chunk's cost: the pieces' average)
@@ -6456,13 +6756,15 @@ bool RackRenderer::runBatchedGraph (const RackState& state, bool playing, int ch
                 else if (const auto t = at - channels + 1; measureUnits && t < maxMixerTracks)
                     trackCostNs[(std::size_t) t] = (std::uint32_t) std::min<std::uint64_t> (spent / count, 0xffffffffu);
                 const auto fused = (std::size_t) at < state.graphFused.size() ? state.graphFused[(std::size_t) at] : -1;
-                if (fused < 0)
+                if (fused < 0 || (liveA && liveInA[(std::size_t) fused] == 0)) // (round A: a chain into round B is cut there)
                     break;
                 at = fused;
             }
             for (auto k = state.graphNextStart[(std::size_t) at]; k < state.graphNextStart[(std::size_t) at + 1]; ++k)
             {
                 const auto j = state.graphNext[(std::size_t) k];
+                if (liveA && liveInA[(std::size_t) j] == 0) // (round B's: its pending count left out round A's units)
+                    continue;
                 if (graphPending[(std::size_t) j].fetch_sub (1, std::memory_order_acq_rel) == 1)
                 {
                     const auto pos = graphPushed.fetch_add (1, std::memory_order_acq_rel);
@@ -6480,7 +6782,7 @@ bool RackRenderer::runBatchedGraph (const RackState& state, bool playing, int ch
         while (takenCount > 0)
             chain (taken[(std::size_t) --takenCount], chain);
     };
-    workers->parallelFor (tasks - state.graphFusedCount, task);
+    workers->parallelFor (round == 0 ? tasks - state.graphFusedCount : round == 1 ? liveCountA : liveCountB, task);
     return true;
 }
 

+ 26 - 0
src/core/RackRenderer.h

@@ -718,6 +718,11 @@ public:
     // round: processBatched), and how many of them did go to the workers in that one round (diagnostics, A/B)
     std::uint64_t batchedBlockCount() const noexcept { return batchedBlocks; }
     std::uint64_t batchedRoundCount() const noexcept { return batchedRounds; }
+    // Round 190lv (sched2): batched blocks of a project with live controllers, run as two rounds (the controllers' sources
+    // and what feeds them, then the rest; processBatched's liveBatch)
+    std::uint64_t batchedLiveRoundCount() const noexcept { return batchedLiveRounds; }
+    // ... and why the last state's live controllers could not be ("" = they can, or none were looked at)
+    const char* livePlanRefusal() const noexcept { return livePlanWhy; }
     // ... and the blocks that could not be, by the first reason found (every block is looked at, one cut in a single
     // piece too: that one runs one round anyway). Audio thread counters, read them between blocks (diagnostics).
     enum class BatchRefusal : int
@@ -893,6 +898,7 @@ private:
         bool live = false, recording = false;     // (the live input, as runTaskGraph reads it per chunk)
         bool params = false, paramsLive = false;  // updateAutomation's applyPluginParams, deferred to the units
         int paramsFirst = 0;
+        bool liveDeferred = false; // (round 190lv) updateAutomation's live loop left a placeholder: the source writes it
     };
     std::array<Piece, maxPieces> pieces {};
     int pieceCount = 0;
@@ -916,6 +922,26 @@ private:
     void carryPieceState (int from, int to) noexcept;
     bool unitParamsBend = false, unitParamsTimed = false, unitParamsHeld = false;
     bool runBatchedGraph (const RackState& state, bool playing, int channels, MeterBank* meters) noexcept;
+    // ---- Round 190lv (sched2): batched blocks with live controllers --------------------------------------------------
+    // A controller's output after piece k is what piece k+1's serial part reads (updateAutomation's live loop). Batched,
+    // those parts leave a placeholder for pieces k >= 1 (deferLive), and the block runs two rounds: round A = the
+    // controllers' sources and every unit that feeds them (liveInA: the upstream closure, so nothing in it waits for a
+    // unit outside), each source writing its live values into the next piece's slot right after rendering a piece; round
+    // B = the rest. Exact only where no later serial part touches a live value and a round-A unit reads only its own source's
+    // or an upstream source's (buildLivePlan).
+    bool deferLive = false;           // (processBatched's serial parts of pieces >= 1)
+    bool liveBatch = false;           // this block runs the two rounds
+    bool livePlanOk = false;          // the state's live controllers can be batched (for unitParamsState)
+    std::vector<char> liveInA;        // [task] in round A
+    std::vector<int> liveSourceStart, liveSources; // [task] → its live automations (CSR, in liveAutomations' order)
+    std::vector<int> liveRootsA, liveRootsB, livePendingB;
+    int liveRootCountA = 0, liveRootCountB = 0;
+    int liveCountA = 0, liveCountB = 0; // positions of each round (its nodes minus the fused links inside it)
+    std::uint64_t batchedLiveRounds = 0;
+    const char* livePlanWhy = "";
+    bool buildLivePlan (const RackState& state) noexcept;
+    void writeLiveValue (const RackState& state, int automation, int slot) noexcept;
+    bool runGraphRound (const RackState& state, bool playing, int channels, MeterBank* meters, int round) noexcept;
     // processChunk in its three parts (round 190kz): the serial part before the units, the units, the Master's part.
     // chunkPrelude returns the pool to restore after the units (chunkSchedule may set `workers` to nullptr)
     void chunkPrelude (const RackState& state, bool playing, double startTick, int numSamples, bool continuing) noexcept;

+ 1 - 0
src/core/WorkerPool.cpp

@@ -185,6 +185,7 @@ void WorkerPool::Tuning::apply (const char* spec) noexcept
     waitMode.store (std::clamp (parsedValue (spec, "wait", waitMode.load()), 0, 2));
     waitSpinMicros.store (std::clamp (parsedValue (spec, "waitspin", waitSpinMicros.load()), 0, 10000));
     helpPercent.store (std::clamp (parsedValue (spec, "help", helpPercent.load()), 0, 1000));
+    liveRounds.store (parsedValue (spec, "live", liveRounds.load()) != 0 ? 1 : 0);
     // (round 190kz: "rounds=chunk" / "rounds=block", or 0 / 1)
     for (const char* at = spec; at != nullptr && *at != 0;)
     {

+ 5 - 1
src/core/WorkerPool.h

@@ -119,7 +119,11 @@ public:
         // runs its pieces one after another inside it); 0 = one round per piece (before round 190kz). LYNX_POOL accepts
         // "rounds=block" / "rounds=chunk" too.
         std::atomic<int> blockRounds { 1 };
-        void apply (const char* spec) noexcept; // "spin=60,dry=4,adaptive=1,wait=1,waitspin=30,help=0,rounds=block" (missing keys: unchanged)
+        // Round 190lv (sched2): with blockRounds on, the blocks of a project with live controllers (Peak / Formula /
+        // envelope controllers) batched too, in two rounds where that renders the same samples. 0 = those blocks piece
+        // by piece (before round 190lv). LYNX_POOL "live=0" / "live=1".
+        std::atomic<int> liveRounds { 1 };
+        void apply (const char* spec) noexcept; // "spin=60,dry=4,adaptive=1,wait=1,waitspin=30,help=0,rounds=block,live=1" (missing keys: unchanged)
     };
     static Tuning& tuning() noexcept;
 

+ 4 - 2
tools/perfbench.cpp

@@ -589,9 +589,9 @@ int main (int argc, char** argv)
                          ms (p.callerWaitNs - pool0.callerWaitNs), per (p.callerSleeps - pool0.callerSleeps));
             // round 190kz (sched): rounds per device block, and the blocks rendered batched (one round for all their pieces)
             const auto blocks = (double) ((totalSamples + (std::size_t) blockSize - 1) / (std::size_t) blockSize);
-            std::printf ("sched: %.2f rounds per block, batched blocks %llu (one round %llu) since start, of %.0f measured blocks\n",
+            std::printf ("sched: %.2f rounds per block, batched blocks %llu (one round %llu) since start, of %.0f measured blocks; two-round blocks %llu\n",
                          (double) (p.rounds - pool0.rounds) / std::max (1.0, blocks), (unsigned long long) renderer->batchedBlockCount(),
-                         (unsigned long long) renderer->batchedRoundCount(), blocks);
+                         (unsigned long long) renderer->batchedRoundCount(), blocks, (unsigned long long) renderer->batchedLiveRoundCount());
             {
                 static const char* const why[] = { "switched off", "no graph", "big block", "live controllers", "MIDI between units",
                                                    "launchers", "bindings", "held notes", "timed params", "many pieces", "other" };
@@ -601,6 +601,8 @@ int main (int argc, char** argv)
                     if (const auto c = renderer->batchRefusedCount ((RackRenderer::BatchRefusal) k); c > 0)
                         std::printf (" %s %llu", why[k], (unsigned long long) c), any = true;
                 std::printf ("%s\n", any ? "" : " none");
+                if (*renderer->livePlanRefusal() != 0) // (round 190lv)
+                    std::printf ("sched: live controllers kept piece by piece: %s\n", renderer->livePlanRefusal());
             }
         }
         if (ahead != nullptr)