summaryrefslogtreecommitdiffstats
path: root/src/cplusplus.cpp
blob: 35876f58b05298add05541691d1af9c0545982a3 (plain) (blame)
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
// not needed yet -- add custom cpp functions first
#include "cplusplus.h"
#include "vksetup.h"

#define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS     1
#define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h"

#include <imgui.h>
#include <backends/imgui_impl_sdl2.h>
#include <backends/imgui_impl_vulkan.h>

static void check_vk_result(VkResult err)
{
  if (err == 0)
    return;
  fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err);
  if (err < 0)
    abort();
}

void
imgui_draw_cmd(VkCommandBuffer cmd)
{
  ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd);
}

void
imgui_draw()
{
  ImGui::Render();
}

void
imgui_new_frame(vks_context vk)
{
  
  ImGui_ImplVulkan_NewFrame();
  ImGui_ImplSDL2_NewFrame();

  ImGui::NewFrame();


  ImGui::ShowDemoWindow();

  ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);

}

bool
imgui_proc_event(SDL_Event* e)
{
  return ImGui_ImplSDL2_ProcessEvent(e);
}

static VkDescriptorPool imguiPool;
void
init_imgui(vks_context *vk)
{
  VkDescriptorPoolSize pool_sizes[] = {
    { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
    { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
    { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
    { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
    { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
    { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
    { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
    { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
    { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
    { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
    { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
  };

  VkDescriptorPoolCreateInfo pool_info = {};
  pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
  pool_info.maxSets = 1000;
  pool_info.poolSizeCount = std::size(pool_sizes);
  pool_info.pPoolSizes = pool_sizes;

  VK_CHECK(vkCreateDescriptorPool(vk->device, &pool_info, nullptr, &imguiPool));
  
  IMGUI_CHECKVERSION();
  ImGui::CreateContext();
  ImGuiIO& io = ImGui::GetIO();
  io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
  io.ConfigFlags |=
    ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls

  ImGui_ImplSDL2_InitForVulkan(vk->window);
  ImGui_ImplVulkan_InitInfo init_info = {};
  init_info.Instance = vk->instance;
  init_info.PhysicalDevice = vk->physical_device;
  init_info.Device = vk->device;
  //init_info.QueueFamily = ;
  init_info.Queue = vk->graphics_and_compute_queue;
  //init_info.PipelineCache = YOUR_PIPELINE_CACHE;
  init_info.DescriptorPool = imguiPool;
  init_info.Subpass = 0;
  init_info.MinImageCount = 2;
  init_info.ImageCount = 2;
  init_info.MSAASamples = vk->msaa_samples;
  init_info.CheckVkResultFn           = check_vk_result;
  init_info.UseDynamicRendering       = true;
  //init_info.
  init_info.PipelineRenderingCreateInfo.sType =
    VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR;
  init_info.PipelineRenderingCreateInfo.colorAttachmentCount    = 1;
  init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &vk->swapchain.image_format;
  init_info.PipelineRenderingCreateInfo.depthAttachmentFormat   = findDepthFormat(*vk);

  ImGui_ImplVulkan_Init(&init_info);
  // (this gets a bit more complicated, see example app for full reference)
  ImGui_ImplVulkan_CreateFontsTexture();
  // (your code submit a queue)
  ImGui_ImplVulkan_DestroyFontsTexture();
}

void
imgui_destroy(vks_context vk)
{
  ImGui_ImplVulkan_Shutdown();
  vkDestroyDescriptorPool(vk.device, imguiPool, nullptr);
}