Skip to content

Commit

Permalink
JBR-7568 Vulkan: Refactor VKLogicalDevice into VKDevice (#449)
Browse files Browse the repository at this point in the history
* Renamed VKLogicalDevice to VKDevice for conformance and convenience.
* Refactored device->device to device->handle for clarity.

(cherry picked from commit aba56fd)
  • Loading branch information
YaaZ authored and mkartashev committed Dec 9, 2024
1 parent d88e6cc commit 386d1d0
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 238 deletions.
69 changes: 34 additions & 35 deletions src/java.desktop/share/native/common/java2d/vulkan/VKBase.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static void vulkanLibClose() {
if (geInstance->devices[i].texturePool != NULL) {
VKTexturePool_Dispose(geInstance->devices[i].texturePool);
}
if (geInstance->devices[i].vkDestroyDevice != NULL && geInstance->devices[i].device != NULL) {
geInstance->devices[i].vkDestroyDevice(geInstance->devices[i].device, NULL);
if (geInstance->devices[i].vkDestroyDevice != NULL && geInstance->devices[i].handle != VK_NULL_HANDLE) {
geInstance->devices[i].vkDestroyDevice(geInstance->devices[i].handle, NULL);
}
}
free(geInstance->devices);
Expand Down Expand Up @@ -417,9 +417,9 @@ static jboolean VK_FindDevices() {
return JNI_FALSE;
}

geInstance->devices = ARRAY_ALLOC(VKLogicalDevice, physicalDevicesCount);
geInstance->devices = ARRAY_ALLOC(VKDevice, physicalDevicesCount);
if (geInstance->devices == NULL) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "Vulkan: Cannot allocate VKLogicalDevice")
J2dRlsTraceLn(J2D_TRACE_ERROR, "Vulkan: Cannot allocate VKDevice")
return JNI_FALSE;
}

Expand Down Expand Up @@ -585,9 +585,9 @@ static jboolean VK_FindDevices() {
}

ARRAY_PUSH_BACK(&geInstance->devices,
((VKLogicalDevice) {
((VKDevice) {
.name = deviceName,
.device = VK_NULL_HANDLE,
.handle = VK_NULL_HANDLE,
.physicalDevice = geInstance->physicalDevices[i],
.queueFamily = queueFamily,
.enabledLayers = deviceEnabledLayers,
Expand Down Expand Up @@ -645,8 +645,8 @@ static VkRenderPassCreateInfo* VK_GetGenericRenderPassInfo() {
return &renderPassInfo;
}

static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
if (logicalDevice->device != VK_NULL_HANDLE) {
static jboolean VK_InitDevice(VKDevice* device) {
if (device->handle != VK_NULL_HANDLE) {
return JNI_TRUE;
}
if (geInstance == NULL) {
Expand All @@ -655,7 +655,7 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
}
if (verbose) {
for (uint32_t i = 0; i < ARRAY_SIZE(geInstance->devices); i++) {
fprintf(stderr, " %c%d: %s\n", &geInstance->devices[i] == logicalDevice ? '*' : ' ',
fprintf(stderr, " %c%d: %s\n", &geInstance->devices[i] == device ? '*' : ' ',
i, geInstance->devices[i].name);
}
fprintf(stderr, "\n");
Expand All @@ -664,7 +664,7 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
float queuePriority = 1.0f;
VkDeviceQueueCreateInfo queueCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = logicalDevice->queueFamily, // obtained separately
.queueFamilyIndex = device->queueFamily, // obtained separately
.queueCount = 1,
.pQueuePriorities = &queuePriority
};
Expand All @@ -677,23 +677,22 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
.flags = 0,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queueCreateInfo,
.enabledLayerCount = ARRAY_SIZE(logicalDevice->enabledLayers),
.ppEnabledLayerNames = (const char *const *) logicalDevice->enabledLayers,
.enabledExtensionCount = ARRAY_SIZE(logicalDevice->enabledExtensions),
.ppEnabledExtensionNames = (const char *const *) logicalDevice->enabledExtensions,
.enabledLayerCount = ARRAY_SIZE(device->enabledLayers),
.ppEnabledLayerNames = (const char *const *) device->enabledLayers,
.enabledExtensionCount = ARRAY_SIZE(device->enabledExtensions),
.ppEnabledExtensionNames = (const char *const *) device->enabledExtensions,
.pEnabledFeatures = &features10
};

if (geInstance->vkCreateDevice(logicalDevice->physicalDevice, &createInfo, NULL, &logicalDevice->device) != VK_SUCCESS)
if (geInstance->vkCreateDevice(device->physicalDevice, &createInfo, NULL, &device->handle) != VK_SUCCESS)
{
J2dRlsTraceLn1(J2D_TRACE_ERROR, "Cannot create device:\n %s", logicalDevice->name)
J2dRlsTraceLn1(J2D_TRACE_ERROR, "Cannot create device:\n %s", device->name)
vulkanLibClose();
return JNI_FALSE;
}
VkDevice device = logicalDevice->device;
J2dRlsTraceLn1(J2D_TRACE_INFO, "Logical device (%s) created", logicalDevice->name)
J2dRlsTraceLn1(J2D_TRACE_INFO, "Logical device (%s) created", device->name)

#define DEVICE_PROC(NAME) GET_VK_PROC_RET_FALSE_IF_ERR(geInstance->vkGetDeviceProcAddr, logicalDevice, device, NAME)
#define DEVICE_PROC(NAME) GET_VK_PROC_RET_FALSE_IF_ERR(geInstance->vkGetDeviceProcAddr, device, device->handle, NAME)
DEVICE_PROC(vkDestroyDevice);
DEVICE_PROC(vkCreateShaderModule);
DEVICE_PROC(vkCreatePipelineLayout);
Expand Down Expand Up @@ -751,22 +750,22 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
VkCommandPoolCreateInfo poolInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = logicalDevice->queueFamily
.queueFamilyIndex = device->queueFamily
};
if (logicalDevice->vkCreateCommandPool(device, &poolInfo, NULL, &logicalDevice->commandPool) != VK_SUCCESS) {
if (device->vkCreateCommandPool(device->handle, &poolInfo, NULL, &device->commandPool) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_INFO, "failed to create command pool!")
return JNI_FALSE;
}

// Create command buffer
VkCommandBufferAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = logicalDevice->commandPool,
.commandPool = device->commandPool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1
};

if (logicalDevice->vkAllocateCommandBuffers(device, &allocInfo, &logicalDevice->commandBuffer) != VK_SUCCESS) {
if (device->vkAllocateCommandBuffers(device->handle, &allocInfo, &device->commandBuffer) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_INFO, "failed to allocate command buffers!");
return JNI_FALSE;
}
Expand All @@ -781,16 +780,16 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
.flags = VK_FENCE_CREATE_SIGNALED_BIT
};

if (logicalDevice->vkCreateSemaphore(device, &semaphoreInfo, NULL, &logicalDevice->imageAvailableSemaphore) != VK_SUCCESS ||
logicalDevice->vkCreateSemaphore(device, &semaphoreInfo, NULL, &logicalDevice->renderFinishedSemaphore) != VK_SUCCESS ||
logicalDevice->vkCreateFence(device, &fenceInfo, NULL, &logicalDevice->inFlightFence) != VK_SUCCESS)
if (device->vkCreateSemaphore(device->handle, &semaphoreInfo, NULL, &device->imageAvailableSemaphore) != VK_SUCCESS ||
device->vkCreateSemaphore(device->handle, &semaphoreInfo, NULL, &device->renderFinishedSemaphore) != VK_SUCCESS ||
device->vkCreateFence(device->handle, &fenceInfo, NULL, &device->inFlightFence) != VK_SUCCESS)
{
J2dRlsTraceLn(J2D_TRACE_INFO, "failed to create semaphores!");
return JNI_FALSE;
}

logicalDevice->vkGetDeviceQueue(device, logicalDevice->queueFamily, 0, &logicalDevice->queue);
if (logicalDevice->queue == NULL) {
device->vkGetDeviceQueue(device->handle, device->queueFamily, 0, &device->queue);
if (device->queue == NULL) {
J2dRlsTraceLn(J2D_TRACE_INFO, "failed to get device queue!");
return JNI_FALSE;
}
Expand All @@ -800,20 +799,20 @@ static jboolean VK_InitLogicalDevice(VKLogicalDevice* logicalDevice) {
ARRAY_PUSH_BACK(&vertices, ((VKTxVertex){1.0f, -1.0f, 1.0f, 0.0f}));
ARRAY_PUSH_BACK(&vertices, ((VKTxVertex){-1.0f, 1.0f, 0.0f, 1.0f}));
ARRAY_PUSH_BACK(&vertices, ((VKTxVertex){1.0f, 1.0f, 1.0f, 1.0f}));
logicalDevice->blitVertexBuffer = ARRAY_TO_VERTEX_BUF(logicalDevice, vertices);
if (!logicalDevice->blitVertexBuffer) {
device->blitVertexBuffer = ARRAY_TO_VERTEX_BUF(device, vertices);
if (!device->blitVertexBuffer) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "Cannot create vertex buffer")
return JNI_FALSE;
}
ARRAY_FREE(vertices);

logicalDevice->texturePool = VKTexturePool_initWithDevice(logicalDevice);
if (!logicalDevice->texturePool) {
device->texturePool = VKTexturePool_InitWithDevice(device);
if (!device->texturePool) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "Cannot create texture pool")
return JNI_FALSE;
}

geInstance->currentDevice = logicalDevice;
geInstance->currentDevice = device;

return JNI_TRUE;
}
Expand Down Expand Up @@ -855,13 +854,13 @@ Java_sun_java2d_vulkan_VKInstance_initNative(JNIEnv *env, jclass wlge, jlong nat
if (requestedDevice < 0 || (uint32_t)requestedDevice >= ARRAY_SIZE(geInstance->devices)) {
requestedDevice = 0;
}
if (!VK_InitLogicalDevice(&geInstance->devices[requestedDevice])) {
if (!VK_InitDevice(&geInstance->devices[requestedDevice])) {
vulkanLibClose();
return JNI_FALSE;
}

if (geInstance->currentDevice->vkCreateRenderPass(
geInstance->currentDevice->device, VK_GetGenericRenderPassInfo(),
geInstance->currentDevice->handle, VK_GetGenericRenderPassInfo(),
NULL, &geInstance->currentDevice->renderPass) != VK_SUCCESS)
{
J2dRlsTrace(J2D_TRACE_INFO, "Cannot create render pass for device")
Expand Down
12 changes: 6 additions & 6 deletions src/java.desktop/share/native/common/java2d/vulkan/VKBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#include "VKTypes.h"
#include "VKTexturePool.h"

struct VKLogicalDevice {
VkDevice device;
struct VKDevice {
VkDevice handle;
VkPhysicalDevice physicalDevice;
VKRenderer* fillTexturePoly;
VKRenderer* fillColorPoly;
Expand Down Expand Up @@ -107,10 +107,10 @@ struct VKLogicalDevice {


struct VKGraphicsEnvironment {
VkInstance vkInstance;
VkPhysicalDevice* physicalDevices;
VKLogicalDevice* devices;
VKLogicalDevice* currentDevice;
VkInstance vkInstance;
VkPhysicalDevice* physicalDevices;
VKDevice* devices;
VKDevice* currentDevice;

#if defined(DEBUG)
VkDebugUtilsMessengerEXT debugMessenger;
Expand Down
28 changes: 14 additions & 14 deletions src/java.desktop/share/native/common/java2d/vulkan/VKBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ VkResult VKBuffer_FindMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeF
return VK_ERROR_UNKNOWN;
}

VKBuffer* VKBuffer_Create(VKLogicalDevice* logicalDevice, VkDeviceSize size,
VKBuffer* VKBuffer_Create(VKDevice* device, VkDeviceSize size,
VkBufferUsageFlags usage, VkMemoryPropertyFlags properties)
{
VKBuffer* buffer = malloc(sizeof (VKBuffer));
Expand All @@ -58,19 +58,19 @@ VKBuffer* VKBuffer_Create(VKLogicalDevice* logicalDevice, VkDeviceSize size,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
};

if (logicalDevice->vkCreateBuffer(logicalDevice->device, &bufferInfo, NULL, &buffer->buffer) != VK_SUCCESS) {
if (device->vkCreateBuffer(device->handle, &bufferInfo, NULL, &buffer->buffer) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "failed to allocate descriptor sets!")
return NULL;
}

buffer->size = size;

VkMemoryRequirements memRequirements;
logicalDevice->vkGetBufferMemoryRequirements(logicalDevice->device, buffer->buffer, &memRequirements);
device->vkGetBufferMemoryRequirements(device->handle, buffer->buffer, &memRequirements);

uint32_t memoryType;

if (VKBuffer_FindMemoryType(logicalDevice->physicalDevice,
if (VKBuffer_FindMemoryType(device->physicalDevice,
memRequirements.memoryTypeBits,
properties, &memoryType) != VK_SUCCESS)
{
Expand All @@ -84,27 +84,27 @@ VKBuffer* VKBuffer_Create(VKLogicalDevice* logicalDevice, VkDeviceSize size,
.memoryTypeIndex = memoryType
};

if (logicalDevice->vkAllocateMemory(logicalDevice->device, &allocInfo, NULL, &buffer->memory) != VK_SUCCESS) {
if (device->vkAllocateMemory(device->handle, &allocInfo, NULL, &buffer->memory) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "failed to allocate buffer memory!");
return NULL;
}

if (logicalDevice->vkBindBufferMemory(logicalDevice->device, buffer->buffer, buffer->memory, 0) != VK_SUCCESS) {
if (device->vkBindBufferMemory(device->handle, buffer->buffer, buffer->memory, 0) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "failed to bind buffer memory!");
return NULL;
}
return buffer;
}

VKBuffer* VKBuffer_CreateFromData(VKLogicalDevice* logicalDevice, void* vertices, VkDeviceSize bufferSize)
VKBuffer* VKBuffer_CreateFromData(VKDevice* device, void* vertices, VkDeviceSize bufferSize)
{
VKBuffer* buffer = VKBuffer_Create(logicalDevice, bufferSize,
VKBuffer* buffer = VKBuffer_Create(device, bufferSize,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

void* data;
if (logicalDevice->vkMapMemory(logicalDevice->device, buffer->memory, 0, bufferSize, 0, &data) != VK_SUCCESS) {
if (device->vkMapMemory(device->handle, buffer->memory, 0, bufferSize, 0, &data) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "failed to map memory!");
return NULL;
}
Expand All @@ -119,23 +119,23 @@ VKBuffer* VKBuffer_CreateFromData(VKLogicalDevice* logicalDevice, void* vertices
};


if (logicalDevice->vkFlushMappedMemoryRanges(logicalDevice->device, 1, &memoryRange) != VK_SUCCESS) {
if (device->vkFlushMappedMemoryRanges(device->handle, 1, &memoryRange) != VK_SUCCESS) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "failed to flush memory!");
return NULL;
}
logicalDevice->vkUnmapMemory(logicalDevice->device, buffer->memory);
device->vkUnmapMemory(device->handle, buffer->memory);
buffer->size = bufferSize;

return buffer;
}

void VKBuffer_free(VKLogicalDevice* logicalDevice, VKBuffer* buffer) {
void VKBuffer_free(VKDevice* device, VKBuffer* buffer) {
if (buffer != NULL) {
if (buffer->buffer != VK_NULL_HANDLE) {
logicalDevice->vkDestroyBuffer(logicalDevice->device, buffer->buffer, NULL);
device->vkDestroyBuffer(device->handle, buffer->buffer, NULL);
}
if (buffer->memory != VK_NULL_HANDLE) {
logicalDevice->vkFreeMemory(logicalDevice->device, buffer->memory, NULL);
device->vkFreeMemory(device->handle, buffer->memory, NULL);
}
free(buffer);
}
Expand Down
6 changes: 3 additions & 3 deletions src/java.desktop/share/native/common/java2d/vulkan/VKBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ struct VKBuffer {
VkResult VKBuffer_FindMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
VkMemoryPropertyFlags properties, uint32_t* pMemoryType);

VKBuffer* VKBuffer_Create(VKLogicalDevice* logicalDevice, VkDeviceSize size,
VKBuffer* VKBuffer_Create(VKDevice* device, VkDeviceSize size,
VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);

VKBuffer* VKBuffer_CreateFromData(VKLogicalDevice* logicalDevice, void* vertices, VkDeviceSize bufferSize);
VKBuffer* VKBuffer_CreateFromData(VKDevice* device, void* vertices, VkDeviceSize bufferSize);

void VKBuffer_free(VKLogicalDevice* logicalDevice, VKBuffer* buffer);
void VKBuffer_free(VKDevice* device, VKBuffer* buffer);

#endif // VKBuffer_h_Included
Loading

0 comments on commit 386d1d0

Please sign in to comment.