Compare commits
4 Commits
b8a15406da
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 7213c85fdf | |||
| 9d4b85c940 | |||
| 3ce7962d4c | |||
| 5b363a8c57 |
@@ -48,6 +48,7 @@ add_executable(vk_expe
|
|||||||
src/vk/Pipeline.cpp
|
src/vk/Pipeline.cpp
|
||||||
src/vk/Memory.cpp
|
src/vk/Memory.cpp
|
||||||
src/vk/Buffer.cpp
|
src/vk/Buffer.cpp
|
||||||
|
src/vk/Image.cpp
|
||||||
src/vk/ImageView.cpp
|
src/vk/ImageView.cpp
|
||||||
src/vk/DescriptorSetLayout.cpp
|
src/vk/DescriptorSetLayout.cpp
|
||||||
src/vk/PipelineLayout.cpp
|
src/vk/PipelineLayout.cpp
|
||||||
@@ -56,10 +57,11 @@ add_executable(vk_expe
|
|||||||
src/vk/Swapchain.cpp
|
src/vk/Swapchain.cpp
|
||||||
|
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
src/Camera.cpp
|
||||||
src/Simplex.cpp
|
src/Simplex.cpp
|
||||||
src/Planet.cpp
|
src/Planet.cpp
|
||||||
src/VkExpe.cpp
|
src/VkExpe.cpp
|
||||||
src/VulkanTutorial.cpp
|
src/Renderer.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_shaders(vk_expe
|
add_shaders(vk_expe
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ void main() {
|
|||||||
|
|
||||||
vec2 v0 = p0 - p2;
|
vec2 v0 = p0 - p2;
|
||||||
vec2 v1 = p1 - p2;
|
vec2 v1 = p1 - p2;
|
||||||
out_edge_dist[i] = determinant(mat2(v0, v1)) / length(v1);
|
out_edge_dist[i] = abs(determinant(mat2(v0, v1))) / length(v1);
|
||||||
|
|
||||||
EmitVertex();
|
EmitVertex();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,11 +18,12 @@ layout(location = 1) out vec3 out_normal;
|
|||||||
layout(location = 2) out vec3 out_color;
|
layout(location = 2) out vec3 out_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float lod = clamp(
|
// float lod = clamp(
|
||||||
2.0 * dot(transpose(uniforms.scene_from_model)[0].xyz, in_position) + 0.5,
|
// 2.0 * dot(transpose(uniforms.scene_from_model)[0].xyz, in_position) + 0.5,
|
||||||
0.0, 1.0
|
// 0.0, 1.0
|
||||||
);
|
// );
|
||||||
vec3 position = mix(in_position2, in_position, lod);
|
// vec3 position = mix(in_position2, in_position, lod);
|
||||||
|
vec3 position = in_position;
|
||||||
out_position =
|
out_position =
|
||||||
uniforms.projection_from_scene *
|
uniforms.projection_from_scene *
|
||||||
uniforms.scene_from_model *
|
uniforms.scene_from_model *
|
||||||
|
|||||||
70
src/Camera.cpp
Normal file
70
src/Camera.cpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// Copyright 2022 Simon Boyé
|
||||||
|
|
||||||
|
#include <Camera.h>
|
||||||
|
|
||||||
|
|
||||||
|
Camera::Camera() noexcept
|
||||||
|
: m_left(-1)
|
||||||
|
, m_right(1)
|
||||||
|
, m_top(-1)
|
||||||
|
, m_bottom(1)
|
||||||
|
, m_near(0.1)
|
||||||
|
, m_far(100)
|
||||||
|
, m_position(Vector3::Zero())
|
||||||
|
, m_direction(Vector3::UnitZ())
|
||||||
|
, m_down(Vector3::UnitY())
|
||||||
|
{}
|
||||||
|
|
||||||
|
Camera::~Camera() = default;
|
||||||
|
|
||||||
|
|
||||||
|
void Camera::set_projection(Real h_fov, Real width_height_ratio) noexcept {
|
||||||
|
const Real hx = std::tan(h_fov / Real(2));
|
||||||
|
const Real hy = hx / width_height_ratio;
|
||||||
|
set_projection(
|
||||||
|
-hx, hx,
|
||||||
|
-hy, hy
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::update_aspect_ratio(Real width_height_ratio) noexcept {
|
||||||
|
const Real prev_ratio = (m_right - m_left) / (m_bottom - m_top);
|
||||||
|
const Real next_ratio = width_height_ratio;
|
||||||
|
|
||||||
|
const Real sx = (Real(1) + Real(1) / prev_ratio) / (Real(1) + Real(1) / next_ratio);
|
||||||
|
const Real sy = (Real(1) + prev_ratio) / (Real(1) + next_ratio);
|
||||||
|
|
||||||
|
m_left *= sx;
|
||||||
|
m_right *= sx;
|
||||||
|
m_top *= sy;
|
||||||
|
m_bottom *= sy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix3 Camera::basis() const noexcept {
|
||||||
|
Matrix3 basis;
|
||||||
|
basis << m_down.cross(m_direction), m_down, m_direction;
|
||||||
|
return basis;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix4 Camera::view_matrix() const noexcept {
|
||||||
|
Matrix3 linear_view;
|
||||||
|
linear_view <<
|
||||||
|
m_down.cross(m_direction).transpose(),
|
||||||
|
m_down.transpose(),
|
||||||
|
m_direction.transpose();
|
||||||
|
Matrix4 view;
|
||||||
|
view << linear_view, linear_view * -m_position,
|
||||||
|
Vector4::UnitW().transpose();
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix4 Camera::projection_matrix() const noexcept {
|
||||||
|
return ::projection_matrix(
|
||||||
|
m_near * m_left, m_near * m_right,
|
||||||
|
m_near * m_top, m_near * m_bottom,
|
||||||
|
m_near, m_far
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
92
src/Camera.h
Normal file
92
src/Camera.h
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// Copyright 2022 Simon Boyé
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <core/math.h>
|
||||||
|
|
||||||
|
|
||||||
|
class Camera {
|
||||||
|
public:
|
||||||
|
Camera() noexcept;
|
||||||
|
~Camera();
|
||||||
|
|
||||||
|
inline Real left() const noexcept {
|
||||||
|
return m_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Real right() const noexcept {
|
||||||
|
return m_right;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Real top() const noexcept {
|
||||||
|
return m_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Real bottom() const noexcept {
|
||||||
|
return m_bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_projection(Real left, Real right, Real top, Real bottom) noexcept {
|
||||||
|
m_left = left;
|
||||||
|
m_right = right;
|
||||||
|
m_top = top;
|
||||||
|
m_bottom = bottom;
|
||||||
|
}
|
||||||
|
void set_projection(Real h_fov, Real width_height_ratio) noexcept;
|
||||||
|
void update_aspect_ratio(Real width_height_ratio) noexcept;
|
||||||
|
|
||||||
|
|
||||||
|
inline Real near() const noexcept {
|
||||||
|
return m_near;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Real far() const noexcept {
|
||||||
|
return m_far;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_clip_distances(Real near, Real far) noexcept {
|
||||||
|
m_near = near;
|
||||||
|
m_far = far;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline Vector3 position() const noexcept {
|
||||||
|
return m_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void set_position(const Vector3& position) noexcept {
|
||||||
|
m_position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vector3 direction() const noexcept {
|
||||||
|
return m_direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void set_direction(const Vector3& direction) noexcept {
|
||||||
|
m_direction = direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vector3 down() const noexcept {
|
||||||
|
return m_down;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void set_down(const Vector3& down) noexcept {
|
||||||
|
m_down = down;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix3 basis() const noexcept;
|
||||||
|
Matrix4 view_matrix() const noexcept;
|
||||||
|
Matrix4 projection_matrix() const noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Real m_left;
|
||||||
|
Real m_right;
|
||||||
|
Real m_top;
|
||||||
|
Real m_bottom;
|
||||||
|
|
||||||
|
Real m_near;
|
||||||
|
Real m_far;
|
||||||
|
|
||||||
|
Vector3 m_position;
|
||||||
|
Vector3 m_direction;
|
||||||
|
Vector3 m_down;
|
||||||
|
};
|
||||||
825
src/Renderer.cpp
Normal file
825
src/Renderer.cpp
Normal file
@@ -0,0 +1,825 @@
|
|||||||
|
// Copyright 2022 Simon Boyé
|
||||||
|
#include <Renderer.h>
|
||||||
|
|
||||||
|
#include <Planet.h>
|
||||||
|
|
||||||
|
#include <vk/ShaderModule.h>
|
||||||
|
|
||||||
|
#include <core/utils.h>
|
||||||
|
#include <core/Logger.h>
|
||||||
|
|
||||||
|
#include <SDL2/SDL_vulkan.h>
|
||||||
|
|
||||||
|
#include <Eigen/Geometry>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
VkVertexInputBindingDescription vertex_binding_description() {
|
||||||
|
return {
|
||||||
|
.binding = 0,
|
||||||
|
.stride = sizeof(Vertex),
|
||||||
|
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<VkVertexInputAttributeDescription, 4> vertex_attributes_description() {
|
||||||
|
return {
|
||||||
|
VkVertexInputAttributeDescription {
|
||||||
|
.location = 0,
|
||||||
|
.binding = 0,
|
||||||
|
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
|
.offset = offsetof(Vertex, position),
|
||||||
|
},
|
||||||
|
VkVertexInputAttributeDescription {
|
||||||
|
.location = 1,
|
||||||
|
.binding = 0,
|
||||||
|
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
|
.offset = offsetof(Vertex, position2),
|
||||||
|
},
|
||||||
|
VkVertexInputAttributeDescription {
|
||||||
|
.location = 2,
|
||||||
|
.binding = 0,
|
||||||
|
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
|
.offset = offsetof(Vertex, normal),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.location = 3,
|
||||||
|
.binding = 0,
|
||||||
|
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
|
.offset = offsetof(Vertex, color),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<Vertex> vertices = {
|
||||||
|
{{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
|
||||||
|
{{ 1.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
|
||||||
|
{{-1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
|
||||||
|
{{ 1.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
|
||||||
|
|
||||||
|
{{0.0f, -1.0f, -1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f}},
|
||||||
|
{{0.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f}},
|
||||||
|
{{0.0f, 1.0f, -1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f}},
|
||||||
|
{{0.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f}},
|
||||||
|
|
||||||
|
{{-1.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}},
|
||||||
|
{{ 1.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}},
|
||||||
|
{{-1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}},
|
||||||
|
{{ 1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<uint32_t> indices = {
|
||||||
|
0, 1, 2,
|
||||||
|
2, 1, 3,
|
||||||
|
|
||||||
|
4, 5, 6,
|
||||||
|
6, 5, 7,
|
||||||
|
|
||||||
|
8, 9, 10,
|
||||||
|
10, 9, 11,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Uniforms {
|
||||||
|
alignas(16) Eigen::Matrix4f scene_from_model;
|
||||||
|
alignas(16) Eigen::Matrix4f projection_from_scene;
|
||||||
|
alignas(8) Eigen::Vector2f half_screen_size;
|
||||||
|
alignas(4) float lod;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Renderer::Renderer() {
|
||||||
|
m_swapchain.register_creation_callback(
|
||||||
|
std::bind(&Renderer::create_swapchain_objects, this));
|
||||||
|
|
||||||
|
// auto const subdiv_count = 4;
|
||||||
|
|
||||||
|
// vertices.resize(6 * Cell::vertex_count(subdiv_count));
|
||||||
|
// indices.resize(6 * 3 * Cell::triangle_count(subdiv_count));
|
||||||
|
|
||||||
|
// Vector3AV positions(
|
||||||
|
// vertices.size(), vertices.data(),
|
||||||
|
// sizeof(Vertex), offsetof(Vertex, position)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Vector3AV positions2(
|
||||||
|
// vertices.size(), vertices.data(),
|
||||||
|
// sizeof(Vertex), offsetof(Vertex, position2)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Vector3AV normals(
|
||||||
|
// vertices.size(), vertices.data(),
|
||||||
|
// sizeof(Vertex), offsetof(Vertex, normal)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Vector3AV colors(
|
||||||
|
// vertices.size(), vertices.data(),
|
||||||
|
// sizeof(Vertex), offsetof(Vertex, color)
|
||||||
|
// );
|
||||||
|
|
||||||
|
// TriangleAV triangles(
|
||||||
|
// indices.size() / 3,
|
||||||
|
// indices.data()
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Planet planet;
|
||||||
|
// planet.build_mesh(positions, positions2, normals, triangles, subdiv_count);
|
||||||
|
|
||||||
|
// for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) {
|
||||||
|
// colors[vertex_index] =
|
||||||
|
// positions[vertex_index] / 2.0f + Vector3::Constant(0.5f);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for(size_t vi = 0; vi < vertices.size(); vi += 1)
|
||||||
|
// logger.debug() << "v" << vi << ": "
|
||||||
|
// << vertices[vi].position.transpose();
|
||||||
|
// for(size_t ii = 0; 3 * ii < indices.size(); ii += 1)
|
||||||
|
// logger.debug() << "i" << ii << ": "
|
||||||
|
// << indices[3 * ii + 0] << ", "
|
||||||
|
// << indices[3 * ii + 1] << ", "
|
||||||
|
// << indices[3 * ii + 2];
|
||||||
|
// abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
Renderer::~Renderer() {
|
||||||
|
if(m_context)
|
||||||
|
vkDeviceWaitIdle(m_context.device());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::initialize(SDL_Window* window) {
|
||||||
|
auto const context_settings = vk::ContextSettings()
|
||||||
|
#if defined(VKEXPE_ENABLE_VALIDATION) || !defined(NDEBUG)
|
||||||
|
.with_debug(true)
|
||||||
|
#endif
|
||||||
|
.with_queue(GRAPHIC_QUEUE, VK_QUEUE_GRAPHICS_BIT)
|
||||||
|
.with_window(window);
|
||||||
|
m_context.initialize(context_settings);
|
||||||
|
|
||||||
|
create_command_pool();
|
||||||
|
|
||||||
|
create_descriptor_set_layout();
|
||||||
|
create_pipeline_layout();
|
||||||
|
create_render_pass();
|
||||||
|
|
||||||
|
// m_uniform_buffer_memory is allocated when uniform buffer is first created.
|
||||||
|
|
||||||
|
create_vertex_buffer();
|
||||||
|
create_index_buffer();
|
||||||
|
|
||||||
|
auto const swapchain_settings = vk::SwapchainSettings(&m_context)
|
||||||
|
.with_queue(m_context.queue_family(GRAPHIC_QUEUE));
|
||||||
|
m_swapchain.initialize(swapchain_settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::set_camera(const Camera& camera) {
|
||||||
|
m_camera = camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::draw_frame() {
|
||||||
|
m_swapchain.begin_frame();
|
||||||
|
const auto image_index = m_swapchain.current_image_index();
|
||||||
|
auto& image_states = m_image_states[image_index];
|
||||||
|
|
||||||
|
m_camera.update_aspect_ratio(Real(m_swapchain.extent().width) / Real(m_swapchain.extent().height));
|
||||||
|
const Matrix4 view = m_camera.view_matrix();
|
||||||
|
const Matrix4 proj = m_camera.projection_matrix();
|
||||||
|
|
||||||
|
Transform model = Transform::Identity();
|
||||||
|
|
||||||
|
const Uniforms uniforms = {
|
||||||
|
.scene_from_model = model.matrix(),
|
||||||
|
.projection_from_scene = proj * view,
|
||||||
|
.half_screen_size = {
|
||||||
|
0.5 * m_swapchain.extent().width,
|
||||||
|
0.5 * m_swapchain.extent().height,
|
||||||
|
},
|
||||||
|
// .lod = std::cos(alpha) * 0.5f + 0.5f,
|
||||||
|
};
|
||||||
|
|
||||||
|
void* uniform_buffer = m_uniform_buffer_memory.map(
|
||||||
|
m_context,
|
||||||
|
image_states.m_uniform_buffer_offset,
|
||||||
|
sizeof(Uniforms)
|
||||||
|
);
|
||||||
|
std::memcpy(uniform_buffer, &uniforms, sizeof(Uniforms));
|
||||||
|
m_uniform_buffer_memory.unmap(m_context);
|
||||||
|
|
||||||
|
std::vector<VkDrawIndexedIndirectCommand> draw_commands {
|
||||||
|
{
|
||||||
|
.indexCount = uint32_t(indices.size()),
|
||||||
|
.instanceCount = 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Byte* draw_buffer = image_states.m_draw_buffer.memory().map(
|
||||||
|
m_context,
|
||||||
|
0,
|
||||||
|
DrawBufferOffset + 1 * sizeof(VkDrawIndexedIndirectCommand)
|
||||||
|
);
|
||||||
|
*reinterpret_cast<uint32_t*>(draw_buffer) = uint32_t(draw_commands.size());
|
||||||
|
std::memcpy(
|
||||||
|
draw_buffer + DrawBufferOffset,
|
||||||
|
draw_commands.data(),
|
||||||
|
draw_commands.size() * sizeof(VkDrawIndexedIndirectCommand)
|
||||||
|
);
|
||||||
|
image_states.m_draw_buffer.memory().unmap(m_context);
|
||||||
|
|
||||||
|
VkSemaphore wait_semaphores[] = {
|
||||||
|
m_swapchain.ready_to_render(),
|
||||||
|
};
|
||||||
|
VkSemaphore done_semaphores[] = {
|
||||||
|
image_states.m_render_done,
|
||||||
|
};
|
||||||
|
VkPipelineStageFlags stages[] = {
|
||||||
|
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
};
|
||||||
|
VkCommandBuffer command_buffers[] = {
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
};
|
||||||
|
VkSubmitInfo submit_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
||||||
|
.waitSemaphoreCount = 1,
|
||||||
|
.pWaitSemaphores = wait_semaphores,
|
||||||
|
.pWaitDstStageMask = stages,
|
||||||
|
.commandBufferCount = 1,
|
||||||
|
.pCommandBuffers = command_buffers,
|
||||||
|
.signalSemaphoreCount = 1,
|
||||||
|
.pSignalSemaphores = done_semaphores,
|
||||||
|
};
|
||||||
|
|
||||||
|
if(vkQueueSubmit(
|
||||||
|
m_context.queue(GRAPHIC_QUEUE),
|
||||||
|
1, &submit_info,
|
||||||
|
m_swapchain.render_done()
|
||||||
|
))
|
||||||
|
throw std::runtime_error("failed to submit draw command buffer");
|
||||||
|
|
||||||
|
m_swapchain.swap_buffers(done_semaphores);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::invalidate_swapchain() {
|
||||||
|
m_swapchain.invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_swapchain_objects() {
|
||||||
|
m_image_states.clear();
|
||||||
|
m_swapchain_states.~SwapchainStates();
|
||||||
|
|
||||||
|
new(&m_swapchain_states) SwapchainStates();
|
||||||
|
initialize_swapchain_states();
|
||||||
|
|
||||||
|
m_image_states.resize(m_swapchain.image_count());
|
||||||
|
for(size_t image_index = 0; image_index < m_image_states.size(); image_index += 1)
|
||||||
|
initialize_image_states(image_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_command_pool() {
|
||||||
|
m_command_pool = vk::CommandPool(
|
||||||
|
m_context,
|
||||||
|
m_context.queue_family(GRAPHIC_QUEUE)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_descriptor_set_layout() {
|
||||||
|
VkDescriptorSetLayoutBinding binding[] {
|
||||||
|
{
|
||||||
|
.binding = 0,
|
||||||
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||||
|
.descriptorCount = 1,
|
||||||
|
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
m_descriptor_set_layout = vk::DescriptorSetLayout(m_context, binding);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_pipeline_layout() {
|
||||||
|
VkDescriptorSetLayout set_layouts[] {
|
||||||
|
m_descriptor_set_layout,
|
||||||
|
};
|
||||||
|
m_pipeline_layout = vk::PipelineLayout(m_context, set_layouts);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_render_pass() {
|
||||||
|
VkAttachmentDescription attachments[] {
|
||||||
|
{
|
||||||
|
.format = m_context.surface_format().format,
|
||||||
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
|
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
|
||||||
|
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
||||||
|
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
||||||
|
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
||||||
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.format = VK_FORMAT_X8_D24_UNORM_PACK32,
|
||||||
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
|
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
|
||||||
|
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
||||||
|
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
||||||
|
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
||||||
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
VkAttachmentReference color_attachment_ref {
|
||||||
|
.attachment = 0,
|
||||||
|
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||||
|
};
|
||||||
|
VkAttachmentReference depth_attachment_ref {
|
||||||
|
.attachment = 1,
|
||||||
|
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkSubpassDescription subpass {
|
||||||
|
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||||
|
.colorAttachmentCount = 1,
|
||||||
|
.pColorAttachments = &color_attachment_ref,
|
||||||
|
.pDepthStencilAttachment = &depth_attachment_ref,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkSubpassDependency subpass_dep = {
|
||||||
|
.srcSubpass = VK_SUBPASS_EXTERNAL,
|
||||||
|
.dstSubpass = 0,
|
||||||
|
.srcStageMask =
|
||||||
|
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
|
||||||
|
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.dstStageMask =
|
||||||
|
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
|
||||||
|
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
||||||
|
.srcAccessMask = 0,
|
||||||
|
.dstAccessMask =
|
||||||
|
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
|
||||||
|
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkRenderPassCreateInfo render_pass_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
|
||||||
|
.attachmentCount = uint32_t(std::extent_v<typeof(attachments)>),
|
||||||
|
.pAttachments = attachments,
|
||||||
|
.subpassCount = 1,
|
||||||
|
.pSubpasses = &subpass,
|
||||||
|
.dependencyCount = 1,
|
||||||
|
.pDependencies = &subpass_dep,
|
||||||
|
};
|
||||||
|
|
||||||
|
m_render_pass = vk::RenderPass(m_context, render_pass_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_vertex_buffer() {
|
||||||
|
VkDeviceSize size = sizeof(vertices[0]) * vertices.size();
|
||||||
|
m_vertex_buffer = vk::Buffer(
|
||||||
|
m_context,
|
||||||
|
size,
|
||||||
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
|
||||||
|
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||||
|
);
|
||||||
|
m_vertex_buffer.upload(size, vertices.data(), m_context.queue_family(GRAPHIC_QUEUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_index_buffer() {
|
||||||
|
VkDeviceSize size = sizeof(indices[0]) * indices.size();
|
||||||
|
m_index_buffer = vk::Buffer(
|
||||||
|
m_context,
|
||||||
|
size,
|
||||||
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
||||||
|
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||||
|
);
|
||||||
|
m_index_buffer.upload(size, indices.data(), m_context.queue_family(GRAPHIC_QUEUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::initialize_swapchain_states() {
|
||||||
|
create_descriptor_pool();
|
||||||
|
create_graphic_pipeline();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_descriptor_pool() {
|
||||||
|
VkDescriptorPoolSize pool_sizes[] {
|
||||||
|
{
|
||||||
|
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||||
|
.descriptorCount = uint32_t(m_swapchain.image_count()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
m_swapchain_states.m_descriptor_pool = vk::DescriptorPool(
|
||||||
|
m_context,
|
||||||
|
uint32_t(m_swapchain.image_count()),
|
||||||
|
pool_sizes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_graphic_pipeline() {
|
||||||
|
auto vertex_shader_module = vk::ShaderModule(m_context, "shaders/shader.vert.spv");
|
||||||
|
auto geometry_shader_module = vk::ShaderModule(m_context, "shaders/shader.geom.spv");
|
||||||
|
auto fragment_shader_module = vk::ShaderModule(m_context, "shaders/shader.frag.spv");
|
||||||
|
|
||||||
|
VkPipelineShaderStageCreateInfo shader_stage_infos[] = {
|
||||||
|
{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
|
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
||||||
|
.module = vertex_shader_module,
|
||||||
|
.pName = "main",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
|
.stage = VK_SHADER_STAGE_GEOMETRY_BIT,
|
||||||
|
.module = geometry_shader_module,
|
||||||
|
.pName = "main",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||||
|
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||||
|
.module = fragment_shader_module,
|
||||||
|
.pName = "main",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const VkVertexInputBindingDescription vertex_bindings[] = {
|
||||||
|
vertex_binding_description(),
|
||||||
|
};
|
||||||
|
const auto vertex_attributes = vertex_attributes_description();
|
||||||
|
|
||||||
|
VkPipelineVertexInputStateCreateInfo vertex_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
|
||||||
|
.vertexBindingDescriptionCount = 1,
|
||||||
|
.pVertexBindingDescriptions = vertex_bindings,
|
||||||
|
.vertexAttributeDescriptionCount = uint32_t(vertex_attributes.size()),
|
||||||
|
.pVertexAttributeDescriptions = vertex_attributes.data(),
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineInputAssemblyStateCreateInfo assembly_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
||||||
|
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||||
|
.primitiveRestartEnable = VK_FALSE,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkViewport viewport {
|
||||||
|
.x = 0.0f,
|
||||||
|
.y = 0.0f,
|
||||||
|
.width = float(m_swapchain.extent().width),
|
||||||
|
.height = float(m_swapchain.extent().height),
|
||||||
|
.minDepth = 0.0f,
|
||||||
|
.maxDepth = 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkRect2D scissor = {
|
||||||
|
.offset = { 0, 0 },
|
||||||
|
.extent = m_swapchain.extent(),
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineViewportStateCreateInfo viewport_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
||||||
|
.viewportCount = 1,
|
||||||
|
.pViewports = &viewport,
|
||||||
|
.scissorCount = 1,
|
||||||
|
.pScissors = &scissor,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineRasterizationStateCreateInfo rasterization_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
||||||
|
.depthClampEnable = VK_FALSE,
|
||||||
|
.rasterizerDiscardEnable = VK_FALSE,
|
||||||
|
.polygonMode = VK_POLYGON_MODE_FILL,
|
||||||
|
.cullMode = VK_CULL_MODE_NONE,
|
||||||
|
// .cullMode = VK_CULL_MODE_BACK_BIT,
|
||||||
|
.frontFace = VK_FRONT_FACE_CLOCKWISE,
|
||||||
|
.depthBiasEnable = VK_FALSE,
|
||||||
|
.depthBiasConstantFactor = 0.0f,
|
||||||
|
.depthBiasClamp = 0.0f,
|
||||||
|
.depthBiasSlopeFactor = 0.0f,
|
||||||
|
.lineWidth = 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineMultisampleStateCreateInfo multisample_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
||||||
|
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
|
.sampleShadingEnable = VK_FALSE,
|
||||||
|
.minSampleShading = 1.0f,
|
||||||
|
.pSampleMask = nullptr,
|
||||||
|
.alphaToCoverageEnable = VK_FALSE,
|
||||||
|
.alphaToOneEnable = VK_FALSE,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineDepthStencilStateCreateInfo depth_stencil_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
|
||||||
|
.depthTestEnable = VK_TRUE,
|
||||||
|
.depthWriteEnable = VK_TRUE,
|
||||||
|
.depthCompareOp = VK_COMPARE_OP_LESS,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineColorBlendAttachmentState color_blend_attachment {
|
||||||
|
.blendEnable = VK_FALSE,
|
||||||
|
.srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
|
||||||
|
.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
|
||||||
|
.colorBlendOp = VK_BLEND_OP_ADD,
|
||||||
|
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
|
||||||
|
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
|
||||||
|
.alphaBlendOp = VK_BLEND_OP_ADD,
|
||||||
|
.colorWriteMask =
|
||||||
|
VK_COLOR_COMPONENT_R_BIT |
|
||||||
|
VK_COLOR_COMPONENT_G_BIT |
|
||||||
|
VK_COLOR_COMPONENT_B_BIT |
|
||||||
|
VK_COLOR_COMPONENT_A_BIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPipelineColorBlendStateCreateInfo color_blend_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
||||||
|
.logicOpEnable = VK_FALSE,
|
||||||
|
.logicOp = VK_LOGIC_OP_COPY,
|
||||||
|
.attachmentCount = 1,
|
||||||
|
.pAttachments = &color_blend_attachment,
|
||||||
|
.blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f },
|
||||||
|
};
|
||||||
|
|
||||||
|
VkGraphicsPipelineCreateInfo pipeline_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
|
||||||
|
.stageCount = 3,
|
||||||
|
.pStages = shader_stage_infos,
|
||||||
|
.pVertexInputState = &vertex_info,
|
||||||
|
.pInputAssemblyState = &assembly_info,
|
||||||
|
.pViewportState = &viewport_info,
|
||||||
|
.pRasterizationState = &rasterization_info,
|
||||||
|
.pMultisampleState = &multisample_info,
|
||||||
|
.pDepthStencilState = &depth_stencil_info,
|
||||||
|
.pColorBlendState = &color_blend_info,
|
||||||
|
.pDynamicState = nullptr,
|
||||||
|
.layout = m_pipeline_layout,
|
||||||
|
.renderPass = m_render_pass,
|
||||||
|
.subpass = 0,
|
||||||
|
.basePipelineHandle = VK_NULL_HANDLE,
|
||||||
|
.basePipelineIndex = -1,
|
||||||
|
};
|
||||||
|
|
||||||
|
m_swapchain_states.m_pipeline = vk::Pipeline(m_context, pipeline_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::initialize_image_states(size_t image_index) {
|
||||||
|
auto& image_states = m_image_states[image_index];
|
||||||
|
image_states.m_image_index = image_index;
|
||||||
|
|
||||||
|
create_depth_buffer(image_states);
|
||||||
|
create_framebuffer(image_states);
|
||||||
|
|
||||||
|
create_uniform_buffer(image_states);
|
||||||
|
create_descriptor_set(image_states);
|
||||||
|
|
||||||
|
create_draw_buffer(image_states);
|
||||||
|
|
||||||
|
create_command_buffer(image_states);
|
||||||
|
|
||||||
|
image_states.m_render_done = vk::Semaphore(m_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_depth_buffer(ImageStates& image_states)
|
||||||
|
{
|
||||||
|
image_states.m_depth_buffer = vk::Image(
|
||||||
|
m_context,
|
||||||
|
VkImageCreateInfo {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
||||||
|
.imageType = VK_IMAGE_TYPE_2D,
|
||||||
|
.format = VK_FORMAT_X8_D24_UNORM_PACK32,
|
||||||
|
.extent = {
|
||||||
|
m_swapchain.extent().width,
|
||||||
|
m_swapchain.extent().height,
|
||||||
|
1
|
||||||
|
},
|
||||||
|
.mipLevels = 1,
|
||||||
|
.arrayLayers = 1,
|
||||||
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||||
|
.tiling = VK_IMAGE_TILING_OPTIMAL,
|
||||||
|
.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
||||||
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||||
|
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||||
|
},
|
||||||
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||||
|
);
|
||||||
|
|
||||||
|
image_states.m_depth_buffer_view = vk::ImageView(
|
||||||
|
m_context,
|
||||||
|
VkImageViewCreateInfo {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
||||||
|
.image = image_states.m_depth_buffer,
|
||||||
|
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
||||||
|
.format = VK_FORMAT_X8_D24_UNORM_PACK32,
|
||||||
|
.components = {
|
||||||
|
.r = VK_COMPONENT_SWIZZLE_R,
|
||||||
|
.g = VK_COMPONENT_SWIZZLE_G,
|
||||||
|
.b = VK_COMPONENT_SWIZZLE_B,
|
||||||
|
.a = VK_COMPONENT_SWIZZLE_A,
|
||||||
|
},
|
||||||
|
.subresourceRange = {
|
||||||
|
.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
|
||||||
|
.baseMipLevel = 0,
|
||||||
|
.levelCount = 1,
|
||||||
|
.baseArrayLayer = 0,
|
||||||
|
.layerCount = 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_framebuffer(ImageStates& image_states) {
|
||||||
|
VkImageView views[] {
|
||||||
|
m_swapchain.image_view(image_states.m_image_index),
|
||||||
|
image_states.m_depth_buffer_view,
|
||||||
|
};
|
||||||
|
image_states.m_framebuffer = vk::Framebuffer(
|
||||||
|
m_context,
|
||||||
|
m_render_pass,
|
||||||
|
views,
|
||||||
|
m_swapchain.extent()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::create_uniform_buffer(ImageStates& image_states) {
|
||||||
|
const auto image_index = image_states.m_image_index;
|
||||||
|
|
||||||
|
image_states.m_uniform_buffer = vk::Buffer(
|
||||||
|
m_context,
|
||||||
|
sizeof(Uniforms),
|
||||||
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
||||||
|
);
|
||||||
|
auto& buffer = image_states.m_uniform_buffer;
|
||||||
|
|
||||||
|
if(!m_uniform_buffer_memory) {
|
||||||
|
VkMemoryRequirements memory_requirements =
|
||||||
|
buffer.memory_requirements();
|
||||||
|
|
||||||
|
m_uniform_buffer_memory = m_context.allocator().allocate(
|
||||||
|
memory_requirements.size * m_swapchain.image_count(),
|
||||||
|
memory_requirements.memoryTypeBits,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
||||||
|
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
||||||
|
);
|
||||||
|
|
||||||
|
for (size_t index = 0; index < m_swapchain.image_count(); index += 1)
|
||||||
|
m_image_states[index].m_uniform_buffer_offset =
|
||||||
|
index * memory_requirements.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.bind_memory(
|
||||||
|
m_uniform_buffer_memory,
|
||||||
|
image_states.m_uniform_buffer_offset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_descriptor_set(ImageStates& image_states) {
|
||||||
|
image_states.m_descriptor_set = vk::DescriptorSet(
|
||||||
|
m_context,
|
||||||
|
m_swapchain_states.m_descriptor_pool,
|
||||||
|
m_descriptor_set_layout
|
||||||
|
);
|
||||||
|
|
||||||
|
VkDescriptorBufferInfo buffer_info {
|
||||||
|
.buffer = image_states.m_uniform_buffer,
|
||||||
|
.offset = 0,
|
||||||
|
.range = sizeof(Uniforms),
|
||||||
|
};
|
||||||
|
VkWriteDescriptorSet write {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||||
|
.dstSet = image_states.m_descriptor_set,
|
||||||
|
.dstBinding = 0,
|
||||||
|
.dstArrayElement = 0,
|
||||||
|
.descriptorCount = 1,
|
||||||
|
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||||
|
.pBufferInfo = &buffer_info,
|
||||||
|
};
|
||||||
|
vkUpdateDescriptorSets(
|
||||||
|
m_context.device(),
|
||||||
|
1, &write,
|
||||||
|
0, nullptr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_draw_buffer(ImageStates& image_states) {
|
||||||
|
image_states.m_draw_buffer = vk::Buffer(
|
||||||
|
m_context,
|
||||||
|
DrawBufferOffset + MaxDrawTileCount * sizeof(VkDrawIndexedIndirectCommand),
|
||||||
|
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
||||||
|
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::create_command_buffer(ImageStates& image_states) {
|
||||||
|
image_states.m_command_buffer = vk::CommandBuffer(m_context, m_command_pool);
|
||||||
|
|
||||||
|
VkCommandBufferBeginInfo begin_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
||||||
|
.flags = 0,
|
||||||
|
.pInheritanceInfo = nullptr,
|
||||||
|
};
|
||||||
|
|
||||||
|
logger.debug() << "record command buffer " << image_states.m_image_index
|
||||||
|
<< " (" << image_states.m_command_buffer << ")";
|
||||||
|
|
||||||
|
if(vkBeginCommandBuffer(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
&begin_info
|
||||||
|
) != VK_SUCCESS)
|
||||||
|
throw std::runtime_error("failed to begin command buffer");
|
||||||
|
|
||||||
|
VkClearValue clear_values[] = {
|
||||||
|
{
|
||||||
|
.color = {
|
||||||
|
.float32 = { 0.0f, 0.0f, 0.0f, 1.0f }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.depthStencil = {
|
||||||
|
.depth = 1.0f,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
VkRenderPassBeginInfo pass_info {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
||||||
|
.renderPass = m_render_pass,
|
||||||
|
.framebuffer = image_states.m_framebuffer,
|
||||||
|
.renderArea = {
|
||||||
|
.offset = { 0, 0 },
|
||||||
|
.extent = m_swapchain.extent(),
|
||||||
|
},
|
||||||
|
.clearValueCount = uint32_t(std::extent_v<typeof(clear_values)>),
|
||||||
|
.pClearValues = clear_values,
|
||||||
|
};
|
||||||
|
|
||||||
|
vkCmdBeginRenderPass(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
&pass_info,
|
||||||
|
VK_SUBPASS_CONTENTS_INLINE
|
||||||
|
);
|
||||||
|
|
||||||
|
vkCmdBindPipeline(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||||
|
m_swapchain_states.m_pipeline
|
||||||
|
);
|
||||||
|
|
||||||
|
VkBuffer vertex_buffers[] = {
|
||||||
|
m_vertex_buffer,
|
||||||
|
};
|
||||||
|
VkDeviceSize offsets[] = {
|
||||||
|
0,
|
||||||
|
};
|
||||||
|
vkCmdBindVertexBuffers(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
vertex_buffers,
|
||||||
|
offsets
|
||||||
|
);
|
||||||
|
|
||||||
|
vkCmdBindIndexBuffer(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
m_index_buffer,
|
||||||
|
0,
|
||||||
|
VK_INDEX_TYPE_UINT32
|
||||||
|
);
|
||||||
|
|
||||||
|
VkDescriptorSet descriptor_sets[] {
|
||||||
|
image_states.m_descriptor_set,
|
||||||
|
};
|
||||||
|
vkCmdBindDescriptorSets(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||||
|
m_pipeline_layout,
|
||||||
|
0, 1, descriptor_sets,
|
||||||
|
0, nullptr
|
||||||
|
);
|
||||||
|
|
||||||
|
m_context.cmdDrawIndexedIndirectCount(
|
||||||
|
image_states.m_command_buffer,
|
||||||
|
image_states.m_draw_buffer,
|
||||||
|
DrawBufferOffset,
|
||||||
|
image_states.m_draw_buffer,
|
||||||
|
0,
|
||||||
|
MaxDrawTileCount,
|
||||||
|
sizeof(VkDrawIndexedIndirectCommand)
|
||||||
|
);
|
||||||
|
|
||||||
|
vkCmdEndRenderPass(image_states.m_command_buffer);
|
||||||
|
|
||||||
|
if(vkEndCommandBuffer(image_states.m_command_buffer) != VK_SUCCESS)
|
||||||
|
throw("failed to record command buffer");
|
||||||
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
// Copyright 2022 Simon Boyé
|
// Copyright 2022 Simon Boyé
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <Camera.h>
|
||||||
|
|
||||||
#include <vk/Swapchain.h>
|
#include <vk/Swapchain.h>
|
||||||
#include <vk/DescriptorSet.h>
|
#include <vk/DescriptorSet.h>
|
||||||
#include <vk/DescriptorPool.h>
|
#include <vk/DescriptorPool.h>
|
||||||
#include <vk/PipelineLayout.h>
|
#include <vk/PipelineLayout.h>
|
||||||
#include <vk/DescriptorSetLayout.h>
|
#include <vk/DescriptorSetLayout.h>
|
||||||
|
#include <vk/Image.h>
|
||||||
#include <vk/Buffer.h>
|
#include <vk/Buffer.h>
|
||||||
#include <vk/Pipeline.h>
|
#include <vk/Pipeline.h>
|
||||||
#include <vk/Framebuffer.h>
|
#include <vk/Framebuffer.h>
|
||||||
@@ -31,7 +34,7 @@ VkVertexInputBindingDescription vertex_binding_description();
|
|||||||
std::array<VkVertexInputAttributeDescription, 4> vertex_attributes_description();
|
std::array<VkVertexInputAttributeDescription, 4> vertex_attributes_description();
|
||||||
|
|
||||||
|
|
||||||
class VulkanTutorial {
|
class Renderer {
|
||||||
public:
|
public:
|
||||||
enum QueueIndex {
|
enum QueueIndex {
|
||||||
GRAPHIC_QUEUE,
|
GRAPHIC_QUEUE,
|
||||||
@@ -43,62 +46,95 @@ public:
|
|||||||
using SecondsD = std::chrono::duration<double>;
|
using SecondsD = std::chrono::duration<double>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VulkanTutorial();
|
Renderer();
|
||||||
VulkanTutorial(const VulkanTutorial&) = delete;
|
Renderer(const Renderer&) = delete;
|
||||||
~VulkanTutorial();
|
~Renderer();
|
||||||
|
|
||||||
VulkanTutorial& operator=(const VulkanTutorial&) = delete;
|
Renderer& operator=(const Renderer&) = delete;
|
||||||
|
|
||||||
void initialize(SDL_Window* window);
|
void initialize(SDL_Window* window);
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
void set_camera(const Vector3& camera_position, const Vector3& camera_z, const Vector3& camera_y);
|
void set_camera(const Camera& camera);
|
||||||
|
|
||||||
void draw_frame();
|
void draw_frame();
|
||||||
void invalidate_swapchain();
|
void invalidate_swapchain();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void create_swapchain_objects(uint32_t image_count);
|
struct SwapchainStates;
|
||||||
void destroy_swapchain_objects();
|
struct ImageStates;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void create_swapchain_objects();
|
||||||
|
|
||||||
void create_render_pass();
|
|
||||||
void create_descriptor_set_layout();
|
|
||||||
void create_graphic_pipeline();
|
|
||||||
void create_framebuffers();
|
|
||||||
void create_command_pool();
|
void create_command_pool();
|
||||||
|
|
||||||
|
void create_descriptor_set_layout();
|
||||||
|
void create_pipeline_layout();
|
||||||
|
void create_render_pass();
|
||||||
|
|
||||||
void create_vertex_buffer();
|
void create_vertex_buffer();
|
||||||
void create_index_buffer();
|
void create_index_buffer();
|
||||||
void create_uniform_buffer();
|
|
||||||
|
void initialize_swapchain_states();
|
||||||
|
|
||||||
void create_descriptor_pool();
|
void create_descriptor_pool();
|
||||||
void create_descriptor_sets();
|
void create_graphic_pipeline();
|
||||||
void create_command_buffers();
|
|
||||||
|
void initialize_image_states(size_t image_index);
|
||||||
|
|
||||||
|
void create_depth_buffer(ImageStates& image_states);
|
||||||
|
void create_framebuffer(ImageStates& image_states);
|
||||||
|
|
||||||
|
void create_uniform_buffer(ImageStates& image_states);
|
||||||
|
void create_descriptor_set(ImageStates& image_states);
|
||||||
|
|
||||||
|
void create_draw_buffer(ImageStates& image_states);
|
||||||
|
|
||||||
|
void create_command_buffer(ImageStates& image_states);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr uint32_t DrawBufferOffset = 4;
|
||||||
|
static constexpr uint32_t MaxDrawTileCount = 256;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
vk::Context m_context;
|
vk::Context m_context;
|
||||||
vk::Swapchain m_swapchain;
|
|
||||||
VkQueue m_graphic_queue = nullptr;
|
|
||||||
VkQueue m_presentation_queue = nullptr;
|
|
||||||
|
|
||||||
vk::RenderPass m_render_pass;
|
vk::CommandPool m_command_pool;
|
||||||
|
|
||||||
vk::DescriptorSetLayout m_descriptor_set_layout;
|
vk::DescriptorSetLayout m_descriptor_set_layout;
|
||||||
vk::PipelineLayout m_pipeline_layout;
|
vk::PipelineLayout m_pipeline_layout;
|
||||||
vk::Pipeline m_pipeline;
|
vk::RenderPass m_render_pass;
|
||||||
std::vector<vk::Framebuffer> m_framebuffers;
|
|
||||||
vk::CommandPool m_command_pool;
|
|
||||||
vk::Buffer m_vertex_buffer;
|
vk::Buffer m_vertex_buffer;
|
||||||
vk::Buffer m_index_buffer;
|
vk::Buffer m_index_buffer;
|
||||||
std::vector<vk::Buffer> m_uniform_buffers;
|
|
||||||
std::vector<VkDeviceSize> m_uniform_buffer_offsets;
|
|
||||||
vk::MemoryBlock m_uniform_buffer_memory;
|
vk::MemoryBlock m_uniform_buffer_memory;
|
||||||
|
|
||||||
|
vk::Swapchain m_swapchain;
|
||||||
|
|
||||||
|
struct SwapchainStates {
|
||||||
vk::DescriptorPool m_descriptor_pool;
|
vk::DescriptorPool m_descriptor_pool;
|
||||||
std::vector<vk::DescriptorSet> m_descriptor_sets;
|
vk::Pipeline m_pipeline;
|
||||||
std::vector<vk::CommandBuffer> m_command_buffers;
|
};
|
||||||
std::vector<vk::Semaphore> m_render_done;
|
SwapchainStates m_swapchain_states;
|
||||||
|
|
||||||
Vector3 m_camera_position = Vector3(0.0f, 0.0f, -3.0f);
|
struct ImageStates {
|
||||||
Vector3 m_camera_z = Vector3(0.0f, 0.0f, 1.0f);
|
size_t m_image_index;
|
||||||
Vector3 m_camera_y = Vector3(0.0f, 1.0f, 0.0f);
|
|
||||||
|
|
||||||
TimePoint m_last_frame_time;
|
vk::Image m_depth_buffer;
|
||||||
Duration m_time = Duration(0);
|
vk::ImageView m_depth_buffer_view;
|
||||||
|
vk::Framebuffer m_framebuffer;
|
||||||
|
|
||||||
|
vk::Buffer m_uniform_buffer;
|
||||||
|
VkDeviceSize m_uniform_buffer_offset;
|
||||||
|
vk::DescriptorSet m_descriptor_set;
|
||||||
|
|
||||||
|
vk::Buffer m_draw_buffer;
|
||||||
|
|
||||||
|
vk::CommandBuffer m_command_buffer;
|
||||||
|
vk::Semaphore m_render_done;
|
||||||
|
};
|
||||||
|
std::vector<ImageStates> m_image_states;
|
||||||
|
|
||||||
|
Camera m_camera;
|
||||||
};
|
};
|
||||||
@@ -15,6 +15,12 @@ void SdlWindowDeleter::operator()(SDL_Window* window) const {
|
|||||||
|
|
||||||
|
|
||||||
VkExpe::VkExpe(int argc, char** argv) {
|
VkExpe::VkExpe(int argc, char** argv) {
|
||||||
|
m_camera.set_position(Vector3(0.0f, 0.0f, -3.0f));
|
||||||
|
m_camera.set_direction(Vector3::UnitZ());
|
||||||
|
m_camera.set_down(Vector3::UnitY());
|
||||||
|
|
||||||
|
m_camera.set_projection(M_PI / 3.0, 1.0);
|
||||||
|
m_camera.set_clip_distances(0.1, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
VkExpe::~VkExpe() {
|
VkExpe::~VkExpe() {
|
||||||
@@ -43,7 +49,7 @@ void VkExpe::initialize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VkExpe::shutdown() {
|
void VkExpe::shutdown() {
|
||||||
m_vulkan.shutdown();
|
recreate_object(m_vulkan);
|
||||||
|
|
||||||
m_window.reset();
|
m_window.reset();
|
||||||
|
|
||||||
@@ -64,9 +70,22 @@ void VkExpe::run() {
|
|||||||
|
|
||||||
auto last_time = std::chrono::high_resolution_clock::now();
|
auto last_time = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
SDL_Event event;
|
// SDL_Event event;
|
||||||
|
std::vector<SDL_Event> events;
|
||||||
|
events.reserve(128);
|
||||||
while(m_running) {
|
while(m_running) {
|
||||||
while(SDL_PollEvent(&event)) {
|
SDL_PumpEvents();
|
||||||
|
|
||||||
|
int event_count = SDL_PeepEvents(nullptr, 0, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
|
||||||
|
if(event_count < 0) {
|
||||||
|
logger.error() << SDL_GetError();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
events.resize(event_count);
|
||||||
|
SDL_PeepEvents(events.data(), event_count, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
|
||||||
|
|
||||||
|
for(const auto& event: events) {
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
m_running = false;
|
m_running = false;
|
||||||
@@ -109,7 +128,7 @@ void VkExpe::run() {
|
|||||||
|
|
||||||
update(std::chrono::duration<double>(elapsed).count());
|
update(std::chrono::duration<double>(elapsed).count());
|
||||||
|
|
||||||
m_vulkan.set_camera(m_camera_position, m_camera_z, m_camera_y);
|
m_vulkan.set_camera(m_camera);
|
||||||
m_vulkan.draw_frame();
|
m_vulkan.draw_frame();
|
||||||
|
|
||||||
m_mouse_offset = Vector2::Zero();
|
m_mouse_offset = Vector2::Zero();
|
||||||
@@ -128,14 +147,16 @@ void VkExpe::update(double elapsed) {
|
|||||||
const Real x_sensi = 0.001;
|
const Real x_sensi = 0.001;
|
||||||
const Real y_sensi = -0.001;
|
const Real y_sensi = -0.001;
|
||||||
|
|
||||||
const Vector3 camera_x = m_camera_y.cross(m_camera_z);
|
const Vector3 camera_z = m_camera.direction();
|
||||||
|
const Vector3 camera_y = m_camera.down();
|
||||||
|
const Vector3 camera_x = camera_y.cross(camera_z);
|
||||||
Vector3 axis =
|
Vector3 axis =
|
||||||
x_sensi * m_mouse_offset[0] * m_camera_y +
|
x_sensi * m_mouse_offset[0] * camera_y +
|
||||||
y_sensi * m_mouse_offset[1] * camera_x;
|
y_sensi * m_mouse_offset[1] * camera_x;
|
||||||
Real rot_norm = axis.norm();
|
Real rot_norm = axis.norm();
|
||||||
AngleAxis rot(rot_norm, axis / rot_norm);
|
AngleAxis rot(rot_norm, axis / rot_norm);
|
||||||
m_camera_y = rot * m_camera_y;
|
m_camera.set_down(rot * camera_y);
|
||||||
m_camera_z = rot * m_camera_z;
|
m_camera.set_direction(rot * camera_z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 walk_direction = Vector3::Zero();
|
Vector3 walk_direction = Vector3::Zero();
|
||||||
@@ -156,8 +177,9 @@ void VkExpe::update(double elapsed) {
|
|||||||
walk_direction.normalize();
|
walk_direction.normalize();
|
||||||
const Real base_velocity = 1;
|
const Real base_velocity = 1;
|
||||||
|
|
||||||
Matrix3 camera_basis;
|
const Matrix3 basis = m_camera.basis();
|
||||||
camera_basis << m_camera_y.cross(m_camera_z), m_camera_y, m_camera_z;
|
m_camera.set_position(
|
||||||
m_camera_position += elapsed * base_velocity * (camera_basis * walk_direction);
|
m_camera.position() + elapsed * base_velocity * (basis * walk_direction)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
// Copyright 2022 Simon Boyé
|
// Copyright 2022 Simon Boyé
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <VulkanTutorial.h>
|
#include <Renderer.h>
|
||||||
|
#include <Camera.h>
|
||||||
|
|
||||||
#include <core/math.h>
|
#include <core/math.h>
|
||||||
|
|
||||||
@@ -32,11 +33,9 @@ public:
|
|||||||
void update(double elapsed);
|
void update(double elapsed);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VulkanTutorial m_vulkan;
|
Renderer m_vulkan;
|
||||||
|
|
||||||
Vector3 m_camera_position = Vector3(0.0f, 0.0f, -3.0f);
|
Camera m_camera;
|
||||||
Vector3 m_camera_z = Vector3(0.0f, 0.0f, 1.0f);
|
|
||||||
Vector3 m_camera_y = Vector3(0.0f, 1.0f, 0.0f);
|
|
||||||
|
|
||||||
WindowUP m_window;
|
WindowUP m_window;
|
||||||
bool m_running = false;
|
bool m_running = false;
|
||||||
|
|||||||
@@ -1,748 +0,0 @@
|
|||||||
// Copyright 2022 Simon Boyé
|
|
||||||
#include <VulkanTutorial.h>
|
|
||||||
|
|
||||||
#include <Planet.h>
|
|
||||||
|
|
||||||
#include <vk/ShaderModule.h>
|
|
||||||
|
|
||||||
#include <core/utils.h>
|
|
||||||
#include <core/Logger.h>
|
|
||||||
|
|
||||||
#include <SDL2/SDL_vulkan.h>
|
|
||||||
|
|
||||||
#include <Eigen/Geometry>
|
|
||||||
|
|
||||||
#include <cassert>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <array>
|
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
|
|
||||||
VkVertexInputBindingDescription vertex_binding_description() {
|
|
||||||
return {
|
|
||||||
.binding = 0,
|
|
||||||
.stride = sizeof(Vertex),
|
|
||||||
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<VkVertexInputAttributeDescription, 4> vertex_attributes_description() {
|
|
||||||
return {
|
|
||||||
VkVertexInputAttributeDescription {
|
|
||||||
.location = 0,
|
|
||||||
.binding = 0,
|
|
||||||
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
|
||||||
.offset = offsetof(Vertex, position),
|
|
||||||
},
|
|
||||||
VkVertexInputAttributeDescription {
|
|
||||||
.location = 1,
|
|
||||||
.binding = 0,
|
|
||||||
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
|
||||||
.offset = offsetof(Vertex, position2),
|
|
||||||
},
|
|
||||||
VkVertexInputAttributeDescription {
|
|
||||||
.location = 2,
|
|
||||||
.binding = 0,
|
|
||||||
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
|
||||||
.offset = offsetof(Vertex, normal),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.location = 3,
|
|
||||||
.binding = 0,
|
|
||||||
.format = VK_FORMAT_R32G32B32_SFLOAT,
|
|
||||||
.offset = offsetof(Vertex, color),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<Vertex> vertices = {
|
|
||||||
// {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
|
|
||||||
// {{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
|
|
||||||
// {{-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}},
|
|
||||||
// {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}},
|
|
||||||
|
|
||||||
// {{-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f, 1.0f}},
|
|
||||||
// {{0.5f, -0.5f, 0.5f}, {1.0f, 0.0f, 1.0f}},
|
|
||||||
// {{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 0.0f}},
|
|
||||||
// {{0.5f, 0.5f, 0.5f}, {0.2f, 0.2f, 0.2f}},
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<uint32_t> indices = {
|
|
||||||
// 0, 1, 2,
|
|
||||||
// 2, 1, 3,
|
|
||||||
|
|
||||||
// 1, 5, 3,
|
|
||||||
// 3, 5, 7,
|
|
||||||
|
|
||||||
// 5, 4, 7,
|
|
||||||
// 7, 4, 6,
|
|
||||||
|
|
||||||
// 4, 0, 6,
|
|
||||||
// 6, 0, 2,
|
|
||||||
|
|
||||||
// 0, 4, 1,
|
|
||||||
// 1, 4, 5,
|
|
||||||
|
|
||||||
// 2, 3, 6,
|
|
||||||
// 6, 3, 7,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Uniforms {
|
|
||||||
alignas(16) Eigen::Matrix4f scene_from_model;
|
|
||||||
alignas(16) Eigen::Matrix4f projection_from_scene;
|
|
||||||
alignas(8) Eigen::Vector2f half_screen_size;
|
|
||||||
alignas(4) float lod;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
VulkanTutorial::VulkanTutorial() {
|
|
||||||
m_swapchain.register_creation_callback(
|
|
||||||
std::bind(&VulkanTutorial::create_swapchain_objects, this, std::placeholders::_1));
|
|
||||||
m_swapchain.register_destruction_callback(
|
|
||||||
std::bind(&VulkanTutorial::destroy_swapchain_objects, this));
|
|
||||||
|
|
||||||
auto const subdiv_count = 4;
|
|
||||||
|
|
||||||
vertices.resize(6 * Cell::vertex_count(subdiv_count));
|
|
||||||
indices.resize(6 * 3 * Cell::triangle_count(subdiv_count));
|
|
||||||
|
|
||||||
Vector3AV positions(
|
|
||||||
vertices.size(), vertices.data(),
|
|
||||||
sizeof(Vertex), offsetof(Vertex, position)
|
|
||||||
);
|
|
||||||
|
|
||||||
Vector3AV positions2(
|
|
||||||
vertices.size(), vertices.data(),
|
|
||||||
sizeof(Vertex), offsetof(Vertex, position2)
|
|
||||||
);
|
|
||||||
|
|
||||||
Vector3AV normals(
|
|
||||||
vertices.size(), vertices.data(),
|
|
||||||
sizeof(Vertex), offsetof(Vertex, normal)
|
|
||||||
);
|
|
||||||
|
|
||||||
Vector3AV colors(
|
|
||||||
vertices.size(), vertices.data(),
|
|
||||||
sizeof(Vertex), offsetof(Vertex, color)
|
|
||||||
);
|
|
||||||
|
|
||||||
TriangleAV triangles(
|
|
||||||
indices.size() / 3,
|
|
||||||
indices.data()
|
|
||||||
);
|
|
||||||
|
|
||||||
Planet planet;
|
|
||||||
planet.build_mesh(positions, positions2, normals, triangles, subdiv_count);
|
|
||||||
|
|
||||||
for (size_t vertex_index = 0; vertex_index < vertices.size(); vertex_index += 1) {
|
|
||||||
colors[vertex_index] =
|
|
||||||
positions[vertex_index] / 2.0f + Vector3::Constant(0.5f);
|
|
||||||
}
|
|
||||||
|
|
||||||
// for(size_t vi = 0; vi < vertices.size(); vi += 1)
|
|
||||||
// logger.debug() << "v" << vi << ": "
|
|
||||||
// << vertices[vi].position.transpose();
|
|
||||||
// for(size_t ii = 0; 3 * ii < indices.size(); ii += 1)
|
|
||||||
// logger.debug() << "i" << ii << ": "
|
|
||||||
// << indices[3 * ii + 0] << ", "
|
|
||||||
// << indices[3 * ii + 1] << ", "
|
|
||||||
// << indices[3 * ii + 2];
|
|
||||||
// abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
VulkanTutorial::~VulkanTutorial() {
|
|
||||||
shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::initialize(SDL_Window* window) {
|
|
||||||
auto const context_settings = vk::ContextSettings()
|
|
||||||
#if defined(VKEXPE_ENABLE_VALIDATION) || !defined(NDEBUG)
|
|
||||||
.with_debug(true)
|
|
||||||
#endif
|
|
||||||
.with_queue(GRAPHIC_QUEUE, VK_QUEUE_GRAPHICS_BIT)
|
|
||||||
.with_window(window);
|
|
||||||
m_context.initialize(context_settings);
|
|
||||||
|
|
||||||
create_command_pool();
|
|
||||||
create_descriptor_set_layout();
|
|
||||||
create_vertex_buffer();
|
|
||||||
create_index_buffer();
|
|
||||||
|
|
||||||
auto const swapchain_settings = vk::SwapchainSettings(&m_context)
|
|
||||||
.with_queue(m_context.queue_family(GRAPHIC_QUEUE));
|
|
||||||
m_swapchain.initialize(swapchain_settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::shutdown() {
|
|
||||||
if(!m_context.instance())
|
|
||||||
return;
|
|
||||||
|
|
||||||
vkDeviceWaitIdle(m_context.device());
|
|
||||||
|
|
||||||
destroy_swapchain_objects();
|
|
||||||
|
|
||||||
m_command_pool.destroy();
|
|
||||||
m_index_buffer.destroy();
|
|
||||||
m_vertex_buffer.destroy();
|
|
||||||
m_descriptor_set_layout.destroy();
|
|
||||||
|
|
||||||
m_render_done.clear();
|
|
||||||
|
|
||||||
m_swapchain.shutdown();
|
|
||||||
m_context.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::set_camera(const Vector3& camera_position, const Vector3& camera_z, const Vector3& camera_y) {
|
|
||||||
m_camera_position = camera_position;
|
|
||||||
m_camera_z = camera_z;
|
|
||||||
m_camera_y = camera_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::draw_frame() {
|
|
||||||
m_swapchain.begin_frame();
|
|
||||||
auto const image_index = m_swapchain.current_image_index();
|
|
||||||
|
|
||||||
const auto now = Clock::now();
|
|
||||||
if(m_last_frame_time.time_since_epoch() != Duration(0)) {
|
|
||||||
m_time += now - m_last_frame_time;
|
|
||||||
}
|
|
||||||
m_last_frame_time = now;
|
|
||||||
|
|
||||||
Matrix3 linear_view;
|
|
||||||
linear_view <<
|
|
||||||
m_camera_y.cross(m_camera_z).transpose(),
|
|
||||||
m_camera_y.transpose(),
|
|
||||||
m_camera_z.transpose();
|
|
||||||
Matrix4 view;
|
|
||||||
view << linear_view, linear_view * -m_camera_position,
|
|
||||||
Vector4::UnitW().transpose();
|
|
||||||
|
|
||||||
const float fov = M_PI / 3.0f;
|
|
||||||
const float near = 0.1f;
|
|
||||||
const float far = 10.0f;
|
|
||||||
const float hx = near * std::tan(fov / 2.0f);
|
|
||||||
const float hy = hx * m_swapchain.extent().height / m_swapchain.extent().width;
|
|
||||||
const Eigen::Matrix4f proj = projection_matrix(
|
|
||||||
-hx, hx,
|
|
||||||
-hy, hy,
|
|
||||||
near, far
|
|
||||||
);
|
|
||||||
|
|
||||||
Transform model = Transform::Identity();
|
|
||||||
|
|
||||||
const Uniforms uniforms = {
|
|
||||||
.scene_from_model = model.matrix(),
|
|
||||||
.projection_from_scene = proj * view,
|
|
||||||
.half_screen_size = {
|
|
||||||
0.5 * m_swapchain.extent().width,
|
|
||||||
0.5 * m_swapchain.extent().height,
|
|
||||||
},
|
|
||||||
// .lod = std::cos(alpha) * 0.5f + 0.5f,
|
|
||||||
};
|
|
||||||
|
|
||||||
void* uniform_buffer = m_uniform_buffer_memory.map(
|
|
||||||
m_context,
|
|
||||||
m_uniform_buffer_offsets[image_index],
|
|
||||||
sizeof(Uniforms)
|
|
||||||
);
|
|
||||||
std::memcpy(uniform_buffer, &uniforms, sizeof(Uniforms));
|
|
||||||
m_uniform_buffer_memory.unmap(m_context);
|
|
||||||
|
|
||||||
VkSemaphore wait_semaphores[] = {
|
|
||||||
m_swapchain.ready_to_render(),
|
|
||||||
};
|
|
||||||
VkSemaphore done_semaphores[] = {
|
|
||||||
m_render_done[image_index],
|
|
||||||
};
|
|
||||||
VkPipelineStageFlags stages[] = {
|
|
||||||
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
};
|
|
||||||
VkCommandBuffer command_buffers[] = {
|
|
||||||
m_command_buffers[image_index],
|
|
||||||
};
|
|
||||||
VkSubmitInfo submit_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
|
||||||
.waitSemaphoreCount = 1,
|
|
||||||
.pWaitSemaphores = wait_semaphores,
|
|
||||||
.pWaitDstStageMask = stages,
|
|
||||||
.commandBufferCount = 1,
|
|
||||||
.pCommandBuffers = command_buffers,
|
|
||||||
.signalSemaphoreCount = 1,
|
|
||||||
.pSignalSemaphores = done_semaphores,
|
|
||||||
};
|
|
||||||
|
|
||||||
if(vkQueueSubmit(
|
|
||||||
m_context.queue(GRAPHIC_QUEUE),
|
|
||||||
1, &submit_info,
|
|
||||||
m_swapchain.render_done()
|
|
||||||
))
|
|
||||||
throw std::runtime_error("failed to submit draw command buffer");
|
|
||||||
|
|
||||||
m_swapchain.swap_buffers(done_semaphores);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::invalidate_swapchain() {
|
|
||||||
m_swapchain.invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_swapchain_objects(uint32_t image_count) {
|
|
||||||
create_render_pass();
|
|
||||||
create_framebuffers();
|
|
||||||
create_uniform_buffer();
|
|
||||||
create_descriptor_pool();
|
|
||||||
create_descriptor_sets();
|
|
||||||
create_graphic_pipeline();
|
|
||||||
create_command_buffers();
|
|
||||||
|
|
||||||
m_render_done.clear();
|
|
||||||
for(size_t index = 0; index < m_swapchain.image_count(); index += 1) {
|
|
||||||
m_render_done.emplace_back(m_context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::destroy_swapchain_objects() {
|
|
||||||
if(m_command_pool == VK_NULL_HANDLE)
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_command_buffers.clear();
|
|
||||||
|
|
||||||
for(auto& buffer: m_uniform_buffers)
|
|
||||||
buffer.destroy();
|
|
||||||
m_uniform_buffers.clear();
|
|
||||||
m_uniform_buffer_memory.free();
|
|
||||||
|
|
||||||
m_descriptor_pool.destroy();
|
|
||||||
|
|
||||||
m_framebuffers.clear();
|
|
||||||
|
|
||||||
m_pipeline.destroy();
|
|
||||||
m_pipeline_layout.destroy();
|
|
||||||
m_render_pass.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_render_pass() {
|
|
||||||
VkAttachmentDescription color_attachment {
|
|
||||||
.format = m_context.surface_format().format,
|
|
||||||
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
||||||
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
|
|
||||||
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
|
||||||
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
|
||||||
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
|
||||||
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
|
||||||
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkAttachmentReference color_attachment_ref = {
|
|
||||||
.attachment = 0,
|
|
||||||
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkSubpassDescription subpass {
|
|
||||||
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
||||||
.colorAttachmentCount = 1,
|
|
||||||
.pColorAttachments = &color_attachment_ref,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkSubpassDependency subpass_dep = {
|
|
||||||
.srcSubpass = VK_SUBPASS_EXTERNAL,
|
|
||||||
.dstSubpass = 0,
|
|
||||||
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
|
|
||||||
.srcAccessMask = 0,
|
|
||||||
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkRenderPassCreateInfo render_pass_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
|
|
||||||
.attachmentCount = 1,
|
|
||||||
.pAttachments = &color_attachment,
|
|
||||||
.subpassCount = 1,
|
|
||||||
.pSubpasses = &subpass,
|
|
||||||
.dependencyCount = 1,
|
|
||||||
.pDependencies = &subpass_dep,
|
|
||||||
};
|
|
||||||
|
|
||||||
m_render_pass = vk::RenderPass(m_context, render_pass_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_descriptor_set_layout() {
|
|
||||||
VkDescriptorSetLayoutBinding binding[] {
|
|
||||||
{
|
|
||||||
.binding = 0,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
m_descriptor_set_layout = vk::DescriptorSetLayout(m_context, binding);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_graphic_pipeline() {
|
|
||||||
auto vertex_shader_module = vk::ShaderModule(m_context, "shaders/shader.vert.spv");
|
|
||||||
auto geometry_shader_module = vk::ShaderModule(m_context, "shaders/shader.geom.spv");
|
|
||||||
auto fragment_shader_module = vk::ShaderModule(m_context, "shaders/shader.frag.spv");
|
|
||||||
|
|
||||||
VkPipelineShaderStageCreateInfo shader_stage_infos[] = {
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
||||||
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
|
||||||
.module = vertex_shader_module,
|
|
||||||
.pName = "main",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
||||||
.stage = VK_SHADER_STAGE_GEOMETRY_BIT,
|
|
||||||
.module = geometry_shader_module,
|
|
||||||
.pName = "main",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
|
||||||
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
.module = fragment_shader_module,
|
|
||||||
.pName = "main",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const VkVertexInputBindingDescription vertex_bindings[] = {
|
|
||||||
vertex_binding_description(),
|
|
||||||
};
|
|
||||||
const auto vertex_attributes = vertex_attributes_description();
|
|
||||||
|
|
||||||
VkPipelineVertexInputStateCreateInfo vertex_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
|
|
||||||
.vertexBindingDescriptionCount = 1,
|
|
||||||
.pVertexBindingDescriptions = vertex_bindings,
|
|
||||||
.vertexAttributeDescriptionCount = uint32_t(vertex_attributes.size()),
|
|
||||||
.pVertexAttributeDescriptions = vertex_attributes.data(),
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo assembly_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
|
|
||||||
.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
|
||||||
.primitiveRestartEnable = VK_FALSE,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkViewport viewport {
|
|
||||||
.x = 0.0f,
|
|
||||||
.y = 0.0f,
|
|
||||||
.width = float(m_swapchain.extent().width),
|
|
||||||
.height = float(m_swapchain.extent().height),
|
|
||||||
.minDepth = 0.0f,
|
|
||||||
.maxDepth = 1.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkRect2D scissor = {
|
|
||||||
.offset = { 0, 0 },
|
|
||||||
.extent = m_swapchain.extent(),
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineViewportStateCreateInfo viewport_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
|
|
||||||
.viewportCount = 1,
|
|
||||||
.pViewports = &viewport,
|
|
||||||
.scissorCount = 1,
|
|
||||||
.pScissors = &scissor,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineRasterizationStateCreateInfo rasterization_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
|
|
||||||
.depthClampEnable = VK_FALSE,
|
|
||||||
.rasterizerDiscardEnable = VK_FALSE,
|
|
||||||
.polygonMode = VK_POLYGON_MODE_FILL,
|
|
||||||
// .cullMode = VK_CULL_MODE_NONE,
|
|
||||||
.cullMode = VK_CULL_MODE_BACK_BIT,
|
|
||||||
.frontFace = VK_FRONT_FACE_CLOCKWISE,
|
|
||||||
.depthBiasEnable = VK_FALSE,
|
|
||||||
.depthBiasConstantFactor = 0.0f,
|
|
||||||
.depthBiasClamp = 0.0f,
|
|
||||||
.depthBiasSlopeFactor = 0.0f,
|
|
||||||
.lineWidth = 1.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineMultisampleStateCreateInfo multisample_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
|
|
||||||
.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
|
|
||||||
.sampleShadingEnable = VK_FALSE,
|
|
||||||
.minSampleShading = 1.0f,
|
|
||||||
.pSampleMask = nullptr,
|
|
||||||
.alphaToCoverageEnable = VK_FALSE,
|
|
||||||
.alphaToOneEnable = VK_FALSE,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineColorBlendAttachmentState color_blend_attachment {
|
|
||||||
.blendEnable = VK_FALSE,
|
|
||||||
.srcColorBlendFactor = VK_BLEND_FACTOR_ONE,
|
|
||||||
.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO,
|
|
||||||
.colorBlendOp = VK_BLEND_OP_ADD,
|
|
||||||
.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE,
|
|
||||||
.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO,
|
|
||||||
.alphaBlendOp = VK_BLEND_OP_ADD,
|
|
||||||
.colorWriteMask =
|
|
||||||
VK_COLOR_COMPONENT_R_BIT |
|
|
||||||
VK_COLOR_COMPONENT_G_BIT |
|
|
||||||
VK_COLOR_COMPONENT_B_BIT |
|
|
||||||
VK_COLOR_COMPONENT_A_BIT,
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPipelineColorBlendStateCreateInfo color_blend_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
|
|
||||||
.logicOpEnable = VK_FALSE,
|
|
||||||
.logicOp = VK_LOGIC_OP_COPY,
|
|
||||||
.attachmentCount = 1,
|
|
||||||
.pAttachments = &color_blend_attachment,
|
|
||||||
.blendConstants = { 0.0f, 0.0f, 0.0f, 0.0f },
|
|
||||||
};
|
|
||||||
|
|
||||||
VkDescriptorSetLayout set_layouts[] {
|
|
||||||
m_descriptor_set_layout,
|
|
||||||
};
|
|
||||||
m_pipeline_layout = vk::PipelineLayout(m_context, set_layouts);
|
|
||||||
|
|
||||||
VkGraphicsPipelineCreateInfo pipeline_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
|
|
||||||
.stageCount = 3,
|
|
||||||
.pStages = shader_stage_infos,
|
|
||||||
.pVertexInputState = &vertex_info,
|
|
||||||
.pInputAssemblyState = &assembly_info,
|
|
||||||
.pViewportState = &viewport_info,
|
|
||||||
.pRasterizationState = &rasterization_info,
|
|
||||||
.pMultisampleState = &multisample_info,
|
|
||||||
.pDepthStencilState = nullptr,
|
|
||||||
.pColorBlendState = &color_blend_info,
|
|
||||||
.pDynamicState = nullptr,
|
|
||||||
.layout = m_pipeline_layout,
|
|
||||||
.renderPass = m_render_pass,
|
|
||||||
.subpass = 0,
|
|
||||||
.basePipelineHandle = VK_NULL_HANDLE,
|
|
||||||
.basePipelineIndex = -1,
|
|
||||||
};
|
|
||||||
|
|
||||||
m_pipeline = vk::Pipeline(m_context, pipeline_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_framebuffers() {
|
|
||||||
m_framebuffers.clear();
|
|
||||||
for(size_t index = 0; index < m_swapchain.image_count(); index += 1) {
|
|
||||||
VkImageView view = m_swapchain.image_view(index);
|
|
||||||
m_framebuffers.emplace_back(
|
|
||||||
m_context,
|
|
||||||
m_render_pass,
|
|
||||||
&view,
|
|
||||||
m_swapchain.extent()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_command_pool() {
|
|
||||||
if(!m_command_pool.is_null())
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_command_pool = vk::CommandPool(
|
|
||||||
m_context,
|
|
||||||
m_context.queue_family(GRAPHIC_QUEUE)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_vertex_buffer() {
|
|
||||||
if(m_vertex_buffer)
|
|
||||||
return;
|
|
||||||
|
|
||||||
VkDeviceSize size = sizeof(vertices[0]) * vertices.size();
|
|
||||||
m_vertex_buffer = vk::Buffer(
|
|
||||||
m_context,
|
|
||||||
size,
|
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
|
|
||||||
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
|
||||||
);
|
|
||||||
m_vertex_buffer.upload(size, vertices.data(), m_context.queue_family(GRAPHIC_QUEUE));
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_index_buffer() {
|
|
||||||
if(m_index_buffer)
|
|
||||||
return;
|
|
||||||
|
|
||||||
VkDeviceSize size = sizeof(indices[0]) * indices.size();
|
|
||||||
m_index_buffer = vk::Buffer(
|
|
||||||
m_context,
|
|
||||||
size,
|
|
||||||
VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
|
||||||
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
|
||||||
);
|
|
||||||
m_index_buffer.upload(size, indices.data(), m_context.queue_family(GRAPHIC_QUEUE));
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_uniform_buffer() {
|
|
||||||
m_uniform_buffers.resize(m_swapchain.image_count());
|
|
||||||
for(size_t index = 0; index < m_uniform_buffers.size(); index += 1) {
|
|
||||||
m_uniform_buffers[index] = vk::Buffer(
|
|
||||||
m_context,
|
|
||||||
sizeof(Uniforms),
|
|
||||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkMemoryRequirements memory_requirements =
|
|
||||||
m_uniform_buffers[0].memory_requirements();
|
|
||||||
|
|
||||||
m_uniform_buffer_memory = m_context.allocator().allocate(
|
|
||||||
memory_requirements.size * m_swapchain.image_count(),
|
|
||||||
memory_requirements.memoryTypeBits,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
|
||||||
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
|
||||||
);
|
|
||||||
|
|
||||||
m_uniform_buffer_offsets.resize(m_uniform_buffers.size());
|
|
||||||
for(size_t index = 0; index < m_uniform_buffers.size(); index += 1) {
|
|
||||||
m_uniform_buffer_offsets[index] = index * memory_requirements.size;
|
|
||||||
m_uniform_buffers[index].bind_memory(
|
|
||||||
m_uniform_buffer_memory,
|
|
||||||
m_uniform_buffer_offsets[index]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_descriptor_pool() {
|
|
||||||
VkDescriptorPoolSize pool_sizes[] {
|
|
||||||
{
|
|
||||||
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
.descriptorCount = uint32_t(m_swapchain.image_count()),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
m_descriptor_pool = vk::DescriptorPool(
|
|
||||||
m_context,
|
|
||||||
uint32_t(m_swapchain.image_count()),
|
|
||||||
pool_sizes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_descriptor_sets() {
|
|
||||||
m_descriptor_sets.clear();
|
|
||||||
for(size_t index = 0; index < m_swapchain.image_count(); index += 1) {
|
|
||||||
m_descriptor_sets.emplace_back(m_context, m_descriptor_pool, m_descriptor_set_layout);
|
|
||||||
VkDescriptorBufferInfo buffer_info {
|
|
||||||
.buffer = m_uniform_buffers[index],
|
|
||||||
.offset = 0,
|
|
||||||
.range = sizeof(Uniforms),
|
|
||||||
};
|
|
||||||
|
|
||||||
VkWriteDescriptorSet write {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
||||||
.dstSet = m_descriptor_sets[index],
|
|
||||||
.dstBinding = 0,
|
|
||||||
.dstArrayElement = 0,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
||||||
.pBufferInfo = &buffer_info,
|
|
||||||
};
|
|
||||||
vkUpdateDescriptorSets(
|
|
||||||
m_context.device(),
|
|
||||||
1, &write,
|
|
||||||
0, nullptr
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanTutorial::create_command_buffers() {
|
|
||||||
m_command_buffers.clear();
|
|
||||||
for(size_t index = 0; index < m_framebuffers.size(); index += 1) {
|
|
||||||
m_command_buffers.emplace_back(m_context, m_command_pool);
|
|
||||||
VkCommandBuffer command_buffer = m_command_buffers[index];
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo begin_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
||||||
.flags = 0,
|
|
||||||
.pInheritanceInfo = nullptr,
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.debug() << "record command buffer " << index << " (" << command_buffer << ")";
|
|
||||||
|
|
||||||
if(vkBeginCommandBuffer(
|
|
||||||
command_buffer,
|
|
||||||
&begin_info
|
|
||||||
) != VK_SUCCESS)
|
|
||||||
throw std::runtime_error("failed to begin command buffer");
|
|
||||||
|
|
||||||
VkClearValue clear_color = {
|
|
||||||
.color = {
|
|
||||||
.float32 = { 0.0f, 0.0f, 0.0f, 1.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
VkRenderPassBeginInfo pass_info {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
|
||||||
.renderPass = m_render_pass,
|
|
||||||
.framebuffer = m_framebuffers[index],
|
|
||||||
.renderArea = {
|
|
||||||
.offset = { 0, 0 },
|
|
||||||
.extent = m_swapchain.extent(),
|
|
||||||
},
|
|
||||||
.clearValueCount = 1,
|
|
||||||
.pClearValues = &clear_color,
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdBeginRenderPass(
|
|
||||||
command_buffer,
|
|
||||||
&pass_info,
|
|
||||||
VK_SUBPASS_CONTENTS_INLINE
|
|
||||||
);
|
|
||||||
|
|
||||||
vkCmdBindPipeline(
|
|
||||||
command_buffer,
|
|
||||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
||||||
m_pipeline
|
|
||||||
);
|
|
||||||
|
|
||||||
VkBuffer vertex_buffers[] = {
|
|
||||||
m_vertex_buffer,
|
|
||||||
};
|
|
||||||
VkDeviceSize offsets[] = {
|
|
||||||
0,
|
|
||||||
};
|
|
||||||
vkCmdBindVertexBuffers(
|
|
||||||
command_buffer,
|
|
||||||
0,
|
|
||||||
1,
|
|
||||||
vertex_buffers,
|
|
||||||
offsets
|
|
||||||
);
|
|
||||||
|
|
||||||
vkCmdBindIndexBuffer(
|
|
||||||
command_buffer,
|
|
||||||
m_index_buffer,
|
|
||||||
0,
|
|
||||||
VK_INDEX_TYPE_UINT32
|
|
||||||
);
|
|
||||||
|
|
||||||
VkDescriptorSet descriptor_sets[] {
|
|
||||||
m_descriptor_sets[index],
|
|
||||||
};
|
|
||||||
vkCmdBindDescriptorSets(
|
|
||||||
command_buffer,
|
|
||||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
||||||
m_pipeline_layout,
|
|
||||||
0, 1, descriptor_sets,
|
|
||||||
0, nullptr
|
|
||||||
);
|
|
||||||
|
|
||||||
vkCmdDrawIndexed(
|
|
||||||
command_buffer,
|
|
||||||
uint32_t(indices.size()),
|
|
||||||
1,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
vkCmdEndRenderPass(command_buffer);
|
|
||||||
|
|
||||||
if(vkEndCommandBuffer(command_buffer) != VK_SUCCESS)
|
|
||||||
throw("failed to record command buffer");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,12 +11,12 @@ Eigen::Matrix4f projection_matrix(float x_min, float x_max, float y_min, float y
|
|||||||
auto const dy = y_max - y_min;
|
auto const dy = y_max - y_min;
|
||||||
auto const dz = z_max - z_min;
|
auto const dz = z_max - z_min;
|
||||||
|
|
||||||
auto const pz = z_max * z_min;
|
auto const sz = z_max / dz;
|
||||||
|
|
||||||
return (Eigen::Matrix4f() <<
|
return (Eigen::Matrix4f() <<
|
||||||
2.0f * z_min / dx, 0.0f, -cx / dx, 0.0f,
|
2.0f * z_min / dx, 0.0f, -cx / dx, 0.0f,
|
||||||
0.0f, 2.0f * z_min / dy, -cy / dy, 0.0f,
|
0.0f, 2.0f * z_min / dy, -cy / dy, 0.0f,
|
||||||
0.0f, 0.0f, cz / dz, -2.0f * pz / dz,
|
0.0f, 0.0f, sz, (1 - sz) * z_max,
|
||||||
0.0f, 0.0f, 1.0f, 0.0f
|
0.0f, 0.0f, 1.0f, 0.0f
|
||||||
).finished();
|
).finished();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,14 @@ Guard<T> make_guard(T&& teardown) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T& recreate_object(T& object) {
|
||||||
|
object.~T();
|
||||||
|
new(&object) T;
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
std::string cat(Args&&... args) {
|
std::string cat(Args&&... args) {
|
||||||
std::ostringstream ostream;
|
std::ostringstream ostream;
|
||||||
@@ -57,10 +65,10 @@ public:
|
|||||||
assert((size == 0 && m_data == nullptr) || (size != 0 && m_data != nullptr));
|
assert((size == 0 && m_data == nullptr) || (size != 0 && m_data != nullptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
Array(T* item)
|
// Array(T* item)
|
||||||
: m_size(1)
|
// : m_size(1)
|
||||||
, m_data(item)
|
// , m_data(item)
|
||||||
{}
|
// {}
|
||||||
|
|
||||||
template<size_t Size>
|
template<size_t Size>
|
||||||
Array(T (&array)[Size])
|
Array(T (&array)[Size])
|
||||||
|
|||||||
@@ -56,10 +56,8 @@ Buffer::Buffer(Buffer&& other) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Buffer::~Buffer() noexcept {
|
Buffer::~Buffer() noexcept {
|
||||||
if(!is_null()) {
|
if(!is_null())
|
||||||
logger.warning() << "Buffer deleted before being destroyed";
|
|
||||||
destroy();
|
destroy();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer& Buffer::operator=(Buffer&& other) noexcept {
|
Buffer& Buffer::operator=(Buffer&& other) noexcept {
|
||||||
|
|||||||
@@ -562,7 +562,7 @@ void Context::create_instance(const ContextSettings& settings) {
|
|||||||
if(vkCreateInstance(&instance_info, nullptr, &m_instance) != VK_SUCCESS)
|
if(vkCreateInstance(&instance_info, nullptr, &m_instance) != VK_SUCCESS)
|
||||||
throw std::runtime_error("failed to create vulkan instance");
|
throw std::runtime_error("failed to create vulkan instance");
|
||||||
|
|
||||||
initialize_extension_functions();
|
initialize_instance_extension_functions();
|
||||||
|
|
||||||
if(settings.debug()) {
|
if(settings.debug()) {
|
||||||
createDebugUtilsMessenger(
|
createDebugUtilsMessenger(
|
||||||
@@ -733,7 +733,9 @@ void Context::create_device(const ContextSettings& settings) {
|
|||||||
.geometryShader = true,
|
.geometryShader = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<const char*> extensions;
|
std::vector<const char*> extensions {
|
||||||
|
VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME,
|
||||||
|
};
|
||||||
if(m_window) {
|
if(m_window) {
|
||||||
extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
||||||
};
|
};
|
||||||
@@ -756,6 +758,8 @@ void Context::create_device(const ContextSettings& settings) {
|
|||||||
) != VK_SUCCESS)
|
) != VK_SUCCESS)
|
||||||
throw std::runtime_error("failed to create logical device");
|
throw std::runtime_error("failed to create logical device");
|
||||||
|
|
||||||
|
initialize_device_extension_functions();
|
||||||
|
|
||||||
m_queues.resize(m_queue_families.size());
|
m_queues.resize(m_queue_families.size());
|
||||||
for(size_t index = 0; index < m_queue_families.size(); ++index) {
|
for(size_t index = 0; index < m_queue_families.size(); ++index) {
|
||||||
vkGetDeviceQueue(
|
vkGetDeviceQueue(
|
||||||
@@ -777,10 +781,10 @@ void Context::create_internal_objects() {
|
|||||||
m_transfer_fence = Fence(*this);
|
m_transfer_fence = Fence(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Context::initialize_extension_functions() {
|
void Context::initialize_instance_extension_functions() {
|
||||||
uint32_t errors_count = 0;
|
uint32_t errors_count = 0;
|
||||||
|
|
||||||
#define GET_PROC_ADDR(ptr_name, func_name) \
|
#define GET_INSTANCE_PROC_ADDR(ptr_name, func_name) \
|
||||||
ptr_name = (PFN_vk ## func_name)vkGetInstanceProcAddr( \
|
ptr_name = (PFN_vk ## func_name)vkGetInstanceProcAddr( \
|
||||||
m_instance, "vk" #func_name); \
|
m_instance, "vk" #func_name); \
|
||||||
if(ptr_name == nullptr) \
|
if(ptr_name == nullptr) \
|
||||||
@@ -789,9 +793,27 @@ void Context::initialize_extension_functions() {
|
|||||||
errors_count += 1; \
|
errors_count += 1; \
|
||||||
}
|
}
|
||||||
|
|
||||||
GET_PROC_ADDR(createDebugUtilsMessenger, CreateDebugUtilsMessengerEXT)
|
GET_INSTANCE_PROC_ADDR(createDebugUtilsMessenger, CreateDebugUtilsMessengerEXT)
|
||||||
GET_PROC_ADDR(destroyDebugUtilsMessenger, DestroyDebugUtilsMessengerEXT)
|
GET_INSTANCE_PROC_ADDR(destroyDebugUtilsMessenger, DestroyDebugUtilsMessengerEXT)
|
||||||
GET_PROC_ADDR(setDebugUtilsObjectName, SetDebugUtilsObjectNameEXT)
|
GET_INSTANCE_PROC_ADDR(setDebugUtilsObjectName, SetDebugUtilsObjectNameEXT)
|
||||||
|
|
||||||
|
if(errors_count != 0)
|
||||||
|
throw std::runtime_error("failed to load extensions");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Context::initialize_device_extension_functions() {
|
||||||
|
uint32_t errors_count = 0;
|
||||||
|
|
||||||
|
#define GET_DEVICE_PROC_ADDR(ptr_name, func_name) \
|
||||||
|
ptr_name = (PFN_vk ## func_name)vkGetDeviceProcAddr( \
|
||||||
|
m_device, "vk" #func_name); \
|
||||||
|
if(ptr_name == nullptr) \
|
||||||
|
{ \
|
||||||
|
logger.error() << "failed to load extension function 'vk" #func_name "'"; \
|
||||||
|
errors_count += 1; \
|
||||||
|
}
|
||||||
|
|
||||||
|
GET_DEVICE_PROC_ADDR(cmdDrawIndexedIndirectCount, CmdDrawIndexedIndirectCountKHR)
|
||||||
|
|
||||||
if(errors_count != 0)
|
if(errors_count != 0)
|
||||||
throw std::runtime_error("failed to load extensions");
|
throw std::runtime_error("failed to load extensions");
|
||||||
|
|||||||
@@ -154,6 +154,8 @@ public:
|
|||||||
PFN_vkDestroyDebugUtilsMessengerEXT destroyDebugUtilsMessenger = nullptr;
|
PFN_vkDestroyDebugUtilsMessengerEXT destroyDebugUtilsMessenger = nullptr;
|
||||||
PFN_vkSetDebugUtilsObjectNameEXT setDebugUtilsObjectName = nullptr;
|
PFN_vkSetDebugUtilsObjectNameEXT setDebugUtilsObjectName = nullptr;
|
||||||
|
|
||||||
|
PFN_vkCmdDrawIndexedIndirectCountKHR cmdDrawIndexedIndirectCount = nullptr;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void create_instance(const ContextSettings& settings);
|
void create_instance(const ContextSettings& settings);
|
||||||
void create_surface(const ContextSettings& settings);
|
void create_surface(const ContextSettings& settings);
|
||||||
@@ -165,7 +167,8 @@ private:
|
|||||||
void create_device(const ContextSettings& settings);
|
void create_device(const ContextSettings& settings);
|
||||||
void create_internal_objects();
|
void create_internal_objects();
|
||||||
|
|
||||||
void initialize_extension_functions();
|
void initialize_instance_extension_functions();
|
||||||
|
void initialize_device_extension_functions();
|
||||||
|
|
||||||
static VKAPI_ATTR VkBool32 VKAPI_CALL log_debug_message(
|
static VKAPI_ATTR VkBool32 VKAPI_CALL log_debug_message(
|
||||||
VkDebugUtilsMessageSeverityFlagBitsEXT severity,
|
VkDebugUtilsMessageSeverityFlagBitsEXT severity,
|
||||||
|
|||||||
124
src/vk/Image.cpp
Normal file
124
src/vk/Image.cpp
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
// Copyright 2022 Simon Boyé
|
||||||
|
|
||||||
|
#include <vk/Image.h>
|
||||||
|
#include <vk/Context.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
namespace vk {
|
||||||
|
|
||||||
|
|
||||||
|
Image::Image() noexcept {
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::Image(Context& context, VkImageCreateInfo create_info)
|
||||||
|
: Wrapper(context)
|
||||||
|
{
|
||||||
|
assert(m_context);
|
||||||
|
|
||||||
|
if(vkCreateImage(
|
||||||
|
context.device(),
|
||||||
|
&create_info,
|
||||||
|
nullptr,
|
||||||
|
&m_image
|
||||||
|
) != VK_SUCCESS)
|
||||||
|
throw std::runtime_error("failed to create image");
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::Image(Context& context, VkImageCreateInfo create_info, VkMemoryPropertyFlags memory_properties)
|
||||||
|
: Image(context, create_info)
|
||||||
|
{
|
||||||
|
allocate_and_bind_memory(memory_properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::Image(Image&& other) noexcept
|
||||||
|
{
|
||||||
|
swap(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
Image::~Image() noexcept {
|
||||||
|
if(!is_null())
|
||||||
|
destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Image& Image::operator=(Image&& other) noexcept {
|
||||||
|
swap(other);
|
||||||
|
if(other)
|
||||||
|
other.destroy();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
VkMemoryRequirements Image::memory_requirements() const noexcept {
|
||||||
|
assert(!is_null());
|
||||||
|
assert(*m_context);
|
||||||
|
|
||||||
|
VkMemoryRequirements memory_requirements;
|
||||||
|
vkGetImageMemoryRequirements(
|
||||||
|
m_context->device(),
|
||||||
|
m_image,
|
||||||
|
&memory_requirements
|
||||||
|
);
|
||||||
|
|
||||||
|
return memory_requirements;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Image::bind_memory(const MemoryBlock& memory_block, VkDeviceSize offset) {
|
||||||
|
assert(!is_null());
|
||||||
|
assert(*m_context);
|
||||||
|
assert(memory_block);
|
||||||
|
|
||||||
|
// m_memory = std::move(memory_block);
|
||||||
|
if(vkBindImageMemory(
|
||||||
|
m_context->device(),
|
||||||
|
m_image,
|
||||||
|
memory_block.device_memory(),
|
||||||
|
memory_block.offset() + offset
|
||||||
|
) != VK_SUCCESS)
|
||||||
|
throw std::runtime_error("failed to bind image memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::bind_memory(MemoryBlock&& memory_block) {
|
||||||
|
bind_memory(memory_block);
|
||||||
|
m_memory = std::move(memory_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::allocate_and_bind_memory(VkMemoryPropertyFlags memory_properties) {
|
||||||
|
assert(!is_null());
|
||||||
|
assert(*m_context);
|
||||||
|
|
||||||
|
const auto memory_requirements = this->memory_requirements();
|
||||||
|
m_memory = m_context->allocator().allocate(
|
||||||
|
memory_requirements.size,
|
||||||
|
memory_requirements.memoryTypeBits,
|
||||||
|
memory_properties
|
||||||
|
);
|
||||||
|
|
||||||
|
bind_memory(m_memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Image::destroy() noexcept {
|
||||||
|
assert(!is_null());
|
||||||
|
assert(m_context);
|
||||||
|
|
||||||
|
if(m_memory) {
|
||||||
|
m_memory.free();
|
||||||
|
m_memory = MemoryBlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
vkDestroyImage(
|
||||||
|
m_context->device(),
|
||||||
|
m_image,
|
||||||
|
nullptr
|
||||||
|
);
|
||||||
|
|
||||||
|
m_context = nullptr;
|
||||||
|
m_image = VK_NULL_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
66
src/vk/Image.h
Normal file
66
src/vk/Image.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
// Copyright 2022 Simon Boyé
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vk/forward.h>
|
||||||
|
#include <vk/Wrapper.h>
|
||||||
|
#include <vk/Memory.h>
|
||||||
|
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace vk {
|
||||||
|
|
||||||
|
|
||||||
|
class Image: public Wrapper {
|
||||||
|
public:
|
||||||
|
Image() noexcept;
|
||||||
|
Image(Context& context, VkImageCreateInfo create_info);
|
||||||
|
Image(Context& context, VkImageCreateInfo create_info, VkMemoryPropertyFlags memory_properties);
|
||||||
|
Image(const Image&) = default;
|
||||||
|
Image(Image&& other) noexcept;
|
||||||
|
~Image() noexcept;
|
||||||
|
|
||||||
|
Image& operator=(const Image&) = default;
|
||||||
|
Image& operator=(Image&& other) noexcept;
|
||||||
|
|
||||||
|
explicit inline operator bool() const noexcept {
|
||||||
|
return !is_null();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool is_null() const noexcept {
|
||||||
|
return m_image == VK_NULL_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline operator VkImage() noexcept {
|
||||||
|
return m_image;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline VkImage image() noexcept {
|
||||||
|
return m_image;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryRequirements memory_requirements() const noexcept;
|
||||||
|
|
||||||
|
void bind_memory(const MemoryBlock& memory_block, VkDeviceSize offset=0);
|
||||||
|
void bind_memory(MemoryBlock&& memory_block);
|
||||||
|
void allocate_and_bind_memory(VkMemoryPropertyFlags memory_properties);
|
||||||
|
|
||||||
|
inline void swap(Image& other) noexcept {
|
||||||
|
using std::swap;
|
||||||
|
Wrapper::swap(other);
|
||||||
|
swap(m_image, other.m_image);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend inline void swap(Image& image_0, Image& image_1) noexcept {
|
||||||
|
image_0.swap(image_1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy() noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
VkImage m_image = VK_NULL_HANDLE;
|
||||||
|
MemoryBlock m_memory;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -42,10 +42,8 @@ MemoryBlock::MemoryBlock(MemoryBlock&& other) noexcept
|
|||||||
}
|
}
|
||||||
|
|
||||||
MemoryBlock::~MemoryBlock() noexcept {
|
MemoryBlock::~MemoryBlock() noexcept {
|
||||||
if(!is_null()) {
|
if(!is_null())
|
||||||
logger.warning() << "MemoryBlock deleted before being freed";
|
|
||||||
free();
|
free();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryBlock& MemoryBlock::operator=(MemoryBlock&& other) noexcept {
|
MemoryBlock& MemoryBlock::operator=(MemoryBlock&& other) noexcept {
|
||||||
@@ -82,11 +80,11 @@ void MemoryBlock::free() noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void* MemoryBlock::map(Context& context) {
|
Byte* MemoryBlock::map(Context& context) {
|
||||||
return map(context, 0, m_size);
|
return map(context, 0, m_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* MemoryBlock::map(Context& context, VkDeviceSize offset, VkDeviceSize size) {
|
Byte* MemoryBlock::map(Context& context, VkDeviceSize offset, VkDeviceSize size) {
|
||||||
assert(!is_null());
|
assert(!is_null());
|
||||||
assert(context);
|
assert(context);
|
||||||
assert(offset + size <= m_size);
|
assert(offset + size <= m_size);
|
||||||
@@ -102,7 +100,7 @@ void* MemoryBlock::map(Context& context, VkDeviceSize offset, VkDeviceSize size)
|
|||||||
) != VK_SUCCESS)
|
) != VK_SUCCESS)
|
||||||
throw std::runtime_error("failed to map memory");
|
throw std::runtime_error("failed to map memory");
|
||||||
|
|
||||||
return ptr;
|
return reinterpret_cast<Byte*>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryBlock::unmap(Context& context) noexcept {
|
void MemoryBlock::unmap(Context& context) noexcept {
|
||||||
@@ -176,6 +174,8 @@ MemoryBlock MemoryPage::allocate(VkDeviceSize size) noexcept {
|
|||||||
if(block_it == m_blocks.end())
|
if(block_it == m_blocks.end())
|
||||||
return MemoryBlock();
|
return MemoryBlock();
|
||||||
|
|
||||||
|
const auto offset = block_it->offset;
|
||||||
|
|
||||||
block_it[0].is_free = false;
|
block_it[0].is_free = false;
|
||||||
if (block_it[0].offset != block_it[1].offset) {
|
if (block_it[0].offset != block_it[1].offset) {
|
||||||
m_blocks.emplace(std::next(block_it), Block{
|
m_blocks.emplace(std::next(block_it), Block{
|
||||||
@@ -183,7 +183,7 @@ MemoryBlock MemoryPage::allocate(VkDeviceSize size) noexcept {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return MemoryBlock(m_device_memory, size, block_it[0].offset, this, m_memory_type);
|
return MemoryBlock(m_device_memory, size, offset, this, m_memory_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryPage::p_free(MemoryBlock& block) noexcept {
|
void MemoryPage::p_free(MemoryBlock& block) noexcept {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Copyright 2022 Simon Boyé
|
// Copyright 2022 Simon Boyé
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <core/types.h>
|
||||||
|
|
||||||
#include <vk/forward.h>
|
#include <vk/forward.h>
|
||||||
|
|
||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
@@ -61,8 +63,8 @@ public:
|
|||||||
|
|
||||||
void free() noexcept;
|
void free() noexcept;
|
||||||
|
|
||||||
void* map(Context& context);
|
Byte* map(Context& context);
|
||||||
void* map(Context& context, VkDeviceSize offset, VkDeviceSize size);
|
Byte* map(Context& context, VkDeviceSize offset, VkDeviceSize size);
|
||||||
void unmap(Context& context) noexcept;
|
void unmap(Context& context) noexcept;
|
||||||
|
|
||||||
void flush(Context& context);
|
void flush(Context& context);
|
||||||
|
|||||||
@@ -324,7 +324,7 @@ void Swapchain::create() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(auto& callback: m_creation_callbacks)
|
for(auto& callback: m_creation_callbacks)
|
||||||
callback(image_count);
|
callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Swapchain::destroy() {
|
void Swapchain::destroy() {
|
||||||
@@ -332,11 +332,7 @@ void Swapchain::destroy() {
|
|||||||
callback();
|
callback();
|
||||||
|
|
||||||
m_frame_resources.clear();
|
m_frame_resources.clear();
|
||||||
|
m_image_resources.clear();
|
||||||
for(auto& image_resources: m_image_resources) {
|
|
||||||
image_resources.render_done.destroy();
|
|
||||||
image_resources.view.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_context->destroy_swapchain(m_swapchain);
|
m_context->destroy_swapchain(m_swapchain);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public:
|
|||||||
static constexpr uint32_t CURRENT_IMAGE_INDEX = UINT32_MAX;
|
static constexpr uint32_t CURRENT_IMAGE_INDEX = UINT32_MAX;
|
||||||
static constexpr uint32_t MAX_FRAMES_IN_FLIGHT = 2;
|
static constexpr uint32_t MAX_FRAMES_IN_FLIGHT = 2;
|
||||||
|
|
||||||
using CreationCallback = std::function<void(uint32_t)>;
|
using CreationCallback = std::function<void()>;
|
||||||
using DestructionCallback = std::function<void()>;
|
using DestructionCallback = std::function<void()>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user