Skip to content

Commit

Permalink
ReWrite vulkan_hello_world and vk_app
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiwenTang committed Oct 23, 2021
1 parent f23932d commit 2991945
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 220 deletions.
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ endif()

#example_config.h
set(EXAMPLE_IMAGE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/images/")
set(EXAMPLE_VK_SHADER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/vk_shaders/binary/")
configure_file(example_config.h.in example_config.hpp @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

Expand Down
3 changes: 2 additions & 1 deletion example/example_config.h.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

#cmakedefine EXAMPLE_IMAGE_ROOT "@EXAMPLE_IMAGE_ROOT@"
#cmakedefine EXAMPLE_IMAGE_ROOT "@EXAMPLE_IMAGE_ROOT@"
#cmakedefine EXAMPLE_VK_SHADER_ROOT "@EXAMPLE_VK_SHADER_ROOT@"
211 changes: 0 additions & 211 deletions example/utils/vk_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ void VkApp::Run() {
// command buffer
CreateCommandPoolAndBuffer();
CreateSyncObject();
CreatePipeline();
CreateVertexBuffer();

this->OnCreate();

Expand Down Expand Up @@ -602,13 +600,6 @@ void VkApp::BeginForDraw() {

vk_command_buffer_->beginRenderPass(
&render_pass_begin_info, vk::SubpassContents::eInline, vk_dispatch_);

vk_command_buffer_->bindPipeline(vk::PipelineBindPoint::eGraphics,
vk_pipeline_.get(), vk_dispatch_);

vk_command_buffer_->bindVertexBuffers(0, vk_vertex_buffer_.get(), {0});

vk_command_buffer_->draw(3, 1, 0, 0);
}

void VkApp::EndForDraw() {
Expand Down Expand Up @@ -647,208 +638,6 @@ void VkApp::EndForDraw() {
vk_device_->waitIdle(vk_dispatch_);
}

void VkApp::CreatePipeline() {
auto vert_shader_module_ret = vk_device_->createShaderModuleUnique(
vk::ShaderModuleCreateInfo{
{},
vk_demo_buffer_shader_vert_spv_size,
(const uint32_t*)vk_demo_buffer_shader_vert_spv},
nullptr, vk_dispatch_);
assert(vert_shader_module_ret.result == vk::Result::eSuccess);

auto frag_shader_module_ret = vk_device_->createShaderModuleUnique(
vk::ShaderModuleCreateInfo{{},
vk_demo_shader_frag_spv_size,
(const uint32_t*)vk_demo_shader_frag_spv},
nullptr, vk_dispatch_);
assert(frag_shader_module_ret.result == vk::Result::eSuccess);

std::vector<vk::PipelineShaderStageCreateInfo>
pipeline_shader_stage_create_info;

pipeline_shader_stage_create_info.emplace_back(
vk::PipelineShaderStageCreateInfo{{},
vk::ShaderStageFlagBits::eVertex,
vert_shader_module_ret.value.get(),
"main"});

pipeline_shader_stage_create_info.emplace_back(
vk::PipelineShaderStageCreateInfo{{},
vk::ShaderStageFlagBits::eFragment,
frag_shader_module_ret.value.get(),
"main"});

// description for vertex buffer
vk::VertexInputBindingDescription vertex_binding_desc{
0, 6 * sizeof(float), vk::VertexInputRate::eVertex};

std::array<vk::VertexInputAttributeDescription, 2>
vertex_input_attr_description{};

vertex_input_attr_description[0].binding = 0;
vertex_input_attr_description[0].location = 0;
vertex_input_attr_description[0].format = vk::Format::eR32G32Sfloat;
vertex_input_attr_description[0].offset = 0;

vertex_input_attr_description[1].binding = 0;
vertex_input_attr_description[1].location = 1;
vertex_input_attr_description[1].format = vk::Format::eR32G32B32A32Sfloat;
vertex_input_attr_description[1].offset = 2 * sizeof(float);

vk::PipelineInputAssemblyStateCreateInfo
pipeline_input_assembly_state_create_info{
{}, vk::PrimitiveTopology::eTriangleList, VK_FALSE};

vk::PipelineVertexInputStateCreateInfo
pipeline_vertex_input_state_create_info{
{}, vertex_binding_desc, vertex_input_attr_description};

vk::Viewport view_port{
0.f, 0.f, (float)vk_frame_extent_.width, (float)vk_frame_extent_.height,
0.f, 1.f};

vk::Rect2D scissor{vk::Offset2D{0, 0}, vk_frame_extent_};

vk::PipelineViewportStateCreateInfo pipeline_viewport_state_create_info{
{}, 1, &view_port, 1, &scissor};

vk::PipelineRasterizationStateCreateInfo
pipeline_rasterization_state_create_info{{},
VK_FALSE,
VK_FALSE,
vk::PolygonMode::eFill,
vk::CullModeFlagBits::eBack,
vk::FrontFace::eClockwise,
VK_FALSE,
0.f,
0.f,
0.f,
1.f};

vk::PipelineMultisampleStateCreateInfo pipeline_multisample_state_create_info{
{}, vk::SampleCountFlagBits::e1};

vk::StencilOpState stencil_op_state{
vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep,
vk::CompareOp::eAlways};

vk::PipelineDepthStencilStateCreateInfo
pipeline_depth_stencil_state_create_info{
vk::PipelineDepthStencilStateCreateFlags{},
false,
false,
vk::CompareOp::eLess,
false,
false,
stencil_op_state,
stencil_op_state,
0.f,
1.f};

vk::ColorComponentFlags color_component_flags = {
vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA};

vk::PipelineColorBlendAttachmentState pipeline_color_blend_attachment_state{
true,
vk::BlendFactor::eSrcAlpha,
vk::BlendFactor::eOneMinusSrcAlpha,
vk::BlendOp::eAdd,
vk::BlendFactor::eSrcAlpha,
vk::BlendFactor::eOneMinusSrcAlpha,
vk::BlendOp::eAdd,
color_component_flags};

vk::PipelineColorBlendStateCreateInfo pipeline_color_blend_state_create_info{
vk::PipelineColorBlendStateCreateFlags{},
false,
vk::LogicOp::eCopy,
pipeline_color_blend_attachment_state,
{1.f, 1.f, 1.f, 1.f}};

std::array<vk::DynamicState, 2> dynamic_state = {vk::DynamicState::eViewport,
vk::DynamicState::eScissor};

vk::PipelineDynamicStateCreateInfo pipeline_dynamic_state_create_info{
vk::PipelineDynamicStateCreateFlags{}, dynamic_state};

vk::PipelineLayoutCreateInfo pipeline_layout_create_info{

};

auto pipeline_layout_ret = vk_device_->createPipelineLayoutUnique(
pipeline_layout_create_info, nullptr, vk_dispatch_);

assert(pipeline_layout_ret.result == vk::Result::eSuccess);
vk_pipeline_layout_ = std::move(pipeline_layout_ret.value);

vk::GraphicsPipelineCreateInfo pipeline_create_info{
vk::PipelineCreateFlags{},
pipeline_shader_stage_create_info,
&pipeline_vertex_input_state_create_info,
&pipeline_input_assembly_state_create_info,
nullptr,
&pipeline_viewport_state_create_info,
&pipeline_rasterization_state_create_info,
&pipeline_multisample_state_create_info,
&pipeline_depth_stencil_state_create_info,
&pipeline_color_blend_state_create_info,
&pipeline_dynamic_state_create_info,
vk_pipeline_layout_.get(),
vk_render_pass_.get()};

auto result = vk_device_->createGraphicsPipelineUnique(
nullptr, pipeline_create_info, nullptr, vk_dispatch_);

assert(result.result == vk::Result::eSuccess);

vk_pipeline_ = std::move(result.value);
}

void VkApp::CreateVertexBuffer() {
std::vector<float> vertex{0.f, -0.5f, 1.f, 0.f, 1.f, 1.0f,
0.5f, 0.5f, 0.f, 1.f, 0.f, 0.5f,
-0.5f, 0.5f, 0.f, 0.f, 1.f, 0.0f};
vk::BufferCreateInfo buffer_create_info{
{},
vertex.size() * sizeof(float),
vk::BufferUsageFlagBits::eVertexBuffer,
vk::SharingMode::eExclusive};

auto buffer_create_ret =
vk_device_->createBufferUnique(buffer_create_info, nullptr, vk_dispatch_);

assert(buffer_create_ret.result == vk::Result::eSuccess);

vk_vertex_buffer_ = std::move(buffer_create_ret.value);

auto memory_requirements = vk_device_->getBufferMemoryRequirements(
vk_vertex_buffer_.get(), vk_dispatch_);

vk::MemoryAllocateInfo memory_allocate_info{
memory_requirements.size,
FindMemoryType(memory_requirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent)};

auto allocate_ret = vk_device_->allocateMemoryUnique(memory_allocate_info,
nullptr, vk_dispatch_);

assert(allocate_ret.result == vk::Result::eSuccess);
vk_vertex_buffer_memory_ = std::move(allocate_ret.value);

void* data;
vk_device_->mapMemory(vk_vertex_buffer_memory_.get(), 0,
buffer_create_info.size, vk::MemoryMapFlagBits{}, &data,
vk_dispatch_);

std::memcpy(data, vertex.data(), (size_t)buffer_create_info.size);

vk_device_->unmapMemory(vk_vertex_buffer_memory_.get(), vk_dispatch_);

vk_device_->bindBufferMemory(vk_vertex_buffer_.get(),
vk_vertex_buffer_memory_.get(), 0, vk_dispatch_);
}

uint32_t VkApp::FindMemoryType(uint32_t typeFilter,
vk::MemoryPropertyFlags properties) {
Expand Down
25 changes: 17 additions & 8 deletions example/utils/vk_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ class VkApp {
void Update(float elapsed_time);

protected:
vk::Device GetVkDevice() { return vk_device_.get(); }
vk::SurfaceKHR GetVkSurface() { return vk_surface_.get(); }
vk::CommandBuffer GetVkCommandBuffer() { return vk_command_buffer_.get(); }
vk::CommandPool GetVkCommandPool() { return vk_command_pool_.get(); }
vk::Extent2D GetFrameExtend() { return vk_frame_extent_; }
vk::Format GetFrameFormat() { return vk_color_attachment_format_; }
vk::DispatchLoaderStatic const& GetVkDispatch() { return vk_dispatch_; }
vk::Framebuffer GetCurrentFramebuffer() {
return vk_swap_chain_frame_buffer_[vk_current_frame_index_].get();
}
vk::RenderPass GetAppRenderPass() { return vk_render_pass_.get(); }

// util function may move to other class
uint32_t FindMemoryType(uint32_t typeFilter,
vk::MemoryPropertyFlags properties);

virtual void OnCreate() {}
virtual void OnUpdate(float elapsed_time) {}
virtual void OnDestroy() {}
Expand All @@ -40,10 +56,6 @@ class VkApp {
void CreateRenderPass();
void CreateFramebuffer();
void CreateSyncObject();
void CreatePipeline();
void CreateVertexBuffer();
uint32_t FindMemoryType(uint32_t typeFilter,
vk::MemoryPropertyFlags properties);

void BeginForDraw();
void EndForDraw();
Expand Down Expand Up @@ -75,11 +87,8 @@ class VkApp {
uint32_t vk_current_frame_index_ = 0;
vk::UniqueSemaphore vk_image_acquired_semaphore_;
vk::UniqueFence vk_draw_fence_;
vk::UniquePipelineLayout vk_pipeline_layout_;
vk::UniquePipeline vk_pipeline_;
vk::UniqueBuffer vk_vertex_buffer_;
vk::UniqueDeviceMemory vk_vertex_buffer_memory_;
};

} // namespace example

#endif // EXAMPLE_UTILS_VK_APP_HPP
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2991945

Please sign in to comment.