[vulkan] Move projection matrix code to its own file

And move more stuff over to simd.
This commit is contained in:
Bill Currie 2021-04-25 15:47:50 +09:00
parent 8b6136e6f5
commit c86f0c9449
10 changed files with 135 additions and 88 deletions

View file

@ -148,6 +148,7 @@ include_qf_vulkan = \
include/QF/Vulkan/instance.h \
include/QF/Vulkan/memory.h \
include/QF/Vulkan/pipeline.h \
include/QF/Vulkan/projection.h \
include/QF/Vulkan/qf_alias.h \
include/QF/Vulkan/qf_bsp.h \
include/QF/Vulkan/qf_compose.h \

View file

@ -0,0 +1,10 @@
#ifndef __QF_Vulkan_projection_h
#define __QF_Vulkan_projection_h
#include "QF/simd/types.h"
void QFV_Orthographic (mat4f_t proj, float xmin, float xmax,
float ymin, float ymax, float znear, float zfar);
void QFV_Perspective (mat4f_t proj, float fov, float aspect);
#endif//__QF_Vulkan_projection_h

View file

@ -37,6 +37,8 @@
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/command.h"
#include "QF/simd/types.h"
typedef struct bspvert_s {
quat_t vertex;
quat_t tlst;
@ -129,9 +131,9 @@ typedef struct bspctx_s {
struct qfv_tex_s *default_skybox;
struct qfv_tex_s *skybox_tex;
quat_t sky_rotation[2];
quat_t sky_velocity;
quat_t sky_fix;
vec4f_t sky_rotation[2];
vec4f_t sky_velocity;
vec4f_t sky_fix;
double sky_time;
quat_t default_color;

View file

@ -35,6 +35,7 @@
#include "QF/modelgen.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/command.h"
#include "QF/Vulkan/image.h"
#include "QF/simd/types.h"
typedef struct qfv_light_s {
@ -49,6 +50,7 @@ typedef struct qfv_light_s {
typedef struct qfv_lightset_s DARRAY_TYPE (qfv_light_t) qfv_lightset_t;
typedef struct qfv_lightleafset_s DARRAY_TYPE (int) qfv_lightleafset_t;
typedef struct qfv_lightvisset_s DARRAY_TYPE (byte) qfv_lightvisset_t;
typedef struct qfv_lightmatset_s DARRAY_TYPE (mat4f_t) qfv_lightmatset_t;
#define NUM_LIGHTS 256
#define NUM_STYLES 64
@ -100,6 +102,8 @@ typedef struct lightingctx_s {
VkDeviceMemory light_memory;
qfv_lightset_t lights;
qfv_lightleafset_t lightleafs;
qfv_lightmatset_t lightmats;
//qfv_imageviewset_t lightviews;
} lightingctx_t;
struct vulkan_ctx_s;

View file

@ -7,6 +7,7 @@
#include <vulkan/vulkan.h>
#include "QF/darray.h"
#include "QF/simd/types.h"
typedef struct vulkan_frame_s {
VkFramebuffer framebuffer;
@ -23,10 +24,10 @@ typedef struct vulkan_matrices_s {
VkBuffer buffer_2d;
VkBuffer buffer_3d;
VkDeviceMemory memory;
float *projection_2d;
float *projection_3d;
float *view_3d;
float *sky_3d;
vec4f_t *projection_2d;
vec4f_t *projection_3d;
vec4f_t *view_3d;
vec4f_t *sky_3d;
} vulkan_matrices_t;
typedef struct vulkan_frameset_s

View file

@ -257,6 +257,7 @@ libs_video_renderer_librender_vulkan_la_SOURCES = \
libs/video/renderer/vulkan/instance.c \
libs/video/renderer/vulkan/memory.c \
libs/video/renderer/vulkan/pipeline.c \
libs/video/renderer/vulkan/projection.c \
libs/video/renderer/vulkan/renderpass.c \
libs/video/renderer/vulkan/scrap.c \
libs/video/renderer/vulkan/shader.c \

View file

@ -0,0 +1,83 @@
/*
proejct.c
Vulkan projection matrices
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
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_MATH_H
# include <math.h>
#endif
#include "QF/cvar.h"
#include "QF/Vulkan/projection.h"
#include "r_internal.h"
void
QFV_Orthographic (mat4f_t proj, float xmin, float xmax, float ymin, float ymax,
float znear, float zfar)
{
proj[0] = (vec4f_t) {
2 / (xmax - xmin),
0,
0,
0
};
proj[1] = (vec4f_t) {
0,
2 / (ymax - ymin),
0,
0
};
proj[2] = (vec4f_t) {
0,
0,
1 / (znear - zfar),
0
};
proj[3] = (vec4f_t) {
-(xmax + xmin) / (xmax - xmin),
-(ymax + ymin) / (ymax - ymin),
znear / (znear - zfar),
1,
};
}
void
QFV_Perspective (mat4f_t proj, float fov, float aspect)
{
float f = 1 / tan (fov * M_PI / 360);
float neard, fard;
neard = r_nearclip->value;
fard = r_farclip->value;
proj[0] = (vec4f_t) { f / aspect, 0, 0, 0 };
proj[1] = (vec4f_t) { 0, -f, 0, 0 };
proj[2] = (vec4f_t) { 0, 0, fard / (neard - fard), -1 };
proj[3] = (vec4f_t) { 0, 0, (neard * fard) / (neard - fard), 0 };
}

View file

@ -422,11 +422,11 @@ Vulkan_BuildDisplayLists (model_t **models, int num_models, vulkan_ctx_t *ctx)
bsppoly_t *poly;
mod_brush_t *brush;
QuatSet (0, 0, sqrt(0.5), sqrt(0.5), bctx->sky_fix); // proper skies
QuatSet (0, 0, 0, 1, bctx->sky_rotation[0]);
QuatCopy (bctx->sky_rotation[0], bctx->sky_rotation[1]);
QuatSet (0, 0, 0, 0, bctx->sky_velocity);
QuatExp (bctx->sky_velocity, bctx->sky_velocity);
bctx->sky_fix = (vec4f_t) { 0, 0, 1, 1 } * sqrtf (0.5);
bctx->sky_rotation[0] = (vec4f_t) { 0, 0, 0, 1};
bctx->sky_rotation[1] = bctx->sky_rotation[0];
bctx->sky_velocity = (vec4f_t) { };
bctx->sky_velocity = qexpf (bctx->sky_velocity);
bctx->sky_time = vr_data.realtime;
// now run through all surfaces, chaining them to their textures, thus
@ -995,26 +995,26 @@ turb_end (vulkan_ctx_t *ctx)
}
static void
spin (mat4_t mat, bspctx_t *bctx)
spin (mat4f_t mat, bspctx_t *bctx)
{
quat_t q;
mat4_t m;
vec4f_t q;
mat4f_t m;
float blend;
while (vr_data.realtime - bctx->sky_time > 1) {
QuatCopy (bctx->sky_rotation[1], bctx->sky_rotation[0]);
QuatMult (bctx->sky_velocity, bctx->sky_rotation[0],
bctx->sky_rotation[1]);
bctx->sky_rotation[0] = bctx->sky_rotation[1];
bctx->sky_rotation[1] = qmulf (bctx->sky_velocity,
bctx->sky_rotation[0]);
bctx->sky_time += 1;
}
blend = bound (0, (vr_data.realtime - bctx->sky_time), 1);
QuatBlend (bctx->sky_rotation[0], bctx->sky_rotation[1], blend, q);
QuatMult (bctx->sky_fix, q, q);
Mat4Identity (mat);
VectorNegate (r_origin, mat + 12);
QuatToMatrix (q, m, 1, 1);
Mat4Mult (m, mat, mat);
q = Blend (bctx->sky_rotation[0], bctx->sky_rotation[1], blend);
q = normalf (qmulf (bctx->sky_fix, q));
mat4fidentity (mat);
VectorNegate (r_origin, mat[3]);
mat4fquat (m, q);
mmulf (mat, m, mat);
}
static void

View file

@ -284,6 +284,7 @@ Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
DARRAY_INIT (&lctx->lights, 16);
DARRAY_INIT (&lctx->lightleafs, 16);
DARRAY_INIT (&lctx->lightmats, 16);
size_t frames = ctx->frames.size;
DARRAY_INIT (&lctx->frames, frames);
@ -410,6 +411,7 @@ Vulkan_Lighting_Shutdown (vulkan_ctx_t *ctx)
dfunc->vkDestroyPipeline (device->dev, lctx->pipeline, 0);
DARRAY_CLEAR (&lctx->lights);
DARRAY_CLEAR (&lctx->lightleafs);
DARRAY_CLEAR (&lctx->lightmats);
free (lctx->frames.a);
free (lctx);
}
@ -606,6 +608,7 @@ Vulkan_LoadLights (model_t *model, const char *entity_data, vulkan_ctx_t *ctx)
lctx->lights.size = 0;
lctx->lightleafs.size = 0;
lctx->lightmats.size = 0;
script_t *script = Script_New ();
Script_Start (script, "ent data", entity_data);

View file

@ -32,16 +32,13 @@
# include <math.h>
#endif
#include "QF/cvar.h"
#include "QF/mathlib.h"
#include "QF/sys.h"
#include "QF/vid.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/buffer.h"
#include "QF/Vulkan/device.h"
#include "QF/Vulkan/projection.h"
#include "compat.h"
#include "d_iface.h"
#include "r_internal.h"
#include "vid_vulkan.h"
@ -49,61 +46,6 @@
#define MAT_SIZE (16 * sizeof (float))
static void
ortho_mat (float *proj, float xmin, float xmax, float ymin, float ymax,
float znear, float zfar)
{
proj[0] = 2 / (xmax - xmin);
proj[4] = 0;
proj[8] = 0;
proj[12] = -(xmax + xmin) / (xmax - xmin);
proj[1] = 0;
proj[5] = 2 / (ymax - ymin);
proj[9] = 0;
proj[13] = -(ymax + ymin) / (ymax - ymin);
proj[2] = 0;
proj[6] = 0;
proj[10] = 1 / (znear - zfar);
proj[14] = znear / (znear - zfar);
proj[3] = 0;
proj[7] = 0;
proj[11] = 0;
proj[15] = 1;
}
static void
persp_mat (float *proj, float fov, float aspect)
{
float f = 1 / tan (fov * M_PI / 360);
float neard, fard;
neard = r_nearclip->value;
fard = r_farclip->value;
proj[0] = f / aspect;
proj[4] = 0;
proj[8] = 0;
proj[12] = 0;
proj[1] = 0;
proj[5] = -f;
proj[9] = 0;
proj[13] = 0;
proj[2] = 0;
proj[6] = 0;
proj[10] = fard / (neard - fard);
proj[14] = (neard * fard) / (neard - fard);
proj[3] = 0;
proj[7] = 0;
proj[11] = -1;
proj[15] = 0;
}
void
Vulkan_DestroyMatrices (vulkan_ctx_t *ctx)
{
@ -150,9 +92,9 @@ Vulkan_CreateMatrices (vulkan_ctx_t *ctx)
void *data;
dfunc->vkMapMemory (device->dev, mat->memory, 0, size, 0, &data);
mat->projection_2d = data;
mat->projection_3d = mat->projection_2d + offset / sizeof (float);
mat->view_3d = mat->projection_3d + 16;
mat->sky_3d = mat->view_3d + 16;
mat->projection_3d = (vec4f_t *) ((byte *) mat->projection_2d + offset);
mat->view_3d = mat->projection_3d + 4;
mat->sky_3d = mat->view_3d + 4;
}
void
@ -165,10 +107,10 @@ Vulkan_CalcProjectionMatrices (vulkan_ctx_t *ctx)
int width = vid.conwidth;
int height = vid.conheight;
ortho_mat (mat->projection_2d, 0, width, 0, height, -99999, 99999);
QFV_Orthographic (mat->projection_2d, 0, width, 0, height, -99999, 99999);
float aspect = (float) r_refdef.vrect.width / r_refdef.vrect.height;
persp_mat (mat->projection_3d, r_refdef.fov_y, aspect);
QFV_Perspective (mat->projection_3d, r_refdef.fov_y, aspect);
#if 0
Sys_MaskPrintf (SYS_vulkan, "ortho:\n");
Sys_MaskPrintf (SYS_vulkan, " [[%g, %g, %g, %g],\n",