Skip to content

Commit 9e63f60

Browse files
authored
Merge pull request #41 from CapsCollective/feature/2D-quad-renderer
Implemented true 2D rendering
2 parents b7c26ca + b9c49c2 commit 9e63f60

39 files changed

+1225
-237
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DEBUG ?= 1
1212
ifeq ($(DEBUG), 1)
1313
override CXXFLAGS += -g -DDEBUG -DCC_LOG_LEVEL=2
1414
else
15-
override CXXFLAGS += -DNDEBUG -DCC_LOG_LEVEL=0
15+
override CXXFLAGS += -DNDEBUG -DCC_LOG_LEVEL=0 -O3
1616
endif
1717

1818
# Set platform vars
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 vec3 cellColour;
12+
layout(location=1) in flat float cellSpacing;
13+
layout(location=2) in flat float lineWidth;
14+
layout(location=3) in flat float cellMultiple;
15+
layout(location=4) in flat float fadeFactor;
16+
layout(location=5) in flat float scale;
17+
18+
// output variables
19+
layout(location = 0) out vec4 outColour;
20+
21+
float getAxisGridColour(float uv, float width, float factor, float derivative)
22+
{
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+
*/
33+
return max(step(abs(fract(uv - 0.5) -0.5) / derivative, width), factor);
34+
}
35+
36+
vec4 grid(float spacing, float width, vec2 uv, vec3 lColour)
37+
{
38+
float derivative = 1.0 / spacing;
39+
40+
vec4 colour = vec4(getAxisGridColour(uv.y, width, getAxisGridColour(uv.x, width, 0.0, derivative), derivative));
41+
42+
// Check if the uv sits within the range of the cell multiples
43+
vec2 modCoords = vec2(mod(uv.x, cellMultiple), mod(uv.y, cellMultiple));
44+
45+
// Fade the cell colours depending on whether they fall within the cell multiples range or not
46+
vec4 mixColour = mix(vec4(cellColour * fadeFactor, fadeFactor), vec4(cellColour, 1.0), float(modCoords.x < width || modCoords.y < width));
47+
48+
// Change the alpha based on whether the fragment is a grid line or not
49+
colour = mix(colour, vec4(mixColour), colour.a);
50+
51+
return colour;
52+
}
53+
54+
void main()
55+
{
56+
// Determine the spacing and width of our cells
57+
const float spacing = cellSpacing * scale;
58+
const float width = lineWidth / spacing;
59+
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)
63+
vec2 uv = (gl_FragCoord.xy - 0.5) / spacing;
64+
65+
outColour = grid(spacing, width, uv, cellColour);
66+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
vec2 quadVertices[4] = {
12+
vec2(1.0, 1.0),
13+
vec2(1.0, -1.0),
14+
vec2(-1.0, -1.0),
15+
vec2(-1.0, 1.0)
16+
};
17+
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
24+
25+
layout (push_constant) uniform PushConstant {
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
28+
} pushConstant;
29+
30+
void main()
31+
{
32+
vec2 pos = quadVertices[gl_VertexIndex];
33+
gl_Position = vec4(quadVertices[gl_VertexIndex], 0.0, 1.0);
34+
35+
cellColour = pushConstant.cellColouring.xyz;
36+
fadeFactor = pushConstant.cellColouring.w;
37+
cellSpacing = pushConstant.cellDimensions.x;
38+
cellMultiple = pushConstant.cellDimensions.y;
39+
scale = pushConstant.cellDimensions.z;
40+
lineWidth = pushConstant.cellDimensions.w;
41+
}
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 inColour;
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 = texture(tex[texId], uv);
21+
outColour = sampled * inColour;
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// Per-instance vertex data
12+
layout (location = 0) in mat4 inTransform;
13+
layout (location = 4) in vec4 inColour;
14+
layout (location = 5) in vec4 inUv;
15+
16+
layout (location = 0) out vec4 outColour;
17+
layout (location = 1) out vec2 outUv;
18+
layout (location = 2) out flat uint outTexId;
19+
20+
layout (push_constant) uniform PushConstant {
21+
uint textureIndex;
22+
} pushConstant;
23+
24+
vec2 quadVertices[4] = {
25+
vec2(1.0, 1.0),
26+
vec2(1.0, -1.0),
27+
vec2(-1.0, -1.0),
28+
vec2(-1.0, 1.0)
29+
};
30+
31+
layout (binding = 0) uniform CameraData {
32+
mat4 projectionMatrix;
33+
mat4 viewMatrix;
34+
} cameraData;
35+
36+
void main() {
37+
38+
vec4 vertexPosition = inTransform * vec4(quadVertices[gl_VertexIndex], 1.0, 1.0);
39+
gl_Position = cameraData.projectionMatrix * cameraData.viewMatrix * vertexPosition;
40+
41+
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (inUv.x + inUv.z))
42+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * (inUv.x));
43+
float uvy = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (inUv.y + inUv.w))
44+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * (inUv.y));
45+
46+
outColour = inColour;
47+
outUv = vec2(uvx, uvy);
48+
outTexId = pushConstant.textureIndex;
49+
}
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 = texture(tex[texId], uv);
21+
outColour = sampled * fragColour;
22+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 inTransform;
13+
layout (location = 4) in vec4 inColour;
14+
layout (location = 5) in vec4 inUv;
15+
16+
layout (location = 0) out vec4 outColour;
17+
layout (location = 1) out vec2 outUv;
18+
layout (location = 2) out flat uint outTexId;
19+
20+
layout (push_constant) uniform PushConstant {
21+
uint textureIndex;
22+
} pushConstant;
23+
24+
vec3 quadVertices[4] = {
25+
vec3(1.0, 1.0, 0),
26+
vec3(1.0, -1.0, 0),
27+
vec3(-1.0, -1.0, 0),
28+
vec3(-1.0, 1.0, 0)
29+
};
30+
31+
struct CameraData
32+
{
33+
mat4 projectionMatrix;
34+
mat4 viewMatrix;
35+
};
36+
37+
layout (binding = 0) uniform GlobalData {
38+
CameraData cameraData;
39+
} globalData;
40+
41+
CameraData camera = globalData.cameraData;
42+
43+
void main() {
44+
gl_Position = camera.projectionMatrix * camera.viewMatrix * inTransform * vec4(quadVertices[gl_VertexIndex], 1.0);
45+
46+
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (inUv.x + inUv.z))
47+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * (inUv.x));
48+
float uvy = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (inUv.y + inUv.w))
49+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * (inUv.y));
50+
51+
outColour = inColour;
52+
outUv = vec2(uvx, uvy);
53+
outTexId = pushConstant.textureIndex;
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
float distance = texture(tex[texId], uv).r;
21+
float smoothWidth = fwidth(distance);
22+
float alpha = smoothstep(0.5 - smoothWidth, 0.5 + smoothWidth, distance);
23+
24+
outColour = mix(vec4(fragColour.rgb, 0.0), fragColour, alpha);
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 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
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+
layout (binding = 0) uniform CameraData {
27+
mat4 projectionMatrix;
28+
mat4 viewMatrix;
29+
} cameraData;
30+
31+
void main() {
32+
vec2 coords = vec2(texData.xy);
33+
vec2 dimensions = vec2(texData.zw);
34+
35+
// Find the UV based on the vertex index + min values = coordinate + dimension
36+
float uvx = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (texData.x + texData.z))
37+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * (texData.x));
38+
float uvy = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (texData.y + texData.w))
39+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * (texData.y));
40+
41+
// Find the vertex position based off the vertex index + min values = coordinate + dimension
42+
float posX = (float(gl_VertexIndex == 0 || gl_VertexIndex == 1) * (coordinates.x + coordinates.z))
43+
+ (float(gl_VertexIndex == 2 || gl_VertexIndex == 3) * coordinates.x);
44+
float posY = (float(gl_VertexIndex == 0 || gl_VertexIndex == 3) * (coordinates.y + coordinates.w))
45+
+ (float(gl_VertexIndex == 1 || gl_VertexIndex == 2) * coordinates.y);
46+
47+
gl_Position = cameraData.projectionMatrix * cameraData.viewMatrix * transform * vec4(posX, posY, 1.0, 1.0);
48+
49+
fragColor = colour;
50+
outUv = vec2(uvx, uvy);
51+
outTexId = textureData.textureIndex;
52+
}

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
}

0 commit comments

Comments
 (0)