From e17632c2f6e5ffe3c807bb138153a05a35f7a6fe Mon Sep 17 00:00:00 2001 From: Agustin Alba Chicar Date: Tue, 6 Dec 2022 09:12:29 -0300 Subject: [PATCH] Cardboard SDK v1.19.0 --- hellocardboard-android/build.gradle | 2 +- hellocardboard-ios/HelloCardboard-Info.plist | 4 +- sdk/build.gradle | 2 +- sdk/rendering/android/shaders/distortion.vert | 20 +- .../android/shaders/distortion_vert.spv.h | 138 ++++++------ .../android/vulkan_distortion_renderer.cc | 75 ++----- .../xr_unity_plugin/vulkan/vulkan_renderer.cc | 9 +- .../vulkan/vulkan_widgets_renderer.cc | 201 ++++++++++-------- .../vulkan/vulkan_widgets_renderer.h | 59 +++-- 9 files changed, 274 insertions(+), 236 deletions(-) diff --git a/hellocardboard-android/build.gradle b/hellocardboard-android/build.gradle index 5c56220b..8bd4d7a2 100644 --- a/hellocardboard-android/build.gradle +++ b/hellocardboard-android/build.gradle @@ -18,7 +18,7 @@ android { minSdkVersion 24 targetSdkVersion 31 versionCode 1 - versionName "1.18.0" + versionName "1.19.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' ndk { abiFilters 'armeabi-v7a', 'arm64-v8a' diff --git a/hellocardboard-ios/HelloCardboard-Info.plist b/hellocardboard-ios/HelloCardboard-Info.plist index 469070a5..3516fe2e 100644 --- a/hellocardboard-ios/HelloCardboard-Info.plist +++ b/hellocardboard-ios/HelloCardboard-Info.plist @@ -15,9 +15,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.18.0 + 1.19.0 CFBundleVersion - 1.18.0 + 1.19.0 LSRequiresIPhoneOS NSCameraUsageDescription diff --git a/sdk/build.gradle b/sdk/build.gradle index 60d78300..9492fdb9 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -18,7 +18,7 @@ android { minSdkVersion 24 targetSdkVersion 31 versionCode 1 - versionName "1.18.0" + versionName "1.19.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' ndk { abiFilters 'armeabi-v7a', 'arm64-v8a' diff --git a/sdk/rendering/android/shaders/distortion.vert b/sdk/rendering/android/shaders/distortion.vert index 693a54d6..bb3a6c5d 100644 --- a/sdk/rendering/android/shaders/distortion.vert +++ b/sdk/rendering/android/shaders/distortion.vert @@ -18,23 +18,23 @@ #extension GL_ARB_shading_language_420pack : enable precision mediump float; -layout (binding = 1) uniform UniformBufferObject -{ - float left_u; - float right_u; - float top_v; - float bottom_v; -} ubo; - layout (location = 0) in vec2 a_Position; layout (location = 1) in vec2 a_TexCoords; layout (location = 0) out vec2 v_TexCoords; layout (location = 1) out vec2 u_Start; layout (location = 2) out vec2 u_End; +layout( push_constant ) uniform constants +{ + float left_u; + float right_u; + float top_v; + float bottom_v; +} push_constants; + void main() { gl_Position = vec4(a_Position, 0, 1); v_TexCoords = a_TexCoords; - u_Start = vec2(ubo.left_u, ubo.bottom_v); - u_End = vec2(ubo.right_u, ubo.top_v); + u_Start = vec2(push_constants.left_u, push_constants.bottom_v); + u_End = vec2(push_constants.right_u, push_constants.top_v); } diff --git a/sdk/rendering/android/shaders/distortion_vert.spv.h b/sdk/rendering/android/shaders/distortion_vert.spv.h index a000068d..972828ed 100644 --- a/sdk/rendering/android/shaders/distortion_vert.spv.h +++ b/sdk/rendering/android/shaders/distortion_vert.spv.h @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - // 1011.5.0 - #pragma once +// 1011.5.0 +#pragma once const uint32_t distortion_vert[] = { - 0x07230203, 0x00010000, 0x0008000a, 0x00000032, 0x00000000, 0x00020011, + 0x07230203, 0x00010000, 0x0008000a, 0x00000035, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x000b000f, 0x00000000, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x00000012, 0x0000001c, @@ -32,67 +32,81 @@ const uint32_t distortion_vert[] = { 0x00030005, 0x0000000d, 0x00000000, 0x00050005, 0x00000012, 0x6f505f61, 0x69746973, 0x00006e6f, 0x00050005, 0x0000001c, 0x65545f76, 0x6f6f4378, 0x00736472, 0x00050005, 0x0000001d, 0x65545f61, 0x6f6f4378, 0x00736472, - 0x00040005, 0x0000001f, 0x74535f75, 0x00747261, 0x00070005, 0x00000020, + 0x00040005, 0x0000001f, 0x74535f75, 0x00747261, 0x00050005, 0x00000020, + 0x736e6f63, 0x746e6174, 0x00000073, 0x00050006, 0x00000020, 0x00000000, + 0x7466656c, 0x0000755f, 0x00050006, 0x00000020, 0x00000001, 0x68676972, + 0x00755f74, 0x00050006, 0x00000020, 0x00000002, 0x5f706f74, 0x00000076, + 0x00060006, 0x00000020, 0x00000003, 0x74746f62, 0x765f6d6f, 0x00000000, + 0x00060005, 0x00000022, 0x68737550, 0x736e6f43, 0x746e6174, 0x00000073, + 0x00040005, 0x0000002a, 0x6e455f75, 0x00000064, 0x00070005, 0x00000032, 0x66696e55, 0x426d726f, 0x65666675, 0x6a624f72, 0x00746365, 0x00050006, - 0x00000020, 0x00000000, 0x7466656c, 0x0000755f, 0x00050006, 0x00000020, - 0x00000001, 0x68676972, 0x00755f74, 0x00050006, 0x00000020, 0x00000002, - 0x5f706f74, 0x00000076, 0x00060006, 0x00000020, 0x00000003, 0x74746f62, - 0x765f6d6f, 0x00000000, 0x00030005, 0x00000022, 0x006f6275, 0x00040005, - 0x0000002a, 0x6e455f75, 0x00000064, 0x00050048, 0x0000000b, 0x00000000, - 0x0000000b, 0x00000000, 0x00050048, 0x0000000b, 0x00000001, 0x0000000b, - 0x00000001, 0x00050048, 0x0000000b, 0x00000002, 0x0000000b, 0x00000003, - 0x00030047, 0x0000000b, 0x00000002, 0x00030047, 0x00000012, 0x00000000, - 0x00040047, 0x00000012, 0x0000001e, 0x00000000, 0x00030047, 0x00000013, - 0x00000000, 0x00030047, 0x0000001c, 0x00000000, 0x00040047, 0x0000001c, - 0x0000001e, 0x00000000, 0x00030047, 0x0000001d, 0x00000000, 0x00040047, - 0x0000001d, 0x0000001e, 0x00000001, 0x00030047, 0x0000001e, 0x00000000, - 0x00030047, 0x0000001f, 0x00000000, 0x00040047, 0x0000001f, 0x0000001e, - 0x00000001, 0x00040048, 0x00000020, 0x00000000, 0x00000000, 0x00050048, - 0x00000020, 0x00000000, 0x00000023, 0x00000000, 0x00040048, 0x00000020, - 0x00000001, 0x00000000, 0x00050048, 0x00000020, 0x00000001, 0x00000023, - 0x00000004, 0x00040048, 0x00000020, 0x00000002, 0x00000000, 0x00050048, - 0x00000020, 0x00000002, 0x00000023, 0x00000008, 0x00040048, 0x00000020, - 0x00000003, 0x00000000, 0x00050048, 0x00000020, 0x00000003, 0x00000023, - 0x0000000c, 0x00030047, 0x00000020, 0x00000002, 0x00040047, 0x00000022, - 0x00000022, 0x00000000, 0x00040047, 0x00000022, 0x00000021, 0x00000001, + 0x00000032, 0x00000000, 0x7466656c, 0x0000755f, 0x00050006, 0x00000032, + 0x00000001, 0x68676972, 0x00755f74, 0x00050006, 0x00000032, 0x00000002, + 0x5f706f74, 0x00000076, 0x00060006, 0x00000032, 0x00000003, 0x74746f62, + 0x765f6d6f, 0x00000000, 0x00030005, 0x00000034, 0x006f6275, 0x00050048, + 0x0000000b, 0x00000000, 0x0000000b, 0x00000000, 0x00050048, 0x0000000b, + 0x00000001, 0x0000000b, 0x00000001, 0x00050048, 0x0000000b, 0x00000002, + 0x0000000b, 0x00000003, 0x00030047, 0x0000000b, 0x00000002, 0x00030047, + 0x00000012, 0x00000000, 0x00040047, 0x00000012, 0x0000001e, 0x00000000, + 0x00030047, 0x00000013, 0x00000000, 0x00030047, 0x0000001c, 0x00000000, + 0x00040047, 0x0000001c, 0x0000001e, 0x00000000, 0x00030047, 0x0000001d, + 0x00000000, 0x00040047, 0x0000001d, 0x0000001e, 0x00000001, 0x00030047, + 0x0000001e, 0x00000000, 0x00030047, 0x0000001f, 0x00000000, 0x00040047, + 0x0000001f, 0x0000001e, 0x00000001, 0x00040048, 0x00000020, 0x00000000, + 0x00000000, 0x00050048, 0x00000020, 0x00000000, 0x00000023, 0x00000000, + 0x00040048, 0x00000020, 0x00000001, 0x00000000, 0x00050048, 0x00000020, + 0x00000001, 0x00000023, 0x00000004, 0x00040048, 0x00000020, 0x00000002, + 0x00000000, 0x00050048, 0x00000020, 0x00000002, 0x00000023, 0x00000008, + 0x00040048, 0x00000020, 0x00000003, 0x00000000, 0x00050048, 0x00000020, + 0x00000003, 0x00000023, 0x0000000c, 0x00030047, 0x00000020, 0x00000002, 0x00030047, 0x00000025, 0x00000000, 0x00030047, 0x00000028, 0x00000000, 0x00030047, 0x00000029, 0x00000000, 0x00030047, 0x0000002a, 0x00000000, 0x00040047, 0x0000002a, 0x0000001e, 0x00000002, 0x00030047, 0x0000002d, 0x00000000, 0x00030047, 0x00000030, 0x00000000, 0x00030047, 0x00000031, - 0x00000000, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, - 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, - 0x00000004, 0x00040015, 0x00000008, 0x00000020, 0x00000000, 0x0004002b, - 0x00000008, 0x00000009, 0x00000001, 0x0004001c, 0x0000000a, 0x00000006, - 0x00000009, 0x0005001e, 0x0000000b, 0x00000007, 0x00000006, 0x0000000a, - 0x00040020, 0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c, - 0x0000000d, 0x00000003, 0x00040015, 0x0000000e, 0x00000020, 0x00000001, - 0x0004002b, 0x0000000e, 0x0000000f, 0x00000000, 0x00040017, 0x00000010, - 0x00000006, 0x00000002, 0x00040020, 0x00000011, 0x00000001, 0x00000010, - 0x0004003b, 0x00000011, 0x00000012, 0x00000001, 0x0004002b, 0x00000006, - 0x00000014, 0x00000000, 0x0004002b, 0x00000006, 0x00000015, 0x3f800000, - 0x00040020, 0x00000019, 0x00000003, 0x00000007, 0x00040020, 0x0000001b, - 0x00000003, 0x00000010, 0x0004003b, 0x0000001b, 0x0000001c, 0x00000003, - 0x0004003b, 0x00000011, 0x0000001d, 0x00000001, 0x0004003b, 0x0000001b, - 0x0000001f, 0x00000003, 0x0006001e, 0x00000020, 0x00000006, 0x00000006, - 0x00000006, 0x00000006, 0x00040020, 0x00000021, 0x00000002, 0x00000020, - 0x0004003b, 0x00000021, 0x00000022, 0x00000002, 0x00040020, 0x00000023, - 0x00000002, 0x00000006, 0x0004002b, 0x0000000e, 0x00000026, 0x00000003, - 0x0004003b, 0x0000001b, 0x0000002a, 0x00000003, 0x0004002b, 0x0000000e, - 0x0000002b, 0x00000001, 0x0004002b, 0x0000000e, 0x0000002e, 0x00000002, - 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, - 0x00000005, 0x0004003d, 0x00000010, 0x00000013, 0x00000012, 0x00050051, - 0x00000006, 0x00000016, 0x00000013, 0x00000000, 0x00050051, 0x00000006, - 0x00000017, 0x00000013, 0x00000001, 0x00070050, 0x00000007, 0x00000018, - 0x00000016, 0x00000017, 0x00000014, 0x00000015, 0x00050041, 0x00000019, - 0x0000001a, 0x0000000d, 0x0000000f, 0x0003003e, 0x0000001a, 0x00000018, - 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, 0x0003003e, 0x0000001c, - 0x0000001e, 0x00050041, 0x00000023, 0x00000024, 0x00000022, 0x0000000f, - 0x0004003d, 0x00000006, 0x00000025, 0x00000024, 0x00050041, 0x00000023, - 0x00000027, 0x00000022, 0x00000026, 0x0004003d, 0x00000006, 0x00000028, - 0x00000027, 0x00050050, 0x00000010, 0x00000029, 0x00000025, 0x00000028, - 0x0003003e, 0x0000001f, 0x00000029, 0x00050041, 0x00000023, 0x0000002c, - 0x00000022, 0x0000002b, 0x0004003d, 0x00000006, 0x0000002d, 0x0000002c, - 0x00050041, 0x00000023, 0x0000002f, 0x00000022, 0x0000002e, 0x0004003d, - 0x00000006, 0x00000030, 0x0000002f, 0x00050050, 0x00000010, 0x00000031, - 0x0000002d, 0x00000030, 0x0003003e, 0x0000002a, 0x00000031, 0x000100fd, - 0x00010038}; + 0x00000000, 0x00040048, 0x00000032, 0x00000000, 0x00000000, 0x00050048, + 0x00000032, 0x00000000, 0x00000023, 0x00000000, 0x00040048, 0x00000032, + 0x00000001, 0x00000000, 0x00050048, 0x00000032, 0x00000001, 0x00000023, + 0x00000004, 0x00040048, 0x00000032, 0x00000002, 0x00000000, 0x00050048, + 0x00000032, 0x00000002, 0x00000023, 0x00000008, 0x00040048, 0x00000032, + 0x00000003, 0x00000000, 0x00050048, 0x00000032, 0x00000003, 0x00000023, + 0x0000000c, 0x00030047, 0x00000032, 0x00000002, 0x00040047, 0x00000034, + 0x00000022, 0x00000000, 0x00040047, 0x00000034, 0x00000021, 0x00000001, + 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, + 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004, + 0x00040015, 0x00000008, 0x00000020, 0x00000000, 0x0004002b, 0x00000008, + 0x00000009, 0x00000001, 0x0004001c, 0x0000000a, 0x00000006, 0x00000009, + 0x0005001e, 0x0000000b, 0x00000007, 0x00000006, 0x0000000a, 0x00040020, + 0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c, 0x0000000d, + 0x00000003, 0x00040015, 0x0000000e, 0x00000020, 0x00000001, 0x0004002b, + 0x0000000e, 0x0000000f, 0x00000000, 0x00040017, 0x00000010, 0x00000006, + 0x00000002, 0x00040020, 0x00000011, 0x00000001, 0x00000010, 0x0004003b, + 0x00000011, 0x00000012, 0x00000001, 0x0004002b, 0x00000006, 0x00000014, + 0x00000000, 0x0004002b, 0x00000006, 0x00000015, 0x3f800000, 0x00040020, + 0x00000019, 0x00000003, 0x00000007, 0x00040020, 0x0000001b, 0x00000003, + 0x00000010, 0x0004003b, 0x0000001b, 0x0000001c, 0x00000003, 0x0004003b, + 0x00000011, 0x0000001d, 0x00000001, 0x0004003b, 0x0000001b, 0x0000001f, + 0x00000003, 0x0006001e, 0x00000020, 0x00000006, 0x00000006, 0x00000006, + 0x00000006, 0x00040020, 0x00000021, 0x00000009, 0x00000020, 0x0004003b, + 0x00000021, 0x00000022, 0x00000009, 0x00040020, 0x00000023, 0x00000009, + 0x00000006, 0x0004002b, 0x0000000e, 0x00000026, 0x00000003, 0x0004003b, + 0x0000001b, 0x0000002a, 0x00000003, 0x0004002b, 0x0000000e, 0x0000002b, + 0x00000001, 0x0004002b, 0x0000000e, 0x0000002e, 0x00000002, 0x0006001e, + 0x00000032, 0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00040020, + 0x00000033, 0x00000002, 0x00000032, 0x0004003b, 0x00000033, 0x00000034, + 0x00000002, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, + 0x000200f8, 0x00000005, 0x0004003d, 0x00000010, 0x00000013, 0x00000012, + 0x00050051, 0x00000006, 0x00000016, 0x00000013, 0x00000000, 0x00050051, + 0x00000006, 0x00000017, 0x00000013, 0x00000001, 0x00070050, 0x00000007, + 0x00000018, 0x00000016, 0x00000017, 0x00000014, 0x00000015, 0x00050041, + 0x00000019, 0x0000001a, 0x0000000d, 0x0000000f, 0x0003003e, 0x0000001a, + 0x00000018, 0x0004003d, 0x00000010, 0x0000001e, 0x0000001d, 0x0003003e, + 0x0000001c, 0x0000001e, 0x00050041, 0x00000023, 0x00000024, 0x00000022, + 0x0000000f, 0x0004003d, 0x00000006, 0x00000025, 0x00000024, 0x00050041, + 0x00000023, 0x00000027, 0x00000022, 0x00000026, 0x0004003d, 0x00000006, + 0x00000028, 0x00000027, 0x00050050, 0x00000010, 0x00000029, 0x00000025, + 0x00000028, 0x0003003e, 0x0000001f, 0x00000029, 0x00050041, 0x00000023, + 0x0000002c, 0x00000022, 0x0000002b, 0x0004003d, 0x00000006, 0x0000002d, + 0x0000002c, 0x00050041, 0x00000023, 0x0000002f, 0x00000022, 0x0000002e, + 0x0004003d, 0x00000006, 0x00000030, 0x0000002f, 0x00050050, 0x00000010, + 0x00000031, 0x0000002d, 0x00000030, 0x0003003e, 0x0000002a, 0x00000031, + 0x000100fd, 0x00010038}; \ No newline at end of file diff --git a/sdk/rendering/android/vulkan_distortion_renderer.cc b/sdk/rendering/android/vulkan_distortion_renderer.cc index b76e4af8..e9d51435 100644 --- a/sdk/rendering/android/vulkan_distortion_renderer.cc +++ b/sdk/rendering/android/vulkan_distortion_renderer.cc @@ -40,7 +40,7 @@ namespace cardboard::rendering { -struct UniformBufferObject { +struct PushConstantsObject { float left_u; float right_u; float top_v; @@ -102,11 +102,6 @@ class VulkanDistortionRenderer : public DistortionRenderer { vkFreeMemory(logical_device_, vertex_buffers_memory_[kLeft], nullptr); vkDestroyBuffer(logical_device_, vertex_buffers_[kRight], nullptr); vkFreeMemory(logical_device_, vertex_buffers_memory_[kRight], nullptr); - - vkDestroyBuffer(logical_device_, uniform_buffers_[kLeft], nullptr); - vkFreeMemory(logical_device_, uniform_buffers_memory_[kLeft], nullptr); - vkDestroyBuffer(logical_device_, uniform_buffers_[kRight], nullptr); - vkFreeMemory(logical_device_, uniform_buffers_memory_[kRight], nullptr); } void SetMesh(const CardboardMesh* mesh, CardboardEye eye) override { @@ -216,7 +211,7 @@ class VulkanDistortionRenderer : public DistortionRenderer { */ void CreateSharedVulkanObjects() { // Create DescriptorSet Layout - VkDescriptorSetLayoutBinding bindings[2]; + VkDescriptorSetLayoutBinding bindings[1]; VkDescriptorSetLayoutBinding sampler_layout_binding{ .binding = 0, @@ -227,31 +222,29 @@ class VulkanDistortionRenderer : public DistortionRenderer { }; bindings[0] = sampler_layout_binding; - VkDescriptorSetLayoutBinding ubo_layout_binding{ - .binding = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .descriptorCount = 1, - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, - .pImmutableSamplers = nullptr, - }; - bindings[1] = ubo_layout_binding; - VkDescriptorSetLayoutCreateInfo layout_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 2, + .bindingCount = 1, .pBindings = bindings, }; CALL_VK(vkCreateDescriptorSetLayout(logical_device_, &layout_info, nullptr, &descriptor_set_layout_)); + // Setup push constants. + VkPushConstantRange push_constant_range = { + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, + .offset = 0, + .size = sizeof(PushConstantsObject), + }; + // Create Pipeline Layout VkPipelineLayoutCreateInfo pipeline_layout_create_info{ .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .pNext = nullptr, .setLayoutCount = 1, .pSetLayouts = &descriptor_set_layout_, - .pushConstantRangeCount = 0, - .pPushConstantRanges = nullptr, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &push_constant_range, }; CALL_VK(vkCreatePipelineLayout(logical_device_, &pipeline_layout_create_info, nullptr, @@ -290,31 +283,20 @@ class VulkanDistortionRenderer : public DistortionRenderer { */ void CreatePerEyeVulkanObjects(CardboardEye eye) { // Create Descriptor Pool - VkDescriptorPoolSize pool_sizes[2]; + VkDescriptorPoolSize pool_sizes[1]; pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; pool_sizes[0].descriptorCount = static_cast(swapchain_image_count_); - pool_sizes[1].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - pool_sizes[1].descriptorCount = - static_cast(swapchain_image_count_); VkDescriptorPoolCreateInfo pool_info{}; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.poolSizeCount = 2; + pool_info.poolSizeCount = 1; pool_info.pPoolSizes = pool_sizes; pool_info.maxSets = static_cast(swapchain_image_count_); CALL_VK(vkCreateDescriptorPool(logical_device_, &pool_info, nullptr, &descriptor_pool_[eye])); - // Create Uniform Buffers - VkDeviceSize buffer_size = sizeof(UniformBufferObject); - - CreateBuffer(buffer_size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - uniform_buffers_[eye], uniform_buffers_memory_[eye]); - // Create Descriptor Sets std::vector layouts(swapchain_image_count_, descriptor_set_layout_); @@ -567,18 +549,14 @@ class VulkanDistortionRenderer : public DistortionRenderer { const CardboardEyeTextureDescription* eye_description, CardboardEye eye, VkCommandBuffer command_buffer, uint32_t image_index, int x, int y, int width, int height) { - // Update Uniform Buffer - UniformBufferObject ubo{ + // Update Push constants. + PushConstantsObject push_constants { .left_u = eye_description->left_u, .right_u = eye_description->right_u, .top_v = eye_description->top_v, .bottom_v = eye_description->bottom_v, }; - void* data; - vkMapMemory(logical_device_, uniform_buffers_memory_[eye], 0, sizeof(ubo), - 0, &data); - memcpy(data, &ubo, sizeof(ubo)); - vkUnmapMemory(logical_device_, uniform_buffers_memory_[eye]); + vkCmdPushConstants(command_buffer, pipeline_layout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(PushConstantsObject), &push_constants); // Update image and view VkImage current_image = reinterpret_cast(eye_description->texture); @@ -611,17 +589,13 @@ class VulkanDistortionRenderer : public DistortionRenderer { &image_views_[eye][image_index])); // Update Descriptor Sets - VkDescriptorBufferInfo buffer_info{.buffer = uniform_buffers_[eye], - .offset = 0, - .range = sizeof(UniformBufferObject)}; - VkDescriptorImageInfo image_info{ .sampler = texture_sampler_, .imageView = image_views_[eye][image_index], .imageLayout = VK_IMAGE_LAYOUT_GENERAL, }; - VkWriteDescriptorSet descriptor_writes[2]; + VkWriteDescriptorSet descriptor_writes[1]; descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptor_writes[0].dstSet = descriptor_sets_[eye][image_index]; @@ -633,16 +607,7 @@ class VulkanDistortionRenderer : public DistortionRenderer { descriptor_writes[0].pImageInfo = &image_info; descriptor_writes[0].pNext = nullptr; - descriptor_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptor_writes[1].dstSet = descriptor_sets_[eye][image_index]; - descriptor_writes[1].dstBinding = 1; - descriptor_writes[1].dstArrayElement = 0; - descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_writes[1].descriptorCount = 1; - descriptor_writes[1].pBufferInfo = &buffer_info; - descriptor_writes[1].pNext = nullptr; - - vkUpdateDescriptorSets(logical_device_, 2, descriptor_writes, 0, nullptr); + vkUpdateDescriptorSets(logical_device_, 1, descriptor_writes, 0, nullptr); // Update Viewport and scissor VkViewport viewport = {.x = static_cast(x), @@ -725,8 +690,6 @@ class VulkanDistortionRenderer : public DistortionRenderer { VkDeviceMemory vertex_buffers_memory_[2]; VkBuffer index_buffers_[2]; VkDeviceMemory index_buffers_memory_[2]; - VkBuffer uniform_buffers_[2]; - VkDeviceMemory uniform_buffers_memory_[2]; VkDescriptorPool descriptor_pool_[2]; std::vector descriptor_sets_[2]; std::vector image_views_[2]; diff --git a/sdk/unity/xr_unity_plugin/vulkan/vulkan_renderer.cc b/sdk/unity/xr_unity_plugin/vulkan/vulkan_renderer.cc index c8033616..662a9b31 100644 --- a/sdk/unity/xr_unity_plugin/vulkan/vulkan_renderer.cc +++ b/sdk/unity/xr_unity_plugin/vulkan/vulkan_renderer.cc @@ -190,7 +190,7 @@ class VulkanRenderer : public Renderer { .flags = 0, .image = swapchain_images_[i], .viewType = VK_IMAGE_VIEW_TYPE_2D, - .format = VK_FORMAT_R8G8B8A8_UNORM, + .format = VK_FORMAT_R8G8B8A8_SRGB, .components = { .r = VK_COMPONENT_SWIZZLE_R, @@ -272,8 +272,8 @@ class VulkanRenderer : public Renderer { } void SetupWidgets() override { - widget_renderer_ = std::make_unique(physical_device_, - logical_device_); + widget_renderer_ = std::make_unique( + physical_device_, logical_device_, swapchain_image_count_); } void RenderWidgets(const ScreenParams& screen_params, @@ -283,7 +283,8 @@ class VulkanRenderer : public Renderer { } widget_renderer_->RenderWidgets(screen_params, widget_params, - current_command_buffer_, render_pass_); + current_command_buffer_, image_index, + render_pass_); } void TeardownWidgets() override { diff --git a/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.cc b/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.cc index c2047557..60510cd0 100644 --- a/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.cc +++ b/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.cc @@ -47,10 +47,14 @@ bool widgetsOccupySameArea(const Renderer::WidgetParams& widget_params_left, } // namespace VulkanWidgetsRenderer::VulkanWidgetsRenderer(VkPhysicalDevice physical_device, - VkDevice logical_device) + VkDevice logical_device, + const int swapchain_image_count) : physical_device_(physical_device), logical_device_(logical_device), - widget_image_count_(1), + current_render_pass_(VK_NULL_HANDLE), + swapchain_image_count_(swapchain_image_count), + indices_count_(0), + texture_sampler_(VK_NULL_HANDLE), descriptor_set_layout_(VK_NULL_HANDLE), pipeline_layout_(VK_NULL_HANDLE), graphics_pipeline_(VK_NULL_HANDLE), @@ -58,9 +62,8 @@ VulkanWidgetsRenderer::VulkanWidgetsRenderer(VkPhysicalDevice physical_device, vertex_buffers_memory_(0), index_buffers_(VK_NULL_HANDLE), index_buffers_memory_(VK_NULL_HANDLE), - descriptor_pool_(VK_NULL_HANDLE), - descriptor_sets_(0), - image_views_(0) { + widgets_data_(0), + current_widget_params_(0) { if (!rendering::LoadVulkan()) { CARDBOARD_LOGE("Failed to load vulkan lib in cardboard!"); return; @@ -70,47 +73,37 @@ VulkanWidgetsRenderer::VulkanWidgetsRenderer(VkPhysicalDevice physical_device, } VulkanWidgetsRenderer::~VulkanWidgetsRenderer() { - for (uint32_t i = 0; i < image_views_.size(); i++) { - CleanTextureImageView(i); - } - rendering::vkDestroySampler(logical_device_, texture_sampler_, nullptr); rendering::vkDestroyPipelineLayout(logical_device_, pipeline_layout_, nullptr); rendering::vkDestroyDescriptorSetLayout(logical_device_, descriptor_set_layout_, nullptr); - rendering::vkDestroyDescriptorPool(logical_device_, descriptor_pool_, - nullptr); - + SetWidgetImageCount(0); CleanPipeline(); rendering::vkDestroyBuffer(logical_device_, index_buffers_, nullptr); rendering::vkFreeMemory(logical_device_, index_buffers_memory_, nullptr); for (uint32_t i = 0; i < vertex_buffers_.size(); i++) { - if (vertex_buffers_[i] != VK_NULL_HANDLE) { - rendering::vkDestroyBuffer(logical_device_, vertex_buffers_[i], nullptr); - rendering::vkFreeMemory(logical_device_, vertex_buffers_memory_[i], - nullptr); - } + CleanVertexBuffer(i); } } void VulkanWidgetsRenderer::RenderWidgets( const Renderer::ScreenParams& screen_params, const std::vector& widgets_params, - const VkCommandBuffer command_buffer, const VkRenderPass render_pass) { + const VkCommandBuffer command_buffer, const uint32_t swapchain_image_index, + const VkRenderPass render_pass) { // If the amount of widgets change, then recreate the objects related to them. - if (widget_image_count_ != widgets_params.size()) { - widget_image_count_ = widgets_params.size(); - CreatePerWidgetVulkanObjects(); + if (widgets_data_.size() != widgets_params.size()) { + SetWidgetImageCount(widgets_params.size()); UpdateVertexBuffers(widgets_params, screen_params); current_widget_params_ = widgets_params; } else { // If the position or the size of a widget changes, then update its vertex // buffer. - for (uint32_t i = 0; i < widget_image_count_; i++) { + for (uint32_t i = 0; i < widgets_data_.size(); i++) { if (!widgetsOccupySameArea(current_widget_params_[i], widgets_params[i])) { UpdateVertexBuffer(widgets_params[i], screen_params, i); @@ -124,8 +117,9 @@ void VulkanWidgetsRenderer::RenderWidgets( CreateGraphicsPipeline(); } - for (uint32_t i = 0; i < widget_image_count_; i++) { - RenderWidget(widgets_params[i], command_buffer, i, screen_params); + for (uint32_t i = 0; i < widgets_data_.size(); i++) { + RenderWidget(widgets_params[i], command_buffer, i, swapchain_image_index, + screen_params); } } @@ -222,40 +216,61 @@ void VulkanWidgetsRenderer::CreateSharedVulkanObjects() { CreateIndexBuffer(square_texture_indices); } -void VulkanWidgetsRenderer::CreatePerWidgetVulkanObjects() { - // Create Descriptor Pool - VkDescriptorPoolSize pool_sizes[1]; - pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - pool_sizes[0].descriptorCount = static_cast(widget_image_count_); - - VkDescriptorPoolCreateInfo pool_info{}; - pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.poolSizeCount = 1; - pool_info.pPoolSizes = pool_sizes; - pool_info.maxSets = static_cast(widget_image_count_); - - CALL_VK(rendering::vkCreateDescriptorPool(logical_device_, &pool_info, - nullptr, &descriptor_pool_)); - - // Create Descriptor Sets - std::vector layouts(widget_image_count_, - descriptor_set_layout_); - VkDescriptorSetAllocateInfo alloc_info{}; - alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.descriptorPool = descriptor_pool_; - alloc_info.descriptorSetCount = static_cast(widget_image_count_); - alloc_info.pSetLayouts = layouts.data(); - - descriptor_sets_.resize(widget_image_count_); - CALL_VK(rendering::vkAllocateDescriptorSets(logical_device_, &alloc_info, - descriptor_sets_.data())); - - // Set the size of image view array to the amount of widgets. - image_views_.resize(widget_image_count_); - - // Set the size of the vertex buffers array to the amount of widgets. - vertex_buffers_.resize(widget_image_count_); - vertex_buffers_memory_.resize(widget_image_count_); +void VulkanWidgetsRenderer::SetWidgetImageCount( + const uint32_t widget_image_count) { + // Clean. + for (uint32_t widget_index = 0; widget_index < widgets_data_.size(); + widget_index++) { + // Clean image views per widget. + for (uint32_t image_index = 0; + image_index < widgets_data_[widget_index].image_views.size(); + image_index++) { + CleanTextureImageView(widget_index, image_index); + } + // Clean descriptor pool per widget. + rendering::vkDestroyDescriptorPool( + logical_device_, widgets_data_[widget_index].descriptor_pool, nullptr); + } + + // Resize. + widgets_data_.resize(widget_image_count); + + // Recreate. + for (uint32_t widget = 0; widget < widgets_data_.size(); widget++) { + // Create Descriptor Pool + VkDescriptorPoolSize pool_sizes[1]; + pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + pool_sizes[0].descriptorCount = + static_cast(swapchain_image_count_); + + VkDescriptorPoolCreateInfo pool_info{}; + pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + pool_info.poolSizeCount = 1; + pool_info.pPoolSizes = pool_sizes; + pool_info.maxSets = static_cast(swapchain_image_count_); + + CALL_VK(rendering::vkCreateDescriptorPool( + logical_device_, &pool_info, nullptr, + &widgets_data_[widget].descriptor_pool)); + + // Create Descriptor Sets + std::vector layouts(swapchain_image_count_, + descriptor_set_layout_); + VkDescriptorSetAllocateInfo alloc_info{}; + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + alloc_info.descriptorPool = widgets_data_[widget].descriptor_pool; + alloc_info.descriptorSetCount = + static_cast(swapchain_image_count_); + alloc_info.pSetLayouts = layouts.data(); + widgets_data_[widget].descriptor_sets.resize(swapchain_image_count_); + + CALL_VK(rendering::vkAllocateDescriptorSets( + logical_device_, &alloc_info, + widgets_data_[widget].descriptor_sets.data())); + + // Set the size of image view array to the amount of swapchain images. + widgets_data_[widget].image_views.resize(swapchain_image_count_); + } } void VulkanWidgetsRenderer::CreateGraphicsPipeline() { @@ -470,20 +485,30 @@ uint32_t VulkanWidgetsRenderer::FindMemoryType( void VulkanWidgetsRenderer::UpdateVertexBuffers( const std::vector& widgets_params, const unity::Renderer::ScreenParams& screen_params) { - for (uint32_t i = 0; i < widget_image_count_; i++) { - UpdateVertexBuffer(widgets_params[i], screen_params, i); + vertex_buffers_.resize(widgets_params.size()); + vertex_buffers_memory_.resize(widgets_params.size()); + + for (uint32_t widget_index = 0; widget_index < widgets_params.size(); + widget_index++) { + UpdateVertexBuffer(widgets_params[widget_index], screen_params, + widget_index); } } -void VulkanWidgetsRenderer::UpdateVertexBuffer( - const unity::Renderer::WidgetParams& widget_params, - const unity::Renderer::ScreenParams& screen_params, const uint32_t index) { - if (vertex_buffers_[index] != VK_NULL_HANDLE) { - rendering::vkDestroyBuffer(logical_device_, vertex_buffers_[index], +void VulkanWidgetsRenderer::CleanVertexBuffer(const uint32_t widget_index) { + if (vertex_buffers_[widget_index] != VK_NULL_HANDLE) { + rendering::vkDestroyBuffer(logical_device_, vertex_buffers_[widget_index], nullptr); - rendering::vkFreeMemory(logical_device_, vertex_buffers_memory_[index], - nullptr); + rendering::vkFreeMemory(logical_device_, + vertex_buffers_memory_[widget_index], nullptr); } +} + +void VulkanWidgetsRenderer::UpdateVertexBuffer( + const unity::Renderer::WidgetParams& widget_params, + const unity::Renderer::ScreenParams& screen_params, + const uint32_t widget_index) { + CleanVertexBuffer(widget_index); // Convert coordinates to normalized space (-1,-1 - +1,+1) float x = @@ -506,23 +531,24 @@ void VulkanWidgetsRenderer::UpdateVertexBuffer( {x + width, y, 1.0f, 1.0f}}; // Create vertices for the widget. - CreateVertexBuffer(vertices, index); + CreateVertexBuffer(vertices, widget_index); } void VulkanWidgetsRenderer::RenderWidget( const unity::Renderer::WidgetParams& widget_params, - VkCommandBuffer command_buffer, uint32_t image_index, + VkCommandBuffer command_buffer, const uint32_t widget_index, + const uint32_t swapchain_image_index, const unity::Renderer::ScreenParams& screen_params) { // Update image and view VkImage* current_image = reinterpret_cast(widget_params.texture); - CleanTextureImageView(image_index); + CleanTextureImageView(widget_index, swapchain_image_index); const VkImageViewCreateInfo view_create_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .pNext = nullptr, .flags = 0, .image = *current_image, .viewType = VK_IMAGE_VIEW_TYPE_2D, - // This format must match the images format as can be seen in the Unity + // This format must match the images format as can be seen in the Unity // editor inspector. .format = VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, .components = @@ -541,21 +567,23 @@ void VulkanWidgetsRenderer::RenderWidget( .layerCount = 1, }, }; - CALL_VK(rendering::vkCreateImageView(logical_device_, &view_create_info, - nullptr /* pAllocator */, - &image_views_[image_index])); + CALL_VK(rendering::vkCreateImageView( + logical_device_, &view_create_info, nullptr /* pAllocator */, + &widgets_data_[widget_index].image_views[swapchain_image_index])); // Update Descriptor Sets VkDescriptorImageInfo image_info{ .sampler = texture_sampler_, - .imageView = image_views_[image_index], + .imageView = + widgets_data_[widget_index].image_views[swapchain_image_index], .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, }; VkWriteDescriptorSet descriptor_writes[1]; descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptor_writes[0].dstSet = descriptor_sets_[image_index]; + descriptor_writes[0].dstSet = + widgets_data_[widget_index].descriptor_sets[swapchain_image_index]; descriptor_writes[0].dstBinding = 0; descriptor_writes[0].dstArrayElement = 0; descriptor_writes[0].descriptorType = @@ -593,14 +621,14 @@ void VulkanWidgetsRenderer::RenderWidget( VkDeviceSize offset = 0; rendering::vkCmdBindVertexBuffers(command_buffer, 0, 1, - &vertex_buffers_[image_index], &offset); - + &vertex_buffers_[widget_index], &offset); rendering::vkCmdBindIndexBuffer(command_buffer, index_buffers_, 0, VK_INDEX_TYPE_UINT16); rendering::vkCmdBindDescriptorSets( command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout_, 0, 1, - &descriptor_sets_[image_index], 0, nullptr); + &widgets_data_[widget_index].descriptor_sets[swapchain_image_index], 0, + nullptr); rendering::vkCmdDrawIndexed( command_buffer, static_cast(indices_count_), 1, 0, 0, 0); } @@ -612,11 +640,16 @@ void VulkanWidgetsRenderer::CleanPipeline() { } } -void VulkanWidgetsRenderer::CleanTextureImageView(int index) { - if (image_views_[index] != VK_NULL_HANDLE) { - rendering::vkDestroyImageView(logical_device_, image_views_[index], - nullptr /* vkDestroyImageView */); - image_views_[index] = VK_NULL_HANDLE; +void VulkanWidgetsRenderer::CleanTextureImageView( + const int widget_index, const int swapchain_image_index) { + if (widgets_data_[widget_index].image_views[swapchain_image_index] != + VK_NULL_HANDLE) { + rendering::vkDestroyImageView( + logical_device_, + widgets_data_[widget_index].image_views[swapchain_image_index], + nullptr /* vkDestroyImageView */); + widgets_data_[widget_index].image_views[swapchain_image_index] = + VK_NULL_HANDLE; } } diff --git a/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.h b/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.h index e6f76496..636180eb 100644 --- a/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.h +++ b/sdk/unity/xr_unity_plugin/vulkan/vulkan_widgets_renderer.h @@ -35,9 +35,11 @@ class VulkanWidgetsRenderer { * * @param physical_device Vulkan physical device. * @param logical_device Vulkan logical device. + * @param swapchain_image_count Number of images available in the swapchain. */ VulkanWidgetsRenderer(VkPhysicalDevice physical_device, - VkDevice logical_device); + VkDevice logical_device, + const int swapchain_image_count); /** * Destructor. Frees renderer resources. @@ -51,11 +53,13 @@ class VulkanWidgetsRenderer { * @param widgets_params Params for each widget. This includes position to * render and texture. * @param command_buffer VkCommandBuffer to be bond. + * @param swapchain_image_index Swapchain image to be rendered. * @param render_pass Render pass used. */ void RenderWidgets(const Renderer::ScreenParams& screen_params, const std::vector& widgets_params, const VkCommandBuffer command_buffer, + const uint32_t swapchain_image_index, const VkRenderPass render_pass); private: @@ -78,6 +82,17 @@ class VulkanWidgetsRenderer { float tex_v; }; + /** + * @struct Data required for each widget. + */ + struct PerWidgetData { + VkDescriptorPool descriptor_pool; + // Size should be the size of the swapchain. + std::vector descriptor_sets; + // Size should be the size of the swapchain. + std::vector image_views; + }; + static constexpr float Lerp(float start, float end, float val) { return start + (end - start) * val; } @@ -126,12 +141,14 @@ class VulkanWidgetsRenderer { void CreateSharedVulkanObjects(); /** - * Creates required vulkan objects for the given eye. + * Creates required vulkan objects for the given widget. + * + * @param widget Number of the widget. */ - void CreatePerWidgetVulkanObjects(); + void SetWidgetImageCount(const uint32_t widget); /** - * Creates the graphics pipeline for the given eye. + * Creates the graphics pipeline. * It cleans the previous pipeline if it exists. */ void CreateGraphicsPipeline(); @@ -162,32 +179,44 @@ class VulkanWidgetsRenderer { * * @param widget_params Texture for the widget. * @param command_buffer VkCommandBuffer to be bond. - * @param image_index Index of current image in the image views array. + * @param swapchain_image_index Index of current image in the image views + * array. * @param screen_params Screen parameters of the rendering area. */ void RenderWidget(const unity::Renderer::WidgetParams& widget_params, - VkCommandBuffer command_buffer, uint32_t image_index, + VkCommandBuffer command_buffer, const uint32_t widget_index, + const uint32_t swapchain_image_index, const unity::Renderer::ScreenParams& screen_params); /** - * Cleans the graphics pipeline of the given eye. + * Cleans the graphics pipeline. */ void CleanPipeline(); /** - * Cleans the image view of the given eye and swapchain image index. + * Cleans the image view of the given widget and swapchain image index. * - * @param index The index of the image in the swapchain. + * @param widget_index The index of the widget. + * @param swapchain_image_index The index of the image in the swapchain. */ - void CleanTextureImageView(int index); + void CleanTextureImageView(const int widget_index, + const int swapchain_image_index); /** * Creates a vertex buffer and store it internally. * * @param vertices Content of the vertex buffer. - * @param index The index of the buffer. + * @param widget_index The index of the widget related to the buffer. + */ + void CreateVertexBuffer(std::vector vertices, + const uint32_t widget_index); + + /** + * Cleans a vertex buffer. + * + * @param widget_index The index of the widget related to the buffer. */ - void CreateVertexBuffer(std::vector vertices, const uint32_t index); + void CleanVertexBuffer(const uint32_t widget_index); /** * Creates an index buffer and store it internally. @@ -202,7 +231,7 @@ class VulkanWidgetsRenderer { VkRenderPass current_render_pass_; // Variables created and maintained by the widget renderer. - uint32_t widget_image_count_; + uint32_t swapchain_image_count_; int indices_count_; VkSampler texture_sampler_; VkDescriptorSetLayout descriptor_set_layout_; @@ -212,9 +241,7 @@ class VulkanWidgetsRenderer { std::vector vertex_buffers_memory_; VkBuffer index_buffers_; VkDeviceMemory index_buffers_memory_; - VkDescriptorPool descriptor_pool_; - std::vector descriptor_sets_; - std::vector image_views_; + std::vector widgets_data_; std::vector current_widget_params_; };