Merge branch 'vulkan' into better-sampling

This commit is contained in:
Ivan Avdeev 2022-01-06 13:34:43 -08:00
commit 56c0a3273e
8 changed files with 176 additions and 10 deletions

View File

@ -63,6 +63,7 @@ GNU General Public License for more details.
#define PARM_TEX_MEMORY 38 // returns total memory of uploaded texture in bytes
#define PARM_DELUXEDATA 39 // nasty hack, convert int to pointer
#define PARM_SHADOWDATA 40 // nasty hack, convert int to pointer
#define PARM_MODERNFLASHLIGHT 41 // new dynamic flashlight, initially for Vulkan render
// skybox ordering
enum

View File

@ -2696,11 +2696,14 @@ void CL_AddEntityEffects( cl_entity_t *ent )
if( FBitSet( ent->curstate.effects, EF_BRIGHTFIELD ))
R_EntityParticles( ent );
if( FBitSet( ent->curstate.effects, EF_DIMLIGHT ))
if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT ))
{
if( ent->player && !Host_IsQuakeCompatible( ))
if ( ent->player && !Host_IsQuakeCompatible( ))
{
CL_UpdateFlashlight( ent );
if ( !REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1))
{
CL_UpdateFlashlight( ent );
}
}
else
{

View File

@ -440,6 +440,30 @@ static int enumerateDevices( vk_available_device_t **available_devices ) {
return this_device - *available_devices;
}
static void devicePrintMemoryInfo(const VkPhysicalDeviceMemoryProperties *props, const VkPhysicalDeviceMemoryBudgetPropertiesEXT *budget) {
gEngine.Con_Printf("Memory heaps: %d\n", props->memoryHeapCount);
for (int i = 0; i < (int)props->memoryHeapCount; ++i) {
const VkMemoryHeap* const heap = props->memoryHeaps + i;
gEngine.Con_Printf(" %d: size=%dMb used=%dMb avail=%dMb device_local=%d\n", i,
(int)(heap->size / (1024 * 1024)),
(int)(budget->heapUsage[i] / (1024 * 1024)),
(int)(budget->heapBudget[i] / (1024 * 1024)),
!!(heap->flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT));
}
gEngine.Con_Printf("Memory types: %d\n", props->memoryTypeCount);
for (int i = 0; i < (int)props->memoryTypeCount; ++i) {
const VkMemoryType* const type = props->memoryTypes + i;
gEngine.Con_Printf(" %d: heap=%d flags=%c%c%c%c%c\n", i, type->heapIndex,
type->propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? 'D' : '.',
type->propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? 'V' : '.',
type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 'C' : '.',
type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT ? '$' : '.',
type->propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT ? 'L' : '.'
);
}
}
static qboolean createDevice( void ) {
void *head = NULL;
vk_available_device_t *available_devices;
@ -531,13 +555,20 @@ static qboolean createDevice( void ) {
.ppEnabledExtensionNames = device_extensions,
};
// FIXME do only once
vkGetPhysicalDeviceMemoryProperties(candidate_device->device, &vk_core.physical_device.memory_properties);
{
vk_core.physical_device.memory_properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
vk_core.physical_device.memory_properties2.pNext = &vk_core.physical_device.memory_budget;
vk_core.physical_device.memory_budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
vk_core.physical_device.memory_budget.pNext = NULL;
vkGetPhysicalDeviceMemoryProperties2(candidate_device->device, &vk_core.physical_device.memory_properties2);
}
gEngine.Con_Printf("Trying device #%d: %04x:%04x %d %s %u.%u.%u %u.%u.%u\n",
i, candidate_device->props.vendorID, candidate_device->props.deviceID, candidate_device->props.deviceType, candidate_device->props.deviceName,
XVK_PARSE_VERSION(candidate_device->props.driverVersion), XVK_PARSE_VERSION(candidate_device->props.apiVersion));
devicePrintMemoryInfo(&vk_core.physical_device.memory_properties2.memoryProperties, &vk_core.physical_device.memory_budget);
{
const VkResult result = vkCreateDevice(candidate_device->device, &create_info, NULL, &vk_core.device);
if (result != VK_SUCCESS) {
@ -873,11 +904,11 @@ void destroyFence(VkFence fence) {
}
static uint32_t findMemoryWithType(uint32_t type_index_bits, VkMemoryPropertyFlags flags) {
for (uint32_t i = 0; i < vk_core.physical_device.memory_properties.memoryTypeCount; ++i) {
for (uint32_t i = 0; i < vk_core.physical_device.memory_properties2.memoryProperties.memoryTypeCount; ++i) {
if (!(type_index_bits & (1 << i)))
continue;
if ((vk_core.physical_device.memory_properties.memoryTypes[i].propertyFlags & flags) == flags)
if ((vk_core.physical_device.memory_properties2.memoryProperties.memoryTypes[i].propertyFlags & flags) == flags)
return i;
}
@ -900,6 +931,15 @@ device_memory_t allocateDeviceMemory(VkMemoryRequirements req, VkMemoryPropertyF
.memoryTypeIndex = findMemoryWithType(req.memoryTypeBits, props),
};
gEngine.Con_Reportf("allocateDeviceMemory size=%zu memoryTypeBits=0x%x memoryProperties=%c%c%c%c%c flags=0x%x => typeIndex=%d\n", req.size, req.memoryTypeBits,
props & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? 'D' : '.',
props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? 'V' : '.',
props & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 'C' : '.',
props & VK_MEMORY_PROPERTY_HOST_CACHED_BIT ? '$' : '.',
props & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT ? 'L' : '.',
flags,
mai.memoryTypeIndex);
ASSERT(mai.memoryTypeIndex != UINT32_MAX);
XVK_CHECK(vkAllocateMemory(vk_core.device, &mai, NULL, &ret.device_memory));
return ret;

View File

@ -38,7 +38,8 @@ typedef struct vk_buffer_s
typedef struct physical_device_s {
VkPhysicalDevice device;
VkPhysicalDeviceMemoryProperties memory_properties;
VkPhysicalDeviceMemoryProperties2 memory_properties2;
VkPhysicalDeviceMemoryBudgetPropertiesEXT memory_budget;
VkPhysicalDeviceProperties properties;
VkPhysicalDeviceProperties2 properties2;
VkPhysicalDeviceAccelerationStructurePropertiesKHR properties_accel;
@ -133,7 +134,7 @@ do { \
X(vkGetPhysicalDeviceFeatures2) \
X(vkGetPhysicalDeviceQueueFamilyProperties) \
X(vkGetPhysicalDeviceSurfaceSupportKHR) \
X(vkGetPhysicalDeviceMemoryProperties) \
X(vkGetPhysicalDeviceMemoryProperties2) \
X(vkGetPhysicalDeviceSurfacePresentModesKHR) \
X(vkGetPhysicalDeviceSurfaceFormatsKHR) \
X(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \

View File

@ -15,6 +15,10 @@
#include <string.h>
#include <ctype.h> // isalnum...
#include "camera.h"
#include "pm_defs.h"
#include "pmtrace.h"
#define PROFILER_SCOPES(X) \
X(finalize , "VK_LightsFrameFinalize"); \
X(emissive_surface, "VK_LightsAddEmissiveSurface"); \
@ -836,6 +840,101 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty
return index;
}
void R_LightAddFlashlight(const struct cl_entity_s *ent, qboolean local_player ) {
// parameters
const float hack_attenuation = 0.1;
float radius = 1.0;
// TODO: better tune it
const float _cone = 10.0;
const float _cone2 = 30.0;
const vec3_t light_color = {255, 255, 192};
float light_intensity = 300;
vec3_t color;
vec3_t origin;
vec3_t angles;
vk_light_entity_t le;
float thirdperson_offset = 25;
vec3_t forward, view_ofs;
vec3_t vecSrc, vecEnd;
pmtrace_t *trace;
if( local_player )
{
// local player case
// position
if (gEngine.EngineGetParm(PARM_THIRDPERSON, 0)) { // thirdperson
AngleVectors( g_camera.viewangles, forward, NULL, NULL );
view_ofs[0] = view_ofs[1] = 0.0f;
if( ent->curstate.usehull == 1 ) {
view_ofs[2] = 12.0f; // VEC_DUCK_VIEW;
} else {
view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT
}
VectorAdd( ent->origin, view_ofs, vecSrc );
VectorMA( vecSrc, thirdperson_offset, forward, vecEnd );
trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX );
VectorCopy( trace->endpos, origin );
VectorCopy( forward, le.dir);
} else { // firstperson
// based on https://github.com/SNMetamorph/PrimeXT/blob/0869b1abbddd13c1229769d8cd71941610be0bf3/client/flashlight.cpp#L35
origin[0] = g_camera.vieworg[0] + (g_camera.vright[0] * (-4.0f)) + (g_camera.vforward[0] * 14.0); // forward-back
origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * (-4.0f)) + (g_camera.vforward[1] * 14.0); // left-right
origin[2] = g_camera.vieworg[2] + (g_camera.vright[2] * (-4.0f)) + (g_camera.vforward[2] * 14.0); // up-down
origin[2] += 2.0f;
VectorCopy(g_camera.vforward, le.dir);
}
}
else // non-local player case
{
thirdperson_offset = 10;
radius = 10;
light_intensity = 60;
VectorCopy( ent->angles, angles );
// NOTE: pitch divided by 3.0 twice. So we need apply 3^2 = 9
angles[PITCH] = ent->curstate.angles[PITCH] * 9.0f;
angles[YAW] = ent->angles[YAW];
angles[ROLL] = 0.0f; // roll not used
AngleVectors( angles, angles, NULL, NULL );
view_ofs[0] = view_ofs[1] = 0.0f;
if( ent->curstate.usehull == 1 ) {
view_ofs[2] = 12.0f; // VEC_DUCK_VIEW;
} else {
view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT
}
VectorAdd( ent->origin, view_ofs, vecSrc );
VectorMA( vecSrc, thirdperson_offset, angles, vecEnd );
trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX );
VectorCopy( trace->endpos, origin );
VectorCopy( angles, le.dir );
}
VectorCopy(origin, le.origin);
// prepare colors by parseEntPropRgbav
VectorScale(light_color, light_intensity / 255.0f, color);
// convert colors by weirdGoldsrcLightScaling
float l1 = Q_max(color[0], Q_max(color[1], color[2]));
l1 = l1 * l1 / 10;
VectorScale(color, l1, le.color);
// convert stopdots by parseStopDot
le.stopdot = cosf(_cone * M_PI / 180.f);
le.stopdot2 = cosf(_cone2 * M_PI / 180.f);
/*
gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f) dir=(%f %f %f)\n",
le.origin[0], le.origin[1], le.origin[2],
le.color[0], le.color[1], le.color[2],
le.dir[0], le.dir[1], le.dir[2]);
*/
addSpotLight(&le, radius, 0, hack_attenuation, false);
}
static float sphereSolidAngleFromDistDiv2Pi(float r, float d) {
return 1. - sqrt(d*d - r*r)/d;
}

View File

@ -92,3 +92,6 @@ void XVK_GetEmissiveForTexture( vec3_t out, int texture_id );
void VK_LightsFrameFinalize( void );
int R_LightCellIndex( const int light_cell[3] );
struct cl_entity_s;
void R_LightAddFlashlight( const struct cl_entity_s *ent, qboolean local_player );

View File

@ -1,4 +1,5 @@
#include "vk_core.h"
#include "vk_cvar.h"
#include "vk_common.h"
#include "vk_textures.h"
#include "vk_renderstate.h"
@ -228,6 +229,7 @@ static const char *getParmName(int parm)
case PARM_TEX_MEMORY: return "PARM_TEX_MEMORY";
case PARM_DELUXEDATA: return "PARM_DELUXEDATA";
case PARM_SHADOWDATA: return "PARM_SHADOWDATA";
case PARM_MODERNFLASHLIGHT: return "PARM_MODERNFLASHLIGHT";
default: return "UNKNOWN";
}
}
@ -248,6 +250,11 @@ static int VK_RefGetParm( int parm, int arg )
case PARM_TEX_FLAGS:
tex = findTexture(arg);
return tex->flags;
case PARM_MODERNFLASHLIGHT:
if (CVAR_TO_BOOL( vk_rtx )) {
return true;
}
return false;
}
PRINT_NOT_IMPLEMENTED_ARGS("(%s(%d), %d)", getParmName(parm), parm, arg);

View File

@ -596,7 +596,7 @@ static void drawEntity( cl_entity_t *ent, int render_mode )
static float g_frametime = 0;
void VK_SceneRender( const ref_viewpass_t *rvp ) {
int current_pipeline_index = kRenderNormal;
const cl_entity_t* const local_player = gEngine.GetLocalPlayer();
g_frametime = /*FIXME VK RP_NORMALPASS( )) ? */
gpGlobals->time - gpGlobals->oldtime
@ -629,11 +629,23 @@ void VK_SceneRender( const ref_viewpass_t *rvp ) {
}
}
{
// Draw flashlight for local player
if( FBitSet( local_player->curstate.effects, EF_DIMLIGHT )) {
R_LightAddFlashlight(local_player, true);
}
}
// Draw opaque entities
for (int i = 0; i < g_lists.draw_list->num_solid_entities; ++i)
{
cl_entity_t *ent = g_lists.draw_list->solid_entities[i];
drawEntity(ent, kRenderNormal);
// Draw flashlight for other players
if( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) && ent != local_player) {
R_LightAddFlashlight(ent, false);
}
}
// Draw opaque beams