[gtk+/wip/otte/vulkan: 7/28] vulkan: Make MVP matrix available to vertex shader
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/otte/vulkan: 7/28] vulkan: Make MVP matrix available to vertex shader
- Date: Fri, 9 Dec 2016 06:27:38 +0000 (UTC)
commit e4394f6e097917d9090bdcc99f075a2bf91417ef
Author: Benjamin Otte <otte redhat com>
Date: Wed Dec 7 01:48:31 2016 +0100
vulkan: Make MVP matrix available to vertex shader
We use push constants for this.
gsk/gskvulkanpipeline.c | 12 +++++++-
gsk/gskvulkanrenderer.c | 50 +++++++++++++++++++++++++++-------
gsk/resources/vulkan/blit.vert.glsl | 6 +++-
gsk/resources/vulkan/blit.vert.spv | Bin 848 -> 1104 bytes
4 files changed, 55 insertions(+), 13 deletions(-)
---
diff --git a/gsk/gskvulkanpipeline.c b/gsk/gskvulkanpipeline.c
index 012eb8f..25e81f5 100644
--- a/gsk/gskvulkanpipeline.c
+++ b/gsk/gskvulkanpipeline.c
@@ -4,6 +4,8 @@
#include "gskvulkanshaderprivate.h"
+#include <graphene.h>
+
struct _GskVulkanPipeline
{
GObject parent_instance;
@@ -93,7 +95,15 @@ gsk_vulkan_pipeline_new (GdkVulkanContext *context,
&(VkPipelineLayoutCreateInfo) {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
- .pSetLayouts = &self->descriptor_set_layout
+ .pSetLayouts = &self->descriptor_set_layout,
+ .pushConstantRangeCount = 1,
+ .pPushConstantRanges = (VkPushConstantRange[1]) {
+ {
+ .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
+ .offset = 0,
+ .size = sizeof (graphene_matrix_t)
+ }
+ }
},
NULL,
&self->pipeline_layout);
diff --git a/gsk/gskvulkanrenderer.c b/gsk/gskvulkanrenderer.c
index 060459b..e2f9591 100644
--- a/gsk/gskvulkanrenderer.c
+++ b/gsk/gskvulkanrenderer.c
@@ -12,6 +12,8 @@
#include "gskvulkanimageprivate.h"
#include "gskvulkanpipelineprivate.h"
+#include <graphene.h>
+
typedef struct _GskVulkanTarget GskVulkanTarget;
#ifdef G_ENABLE_DEBUG
@@ -374,14 +376,17 @@ gsk_vulkan_renderer_do_render_commands (GskVulkanRenderer *self,
VkCommandBuffer command_buffer)
{
GskVulkanBuffer *buffer;
+ GdkWindow *window = gsk_renderer_get_window (GSK_RENDERER (self));
+ int width = gdk_window_get_width (window);
+ int height = gdk_window_get_height (window);
float pts[] = {
- -1.0, -1.0, 0.0, 0.0,
- 1.0, -1.0, 1.0, 0.0,
- -1.0, 1.0, 0.0, 1.0,
+ 0.0, 0.0, 0.0, 0.0,
+ width, 0.0, 1.0, 0.0,
+ 0.0, height, 0.0, 1.0,
- -1.0, 1.0, 0.0, 1.0,
- 1.0, -1.0, 1.0, 0.0,
- 1.0, 1.0, 1.0, 1.0
+ 0.0, height, 0.0, 1.0,
+ width, 0.0, 1.0, 0.0,
+ width, height, 1.0, 1.0
};
guchar *data;
@@ -419,6 +424,9 @@ gsk_vulkan_renderer_do_render_commands (GskVulkanRenderer *self,
gsk_vulkan_buffer_free (buffer);
}
+#define ORTHO_NEAR_PLANE -10000
+#define ORTHO_FAR_PLANE 10000
+
static void
gsk_vulkan_renderer_do_render_pass (GskVulkanRenderer *self,
VkCommandBuffer command_buffer,
@@ -426,10 +434,15 @@ gsk_vulkan_renderer_do_render_pass (GskVulkanRenderer *self,
{
GdkRectangle extents;
GdkWindow *window;
+ int scale_factor, unscaled_width, unscaled_height;
+ graphene_matrix_t modelview, projection, mvp;
window = gsk_renderer_get_window (GSK_RENDERER (self));
cairo_region_get_extents (gdk_drawing_context_get_clip (gsk_renderer_get_drawing_context (GSK_RENDERER
(self))),
&extents);
+ scale_factor = gsk_renderer_get_scale_factor (GSK_RENDERER (self));
+ unscaled_width = gdk_window_get_width (window) * scale_factor;
+ unscaled_height = gdk_window_get_height (window) * scale_factor;
vkUpdateDescriptorSets (gdk_vulkan_context_get_device (self->vulkan),
1,
@@ -457,7 +470,7 @@ gsk_vulkan_renderer_do_render_pass (GskVulkanRenderer *self,
.framebuffer = self->targets[gdk_vulkan_context_get_draw_index
(self->vulkan)]->framebuffer,
.renderArea = {
{ 0, 0 },
- { gdk_window_get_width (window), gdk_window_get_height (window) }
+ { unscaled_width, unscaled_height }
},
.clearValueCount = 1,
.pClearValues = (VkClearValue [1]) {
@@ -472,8 +485,8 @@ gsk_vulkan_renderer_do_render_pass (GskVulkanRenderer *self,
&(VkViewport) {
.x = 0,
.y = 0,
- .width = gdk_window_get_width (window),
- .height = gdk_window_get_height (window),
+ .width = unscaled_width,
+ .height = unscaled_height,
.minDepth = 0,
.maxDepth = 1
});
@@ -482,10 +495,25 @@ gsk_vulkan_renderer_do_render_pass (GskVulkanRenderer *self,
0,
1,
&(VkRect2D) {
- { extents.x, extents.y },
- { extents.width, extents.height }
+ { extents.x * scale_factor, extents.y * scale_factor },
+ { extents.width * scale_factor, extents.height * scale_factor }
});
+ graphene_matrix_init_scale (&modelview, scale_factor, scale_factor, 1.0);
+ graphene_matrix_init_ortho (&projection,
+ 0, unscaled_width,
+ 0, unscaled_height,
+ ORTHO_NEAR_PLANE,
+ ORTHO_FAR_PLANE);
+ graphene_matrix_multiply (&modelview, &projection, &mvp);
+
+ vkCmdPushConstants (command_buffer,
+ gsk_vulkan_pipeline_get_pipeline_layout (self->pipeline),
+ VK_SHADER_STAGE_VERTEX_BIT,
+ 0,
+ sizeof (graphene_matrix_t),
+ &mvp);
+
gsk_vulkan_renderer_do_render_commands (self, command_buffer);
vkCmdEndRenderPass (command_buffer);
diff --git a/gsk/resources/vulkan/blit.vert.glsl b/gsk/resources/vulkan/blit.vert.glsl
index 3865405..2522b7e 100644
--- a/gsk/resources/vulkan/blit.vert.glsl
+++ b/gsk/resources/vulkan/blit.vert.glsl
@@ -3,6 +3,10 @@
layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec2 inTexCoord;
+layout(push_constant) uniform PushConstants {
+ mat4 mvp;
+} push;
+
layout(location = 0) out vec2 outTexCoord;
out gl_PerVertex {
@@ -10,6 +14,6 @@ out gl_PerVertex {
};
void main() {
- gl_Position = vec4 (inPosition, 0.0, 1.0);
+ gl_Position = push.mvp * vec4 (inPosition, 0.0, 1.0);
outTexCoord = inTexCoord;
}
diff --git a/gsk/resources/vulkan/blit.vert.spv b/gsk/resources/vulkan/blit.vert.spv
index 6641397..3deee1c 100644
Binary files a/gsk/resources/vulkan/blit.vert.spv and b/gsk/resources/vulkan/blit.vert.spv differ
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]