-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo.cpp
More file actions
131 lines (112 loc) · 4.47 KB
/
Video.cpp
File metadata and controls
131 lines (112 loc) · 4.47 KB
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
#include "Video.hpp"
#include "Buffer.hpp"
#include "Log.hpp"
#include "Pipeline.hpp"
#include "UniformBuffer.hpp"
#include "Vertex.hpp"
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_vulkan.h>
#include <algorithm>
#include <fmt/format.h>
#include <glm/common.hpp>
#include <glm/mat2x2.hpp>
#include <span>
#include <vulkan/vulkan_beta.h>
Video::Video()
: m_Window(
"Untitled Game", {SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED},
{1200, 800}, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN),
m_Instance(m_Window, m_Context), m_Surface(m_Window, m_Instance),
m_Device(m_Instance, m_Surface), m_Swapchain(m_Device, m_Surface),
m_Queue(m_Device.Get(), m_QueueFamilyIndex, 0),
m_RenderPass(m_Device, m_Surface),
m_VertexBuffer(
m_Device, vertices.size(), vk::BufferUsageFlagBits::eVertexBuffer),
m_UniformBuffers(std::move(ConstructUniformBuffers())),
m_Framebuffers(m_Swapchain, m_RenderPass, m_Device),
m_CommandBuffers(
m_Device, m_QueueFamilyIndex, m_Swapchain.GetImageCount()),
m_SyncObjects(m_Device),
m_Descriptors(m_Device, m_UniformBuffers, m_Swapchain.GetImageCount()),
m_Pipeline(m_Device, m_RenderPass, m_Surface, m_Descriptors)
{
// buffers
FillVertexBuffer();
}
Video::~Video()
{
m_Device.Get().waitIdle();
m_Queue.waitIdle();
}
void Video::Render()
{
vk::raii::Device& device = m_Device.Get();
device.waitForFences(
*m_SyncObjects.inFlightFences.at(m_CurrentImage), VK_TRUE,
std::numeric_limits<uint64_t>::max());
device.resetFences(*m_SyncObjects.inFlightFences.at(m_CurrentImage));
auto [result, imageIndex] = m_Swapchain.Get().acquireNextImage(
std::numeric_limits<uint64_t>::max(),
*m_SyncObjects.imageAvailableSemaphores.at(m_CurrentImage));
vk::raii::CommandBuffer& commandBuffer = m_CommandBuffers[m_CurrentImage];
commandBuffer.reset();
vk::ClearColorValue clearColor(0.0f, 0.0f, 0.0f, 1.0f);
vk::ClearValue clearValue(clearColor);
vk::RenderPassBeginInfo renderPassBeginInfo(
*m_RenderPass.Get(), *m_Framebuffers[m_CurrentImage],
vk::Rect2D({}, m_Surface.surfaceCapabilities.currentExtent),
clearValue);
commandBuffer.begin({vk::CommandBufferUsageFlagBits::eSimultaneousUse});
commandBuffer.beginRenderPass(
renderPassBeginInfo, vk::SubpassContents::eInline);
commandBuffer.bindPipeline(
vk::PipelineBindPoint::eGraphics, *m_Pipeline.Get());
vk::DeviceSize offset = 0;
commandBuffer.bindVertexBuffers(0, *m_VertexBuffer.Get(), offset);
commandBuffer.bindDescriptorSets(
vk::PipelineBindPoint::eGraphics, *m_Pipeline.GetLayout(), 0,
*m_Descriptors.GetSets().at(m_CurrentImage), nullptr);
commandBuffer.draw(static_cast<uint32_t>(vertices.size()), 1, 0, 0);
commandBuffer.endRenderPass();
commandBuffer.end();
vk::PipelineStageFlags waitFlags =
vk::PipelineStageFlagBits::eColorAttachmentOutput;
vk::SubmitInfo submitInfo(
*m_SyncObjects.imageAvailableSemaphores.at(m_CurrentImage), waitFlags,
*commandBuffer,
*m_SyncObjects.renderFinishedSemaphores.at(m_CurrentImage));
m_Queue.submit(
submitInfo, *m_SyncObjects.inFlightFences.at(m_CurrentImage));
vk::PresentInfoKHR presentInfo(
*m_SyncObjects.renderFinishedSemaphores.at(m_CurrentImage),
*m_Swapchain.Get(), m_CurrentImage);
vk::Result presentResult = m_Queue.presentKHR(presentInfo);
m_CurrentImage = (m_CurrentImage + 1) % m_Swapchain.GetImageCount();
}
void Video::UpdateUnformBuffers(float theta)
{
UniformBufferObject& buffer =
m_UniformBuffers.at(m_CurrentImage).GetMemory().front();
buffer.rotation[0].x = cos(theta * M_PI / 180);
buffer.rotation[0].y = -sin(theta * M_PI / 180);
buffer.rotation[1].x = sin(theta * M_PI / 180);
buffer.rotation[1].y = cos(theta * M_PI / 180);
buffer.colorRotation = theta;
}
void Video::FillVertexBuffer()
{
// load hard-coded vertices into memory
std::span<Vertex> memorySpan = m_VertexBuffer.GetMemory();
std::copy_n(vertices.begin(), vertices.size(), memorySpan.begin());
}
std::vector<Buffer<UniformBufferObject>> Video::ConstructUniformBuffers()
{
std::vector<Buffer<UniformBufferObject>> uniformBuffers;
for (size_t i = 0; i < m_Swapchain.GetImageCount(); i++)
{
uniformBuffers.emplace_back(
m_Device, 1, vk::BufferUsageFlagBits::eUniformBuffer);
}
return uniformBuffers;
}