Skip to content
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

feat: bit pack relic set solutions array #566

Draft
wants to merge 1 commit into
base: beta
Choose a base branch
from
Draft
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: 7 additions & 1 deletion src/lib/gpu/injection/generateWgsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ function format(text: string) {
function injectSetFilters(wgsl: string, gpuParams: GpuConstants) {
// CTRL+ F: RESULTS ASSIGNMENT
return wgsl.replace('/* INJECT SET FILTERS */', indent(`
if (relicSetSolutionsMatrix[relicSetIndex] < 1 || ornamentSetSolutionsMatrix[ornamentSetIndex] < 1) {

let arrayIndex = relicSetIndex >> 5u;
let bitPosition = relicSetIndex & 31u;
let packedValue: i32 = relicSetSolutionsMatrix[arrayIndex];
let unpackedValue = (u32(packedValue) >> (31u - bitPosition)) & 1u;

if (unpackedValue < 1 || ornamentSetSolutionsMatrix[ornamentSetIndex] < 1) {
results[index] = ${gpuParams.DEBUG ? 'ComputedStats()' : '-failures; failures = failures + 1'};
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions src/lib/gpu/webgpuOptimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ import { COMPUTE_ENGINE_GPU_EXPERIMENTAL } from 'lib/constants'
import { getWebgpuDevice } from 'lib/gpu/webgpuDevice'
import { GpuExecutionContext, RelicsByPart } from 'lib/gpu/webgpuTypes'

export function condenseRelicSetSolutions(relicSetSolutions: number[]) {
function bitPackArray(arr: number[]) {
const paddedLength = Math.ceil(arr.length / 32) * 32
const paddedArray = new Array(paddedLength).fill(0)

for (let i = 0; i < arr.length; i++) {
paddedArray[i] = arr[i]
}

const result: number[] = []

for (let i = 0; i < paddedArray.length; i += 32) {
let packedValue = 0

for (let j = 0; j < 32; j++) {
packedValue |= (paddedArray[i + j] << (31 - j))
}

result.push(packedValue >>> 0)
}

return result
}

return bitPackArray(relicSetSolutions)
}

export async function gpuOptimize(props: {
params: OptimizerParams
request: Form
Expand Down
3 changes: 2 additions & 1 deletion src/lib/optimizer/relicSetSolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Constants, RelicSetFilterOptions } from 'lib/constants'
import { Utils } from 'lib/utils'
import { condenseRelicSetSolutions } from 'lib/gpu/webgpuOptimizer'

// Here be dragons
export function generateRelicSetSolutions(request) {
Expand Down Expand Up @@ -221,7 +222,7 @@ function convertRelicSetIndicesTo1D(setIndices) {
}
}

return arr
return condenseRelicSetSolutions(arr)
}

const permutator = (inputArr) => {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/worker/optimizerWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ self.onmessage = function (e: MessageEvent) {
const relicSetIndex = setH + setB * relicSetCount + setG * relicSetCount * relicSetCount + setF * relicSetCount * relicSetCount * relicSetCount
const ornamentSetIndex = setP + setL * ornamentSetCount

const arrayIndex = relicSetIndex >> 5
const bitPosition = relicSetIndex & 31
const packedValue = relicSetSolutions[arrayIndex]
const unpackedValue = (packedValue >> (31 - bitPosition)) & 1

// Exit early if sets don't match
if (relicSetSolutions[relicSetIndex] != 1 || ornamentSetSolutions[ornamentSetIndex] != 1) {
if (unpackedValue != 1 || ornamentSetSolutions[ornamentSetIndex] != 1) {
continue
}

Expand Down