Skip to content

Commit f44414f

Browse files
committed
remove helper functions from vulkan bindings
1 parent fbdb9a3 commit f44414f

File tree

3 files changed

+69
-72
lines changed

3 files changed

+69
-72
lines changed

examples/vulkan_bindgen.lua

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,6 @@ local res = build_lua(
6161
typedef void* xcb_window_t;
6262
]=]
6363
64-
do
65-
local cache = {}
66-
function mod.EnumToString(enum_type, index)
67-
if not index then
68-
index = tonumber(enum_type)
69-
end
70-
local enum_id = tonumber(ffi.typeof(enum_type))
71-
72-
if cache[enum_id] ~= nil then return cache[enum_id] end
73-
74-
local enum_ctype = ffi.typeinfo(enum_id)
75-
local current_index = 0
76-
local sib = enum_ctype.sib
77-
78-
while sib do
79-
local sib_ctype = ffi.typeinfo(sib)
80-
local CT_code = bit.rshift(sib_ctype.info, 28)
81-
82-
if CT_code == 11 then -- constant
83-
if current_index == index then
84-
cache[enum_id] = sib_ctype.name
85-
return cache[enum_id]
86-
end
87-
88-
current_index = current_index + 1
89-
end
90-
91-
sib = sib_ctype.sib
92-
end
93-
94-
cache[enum_id] = false
95-
96-
return nil
97-
end
98-
end
99-
10064
function mod.GetExtension(lib, instance, name)
10165
local ptr = lib.vkGetInstanceProcAddr(instance, name)
10266
@@ -106,32 +70,6 @@ local res = build_lua(
10670
return func
10771
end
10872
109-
do
110-
local t_cache = {}
111-
112-
local function array_type(t, len)
113-
if len then
114-
t_cache[t] = t_cache[t] or ffi.typeof("$[" .. len .. "]", t)
115-
return t_cache[t]
116-
end
117-
118-
t_cache[t] = t_cache[t] or ffi.typeof("$[?]", t)
119-
return t_cache[t]
120-
end
121-
122-
function mod.Array(t, len, ctor)
123-
if ctor then return array_type(t, len)(ctor) end
124-
125-
return array_type(t, len)
126-
end
127-
128-
function mod.Box(t, ctor)
129-
if ctor then return array_type(t, 1)({ctor}) end
130-
131-
return array_type(t, 1)
132-
end
133-
end
134-
13573
function mod.find_library()
13674
local function try_load(tbl)
13775
local errors = {}
@@ -155,6 +93,9 @@ local res = build_lua(
15593
15694
-- Try MoltenVK directly first (more reliable on macOS)
15795
if home then
96+
if vulkan_sdk then
97+
table.insert(paths, home .. "/VulkanSDK/1.4.328.1/macOS/lib/libvulkan.1.dylib")
98+
end
15899
table.insert(paths, home .. "/VulkanSDK/1.4.328.1/macOS/lib/libMoltenVK.dylib")
159100
end
160101
@@ -182,9 +123,61 @@ f:close()
182123
local ffi = require("ffi")
183124

184125
do
126+
local function array_type(t, len)
127+
if len then return ffi.typeof("$[" .. len .. "]", t) end
128+
129+
return ffi.typeof("$[?]", t)
130+
end
131+
132+
local function Array(t, len, ctor)
133+
if ctor then return array_type(t, len)(ctor) end
134+
135+
return array_type(t, len)
136+
end
137+
138+
local function Box(t, ctor)
139+
if ctor then return array_type(t, 1)({ctor}) end
140+
141+
return array_type(t, 1)
142+
end
143+
144+
local function get_enums(enum_type)
145+
local out = {}
146+
local enum_id = tonumber(ffi.typeof(enum_type))
147+
local enum_ctype = ffi.typeinfo(enum_id)
148+
local sib = enum_ctype.sib
149+
150+
while sib do
151+
local sib_ctype = ffi.typeinfo(sib)
152+
local CT_code = bit.rshift(sib_ctype.info, 28)
153+
local current_index = sib_ctype.size
154+
155+
-- bug?
156+
if current_index == nil then current_index = -1 end
157+
158+
if CT_code == 11 then out[sib_ctype.name] = current_index end
159+
160+
sib = sib_ctype.sib
161+
end
162+
163+
return out
164+
end
165+
166+
local function enum_to_string(enum_type, value)
167+
if not value then value = enum_type end
168+
169+
local enums = get_enums(enum_type)
170+
171+
for k, v in pairs(enums) do
172+
if v == value then return k end
173+
end
174+
175+
return "unknown enum value: " .. tostring(value)
176+
end
177+
185178
local vk = require("vulkan")
186179
local lib = vk.find_library()
187-
local appInfo = vk.Box(
180+
local appInfo = Box(
188181
vk.VkApplicationInfo,
189182
{
190183
sType = "VK_STRUCTURE_TYPE_APPLICATION_INFO",
@@ -195,7 +188,7 @@ do
195188
apiVersion = vk.VK_API_VERSION_1_0,
196189
}
197190
)
198-
local createInfo = vk.Box(
191+
local createInfo = Box(
199192
vk.VkInstanceCreateInfo,
200193
{
201194
sType = "VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO",
@@ -208,25 +201,25 @@ do
208201
ppEnabledExtensionNames = nil,
209202
}
210203
)
211-
local instance = vk.Box(vk.VkInstance)()
204+
local instance = Box(vk.VkInstance)()
212205
local result = lib.vkCreateInstance(createInfo, nil, instance)
213206

214207
if result ~= 0 then
215-
error("failed to create vulkan instance: " .. vk.EnumToString(result))
208+
error("failed to create vulkan instance: " .. enum_to_string(result))
216209
end
217210

218-
print("vulkan instance created successfully: " .. vk.EnumToString(result))
211+
print("vulkan instance created successfully: " .. enum_to_string(result))
219212
local deviceCount = ffi.new("uint32_t[1]", 0)
220213
result = lib.vkEnumeratePhysicalDevices(instance[0], deviceCount, nil)
221214

222215
if result ~= 0 or deviceCount[0] == 0 then error("devices found") end
223216

224217
print(string.format("found %d physical device(s)", deviceCount[0]))
225-
local devices = vk.Array(vk.VkPhysicalDevice)(deviceCount[0])
218+
local devices = Array(vk.VkPhysicalDevice)(deviceCount[0])
226219
result = lib.vkEnumeratePhysicalDevices(instance[0], deviceCount, devices)
227220

228221
for i = 0, deviceCount[0] - 1 do
229-
local properties = vk.Box(vk.VkPhysicalDeviceProperties)()
222+
local properties = Box(vk.VkPhysicalDeviceProperties)()
230223
lib.vkGetPhysicalDeviceProperties(devices[i], properties)
231224
local props = properties[0]
232225
-- Decode API version (major.minor.patch)
@@ -244,7 +237,7 @@ do
244237
print(string.format(" driver version: 0x%08X", props.driverVersion))
245238
print(string.format(" vendor id: 0x%04X", props.vendorID))
246239
print(string.format(" device id: 0x%04X", props.deviceID))
247-
print(string.format(" device type: %s", vk.EnumToString(props.deviceType)))
240+
print(string.format(" device type: %s", enum_to_string(props.deviceType)))
248241
-- Print some limits
249242
local limits = props.limits
250243
print(string.format(" max image dimension 2D: %d", tonumber(limits.maxImageDimension2D)))

nattlua/definitions/lua/ffi/ast_walker.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ local function handle_enum(state, node)
4848
for _, field in ipairs(node.fields) do
4949
table.insert(
5050
struct.fields,
51-
{type = "enum_field", identifier = field.tokens["identifier"]:GetValueString()}
51+
{
52+
type = "enum_field",
53+
identifier = field.tokens["identifier"]:GetValueString(),
54+
expression = field.expression,
55+
}
5256
)
5357
end
5458
end

nattlua/definitions/lua/ffi/binding_gen.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ local function build_lua(c_header, expanded_defines, extra_lua)
252252
for i, field in ipairs(v.fields) do
253253
buf:put("\t", field.identifier)
254254

255-
if field.value then buf:put(" = ", field.value) end
255+
if field.expression then buf:put(" = ", field.expression:Render()) end
256256

257257
buf:put(",\n")
258258
end

0 commit comments

Comments
 (0)