Asset slots with hot-reloading
This commit is contained in:
+10
-4
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package game
|
||||
|
||||
import "core:fmt"
|
||||
@@ -60,16 +66,16 @@ register_memory :: proc() {
|
||||
base.register_region("test_memory", &t, size_of(Test_Memory))
|
||||
}
|
||||
|
||||
init_once :: proc() {
|
||||
fmt.println("init once")
|
||||
}
|
||||
|
||||
init :: proc() {
|
||||
fmt.println("hello from .dll!")
|
||||
|
||||
g.should_close = false
|
||||
}
|
||||
|
||||
init_once :: proc() {
|
||||
fmt.println("init once")
|
||||
}
|
||||
|
||||
update :: proc(dt: f32) {
|
||||
fmt.println("dt:", dt)
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ MAGIC_STRING :: 0x72646E73 // "rdns" -> "sndr" in little endian
|
||||
HOT_RELOAD_VERSION :: 1
|
||||
APP_VERSION_SIZE :: 4 + time.MIN_YYYY_DATE_LEN + 8 + 6 // example: `dev-2026-09-23-a4084b3-debug`
|
||||
|
||||
DIGEST_SIZE :: xxhash.XXH64_hash
|
||||
DIGEST :: xxhash.XXH64_hash
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
@@ -44,7 +44,7 @@ Region_Header :: struct #packed #all_or_none {
|
||||
Memory_Region :: struct {
|
||||
ptr_location: rawptr,
|
||||
// TODO: How do we do hash compare without having to hash current memory every frame?
|
||||
digest: DIGEST_SIZE, // hash of this region in last snapshot
|
||||
digest: DIGEST, // hash of this region in last snapshot
|
||||
}
|
||||
|
||||
@(private)
|
||||
|
||||
+157
-21
@@ -8,9 +8,16 @@ package core
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:hash/xxhash"
|
||||
import "core:os"
|
||||
|
||||
import "sindri:base"
|
||||
|
||||
ASSET_NIL :: 0
|
||||
ASSETS_MAX :: 512
|
||||
|
||||
DIGEST :: xxhash.XXH64_hash
|
||||
|
||||
// -----------------------------------------
|
||||
// Types
|
||||
|
||||
@@ -30,51 +37,180 @@ Texture :: struct {
|
||||
data_type: u32,
|
||||
}
|
||||
|
||||
Asset_Idx :: u16
|
||||
|
||||
Asset_Ref :: struct {
|
||||
// TODO: 22 bits index, 10 bits generation, so we can store it in one 32bit number
|
||||
idx: Asset_Idx,
|
||||
gen: u16,
|
||||
}
|
||||
|
||||
Asset_Kind :: enum {
|
||||
Model,
|
||||
Texture,
|
||||
}
|
||||
|
||||
Asset :: struct {
|
||||
kind: Asset_Kind,
|
||||
|
||||
digest: DIGEST,
|
||||
|
||||
data: union #no_nil {
|
||||
Model,
|
||||
Texture,
|
||||
},
|
||||
}
|
||||
|
||||
Assets :: struct {
|
||||
allocator: runtime.Allocator,
|
||||
model_list: map[string]Model,
|
||||
texture_list: map[string]Texture,
|
||||
assets: [ASSETS_MAX]Asset,
|
||||
// TODO: Collapse used and gen into 1 array
|
||||
used: [ASSETS_MAX]bool,
|
||||
gen: [ASSETS_MAX]u16,
|
||||
free_list: [ASSETS_MAX]Asset_Idx,
|
||||
next_free: Asset_Idx, // first free'd slot
|
||||
next_unused: Asset_Idx, // never previously used slot
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// Constructor/destructor
|
||||
|
||||
assets_init :: proc(allocator := context.allocator) -> Assets {
|
||||
return {
|
||||
allocator = allocator,
|
||||
model_list = make(map[string]Model, allocator),
|
||||
texture_list = make(map[string]Texture, allocator),
|
||||
}
|
||||
return {allocator = allocator}
|
||||
}
|
||||
|
||||
assets_destroy :: proc(a: ^Assets) {
|
||||
for _, &model in a.model_list {
|
||||
delete(model.vertices, a.allocator)
|
||||
delete(model.indices, a.allocator)
|
||||
for &asset, i in a.assets {
|
||||
if !a.used[i] do continue
|
||||
m, ok := asset.data.(Model)
|
||||
if !ok do continue
|
||||
delete(m.vertices, a.allocator)
|
||||
delete(m.indices, a.allocator)
|
||||
}
|
||||
|
||||
for _, &texture in a.texture_list {
|
||||
}
|
||||
|
||||
delete(a.model_list)
|
||||
delete(a.texture_list)
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
load_model :: proc(a: ^Assets, path: string) -> ^Model {
|
||||
if path in a.model_list do return &a.model_list[path]
|
||||
asset_add :: proc(a: ^Assets) -> Asset_Ref {
|
||||
slot: Asset_Idx
|
||||
gen: u16
|
||||
|
||||
// Two-tiered searching of slots:
|
||||
// - first search the free-list, that contains freed up slots
|
||||
// - fallback to the index that has never been allocated before
|
||||
if a.next_free != ASSET_NIL {
|
||||
slot = a.next_free
|
||||
a.next_free = a.free_list[a.next_free]
|
||||
a.free_list[slot] = ASSET_NIL // clean up the pop'ed free-slot
|
||||
}
|
||||
else if (a.next_unused < ASSETS_MAX) {
|
||||
if a.next_unused == ASSET_NIL do a.next_unused = 1 // first ever add()
|
||||
slot = a.next_unused
|
||||
a.next_unused += 1
|
||||
}
|
||||
|
||||
if slot != ASSET_NIL {
|
||||
a.assets[slot] = {} // reset data
|
||||
a.used[slot] = true
|
||||
a.gen[slot] += 1
|
||||
gen = a.gen[slot]
|
||||
}
|
||||
|
||||
return {idx = slot, gen = gen}
|
||||
}
|
||||
|
||||
asset_rem :: proc(a: ^Assets, ref: Asset_Ref) {
|
||||
idx := asset_deref(a, ref)
|
||||
if idx == ASSET_NIL do return
|
||||
|
||||
a.used[idx] = false
|
||||
a.free_list[idx] = a.next_free
|
||||
a.next_free = idx
|
||||
}
|
||||
|
||||
asset_get :: proc(a: ^Assets, ref: Asset_Ref) -> ^Asset {
|
||||
return &a.assets[asset_deref(a, ref)]
|
||||
}
|
||||
|
||||
asset_deref :: proc(a: ^Assets, ref: Asset_Ref) -> Asset_Idx {
|
||||
if ref.idx > ASSET_NIL &&
|
||||
ref.idx < ASSETS_MAX &&
|
||||
a.used[ref.idx] &&
|
||||
ref.gen == a.gen[ref.idx] {
|
||||
return ref.idx
|
||||
}
|
||||
|
||||
return ASSET_NIL
|
||||
}
|
||||
|
||||
asset_find :: proc(a: ^Assets, path: string) -> (Asset_Idx, DIGEST) {
|
||||
digest := xxhash.XXH64(transmute([]u8)path)
|
||||
|
||||
found: u16
|
||||
for i: u16 = 1; i < min(a.next_unused, ASSETS_MAX); i += 1 {
|
||||
if a.used[i] && a.assets[i].digest == digest {
|
||||
return i, digest
|
||||
}
|
||||
}
|
||||
|
||||
return ASSET_NIL, digest
|
||||
}
|
||||
|
||||
asset_load_model :: proc(a: ^Assets, path: string) -> Asset_Ref {
|
||||
asset, digest := asset_find(a, path)
|
||||
if asset != ASSET_NIL {
|
||||
fmt.println("[assets] loading cached model", path)
|
||||
return {idx = asset, gen = a.gen[asset]}
|
||||
}
|
||||
|
||||
fmt.println("[assets] loading model", path)
|
||||
vertices, indices, ok := load_obj_model(path, a.allocator)
|
||||
fmt.assertf(ok, "[assets] failed to read model: %s", path)
|
||||
|
||||
a.model_list[path] = {
|
||||
ref := asset_add(a)
|
||||
a.assets[ref.idx] = {
|
||||
kind = .Model,
|
||||
digest = digest,
|
||||
data = Model{vertices = vertices, indices = indices},
|
||||
}
|
||||
|
||||
return ref
|
||||
}
|
||||
|
||||
asset_reload_after_hot_reload :: proc(a: ^Assets, path: string) {
|
||||
pwd, pwd_err := os.get_working_directory(context.temp_allocator)
|
||||
if pwd_err != nil {
|
||||
fmt.eprintfln("[assets] failed to get working directory: %s", pwd_err)
|
||||
return
|
||||
}
|
||||
|
||||
w := os.walker_create(path)
|
||||
defer os.walker_destroy(&w)
|
||||
|
||||
for info in os.walker_walk(&w) {
|
||||
if path, err := os.walker_error(&w); err != nil {
|
||||
fmt.eprintfln("[assets] failed walking %s: %s", path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
a_path := info.fullpath[len(pwd) + 1:]
|
||||
digest := xxhash.XXH64(transmute([]u8)a_path)
|
||||
|
||||
for i: u16 = 1; i < min(a.next_unused, ASSETS_MAX); i += 1 {
|
||||
if !a.used[i] || a.assets[i].kind != .Model do continue
|
||||
if a.assets[i].digest == digest {
|
||||
fmt.println("[assets] loading model", a_path)
|
||||
vertices, indices, ok := load_obj_model(a_path, a.allocator)
|
||||
fmt.assertf(ok, "[assets] failed to read model: %s", a_path)
|
||||
|
||||
a.assets[i].data = Model {
|
||||
vertices = vertices,
|
||||
indices = indices,
|
||||
}
|
||||
|
||||
return &a.model_list[path]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Batching:
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ load_obj_model :: proc(
|
||||
fmt.eprintfln("[obj] failed to read model: %s", path)
|
||||
return nil, nil, false
|
||||
}
|
||||
defer delete(data)
|
||||
defer delete(data, allocator)
|
||||
|
||||
positions: [dynamic]base.Vec3
|
||||
uvs: [dynamic]base.Vec2
|
||||
|
||||
@@ -36,10 +36,13 @@ Thing_Kind :: enum {
|
||||
// Fixed size fat struct
|
||||
Thing :: struct {
|
||||
kind: Thing_Kind,
|
||||
|
||||
pos: base.Vec3,
|
||||
rot: base.Vec3,
|
||||
scale: base.Vec3,
|
||||
|
||||
model: ^Model,
|
||||
|
||||
// TODO: intrusive list
|
||||
// parent: Thing_Ref
|
||||
// first_kid: Thing_Ref
|
||||
@@ -60,7 +63,7 @@ Things :: struct {
|
||||
// -----------------------------------------
|
||||
// Public functions
|
||||
|
||||
add :: proc(t: ^Things) -> Thing_Ref {
|
||||
thing_add :: proc(t: ^Things) -> Thing_Ref {
|
||||
slot: Thing_Idx
|
||||
gen: u16
|
||||
|
||||
@@ -88,8 +91,8 @@ add :: proc(t: ^Things) -> Thing_Ref {
|
||||
return {idx = slot, gen = gen}
|
||||
}
|
||||
|
||||
rem :: proc(t: ^Things, ref: Thing_Ref) {
|
||||
idx := deref(t, ref)
|
||||
thing_rem :: proc(t: ^Things, ref: Thing_Ref) {
|
||||
idx := thing_deref(t, ref)
|
||||
if idx == THING_NIL do return
|
||||
|
||||
t.used[idx] = false
|
||||
@@ -97,11 +100,11 @@ rem :: proc(t: ^Things, ref: Thing_Ref) {
|
||||
t.next_free = idx
|
||||
}
|
||||
|
||||
get :: proc(t: ^Things, ref: Thing_Ref) -> ^Thing {
|
||||
return &t.things[deref(t, ref)]
|
||||
thing_get :: proc(t: ^Things, ref: Thing_Ref) -> ^Thing {
|
||||
return &t.things[thing_deref(t, ref)]
|
||||
}
|
||||
|
||||
deref :: proc(t: ^Things, ref: Thing_Ref) -> Thing_Idx {
|
||||
thing_deref :: proc(t: ^Things, ref: Thing_Ref) -> Thing_Idx {
|
||||
if ref.idx > THING_NIL &&
|
||||
ref.idx < THINGS_MAX &&
|
||||
t.used[ref.idx] &&
|
||||
|
||||
+30
-8
@@ -62,23 +62,33 @@ main :: proc() {
|
||||
// Assets
|
||||
assets := core.assets_init(allocator)
|
||||
defer core.assets_destroy(&assets)
|
||||
load_test_model(&assets)
|
||||
|
||||
// Large Array of Things
|
||||
things := new(core.Things, allocator)
|
||||
defer free(things, allocator)
|
||||
camera := core.add(things)
|
||||
camera := core.thing_add(things)
|
||||
|
||||
// Register memory regions
|
||||
base.register_region("assets", cast(rawptr)&assets, size_of(core.Assets))
|
||||
base.register_region("things", things, size_of(core.Things))
|
||||
|
||||
api.register_memory()
|
||||
|
||||
// Init
|
||||
fresh, err := base.read_snapshot()
|
||||
fmt.assertf(err == nil, "[main] failed to read snapshot", err)
|
||||
if fresh do api.init_once()
|
||||
|
||||
assets.allocator = allocator
|
||||
api.init()
|
||||
|
||||
// Init once
|
||||
if fresh {
|
||||
api.init_once()
|
||||
load_test_model(&assets) // tmp
|
||||
}
|
||||
else {
|
||||
core.asset_reload_after_hot_reload(&assets, "assets")
|
||||
}
|
||||
upload_test_model(&assets) // tmp
|
||||
|
||||
dt: f64
|
||||
gt: f64
|
||||
st: f64
|
||||
@@ -94,6 +104,9 @@ main :: proc() {
|
||||
api.update(cast(f32)dt)
|
||||
base.write_snapshot()
|
||||
|
||||
// loop through Things, if model -> render
|
||||
// register the Things inside game.odin
|
||||
|
||||
platform.frame()
|
||||
api.render()
|
||||
|
||||
@@ -117,9 +130,18 @@ test :: proc(e: ^base.Key_Press_Event, _: rawptr) -> bool {
|
||||
}
|
||||
|
||||
// Load model from disk and upload it
|
||||
load_test_model :: proc(assets: ^core.Assets) {
|
||||
cube := core.load_model(assets, "assets/cube.obj")
|
||||
suzanne := core.load_model(assets, "assets/suzanne.obj")
|
||||
load_test_model :: proc(a: ^core.Assets) {
|
||||
cube_ref := core.asset_load_model(a, "assets/cube.obj")
|
||||
suzanne_ref := core.asset_load_model(a, "assets/suzanne.obj")
|
||||
}
|
||||
|
||||
// Load model from disk and upload it
|
||||
upload_test_model :: proc(a: ^core.Assets) {
|
||||
cube_ref := core.asset_load_model(a, "assets/cube.obj")
|
||||
suzanne_ref := core.asset_load_model(a, "assets/suzanne.obj")
|
||||
|
||||
cube := core.asset_get(a, cube_ref).data.(core.Model)
|
||||
suzanne := core.asset_get(a, suzanne_ref).data.(core.Model)
|
||||
|
||||
platform.create_material("dummy")
|
||||
platform.create_renderable(
|
||||
|
||||
Reference in New Issue
Block a user