File include/jen/graphics.h changed (mode: 100644) (index cdd29e4..4c9f25f) |
34 |
34 |
namespace jen { |
namespace jen { |
35 |
35 |
struct ModuleGraphics; |
struct ModuleGraphics; |
36 |
36 |
struct DrawFrameLoop; |
struct DrawFrameLoop; |
|
37 |
|
/// @brief Options for DebugOverlay (display some information on screen). |
|
38 |
|
struct DebugOverlaySettings { |
|
39 |
|
/// @brief Initialize with default values. |
|
40 |
|
constexpr void set_default() { |
|
41 |
|
is_visible = false; |
|
42 |
|
toggle_key = Key::Board::f1; |
|
43 |
|
p_font_path = "fonts/IBMPlexMono.ttf"; |
|
44 |
|
} |
|
45 |
|
/// Is enabled by default |
|
46 |
|
bool is_visible; |
|
47 |
|
/// Keyboard code to toggle overlay. |
|
48 |
|
Key::Board toggle_key; |
|
49 |
|
/// Path to font for displayed text, must not be nullptr. |
|
50 |
|
const char *p_font_path; |
|
51 |
|
}; |
37 |
52 |
} |
} |
38 |
53 |
/// @brief 3D Graphics renderer |
/// @brief 3D Graphics renderer |
39 |
54 |
/// ModuleGraphics can only be created and destroyed by the Framework. |
/// ModuleGraphics can only be created and destroyed by the Framework. |
|
... |
... |
struct jen::ModuleGraphics |
49 |
64 |
/// @param settings GraphicsSettings with valid values |
/// @param settings GraphicsSettings with valid values |
50 |
65 |
/// @return User optionally can check for VK_ERROR_OUT_OF_HOST_MEMORY or |
/// @return User optionally can check for VK_ERROR_OUT_OF_HOST_MEMORY or |
51 |
66 |
/// VK_ERROR_OUT_OF_DEVICE_MEMORY to free up some memory. |
/// VK_ERROR_OUT_OF_DEVICE_MEMORY to free up some memory. |
52 |
|
Result apply_settings(const GraphicsSettings &settings); |
|
|
67 |
|
Result |
|
68 |
|
apply_settings(const GraphicsSettings &settings); |
|
69 |
|
|
|
70 |
|
|
|
71 |
|
/// @brief Load overlay -- on-screen debug info. |
|
72 |
|
/// @param settings DebugOverlaySettings |
|
73 |
|
/// @return Possible values are: VK_ERROR_OUT_OF_HOST_MEMORY, |
|
74 |
|
/// vkw::ERROR_EXIST (overlay is already loaded), ERROR_FILE_OPENING. |
|
75 |
|
Result |
|
76 |
|
overlay_load(const DebugOverlaySettings &settings); |
|
77 |
|
/// @brief Destroy overlay. |
|
78 |
|
/// If overlay is not loaded, nothing will be changed. |
|
79 |
|
void |
|
80 |
|
overlay_unload(); |
|
81 |
|
|
|
82 |
|
|
53 |
83 |
/// @brief Update camera and frustum parameters |
/// @brief Update camera and frustum parameters |
54 |
|
void apply_camera(const Camera&, const Frustum&); |
|
|
84 |
|
void |
|
85 |
|
apply_camera(const Camera&, const Frustum&); |
55 |
86 |
/// @brief Update shadow-casting light. |
/// @brief Update shadow-casting light. |
56 |
87 |
/// @param l light for shadow mapping |
/// @param l light for shadow mapping |
57 |
|
void apply_light_shadow(const Light &l); |
|
|
88 |
|
void |
|
89 |
|
apply_light_shadow(const Light &l); |
58 |
90 |
/// @brief Provide pointer to light information. |
/// @brief Provide pointer to light information. |
59 |
91 |
/// Light data must not change simultaneously with function draw_frame |
/// Light data must not change simultaneously with function draw_frame |
60 |
92 |
/// (data pointed to by array LightsDraw.lights). |
/// (data pointed to by array LightsDraw.lights). |
|
... |
... |
struct jen::ModuleGraphics |
63 |
95 |
/// checked in draw_frame function and will be set to false. |
/// checked in draw_frame function and will be set to false. |
64 |
96 |
/// @param p_lights pointer to LightsDraw, must be valid pointer until |
/// @param p_lights pointer to LightsDraw, must be valid pointer until |
65 |
97 |
/// new pointer is provided or ModuleGraphics is destroyed |
/// new pointer is provided or ModuleGraphics is destroyed |
66 |
|
void apply_lights(LightsDraw *p_lights); |
|
|
98 |
|
void |
|
99 |
|
apply_lights(LightsDraw *p_lights); |
67 |
100 |
/// @brief Run data to GPU writing process and acquire GpuData memory handle. |
/// @brief Run data to GPU writing process and acquire GpuData memory handle. |
68 |
101 |
/// User memory under WriteData.p ptr must be valid when the writing process |
/// User memory under WriteData.p ptr must be valid when the writing process |
69 |
102 |
/// is occurs. To check writtings process status, use is_resource_ready. |
/// is occurs. To check writtings process status, use is_resource_ready. |
|
... |
... |
struct jen::ModuleGraphics |
76 |
109 |
/// @param free_src If this flag is set to true, WriteData.p memory will |
/// @param free_src If this flag is set to true, WriteData.p memory will |
77 |
110 |
/// be deallocated in the end of the writing proccess |
/// be deallocated in the end of the writing proccess |
78 |
111 |
/// @return If not VK_SUCCESS then nothing is done |
/// @return If not VK_SUCCESS then nothing is done |
|
112 |
|
|
|
113 |
|
|
79 |
114 |
[[nodiscard]] Result |
[[nodiscard]] Result |
80 |
115 |
create(const WriteData &w, GpuData **pp_dst, bool free_src); |
create(const WriteData &w, GpuData **pp_dst, bool free_src); |
81 |
116 |
/// @brief Run data to GPU writing process and write GpuData to user memory. |
/// @brief Run data to GPU writing process and write GpuData to user memory. |
|
... |
... |
struct jen::ModuleGraphics |
87 |
122 |
/// @see create(const WriteData&, GpuData**, bool) |
/// @see create(const WriteData&, GpuData**, bool) |
88 |
123 |
[[nodiscard]] Result |
[[nodiscard]] Result |
89 |
124 |
create(const WriteData& w, GpuData *p_allocated, bool free_src); |
create(const WriteData& w, GpuData *p_allocated, bool free_src); |
|
125 |
|
/// @brief Free memory and GpuData handle. |
|
126 |
|
/// GpuData handle becomes invalid after freeing |
|
127 |
|
/// @param p Valid GpuData handle, acquired from ModuleGraphics::create |
|
128 |
|
/// @param free_src If set to true, specified in ModuleGraphics::create |
|
129 |
|
/// WriteData.p memory will be deallocated |
|
130 |
|
/// (no effect if it is already deallocated after create) |
|
131 |
|
void |
|
132 |
|
destroy(GpuData *p, bool free_src); |
|
133 |
|
|
|
134 |
|
|
90 |
135 |
/// @brief Run gpu texture creation process and acquire GpuTexture handle. |
/// @brief Run gpu texture creation process and acquire GpuTexture handle. |
91 |
136 |
/// User memory under jrf::Image.p_pixels pointer must be valid when the |
/// User memory under jrf::Image.p_pixels pointer must be valid when the |
92 |
137 |
/// texture creation process occurs. To check process status, |
/// texture creation process occurs. To check process status, |
|
... |
... |
struct jen::ModuleGraphics |
101 |
146 |
/// be destroyed in the end if the writing proccess |
/// be destroyed in the end if the writing proccess |
102 |
147 |
[[nodiscard]] Result |
[[nodiscard]] Result |
103 |
148 |
create(const jrf::Image &texture, GpuTexture **pp_dst, bool free_src); |
create(const jrf::Image &texture, GpuTexture **pp_dst, bool free_src); |
104 |
|
/// @brief Free memory and GpuData handle. |
|
105 |
|
/// GpuData handle becomes invalid after freeing |
|
106 |
|
/// @param p Valid GpuData handle, acquired from ModuleGraphics::create |
|
107 |
|
/// @param free_src If set to true, specified in ModuleGraphics::create |
|
108 |
|
/// WriteData.p memory will be deallocated |
|
109 |
|
/// (no effect if it is already deallocated after create) |
|
110 |
|
void destroy(GpuData *p, bool free_src); |
|
111 |
149 |
/// @brief Free memory and GpuTexture handle. |
/// @brief Free memory and GpuTexture handle. |
112 |
150 |
/// GpuTexture handle becomes invalid after freeing |
/// GpuTexture handle becomes invalid after freeing |
113 |
151 |
/// @param p Valid GpuTexture handle, acquired from ModuleGraphics::create |
/// @param p Valid GpuTexture handle, acquired from ModuleGraphics::create |
114 |
152 |
/// @param free_src If set to true, specified in ModuleGraphics::create |
/// @param free_src If set to true, specified in ModuleGraphics::create |
115 |
153 |
/// jrf::Image will be destroyed |
/// jrf::Image will be destroyed |
116 |
154 |
/// (no effect if it is already destroyed after create) |
/// (no effect if it is already destroyed after create) |
117 |
|
void destroy(GpuTexture *p, bool free_src); |
|
|
155 |
|
void |
|
156 |
|
destroy(GpuTexture *p, bool free_src); |
|
157 |
|
|
118 |
158 |
|
|
119 |
159 |
/// @brief Create GlyphManager handle with font from file. |
/// @brief Create GlyphManager handle with font from file. |
120 |
160 |
/// @param font_path path to font file with |
/// @param font_path path to font file with |
|
... |
... |
struct jen::ModuleGraphics |
123 |
163 |
/// @return true if success, else nothing is created |
/// @return true if success, else nothing is created |
124 |
164 |
[[nodiscard]] bool |
[[nodiscard]] bool |
125 |
165 |
create(const char* font_path, GlyphManager **pp_dst); |
create(const char* font_path, GlyphManager **pp_dst); |
126 |
|
|
|
127 |
166 |
/// @brief Create or update text on the gpu. |
/// @brief Create or update text on the gpu. |
128 |
167 |
/// After this function call this text will be drawn every frame untill |
/// After this function call this text will be drawn every frame untill |
129 |
168 |
/// ModuleGraphics::destroy(GpuText*) is called with this GpuText handle. |
/// ModuleGraphics::destroy(GpuText*) is called with this GpuText handle. |
|
... |
... |
struct jen::ModuleGraphics |
145 |
184 |
/// @brief Stop gpu text drawing and free GpuText handle. |
/// @brief Stop gpu text drawing and free GpuText handle. |
146 |
185 |
/// After this call, GpuText handle cant be used again for text_update |
/// After this call, GpuText handle cant be used again for text_update |
147 |
186 |
/// @param p_text Text to free |
/// @param p_text Text to free |
148 |
|
void destroy(GpuText *p_text); |
|
|
187 |
|
void |
|
188 |
|
destroy(GpuText *p_text); |
149 |
189 |
/// @brief Destroy GlyphManager handle and font data. |
/// @brief Destroy GlyphManager handle and font data. |
150 |
190 |
/// GlyphManager handle is freed and invalid after this function. |
/// GlyphManager handle is freed and invalid after this function. |
151 |
191 |
/// @param p_font Valid GlyphManager handle, all GpuText handles created |
/// @param p_font Valid GlyphManager handle, all GpuText handles created |
152 |
192 |
/// with this GlyphManager handle must be destroyed before. |
/// with this GlyphManager handle must be destroyed before. |
153 |
|
void destroy(GlyphManager *p_font); |
|
|
193 |
|
void |
|
194 |
|
destroy(GlyphManager *p_font); |
|
195 |
|
|
|
196 |
|
|
154 |
197 |
/// @brief Acquire swapchain image, draw on it and present to window's surface |
/// @brief Acquire swapchain image, draw on it and present to window's surface |
155 |
198 |
/// @param models Constant array of models to use in rendering operation |
/// @param models Constant array of models to use in rendering operation |
156 |
199 |
/// @return If not VK_SUCCESS then frame is not presented |
/// @return If not VK_SUCCESS then frame is not presented |
157 |
200 |
[[nodiscard]] Result |
[[nodiscard]] Result |
158 |
201 |
draw_frame(const jl::rarray<const Model> &models); |
draw_frame(const jl::rarray<const Model> &models); |
|
202 |
|
|
|
203 |
|
|
159 |
204 |
struct Data; |
struct Data; |
160 |
205 |
/// @brief Pointer to hidden data of ModuleGraphics. |
/// @brief Pointer to hidden data of ModuleGraphics. |
161 |
206 |
/// Because of this pointer, ModuleGraphics can be shared by value. |
/// Because of this pointer, ModuleGraphics can be shared by value. |
File src/graphics/graphics_interface.cpp changed (mode: 100644) (index 513494c..b84e5f1) |
... |
... |
init(Instance *p_inst, Device *p_dev, const GraphicsSettings &setts) |
74 |
74 |
goto EXIT; |
goto EXIT; |
75 |
75 |
} |
} |
76 |
76 |
|
|
77 |
|
if (settings.debug_overlay.is_enabled) { |
|
78 |
|
if (not jl::allocate(&p_debug_overlay)) { |
|
79 |
|
destroy(6); |
|
80 |
|
goto EXIT; |
|
81 |
|
} |
|
82 |
|
res = p_debug_overlay->init({this}, settings.debug_overlay.font_path); |
|
83 |
|
if (res != VK_SUCCESS) { |
|
84 |
|
destroy(7); |
|
85 |
|
goto EXIT; |
|
86 |
|
} |
|
87 |
|
} |
|
88 |
|
else p_debug_overlay = nullptr; |
|
|
77 |
|
p_debug_overlay = nullptr; |
89 |
78 |
EXIT: |
EXIT: |
90 |
79 |
if (res != VK_SUCCESS) |
if (res != VK_SUCCESS) |
91 |
80 |
fprintf(stderr, "Graphics module loading failed %s\n", vkw::to_string(res)); |
fprintf(stderr, "Graphics module loading failed %s\n", vkw::to_string(res)); |
|
... |
... |
void ModuleGraphics::Data:: |
100 |
89 |
destroy(int code) |
destroy(int code) |
101 |
90 |
{ |
{ |
102 |
91 |
(void)p_device->device.wait_idle(); |
(void)p_device->device.wait_idle(); |
|
92 |
|
ModuleGraphics{this}.overlay_unload(); |
103 |
93 |
|
|
104 |
94 |
switch(code) |
switch(code) |
105 |
95 |
{ |
{ |
106 |
96 |
default: |
default: |
107 |
|
[[fallthrough]]; case 8: if (p_debug_overlay != nullptr) |
|
108 |
|
p_debug_overlay->destroy({this}); |
|
109 |
|
[[fallthrough]]; case 7: if (p_debug_overlay != nullptr) |
|
110 |
|
jl::deallocate(&p_debug_overlay); |
|
111 |
97 |
[[fallthrough]]; case 6: draw_data.destroy(); |
[[fallthrough]]; case 6: draw_data.destroy(); |
112 |
98 |
[[fallthrough]]; case 5: destroyer.destroy(); |
[[fallthrough]]; case 5: destroyer.destroy(); |
113 |
99 |
[[fallthrough]]; case 4: gpu_transfer.join(); |
[[fallthrough]]; case 4: gpu_transfer.join(); |
|
... |
... |
apply_settings(const GraphicsSettings &settings) { |
214 |
200 |
return VK_SUCCESS; |
return VK_SUCCESS; |
215 |
201 |
} |
} |
216 |
202 |
|
|
|
203 |
|
|
|
204 |
|
[[nodiscard]] Result ModuleGraphics:: |
|
205 |
|
overlay_load(const DebugOverlaySettings &settings) { |
|
206 |
|
if (p->p_debug_overlay != nullptr) |
|
207 |
|
return vkw::ERROR_EXIST; |
|
208 |
|
if (not jl::allocate(&p->p_debug_overlay)) |
|
209 |
|
return VK_ERROR_OUT_OF_HOST_MEMORY; |
|
210 |
|
Result res; |
|
211 |
|
res = p->p_debug_overlay->init(*this, settings); |
|
212 |
|
if (res != VK_SUCCESS) { |
|
213 |
|
jl::deallocate(&p->p_debug_overlay); |
|
214 |
|
return res; |
|
215 |
|
} |
|
216 |
|
return VK_SUCCESS; |
|
217 |
|
} |
|
218 |
|
void ModuleGraphics:: |
|
219 |
|
overlay_unload() { |
|
220 |
|
if (p->p_debug_overlay == nullptr) |
|
221 |
|
return; |
|
222 |
|
p->p_debug_overlay->destroy(*this); |
|
223 |
|
jl::deallocate(&p->p_debug_overlay); |
|
224 |
|
} |
|
225 |
|
|
|
226 |
|
|
217 |
227 |
jen::ResourceState jen::resource_state(const jen::GpuData * const p) { |
jen::ResourceState jen::resource_state(const jen::GpuData * const p) { |
218 |
228 |
return p->state; |
return p->state; |
219 |
229 |
} |
} |
|
... |
... |
run(ModuleGraphics mg, void *p_update_arg, void(*pf_update)(void*)) |
363 |
373 |
elapsed_after_update = last_update_time.elapsed(update_time); |
elapsed_after_update = last_update_time.elapsed(update_time); |
364 |
374 |
last_update_time = update_time; |
last_update_time = update_time; |
365 |
375 |
|
|
366 |
|
if (window.is_damaged || (not pause)) { |
|
|
376 |
|
if (not pause or window.is_damaged) { |
367 |
377 |
pf_update(p_update_arg); |
pf_update(p_update_arg); |
368 |
378 |
if (break_loop) |
if (break_loop) |
369 |
379 |
break; |
break; |
370 |
|
|
|
371 |
380 |
if (not draw) |
if (not draw) |
372 |
381 |
continue; |
continue; |
373 |
|
|
|
374 |
382 |
result = mg.draw_frame(models); |
result = mg.draw_frame(models); |
375 |
383 |
is_drawn = result == VK_SUCCESS; |
is_drawn = result == VK_SUCCESS; |
376 |
384 |
} |
} |