Skip to content

Commit

Permalink
Switch to a fragment shader solution for border tile rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Jupeyy committed Nov 4, 2023
1 parent dbfe888 commit 5fbfd26
Show file tree
Hide file tree
Showing 27 changed files with 804 additions and 1,159 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ set(EXPECTED_DATA
editor/entities_clear/vanilla.png
editor/front.png
editor/speed_arrow.png
editor/speed_arrow_array.png
editor/speedup.png
editor/switch.png
editor/tele.png
Expand Down Expand Up @@ -1550,6 +1551,8 @@ set(EXPECTED_DATA
shader/text.vert
shader/tile.frag
shader/tile.vert
shader/tile_border.frag
shader/tile_border.vert
shader/vulkan/prim.frag
shader/vulkan/prim.vert
shader/vulkan/prim3d.frag
Expand All @@ -1564,6 +1567,8 @@ set(EXPECTED_DATA
shader/vulkan/text.vert
shader/vulkan/tile.frag
shader/vulkan/tile.vert
shader/vulkan/tile_border.frag
shader/vulkan/tile_border.vert
skins/PaladiN.png
skins/antiantey.png
skins/beast.png
Expand Down
16 changes: 5 additions & 11 deletions cmake/BuildVulkanShaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,12 @@ if(NOT FOUND_MATCHING_SHA256_FILE)
generate_shader_file("-DTW_TILE_TEXTURED" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.frag" "data/shader/vulkan/tile_textured.frag.spv")
generate_shader_file("-DTW_TILE_TEXTURED" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.vert" "data/shader/vulkan/tile_textured.vert.spv")

generate_shader_file("-DTW_TILE_BORDER" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.frag" "data/shader/vulkan/tile_border.frag.spv")
generate_shader_file("-DTW_TILE_BORDER" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.vert" "data/shader/vulkan/tile_border.vert.spv")

generate_shader_file("-DTW_TILE_BORDER" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.frag" "data/shader/vulkan/tile_border_textured.frag.spv")
generate_shader_file("-DTW_TILE_BORDER" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.vert" "data/shader/vulkan/tile_border_textured.vert.spv")

generate_shader_file("-DTW_TILE_BORDER_LINE" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.frag" "data/shader/vulkan/tile_border_line.frag.spv")
generate_shader_file("-DTW_TILE_BORDER_LINE" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.vert" "data/shader/vulkan/tile_border_line.vert.spv")

generate_shader_file("-DTW_TILE_BORDER_LINE" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.frag" "data/shader/vulkan/tile_border_line_textured.frag.spv")
generate_shader_file("-DTW_TILE_BORDER_LINE" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile.vert" "data/shader/vulkan/tile_border_line_textured.vert.spv")
generate_shader_file("" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile_border.frag" "data/shader/vulkan/tile_border.frag.spv")
generate_shader_file("" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile_border.vert" "data/shader/vulkan/tile_border.vert.spv")

generate_shader_file("" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile_border.frag" "data/shader/vulkan/tile_border_textured.frag.spv")
generate_shader_file("" "-DTW_TILE_TEXTURED" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/tile_border.vert" "data/shader/vulkan/tile_border_textured.vert.spv")

# quad layer
generate_shader_file("" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/quad.frag" "data/shader/vulkan/quad.frag.spv")
generate_shader_file("" "" "${PROJECT_SOURCE_DIR}/data/shader/vulkan/quad.vert" "data/shader/vulkan/quad.vert.spv")
Expand Down
Binary file added data/editor/speed_arrow_array.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 2 additions & 27 deletions data/shader/tile.vert
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
layout (location = 0) in vec2 inVertex;
#ifdef TW_TILE_TEXTURED
layout (location = 1) in vec3 inVertexTexCoord;
layout (location = 1) in uvec4 inVertexTexCoord;
#endif

uniform mat4x2 gPos;

#if defined(TW_TILE_BORDER) || defined(TW_TILE_BORDER_LINE)
uniform vec2 gDir;
uniform vec2 gOffset;
#endif

#if defined(TW_TILE_BORDER)
uniform int gJumpIndex;
#endif

#ifdef TW_TILE_TEXTURED
noperspective out vec3 TexCoord;
#endif

void main()
{
#if defined(TW_TILE_BORDER)
vec4 VertPos = vec4(inVertex, 0.0, 1.0);
int XCount = gl_InstanceID - (int(gl_InstanceID/gJumpIndex) * gJumpIndex);
int YCount = (int(gl_InstanceID/gJumpIndex));
VertPos.x += gOffset.x + gDir.x * float(XCount);
VertPos.y += gOffset.y + gDir.y * float(YCount);

gl_Position = vec4(gPos * VertPos, 0.0, 1.0);
#elif defined(TW_TILE_BORDER_LINE)
vec4 VertPos = vec4(inVertex.x + gOffset.x, inVertex.y + gOffset.y, 0.0, 1.0);
VertPos.x += gDir.x * float(gl_InstanceID);
VertPos.y += gDir.y * float(gl_InstanceID);

gl_Position = vec4(gPos * VertPos, 0.0, 1.0);
#else
gl_Position = vec4(gPos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0);
#endif

#ifdef TW_TILE_TEXTURED
TexCoord = inVertexTexCoord;
TexCoord = vec3(inVertexTexCoord.xyz);
#endif
}
28 changes: 28 additions & 0 deletions data/shader/tile_border.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifdef TW_TILE_TEXTURED
#ifdef TW_TILE_3D_TEXTURED
uniform sampler3D gTextureSampler;
#else
uniform sampler2DArray gTextureSampler;
#endif
#endif

uniform vec4 gVertColor;

#ifdef TW_TILE_TEXTURED
noperspective in vec3 TexCoord;
#endif

out vec4 FragClr;

void main()
{
#ifdef TW_TILE_TEXTURED
vec3 realTexCoords = vec3(fract(TexCoord.xy), TexCoord.z);
vec2 dx = dFdx(TexCoord.xy);
vec2 dy = dFdy(TexCoord.xy);
vec4 tex = textureGrad(gTextureSampler, realTexCoords, dx, dy);
FragClr = tex * gVertColor;
#else
FragClr = gVertColor;
#endif
}
28 changes: 28 additions & 0 deletions data/shader/tile_border.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
layout (location = 0) in vec2 inVertex;
#ifdef TW_TILE_TEXTURED
layout (location = 1) in uvec4 inVertexTexCoord;
#endif

uniform mat4x2 gPos;

uniform vec2 gOffset;
uniform vec2 gScale;

#ifdef TW_TILE_TEXTURED
noperspective out vec3 TexCoord;
#endif

void main()
{
// scale then position vertex
vec2 VertexPos = (inVertex * gScale) + gOffset;
gl_Position = vec4(gPos * vec4(VertexPos, 0.0, 1.0), 0.0, 1.0);

#ifdef TW_TILE_TEXTURED
// scale the texture coordinates too
vec2 TexScale = gScale;
if (float(inVertexTexCoord.w) > 0.0)
TexScale = gScale.yx;
TexCoord = vec3(vec2(inVertexTexCoord.xy) * TexScale, float(inVertexTexCoord.z));
#endif
}
29 changes: 2 additions & 27 deletions data/shader/vulkan/tile.vert
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@

layout (location = 0) in vec2 inVertex;
#ifdef TW_TILE_TEXTURED
layout (location = 1) in vec3 inVertexTexCoord;
layout (location = 1) in uvec4 inVertexTexCoord;
#endif

layout(push_constant) uniform SPosBO {
layout(offset = 0) uniform mat4x2 gPos;

#if defined(TW_TILE_BORDER) || defined(TW_TILE_BORDER_LINE)
layout(offset = 32) uniform vec2 gDir;
layout(offset = 40) uniform vec2 gOffset;
#endif

#if defined(TW_TILE_BORDER)
layout(offset = 48) uniform int gJumpIndex;
#endif
} gPosBO;

#ifdef TW_TILE_TEXTURED
Expand All @@ -25,25 +16,9 @@ layout (location = 0) noperspective out vec3 TexCoord;

void main()
{
#if defined(TW_TILE_BORDER)
vec4 VertPos = vec4(inVertex, 0.0, 1.0);
int XCount = gl_InstanceIndex - (int(gl_InstanceIndex/gPosBO.gJumpIndex) * gPosBO.gJumpIndex);
int YCount = (int(gl_InstanceIndex/gPosBO.gJumpIndex));
VertPos.x += gPosBO.gOffset.x + gPosBO.gDir.x * float(XCount);
VertPos.y += gPosBO.gOffset.y + gPosBO.gDir.y * float(YCount);

gl_Position = vec4(gPosBO.gPos * VertPos, 0.0, 1.0);
#elif defined(TW_TILE_BORDER_LINE)
vec4 VertPos = vec4(inVertex.x + gPosBO.gOffset.x, inVertex.y + gPosBO.gOffset.y, 0.0, 1.0);
VertPos.x += gPosBO.gDir.x * float(gl_InstanceIndex);
VertPos.y += gPosBO.gDir.y * float(gl_InstanceIndex);

gl_Position = vec4(gPosBO.gPos * VertPos, 0.0, 1.0);
#else
gl_Position = vec4(gPosBO.gPos * vec4(inVertex, 0.0, 1.0), 0.0, 1.0);
#endif

#ifdef TW_TILE_TEXTURED
TexCoord = inVertexTexCoord;
TexCoord = vec3(inVertexTexCoord.xyz);
#endif
}
28 changes: 28 additions & 0 deletions data/shader/vulkan/tile_border.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

#ifdef TW_TILE_TEXTURED
layout(binding = 0) uniform sampler2DArray gTextureSampler;
#endif

layout(push_constant) uniform SVertexColorBO {
layout(offset = 64) uniform vec4 gVertColor;
} gColorBO;

#ifdef TW_TILE_TEXTURED
layout (location = 0) noperspective in vec3 TexCoord;
#endif

layout (location = 0) out vec4 FragClr;
void main()
{
#ifdef TW_TILE_TEXTURED
vec3 realTexCoords = vec3(fract(TexCoord.xy), TexCoord.z);
vec2 dx = dFdx(TexCoord.xy);
vec2 dy = dFdy(TexCoord.xy);
vec4 tex = textureGrad(gTextureSampler, realTexCoords, dx, dy);
FragClr = tex * gColorBO.gVertColor;
#else
FragClr = gColorBO.gVertColor;
#endif
}
33 changes: 33 additions & 0 deletions data/shader/vulkan/tile_border.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable

layout (location = 0) in vec2 inVertex;
#ifdef TW_TILE_TEXTURED
layout (location = 1) in uvec4 inVertexTexCoord;
#endif

layout(push_constant) uniform SPosBO {
layout(offset = 0) uniform mat4x2 gPos;

layout(offset = 32) uniform vec2 gOffset;
layout(offset = 40) uniform vec2 gScale;
} gPosBO;

#ifdef TW_TILE_TEXTURED
layout (location = 0) noperspective out vec3 TexCoord;
#endif

void main()
{
// scale then position vertex
vec2 VertexPos = (inVertex * gPosBO.gScale) + gPosBO.gOffset;
gl_Position = vec4(gPosBO.gPos * vec4(VertexPos, 0.0, 1.0), 0.0, 1.0);

#ifdef TW_TILE_TEXTURED
// scale the texture coordinates too
vec2 TexScale = gPosBO.gScale;
if (inVertexTexCoord.w > 0)
TexScale = gPosBO.gScale.yx;
TexCoord = vec3(vec2(inVertexTexCoord.xy) * TexScale, float(inVertexTexCoord.z));
#endif
}
2 changes: 2 additions & 0 deletions src/base/vmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define BASE_VMATH_H

#include <cmath>
#include <cstdint>

#include "math.h"

Expand Down Expand Up @@ -399,5 +400,6 @@ class vector4_base
typedef vector4_base<float> vec4;
typedef vector4_base<bool> bvec4;
typedef vector4_base<int> ivec4;
typedef vector4_base<uint8_t> ubvec4;

#endif
Loading

0 comments on commit 5fbfd26

Please sign in to comment.