Skip to content

Commit 70600fd

Browse files
committed
Improve
1 parent 2e8fe19 commit 70600fd

File tree

82 files changed

+419
-255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+419
-255
lines changed

.gitattributes

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
.gitignore export-ignore
2-
.gitattributes export-ignore
1+
/.gitignore export-ignore
2+
/.gitattributes export-ignore
33

4-
.github export-ignore
4+
/.github export-ignore
55

6-
models export-ignore
7-
shaders export-ignore
8-
textures export-ignore
9-
src export-ignore
10-
CMakeLists.txt export-ignore
11-
CMakePresets.json export-ignore
6+
/models export-ignore
7+
/shaders export-ignore
8+
/textures export-ignore
9+
/src export-ignore
10+
/CMakeLists.txt export-ignore
11+
/CMakePresets.json export-ignore

docs/codes/03/40_pushconstant/main.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -690,14 +690,17 @@ class HelloTriangleApplication {
690690
depthStencil.depthBoundsTestEnable = false; // Optional
691691
depthStencil.stencilTestEnable = false; // Optional
692692

693-
vk::PushConstantRange pushConstantRange;
694-
pushConstantRange.stageFlags = vk::ShaderStageFlagBits::eVertex;
695-
pushConstantRange.offset = 0;
696-
pushConstantRange.size = sizeof(PushConstantData);
693+
std::array<vk::PushConstantRange, 2> pushConstantRanges;
694+
pushConstantRanges[0].stageFlags = vk::ShaderStageFlagBits::eVertex;
695+
pushConstantRanges[0].offset = 0;
696+
pushConstantRanges[0].size = sizeof(glm::mat4);
697+
pushConstantRanges[1].stageFlags = vk::ShaderStageFlagBits::eFragment;
698+
pushConstantRanges[1].offset = sizeof(glm::mat4);
699+
pushConstantRanges[1].size = sizeof(uint32_t);
697700

698701
vk::PipelineLayoutCreateInfo pipelineLayoutInfo;
699702
pipelineLayoutInfo.setSetLayouts(*m_descriptorSetLayout);
700-
pipelineLayoutInfo.setPushConstantRanges( pushConstantRange );
703+
pipelineLayoutInfo.setPushConstantRanges( pushConstantRanges );
701704

702705
m_pipelineLayout = m_device.createPipelineLayout( pipelineLayoutInfo );
703706

@@ -827,14 +830,22 @@ class HelloTriangleApplication {
827830
glm::radians(-90.0f),
828831
glm::vec3(0.0f, 0.0f, 1.0f)
829832
);
833+
pcData.enableTexture = 1;
830834
} else {
831835
pcData.model = glm::translate(glm::mat4(1.0f), glm::vec3(0.5f, 0.12f, 0.0f));
836+
pcData.enableTexture = 0;
832837
}
833-
commandBuffer.pushConstants<PushConstantData>(
838+
commandBuffer.pushConstants<glm::mat4>(
834839
m_pipelineLayout,
835840
vk::ShaderStageFlagBits::eVertex,
836841
0, // offset
837-
pcData
842+
pcData.model
843+
);
844+
commandBuffer.pushConstants<uint32_t>(
845+
m_pipelineLayout,
846+
vk::ShaderStageFlagBits::eFragment,
847+
sizeof(glm::mat4), // offset
848+
pcData.enableTexture
838849
);
839850
commandBuffer.drawIndexed(
840851
counter == m_indicesOffsets.size() ? m_indices.size() - firstIndex : m_indicesOffsets[counter] - firstIndex,
@@ -1614,7 +1625,7 @@ class HelloTriangleApplication {
16141625

16151626
m_indicesOffsets.push_back(m_indices.size());
16161627

1617-
std::unordered_map<
1628+
static std::unordered_map<
16181629
Vertex,
16191630
uint32_t,
16201631
decltype( [](const Vertex& vertex) -> size_t {
@@ -1641,8 +1652,6 @@ class HelloTriangleApplication {
16411652
attrib.texcoords[2 * index.texcoord_index],
16421653
1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
16431654
};
1644-
} else {
1645-
vertex.texCoord = {0.61f, 0.17f};
16461655
}
16471656

16481657
vertex.color = {1.0f, 1.0f, 1.0f};
@@ -1861,6 +1870,7 @@ class HelloTriangleApplication {
18611870
/// push constant
18621871
struct PushConstantData {
18631872
glm::mat4 model;
1873+
uint32_t enableTexture;
18641874
};
18651875
/////////////////////////////////////////////////////////////////
18661876
};

docs/codes/03/40_pushconstant/main.diff

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index b0d2dfe..6258fc4 100644
2+
index b0d2dfe..a73f3c8 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
55
@@ -47,6 +47,7 @@ private:
@@ -28,23 +28,26 @@ index b0d2dfe..6258fc4 100644
2828
createVertexBuffer();
2929
createIndexBuffer();
3030
createUniformBuffers();
31-
@@ -687,8 +690,15 @@ private:
31+
@@ -687,8 +690,18 @@ private:
3232
depthStencil.depthBoundsTestEnable = false; // Optional
3333
depthStencil.stencilTestEnable = false; // Optional
3434

35-
+ vk::PushConstantRange pushConstantRange;
36-
+ pushConstantRange.stageFlags = vk::ShaderStageFlagBits::eVertex;
37-
+ pushConstantRange.offset = 0;
38-
+ pushConstantRange.size = sizeof(PushConstantData);
35+
+ std::array<vk::PushConstantRange, 2> pushConstantRanges;
36+
+ pushConstantRanges[0].stageFlags = vk::ShaderStageFlagBits::eVertex;
37+
+ pushConstantRanges[0].offset = 0;
38+
+ pushConstantRanges[0].size = sizeof(glm::mat4);
39+
+ pushConstantRanges[1].stageFlags = vk::ShaderStageFlagBits::eFragment;
40+
+ pushConstantRanges[1].offset = sizeof(glm::mat4);
41+
+ pushConstantRanges[1].size = sizeof(uint32_t);
3942
+
4043
vk::PipelineLayoutCreateInfo pipelineLayoutInfo;
4144
pipelineLayoutInfo.setSetLayouts(*m_descriptorSetLayout);
42-
+ pipelineLayoutInfo.setPushConstantRanges( pushConstantRange );
45+
+ pipelineLayoutInfo.setPushConstantRanges( pushConstantRanges );
4346
+
4447
m_pipelineLayout = m_device.createPipelineLayout( pipelineLayoutInfo );
4548

4649
vk::GraphicsPipelineCreateInfo pipelineInfo;
47-
@@ -805,7 +815,36 @@ private:
50+
@@ -805,7 +818,44 @@ private:
4851
nullptr
4952
);
5053

@@ -61,14 +64,22 @@ index b0d2dfe..6258fc4 100644
6164
+ glm::radians(-90.0f),
6265
+ glm::vec3(0.0f, 0.0f, 1.0f)
6366
+ );
67+
+ pcData.enableTexture = 1;
6468
+ } else {
6569
+ pcData.model = glm::translate(glm::mat4(1.0f), glm::vec3(0.5f, 0.12f, 0.0f));
70+
+ pcData.enableTexture = 0;
6671
+ }
67-
+ commandBuffer.pushConstants<PushConstantData>(
72+
+ commandBuffer.pushConstants<glm::mat4>(
6873
+ m_pipelineLayout,
6974
+ vk::ShaderStageFlagBits::eVertex,
7075
+ 0, // offset
71-
+ pcData
76+
+ pcData.model
77+
+ );
78+
+ commandBuffer.pushConstants<uint32_t>(
79+
+ m_pipelineLayout,
80+
+ vk::ShaderStageFlagBits::eFragment,
81+
+ sizeof(glm::mat4), // offset
82+
+ pcData.enableTexture
7283
+ );
7384
+ commandBuffer.drawIndexed(
7485
+ counter == m_indicesOffsets.size() ? m_indices.size() - firstIndex : m_indicesOffsets[counter] - firstIndex,
@@ -82,7 +93,15 @@ index b0d2dfe..6258fc4 100644
8293

8394
commandBuffer.endRenderPass();
8495
commandBuffer.end();
85-
@@ -1124,16 +1163,6 @@ private:
96+
@@ -1069,7 +1119,6 @@ private:
97+
/////////////////////////////////////////////////////////////////
98+
/// descriptor layout and buffer
99+
struct alignas(16) UniformBufferObject {
100+
- glm::mat4 model;
101+
glm::mat4 view;
102+
glm::mat4 proj;
103+
};
104+
@@ -1124,16 +1173,6 @@ private:
86105
front = glm::normalize(front);
87106

88107
UniformBufferObject ubo{};
@@ -99,7 +118,7 @@ index b0d2dfe..6258fc4 100644
99118
ubo.view = glm::lookAt(
100119
m_cameraPos,
101120
m_cameraPos + front,
102-
@@ -1573,15 +1602,17 @@ private:
121+
@@ -1573,17 +1612,19 @@ private:
103122

104123
/////////////////////////////////////////////////////////////////
105124
/// load model
@@ -117,9 +136,12 @@ index b0d2dfe..6258fc4 100644
117136
+
118137
+ m_indicesOffsets.push_back(m_indices.size());
119138

120-
std::unordered_map<
139+
- std::unordered_map<
140+
+ static std::unordered_map<
121141
Vertex,
122-
@@ -1605,10 +1636,14 @@ private:
142+
uint32_t,
143+
decltype( [](const Vertex& vertex) -> size_t {
144+
@@ -1605,10 +1646,12 @@ private:
123145
attrib.vertices[3 * index.vertex_index + 2]
124146
};
125147

@@ -132,13 +154,11 @@ index b0d2dfe..6258fc4 100644
132154
+ attrib.texcoords[2 * index.texcoord_index],
133155
+ 1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
134156
+ };
135-
+ } else {
136-
+ vertex.texCoord = {0.61f, 0.17f};
137157
+ }
138158

139159
vertex.color = {1.0f, 1.0f, 1.0f};
140160

141-
@@ -1821,6 +1856,13 @@ private:
161+
@@ -1821,6 +1864,14 @@ private:
142162
);
143163
}
144164
/////////////////////////////////////////////////////////////////
@@ -147,6 +167,7 @@ index b0d2dfe..6258fc4 100644
147167
+ /// push constant
148168
+ struct PushConstantData {
149169
+ glm::mat4 model;
170+
+ uint32_t enableTexture;
150171
+ };
151172
+ /////////////////////////////////////////////////////////////////
152173
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
diff --git a/shaders/shader.frag b/shaders/shader.frag
2+
index 873f541..c763702 100644
3+
--- a/shaders/shader.frag
4+
+++ b/shaders/shader.frag
5+
@@ -1,5 +1,9 @@
6+
#version 450
7+
8+
+layout(push_constant) uniform PushConstants {
9+
+ layout(offset = 64) uint enableTexture;
10+
+} pc;
11+
+
12+
layout(binding = 1) uniform sampler2D texSampler;
13+
14+
layout(location = 0) in vec3 fragColor;
15+
@@ -8,5 +12,9 @@ layout(location = 1) in vec2 fragTexCoord;
16+
layout(location = 0) out vec4 outColor;
17+
18+
void main() {
19+
- outColor = texture(texSampler, fragTexCoord);
20+
+ if (pc.enableTexture > 0) {
21+
+ outColor = texture(texSampler, fragTexCoord);
22+
+ } else {
23+
+ outColor = vec4(fragColor, 1);
24+
+ }
25+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#version 450
22

3+
layout(push_constant) uniform PushConstants {
4+
layout(offset = 64) uint enableTexture;
5+
} pc;
6+
37
layout(binding = 1) uniform sampler2D texSampler;
48

59
layout(location = 0) in vec3 fragColor;
@@ -8,5 +12,9 @@ layout(location = 1) in vec2 fragTexCoord;
812
layout(location = 0) out vec4 outColor;
913

1014
void main() {
11-
outColor = texture(texSampler, fragTexCoord);
15+
if (pc.enableTexture > 0) {
16+
outColor = texture(texSampler, fragTexCoord);
17+
} else {
18+
outColor = vec4(fragColor, 1);
19+
}
1220
}

docs/codes/03/40_pushconstant/shaders/shader.vert

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ layout(push_constant) uniform PushConstants {
55
} pc;
66

77
layout(binding = 0) uniform UniformBufferObject {
8-
mat4 model;
98
mat4 view;
109
mat4 proj;
1110
} ubo;

docs/codes/03/40_pushconstant/shaders/vert.diff

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
diff --git a/shaders/shader.vert b/shaders/shader.vert
2-
index 840711c..b70f03e 100644
2+
index 840711c..1986b39 100644
33
--- a/shaders/shader.vert
44
+++ b/shaders/shader.vert
5-
@@ -1,5 +1,9 @@
5+
@@ -1,7 +1,10 @@
66
#version 450
77

8+
-layout(binding = 0) uniform UniformBufferObject {
89
+layout(push_constant) uniform PushConstants {
9-
+ mat4 model;
10+
mat4 model;
1011
+} pc;
1112
+
12-
layout(binding = 0) uniform UniformBufferObject {
13-
mat4 model;
13+
+layout(binding = 0) uniform UniformBufferObject {
1414
mat4 view;
15-
@@ -14,7 +18,7 @@ layout(location = 0) out vec3 fragColor;
15+
mat4 proj;
16+
} ubo;
17+
@@ -14,7 +17,7 @@ layout(location = 0) out vec3 fragColor;
1618
layout(location = 1) out vec2 fragTexCoord;
1719

1820
void main() {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)