Skip to content

Commit

Permalink
Handle multiple queues correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Jul 14, 2024
1 parent fd2c03f commit ad998f4
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.io7m.immutables.styles.ImmutablesStyleType;
import org.immutables.value.Value;

import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -47,18 +48,14 @@ public interface VulkanLogicalDeviceQueueCreateInfoType
VulkanQueueFamilyIndex queueFamilyIndex();

/**
* @return The number of queues to create
*/

@Value.Parameter
int queueCount();

/**
* Set the priorities of the created queues. The number of priority values
* set will implicitly set the number of queues to create.
*
* @return The priorities of the queues
*/

@Value.Parameter
float[] queuePriorities();
List<Float> queuePriorities();

/**
* Check preconditions for the type.
Expand All @@ -67,11 +64,12 @@ public interface VulkanLogicalDeviceQueueCreateInfoType
@Value.Check
default void checkPreconditions()
{
if (this.queueCount() <= 0) {
final var count = this.queuePriorities().size();
if (count <= 0) {
throw new IllegalArgumentException(
new StringBuilder(32)
.append("Queue count ")
.append(this.queueCount())
.append(count)
.append(" must be positive")
.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,8 @@ public void execute()

logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(graphicsQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,8 @@ public void execute()

logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(graphicsQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,19 +905,17 @@ public void execute()

logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(graphicsQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());

if (!Objects.equals(
graphicsQueueProps.queueFamilyIndex(),
presentationQueueProps.queueFamilyIndex())) {
logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(presentationQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1838,19 +1838,17 @@ public void execute()

logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(graphicsQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());

if (!Objects.equals(
graphicsQueueProps.queueFamilyIndex(),
presentationQueueProps.queueFamilyIndex())) {
logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(presentationQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ public void execute()

logicalDeviceInfoBuilder.addQueueCreateInfos(
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(graphicsQueueProps.queueFamilyIndex())
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build());

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,23 @@ private void initializeQueues()

final var queueBuffer = stack.mallocPointer(1);
for (final var queue_info : queueRequests) {
for (var queueIndex = 0; queueIndex < queue_info.queueCount(); ++queueIndex) {
final var priorities =
queue_info.queuePriorities();

for (var queueIndex = 0; queueIndex < priorities.size(); ++queueIndex) {
final var queueFamilyIndex =
queue_info.queueFamilyIndex();
final var family =
families.get(queueFamilyIndex);

if (LOG.isTraceEnabled()) {
LOG.trace(
"Requesting queue {} of family {}",
Integer.valueOf(queueIndex),
queueFamilyIndex
);
}

VK10.vkGetDeviceQueue(
this.device,
queueFamilyIndex.value(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,25 @@ private static VkDeviceQueueCreateInfo.Buffer createQueueBuffer(
VkDeviceQueueCreateInfo.malloc(queueCount, stack);

for (var index = 0; index < queueCount; ++index) {
final var queueInfo = infos.get(index);
final var queueInfo =
infos.get(index);
final var priorities =
queueInfo.queuePriorities();
final var priorityArray =
stack.callocFloat(priorities.size());

for (final var x : priorities) {
priorityArray.put(x.floatValue());
}

priorityArray.flip();

vkQueueBuffer.position(index);
vkQueueBuffer.sType(VK10.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO)
.pNext(0L)
.flags(0)
.queueFamilyIndex(queueInfo.queueFamilyIndex().value())
.pQueuePriorities(stack.floats(queueInfo.queuePriorities()));
.pQueuePriorities(priorityArray);
}
vkQueueBuffer.position(0);
return vkQueueBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ protected VulkanLogicalDeviceType createLogicalDevice(
{
final var queue =
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.addQueuePriorities(1.0f)
.setQueueFamilyIndex(
device.queueFamilyFindWithFlags(VK_QUEUE_TRANSFER_BIT)
.orElseThrow()
.queueFamilyIndex())
.setQueuePriorities(1.0f)
.build();

return device.createLogicalDevice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ protected VulkanLogicalDeviceType createLogicalDevice(

final var queue =
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(queueFamilyIndex)
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build();

return device.createLogicalDevice(
Expand Down Expand Up @@ -178,9 +177,8 @@ public void testCreateLogicalDeviceWithFeatures()

final var queue =
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(queueFamilyIndex)
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build();

final var logicalInfo =
Expand Down Expand Up @@ -253,9 +251,8 @@ public void testCreateLogicalDeviceWithFeaturesHighest()

final var queue =
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueCount(1)
.setQueueFamilyIndex(queueFamilyIndex)
.setQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build();

final var logicalInfo =
Expand All @@ -274,6 +271,90 @@ public void testCreateLogicalDeviceWithFeaturesHighest()
}
}

/**
* Create a logical device with multiple queues (if supported).
*
* @throws VulkanException On errors
*/

@Test
public void testCreateLogicalDeviceWithQueues()
throws VulkanException
{
Assumptions.assumeTrue(this.shouldRun());

final var instances =
VulkanLWJGLInstanceProvider.create();

final var requestedVersion =
VulkanVersions.encode(1, 0, 0);

final var instanceInfo =
VulkanInstanceCreateInfo.builder()
.setApplicationInfo(
VulkanApplicationInfo.of(
"com.io7m.jcoronado.tests.Test",
VulkanVersions.encode(0, 0, 1),
"com.io7m.jcoronado.tests",
VulkanVersions.encode(0, 0, 1),
requestedVersion))
.setEnabledLayers(List.of("VK_LAYER_KHRONOS_validation"))
.build();

try (var newInstance =
instances.createInstance(instanceInfo, Optional.empty())) {

this.logger().debug(
"Vulkan Maximum: {}",
newInstance.apiVersionMaximumSupported().toHumanString());
this.logger().debug(
"Vulkan Requested: {}",
newInstance.apiVersionUsed().toHumanString());

try (var physicalDevice =
newInstance.physicalDevices()
.get(0)) {

final var queueCountEnough =
physicalDevice.queueFamilies()
.values()
.stream()
.anyMatch(q -> q.queueCount() > 1);

Assumptions.assumeTrue(
queueCountEnough,
"Implementation supports multiple queues.");

for (final var queueFamily :
physicalDevice.queueFamilies().values()) {

if (queueFamily.queueCount() > 1) {
final var queue =
VulkanLogicalDeviceQueueCreateInfo.builder()
.setQueueFamilyIndex(queueFamily.queueFamilyIndex())
.addQueuePriorities(1.0f)
.addQueuePriorities(1.0f)
.build();

final var logicalInfo =
VulkanLogicalDeviceCreateInfo.builder()
.setFeatures(physicalDevice.features())
.addQueueCreateInfos(queue)
.build();

try (var logicalDevice =
physicalDevice.createLogicalDevice(logicalInfo)) {
this.logger().debug("waiting for device to idle...");
logicalDevice.waitIdle();
assertFalse(logicalDevice.isClosed());
return;
}
}
}
}
}
}

private static void saveDriverProperties(
final VulkanPhysicalDeviceType physicalDevice)
throws VulkanException, IOException
Expand Down

0 comments on commit ad998f4

Please sign in to comment.