Skip to content

Commit b9c49c2

Browse files
committed
Fixed 2D depth buffering issues
1 parent 254f779 commit b9c49c2

File tree

12 files changed

+40
-59
lines changed

12 files changed

+40
-59
lines changed

Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
include make/Functions.mk
88
include make/Platform.mk
99

10-
optimisation := -O3
11-
1210
# Set debugging build flags
1311
DEBUG ?= 1
1412
ifeq ($(DEBUG), 1)
1513
override CXXFLAGS += -g -DDEBUG -DCC_LOG_LEVEL=2
16-
optimisation :=
1714
else
1815
override CXXFLAGS += -DNDEBUG -DCC_LOG_LEVEL=0 -O3
1916
endif
@@ -52,7 +49,7 @@ export exampleGameApp := $(binDir)/examples/game/build/app
5249
export exampleRenderApp := $(binDir)/examples/render/build/app
5350

5451
# Set build vars
55-
export compileFlags := -Wall -std=c++17 $(optimisation)
52+
export compileFlags := -Wall -std=c++17
5653
export linkFlags += -L $(libDir)
5754
buildFlagsFile:=.buildflags
5855

engine/render/assets/shaders/Quad2DInstanced.vert

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ layout (binding = 0) uniform CameraData {
3535

3636
void main() {
3737

38-
vec4 vertexPosition = inTransform * vec4(quadVertices[gl_VertexIndex], 0.0, 1.0);
38+
vec4 vertexPosition = inTransform * vec4(quadVertices[gl_VertexIndex], 1.0, 1.0);
3939
gl_Position = cameraData.projectionMatrix * cameraData.viewMatrix * vertexPosition;
4040

4141
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (inUv.x + inUv.z))

engine/render/assets/shaders/Text2DInstanced.vert

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
// Per instance data
1212
layout(location = 0) in mat4 transform;
13-
layout(location = 4) in vec4 texData; // Stores the minimum x and y values for the glyph in the texture + the glyph's dimensions in the texture
14-
layout(location = 5) in vec4 colour;
13+
layout(location = 4) in vec4 colour;
14+
layout(location = 5) in vec4 texData; // Stores the minimum x and y values for the glyph in the texture + the glyph's dimensions in the texture
1515
layout(location = 6) in vec4 coordinates; // stores x and y coordinates in space + the glyph's dimensions in space
1616

1717
layout(location = 0) out vec4 fragColor;
@@ -44,7 +44,7 @@ void main() {
4444
float posY = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (coordinates.y + coordinates.w))
4545
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * coordinates.y);
4646

47-
gl_Position = cameraData.projectionMatrix * cameraData.viewMatrix * transform * vec4(posX, posY, 0, 1.0);
47+
gl_Position = cameraData.projectionMatrix * cameraData.viewMatrix * transform * vec4(posX, posY, 1.0, 1.0);
4848

4949
fragColor = colour;
5050
outUv = vec2(uvx, uvy);

engine/render/renderer/Renderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void Renderer::DrawText2D(const char* const text,
131131
const IColour colour,
132132
const uint8_t zIndex)
133133
{
134-
renderer2D.DrawText2D(text, position, scale, font, colour);
134+
renderer2D.DrawText2D(text, position, scale, font, colour, rotation, zIndex);
135135
}
136136

137137
void Renderer::DrawGrid2D(float spacing,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ void Font::PopulateTextureAtlas(uint8_t* buffer)
150150
glyph.widthNormalised /= extent.width;
151151
glyph.heightNormalised /= extent.height;
152152

153-
Utils::Extent3D imageExtent {(uint32_t) glyph.width, (uint32_t) glyph.height, 1};
153+
Utils::Extent3D imageExtent {(uint32_t) (glyph.width), (uint32_t) (glyph.height), 1};
154154

155-
size_t size = sizeof(uint8_t) * glyph.width * glyph.height;
155+
size_t size = sizeof(uint8_t) * (glyph.width * glyph.height);
156156
if (size == 0) continue;
157157

158158
texture.CopyToRegion(buffer + glyph.bufferOffset,

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void Renderer2D::Initialise(const char* const globalDataName)
7171
quadIndexBuffer = Vulkan::IndexBuffer(6, fontIndices);
7272

7373
quadVertexBuffer = Vulkan::VertexBuffer(sizeof(QuadVertex) * QUAD_VERTEX_BUFFER_SIZE);
74-
textVertexBuffer = Vulkan::VertexBuffer(sizeof(FontData) * TEXT_VERTEX_BUFFER_SIZE);
74+
textVertexBuffer = Vulkan::VertexBuffer(sizeof(FontVertex) * TEXT_VERTEX_BUFFER_SIZE);
7575
}
7676

7777
void Renderer2D::DrawQuad(const Vec2 position,
@@ -93,7 +93,7 @@ void Renderer2D::DrawQuad(const Vec2 position,
9393
layerQuads[texIndex] = MHArray<QuadVertex>(MAX_QUADS_PER_LAYER);
9494

9595
layerQuads[texIndex].Append(
96-
{Graphics::CalculateTransform3D(Vec3(position.x + scale.x, position.y + scale.y, zIndex),
96+
{Graphics::CalculateTransform3D(Vec3(position.x + scale.x, position.y + scale.y, 1.f),
9797
{0.f, 0.f, rotation},
9898
scale),
9999
ToFColour(colour),
@@ -115,7 +115,7 @@ void Renderer2D::DrawText2D(const char* const text,
115115
auto& layerQuads = characters[zIndex];
116116

117117
if (texIndex >= layerQuads.Count())
118-
layerQuads[texIndex] = MHArray<FontData>(MAX_TEXTS_PER_FONT * MAX_CHARS_PER_TEXT);
118+
layerQuads[texIndex] = MHArray<FontVertex>(MAX_TEXTS_PER_FONT * MAX_CHARS_PER_TEXT);
119119

120120
auto& fontTexts = layerQuads[texIndex];
121121

@@ -136,11 +136,11 @@ void Renderer2D::DrawText2D(const char* const text,
136136
Vec4 coordinates = {x + glyph.bearingX, y - (height + yOffset), glyph.width, height};
137137

138138
fontTexts.Append(
139-
{Graphics::CalculateTransform3D(Vec3(position.x, position.y + scale.y, zIndex),
139+
{Graphics::CalculateTransform3D(Vec3(position.x, position.y + scale.y, 1.f),
140140
{0.f, 0.f, rotation},
141141
scale),
142-
{glyph.uvxMin, glyph.uvyMin, glyph.widthNormalised, glyph.heightNormalised},
143142
ToFColour(colour),
143+
{glyph.uvxMin, glyph.uvyMin, glyph.widthNormalised, glyph.heightNormalised},
144144
coordinates / textScale});
145145

146146
x += glyph.advance >> 6;
@@ -199,7 +199,7 @@ void Renderer2D::RenderText(Vulkan::CommandBuffer& buffer, size_t index)
199199
uint64_t vertexBufferOffset =
200200
(index * MAX_TEXTURES) + (j * MAX_TEXTS_PER_FONT * MAX_CHARS_PER_TEXT);
201201

202-
textVertexBuffer.Update(sizeof(FontData),
202+
textVertexBuffer.Update(sizeof(FontVertex),
203203
quadArr.Data(),
204204
quadArr.Count(),
205205
vertexBufferOffset);

engine/render/renderer/renderer/Renderer2D.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ class Renderer2D
6464
Vec4 uvData {};
6565
};
6666

67-
struct FontData
67+
struct FontVertex
6868
{
6969
Mat4 transform {};
70-
Vec4 uvData {};
7170
FColour colour {};
71+
Vec4 uvData {};
7272
Vec4 position {};
7373
};
7474

@@ -118,7 +118,7 @@ class Renderer2D
118118
Vulkan::Material textMaterial;
119119
Vulkan::VertexBuffer textVertexBuffer;
120120

121-
SArray<MSArray<MHArray<FontData>, MAX_TEXTURES>, MAX_LAYERS> characters;
121+
SArray<MSArray<MHArray<FontVertex>, MAX_TEXTURES>, MAX_LAYERS> characters;
122122

123123
// 2D grid
124124

engine/utils/math/Graphics.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Siege::Graphics
1515
{
1616

17-
Mat4 Perspective(const float& fovy, const float& aspect, const float& near, const float& far)
17+
Mat4 Perspective(float fovy, float aspect, float near, float far)
1818
{
1919
const float tanHalfFovy = tan(fovy / 2.f);
2020

@@ -29,12 +29,7 @@ Mat4 Perspective(const float& fovy, const float& aspect, const float& near, cons
2929
return projectionMatrix;
3030
}
3131

32-
Mat4 Orthographic(const float& left,
33-
const float& right,
34-
const float& top,
35-
const float& bottom,
36-
const float& near,
37-
const float& far)
32+
Mat4 Orthographic(float left, float right, float top, float bottom, float near, float far)
3833
{
3934
Mat4 projectionMatrix = Siege::Mat4::Identity;
4035

@@ -142,7 +137,7 @@ Mat4 CalculateTransform3D(const Vec3& position, const Vec3& rotation, const Vec3
142137
{position.x, position.y, position.z, 1.0f}};
143138
}
144139

145-
Mat3 CalculateTransform2D(const Vec2& position, const float& rotation, const Vec2& scale)
140+
Mat3 CalculateTransform2D(const Vec2& position, float rotation, const Vec2& scale)
146141
{
147142
const float s = Float::Sin(rotation);
148143
const float c = Float::Cos(rotation);

engine/utils/math/Graphics.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Siege::Graphics
2626
* @param far the far clipping range.
2727
* @return a 4x4 perspective projection matrix.
2828
*/
29-
Mat4 Perspective(const float& fovy, const float& aspect, const float& near, const float& far);
29+
Mat4 Perspective(float fovy, float aspect, float near, float far);
3030

3131
/**
3232
* Returns a 4x4 orthographic projection (typically used for 2D projections)
@@ -38,12 +38,7 @@ Mat4 Perspective(const float& fovy, const float& aspect, const float& near, cons
3838
* @param far the far clipping range.
3939
* @return a 4x4 orthographic projection matrix.
4040
*/
41-
Mat4 Orthographic(const float& left,
42-
const float& right,
43-
const float& top,
44-
const float& bottom,
45-
const float& near,
46-
const float& far);
41+
Mat4 Orthographic(float left, float right, float top, float bottom, float near, float far);
4742

4843
/**
4944
* Creates a projection matrix that's looking at a specific directioin.
@@ -89,7 +84,7 @@ Mat4 CalculateTransform3D(const Vec3& position, const Vec3& rotation, const Vec3
8984
* @param scale the scale of the object.
9085
* @return the transform matrix of the object's spatial positioning.
9186
*/
92-
Mat3 CalculateTransform2D(const Vec2& position, const float& rotation, const Vec2& scale);
87+
Mat3 CalculateTransform2D(const Vec2& position, float rotation, const Vec2& scale);
9388

9489
/**
9590
* Calculates the normal matrix of a 3D object.

examples/render/src/Camera.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@
1212
#include <render/input/Input.h>
1313
#include <utils/math/Float.h>
1414

15-
void Camera::UpdatePerspectiveProjection(const float& fovy,
16-
const float& aspect,
17-
const float& near,
18-
const float& far)
15+
void Camera::UpdatePerspectiveProjection(float fovy, float aspect, float near, float far)
1916
{
2017
projectionMatrix = Siege::Graphics::Perspective(fovy, aspect, near, far);
2118
}
2219

23-
void Camera::UpdateOrthographicProjection(const float& left,
24-
const float& right,
25-
const float& top,
26-
const float& bottom,
27-
const float& near,
28-
const float& far)
20+
void Camera::UpdateOrthographicProjection(float left,
21+
float right,
22+
float top,
23+
float bottom,
24+
float near,
25+
float far)
2926
{
3027
projectionMatrix = Siege::Graphics::Orthographic(left, right, top, bottom, near, far);
3128
}

0 commit comments

Comments
 (0)