...
 
Commits (4)
......@@ -6,6 +6,8 @@ spirv_shaders(SHADER_HEADERS
shaders/none.frag
shaders/panotools.frag
shaders/vive.frag
shaders/meshuv.vert
shaders/meshuv.frag
)
set(GL_SOURCE_FILES
......
......@@ -765,6 +765,7 @@ vk_get_device_functions(struct vk_bundle *vk)
vk->vkCmdEndRenderPass = GET_DEV_PROC(vk, vkCmdEndRenderPass);
vk->vkCmdBindDescriptorSets = GET_DEV_PROC(vk, vkCmdBindDescriptorSets);
vk->vkCmdBindPipeline = GET_DEV_PROC(vk, vkCmdBindPipeline);
vk->vkCmdBindVertexBuffers = GET_DEV_PROC(vk, vkCmdBindVertexBuffers);
vk->vkCmdDraw = GET_DEV_PROC(vk, vkCmdDraw);
vk->vkEndCommandBuffer = GET_DEV_PROC(vk, vkEndCommandBuffer);
vk->vkFreeCommandBuffers = GET_DEV_PROC(vk, vkFreeCommandBuffers);
......
......@@ -131,6 +131,7 @@ struct vk_bundle
PFN_vkCmdEndRenderPass vkCmdEndRenderPass;
PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets;
PFN_vkCmdBindPipeline vkCmdBindPipeline;
PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers;
PFN_vkCmdDraw vkCmdDraw;
PFN_vkEndCommandBuffer vkEndCommandBuffer;
PFN_vkFreeCommandBuffers vkFreeCommandBuffers;
......
......@@ -10,6 +10,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main/comp_settings.h"
#include "main/comp_compositor.h"
......@@ -25,6 +26,13 @@
#include "shaders/panotools.frag.h"
#include "shaders/vive.frag.h"
#include "util/u_misc.h"
#include "shaders/meshuv.vert.h"
#include "shaders/meshuv.frag.h"
#include "shaders/psvr_both_uvs.h"
#pragma GCC diagnostic pop
/*
......@@ -38,8 +46,8 @@ comp_distortion_update_uniform_buffer_warp(struct comp_distortion *d,
struct comp_compositor *c);
static void
comp_distortion_init_uniform_buffer(struct comp_distortion *d,
struct comp_compositor *c);
comp_distortion_init_buffers(struct comp_distortion *d,
struct comp_compositor *c);
XRT_MAYBE_UNUSED static void
comp_distortion_update_descriptor_sets(struct comp_distortion *d,
......@@ -179,7 +187,7 @@ comp_distortion_init(struct comp_distortion *d,
d->ubo_vp_data[0].flip_y = flip_y;
d->ubo_vp_data[1].flip_y = flip_y;
comp_distortion_init_uniform_buffer(d, c);
comp_distortion_init_buffers(d, c);
comp_distortion_update_uniform_buffer_warp(d, c);
comp_distortion_init_descriptor_set_layout(d);
comp_distortion_init_pipeline_layout(d);
......@@ -275,9 +283,31 @@ comp_distortion_init_pipeline(struct comp_distortion *d,
.pDynamicStates = dynamic_states,
};
const uint32_t *vertex_shader_code;
size_t vertex_shader_size;
const uint32_t *fragment_shader_code;
size_t fragment_shader_size;
/*
* By default, we will generate positions and UVs for the full screen
* quad from the gl_VertexIndex, and use the 'generic' distortion vertex
* shader
*/
VkPipelineVertexInputStateCreateInfo vertex_input_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = NULL,
};
VkVertexInputBindingDescription vertex_input_binding_description;
VkVertexInputAttributeDescription
vertex_input_attribute_descriptions[2];
vertex_shader_code = shaders_distortion_vert;
vertex_shader_size = sizeof(shaders_distortion_vert);
switch (d->distortion_model) {
case XRT_DISTORTION_MODEL_NONE:
fragment_shader_code = shaders_none_frag;
......@@ -291,34 +321,60 @@ comp_distortion_init_pipeline(struct comp_distortion *d,
fragment_shader_code = shaders_vive_frag;
fragment_shader_size = sizeof(shaders_vive_frag);
break;
case XRT_DISTORTION_MODEL_MESHUV:
vertex_input_attribute_descriptions[0].binding = 0;
vertex_input_attribute_descriptions[0].location = 0;
vertex_input_attribute_descriptions[0].format =
VK_FORMAT_R32G32_SFLOAT;
vertex_input_attribute_descriptions[0].offset = 0;
vertex_input_attribute_descriptions[1].binding = 0;
vertex_input_attribute_descriptions[1].location = 1;
vertex_input_attribute_descriptions[1].format =
VK_FORMAT_R32G32_SFLOAT;
vertex_input_attribute_descriptions[1].offset =
sizeof(VK_FORMAT_R32G32_SFLOAT);
vertex_input_binding_description.binding = 0;
vertex_input_binding_description.inputRate =
VK_VERTEX_INPUT_RATE_VERTEX;
vertex_input_binding_description.stride =
sizeof(d->vbo_meshuv_data);
vertex_input_state.vertexAttributeDescriptionCount = 2;
vertex_input_state.pVertexAttributeDescriptions =
vertex_input_attribute_descriptions;
vertex_input_state.vertexBindingDescriptionCount = 1;
vertex_input_state.pVertexBindingDescriptions =
&vertex_input_binding_description;
vertex_shader_code = shaders_meshuv_vert;
vertex_shader_size = sizeof(shaders_meshuv_vert);
fragment_shader_code = shaders_meshuv_frag;
fragment_shader_size = sizeof(shaders_meshuv_frag);
break;
default:
fragment_shader_code = shaders_panotools_frag;
fragment_shader_size = sizeof(shaders_panotools_frag);
break;
}
VkPipelineShaderStageCreateInfo shader_stages[2] = {
_shader_load(d->vk, shaders_distortion_vert,
sizeof(shaders_distortion_vert),
_shader_load(d->vk, vertex_shader_code, vertex_shader_size,
VK_SHADER_STAGE_VERTEX_BIT),
_shader_load(d->vk, fragment_shader_code, fragment_shader_size,
VK_SHADER_STAGE_FRAGMENT_BIT),
};
/*
* We will generate positions and UVs for the full screen quad
* from the gl_VertexIndex
*/
VkPipelineVertexInputStateCreateInfo empty_input_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
};
VkGraphicsPipelineCreateInfo pipeline_info = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.flags = 0,
.stageCount = ARRAY_SIZE(shader_stages),
.pStages = shader_stages,
.pVertexInputState = &empty_input_state,
.pVertexInputState = &vertex_input_state,
.pInputAssemblyState = &input_assembly_state,
.pViewportState = &viewport_state,
.pRasterizationState = &rasterization_state,
......@@ -434,6 +490,7 @@ comp_distortion_update_descriptor_set(struct comp_distortion *d,
comp_distortion_get_uniform_write_descriptor_set(d, 1, eye),
// Binding 2 : view uniform buffer
comp_distortion_get_uniform_write_descriptor_set_vp(d, 2, eye),
};
vk->vkUpdateDescriptorSets(vk->device,
......@@ -533,6 +590,27 @@ comp_distortion_draw_quad(struct comp_distortion *d,
vk->vkCmdDraw(command_buffer, 3, 1, 0, 0);
}
void
comp_distortion_draw_mesh(struct comp_distortion *d,
VkCommandBuffer command_buffer,
int eye)
{
struct vk_bundle *vk = d->vk;
vk->vkCmdBindDescriptorSets(
command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, d->pipeline_layout,
0, 1, &d->descriptor_sets[eye], 0, NULL);
vk->vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
d->pipeline);
VkDeviceSize offsets[] = {0};
vk->vkCmdBindVertexBuffers(command_buffer, 0, 1,
&(d->vbo_handle.buffer), offsets);
vk->vkCmdDraw(command_buffer, DISTORTION_MESH_VERTEX_COUNT, 1, 0, 0);
}
// Update fragment shader hmd warp uniform block
static void
comp_distortion_update_uniform_buffer_warp(struct comp_distortion *d,
......@@ -562,6 +640,7 @@ comp_distortion_update_uniform_buffer_warp(struct comp_distortion *d,
memcpy(d->ubo_handle.mapped, &d->ubo_vive, sizeof(d->ubo_vive));
break;
case XRT_DISTORTION_MODEL_PANOTOOLS:
case XRT_DISTORTION_MODEL_MESHUV:
default:
/*
* Pano vision fragment shader
......@@ -690,31 +769,54 @@ err_buffer:
}
static void
comp_distortion_init_uniform_buffer(struct comp_distortion *d,
struct comp_compositor *c)
comp_distortion_init_buffers(struct comp_distortion *d,
struct comp_compositor *c)
{
struct vk_bundle *vk = &c->vk;
VkMemoryPropertyFlags memory_property_flags = 0;
VkBufferUsageFlags usage_flags = 0;
VkBufferUsageFlags ubo_usage_flags = 0;
VkBufferUsageFlags vbo_usage_flags = 0;
VkResult ret;
// Using the same flags for all uniform buffers.
usage_flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
ubo_usage_flags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
memory_property_flags |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
memory_property_flags |= VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
// distortion ubo
VkDeviceSize ubo_size;
vbo_usage_flags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
// distortion ubo/vbo
VkDeviceSize ubo_size = 0;
VkDeviceSize vbo_size = 0;
switch (d->distortion_model) {
case XRT_DISTORTION_MODEL_PANOTOOLS:
ubo_size = sizeof(d->ubo_pano);
break;
case XRT_DISTORTION_MODEL_MESHUV:
ubo_size = sizeof(d->ubo_pano);
vbo_size =
sizeof(d->vbo_meshuv_data) * DISTORTION_MESH_VERTEX_COUNT;
ret = _create_buffer(vk, vbo_usage_flags, memory_property_flags,
&d->vbo_handle, vbo_size, &psvr_both_uvs);
if (ret != VK_SUCCESS) {
VK_DEBUG(vk, "Failed to create meshuv vbo buffer!");
break;
}
ret = _buffer_map(vk, &d->vbo_handle, vbo_size, 0);
if (ret != VK_SUCCESS) {
VK_DEBUG(vk, "Failed to map meshuv vbo buffer!");
break;
}
break;
case XRT_DISTORTION_MODEL_VIVE: ubo_size = sizeof(d->ubo_vive); break;
default: ubo_size = sizeof(d->ubo_pano);
}
ret = _create_buffer(vk, usage_flags, memory_property_flags,
ret = _create_buffer(vk, ubo_usage_flags, memory_property_flags,
&d->ubo_handle, ubo_size, NULL);
if (ret != VK_SUCCESS) {
VK_DEBUG(vk, "Failed to create warp ubo buffer!");
......@@ -725,7 +827,7 @@ comp_distortion_init_uniform_buffer(struct comp_distortion *d,
}
// vp ubos
ret = _create_buffer(vk, usage_flags, memory_property_flags,
ret = _create_buffer(vk, ubo_usage_flags, memory_property_flags,
&d->ubo_viewport_handles[0],
sizeof(d->ubo_vp_data[0]), NULL);
if (ret != VK_SUCCESS) {
......@@ -736,7 +838,7 @@ comp_distortion_init_uniform_buffer(struct comp_distortion *d,
VK_DEBUG(vk, "Failed to map vp ubo buffer[0]!");
}
ret = _create_buffer(vk, usage_flags, memory_property_flags,
ret = _create_buffer(vk, ubo_usage_flags, memory_property_flags,
&d->ubo_viewport_handles[1],
sizeof(d->ubo_vp_data[1]), NULL);
if (ret != VK_SUCCESS) {
......
......@@ -17,6 +17,10 @@
extern "C" {
#endif
// our distortion meshes are 16x16 quads, 2 tris per quad.
// we should use indexed rendering and allow arbitrary vertex counts,
// but this will do for now
#define DISTORTION_MESH_VERTEX_COUNT 16 * 16 * 6
/*
*
......@@ -56,6 +60,8 @@ struct comp_distortion
struct comp_uniform_buffer ubo_handle;
struct comp_uniform_buffer ubo_viewport_handles[2];
struct comp_uniform_buffer vbo_handle;
enum xrt_distortion_model distortion_model;
struct
......@@ -76,6 +82,12 @@ struct comp_distortion
float grow_for_undistort;
} ubo_vive;
// vec2 for pos, vec2 for uv
struct
{
float vertexPosUV[4];
} vbo_meshuv_data;
struct
{
struct xrt_matrix_2x2 rot;
......@@ -140,6 +152,11 @@ comp_distortion_draw_quad(struct comp_distortion *d,
VkCommandBuffer command_buffer,
int eye);
void
comp_distortion_draw_mesh(struct comp_distortion *d,
VkCommandBuffer command_buffer,
int eye);
#ifdef __cplusplus
}
......
......@@ -346,16 +346,22 @@ renderer_build_command_buffer(struct comp_renderer *r,
vk->vkCmdSetViewport(command_buffer, 0, 1, &viewport);
vk->vkCmdSetScissor(command_buffer, 0, 1, &scissor);
comp_distortion_draw_quad(r->distortion, command_buffer, 0);
if (r->distortion->distortion_model == XRT_DISTORTION_MODEL_MESHUV) {
comp_distortion_draw_mesh(r->distortion, command_buffer, 0);
} else {
comp_distortion_draw_quad(r->distortion, command_buffer, 0);
}
renderer_set_viewport_scissor(scale_x, scale_y, &viewport, &scissor,
&r->c->xdev->views[1]);
vk->vkCmdSetViewport(command_buffer, 0, 1, &viewport);
vk->vkCmdSetScissor(command_buffer, 0, 1, &scissor);
comp_distortion_draw_quad(r->distortion, command_buffer, 1);
if (r->distortion->distortion_model == XRT_DISTORTION_MODEL_MESHUV) {
comp_distortion_draw_mesh(r->distortion, command_buffer, 1);
} else {
comp_distortion_draw_quad(r->distortion, command_buffer, 1);
}
vk->vkCmdEndRenderPass(command_buffer);
ret = vk->vkEndCommandBuffer(command_buffer);
......
// Copyright 2017, James Sarrett.
// Copyright 2017, Bastiaan Olij.
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
// Author: James Sarrett <jsarrett@gmail.com>
// Author: Bastiaan Olij <mux213@gmail.com>
// Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
// Author: Pete Black <pete.black@collabora.com>
#version 450
layout (binding = 0) uniform sampler2D texSampler;
layout (binding = 1, std140) uniform UBO
{
// Distoriton coefficients (PanoTools model) [a,b,c,d]
vec4 HmdWarpParam;
// chromatic distortion post scaling
vec4 aberr;
// Position of lens center in m (usually eye_w/2, eye_h/2)
vec2 LensCenter[2];
// Scale from texture co-ords to m (usually eye_w, eye_h)
vec2 ViewportScale;
// Distortion overall scale in m (usually ~eye_w/2)
float WarpScale;
} ubo;
layout (location = 0) in vec2 inUV;
layout (location = 1) flat in int inViewIndex;
layout (location = 0) out vec4 outColor;
void main()
{
const int i = inViewIndex;
vec3 color = texture(texSampler, inUV).xyz;
outColor = vec4(color, 1.0);
}
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
// Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
// Author: Pete Black <pete.black@collabora.com>
#version 450
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 uv;
layout (location = 0) out vec2 outUV;
layout (location = 1) out int outViewIndex;
layout (binding = 2, std140) uniform UBO
{
vec4 rot;
int viewport_id;
bool flip_y;
} ubo_vp;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
mat2x2 rot = {
ubo_vp.rot.xy,
ubo_vp.rot.zw,
};
outViewIndex = ubo_vp.viewport_id;
outUV = uv;
gl_Position = vec4(pos, 0.0f, 1.0f);
if (ubo_vp.flip_y)
outUV.y = 1.0 - outUV.y;
}
This diff is collapsed.
......@@ -40,6 +40,7 @@ enum xrt_distortion_model
XRT_DISTORTION_MODEL_NONE = 1 << 0,
XRT_DISTORTION_MODEL_PANOTOOLS = 1 << 1,
XRT_DISTORTION_MODEL_VIVE = 1 << 2,
XRT_DISTORTION_MODEL_MESHUV = 1 << 4,
// clang-format on
};
......