rt: use proper staging for light uploading

also don't upload clusters as we're thinking about sunsetting them in their current form.
This commit is contained in:
Ivan 'provod' Avdeev 2022-08-15 09:20:42 -07:00 committed by Ivan Avdeev
parent 41e5757c0a
commit 7f3dca0993
4 changed files with 52 additions and 46 deletions

View File

@ -116,11 +116,14 @@ void computePointLights(vec3 P, vec3 N, uint cluster_index, vec3 throughput, vec
void computeLighting(vec3 P, vec3 N, vec3 throughput, vec3 view_dir, MaterialProperties material, out vec3 diffuse, out vec3 specular) {
diffuse = specular = vec3(0.);
const ivec3 light_cell = ivec3(floor(P / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min;
const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y)));
#ifdef USE_CLUSTERS
if (any(greaterThanEqual(light_cell, light_grid.grid_size)) || cluster_index >= MAX_LIGHT_CLUSTERS)
return; // throughput * vec3(1., 0., 0.);
#endif
//diffuse = specular = vec3(1.);
//return;

View File

@ -8,6 +8,7 @@
#include "shaders/ray_interop.h"
#include "bitarray.h"
#include "profiler.h"
#include "vk_staging.h"
#include "mod_local.h"
#include "xash3d_mathlib.h"
@ -52,7 +53,6 @@ static struct {
vk_emissive_texture_t emissive_textures[MAX_TEXTURES];
} map;
vk_buffer_t staging;
vk_buffer_t buffer;
int num_polygons;
@ -102,17 +102,10 @@ qboolean VK_LightsInit( void ) {
return false;
}
if (!VK_BufferCreate("rt lights staging buffer", &g_lights_.staging, sizeof(struct Lights),
VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
// FIXME complain, handle
return false;
}
return true;
}
void VK_LightsShutdown( void ) {
VK_BufferDestroy(&g_lights_.staging);
VK_BufferDestroy(&g_lights_.buffer);
gEngine.Cmd_RemoveCommand("vk_lights_dump");
@ -1147,11 +1140,7 @@ static void uploadGrid( vk_ray_shader_light_grid_t *grid ) {
}
}
vk_lights_bindings_t VK_LightsUpload( VkCommandBuffer cmdbuf ) {
// Upload polygon lights
struct Lights *lights = g_lights_.staging.mapped;
struct LightsMetadata *metadata = &lights->metadata;
static void uploadPolygonLights( struct LightsMetadata *metadata ) {
ASSERT(g_lights_.num_polygons <= MAX_EMISSIVE_KUSOCHKI);
metadata->num_polygons = g_lights_.num_polygons;
for (int i = 0; i < g_lights_.num_polygons; ++i) {
@ -1173,6 +1162,14 @@ vk_lights_bindings_t VK_LightsUpload( VkCommandBuffer cmdbuf ) {
dst_poly->vertices_count_offset = (src_poly->vertices.count << 16) | (src_poly->vertices.offset);
}
// TODO static assert
ASSERT(sizeof(metadata->polygon_vertices) >= sizeof(g_lights_.polygon_vertices));
for (int i = 0; i < g_lights_.num_polygon_vertices; ++i) {
VectorCopy(g_lights_.polygon_vertices[i], metadata->polygon_vertices[i]);
}
}
static void uploadPointLights( struct LightsMetadata *metadata ) {
metadata->num_point_lights = g_lights_.num_point_lights;
for (int i = 0; i < g_lights_.num_point_lights; ++i) {
vk_point_light_t *const src = g_lights_.point_lights + i;
@ -1189,42 +1186,30 @@ vk_lights_bindings_t VK_LightsUpload( VkCommandBuffer cmdbuf ) {
dst->environment = !!(src->flags & LightFlag_Environment);
}
}
uploadGrid( &lights->grid );
vk_lights_bindings_t VK_LightsUpload( VkCommandBuffer cmdbuf ) {
// TODO static assert
ASSERT(sizeof(metadata->polygon_vertices) >= sizeof(g_lights_.polygon_vertices));
for (int i = 0; i < g_lights_.num_polygon_vertices; ++i) {
VectorCopy(g_lights_.polygon_vertices[i], metadata->polygon_vertices[i]);
}
const vk_staging_region_t locked = R_VkStagingLockForBuffer( (vk_staging_buffer_args_t) {
.buffer = g_lights_.buffer.buffer,
.offset = 0,
.size = sizeof(struct LightsMetadata),
.alignment = 16,
} );
{
DEBUG_BEGIN(cmdbuf, "upload lights");
const uint32_t size = sizeof(struct LightsMetadata) + 8 * sizeof(uint32_t); // ....
//const uint32_t size = sizeof(struct Lights);
const VkBufferCopy regions[] = {
{
.srcOffset = 0,
.dstOffset = 0,
.size = size,
},
};
vkCmdCopyBuffer(cmdbuf, g_lights_.staging.buffer, g_lights_.buffer.buffer, COUNTOF(regions), regions);
ASSERT(locked.ptr);
const VkBufferMemoryBarrier bmb[] = {{
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.buffer = g_lights_.buffer.buffer,
.offset = 0,
.size = size,
}};
vkCmdPipelineBarrier(cmdbuf,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
0, 0, NULL, ARRAYSIZE(bmb), bmb, 0, NULL);
DEBUG_END(cmdbuf);
}
struct LightsMetadata *metadata = locked.ptr;
uploadPolygonLights( metadata );
uploadPointLights( metadata );
// FIXME uploadGrid( &lights->grid );
R_VkStagingUnlock( locked.handle );
// TODO probably should do this somewhere else
R_VkStagingCommit( cmdbuf );
return (vk_lights_bindings_t){
.buffer = g_lights_.buffer.buffer,

View File

@ -612,6 +612,24 @@ static void performTracing(VkCommandBuffer cmdbuf, const perform_tracing_args_t*
}
RayPassPerform( cmdbuf, args->frame_index, g_rtx.pass.primary_ray, &res );
{
//const uint32_t size = sizeof(struct Lights);
//const uint32_t size = sizeof(struct LightsMetadata); // + 8 * sizeof(uint32_t);
const VkBufferMemoryBarrier bmb[] = {{
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.buffer = args->light_bindings->buffer,
.offset = 0,
.size = VK_WHOLE_SIZE,
}};
vkCmdPipelineBarrier(cmdbuf,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR,
0, 0, NULL, ARRAYSIZE(bmb), bmb, 0, NULL);
}
RayPassPerform( cmdbuf, args->frame_index, g_rtx.pass.light_direct_poly, &res );
RayPassPerform( cmdbuf, args->frame_index, g_rtx.pass.light_direct_point, &res );
RayPassPerform( cmdbuf, args->frame_index, g_rtx.pass.denoiser, &res );

View File

@ -55,7 +55,7 @@ vk_staging_region_t R_VkStagingLockForBuffer(vk_staging_buffer_args_t args) {
const int index = g_staging.buffers.count;
const uint32_t offset = aloRingAlloc(&g_staging.ring, args.size, args.alignment);
const uint32_t offset = aloRingAlloc(&g_staging.ring, args.size, args.alignment < 1 ? 1 : args.alignment );
if (offset == ALO_ALLOC_FAILED)
return (vk_staging_region_t){0};
if (g_staging.frames[1].offset == ALO_ALLOC_FAILED)