2016-12-07 13:50:52 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanrenderprivate.h"
|
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
#include "gskrendererprivate.h"
|
|
|
|
#include "gskvulkanbufferprivate.h"
|
2016-12-16 05:10:24 +00:00
|
|
|
#include "gskvulkancommandpoolprivate.h"
|
2016-12-07 13:50:52 +00:00
|
|
|
#include "gskvulkanpipelineprivate.h"
|
2016-12-08 12:26:36 +00:00
|
|
|
#include "gskvulkanrenderpassprivate.h"
|
|
|
|
|
2016-12-18 01:18:01 +00:00
|
|
|
#include "gskvulkanblendpipelineprivate.h"
|
2017-01-10 13:59:20 +00:00
|
|
|
#include "gskvulkanborderpipelineprivate.h"
|
2017-01-18 03:07:09 +00:00
|
|
|
#include "gskvulkanboxshadowpipelineprivate.h"
|
2016-12-18 01:18:01 +00:00
|
|
|
#include "gskvulkancolorpipelineprivate.h"
|
2016-12-30 05:46:34 +00:00
|
|
|
#include "gskvulkaneffectpipelineprivate.h"
|
2016-12-26 16:11:13 +00:00
|
|
|
#include "gskvulkanlineargradientpipelineprivate.h"
|
2016-12-18 01:18:01 +00:00
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
#define ORTHO_NEAR_PLANE -10000
|
|
|
|
#define ORTHO_FAR_PLANE 10000
|
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
#define DESCRIPTOR_POOL_MAXSETS 128
|
|
|
|
#define DESCRIPTOR_POOL_MAXSETS_INCREASE 128
|
|
|
|
|
2016-12-08 16:55:32 +00:00
|
|
|
struct _GskVulkanRender
|
|
|
|
{
|
|
|
|
GskRenderer *renderer;
|
|
|
|
GdkVulkanContext *vulkan;
|
|
|
|
|
|
|
|
graphene_matrix_t mvp;
|
|
|
|
int scale_factor;
|
2016-12-22 18:01:07 +00:00
|
|
|
VkRect2D viewport;
|
2016-12-26 23:18:52 +00:00
|
|
|
cairo_region_t *clip;
|
2016-12-08 16:55:32 +00:00
|
|
|
|
2016-12-08 21:35:16 +00:00
|
|
|
GHashTable *framebuffers;
|
2016-12-16 05:10:24 +00:00
|
|
|
GskVulkanCommandPool *command_pool;
|
2016-12-08 17:03:05 +00:00
|
|
|
VkFence fence;
|
2016-12-08 23:06:59 +00:00
|
|
|
VkRenderPass render_pass;
|
2016-12-14 07:00:58 +00:00
|
|
|
GskVulkanPipelineLayout *layout;
|
2016-12-17 03:22:44 +00:00
|
|
|
GskVulkanUploader *uploader;
|
2017-01-06 16:16:38 +00:00
|
|
|
GskVulkanBuffer *vertex_buffer;
|
2016-12-09 01:55:47 +00:00
|
|
|
|
|
|
|
GHashTable *descriptor_set_indexes;
|
2016-12-08 23:06:59 +00:00
|
|
|
VkDescriptorPool descriptor_pool;
|
2016-12-09 01:55:47 +00:00
|
|
|
uint32_t descriptor_pool_maxsets;
|
|
|
|
VkDescriptorSet *descriptor_sets;
|
|
|
|
gsize n_descriptor_sets;
|
2016-12-14 08:40:15 +00:00
|
|
|
GskVulkanPipeline *pipelines[GSK_VULKAN_N_PIPELINES];
|
2016-12-08 17:03:05 +00:00
|
|
|
|
2016-12-08 21:35:16 +00:00
|
|
|
GskVulkanImage *target;
|
|
|
|
|
2016-12-08 16:55:32 +00:00
|
|
|
GSList *render_passes;
|
|
|
|
GSList *cleanup_images;
|
|
|
|
};
|
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
static void
|
2016-12-26 23:18:52 +00:00
|
|
|
gsk_vulkan_render_setup (GskVulkanRender *self,
|
|
|
|
GskVulkanImage *target,
|
|
|
|
const graphene_rect_t *rect)
|
2016-12-08 12:26:36 +00:00
|
|
|
{
|
|
|
|
GdkWindow *window = gsk_renderer_get_window (self->renderer);
|
|
|
|
graphene_matrix_t modelview, projection;
|
2016-12-26 23:18:52 +00:00
|
|
|
|
|
|
|
self->target = g_object_ref (target);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-22 18:01:07 +00:00
|
|
|
if (rect)
|
|
|
|
{
|
2016-12-26 13:49:34 +00:00
|
|
|
self->viewport = (VkRect2D) { { rect->origin.x, rect->origin.y }, { rect->size.width, rect->size.height } };
|
2016-12-22 18:01:07 +00:00
|
|
|
self->scale_factor = 1;
|
2016-12-26 23:18:52 +00:00
|
|
|
self->clip = cairo_region_create_rectangle (&(cairo_rectangle_int_t) {
|
|
|
|
0, 0,
|
|
|
|
gsk_vulkan_image_get_width (target), gsk_vulkan_image_get_height (target)
|
|
|
|
});
|
2016-12-22 18:01:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->scale_factor = gsk_renderer_get_scale_factor (self->renderer);
|
|
|
|
self->viewport.offset = (VkOffset2D) { 0, 0 };
|
|
|
|
self->viewport.extent.width = gdk_window_get_width (window) * self->scale_factor;
|
|
|
|
self->viewport.extent.height = gdk_window_get_height (window) * self->scale_factor;
|
2016-12-26 23:18:52 +00:00
|
|
|
self->clip = gdk_drawing_context_get_clip (gsk_renderer_get_drawing_context (self->renderer));
|
2016-12-22 18:01:07 +00:00
|
|
|
}
|
2016-12-08 12:26:36 +00:00
|
|
|
|
|
|
|
graphene_matrix_init_scale (&modelview, self->scale_factor, self->scale_factor, 1.0);
|
|
|
|
graphene_matrix_init_ortho (&projection,
|
2016-12-26 13:49:34 +00:00
|
|
|
self->viewport.offset.x, self->viewport.offset.x + self->viewport.extent.width,
|
|
|
|
self->viewport.offset.y, self->viewport.offset.y + self->viewport.extent.height,
|
2016-12-08 12:26:36 +00:00
|
|
|
ORTHO_NEAR_PLANE,
|
|
|
|
ORTHO_FAR_PLANE);
|
2016-12-22 18:01:07 +00:00
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
graphene_matrix_multiply (&modelview, &projection, &self->mvp);
|
|
|
|
}
|
2016-12-07 13:50:52 +00:00
|
|
|
|
2016-12-08 16:46:08 +00:00
|
|
|
GskVulkanRender *
|
|
|
|
gsk_vulkan_render_new (GskRenderer *renderer,
|
2016-12-08 23:06:59 +00:00
|
|
|
GdkVulkanContext *context)
|
2016-12-07 13:50:52 +00:00
|
|
|
{
|
2016-12-08 16:46:08 +00:00
|
|
|
GskVulkanRender *self;
|
2016-12-08 17:03:05 +00:00
|
|
|
VkDevice device;
|
2016-12-08 16:46:08 +00:00
|
|
|
|
|
|
|
self = g_slice_new0 (GskVulkanRender);
|
|
|
|
|
2016-12-07 13:50:52 +00:00
|
|
|
self->vulkan = context;
|
2016-12-08 12:26:36 +00:00
|
|
|
self->renderer = renderer;
|
2016-12-08 21:35:16 +00:00
|
|
|
self->framebuffers = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2016-12-09 01:55:47 +00:00
|
|
|
self->descriptor_set_indexes = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2016-12-07 13:50:52 +00:00
|
|
|
|
2016-12-08 17:03:05 +00:00
|
|
|
device = gdk_vulkan_context_get_device (self->vulkan);
|
|
|
|
|
2016-12-16 05:10:24 +00:00
|
|
|
self->command_pool = gsk_vulkan_command_pool_new (self->vulkan);
|
2016-12-08 17:03:05 +00:00
|
|
|
GSK_VK_CHECK (vkCreateFence, device,
|
|
|
|
&(VkFenceCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
2016-12-08 17:40:35 +00:00
|
|
|
.flags = VK_FENCE_CREATE_SIGNALED_BIT
|
2016-12-08 17:03:05 +00:00
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->fence);
|
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
self->descriptor_pool_maxsets = DESCRIPTOR_POOL_MAXSETS;
|
2016-12-08 23:06:59 +00:00
|
|
|
GSK_VK_CHECK (vkCreateDescriptorPool, device,
|
|
|
|
&(VkDescriptorPoolCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
2016-12-09 01:55:47 +00:00
|
|
|
.maxSets = self->descriptor_pool_maxsets,
|
2016-12-08 23:06:59 +00:00
|
|
|
.poolSizeCount = 1,
|
|
|
|
.pPoolSizes = (VkDescriptorPoolSize[1]) {
|
|
|
|
{
|
|
|
|
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
2017-01-06 04:39:03 +00:00
|
|
|
.descriptorCount = self->descriptor_pool_maxsets
|
2016-12-08 23:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->descriptor_pool);
|
|
|
|
|
|
|
|
GSK_VK_CHECK (vkCreateRenderPass, gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
&(VkRenderPassCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
|
|
|
|
.attachmentCount = 1,
|
|
|
|
.pAttachments = (VkAttachmentDescription[]) {
|
|
|
|
{
|
|
|
|
.format = gdk_vulkan_context_get_image_format (self->vulkan),
|
|
|
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
2016-12-09 04:51:51 +00:00
|
|
|
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
|
2016-12-08 23:06:59 +00:00
|
|
|
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
|
|
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.subpassCount = 1,
|
|
|
|
.pSubpasses = (VkSubpassDescription []) {
|
|
|
|
{
|
|
|
|
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
|
|
.inputAttachmentCount = 0,
|
|
|
|
.colorAttachmentCount = 1,
|
|
|
|
.pColorAttachments = (VkAttachmentReference []) {
|
|
|
|
{
|
|
|
|
.attachment = 0,
|
|
|
|
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.pResolveAttachments = (VkAttachmentReference []) {
|
|
|
|
{
|
|
|
|
.attachment = VK_ATTACHMENT_UNUSED,
|
|
|
|
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.pDepthStencilAttachment = NULL,
|
|
|
|
.preserveAttachmentCount = 1,
|
|
|
|
.pPreserveAttachments = (uint32_t []) { 0 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.dependencyCount = 0
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->render_pass);
|
|
|
|
|
2016-12-14 07:00:58 +00:00
|
|
|
self->layout = gsk_vulkan_pipeline_layout_new (self->vulkan);
|
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
self->uploader = gsk_vulkan_uploader_new (self->vulkan, self->command_pool);
|
|
|
|
|
2016-12-08 16:46:08 +00:00
|
|
|
return self;
|
2016-12-07 13:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 21:35:16 +00:00
|
|
|
typedef struct {
|
|
|
|
VkFramebuffer framebuffer;
|
|
|
|
} HashFramebufferEntry;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_render_remove_framebuffer_from_image (gpointer data,
|
|
|
|
GObject *image)
|
|
|
|
{
|
|
|
|
GskVulkanRender *self = data;
|
|
|
|
HashFramebufferEntry *fb;
|
|
|
|
|
|
|
|
fb = g_hash_table_lookup (self->framebuffers, image);
|
|
|
|
g_hash_table_remove (self->framebuffers, image);
|
|
|
|
|
|
|
|
vkDestroyFramebuffer (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
fb->framebuffer,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_slice_free (HashFramebufferEntry, fb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VkFramebuffer
|
|
|
|
gsk_vulkan_render_get_framebuffer (GskVulkanRender *self,
|
|
|
|
GskVulkanImage *image)
|
|
|
|
{
|
|
|
|
HashFramebufferEntry *fb;
|
|
|
|
|
|
|
|
fb = g_hash_table_lookup (self->framebuffers, image);
|
|
|
|
if (fb)
|
|
|
|
return fb->framebuffer;
|
|
|
|
|
|
|
|
fb = g_slice_new0 (HashFramebufferEntry);
|
|
|
|
GSK_VK_CHECK (vkCreateFramebuffer, gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
&(VkFramebufferCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
|
|
|
|
.renderPass = self->render_pass,
|
|
|
|
.attachmentCount = 1,
|
|
|
|
.pAttachments = (VkImageView[1]) {
|
|
|
|
gsk_vulkan_image_get_image_view (image)
|
|
|
|
},
|
|
|
|
.width = gsk_vulkan_image_get_width (image),
|
|
|
|
.height = gsk_vulkan_image_get_height (image),
|
|
|
|
.layers = 1
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&fb->framebuffer);
|
|
|
|
g_hash_table_insert (self->framebuffers, image, fb);
|
|
|
|
g_object_weak_ref (G_OBJECT (image), gsk_vulkan_render_remove_framebuffer_from_image, self);
|
|
|
|
|
|
|
|
return fb->framebuffer;
|
|
|
|
}
|
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_render_add_cleanup_image (GskVulkanRender *self,
|
|
|
|
GskVulkanImage *image)
|
|
|
|
{
|
|
|
|
self->cleanup_images = g_slist_prepend (self->cleanup_images, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_render_add_node (GskVulkanRender *self,
|
|
|
|
GskRenderNode *node)
|
|
|
|
{
|
|
|
|
GskVulkanRenderPass *pass = gsk_vulkan_render_pass_new (self->vulkan);
|
|
|
|
|
|
|
|
self->render_passes = g_slist_prepend (self->render_passes, pass);
|
|
|
|
|
2016-12-23 20:36:17 +00:00
|
|
|
gsk_vulkan_render_pass_add (pass,
|
|
|
|
self,
|
|
|
|
&self->mvp,
|
|
|
|
&GRAPHENE_RECT_INIT (
|
|
|
|
self->viewport.offset.x, self->viewport.offset.y,
|
|
|
|
self->viewport.extent.width, self->viewport.extent.height
|
|
|
|
),
|
|
|
|
node);
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_render_upload (GskVulkanRender *self)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = self->render_passes; l; l = l->next)
|
|
|
|
{
|
2016-12-17 03:22:44 +00:00
|
|
|
gsk_vulkan_render_pass_upload (l->data, self, self->uploader);
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
2016-12-17 03:54:19 +00:00
|
|
|
|
|
|
|
gsk_vulkan_uploader_upload (self->uploader);
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gsize
|
2016-12-18 00:45:07 +00:00
|
|
|
gsk_vulkan_renderer_count_vertex_data (GskVulkanRender *self)
|
2016-12-08 12:26:36 +00:00
|
|
|
{
|
2016-12-18 00:45:07 +00:00
|
|
|
gsize n_bytes;
|
2016-12-08 12:26:36 +00:00
|
|
|
GSList *l;
|
|
|
|
|
2016-12-18 00:45:07 +00:00
|
|
|
n_bytes = 0;
|
2016-12-08 12:26:36 +00:00
|
|
|
for (l = self->render_passes; l; l = l->next)
|
|
|
|
{
|
2016-12-18 00:45:07 +00:00
|
|
|
n_bytes += gsk_vulkan_render_pass_count_vertex_data (l->data);
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 00:45:07 +00:00
|
|
|
return n_bytes;
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GskVulkanBuffer *
|
2016-12-18 00:45:07 +00:00
|
|
|
gsk_vulkan_render_collect_vertex_data (GskVulkanRender *self)
|
2016-12-08 12:26:36 +00:00
|
|
|
{
|
|
|
|
GskVulkanBuffer *buffer;
|
2016-12-18 00:45:07 +00:00
|
|
|
guchar *data;
|
2016-12-08 12:26:36 +00:00
|
|
|
GSList *l;
|
2016-12-18 00:45:07 +00:00
|
|
|
gsize offset, n_bytes;
|
2016-12-08 12:26:36 +00:00
|
|
|
|
|
|
|
offset = 0;
|
2016-12-18 00:45:07 +00:00
|
|
|
n_bytes = gsk_vulkan_renderer_count_vertex_data (self);
|
|
|
|
buffer = gsk_vulkan_buffer_new (self->vulkan, n_bytes);
|
|
|
|
data = gsk_vulkan_buffer_map (buffer);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
|
|
|
for (l = self->render_passes; l; l = l->next)
|
|
|
|
{
|
2016-12-18 00:45:07 +00:00
|
|
|
offset += gsk_vulkan_render_pass_collect_vertex_data (l->data, data, offset, n_bytes - offset);
|
|
|
|
g_assert (offset <= n_bytes);
|
2016-12-08 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gsk_vulkan_buffer_unmap (buffer);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-12-14 08:40:15 +00:00
|
|
|
GskVulkanPipeline *
|
|
|
|
gsk_vulkan_render_get_pipeline (GskVulkanRender *self,
|
|
|
|
GskVulkanPipelineType type)
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
2016-12-18 01:18:01 +00:00
|
|
|
GskVulkanPipeline * (* create_func) (GskVulkanPipelineLayout *layout, const char *name, VkRenderPass render_pass);
|
2016-12-14 08:40:15 +00:00
|
|
|
} pipeline_info[GSK_VULKAN_N_PIPELINES] = {
|
2017-01-01 22:47:53 +00:00
|
|
|
{ "blend", gsk_vulkan_blend_pipeline_new },
|
|
|
|
{ "blend-clip", gsk_vulkan_blend_pipeline_new },
|
|
|
|
{ "blend-clip-rounded", gsk_vulkan_blend_pipeline_new },
|
2016-12-24 05:16:54 +00:00
|
|
|
{ "color", gsk_vulkan_color_pipeline_new },
|
2016-12-25 05:01:54 +00:00
|
|
|
{ "color-clip", gsk_vulkan_color_pipeline_new },
|
2016-12-26 16:11:13 +00:00
|
|
|
{ "color-clip-rounded", gsk_vulkan_color_pipeline_new },
|
|
|
|
{ "linear", gsk_vulkan_linear_gradient_pipeline_new },
|
|
|
|
{ "linear-clip", gsk_vulkan_linear_gradient_pipeline_new },
|
2016-12-30 05:46:34 +00:00
|
|
|
{ "linear-clip-rounded", gsk_vulkan_linear_gradient_pipeline_new },
|
2016-12-31 12:24:21 +00:00
|
|
|
{ "color-matrix", gsk_vulkan_effect_pipeline_new },
|
|
|
|
{ "color-matrix-clip", gsk_vulkan_effect_pipeline_new },
|
2017-01-10 13:59:20 +00:00
|
|
|
{ "color-matrix-clip-rounded", gsk_vulkan_effect_pipeline_new },
|
|
|
|
{ "border", gsk_vulkan_border_pipeline_new },
|
|
|
|
{ "border-clip", gsk_vulkan_border_pipeline_new },
|
2017-01-18 03:07:09 +00:00
|
|
|
{ "border-clip-rounded", gsk_vulkan_border_pipeline_new },
|
|
|
|
{ "inset-shadow", gsk_vulkan_box_shadow_pipeline_new },
|
|
|
|
{ "inset-shadow-clip", gsk_vulkan_box_shadow_pipeline_new },
|
|
|
|
{ "inset-shadow-clip-rounded", gsk_vulkan_box_shadow_pipeline_new },
|
|
|
|
{ "outset-shadow", gsk_vulkan_box_shadow_pipeline_new },
|
|
|
|
{ "outset-shadow-clip", gsk_vulkan_box_shadow_pipeline_new },
|
|
|
|
{ "outset-shadow-clip-rounded", gsk_vulkan_box_shadow_pipeline_new },
|
2016-12-14 08:40:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
g_return_val_if_fail (type < GSK_VULKAN_N_PIPELINES, NULL);
|
|
|
|
|
|
|
|
if (self->pipelines[type] == NULL)
|
|
|
|
{
|
2016-12-18 01:18:01 +00:00
|
|
|
self->pipelines[type] = pipeline_info[type].create_func (self->layout,
|
|
|
|
pipeline_info[type].name,
|
|
|
|
self->render_pass);
|
2016-12-14 08:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self->pipelines[type];
|
|
|
|
}
|
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
VkDescriptorSet
|
|
|
|
gsk_vulkan_render_get_descriptor_set (GskVulkanRender *self,
|
|
|
|
gsize id)
|
|
|
|
{
|
|
|
|
g_assert (id < self->n_descriptor_sets);
|
|
|
|
|
|
|
|
return self->descriptor_sets[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
gsize
|
|
|
|
gsk_vulkan_render_reserve_descriptor_set (GskVulkanRender *self,
|
|
|
|
GskVulkanImage *source)
|
|
|
|
{
|
|
|
|
gpointer id_plus_one;
|
|
|
|
|
|
|
|
id_plus_one = g_hash_table_lookup (self->descriptor_set_indexes, source);
|
|
|
|
if (id_plus_one)
|
|
|
|
return GPOINTER_TO_SIZE (id_plus_one) - 1;
|
|
|
|
|
|
|
|
id_plus_one = GSIZE_TO_POINTER (g_hash_table_size (self->descriptor_set_indexes) + 1);
|
|
|
|
g_hash_table_insert (self->descriptor_set_indexes, source, id_plus_one);
|
|
|
|
|
|
|
|
return GPOINTER_TO_SIZE (id_plus_one) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_render_prepare_descriptor_sets (GskVulkanRender *self,
|
|
|
|
VkSampler sampler)
|
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
|
|
|
VkDevice device;
|
|
|
|
GSList *l;
|
|
|
|
guint i, needed_sets;
|
|
|
|
|
|
|
|
device = gdk_vulkan_context_get_device (self->vulkan);
|
|
|
|
|
|
|
|
for (l = self->render_passes; l; l = l->next)
|
|
|
|
{
|
|
|
|
gsk_vulkan_render_pass_reserve_descriptor_sets (l->data, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
needed_sets = g_hash_table_size (self->descriptor_set_indexes);
|
|
|
|
if (needed_sets > self->n_descriptor_sets)
|
|
|
|
{
|
2016-12-09 05:09:52 +00:00
|
|
|
if (needed_sets > self->descriptor_pool_maxsets)
|
|
|
|
{
|
|
|
|
guint added_sets = needed_sets - self->descriptor_pool_maxsets;
|
|
|
|
added_sets = added_sets + DESCRIPTOR_POOL_MAXSETS_INCREASE - 1;
|
|
|
|
added_sets -= added_sets % DESCRIPTOR_POOL_MAXSETS_INCREASE;
|
|
|
|
|
|
|
|
vkDestroyDescriptorPool (device,
|
|
|
|
self->descriptor_pool,
|
|
|
|
NULL);
|
|
|
|
self->descriptor_pool_maxsets += added_sets;
|
|
|
|
GSK_VK_CHECK (vkCreateDescriptorPool, device,
|
|
|
|
&(VkDescriptorPoolCreateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
|
|
|
.maxSets = self->descriptor_pool_maxsets,
|
|
|
|
.poolSizeCount = 1,
|
|
|
|
.pPoolSizes = (VkDescriptorPoolSize[1]) {
|
|
|
|
{
|
|
|
|
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
2017-01-06 04:39:03 +00:00
|
|
|
.descriptorCount = self->descriptor_pool_maxsets
|
2016-12-09 05:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
NULL,
|
|
|
|
&self->descriptor_pool);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSK_VK_CHECK (vkResetDescriptorPool, device,
|
|
|
|
self->descriptor_pool,
|
|
|
|
0);
|
|
|
|
}
|
2016-12-09 01:55:47 +00:00
|
|
|
|
|
|
|
self->n_descriptor_sets = needed_sets;
|
|
|
|
self->descriptor_sets = g_renew (VkDescriptorSet, self->descriptor_sets, needed_sets);
|
2017-01-06 17:10:12 +00:00
|
|
|
}
|
2016-12-09 01:55:47 +00:00
|
|
|
|
2017-01-06 17:10:12 +00:00
|
|
|
VkDescriptorSetLayout *layouts = g_newa (VkDescriptorSetLayout, needed_sets);
|
|
|
|
for (i = 0; i < needed_sets; i++)
|
|
|
|
{
|
|
|
|
layouts[i] = gsk_vulkan_pipeline_layout_get_descriptor_set_layout (self->layout);
|
2016-12-09 01:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 17:10:12 +00:00
|
|
|
GSK_VK_CHECK (vkAllocateDescriptorSets, device,
|
|
|
|
&(VkDescriptorSetAllocateInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
|
|
|
.descriptorPool = self->descriptor_pool,
|
|
|
|
.descriptorSetCount = needed_sets,
|
|
|
|
.pSetLayouts = layouts
|
|
|
|
},
|
|
|
|
self->descriptor_sets);
|
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->descriptor_set_indexes);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
|
|
|
{
|
|
|
|
GskVulkanImage *image = key;
|
|
|
|
gsize id = GPOINTER_TO_SIZE (value) - 1;
|
|
|
|
|
|
|
|
vkUpdateDescriptorSets (device,
|
|
|
|
1,
|
|
|
|
(VkWriteDescriptorSet[1]) {
|
|
|
|
{
|
|
|
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
|
|
.dstSet = self->descriptor_sets[id],
|
|
|
|
.dstBinding = 0,
|
|
|
|
.dstArrayElement = 0,
|
|
|
|
.descriptorCount = 1,
|
|
|
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
.pImageInfo = &(VkDescriptorImageInfo) {
|
|
|
|
.sampler = sampler,
|
|
|
|
.imageView = gsk_vulkan_image_get_image_view (image),
|
|
|
|
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
void
|
|
|
|
gsk_vulkan_render_draw (GskVulkanRender *self,
|
|
|
|
VkSampler sampler)
|
|
|
|
{
|
2016-12-16 05:10:24 +00:00
|
|
|
VkCommandBuffer command_buffer;
|
2016-12-08 12:26:36 +00:00
|
|
|
GSList *l;
|
2016-12-26 23:18:52 +00:00
|
|
|
guint i;
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
gsk_vulkan_render_prepare_descriptor_sets (self, sampler);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-16 05:10:24 +00:00
|
|
|
command_buffer = gsk_vulkan_command_pool_get_buffer (self->command_pool);
|
|
|
|
|
2017-01-06 16:16:38 +00:00
|
|
|
self->vertex_buffer = gsk_vulkan_render_collect_vertex_data (self);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-16 05:10:24 +00:00
|
|
|
vkCmdSetViewport (command_buffer,
|
2016-12-08 12:26:36 +00:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
&(VkViewport) {
|
2016-12-26 13:49:34 +00:00
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
2016-12-22 18:01:07 +00:00
|
|
|
.width = self->viewport.extent.width,
|
|
|
|
.height = self->viewport.extent.height,
|
2016-12-08 12:26:36 +00:00
|
|
|
.minDepth = 0,
|
|
|
|
.maxDepth = 1
|
|
|
|
});
|
|
|
|
|
2016-12-26 23:18:52 +00:00
|
|
|
for (i = 0; i < cairo_region_num_rectangles (self->clip); i++)
|
|
|
|
{
|
|
|
|
cairo_rectangle_int_t rect;
|
|
|
|
|
|
|
|
cairo_region_get_rectangle (self->clip, i, &rect);
|
|
|
|
|
|
|
|
vkCmdSetScissor (command_buffer,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
&(VkRect2D) {
|
|
|
|
{ rect.x * self->scale_factor, rect.y * self->scale_factor },
|
|
|
|
{ rect.width * self->scale_factor, rect.height * self->scale_factor }
|
|
|
|
});
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass (command_buffer,
|
|
|
|
&(VkRenderPassBeginInfo) {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
|
|
|
.renderPass = self->render_pass,
|
|
|
|
.framebuffer = gsk_vulkan_render_get_framebuffer (self, self->target),
|
|
|
|
.renderArea = {
|
|
|
|
{ rect.x * self->scale_factor, rect.y * self->scale_factor },
|
|
|
|
{ rect.width * self->scale_factor, rect.height * self->scale_factor }
|
|
|
|
},
|
|
|
|
.clearValueCount = 1,
|
|
|
|
.pClearValues = (VkClearValue [1]) {
|
|
|
|
{ .color = { .float32 = { 0.f, 0.f, 0.f, 0.f } } }
|
2016-12-22 18:01:07 +00:00
|
|
|
}
|
2016-12-08 12:26:36 +00:00
|
|
|
},
|
2016-12-26 23:18:52 +00:00
|
|
|
VK_SUBPASS_CONTENTS_INLINE);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-26 23:18:52 +00:00
|
|
|
for (l = self->render_passes; l; l = l->next)
|
|
|
|
{
|
2017-01-06 16:16:38 +00:00
|
|
|
gsk_vulkan_render_pass_draw (l->data, self, self->vertex_buffer, self->layout, command_buffer);
|
2016-12-26 23:18:52 +00:00
|
|
|
}
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-26 23:18:52 +00:00
|
|
|
vkCmdEndRenderPass (command_buffer);
|
|
|
|
}
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2016-12-16 05:10:24 +00:00
|
|
|
gsk_vulkan_command_pool_submit_buffer (self->command_pool, command_buffer, self->fence);
|
2016-12-07 13:50:52 +00:00
|
|
|
|
2016-12-08 18:56:56 +00:00
|
|
|
if (GSK_RENDER_MODE_CHECK (SYNC))
|
|
|
|
{
|
|
|
|
GSK_VK_CHECK (vkWaitForFences, gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
1,
|
|
|
|
&self->fence,
|
|
|
|
VK_TRUE,
|
|
|
|
INT64_MAX);
|
|
|
|
}
|
2016-12-07 13:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 18:01:07 +00:00
|
|
|
GskTexture *
|
|
|
|
gsk_vulkan_render_download_target (GskVulkanRender *self)
|
|
|
|
{
|
|
|
|
gsk_vulkan_uploader_reset (self->uploader);
|
|
|
|
|
|
|
|
return gsk_vulkan_image_download (self->target, self->uploader);
|
|
|
|
}
|
|
|
|
|
2016-12-08 17:40:35 +00:00
|
|
|
static void
|
|
|
|
gsk_vulkan_render_cleanup (GskVulkanRender *self)
|
2016-12-07 13:50:52 +00:00
|
|
|
{
|
2016-12-08 17:03:05 +00:00
|
|
|
VkDevice device = gdk_vulkan_context_get_device (self->vulkan);
|
|
|
|
|
2016-12-08 17:40:35 +00:00
|
|
|
/* XXX: Wait for fence here or just in reset()? */
|
|
|
|
GSK_VK_CHECK (vkWaitForFences, device,
|
|
|
|
1,
|
|
|
|
&self->fence,
|
|
|
|
VK_TRUE,
|
|
|
|
INT64_MAX);
|
|
|
|
|
|
|
|
GSK_VK_CHECK (vkResetFences, device,
|
|
|
|
1,
|
|
|
|
&self->fence);
|
2016-12-16 05:10:24 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
gsk_vulkan_uploader_reset (self->uploader);
|
|
|
|
|
2016-12-16 05:10:24 +00:00
|
|
|
gsk_vulkan_command_pool_reset (self->command_pool);
|
2016-12-08 12:26:36 +00:00
|
|
|
|
2017-01-06 16:16:38 +00:00
|
|
|
g_clear_pointer (&self->vertex_buffer, gsk_vulkan_buffer_free);
|
|
|
|
|
2016-12-09 01:55:47 +00:00
|
|
|
g_hash_table_remove_all (self->descriptor_set_indexes);
|
2017-01-06 15:31:03 +00:00
|
|
|
GSK_VK_CHECK (vkResetDescriptorPool, device,
|
|
|
|
self->descriptor_pool,
|
|
|
|
0);
|
2016-12-09 01:55:47 +00:00
|
|
|
|
2016-12-08 12:26:36 +00:00
|
|
|
g_slist_free_full (self->render_passes, (GDestroyNotify) gsk_vulkan_render_pass_free);
|
2016-12-08 17:40:35 +00:00
|
|
|
self->render_passes = NULL;
|
2016-12-08 19:19:56 +00:00
|
|
|
g_slist_free_full (self->cleanup_images, g_object_unref);
|
2016-12-08 17:40:35 +00:00
|
|
|
self->cleanup_images = NULL;
|
2016-12-08 21:35:16 +00:00
|
|
|
|
2016-12-26 23:18:52 +00:00
|
|
|
g_clear_pointer (&self->clip, cairo_region_destroy);
|
2016-12-08 21:35:16 +00:00
|
|
|
g_clear_object (&self->target);
|
2016-12-08 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_render_free (GskVulkanRender *self)
|
|
|
|
{
|
2016-12-08 21:35:16 +00:00
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
2016-12-08 17:40:35 +00:00
|
|
|
VkDevice device;
|
2016-12-14 08:40:15 +00:00
|
|
|
guint i;
|
2016-12-08 17:40:35 +00:00
|
|
|
|
|
|
|
gsk_vulkan_render_cleanup (self);
|
|
|
|
|
|
|
|
device = gdk_vulkan_context_get_device (self->vulkan);
|
2016-12-08 16:46:08 +00:00
|
|
|
|
2016-12-08 21:35:16 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->framebuffers);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
|
|
|
{
|
|
|
|
HashFramebufferEntry *fb = value;
|
|
|
|
|
|
|
|
vkDestroyFramebuffer (gdk_vulkan_context_get_device (self->vulkan),
|
|
|
|
fb->framebuffer,
|
|
|
|
NULL);
|
|
|
|
g_slice_free (HashFramebufferEntry, fb);
|
|
|
|
g_object_weak_unref (G_OBJECT (key), gsk_vulkan_render_remove_framebuffer_from_image, self);
|
|
|
|
g_hash_table_iter_remove (&iter);
|
|
|
|
}
|
|
|
|
g_hash_table_unref (self->framebuffers);
|
|
|
|
|
2016-12-14 08:40:15 +00:00
|
|
|
for (i = 0; i < GSK_VULKAN_N_PIPELINES; i++)
|
|
|
|
g_clear_object (&self->pipelines[i]);
|
2016-12-08 23:06:59 +00:00
|
|
|
|
2016-12-17 03:22:44 +00:00
|
|
|
g_clear_pointer (&self->uploader, gsk_vulkan_uploader_free);
|
|
|
|
|
2016-12-14 07:00:58 +00:00
|
|
|
g_clear_pointer (&self->layout, gsk_vulkan_pipeline_layout_unref);
|
|
|
|
|
2016-12-08 23:06:59 +00:00
|
|
|
vkDestroyRenderPass (device,
|
|
|
|
self->render_pass,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
vkDestroyDescriptorPool (device,
|
|
|
|
self->descriptor_pool,
|
|
|
|
NULL);
|
2016-12-09 01:55:47 +00:00
|
|
|
g_free (self->descriptor_sets);
|
|
|
|
g_hash_table_unref (self->descriptor_set_indexes);
|
2016-12-08 23:06:59 +00:00
|
|
|
|
2016-12-08 17:03:05 +00:00
|
|
|
vkDestroyFence (device,
|
|
|
|
self->fence,
|
|
|
|
NULL);
|
2016-12-16 05:10:24 +00:00
|
|
|
|
|
|
|
gsk_vulkan_command_pool_free (self->command_pool);
|
2016-12-08 17:03:05 +00:00
|
|
|
|
2016-12-08 16:46:08 +00:00
|
|
|
g_slice_free (GskVulkanRender, self);
|
2016-12-07 13:50:52 +00:00
|
|
|
}
|
2016-12-08 16:55:32 +00:00
|
|
|
|
2016-12-08 17:40:35 +00:00
|
|
|
gboolean
|
|
|
|
gsk_vulkan_render_is_busy (GskVulkanRender *self)
|
|
|
|
{
|
|
|
|
return vkGetFenceStatus (gdk_vulkan_context_get_device (self->vulkan), self->fence) != VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-22 18:01:07 +00:00
|
|
|
gsk_vulkan_render_reset (GskVulkanRender *self,
|
|
|
|
GskVulkanImage *target,
|
|
|
|
const graphene_rect_t *rect)
|
2016-12-08 17:40:35 +00:00
|
|
|
{
|
|
|
|
gsk_vulkan_render_cleanup (self);
|
|
|
|
|
2016-12-26 23:18:52 +00:00
|
|
|
gsk_vulkan_render_setup (self, target, rect);
|
2016-12-08 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 16:55:32 +00:00
|
|
|
GskRenderer *
|
|
|
|
gsk_vulkan_render_get_renderer (GskVulkanRender *self)
|
|
|
|
{
|
|
|
|
return self->renderer;
|
|
|
|
}
|