Skip to content

Commit

Permalink
Check whether gpu-assisted validation and debug-printf are supported …
Browse files Browse the repository at this point in the history
…before enabling them
  • Loading branch information
knokko committed Nov 18, 2024
1 parent 8585556 commit e458fd8
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.knokko.boiler.builders;

import com.github.knokko.boiler.builders.instance.ValidationFeaturesChecker;
import com.github.knokko.boiler.exceptions.MissingVulkanExtensionException;
import com.github.knokko.boiler.exceptions.MissingVulkanLayerException;
import com.github.knokko.boiler.utilities.CollectionHelper;
Expand Down Expand Up @@ -89,23 +90,28 @@ static Result createInstance(BoilerBuilder builder) {

VkValidationFeaturesEXT pValidationFeatures;
if (builder.validationFeatures != null) {

pValidationFeatures = VkValidationFeaturesEXT.calloc(stack);
pValidationFeatures.sType$Default();
pValidationFeatures.pNext(0L);

var supportedValidationFeatures = new ValidationFeaturesChecker(stack);

var validationFlags = stack.callocInt(5);
if (builder.validationFeatures.gpuAssisted())
if (builder.validationFeatures.gpuAssisted() && supportedValidationFeatures.gpuAssistedValidation)
validationFlags.put(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT);
if (builder.validationFeatures.gpuAssistedReserve())
if (builder.validationFeatures.gpuAssistedReserve() && supportedValidationFeatures.gpuAssistedValidation)
validationFlags.put(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT);
if (builder.validationFeatures.bestPractices())
validationFlags.put(VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT);
if (builder.validationFeatures.debugPrint())
if (builder.validationFeatures.debugPrint() && supportedValidationFeatures.debugPrintf)
validationFlags.put(VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT);
if (builder.validationFeatures.synchronization())
validationFlags.put(VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT);
validationFlags.flip();

supportedValidationFeatures.destroy();

if (validationFlags.limit() > 0) pValidationFeatures.pEnabledValidationFeatures(validationFlags);
else pValidationFeatures = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.github.knokko.boiler.builders.instance;

import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;

import static com.github.knokko.boiler.exceptions.VulkanFailureException.assertVkSuccess;
import static org.lwjgl.vulkan.VK10.*;
import static org.lwjgl.vulkan.VK11.*;
import static org.lwjgl.vulkan.VK12.VK_API_VERSION_1_2;

public class ValidationFeaturesChecker {

private final VkInstance vkInstance;
public final boolean gpuAssistedValidation, debugPrintf;

public ValidationFeaturesChecker(MemoryStack stack) {
if (VK.getInstanceVersionSupported() < VK_API_VERSION_1_2) {
this.vkInstance = null;
this.gpuAssistedValidation = false;
this.debugPrintf = false;
return;
}

var appInfo = VkApplicationInfo.calloc(stack);
appInfo.sType$Default();
appInfo.apiVersion(VK_API_VERSION_1_2);

var ciInstance = VkInstanceCreateInfo.calloc(stack);
ciInstance.sType$Default();
ciInstance.pApplicationInfo(appInfo);

var pInstance = stack.callocPointer(1);
assertVkSuccess(vkCreateInstance(
ciInstance, null, pInstance
), "CreateInstance", "validation feature check");
this.vkInstance = new VkInstance(pInstance.get(), ciInstance);

var pNumDevices = stack.callocInt(1);
assertVkSuccess(vkEnumeratePhysicalDevices(
vkInstance, pNumDevices, null
), "EnumeratePhysicalDevices", "validation feature check count");
int numDevices = pNumDevices.get(0);

var pDevices = stack.callocPointer(numDevices);
assertVkSuccess(vkEnumeratePhysicalDevices(
vkInstance, pNumDevices, pDevices
), "EnumeratePhysicalDevices", "validation feature check count");

boolean supportsGpuAv = true;
boolean supportsPrint = true;

var features = VkPhysicalDeviceFeatures2.calloc(stack);
features.sType$Default();
var features12 = VkPhysicalDeviceVulkan12Features.calloc(stack);
features12.sType$Default();
features.pNext(features12);

for (int index = 0; index < numDevices; index++) {
var device = new VkPhysicalDevice(pDevices.get(index), vkInstance);
vkGetPhysicalDeviceFeatures2(device, features);

supportsGpuAv &= features12.bufferDeviceAddress();
supportsPrint &= features.features().fragmentStoresAndAtomics() &&
features.features().vertexPipelineStoresAndAtomics() && features12.timelineSemaphore();
}

this.gpuAssistedValidation = supportsGpuAv;
this.debugPrintf = supportsPrint;
}

public void destroy() {
if (vkInstance != null) vkDestroyInstance(vkInstance, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.knokko.boiler.builders;

import com.github.knokko.boiler.builders.instance.ValidationFeatures;
import com.github.knokko.boiler.builders.instance.ValidationFeaturesChecker;
import com.github.knokko.boiler.builders.queue.QueueFamilyAllocation;
import com.github.knokko.boiler.builders.queue.QueueFamilyMapping;
import com.github.knokko.boiler.debug.ValidationException;
Expand Down Expand Up @@ -83,13 +84,20 @@ public void testComplexInstanceBuilder() {
.vkInstanceCreator((ciInstance, stack) -> {
pDidCallInstanceCreator[0] = true;

var validationFeatures = VkValidationFeaturesEXT.create(ciInstance.pNext());
assertEquals(VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, validationFeatures.sType());

var validationFlags = Objects.requireNonNull(validationFeatures.pEnabledValidationFeatures());
assertEquals(2, validationFeatures.enabledValidationFeatureCount());
assertEquals(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT, validationFlags.get(0));
assertEquals(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT, validationFlags.get(1));
var check = new ValidationFeaturesChecker(stack);
if (check.gpuAssistedValidation) {
assertNotEquals(0L, ciInstance.pNext());
var validationFeatures = VkValidationFeaturesEXT.create(ciInstance.pNext());
assertEquals(VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT, validationFeatures.sType());

var validationFlags = Objects.requireNonNull(validationFeatures.pEnabledValidationFeatures());
assertEquals(2, validationFeatures.enabledValidationFeatureCount());
assertEquals(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT, validationFlags.get(0));
assertEquals(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT, validationFlags.get(1));
} else {
assertEquals(0L, ciInstance.pNext());
System.err.println("Is GPU-assisted validation really not supported?");
}

var appInfo = Objects.requireNonNull(ciInstance.pApplicationInfo());
assertEquals("TestComplexVulkan1.2", appInfo.pApplicationNameString());
Expand Down

0 comments on commit e458fd8

Please sign in to comment.