lava 0.4.9

Rust wrapper to manipulate Vulkan more conveniently than with bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
const {
    getRawVkTypeName,
    getWrappedVkTypeName,
    argToString,
    getFieldsInformation,
    isStructOrHandle,
    isStruct,
    getCountVarNameValue,
    documentType
} = require('./utils');

const VK_SUCCESS = 0;
const VK_NULL_HANDLE = 0;

function generateVkHandleDefinition(def) {
    def.rawTypeName = getRawVkTypeName(def.name);
    def.wrappedTypeName = getWrappedVkTypeName(def.name);

    for (let func of def.functions) {
        if (!func.argsInfo) {
            func.argsInfo = getFieldsInformation(func.args);
        }
    }

    makeMethodNames(def, def.functions);

    return [
        genUses(def),
        genRawType(def),
        genWrappedType(def),
        genVkRawTypeTrait(def),
        genVkWrappedTypeTrait(def),
        genDefaultTrait(def),
        genPartialEqTrait(def),
        genVkSetupTrait(def),
        genSpecialMethods(def),
        genMethods(def),
        // genDropTrait(def)
    ];
}

function genUses() {
    const uses = new Set([
        `utils::c_bindings::*`,
        `utils::vk_traits::*`,
        `utils::vk_ptr::*`,
        `utils::vk_convert::*`,
        'std::os::raw::c_char',
        'std::ops::Drop',
        'std::ptr',
        'std::mem',
        'std::cmp',
        'std::slice',
        `vulkan::*`,
        `vulkan::vk::*`,
    ]);

    return Array.from(uses.values()).map(str => `use ${str};`);
}

function makeMethodNames(handle, functions) {
    const assigned = new Set();

    for (const func of functions) {
        const singularToReplace = handle ? handle.name.replace('Vk', '') : '';
        const pluralToReplace = handle ? singularToReplace + 's' : '';

        let extension = '';
        let methodName = func.name
            .replace(/^vk/g, '')
            .replace(pluralToReplace, '')
            .replace(singularToReplace, '')
            .replace(/[A-Z]+$/, ext => { extension = ext.toLowerCase(); return ''; })
            .toSnakeCase();

        if (handle && handle.name === 'VkDeviceMemory') {
            methodName = methodName.replace('_memory', '');
        }

        if (!handle) {
            methodName = `vk_${methodName}`;
        }

        if (assigned.has(methodName)) {
            methodName += `_${extension}`;
        }

        assigned.add(methodName);
        func.methodName = methodName;
    }
}

function genRawType(def) {
    return [
        `#[doc(hidden)]`,
        `pub type ${def.rawTypeName} = u64;`
    ];
}

function genWrappedType(def) {
    return [
        documentType(def),
        `#[derive(Debug, Clone, Copy)]`,
        `pub struct ${def.wrappedTypeName}`, [
            `_handle: ${def.rawTypeName},`,
            `_fn_table: *mut VkFunctionTable`
        ]
    ];
}

function genVkRawTypeTrait(def) {
    return [
        `impl VkRawType<${def.wrappedTypeName}> for ${def.rawTypeName}`, [
            `fn vk_to_wrapped(src: &${def.rawTypeName}) -> ${def.wrappedTypeName}`, [
                def.wrappedTypeName, [
                    `_handle: *src,`,
                    `_fn_table: ptr::null_mut()`
                ]
            ],
        ]
    ];
}

function genVkWrappedTypeTrait(def) {
    return [
        `impl VkWrappedType<${def.rawTypeName}> for ${def.wrappedTypeName}`, [
            `fn vk_to_raw(src: &${def.wrappedTypeName}, dst: &mut ${def.rawTypeName})`, [
                `*dst = src._handle`
            ]
        ]
    ];
}

function genDefaultTrait(def) {
    return [
        `impl Default for ${def.wrappedTypeName}`, [
            `fn default() -> ${def.wrappedTypeName}`, [
                def.wrappedTypeName, [
                    `_handle: ${VK_NULL_HANDLE},`,
                    `_fn_table: ptr::null_mut()`
                ]
            ]
        ]
    ];
}

function genPartialEqTrait(def) {
    return [
        `impl PartialEq for ${def.wrappedTypeName}`, [
            `fn eq(&self, other: &${def.wrappedTypeName}) -> bool`, [
                `self._handle == other._handle`
            ]
        ]
    ];  
}

function genVkSetupTrait(def) {
    return [
        `impl VkSetup for ${def.wrappedTypeName}`, [
            `fn vk_setup(&mut self, fn_table: *mut VkFunctionTable)`, [
                `self._fn_table = fn_table;`
            ]
        ]
    ]
}

function genSpecialMethods(def) {
    if (def.name === 'VkInstance') {
        return [
            `impl ${def.wrappedTypeName}`, [
                `pub fn create_surface<F : Fn(u64, *const c_void, *mut u64) -> i32>(&self, create_fn: F) -> LavaResult<khr::VkSurface>`, [
                    `unsafe`, [
                        `let raw_surface = &mut mem::zeroed() as *mut khr::RawVkSurface;`,
                        `let vk_result = create_fn(self._handle, ptr::null(), raw_surface);`,
                        `let mut surface = new_vk_value(raw_surface);`,
                        `VkSetup::vk_setup(&mut surface, self._fn_table);`,
                        `if vk_result != 0 { return Err((RawVkResult::vk_to_wrapped(&vk_result), surface)) }`,
                        `Ok(surface)`
                    ]
                ]
            ]
        ]
    }
}

const VK_HANDLE_DOC = '/// Returns the internal Vulkan handle for the object.';
const IS_NULL_DOC = '/// Indicates if the Vulkan internal handle for this object is 0.';
const NULL_DOC = '/// Creates an object with a null Vulkan internal handle.\n///\n/// Calling a method with a null handle will most likely result in a crash.';

function genMethods(def) {
    const handleMethod = [
        `\n${VK_HANDLE_DOC}\npub fn vk_handle(&self) -> u64`, [
            `self._handle`
        ],
        `\n${IS_NULL_DOC}\npub fn is_null(&self) -> bool`, [
            `self._handle == 0`
        ],
        `\n${NULL_DOC}\npub fn null() -> Self`, [
            `Self`, [
                `_handle: 0,`,
                `_fn_table: ptr::null_mut()`
            ]
        ],
    ];
    return [
        `impl ${def.wrappedTypeName}`,
        def.functions.map(func => functionToMethod(def, func)).reduce((acc, block) => acc.concat(block), handleMethod)
    ];
}

function getRawVarName(varName) {
    return `raw_${varName}`;
}

function idFunction(name) {
    return name;
}


const FORBIDDEN_FUNCTION_LIST = [
    // This function returns two arrays. The current system cannot properly generate a wrapper for it.
    'vkEnumeratePhysicalDeviceQueueFamilyPerformanceQueryCountersKHR'
];

function functionToMethod(handle, func) {
    if (FORBIDDEN_FUNCTION_LIST.includes(func.name)) {
        return null;
    }

    // const DEBUG = func.name === 'vkGetAccelerationStructureHandleNV';
    // if (DEBUG) console.log(func.args);

    const isVkQueuePresentKHR = func.name === 'vkQueuePresentKHR';

    const lastArg = func.args.last();
    const beforeLastArg = func.args.beforeLast();
    const createSomething = lastArg.isPointer && !lastArg.isConst && (lastArg.name !== 'pData' || beforeLastArg.name !== 'dataSize');
    const beforeLastArgIsCountPtr = createSomething && beforeLastArg && !!beforeLastArg.countFor.length && beforeLastArg.isPointer;
    const createList = createSomething && lastArg.countField;
    const returnVkResult = func.type === 'VkResult';

    const methodName = func.methodName;
    const methodArgs = handle ? [{type: '&self'}] : [];
    const statements = [];
    const freeStataments = [];
    let returnStatement = null;
    
    const functionRustArgs = func.argsInfo.map((arg, index) => {
        const rawArg = func.args[index];

        if (createList && rawArg.countFor.includes(lastArg.name) && !beforeLastArgIsCountPtr) {
            const countVarName = getRawVarName(arg.varName);
            statements.push(`let ${countVarName} = ${arg.toRaw(idFunction)};`)
            return countVarName;
        } else if (index === func.args.length - 1 && createSomething) {
            return getRawVarName(arg.varName);
        } else if (index === func.args.length - 2 && createSomething && beforeLastArgIsCountPtr) {
            return getRawVarName(arg.varName);
        } else if (arg.varName === 'allocator') {
            return 'ptr::null()';
        } else if (handle && index <= 1 && arg.typeName === handle.name) {
            return `self._handle`;
        } else if (handle && handle.parent && index === 0 && arg.typeName === handle.parent.name) {
            return handle.parent.name === 'VkInstance' ? `(*self._fn_table).instance` : `(*self._fn_table).device`;
        } else {
            const methodArgName = arg.varName;
            const functionArgName = getRawVarName(methodArgName);
            
            if (arg.wrappedType) {
                const methodArg = { name: methodArgName, type: arg.wrappedType };

                if (arg.extension) {
                    methodArg.type = arg.wrappedType.replace(arg.wrappedTypeName, `${arg.extension}::${arg.wrappedTypeName}`)
                }

                methodArgs.push(methodArg);
            }

            statements.push(`let ${functionArgName} = ${arg.toRaw(idFunction, true)};`);

            if (arg.freeRaw) {
                freeStataments.push(`${arg.freeRaw(getRawVarName)};`);
            }

            return functionArgName;
        }
    });

    if (createList && !lastArg.countField) {
        throw new Error(`function ${func.name} returns an array but does not take a count, need manual review`);
    }

    const isVkMapMemory = func.name === 'vkMapMemory';
    const funcNameValue = handle ? `((&*self._fn_table).${func.name})` : func.name;
    const functionCall = `${funcNameValue}(${functionRustArgs.join(', ')})`;

    let returnType = null;
    let generics = '';

    if (createSomething) {
        if (returnVkResult) {
            statements.push(`let mut vk_result = ${VK_SUCCESS};`);
        }

        const wrappedFunctionCall = returnVkResult ? `vk_result = ${functionCall};` : `${functionCall};`;
        const createdType = func.argsInfo.last();
        let createdRawTypeName = prefixWithExtension(createdType.extension, createdType.rawTypeName);
        let createdWrappedTypeName = prefixWithExtension(createdType.extension, createdType.wrappedTypeName);

        if (isVkMapMemory) {
            createdRawTypeName = '*mut c_void';
            createdWrappedTypeName = `&'a mut [c_void]`;
            generics = `<'a>`;
        }

        const setupResult = createdType.typeName === 'VkInstance' || (handle && isStructOrHandle(createdType));
        const resutlIsStruct = isStruct(createdType);

        const wrappedResultVarName = createdType.varName;
        const rawResultName = getRawVarName(createdType.varName);

        if (!createList) {
            statements.push(
                `let ${rawResultName} = &mut mem::zeroed() as *mut ${createdRawTypeName};`,
                ``,
                wrappedFunctionCall,
                ``,
                `let${setupResult ? ' mut' : ''} ${wrappedResultVarName} = ${createdType.toWrapped(getRawVarName)};`,
            );

            if (isVkMapMemory) {
                statements[statements.length - 1] = `let ${wrappedResultVarName} = slice::from_raw_parts_mut(*${rawResultName}, size);`
            }

            if (setupResult) {
                const isInstance = createdType.typeName === 'VkInstance';
                const isDevice = createdType.typeName === 'VkDevice';

                let functionTable = `self._fn_table`;

                if (isInstance) {
                    functionTable = `Box::into_raw(Box::new(VkFunctionTable::from_instance(*${rawResultName})))`;
                } else if (isDevice) {
                    functionTable = `Box::into_raw(Box::new(VkFunctionTable::from_device(*${rawResultName})))`;
                }

                const setupStatements = [
                    `let fn_table = ${functionTable};`,
                    `VkSetup::vk_setup(&mut ${wrappedResultVarName}, fn_table);`
                ];

                if (returnVkResult) {
                    statements.push(`if vk_result == ${VK_SUCCESS}`, setupStatements);
                } else {
                    for (const statement of setupStatements) {
                        statements.push(statement);
                    }
                }
            }

            if (resutlIsStruct) {
                freeStataments.push(
                    // `${createdRawTypeName}::vk_free(${rawResultName}.as_ref().unwrap());`
                    // `free_ptr(${rawResultName}));`
                );
            }
        } else {
            const rawCountName = getCountVarNameValue(lastArg.countField, getRawVarName);

            if (beforeLastArgIsCountPtr) {
                statements.push(
                    `let mut ${rawResultName} : *mut ${createdRawTypeName} = ptr::null_mut();`,
                    `let ${rawCountName} = &mut mem::zeroed() as *mut ${func.argsInfo.beforeLast().rawTypeName};`,
                    wrappedFunctionCall
                );
            }

            statements.push(
                `${beforeLastArgIsCountPtr ? '' : 'let '}${rawResultName} = calloc(${beforeLastArgIsCountPtr ? '*' : ''}${rawCountName} as usize, mem::size_of::<${createdRawTypeName}>()) as *mut ${createdRawTypeName};`,
                ``,
                wrappedFunctionCall,
                ``,
                `let${setupResult ? ' mut' : ''} ${wrappedResultVarName} = ${createdType.toWrapped(getRawVarName)};`
            );

            if (setupResult) {
                const setupStatement = `for elt in &mut ${wrappedResultVarName} { VkSetup::vk_setup(elt, self._fn_table); }`;

                if (returnVkResult) {
                    statements.push(`if vk_result == ${VK_SUCCESS}`, [ setupStatement ]);
                } else {
                    statements.push(setupStatement);
                }
            }

            if (createdType.typeName.startsWith('Vk')) {
                freeStataments.push(`free(${rawResultName} as *mut u8);`);
                // freeStataments.push(`${createdType.freeRaw(getRawVarName)};`);
            }
        }

        returnType = createdWrappedTypeName;

        if (createList) {
            returnType = `Vec<${returnType}>`;
        }

        if (returnVkResult) {
            returnType = `LavaResult<${returnType}>`;
            returnStatement = `if vk_result == ${VK_SUCCESS} { Ok(${wrappedResultVarName}) } else { Err((RawVkResult::vk_to_wrapped(&vk_result), ${wrappedResultVarName})) }`
        } else {
            returnStatement = wrappedResultVarName;
        }
    } else if (returnVkResult) {
        statements.push(`let vk_result = ${functionCall};`);

        if (isVkQueuePresentKHR) {
            returnType = `LavaResult<Vec<VkResult>>`;
            statements.push(`let vk_results : Vec<VkResult> = if (*raw_present_info).results.is_null() { Vec::new() } else { new_vk_array((*raw_present_info).swapchain_count, (*raw_present_info).results) };`);
            returnStatement = `if vk_result == ${VK_SUCCESS} { Ok(vk_results) } else { Err((RawVkResult::vk_to_wrapped(&vk_result), vk_results)) }`;
        } else if (methodName === 'get_status') {
            returnType = `VkResult`;
            returnStatement = `RawVkResult::vk_to_wrapped(&vk_result)`;
        } else {
            returnType = 'LavaResult<()>';
            returnStatement = `if vk_result == ${VK_SUCCESS} { Ok(()) } else { Err((RawVkResult::vk_to_wrapped(&vk_result), ())) }`;
        }
    } else {
        statements.push(`${functionCall};`);
    }

    if (func.name === 'vkDestroyInstance' || func.name === 'vkDestroyDevice') {
        freeStataments.push(`if !self._fn_table.is_null() { Box::from_raw(self._fn_table); }`);
    }

    const argList = methodArgs.map(argToString).join(', ');
    const returnInfo = returnType ? ` -> ${returnType}` : '';
    
    const allStatements = statements.concat(freeStataments);

    if (returnStatement) {
        allStatements.push(returnStatement);
    }

    const doc = [documentType(func)];

    if (isVkQueuePresentKHR) {
        doc.push(
            '///',
            '/// If `present_info.results` is true, then the method returns a vector of `VkResult` with each value indicating the individual result for the swapchain with the corresponding index. If it is false, then the vector is always empty.'
        );
    }

    return [
        `\n${doc.join('\n')}\npub fn ${methodName}${generics}(${argList})${returnInfo}`,
        [`unsafe`, allStatements]
    ];
}

function prefixWithExtension(ext, name) {
    return ext ? `${ext}::${name}` : name;
}

module.exports = {
    generateVkHandleDefinition,
    makeMethodNames,
    functionToMethod
};