Skip to content

Commit 7c6d20d

Browse files
committed
Added comments and cleaned old code
1 parent 5097b26 commit 7c6d20d

File tree

16 files changed

+143
-105
lines changed

16 files changed

+143
-105
lines changed

engine/render/assets/shaders/Grid2D.frag

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ layout(location = 0) out vec4 outColour;
2020

2121
float getAxisGridColour(float uv, float width, float factor, float derivative)
2222
{
23+
/**
24+
* There are a few things happening here:
25+
* 1. The fract function returns the fraction of the given value (the parts after the decimal place). This returns to us
26+
* another normalised coordinate which we can then use to repeat patterns.
27+
* 2. We get the fraction of the uv (remember the has been divided by the spacing we provided earlier), and offset it
28+
* by half the screen.
29+
* 3. We then get the absolute value of the new UV - half the screen (so that we don't get any negatives)
30+
* 4. We divide this by the derivative to give us a smaller space (used for anti-aliasing)
31+
* 5. We then use a step function to determine if the provided UV coordinate is within the cell borders
32+
*/
2333
return max(step(abs(fract(uv - 0.5) -0.5) / derivative, width), factor);
2434
}
2535

@@ -29,20 +39,27 @@ vec4 grid(float spacing, float width, vec2 uv, vec3 lColour)
2939

3040
vec4 colour = vec4(getAxisGridColour(uv.y, width, getAxisGridColour(uv.x, width, 0.0, derivative), derivative));
3141

42+
// Check if the uv sits within the range of the cell multiples
3243
vec2 modCoords = vec2(mod(uv.x, cellMultiple), mod(uv.y, cellMultiple));
3344

45+
// Fade the cell colours depending on whether they fall within the cell multiples range or not
3446
vec4 mixColour = mix(vec4(cellColour * fadeFactor, fadeFactor), vec4(cellColour, 1.0), float(modCoords.x < width || modCoords.y < width));
3547

48+
// Change the alpha based on whether the fragment is a grid line or not
3649
colour = mix(colour, vec4(mixColour), colour.a);
3750

3851
return colour;
3952
}
4053

4154
void main()
4255
{
56+
// Determine the spacing and width of our cells
4357
const float spacing = cellSpacing * scale;
4458
const float width = lineWidth / spacing;
4559

60+
// Determine the uv location of the fragment we're dealing with
61+
// NOTE: in glsl, gl_FragCoord always provides the fragment location + 0.5. As such, fragment 0,0 is represented as
62+
// (0.5,0.5)
4663
vec2 uv = (gl_FragCoord.xy - 0.5) / spacing;
4764

4865
outColour = grid(spacing, width, uv, cellColour);

engine/render/assets/shaders/Grid2D.vert

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ vec2 quadVertices[4] = {
1515
vec2(-1.0, 1.0)
1616
};
1717

18-
layout(location=0) out vec3 cellColour;
19-
layout(location=1) out flat float cellSpacing;
20-
layout(location=2) out flat float lineWidth;
21-
layout(location=3) out flat float cellMultiple;
22-
layout(location=4) out flat float fadeFactor;
23-
layout(location=5) out flat float scale;
18+
layout(location=0) out vec3 cellColour; // The colour of our grid lines
19+
layout(location=1) out flat float cellSpacing; // the spacing between lines in pixels
20+
layout(location=2) out flat float lineWidth; // the width of our lines
21+
layout(location=3) out flat float cellMultiple; // determines how many cells will be encompassed in a sub-grid
22+
layout(location=4) out flat float fadeFactor; // determines how much sub-cells are faded
23+
layout(location=5) out flat float scale; // the DPI scale of the screen
2424

2525
layout (push_constant) uniform PushConstant {
26-
vec4 cellColouring;
27-
vec4 cellDimensions;
26+
vec4 cellColouring; // Stores the cellColour in xyz and fadeFactor in w
27+
vec4 cellDimensions; // Stores the cell spacing in x, multiple in y, dimensions in z, and width in w
2828
} pushConstant;
2929

3030
void main()

engine/render/assets/shaders/grid.frag

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,4 @@ void main() {
5656

5757
outColor = (grid(fragPos3D, 1) + grid(fragPos3D, 1))* float(t > 0);
5858
outColor.a *= fading;
59-
60-
if (outColor.a == 0) discard;
6159
}

engine/render/renderer/Renderer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ void Renderer::RecreateSwapChain()
6464
{
6565
ClearDeviceQueue();
6666
auto extent = window.GetExtent();
67-
while (extent.width == 0 || extent.height == 0)
67+
68+
while (!window.IsVisible() || extent.width == 0.0 || extent.height == 0.0)
6869
{
6970
extent = window.GetExtent();
7071
window.WaitEvents();
@@ -82,6 +83,7 @@ void Renderer::RecreateSwapChain()
8283
if (!swapchain.IsSameSwapFormat(oldImageFormat, oldDepthFormat))
8384
{
8485
Renderer3D::RecreateMaterials();
86+
renderer2D.RecreateMaterials();
8587
}
8688
}
8789

@@ -93,7 +95,7 @@ bool Renderer::StartFrame()
9395

9496
auto result = swapchain.AcquireNextImage(&currentImageIndex);
9597

96-
if (result == Vulkan::Utils::ERROR_OUT_OF_DATE)
98+
if (result == Vulkan::Utils::ERROR_OUT_OF_DATE || !window.IsVisible())
9799
{
98100
RecreateSwapChain();
99101
return false;
@@ -161,7 +163,7 @@ void Renderer::EndFrame()
161163

162164
auto result = swapchain.SubmitCommandBuffers(commandBuffers, currentImageIndex);
163165

164-
if (result == Vulkan::Utils::ERROR_RESIZED || window.WasResized())
166+
if (result == Vulkan::Utils::ERROR_RESIZED || window.WasResized() || !window.IsVisible())
165167
{
166168
window.ResetWindowResized();
167169
RecreateSwapChain();

engine/render/renderer/platform/vulkan/Material.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222

2323
namespace Siege::Vulkan
2424
{
25-
Material::Material(Shader vertShader, Shader fragShader) :
25+
Material::Material(Shader vertShader, Shader fragShader, bool isWritingDepth) :
2626
vertexShader {std::move(vertShader)},
27-
fragmentShader {std::move(fragShader)}
27+
fragmentShader {std::move(fragShader)},
28+
isWritingDepth {isWritingDepth}
2829
{
2930
using Vulkan::Context;
3031
using namespace Vulkan::Utils::Descriptor;
@@ -277,7 +278,7 @@ void Material::Recreate()
277278
.WithPushConstant(pushConstant.size, pushConstant.type)
278279
.WithVertexShader(&vertexShader)
279280
.WithFragmentShader(&fragmentShader)
280-
.WithDepthWriting(vertexShader.IsWritingDepth())
281+
.WithDepthWriting(isWritingDepth)
281282
.WithProperties(layouts)
282283
.Build();
283284
}
@@ -408,6 +409,7 @@ void Material::Swap(Material& other)
408409
auto tmpBufferInfos = std::move(bufferInfos);
409410
auto tmpTexture2DInfos = std::move(texture2DInfos);
410411
auto tmpPushConstant = pushConstant;
412+
auto tmpIsWritingDepth = isWritingDepth;
411413

412414
vertexShader = std::move(other.vertexShader);
413415
fragmentShader = std::move(other.fragmentShader);
@@ -420,6 +422,7 @@ void Material::Swap(Material& other)
420422
bufferInfos = std::move(other.bufferInfos);
421423
texture2DInfos = std::move(other.texture2DInfos);
422424
pushConstant = other.pushConstant;
425+
isWritingDepth = other.isWritingDepth;
423426

424427
other.vertexShader = std::move(tmpVertexShader);
425428
other.fragmentShader = std::move(tmpFragmentShader);
@@ -432,5 +435,6 @@ void Material::Swap(Material& other)
432435
other.bufferInfos = std::move(tmpBufferInfos);
433436
other.texture2DInfos = std::move(tmpTexture2DInfos);
434437
other.pushConstant = tmpPushConstant;
438+
other.isWritingDepth = tmpIsWritingDepth;
435439
}
436440
} // namespace Siege::Vulkan

engine/render/renderer/platform/vulkan/Material.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace Siege::Vulkan
3535
* remaking every them every frame
3636
* @param texture2DInfos a list of texture 2D rendering infos used in textures
3737
* @param textureIds a list of texture IDs (used for checking the existence of textures)
38+
* @param isWritingDepth a boolean specifying if the material pipeline is writing to the depth
39+
* buffer
3840
*/
3941
class Material
4042
{
@@ -50,8 +52,9 @@ class Material
5052
* A constructor for the material which expects a vertex and fragment shader at minimum
5153
* @param vertShader the vertex shader to be used by the Material
5254
* @param fragShader the fragment shader to be used by the Material
55+
* @param isWritingDepth specifies if the material pipeline is writing to the depth buffer
5356
*/
54-
Material(Shader vertShader, Shader fragShader);
57+
Material(Shader vertShader, Shader fragShader, bool isWritingDepth = true);
5558
/**
5659
* A move constructor for the Material class
5760
* @param other the Material to be moved
@@ -63,6 +66,9 @@ class Material
6366
*/
6467
~Material();
6568

69+
/**
70+
* Frees the memory held by the material
71+
*/
6672
void Free();
6773

6874
/**
@@ -150,6 +156,9 @@ class Material
150156
MSArray<Property, 10> properties;
151157
};
152158

159+
/**
160+
* A struct specifying a push constant type
161+
*/
153162
struct PushConstant
154163
{
155164
uint32_t size {0};
@@ -274,6 +283,8 @@ class Material
274283
// Texture data
275284
MSArray<VkDescriptorImageInfo, MAX_TEXTURES> texture2DInfos;
276285
MSArray<Hash::StringId, MAX_TEXTURES> textureIds;
286+
287+
bool isWritingDepth {true};
277288
};
278289
} // namespace Siege::Vulkan
279290

engine/render/renderer/platform/vulkan/Pipeline.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Pipeline::Builder& Pipeline::Builder::WithFragmentShader(const Shader* fragShade
4949

5050
Pipeline::Builder& Pipeline::Builder::WithDepthWriting(bool state)
5151
{
52-
isWritingDepth = state;
52+
usingDepthWrite = state;
5353
return *this;
5454
}
5555

@@ -90,6 +90,8 @@ Pipeline Pipeline::Builder::Build()
9090
auto dynamicStateCreateInfos =
9191
CreateDynamicStates(viewportCount + scissorCount, (VkDynamicState*) dynamicStates.Data());
9292

93+
auto depthState = Utils::Pipeline::CreateStencilState(usingDepthWrite);
94+
9395
// Get shader number and their stages.
9496

9597
MSArray<VkPipelineShaderStageCreateInfo, 5> shaderStages {
@@ -148,9 +150,7 @@ Pipeline Pipeline::Builder::Build()
148150
pipelineCreateInfo.pMultisampleState = &Utils::Pipeline::defaultMultiSampleState;
149151
pipelineCreateInfo.pColorBlendState = &Utils::Pipeline::defaultColourBlendStateCreate;
150152
pipelineCreateInfo.pDynamicState = &dynamicStateCreateInfos;
151-
pipelineCreateInfo.pDepthStencilState =
152-
isWritingDepth ? &Utils::Pipeline::defaultStencilCreateState :
153-
&Utils::Pipeline::defaultTransluscentStencilCreateState;
153+
pipelineCreateInfo.pDepthStencilState = &depthState;
154154

155155
pipelineCreateInfo.layout = newPipeline.layout;
156156
pipelineCreateInfo.renderPass = renderPass->GetRenderPass();

engine/render/renderer/platform/vulkan/Pipeline.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ class Pipeline
105105
*/
106106
Builder& WithFragmentShader(const Shader* fragShader);
107107

108+
/**
109+
* Specifies if the pipeline is writing to the depth buffer
110+
* @param state the state of depth buffer writing
111+
* @return a reference to the current builder instance
112+
*/
108113
Builder& WithDepthWriting(bool state);
109114

110115
/**
@@ -114,6 +119,12 @@ class Pipeline
114119
*/
115120
Builder& WithProperties(const MSArray<VkDescriptorSetLayout, 10>& layouts);
116121

122+
/**
123+
* Specifies a push constant to be used by the pipeline
124+
* @param size the size of the push constant
125+
* @param type the shader that the push constant will be pushed to
126+
* @return a reference to the current builder instance
127+
*/
117128
Builder& WithPushConstant(uint32_t size, Utils::ShaderType type);
118129

119130
/**
@@ -136,7 +147,6 @@ class Pipeline
136147

137148
bool usingDepthTest {true};
138149
bool usingDepthWrite {true};
139-
bool isWritingDepth {true};
140150

141151
RenderPass* renderPass {nullptr};
142152
};

engine/render/renderer/platform/vulkan/Shader.cpp

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ Shader::VertexBinding& Shader::VertexBinding::AddFloatVec2Attribute()
4545
return *this;
4646
}
4747

48-
Shader::VertexBinding& Shader::VertexBinding::AddU32Attribute()
49-
{
50-
attributes.Append({stride, Utils::VertexAttributeType::VERTEX_UINT_32, inputRate});
51-
stride += sizeof(uint32_t);
52-
return *this;
53-
}
54-
5548
Shader::VertexBinding& Shader::VertexBinding::AddMat4Attribute()
5649
{
5750
return AddFloatVec4Attribute()
@@ -60,16 +53,6 @@ Shader::VertexBinding& Shader::VertexBinding::AddMat4Attribute()
6053
.AddFloatVec4Attribute();
6154
}
6255

63-
Shader::VertexBinding& Shader::VertexBinding::AddMat2Attribute()
64-
{
65-
return AddFloatVec4Attribute().AddFloatVec4Attribute();
66-
}
67-
68-
Shader::VertexBinding& Shader::VertexBinding::AddMat3Attribute()
69-
{
70-
return AddFloatVec4Attribute().AddFloatVec4Attribute().AddFloatVec4Attribute();
71-
}
72-
7356
Shader::Builder& Shader::Builder::WithGlobalData3DUniform(uint32_t set)
7457
{
7558
return WithUniform<Siege::Renderer3D::GlobalData>("globalData", set);
@@ -138,12 +121,6 @@ Shader::Builder& Shader::Builder::WithVertexBinding(const Shader::VertexBinding&
138121
return *this;
139122
}
140123

141-
Shader::Builder& Shader::Builder::WithDepthWrite(bool state)
142-
{
143-
isWritingDepth = state;
144-
return *this;
145-
}
146-
147124
Shader Shader::Builder::Build() const
148125
{
149126
return Shader(filePath,
@@ -154,8 +131,7 @@ Shader Shader::Builder::Build() const
154131
defaultTextureInfo,
155132
pushConstant,
156133
totalUniformSize,
157-
attributeCount,
158-
isWritingDepth);
134+
attributeCount);
159135
}
160136

161137
Shader::Shader(const Shader& other) :
@@ -165,8 +141,7 @@ Shader::Shader(const Shader& other) :
165141
vertexBindings {other.vertexBindings},
166142
defaultTextureInfo {other.defaultTextureInfo},
167143
pushConstant {other.pushConstant},
168-
totalUniformSize {other.totalUniformSize},
169-
isWritingDepth {other.isWritingDepth}
144+
totalUniformSize {other.totalUniformSize}
170145
{
171146
CreateShaderModule();
172147
}
@@ -179,8 +154,7 @@ Shader::Shader(const String& filePath,
179154
Texture2D::Info tex2DInfo,
180155
PushConstant pushConstant,
181156
size_t totalSize,
182-
uint32_t totalVertexAttributes,
183-
bool isWritingDepth) :
157+
uint32_t totalVertexAttributes) :
184158
filePath {filePath},
185159
type {type},
186160
expectedTopology {expectedTopology},
@@ -189,8 +163,7 @@ Shader::Shader(const String& filePath,
189163
defaultTextureInfo {tex2DInfo},
190164
pushConstant {pushConstant},
191165
totalUniformSize {totalSize},
192-
totalVertexAttributeCount {totalVertexAttributes},
193-
isWritingDepth {isWritingDepth}
166+
totalVertexAttributeCount {totalVertexAttributes}
194167
{
195168
CreateShaderModule();
196169
}
@@ -249,7 +222,6 @@ void Shader::Swap(Shader& other)
249222
auto tmpExpectedTopology = expectedTopology;
250223
auto tmpDefaultTexture2DInfo = defaultTextureInfo;
251224
auto tmpPushConstant = pushConstant;
252-
auto tmpIsWritingDepth = isWritingDepth;
253225

254226
filePath = other.filePath;
255227
type = other.type;
@@ -261,7 +233,6 @@ void Shader::Swap(Shader& other)
261233
expectedTopology = other.expectedTopology;
262234
defaultTextureInfo = other.defaultTextureInfo;
263235
pushConstant = other.pushConstant;
264-
isWritingDepth = other.isWritingDepth;
265236

266237
other.filePath = tmpFilePath;
267238
other.type = tmpShaderType;
@@ -273,6 +244,5 @@ void Shader::Swap(Shader& other)
273244
other.expectedTopology = tmpExpectedTopology;
274245
other.defaultTextureInfo = tmpDefaultTexture2DInfo;
275246
other.pushConstant = tmpPushConstant;
276-
other.isWritingDepth = tmpIsWritingDepth;
277247
}
278248
} // namespace Siege::Vulkan

0 commit comments

Comments
 (0)