quakeforge/libs/video/renderer/vulkan/vulkan_alias.c
Bill Currie 35eec0b2e5 [ecs] Implement hierarchies as components
The main goal of this change was to make it easier to tell when a
hierarchy has been deleted, but as a side benefit, it got rid of the use
of PR_RESMAP. Also, it's easy to track the number of hierarchies.

Unfortunately, it showed how brittle the component side of the ECS is
(scene and canvas registries assumed their components were the first (no
long the case), thus the sweeping changes).

Centerprint doesn't work (but it hasn't for a while).
2024-01-02 16:38:01 +09:00

360 lines
9.7 KiB
C

/*
vulkan_alias.c
Vulkan alias model pipeline
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2012/1/7
Date: 2021/1/26
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/cvar.h"
#include "QF/va.h"
#include "QF/scene/entity.h"
#include "QF/Vulkan/qf_alias.h"
#include "QF/Vulkan/qf_lighting.h"
#include "QF/Vulkan/qf_matrices.h"
#include "QF/Vulkan/qf_palette.h"
#include "QF/Vulkan/qf_texture.h"
#include "QF/Vulkan/debug.h"
#include "QF/Vulkan/device.h"
#include "QF/Vulkan/instance.h"
#include "QF/Vulkan/render.h"
#include "r_internal.h"
#include "vid_vulkan.h"
typedef struct {
mat4f_t mat;
float blend;
byte colors[4];
vec4f_t base_color;
vec4f_t fog;
} alias_push_constants_t;
typedef struct {
mat4f_t mat;
float blend;
uint32_t matrix_base;
} shadow_push_constants_t;
static renderer_t *
alias_get_renderer (entity_t ent)
{
return Ent_GetComponent (ent.id, ent.base + scene_renderer, ent.reg);
}
static animation_t *
alias_get_animation (entity_t ent)
{
return Ent_GetComponent (ent.id, ent.base + scene_animation, ent.reg);
}
static colormap_t *
alias_get_colormap (entity_t ent)
{
return Ent_GetComponent (ent.id, ent.base + scene_colormap, ent.reg);
}
static void
alias_depth_range (qfv_taskctx_t *taskctx, float minDepth, float maxDepth)
{
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto dfunc = device->funcs;
auto viewport = taskctx->pipeline->viewport;
viewport.minDepth = minDepth;
viewport.maxDepth = maxDepth;
dfunc->vkCmdSetViewport (taskctx->cmd, 0, 1, &viewport);
}
void
Vulkan_AliasAddSkin (vulkan_ctx_t *ctx, qfv_alias_skin_t *skin)
{
aliasctx_t *actx = ctx->alias_context;
skin->descriptor = Vulkan_CreateCombinedImageSampler (ctx, skin->view,
actx->sampler);
}
void
Vulkan_AliasRemoveSkin (vulkan_ctx_t *ctx, qfv_alias_skin_t *skin)
{
Vulkan_FreeTexture (ctx, skin->descriptor);
skin->descriptor = 0;
}
static void
push_alias_constants (const mat4f_t mat, float blend, byte *colors,
vec4f_t base_color, int pass, qfv_taskctx_t *taskctx)
{
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto cmd = taskctx->cmd;
auto layout = taskctx->pipeline->layout;
alias_push_constants_t constants = {
.blend = blend,
.colors = { VEC4_EXP (colors) },
.base_color = base_color,
};
qfv_push_constants_t push_constants[] = {
{ VK_SHADER_STAGE_VERTEX_BIT,
field_offset (alias_push_constants_t, mat),
sizeof (mat4f_t), mat },
{ VK_SHADER_STAGE_VERTEX_BIT,
field_offset (alias_push_constants_t, blend),
sizeof (float), &constants.blend },
{ VK_SHADER_STAGE_FRAGMENT_BIT,
field_offset (alias_push_constants_t, colors),
sizeof (constants.colors), constants.colors },
{ VK_SHADER_STAGE_FRAGMENT_BIT,
field_offset (alias_push_constants_t, base_color),
sizeof (constants.base_color), &constants.base_color },
{ VK_SHADER_STAGE_FRAGMENT_BIT,
field_offset (alias_push_constants_t, fog),
sizeof (constants.fog), &constants.fog },
};
QFV_PushConstants (device, cmd, layout, pass ? 5 : 2, push_constants);
}
static void
push_shadow_constants (const mat4f_t mat, float blend, uint16_t *matrix_base,
qfv_taskctx_t *taskctx)
{
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto cmd = taskctx->cmd;
auto layout = taskctx->pipeline->layout;
shadow_push_constants_t constants = {
.blend = blend,
.matrix_base = *matrix_base,
};
qfv_push_constants_t push_constants[] = {
{ VK_SHADER_STAGE_VERTEX_BIT,
field_offset (shadow_push_constants_t, mat),
sizeof (mat4f_t), mat },
{ VK_SHADER_STAGE_VERTEX_BIT,
field_offset (shadow_push_constants_t, blend),
sizeof (float), &constants.blend },
{ VK_SHADER_STAGE_VERTEX_BIT,
field_offset (shadow_push_constants_t, matrix_base),
sizeof (uint32_t), &constants.matrix_base },
};
QFV_PushConstants (device, cmd, layout, 3, push_constants);
}
static void
alias_draw_ent (qfv_taskctx_t *taskctx, entity_t ent, bool pass,
renderer_t *renderer)
{
auto model = renderer->model;
aliashdr_t *hdr;
qfv_alias_skin_t *skin;
uint16_t *matrix_base = taskctx->data;
if (!(hdr = model->aliashdr)) {
hdr = Cache_Get (&model->cache);
}
auto animation = alias_get_animation (ent);
float blend = R_AliasGetLerpedFrames (animation, hdr);
transform_t transform = Entity_Transform (ent);
if (0/*XXX ent->skin && ent->skin->tex*/) {
//skin = ent->skin->tex;
} else {
maliasskindesc_t *skindesc;
skindesc = R_AliasGetSkindesc (animation, renderer->skinnum, hdr);
skin = (qfv_alias_skin_t *) ((byte *) hdr + skindesc->skin);
}
vec4f_t base_color;
byte colors[4];
QuatCopy (renderer->colormod, base_color);
QuatCopy (skin->colors, colors);
if (Ent_HasComponent (ent.id, ent.base + scene_colormap, ent.reg)) {
auto colormap = alias_get_colormap (ent);
colors[0] = colormap->top * 16 + 8;
colors[1] = colormap->bottom * 16 + 8;
}
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto dfunc = device->funcs;
auto cmd = taskctx->cmd;
auto layout = taskctx->pipeline->layout;
auto mesh = (qfv_alias_mesh_t *) ((byte *) hdr + hdr->commands);
VkDeviceSize offsets[] = {
animation->pose1 * hdr->poseverts * sizeof (aliasvrt_t),
animation->pose2 * hdr->poseverts * sizeof (aliasvrt_t),
0,
};
VkBuffer buffers[] = {
mesh->vertex_buffer,
mesh->vertex_buffer,
mesh->uv_buffer,
};
int bindingCount = skin ? 3 : 2;
Vulkan_BeginEntityLabel (ctx, cmd, ent);
dfunc->vkCmdBindVertexBuffers (cmd, 0, bindingCount, buffers, offsets);
dfunc->vkCmdBindIndexBuffer (cmd, mesh->index_buffer, 0,
VK_INDEX_TYPE_UINT32);
if (pass && skin) {
VkDescriptorSet sets[] = {
skin->descriptor,
};
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
layout, 2, 1, sets, 0, 0);
}
if (matrix_base) {
push_shadow_constants (Transform_GetWorldMatrixPtr (transform),
blend, matrix_base, taskctx);
} else {
push_alias_constants (Transform_GetWorldMatrixPtr (transform),
blend, colors, base_color, pass, taskctx);
}
dfunc->vkCmdDrawIndexed (cmd, 3 * hdr->mdl.numtris, 1, 0, 0, 0);
QFV_CmdEndLabel (device, cmd);
}
static void
alias_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
{
qfZoneNamed (zone, true);
auto taskctx = (qfv_taskctx_t *) ectx;
auto pass = *(int *) params[0]->value;
auto stage = *(int *) params[1]->value;
auto ctx = taskctx->ctx;
auto device = ctx->device;
auto dfunc = device->funcs;
auto layout = taskctx->pipeline->layout;
auto cmd = taskctx->cmd;
bool shadow = !!taskctx->data;
VkDescriptorSet sets[] = {
shadow ? Vulkan_Lighting_Descriptors (ctx, ctx->curFrame)
: Vulkan_Matrix_Descriptors (ctx, ctx->curFrame),
Vulkan_Palette_Descriptor (ctx),
};
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
layout, 0, shadow ? 1 : 2, sets, 0, 0);
auto queue = r_ent_queue; //FIXME fetch from scene
for (size_t i = 0; i < queue->ent_queues[mod_alias].size; i++) {
entity_t ent = queue->ent_queues[mod_alias].a[i];
auto renderer = alias_get_renderer (ent);
if ((stage == alias_shadow && renderer->noshadows)
|| (stage == alias_main && renderer->onlyshadows)) {
continue;
}
// FIXME hack the depth range to prevent view model
// from poking into walls
if (stage == alias_main && renderer->depthhack) {
alias_depth_range (taskctx, 0.7, 1);
}
alias_draw_ent (taskctx, ent, pass, renderer);
// unhack in case the view_model is not the last
if (stage == alias_main && renderer->depthhack) {
alias_depth_range (taskctx, 0, 1);
}
}
}
static exprenum_t alias_stage_enum;
static exprtype_t alias_stage_type = {
.name = "alias_stage",
.size = sizeof (int),
.get_string = cexpr_enum_get_string,
.data = &alias_stage_enum,
};
static int alias_stage_values[] = { alias_main, alias_shadow, };
static exprsym_t alias_stage_symbols[] = {
{"main", &alias_stage_type, alias_stage_values + 0},
{"shadow", &alias_stage_type, alias_stage_values + 1},
{}
};
static exprtab_t alias_stage_symtab = { .symbols = alias_stage_symbols };
static exprenum_t alias_stage_enum = {
&alias_stage_type,
&alias_stage_symtab,
};
static exprtype_t *alias_draw_params[] = {
&cexpr_int,
&alias_stage_type,
};
static exprfunc_t alias_draw_func[] = {
{ .func = alias_draw, .num_params = 2, .param_types = alias_draw_params },
{}
};
static exprsym_t alias_task_syms[] = {
{ "alias_draw", &cexpr_function, alias_draw_func },
{}
};
void
Vulkan_Alias_Init (vulkan_ctx_t *ctx)
{
qfvPushDebug (ctx, "alias init");
QFV_Render_AddTasks (ctx, alias_task_syms);
aliasctx_t *actx = calloc (1, sizeof (aliasctx_t));
ctx->alias_context = actx;
qfvPopDebug (ctx);
}
void
Vulkan_Alias_Setup (vulkan_ctx_t *ctx)
{
auto actx = ctx->alias_context;
actx->sampler = QFV_Render_Sampler (ctx, "alias_sampler");
}
void
Vulkan_Alias_Shutdown (vulkan_ctx_t *ctx)
{
//qfv_device_t *device = ctx->device;
//qfv_devfuncs_t *dfunc = device->funcs;
aliasctx_t *actx = ctx->alias_context;
free (actx);
}