Commit 41eb6368 authored by Shu's avatar Shu 🍠
Browse files

Just a Vulkan triangle

parent ba8405e5
......@@ -9,3 +9,5 @@
*.tga filter=lfs diff=lfs merge=lfs -text
*.tiff filter=lfs diff=lfs merge=lfs -text
*.BMP filter=lfs diff=lfs merge=lfs -text
# Shaders
*.spv filter=lfs diff=lfs merge=lfs -text
......@@ -41,8 +41,8 @@ file(GLOB_RECURSE CORE_SOURCES "src/core/*.cpp" "include/tracy/TracyClient.cpp")
set(CORE_HEADERS "include/toml++" "include/robin_hood" "include/libguarded" "include/tracy")
set(CORE_LIBS pthread dl glm::glm_static enet::enet_static zstd::zstd_static)
file(GLOB_RECURSE CLIENT_SOURCES "src/client/*.cpp" "include/imgui/*.cpp" "include/meshoptimizer/*.cpp" "include/gl3w/gl3w.c")
set(CLIENT_HEADERS "include/imgui" "include/meshoptimizer" "include/gl3w")
file(GLOB_RECURSE CLIENT_SOURCES "src/client/*.cpp" "include/imgui/*.cpp" "include/meshoptimizer/*.cpp" "include/gl3w/gl3w.c" "include/volk/volk.c")
set(CLIENT_HEADERS "include/imgui" "include/meshoptimizer" "include/gl3w" "include/volk")
set(CLIENT_LIBS glfw)
file(GLOB_RECURSE SERVER_SOURCES "src/server/*.cpp" "include/FastNoiseSIMD/*.cpp")
......
......@@ -54,6 +54,7 @@ To get a local copy up and running, follow these simple steps.
* Python: utility scripts
* Tracy v0.7: profiling
* glslc: compile vk shaders
### Installation
......
......@@ -3,7 +3,8 @@
## Hello screen again
- [~] Extract OpenGL
- [ ] Minimal Vulkan
- Rename Passes/Programs as Pipelines
- [~] Minimal Vulkan
- [ ] ImGui
- [ ] Config (yaml)
......
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(fragColor, 1.0);
}
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec3 fragColor;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
}
\ No newline at end of file
#version 450 core
layout (constant_id = 0) const bool FOG = true;
layout (constant_id = 1) const bool PBR = true;
layout (constant_id = 2) const bool TRIPLANAR = false;
layout (constant_id = 3) const bool STOCHASTIC = false;
layout (constant_id = 4) const bool BLEND = true;
layout (constant_id = 16) UNIT_SIZE = 8;
// Ouput data
layout(location = 0) out vec3 color;
uniform sampler2DArray TextureAtlas;
uniform sampler2DArray NormalAtlas;
uniform sampler2DArray HOSAtlas;
uniform mat4 View;
uniform vec3 FogColor;
#ifdef GEOMETRY
in GeometryData
#else
in VertexData
#endif
{
vec3 Position_modelspace;
#ifdef GEOMETRY
flat uint Textures[3];
vec3 TextureRatio;
#else
flat uint Texture;
#endif
vec3 FaceNormal_modelspace;
vec3 FaceNormal_worldspace;
vec3 EyeDirection_cameraspace;
vec3 LightDirection_cameraspace;
float Depth;
} vs;
vec3 expand(vec3 v) {
return (v - 0.5) * 2;
}
vec2 hash2D(vec2 s) {
// magic numbers
return fract(sin(mod(vec2(dot(s, vec2(127.1, 311.7)), dot(s, vec2(269.5, 183.3))), 3.14159)) * 43758.5453);
}
vec4 textureStochastic(sampler2DArray sample, vec3 UV) {
if(STOCHASTIC) {
// triangular by approx 2*sqrt(3)
vec2 skewUV = mat2(1.0, 0.0, -0.57735027, 1.15470054) * (UV.xy * 3.46400);
// vertex id and barrycentric coords
vec2 vxID = vec2(floor(skewUV));
vec3 barry = vec3(fract(skewUV), 0);
barry.z = 1.0 - barry.x - barry.y;
vec3 BW_vx0;
vec3 BW_vx1;
vec3 BW_vx2;
vec3 BW_vx3;
if(barry.z > 0) {
BW_vx0 = vec3(vxID, 0);
BW_vx1 = vec3(vxID + vec2(0, 1), 0);
BW_vx2 = vec3(vxID + vec2(1, 0), 0);
BW_vx3 = barry.zyx;
} else {
BW_vx0 = vec3(vxID + vec2(1, 1), 0);
BW_vx1 = vec3(vxID + vec2(1, 0), 0);
BW_vx2 = vec3(vxID + vec2(0, 1), 0);
BW_vx3 = vec3(-barry.z, 1.0 - barry.y, 1.0 - barry.x);
}
vec2 dx = dFdx(UV.xy);
vec2 dy = dFdy(UV.xy);
return textureGrad(sample, vec3(UV.xy + hash2D(BW_vx0.xy), UV.z), dx, dy) * BW_vx3.x +
textureGrad(sample, vec3(UV.xy + hash2D(BW_vx1.xy), UV.z), dx, dy) * BW_vx3.y +
textureGrad(sample, vec3(UV.xy + hash2D(BW_vx2.xy), UV.z), dx, dy) * BW_vx3.z;
} else {
return texture(sample, UV);
}
}
vec4 getTexture(sampler2DArray sample, vec2 UV) {
#ifdef GEOMETRY
if(BLEND) {
vec4 colx = textureStochastic(sample, vec3(UV, vs.Textures[0]));
if(vs.Textures[1] == vs.Textures[0]) {
return vs.Textures[2] == vs.Textures[0] ? colx :
mix(colx, textureStochastic(sample, vec3(UV, vs.Textures[2])), vs.TextureRatio.z);
} else {
vec4 coly = textureStochastic(sample, vec3(UV, vs.Textures[1]));
return vs.Textures[2] == vs.Textures[0] ? mix(colx, coly, vs.TextureRatio.y) : (
vs.Textures[2] == vs.Textures[1] ? mix(coly, colx, vs.TextureRatio.x) :
colx * vs.TextureRatio.x + coly * vs.TextureRatio.y + textureStochastic(sample, vec3(UV, vs.Textures[2])) * vs.TextureRatio.z);
}
} else {
int mainTexture = vs.TextureRatio.x >= vs.TextureRatio.y ?
(vs.TextureRatio.x >= vs.TextureRatio.z ? 0 : 2) :
(vs.TextureRatio.y >= vs.TextureRatio.z ? 1 : 2);
return textureStochastic(sample, vec3(UV, vs.Textures[mainTexture]));
}
#else
return textureStochastic(sample, vec3(UV, vs.Texture));
#endif
}
vec3 getTriTexture(sampler2DArray sample, vec2 crdx, vec2 crdy, vec2 crdz, vec3 weights) {
return getTexture(sample, crdx).rgb * weights.x +
getTexture(sample, crdy).rgb * weights.y +
getTexture(sample, crdz).rgb * weights.z;
}
void main() {
float texScale = 1. / UNIT_SIZE;
if(TRIPLANAR) {
// Triplanar
float plateauSize = 0.001;
float transitionSpeed = 2;
vec3 blendWeights = abs(vs.FaceNormal_modelspace);
blendWeights = blendWeights - plateauSize;
blendWeights = pow(max(blendWeights, 0), vec3(transitionSpeed));
vec2 UVx = vs.Position_modelspace.yz * texScale;
vec2 UVy = vs.Position_modelspace.zx * texScale;
vec2 UVz = vs.Position_modelspace.xy * texScale;
vec3 tex = getTriTexture(TextureAtlas, UVx, UVy, UVz, blendWeights);
if(PBR) {
// Whiteout normal blend
vec3 texNx = expand(getTexture(NormalAtlas, UVx).rgb);
vec3 texNy = expand(getTexture(NormalAtlas, UVy).rgb);
vec3 texNz = expand(getTexture(NormalAtlas, UVz).rgb);
// Swizzle world normals into tangent space and apply Whiteout blend
texNx = vec3(texNx.xy + vs.FaceNormal_worldspace.zy, abs(texNx.z) * vs.FaceNormal_worldspace.x);
texNy = vec3(texNy.xy + vs.FaceNormal_worldspace.xz, abs(texNy.z) * vs.FaceNormal_worldspace.y);
texNz = vec3(texNz.xy + vs.FaceNormal_worldspace.xy, abs(texNz.z) * vs.FaceNormal_worldspace.z);
// Swizzle tangent normals to match world orientation and triblend
vec3 worldNormal = normalize(texNx.zyx * blendWeights.x + texNy.xzy * blendWeights.y +texNz.xyz * blendWeights.z);
vec3 texHOS = getTriTexture(HOSAtlas, UVx, UVy, UVz, blendWeights);
}
} else {
// Cheap planar
vec3 blendWeights = abs(vs.FaceNormal_modelspace);
vec3 nrm = normalize(pow(blendWeights, vec3(80 / sqrt(UNIT_SIZE))));
vec2 UV = (vec2(vs.Position_modelspace.xy * nrm.z) + vec2(vs.Position_modelspace.yz * nrm.x) + vec2(vs.Position_modelspace.zx * nrm.y)) * texScale;
vec3 tex = getTexture(TextureAtlas, UV).rgb;
if(PBR) {
vec3 texN = expand(getTexture(NormalAtlas, UV).rgb);
// Swizzle world normals into tangent space and apply Whiteout blend
// Swizzle tangent normals to match world orientation and triblend
vec3 worldNormal = normalize(vec3(texN.xy + vs.FaceNormal_worldspace.zy, abs(texN.z) * vs.FaceNormal_worldspace.x).zyx * blendWeights.x +
vec3(texN.xy + vs.FaceNormal_worldspace.xz, abs(texN.z) * vs.FaceNormal_worldspace.y).xzy * blendWeights.y +
vec3(texN.xy + vs.FaceNormal_worldspace.xy, abs(texN.z) * vs.FaceNormal_worldspace.z).xyz * blendWeights.z);
vec3 texHOS = getTexture(HOSAtlas, UV).rgb;
}
}
// Colors
if(PBR) {
// Texture properties
vec3 TextureDiffuseColor = tex;
vec3 TextureAmbientColor = vec3(.1) * TextureDiffuseColor * texHOS.y;
vec3 TextureSpecularColor = vec3(.8) * texHOS.z;
vec3 Normal_cameraspace = normalize((View * vec4(worldNormal,0)).xyz);
// Light emission properties
// You probably want to put them as uniforms
vec3 LightColor = vec3(1, 0.9, 0.9);
float LightPower = 1.2f;
// Distance to the light
float distance = 1.0f;//length( LightPosition_worldspace - Position_worldspace );
// Direction of the light (from the fragment to the light)
vec3 l = normalize(vs.LightDirection_cameraspace);
// Cosine of the angle between the normal and the light direction,
// clamped above 0
// - light is at the vertical of the triangle -> 1
// - light is perpendiular to the triangle -> 0
// - light is behind the triangle -> 0
float cosTheta = clamp(dot(Normal_cameraspace,l), 0,1 );
// Eye vector (towards the camera)
vec3 E = normalize(vs.EyeDirection_cameraspace);
// Direction in which the triangle reflects the light
vec3 R = reflect(-l,Normal_cameraspace);
// Cosine of the angle between the Eye vector and the Reflect vector,
// clamped to 0
// - Looking into the reflection -> 1
// - Looking elsewhere -> < 1
float cosAlpha = clamp( dot( E,R ), 0,1 );
float visibility=1.0;
// MAYBE: shadow
color =
// Ambient : simulates indirect lighting
TextureAmbientColor +
// Diffuse : "color" of the object
visibility * TextureDiffuseColor * LightColor * LightPower * cosTheta / (distance * distance) +
// Specular : reflective highlight, like a mirror
visibility * TextureSpecularColor * LightColor * LightPower * pow(cosAlpha,5) / (distance * distance);
} else {
color = tex;
}
if(FOG) {
float ratio = exp(vs.Depth * 0.69)-1;
color = mix(color, pow(FogColor, vec3(2.2)), clamp(ratio, 0, 1));
}
color = pow(color, vec3(1.0 / 2.2));
if(color.r > 1 || color.g > 1 || color.b > 1) {
color = vec3(1, 0, 0); //TODO: bloom
}
}
\ No newline at end of file
#version 450 core
layout (constant_id = 0) const bool FOG = true;
layout (constant_id = 1) const bool PBR = true;
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
in VertexData {
vec3 Position_modelspace;
flat uint Texture;
vec3 FaceNormal_modelspace;
vec3 FaceNormal_worldspace;
vec3 EyeDirection_cameraspace;
vec3 LightDirection_cameraspace;
float Depth;
} vs_in[];
out GeometryData {
vec3 Position_modelspace;
flat uint Textures[3];
vec3 TextureRatio;
vec3 FaceNormal_modelspace;
vec3 FaceNormal_worldspace;
vec3 EyeDirection_cameraspace;
vec3 LightDirection_cameraspace;
float Depth;
} gs;
void main() {
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = gl_in[i].gl_Position;
gs.Position_modelspace = vs_in[i].Position_modelspace;
gs.FaceNormal_modelspace = vs_in[i].FaceNormal_modelspace;
gs.Textures[i] = vs_in[i].Texture;
switch(int(mod(i,3))) {
case 0:
gs.TextureRatio = vec3(1,0,0);
break;
case 1:
gs.TextureRatio = vec3(0,1,0);
break;
case 2:
gs.TextureRatio = vec3(0,0,1);
break;
default:
gs.TextureRatio = vec3(0);
break;
};
if(PBR) {
gs.FaceNormal_worldspace = vs_in[i].FaceNormal_worldspace;
gs.EyeDirection_cameraspace = vs_in[i].EyeDirection_cameraspace;
gs.LightDirection_cameraspace = vs_in[i].LightDirection_cameraspace;
}
#ifdef SHADOW
gs.ShadowCoord = vs_in[i].ShadowCoord;
#endif
if(FOG) {
gs.Depth = vs_in[i].Depth;
}
EmitVertex();
}
EndPrimitive();
}
\ No newline at end of file
#version 450 core
layout (constant_id = 0) const bool FOG = true;
layout (constant_id = 1) const bool PBR = true;
// FS spe
layout (constant_id = 5) const bool DO_CURVATURE = false;
layout (constant_id = 6) const bool CURV_DEPTH = true;
layout(location = 0) in vec3 Position_modelspace;
layout(location = 1) in uint Texture_model;
layout(location = 2) in vec3 Normal_modelspace;
out VertexData {
vec3 Position_modelspace;
flat uint Texture;
vec3 FaceNormal_modelspace;
vec3 FaceNormal_worldspace;
vec3 EyeDirection_cameraspace;
vec3 LightDirection_cameraspace;
float Depth;
} vs;
#ifdef INSTANCED
layout(location = 6) in mat4 Model;
#else
uniform mat4 Model;
#endif
uniform mat4 View;
uniform mat4 Proj;
uniform vec4 SphereProj;
uniform float Curvature;
uniform vec3 LightInvDirection_worldspace;
uniform float FogDepth;
void main(){
vs.Position_modelspace = Position_modelspace;
if(DO_CURVATURE) {
if(Curvature > 0) {
vec3 Position_areaspace = Position_modelspace + SphereProj.xyz;
vec2 sph = vec2(acos(Position_areaspace.z / length(Position_areaspace.xyz)), atan(Position_areaspace.y, Position_areaspace.x));
if(CURV_DEPTH) {
float radius = max(max(abs(Position_areaspace.x), abs(Position_areaspace.y)), abs(Position_areaspace.z));
} else {
float radius = SphereProj.w;
}
vs.Position_modelspace = mix(vs.Position_modelspace, vec3(sin(sph.x)*cos(sph.y), sin(sph.x)*sin(sph.y), cos(sph.x)) * radius - SphereProj.xyz, Curvature);
}
}
vec4 Position_cameraspace = View * Model * vec4(vs.Position_modelspace, 1);
gl_Position = Proj * Position_cameraspace;
if(FOG) {
vs.Depth = length(Position_cameraspace.xyz) / FogDepth;
}
vs.Texture = Texture_model;
vs.FaceNormal_modelspace = normalize(Normal_modelspace);
if(PBR)
// TODO: correct normal curvature
vs.FaceNormal_worldspace = normalize((Model * vec4(vs.FaceNormal_modelspace, 0)).xyz);
// Vector that goes from the vertex to the camera, in camera space.
// In camera space, the camera is at the origin (0,0,0).
vs.EyeDirection_cameraspace = vec3(0,0,0) - Position_cameraspace.xyz;
// Vector that goes from the vertex to the light, in camera space
vs.LightDirection_cameraspace = (View * vec4(LightInvDirection_worldspace,0)).xyz;
}
}
#!/usr/bin/env bash
BASEDIR=$(dirname "$0")
TARGETDIR="$BASEDIR/../content/shaders"
# Tris
glslc -fshader-stage=vertex $BASEDIR/Tris.vs -o $TARGETDIR/Tris.vs.spv
glslc -fshader-stage=fragment $BASEDIR/Tris.fs -o $TARGETDIR/Tris.fs.spv
# Color
glslc -fshader-stage=vertex $TARGETDIR/Color.vs -o $TARGETDIR/Color.vs.spv
glslc -fshader-stage=fragment $TARGETDIR/Color.fs -o $TARGETDIR/Color.fs.spv
# Sky
glslc -fshader-stage=vertex $TARGETDIR/Sky.vs -o $TARGETDIR/Sky.vs.spv
glslc -fshader-stage=fragment $TARGETDIR/Sky.fs -o $TARGETDIR/Sky.fs.spv
# Voxel
glslc -fshader-stage=vertex $BASEDIR/Voxel.vs -o $TARGETDIR/Voxel.vs.spv
glslc -fshader-stage=fragment $BASEDIR/Voxel.fs -o $TARGETDIR/Voxel.fs.spv
glslc -fshader-stage=vertex $BASEDIR/Voxel.vs -DINSTANCED -o $TARGETDIR/Voxel.vs.ins.spv
glslc -fshader-stage=fragment $BASEDIR/Voxel.fs -DINSTANCED -o $TARGETDIR/Voxel.fs.ins.spv
glslc -fshader-stage=vertex $BASEDIR/Voxel.vs -DGEOMETRY -o $TARGETDIR/Voxel.vs.geo.spv
glslc -fshader-stage=geometry $BASEDIR/Voxel.gs -DGEOMETRY -o $TARGETDIR/Voxel.gs.geo.spv
glslc -fshader-stage=fragment $BASEDIR/Voxel.fs -DGEOMETRY -o $TARGETDIR/Voxel.fs.geo.spv
glslc -fshader-stage=vertex $BASEDIR/Voxel.vs -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.vs.geo.ins.spv
glslc -fshader-stage=geometry $BASEDIR/Voxel.gs -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.gs.geo.ins.spv
glslc -fshader-stage=fragment $BASEDIR/Voxel.fs -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.fs.geo.ins.spv
\ No newline at end of file
#include "Client.hpp"
#include "render/gl/Renderer.hpp"
#include "render/gl/UI.hpp"
#include "render/gl/Pipeline.hpp"
#include "render/index.hpp"
#include "render/UI.hpp"
#include "InputMap.hpp"
#include "world/index.hpp"
#include <glm/gtc/matrix_transform.hpp>
......@@ -13,7 +13,7 @@ Client::Client(config::client::options& options): options(options) { }
Client::~Client() { }
void Client::run(server_handle* const localHandle) {
if (!render::gl::Renderer::Load(window, {options.renderer.clear_color}))
if (!render::Load(window, options.preferVulkan, options.renderer))
return;
window.setTargetFPS(options.window.targetFPS);
......@@ -23,7 +23,7 @@ void Client::run(server_handle* const localHandle) {
Controllable player(window.getPtr(), inputs, options.control);
Camera camera(&player, options.camera);
auto *pipeline = static_cast<render::gl::Pipeline*>(render::Renderer::Get()->createPipeline(options.renderer));
auto pipeline = render::Renderer::Get();
pipeline->LightInvDir = glm::normalize(glm::vec3(-.5f, 2, -2));
render::Renderer::Get()->loadUI(window);
......@@ -119,7 +119,6 @@ void Client::run(server_handle* const localHandle) {
world->setContouring(contouring::load(options.contouring.idx, options.contouring.data));
state.contouring = world->getContouring();
}
pipeline->SkyEnable = options.renderer.skybox;
}
{ // Rendering
ZoneScopedN("Render");
......@@ -135,8 +134,7 @@ void Client::run(server_handle* const localHandle) {
{ // Chunks
const auto pass = pipeline->beginWorldPass();
const auto draw = [&](glm::mat4 model, buffer::Abstract *const buffer, const contouring::Abstract::area_info &area, const voxel_pos &pos) {
pipeline->SphereProj = glm::vec4(pos, std::get<1>(area));
pipeline->Curvature = std::get<2>(area);
pipeline->setCurvature(glm::vec4(pos, std::get<1>(area)), std::get<2>(area));
reports.models_count++;
reports.tris_count += buffer->draw(pass(model));
};
......@@ -160,10 +158,10 @@ void Client::run(server_handle* const localHandle) {
}
{ // Entities
const auto pass = pipeline->beginEntityPass();
const auto draw = [&](const std::vector<glm::mat4>& models, buffer::Abstract *const buffer) {
/*const auto draw = [&](const std::vector<glm::mat4>& models, buffer::Abstract *const buffer) {
reports.models_count += models.size();
reports.tris_count += buffer->draw(pass(models));
};
};*/
//world->getEntitiesModels(draw, frustum, offset, options.voxel_density);
}
if(state.look_at.has_value()) { // Indicator
......@@ -178,7 +176,7 @@ void Client::run(server_handle* const localHandle) {
{ // Swap buffers
ZoneScopedN("Swap");
pipeline->swapBuffer(window.getPtr());
pipeline->swapBuffer(window);
inputs.poll();
FrameMarkNamed("Client");
}
......@@ -187,7 +185,6 @@ void Client::run(server_handle* const localHandle) {
} while (!(inputs.isDown(Input::Quit) || window.shouldClose()));
render::UI::Unload();
delete pipeline;
render::Renderer::Unload();
window.destroy();
......
......@@ -42,6 +42,11 @@ bool Window::create(const CreateInfo &opt) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
break;
case CreateInfo::Client::Type::VK:
LOG_W("WIP client type");
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
break;
default:
FATAL("Unsupported client type");
break;
......@@ -105,6 +110,7 @@ void Window::waitTargetFPS() {
}
void Window::destroy() {
glfwDestroyWindow(ptr);
ptr = nullptr;
}
bool Window::shouldClose() { return glfwWindowShouldClose(ptr); }
......
#pragma once
#include "../core/flags.hpp"
#include <tuple>
typedef struct GLFWwindow GLFWwindow;
typedef void (*GLFWframebuffersizefun)(GLFWwindow*, int, int);
......
......@@ -5,7 +5,7 @@
#include <limits.h>
#include <iomanip>
#include "world/Universe.hpp"
#include "render/Pipeline.hpp"
#include "render/Renderer.hpp"
#include "contouring/index.hpp"
#include "control/Camera.hpp"
......@@ -31,6 +31,7 @@ public:
window.targetFPS = config["window"]["target_fps"].value_or(window.targetFPS);