draw static map geometry with debug shader

there are lots of glitches and no textures, but this is a first step!
This commit is contained in:
Ivan Avdeev 2021-01-23 19:38:00 -08:00
parent 4949f6e29e
commit 5d0146e17a
12 changed files with 641 additions and 21 deletions

View File

@ -1,9 +1,13 @@
#!/bin/bash
set -eux
build() {
NAME="$1"
glslc -o "build-debug-amd64/lib/xash3d/valve/$NAME.spv" "ref_vk/shaders/$NAME"
}
build 2d.frag
build 2d.vert
for s in 2d.frag 2d.vert map.vert map.frag
do
build "$s"
done

9
ref_vk/shaders/map.frag Normal file
View File

@ -0,0 +1,9 @@
#version 450
layout(location=0) in vec3 vPos;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(abs(fract(vPos/10.)), 1.);
}

17
ref_vk/shaders/map.vert Normal file
View File

@ -0,0 +1,17 @@
#version 450
layout(set=0,binding=0) uniform UBO {
mat4 worldview;
mat4 projection;
mat4 vkfixup;
mat4 mvp;
} ubo;
layout(location=0) in vec3 aPos;
layout(location=0) out vec3 vPos;
void main() {
vPos = aPos.xyz;
gl_Position = transpose(ubo.vkfixup) * transpose(ubo.mvp) * vec4(aPos.xyz, 1.);
}

View File

@ -116,19 +116,10 @@ void vk2dBegin( void )
void vk2dEnd( void )
{
const VkDeviceSize offset = 0;
const VkViewport viewport[] = {
{0.f, 0.f, (float)vk_frame.surface_caps.currentExtent.width, (float)vk_frame.surface_caps.currentExtent.height, 0.f, 1.f},
};
const VkRect2D scissor[] = {{
{0, 0},
vk_frame.surface_caps.currentExtent,
}};
if (!g2d.num_pics)
return;
vkCmdSetViewport(vk_core.cb, 0, ARRAYSIZE(viewport), viewport);
vkCmdSetScissor(vk_core.cb, 0, ARRAYSIZE(scissor), scissor);
vkCmdBindVertexBuffers(vk_core.cb, 0, 1, &g2d.pics_buffer.buffer, &offset);
for (int i = 0; i <= g2d.current_batch; ++i)
@ -155,9 +146,6 @@ static qboolean createPipelines( void )
VkDescriptorSetLayout descriptor_layouts[] = {
vk_core.descriptor_pool.one_texture_layout,
/* g.descriptors[Descriptors_Global]->layout, */
/* g.descriptors[Descriptors_Lightmaps]->layout, */
/* g.descriptors[Descriptors_Textures]->layout, */
};
VkPipelineLayoutCreateInfo plci = {

View File

@ -410,10 +410,10 @@ static qboolean initDescriptorPool( void )
{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = MAX_TEXTURES,
/*
}, {
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
/*
}, {
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = 1,
@ -429,7 +429,7 @@ static qboolean initDescriptorPool( void )
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pPoolSizes = dps,
.poolSizeCount = ARRAYSIZE(dps),
.maxSets = MAX_TEXTURES,
.maxSets = MAX_TEXTURES + 1,
};
XVK_CHECK(vkCreateDescriptorPool(vk_core.device, &dpci, NULL, &vk_core.descriptor_pool.pool));
@ -464,6 +464,36 @@ static qboolean initDescriptorPool( void )
Mem_Free(tmp_layouts);
}
{
const int num_sets = ARRAYSIZE(vk_core.descriptor_pool.ubo_sets);
// ... TODO find better place for this; this should be per-pipeline/shader
VkDescriptorSetLayoutBinding bindings[] = { {
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
}};
VkDescriptorSetLayoutCreateInfo dslci = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = ARRAYSIZE(bindings),
.pBindings = bindings,
};
VkDescriptorSetLayout* tmp_layouts = Mem_Malloc(vk_core.pool, sizeof(VkDescriptorSetLayout) * num_sets);
VkDescriptorSetAllocateInfo dsai = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = vk_core.descriptor_pool.pool,
.descriptorSetCount = num_sets,
.pSetLayouts = tmp_layouts,
};
XVK_CHECK(vkCreateDescriptorSetLayout(vk_core.device, &dslci, NULL, &vk_core.descriptor_pool.one_uniform_buffer_layout));
for (int i = 0; i < num_sets; ++i)
tmp_layouts[i] = vk_core.descriptor_pool.one_uniform_buffer_layout;
XVK_CHECK(vkAllocateDescriptorSets(vk_core.device, &dsai, vk_core.descriptor_pool.ubo_sets));
Mem_Free(tmp_layouts);
}
return true;
}
@ -572,6 +602,7 @@ void R_VkShutdown( void )
vkDestroyDescriptorPool(vk_core.device, vk_core.descriptor_pool.pool, NULL);
vkDestroyDescriptorSetLayout(vk_core.device, vk_core.descriptor_pool.one_texture_layout, NULL);
vkDestroyDescriptorSetLayout(vk_core.device, vk_core.descriptor_pool.one_uniform_buffer_layout, NULL);
vkDestroySampler(vk_core.device, vk_core.default_sampler, NULL);
destroyBuffer(&vk_core.staging);

View File

@ -50,8 +50,13 @@ typedef struct descriptor_pool_s
VkDescriptorPool pool;
int next_free;
//uint32_t *free_set;
VkDescriptorSet sets[MAX_DESC_SETS];
VkDescriptorSetLayout one_texture_layout;
// FIXME HOW THE F
VkDescriptorSet ubo_sets[1];
VkDescriptorSetLayout one_uniform_buffer_layout;
} descriptor_pool_t;
typedef struct vulkan_core_s {
@ -179,6 +184,9 @@ const char *resultName(VkResult result);
X(vkDestroyDescriptorSetLayout) \
X(vkCmdSetViewport) \
X(vkCmdSetScissor) \
X(vkCmdUpdateBuffer) \
X(vkCmdBindIndexBuffer) \
X(vkCmdDrawIndexed) \
#define X(f) extern PFN_##f f;
DEVICE_FUNCS(X)

View File

@ -99,8 +99,8 @@ static qboolean createRenderPass( void ) {
VkAttachmentDescription attachments[] = {{
.format = VK_FORMAT_B8G8R8A8_UNORM, //SRGB,// FIXME too early swapchain.create_info.imageFormat;
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
//.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
//.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
@ -271,6 +271,7 @@ void R_BeginFrame( qboolean clearScene )
void GL_RenderFrame( const struct ref_viewpass_s *rvp )
{
gEngine.Con_Printf(S_WARN "VK FIXME: %s\n", __FUNCTION__);
FIXME_VK_MapSetViewPass(rvp);
}
void R_EndFrame( void )
@ -326,6 +327,21 @@ void R_EndFrame( void )
vkCmdBeginRenderPass(vk_core.cb, &rpbi, VK_SUBPASS_CONTENTS_INLINE);
{
const VkViewport viewport[] = {
{0.f, 0.f, (float)vk_frame.surface_caps.currentExtent.width, (float)vk_frame.surface_caps.currentExtent.height, 0.f, 1.f},
};
const VkRect2D scissor[] = {{
{0, 0},
vk_frame.surface_caps.currentExtent,
}};
vkCmdSetViewport(vk_core.cb, 0, ARRAYSIZE(viewport), viewport);
vkCmdSetScissor(vk_core.cb, 0, ARRAYSIZE(scissor), scissor);
}
VK_MapRender();
vk2dEnd();
vkCmdEndRenderPass(vk_core.cb);

View File

@ -1,5 +1,16 @@
#include "vk_map.h"
#include "vk_core.h"
#include "vk_buffer.h"
#include "vk_pipeline.h"
#include "vk_framectl.h"
#include "vk_math.h"
#include "ref_params.h"
#include "eiface.h"
#include <math.h>
#include <memory.h>
typedef struct map_vertex_s
{
@ -15,8 +26,89 @@ static struct {
vk_buffer_t index_buffer;
uint32_t num_indices;
vk_buffer_t uniform_buffer;
VkPipelineLayout pipeline_layout;
VkPipeline pipeline;
} gmap;
typedef struct {
matrix4x4 worldview;
matrix4x4 projection;
matrix4x4 vkfixup;
matrix4x4 mvp;
} uniform_data_t;
static qboolean createPipelines( void )
{
/* VkPushConstantRange push_const = { */
/* .offset = 0, */
/* .size = sizeof(AVec3f), */
/* .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, */
/* }; */
VkDescriptorSetLayout descriptor_layouts[] = {
vk_core.descriptor_pool.one_uniform_buffer_layout,
};
VkPipelineLayoutCreateInfo plci = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = ARRAYSIZE(descriptor_layouts),
.pSetLayouts = descriptor_layouts,
/* .pushConstantRangeCount = 1, */
/* .pPushConstantRanges = &push_const, */
};
// FIXME store layout separately
XVK_CHECK(vkCreatePipelineLayout(vk_core.device, &plci, NULL, &gmap.pipeline_layout));
{
VkVertexInputAttributeDescription attribs[] = {
{.binding = 0, .location = 0, .format = VK_FORMAT_R32G32B32_SFLOAT, .offset = offsetof(map_vertex_t, pos)},
};
VkPipelineShaderStageCreateInfo shader_stages[] = {
{
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = loadShader("map.vert.spv"),
.pName = "main",
}, {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.module = loadShader("map.frag.spv"),
.pName = "main",
}};
vk_pipeline_create_info_t ci = {
.layout = gmap.pipeline_layout,
.attribs = attribs,
.num_attribs = ARRAYSIZE(attribs),
.stages = shader_stages,
.num_stages = ARRAYSIZE(shader_stages),
.vertex_stride = sizeof(map_vertex_t),
.depthTestEnable = VK_TRUE,
.depthWriteEnable = VK_TRUE,
.depthCompareOp = VK_COMPARE_OP_LESS,
.blendEnable = VK_FALSE,
};
gmap.pipeline = createPipeline(&ci);
if (!gmap.pipeline)
return false;
for (int i = 0; i < (int)ARRAYSIZE(shader_stages); ++i)
vkDestroyShaderModule(vk_core.device, shader_stages[i].module, NULL);
}
return true;
}
qboolean VK_MapInit( void )
{
const uint32_t vertex_buffer_size = MAX_MAP_VERTS * sizeof(map_vertex_t);
@ -30,13 +122,41 @@ qboolean VK_MapInit( void )
if (!createBuffer(&gmap.index_buffer, index_buffer_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
return false;
if (!createBuffer(&gmap.uniform_buffer, sizeof(uniform_data_t), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
return false;
if (!createPipelines())
return false;
{
VkDescriptorBufferInfo dbi = {
.buffer = gmap.uniform_buffer.buffer,
.offset = 0,
.range = VK_WHOLE_SIZE,
};
VkWriteDescriptorSet wds[] = { {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &dbi,
.dstSet = vk_core.descriptor_pool.ubo_sets[0], // FIXME
}};
vkUpdateDescriptorSets(vk_core.device, ARRAYSIZE(wds), wds, 0, NULL);
}
return true;
}
void VK_MapShutdown( void )
{
vkDestroyPipeline( vk_core.device, gmap.pipeline, NULL );
vkDestroyPipelineLayout( vk_core.device, gmap.pipeline_layout, NULL );
destroyBuffer( &gmap.vertex_buffer );
destroyBuffer( &gmap.index_buffer );
destroyBuffer( &gmap.uniform_buffer );
}
// tell the renderer what new map is started
@ -46,7 +166,7 @@ void R_NewMap( void )
const model_t *world = gEngine.pfnGetModelByIndex(1);
map_vertex_t *bvert = gmap.vertex_buffer.mapped;
uint32_t *bind = gmap.index_buffer.mapped;
uint16_t *bind = gmap.index_buffer.mapped;
// Free previous map data
gmap.num_vertices = 0;
@ -74,6 +194,8 @@ void R_NewMap( void )
const medge_t *edge = world->edges + (iedge >= 0 ? iedge : -iedge);
const mvertex_t *vertex = world->vertexes + (iedge >= 0 ? edge->v[0] : edge->v[1]);
//gEngine.Con_Printf("VERT %u %f %f %f\n", gmap.num_vertices, vertex->position[0], vertex->position[1], vertex->position[2]);
bvert[gmap.num_vertices++] = (map_vertex_t){
{vertex->position[0], vertex->position[1], vertex->position[2]}
};
@ -83,8 +205,9 @@ void R_NewMap( void )
ASSERT(gmap.num_indices < (MAX_MAP_VERTS * 3 - 3));
ASSERT(first_vertex_index + k < UINT16_MAX);
bind[gmap.num_indices++] = (uint16_t)(first_vertex_index + 0);
bind[gmap.num_indices++] = (uint16_t)(first_vertex_index + k);
bind[gmap.num_indices++] = (uint16_t)(first_vertex_index + k - 1);
bind[gmap.num_indices++] = (uint16_t)(first_vertex_index + k);
//gEngine.Con_Printf("INDX %u %d %d %d\n", gmap.num_indices, (int)bind[gmap.num_indices-3], (int)bind[gmap.num_indices-2], (int)bind[gmap.num_indices-1]);
}
}
}
@ -92,6 +215,137 @@ void R_NewMap( void )
gEngine.Con_Reportf("Loaded surfaces: %d, vertices: %u\n, indices: %u", world->numsurfaces, gmap.num_vertices, gmap.num_indices);
}
// FIXME this is a total garbage. pls avoid adding even more weird local static state
static ref_viewpass_t fixme_rvp;
void FIXME_VK_MapSetViewPass( const struct ref_viewpass_s *rvp )
{
fixme_rvp = *rvp;
}
static float R_GetFarClip( void )
{
/* FIXME
if( WORLDMODEL && RI.drawWorld )
return MOVEVARS->zmax * 1.73f;
*/
return 2048.0f;
}
#define max( a, b ) (((a) > (b)) ? (a) : (b))
#define min( a, b ) (((a) < (b)) ? (a) : (b))
static void R_SetupProjectionMatrix( matrix4x4 m )
{
float xMin, xMax, yMin, yMax, zNear, zFar;
/*
if( RI.drawOrtho )
{
const ref_overview_t *ov = gEngfuncs.GetOverviewParms();
Matrix4x4_CreateOrtho( m, ov->xLeft, ov->xRight, ov->yTop, ov->yBottom, ov->zNear, ov->zFar );
return;
}
*/
const float farClip = R_GetFarClip();
zNear = 4.0f;
zFar = max( 256.0f, farClip );
yMax = zNear * tan( fixme_rvp.fov_y * M_PI_F / 360.0f );
yMin = -yMax;
xMax = zNear * tan( fixme_rvp.fov_x * M_PI_F / 360.0f );
xMin = -xMax;
Matrix4x4_CreateProjection( m, xMax, xMin, yMax, yMin, zNear, zFar );
}
static void R_SetupModelviewMatrix( matrix4x4 m )
{
Matrix4x4_CreateModelview( m );
Matrix4x4_ConcatRotate( m, -fixme_rvp.viewangles[2], 1, 0, 0 );
Matrix4x4_ConcatRotate( m, -fixme_rvp.viewangles[0], 0, 1, 0 );
Matrix4x4_ConcatRotate( m, -fixme_rvp.viewangles[1], 0, 0, 1 );
Matrix4x4_ConcatTranslate( m, -fixme_rvp.vieworigin[0], -fixme_rvp.vieworigin[1], -fixme_rvp.vieworigin[2] );
}
void VK_MapRender( void )
{
{
uniform_data_t *ubo = gmap.uniform_buffer.mapped;
matrix4x4 worldview={0}, projection={0}, mvp={0}, tmp={0};
//uniform_data_t uniform_data = {0};
// Vulkan has Y pointing down, and z should end up in (0, 1)
const matrix4x4 vk_proj_fixup = {
{1, 0, 0, 0},
{0, -1, 0, 0},
{0, 0, .5, 0},
{0, 0, .5, 1}
};
R_SetupModelviewMatrix( worldview );
R_SetupProjectionMatrix( projection );
memcpy(&ubo->worldview, worldview, sizeof(matrix4x4));
memcpy(&ubo->projection, projection, sizeof(matrix4x4));
Matrix4x4_Concat( mvp, projection, worldview);
memcpy(&ubo->mvp, mvp, sizeof(matrix4x4));
memcpy(&ubo->vkfixup, vk_proj_fixup, sizeof(matrix4x4));
//Matrix4x4_Concat( mvp, projection, worldview);
//Matrix4x4_Concat( mvp, tmp, worldview );
//Matrix4x4_Concat( tmp, vk_proj_fixup, mvp);
//memcpy(gmap.uniform_buffer.mapped, tmp, sizeof(tmp));
//memcpy(gmap.uniform_buffer.mapped, mvp, sizeof(tmp));
//memcpy(gmap.uniform_buffer.mapped, tmp, sizeof(tmp));
/*
vkCmdUpdateBuffer(vk_core.cb, gmap.uniform_buffer.buffer, 0, sizeof(uniform_data), &uniform_data);
VkMemoryBarrier mem_barrier = {
.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
};
vkCmdPipelineBarrier(vk_core.cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
VK_DEPENDENCY_DEVICE_GROUP_BIT, 1, &mem_barrier, 0, NULL, 0, NULL);
*/
}
/* TODO
if( RP_NORMALPASS( ))
{
int x, x2, y, y2;
// set up viewport (main, playersetup)
x = floor( RI.viewport[0] * gpGlobals->width / gpGlobals->width );
x2 = ceil(( RI.viewport[0] + RI.viewport[2] ) * gpGlobals->width / gpGlobals->width );
y = floor( gpGlobals->height - RI.viewport[1] * gpGlobals->height / gpGlobals->height );
y2 = ceil( gpGlobals->height - ( RI.viewport[1] + RI.viewport[3] ) * gpGlobals->height / gpGlobals->height );
pglViewport( x, y2, x2 - x, y - y2 );
}
else
{
// envpass, mirrorpass
pglViewport( RI.viewport[0], RI.viewport[1], RI.viewport[2], RI.viewport[3] );
}
*/
// ...
{
const VkDeviceSize offset = 0;
vkCmdBindPipeline(vk_core.cb, VK_PIPELINE_BIND_POINT_GRAPHICS, gmap.pipeline);
vkCmdBindVertexBuffers(vk_core.cb, 0, 1, &gmap.vertex_buffer.buffer, &offset);
vkCmdBindIndexBuffer(vk_core.cb, gmap.index_buffer.buffer, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindDescriptorSets(vk_core.cb, VK_PIPELINE_BIND_POINT_GRAPHICS, gmap.pipeline_layout, 0, 1, vk_core.descriptor_pool.ubo_sets, 0, NULL);
vkCmdDrawIndexed(vk_core.cb, gmap.num_indices, 1, 0, 0, 0);
}
}
void R_RenderScene( void )
{
gEngine.Con_Printf(S_WARN "VK FIXME: %s\n", __FUNCTION__);

View File

@ -2,8 +2,12 @@
#include "xash3d_types.h"
struct ref_viewpass_s;
qboolean VK_MapInit( void );
void VK_MapShutdown( void );
void FIXME_VK_MapSetViewPass( const struct ref_viewpass_s *rvp );
void VK_MapRender( void );
void R_NewMap( void );
void R_RenderScene( void );

268
ref_vk/vk_math.c Normal file
View File

@ -0,0 +1,268 @@
/*
gl_rmath.c - renderer mathlib
Copyright (C) 2010 Uncle Mike
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 3 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.
*/
#include "vk_math.h"
#include <memory.h>
/*
========================================================================
Matrix4x4 operations (private to renderer)
========================================================================
*/
void Matrix4x4_Concat( matrix4x4 out, const matrix4x4 in1, const matrix4x4 in2 )
{
out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0] + in1[0][3] * in2[3][0];
out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1] + in1[0][3] * in2[3][1];
out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2] + in1[0][3] * in2[3][2];
out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3] * in2[3][3];
out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0] + in1[1][3] * in2[3][0];
out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1] + in1[1][3] * in2[3][1];
out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2] + in1[1][3] * in2[3][2];
out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3] * in2[3][3];
out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0] + in1[2][3] * in2[3][0];
out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1] + in1[2][3] * in2[3][1];
out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2] + in1[2][3] * in2[3][2];
out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3] * in2[3][3];
out[3][0] = in1[3][0] * in2[0][0] + in1[3][1] * in2[1][0] + in1[3][2] * in2[2][0] + in1[3][3] * in2[3][0];
out[3][1] = in1[3][0] * in2[0][1] + in1[3][1] * in2[1][1] + in1[3][2] * in2[2][1] + in1[3][3] * in2[3][1];
out[3][2] = in1[3][0] * in2[0][2] + in1[3][1] * in2[1][2] + in1[3][2] * in2[2][2] + in1[3][3] * in2[3][2];
out[3][3] = in1[3][0] * in2[0][3] + in1[3][1] * in2[1][3] + in1[3][2] * in2[2][3] + in1[3][3] * in2[3][3];
}
/*
================
Matrix4x4_CreateProjection
NOTE: produce quake style world orientation
================
*/
void Matrix4x4_CreateProjection( matrix4x4 out, float xMax, float xMin, float yMax, float yMin, float zNear, float zFar )
{
out[0][0] = ( 2.0f * zNear ) / ( xMax - xMin );
out[1][1] = ( 2.0f * zNear ) / ( yMax - yMin );
out[2][2] = -( zFar + zNear ) / ( zFar - zNear );
out[3][3] = out[0][1] = out[1][0] = out[3][0] = out[0][3] = out[3][1] = out[1][3] = 0.0f;
out[2][0] = 0.0f;
out[2][1] = 0.0f;
out[0][2] = ( xMax + xMin ) / ( xMax - xMin );
out[1][2] = ( yMax + yMin ) / ( yMax - yMin );
out[3][2] = -1.0f;
out[2][3] = -( 2.0f * zFar * zNear ) / ( zFar - zNear );
}
void Matrix4x4_CreateOrtho( matrix4x4 out, float xLeft, float xRight, float yBottom, float yTop, float zNear, float zFar )
{
out[0][0] = 2.0f / (xRight - xLeft);
out[1][1] = 2.0f / (yTop - yBottom);
out[2][2] = -2.0f / (zFar - zNear);
out[3][3] = 1.0f;
out[0][1] = out[0][2] = out[1][0] = out[1][2] = out[3][0] = out[3][1] = out[3][2] = 0.0f;
out[2][0] = 0.0f;
out[2][1] = 0.0f;
out[0][3] = -(xRight + xLeft) / (xRight - xLeft);
out[1][3] = -(yTop + yBottom) / (yTop - yBottom);
out[2][3] = -(zFar + zNear) / (zFar - zNear);
}
/*
================
Matrix4x4_CreateModelview
NOTE: produce quake style world orientation
================
*/
void Matrix4x4_CreateModelview( matrix4x4 out )
{
out[0][0] = out[1][1] = out[2][2] = 0.0f;
out[3][0] = out[0][3] = 0.0f;
out[3][1] = out[1][3] = 0.0f;
out[3][2] = out[2][3] = 0.0f;
out[3][3] = 1.0f;
out[1][0] = out[0][2] = out[2][1] = 0.0f;
out[2][0] = out[0][1] = -1.0f;
out[1][2] = 1.0f;
}
void Matrix4x4_ToArrayFloatGL( const matrix4x4 in, float out[16] )
{
out[ 0] = in[0][0];
out[ 1] = in[1][0];
out[ 2] = in[2][0];
out[ 3] = in[3][0];
out[ 4] = in[0][1];
out[ 5] = in[1][1];
out[ 6] = in[2][1];
out[ 7] = in[3][1];
out[ 8] = in[0][2];
out[ 9] = in[1][2];
out[10] = in[2][2];
out[11] = in[3][2];
out[12] = in[0][3];
out[13] = in[1][3];
out[14] = in[2][3];
out[15] = in[3][3];
}
void Matrix4x4_FromArrayFloatGL( matrix4x4 out, const float in[16] )
{
out[0][0] = in[0];
out[1][0] = in[1];
out[2][0] = in[2];
out[3][0] = in[3];
out[0][1] = in[4];
out[1][1] = in[5];
out[2][1] = in[6];
out[3][1] = in[7];
out[0][2] = in[8];
out[1][2] = in[9];
out[2][2] = in[10];
out[3][2] = in[11];
out[0][3] = in[12];
out[1][3] = in[13];
out[2][3] = in[14];
out[3][3] = in[15];
}
void Matrix4x4_CreateTranslate( matrix4x4 out, float x, float y, float z )
{
out[0][0] = 1.0f;
out[0][1] = 0.0f;
out[0][2] = 0.0f;
out[0][3] = x;
out[1][0] = 0.0f;
out[1][1] = 1.0f;
out[1][2] = 0.0f;
out[1][3] = y;
out[2][0] = 0.0f;
out[2][1] = 0.0f;
out[2][2] = 1.0f;
out[2][3] = z;
out[3][0] = 0.0f;
out[3][1] = 0.0f;
out[3][2] = 0.0f;
out[3][3] = 1.0f;
}
void Matrix4x4_CreateRotate( matrix4x4 out, float angle, float x, float y, float z )
{
float len, c, s;
len = x * x + y * y + z * z;
if( len != 0.0f ) len = 1.0f / sqrt( len );
x *= len;
y *= len;
z *= len;
angle *= (-M_PI_F / 180.0f);
SinCos( angle, &s, &c );
out[0][0]=x * x + c * (1 - x * x);
out[0][1]=x * y * (1 - c) + z * s;
out[0][2]=z * x * (1 - c) - y * s;
out[0][3]=0.0f;
out[1][0]=x * y * (1 - c) - z * s;
out[1][1]=y * y + c * (1 - y * y);
out[1][2]=y * z * (1 - c) + x * s;
out[1][3]=0.0f;
out[2][0]=z * x * (1 - c) + y * s;
out[2][1]=y * z * (1 - c) - x * s;
out[2][2]=z * z + c * (1 - z * z);
out[2][3]=0.0f;
out[3][0]=0.0f;
out[3][1]=0.0f;
out[3][2]=0.0f;
out[3][3]=1.0f;
}
void Matrix4x4_CreateScale( matrix4x4 out, float x )
{
out[0][0] = x;
out[0][1] = 0.0f;
out[0][2] = 0.0f;
out[0][3] = 0.0f;
out[1][0] = 0.0f;
out[1][1] = x;
out[1][2] = 0.0f;
out[1][3] = 0.0f;
out[2][0] = 0.0f;
out[2][1] = 0.0f;
out[2][2] = x;
out[2][3] = 0.0f;
out[3][0] = 0.0f;
out[3][1] = 0.0f;
out[3][2] = 0.0f;
out[3][3] = 1.0f;
}
void Matrix4x4_CreateScale3( matrix4x4 out, float x, float y, float z )
{
out[0][0] = x;
out[0][1] = 0.0f;
out[0][2] = 0.0f;
out[0][3] = 0.0f;
out[1][0] = 0.0f;
out[1][1] = y;
out[1][2] = 0.0f;
out[1][3] = 0.0f;
out[2][0] = 0.0f;
out[2][1] = 0.0f;
out[2][2] = z;
out[2][3] = 0.0f;
out[3][0] = 0.0f;
out[3][1] = 0.0f;
out[3][2] = 0.0f;
out[3][3] = 1.0f;
}
void Matrix4x4_ConcatTranslate( matrix4x4 out, float x, float y, float z )
{
matrix4x4 base, temp;
Matrix4x4_Copy( base, out );
Matrix4x4_CreateTranslate( temp, x, y, z );
Matrix4x4_Concat( out, base, temp );
}
void Matrix4x4_ConcatRotate( matrix4x4 out, float angle, float x, float y, float z )
{
matrix4x4 base, temp;
Matrix4x4_Copy( base, out );
Matrix4x4_CreateRotate( temp, angle, x, y, z );
Matrix4x4_Concat( out, base, temp );
}
void Matrix4x4_ConcatScale( matrix4x4 out, float x )
{
matrix4x4 base, temp;
Matrix4x4_Copy( base, out );
Matrix4x4_CreateScale( temp, x );
Matrix4x4_Concat( out, base, temp );
}
void Matrix4x4_ConcatScale3( matrix4x4 out, float x, float y, float z )
{
matrix4x4 base, temp;
Matrix4x4_Copy( base, out );
Matrix4x4_CreateScale3( temp, x, y, z );
Matrix4x4_Concat( out, base, temp );
}

21
ref_vk/vk_math.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "xash3d_types.h"
#include "const.h"
#include "com_model.h"
#include "xash3d_mathlib.h"
void Matrix4x4_ToArrayFloatGL( const matrix4x4 in, float out[16] );
void Matrix4x4_FromArrayFloatGL( matrix4x4 out, const float in[16] );
void Matrix4x4_Concat( matrix4x4 out, const matrix4x4 in1, const matrix4x4 in2 );
void Matrix4x4_ConcatTranslate( matrix4x4 out, float x, float y, float z );
void Matrix4x4_ConcatRotate( matrix4x4 out, float angle, float x, float y, float z );
void Matrix4x4_ConcatScale( matrix4x4 out, float x );
void Matrix4x4_ConcatScale3( matrix4x4 out, float x, float y, float z );
void Matrix4x4_CreateTranslate( matrix4x4 out, float x, float y, float z );
void Matrix4x4_CreateRotate( matrix4x4 out, float angle, float x, float y, float z );
void Matrix4x4_CreateScale( matrix4x4 out, float x );
void Matrix4x4_CreateScale3( matrix4x4 out, float x, float y, float z );
void Matrix4x4_CreateProjection(matrix4x4 out, float xMax, float xMin, float yMax, float yMin, float zNear, float zFar);
void Matrix4x4_CreateOrtho(matrix4x4 m, float xLeft, float xRight, float yBottom, float yTop, float zNear, float zFar);
void Matrix4x4_CreateModelview( matrix4x4 out );

View File

@ -34,7 +34,7 @@ VkPipeline createPipeline(const vk_pipeline_create_info_t *ci)
VkPipelineRasterizationStateCreateInfo raster_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.polygonMode = VK_POLYGON_MODE_FILL,
.cullMode = VK_CULL_MODE_BACK_BIT,
.cullMode = VK_CULL_MODE_NONE,// BACK_BIT,
.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
.lineWidth = 1.f,
};