-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkghl.h
205 lines (175 loc) · 7.07 KB
/
vkghl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#pragma once
#include <iostream>
#include <thread>
#ifdef __clangd__
// For IDE
#include "layer_factory.h"
#include "layer_factory.cpp"
#endif
class VkGHL : public layer_factory {
using Clock = std::chrono::steady_clock;
using TimeDiff = std::chrono::nanoseconds;
using TimePoint = typename Clock::time_point;
public:
VkGHL()
: targetFrameTime(getFrameTime()),
vSync(getVSync()),
mipLODBias(getLodBias()),
AF(getAF()),
retroFilter(getRetro()),
frameOverhead(0),
frameStart(Clock::now()),
frameEnd(Clock::now()) {
layer_name = "VkGHL";
isDisabled = !testSettings();
if (isDisabled)
std::cout << "VkGHL: Disabled\n";
}
VkResult PostCallQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo, VkResult result) override;
VkResult PreCallCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator,
VkSwapchainKHR *pSwapchain) override;
VkResult PreCallCreateSampler(VkDevice device, const VkSamplerCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
VkSampler *pSampler) override;
VkResult PostCallGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, uint32_t *pPresentModeCount,
VkPresentModeKHR *pPresentModes, VkResult result) override;
private:
VkPresentModeKHR getVSync();
TimeDiff getFrameTime();
float getLodBias();
float getAF();
bool getRetro();
inline void limiter();
const bool testSettings();
const char *getPresentModeName(VkPresentModeKHR mode);
inline bool isValidFPS();
inline bool isValidVSync();
inline bool isValidBias();
inline bool isValidAF();
inline bool isValidRetro();
VkPresentModeKHR vSync;
const TimeDiff targetFrameTime;
float mipLODBias;
float AF;
bool retroFilter;
TimeDiff frameOverhead;
TimePoint frameStart;
TimePoint frameEnd;
bool isDisabled;
};
VkResult VkGHL::PostCallQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo, VkResult result) {
if (isDisabled)
return VK_SUCCESS;
frameStart = Clock::now();
limiter();
frameEnd = Clock::now();
return VK_SUCCESS;
}
VkResult VkGHL::PostCallGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, uint32_t *pPresentModeCount,
VkPresentModeKHR *pPresentModes, VkResult result) {
if (pPresentModes != nullptr) {
std::vector<VkPresentModeKHR> present_modes;
present_modes.insert(present_modes.begin(), pPresentModes, pPresentModes + *pPresentModeCount);
std::cout << "VkGHL: Present modes cnt: " << *pPresentModeCount << "\n";
for (auto present_mode : present_modes)
std::cout << "VkGHL: " << getPresentModeName(present_mode) << ": " << present_mode << "\n";
}
return VK_SUCCESS;
}
VkResult VkGHL::PreCallCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator,
VkSwapchainKHR *pSwapchain) {
if (isDisabled)
return VK_SUCCESS;
if (isValidVSync())
const_cast<VkPresentModeKHR &>(pCreateInfo->presentMode) = vSync;
return VK_SUCCESS;
}
VkResult VkGHL::PreCallCreateSampler(VkDevice device, const VkSamplerCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
VkSampler *pSampler) {
if (isDisabled)
return VK_SUCCESS;
if (isValidBias())
const_cast<float &>(pCreateInfo->mipLodBias) = mipLODBias;
if (isValidAF()) {
const_cast<VkBool32 &>(pCreateInfo->anisotropyEnable) = !(AF == 0);
const_cast<float &>(pCreateInfo->maxAnisotropy) = AF;
}
if (isValidRetro()) {
const_cast<VkFilter &>(pCreateInfo->magFilter) = VkFilter::VK_FILTER_NEAREST;
const_cast<VkFilter &>(pCreateInfo->minFilter) = VkFilter::VK_FILTER_NEAREST;
const_cast<VkSamplerMipmapMode &>(pCreateInfo->mipmapMode) = VkSamplerMipmapMode::VK_SAMPLER_MIPMAP_MODE_NEAREST;
}
return VK_SUCCESS;
}
std::chrono::nanoseconds VkGHL::getFrameTime() {
const char *fpsLimit = std::getenv("FPS");
if (fpsLimit != nullptr && !std::string(fpsLimit).empty()) {
std::cout << "VkGHL: FPS limit: " << fpsLimit << " fps\n";
double fps = std::stod(fpsLimit);
if (fps != 0.0)
return TimeDiff(uint64_t(1000000000.0f / fps));
}
return TimeDiff(0);
}
VkPresentModeKHR VkGHL::getVSync() {
const char *vsyncType = std::getenv("VSYNC");
if (vsyncType != nullptr && !std::string(vsyncType).empty()) {
std::cout << "VkGHL: VSync: " << vsyncType << "\n";
int8_t vs = std::stoi(vsyncType);
if (vs >= 0)
return static_cast<VkPresentModeKHR>(vs);
}
return VkPresentModeKHR::VK_PRESENT_MODE_MAX_ENUM_KHR;
}
float VkGHL::getLodBias() {
const char *lodBias = std::getenv("MIPLODBIAS");
if (lodBias != nullptr && !std::string(lodBias).empty()) {
std::cout << "VkGHL: mipLODBias: " << lodBias << "\n";
float mip = std::stof(lodBias);
if (mip >= -16.0f && mip <= 15.99f)
return mip;
}
return 666;
}
float VkGHL::getAF() {
const char *aF = std::getenv("AF");
if (aF != nullptr && !std::string(aF).empty()) {
std::cout << "VkGHL: AF: " << aF << "\n";
float af = std::stof(aF);
if (af >= 0 && af <= 16)
return af;
}
return 666;
}
bool VkGHL::getRetro() {
const char *retro = std::getenv("RETRO");
if (retro != nullptr && !std::string(retro).empty()) {
std::cout << "VkGHL: RETRO: " << retro << "\n";
return !(std::string(retro) == "0");
}
return false;
}
inline void VkGHL::limiter() {
TimeDiff sleepTime = targetFrameTime - (Clock::now() - frameEnd);
if (sleepTime > frameOverhead) {
TimeDiff adjustedSleepTime = sleepTime - frameOverhead;
std::this_thread::sleep_for(TimeDiff(adjustedSleepTime));
frameOverhead = ((Clock::now() - frameStart) - adjustedSleepTime + frameOverhead * 99) / 100;
}
}
inline bool VkGHL::isValidVSync() {
return vSync >= VkPresentModeKHR::VK_PRESENT_MODE_BEGIN_RANGE_KHR && vSync <= VkPresentModeKHR::VK_PRESENT_MODE_END_RANGE_KHR;
}
inline bool VkGHL::isValidFPS() { return targetFrameTime > TimeDiff(0); }
inline bool VkGHL::isValidBias() { return mipLODBias >= -16.0f && mipLODBias <= 15.99f; }
inline bool VkGHL::isValidAF() { return AF == 0 || (AF >= 1 && AF <= 16); }
inline bool VkGHL::isValidRetro() { return retroFilter; }
const bool VkGHL::testSettings() { return isValidFPS() || isValidVSync() || isValidBias() || isValidAF() || isValidRetro(); }
inline const char *VkGHL::getPresentModeName(VkPresentModeKHR mode) {
std::unordered_map<VkPresentModeKHR, const char *> modes;
modes = {{VK_PRESENT_MODE_FIFO_RELAXED_KHR, "VK_PRESENT_MODE_FIFO_RELAXED_KHR"},
{VK_PRESENT_MODE_IMMEDIATE_KHR, "VK_PRESENT_MODE_IMMEDIATE_KHR"},
{VK_PRESENT_MODE_MAILBOX_KHR, "VK_PRESENT_MODE_MAILBOX_KHR"},
{VK_PRESENT_MODE_FIFO_KHR, "VK_PRESENT_MODE_FIFO_KHR"}};
return modes[mode];
}
VkGHL vkghl;