Skip to content

Commit ff7e1e0

Browse files
committed
changes from c3 updates
1 parent 1e81c3e commit ff7e1e0

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

chapter-2/vk_engine.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
constexpr bool bUseValidationLayers = true;
2626

2727
//chapter stage for refactors/changes
28-
#define CHAPTER_STAGE 2
28+
#define CHAPTER_STAGE 3
2929

3030
//we want to immediately abort when there is an error. In normal engines this would give an error message to the user, or perform a dump of state.
3131
using namespace std;
@@ -97,7 +97,7 @@ void VulkanEngine::cleanup()
9797
SDL_DestroyWindow(_window);
9898
}
9999
}
100-
void VulkanEngine::draw_main(VkCommandBuffer cmd)
100+
void VulkanEngine::draw_background(VkCommandBuffer cmd)
101101
{
102102
#if CHAPTER_STAGE == 0
103103
//> draw_clear
@@ -120,7 +120,7 @@ void VulkanEngine::draw_main(VkCommandBuffer cmd)
120120
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, _gradientPipelineLayout, 0, 1, &_drawImageDescriptors, 0, nullptr);
121121

122122
// execute the compute pipeline dispatch. We are using 16x16 workgroup size so we need to divide by it
123-
vkCmdDispatch(cmd, std::ceil(_swapchainExtent.width / 16.0), std::ceil(_swapchainExtent.height / 16.0), 1);
123+
vkCmdDispatch(cmd, std::ceil(_drawExtent.width / 16.0), std::ceil(_drawExtent.height / 16.0), 1);
124124
//< draw_comp
125125
#elif CHAPTER_STAGE == 2
126126

@@ -208,7 +208,7 @@ void VulkanEngine::draw()
208208
// we will overwrite it all so we dont care about what was the older layout
209209
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
210210

211-
draw_main(cmd);
211+
draw_background(cmd);
212212

213213
//transition the draw image and the swapchain image into their correct transfer layouts
214214
vkutil::transition_image(cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
@@ -271,28 +271,35 @@ void VulkanEngine::run()
271271
{
272272
SDL_Event e;
273273
bool bQuit = false;
274-
static bool skipDrawing = false;
275274
//main loop
276275
while (!bQuit)
277276
{
278277
//Handle events on queue
279-
while (SDL_PollEvent(&e) != 0)
280-
{
278+
while (SDL_PollEvent(&e) != 0) {
281279
//close the window when user alt-f4s or clicks the X button
282280
if (e.type == SDL_QUIT) bQuit = true;
283-
if (e.type == SDL_WINDOWEVENT_MINIMIZED) {
284-
skipDrawing = true;
285-
}
286-
if (e.type == SDL_WINDOWEVENT_RESTORED) {
287-
skipDrawing = false;
288-
}
289-
if (e.type == SDL_WINDOWEVENT_RESIZED) {
290-
291-
rebuild_swapchain();
281+
282+
if (e.type == SDL_WINDOWEVENT) {
283+
284+
if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
285+
stop_rendering = true;
286+
}
287+
if (e.window.event == SDL_WINDOWEVENT_RESTORED) {
288+
stop_rendering = false;
289+
}
292290
}
291+
292+
//send SDL event to imgui for handling
293293
ImGui_ImplSDL2_ProcessEvent(&e);
294294
}
295295

296+
//do not draw if we are minimized
297+
if (stop_rendering) {
298+
//throttle the speed to avoid the endless spinning
299+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
300+
continue;
301+
}
302+
296303
// imgui new frame
297304
ImGui_ImplVulkan_NewFrame();
298305
ImGui_ImplSDL2_NewFrame(_window);
@@ -318,12 +325,8 @@ void VulkanEngine::run()
318325
ImGui::Render();
319326

320327
//< imgui_bk
321-
if (!skipDrawing) {
322-
draw();
323-
}
324-
else {
325-
std::this_thread::sleep_for(std::chrono::milliseconds{100});
326-
}
328+
329+
draw();
327330
}
328331
}
329332

@@ -503,20 +506,22 @@ void VulkanEngine::init_swapchain()
503506
create_swapchain(_windowExtent.width, _windowExtent.height);
504507

505508
//> init_swap
506-
//depth image size will match the window
509+
//draw image size will match the window
507510
VkExtent3D drawImageExtent = {
508511
_windowExtent.width,
509512
_windowExtent.height,
510513
1
511514
};
512515

513-
//hardcoding the depth format to 32 bit float
516+
//hardcoding the draw format to 32 bit float
514517
_drawImage.imageFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
518+
_drawImage.imageExtent = drawImageExtent;
515519

516520
VkImageUsageFlags drawImageUsages{};
517521
drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
518522
drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
519523
drawImageUsages |= VK_IMAGE_USAGE_STORAGE_BIT;
524+
drawImageUsages |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
520525

521526
VkImageCreateInfo rimg_info = vkinit::image_create_info(_drawImage.imageFormat, drawImageUsages, drawImageExtent);
522527

@@ -560,7 +565,7 @@ void VulkanEngine::init_commands()
560565
//> imm_cmd
561566
VK_CHECK(vkCreateCommandPool(_device, &commandPoolInfo, nullptr, &_immCommandPool));
562567

563-
// allocate the default command buffer that we will use for rendering
568+
// allocate the command buffer for immediate submits
564569
VkCommandBufferAllocateInfo cmdAllocInfo = vkinit::command_buffer_allocate_info(_immCommandPool, 1);
565570

566571
VK_CHECK(vkAllocateCommandBuffers(_device, &cmdAllocInfo, &_immCommandBuffer));
@@ -726,7 +731,7 @@ VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipe
726731
VkShaderModule computeDrawShader;
727732
if (!vkutil::load_shader_module("../../shaders/gradient.comp.spv", _device, &computeDrawShader))
728733
{
729-
std::cout << "Error when building the compute shader" << std::endl;
734+
fmt::print("Error when building the compute shader \n");
730735
}
731736

732737
VkPipelineShaderStageCreateInfo stageinfo{};
@@ -757,7 +762,7 @@ VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipe
757762
#elif CHAPTER_STAGE == 2
758763
VkShaderModule computeDrawShader;
759764
if (!vkutil::load_shader_module("../../shaders/gradient_color.comp.spv", _device, &computeDrawShader)) {
760-
std::cout << "Error when building the compute shader" << std::endl;
765+
fmt::print("Error when building the compute shader \n");
761766
}
762767

763768
VkPipelineShaderStageCreateInfo stageinfo{};
@@ -785,12 +790,12 @@ _mainDeletionQueue.push_function([&]() {
785790
//> comp_pipeline_multi
786791
VkShaderModule gradientShader;
787792
if (!vkutil::load_shader_module("../../shaders/gradient_color.comp.spv", _device, &gradientShader)) {
788-
std::cout << "Error when building the compute shader" << std::endl;
793+
fmt::print("Error when building the compute shader \n");
789794
}
790795

791796
VkShaderModule skyShader;
792797
if (!vkutil::load_shader_module("../../shaders/sky.comp.spv", _device, &skyShader)) {
793-
std::cout << "Error when building the compute shader" << std::endl;
798+
fmt::print("Error when building the compute shader \n");
794799
}
795800

796801
VkPipelineShaderStageCreateInfo stageinfo{};

chapter-2/vk_engine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class VulkanEngine {
6161

6262
bool _isInitialized{ false };
6363
int _frameNumber {0};
64-
64+
bool stop_rendering{false};
6565
VkExtent2D _windowExtent{ 1700 , 900 };
6666

6767
struct SDL_Window* _window{ nullptr };
@@ -120,7 +120,7 @@ class VulkanEngine {
120120
//draw loop
121121
void draw();
122122

123-
void draw_main(VkCommandBuffer cmd);
123+
void draw_background(VkCommandBuffer cmd);
124124
void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView);
125125

126126
//run main loop

chapter-3/vk_engine.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void VulkanEngine::init()
6868

6969

7070
void VulkanEngine::init_default_data() {
71-
//> init_data
71+
//> init_data
7272
std::array<Vertex,4> rect_vertices;
7373

7474
rect_vertices[0].position = {0.5,-0.5, 0};
@@ -874,20 +874,19 @@ void VulkanEngine::init_triangle_pipeline()
874874
//> triangle_shaders
875875
VkShaderModule triangleFragShader;
876876
if (!vkutil::load_shader_module("../../shaders/colored_triangle.frag.spv", _device, &triangleFragShader)) {
877-
std::cout << "Error when building the triangle fragment shader module" << std::endl;
877+
fmt::print("Error when building the triangle fragment shader module");
878878
}
879879
else {
880-
std::cout << "Triangle fragment shader succesfully loaded" << std::endl;
880+
fmt::print("Triangle fragment shader succesfully loaded");
881881
}
882882

883883
VkShaderModule triangleVertexShader;
884884
if (!vkutil::load_shader_module("../../shaders/colored_triangle.vert.spv", _device, &triangleVertexShader)) {
885-
std::cout << "Error when building the triangle vertex shader module" << std::endl;
885+
fmt::print("Error when building the triangle vertex shader module");
886886
}
887887
else {
888-
std::cout << "Triangle vertex shader succesfully loaded" << std::endl;
888+
fmt::print("Triangle vertex shader succesfully loaded");
889889
}
890-
891890

892891
//build the pipeline layout that controls the inputs/outputs of the shader
893892
//we are not using descriptor sets or other systems yet, so no need to use anything other than empty default
@@ -979,18 +978,18 @@ void VulkanEngine::init_mesh_pipeline()
979978
//> rectangle_shaders
980979
VkShaderModule triangleFragShader;
981980
if (!vkutil::load_shader_module("../../shaders/colored_triangle.frag.spv", _device, &triangleFragShader)) {
982-
std::cout << "Error when building the triangle fragment shader module" << std::endl;
981+
fmt::print("Error when building the triangle fragment shader module");
983982
}
984983
else {
985-
std::cout << "Triangle fragment shader succesfully loaded" << std::endl;
984+
fmt::print("Triangle fragment shader succesfully loaded");
986985
}
987986

988987
VkShaderModule triangleVertexShader;
989988
if (!vkutil::load_shader_module("../../shaders/colored_triangle_mesh.vert.spv", _device, &triangleVertexShader)) {
990-
std::cout << "Error when building the mesh vertex shader module" << std::endl;
989+
fmt::print("Error when building the triangle vertex shader module");
991990
}
992991
else {
993-
std::cout << "Triangle vertex shader succesfully loaded" << std::endl;
992+
fmt::print("Triangle vertex shader succesfully loaded");
994993
}
995994

996995
VkPushConstantRange bufferRange{};
@@ -1025,8 +1024,7 @@ void VulkanEngine::init_mesh_pipeline()
10251024
//no blending
10261025
pipelineBuilder.disable_blending();
10271026

1028-
//pipelineBuilder.disable_depthtest();
1029-
pipelineBuilder.enable_depthtest(true, VK_COMPARE_OP_GREATER_OR_EQUAL);
1027+
pipelineBuilder.disable_depthtest();
10301028

10311029
//connect the image format we will draw into, from draw image
10321030
pipelineBuilder.set_color_attachment_format(_drawImage.imageFormat);

shaders/colored_triangle.frag

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//glsl version 4.5
1+
//> all
22
#version 450
33

44
//shader input
@@ -7,9 +7,9 @@ layout (location = 0) in vec3 inColor;
77
//output write
88
layout (location = 0) out vec4 outFragColor;
99

10-
1110
void main()
1211
{
1312
//return red
1413
outFragColor = vec4(inColor,1.0f);
15-
}
14+
}
15+
//< all

shaders/colored_triangle.vert

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//we will be using glsl version 4.5 syntax
1+
//> all
22
#version 450
33

44
layout (location = 0) out vec3 outColor;
@@ -22,4 +22,5 @@ void main()
2222
//output the position of each vertex
2323
gl_Position = vec4(positions[gl_VertexIndex], 1.0f);
2424
outColor = colors[gl_VertexIndex];
25-
}
25+
}
26+
//< all

shared/vk_pipelines.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <vk_pipelines.h>
22

33
#include <fstream>
4-
#include <iostream>
54
#include "vk_initializers.h"
65

76
//> pipe_clear

0 commit comments

Comments
 (0)