Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
deluksic committed Jan 13, 2025
1 parent f86e127 commit 23acc89
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 107 deletions.
1 change: 1 addition & 0 deletions docs/assets/index-BlA0zTNF.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion docs/assets/index-DimjPUGy.css

This file was deleted.

83 changes: 83 additions & 0 deletions docs/assets/index-DnxXs-qt.js

Large diffs are not rendered by default.

81 changes: 0 additions & 81 deletions docs/assets/index-ZI7z1YVS.js

This file was deleted.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" type="image/ico" href="./assets/favicon-mtvwWgEY.ico" />
<title>Solid App</title>
<script type="module" crossorigin src="./assets/index-ZI7z1YVS.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-DimjPUGy.css">
<script type="module" crossorigin src="./assets/index-DnxXs-qt.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-BlA0zTNF.css">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
1 change: 1 addition & 0 deletions src/demo/App.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
position: relative;
height: 100dvh;
color: var(--color-text);
background-color: var(--color-background);
}

.controls {
Expand Down
8 changes: 4 additions & 4 deletions src/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function Inside() {
setSelectedCircleIndex(undefined)

{
const circle = circles[index]!
const circle = circles()[index]!
if (vec2.distance(circle.center, world) < circle.radius) {
return
}
Expand Down Expand Up @@ -129,7 +129,7 @@ function Inside() {

return (
<Circles
circles={circles.map((c, i) => ({
circles={circles().map((c, i) => ({
...c,
styleIndex: theme().getStyleIndex(style(i)),
}))}
Expand All @@ -142,7 +142,7 @@ function Inside() {
export function App() {
return (
<div class={ui.page} classList={{ [ui[colorScheme()]]: true }}>
<div class={ui.circleCounter}>Number of Circles: {circles.length}</div>
<div class={ui.circleCounter}>Number of Circles: {circles().length}</div>
<div class={ui.controls}>
<label>
<input
Expand All @@ -163,7 +163,7 @@ export function App() {
Debug mode
</label>
</div>
<Show when={circles.length === 0}>
<Show when={circles().length === 0}>
<div class={ui.welcomeMessage}>
<h1>Apollonian Circles</h1>
<p class={ui.welcomeMessageSubtitle}>
Expand Down
9 changes: 5 additions & 4 deletions src/demo/Circles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CircleStyle = struct({
fadeColor: vec4f,
})

const N = 10000
const N = 100000
const SUBDIVS = 24

const uniformBindGroupLayout = tgpu.bindGroupLayout({
Expand Down Expand Up @@ -128,14 +128,15 @@ export function Circles(props: CirclesProps) {
const camera = useCamera()
const { root, device } = useRootContext()
const { context } = useCanvas()
const circles = createMemo(() => props.circles)

const circlesBuffer = root
.createBuffer(arrayOf(Circle, N))
.$usage('storage')
.$name('circles')

createEffect(() => {
circlesBuffer.write(props.circles)
circlesBuffer.write(circles())
})

const colorBuffer = root
Expand Down Expand Up @@ -246,8 +247,8 @@ export function Circles(props: CirclesProps) {
pass.setPipeline(pipeline)
pass.setBindGroup(0, root.unwrap(camera.bindGroup))
pass.setBindGroup(1, root.unwrap(uniformBindGroup))
if (props.circles.length > 0) {
pass.draw((SUBDIVS + 1) * 2, props.circles.length)
if (circles().length > 0) {
pass.draw((SUBDIVS + 1) * 2, circles().length)
}
pass.end()

Expand Down
30 changes: 15 additions & 15 deletions src/demo/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ function vec2Normalize(a: v2f) {
// center: vec2f(random() * 10 - 5, random() * 10 - 5),
// radius: random() * 0.05 + 0.05,
// }))
export const [circles, setCircles] = createStore<Circle[]>([])
export const [circles, setCircles] = createSignal<Circle[]>([], {
equals: false,
})
export const [debug, setDebug] = createSignal(false)
export const [colorScheme, setColorScheme] = createSignal(
getPreferredColorScheme(),
)

export function chooseOrCreateCircle(xy: v2f): number {
const circles_ = unwrap(circles)
const circles_ = circles()
const index = circles_.findIndex(
(c) => vec2.distance(c.center, xy) < c.radius,
)
Expand All @@ -54,13 +56,13 @@ export function chooseOrCreateCircle(xy: v2f): number {
circles.push({ center: xy, radius: 0 })
}),
)
return circles.length - 1
return circles().length - 1
}
return index
}

export function closestFirstCircle(queryCircleIndex: number) {
const circles_ = unwrap(circles)
const circles_ = circles()
let radius = Number.POSITIVE_INFINITY
let index = undefined
const { center } = circles_[queryCircleIndex]!
Expand Down Expand Up @@ -89,7 +91,7 @@ export function closestSecondCircle(
firstCircleIndex: number,
queryCircleIndex: number,
) {
const circles_ = unwrap(circles)
const circles_ = circles()
const firstCircle = circles_[firstCircleIndex]!
const queryCircle = circles_[queryCircleIndex]!
const d = vec2Normalize(
Expand Down Expand Up @@ -209,13 +211,13 @@ export function closestThirdCircle(
secondCircleIndex: number,
queryCircleIndex: number,
) {
const circles_ = unwrap(circles)
const circles_ = circles()
const queryCircle = circles_[queryCircleIndex]!
const firstCircle = circles_[firstCircleIndex]!
const secondCircle = circles_[secondCircleIndex]!
let radius = Number.POSITIVE_INFINITY
let index: number | undefined = undefined
for (let i = 0; i < circles.length; ++i) {
for (let i = 0; i < circles_.length; ++i) {
if (
i === queryCircleIndex ||
i === firstCircleIndex ||
Expand Down Expand Up @@ -247,7 +249,7 @@ export async function growUntilRadius(
zoom: Accessor<number>,
stop: Promise<PointerEvent>,
) {
const circle = circles[index]!
const circle = circles()[index]!
while (true) {
const result = await Promise.race([nextAnimationFrame(), stop])
if (result instanceof PointerEvent) {
Expand All @@ -258,13 +260,11 @@ export async function growUntilRadius(
if (!nextCenter) {
return
}
setCircles(
index,
produce((circle) => {
circle.radius = nextRadius
circle.center = nextCenter
}),
)
setCircles((p) => {
p[index]!.radius = nextRadius
p[index]!.center = nextCenter
return p
})
if (nextRadius === maxRadius) {
return
}
Expand Down

0 comments on commit 23acc89

Please sign in to comment.