Skip to content

Commit 2ac127f

Browse files
authored
Resources (#94)
* Code_Refactor * Resources
1 parent fc42962 commit 2ac127f

File tree

12 files changed

+171
-159
lines changed

12 files changed

+171
-159
lines changed

HelloVulkan/Header/Apps/AppBase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class AppBase
4343
void InitCamera();
4444

4545
// Resources
46-
void InitIBLResources(const std::string& hdrFile);
4746
void InitSharedResources();
4847

4948
// Functions related to the main loop

HelloVulkan/Header/Resources/ResourcesIBL.h

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,13 @@
77
struct ResourcesIBL
88
{
99
public:
10-
ResourcesIBL() = default;
11-
~ResourcesIBL()
12-
{
13-
Destroy();
14-
}
10+
ResourcesIBL(VulkanContext& ctx, const std::string& hdrFile);
11+
~ResourcesIBL();
12+
void Destroy();
1513

16-
void Destroy()
17-
{
18-
environmentCubemap_.Destroy();
19-
diffuseCubemap_.Destroy();
20-
specularCubemap_.Destroy();
21-
brdfLut_.Destroy();
22-
}
23-
24-
void SetDebugNames(VulkanContext& ctx)
25-
{
26-
environmentCubemap_.SetDebugName(ctx, "Environment_Cubemap");
27-
diffuseCubemap_.SetDebugName(ctx, "Diffuse_Cubemap");
28-
specularCubemap_.SetDebugName(ctx, "Specular_Cubemap");
29-
brdfLut_.SetDebugName(ctx, "BRDF_LUT");
30-
}
14+
private:
15+
void Create(VulkanContext& ctx, const std::string& hdrFile);
16+
void SetDebugNames(VulkanContext& ctx);
3117

3218
public:
3319
VulkanImage environmentCubemap_;

HelloVulkan/Header/Resources/ResourcesShadow.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,11 @@
77
struct ResourcesShadow
88
{
99
public:
10-
ResourcesShadow() = default;
11-
~ResourcesShadow()
12-
{
13-
Destroy();
14-
}
10+
ResourcesShadow();
11+
~ResourcesShadow();
1512

16-
void CreateSingleShadowMap(VulkanContext& ctx)
17-
{
18-
// Init shadow map
19-
shadowMap_.CreateDepthResources(
20-
ctx,
21-
ShadowConfig::DepthSize,
22-
ShadowConfig::DepthSize,
23-
1u,// layerCount
24-
VK_SAMPLE_COUNT_1_BIT,
25-
VK_IMAGE_USAGE_SAMPLED_BIT);
26-
shadowMap_.CreateDefaultSampler(
27-
ctx,
28-
0.f,
29-
1.f,
30-
VK_FILTER_LINEAR,
31-
VK_FILTER_LINEAR,
32-
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE);
33-
shadowMap_.SetDebugName(ctx, "Single_Layer_Shadow_Map");
34-
35-
device_ = ctx.GetDevice();
36-
}
37-
38-
void Destroy()
39-
{
40-
shadowMap_.Destroy();
41-
}
13+
void CreateSingleShadowMap(VulkanContext& ctx);
14+
void Destroy();
4215

4316
public:
4417
VulkanImage shadowMap_;

HelloVulkan/Header/Resources/ResourcesShared.h

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,11 @@
77
struct ResourcesShared
88
{
99
public:
10-
ResourcesShared() = default;
11-
~ResourcesShared()
12-
{
13-
Destroy();
14-
}
10+
ResourcesShared();
11+
~ResourcesShared();
1512

16-
void Create(VulkanContext& ctx)
17-
{
18-
depthImage_.Destroy();
19-
multiSampledColorImage_.Destroy();
20-
singleSampledColorImage_.Destroy();
21-
22-
const VkSampleCountFlagBits msaaSamples = ctx.GetMSAASampleCount();
23-
const uint32_t width = ctx.GetSwapchainWidth();
24-
const uint32_t height = ctx.GetSwapchainHeight();
25-
26-
// Depth attachment (OnScreen and offscreen)
27-
depthImage_.CreateDepthResources(
28-
ctx,
29-
width,
30-
height,
31-
1u, // layerCount
32-
msaaSamples);
33-
depthImage_.SetDebugName(ctx, "Depth_Image");
34-
35-
// Color attachments
36-
// Multi-sampled (MSAA)
37-
multiSampledColorImage_.CreateColorResources(
38-
ctx,
39-
width,
40-
height,
41-
msaaSamples);
42-
multiSampledColorImage_.SetDebugName(ctx, "Multisampled_Color_Image");
43-
44-
// Single-sampled
45-
singleSampledColorImage_.CreateColorResources(
46-
ctx,
47-
width,
48-
height);
49-
singleSampledColorImage_.SetDebugName(ctx, "Singlesampled_Color_Image");
50-
}
51-
52-
void Destroy()
53-
{
54-
depthImage_.Destroy();
55-
multiSampledColorImage_.Destroy();
56-
singleSampledColorImage_.Destroy();
57-
}
13+
void Create(VulkanContext& ctx);
14+
void Destroy();
5815

5916
public:
6017
VulkanImage multiSampledColorImage_;

HelloVulkan/Source/Apps/AppBase.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
#include "Configs.h"
33
#include "VulkanUtility.h"
44

5-
// IBL
6-
#include "PipelineEquirect2Cube.h"
7-
#include "PipelineCubeFilter.h"
8-
#include "PipelineBRDFLUT.h"
9-
105
#include "volk.h"
116

127
// Init GLSLang
@@ -375,39 +370,4 @@ void AppBase::InitSharedResources()
375370
{
376371
resShared_ = std::make_unique<ResourcesShared>();
377372
resShared_->Create(vulkanContext_);
378-
}
379-
380-
void AppBase::InitIBLResources(const std::string& hdrFile)
381-
{
382-
resIBL_ = std::make_unique<ResourcesIBL>();
383-
384-
// Create a cubemap from the input HDR
385-
{
386-
PipelineEquirect2Cube e2c(
387-
vulkanContext_,
388-
hdrFile);
389-
e2c.OffscreenRender(vulkanContext_,
390-
&(resIBL_->environmentCubemap_)); // Output
391-
}
392-
393-
// Cube filtering
394-
{
395-
PipelineCubeFilter cubeFilter(vulkanContext_, &(resIBL_->environmentCubemap_));
396-
// Diffuse
397-
cubeFilter.OffscreenRender(vulkanContext_,
398-
&(resIBL_->diffuseCubemap_),
399-
CubeFilterType::Diffuse);
400-
// Specular
401-
cubeFilter.OffscreenRender(vulkanContext_,
402-
&(resIBL_->specularCubemap_),
403-
CubeFilterType::Specular);
404-
}
405-
406-
// BRDF look up table
407-
{
408-
PipelineBRDFLUT brdfLUTCompute(vulkanContext_);
409-
brdfLUTCompute.CreateLUT(vulkanContext_, &(resIBL_->brdfLut_));
410-
}
411-
412-
resIBL_->SetDebugNames(vulkanContext_);
413373
}

HelloVulkan/Source/Apps/AppPBRBindless.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void AppPBRBindless::Init()
2424
InitSharedResources();
2525

2626
// Image-Based Lighting
27-
InitIBLResources(AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
27+
resIBL_ = std::make_unique<ResourcesIBL>(vulkanContext_, AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
2828
cubemapMipmapCount_ = static_cast<float>(Utility::MipMapCount(IBLConfig::InputCubeSideLength));
2929

3030
// Scene
@@ -48,8 +48,7 @@ void AppPBRBindless::Init()
4848
vulkanContext_,
4949
&(resIBL_->environmentCubemap_),
5050
resShared_.get(),
51-
// This is the first offscreen render pass so
52-
// we need to clear the color attachment and depth attachment
51+
// This is the first offscreen render pass so we need to clear the color attachment and depth attachment
5352
RenderPassBit::ColorClear | RenderPassBit::DepthClear);
5453
pbrPtr_ = std::make_unique<PipelinePBRBindless>(
5554
vulkanContext_,
@@ -63,8 +62,7 @@ void AppPBRBindless::Init()
6362
resShared_.get());
6463
// Resolve multiSampledColorImage_ to singleSampledColorImage_
6564
resolveMSPtr_ = std::make_unique<PipelineResolveMS>(vulkanContext_, resShared_.get());
66-
// This is on-screen render pass that transfers
67-
// singleSampledColorImage_ to swapchain image
65+
// This is on-screen render pass that transfers singleSampledColorImage_ to swapchain image
6866
tonemapPtr_ = std::make_unique<PipelineTonemap>(vulkanContext_, &(resShared_->singleSampledColorImage_));
6967
// ImGui here
7068
imguiPtr_ = std::make_unique<PipelineImGui>(vulkanContext_, vulkanInstance_.GetInstance(), glfwWindow_);

HelloVulkan/Source/Apps/AppPBRClusterForward.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void AppPBRClusterForward::Init()
2424
InitLights();
2525

2626
// Image-Based Lighting
27-
InitIBLResources(AppConfig::TextureFolder + "dikhololo_night_4k.hdr");
27+
resIBL_ = std::make_unique<ResourcesIBL>(vulkanContext_, AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
2828
cubemapMipmapCount_ = static_cast<float>(Utility::MipMapCount(IBLConfig::InputCubeSideLength));
2929

3030
resCF_ = std::make_unique<ResourcesClusterForward>();
@@ -44,8 +44,7 @@ void AppPBRClusterForward::Init()
4444
vulkanContext_,
4545
&(resIBL_->environmentCubemap_),
4646
resShared_.get(),
47-
// This is the first offscreen render pass so
48-
// we need to clear the color attachment and depth attachment
47+
// This is the first offscreen render pass so we need to clear the color attachment and depth attachment
4948
RenderPassBit::ColorClear | RenderPassBit::DepthClear);
5049
aabbPtr_ = std::make_unique<PipelineAABBGenerator>(vulkanContext_, resCF_.get());
5150
lightCullPtr_ = std::make_unique<PipelineLightCulling>(vulkanContext_, resLight_.get(), resCF_.get());
@@ -59,8 +58,7 @@ void AppPBRClusterForward::Init()
5958
lightPtr_ = std::make_unique<PipelineLightRender>(vulkanContext_, resLight_.get(), resShared_.get());
6059
// Resolve multiSampledColorImage_ to singleSampledColorImage_
6160
resolveMSPtr_ = std::make_unique<PipelineResolveMS>(vulkanContext_, resShared_.get());
62-
// This is on-screen render pass that transfers
63-
// singleSampledColorImage_ to swapchain image
61+
// This is on-screen render pass that transfers singleSampledColorImage_ to swapchain image
6462
tonemapPtr_ = std::make_unique<PipelineTonemap>(vulkanContext_, &(resShared_->singleSampledColorImage_));
6563
// ImGui here
6664
imguiPtr_ = std::make_unique<PipelineImGui>(vulkanContext_, vulkanInstance_.GetInstance(), glfwWindow_);

HelloVulkan/Source/Apps/AppPBRShadow.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void AppPBRShadow::Init()
2727
InitSharedResources();
2828

2929
// Image-Based Lighting
30-
InitIBLResources(AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
30+
resIBL_ = std::make_unique<ResourcesIBL>(vulkanContext_, AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
3131
cubemapMipmapCount_ = static_cast<float>(Utility::MipMapCount(IBLConfig::InputCubeSideLength));
3232

3333
std::vector<std::string> modelFiles = {
@@ -58,8 +58,7 @@ void AppPBRShadow::Init()
5858
vulkanContext_,
5959
&(resIBL_->environmentCubemap_),
6060
resShared_.get(),
61-
// This is the first offscreen render pass so
62-
// we need to clear the color attachment and depth attachment
61+
// This is the first offscreen render pass so we need to clear the color attachment and depth attachment
6362
RenderPassBit::ColorClear | RenderPassBit::DepthClear);
6463
pbrPtr_ = std::make_unique<PipelinePBRShadow>(
6564
vulkanContext_,
@@ -72,8 +71,7 @@ void AppPBRShadow::Init()
7271
lightPtr_ = std::make_unique<PipelineLightRender>(vulkanContext_, resLights_.get(), resShared_.get());
7372
// Resolve multiSampledColorImage_ to singleSampledColorImage_
7473
resolveMSPtr_ = std::make_unique<PipelineResolveMS>(vulkanContext_, resShared_.get());
75-
// This is on-screen render pass that transfers
76-
// singleSampledColorImage_ to swapchain image
74+
// This is on-screen render pass that transfers singleSampledColorImage_ to swapchain image
7775
tonemapPtr_ = std::make_unique<PipelineTonemap>(vulkanContext_, &(resShared_->singleSampledColorImage_));
7876
// ImGui here
7977
imguiPtr_ = std::make_unique<PipelineImGui>(vulkanContext_, vulkanInstance_.GetInstance(), glfwWindow_);

HelloVulkan/Source/Apps/AppPBRSlotBased.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void AppPBRSlotBased::Init()
2424
InitSharedResources();
2525

2626
// Image-Based Lighting
27-
InitIBLResources(AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
27+
resIBL_ = std::make_unique<ResourcesIBL>(vulkanContext_, AppConfig::TextureFolder + "piazza_bologni_1k.hdr");
2828
cubemapMipmapCount_ = static_cast<float>(Utility::MipMapCount(IBLConfig::InputCubeSideLength));
2929

3030
model_ = std::make_unique<Model>();
@@ -40,8 +40,7 @@ void AppPBRSlotBased::Init()
4040
vulkanContext_,
4141
&(resIBL_->environmentCubemap_),
4242
resShared_.get(),
43-
// This is the first offscreen render pass so
44-
// we need to clear the color attachment and depth attachment
43+
// This is the first offscreen render pass so we need to clear the color attachment and depth attachment
4544
RenderPassBit::ColorClear | RenderPassBit::DepthClear);
4645
pbrPtr_ = std::make_unique<PipelinePBRSlotBased>(
4746
vulkanContext_,
@@ -52,8 +51,7 @@ void AppPBRSlotBased::Init()
5251
lightPtr_ = std::make_unique<PipelineLightRender>(vulkanContext_, resLights_.get(), resShared_.get());
5352
// Resolve multiSampledColorImage_ to singleSampledColorImage_
5453
resolveMSPtr_ = std::make_unique<PipelineResolveMS>(vulkanContext_, resShared_.get());
55-
// This is on-screen render pass that transfers
56-
// singleSampledColorImage_ to swapchain image
54+
// This is on-screen render pass that transfers singleSampledColorImage_ to swapchain image
5755
tonemapPtr_ = std::make_unique<PipelineTonemap>(vulkanContext_, &(resShared_->singleSampledColorImage_));
5856
// ImGui here
5957
imguiPtr_ = std::make_unique<PipelineImGui>(vulkanContext_, vulkanInstance_.GetInstance(), glfwWindow_);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "ResourcesIBL.h"
2+
3+
// IBL
4+
#include "PipelineEquirect2Cube.h"
5+
#include "PipelineCubeFilter.h"
6+
#include "PipelineBRDFLUT.h"
7+
8+
ResourcesIBL::ResourcesIBL(VulkanContext& ctx, const std::string& hdrFile)
9+
{
10+
Create(ctx, hdrFile);
11+
SetDebugNames(ctx);
12+
}
13+
14+
ResourcesIBL::~ResourcesIBL()
15+
{
16+
Destroy();
17+
}
18+
19+
void ResourcesIBL::Create(VulkanContext& ctx, const std::string& hdrFile)
20+
{
21+
// Create a cubemap from the input HDR
22+
{
23+
PipelineEquirect2Cube e2c(ctx, hdrFile);
24+
e2c.OffscreenRender(ctx, &environmentCubemap_);
25+
}
26+
27+
// Cube filtering
28+
{
29+
PipelineCubeFilter cubeFilter(ctx, &(environmentCubemap_));
30+
cubeFilter.OffscreenRender(ctx, &diffuseCubemap_, CubeFilterType::Diffuse);
31+
cubeFilter.OffscreenRender(ctx, &specularCubemap_, CubeFilterType::Specular);
32+
}
33+
34+
// BRDF look up table
35+
{
36+
PipelineBRDFLUT brdfLUTCompute(ctx);
37+
brdfLUTCompute.CreateLUT(ctx, &brdfLut_);
38+
}
39+
}
40+
41+
void ResourcesIBL::Destroy()
42+
{
43+
environmentCubemap_.Destroy();
44+
diffuseCubemap_.Destroy();
45+
specularCubemap_.Destroy();
46+
brdfLut_.Destroy();
47+
}
48+
49+
void ResourcesIBL::SetDebugNames(VulkanContext& ctx)
50+
{
51+
environmentCubemap_.SetDebugName(ctx, "Environment_Cubemap");
52+
diffuseCubemap_.SetDebugName(ctx, "Diffuse_Cubemap");
53+
specularCubemap_.SetDebugName(ctx, "Specular_Cubemap");
54+
brdfLut_.SetDebugName(ctx, "BRDF_LUT");
55+
}

0 commit comments

Comments
 (0)