43 VkBufferCopy copyRegion = {
49 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
50 .dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_NV,
53 .size = VK_WHOLE_SIZE,
63 vkpt_submit_command_buffer(cmd_buf,
qvk.
queue_graphics, (1 <<
qvk.
device_count) - 1, 0, NULL, NULL, NULL, 0, NULL, NULL,
qvk.
fence_vertex_sync);
75 VkBufferCopy copyRegion = {
99 assert(!
"Vertex buffer overflow");
103 memcpy(vbo->positions_bsp, bsp_mesh->
positions, num_vertices *
sizeof(
float) * 3 );
104 memcpy(vbo->tex_coords_bsp, bsp_mesh->
tex_coords,num_vertices *
sizeof(
float) * 2 );
105 memcpy(vbo->tangents_bsp, bsp_mesh->
tangents, num_vertices *
sizeof(
float));
106 memcpy(vbo->materials_bsp, bsp_mesh->
materials, num_vertices *
sizeof(uint32_t) / 3);
107 memcpy(vbo->clusters_bsp, bsp_mesh->
clusters, num_vertices *
sizeof(uint32_t) / 3);
108 memcpy(vbo->texel_density_bsp, bsp_mesh->
texel_density, num_vertices *
sizeof(
float) / 3);
113 assert(!
"Visibility buffer overflow");
117 memcpy(vbo->sky_visibility, bsp_mesh->
sky_visibility, (num_clusters + 7) / 8);
158 for (
int j = 0; j < bsp->visrowsize; j++) {
160 for (
int k = 0; k < 8; ++k) {
161 if (mask[j] & (1 << k))
183 dst_list_offsets[
c] = tail;
185 tail += original_size;
198 const char* mask =
BSP_GetPvs(bsp, transformed_model_lights[nlight].cluster);
200 for (
int j = 0; j < bsp->visrowsize; j++) {
202 for (
int k = 0; k < 8; ++k) {
203 if (mask[j] & (1 << k))
205 int other_cluster = j * 8 + k;
217 float style_scale = 1.f;
218 float prev_style = 1.f;
222 style_scale = max(0, min(1, style_scale));
225 prev_style = max(0, min(1, prev_style));
230 VectorCopy(light->
positions + 0, vblight + 0);
231 VectorCopy(light->
positions + 3, vblight + 4);
232 VectorCopy(light->
positions + 6, vblight + 8);
234 if (light->
color[0] < 0.f)
236 vblight[3] = -sky_radiance[0] * 0.5f;
237 vblight[7] = -sky_radiance[1] * 0.5f;
238 vblight[11] = -sky_radiance[2] * 0.5f;
242 vblight[3] = light->
color[0] * mat_scale;
243 vblight[7] = light->
color[1] * mat_scale;
244 vblight[11] = light->
color[2] * mat_scale;
247 vblight[12] = style_scale;
248 vblight[13] = prev_style;
267 static int const shift = 13;
268 static int const shiftSign = 16;
270 static int32_t
const infN = 0x7F800000;
271 static int32_t
const maxN = 0x477FE000;
272 static int32_t
const minN = 0x38800000;
273 static int32_t
const signN = 0x80000000;
275 static int32_t
const infC = 0x3FC00;
276 static int32_t
const nanN = 0x7F802000;
277 static int32_t
const maxC = 0x23BFF;
278 static int32_t
const minC = 0x1C400;
279 static int32_t
const signC = 0x8000;
281 static int32_t
const mulN = 0x52000000;
282 static int32_t
const mulC = 0x33800000;
284 static int32_t
const subC = 0x003FF;
285 static int32_t
const norC = 0x00400;
287 static int32_t
const maxD = 0x1C000;
288 static int32_t
const minD = 0x1C000;
292 uint32_t sign = v.
si & signN;
297 v.
si ^= (s.
si ^ v.
si) & -(minN > v.
si);
298 v.
si ^= (infN ^ v.
si) & -((infN > v.
si) & (v.
si > maxN));
299 v.
si ^= (nanN ^ v.
si) & -((nanN > v.
si) & (v.
si > infN));
301 v.
si ^= ((v.
si - maxD) ^ v.
si) & -(v.
si > maxC);
302 v.
si ^= ((v.
si - minD) ^ v.
si) & -(v.
si > subC);
351 light_poly_t* light = transformed_model_lights + nlight;
352 float* vblight = lbo->light_polys + (nlight + model_light_offset) * (
LIGHT_POLY_VEC4S * 4);
358 lbo->light_list_offsets[0] = 0;
359 lbo->light_list_offsets[1] = 0;
372 float style_scale = 1.f;
376 style_scale = max(0, min(1, style_scale));
378 lbo->light_styles[nstyle] = style_scale;
385 for (
int nmat = 0; nmat < nmaterials; nmat++)
388 uint32_t* mat_data = lbo->material_table + nmat * 4;
389 memset(mat_data, 0,
sizeof(uint32_t) * 4);
394 mat_data[1] |= (material->
num_frames & 0x000f) << 28;
395 mat_data[1] |= (material->
next_frame & 0x0fff) << 16;
418 int vertex_offset = 0;
419 for(
int i = 0; i < MAX_MODELS; i++) {
424 for (
int nmesh = 0; nmesh <
r_models[i].nummeshes; nmesh++)
428 m->idx_offset = idx_offset;
429 m->vertex_offset = vertex_offset;
433 int num_verts =
r_models[i].numframes *
m->numverts;
434 assert(num_verts > 0);
436 for (
int j = 0; j < num_verts; j++)
437 Com_Printf(
"%f %f %f\n",
442 for (
int j = 0; j <
m->numtris; j++)
443 Com_Printf(
"%d %d %d\n",
444 m->indices[j * 3 + 0],
445 m->indices[j * 3 + 1],
446 m->indices[j * 3 + 2]);
451 snprintf(buf,
sizeof buf,
"model_%04d.obj", i);
452 FILE *f = fopen(buf,
"wb+");
454 for (
int j = 0; j <
m->numverts; j++) {
455 fprintf(f,
"v %f %f %f\n",
460 for (
int j = 0; j <
m->numindices / 3; j++) {
461 fprintf(f,
"f %d %d %d\n",
462 m->indices[j * 3 + 0] + 1,
463 m->indices[j * 3 + 1] + 1,
464 m->indices[j * 3 + 2] + 1);
469 memcpy(vbo->positions_model + vertex_offset * 3,
m->positions,
sizeof(
float) * 3 * num_verts);
470 memcpy(vbo->normals_model + vertex_offset * 3,
m->normals,
sizeof(
float) * 3 * num_verts);
471 memcpy(vbo->tex_coords_model + vertex_offset * 2,
m->tex_coords,
sizeof(
float) * 2 * num_verts);
472 memcpy(vbo->tangents_model + vertex_offset * 4,
m->tangents,
sizeof(
float) * 4 * num_verts);
473 memcpy(vbo->idx_model + idx_offset,
m->indices,
sizeof(uint32_t) *
m->numindices);
475 vertex_offset += num_verts;
476 idx_offset +=
m->numtris * 3;
494 VkDescriptorSetLayoutBinding vbo_layout_bindings[] = {
496 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
497 .descriptorCount = 1,
499 .stageFlags = VK_SHADER_STAGE_ALL,
502 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
503 .descriptorCount = 1,
505 .stageFlags = VK_SHADER_STAGE_ALL,
508 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
509 .descriptorCount = 1,
511 .stageFlags = VK_SHADER_STAGE_ALL,
514 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
515 .descriptorCount = 1,
517 .stageFlags = VK_SHADER_STAGE_ALL,
520 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
521 .descriptorCount = 1,
523 .stageFlags = VK_SHADER_STAGE_ALL,
526 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
527 .descriptorCount = 1,
529 .stageFlags = VK_SHADER_STAGE_ALL,
532 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
533 .descriptorCount = 3,
535 .stageFlags = VK_SHADER_STAGE_ALL,
539 VkDescriptorSetLayoutCreateInfo layout_info = {
540 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
541 .bindingCount =
LENGTH(vbo_layout_bindings),
542 .pBindings = vbo_layout_bindings,
549 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
550 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
554 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
555 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
558 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
559 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
564 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
565 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
569 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
570 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
573 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
574 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
577 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
578 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
583 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
584 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
587 VkDescriptorPoolSize pool_size = {
588 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
589 .descriptorCount =
LENGTH(vbo_layout_bindings),
592 VkDescriptorPoolCreateInfo pool_info = {
593 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
595 .pPoolSizes = &pool_size,
601 VkDescriptorSetAllocateInfo descriptor_set_alloc_info = {
602 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
604 .descriptorSetCount = 1,
610 VkDescriptorBufferInfo buf_info = {
616 VkWriteDescriptorSet output_buf_write = {
617 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
620 .dstArrayElement = 0,
621 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
622 .descriptorCount = 1,
623 .pBufferInfo = &buf_info,
626 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
628 output_buf_write.dstBinding = 1;
631 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
633 output_buf_write.dstBinding = 2;
636 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
638 output_buf_write.dstBinding = 3;
641 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
643 output_buf_write.dstBinding = 4;
646 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
648 output_buf_write.dstBinding = 5;
649 output_buf_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
652 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
665 return VK_ERROR_MEMORY_MAP_FAILED;
713 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
714 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
719 VkDescriptorBufferInfo light_stats_buf_info[] = { {
733 VkWriteDescriptorSet output_buf_write = {
734 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
737 .dstArrayElement = 0,
738 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
739 .descriptorCount =
LENGTH(light_stats_buf_info),
740 .pBufferInfo = light_stats_buf_info,
743 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
768 VkDescriptorSetLayout desc_set_layouts[] = {
773 .setLayoutCount =
LENGTH(desc_set_layouts),
774 .pSetLayouts = desc_set_layouts,
777 VkComputePipelineCreateInfo compute_pipeline_info[] = {
779 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
780 .stage =
SHADER_STAGE(QVK_MOD_INSTANCE_GEOMETRY_COMP, VK_SHADER_STAGE_COMPUTE_BIT),
784 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
785 .stage =
SHADER_STAGE(QVK_MOD_ANIMATE_MATERIALS_COMP, VK_SHADER_STAGE_COMPUTE_BIT),
820 VkDescriptorSet desc_sets[] = {
825 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_COMPUTE,
828 vkCmdDispatch(cmd_buf, num_instances, 1, 1);
830 if (update_world_animations)
835 vkCmdDispatch(cmd_buf, num_groups, 1, 1);
838 VkBufferMemoryBarrier barrier = {
839 .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
840 .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
841 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
848 vkCmdPipelineBarrier(
850 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
851 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,