[vulkan] Add infinite plane rendering

The grid calculations are modified from those of Inigo Quilez
(https://iquilezles.org/articles/filterableprocedurals/), but give very
nice results: when thin enough, the lines fade out nicely instead of
producing crazy moire patterns. Though currently disabled, the default
planes are the xy, yz and zx planes with colored axes.
This commit is contained in:
Bill Currie 2023-07-21 11:59:25 +09:00
parent 72f7fcea47
commit fde1ef0159
10 changed files with 450 additions and 1 deletions

View file

@ -215,6 +215,7 @@ include_qf_vulkan = \
include/QF/Vulkan/qf_output.h \
include/QF/Vulkan/qf_palette.h \
include/QF/Vulkan/qf_particles.h \
include/QF/Vulkan/qf_planes.h \
include/QF/Vulkan/qf_scene.h \
include/QF/Vulkan/qf_sprite.h \
include/QF/Vulkan/qf_texture.h \

View file

@ -0,0 +1,62 @@
/*
qf_planes.h
Vulkan debug planes rendering
Copyright (C) 2023 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2023/7/20
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifndef __QF_Vulkan_qf_planes_h
#define __QF_Vulkan_qf_planes_h
#include "QF/darray.h"
#include "QF/model.h"
#include "QF/modelgen.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/command.h"
typedef struct planes_frame_s {
VkDescriptorSet descriptor;
} planes_frame_t;
typedef struct planes_frameset_s
DARRAY_TYPE (planes_frame_t) planes_frameset_t;
typedef struct planesctx_s {
planes_frameset_t frames;
VkSampler sampler;
struct qfv_dsmanager_s *dsmanager;
struct qfv_resource_s *resources;
} planesctx_t;
struct vulkan_ctx_s;
struct entity_s;
struct mod_planes_ctx_s;
struct planes_s;
void Vulkan_Planes_Init (struct vulkan_ctx_s *ctx);
void Vulkan_Planes_Setup (struct vulkan_ctx_s *ctx);
void Vulkan_Planes_Shutdown (struct vulkan_ctx_s *ctx);
#endif//__QF_Vulkan_qf_planes_h

View file

@ -49,6 +49,7 @@ typedef struct vulkan_ctx_s {
struct scenectx_s *scene_context;
struct palettectx_s *palette_context;
struct particlectx_s *particle_context;
struct planesctx_s *planes_context;
struct spritectx_s *sprite_context;
struct drawctx_s *draw_context;
struct lightingctx_s *lighting_context;

View file

@ -247,6 +247,7 @@ libs_video_renderer_librender_vulkan_la_SOURCES = \
libs/video/renderer/vulkan/vulkan_output.c \
libs/video/renderer/vulkan/vulkan_palette.c \
libs/video/renderer/vulkan/vulkan_particles.c \
libs/video/renderer/vulkan/vulkan_planes.c \
libs/video/renderer/vulkan/vulkan_scene.c \
libs/video/renderer/vulkan/vulkan_sprite.c \
libs/video/renderer/vulkan/vulkan_texture.c \
@ -342,6 +343,7 @@ lightingf_c = $(vkshaderpath)/lighting.frag.spvc
lighting_h = $(vkshaderpath)/lighting.h
composef_src = $(vkshaderpath)/compose.frag
composef_c = $(vkshaderpath)/compose.frag.spvc
infplane = $(vkshaderpath)/infplane.finc
oit_blend = $(vkshaderpath)/oit_blend.finc
oit_store = $(vkshaderpath)/oit_store.finc
oit_h = $(vkshaderpath)/oit.h
@ -371,6 +373,8 @@ fstriangle_src = $(vkshaderpath)/fstriangle.vert
fstriangle_c = $(vkshaderpath)/fstriangle.vert.spvc
fstrianglest_src = $(vkshaderpath)/fstrianglest.vert
fstrianglest_c = $(vkshaderpath)/fstrianglest.vert.spvc
gridplane_src = $(vkshaderpath)/gridplane.frag
gridplane_c = $(vkshaderpath)/gridplane.frag.spvc
pushcolor_src = $(vkshaderpath)/pushcolor.frag
pushcolor_c = $(vkshaderpath)/pushcolor.frag.spvc
fisheye_src = $(vkshaderpath)/fisheye.frag
@ -461,6 +465,8 @@ $(fstriangle_c): $(fstriangle_src)
$(fstrianglest_c): $(fstrianglest_src)
$(gridplane_c): $(infplane) $(gridplane_src)
$(pushcolor_c): $(pushcolor_src)
$(fisheye_c): $(fisheye_src) $(matrices_h)
@ -512,6 +518,7 @@ vkshader_c = \
$(passthrough_c) \
$(fstriangle_c) \
$(fstrianglest_c) \
$(gridplane_c) \
$(pushcolor_c) \
$(shadow_c) \
$(fisheye_c) \
@ -552,6 +559,7 @@ EXTRA_DIST += \
$(rp_main_def_src) \
$(rp_main_fwd_src) \
$(smp_quake_src) \
$(infplane) \
$(oit_blend) \
$(oit_store) \
$(oit_h) \
@ -602,6 +610,7 @@ EXTRA_DIST += \
$(passthrough_src) \
$(fstriangle_src) \
$(fstrianglest_src) \
$(gridplane_src) \
$(pushcolor_src) \
$(fisheye_src) \
$(waterwarp_src)

View file

@ -53,6 +53,7 @@
#include "QF/Vulkan/qf_output.h"
#include "QF/Vulkan/qf_palette.h"
#include "QF/Vulkan/qf_particles.h"
#include "QF/Vulkan/qf_planes.h"
#include "QF/Vulkan/qf_scene.h"
#include "QF/Vulkan/qf_sprite.h"
#include "QF/Vulkan/qf_texture.h"
@ -106,6 +107,7 @@ vulkan_R_Init (void)
Vulkan_Bsp_Init (vulkan_ctx);
Vulkan_IQM_Init (vulkan_ctx);
Vulkan_Particles_Init (vulkan_ctx);
Vulkan_Planes_Init (vulkan_ctx);
Vulkan_Sprite_Init (vulkan_ctx);
Vulkan_Draw_Init (vulkan_ctx);
Vulkan_Lighting_Init (vulkan_ctx);
@ -128,6 +130,7 @@ vulkan_R_Init (void)
Vulkan_Compose_Setup (vulkan_ctx);
Vulkan_Draw_Setup (vulkan_ctx);
Vulkan_Particles_Setup (vulkan_ctx);
Vulkan_Planes_Setup (vulkan_ctx);
Vulkan_Translucent_Setup (vulkan_ctx);
Vulkan_Lighting_Setup (vulkan_ctx);
@ -550,6 +553,7 @@ vulkan_vid_render_shutdown (void)
Vulkan_Lighting_Shutdown (vulkan_ctx);
Vulkan_Draw_Shutdown (vulkan_ctx);
Vulkan_Sprite_Shutdown (vulkan_ctx);
Vulkan_Planes_Shutdown (vulkan_ctx);
Vulkan_Particles_Shutdown (vulkan_ctx);
Vulkan_IQM_Shutdown (vulkan_ctx);
Vulkan_Bsp_Shutdown (vulkan_ctx);

View file

@ -557,6 +557,18 @@ properties = {
};
};
};
gridplane = {
shader = {
fragment = {
stage = fragment;
name = main;
module = $builtin/gridplane.frag;
};
};
layout = {
descriptorSets = (matrix_set, planes_set);
};
};
lighting = {
shader = {
vertex_splat = {
@ -817,6 +829,16 @@ descriptorSetLayouts = {
},
);
};
planes_set = {
bindings = (
{
binding = 0;
descriptorType = storage_buffer;
descriptorCount = 1;
stageFlags = fragment;
},
);
};
sprite_set = {
bindings = (
{
@ -1492,7 +1514,10 @@ renderpasses = {
emission = shader_read_only_optimal;
};
color = {
output = color_attachment_optimal;
output = {
layout = color_attachment_optimal;
blend = $additive_blend;
};
};
preserve = (depth, normal, position);
};
@ -1566,6 +1591,22 @@ renderpasses = {
rasterization = $debug_poly_lines;
depthStencil = $depth_disable;
};
debug_planes = {
@inherit = $compose_base;
disabled = true;
color = $color.lights;
tasks = (
{ func = debug_planes_draw; },
);
stages = (
$fstriangle.shader.vertexst,
$gridplane.shader.fragment,
);
layout = $gridplane.layout;
depthStencil = $depth_disable;
};
};
};
};

View file

@ -127,6 +127,8 @@ static
static
#include "libs/video/renderer/vulkan/shader/fstrianglest.vert.spvc"
static
#include "libs/video/renderer/vulkan/shader/gridplane.frag.spvc"
static
#include "libs/video/renderer/vulkan/shader/pushcolor.frag.spvc"
static
#include "libs/video/renderer/vulkan/shader/fisheye.frag.spvc"
@ -184,6 +186,7 @@ static shaderdata_t builtin_shaders[] = {
{ "passthrough.vert", passthrough_vert, sizeof (passthrough_vert) },
{ "fstriangle.vert", fstriangle_vert, sizeof (fstriangle_vert) },
{ "fstrianglest.vert", fstrianglest_vert, sizeof (fstrianglest_vert) },
{ "gridplane.frag", gridplane_frag, sizeof (gridplane_frag) },
{ "pushcolor.frag", pushcolor_frag, sizeof (pushcolor_frag) },
{ "fisheye.frag", fisheye_frag, sizeof (fisheye_frag) },
{ "waterwarp.frag", waterwarp_frag, sizeof (waterwarp_frag) },

View file

@ -0,0 +1,70 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_multiview : enable
#include "infplane.finc"
layout (set = 0, binding = 0) uniform
#include "matrices.h"
;
struct PlaneData {
mat3x4 p;
vec4 scolor;
vec4 tcolor;
};
layout (set = 1, binding = 0) buffer Planes {
int numPlanes;
PlaneData planes[];
};
layout (location = 0) in vec2 uv;
layout (location = 0) out vec4 frag_color;
const float N = 128.0; // grid ratio
vec3 gridTextureGradBox( in vec2 p, in vec2 ddx, in vec2 ddy )
{
// filter kernel
vec2 w = max(abs(ddx), abs(ddy)) + 0.01;
// analytic (box) filtering
vec2 a = p + 0.5*w + vec2(0.5/N);
vec2 b = p - 0.5*w + vec2(0.5/N);
vec2 i = (floor(a)+min(fract(a)*N,1.0)-
floor(b)-min(fract(b)*N,1.0))/(N*w);
//pattern
return vec3(1 - (1.0-i.x)*(1.0-i.y), i.x, i.y);
}
void
main (void)
{
mat4 p = inverse (View[gl_ViewIndex]);
mat3 cam = mat3(vec3(p[0]) / Projection3d[0][0],
vec3(p[1]) / Projection3d[1][1],
vec3(p[2]));
vec4 c = vec4(0);
vec2 UV = 2 * uv - vec2(1,1);
for (int i = 0; i < numPlanes; i++) {
vec4 pst = plane_st (mat3(planes[i].p), cam, vec3(UV, 1));
vec2 st = pst.xy;
vec2 ddx_st = dFdx (st);
vec2 ddy_st = dFdy (st);
bool d = pst.w * planes[i].p[0][3] > 0;
if (!d) continue;
vec3 g = gridTextureGradBox(st, ddx_st, ddy_st);
float c1 = abs(st.y) < 0.5 ? g[2] : 0;
float c2 = abs(st.x) < 0.5 ? g[1] : 0;
float c0 = g[0] * (1 - c1) * (1 - c2);
c += vec4(1) * c0 + planes[i].scolor * c1 + planes[i].tcolor * c2;
//st = fract(st);
//st = st - st * st;
//float s = st.x * st.y;
//s = 20 * (0.05 - s);
//s = max (s, 0);
//c += pst.w * planes[i][0][3] > 0 ? s * vec4(1,1,1,0.4): vec4(0);
}
frag_color = c;
}

View file

@ -0,0 +1,5 @@
vec4 plane_st (mat3 plane, mat3 cam, vec3 vec)
{
vec3 i = plane * (cam * vec);
return vec4 (i/i.z, i.z);
}

View file

@ -0,0 +1,253 @@
/*
vulkan_planes.c
Vulkan debug planes pipeline
Copyright (C) 2023 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2023/7/20
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "QF/simd/types.h"
#include "QF/va.h"
#include "QF/Vulkan/qf_matrices.h"
#include "QF/Vulkan/qf_planes.h"
#include "QF/Vulkan/barrier.h"
#include "QF/Vulkan/debug.h"
#include "QF/Vulkan/descriptor.h"
#include "QF/Vulkan/device.h"
#include "QF/Vulkan/dsmanager.h"
#include "QF/Vulkan/instance.h"
#include "QF/Vulkan/resource.h"
#include "QF/Vulkan/render.h"
#include "QF/Vulkan/staging.h"
#include "r_internal.h"
#include "vid_vulkan.h"
typedef struct {
vec4f_t p[3];
vec4f_t scolor;
vec4f_t tcolor;
} qfv_plane_t;
typedef struct {
int num_planes;
qfv_plane_t planes[64];
} qfv_planebuf_t;
static qfv_plane_t
make_plane (vec4f_t s, vec4f_t t, vec4f_t scolor, vec4f_t tcolor)
{
vec4f_t n = crossf (s, t);
vec4f_t d = -r_refdef.camera[3];
vec4f_t S = dotf (t, t) * s - dotf (s, t) * t;
vec4f_t T = dotf (s, s) * t - dotf (s, t) * s;
vec4f_t dn = dotf (d, n);
vec4f_t Sd = dotf (S, d);
vec4f_t Td = dotf (T, d);
vec4f_t SS = dn * S - Sd * n;
vec4f_t TT = dn * T - Td * n;
vec4f_t nn = dotf (n, n) * n;
return (qfv_plane_t) {
.p = {
{SS[0], TT[0], nn[0], dn[0]},
{SS[1], TT[1], nn[1], 0},
{SS[2], TT[2], nn[2], 0},
},
.scolor = scolor,
.tcolor = tcolor,
};
}
static void
debug_planes_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
{
auto taskctx = (qfv_taskctx_t *) ectx;
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto dfunc = device->funcs;
auto pctx = ctx->planes_context;
auto pframe = &pctx->frames.a[ctx->curFrame];
auto layout = taskctx->pipeline->layout;
auto cmd = taskctx->cmd;
auto packet = QFV_PacketAcquire (ctx->staging);
qfv_planebuf_t *planes = QFV_PacketExtend (packet, sizeof (qfv_planebuf_t));
vec4f_t x = {256, 0, 0, 0};
vec4f_t y = {0, 256, 0, 0};
vec4f_t z = {0, 0, 256, 0};
vec4f_t r = {1, 0, 0, 1 };
vec4f_t g = {0, 1, 0, 1 };
vec4f_t b = {0, 0, 1, 1 };
*planes = (qfv_planebuf_t) {
.num_planes = 3,
.planes[0] = make_plane (x, y, r, g),
.planes[1] = make_plane (y, z, g, b),
.planes[2] = make_plane (z, x, b, r),
};
auto buffer = &pctx->resources->objects[ctx->curFrame].buffer;
auto bb = &bufferBarriers[qfv_BB_TransferWrite_to_UniformRead];
QFV_PacketCopyBuffer (packet, buffer->buffer, 0, bb);
QFV_PacketSubmit (packet);
VkDescriptorSet sets[] = {
Vulkan_Matrix_Descriptors (ctx, ctx->curFrame),
pframe->descriptor,
};
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
layout, 0, 2, sets, 0, 0);
dfunc->vkCmdDraw (cmd, 3, 1, 0, 0);
}
static exprtype_t *debug_planes_draw_params[] = {
&cexpr_int,
};
static exprfunc_t debug_planes_draw_func[] = {
{ .func = debug_planes_draw, .num_params = 1, .param_types = debug_planes_draw_params },
{}
};
static exprsym_t debug_planes_task_syms[] = {
{ "debug_planes_draw", &cexpr_function, debug_planes_draw_func },
{}
};
void
Vulkan_Planes_Init (vulkan_ctx_t *ctx)
{
qfvPushDebug (ctx, "debug planes init");
QFV_Render_AddTasks (ctx, debug_planes_task_syms);
planesctx_t *pctx = calloc (1, sizeof (planesctx_t));
ctx->planes_context = pctx;
qfvPopDebug (ctx);
}
void
Vulkan_Planes_Setup (vulkan_ctx_t *ctx)
{
auto device = ctx->device;
auto dfunc = device->funcs;
auto pctx = ctx->planes_context;
auto rctx = ctx->render_context;
size_t frames = rctx->frames.size;
DARRAY_INIT (&pctx->frames, frames);
DARRAY_RESIZE (&pctx->frames, frames);
pctx->frames.grow = 0;
pctx->resources = malloc (sizeof (qfv_resource_t)
+ sizeof (qfv_resobj_t[frames]));
*pctx->resources = (qfv_resource_t) {
.name = "planes",
.va_ctx = ctx->va_ctx,
.memory_properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
.num_objects = frames,
.objects = (qfv_resobj_t *) &pctx->resources[1],
};
for (size_t i = 0; i < frames; i++) {
auto obj = &pctx->resources->objects[i];
*obj = (qfv_resobj_t) {
.name = va (ctx->va_ctx, "planes:%zd", i),
.type = qfv_res_buffer,
.buffer = {
.size = sizeof (qfv_planebuf_t),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
},
};
}
QFV_CreateResource (device, pctx->resources);
auto packet = QFV_PacketAcquire (ctx->staging);
qfv_planebuf_t *planes = QFV_PacketExtend (packet, sizeof (qfv_planebuf_t));
*planes = (qfv_planebuf_t) {
.num_planes = 3,
.planes[0] = {{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
}},
.planes[1] = {{
{0, 1, 0, 0},
{0, 0, 1, 0},
{1, 0, 0, 0},
}},
.planes[2] = {{
{0, 0, 1, 0},
{1, 0, 0, 0},
{0, 1, 0, 0},
}},
};
for (size_t i = 0; i < frames; i++) {
auto buffer = &pctx->resources->objects[i].buffer;
auto bb = &bufferBarriers[qfv_BB_TransferWrite_to_UniformRead];
QFV_PacketCopyBuffer (packet, buffer->buffer, 0, bb);
}
QFV_PacketSubmit (packet);
auto dsmanager = QFV_Render_DSManager (ctx, "planes_set");
for (size_t i = 0; i < frames; i++) {
auto pframe = &pctx->frames.a[i];
auto buffer = &pctx->resources->objects[i].buffer;
auto set = QFV_DSManager_AllocSet (dsmanager);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET, set,
va (ctx->va_ctx, "debug:planes_set:%zd", i));
pframe->descriptor = set;
VkDescriptorBufferInfo bufferInfo[] = {
{ buffer->buffer, 0, VK_WHOLE_SIZE },
};
VkWriteDescriptorSet write[] = {
{ VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = set,
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.pBufferInfo = bufferInfo,
},
};
dfunc->vkUpdateDescriptorSets (device->dev, 1, write, 0, 0);
}
}
void
Vulkan_Planes_Shutdown (vulkan_ctx_t *ctx)
{
auto device = ctx->device;
auto pctx = ctx->planes_context;
QFV_DestroyResource (device, pctx->resources);
free (pctx->frames.a);
free (pctx);
}