I am looking for a clean way to initialize multi pointers inline.
They are very common in the vulkan package and initializing them can be a chore.
The best way i have found thus far has been taking the address of a slice literal : ppEnabledLayerNames = raw_data([]cstring{"VK_LAYER_KHRONOS_validation"}),
I am surprised there is no direct way, e.g.: ppEnabledLayerNames = [^]cstring{"VK_LAYER_KHRONOS_validation"},
or even: ppEnabledLayerNames = {"VK_LAYER_KHRONOS_validation"},
Is this intentional, and if so, is there a cleaner way to declare the multi pointer?
So far this has been my take, but I have also been looking for more ways to work with multi-pointers. So if anyone can provide some expertise, that would be great.
If a multi-pointer is created directly from a literal, like in your first example (which I think is how it’s done), then there is no way to get it’s length later down the road. Many of the procedures in graphics libraries, including, vulkan, require both the raw_data, and a length and/or count of elements to read. So it may be better to manage the “un_raw” data separately, then convert it just prior to using a procedure that wants it.
So pretend that below printf’s are graphics procedures that wants raw data, and a length or number of points/vertices to use.
point :: [3]f32
line := []point{{0.0, 0.0, 0.0}, {42.0, 42.0, 42.0}}
raw_line := raw_data(line)
//does not know length
fmt.printfln("points: %v", raw_line)
//this procedure knows length
fmt.printfln("points %v", raw_line[0:len(line)])
I believe that raw_data([]cstring{"VK_LAYER_KHRONOS_validation"}) is your only option if you truly need multi-pointers to be instantiated at assignment.
Looking through some of my Vulkan code, I never use that pattern for multi-pointers. I typically opt for an approach that is a visually simpler at the assignment locations, especially when considering some of the larger initialization structs.
// single cstring to multi-pointer (create_info: vk.InstanceCreateInfo)
expected_layer_name : cstring = "VK_LAYER_KHRONOS_validation"
create_info.enabledLayerCount = 1
create_info.ppEnabledLayerNames = &expected_layer_name
There could also be a number of instances where having the multi-pointer declared in such a compact way will mean that you also need to manually update the corresponding count/array size fields manually. This can be a pain for certain things that you may want to trivially enable/disable through out development, like certain validation or pipeline features. (note: I also typically use import vk "vendor:vulkan" for a closer naming convention to the official documentation)