comp_distortion.h 3.25 KB
Newer Older
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2019, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
 * @file
 * @brief  Distortion shader code header.
 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * @author Jakob Bornecrantz <jakob@collabora.com>
 * @ingroup comp
 */

#pragma once

#include "main/comp_settings.h"
#include "main/comp_compositor.h"

Pete Black's avatar
Pete Black committed
16

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#ifdef __cplusplus
extern "C" {
#endif


/*
 *
 * Structs
 *
 */


/*!
 * Helper buffer for a single uniform buffer.
 *
 * @ingroup comp
 */
struct comp_uniform_buffer
{
	VkDevice device;
	VkBuffer buffer;
	VkDeviceMemory memory;
	VkDescriptorBufferInfo descriptor;
	VkDeviceSize size;
	VkDeviceSize alignment;
	void *mapped;
	VkBufferUsageFlags usageFlags;
	VkMemoryPropertyFlags memoryPropertyFlags;
};

/*!
 * Helper struct that encapsulate a distortion rendering code.
 *
 * @ingroup comp
 */
struct comp_distortion
{
	// Holds all of the needed common Vulkan things.
	struct vk_bundle *vk;

	struct comp_uniform_buffer ubo_handle;
Pete Black's avatar
Pete Black committed
58
	struct comp_uniform_buffer vbo_handle;
59
	struct comp_uniform_buffer index_handle;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
60 61
	struct comp_uniform_buffer ubo_viewport_handles[2];

62 63
	enum xrt_distortion_model distortion_model;

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
64 65 66 67 68 69 70
	struct
	{
		float hmd_warp_param[4];
		float aberr[4];
		float lens_center[2][4];
		float viewport_scale[2];
		float warp_scale;
71
	} ubo_pano;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
72

73 74 75 76 77 78 79 80 81
	struct
	{
		float coefficients[2][3][4];
		float center[2][4];
		float undistort_r2_cutoff[4];
		float aspect_x_over_y;
		float grow_for_undistort;
	} ubo_vive;

Pete Black's avatar
Pete Black committed
82 83
	struct
	{
84 85
		float *vertices;
		int *indices;
Pete Black's avatar
Pete Black committed
86
		size_t stride;
87 88 89 90 91
		size_t num_vertices;
		size_t num_indices[2];
		size_t offset_indices[2];
		size_t total_num_indices;
	} mesh;
Pete Black's avatar
Pete Black committed
92

Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
93 94 95 96 97 98 99 100 101 102 103 104
	struct
	{
		struct xrt_matrix_2x2 rot;
		int viewport_id;
		bool flip_y;
	} ubo_vp_data[2];

	VkPipelineLayout pipeline_layout;
	VkPipeline pipeline;

	VkDescriptorSetLayout descriptor_set_layout;
	VkDescriptorSet descriptor_sets[2];
105 106

	bool quirk_draw_lines;
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
};


/*
 *
 * Functions.
 *
 */

/*!
 * Init a distortion, pass in the distortion so it can be embedded in a struct.
 *
 * @ingroup comp
 */
void
comp_distortion_init(struct comp_distortion *d,
                     struct comp_compositor *c,
                     VkRenderPass render_pass,
                     VkPipelineCache pipeline_cache,
                     enum xrt_distortion_model distortion_model,
Pete Black's avatar
Pete Black committed
127
                     struct xrt_hmd_parts *parts,
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                     VkDescriptorPool descriptor_pool,
                     bool flip_y);

/*!
 * Free and destroy all fields, does not free the destortion itself.
 *
 * @ingroup comp
 */
void
comp_distortion_destroy(struct comp_distortion *d);

/*!
 * Update the descriptor set to a new image.
 *
 * @ingroup comp
 */
void
comp_distortion_update_descriptor_set(struct comp_distortion *d,
                                      VkSampler sampler,
                                      VkImageView view,
                                      uint32_t eye);

/*!
 * Submit draw commands to the given command_buffer.
 *
 * @ingroup comp
 */
void
comp_distortion_draw_quad(struct comp_distortion *d,
                          VkCommandBuffer command_buffer,
                          int eye);

Pete Black's avatar
Pete Black committed
160 161 162 163
void
comp_distortion_draw_mesh(struct comp_distortion *d,
                          VkCommandBuffer command_buffer,
                          int eye);
Jakob Bornecrantz's avatar
Jakob Bornecrantz committed
164 165 166 167

#ifdef __cplusplus
}
#endif