20#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
21#define GLFW_INCLUDE_VULKAN
23#include <GLFW/glfw3.h>
29#ifndef GPUCA_STANDALONE
30#include <DebugGUI/imgui.h>
31#include <DebugGUI/DebugGUI.h>
44int32_t GPUDisplayFrontendGlfw::GetKey(int32_t
key)
46 if (
key == GLFW_KEY_KP_SUBTRACT) {
49 if (
key == GLFW_KEY_KP_ADD) {
52 if (
key == GLFW_KEY_LEFT_SHIFT ||
key == GLFW_KEY_RIGHT_SHIFT) {
55 if (
key == GLFW_KEY_LEFT_ALT) {
58 if (
key == GLFW_KEY_RIGHT_ALT) {
61 if (
key == GLFW_KEY_LEFT_CONTROL) {
64 if (
key == GLFW_KEY_RIGHT_CONTROL) {
67 if (
key == GLFW_KEY_UP) {
70 if (
key == GLFW_KEY_DOWN) {
73 if (
key == GLFW_KEY_LEFT) {
76 if (
key == GLFW_KEY_RIGHT) {
79 if (
key == GLFW_KEY_PAGE_UP) {
82 if (
key == GLFW_KEY_PAGE_DOWN) {
85 if (
key == GLFW_KEY_ESCAPE) {
88 if (
key == GLFW_KEY_SPACE) {
91 if (
key == GLFW_KEY_HOME) {
94 if (
key == GLFW_KEY_END) {
97 if (
key == GLFW_KEY_INSERT) {
100 if (
key == GLFW_KEY_ENTER) {
103 if (
key == GLFW_KEY_F1) {
106 if (
key == GLFW_KEY_F2) {
109 if (
key == GLFW_KEY_F3) {
112 if (
key == GLFW_KEY_F4) {
115 if (
key == GLFW_KEY_F5) {
118 if (
key == GLFW_KEY_F6) {
121 if (
key == GLFW_KEY_F7) {
124 if (
key == GLFW_KEY_F8) {
127 if (
key == GLFW_KEY_F9) {
130 if (
key == GLFW_KEY_F10) {
133 if (
key == GLFW_KEY_F11) {
136 if (
key == GLFW_KEY_F12) {
142void GPUDisplayFrontendGlfw::GetKey(int32_t
key, int32_t scancode, int32_t mods, int32_t& keyOut, int32_t& keyPressOut)
144 int32_t specialKey = GetKey(
key);
145 const char*
str = glfwGetKeyName(
key, scancode);
146 char localeKey =
str ?
str[0] : 0;
147 if ((mods & GLFW_MOD_SHIFT) && localeKey >=
'a' && localeKey <=
'z') {
148 localeKey +=
'A' -
'a';
153 keyOut = keyPressOut = specialKey;
155 keyOut = keyPressOut = (
uint8_t)localeKey;
156 if (keyPressOut >=
'a' && keyPressOut <=
'z') {
157 keyPressOut +=
'A' -
'a';
162void GPUDisplayFrontendGlfw::error_callback(int32_t error,
const char*
description) { fprintf(stderr,
"Error: %s\n",
description); }
164void GPUDisplayFrontendGlfw::key_callback(GLFWwindow* window, int32_t
key, int32_t scancode, int32_t action, int32_t mods)
166 int32_t handleKey = 0, keyPress = 0;
167 GetKey(
key, scancode, mods, handleKey, keyPress);
168 if (handleKey < 32) {
169 if (action == GLFW_PRESS) {
170 me->mKeys[keyPress] =
true;
171 me->mKeysShift[keyPress] = mods & GLFW_MOD_SHIFT;
172 me->HandleKey(handleKey);
173 }
else if (action == GLFW_RELEASE) {
174 me->mKeys[keyPress] =
false;
175 me->mKeysShift[keyPress] =
false;
177 }
else if (handleKey < 256) {
178 if (action == GLFW_PRESS) {
179 me->mLastKeyDown = handleKey;
180 }
else if (action == GLFW_RELEASE) {
181 keyPress = (
uint8_t)
me->mKeyDownMap[handleKey];
182 me->mKeys[keyPress] =
false;
183 me->mKeysShift[keyPress] =
false;
188void GPUDisplayFrontendGlfw::char_callback(GLFWwindow* window, uint32_t codepoint)
191 if (codepoint < 256) {
193 if (keyPress >=
'a' && keyPress <=
'z') {
194 keyPress +=
'A' -
'a';
196 me->mKeyDownMap[
me->mLastKeyDown] = keyPress;
197 me->mKeys[keyPress] =
true;
199 me->HandleKey(codepoint);
203void GPUDisplayFrontendGlfw::mouseButton_callback(GLFWwindow* window, int32_t button, int32_t action, int32_t mods)
205 if (action == GLFW_PRESS) {
208 }
else if (button == 1) {
209 me->mMouseDnR =
true;
211 me->mMouseDnX =
me->mMouseMvX;
212 me->mMouseDnY =
me->mMouseMvY;
213 }
else if (action == GLFW_RELEASE) {
215 me->mMouseDn =
false;
216 }
else if (button == 1) {
217 me->mMouseDnR =
false;
222void GPUDisplayFrontendGlfw::scroll_callback(GLFWwindow* window,
double x,
double y) {
me->mMouseWheel +=
y * 100; }
224void GPUDisplayFrontendGlfw::cursorPos_callback(GLFWwindow* window,
double x,
double y)
230void GPUDisplayFrontendGlfw::resize_callback(GLFWwindow* window, int32_t
width, int32_t
height) {
me->ResizeScene(
width,
height); }
232#ifndef GPUCA_STANDALONE
233void GPUDisplayFrontendGlfw::DisplayLoop()
235 ImGui::SetNextWindowPos(ImVec2(0, 0));
236 ImGui::SetNextWindowSize(ImVec2(
me->mDisplayWidth,
me->mDisplayHeight));
237 ImGui::SetNextWindowBgAlpha(0.f);
238 ImGui::Begin(
"Console",
nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
244int32_t GPUDisplayFrontendGlfw::FrontendMain()
249 fprintf(stderr,
"Error initializing glfw\n");
252 glfwSetErrorCallback(error_callback);
254 glfwWindowHint(GLFW_MAXIMIZED, 1);
256 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
261 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 0);
262 glfwWindowHint(GLFW_OPENGL_PROFILE,
mBackend->
CoreProfile() ? GLFW_OPENGL_CORE_PROFILE : GLFW_OPENGL_COMPAT_PROFILE);
266 fprintf(stderr,
"Error creating glfw window\n");
271 glfwMakeContextCurrent(mWindow);
274 glfwSetKeyCallback(mWindow, key_callback);
275 glfwSetCharCallback(mWindow, char_callback);
276 glfwSetMouseButtonCallback(mWindow, mouseButton_callback);
277 glfwSetScrollCallback(mWindow, scroll_callback);
278 glfwSetCursorPosCallback(mWindow, cursorPos_callback);
279 glfwSetWindowSizeCallback(mWindow, resize_callback);
284 pthread_mutex_lock(&mSemLockExit);
286 pthread_mutex_unlock(&mSemLockExit);
289 fprintf(stderr,
"Error initializing GL extension wrapper\n");
294 fprintf(stderr,
"Error in GLFW display initialization\n");
298 while (!glfwWindowShouldClose(mWindow)) {
301 fprintf(stderr,
"Error drawing GL scene\n");
305 glfwSwapBuffers(mWindow);
312 pthread_mutex_lock(&mSemLockExit);
313 glfwDestroyWindow(mWindow);
315 mGlfwRunning =
false;
316 pthread_mutex_unlock(&mSemLockExit);
323 pthread_mutex_lock(&mSemLockExit);
325 glfwSetWindowShouldClose(mWindow,
true);
327 pthread_mutex_unlock(&mSemLockExit);
328 while (mGlfwRunning) {
339 GPUInfo(
"Setting Full Screen %d", (int32_t)set);
341 glfwGetWindowPos(mWindow, &mWindowX, &mWindowY);
342 glfwGetWindowSize(mWindow, &mWindowWidth, &mWindowHeight);
343 GLFWmonitor* primary = glfwGetPrimaryMonitor();
344 const GLFWvidmode*
mode = glfwGetVideoMode(primary);
345 glfwSetWindowMonitor(mWindow, primary, 0, 0,
mode->width,
mode->height,
mode->refreshRate);
347 glfwSetWindowMonitor(mWindow,
nullptr, mWindowX, mWindowY, mWindowWidth, mWindowHeight, GLFW_DONT_CARE);
354 glfwMaximizeWindow(mWindow);
356 glfwRestoreWindow(mWindow);
364#ifdef GPUCA_STANDALONE
378#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
379 return glfwCreateWindowSurface(*(VkInstance*)instance, mWindow,
nullptr, (VkSurfaceKHR*)surface) != VK_SUCCESS;
387 uint32_t glfwExtensionCount = 0;
388#ifdef GPUCA_BUILD_EVENT_DISPLAY_VULKAN
389 p = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
391 return glfwExtensionCount;
header::DataDescription description
virtual bool CoreProfile()
virtual int32_t ExtInit()
void SetVSync(bool enable) override
void getSize(int32_t &width, int32_t &height) override
void OpenGLPrint(const char *s, float x, float y, float r, float g, float b, float a, bool fromBotton=true) override
void ToggleMaximized(bool set) override
uint32_t getReqVulkanExtensions(const char **&p) override
void DisplayExit() override
void SwitchFullscreen(bool set) override
int32_t getVulkanSurface(void *instance, void *surface) override
bool EnableSendKey() override
static constexpr int32_t KEY_F9
const char * mFrontendName
static constexpr int32_t KEY_F2
static constexpr int32_t KEY_ALT
static constexpr int32_t KEY_F12
static constexpr int32_t KEY_F7
static constexpr int32_t KEY_END
static constexpr int32_t KEY_PAGEDOWN
static constexpr int32_t KEY_RALT
volatile int32_t mDisplayControl
static constexpr int32_t KEY_F10
static constexpr int32_t KEY_F4
static constexpr int32_t KEY_LEFT
static constexpr int32_t KEY_ENTER
static constexpr int32_t KEY_F6
static constexpr int32_t KEY_CTRL
static constexpr int32_t KEY_F1
static constexpr const char * DISPLAY_WINDOW_NAME
static constexpr int32_t INIT_WIDTH
static constexpr int32_t GL_MIN_VERSION_MAJOR
frontendTypes mFrontendType
static constexpr int32_t KEY_SHIFT
static constexpr int32_t KEY_F3
static constexpr int32_t KEY_INSERT
static constexpr int32_t KEY_F11
static constexpr int32_t KEY_SPACE
static constexpr int32_t KEY_F5
static constexpr int32_t KEY_PAGEUP
static constexpr int32_t KEY_HOME
GPUDisplayBackend * mBackend
static constexpr int32_t GL_MIN_VERSION_MINOR
int32_t InitDisplay(bool initFailure=false)
GPUDisplayBackend * backend()
static constexpr int32_t KEY_UP
static constexpr int32_t KEY_DOWN
static constexpr int32_t KEY_F8
static constexpr int32_t INIT_HEIGHT
static constexpr int32_t KEY_RIGHT
static constexpr int32_t KEY_RCTRL
static constexpr int32_t KEY_ESCAPE
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
uint8_t itsSharedClusterMap uint8_t