summaryrefslogtreecommitdiff
path: root/src/core.cpp
blob: 8d828d6ad4ce75f4eb3b816cd29dc6ba59e2f430 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * Copyright 2022 Frederico de Oliveira Linhares
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "core.hpp"

#ifdef DEBUG
#include <sstream>
#endif

#include <yaml-cpp/yaml.h>

std::random_device random_seed;
std::mt19937 random_number_generator;

#ifdef DEBUG
static VKAPI_ATTR VkBool32 VKAPI_CALL
vk_debug_callback(
  VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
  VkDebugUtilsMessageTypeFlagsEXT message_type,
  const VkDebugUtilsMessengerCallbackDataEXT* callback_data,
  void* _obj)
{
  // Set level.
  Log::Level log_level;
  if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT)
    log_level = Log::Level::Trace;
  else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
    log_level = Log::Level::Information;
  else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
    log_level = Log::Level::Warning;
  else if(message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
    log_level = Log::Level::Error;

  // Log message.
  cg_core.log.message(log_level, callback_data->pMessage);

  return VK_FALSE;
}
#endif

static void
load_variables(void *obj)
{
  using namespace std::chrono;

  YAML::Node root = YAML::LoadFile(cg_core.config_file);

  std::string game_name = root["name"].as<std::string>().c_str();
  cg_core.game_name = new char[game_name.size() + 1];
  strcpy(cg_core.game_name, game_name.c_str());

  cg_core.screen_width = root["screen"]["width"].as<int>();
  cg_core.screen_height = root["screen"]["height"].as<int>();

  cg_core.game_version_major = 0;
  cg_core.game_version_minor = 1;
  cg_core.game_version_patch = 0;

  cg_core.fps = 30;
  cg_core.max_frame_duration =
    duration<long long, std::milli>(1000 / cg_core.fps);
}

static void
unload_variables(void *obj)
{
  delete[] cg_core.game_name;
}

static void
load_sdl(void *obj)
{
  if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
  {
    std::string error{"SDL could not initialize! SDL Error → "};
    error += SDL_GetError();
    throw error;
  }

  if(SDL_Vulkan_LoadLibrary(nullptr) != 0)
  {
    SDL_Quit();
    std::string error{"SDL could not initialize Vulkan! SDL_Error → "};
    error += SDL_GetError();
    throw CommandError{error};
  }
}

static void
unload_sdl(void *obj)
{
  SDL_Vulkan_UnloadLibrary();
  SDL_Quit();
}

static void
load_sdl_image(void *obj)
{
  int flags = IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF;
  if(!(IMG_Init(flags) & flags))
  {
    std::string error{"Could not initialize SDL image → "};
    error += IMG_GetError();
    throw CommandError{error};
  }
}

static void
unload_sdl_image(void *obj)
{
  IMG_Quit();
}

static void
load_sdl_mixer(void *obj)
{
  int flags = MIX_INIT_OGG|MIX_INIT_MOD;
  int initted = Mix_Init(flags);
  if(initted&flags != flags)
  {
    std::string error{"Could not initialize SDL mixer → "};
    error += Mix_GetError();
    throw CommandError{error};
  }
}

static void
unload_sdl_mixer(void *obj)
{
  while(Mix_Init(0)) Mix_Quit();
}

static void
load_sdl_open_audio(void *obj)
{
  if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) == -1)
  {
    std::string error{"Could not open SDL mixer audio → "};
    error += Mix_GetError();
    throw CommandError{error};
  }
}

static void
unload_sdl_open_audio(void *obj)
{
  Mix_CloseAudio();
}

static void
load_window(void *obj)
{
  cg_core.window = NULL;
  cg_core.window = SDL_CreateWindow(
    cg_core.game_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
    cg_core.screen_width, cg_core.screen_height, SDL_WINDOW_VULKAN);
  if(cg_core.window == NULL)
  {
    std::string error{"Window could not be created! SDL_Error → "};
    error += SDL_GetError();
    throw CommandError{error};
  }
}

static void
unload_window(void *obj)
{
  SDL_DestroyWindow(cg_core.window);
}

static void
load_vk_instance(void *obj)
{
  std::vector<const char*> vk_extensions;
  std::vector<const char*> vk_required_layers_names;

  // Get extensions.
  {
    uint32_t vk_extensions_count;
    std::vector<const char*> vk_required_extensions;

    // Load debuging layers.
#ifdef DEBUG
    vk_required_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
    vk_required_layers_names.push_back("VK_LAYER_KHRONOS_validation");
#endif

    // Get extensions for SDL.
    {
      uint32_t vk_sdl_extension_count;
      std::vector<const char*> vk_sdl_extensions;

      if(!SDL_Vulkan_GetInstanceExtensions(
	   cg_core.window, &vk_sdl_extension_count, nullptr))
      {
	std::string error{
	  "Vulkan extensions could not be loaded by SDL! SDL_Error: "};
	error += SDL_GetError();
	throw CommandError{error};
      }

      vk_sdl_extensions.resize(vk_sdl_extension_count);
      SDL_Vulkan_GetInstanceExtensions(
	cg_core.window, &vk_sdl_extension_count, vk_sdl_extensions.data());

      // Combine all extensions.
      vk_extensions_count = vk_sdl_extension_count +
	vk_required_extensions.size();
      vk_extensions.resize(vk_extensions_count);
      for(auto i{0}; i < vk_sdl_extension_count; i++)
	vk_extensions[i] = vk_sdl_extensions[i];
      for(auto i{0}; i < vk_required_extensions.size(); i++)
	vk_extensions[i + vk_sdl_extension_count] = vk_required_extensions[i];
    }

#ifdef DEBUG
    cg_core.log.message(Log::Level::Trace, "Enabled VK extensions.");
    for(auto vk_extension: vk_extensions)
    {
      std::string message{"Extension name: "};
      message += vk_extension;
      cg_core.log.message(Log::Level::Trace, message);
    }
#endif
  }

  // Get available instance layers.
  {
    uint32_t vk_available_layers_count;
    std::vector<VkLayerProperties> vk_available_layers;
    std::vector<const char*> vk_available_layers_names;

    vkEnumerateInstanceLayerProperties(&vk_available_layers_count, nullptr);
    vk_available_layers.resize(vk_available_layers_count);
    vkEnumerateInstanceLayerProperties(&vk_available_layers_count,
				       vk_available_layers.data());
    vk_available_layers_names.resize(vk_available_layers_count);
#ifdef DEBUG
    cg_core.log.message(Log::Level::Trace, "Available VK instance layers.");
#endif
    for(uint32_t i = 0; i < vk_available_layers_count; i++)
    {
#ifdef DEBUG
      std::stringstream message{};
      message << "\nname: " << vk_available_layers[i].layerName << std::endl;
      message << "Description: " << vk_available_layers[i].description <<
	std::endl;
      message << "Spec version: " << vk_available_layers[i].specVersion <<
	std::endl;
      message << "Implementation version: " <<
	vk_available_layers[i].implementationVersion << std::endl;
      cg_core.log.message(Log::Level::Trace, message.str());
#endif

      vk_available_layers_names[i] = vk_available_layers[i].layerName;
    }

    // If required layers are not all available.
    if(!std::includes(
	 vk_available_layers_names.begin(), vk_available_layers_names.end(),
	 vk_required_layers_names.begin(), vk_required_layers_names.begin()))
      throw CommandError{"Some required Vulkan layers are not available."};
  }

  {
    VkApplicationInfo app_info;
    app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    app_info.pNext = nullptr;
    app_info.pApplicationName = cg_core.game_name;
    app_info.applicationVersion = VK_MAKE_VERSION(
      cg_core.game_version_major,
      cg_core.game_version_minor,
      cg_core.game_version_patch);
    app_info.pEngineName = "CandyGear";
    app_info.engineVersion = VK_MAKE_VERSION(
      CANDY_GEAR_VERSION_MAJOR,
      CANDY_GEAR_VERSION_MINOR,
      CANDY_GEAR_VERSION_PATCH);
    app_info.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo create_info;
    create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    create_info.pNext = nullptr;
    create_info.flags = 0;
    create_info.pApplicationInfo = &app_info;
    create_info.enabledExtensionCount = vk_extensions.size();
    create_info.ppEnabledExtensionNames = vk_extensions.data();
    create_info.enabledLayerCount = vk_required_layers_names.size();
    create_info.ppEnabledLayerNames = vk_required_layers_names.data();

    VkResult result =
      vkCreateInstance(&create_info, nullptr, &cg_core.vk_instance);
    if(result != VK_SUCCESS)
    {
      std::string error{""};
      switch(result)
      {
      case VK_ERROR_LAYER_NOT_PRESENT:
	error = " Layer not present.";
	break;
      case VK_ERROR_EXTENSION_NOT_PRESENT:
	error = " Extension not present.";
      }
      error = "Failed to create Vulkan instance." + error;
      throw CommandError{error};
    }
  }
}

static void
unload_vk_instance(void *obj)
{
  vkDestroyInstance(cg_core.vk_instance, nullptr);
}

static void
load_window_surface(void *obj)
{
  if(!SDL_Vulkan_CreateSurface(
       cg_core.window, cg_core.vk_instance, &cg_core.window_surface))
  {
    std::string error{"Failed to create a window surface → "};
    error += SDL_GetError();
    throw CommandError{error};
  }
}

static void
unload_window_surface(void *obj)
{
  vkDestroySurfaceKHR(cg_core.vk_instance, cg_core.window_surface, nullptr);
}

#ifdef DEBUG
static void
load_vk_debug_callback(void *obj)
{
  PFN_vkCreateDebugUtilsMessengerEXT debug_messenger;

  // A Vulkan instance extension named VK_EXT_debug_utils and a Vulkan instance
  // layer named VK_LAYER_LUNARG_standard_validation are required to enable
  // this callback. These instance extension and instance layer are loaded at
  // Instance::load_vk_instance.
  VkDebugUtilsMessengerCreateInfoEXT create_info;
  create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  create_info.pNext = nullptr;
  create_info.messageSeverity =
      VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
      VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
      VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
      VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
  create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
			    VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
			    VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
  create_info.pfnUserCallback = vk_debug_callback;
  create_info.pUserData = nullptr;
  create_info.flags = 0;

  debug_messenger = (PFN_vkCreateDebugUtilsMessengerEXT)
    vkGetInstanceProcAddr(cg_core.vk_instance,
			  "vkCreateDebugUtilsMessengerEXT");

  if(debug_messenger(cg_core.vk_instance, &create_info, nullptr,
		     &cg_core.vk_callback) != VK_SUCCESS)
    CommandError{"Failed to setup debug callback for Vulkan."};
}

static void
unload_vk_debug_callback(void *obj)
{
  PFN_vkDestroyDebugUtilsMessengerEXT debug_messenger;

  debug_messenger = (PFN_vkDestroyDebugUtilsMessengerEXT)
    vkGetInstanceProcAddr(cg_core.vk_instance,
			  "vkDestroyDebugUtilsMessengerEXT");

  debug_messenger(cg_core.vk_instance, cg_core.vk_callback, nullptr);
}
#endif

static void
load_vk_devices(void *obj)
{
  uint32_t devices_count;
  std::vector<VkPhysicalDevice> vk_physical_devices;

  // Enumerate physical devices
  {
    vkEnumeratePhysicalDevices(cg_core.vk_instance, &devices_count, nullptr);
    if(devices_count < 1)
      CommandError{"Failed to find GPUs with Vulkan support."};
    vk_physical_devices.resize(devices_count);
    vkEnumeratePhysicalDevices(
      cg_core.vk_instance, &devices_count, vk_physical_devices.data());
  }

#ifdef DEBUG
  cg_core.log.message(Log::Level::Trace, "Physical devices properties");
#endif

  cg_core.vk_devices.reserve(devices_count);
  for(auto i = 0; i < devices_count; i++)
  {
    // Use swapchain on first device.
    if(i == 0)
    {
      cg_core.vk_devices.emplace_back(vk_physical_devices[i], true);
      cg_core.vk_device_with_swapchain = &cg_core.vk_devices[i];
    }
    else
      cg_core.vk_devices.emplace_back(vk_physical_devices[i], false);
  }
}

static void
unload_vk_devices(void *obj)
{
  cg_core.vk_devices.clear();
}

static void
load_vk_swapchain(void *obj)
{
  try { cg_core.vk_swapchain = new VK::Swapchain(); }
  catch(const CommandError &error)
  {
    std::string error_message{"Failed to create swapchain → "};
    error_message += error.what();
    throw CommandError{error_message};
  }
}

static void
unload_vk_swapchain(void *obj)
{
  delete cg_core.vk_swapchain;
}

static void
load_vk_graphics_pipeline_3d_layout(void *obj)
{
  try
  {
    cg_core.vk_graphics_pipeline_3d_layout =
      new VK::GraphicsPipeline3DLayout();
  }
  catch(const CommandError &e)
  {
    throw CommandError{"Failed to create 3d graphics pipeline."};
  }
}

static void
unload_vk_graphics_pipeline_3d_layout(void *obj)
{
  delete cg_core.vk_graphics_pipeline_3d_layout;
}

static void
load_vk_graphics_pipeline_2d_layout(void *obj)
{
  try
  {
    cg_core.vk_graphics_pipeline_2d_layout =
      new VK::GraphicsPipeline2DLayout();
  }
  catch(const CommandError &e)
  {
    throw CommandError{"Failed to create 2d graphics pipeline."};
  }
}

static void
unload_vk_graphics_pipeline_2d_layout(void *obj)
{
  delete cg_core.vk_graphics_pipeline_2d_layout;
}

void
load_vk_graphics_pipeline_3d(void *obj)
{
  try
  {
    cg_core.vk_graphics_pipeline_3d =
      std::make_unique<VK::GraphicsPipeline3D>();
  }
  catch(const CommandError &e)
  {
    throw CommandError{"Failed to create 3d graphics pipeline."};
  }
}

void
unload_vk_graphics_pipeline_3d(void *obj)
{
  cg_core.vk_graphics_pipeline_3d = nullptr;
}

void
load_vk_graphics_pipeline_2d(void *obj)
{
  try
  {
    cg_core.vk_graphics_pipeline_2d =
      std::make_unique<VK::GraphicsPipeline2D>();
  }
  catch(const CommandError &e)
  {
    throw CommandError{"Failed to create 2d graphics pipeline."};
  }
}

void
unload_vk_graphics_pipeline_2d(void *obj)
{
  cg_core.vk_graphics_pipeline_2d = nullptr;
}

static void
load_vk_renderer(void *obj)
{
  try
  {
    glm::vec4 region(
      0.f, 0.f,
      static_cast<float>(cg_core.screen_width),
      static_cast<float>(cg_core.screen_height));
    cg_core.vk_renderer = new VK::Renderer(
      {},
      {std::make_shared<VK::View3D>(region)});
  }
  catch(const CommandError &e)
  {
    throw CommandError{"Failed to create renderer."};
  }
}

static void
unload_vk_renderer(void *obj)
{
  delete cg_core.vk_renderer;
}

static void
load_mruby(void *obj)
{
  cg_core.mrb = mrb_open();
  if (!cg_core.mrb) throw CommandError{"Failed to initialize mruby."};
}

static void
unload_mruby(void *obj)
{
  mrb_close(cg_core.mrb);
}

static void
load_mruby_symbols(void *obj)
{
  cg_core.sym_fatal = mrb_intern_cstr(cg_core.mrb, "fatal");
  cg_core.sym_error = mrb_intern_cstr(cg_core.mrb, "error");
  cg_core.sym_warning = mrb_intern_cstr(cg_core.mrb, "warning");
  cg_core.sym_information = mrb_intern_cstr(cg_core.mrb, "information");
  cg_core.sym_debug = mrb_intern_cstr(cg_core.mrb, "debug");
  cg_core.sym_trace = mrb_intern_cstr(cg_core.mrb, "trace");
}

const CommandChain cg_sCore::loader{
  {&load_variables, &unload_variables},
  {&load_sdl, &unload_sdl},
  {&load_sdl_image, &unload_sdl_image},
  {&load_sdl_mixer, &unload_sdl_mixer},
  {&load_sdl_open_audio, &unload_sdl_open_audio},
  {&load_window, &unload_window},
  {&load_vk_instance, &unload_vk_instance},
  {&load_window_surface, &unload_window_surface},
#ifdef DEBUG
  {&load_vk_debug_callback, &unload_vk_debug_callback},
#endif
  {&load_vk_devices, &unload_vk_devices},
  {&load_vk_swapchain, &unload_vk_swapchain},

  {&load_vk_graphics_pipeline_3d_layout,
   &unload_vk_graphics_pipeline_3d_layout},
  {&load_vk_graphics_pipeline_2d_layout,
   &unload_vk_graphics_pipeline_2d_layout},
  {&load_vk_graphics_pipeline_3d, &unload_vk_graphics_pipeline_3d},
  {&load_vk_graphics_pipeline_2d, &unload_vk_graphics_pipeline_2d},

  {&load_vk_renderer, &unload_vk_renderer},
  {&load_mruby, &unload_mruby},
  {&load_mruby_symbols, nullptr}
};