optimizing searcher

This commit is contained in:
Lilith Schier 2026-10-05 17:56:37 +02:00
parent 48fd06e124
commit 5794f50856
6 changed files with 1067 additions and 161 deletions

View file

@ -11,7 +11,7 @@ pub fn build(b: *Build) !void {
const tracy_callstack = b.option(u32, "tracy_callstack", "Size of the collected callstack") orelse 0;
const dancer_static_lib = dancer_static_lib: {
const odin_exec = b.addSystemCommand(&.{ "odin", "build", "-build-mode:static" });
const odin_exec = b.addSystemCommand(&.{ "odin", "build", "-build-mode:static", "-microarch:x86-64-v3" });
odin_exec.addArg(switch (optimize) {
.Debug => "-o:minimal",
.ReleaseSafe => "-o:speed",

82
src/cards.odin Normal file
View file

@ -0,0 +1,82 @@
package game
@(rodata)
CARDS := [Card_Handle(TAROT_COUNT)]Card {
{.Major, 0},
{.Major, 1},
{.Major, 2},
{.Major, 3},
{.Major, 4},
{.Major, 5},
{.Major, 6},
{.Major, 7},
{.Major, 8},
{.Major, 9},
{.Major, 10},
{.Major, 11},
{.Major, 12},
{.Major, 13},
{.Major, 14},
{.Major, 15},
{.Major, 16},
{.Major, 17},
{.Major, 18},
{.Major, 19},
{.Major, 20},
{.Major, 21},
{.Chalice, 1},
{.Chalice, 2},
{.Chalice, 3},
{.Chalice, 4},
{.Chalice, 5},
{.Chalice, 6},
{.Chalice, 7},
{.Chalice, 8},
{.Chalice, 9},
{.Chalice, 10},
{.Chalice, 11},
{.Chalice, 12},
{.Chalice, 13},
{.Chalice, 14},
{.Coin, 1},
{.Coin, 2},
{.Coin, 3},
{.Coin, 4},
{.Coin, 5},
{.Coin, 6},
{.Coin, 7},
{.Coin, 8},
{.Coin, 9},
{.Coin, 10},
{.Coin, 11},
{.Coin, 12},
{.Coin, 13},
{.Coin, 14},
{.Sword, 1},
{.Sword, 2},
{.Sword, 3},
{.Sword, 4},
{.Sword, 5},
{.Sword, 6},
{.Sword, 7},
{.Sword, 8},
{.Sword, 9},
{.Sword, 10},
{.Sword, 11},
{.Sword, 12},
{.Sword, 13},
{.Sword, 14},
{.Wand, 1},
{.Wand, 2},
{.Wand, 3},
{.Wand, 4},
{.Wand, 5},
{.Wand, 6},
{.Wand, 7},
{.Wand, 8},
{.Wand, 9},
{.Wand, 10},
{.Wand, 11},
{.Wand, 12},
{.Wand, 13},
{.Wand, 14},
}

View file

@ -5,6 +5,7 @@ import "base:intrinsics"
import "base:runtime"
import "core:c"
import hm "core:container/handle_map"
import pq "core:container/priority_queue"
import "core:container/queue"
import "core:container/topological_sort"
import "core:fmt"
@ -12,8 +13,11 @@ import "core:log"
import "core:math/rand"
import "core:mem"
import "core:reflect"
import "core:simd"
import "core:slice"
import "core:sort"
import "core:strings"
import "tracy"
import ray "vendor:raylib"
import "vendor:raylib/rlgl"
@ -25,6 +29,7 @@ CARD_SIZE :: Vec2{100, 100 * CARD_PROPORTIONS}
CARD_ROUNDNESS :: 0.05
SUIT_TEX_SIZE :: Vec2{128, 128}
COLUMN_COUNT :: 11
COLUMN_HEIGHT :: 28
ROUNDED_RECT_SEGMENTS :: 6
GAP :: 16
CARD_SEPARATION :: 24
@ -39,7 +44,6 @@ Game :: struct {
static_arena_data: [1 * mem.Megabyte]byte,
// Allocations here will never be freed
static_allocator: runtime.Allocator,
cards: [TAROT_COUNT]Card,
entities: [TAROT_COUNT]Card_Entity,
state: State,
displayed_state: State,
@ -81,25 +85,35 @@ Card_State_Kind :: enum {
Locked,
}
State :: struct {
columns: [COLUMN_COUNT][dynamic; 30]Card_Handle,
columns: [COLUMN_COUNT]Column,
major: [Major_Pile_Kind][dynamic; MAJOR_COUNT]Card_Handle,
suits: [4][dynamic; MINOR_COUNT]Card_Handle,
minor: [4][dynamic; MINOR_COUNT]Card_Handle,
aside: [dynamic; 1]Card_Handle,
}
Canonical_State :: struct {
places: [TAROT_COUNT]Compressed_Place,
}
Compressed_Place :: bit_field [2]byte {
height: int | 5,
col: int | 4,
is_in_column: bool | 1,
is_aside: bool | 1,
}
Column :: [dynamic; COLUMN_HEIGHT]Card_Handle
Move :: struct {
card: Card_Handle,
from: Place,
to: Place,
card: Card_Handle,
}
Place :: struct {
kind: Place_Kind,
index: int,
index: i8,
}
Major_Pile_Kind :: enum {
Major_Pile_Kind :: enum i8 {
Low,
High,
}
Place_Kind :: enum {
Place_Kind :: enum i8 {
Column,
Major,
Suit,
@ -107,10 +121,15 @@ Place_Kind :: enum {
}
init :: proc() {
tracy.Zone()
game.run = true
ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT, .MSAA_4X_HINT})
ray.InitWindow(1600, 900, "Fortune's Foundation")
{ray.BeginDrawing(); defer ray.EndDrawing()
ray.ClearBackground(ray.RAYWHITE)
ray.DrawText("Generating puzzle,\nPlease wait...", 20, 20, 20, ray.GRAY)
}
// NOTE(lilith): Sorting statically included assets
assets := #load_directory("assets")
@ -134,22 +153,6 @@ init :: proc() {
ray.SetTextureFilter(game.suit_textures[suit], .TRILINEAR)
}
for number in 0 ..< MAJOR_COUNT {
game.cards[number] = Card {
suit = .Major,
number = i8(number),
}
}
for suit in Suit.Chalice ..= Suit.Wand {
suit_offset := MAJOR_COUNT + (int(suit) - 1) * MINOR_COUNT
for number in 1 ..= MINOR_COUNT {
game.cards[suit_offset + number - 1] = Card {
suit = suit,
number = i8(number),
}
}
}
game.card_font = ray.LoadFontFromMemory(
".ttf",
psz_from_slice(game.assets["Besley-Bold.ttf"].data),
@ -160,13 +163,28 @@ init :: proc() {
auto_moves := make([dynamic]Move, context.temp_allocator)
reverse_moves := state_init_reverse(&game.state)
random_game_count: int
for {
random_game_count += 1
log.debugf("generating random game %d", random_game_count)
state_init_fully_random(&game.state)
// if state_trivially_removable_count(game.state) > 0 {
// log.debugf("rejecting due to trivial solves")
// continue
// }
log.debugf("searching solution")
if ok := state_is_solvable(game.state); ok {
break
}
free_all(context.temp_allocator)
log.debugf("gave up on the solution")
}
game.displayed_state = game.state
log.debugf("Generated game: %#v", game.state)
// #reverse for move in reverse_moves {
// queue.append(&game.auto_moves, move)
// }
state_make_stacks(&game.state, &auto_moves)
state_make_stacks_with_effects(&game.state, &auto_moves)
queue.append(&game.auto_moves, ..auto_moves[:])
// solution, _ := state_find_solution(game.state, context.temp_allocator)
// log.infof("Solution: %v", solution)
@ -176,6 +194,7 @@ init :: proc() {
}
update :: proc() {
tracy.Zone()
free_all(context.temp_allocator)
delta := ray.GetFrameTime()
ray.BeginDrawing(); defer ray.EndDrawing()
@ -213,7 +232,7 @@ update :: proc() {
draggin_card_over_aside :=
game.dragged_card != nil &&
hovered_place.kind == .Suit &&
!state_can_stack_suit(&game.displayed_state, game.dragged_card.?) &&
!state_can_stack_suit(game.displayed_state, game.dragged_card.?) &&
len(game.state.aside) == 0
if draggin_card_over_aside {
game.dragged_card_rotation = -math.PI / 2
@ -229,7 +248,7 @@ update :: proc() {
to = new_place,
card = dragged_card,
}
effects, ok := state_perform_move(
effects, ok := state_perform_move_with_effects(
&game.state,
move,
allocator = context.temp_allocator,
@ -266,7 +285,7 @@ update :: proc() {
entity.target_rotation = 0
}
for major_pile, major_pile_kind in game.displayed_state.major {
place := Place{.Major, int(major_pile_kind)}
place := Place{.Major, i8(major_pile_kind)}
position := card_position(place)
slot_draw(position)
for handle in major_pile {
@ -279,7 +298,7 @@ update :: proc() {
}
for suit in Suit.Chalice ..= Suit.Wand {
suit_index := int(suit) - 1
suit_index := i8(suit) - 1
suit_position := card_position({.Suit, suit_index})
slot_draw(suit_position)
@ -292,15 +311,15 @@ update :: proc() {
rotation = 0,
tint = {255, 255, 255, 100},
)
for handle in game.displayed_state.suits[suit_index] {
for handle in game.displayed_state.minor[suit_index] {
game.entities[int(handle)].target_position = suit_position
}
}
for column, column_index in game.displayed_state.columns {
slot_draw(card_position({.Column, column_index}))
slot_draw(card_position({.Column, i8(column_index)}))
for handle, cc_index in column {
position := card_position({.Column, column_index}, cc_index)
position := card_position({.Column, i8(column_index)}, cc_index)
game.entities[int(handle)].target_position = position
}
}
@ -339,7 +358,7 @@ update :: proc() {
}
for suit in Suit.Chalice ..= Suit.Wand {
suit_index := int(suit) - 1
for handle in game.displayed_state.suits[suit_index] {
for handle in game.displayed_state.minor[suit_index] {
card_draw(handle)
}
}
@ -435,6 +454,7 @@ card_draw :: proc(handle: Card_Handle) {
}
state_init_fully_random :: proc(state: ^State) {
tracy.Zone()
deck := [TAROT_COUNT]Card_Handle{}
for &handle, index in deck {handle = Card_Handle(index)}
rand.shuffle(deck[:])
@ -448,7 +468,7 @@ state_init_fully_random :: proc(state: ^State) {
}
for column, column_index in game.state.columns {
for handle, cc_index in column {
position := card_position({.Column, column_index}, cc_index)
position := card_position({.Column, i8(column_index)}, cc_index)
obj := &game_cards_zipped()[int(handle)]
obj.entity.target_position = position
}
@ -457,7 +477,7 @@ state_init_fully_random :: proc(state: ^State) {
state_init_solved :: proc(state: ^State) {
state^ = {}
for &suit_pile, suit_index in state.suits {
for &suit_pile, suit_index in state.minor {
suit := Suit(suit_index + 1)
for number in 1 ..= MINOR_COUNT {
append(&suit_pile, Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT + number - 1))
@ -498,12 +518,12 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
state_init_solved(state)
Initializer :: struct {
moves: [dynamic]Move,
states_seen: map[[616]byte]struct{},
states_seen: map[State_Bytes]struct{},
state: ^State,
}
initializer := Initializer {
moves = make([dynamic]Move, context.temp_allocator),
states_seen = make(map[[616]byte]struct{}, context.temp_allocator),
states_seen = make(map[State_Bytes]struct{}, context.temp_allocator),
state = state,
}
column_indices: [COLUMN_COUNT]int
@ -530,7 +550,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
rand.shuffle(suits)
for suit in suits[:rand.int_range(0, 2) + (bury_majors ? 2 : 3)] {
suit_index := int(suit) - 1
clear(&state.suits[suit_index])
clear(&state.minor[suit_index])
append(
&state.columns[outer_column_idcs[suit_index + 4]],
Card_Handle(MAJOR_COUNT + suit_index * MINOR_COUNT),
@ -551,7 +571,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
stowed_majors: int
for major_pile in state.major {stowed_majors += len(major_pile)}
stowed_minors: int
for minor_pile in state.suits {stowed_minors += len(minor_pile)}
for minor_pile in state.minor {stowed_minors += len(minor_pile)}
if stowed_majors + stowed_minors == 0 {break}
if rand.float32() < f32(stowed_majors) / f32(stowed_majors + stowed_minors) {
major_clear: for &major_pile, major_pile_kind in state.major {
@ -570,8 +590,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
) {continue}
}
move := Move {
from = {.Column, from_column_index},
to = {.Major, int(major_pile_kind)},
from = {.Column, i8(from_column_index)},
to = {.Major, i8(major_pile_kind)},
card = slice.last(major_pile[:]),
}
state_reverse_single_move(state, move)
@ -584,8 +604,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
from_column := &state.columns[from_column_index]
if len(from_column) >= 8 {continue}
move := Move {
from = {.Column, from_column_index},
to = {.Major, int(major_pile_kind)},
from = {.Column, i8(from_column_index)},
to = {.Major, i8(major_pile_kind)},
card = slice.last(major_pile[:]),
}
state_reverse_single_move(state, move)
@ -595,7 +615,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
}
}
} else {
suit_clear: for &suit_pile, suit_index in state.suits {
suit_clear: for &suit_pile, suit_index in state.minor {
if len(state.aside) > 0 {break}
if len(suit_pile) > 0 {
rand.shuffle(outer_column_idcs[:])
@ -612,8 +632,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
) {continue}
}
move := Move {
from = {.Column, from_column_index},
to = {.Suit, suit_index},
from = {.Column, i8(from_column_index)},
to = {.Suit, i8(suit_index)},
card = to_handle,
}
state_reverse_single_move(state, move)
@ -626,8 +646,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
from_column := &state.columns[from_column_index]
if len(from_column) >= 8 {continue}
move := Move {
from = {.Column, from_column_index},
to = {.Suit, suit_index},
from = {.Column, i8(from_column_index)},
to = {.Suit, i8(suit_index)},
card = slice.last(suit_pile[:]),
}
state_reverse_single_move(state, move)
@ -648,7 +668,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
if len(column) >= 7 && column_index >= 10 {continue}
if len(column) >= 0 && column_index == 5 {continue}
move := Move {
from = {.Column, column_index},
from = {.Column, i8(column_index)},
to = {.Aside, 0},
card = state.aside[0],
}
@ -690,8 +710,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
if len(to_column) == 0 {continue}
if len(to_column) == 1 && len(from_column) == 0 {continue}
move := Move {
from = {.Column, from_column_index},
to = {.Column, to_column_index},
from = {.Column, i8(from_column_index)},
to = {.Column, i8(to_column_index)},
card = slice.last(to_column[:]),
}
if len(from_column) > 0 {
@ -713,8 +733,8 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
if len(to_column) == 0 {continue}
if len(to_column) == 1 && len(from_column) == 0 {continue}
move := Move {
from = {.Column, from_column_index},
to = {.Column, to_column_index},
from = {.Column, i8(from_column_index)},
to = {.Column, i8(to_column_index)},
card = slice.last(to_column[:]),
}
if state_is_move_valid_reverse(state, move) {
@ -730,7 +750,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
if len(to_column) == 0 {continue}
move := Move {
from = {.Aside, 0},
to = {.Column, to_column_index},
to = {.Column, i8(to_column_index)},
card = slice.last(to_column[:]),
}
if state_is_move_valid_reverse(state, move) {
@ -742,7 +762,7 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
for from_column_index in column_indices {
from_column := &state.columns[from_column_index]
move := Move {
from = {.Column, from_column_index},
from = {.Column, i8(from_column_index)},
to = {.Aside, 0},
card = state.aside[0],
}
@ -808,9 +828,9 @@ state_init_reverse :: proc(state: ^State) -> (moves: [dynamic]Move) {
}
State_Bytes :: [616]byte
State_Bytes :: [TAROT_COUNT * 2]byte
state_to_bytes :: proc(state: State) -> State_Bytes {
return transmute(State_Bytes)state
return transmute(State_Bytes)canonical_state_from_state(state)
}
state_reverse_single_move :: proc(state: ^State, move: Move) {
@ -820,111 +840,265 @@ state_reverse_single_move :: proc(state: ^State, move: Move) {
state_push(state, move.from, moved_card)
}
state_find_solution :: proc(
state: State,
allocator := context.allocator,
) -> (
solution: [dynamic]Move,
is_solution: bool,
) {
DEPTH_LIMIT :: 5000
TRIAL_LIMIT :: 100000000
solution = make([dynamic]Move, allocator)
searcher := Searcher {
states = make([dynamic]State, allocator),
moves = make([dynamic]Move, allocator),
rejectables = make(map[[616]byte]struct{}, allocator),
}
append(&searcher.states, state)
return solution, inner(&searcher)
state_is_solvable :: proc(state: State) -> (is_solution: bool) {
tracy.Zone()
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
allocator := context.temp_allocator
context.allocator = allocator
TRIAL_LIMIT :: 10_000
NO_PROGRESS_TRIAL_BASE_LIMIT :: 50
NO_PROGRESS_TRIAL_SCALING_LIMIT :: 10
Searcher :: struct {
states: [dynamic]State,
moves: [dynamic]Move,
rejectables: map[[616]byte]struct{},
trials: int,
queue: pq.Priority_Queue(Search_Item),
visited: map[State_Bytes]struct{},
trial_count: int,
no_progress_count: int,
best_distance: int,
}
Search_Item :: struct {
state: ^State,
cost: int,
distance: int,
}
inner :: proc(searcher: ^Searcher) -> (is_solution: bool) {
if len(searcher.moves) > DEPTH_LIMIT {return false}
searcher.trials += 1
if searcher.trials > TRIAL_LIMIT {return false}
state := slice.last(searcher.states[:])
if state_is_solved(&state) {return true}
// log.debugf("depth: %d, state: %v", len(searcher.moves), state)
searcher := Searcher {
visited = make(map[State_Bytes]struct{}, 128),
}
pq.init(&searcher.queue, less = proc(a, b: Search_Item) -> bool {
return a.distance + a.cost < b.distance + b.cost
}, swap = pq.default_swap_proc(Search_Item), capacity = 2048)
recurse :: proc(searcher: ^Searcher, move: Move) -> (is_solution: bool) {
state := slice.last(searcher.states[:])
if _, ok := state_perform_move(
&state,
move,
auto_make_stacks = true,
allocator = context.temp_allocator,
); ok {
for previous_state in searcher.states {
if reflect.eq(
previous_state,
state,
including_indirect_array_recursion = true,
recursion_level = 4,
) {return false}
}
if transmute([616]byte)state in searcher.rejectables {return false}
append(&searcher.moves, move)
append(&searcher.states, state)
if inner(searcher) {return true}
searcher.rejectables[transmute([616]byte)state] = {}
pop(&searcher.states)
pop(&searcher.moves)
state := state
state_make_stacks(&state)
searcher.best_distance = state_solution_distance(state)
pq.push(
&searcher.queue,
Search_Item{state = new_clone(state), distance = searcher.best_distance},
)
for search_item in pq.pop_safe(&searcher.queue) {
searcher.trial_count += 1
if searcher.trial_count > TRIAL_LIMIT {return false}
// log.debugf("search item: %v", search_item)
state := search_item.state
if search_item.distance < searcher.best_distance {
searcher.best_distance = search_item.distance
searcher.no_progress_count = 0
log.debugf("new best distance: %d", searcher.best_distance)
} else {
searcher.no_progress_count += 1
if searcher.no_progress_count >
NO_PROGRESS_TRIAL_BASE_LIMIT +
(searcher.trial_count / NO_PROGRESS_TRIAL_SCALING_LIMIT) {
return false
}
return false
}
tracy.Plot("queue_len", f64(pq.len(searcher.queue)))
tracy.Plot("best_distance", f64(searcher.best_distance))
tracy.Plot("trial_count", f64(searcher.trial_count))
tracy.Plot("no_progress_count", f64(searcher.no_progress_count))
if state_is_solved(search_item.state^) {return true}
// NOTE(lilith): Move things out of the aside
if len(state.aside) > 0 {
for &to_column, to_column_index in state.columns {
move := Move {
card = state.aside[0],
from = {.Aside, 0},
to = {.Column, to_column_index},
to = {.Column, i8(to_column_index)},
}
if recurse(searcher, move) {return true}
add_move(&searcher, search_item, move)
}
}
has_empty_columns := false
for column in state.columns {if len(column) == 0 {has_empty_columns = true; break}}
for &from_column, from_column_index in state.columns {
if len(from_column) == 0 {continue}
// NOTE(lilith): Move cards between columns
for &to_column, to_column_index in state.columns {
if len(from_column) == 1 && len(to_column) == 0 {continue}
move := Move {
card = slice.last(from_column[:]),
from = {.Column, from_column_index},
to = {.Column, to_column_index},
from = {.Column, i8(from_column_index)},
to = {.Column, i8(to_column_index)},
}
if recurse(searcher, move) {return true}
add_move(&searcher, search_item, move)
}
if !has_empty_columns {
// NOTE(lilith): Move cards into the aside
{
move := Move {
card = slice.last(from_column[:]),
from = {.Column, from_column_index},
from = {.Column, i8(from_column_index)},
to = {.Suit, 0},
}
if recurse(searcher, move) {return true}
add_move(&searcher, search_item, move)
}
}
return false
}
add_move :: proc(searcher: ^Searcher, search_item: Search_Item, move: Move) {
state := search_item.state^
if !state_perform_move(&state, move, auto_make_stacks = true) {return}
_, _, just_inserted, _ := map_entry(&searcher.visited, state_to_bytes(state))
if !just_inserted {return}
pq.push(
&searcher.queue,
Search_Item {
state = new_clone(state),
distance = state_solution_distance(state),
cost = search_item.cost + 1,
},
)
}
return false
}
canonical_state_from_state :: proc(state: State) -> (canonical: Canonical_State) #no_bounds_check {
state := state
Sort_Data :: struct {
column_idcs: [COLUMN_COUNT]int,
columns: ^[COLUMN_COUNT]Column,
}
sort_data := Sort_Data {
columns = &state.columns,
}
for &column_index, i in sort_data.column_idcs {column_index = i}
it := sort.Interface {
collection = &sort_data,
len = proc(_: sort.Interface) -> int {return COLUMN_COUNT},
less = proc(it: sort.Interface, a, b: int) -> bool {
sort_data := cast(^Sort_Data)it.collection
col_a, col_b :=
sort_data.columns[sort_data.column_idcs[a]],
sort_data.columns[sort_data.column_idcs[b]]
if len(col_a) < len(col_b) {return true}
return len(col_a) > 0 && len(col_b) > 0 && col_a[0] < col_b[0]
},
swap = proc(it: sort.Interface, a, b: int) {
sort_data := cast(^Sort_Data)it.collection
s := sort_data.column_idcs
s[a], s[b] = s[b], s[a]
},
}
sort.sort(it)
for sorted_index, column_index in sort_data.column_idcs {
column := state.columns[sorted_index]
for handle, height in column {
assert(height < COLUMN_HEIGHT)
assert(column_index < COLUMN_COUNT)
canonical.places[int(handle)] = {
height = height,
col = column_index,
is_in_column = true,
}
}
}
if len(state.aside) != 0 {
canonical.places[int(state.aside[0])] = {
is_aside = true,
}
}
return canonical
}
state_solution_distance :: proc(state: State) -> (distance: int) #no_bounds_check {
state := state
open_cards: bit_set[Card_Handle(0) ..< TAROT_COUNT]
open_card_list := [6]Card_Handle {
0 ..< 6 = TAROT_COUNT,
}
for &minor_pile, suit_index in state.minor {
suit := Suit(suit_index + 1)
next_number :=
len(minor_pile) == 0 ? 1 : card_from_handle(slice.last(minor_pile[:])).number + 1
if next_number <= MINOR_COUNT {
open_cards += {handle_from_card({suit, next_number})}
open_card_list[suit_index] = handle_from_card({suit, next_number})
}
}
for &major_pile, major_pile_kind in state.major {
next_number: i8
if major_pile_kind == .Low {
next_number =
len(major_pile) == 0 ? 0 : card_from_handle(slice.last(major_pile[:])).number + 1
} else {
next_number =
len(major_pile) == 0 ? MAJOR_COUNT - 1 : card_from_handle(slice.last(major_pile[:])).number - 1
}
if 0 <= next_number && next_number < MAJOR_COUNT {
open_cards += {handle_from_card({.Major, next_number})}
open_card_list[4 + int(major_pile_kind)] = handle_from_card({.Major, next_number})
}
}
// NOTE(lilith): Don't care about cards in aside here, those shouldn't even be there
bury_depth_sums: #simd[16]i8
lens: #simd[16]i8
for column, column_index in state.columns {
lens = simd.replace(lens, column_index, i8(len(column)))
}
highest_column := simd.reduce_max(lens)
column_ptrs := (#simd[16]rawptr)(
(#simd[16]uintptr)(#simd[16]rawptr{0 ..< COLUMN_COUNT = &state.columns}) +
simd.indices(#simd[16]uintptr) * size_of(Column),
)
column_mask := #simd[16]b8 {
0 ..< COLUMN_COUNT = true,
}
for height in i8(0) ..< highest_column {
depth := lens - (height + 1)
handles := simd.gather(
column_ptrs,
#simd[16]Card_Handle{0 ..< 16 = TAROT_COUNT + 1},
column_mask,
)
for open_card_handle in open_card_list {
eq: #simd[16]u8 = simd.lanes_eq(
handles,
#simd[16]Card_Handle{0 ..< 16 = open_card_handle},
)
under_len := simd.lanes_lt(#simd[16]i8{0 ..< 16 = height}, lens)
mask := simd.bit_and(eq, under_len)
bury_depth_sums += simd.select(mask, depth, #simd[16]i8{0 ..< 16 = 0})
}
column_ptrs = (#simd[16]rawptr)(
(#simd[16]uintptr)(column_ptrs) + uintptr(size_of(Card_Handle)),
)
}
bury_depth := int(simd.reduce_add_bisect(bury_depth_sums))
cards_in_play := int(simd.reduce_add_bisect(lens)) + len(state.aside)
// check_depth: int
// for column, column_index in state.columns {
// for handle, height in column {
// for open_handle in open_card_list {
// if open_handle == handle {
// depth := len(column) - (height + 1)
// check_depth += depth
// }
// }
// }
// }
// assert(bury_depth == check_depth)
return 10 * cards_in_play + bury_depth
}
state_difficulty_score :: proc(state: ^State) -> int {
return(
10000 * len(state.aside) +
10 * state_nonsequential_count(state) -
0 * state_trivially_removable_count(state) +
0 * state_trivially_removable_count(state^) +
0 * state_stacking_score(state) \
)
}
@ -956,19 +1130,20 @@ state_nonsequential_count :: proc(state: ^State) -> (count: int) {
}
state_trivially_removable_count :: proc(state: ^State) -> (count: int) {
state_trivially_removable_count :: proc(state: State) -> (count: int) {
state := state
moves := make([dynamic]Move, context.temp_allocator)
if len(state.aside) > 1 {
for &column, column_index in state.columns {
copied := state^
copied := state
moves := make([dynamic]Move, context.temp_allocator)
state_make_stacks(&copied, &moves)
state_make_stacks_with_effects(&copied, &moves)
count = max(count, len(moves))
clear(&moves)
}
}
copied := state^
state_make_stacks(&copied, &moves)
copied := state
state_make_stacks_with_effects(&copied, &moves)
return len(moves)
}
@ -1013,7 +1188,8 @@ state_major_inversion_count :: proc(state: ^State) -> (count: int) {
return count
}
state_is_solved :: proc(state: ^State) -> bool {
state_is_solved :: proc(state: State) -> bool {
state := state
for &column in state.columns {
if len(column) > 0 {return false}
}
@ -1021,7 +1197,7 @@ state_is_solved :: proc(state: ^State) -> bool {
return true
}
state_perform_move :: proc(
state_perform_move_with_effects :: proc(
state: ^State,
move: Move,
auto_make_stacks := true,
@ -1053,41 +1229,67 @@ state_perform_move :: proc(
}
}
if auto_make_stacks {
state_make_stacks(state, &auto_moves)
state_make_stacks_with_effects(state, &auto_moves)
}
return auto_moves, true
}
state_make_stacks :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
state_perform_move :: proc(state: ^State, move: Move, auto_make_stacks := true) -> (ok: bool) {
if !state_apply_single_move(state, move) {return false}
if move.from.kind == .Column && move.to.kind == .Column {
from_col := &state.columns[move.from.index]
last_moved_handle := move.card
last_moved_card := card_from_handle(move.card)
for len(from_col) > 0 {
next_handle := slice.last(from_col[:])
next_card := card_from_handle(next_handle)
if next_card.suit == last_moved_card.suit &&
abs(next_card.number - last_moved_card.number) == 1 {
next_move := move
next_move.card = next_handle
state_apply_single_move(state, next_move)
last_moved_handle = next_handle
last_moved_card = next_card
} else {break}
}
}
if auto_make_stacks {
state_make_stacks(state)
}
return true
}
state_make_stacks_with_effects :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
big_loop: for {
for &column, column_index in state.columns {
if len(column) == 0 {continue}
handle := slice.last(column[:])
card := card_from_handle(handle)
if state_can_stack_suit(state, handle) {
if state_can_stack_suit(state^, handle) {
move := Move {
card = handle,
from = {.Column, column_index},
to = {.Suit, int(card.suit) - 1},
from = {.Column, i8(column_index)},
to = {.Suit, i8(card.suit) - 1},
}
state_apply_single_move(state, move)
append(auto_moves, move)
continue big_loop
}
if state_can_stack_major_low(state, handle) {
if state_can_stack_major_low(state^, handle) {
move := Move {
card = handle,
from = {.Column, column_index},
from = {.Column, i8(column_index)},
to = {.Major, 0},
}
state_apply_single_move(state, move)
append(auto_moves, move)
continue big_loop
}
if state_can_stack_major_high(state, handle) {
if state_can_stack_major_high(state^, handle) {
move := Move {
card = handle,
from = {.Column, column_index},
from = {.Column, i8(column_index)},
to = {.Major, 1},
}
state_apply_single_move(state, move)
@ -1099,8 +1301,46 @@ state_make_stacks :: proc(state: ^State, auto_moves: ^[dynamic]Move) {
}
}
state_make_stacks :: proc(state: ^State) {
big_loop: for {
for &column, column_index in state.columns {
if len(column) == 0 {continue}
handle := slice.last(column[:])
card := card_from_handle(handle)
if state_can_stack_suit(state^, handle) {
move := Move {
card = handle,
from = {.Column, i8(column_index)},
to = {.Suit, i8(card.suit) - 1},
}
state_apply_single_move(state, move)
continue big_loop
}
if state_can_stack_major_low(state^, handle) {
move := Move {
card = handle,
from = {.Column, i8(column_index)},
to = {.Major, 0},
}
state_apply_single_move(state, move)
continue big_loop
}
if state_can_stack_major_high(state^, handle) {
move := Move {
card = handle,
from = {.Column, i8(column_index)},
to = {.Major, 1},
}
state_apply_single_move(state, move)
continue big_loop
}
}
break
}
}
state_apply_single_move :: proc(state: ^State, move: Move) -> (ok: bool) {
if !state_is_move_valid(state, move) {return false}
if !state_is_move_valid(state^, move) {return false}
moved_card, pop_ok := state_pop(state, move.from)
assert(pop_ok)
assert(moved_card == move.card)
@ -1115,7 +1355,7 @@ state_pop :: proc(state: ^State, place: Place) -> (handle: Card_Handle, ok: bool
case .Major:
return pop_safe(&state.major[Major_Pile_Kind(place.index)])
case .Suit:
return pop_safe(&state.suits[place.index])
return pop_safe(&state.minor[place.index])
case .Aside:
return pop_safe(&state.aside)
}
@ -1128,16 +1368,16 @@ state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
append(&state.columns[place.index], handle)
case .Major:
card := card_from_handle(handle)
if state_can_stack_major_low(state, handle) {
if state_can_stack_major_low(state^, handle) {
append(&state.major[.Low], handle)
} else {
append(&state.major[.High], handle)
}
case .Suit:
if state_can_stack_suit(state, handle) {
if state_can_stack_suit(state^, handle) {
suit_index := int(card_from_handle(handle).suit) - 1
suit_pile := &state.suits[suit_index]
append(&state.suits[suit_index], handle)
suit_pile := &state.minor[suit_index]
append(&state.minor[suit_index], handle)
} else {
append(&state.aside, handle)
}
@ -1146,38 +1386,42 @@ state_push :: proc(state: ^State, place: Place, handle: Card_Handle) {
}
}
state_can_stack_suit :: proc(state: ^State, handle: Card_Handle) -> bool {
state_can_stack_suit :: proc(state: State, handle: Card_Handle) -> bool {
state := state
if len(state.aside) > 0 {return false}
card := card_from_handle(handle)
if card.suit == .Major {return false}
suit_index := int(card.suit) - 1
suit_pile := &state.suits[suit_index]
suit_pile := &state.minor[suit_index]
if len(suit_pile) == 0 {return card.number == 1}
return card.number - 1 == card_from_handle(slice.last(suit_pile[:])).number
}
state_can_stack_major_low :: proc(state: ^State, handle: Card_Handle) -> bool {
state_can_stack_major_low :: proc(state: State, handle: Card_Handle) -> bool {
state := state
card := card_from_handle(handle)
if card.suit != .Major {return false}
if len(state.major[.Low]) == 0 {return card.number == 0}
return card.number - 1 == card_from_handle(slice.last(state.major[.Low][:])).number
}
state_can_stack_major_high :: proc(state: ^State, handle: Card_Handle) -> bool {
state_can_stack_major_high :: proc(state: State, handle: Card_Handle) -> bool {
state := state
card := card_from_handle(handle)
if card.suit != .Major {return false}
if len(state.major[.High]) == 0 {return card.number == MAJOR_COUNT - 1}
return card.number + 1 == card_from_handle(slice.last(state.major[.High][:])).number
}
state_is_move_valid :: proc(state: ^State, move: Move) -> (ok: bool) {
state_is_move_valid :: proc(state: State, move: Move) -> (ok: bool) {
state := state
if move.from == move.to {return false}
moved_card := card_from_handle(move.card)
from_pile := state_get_pile(state, move.from)
from_pile := state_get_pile(&state, move.from)
if len(from_pile) == 0 {return false}
if slice.last(from_pile) != move.card {return false}
to_pile := state_get_pile(state, move.to)
to_pile := state_get_pile(&state, move.to)
#partial switch move.from.kind {
case .Major, .Suit:
return false
@ -1217,7 +1461,7 @@ state_is_move_valid_reverse :: proc(state: ^State, move: Move) -> (ok: bool) {
}
case .Aside:
if len(from_pile) > 0 {return false}
if state_can_stack_suit(state, move.card) {return false}
if state_can_stack_suit(state^, move.card) {return false}
}
#partial switch move.to.kind {
case .Column:
@ -1242,7 +1486,7 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
return state.major[Major_Pile_Kind(place.index)][:]
case .Suit:
assert(0 <= place.index && place.index < 4)
return state.suits[place.index][:]
return state.minor[place.index][:]
case .Aside:
return state.aside[:]
}
@ -1251,19 +1495,28 @@ state_get_pile :: proc(state: ^State, place: Place) -> (pile: []Card_Handle) {
state_card_place :: proc(state: ^State, handle: Card_Handle) -> Place {
for column_index in 0 ..< COLUMN_COUNT {
if slice.contains(state.columns[column_index][:], handle) {return {.Column, column_index}}
if slice.contains(
state.columns[column_index][:],
handle,
) {return {.Column, i8(column_index)}}
}
for &major_pile, major_pile_kind in state.major {
if slice.contains(major_pile[:], handle) {return {.Major, int(major_pile_kind)}}
if slice.contains(major_pile[:], handle) {return {.Major, i8(major_pile_kind)}}
}
for suit in 0 ..< 4 {
if slice.contains(state.suits[suit][:], handle) {return {.Suit, suit}}
if slice.contains(state.minor[suit][:], handle) {return {.Suit, i8(suit)}}
}
if slice.contains(state.aside[:], handle) {return {.Aside, 0}}
unreachable()
}
card_from_handle :: proc(handle: Card_Handle) -> Card {return game.cards[int(handle)]}
card_from_handle :: proc(handle: Card_Handle) -> Card #no_bounds_check {return CARDS[int(handle)]}
handle_from_card :: proc(card: Card) -> Card_Handle {
return Card_Handle(
card.suit == .Major ? card.number : (MAJOR_COUNT + (i8(card.suit) - 1) * MINOR_COUNT + card.number - 1),
)
}
card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
global_offset := Vec2{GAP, GAP}
@ -1288,7 +1541,7 @@ card_position :: proc(place: Place, pile_index: int = 0) -> (position: Vec2) {
place_from_position :: proc(p: Vec2) -> (place: Place) {
for column_index in 0 ..< COLUMN_COUNT {
place := Place{.Column, column_index}
place := Place{.Column, i8(column_index)}
pos := card_position(place) - CARD_SIZE * 0.5 - GAP * 0.5
rect := rrect_from_pos_sz(pos, {CARD_SIZE.x + GAP, 10000})
if rect_contains(rect, p) {return place}
@ -1328,7 +1581,7 @@ game_cards_zipped :: proc() -> #soa[]struct {
card: Card,
entity: Card_Entity,
} {
return soa_zip(card = game.cards[:], entity = game.entities[:])
return soa_zip(card = CARDS[:], entity = game.entities[:])
}
text_color :: proc(card: Card) -> ray.Color {

85
src/tracy/allocator.odin Normal file
View file

@ -0,0 +1,85 @@
package tracy
import "core:c"
import "core:mem"
ProfiledAllocatorData :: struct {
backing_allocator: mem.Allocator,
profiled_allocator: mem.Allocator,
callstack_size: i32,
name: cstring,
}
MakeProfiledAllocator :: proc(
self: ^ProfiledAllocatorData,
callstack_size: i32 = TRACY_CALLSTACK,
secure: b32 = true, // @obsolete
backing_allocator := context.allocator,
) -> mem.Allocator {
self.callstack_size = callstack_size
self.backing_allocator = backing_allocator
self.profiled_allocator = mem.Allocator {
data = self,
procedure = proc(
allocator_data: rawptr,
mode: mem.Allocator_Mode,
size, alignment: int,
old_memory: rawptr,
old_size: int,
location := #caller_location,
) -> (
[]byte,
mem.Allocator_Error,
) {
using self := cast(^ProfiledAllocatorData)allocator_data
new_memory, error := self.backing_allocator.procedure(
self.backing_allocator.data,
mode,
size,
alignment,
old_memory,
old_size,
location,
)
if error == .None {
switch mode {
case .Alloc, .Alloc_Non_Zeroed:
AllocN(raw_data(new_memory), uint(size), self.name, callstack_size)
case .Free:
FreeN(old_memory, self.name, callstack_size)
case .Free_All:
// NOTE: Free_All not supported by this allocator
case .Resize, .Resize_Non_Zeroed:
FreeN(old_memory, self.name, callstack_size)
AllocN(raw_data(new_memory), uint(size), self.name, callstack_size)
case .Query_Info:
// TODO
case .Query_Features:
// TODO
}
}
return new_memory, error
},
}
return self.profiled_allocator
}
@(private = "file", no_instrumentation = true)
EmitAlloc :: #force_inline proc(
new_memory: []byte,
size: int,
callstack_size: i32 = TRACY_CALLSTACK,
) {
when TRACY_ENABLE {
___tracy_emit_memory_alloc_callstack(raw_data(new_memory), c.size_t(size), callstack_size)
}
}
@(private = "file", no_instrumentation = true)
EmitFree :: #force_inline proc(old_memory: rawptr, callstack_size: i32 = TRACY_CALLSTACK) {
when TRACY_ENABLE {
if old_memory == nil {return}
___tracy_emit_memory_free_callstack(old_memory, callstack_size)
}
}

209
src/tracy/bindings.odin Normal file
View file

@ -0,0 +1,209 @@
package tracy
import "core:c"
// NOTE(oskar): You need to recompile the client library with the same value if you change these.
TRACY_MANUAL_LIFETIME :: #config(TRACY_MANUAL_LIFETIME, false)
TRACY_FIBERS :: #config(TRACY_FIBERS, false)
when ODIN_OS == .Darwin do foreign import tracy "tracy.dylib"
when ODIN_OS == .Windows do foreign import tracy "tracy.lib"
when ODIN_OS == .Linux do foreign import tracy "tracy.so"
// NOTE(oskar): Opaque
__tracy_lockable_context_data :: struct {}
__tracy_shared_lockable_context_data :: struct {}
TracyPlotFormatEnum :: enum i32 {
Number,
Memory,
Percentage,
Watt,
}
TracyMessageSeverity :: enum i32 {
Trace, // Broadly track variable states and events in the software program.
Debug, // Describes variable states and details about specific internal events in the software, that are useful for investigations.
Info, // Describes normal events, which inform on the expected progress and state of your software.
Warning, // Describes potentially dangerous situations caused by unexpected events and states.
Error, // Describes the occurance of unexpected behavior. Does not interrupt the execution of the software.
Fatal, // Describes a critical event that will lead to a software failure/crash.
}
___tracy_source_location_data :: struct {
name: cstring,
function: cstring,
file: cstring,
line: u32,
color: u32,
}
___tracy_c_zone_context :: struct {
id: u32,
active: b32,
}
___tracy_gpu_time_data :: struct {
gpuTime: i64,
queryId: u16,
_context: u8,
}
___tracy_gpu_zone_begin_data :: struct {
srcloc: u64,
queryId: u16,
_context: u8,
}
___tracy_gpu_zone_begin_callstack_data :: struct {
srcloc: u64,
depth: i32,
queryId: u16,
_context: u8,
}
___tracy_gpu_zone_end_data :: struct {
queryId: u16,
_context: u8,
}
___tracy_gpu_new_context_data :: struct {
gpuTime: i64,
period: f32,
_context: u8,
flags: u8,
type: u8,
}
___tracy_gpu_context_name_data :: struct {
_context: u8,
name: cstring,
len: u16,
}
___tracy_gpu_calibration_data :: struct {
gpuTime: i64,
cpuDelta: i64,
_context: u8,
}
___tracy_gpu_time_sync_data :: struct {
gpuTime: i64,
_context: u8,
}
when TRACY_ENABLE {
when TRACY_MANUAL_LIFETIME {
@(default_calling_convention = "c")
foreign tracy {
___tracy_startup_profiler :: proc() ---
___tracy_shutdown_profiler :: proc() ---
___tracy_profiler_started :: proc() -> i32 ---
}
}
@(default_calling_convention = "c")
foreign tracy {
___tracy_set_thread_name :: proc(name: cstring) ---
___tracy_alloc_srcloc :: proc(line: u32, source: cstring, sourceSz: c.size_t, function: cstring, functionSz: c.size_t, color: u32 = 0) -> u64 ---
___tracy_alloc_srcloc_name :: proc(line: u32, source: cstring, sourceSz: c.size_t, function: cstring, functionSz: c.size_t, name: cstring, nameSz: c.size_t, color: u32 = 0) -> u64 ---
___tracy_emit_zone_begin :: proc(srcloc: ^___tracy_source_location_data, active: b32) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_callstack :: proc(srcloc: ^___tracy_source_location_data, depth: i32, active: b32) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_alloc :: proc(srcloc: u64, active: b32) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_alloc_callstack :: proc(srcloc: u64, depth: i32, active: b32) -> ___tracy_c_zone_context ---
___tracy_emit_zone_end :: proc(ctx: ___tracy_c_zone_context) ---
___tracy_emit_zone_text :: proc(ctx: ___tracy_c_zone_context, txt: cstring, size: c.size_t) ---
___tracy_emit_zone_text_fmt :: proc(ctx: ___tracy_c_zone_context, fmt: cstring, #c_vararg args: ..any) ---
___tracy_emit_zone_name :: proc(ctx: ___tracy_c_zone_context, txt: cstring, size: c.size_t) ---
___tracy_emit_zone_name_fmt :: proc(ctx: ___tracy_c_zone_context, fmt: cstring, #c_vararg args: ..any) ---
___tracy_emit_zone_color :: proc(ctx: ___tracy_c_zone_context, color: u32) ---
___tracy_emit_zone_value :: proc(ctx: ___tracy_c_zone_context, value: u64) ---
___tracy_emit_gpu_zone_begin :: proc(_: ___tracy_gpu_zone_begin_data) ---
___tracy_emit_gpu_zone_begin_callstack :: proc(_: ___tracy_gpu_zone_begin_callstack_data) ---
___tracy_emit_gpu_zone_begin_alloc :: proc(_: ___tracy_gpu_zone_begin_data) ---
___tracy_emit_gpu_zone_begin_alloc_callstack :: proc(_: ___tracy_gpu_zone_begin_callstack_data) ---
___tracy_emit_gpu_zone_end :: proc(_: ___tracy_gpu_zone_end_data) ---
___tracy_emit_gpu_time :: proc(_: ___tracy_gpu_time_data) ---
___tracy_emit_gpu_new_context :: proc(_: ___tracy_gpu_new_context_data) ---
___tracy_emit_gpu_context_name :: proc(_: ___tracy_gpu_context_name_data) ---
___tracy_emit_gpu_calibration :: proc(_: ___tracy_gpu_calibration_data) ---
___tracy_emit_gpu_time_sync :: proc(_: ___tracy_gpu_time_sync_data) ---
___tracy_emit_gpu_zone_begin_serial :: proc(_: ___tracy_gpu_zone_begin_data) ---
___tracy_emit_gpu_zone_begin_callstack_serial :: proc(_: ___tracy_gpu_zone_begin_callstack_data) ---
___tracy_emit_gpu_zone_begin_alloc_serial :: proc(_: ___tracy_gpu_zone_begin_data) ---
___tracy_emit_gpu_zone_begin_alloc_callstack_serial :: proc(_: ___tracy_gpu_zone_begin_callstack_data) ---
___tracy_emit_gpu_zone_end_serial :: proc(_: ___tracy_gpu_zone_end_data) ---
___tracy_emit_gpu_time_serial :: proc(_: ___tracy_gpu_time_data) ---
___tracy_emit_gpu_new_context_serial :: proc(_: ___tracy_gpu_new_context_data) ---
___tracy_emit_gpu_context_name_serial :: proc(_: ___tracy_gpu_context_name_data) ---
___tracy_emit_gpu_calibration_serial :: proc(_: ___tracy_gpu_calibration_data) ---
___tracy_emit_gpu_time_sync_serial :: proc(_: ___tracy_gpu_time_sync_data) ---
___tracy_emit_memory_alloc :: proc(ptr: rawptr, size: c.size_t) ---
___tracy_emit_memory_alloc_callstack :: proc(ptr: rawptr, size: c.size_t, depth: i32) ---
___tracy_emit_memory_free :: proc(ptr: rawptr) ---
___tracy_emit_memory_free_callstack :: proc(ptr: rawptr, depth: i32) ---
___tracy_emit_memory_alloc_named :: proc(ptr: rawptr, size: c.size_t, name: cstring) ---
___tracy_emit_memory_alloc_callstack_named :: proc(ptr: rawptr, size: c.size_t, depth: i32, name: cstring) ---
___tracy_emit_memory_free_named :: proc(ptr: rawptr, name: cstring) ---
___tracy_emit_memory_free_callstack_named :: proc(ptr: rawptr, depth: i32, name: cstring) ---
___tracy_emit_memory_discard :: proc(name: cstring) ---
___tracy_emit_memory_discard_callstack :: proc(name: cstring, depth: i32) ---
___tracy_emit_logString :: proc(severity: TracyMessageSeverity, color: i32, callstack_depth: i32, size: c.size_t, txt: cstring) ---
___tracy_emit_logStringL :: proc(severity: TracyMessageSeverity, color: i32, callstack_depth: i32, txt: cstring) ---
___tracy_emit_frame_mark :: proc(name: cstring) ---
___tracy_emit_frame_mark_start :: proc(name: cstring) ---
___tracy_emit_frame_mark_end :: proc(name: cstring) ---
___tracy_emit_frame_image :: proc(image: rawptr, w, h: u16, offset: u8, flip: b32) ---
___tracy_emit_plot :: proc(name: cstring, val: f64) ---
___tracy_emit_plot_float :: proc(name: cstring, val: f32) ---
___tracy_emit_plot_int :: proc(name: cstring, val: i64) ---
___tracy_emit_plot_config :: proc(name: cstring, type: TracyPlotFormatEnum, step, fill: b32, color: u32) ---
___tracy_emit_message_appinfo :: proc(txt: cstring, size: c.size_t) ---
___tracy_announce_lockable_ctx :: proc(srcloc: ^___tracy_source_location_data) -> ^__tracy_lockable_context_data ---
___tracy_terminate_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data) ---
___tracy_before_lock_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data) -> b32 ---
___tracy_after_lock_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data) ---
___tracy_after_unlock_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data) ---
___tracy_after_try_lock_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data, acquired: b32) ---
___tracy_mark_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data, srcloc: ^___tracy_source_location_data) ---
___tracy_custom_name_lockable_ctx :: proc(lockdata: ^__tracy_lockable_context_data, name: cstring, nameSz: c.size_t) ---
___tracy_announce_shared_lockable_ctx :: proc(srcloc: ^___tracy_source_location_data) -> ^__tracy_shared_lockable_context_data ---
___tracy_terminate_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) ---
___tracy_before_lock_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) -> b32 ---
___tracy_after_lock_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) ---
___tracy_after_unlock_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) ---
___tracy_after_try_lock_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data, acquired: b32) ---
___tracy_before_lock_shared_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) -> b32 ---
___tracy_after_lock_shared_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) ---
___tracy_after_unlock_shared_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data) ---
___tracy_after_try_lock_shared_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data, acquired: b32) ---
___tracy_mark_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data, srcloc: ^___tracy_source_location_data) ---
___tracy_custom_name_shared_lockable_ctx :: proc(lockdata: ^__tracy_shared_lockable_context_data, name: cstring, nameSz: c.size_t) ---
___tracy_connected :: proc() -> b32 ---
___tracy_begin_sampling_profiling :: proc() -> i32 ---
___tracy_end_sampling_profiling :: proc() ---
___tracy_get_time :: proc() -> i64 ---
}
when TRACY_FIBERS {
@(default_calling_convention = "c")
foreign tracy {
___tracy_fiber_enter :: proc(fiber: cstring) ---
___tracy_fiber_leave :: proc() ---
}
}
}

277
src/tracy/wrapper.odin Normal file
View file

@ -0,0 +1,277 @@
package tracy
import "core:c"
TRACY_ENABLE :: #config(TRACY_ENABLE, false)
TRACY_CALLSTACK :: #config(TRACY_CALLSTACK, 0)
SourceLocationData :: ___tracy_source_location_data
ZoneCtx :: ___tracy_c_zone_context
LockCtx :: __tracy_lockable_context_data
SharedLockCtx :: __tracy_shared_lockable_context_data
// Zone markup
// NOTE: These automatically calls ZoneEnd() at end of scope.
@(deferred_out = ZoneEnd, no_instrumentation = true)
Zone :: #force_inline proc(
active := true,
depth: i32 = TRACY_CALLSTACK,
loc := #caller_location,
) -> (
ctx: ZoneCtx,
) {when TRACY_ENABLE {ctx = ZoneBegin(active, depth, "", 0, loc)}; return}
@(deferred_out = ZoneEnd, no_instrumentation = true)
ZoneN :: #force_inline proc(
name: string,
active := true,
depth: i32 = TRACY_CALLSTACK,
loc := #caller_location,
) -> (
ctx: ZoneCtx,
) {when TRACY_ENABLE {ctx = ZoneBegin(active, depth, name, 0, loc)}; return}
@(deferred_out = ZoneEnd, no_instrumentation = true)
ZoneC :: #force_inline proc(
color: u32,
active := true,
depth: i32 = TRACY_CALLSTACK,
loc := #caller_location,
) -> (
ctx: ZoneCtx,
) {when TRACY_ENABLE {ctx = ZoneBegin(active, depth, "", color, loc)}; return}
@(deferred_out = ZoneEnd, no_instrumentation = true)
ZoneNC :: #force_inline proc(
name: string,
color: u32,
active := true,
depth: i32 = TRACY_CALLSTACK,
loc := #caller_location,
) -> (
ctx: ZoneCtx,
) {when TRACY_ENABLE {ctx = ZoneBegin(active, depth, name, color, loc)}; return}
@(disabled = !TRACY_ENABLE)
ZoneText :: #force_inline proc(
ctx: ZoneCtx,
text: string,
) {when TRACY_ENABLE {when TRACY_ENABLE {___tracy_emit_zone_text(ctx, _sl(text))}}}
@(disabled = !TRACY_ENABLE)
ZoneName :: #force_inline proc(
ctx: ZoneCtx,
name: string,
) {when TRACY_ENABLE {when TRACY_ENABLE {___tracy_emit_zone_name(ctx, _sl(name))}}}
@(disabled = !TRACY_ENABLE)
ZoneColor :: #force_inline proc(
ctx: ZoneCtx,
color: u32,
) {when TRACY_ENABLE {when TRACY_ENABLE {___tracy_emit_zone_color(ctx, color)}}}
@(disabled = !TRACY_ENABLE)
ZoneValue :: #force_inline proc(
ctx: ZoneCtx,
value: u64,
) {when TRACY_ENABLE {when TRACY_ENABLE {___tracy_emit_zone_value(ctx, value)}}}
// NOTE: scoped Zone*() procs also exists, no need of calling this directly.
@(no_instrumentation = true)
ZoneBegin :: #force_inline proc(
active: bool,
depth: i32,
name: string = "",
color: u32 = 0,
loc := #caller_location,
) -> (
ctx: ZoneCtx,
) {
when TRACY_ENABLE {
/* From manual, page 61:
The variable representing an allocated source location is of an opaque type.
After it is passed to one of the zone begin functions, its value cannot be
reused (the variable is consumed). You must allocate a new source location for
each zone begin event, even if the location data would be the same as in the
previous instance.
*/
id := ___tracy_alloc_srcloc_name(
u32(loc.line),
_sl(loc.file_path),
_sl(loc.procedure),
_sl(name),
color,
)
ctx = ___tracy_emit_zone_begin_alloc_callstack(id, depth, b32(active))
}
return
}
// NOTE: scoped Zone*() procs also exists, no need of calling this directly.
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
ZoneEnd :: #force_inline proc(ctx: ZoneCtx) {when TRACY_ENABLE {___tracy_emit_zone_end(ctx)}}
// Memory profiling
// (See allocator.odin for an implementation of an Odin custom allocator using memory profiling.)
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
Alloc :: #force_inline proc(
ptr: rawptr,
size: c.size_t,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_memory_alloc_callstack(ptr, size, depth)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
Free :: #force_inline proc(
ptr: rawptr,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_memory_free_callstack(ptr, depth)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
MemoryDiscard :: #force_inline proc(
name: cstring,
) {when TRACY_ENABLE {___tracy_emit_memory_discard(name)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
AllocN :: #force_inline proc(
ptr: rawptr,
size: c.size_t,
name: cstring,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_memory_alloc_callstack_named(ptr, size, depth, name)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FreeN :: #force_inline proc(
ptr: rawptr,
name: cstring,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_memory_free_callstack_named(ptr, depth, name)}}
// Frame markup
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FrameMark :: #force_inline proc(name: cstring = nil) {when TRACY_ENABLE {___tracy_emit_frame_mark(
name,
)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FrameMarkStart :: #force_inline proc(
name: cstring,
) {when TRACY_ENABLE {___tracy_emit_frame_mark_start(name)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FrameMarkEnd :: #force_inline proc(
name: cstring,
) {when TRACY_ENABLE {___tracy_emit_frame_mark_end(name)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FrameImage :: #force_inline proc(
image: rawptr,
w, h: u16,
offset: u8,
flip: b32,
) {when TRACY_ENABLE {___tracy_emit_frame_image(image, w, h, offset, flip)}}
// Plots
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
Plot :: #force_inline proc(name: cstring, value: f64) {when TRACY_ENABLE {___tracy_emit_plot(
name,
value,
)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
PlotF :: #force_inline proc(
name: cstring,
value: f32,
) {when TRACY_ENABLE {___tracy_emit_plot_float(name, value)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
PlotI :: #force_inline proc(name: cstring, value: i64) {when TRACY_ENABLE {___tracy_emit_plot_int(
name,
value,
)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
PlotConfig :: #force_inline proc(
name: cstring,
type: TracyPlotFormatEnum,
step, fill: b32,
color: u32,
) {when TRACY_ENABLE {___tracy_emit_plot_config(name, type, step, fill, color)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
AppInfo :: #force_inline proc(name: string) {when TRACY_ENABLE {___tracy_emit_message_appinfo(
_sl(name),
)}}
// Messages
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
MessageS :: #force_inline proc(
txt: string,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_logString(.Info, 0, depth, _ls(txt))}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
MessageCS :: #force_inline proc(
txt: string,
color: u32 = 0,
depth: i32 = TRACY_CALLSTACK,
) {when TRACY_ENABLE {___tracy_emit_logString(.Info, i32(color), depth, _ls(txt))}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
SetThreadName :: #force_inline proc(name: cstring) {when TRACY_ENABLE {___tracy_set_thread_name(
name,
)}}
// Connection status
IsConnected :: #force_inline proc() -> bool {return(
cast(bool)___tracy_connected() when TRACY_ENABLE else false \
)}
// Fibers
when TRACY_FIBERS {
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FiberEnter :: #force_inline proc(name: cstring) {when TRACY_ENABLE {___tracy_fiber_enter(
name,
)}}
@(disabled = !TRACY_ENABLE, no_instrumentation = true)
FiberLeave :: #force_inline proc() {when TRACY_ENABLE {___tracy_fiber_leave()}}
}
// These are also available but no higher level wrapper provided at this time.
/*
___tracy_emit_gpu_zone_begin
___tracy_emit_gpu_zone_begin_callstack
___tracy_emit_gpu_zone_begin_alloc
___tracy_emit_gpu_zone_begin_alloc_callstack
___tracy_emit_gpu_zone_end
___tracy_emit_gpu_time
___tracy_emit_gpu_new_context
___tracy_emit_gpu_context_name
___tracy_emit_gpu_calibration
___tracy_emit_gpu_time_sync
___tracy_emit_gpu_zone_begin_serial
___tracy_emit_gpu_zone_begin_callstack_serial
___tracy_emit_gpu_zone_begin_alloc_serial
___tracy_emit_gpu_zone_begin_alloc_callstack_serial
___tracy_emit_gpu_zone_end_serial
___tracy_emit_gpu_time_serial
___tracy_emit_gpu_new_context_serial
___tracy_emit_gpu_context_name_serial
___tracy_emit_gpu_calibration_serial
___tracy_emit_gpu_time_sync_serial
___tracy_announce_lockable_ctx
___tracy_terminate_lockable_ctx
___tracy_before_lock_lockable_ctx
___tracy_after_lock_lockable_ctx
___tracy_after_unlock_lockable_ctx
___tracy_after_try_lock_lockable_ctx
___tracy_mark_lockable_ctx
___tracy_custom_name_lockable_ctx
___tracy_announce_shared_lockable_ctx
___tracy_terminate_shared_lockable_ctx
___tracy_before_lock_shared_lockable_ctx
___tracy_after_lock_shared_lockable_ctx
___tracy_after_unlock_shared_lockable_ctx
___tracy_after_try_lock_shared_lockable_ctx
___tracy_before_lock_shared_shared_lockable_ctx
___tracy_after_lock_shared_shared_lockable_ctx
___tracy_after_unlock_shared_shared_lockable_ctx
___tracy_after_try_lock_shared_shared_lockable_ctx
___tracy_mark_shared_lockable_ctx
___tracy_custom_name_shared_lockable_ctx
*/
// Helper for passing cstring+length to Tracy functions.
@(no_instrumentation = true)
@(private = "file")_sl :: proc(s: string) -> (cstring, c.size_t) {return cstring(raw_data(s)),
c.size_t(len(s))}
// Helper for passing length+cstring to Tracy functions.
@(no_instrumentation = true)
@(private = "file")_ls :: proc(s: string) -> (c.size_t, cstring) {return c.size_t(len(s)),
cstring(raw_data(s))}