mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-02-20 18:52:28 +00:00
[vulkan] Add support for IQM models
Despite the base IQM specification not supporting blend-shapes, I think IQM will become the basis for QF's generic model representation (at least for the more advanced renderers). After my experience with .mu models (KSP) and unity mesh objects (both normal and skinned), and reviewing the IQM spec, it looks like with the addition of support for blend-shapes, IQM is actually pretty good. This is just the preliminary work to get standard IQM models loading in vulkan (seems to work, along with unloading), and they very basics into the renderer (most likely not working: not tested yet). The rest of the renderer seems to be unaffected, though, which is good.
This commit is contained in:
parent
a4f500da3c
commit
bbca22c722
10 changed files with 1262 additions and 1 deletions
113
include/QF/Vulkan/qf_iqm.h
Normal file
113
include/QF/Vulkan/qf_iqm.h
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
qf_iqm.h
|
||||
|
||||
Vulkan specific iqm model stuff
|
||||
|
||||
Copyright (C) 2022 Bill Currie <bill@taniwha.org>
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2022/5/3
|
||||
|
||||
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_iqm_h
|
||||
#define __QF_Vulkan_qf_iqm_h
|
||||
|
||||
#include "QF/darray.h"
|
||||
#include "QF/model.h"
|
||||
#include "QF/modelgen.h"
|
||||
#include "QF/Vulkan/qf_vid.h"
|
||||
#include "QF/Vulkan/command.h"
|
||||
|
||||
// geometry attributes
|
||||
typedef struct iqmgvert_s {
|
||||
float vertex[3];
|
||||
byte bones[4]; // uint
|
||||
byte weights[4]; // unorm
|
||||
} iqmgvert_t;
|
||||
|
||||
// rendering attributes
|
||||
typedef struct iqmrvert_s {
|
||||
float uv[2];
|
||||
float normal[3];
|
||||
float tangent[4];
|
||||
byte color[4]; // unorm
|
||||
} iqmrvert_t;
|
||||
|
||||
typedef struct qfv_iqm_skin_s {
|
||||
VkImageView view;
|
||||
byte colora[4];
|
||||
byte colorb[4];
|
||||
VkDescriptorSet descriptor;
|
||||
} qfv_iqm_skin_t;
|
||||
|
||||
typedef struct qfv_iqm_s {
|
||||
VkBuffer geom_buffer;
|
||||
VkBuffer rend_buffer;
|
||||
VkBuffer index_buffer;
|
||||
qfv_iqm_skin_t *skins;
|
||||
struct qfv_resource_s *mesh;
|
||||
struct qfv_resource_s *bones;
|
||||
} qfv_iqm_t;
|
||||
|
||||
typedef enum {
|
||||
QFV_iqmDepth,
|
||||
QFV_iqmGBuffer,
|
||||
QFV_iqmTranslucent,
|
||||
|
||||
QFV_iqmNumPasses
|
||||
} QFV_IQMSubpass;
|
||||
|
||||
typedef struct iqm_frame_s {
|
||||
qfv_cmdbufferset_t cmdSet;
|
||||
} iqm_frame_t;
|
||||
|
||||
typedef struct iqm_frameset_s
|
||||
DARRAY_TYPE (iqm_frame_t) iqm_frameset_t;
|
||||
|
||||
typedef struct iqmindset_s
|
||||
DARRAY_TYPE (unsigned) iqmindset_t;
|
||||
|
||||
typedef struct iqmctx_s {
|
||||
iqm_frameset_t frames;
|
||||
VkPipeline depth;
|
||||
VkPipeline gbuf;
|
||||
VkPipelineLayout layout;
|
||||
VkSampler sampler;
|
||||
} iqmctx_t;
|
||||
|
||||
struct vulkan_ctx_s;
|
||||
struct qfv_renderframe_s;
|
||||
struct entity_s;
|
||||
struct mod_iqm_ctx_s;
|
||||
|
||||
void Vulkan_Mod_IQMFinish (struct model_s *mod, struct vulkan_ctx_s *ctx);
|
||||
|
||||
void Vulkan_IQMAddSkin (struct vulkan_ctx_s *ctx, qfv_iqm_skin_t *skin);
|
||||
void Vulkan_IQMRemoveSkin (struct vulkan_ctx_s *ctx, qfv_iqm_skin_t *skin);
|
||||
|
||||
void Vulkan_IQMBegin (struct qfv_renderframe_s *rFrame);
|
||||
void Vulkan_DrawIQM (struct entity_s *ent, struct qfv_renderframe_s *rFrame);
|
||||
void Vulkan_IQMEnd (struct qfv_renderframe_s *rFrame);
|
||||
|
||||
void Vulkan_IQM_Init (struct vulkan_ctx_s *ctx);
|
||||
void Vulkan_IQM_Shutdown (struct vulkan_ctx_s *ctx);
|
||||
|
||||
#endif//__QF_Vulkan_qf_iqm_h
|
|
@ -67,6 +67,7 @@ typedef struct vulkan_ctx_s {
|
|||
struct matrixctx_s *matrix_context;
|
||||
struct aliasctx_s *alias_context;
|
||||
struct bspctx_s *bsp_context;
|
||||
struct iqmctx_s *iqm_context;
|
||||
struct particlectx_s *particle_context;
|
||||
struct spritectx_s *sprite_context;
|
||||
struct drawctx_s *draw_context;
|
||||
|
|
|
@ -0,0 +1,468 @@
|
|||
/*
|
||||
vulkan_model_iqm.c
|
||||
|
||||
iqm model processing for Vulkan
|
||||
|
||||
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2022/05/03
|
||||
|
||||
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
|
||||
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRINGS_H
|
||||
# include <strings.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "QF/dstring.h"
|
||||
#include "QF/image.h"
|
||||
#include "QF/quakefs.h"
|
||||
#include "QF/va.h"
|
||||
|
||||
#include "QF/Vulkan/barrier.h"
|
||||
#include "QF/Vulkan/device.h"
|
||||
#include "QF/Vulkan/image.h"
|
||||
#include "QF/Vulkan/resource.h"
|
||||
#include "QF/Vulkan/staging.h"
|
||||
#include "QF/Vulkan/qf_iqm.h"
|
||||
#include "QF/Vulkan/qf_texture.h"
|
||||
|
||||
#include "mod_internal.h"
|
||||
#include "r_shared.h"
|
||||
#include "vid_vulkan.h"
|
||||
|
||||
static byte null_texture[] = {
|
||||
204, 204, 204, 255,
|
||||
204, 204, 204, 255,
|
||||
204, 204, 204, 255,
|
||||
204, 204, 204, 255,
|
||||
};
|
||||
#if 0
|
||||
static byte null_normmap[] = {
|
||||
127, 127, 255, 255,
|
||||
127, 127, 255, 255,
|
||||
127, 127, 255, 255,
|
||||
127, 127, 255, 255,
|
||||
};
|
||||
#endif
|
||||
static void
|
||||
vulkan_iqm_clear (model_t *mod, void *data)
|
||||
{
|
||||
vulkan_ctx_t *ctx = data;
|
||||
qfv_device_t *device = ctx->device;
|
||||
iqm_t *iqm = (iqm_t *) mod->aliashdr;
|
||||
qfv_iqm_t *mesh = iqm->extra_data;
|
||||
|
||||
mod->needload = true;
|
||||
|
||||
QFV_DestroyResource (device, mesh->bones);
|
||||
QFV_DestroyResource (device, mesh->mesh);
|
||||
free (mesh);
|
||||
Mod_FreeIQM (iqm);
|
||||
}
|
||||
|
||||
static void
|
||||
vulkan_iqm_init_image (iqm_t *iqm, int meshnum, qfv_resobj_t *image)
|
||||
{
|
||||
const char *material = iqm->text + iqm->meshes[meshnum].material;
|
||||
dstring_t *str = dstring_new ();
|
||||
dstring_copystr (str, material);
|
||||
QFS_StripExtension (str->str, str->str);
|
||||
|
||||
tex_t *tex;
|
||||
if ((tex = LoadImage (va (0, "textures/%s", str->str), 0))) {
|
||||
*image = (qfv_resobj_t) {
|
||||
.name = material,
|
||||
.type = qfv_res_image,
|
||||
.image = {
|
||||
.type = VK_IMAGE_TYPE_2D,
|
||||
.format = QFV_ImageFormat (tex->format),
|
||||
.extent = {
|
||||
.width = tex->width,
|
||||
.height = tex->height,
|
||||
.depth = 1,
|
||||
},
|
||||
.num_mipmaps = QFV_MipLevels (tex->width, tex->height),
|
||||
.num_layers = 1,
|
||||
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
||||
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
||||
| VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
*image = (qfv_resobj_t) {
|
||||
.name = material,
|
||||
.type = qfv_res_image,
|
||||
.image = {
|
||||
.type = VK_IMAGE_TYPE_2D,
|
||||
.format = QFV_ImageFormat (tex_rgba),
|
||||
.extent = {
|
||||
.width = 2,
|
||||
.height = 2,
|
||||
.depth = 1,
|
||||
},
|
||||
.num_mipmaps = QFV_MipLevels (2, 2),
|
||||
.num_layers = 1,
|
||||
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
||||
| VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
},
|
||||
};
|
||||
}
|
||||
dstring_delete (str);
|
||||
}
|
||||
|
||||
static void
|
||||
iqm_transfer_texture (tex_t *tex, VkImage image, qfv_stagebuf_t *stage,
|
||||
qfv_device_t *device)
|
||||
{
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
|
||||
if (tex->format != tex_rgb && tex->format != tex_rgba) {
|
||||
Sys_Error ("can't transfer iqm image");
|
||||
}
|
||||
// FIXME correct only for rgb and rgba
|
||||
size_t layer_size = tex->width * tex->height * tex->format;
|
||||
|
||||
qfv_packet_t *packet = QFV_PacketAcquire (stage);
|
||||
byte *dst = QFV_PacketExtend (packet, layer_size);
|
||||
|
||||
qfv_imagebarrier_t ib = imageBarriers[qfv_LT_Undefined_to_TransferDst];
|
||||
ib.barrier.image = image;
|
||||
ib.barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
|
||||
ib.barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
||||
dfunc->vkCmdPipelineBarrier (packet->cmd, ib.srcStages, ib.dstStages,
|
||||
0, 0, 0, 0, 0,
|
||||
1, &ib.barrier);
|
||||
memcpy (dst, tex->data, layer_size);
|
||||
|
||||
int mipLevels = QFV_MipLevels (tex->width, tex->height);
|
||||
if (mipLevels == 1) {
|
||||
ib = imageBarriers[qfv_LT_TransferDst_to_ShaderReadOnly];
|
||||
ib.barrier.image = image;
|
||||
ib.barrier.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
|
||||
ib.barrier.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
||||
dfunc->vkCmdPipelineBarrier (packet->cmd, ib.srcStages, ib.dstStages,
|
||||
0, 0, 0, 0, 0,
|
||||
1, &ib.barrier);
|
||||
} else {
|
||||
QFV_GenerateMipMaps (device, packet->cmd, image, mipLevels,
|
||||
tex->width, tex->height, 1);
|
||||
}
|
||||
QFV_PacketSubmit (packet);
|
||||
}
|
||||
|
||||
static void
|
||||
vulkan_iqm_load_textures (model_t *mod, iqm_t *iqm, qfv_iqm_t *mesh,
|
||||
vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
dstring_t *str = dstring_new ();
|
||||
tex_t *tex;
|
||||
size_t buff_size = 0;
|
||||
qfv_resobj_t *objects = mesh->mesh->objects;
|
||||
|
||||
for (int i = 0; i < iqm->num_meshes; i++) {
|
||||
int image_ind = 3 + 2 * i;
|
||||
VkExtent3D extent = objects[image_ind].image.extent;
|
||||
// probably 3 or 4 bytes per pixel FIXME
|
||||
buff_size = max (buff_size, extent.width * extent.height * 4);
|
||||
}
|
||||
|
||||
qfv_stagebuf_t *stage = QFV_CreateStagingBuffer (device,
|
||||
va (ctx->va_ctx, "iqm:%s",
|
||||
mod->name),
|
||||
4 * buff_size,
|
||||
ctx->cmdpool);
|
||||
|
||||
for (int i = 0; i < iqm->num_meshes; i++) {
|
||||
int image_ind = 3 + 2 * i;
|
||||
__auto_type image = &objects[image_ind].image;
|
||||
__auto_type view = &objects[image_ind + 1].image_view;
|
||||
qfv_iqm_skin_t *skin = &mesh->skins[i];
|
||||
*skin = (qfv_iqm_skin_t) {
|
||||
.view = view->view,
|
||||
.colora = { 255, 255, 255, 255 },
|
||||
.colorb = { 255, 255, 255, 255 },
|
||||
};
|
||||
|
||||
dstring_copystr (str, iqm->text + iqm->meshes[i].material);
|
||||
QFS_StripExtension (str->str, str->str);
|
||||
if (!(tex = LoadImage (va (0, "textures/%s", str->str), 1))) {
|
||||
tex_t null_tex = {
|
||||
.width = 2,
|
||||
.height = 2,
|
||||
.format = tex_rgba,
|
||||
.data = null_texture,
|
||||
};
|
||||
tex = &null_tex;
|
||||
}
|
||||
iqm_transfer_texture (tex, image->image, stage, device);
|
||||
}
|
||||
dstring_delete (str);
|
||||
QFV_DestroyStagingBuffer (stage);
|
||||
}
|
||||
|
||||
static void
|
||||
vulkan_iqm_load_arrays (model_t *mod, iqm_t *iqm, qfv_iqm_t *mesh,
|
||||
vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
size_t geom_size = iqm->num_verts * sizeof (iqmgvert_t);
|
||||
size_t rend_size = iqm->num_verts * sizeof (iqmrvert_t);
|
||||
size_t elem_size = iqm->num_elements * sizeof (uint16_t);
|
||||
size_t buff_size = geom_size + rend_size + elem_size + 1024;
|
||||
qfv_stagebuf_t *stage = QFV_CreateStagingBuffer (device,
|
||||
va (ctx->va_ctx, "iqm:%s",
|
||||
mod->name),
|
||||
buff_size, ctx->cmdpool);
|
||||
qfv_packet_t *gpacket = QFV_PacketAcquire (stage);
|
||||
iqmgvert_t *gverts = QFV_PacketExtend (gpacket, geom_size);
|
||||
qfv_packet_t *rpacket = QFV_PacketAcquire (stage);
|
||||
iqmrvert_t *rverts = QFV_PacketExtend (rpacket, rend_size);
|
||||
qfv_packet_t *epacket = QFV_PacketAcquire (stage);
|
||||
uint16_t *elements = QFV_PacketExtend (epacket, elem_size);
|
||||
//FIXME this whole thing is silly, but some person went and interleaved
|
||||
//all the vertex data prematurely
|
||||
for (int i = 0; i < iqm->num_verts; i++) {
|
||||
byte *data = iqm->vertices + i * iqm->stride;
|
||||
iqmgvert_t *gv = gverts + i;
|
||||
iqmrvert_t *rv = rverts + i;
|
||||
for (int j = 0; j < iqm->num_arrays; j++) {
|
||||
__auto_type va = &iqm->vertexarrays[j];
|
||||
// FIXME assumes standard iqm sizes
|
||||
size_t size = 0;
|
||||
switch (va->type) {
|
||||
case IQM_POSITION:
|
||||
size = sizeof (gv->vertex);
|
||||
memcpy (gv->vertex, data, size);
|
||||
break;
|
||||
case IQM_TEXCOORD:
|
||||
size = sizeof (rv->uv);
|
||||
memcpy (rv->uv, data, size);
|
||||
break;
|
||||
case IQM_NORMAL:
|
||||
size = sizeof (rv->normal);
|
||||
memcpy (rv->normal, data, size);
|
||||
break;
|
||||
case IQM_TANGENT:
|
||||
size = sizeof (rv->tangent);
|
||||
memcpy (rv->tangent, data, size);
|
||||
break;
|
||||
case IQM_BLENDINDEXES:
|
||||
size = sizeof (gv->bones);
|
||||
memcpy (gv->bones, data, size);
|
||||
break;
|
||||
case IQM_BLENDWEIGHTS:
|
||||
size = sizeof (gv->weights);
|
||||
memcpy (gv->weights, data, size);
|
||||
break;
|
||||
case IQM_COLOR:
|
||||
size = sizeof (rv->color);
|
||||
memcpy (rv->color, data, size);
|
||||
break;
|
||||
case IQM_CUSTOM:
|
||||
// FIXME model loader doesn't handle these, so nothing to do
|
||||
break;
|
||||
}
|
||||
data += size;
|
||||
}
|
||||
}
|
||||
memcpy (elements, iqm->elements, elem_size);
|
||||
|
||||
qfv_bufferbarrier_t bb[] = {
|
||||
bufferBarriers[qfv_BB_Unknown_to_TransferWrite],
|
||||
bufferBarriers[qfv_BB_Unknown_to_TransferWrite],
|
||||
bufferBarriers[qfv_BB_Unknown_to_TransferWrite],
|
||||
};
|
||||
bb[0].barrier.buffer = mesh->geom_buffer;
|
||||
bb[0].barrier.size = geom_size;
|
||||
bb[1].barrier.buffer = mesh->rend_buffer;
|
||||
bb[1].barrier.size = rend_size;
|
||||
bb[2].barrier.buffer = mesh->index_buffer;
|
||||
bb[2].barrier.size = elem_size;
|
||||
VkBufferCopy copy_region[] = {
|
||||
{ gpacket->offset, 0, geom_size },
|
||||
{ rpacket->offset, 0, rend_size },
|
||||
{ epacket->offset, 0, elem_size },
|
||||
};
|
||||
|
||||
dfunc->vkCmdPipelineBarrier (gpacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[0].barrier, 0, 0);
|
||||
dfunc->vkCmdPipelineBarrier (rpacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[1].barrier, 0, 0);
|
||||
dfunc->vkCmdPipelineBarrier (epacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[2].barrier, 0, 0);
|
||||
dfunc->vkCmdCopyBuffer (gpacket->cmd, stage->buffer,
|
||||
mesh->geom_buffer, 1, ©_region[0]);
|
||||
dfunc->vkCmdCopyBuffer (rpacket->cmd, stage->buffer,
|
||||
mesh->rend_buffer, 1, ©_region[1]);
|
||||
dfunc->vkCmdCopyBuffer (epacket->cmd, stage->buffer,
|
||||
mesh->index_buffer, 1, ©_region[2]);
|
||||
bb[0] = bufferBarriers[qfv_BB_TransferWrite_to_VertexAttrRead];
|
||||
bb[1] = bufferBarriers[qfv_BB_TransferWrite_to_VertexAttrRead];
|
||||
bb[2] = bufferBarriers[qfv_BB_TransferWrite_to_VertexAttrRead];
|
||||
bb[0].barrier.buffer = mesh->geom_buffer;
|
||||
bb[0].barrier.size = geom_size;
|
||||
bb[1].barrier.buffer = mesh->rend_buffer;
|
||||
bb[1].barrier.size = rend_size;
|
||||
bb[2].barrier.buffer = mesh->index_buffer;
|
||||
bb[2].barrier.size = elem_size;
|
||||
dfunc->vkCmdPipelineBarrier (gpacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[0].barrier, 0, 0);
|
||||
dfunc->vkCmdPipelineBarrier (rpacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[1].barrier, 0, 0);
|
||||
dfunc->vkCmdPipelineBarrier (epacket->cmd, bb[0].srcStages, bb[0].dstStages,
|
||||
0, 0, 0, 1, &bb[2].barrier, 0, 0);
|
||||
QFV_PacketSubmit (gpacket);
|
||||
QFV_PacketSubmit (rpacket);
|
||||
QFV_PacketSubmit (epacket);
|
||||
QFV_DestroyStagingBuffer (stage);
|
||||
|
||||
vec4f_t *bone_data;
|
||||
dfunc->vkMapMemory (device->dev, mesh->bones->memory, 0, VK_WHOLE_SIZE,
|
||||
0, (void **)&bone_data);
|
||||
for (int i = 0; i < 3 * iqm->num_joints; i++) {
|
||||
vec4f_t *bone = bone_data + i * 3;
|
||||
bone[0] = (vec4f_t) {1, 0, 0, 0};
|
||||
bone[1] = (vec4f_t) {0, 1, 0, 0};
|
||||
bone[2] = (vec4f_t) {0, 0, 1, 0};
|
||||
}
|
||||
VkMappedMemoryRange range = {
|
||||
VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, 0,
|
||||
mesh->bones->memory, 0, VK_WHOLE_SIZE,
|
||||
};
|
||||
dfunc->vkFlushMappedMemoryRanges (device->dev, 1, &range);
|
||||
|
||||
dfunc->vkUnmapMemory (device->dev, mesh->bones->memory);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_Mod_IQMFinish (model_t *mod, vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
iqm_t *iqm = (iqm_t *) mod->aliashdr;
|
||||
mod->clear = vulkan_iqm_clear;
|
||||
mod->data = ctx;
|
||||
|
||||
// FIXME assumes only one texture per mesh (currently the case, but
|
||||
// when materials are added...)
|
||||
// 2 is for image + image view
|
||||
int num_objects = 4 + 2 * iqm->num_meshes;
|
||||
qfv_iqm_t *mesh = calloc (1, sizeof (qfv_iqm_t)
|
||||
+ 2 * sizeof (qfv_resource_t)
|
||||
+ num_objects * sizeof (qfv_resobj_t)
|
||||
+ iqm->num_meshes * sizeof (qfv_iqm_skin_t));
|
||||
mesh->bones = (qfv_resource_t *) &mesh[1];
|
||||
mesh->mesh = &mesh->bones[1];
|
||||
|
||||
mesh->bones[0] = (qfv_resource_t) {
|
||||
.name = mod->name,
|
||||
.va_ctx = ctx->va_ctx,
|
||||
.memory_properties = VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
|
||||
.num_objects = 1,
|
||||
.objects = (qfv_resobj_t *) &mesh->bones[2],
|
||||
};
|
||||
mesh->bones->objects[0] = (qfv_resobj_t) {
|
||||
.name = "bones",
|
||||
.type = qfv_res_buffer,
|
||||
.buffer = {
|
||||
.size = 3 * iqm->num_joints * 3 * sizeof (vec4f_t),
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
},
|
||||
};
|
||||
|
||||
mesh->mesh[0] = (qfv_resource_t) {
|
||||
.name = "mesh",
|
||||
.va_ctx = ctx->va_ctx,
|
||||
.memory_properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
.num_objects = num_objects - 1,
|
||||
.objects = mesh->bones->objects + 1,
|
||||
};
|
||||
mesh->mesh->objects[0] = (qfv_resobj_t) {
|
||||
.name = "geom",
|
||||
.type = qfv_res_buffer,
|
||||
.buffer = {
|
||||
.size = iqm->num_verts * sizeof (iqmgvert_t),
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
},
|
||||
};
|
||||
mesh->mesh->objects[1] = (qfv_resobj_t) {
|
||||
.name = "rend",
|
||||
.type = qfv_res_buffer,
|
||||
.buffer = {
|
||||
.size = iqm->num_verts * sizeof (iqmrvert_t),
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
},
|
||||
};
|
||||
mesh->mesh->objects[2] = (qfv_resobj_t) {
|
||||
.name = "index",
|
||||
.type = qfv_res_buffer,
|
||||
.buffer = {
|
||||
.size = iqm->num_elements * sizeof (uint16_t),
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT
|
||||
| VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
},
|
||||
};
|
||||
|
||||
for (int i = 0; i < iqm->num_meshes; i++) {
|
||||
int image_ind = 3 + 2 * i;
|
||||
__auto_type image = &mesh->mesh->objects[image_ind];
|
||||
vulkan_iqm_init_image (iqm, i, image);
|
||||
|
||||
mesh->mesh->objects[image_ind + 1] = (qfv_resobj_t) {
|
||||
.name = "view",
|
||||
.type = qfv_res_image_view,
|
||||
.image_view = {
|
||||
.image = image_ind,
|
||||
.type = VK_IMAGE_VIEW_TYPE_2D,
|
||||
.format = mesh->mesh->objects[image_ind].image.format,
|
||||
.aspect = VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
mesh->skins = (qfv_iqm_skin_t *) &mesh->bones->objects[num_objects];
|
||||
|
||||
QFV_CreateResource (device, mesh->mesh);
|
||||
QFV_CreateResource (device, mesh->bones);
|
||||
mesh->geom_buffer = mesh->mesh->objects[0].buffer.buffer;
|
||||
mesh->rend_buffer = mesh->mesh->objects[1].buffer.buffer;
|
||||
mesh->index_buffer = mesh->mesh->objects[2].buffer.buffer;
|
||||
|
||||
vulkan_iqm_load_textures (mod, iqm, mesh, ctx);
|
||||
vulkan_iqm_load_arrays (mod, iqm, mesh, ctx);
|
||||
|
||||
iqm->extra_data = mesh;
|
||||
}
|
|
@ -91,7 +91,7 @@ libs_video_renderer_librender_gl_la_SOURCES = \
|
|||
shader_src= libs/video/renderer/glsl/quakeforge.glsl
|
||||
shader_gen= libs/video/renderer/glsl/quakeforge.slc
|
||||
|
||||
SUFFICES += .frag .vert .spv .spvc .fc .vc .slc .glsl
|
||||
SUFFIXES += .frag .vert .spv .spvc .fc .vc .slc .glsl
|
||||
.glsl.slc:
|
||||
$(V_SED)sed -e 's/^/"/' -e 's/$$/\\n"/' $< > $@.t &&\
|
||||
$(am__mv) $@.t $@
|
||||
|
@ -219,6 +219,7 @@ libs_video_renderer_librender_vulkan_la_SOURCES = \
|
|||
libs/video/renderer/vulkan/vulkan_bsp.c \
|
||||
libs/video/renderer/vulkan/vulkan_compose.c \
|
||||
libs/video/renderer/vulkan/vulkan_draw.c \
|
||||
libs/video/renderer/vulkan/vulkan_iqm.c \
|
||||
libs/video/renderer/vulkan/vulkan_lighting.c \
|
||||
libs/video/renderer/vulkan/vulkan_main.c \
|
||||
libs/video/renderer/vulkan/vulkan_matrices.c \
|
||||
|
@ -300,6 +301,10 @@ alias_gbuf_src = $(vkshaderpath)/alias_gbuf.frag
|
|||
alias_gbuf_c = $(vkshaderpath)/alias_gbuf.frag.spvc
|
||||
alias_shadow_src = $(vkshaderpath)/alias_shadow.vert
|
||||
alias_shadow_c = $(vkshaderpath)/alias_shadow.vert.spvc
|
||||
iqmv_src = $(vkshaderpath)/iqm.vert
|
||||
iqmv_c = $(vkshaderpath)/iqm.vert.spvc
|
||||
iqmf_src = $(vkshaderpath)/iqm.frag
|
||||
iqmf_c = $(vkshaderpath)/iqm.frag.spvc
|
||||
passthrough_src = $(vkshaderpath)/passthrough.vert
|
||||
passthrough_c = $(vkshaderpath)/passthrough.vert.spvc
|
||||
fstriangle_src = $(vkshaderpath)/fstriangle.vert
|
||||
|
@ -359,6 +364,10 @@ $(alias_gbuf_c): $(alias_gbuf_src)
|
|||
|
||||
$(alias_shadow_c): $(alias_shadow_src)
|
||||
|
||||
$(iqmv_c): $(iqmv_src)
|
||||
|
||||
$(iqmf_c): $(iqmf_src)
|
||||
|
||||
$(passthrough_c): $(passthrough_src)
|
||||
|
||||
$(fstriangle_c): $(fstriangle_src)
|
||||
|
@ -395,6 +404,8 @@ vkshader_c = \
|
|||
$(aliasf_c) \
|
||||
$(alias_gbuf_c) \
|
||||
$(alias_shadow_c) \
|
||||
$(iqmv_c) \
|
||||
$(iqmf_c) \
|
||||
$(passthrough_c) \
|
||||
$(fstriangle_c) \
|
||||
$(pushcolor_c) \
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include "QF/Vulkan/qf_bsp.h"
|
||||
#include "QF/Vulkan/qf_compose.h"
|
||||
#include "QF/Vulkan/qf_draw.h"
|
||||
#include "QF/Vulkan/qf_iqm.h"
|
||||
#include "QF/Vulkan/qf_lighting.h"
|
||||
#include "QF/Vulkan/qf_lightmap.h"
|
||||
#include "QF/Vulkan/qf_main.h"
|
||||
|
@ -560,6 +561,7 @@ vulkan_Mod_LoadExternalSkins (mod_alias_ctx_t *alias_ctx)
|
|||
static void
|
||||
vulkan_Mod_IQMFinish (model_t *mod)
|
||||
{
|
||||
Vulkan_Mod_IQMFinish (mod, vulkan_ctx);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
254
libs/video/renderer/vulkan/iqm.plist
Normal file
254
libs/video/renderer/vulkan/iqm.plist
Normal file
|
@ -0,0 +1,254 @@
|
|||
{
|
||||
setLayouts = {
|
||||
texture_set = {
|
||||
bindings = (
|
||||
{
|
||||
binding = 0;
|
||||
descriptorType = combined_image_sampler;
|
||||
descriptorCount = 1;
|
||||
stageFlags = fragment;
|
||||
},
|
||||
);
|
||||
};
|
||||
};
|
||||
pipelineLayouts = {
|
||||
iqm_layout = {
|
||||
setLayouts = (matrix_set, texture_set);
|
||||
pushConstantRanges = (
|
||||
{
|
||||
stageFlags = vertex;
|
||||
offset = 0;
|
||||
size = "16 * 4 + 4";
|
||||
},
|
||||
{
|
||||
stageFlags = fragment;
|
||||
offset = 68;
|
||||
size = "3 * 4 + 2 * 4 * 4 + 4";
|
||||
},
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
depthStencil = {
|
||||
test_and_write = {
|
||||
depthTestEnable = true;
|
||||
depthWriteEnable = true;
|
||||
depthCompareOp = less_or_equal;
|
||||
depthBoundsTestEnable = false;
|
||||
stencilTestEnable = false;
|
||||
};
|
||||
test_only = {
|
||||
depthTestEnable = true;
|
||||
depthWriteEnable = false;
|
||||
depthCompareOp = less_or_equal;
|
||||
depthBoundsTestEnable = false;
|
||||
stencilTestEnable = false;
|
||||
};
|
||||
disable = {
|
||||
depthTestEnable = false;
|
||||
depthWriteEnable = false;
|
||||
depthCompareOp = less_or_equal;
|
||||
depthBoundsTestEnable = false;
|
||||
stencilTestEnable = false;
|
||||
};
|
||||
};
|
||||
|
||||
inputAssembly = {
|
||||
iqm = {
|
||||
topology = triangle_list;
|
||||
primitiveRestartEnable = false;
|
||||
};
|
||||
};
|
||||
|
||||
vertexInput = {
|
||||
iqm = {
|
||||
bindings = (
|
||||
{ binding = 0; stride = 20; inputRate = vertex; },
|
||||
{ binding = 1; stride = 40; inputRate = vertex; },
|
||||
);
|
||||
attributes = (
|
||||
{ location = 0; binding = 0; format = r32g32b32_sfloat; offset = 0; }, // position
|
||||
{ location = 1; binding = 0; format = r8g8b8a8_uint; offset = 0; }, // bonindices
|
||||
{ location = 2; binding = 0; format = r8g8b8a8_unorm; offset = 4; }, // boneweights
|
||||
|
||||
{ location = 3; binding = 1; format = r32g32_sfloat; offset = 0; }, // texcoord
|
||||
{ location = 4; binding = 1; format = r32g32b32_sfloat; offset = 8; }, // normal
|
||||
{ location = 5; binding = 1; format = r32g32b32a32_sfloat; offset = 20; }, // tangent
|
||||
{ location = 6; binding = 1; format = r8g8b8a8_unorm; offset = 36; }, // color
|
||||
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
rasterization = {
|
||||
cw_cull_back = {
|
||||
depthClampEnable = false;
|
||||
rasterizerDiscardEnable = false;
|
||||
polygonMode = fill;
|
||||
cullMode = back;
|
||||
frontFace = clockwise;
|
||||
depthBiasEnable = false;
|
||||
lineWidth = 1;
|
||||
};
|
||||
counter_cw_cull_back = {
|
||||
depthClampEnable = false;
|
||||
rasterizerDiscardEnable = false;
|
||||
polygonMode = fill;
|
||||
cullMode = back;
|
||||
frontFace = counter_clockwise;
|
||||
depthBiasEnable = false;
|
||||
lineWidth = 1;
|
||||
};
|
||||
};
|
||||
|
||||
multisample = {
|
||||
rasterizationSamples = $msaaSamples;
|
||||
sampleShadingEnable = false;
|
||||
minSampleShading = 0.5f;
|
||||
alphaToCoverageEnable = false;
|
||||
alphaToOneEnable = false;
|
||||
};
|
||||
|
||||
viewport = {
|
||||
viewports = (
|
||||
{
|
||||
x = 0; y = 0;
|
||||
width = 640; height = 480;
|
||||
minDepth = 0; maxDepth = 1;
|
||||
}
|
||||
);
|
||||
scissors = (
|
||||
{
|
||||
offset = { x = 0; y = 0 };
|
||||
extent = { width = 640; height = 480; };
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
attachmentBlendOp = {
|
||||
disabled = {
|
||||
blendEnable = false;
|
||||
srcColorBlendFactor = src_alpha;
|
||||
dstColorBlendFactor = one_minus_src_alpha;
|
||||
colorBlendOp = add;
|
||||
srcAlphaBlendFactor = src_alpha;
|
||||
dstAlphaBlendFactor = one_minus_src_alpha;
|
||||
alphaBlendOp = add;
|
||||
colorWriteMask = r|g|b|a;
|
||||
};
|
||||
alpha_blend = {
|
||||
blendEnable = true;
|
||||
srcColorBlendFactor = src_alpha;
|
||||
dstColorBlendFactor = one_minus_src_alpha;
|
||||
colorBlendOp = add;
|
||||
srcAlphaBlendFactor = src_alpha;
|
||||
dstAlphaBlendFactor = one_minus_src_alpha;
|
||||
alphaBlendOp = add;
|
||||
colorWriteMask = r|g|b|a;
|
||||
};
|
||||
};
|
||||
|
||||
pipelines = {
|
||||
iqm_shadow = {
|
||||
subpass = 0;
|
||||
stages = (
|
||||
{
|
||||
stage = vertex;
|
||||
name = main;
|
||||
module = $builtin/iqm_shadow.vert;
|
||||
},
|
||||
);
|
||||
vertexInput = {
|
||||
bindings = (
|
||||
"$properties.vertexInput.iqm.bindings[0]",
|
||||
"$properties.vertexInput.iqm.bindings[1]",
|
||||
);
|
||||
attributes = (
|
||||
"$properties.vertexInput.iqm.attributes[0]",
|
||||
"$properties.vertexInput.iqm.attributes[1]",
|
||||
"$properties.vertexInput.iqm.attributes[2]",
|
||||
"$properties.vertexInput.iqm.attributes[3]",
|
||||
);
|
||||
};
|
||||
inputAssembly = $properties.inputAssembly.iqm;
|
||||
viewport = $properties.viewport;
|
||||
rasterization = $properties.rasterization.cw_cull_back;
|
||||
multisample = $properties.multisample;
|
||||
depthStencil = $properties.depthStencil.test_and_write;
|
||||
colorBlend = $properties.pipelines.iqm_gbuf.colorBlend;
|
||||
dynamic = {
|
||||
dynamicState = ( viewport, scissor );
|
||||
};
|
||||
layout = iqm_layout;
|
||||
};
|
||||
iqm_depth = {
|
||||
subpass = 0;
|
||||
stages = (
|
||||
{
|
||||
stage = vertex;
|
||||
name = main;
|
||||
module = $builtin/iqm_depth.vert;
|
||||
},
|
||||
);
|
||||
vertexInput = {
|
||||
// depth pass doesn't use UVs
|
||||
bindings = (
|
||||
"$properties.vertexInput.iqm.bindings[0]",
|
||||
"$properties.vertexInput.iqm.bindings[1]",
|
||||
);
|
||||
attributes = (
|
||||
"$properties.vertexInput.iqm.attributes[0]",
|
||||
"$properties.vertexInput.iqm.attributes[1]",
|
||||
"$properties.vertexInput.iqm.attributes[2]",
|
||||
"$properties.vertexInput.iqm.attributes[3]",
|
||||
);
|
||||
};
|
||||
inputAssembly = $properties.inputAssembly.iqm;
|
||||
viewport = $properties.viewport;
|
||||
rasterization = $properties.rasterization.cw_cull_back;
|
||||
multisample = $properties.multisample;
|
||||
depthStencil = $properties.depthStencil.test_and_write;
|
||||
colorBlend = $properties.pipelines.iqm_gbuf.colorBlend;
|
||||
dynamic = {
|
||||
dynamicState = ( viewport, scissor );
|
||||
};
|
||||
layout = iqm_layout;
|
||||
renderPass = renderpass;
|
||||
};
|
||||
iqm_gbuf = {
|
||||
subpass = 2;
|
||||
stages = (
|
||||
{
|
||||
stage = vertex;
|
||||
name = main;
|
||||
module = $builtin/iqm.vert;
|
||||
},
|
||||
{
|
||||
stage = fragment;
|
||||
name = main;
|
||||
module = $builtin/iqm_gbuf.frag;
|
||||
},
|
||||
);
|
||||
vertexInput = $properties.vertexInput.iqm;
|
||||
inputAssembly = $properties.inputAssembly.iqm;
|
||||
viewport = $properties.viewport;
|
||||
rasterization = $properties.rasterization.cw_cull_back;
|
||||
multisample = $properties.multisample;
|
||||
depthStencil = $properties.depthStencil.test_only;
|
||||
colorBlend = {
|
||||
logicOpEnable = false;
|
||||
attachments = (
|
||||
$properties.attachmentBlendOp.disabled,
|
||||
$properties.attachmentBlendOp.disabled,
|
||||
$properties.attachmentBlendOp.disabled,
|
||||
$properties.attachmentBlendOp.disabled,
|
||||
);
|
||||
};
|
||||
dynamic = {
|
||||
dynamicState = ( viewport, scissor, blend_constants );
|
||||
};
|
||||
layout = iqm_layout;
|
||||
renderPass = renderpass;
|
||||
};
|
||||
};
|
||||
}
|
|
@ -93,6 +93,10 @@ static
|
|||
static
|
||||
#include "libs/video/renderer/vulkan/shader/alias_shadow.vert.spvc"
|
||||
static
|
||||
#include "libs/video/renderer/vulkan/shader/iqm.vert.spvc"
|
||||
static
|
||||
#include "libs/video/renderer/vulkan/shader/iqm.frag.spvc"
|
||||
static
|
||||
#include "libs/video/renderer/vulkan/shader/passthrough.vert.spvc"
|
||||
static
|
||||
#include "libs/video/renderer/vulkan/shader/fstriangle.vert.spvc"
|
||||
|
@ -135,6 +139,8 @@ static shaderdata_t builtin_shaders[] = {
|
|||
{ "alias.frag", alias_frag, sizeof (alias_frag) },
|
||||
{ "alias_gbuf.frag", alias_gbuf_frag, sizeof (alias_gbuf_frag) },
|
||||
{ "alias_shadow.vert", alias_shadow_vert, sizeof (alias_shadow_vert) },
|
||||
{ "iqm.vert", iqm_vert, sizeof (iqm_vert) },
|
||||
{ "iqm.frag", iqm_frag, sizeof (iqm_frag) },
|
||||
{ "passthrough.vert", passthrough_vert, sizeof (passthrough_vert) },
|
||||
{ "fstriangle.vert", fstriangle_vert, sizeof (fstriangle_vert) },
|
||||
{ "pushcolor.frag", pushcolor_frag, sizeof (pushcolor_frag) },
|
||||
|
|
44
libs/video/renderer/vulkan/shader/iqm.frag
Normal file
44
libs/video/renderer/vulkan/shader/iqm.frag
Normal file
|
@ -0,0 +1,44 @@
|
|||
#version 450
|
||||
|
||||
layout (set = 1, binding = 0) uniform sampler2DArray Skin;
|
||||
|
||||
layout (push_constant) uniform PushConstants {
|
||||
layout (offset = 68)
|
||||
uint colorA;
|
||||
uint colorB;
|
||||
vec4 base_color;
|
||||
vec4 fog;
|
||||
};
|
||||
|
||||
layout (location = 0) in vec2 texcoord;
|
||||
layout (location = 1) in vec4 position;
|
||||
layout (location = 2) in vec3 normal;
|
||||
layout (location = 3) in vec3 tangent;
|
||||
layout (location = 4) in vec3 bitangent;
|
||||
layout (location = 5) in vec3 color;
|
||||
|
||||
layout (location = 0) out vec4 frag_color;
|
||||
layout (location = 1) out vec4 frag_emission;
|
||||
layout (location = 2) out vec4 frag_normal;
|
||||
layout (location = 3) out vec4 frag_position;
|
||||
|
||||
void
|
||||
main (void)
|
||||
{
|
||||
vec4 c;
|
||||
vec4 e;
|
||||
vec3 n;
|
||||
int i;
|
||||
mat3 tbn = mat3 (tangent, bitangent, normal);
|
||||
|
||||
c = texture (Skin, vec3 (texcoord, 0)) * base_color;
|
||||
c += texture (Skin, vec3 (texcoord, 1)) * unpackUnorm4x8(colorA);
|
||||
c += texture (Skin, vec3 (texcoord, 2)) * unpackUnorm4x8(colorB);
|
||||
e = texture (Skin, vec3 (texcoord, 3));
|
||||
n = texture (Skin, vec3 (texcoord, 4)).xyz * 2 - 1;
|
||||
|
||||
frag_color = c;
|
||||
frag_emission = e;
|
||||
frag_normal = vec4(tbn * n, 1);
|
||||
frag_position = position;
|
||||
}
|
52
libs/video/renderer/vulkan/shader/iqm.vert
Normal file
52
libs/video/renderer/vulkan/shader/iqm.vert
Normal file
|
@ -0,0 +1,52 @@
|
|||
#version 450
|
||||
|
||||
layout (set = 0, binding = 0) uniform Matrices {
|
||||
mat4 Projection3d;
|
||||
mat4 View;
|
||||
mat4 Sky;
|
||||
mat4 Projection2d;
|
||||
};
|
||||
|
||||
layout (set = 3, binding = 0) buffer Bones {
|
||||
// NOTE these are transposed, so v * m
|
||||
mat3x4 bones[];
|
||||
};
|
||||
|
||||
layout (push_constant) uniform PushConstants {
|
||||
mat4 Model;
|
||||
float blend;
|
||||
};
|
||||
|
||||
layout (location = 0) in vec3 vposition;
|
||||
layout (location = 1) in ivec4 vbones;
|
||||
layout (location = 2) in vec4 vweights;
|
||||
layout (location = 3) in vec2 vtexcoord;
|
||||
layout (location = 4) in vec3 vnormal;
|
||||
layout (location = 5) in vec4 vtangent;
|
||||
layout (location = 6) in vec4 vcolor;
|
||||
|
||||
layout (location = 0) out vec2 texcoord;
|
||||
layout (location = 1) out vec4 position;
|
||||
layout (location = 2) out vec3 normal;
|
||||
layout (location = 3) out vec3 tangent;
|
||||
layout (location = 4) out vec3 bitangent;
|
||||
layout (location = 5) out vec4 color;
|
||||
|
||||
void
|
||||
main (void)
|
||||
{
|
||||
mat3x4 m = bones[vbones.x] * vweights.x;
|
||||
m += bones[vbones.y] * vweights.y;
|
||||
m += bones[vbones.z] * vweights.z;
|
||||
m += bones[vbones.w] * vweights.w;
|
||||
vec4 pos = vec4 (Model * vec4(vposition, 1) * m, 1);
|
||||
gl_Position = Projection3d * (View * pos);
|
||||
position = pos;
|
||||
mat3 adjTrans = mat3 (cross(m[1].xyz, m[2].xyz), cross(m[2].xyz, m[0].xyz),
|
||||
cross(m[0].xyz, m[1].xyz));
|
||||
normal = mat3 (Model) * vnormal * adjTrans;
|
||||
tangent = mat3 (Model) * vtangent.xyz * adjTrans;
|
||||
bitangent = cross (normal, tangent) * vtangent.w;
|
||||
texcoord = vtexcoord;
|
||||
color = vcolor;
|
||||
}
|
310
libs/video/renderer/vulkan/vulkan_iqm.c
Normal file
310
libs/video/renderer/vulkan/vulkan_iqm.c
Normal file
|
@ -0,0 +1,310 @@
|
|||
/*
|
||||
vulkan_iqm.c
|
||||
|
||||
Vulkan IQM model pipeline
|
||||
|
||||
Copyright (C) 2022 Bill Currie <bill@taniwha.org>
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2022/5/3
|
||||
|
||||
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_iqm.h"
|
||||
#include "QF/Vulkan/qf_matrices.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/renderpass.h"
|
||||
|
||||
#include "r_internal.h"
|
||||
#include "vid_vulkan.h"
|
||||
|
||||
typedef struct {
|
||||
mat4f_t mat;
|
||||
float blend;
|
||||
byte colorA[4];
|
||||
byte colorB[4];
|
||||
vec4f_t base_color;
|
||||
vec4f_t fog;
|
||||
} iqm_push_constants_t;
|
||||
|
||||
static const char * __attribute__((used)) iqm_pass_names[] = {
|
||||
"depth",
|
||||
"g-buffer",
|
||||
"translucent",
|
||||
};
|
||||
|
||||
static QFV_Subpass subpass_map[] = {
|
||||
QFV_passDepth, // QFV_iqmDepth
|
||||
QFV_passGBuffer, // QFV_iqmGBuffer
|
||||
QFV_passTranslucent, // QFV_iqmTranslucent
|
||||
};
|
||||
|
||||
static void
|
||||
emit_commands (VkCommandBuffer cmd, int pose1, int pose2,
|
||||
qfv_iqm_skin_t **skins,
|
||||
uint32_t numPC, qfv_push_constants_t *constants,
|
||||
iqm_t *iqm, qfv_renderframe_t *rFrame)
|
||||
{
|
||||
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
||||
qfv_device_t *device = ctx->device;
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
|
||||
__auto_type mesh = (qfv_iqm_t *) iqm->extra_data;
|
||||
|
||||
VkDeviceSize offsets[] = { 0, 0, };
|
||||
VkBuffer buffers[] = {
|
||||
mesh->geom_buffer,
|
||||
mesh->rend_buffer,
|
||||
};
|
||||
int bindingCount = skins ? 2 : 1;
|
||||
|
||||
dfunc->vkCmdBindVertexBuffers (cmd, 0, bindingCount, buffers, offsets);
|
||||
dfunc->vkCmdBindIndexBuffer (cmd, mesh->index_buffer, 0,
|
||||
VK_INDEX_TYPE_UINT32);
|
||||
QFV_PushConstants (device, cmd, ictx->layout, numPC, constants);
|
||||
for (int i = 0; i < iqm->num_meshes; i++) {
|
||||
if (skins) {
|
||||
VkDescriptorSet sets[] = {
|
||||
skins[i]->descriptor,
|
||||
};
|
||||
dfunc->vkCmdBindDescriptorSets (cmd,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ictx->layout, 1, 1, sets, 0, 0);
|
||||
}
|
||||
dfunc->vkCmdDrawIndexed (cmd, 3 * iqm->meshes[i].num_triangles, 1,
|
||||
3 * iqm->meshes[i].first_triangle, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_DrawIQM (entity_t *ent, qfv_renderframe_t *rFrame)
|
||||
{
|
||||
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
iqm_frame_t *aframe = &ictx->frames.a[ctx->curFrame];
|
||||
model_t *model = ent->renderer.model;
|
||||
iqm_t *iqm = (iqm_t *) model->aliashdr;
|
||||
qfv_iqm_t *mesh = iqm->extra_data;
|
||||
qfv_iqm_skin_t **skins = &mesh->skins;
|
||||
iqm_push_constants_t constants = {};
|
||||
|
||||
constants.blend = R_IQMGetLerpedFrames (ent, iqm);
|
||||
|
||||
qfv_push_constants_t push_constants[] = {
|
||||
{ VK_SHADER_STAGE_VERTEX_BIT,
|
||||
field_offset (iqm_push_constants_t, mat),
|
||||
sizeof (mat4f_t), Transform_GetWorldMatrixPtr (ent->transform) },
|
||||
{ VK_SHADER_STAGE_VERTEX_BIT,
|
||||
field_offset (iqm_push_constants_t, blend),
|
||||
sizeof (float), &constants.blend },
|
||||
{ VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
field_offset (iqm_push_constants_t, colorA),
|
||||
sizeof (constants.colorA), constants.colorA },
|
||||
{ VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
field_offset (iqm_push_constants_t, colorB),
|
||||
sizeof (constants.colorB), constants.colorB },
|
||||
{ VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
field_offset (iqm_push_constants_t, base_color),
|
||||
sizeof (constants.base_color), &constants.base_color },
|
||||
{ VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
field_offset (iqm_push_constants_t, fog),
|
||||
sizeof (constants.fog), &constants.fog },
|
||||
};
|
||||
|
||||
QuatCopy (ent->renderer.colormod, constants.base_color);
|
||||
QuatCopy (skins[0]->colora, constants.colorA);
|
||||
QuatCopy (skins[0]->colorb, constants.colorB);
|
||||
QuatZero (constants.fog);
|
||||
|
||||
emit_commands (aframe->cmdSet.a[QFV_iqmDepth],
|
||||
ent->animation.pose1, ent->animation.pose2,
|
||||
0, 2, push_constants, iqm, rFrame);
|
||||
emit_commands (aframe->cmdSet.a[QFV_iqmGBuffer],
|
||||
ent->animation.pose1, ent->animation.pose2,
|
||||
skins, 6, push_constants, iqm, rFrame);
|
||||
}
|
||||
|
||||
static void
|
||||
alias_begin_subpass (QFV_IQMSubpass subpass, VkPipeline pipeline,
|
||||
qfv_renderframe_t *rFrame)
|
||||
{
|
||||
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
||||
qfv_device_t *device = ctx->device;
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
__auto_type cframe = &ctx->frames.a[ctx->curFrame];
|
||||
iqm_frame_t *aframe = &ictx->frames.a[ctx->curFrame];
|
||||
VkCommandBuffer cmd = aframe->cmdSet.a[subpass];
|
||||
|
||||
dfunc->vkResetCommandBuffer (cmd, 0);
|
||||
VkCommandBufferInheritanceInfo inherit = {
|
||||
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, 0,
|
||||
rFrame->renderpass->renderpass, subpass_map[subpass],
|
||||
cframe->framebuffer,
|
||||
0, 0, 0,
|
||||
};
|
||||
VkCommandBufferBeginInfo beginInfo = {
|
||||
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 0,
|
||||
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
|
||||
| VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &inherit,
|
||||
};
|
||||
dfunc->vkBeginCommandBuffer (cmd, &beginInfo);
|
||||
|
||||
QFV_duCmdBeginLabel (device, cmd, va (ctx->va_ctx, "iqm:%s",
|
||||
iqm_pass_names[subpass]),
|
||||
{ 0.6, 0.5, 0, 1});
|
||||
|
||||
dfunc->vkCmdBindPipeline (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
VkDescriptorSet sets[] = {
|
||||
Vulkan_Matrix_Descriptors (ctx, ctx->curFrame),
|
||||
};
|
||||
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ictx->layout, 0, 1, sets, 0, 0);
|
||||
dfunc->vkCmdSetViewport (cmd, 0, 1, &rFrame->renderpass->viewport);
|
||||
dfunc->vkCmdSetScissor (cmd, 0, 1, &rFrame->renderpass->scissor);
|
||||
|
||||
//XXX glsl_Fog_GetColor (fog);
|
||||
//XXX fog[3] = glsl_Fog_GetDensity () / 64.0;
|
||||
}
|
||||
|
||||
static void
|
||||
alias_end_subpass (VkCommandBuffer cmd, vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
|
||||
QFV_duCmdEndLabel (device, cmd);
|
||||
dfunc->vkEndCommandBuffer (cmd);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQMBegin (qfv_renderframe_t *rFrame)
|
||||
{
|
||||
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
iqm_frame_t *aframe = &ictx->frames.a[ctx->curFrame];
|
||||
|
||||
//XXX quat_t fog;
|
||||
DARRAY_APPEND (&rFrame->subpassCmdSets[QFV_passDepth],
|
||||
aframe->cmdSet.a[QFV_iqmDepth]);
|
||||
DARRAY_APPEND (&rFrame->subpassCmdSets[QFV_passGBuffer],
|
||||
aframe->cmdSet.a[QFV_iqmGBuffer]);
|
||||
|
||||
alias_begin_subpass (QFV_iqmDepth, ictx->depth, rFrame);
|
||||
alias_begin_subpass (QFV_iqmGBuffer, ictx->gbuf, rFrame);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQMEnd (qfv_renderframe_t *rFrame)
|
||||
{
|
||||
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
iqm_frame_t *aframe = &ictx->frames.a[ctx->curFrame];
|
||||
|
||||
alias_end_subpass (aframe->cmdSet.a[QFV_iqmDepth], ctx);
|
||||
alias_end_subpass (aframe->cmdSet.a[QFV_iqmGBuffer], ctx);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQMAddSkin (vulkan_ctx_t *ctx, qfv_iqm_skin_t *skin)
|
||||
{
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
skin->descriptor = Vulkan_CreateCombinedImageSampler (ctx, skin->view,
|
||||
ictx->sampler);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQMRemoveSkin (vulkan_ctx_t *ctx, qfv_iqm_skin_t *skin)
|
||||
{
|
||||
Vulkan_FreeTexture (ctx, skin->descriptor);
|
||||
skin->descriptor = 0;
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQM_Init (vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
|
||||
qfvPushDebug (ctx, "iqm init");
|
||||
|
||||
iqmctx_t *ictx = calloc (1, sizeof (iqmctx_t));
|
||||
ctx->iqm_context = ictx;
|
||||
|
||||
size_t frames = ctx->frames.size;
|
||||
DARRAY_INIT (&ictx->frames, frames);
|
||||
DARRAY_RESIZE (&ictx->frames, frames);
|
||||
ictx->frames.grow = 0;
|
||||
|
||||
ictx->depth = Vulkan_CreateGraphicsPipeline (ctx, "alias_depth");
|
||||
ictx->gbuf = Vulkan_CreateGraphicsPipeline (ctx, "alias_gbuf");
|
||||
ictx->layout = Vulkan_CreatePipelineLayout (ctx, "alias_layout");
|
||||
ictx->sampler = Vulkan_CreateSampler (ctx, "alias_sampler");
|
||||
|
||||
for (size_t i = 0; i < frames; i++) {
|
||||
__auto_type aframe = &ictx->frames.a[i];
|
||||
|
||||
DARRAY_INIT (&aframe->cmdSet, QFV_iqmNumPasses);
|
||||
DARRAY_RESIZE (&aframe->cmdSet, QFV_iqmNumPasses);
|
||||
aframe->cmdSet.grow = 0;
|
||||
|
||||
QFV_AllocateCommandBuffers (device, ctx->cmdpool, 1, &aframe->cmdSet);
|
||||
|
||||
for (int j = 0; j < QFV_iqmNumPasses; j++) {
|
||||
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER,
|
||||
aframe->cmdSet.a[j],
|
||||
va (ctx->va_ctx, "cmd:iqm:%zd:%s", i,
|
||||
iqm_pass_names[j]));
|
||||
}
|
||||
}
|
||||
qfvPopDebug (ctx);
|
||||
}
|
||||
|
||||
void
|
||||
Vulkan_IQM_Shutdown (vulkan_ctx_t *ctx)
|
||||
{
|
||||
qfv_device_t *device = ctx->device;
|
||||
qfv_devfuncs_t *dfunc = device->funcs;
|
||||
iqmctx_t *ictx = ctx->iqm_context;
|
||||
|
||||
for (size_t i = 0; i < ictx->frames.size; i++) {
|
||||
__auto_type aframe = &ictx->frames.a[i];
|
||||
free (aframe->cmdSet.a);
|
||||
}
|
||||
|
||||
dfunc->vkDestroyPipeline (device->dev, ictx->depth, 0);
|
||||
dfunc->vkDestroyPipeline (device->dev, ictx->gbuf, 0);
|
||||
free (ictx->frames.a);
|
||||
free (ictx);
|
||||
}
|
Loading…
Reference in a new issue