blob: adabd57c091ca3ecc751b44c6c1024e8b8be2b6c [file] [log] [blame]
Tony Barbour2f18b292016-02-25 15:44:10 -07001/*
2 * Copyright (C) 2016 Google, Inc.
3 *
Jon Ashburn43b53e82016-04-19 11:30:31 -06004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Tony Barbour2f18b292016-02-25 15:44:10 -07007 *
Jon Ashburn43b53e82016-04-19 11:30:31 -06008 * http://www.apache.org/licenses/LICENSE-2.0
Tony Barbour2f18b292016-02-25 15:44:10 -07009 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Tony Barbour2f18b292016-02-25 15:44:10 -070015 */
16
17#ifndef SHELL_H
18#define SHELL_H
19
20#include <queue>
21#include <vector>
22#include <stdexcept>
23#include <vulkan/vulkan.h>
24
25#include "Game.h"
26
27class Game;
28
29class Shell {
30public:
31 Shell(const Shell &sh) = delete;
32 Shell &operator=(const Shell &sh) = delete;
33 virtual ~Shell() {}
34
35 struct BackBuffer {
36 uint32_t image_index;
37
38 VkSemaphore acquire_semaphore;
39 VkSemaphore render_semaphore;
40
41 // signaled when this struct is ready for reuse
42 VkFence present_fence;
43 };
44
45 struct Context {
46 VkInstance instance;
47 VkDebugReportCallbackEXT debug_report;
48
49 VkPhysicalDevice physical_dev;
50 uint32_t game_queue_family;
51 uint32_t present_queue_family;
52
53 VkDevice dev;
54 VkQueue game_queue;
55 VkQueue present_queue;
56
57 std::queue<BackBuffer> back_buffers;
58
59 VkSurfaceKHR surface;
60 VkSurfaceFormatKHR format;
61
62 VkSwapchainKHR swapchain;
63 VkExtent2D extent;
64
65 BackBuffer acquired_back_buffer;
66 };
67 const Context &context() const { return ctx_; }
68
69 enum LogPriority {
70 LOG_DEBUG,
71 LOG_INFO,
72 LOG_WARN,
73 LOG_ERR,
74 };
75 virtual void log(LogPriority priority, const char *msg);
76
77 virtual void run() = 0;
78 virtual void quit() = 0;
79
80protected:
81 Shell(Game &game);
82
83 void init_vk();
84 void cleanup_vk();
85
86 void create_context();
87 void destroy_context();
88
89 void resize_swapchain(uint32_t width_hint, uint32_t height_hint);
90
91 void add_game_time(float time);
92
93 void acquire_back_buffer();
94 void present_back_buffer();
95
96 Game &game_;
97 const Game::Settings &settings_;
98
99 std::vector<const char *> instance_layers_;
100 std::vector<const char *> instance_extensions_;
101
102 std::vector<const char *> device_layers_;
103 std::vector<const char *> device_extensions_;
104
105private:
106 bool debug_report_callback(VkDebugReportFlagsEXT flags,
107 VkDebugReportObjectTypeEXT obj_type,
108 uint64_t object,
109 size_t location,
110 int32_t msg_code,
111 const char *layer_prefix,
112 const char *msg);
113 static VKAPI_ATTR VkBool32 VKAPI_CALL debug_report_callback(
114 VkDebugReportFlagsEXT flags,
115 VkDebugReportObjectTypeEXT obj_type,
116 uint64_t object,
117 size_t location,
118 int32_t msg_code,
119 const char *layer_prefix,
120 const char *msg,
121 void *user_data)
122 {
123 Shell *shell = reinterpret_cast<Shell *>(user_data);
124 return shell->debug_report_callback(flags, obj_type, object, location, msg_code, layer_prefix, msg);
125 }
126
127 void assert_all_instance_layers() const;
128 void assert_all_instance_extensions() const;
129
130 bool has_all_device_layers(VkPhysicalDevice phy) const;
131 bool has_all_device_extensions(VkPhysicalDevice phy) const;
132
133 // called by init_vk
134 virtual PFN_vkGetInstanceProcAddr load_vk() = 0;
135 virtual bool can_present(VkPhysicalDevice phy, uint32_t queue_family) = 0;
136 void init_instance();
137 void init_debug_report();
138 void init_physical_dev();
139
140 // called by create_context
141 void create_dev();
142 void create_back_buffers();
143 void destroy_back_buffers();
144 virtual VkSurfaceKHR create_surface(VkInstance instance) = 0;
145 void create_swapchain();
146 void destroy_swapchain();
147
148 void fake_present();
149
150 Context ctx_;
151
152 const float game_tick_;
153 float game_time_;
154};
155
156#endif // SHELL_H