Skip to content

Commit b7c26ca

Browse files
authored
Merge pull request #38 from CapsCollective/feature/text-rendering
Added 3D text rendering capabilities
2 parents 99cc2e5 + bf4c2b6 commit b7c26ca

Some content is hidden

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

68 files changed

+2525
-457
lines changed

.github/workflows/windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v2
2121

2222
- name: perform setup
23-
run: ./scripts/setup.bat
23+
run: ./scripts/Setup.ps1
2424

2525
- name: build all targets
2626
run: mingw32-make

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@
3737
[submodule "vendor/vulkan/Vulkan-ValidationLayers"]
3838
path = vendor/vulkan/Vulkan-ValidationLayers
3939
url = https://github.com/KhronosGroup/Vulkan-ValidationLayers.git
40+
[submodule "vendor/zlib"]
41+
path = vendor/zlib
42+
url = https://github.com/madler/zlib.git
43+
[submodule "vendor/libpng"]
44+
path = vendor/libpng
45+
url = https://github.com/glennrp/libpng.git
46+
[submodule "vendor/freetype"]
47+
path = vendor/freetype
48+
url = https://github.com/freetype/freetype.git

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ $ sudo apt install libasound2-dev mesa-common-dev libx11-dev libxrandr-dev libxi
8989
```
9090
```console
9191
// Windows
92-
> ./scripts/setup.bat
92+
> ./scripts/Setup.ps1
9393
```
9494

9595
3. This should install all required dependencies. Once completed a `.env` file will be generated with all required variables. If the build is completed with no issue then you can proceed to build the project.
@@ -104,14 +104,14 @@ $ ./scripts/setup.sh --include-validation-layers
104104
```
105105
```console
106106
// Windows
107-
> ./scripts/setup.bat --include-validation-layers
107+
> ./scripts/Setup.ps1 -Include_Validation_Layers
108108
```
109109

110110
**NOTE**: Building with this option can take some time to complete. Please be patient while the project builds the required validation layers.
111111

112112
### Building the Project
113113

114-
Assuming all dependencies have been satisfied, the project can be build using the following command:
114+
Assuming all dependencies have been satisfied, the project can be built using the following command:
115115

116116
```console
117117
// Linux and macOS

engine/render/Makefile

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ renderSources := $(call rwildcard,$(renderSrcDir)/,*.cpp)
1414
renderObjects := $(call findobjs,$(renderSrcDir),$(renderBinDir),$(renderSources))
1515
renderDepends := $(patsubst %.o, %.d, $(call rwildcard,$(renderBinDir)/,*.o))
1616
renderBuildDir := $(renderBinDir)/build
17+
renderLibs := $(vendorDir)/glfw/build/src/libglfw3.a $(vendorDir)/zlib/build/lib/libz.a \
18+
$(vendorDir)/libpng/build/libpng.a $(vendorDir)/freetype/build/libfreetype.a
1719

1820
# Set shader build vars
1921
vertSources := $(call rwildcard,assets/shaders,*.vert)
@@ -23,44 +25,31 @@ fragObjects := $(patsubst %.frag,$(renderBuildDir)/%.frag.spv,$(fragSources))
2325

2426
linkFlags += -l utils
2527

26-
# Set build vars
27-
ifeq ($(platform), windows)
28-
volkDefines = VK_USE_PLATFORM_WIN32_KHR
29-
glslangValidator := $(vendorDir)\glslang\build\install\bin\glslangValidator.exe
30-
else ifeq ($(platform), linux)
31-
volkDefines = VK_USE_PLATFORM_XLIB_KHR
32-
glslangValidator := $(vendorDir)/glslang/build/install/bin/glslangValidator
33-
else ifeq ($(platform), macos)
34-
volkDefines = VK_USE_PLATFORM_MACOS_MVK
35-
glslangValidator := $(vendorDir)/glslang/build/install/bin/glslangValidator
36-
endif
3728
compileFlags += -I $(vendorDir)/vulkan/include -I $(vendorDir)/glfw/include -I $(vendorDir)/glm \
38-
-I $(vendorDir)/tinyobjloader -I $(vendorDir)/stb_image
29+
-I $(vendorDir)/tinyobjloader -I $(vendorDir)/stb_image -I $(vendorDir)/zlib/build/include \
30+
-I $(vendorDir)/libpng -I $(vendorDir)/include/freetype
3931

4032
.PHONY: all vulkan-libs
4133

4234
all: $(renderLib) $(vertObjects) $(fragObjects) vulkan-libs
4335

4436
# Build the static library
4537
$(renderLib): $(renderObjects)
46-
$(MKDIR) $(call platformpth, $(libDir))
4738
$(RM) $(call platformpth, $(renderLib))
48-
$(call COPY,$(vendorDir)/glfw/src,$(libDir),libglfw3.a)
49-
mv $(call platformpth,$(libDir)/libglfw3.a) $(call platformpth,$(libDir)/librender.a)
50-
ar -rcs $(renderLib) $(renderObjects)
39+
$(call COMBINE_LIBS, $(renderLibs), $(renderObjects), $(libDir), render)
5140

5241
# Add all rules from dependency files
5342
-include $(renderDepends)
5443

5544
# Compile object files to the bin directory
5645
$(renderBinDir)/%.o: $(renderSrcDir)/%.cpp
5746
$(MKDIR) $(call platformpth, $(@D))
58-
$(CXX) -MMD -MP -c $(compileFlags) -I $(engineDir) $< -o $@ $(CXXFLAGS) -D$(volkDefines)
47+
$(CXX) -MMD -MP -c $(compileFlags) -I $(engineDir) $< -o $@ $(CXXFLAGS) -DVK_USE_PLATFORM_$(VOLK_OS)
5948

6049
# Compile shaders to the build directory
6150
$(renderBuildDir)/assets/shaders/%.spv: $(renderSrcDir)/assets/shaders/%
6251
$(MKDIR) $(call platformpth, $(@D))
63-
$(call platformpth,$(glslangValidator)) $< -V -o $@
52+
$(call platformpth,$(vendorDir)/glslang/build/StandAlone/glslangValidator$(EXE_NAME)) $< -V -o $@
6453

6554
# Copy Vulkan libraries to the build directory
6655
vulkan-libs:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3+
//
4+
// This code is released under an unmodified zlib license.
5+
// For conditions of distribution and use, please see:
6+
// https://opensource.org/licenses/Zlib
7+
//
8+
9+
#version 460
10+
11+
layout (location = 0) in vec4 fragColour;
12+
layout (location = 1) in vec2 uv;
13+
layout (location = 2) in flat uint texId;
14+
15+
layout (location = 0) out vec4 outColour;
16+
17+
layout(binding = 1) uniform sampler2D tex[16];
18+
19+
void main() {
20+
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(tex[texId], uv).r);
21+
outColour = fragColour * sampled;
22+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Copyright (c) 2022 Jonathan Moallem (@J-Mo63) & Aryeh Zinn (@Raelr)
3+
//
4+
// This code is released under an unmodified zlib license.
5+
// For conditions of distribution and use, please see:
6+
// https://opensource.org/licenses/Zlib
7+
//
8+
9+
#version 450
10+
11+
// Per instance data
12+
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;
15+
layout(location = 6) in vec4 coordinates; // stores x and y coordinates in space + the glyph's dimensions in space
16+
17+
layout(location = 0) out vec4 fragColor;
18+
layout(location = 1) out vec2 outUv;
19+
layout(location = 2) out uint outTexId;
20+
21+
layout (push_constant) uniform TextureData
22+
{
23+
uint textureIndex;
24+
} textureData;
25+
26+
struct CameraData
27+
{
28+
mat4 projectionMatrix;
29+
mat4 viewMatrix;
30+
};
31+
32+
layout (binding = 0) uniform GlobalData {
33+
CameraData cameraData;
34+
} globalData;
35+
36+
CameraData camera = globalData.cameraData;
37+
38+
void main() {
39+
vec2 coords = vec2(texData.xy);
40+
vec2 dimensions = vec2(texData.zw);
41+
42+
// Find the UV based on the vertex index - max values = coordinate + dimension
43+
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (texData.x + texData.z))
44+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * (texData.x));
45+
float uvy = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (texData.y + texData.w))
46+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * (texData.y));
47+
48+
// Find the vertex position based off the vertex index - max values = coordinate + dimension
49+
float posX = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (coordinates.x + coordinates.z))
50+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * coordinates.x);
51+
float posY = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (coordinates.y + coordinates.w))
52+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * coordinates.y);
53+
54+
gl_Position = camera.projectionMatrix * camera.viewMatrix * transform * vec4(posX, posY, 0, 1.0);
55+
56+
fragColor = colour;
57+
outUv = vec2(uvx, uvy);
58+
outTexId = textureData.textureIndex;
59+
}

engine/render/assets/shaders/texturedQuad.frag

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ layout (location = 0) out vec4 outColour;
99
layout(binding = 1) uniform sampler2D tex[16];
1010

1111
void main() {
12-
vec4 sampled = vec4(texture(tex[texId], uv));
13-
outColour = vec4(fragColour, 1.0) * sampled;
12+
vec3 sampled = texture(tex[texId], uv).rgb;
13+
outColour = vec4(fragColour * sampled, 1.0);
1414
}

engine/render/renderer/Renderer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#include "Renderer.h"
1212

13+
#include "platform/vulkan/Font.h"
1314
#include "platform/vulkan/utils/Types.h"
15+
#include "statics/Statics.h"
1416

1517
namespace Siege
1618
{
@@ -20,6 +22,8 @@ uint32_t Renderer::currentFrameIndex = 0;
2022

2123
Renderer::Renderer(Window& window) : window {window}
2224
{
25+
Statics::Initialise();
26+
2327
if (instance == nullptr) instance = this;
2428

2529
auto extent = window.GetExtent();
@@ -48,14 +52,15 @@ Renderer::~Renderer()
4852
DescriptorPool::DestroyPool();
4953
Renderer3D::DestroyRenderer3D();
5054
Renderer2D::DestroyRenderer2D();
55+
Statics::Free();
5156
}
5257

5358
void Renderer::DrawFrame()
5459
{
5560
Renderer2D::GlobalData global2DData = {projection};
5661

57-
Renderer3D::Render(currentFrameIndex, commandBuffers, projection);
5862
Renderer2D::Render(commandBuffers, global2DData, currentFrameIndex);
63+
Renderer3D::Render(currentFrameIndex, commandBuffers, projection);
5964
}
6065

6166
void Renderer::RecreateSwapChain()
@@ -113,6 +118,7 @@ bool Renderer::StartFrame()
113118
void Renderer::EndFrame()
114119
{
115120
Renderer2D::Update();
121+
Renderer3D::Update();
116122

117123
CC_ASSERT(isFrameStarted, "Can't end frame while frame is not in progress!")
118124

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212
#include <utils/collections/StackArray.h>
1313
#include <volk/volk.h>
1414

15+
#ifdef __APPLE__
16+
#define EXTENSIONS_COUNT 3
17+
#define OS_EXTENSIONS , "VK_KHR_portability_subset"
18+
#else
19+
#define EXTENSIONS_COUNT 2
20+
#define OS_EXTENSIONS
21+
#endif
22+
1523
namespace Siege::Vulkan
1624
{
1725
class Config
1826
{
1927
public:
2028

2129
static constexpr uint32_t VALIDATION_LAYERS_COUNT = 1;
22-
static constexpr uint32_t EXTENSIONS_COUNT = 2;
2330

2431
/**
2532
* An array storing all required validation layers (if enabled).
@@ -33,7 +40,7 @@ class Config
3340
**/
3441
static constexpr SArray<const char*, EXTENSIONS_COUNT> deviceExtensions = {
3542
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
36-
VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME};
43+
VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME OS_EXTENSIONS};
3744
};
3845
} // namespace Siege::Vulkan
3946

0 commit comments

Comments
 (0)