Skip to content

Fix new components simulation #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/pages/edit/Editor/renderer/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export default function CCComponentEditorRendererBackground() {
const startPerspective = componentEditorState.perspective;
const startPoint = vector2.fromDomEvent(pointerDownEvent.nativeEvent);

pointerDownEvent.currentTarget.setPointerCapture(
pointerDownEvent.pointerId,
);
currentTarget.setPointerCapture(pointerDownEvent.pointerId);
const onPointerMove = (pointerMoveEvent: PointerEvent) => {
const endPoint = vector2.fromDomEvent(pointerMoveEvent);
componentEditorState.setPerspective({
Expand All @@ -35,9 +33,7 @@ export default function CCComponentEditorRendererBackground() {
const onPointerUp = () => {
currentTarget.removeEventListener("pointermove", onPointerMove);
currentTarget.removeEventListener("pointerup", onPointerUp);
pointerDownEvent.currentTarget.releasePointerCapture(
pointerDownEvent.pointerId,
);
currentTarget.releasePointerCapture(pointerDownEvent.pointerId);
};
currentTarget.addEventListener("pointermove", onPointerMove);
currentTarget.addEventListener("pointerup", onPointerUp);
Expand Down
6 changes: 5 additions & 1 deletion src/pages/edit/Editor/renderer/NodePin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function CCComponentEditorRendererNodePin({
const componentEditorState = useComponentEditorStore()();
const nodePin = nullthrows(store.nodePins.get(nodePinId));
const node = nullthrows(store.nodes.get(nodePin.nodeId));
const nodePins = store.nodePins.getManyByNodeId(node.id);
const componentPin = nullthrows(
store.componentPins.get(nodePin.componentPinId),
);
Expand Down Expand Up @@ -152,7 +153,10 @@ export default function CCComponentEditorRendererNodePin({
implementationComponentPin.type === "input"
) {
nodePinValue = nullthrows(
componentEditorState.getInputValue(implementationComponentPin.id),
componentEditorState.getInputValue(
implementationComponentPin.id,
nodePins,
),
);
} else {
nodePinValue = nullthrows(
Expand Down
16 changes: 13 additions & 3 deletions src/pages/edit/Editor/store/slices/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import simulateComponent from "../../../../../../store/componentEvaluator";
import type { CCComponentPinId } from "../../../../../../store/componentPin";
import type { CCConnectionId } from "../../../../../../store/connection";
import type { CCNodeId } from "../../../../../../store/node";
import type { CCNodePinId } from "../../../../../../store/nodePin";
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";
import type { ComponentEditorSliceCreator } from "../../types";
import type { EditorStoreCoreSlice } from "./types";

Expand Down Expand Up @@ -46,12 +46,13 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
},
/** @private */
inputValues: new Map(),
getInputValue(componentPinId: CCComponentPinId) {
getInputValue(componentPinId: CCComponentPinId, nodePins: CCNodePin[]) {
const value = get().inputValues.get(componentPinId);
if (!value) {
const multiplexability =
store.componentPins.getComponentPinMultiplexability(
componentPinId,
nodePins,
);
if (multiplexability === "undecidable") {
throw new Error("Cannot determine multiplexability");
Expand Down Expand Up @@ -174,8 +175,17 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
const inputValues = new Map<CCComponentPinId, SimulationValue>();
const pins = store.componentPins.getManyByComponentId(componentId);
for (const pin of pins) {
invariant(pin.implementation);
if (pin.type === "input") {
inputValues.set(pin.id, editorState.getInputValue(pin.id));
const nodePin = store.nodePins.get(pin.implementation);
invariant(nodePin);
const node = store.nodes.get(nodePin.nodeId);
invariant(node);
const nodePins = store.nodePins.getManyByNodeId(node.id);
inputValues.set(
pin.id,
editorState.getInputValue(pin.id, nodePins),
);
}
}
simulationCachedFrames.push(
Expand Down
7 changes: 5 additions & 2 deletions src/pages/edit/Editor/store/slices/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Vector2 } from "../../../../../../common/vector2";
import type { CCComponentPinId } from "../../../../../../store/componentPin";
import type { CCConnectionId } from "../../../../../../store/connection";
import type { CCNodeId } from "../../../../../../store/node";
import type { CCNodePinId } from "../../../../../../store/nodePin";
import type { CCNodePin, CCNodePinId } from "../../../../../../store/nodePin";

export type EditorMode = EditorModeEdit | EditorModePlay;
export type EditorModeEdit = "edit";
Expand All @@ -30,7 +30,10 @@ export type EditorStoreCoreSlice = {
target: NodePinPropertyEditorTarget | null,
): void;
inputValues: Map<InputValueKey, SimulationValue>;
getInputValue(componentPinId: CCComponentPinId): SimulationValue;
getInputValue(
componentPinId: CCComponentPinId,
nodePins: CCNodePin[],
): SimulationValue;
setInputValue(componentPinId: CCComponentPinId, value: SimulationValue): void;
setEditorMode(mode: EditorMode): void;
resetTimeStep(): void;
Expand Down
43 changes: 39 additions & 4 deletions src/store/componentPin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
or,
xor,
} from "./intrinsics/definitions";
import type { CCNodePinId } from "./nodePin";
import type { CCNodePin, CCNodePinId } from "./nodePin";

export type CCComponentPin = {
readonly id: CCComponentPinId;
Expand Down Expand Up @@ -47,7 +47,11 @@ export type CCPinImplementation = CCNodePinId | null;

export type CCPinMultiplexability =
| { isMultiplexable: true }
| { isMultiplexable: false; multiplicity: number };
| {
isMultiplexable: false;
multiplicity: number;
};
// | { isMultiplexable: false; multiplicity: number };

export type CCComponentPinMultiplexability =
| CCPinMultiplexability
Expand Down Expand Up @@ -239,6 +243,7 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
*/
getComponentPinMultiplexability(
pinId: CCComponentPinId,
nodePins: CCNodePin[],
): CCComponentPinMultiplexability {
const pin = this.#pins.get(pinId);
invariant(pin);
Expand All @@ -264,13 +269,43 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
return "undecidable";
}
case nullthrows(aggregate.outputPin.id): {
return "undecidable";
const multiplicity = nodePins
.filter((pin) => {
const componentPin = this.#store.componentPins.get(
pin.componentPinId,
);
invariant(componentPin);
return componentPin.type === "input";
})
.reduce((acc, pin) => {
invariant(pin.userSpecifiedBitWidth);
return acc + pin.userSpecifiedBitWidth;
}, 0);
return {
isMultiplexable: false,
multiplicity,
};
}
case nullthrows(decompose.outputPin.id): {
return "undecidable";
}
case nullthrows(decompose.inputPin.In.id): {
return "undecidable";
const multiplicity = nodePins
.filter((pin) => {
const componentPin = this.#store.componentPins.get(
pin.componentPinId,
);
invariant(componentPin);
return componentPin.type === "output";
})
.reduce((acc, pin) => {
invariant(pin.userSpecifiedBitWidth);
return acc + pin.userSpecifiedBitWidth;
}, 0);
return {
isMultiplexable: false,
multiplicity,
};
}
case nullthrows(broadcast.inputPin.In.id): {
return { isMultiplexable: false, multiplicity: 1 };
Expand Down
15 changes: 9 additions & 6 deletions src/store/intrinsics/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function createUnaryOperator(
evaluate: (input) => {
invariant(input.A[0] && !input.A[1]);
const A = input.A[0];
return A.map((a) => [evaluate(nullthrows(a))]);
return [A.map((a) => evaluate(nullthrows(a)))];
},
});
}
Expand All @@ -47,9 +47,11 @@ function createBinaryOperator(
const A = input.A[0];
const B = input.B[0];
invariant(A.length === B.length);
return Array.from({ length: input.A.length }, (_, i) => [
evaluate(nullthrows(A[i]), nullthrows(B[i])),
]);
return [
Array.from({ length: A.length }, (_, i) =>
evaluate(nullthrows(A[i]), nullthrows(B[i])),
),
];
},
});
}
Expand Down Expand Up @@ -123,10 +125,11 @@ export const broadcast = new IntrinsicComponentDefinition({
out: { name: "Out", isBitWidthConfigurable: true },
evaluate: (input, outputShape) => {
invariant(input.In[0] && !input.In[1]);
const inputValue = input.In[0];
invariant(input.In[0][0] !== undefined && !input.In[0][1]);
const inputValue = input.In[0][0];
invariant(outputShape[0] && !outputShape[1]);
const outputMultiplicity = outputShape[0].multiplicity;
return Array.from({ length: outputMultiplicity }, () => inputValue);
return [Array.from({ length: outputMultiplicity }, () => inputValue)];
},
});

Expand Down
11 changes: 9 additions & 2 deletions src/store/nodePin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,19 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
const node = nullthrows(this.#store.nodes.get(nodeId));
const nodePins = this.getManyByNodeId(node.id);
const givenPinMultiplexability =
this.#store.componentPins.getComponentPinMultiplexability(pinId);
this.#store.componentPins.getComponentPinMultiplexability(
pinId,
nodePins,
);
if (givenPinMultiplexability === "undecidable") {
invariant(
userSpecifiedBitWidth,
"Multiplexability of undecidable pin must be contained in multiplexabilityEnv",
);
return { isMultiplexable: false, multiplicity: userSpecifiedBitWidth };
return {
isMultiplexable: false,
multiplicity: userSpecifiedBitWidth,
};
}
if (!givenPinMultiplexability.isMultiplexable) {
return givenPinMultiplexability;
Expand All @@ -203,6 +209,7 @@ export class CCNodePinStore extends EventEmitter<CCNodePinStoreEvents> {
const pinMultiplexability =
this.#store.componentPins.getComponentPinMultiplexability(
nodePin.componentPinId,
nodePins,
);
if (pinMultiplexability === "undecidable") {
throw new Error("unreachable");
Expand Down