Overte C++ Documentation
vk/src/vk/Helpers.h
1 //
2 // Created by Bradley Austin Davis on 2018/10/29
3 // Copyright 2018 High Fidelity, Inc.
4 // Copyright 2021-2024 Overte e.V.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 // Contains parts of Vulkan Samples, Copyright (c) 2018, Sascha Willems, distributed on MIT License.
11 //
12 #pragma once
13 
14 #include "Config.h"
15 #include <array>
16 #include <vector>
17 
18 #include <QtCore/QUuid>
19 
20 
21 class QOpenGLContext;
22 
23 namespace vks { namespace util {
24 
25 
26 inline VkColorComponentFlags fullColorWriteMask() {
27  return VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT |
28  VK_COLOR_COMPONENT_A_BIT;
29 }
30 
31 inline VkViewport viewport(float width, float height, float minDepth = 0, float maxDepth = 1) {
32  VkViewport viewport;
33  viewport.width = width;
34  viewport.height = height;
35  viewport.minDepth = minDepth;
36  viewport.maxDepth = maxDepth;
37  return viewport;
38 }
39 
40 inline VkViewport viewport(const glm::uvec2& size, float minDepth = 0, float maxDepth = 1) {
41  return viewport((float)size.x, (float)size.y, minDepth, maxDepth);
42 }
43 
44 inline VkViewport viewport(const VkExtent2D& size, float minDepth = 0, float maxDepth = 1) {
45  return viewport((float)size.width, (float)size.height, minDepth, maxDepth);
46 }
47 
48 inline VkRect2D rect2D(uint32_t width, uint32_t height, int32_t offsetX = 0, int32_t offsetY = 0) {
49  VkRect2D rect2D;
50  rect2D.extent.width = width;
51  rect2D.extent.height = height;
52  rect2D.offset.x = offsetX;
53  rect2D.offset.y = offsetY;
54  return rect2D;
55 }
56 
57 inline VkRect2D rect2D(const glm::uvec2& size, const glm::ivec2& offset = glm::ivec2(0)) {
58  return rect2D(size.x, size.y, offset.x, offset.y);
59 }
60 
61 inline VkRect2D rect2D(const VkExtent2D& size, const VkOffset2D& offset = VkOffset2D()) {
62  return rect2D(size.width, size.height, offset.x, offset.y);
63 }
64 
65 inline VkAccessFlags accessFlagsForLayout(VkImageLayout layout) {
66  switch (layout) {
67  case VK_IMAGE_LAYOUT_PREINITIALIZED:
68  return VK_ACCESS_HOST_WRITE_BIT;
69  case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
70  return VK_ACCESS_TRANSFER_WRITE_BIT;
71  case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
72  return VK_ACCESS_TRANSFER_READ_BIT;
73  case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
74  return VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
75  case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
76  return VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
77  case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
78  return VK_ACCESS_SHADER_READ_BIT;
79  default:
80  return VkAccessFlags();
81  }
82 }
83 
84 bool loadPipelineCacheData(std::vector<uint8_t>& outCache);
85 void savePipelineCacheData(const std::vector<uint8_t>& cache);
86 
87 namespace gl {
88 
89 using UuidSet = std::set<QUuid>;
90 
91 UuidSet getUuids();
92 bool contextSupported(QOpenGLContext*);
93 
94 } // namespace vks::util::gl
95 
96 }} // namespace vks::util