~sircmpwn/xrgears

ref: a63ec76c08695cbc0735573656741035adc9fe4d xrgears/vkc/vkcRenderer.cpp -rw-r--r-- 10.8 KiB
a63ec76c — Lubosz Sarnecki vkc: move frame buffers to renderer. 4 years ago
                                                                                
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
#include <assert.h>
#include <sys/time.h>

#include <array>

#include "vkcRenderer.hpp"

namespace vkc {

Renderer::Renderer(uint32_t w, uint32_t h) : width(w), height(h) {
  gettimeofday(&start_tv, NULL);
}

Renderer::~Renderer() {
}

void Renderer::init_vk(const char *extension) {
  VkApplicationInfo appinfo = {};
  appinfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  appinfo.pApplicationName = "vkcube";
  appinfo.apiVersion = VK_MAKE_VERSION(1, 0, 2);

  const char * names[2] = {
    VK_KHR_SURFACE_EXTENSION_NAME,
    extension,
  };

  VkInstanceCreateInfo instanceinfo = {};
  instanceinfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  instanceinfo.pApplicationInfo = &appinfo;
  instanceinfo.enabledExtensionCount = extension ? (uint32_t) 2 : 0;
  instanceinfo.ppEnabledExtensionNames = names;

  vkCreateInstance(&instanceinfo,
                   NULL,
                   &instance);

  uint32_t count;
  vkEnumeratePhysicalDevices(instance, &count, NULL);
  VkPhysicalDevice pd[count];
  vkEnumeratePhysicalDevices(instance, &count, pd);
  physical_device = pd[0];
  vik_log_d("%d physical devices", count);

  VkPhysicalDeviceProperties properties;
  vkGetPhysicalDeviceProperties(physical_device, &properties);
  vik_log_i("vendor id %04x, device name %s",
         properties.vendorID, properties.deviceName);

  vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &count, NULL);
  assert(count > 0);
  VkQueueFamilyProperties props[count];
  vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &count, props);
  assert(props[0].queueFlags & VK_QUEUE_GRAPHICS_BIT);

  float prois[] = { 1.0f };

  VkDeviceQueueCreateInfo queueinfo = {};
  queueinfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  queueinfo.queueFamilyIndex = 0;
  queueinfo.queueCount = 1;
  queueinfo.pQueuePriorities = prois;

  const char * const extnames[] = {
    VK_KHR_SWAPCHAIN_EXTENSION_NAME,
  };

  VkDeviceCreateInfo deviceInfo = {};
  deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  deviceInfo.queueCreateInfoCount = 1;
  deviceInfo.pQueueCreateInfos = &queueinfo;
  deviceInfo.enabledExtensionCount = 1;
  deviceInfo.ppEnabledExtensionNames = extnames;

  vkCreateDevice(physical_device,
                 &deviceInfo,
                 NULL,
                 &device);

  vkGetDeviceQueue(device, 0, 0, &queue);
}

void Renderer::create_vulkan_swapchain() {
  vik_log_d("Creating vk swapchain");
  swap_chain = new SwapChainVK();
  SwapChainVK* sc = (SwapChainVK*) swap_chain;
  sc->create(device, physical_device, surface, image_format, width, height);
  sc->update_images(device, image_format);
  create_frame_buffers();
}

VkFormat Renderer::choose_surface_format() {
  uint32_t num_formats = 0;
  vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                       &num_formats, NULL);
  assert(num_formats > 0);

  VkSurfaceFormatKHR formats[num_formats];

  vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                       &num_formats, formats);

  VkFormat format = VK_FORMAT_UNDEFINED;
  for (int i = 0; i < num_formats; i++) {
    switch (formats[i].format) {
      case VK_FORMAT_R8G8B8A8_SRGB:
      case VK_FORMAT_B8G8R8A8_SRGB:
        /* These formats are all fine */
        format = formats[i].format;
        break;
      case VK_FORMAT_R8G8B8_SRGB:
      case VK_FORMAT_B8G8R8_SRGB:
      case VK_FORMAT_R5G6B5_UNORM_PACK16:
      case VK_FORMAT_B5G6R5_UNORM_PACK16:
        /* We would like to support these but they don't seem to work. */
      default:
        continue;
    }
  }

  assert(format != VK_FORMAT_UNDEFINED);

  return format;
}

void Renderer::init_render_pass() {

  std::array<VkAttachmentDescription, 1> attachments = {};
  attachments[0].format = image_format;
  attachments[0].samples = (VkSampleCountFlagBits) 1;
  attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
  attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

  std::array<VkAttachmentReference, 1> color_attachments = {};
  color_attachments[0].attachment = 0;
  color_attachments[0].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

  std::array<VkAttachmentReference, 1> resolve_attachments = {};
  resolve_attachments[0].attachment = VK_ATTACHMENT_UNUSED;
  resolve_attachments[0].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

  uint32_t presereve_attachments[] = { 0 };

  std::array<VkSubpassDescription, 1> sub_pass_desc = {};
  sub_pass_desc[0].pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  sub_pass_desc[0].inputAttachmentCount = 0;
  sub_pass_desc[0].colorAttachmentCount = 1;
  sub_pass_desc[0].pColorAttachments = color_attachments.data();
  sub_pass_desc[0].pResolveAttachments = resolve_attachments.data();
  sub_pass_desc[0].pDepthStencilAttachment = NULL;
  sub_pass_desc[0].preserveAttachmentCount = 1;
  sub_pass_desc[0].pPreserveAttachments = presereve_attachments;

  VkRenderPassCreateInfo render_pass_create_info = {};
  render_pass_create_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  render_pass_create_info.attachmentCount = 1;
  render_pass_create_info.pAttachments = attachments.data();
  render_pass_create_info.subpassCount = 1;
  render_pass_create_info.pSubpasses = sub_pass_desc.data();
  render_pass_create_info.dependencyCount = 0;

  vkCreateRenderPass(device,
                     &render_pass_create_info,
                     NULL,
                     &render_pass);
}

void Renderer::init_vk_objects() {
  VkFenceCreateInfo fenceinfo = {};
  fenceinfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  fenceinfo.flags = 0;

  vkCreateFence(device,
                &fenceinfo,
                NULL,
                &fence);

  VkCommandPoolCreateInfo commandpoolinfo = {};
  commandpoolinfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  commandpoolinfo.queueFamilyIndex = 0;
  commandpoolinfo.flags = 0;

  vkCreateCommandPool(device,
                      &commandpoolinfo,
                      NULL,
                      &cmd_pool);

  VkSemaphoreCreateInfo semaphoreinfo = {};
  semaphoreinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;

  vkCreateSemaphore(device,
                    &semaphoreinfo,
                    NULL,
                    &semaphore);
}

void Renderer::submit_queue() {
  VkPipelineStageFlags stageflags[] = {
    VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  };

  VkSubmitInfo submitInfo = {};
  submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  submitInfo.waitSemaphoreCount = 1;
  submitInfo.pWaitSemaphores = &semaphore;
  submitInfo.pWaitDstStageMask = stageflags;
  submitInfo.commandBufferCount = 1;
  submitInfo.pCommandBuffers = &cmd_buffer;

  vkQueueSubmit(queue, 1, &submitInfo, fence);
}

static uint64_t get_ms_from_tv(const timeval& tv) {
  return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

uint64_t Renderer::get_animation_time() {
  timeval tv;
  gettimeofday(&tv, NULL);
  return (get_ms_from_tv(tv) - get_ms_from_tv(start_tv)) / 5;
}

void Renderer::wait_and_reset_fences() {
  VkFence fences[] = { fence };
  vkWaitForFences(device, 1, fences, VK_TRUE, INT64_MAX);
  vkResetFences(device, 1, &fence);
  vkResetCommandPool(device, cmd_pool, 0);
}

void Renderer::build_command_buffer(VkFramebuffer frame_buffer) {
  VkCommandBufferAllocateInfo cmdBufferAllocateInfo = {};
  cmdBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  cmdBufferAllocateInfo.commandPool = cmd_pool;
  cmdBufferAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  cmdBufferAllocateInfo.commandBufferCount = 1;

  VkResult r = vkAllocateCommandBuffers(device,
                                        &cmdBufferAllocateInfo,
                                        &cmd_buffer);
  vik_log_e_if(r != VK_SUCCESS, "vkAllocateCommandBuffers: %s",
               vks::Log::result_string(r).c_str());

  VkCommandBufferBeginInfo cmdBufferBeginInfo = {};
  cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  cmdBufferBeginInfo.flags = 0;

  r = vkBeginCommandBuffer(cmd_buffer, &cmdBufferBeginInfo);
  vik_log_e_if(r != VK_SUCCESS, "vkBeginCommandBuffer: %s",
               vks::Log::result_string(r).c_str());

  std::array<VkClearValue,1> clearValues = {};
  clearValues[0].color = { { 0.2f, 0.2f, 0.2f, 1.0f } };

  VkRenderPassBeginInfo passBeginInfo = {};
  passBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  passBeginInfo.renderPass = render_pass;
  passBeginInfo.framebuffer = frame_buffer;
  passBeginInfo.renderArea = { { 0, 0 }, { width, height } };
  /*
    passBeginInfo.renderArea.offset.x = 0;
    passBeginInfo.renderArea.offset.y = 0;
    passBeginInfo.renderArea.extent.width = width;
    passBeginInfo.renderArea.extent.height = height;
    */
  passBeginInfo.clearValueCount = 1;
  passBeginInfo.pClearValues = clearValues.data();

  vkCmdBeginRenderPass(cmd_buffer,
                       &passBeginInfo,
                       VK_SUBPASS_CONTENTS_INLINE);

  VkBuffer buffers[] = {
    buffer,
    buffer,
    buffer
  };

  VkDeviceSize offsets[] = {
    vertex_offset,
    colors_offset,
    normals_offset
  };

  vkCmdBindVertexBuffers(cmd_buffer, 0, 3,
                         buffers,
                         offsets);

  vkCmdBindPipeline(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);

  vkCmdBindDescriptorSets(cmd_buffer,
                          VK_PIPELINE_BIND_POINT_GRAPHICS,
                          pipeline_layout,
                          0, 1,
                          &descriptor_set, 0, NULL);

  const VkViewport viewport = {
    .x = 0,
    .y = 0,
    .width = (float)width,
    .height = (float)height,
    .minDepth = 0,
    .maxDepth = 1,
  };
  vkCmdSetViewport(cmd_buffer, 0, 1, &viewport);

  const VkRect2D scissor = {
    .offset = { 0, 0 },
    .extent = { width, height },
  };
  vkCmdSetScissor(cmd_buffer, 0, 1, &scissor);

  vkCmdDraw(cmd_buffer, 4, 1, 0, 0);
  vkCmdDraw(cmd_buffer, 4, 1, 4, 0);
  vkCmdDraw(cmd_buffer, 4, 1, 8, 0);
  vkCmdDraw(cmd_buffer, 4, 1, 12, 0);
  vkCmdDraw(cmd_buffer, 4, 1, 16, 0);
  vkCmdDraw(cmd_buffer, 4, 1, 20, 0);

  vkCmdEndRenderPass(cmd_buffer);

  r = vkEndCommandBuffer(cmd_buffer);
  vik_log_e_if(r != VK_SUCCESS,
               "vkEndCommandBuffer: %s", vks::Log::result_string(r).c_str());
}

void Renderer::render(uint32_t index) {
  build_command_buffer(frame_buffers[index]);
  submit_queue();
  wait_and_reset_fences();
}

void Renderer::render_swapchain_vk() {
  SwapChainVK *sc = (SwapChainVK *) swap_chain;
  uint32_t present_index = 0;
  VkResult result = sc->acquire_next_image(device, semaphore, &present_index);
  switch (result) {
    case VK_SUCCESS:
      render(present_index);
      vik_log_check(sc->present(queue, present_index));
      break;
    case VK_TIMEOUT:
      // TODO: XCB times out
      break;
    default:
      vik_log_e("vkAcquireNextImageKHR failed: %s",
                 vks::Log::result_string(result).c_str());
      break;
  }
}
}