Harbor

branch main
showing the latest snapshot on main
sync.odin 1.4 KB · Plain text
core/sync.odin 0644 Raw
package gpu_core

import "core:log"
import vk "vendor:vulkan"

MAX_FRAMES_IN_FLIGHT :: 2

Frame_Sync :: struct {
	image_available: vk.Semaphore,
	in_flight:       vk.Fence,
}

create_sync_objects :: proc(dev: ^Gpu_Device) -> (syncs: [MAX_FRAMES_IN_FLIGHT]Frame_Sync, ok: bool) {
	sem_info := vk.SemaphoreCreateInfo{
		sType = .SEMAPHORE_CREATE_INFO,
	}

	fence_info := vk.FenceCreateInfo{
		sType = .FENCE_CREATE_INFO,
		flags = {.SIGNALED},
	}

	for i in 0..<MAX_FRAMES_IN_FLIGHT {
		result := vk.CreateSemaphore(dev.device, &sem_info, nil, &syncs[i].image_available)
		if result != .SUCCESS {
			log.errorf("gpu/core: failed to create image_available semaphore %d: %v", i, result)
			destroy_sync_objects(dev, &syncs)
			return {}, false
		}

		result = vk.CreateFence(dev.device, &fence_info, nil, &syncs[i].in_flight)
		if result != .SUCCESS {
			log.errorf("gpu/core: failed to create in_flight fence %d: %v", i, result)
			destroy_sync_objects(dev, &syncs)
			return {}, false
		}
	}

	return syncs, true
}

destroy_sync_objects :: proc(dev: ^Gpu_Device, syncs: ^[MAX_FRAMES_IN_FLIGHT]Frame_Sync) {
	for i in 0..<MAX_FRAMES_IN_FLIGHT {
		if syncs[i].image_available != 0 {
			vk.DestroySemaphore(dev.device, syncs[i].image_available, nil)
			syncs[i].image_available = 0
		}
		if syncs[i].in_flight != 0 {
			vk.DestroyFence(dev.device, syncs[i].in_flight, nil)
			syncs[i].in_flight = 0
		}
	}
}