Overte C++ Documentation
VKShared.h
1 //
2 // Created by Bradley Austin Davis on 2016/08/07
3 // Adapted for Vulkan in 2022-2025 by dr Karol Suprynowicz.
4 // Copyright 2013-2018 High Fidelity, Inc.
5 // Copyright 2023-2025 Overte e.V.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 // SPDX-License-Identifier: Apache-2.0
10 //
11 #ifndef hifi_gpu_VKShared_h
12 #define hifi_gpu_VKShared_h
13 
14 #include <vk/Config.h>
15 #include <gpu/Forward.h>
16 #include <gpu/Format.h>
17 #include <gpu/Context.h>
18 #include "VKBackend.h"
19 #include <Sampler.h>
20 
21 Q_DECLARE_LOGGING_CATEGORY(gpu_vk_logging)
22 Q_DECLARE_LOGGING_CATEGORY(trace_gpu_vk)
23 Q_DECLARE_LOGGING_CATEGORY(trace_gpu_vk_detail)
24 
25 namespace gpu { namespace vk {
26 
27 gpu::Size getDedicatedMemory();
28 ComparisonFunction comparisonFuncFromGL(VkCompareOp func);
29 State::StencilOp stencilOpFromGL(VkStencilOp stencilOp);
30 State::BlendOp blendOpFromGL(VkBlendOp blendOp);
31 State::BlendArg blendArgFromGL(VkBlendFactor blendArg);
32 
33 VkFormat evalTexelFormatInternal(const Element& dstFormat, const vks::Context &context);
34 
35 //bool isDepthStencilFormat(VkFormat format);
36 bool formatHasStencil(VkFormat format);
37 
38 VkColorComponentFlags colorMaskToVk(const gpu::State::ColorMask &mask);
39 
40 static const VkBlendOp BLEND_OPS_TO_VK[State::NUM_BLEND_OPS] = {
41  VK_BLEND_OP_ADD,
42  VK_BLEND_OP_SUBTRACT,
43  VK_BLEND_OP_REVERSE_SUBTRACT,
44  VK_BLEND_OP_MIN,
45  VK_BLEND_OP_MAX
46 };
47 
48 static const VkBlendFactor BLEND_ARGS_TO_VK[State::NUM_BLEND_ARGS] = {
49  VK_BLEND_FACTOR_ZERO,
50  VK_BLEND_FACTOR_ONE,
51  VK_BLEND_FACTOR_SRC_COLOR,
52  VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
53  VK_BLEND_FACTOR_SRC_ALPHA,
54  VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
55  VK_BLEND_FACTOR_DST_ALPHA,
56  VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
57  VK_BLEND_FACTOR_DST_COLOR,
58  VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
59  VK_BLEND_FACTOR_SRC_ALPHA_SATURATE,
60  VK_BLEND_FACTOR_CONSTANT_COLOR,
61  VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
62  VK_BLEND_FACTOR_CONSTANT_ALPHA,
63  VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
64 };
65 
66 static const VkCompareOp COMPARISON_TO_VK[static_cast<size_t>(ComparisonFunction::NUM_COMPARISON_FUNCS)] = {
67  VK_COMPARE_OP_NEVER,
68  VK_COMPARE_OP_LESS,
69  VK_COMPARE_OP_EQUAL,
70  VK_COMPARE_OP_LESS_OR_EQUAL,
71  VK_COMPARE_OP_GREATER,
72  VK_COMPARE_OP_NOT_EQUAL,
73  VK_COMPARE_OP_GREATER_OR_EQUAL,
74  VK_COMPARE_OP_ALWAYS
75 };
76 
77 static const VkPrimitiveTopology PRIMITIVE_TO_VK[gpu::NUM_PRIMITIVES] = {
78  VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
79  VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
80  VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
81  VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
82  VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
83  VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
84 };
85 
86 enum VKSyncState {
87  // The object is currently undergoing no processing, although it's content
88  // may be out of date, or it's storage may be invalid relative to the
89  // owning GPU object
90  Idle,
91  // The object has been queued for transfer to the GPU
92  Pending,
93  // The object has been transferred to the GPU, but is awaiting
94  // any post transfer operations that may need to occur on the
95  // primary rendering thread
96  Transferred,
97 };
98 
99 // VKTODO is it needed?
100 /*static const enum ELEMENT_TYPE_TO_VK[gpu::NUM_TYPES] = {
101  VK_FLOAT,
102  VK_INT,
103  VK_UNSIGNED_INT,
104  VK_HALF_FLOAT,
105  VK_SHORT,
106  VK_UNSIGNED_SHORT,
107  VK_BYTE,
108  VK_UNSIGNED_BYTE,
109  // Normalized values
110  VK_INT,
111  VK_UNSIGNED_INT,
112  VK_SHORT,
113  VK_UNSIGNED_SHORT,
114  VK_BYTE,
115  VK_UNSIGNED_BYTE
116 };*/
117 
118 class VKBackend;
119 
120 template <typename GPUType>
121 struct VKObject : public GPUObject {
122 public:
123  VKObject(VKBackend& backend, const GPUType& gpuObject) : _gpuObject(gpuObject), _backend(backend.shared_from_this()) {}
124 
125  virtual ~VKObject() { }
126 
127  const GPUType& _gpuObject;
128 protected:
129  // VKTODO: Maybe replace with regular pointer for efficiency
130  const std::weak_ptr<VKBackend> _backend;
131 };
132 
133 } } // namespace gpu::vulkan
134 
135 #endif
136 
137 
138