[vulkan] Rework lighting to use splats

It's currently slower, and the cone splats are buggy, but the lighting
code itself got some nice cleanups.
This commit is contained in:
Bill Currie 2023-06-28 01:01:56 +09:00
parent 03cfd2530b
commit 85128a3e86
13 changed files with 593 additions and 226 deletions

View file

@ -36,7 +36,7 @@
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/command.h"
#define COMPOSE_IMAGE_INFOS 1
#define COMPOSE_IMAGE_INFOS 3
typedef struct composeframe_s {
VkDescriptorImageInfo imageInfo[COMPOSE_IMAGE_INFOS];

View file

@ -61,7 +61,12 @@ typedef struct qfv_light_buffer_s {
#define LIGHTING_DESCRIPTORS (LIGHTING_BUFFER_INFOS + LIGHTING_ATTACH_INFOS + 1)
typedef struct lightingframe_s {
VkBuffer light_buffer;
VkBuffer data_buffer;
VkBuffer id_buffer;
uint32_t ico_count;
uint32_t cone_count;
uint32_t flat_count;
VkDescriptorBufferInfo bufferInfo[LIGHTING_BUFFER_INFOS];
VkDescriptorImageInfo attachInfo[LIGHTING_ATTACH_INFOS];
VkDescriptorImageInfo shadowInfo[LIGHTING_SHADOW_INFOS];
@ -94,18 +99,22 @@ typedef struct light_renderer_set_s
typedef struct lightingctx_s {
lightingframeset_t frames;
VkPipeline pipeline;
VkSampler sampler;
VkDeviceMemory light_memory;
struct qfv_resource_s *shadow_resources;
struct qfv_resource_s *light_resources;
qfv_lightmatset_t light_mats;
qfv_imageset_t light_images;
light_renderer_set_t light_renderers;
VkRenderPass renderpass_6;
VkRenderPass renderpass_4;
VkRenderPass renderpass_1;
VkBuffer splat_verts;
VkBuffer splat_inds;
struct lightingdata_s *ldata;
struct scene_s *scene;
} lightingctx_t;

View file

@ -64,7 +64,7 @@ enum {
QFV_attachEmission,
QFV_attachNormal,
QFV_attachPosition,
QFV_attachOpaque,
QFV_attachLight,
QFV_attachSwapchain,
};

View file

@ -320,8 +320,13 @@ bsp_skyf_src = $(vkshaderpath)/bsp_sky.frag
bsp_skyf_c = $(vkshaderpath)/bsp_sky.frag.spvc
bsp_turbf_src = $(vkshaderpath)/bsp_turb.frag
bsp_turbf_c = $(vkshaderpath)/bsp_turb.frag.spvc
light_flat_src = $(vkshaderpath)/light_flat.vert
light_flat_c = $(vkshaderpath)/light_flat.vert.spvc
light_splat_src = $(vkshaderpath)/light_splat.vert
light_splat_c = $(vkshaderpath)/light_splat.vert.spvc
lightingf_src = $(vkshaderpath)/lighting.frag
lightingf_c = $(vkshaderpath)/lighting.frag.spvc
lighting_h = $(vkshaderpath)/lighting.h
composef_src = $(vkshaderpath)/compose.frag
composef_c = $(vkshaderpath)/compose.frag.spvc
oit_blend = $(vkshaderpath)/oit_blend.finc
@ -401,7 +406,11 @@ $(bsp_skyf_c): $(bsp_skyf_src) $(oit_store) $(oit_h)
$(bsp_turbf_c): $(bsp_turbf_src) $(oit_store) $(oit_h)
$(lightingf_c): $(lightingf_src)
$(light_flat_c): $(light_flat_src) $(lighting_h)
$(light_splat_c): $(light_splat_src) $(lighting_h)
$(lightingf_c): $(lightingf_src) $(lighting_h)
$(composef_c): $(composef_src) $(oit_blend) $(oit_h)
@ -458,6 +467,8 @@ vkshader_c = \
$(bsp_shadow_c) \
$(bsp_skyf_c) \
$(bsp_turbf_c) \
$(light_flat_c) \
$(light_splat_c) \
$(lightingf_c) \
$(composef_c) \
$(aliasv_c) \
@ -539,7 +550,10 @@ EXTRA_DIST += \
$(bsp_shadow_src) \
$(bsp_skyf_src) \
$(bsp_turbf_src) \
$(light_flat_src) \
$(light_splat_src) \
$(lightingf_src) \
$(lighting_h) \
$(composef_src) \
$(aliasv_src) \
$(aliasf_src) \

View file

@ -91,6 +91,15 @@ properties = {
clearValue = { color = "[0, 0, 0, 1]"; };
};
no_cull = {
depthClampEnable = false;
rasterizerDiscardEnable = false;
polygonMode = fill;
cullMode = none;
frontFace = counter_clockwise;
depthBiasEnable = false;
lineWidth = 1;
};
cw_cull_back = {
depthClampEnable = false;
rasterizerDiscardEnable = false;
@ -123,6 +132,13 @@ properties = {
depthBoundsTestEnable = false;
stencilTestEnable = false;
};
depth_test_only_reverse = {
depthTestEnable = true;
depthWriteEnable = false;
depthCompareOp = greater;
depthBoundsTestEnable = false;
stencilTestEnable = false;
};
depth_disable = {
depthTestEnable = false;
depthWriteEnable = false;
@ -150,6 +166,16 @@ properties = {
alphaBlendOp = add;
colorWriteMask = r|g|b|a;
};
additive_blend = {
blendEnable = true;
srcColorBlendFactor = one;
dstColorBlendFactor = one;
colorBlendOp = add;
srcAlphaBlendFactor = zero;
dstAlphaBlendFactor = one;
alphaBlendOp = add;
colorWriteMask = r|g|b|a;
};
pipeline_base = {
viewport = {
viewports = (
@ -519,15 +545,47 @@ properties = {
};
lighting = {
shader = {
vertex_splat = {
stage = vertex;
name = main;
module = $builtin/light_splat.vert;
};
vertex_flat = {
stage = vertex;
name = main;
module = $builtin/light_flat.vert;
};
fragment = {
stage = fragment;
name = main;
module = $builtin/lighting.frag;
};
};
vertexInput_splat = {
bindings = (
{ binding = 0; stride = "4"; inputRate = instance; },
{ binding = 1; stride = "3 * 4"; inputRate = vertex; },
);
attributes = (
{ location = 0; binding = 0; format = r32_uint; offset = 0; },
{ location = 1; binding = 1; format = r32g32b32_sfloat; offset = 0; },
);
};
vertexInput_flat = {
bindings = (
{ binding = 0; stride = "4"; inputRate = instance; },
);
attributes = (
{ location = 0; binding = 0; format = r32_uint; offset = 0; },
);
};
inputAssembly = {
topology = triangle_fan;
primitiveRestartEnable = true;
};
layout = {
descriptorSets = (lighting_attach, lighting_lights,
lighting_shadow);
descriptorSets = (matrix_set, lighting_lights,
lighting_attach, lighting_shadow);
};
};
compose = {
@ -791,7 +849,7 @@ descriptorSetLayouts = {
binding = 0;
descriptorType = uniform_buffer;
descriptorCount = 1;
stageFlags = fragment;
stageFlags = vertex|fragment;
},
);
};
@ -813,6 +871,18 @@ descriptorSetLayouts = {
descriptorCount = 1;
stageFlags = fragment;
},
{
binding = 1;
descriptorType = input_attachment;
descriptorCount = 1;
stageFlags = fragment;
},
{
binding = 2;
descriptorType = input_attachment;
descriptorCount = 1;
stageFlags = fragment;
},
);
};
particle_set = {
@ -892,7 +962,7 @@ images = {
@inherit = $image_base;
format = r32g32b32a32_sfloat;
};
opaque = {
light = {
@inherit = $image_base;
format = r16g16b16a16_sfloat;
};
@ -922,7 +992,7 @@ images = {
@inherit = $cube_image_base;
format = r32g32b32a32_sfloat;
};
cube_opaque = {
cube_light = {
@inherit = $cube_image_base;
format = r16g16b16a16_sfloat;
};
@ -962,10 +1032,10 @@ imageviews = {
image = position;
format = $images.position.format;
};
opaque = {
light = {
@inherit = $view_base;
image = opaque;
format = $images.opaque.format;
image = light;
format = $images.light.format;
};
output = {
@inherit = $view_base;
@ -1000,10 +1070,10 @@ imageviews = {
image = cube_position;
format = $images.cube_position.format;
};
cube_opaque = {
cube_light = {
@inherit = $cube_view_base;
image = cube_opaque;
format = $images.cube_opaque.format;
image = cube_light;
format = $images.cube_light.format;
};
cube_output = {
@inherit = $cube_view_base;
@ -1055,10 +1125,11 @@ renderpasses = {
format = $images.position.format;
view = position;
};
opaque = {
light = {
@inherit = $attachment_base;
format = $images.opaque.format;
view = opaque;
format = $images.light.format;
loadOp = clear;
view = light;
};
output = {
@inherit = $attachment_base;
@ -1361,26 +1432,46 @@ renderpasses = {
position = shader_read_only_optimal;
};
color = {
opaque = {
light = {
layout = color_attachment_optimal;
blend = $blend_disable;
blend = $additive_blend;
};
};
preserve = (output);
};
pipelines = {
lights = {
light_splats = {
@inherit = $compose_base;
color = $color.lights;
tasks = (
{ func = lights_draw; },
{ func = lighting_bind_descriptors; },
{ func = lighting_draw_splats; },
);
stages = (
$fstriangle.shader.vertex,
$lighting.shader.vertex_splat,
$lighting.shader.fragment,
);
vertexInput = $lighting.vertexInput_splat;
inputAssembly = $lighting.inputAssembly;
layout = $lighting.layout;
rasterization = $cw_cull_back;
//depthStencil = $depth_disable;
};
light_flat = {
@inherit = $compose_base;
color = $color.lights;
tasks = (
{ func = lighting_draw_flats; },
);
stages = (
$lighting.shader.vertex_flat,
$lighting.shader.fragment,
);
vertexInput = $lighting.vertexInput_flat;
layout = $lighting.layout;
};
};
@ -1392,12 +1483,14 @@ renderpasses = {
};
attachments = {
input = {
opaque = shader_read_only_optimal;
color = shader_read_only_optimal;
light = shader_read_only_optimal;
emission = shader_read_only_optimal;
};
color = {
output = color_attachment_optimal;
};
preserve = (depth, color, emission, normal, position);
preserve = (depth, normal, position);
};
pipelines = {
compose = {
@ -1459,10 +1552,10 @@ renderpasses = {
format = $images.cube_position.format;
view = cube_position;
};
opaque = {
light = {
@inherit = $attachment_base;
format = $images.cube_opaque.format;
view = cube_opaque;
format = $images.cube_light.format;
view = cube_light;
};
output = {
@inherit = $attachment_base;
@ -1628,6 +1721,7 @@ steps = {
{ func = bsp_visit_world;
params = (main); },
{ func = scene_draw_viewmodel; },
{ func = lighting_update_lights; },
);
};
};

View file

@ -87,6 +87,10 @@ static
static
#include "libs/video/renderer/vulkan/shader/bsp_turb.frag.spvc"
static
#include "libs/video/renderer/vulkan/shader/light_flat.vert.spvc"
static
#include "libs/video/renderer/vulkan/shader/light_splat.vert.spvc"
static
#include "libs/video/renderer/vulkan/shader/lighting.frag.spvc"
static
#include "libs/video/renderer/vulkan/shader/compose.frag.spvc"
@ -150,6 +154,8 @@ static shaderdata_t builtin_shaders[] = {
{ "bsp_shadow.vert", bsp_shadow_vert, sizeof (bsp_shadow_vert) },
{ "bsp_sky.frag", bsp_sky_frag, sizeof (bsp_sky_frag) },
{ "bsp_turb.frag", bsp_turb_frag, sizeof (bsp_turb_frag) },
{ "light_flat.vert", light_flat_vert, sizeof (light_flat_vert) },
{ "light_splat.vert", light_splat_vert, sizeof (light_splat_vert) },
{ "lighting.frag", lighting_frag, sizeof (lighting_frag) },
{ "compose.frag", compose_frag, sizeof (compose_frag) },
{ "alias.vert", alias_vert, sizeof (alias_vert) },

View file

@ -5,18 +5,24 @@
#define OIT_SET 1
#include "oit_blend.finc"
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput opaque;
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput color;
layout (input_attachment_index = 1, set = 0, binding = 1) uniform subpassInput light;
layout (input_attachment_index = 2, set = 0, binding = 2) uniform subpassInput emission;
layout (location = 0) out vec4 frag_color;
void
main (void)
{
vec3 o;
vec3 c;
vec3 l;
vec3 e;
vec3 o;
o = subpassLoad (opaque).rgb;
c = BlendFrags (vec4 (o, 1)).xyz;
c = pow (c, vec3(0.83));//FIXME make gamma correction configurable
frag_color = vec4 (c, 1);
c = subpassLoad (color).rgb;
l = subpassLoad (light).rgb;
e = subpassLoad (emission).rgb;
o = BlendFrags (vec4 (c * l + e, 1)).xyz;
o = pow (o, vec3(0.83));//FIXME make gamma correction configurable
frag_color = vec4 (o, 1);
}

View file

@ -0,0 +1,13 @@
#version 450
layout (location = 0) in uint light_index;
layout (location = 0) out uint light_index_out;
void
main ()
{
float x = (gl_VertexIndex & 2);
float y = (gl_VertexIndex & 1);
gl_Position = vec4 (2, 4, 0, 1) * vec4 (x, y, 0, 1) - vec4 (1, 1, 0, 0);
light_index_out = light_index;
}

View file

@ -0,0 +1,46 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_multiview : enable
layout (set = 0, binding = 0) uniform
#include "matrices.h"
;
#include "lighting.h"
layout (location = 0) in uint light_index;
layout (location = 1) in vec3 splat_vert;
layout (location = 0) out uint light_index_out;
vec4 // assumes a and b are unit vectors
from_to_rotation (vec3 a, vec3 b)
{
float d = dot (a + b, a + b);
float qc = sqrt (d);
vec3 qv = d > 1e-6 ? cross (a, b) / qc : vec3 (1, 0, 0);
return vec4 (qv, qc * 0.5);
}
vec3
quat_mul (vec4 q, vec3 v)
{
vec3 uv = cross (q.xyz, v);
vec3 uuv = cross (q.xyz, uv);
return v + ((uv * q.w) + uuv) * 2;
}
void
main (void)
{
LightData l = lights[light_index];
float sz = 1 / l.attenuation.w;
float c = l.direction.w;
float sxy = sz * (c < 0 ? sqrt (c*c * (1 - c*c)) / (0.5 - c) : 1);
vec3 scale = vec3 (sxy, sxy, sz);
vec4 q = from_to_rotation (vec3 (0, 0, -1), l.direction.xyz);
vec4 pos = vec4 (quat_mul (q, splat_vert * scale), 0);
gl_Position = Projection3d * (View[gl_ViewIndex] * (pos + l.position));
light_index_out = light_index;
}

View file

@ -1,54 +1,26 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
layout (input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput depth;
layout (input_attachment_index = 1, set = 0, binding = 1) uniform subpassInput color;
layout (input_attachment_index = 2, set = 0, binding = 2) uniform subpassInput emission;
layout (input_attachment_index = 3, set = 0, binding = 3) uniform subpassInput normal;
layout (input_attachment_index = 4, set = 0, binding = 4) uniform subpassInput position;
#include "lighting.h"
struct LightData {
vec4 color; // .a is intensity
vec4 position; // .w = 0 -> directional, .w = 1 -> point/cone
vec4 direction; // .w = -cos(cone_angle/2) (1 for omni/dir)
vec4 attenuation;
};
layout (input_attachment_index = 0, set = 2, binding = 0) uniform subpassInput depth;
layout (input_attachment_index = 1, set = 2, binding = 1) uniform subpassInput color;
layout (input_attachment_index = 2, set = 2, binding = 2) uniform subpassInput emission;
layout (input_attachment_index = 3, set = 2, binding = 3) uniform subpassInput normal;
layout (input_attachment_index = 4, set = 2, binding = 4) uniform subpassInput position;
#define StyleMask 0x07f
#define ModelMask 0x380
#define ShadowMask 0xc00
#define LM_LINEAR (0 << 7) // light - dist (or radius + dist if -ve)
#define LM_INVERSE (1 << 7) // distFactor1 * light / dist
#define LM_INVERSE2 (2 << 7) // distFactor2 * light / (dist * dist)
#define LM_INFINITE (3 << 7) // light
#define LM_AMBIENT (4 << 7) // light
#define LM_INVERSE3 (5 << 7) // distFactor2 * light / (dist + distFactor2)**2
#define ST_NONE (0 << 10) // no shadows
#define ST_PLANE (1 << 10) // single plane shadow map (small spotlight)
#define ST_CASCADE (2 << 10) // cascaded shadow maps
#define ST_CUBE (3 << 10) // cubemap (omni, large spotlight)
layout (constant_id = 0) const int MaxLights = 768;
layout (set = 2, binding = 0) uniform sampler2DArrayShadow shadowCascade[MaxLights];
layout (set = 2, binding = 0) uniform sampler2DShadow shadowPlane[MaxLights];
layout (set = 2, binding = 0) uniform samplerCubeShadow shadowCube[MaxLights];
layout (set = 1, binding = 0) uniform Lights {
LightData lights[MaxLights];
int lightCount;
//mat4 shadowMat[MaxLights];
//vec4 shadowCascale[MaxLights];
};
layout (set = 3, binding = 0) uniform sampler2DArrayShadow shadowCascade[MaxLights];
layout (set = 3, binding = 0) uniform sampler2DShadow shadowPlane[MaxLights];
layout (set = 3, binding = 0) uniform samplerCubeShadow shadowCube[MaxLights];
layout (location = 0) flat in uint light_index;
layout (location = 0) out vec4 frag_color;
float
spot_cone (LightData light, vec3 incoming)
spot_cone (vec4 light_direction, vec3 incoming)
{
vec3 dir = light.direction.xyz;
float cone = light.direction.w;
vec3 dir = light_direction.xyz;
float cone = light_direction.w;
float spotdot = dot (incoming, dir);
return 1 - smoothstep (cone, .995 * cone + 0.005, spotdot);
}
@ -82,41 +54,25 @@ void
main (void)
{
//float d = subpassLoad (depth).r;
vec3 c = subpassLoad (color).rgb;
vec3 e = subpassLoad (emission).rgb;
//vec3 c = subpassLoad (color).rgb;
//vec3 e = subpassLoad (emission).rgb;
vec3 n = subpassLoad (normal).rgb;
vec3 p = subpassLoad (position).rgb;
vec3 light = vec3 (0);
//vec3 minLight = vec3 (0);
for (int i = 0; i < lightCount; i++) {
LightData l = lights[i];
vec3 dir = l.position.xyz - l.position.w * p;
float r2 = dot (dir, dir);
vec4 a = l.attenuation;
LightData l = lights[light_index];
vec3 dir = l.position.xyz - l.position.w * p;
float r2 = dot (dir, dir);
vec4 a = l.attenuation;
if (l.position.w * a.w * a.w * r2 >= 1) {
continue;
}
vec4 r = vec4 (r2, sqrt(r2), 1, 0);
vec3 incoming = dir / r.y;
float I = (1 - a.w * r.y) / dot (a, r);
vec4 r = vec4 (r2, sqrt(r2), 1, 0);
vec3 incoming = dir / r.y;
float I = (1 - a.w * r.y) / dot (a, r);
/*int shadow = lights[i].data & ShadowMask;
if (shadow == ST_CASCADE) {
I *= shadow_cascade (shadowCascade[i]);
} else if (shadow == ST_PLANE) {
I *= shadow_plane (shadowPlane[i]);
} else if (shadow == ST_CUBE) {
I *= shadow_cube (shadowCube[i]);
}*/
float namb = dot(l.direction.xyz, l.direction.xyz);
I *= spot_cone (l.direction, incoming) * diffuse (incoming, n);
I = clamp (mix (1, I, namb), 0, 1);
light += I * l.color.w * l.color.xyz;
float namb = dot(l.direction.xyz, l.direction.xyz);
I *= spot_cone (l, incoming) * diffuse (incoming, n);
I = mix (1, I, namb);
light += I * l.color.w * l.color.xyz;
}
//light = max (light, minLight);
frag_color = vec4 (c * light + e, 1);
frag_color = vec4 (light, 1);
}

View file

@ -0,0 +1,15 @@
struct LightData {
vec4 color; // .a is intensity
vec4 position; // .w = 0 -> directional, .w = 1 -> point/cone
vec4 direction; // .w = -cos(cone_angle/2) (1 for omni/dir)
vec4 attenuation;
};
layout (constant_id = 0) const int MaxLights = 768;
layout (set = 1, binding = 0) uniform Lights {
LightData lights[MaxLights];
int lightCount;
//mat4 shadowMat[MaxLights];
//vec4 shadowCascale[MaxLights];
};

View file

@ -43,6 +43,7 @@
#include "QF/cvar.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "QF/Vulkan/qf_compose.h"
#include "QF/Vulkan/qf_translucent.h"
@ -81,7 +82,9 @@ compose_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
auto cmd = taskctx->cmd;
auto fb = &taskctx->renderpass->framebuffer;
cframe->imageInfo[0].imageView = fb->views[QFV_attachOpaque];
cframe->imageInfo[0].imageView = fb->views[QFV_attachColor];
cframe->imageInfo[1].imageView = fb->views[QFV_attachLight];
cframe->imageInfo[2].imageView = fb->views[QFV_attachEmission];
dfunc->vkUpdateDescriptorSets (device->dev, COMPOSE_IMAGE_INFOS,
cframe->descriptors, 0, 0);
@ -118,6 +121,7 @@ Vulkan_Compose_Setup (vulkan_ctx_t *ctx)
{
qfvPushDebug (ctx, "compose init");
auto device = ctx->device;
auto cctx = ctx->compose_context;
auto rctx = ctx->render_context;
@ -128,13 +132,16 @@ Vulkan_Compose_Setup (vulkan_ctx_t *ctx)
auto dsmanager = QFV_Render_DSManager (ctx, "compose_attach");
for (size_t i = 0; i < frames; i++) {
__auto_type cframe = &cctx->frames.a[i];
auto cframe = &cctx->frames.a[i];
auto set = QFV_DSManager_AllocSet (dsmanager);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET, set,
va (ctx->va_ctx, "compose:attach_set:%zd", i));
for (int j = 0; j < COMPOSE_IMAGE_INFOS; j++) {
cframe->imageInfo[j] = base_image_info;
cframe->imageInfo[j].sampler = 0;
cframe->descriptors[j] = base_image_write;
cframe->descriptors[j].dstSet = QFV_DSManager_AllocSet (dsmanager);
cframe->descriptors[j].dstSet = set;
cframe->descriptors[j].dstBinding = j;
cframe->descriptors[j].pImageInfo = &cframe->imageInfo[j];
}

View file

@ -56,6 +56,7 @@
#include "QF/Vulkan/qf_draw.h"
#include "QF/Vulkan/qf_lighting.h"
#include "QF/Vulkan/qf_matrices.h"
#include "QF/Vulkan/qf_texture.h"
#include "QF/Vulkan/barrier.h"
#include "QF/Vulkan/buffer.h"
@ -76,76 +77,22 @@
#include "vid_vulkan.h"
#include "vkparse.h"
static void
update_lights (vulkan_ctx_t *ctx)
{
qfv_device_t *device = ctx->device;
qfv_devfuncs_t *dfunc = device->funcs;
lightingctx_t *lctx = ctx->lighting_context;
lightingdata_t *ldata = lctx->ldata;
lightingframe_t *lframe = &lctx->frames.a[ctx->curFrame];
Light_FindVisibleLights (ldata);
dlight_t *lights[MaxLights];
qfv_packet_t *packet = QFV_PacketAcquire (ctx->staging);
qfv_light_buffer_t *light_data = QFV_PacketExtend (packet,
sizeof (*light_data));
float style_intensities[NumStyles];
for (int i = 0; i < NumStyles; i++) {
style_intensities[i] = d_lightstylevalue[i] / 65536.0;
}
light_data->lightCount = 0;
R_FindNearLights (r_refdef.frame.position, MaxLights - 1, lights);
for (int i = 0; i < MaxLights - 1; i++) {
if (!lights[i]) {
break;
}
light_data->lightCount++;
VectorCopy (lights[i]->color, light_data->lights[i].color);
// dynamic lights seem a tad faint, so 16x map lights
light_data->lights[i].color[3] = lights[i]->radius / 16;
VectorCopy (lights[i]->origin, light_data->lights[i].position);
// dlights are local point sources
light_data->lights[i].position[3] = 1;
light_data->lights[i].attenuation =
(vec4f_t) { 0, 0, 1, 1/lights[i]->radius };
// full sphere, normal light (not ambient)
light_data->lights[i].direction = (vec4f_t) { 0, 0, 1, 1 };
}
for (size_t i = 0; (i < ldata->lightvis.size
&& light_data->lightCount < MaxLights); i++) {
if (ldata->lightvis.a[i]) {
light_t *light = &light_data->lights[light_data->lightCount++];
*light = ldata->lights.a[i];
light->color[3] *= style_intensities[ldata->lightstyles.a[i]];
}
}
if (developer & SYS_lighting) {
Vulkan_Draw_String (vid.width - 32, 8,
va (ctx->va_ctx, "%3d", light_data->lightCount),
ctx);
}
qfv_bufferbarrier_t bb = bufferBarriers[qfv_BB_Unknown_to_TransferWrite];
bb.barrier.buffer = lframe->light_buffer;
bb.barrier.size = sizeof (qfv_light_buffer_t);
dfunc->vkCmdPipelineBarrier (packet->cmd, bb.srcStages, bb.dstStages,
0, 0, 0, 1, &bb.barrier, 0, 0);
VkBufferCopy copy_region[] = {
{ packet->offset, 0, sizeof (qfv_light_buffer_t) },
};
dfunc->vkCmdCopyBuffer (packet->cmd, ctx->staging->buffer,
lframe->light_buffer, 1, &copy_region[0]);
bb = bufferBarriers[qfv_BB_TransferWrite_to_UniformRead];
bb.barrier.buffer = lframe->light_buffer;
bb.barrier.size = sizeof (qfv_light_buffer_t);
dfunc->vkCmdPipelineBarrier (packet->cmd, bb.srcStages, bb.dstStages,
0, 0, 0, 1, &bb.barrier, 0, 0);
QFV_PacketSubmit (packet);
}
#define ico_verts 12
#define cone_verts 7
static int ico_inds[] = {
0, 4, 6, 9, 2, 8, 4, -1,
3, 1, 10, 5, 7, 11, 1, -1,
1, 11, 6, 4, 10, -1,
9, 6, 11, 7, 2, -1,
5, 10, 8, 2, 7, -1,
4, 8, 10,
};
#define num_ico_inds (sizeof (ico_inds) / sizeof (ico_inds[0]))
static int cone_inds[] = {
0, 1, 2, 3, 4, 5, 6, 1, -1,
1, 6, 5, 4, 3, 2,
};
#define num_cone_inds (sizeof (cone_inds) / sizeof (cone_inds[0]))
#if 0
static void
lighting_draw_maps (qfv_orenderframe_t *rFrame)
@ -219,6 +166,103 @@ Vulkan_Lighting_CreateRenderPasses (vulkan_ctx_t *ctx)
lctx->qfv_renderpass = rp;
}
#endif
static void
lighting_update_lights (const exprval_t **params, exprval_t *result,
exprctx_t *ectx)
{
auto taskctx = (qfv_taskctx_t *) ectx;
auto ctx = taskctx->ctx;
auto lctx = ctx->lighting_context;
auto lframe = &lctx->frames.a[ctx->curFrame];
lframe->ico_count = 0;
lframe->cone_count = 0;
lframe->flat_count = 0;
if (!lctx->scene || !lctx->scene->lights) {
return;
}
lightingdata_t *ldata = lctx->ldata;
Light_FindVisibleLights (ldata);
dlight_t *lights[MaxLights];
auto packet = QFV_PacketAcquire (ctx->staging);
qfv_light_buffer_t *light_data = QFV_PacketExtend (packet,
sizeof (*light_data));
float style_intensities[NumStyles];
for (int i = 0; i < NumStyles; i++) {
style_intensities[i] = d_lightstylevalue[i] / 65536.0;
}
uint32_t ico_ids[MaxLights];
uint32_t cone_ids[MaxLights];
uint32_t flat_ids[MaxLights];
light_data->lightCount = 0;
R_FindNearLights (r_refdef.frame.position, MaxLights - 1, lights);
for (int i = 0; i < MaxLights - 1; i++) {
if (!lights[i]) {
break;
}
ico_ids[lframe->ico_count++] = light_data->lightCount++;
VectorCopy (lights[i]->color, light_data->lights[i].color);
// dynamic lights seem a tad faint, so 16x map lights
light_data->lights[i].color[3] = lights[i]->radius / 16;
VectorCopy (lights[i]->origin, light_data->lights[i].position);
// dlights are local point sources
light_data->lights[i].position[3] = 1;
light_data->lights[i].attenuation =
(vec4f_t) { 0, 0, 1, 1/lights[i]->radius };
// full sphere, normal light (not ambient)
light_data->lights[i].direction = (vec4f_t) { 0, 0, 1, 1 };
}
for (size_t i = 0; (i < ldata->lightvis.size
&& light_data->lightCount < MaxLights); i++) {
if (ldata->lightvis.a[i]) {
uint32_t id = light_data->lightCount++;
auto light = &light_data->lights[id];
*light = ldata->lights.a[i];
light->color[3] *= style_intensities[ldata->lightstyles.a[i]];
if (light->position[3] && !VectorIsZero (light->direction)
&& light->attenuation[3]) {
if (light->direction[3] < 0) {
cone_ids[lframe->cone_count++] = id;
} else {
ico_ids[lframe->ico_count++] = id;
}
} else {
flat_ids[lframe->flat_count++] = id;
}
}
}
if (developer & SYS_lighting) {
Vulkan_Draw_String (vid.width - 32, 8,
va (ctx->va_ctx, "%3d", light_data->lightCount),
ctx);
}
QFV_PacketCopyBuffer (packet, lframe->data_buffer, 0,
&bufferBarriers[qfv_BB_TransferWrite_to_UniformRead]);
QFV_PacketSubmit (packet);
packet = QFV_PacketAcquire (ctx->staging);
uint32_t id_count = lframe->ico_count + lframe->cone_count
+ lframe->flat_count;
uint32_t *ids = QFV_PacketExtend (packet, id_count * sizeof (uint32_t));
memcpy (ids, ico_ids, lframe->ico_count * sizeof (uint32_t));
ids += lframe->ico_count;
memcpy (ids, cone_ids, lframe->cone_count * sizeof (uint32_t));
ids += lframe->cone_count;
memcpy (ids, flat_ids, lframe->flat_count * sizeof (uint32_t));
QFV_PacketCopyBuffer (packet, lframe->id_buffer, 0,
&bufferBarriers[qfv_BB_TransferWrite_to_IndexRead]);
QFV_PacketSubmit (packet);
}
static VkDescriptorBufferInfo base_buffer_info = {
0, 0, VK_WHOLE_SIZE
};
@ -245,7 +289,8 @@ static VkWriteDescriptorSet base_image_write = {
};
static void
lights_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
lighting_bind_descriptors (const exprval_t **params, exprval_t *result,
exprctx_t *ectx)
{
auto taskctx = (qfv_taskctx_t *) ectx;
auto ctx = taskctx->ctx;
@ -255,17 +300,10 @@ lights_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
auto cmd = taskctx->cmd;
auto layout = taskctx->pipeline->layout;
if (!lctx->scene) {
return;
}
if (lctx->scene->lights) {
update_lights (ctx);
}
lightingframe_t *lframe = &lctx->frames.a[ctx->curFrame];
auto lframe = &lctx->frames.a[ctx->curFrame];
auto fb = &taskctx->renderpass->framebuffer;
lframe->bufferInfo[0].buffer = lframe->light_buffer;
lframe->bufferInfo[0].buffer = lframe->data_buffer;
lframe->attachInfo[0].imageView = fb->views[QFV_attachDepth];
lframe->attachInfo[1].imageView = fb->views[QFV_attachColor];
lframe->attachInfo[2].imageView = fb->views[QFV_attachEmission];
@ -276,22 +314,87 @@ lights_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
lframe->descriptors, 0, 0);
VkDescriptorSet sets[] = {
lframe->attachWrite[0].dstSet,
Vulkan_Matrix_Descriptors (ctx, ctx->curFrame),
lframe->bufferWrite[0].dstSet,
lframe->attachWrite[0].dstSet,
lframe->shadowWrite.dstSet,
};
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
layout, 0, 3, sets, 0, 0);
dfunc->vkCmdDraw (cmd, 3, 1, 0, 0);
VkBuffer buffers[] = {
lframe->id_buffer,
lctx->splat_verts,
};
VkDeviceSize offsets[] = { 0, 0 };
dfunc->vkCmdBindVertexBuffers (cmd, 0, 2, buffers, offsets);
dfunc->vkCmdBindIndexBuffer (cmd, lctx->splat_inds, 0,
VK_INDEX_TYPE_UINT32);
}
static exprfunc_t lights_draw_func[] = {
{ .func = lights_draw },
static void
lighting_draw_splats (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 lctx = ctx->lighting_context;
auto cmd = taskctx->cmd;
auto lframe = &lctx->frames.a[ctx->curFrame];
if (lframe->ico_count) {
dfunc->vkCmdDrawIndexed (cmd, num_ico_inds, lframe->ico_count, 0, 0, 0);
}
if (lframe->cone_count) {
dfunc->vkCmdDrawIndexed (cmd, num_cone_inds, lframe->cone_count,
num_ico_inds, 12, lframe->ico_count);
}
}
static void
lighting_draw_flats (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 lctx = ctx->lighting_context;
auto cmd = taskctx->cmd;
auto lframe = &lctx->frames.a[ctx->curFrame];
if (!lframe->flat_count) {
return;
}
uint32_t splat_count = lframe->ico_count + lframe->cone_count;
dfunc->vkCmdDraw (cmd, 3, lframe->flat_count, 0, splat_count);
}
static exprfunc_t lighting_update_lights_func[] = {
{ .func = lighting_update_lights },
{}
};
static exprfunc_t lighting_bind_descriptors_func[] = {
{ .func = lighting_bind_descriptors },
{}
};
static exprfunc_t lighting_draw_splats_func[] = {
{ .func = lighting_draw_splats },
{}
};
static exprfunc_t lighting_draw_flats_func[] = {
{ .func = lighting_draw_flats },
{}
};
static exprsym_t lighting_task_syms[] = {
{ "lights_draw", &cexpr_function, lights_draw_func },
{ "lighting_update_lights", &cexpr_function, lighting_update_lights_func },
{ "lighting_bind_descriptors", &cexpr_function,
lighting_bind_descriptors_func },
{ "lighting_draw_splats", &cexpr_function, lighting_draw_splats_func },
{ "lighting_draw_flats", &cexpr_function, lighting_draw_flats_func },
{}
};
@ -304,13 +407,59 @@ Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
QFV_Render_AddTasks (ctx, lighting_task_syms);
}
static void
make_ico (qfv_packet_t *packet)
{
vec3_t *verts = QFV_PacketExtend (packet, sizeof (vec3_t[ico_verts]));
float p = (sqrt(5) + 1) / 2;
float a = sqrt (3) / p;
float b = a / p;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
float my = j & 1 ? a : -a;
float mz = j & 2 ? b : -b;
int vind = i * 4 + j;
int ix = i;
int iy = (i + 1) % 3;
int iz = (i + 2) % 3;
verts[vind][ix] = 0;
verts[vind][iy] = my;
verts[vind][iz] = mz;
}
}
}
static void
make_cone (qfv_packet_t *packet)
{
vec3_t *verts = QFV_PacketExtend (packet, sizeof (vec3_t[cone_verts]));
float a = 2 / sqrt (3);
float b = 1 / sqrt (3);
VectorSet ( 0, 0, 0, verts[0]);
VectorSet ( a, 0, -1, verts[1]);
VectorSet ( b, 1, -1, verts[2]);
VectorSet (-b, 1, -1, verts[3]);
VectorSet (-a, 0, -1, verts[4]);
VectorSet (-b, -1, -1, verts[5]);
VectorSet ( b, -1, -1, verts[6]);
}
static void
write_inds (qfv_packet_t *packet)
{
uint32_t *inds = QFV_PacketExtend (packet, sizeof (ico_inds)
+ sizeof (cone_inds));
memcpy (inds, ico_inds, sizeof (ico_inds));
inds += num_ico_inds;
memcpy (inds, cone_inds, sizeof (cone_inds));
}
void
Vulkan_Lighting_Setup (vulkan_ctx_t *ctx)
{
qfvPushDebug (ctx, "lighting init");
auto device = ctx->device;
auto dfunc = device->funcs;
auto lctx = ctx->lighting_context;
lctx->sampler = QFV_Render_Sampler (ctx, "shadow_sampler");
@ -337,32 +486,79 @@ Vulkan_Lighting_Setup (vulkan_ctx_t *ctx)
DARRAY_RESIZE (&lctx->frames, frames);
lctx->frames.grow = 0;
__auto_type lbuffers = QFV_AllocBufferSet (frames, alloca);
lctx->light_resources = malloc (sizeof (qfv_resource_t)
// splat vertices
+ sizeof (qfv_resobj_t)
// splat indices
+ sizeof (qfv_resobj_t)
// light data
+ sizeof (qfv_resobj_t[frames])
// light indices
+ sizeof (qfv_resobj_t[frames]));
auto splat_verts = (qfv_resobj_t *) &lctx->light_resources[1];
auto splat_inds = &splat_verts[1];
auto light_data = &splat_inds[1];
auto light_ids = &light_data[frames];
lctx->light_resources[0] = (qfv_resource_t) {
.name = "lights",
.va_ctx = ctx->va_ctx,
.memory_properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
.num_objects = 2 + 2 * frames,
.objects = splat_verts,
};
splat_verts[0] = (qfv_resobj_t) {
.name = "splat:vertices",
.type = qfv_res_buffer,
.buffer = {
.size = (20 + 7) * sizeof (vec3_t),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
},
};
splat_inds[0] = (qfv_resobj_t) {
.name = "splat:indices",
.type = qfv_res_buffer,
.buffer = {
.size = sizeof (ico_inds) + sizeof (cone_inds),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
},
};
for (size_t i = 0; i < frames; i++) {
lbuffers->a[i] = QFV_CreateBuffer (device, sizeof (qfv_light_buffer_t),
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_BUFFER,
lbuffers->a[i],
va (ctx->va_ctx, "buffer:lighting:%zd", i));
light_data[i] = (qfv_resobj_t) {
.name = "data",
.type = qfv_res_buffer,
.buffer = {
.size = sizeof (qfv_light_buffer_t),
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
};
light_ids[i] = (qfv_resobj_t) {
.name = "ids",
.type = qfv_res_buffer,
.buffer = {
.size = 2 * MaxLights * sizeof (uint32_t),
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
| VK_BUFFER_USAGE_TRANSFER_DST_BIT,
},
};
}
VkMemoryRequirements requirements;
dfunc->vkGetBufferMemoryRequirements (device->dev, lbuffers->a[0],
&requirements);
size_t light_size = QFV_NextOffset (requirements.size, &requirements);
lctx->light_memory = QFV_AllocBufferMemory (device, lbuffers->a[0],
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
frames * light_size, 0);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DEVICE_MEMORY,
lctx->light_memory, "memory:lighting");
QFV_CreateResource (ctx->device, lctx->light_resources);
lctx->splat_verts = splat_verts[0].buffer.buffer;
lctx->splat_inds = splat_inds[0].buffer.buffer;
auto attach_mgr = QFV_Render_DSManager (ctx, "lighting_attach");
auto lights_mgr = QFV_Render_DSManager (ctx, "lighting_lights");
auto shadow_mgr = QFV_Render_DSManager (ctx, "lighting_shadow");
VkDeviceSize light_offset = 0;
for (size_t i = 0; i < frames; i++) {
__auto_type lframe = &lctx->frames.a[i];
auto lframe = &lctx->frames.a[i];
*lframe = (lightingframe_t) {
.data_buffer = light_data[i].buffer.buffer,
.id_buffer = light_ids[i].buffer.buffer,
};
auto attach = QFV_DSManager_AllocSet (attach_mgr);
auto lights = QFV_DSManager_AllocSet (lights_mgr);
@ -374,11 +570,6 @@ Vulkan_Lighting_Setup (vulkan_ctx_t *ctx)
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET, shadow,
va (ctx->va_ctx, "lighting:shadow_set:%zd", i));
lframe->light_buffer = lbuffers->a[i];
QFV_BindBufferMemory (device, lbuffers->a[i], lctx->light_memory,
light_offset);
light_offset += light_size;
for (int j = 0; j < LIGHTING_BUFFER_INFOS; j++) {
lframe->bufferInfo[j] = base_buffer_info;
lframe->bufferWrite[j] = base_buffer_write;
@ -405,6 +596,19 @@ Vulkan_Lighting_Setup (vulkan_ctx_t *ctx)
lframe->shadowWrite.descriptorCount = LIGHTING_SHADOW_INFOS;
lframe->shadowWrite.pImageInfo = lframe->shadowInfo;
}
auto packet = QFV_PacketAcquire (ctx->staging);
make_ico (packet);
make_cone (packet);
QFV_PacketCopyBuffer (packet, splat_verts[0].buffer.buffer, 0,
&bufferBarriers[qfv_BB_TransferWrite_to_UniformRead]);
QFV_PacketSubmit (packet);
packet = QFV_PacketAcquire (ctx->staging);
write_inds (packet);
QFV_PacketCopyBuffer (packet, splat_inds[0].buffer.buffer, 0,
&bufferBarriers[qfv_BB_TransferWrite_to_IndexRead]);
QFV_PacketSubmit (packet);
qfvPopDebug (ctx);
}
@ -442,12 +646,9 @@ Vulkan_Lighting_Shutdown (vulkan_ctx_t *ctx)
dfunc->vkDestroyRenderPass (device->dev, lctx->renderpass_4, 0);
dfunc->vkDestroyRenderPass (device->dev, lctx->renderpass_1, 0);
for (size_t i = 0; i < lctx->frames.size; i++) {
lightingframe_t *lframe = &lctx->frames.a[i];
dfunc->vkDestroyBuffer (device->dev, lframe->light_buffer, 0);
}
dfunc->vkFreeMemory (device->dev, lctx->light_memory, 0);
dfunc->vkDestroyPipeline (device->dev, lctx->pipeline, 0);
QFV_DestroyResource (device, lctx->light_resources);
free (lctx->light_resources);
DARRAY_CLEAR (&lctx->light_mats);
DARRAY_CLEAR (&lctx->light_images);
DARRAY_CLEAR (&lctx->light_renderers);