Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
vulkan: Clean up renderer
Browse files Browse the repository at this point in the history
- This removes the recording_cb checks since they are done by
  the validation layer anyways.
- Remembering the bound pipeline and avoiding to rebind the same
  pipeline might improve performance (especially if the driver
  doesn't do it).
- We don't have to vkGet*ProcAddr Vulkan 1.1 core functions.
  The original idea was to allow building the library independently
  of the installed loader version but since we have a hard
  requirement of Vulkan 1.1 anyways this does not really
  make sense anymore.
  • Loading branch information
nyorain committed Oct 14, 2021
1 parent 7fe6b0e commit 989b108
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 61 deletions.
10 changes: 1 addition & 9 deletions include/render/vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ struct wlr_vk_instance {
struct {
PFN_vkCreateDebugUtilsMessengerEXT createDebugUtilsMessengerEXT;
PFN_vkDestroyDebugUtilsMessengerEXT destroyDebugUtilsMessengerEXT;

PFN_vkGetPhysicalDeviceProperties2 getPhysicalDeviceProperties2;
PFN_vkGetPhysicalDeviceFeatures2 getPhysicalDeviceFeatures2;
PFN_vkGetPhysicalDeviceFormatProperties2 getPhysicalDeviceFormatProperties2;
PFN_vkGetPhysicalDeviceImageFormatProperties2 getPhysicalDeviceImageFormatProperties2;
} api;
};

Expand Down Expand Up @@ -63,9 +58,6 @@ struct wlr_vk_device {

struct {
PFN_vkGetMemoryFdPropertiesKHR getMemoryFdPropertiesKHR;

PFN_vkBindImageMemory2 bindImageMemory2;
PFN_vkGetImageMemoryRequirements2 getImageMemoryRequirements2;
} api;

uint32_t format_prop_count;
Expand Down Expand Up @@ -186,7 +178,7 @@ struct wlr_vk_renderer {
VkRect2D scissor; // needed for clearing

VkCommandBuffer cb;
bool recording_cb;
VkPipeline bound_pipe;

uint32_t render_width;
uint32_t render_height;
Expand Down
10 changes: 5 additions & 5 deletions render/vulkan/pixel_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static bool query_modifier_support(struct wlr_vk_device *dev,
return false;
}

dev->instance->api.getPhysicalDeviceFormatProperties2(dev->phdev,
vkGetPhysicalDeviceFormatProperties2(dev->phdev,
props->format.vk_format, &fmtp);

props->render_mods = calloc(modp.drmFormatModifierCount,
Expand Down Expand Up @@ -143,7 +143,7 @@ static bool query_modifier_support(struct wlr_vk_device *dev,
fmti.usage = render_usage;

modi.drmFormatModifier = m.drmFormatModifier;
res = dev->instance->api.getPhysicalDeviceImageFormatProperties2(
res = vkGetPhysicalDeviceImageFormatProperties2(
dev->phdev, &fmti, &ifmtp);
if (res != VK_SUCCESS) {
if (res != VK_ERROR_FORMAT_NOT_SUPPORTED) {
Expand Down Expand Up @@ -183,7 +183,7 @@ static bool query_modifier_support(struct wlr_vk_device *dev,
fmti.usage = dma_tex_usage;

modi.drmFormatModifier = m.drmFormatModifier;
res = dev->instance->api.getPhysicalDeviceImageFormatProperties2(
res = vkGetPhysicalDeviceImageFormatProperties2(
dev->phdev, &fmti, &ifmtp);
if (res != VK_SUCCESS) {
if (res != VK_ERROR_FORMAT_NOT_SUPPORTED) {
Expand Down Expand Up @@ -238,7 +238,7 @@ void vulkan_format_props_query(struct wlr_vk_device *dev,
modp.sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT;
fmtp.pNext = &modp;

dev->instance->api.getPhysicalDeviceFormatProperties2(dev->phdev,
vkGetPhysicalDeviceFormatProperties2(dev->phdev,
format->vk_format, &fmtp);

// detailed check
Expand Down Expand Up @@ -267,7 +267,7 @@ void vulkan_format_props_query(struct wlr_vk_device *dev,
fmti.tiling = VK_IMAGE_TILING_OPTIMAL;
fmti.usage = tex_usage;

res = dev->instance->api.getPhysicalDeviceImageFormatProperties2(
res = vkGetPhysicalDeviceImageFormatProperties2(
dev->phdev, &fmti, &ifmtp);
if (res != VK_SUCCESS) {
if (res != VK_ERROR_FORMAT_NOT_SUPPORTED) {
Expand Down
28 changes: 14 additions & 14 deletions render/vulkan/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ static struct wlr_vk_render_buffer *create_render_buffer(
static bool vulkan_bind_buffer(struct wlr_renderer *wlr_renderer,
struct wlr_buffer *wlr_buffer) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(!renderer->recording_cb); // can't be done in between begin/end

if (renderer->current_render_buffer) {
wlr_buffer_unlock(renderer->current_render_buffer->wlr_buffer);
Expand All @@ -548,7 +547,6 @@ static bool vulkan_bind_buffer(struct wlr_renderer *wlr_renderer,
static void vulkan_begin(struct wlr_renderer *wlr_renderer,
uint32_t width, uint32_t height) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(!renderer->recording_cb);
assert(renderer->current_render_buffer);

VkCommandBuffer cb = renderer->cb;
Expand Down Expand Up @@ -582,20 +580,19 @@ static void vulkan_begin(struct wlr_renderer *wlr_renderer,

renderer->render_width = width;
renderer->render_height = height;
renderer->recording_cb = true;
renderer->bound_pipe = VK_NULL_HANDLE;
}

static void vulkan_end(struct wlr_renderer *wlr_renderer) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(renderer->recording_cb);
assert(renderer->current_render_buffer);

VkCommandBuffer render_cb = renderer->cb;
VkCommandBuffer pre_cb = vulkan_record_stage_cb(renderer);

renderer->render_width = 0u;
renderer->render_height = 0u;
renderer->recording_cb = false;
renderer->bound_pipe = VK_NULL_HANDLE;

vkCmdEndRenderPass(render_cb);

Expand Down Expand Up @@ -753,9 +750,8 @@ static void vulkan_end(struct wlr_renderer *wlr_renderer) {

static bool vulkan_render_subtexture_with_matrix(struct wlr_renderer *wlr_renderer,
struct wlr_texture *wlr_texture, const struct wlr_fbox *box,
const float matrix[9], float alpha) {
const float matrix[static 9], float alpha) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(renderer->cb && renderer->recording_cb);
VkCommandBuffer cb = renderer->cb;

struct wlr_vk_texture *texture = vulkan_get_texture(wlr_texture);
Expand All @@ -773,8 +769,12 @@ static bool vulkan_render_subtexture_with_matrix(struct wlr_renderer *wlr_render
wl_list_insert(&renderer->foreign_textures, &texture->foreign_link);
}

vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS,
renderer->current_render_buffer->render_setup->tex_pipe);
VkPipeline pipe = renderer->current_render_buffer->render_setup->tex_pipe;
if (pipe != renderer->bound_pipe) {
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
renderer->bound_pipe = pipe;
}

vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_GRAPHICS,
renderer->pipe_layout, 0, 1, &texture->ds, 0, NULL);

Expand Down Expand Up @@ -819,7 +819,6 @@ static bool vulkan_render_subtexture_with_matrix(struct wlr_renderer *wlr_render
static void vulkan_clear(struct wlr_renderer *wlr_renderer,
const float color[static 4]) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(renderer->cb && renderer->recording_cb);
VkCommandBuffer cb = renderer->cb;

VkClearAttachment att = {0};
Expand All @@ -846,7 +845,6 @@ static void vulkan_clear(struct wlr_renderer *wlr_renderer,
static void vulkan_scissor(struct wlr_renderer *wlr_renderer,
struct wlr_box *box) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(renderer->cb && renderer->recording_cb);
VkCommandBuffer cb = renderer->cb;

uint32_t w = renderer->render_width;
Expand All @@ -871,11 +869,13 @@ static const uint32_t *vulkan_get_shm_texture_formats(
static void vulkan_render_quad_with_matrix(struct wlr_renderer *wlr_renderer,
const float color[static 4], const float matrix[static 9]) {
struct wlr_vk_renderer *renderer = vulkan_get_renderer(wlr_renderer);
assert(renderer->cb && renderer->recording_cb);
VkCommandBuffer cb = renderer->cb;

vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS,
renderer->current_render_buffer->render_setup->quad_pipe);
VkPipeline pipe = renderer->current_render_buffer->render_setup->quad_pipe;
if (pipe != renderer->bound_pipe) {
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipe);
renderer->bound_pipe = pipe;
}

float final_matrix[9];
wlr_matrix_multiply(final_matrix, renderer->projection, matrix);
Expand Down
2 changes: 1 addition & 1 deletion render/vulkan/shaders/texture.frag
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ layout(push_constant) uniform UBO {
} data;

void main() {
out_color = texture(tex, uv);
out_color = textureLod(tex, uv, 0);

// We expect this shader to output pre-alpha-multiplied color values.
// alpha < 0.0 means that this shader should ignore the texture's alpha
Expand Down
4 changes: 2 additions & 2 deletions render/vulkan/texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ VkImage vulkan_import_dmabuf(struct wlr_vk_renderer *renderer,
VkMemoryRequirements2 memr = {0};
memr.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;

renderer->dev->api.getImageMemoryRequirements2(dev, &memri, &memr);
vkGetImageMemoryRequirements2(dev, &memri, &memr);
int mem = vulkan_find_mem_type(renderer->dev, 0,
memr.memoryRequirements.memoryTypeBits & fdp.memoryTypeBits);
if (mem < 0) {
Expand Down Expand Up @@ -554,7 +554,7 @@ VkImage vulkan_import_dmabuf(struct wlr_vk_renderer *renderer,
}
}

res = renderer->dev->api.bindImageMemory2(dev, mem_count, bindi);
res = vkBindImageMemory2(dev, mem_count, bindi);
if (res != VK_SUCCESS) {
wlr_vk_error("vkBindMemory failed", res);
goto error_image;
Expand Down
33 changes: 3 additions & 30 deletions render/vulkan/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,27 +209,6 @@ struct wlr_vk_instance *vulkan_instance_create(size_t ext_count,
goto error;
}

ini->api.getPhysicalDeviceFeatures2 =
(PFN_vkGetPhysicalDeviceFeatures2) vkGetInstanceProcAddr(
ini->instance, "vkGetPhysicalDeviceFeatures2");
ini->api.getPhysicalDeviceProperties2 =
(PFN_vkGetPhysicalDeviceProperties2) vkGetInstanceProcAddr(
ini->instance, "vkGetPhysicalDeviceProperties2");
ini->api.getPhysicalDeviceFormatProperties2 =
(PFN_vkGetPhysicalDeviceFormatProperties2) vkGetInstanceProcAddr(
ini->instance, "vkGetPhysicalDeviceFormatProperties2");
ini->api.getPhysicalDeviceImageFormatProperties2 =
(PFN_vkGetPhysicalDeviceImageFormatProperties2) vkGetInstanceProcAddr(
ini->instance, "vkGetPhysicalDeviceImageFormatProperties2");

if (!ini->api.getPhysicalDeviceFeatures2 ||
!ini->api.getPhysicalDeviceProperties2 ||
!ini->api.getPhysicalDeviceFormatProperties2 ||
!ini->api.getPhysicalDeviceImageFormatProperties2) {
wlr_log(WLR_ERROR, "Could not load required vulkan 1.1 API");
goto error;
}

// debug callback
if (debug_utils_found) {
ini->api.createDebugUtilsMessengerEXT =
Expand Down Expand Up @@ -378,7 +357,7 @@ VkPhysicalDevice vulkan_find_drm_phdev(struct wlr_vk_instance *ini, int drm_fd)
props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
props.pNext = &drm_props;

ini->api.getPhysicalDeviceProperties2(phdev, &props);
vkGetPhysicalDeviceProperties2(phdev, &props);

dev_t primary_devid = makedev(drm_props.primaryMajor, drm_props.primaryMinor);
dev_t render_devid = makedev(drm_props.renderMajor, drm_props.renderMinor);
Expand Down Expand Up @@ -517,14 +496,8 @@ struct wlr_vk_device *vulkan_device_create(struct wlr_vk_instance *ini,
// load api
dev->api.getMemoryFdPropertiesKHR = (PFN_vkGetMemoryFdPropertiesKHR)
vkGetDeviceProcAddr(dev->dev, "vkGetMemoryFdPropertiesKHR");
dev->api.bindImageMemory2 = (PFN_vkBindImageMemory2)
vkGetDeviceProcAddr(dev->dev, "vkBindImageMemory2");
dev->api.getImageMemoryRequirements2 = (PFN_vkGetImageMemoryRequirements2)
vkGetDeviceProcAddr(dev->dev, "vkGetImageMemoryRequirements2");

if (!dev->api.getMemoryFdPropertiesKHR ||
!dev->api.bindImageMemory2 ||
!dev->api.getImageMemoryRequirements2) {

if (!dev->api.getMemoryFdPropertiesKHR) {
wlr_log(WLR_ERROR, "Failed to retrieve required dev function pointers");
goto error;
}
Expand Down

0 comments on commit 989b108

Please sign in to comment.