18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00
Files
maszyna/rendering/vulkan14/vulkan14indirect.cpp
T
maj00r 2edf9c410d feat(render): Vulkan keeps the compact vertex streams of EU07M models; E4D renamed EU07M (#537)
* feat(render): the Vulkan renderer keeps the vertex streams of E4D models as they are, 28 bytes a vertex

Stage 3 of the E4D model format. A model loaded from .e4d (modelformat e4d) reaches the Vulkan renderer as its compact
streams: positions (12 B), attributes (QTangent snorm16x4 + uv f32x2, 16 B), user data and indices, without the 48-byte
gfx::basic_vertex in between.

- gfx: gfx::compact_attributes, geometry_bank::create_compact() and GfxRenderer->Insert_Compact(). The default of
  Insert_Compact decodes to basic_vertex, so the OpenGL 4.6 and the null renderers get the models as before. The CPU
  readers of a chunk (pantograph, bounds, picking, the editor) get basic_vertex made from the streams the first time they
  read it; a chunk which is changed (replace, append) turns into basic_vertex first.
- Vulkan: a bank whose chunks are all compact keeps positions, user data, attributes and indices in buffers of their own;
  vertex_kind::scene_compact reads them (location 5 the quaternion as R16G16B16A16_SNORM). A program gets a COMPACT_VERTEX
  variant the first time a compact chunk is drawn with it, in the background with gfx.vulkan.asyncresources, as the
  MODEL_DRAWS one; the variant of the multi draws makes a compact one of its own. Only vertex.vert and cabprobe.vert
  decode the normal (compact_frame() in common.glsl); the shaders of shadows, prepass, picking and cube shadows read
  position and uv as before.
- Ray tracing: the BLAS of a compact chunk reads the positions with a stride of 12, and the reflections read the normal
  and uv from the attributes (RT_FLAG_COMPACT in the record). Not tried with ray tracing on.
- The loader hands the streams of the file to Insert_Compact as they are; the stars keep their colours in the normals
  of basic_vertex and go as before.

Measured on Vulkan (self-test, 250 models of the game, runs alternating with legacy): device memory 1.60-1.64 GiB
against 1.89 GiB, GPU time a frame 1.27-1.39 ms against 1.37-1.57 ms, the same frame time and load time, the picture
within 1/255 of the legacy one, no errors of Vulkan in the log. Unit tests 1189/1189; -e4d diff gives the same results
through the new decode. Not built with GCC here.

* refactor(model): the new model format is EU07M (.eu07m), not E4D

E4D read as the next version of E3D, which the format isn't meant to be tied to; EU07M goes with the world format .eu07.
The format isn't released, so nothing reads the old name.

- The extension .eu07m, the magic EU7M (the four bytes of the header), model/eu07m/ with eu07m_*.{h,cpp},
  namespace eu07m, docs/eu07m-format.md, tests/unit/test_eu07m.cpp.
- The command line -eu07m convert|dump|validate|diff, TModel3d::LoadFromEU07M, and the key modelformat eu07m|legacy
  of eu07.ini (Global.modelformat_eu07m).
- Nothing else changes: the sections, the records and the components are the same, so a converted file needs only its
  magic and its extension changed.

Unit tests 1189/1189.

* fix(render): EU07M review of #537: no whole vertices kept for the bounds, the compact variant checked by CI and given up when it fails

- The bounding radius, offset() and MaxY() read the positions of a chunk alone (Positions(), geometry_bank::positions()):
  on Vulkan the compact streams are no longer decoded into basic_vertex and kept for the life of the bank as every model
  loads, so the host memory of EU07M models stays below that of E3D. What still needs whole vertices (the editor, the
  picking, the export) decodes them on first use as before.
- A COMPACT_VERTEX variant which doesn't compile is told once in the log and the banks of EU07M models go back to
  basic_vertex (compact_layout_failed()), those built already when they are drawn next, instead of the models being left
  out for the session.
- The shader validator compiles the vk14 shaders which have COMPACT_VERTEX with it, alone and with MODEL_DRAWS, as the
  renderer makes the variants.

* feat(model): the EU07M model files take the .r3a extension, the switch is modelformat r3a

The model files of the format are named .r3a in place of .eu07m, and eu07.ini selects them with modelformat r3a in place of
modelformat eu07m. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither the extension nor
the key has been released, so nothing reads the old ones.

* feat(model): the EU07M model files take the .ng3d extension, the switch is modelformat ng3d

In place of .r3a (2fe1064f9): the model files are .ng3d and eu07.ini selects them with modelformat ng3d. The format keeps its name (EU07M), its magic and the -eu07m command line tool. Neither name has been released.
2026-12-01 18:11:17 +01:00

957 lines
36 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
// the instances drawn from the gpu culling in the vulkan renderer (the stage P5 of the plan; opengl46_renderer::Render_Instances_Indirect()
// and the rest): the cullings of the passes picked as the opengl renderer picks them, the rendering of the pass ended for the compute
// passes and begun again where it was, the walks of the culled groups shared with the opengl renderer (gfx::scene_walk), and the indirect
// draws of the recorded draws, with multi draws where they follow one another with the same state
#include "stdafx.h"
#include "rendering/vulkan14/vulkan14context.h"
#include "rendering/vulkan14/vulkan14commands.h"
#include "rendering/common/walksync.h"
#include "model/AnimModel.h"
#include "model/Model3d.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include <chrono>
namespace
{
VkPipelineStageFlags2 const compute_stage{VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT};
} // namespace
// the cullings
bool vulkan14_renderer::init_cull()
{
auto &ctx{*m_context};
auto &scene{ctx.scene};
auto &cull{scene.cull};
auto const device{ctx.device.handle()};
vk14::cull_environment environment;
environment.memory = &ctx.memory;
environment.uploads = &ctx.uploads;
environment.deletions = &ctx.deletions;
environment.ring = &ctx.ring;
environment.uniform_alignment = ctx.device.caps().limits.minUniformBufferOffsetAlignment;
environment.storage_alignment = ctx.device.caps().limits.minStorageBufferOffsetAlignment;
environment.frames = static_cast<std::uint32_t>(ctx.frames.size());
environment.no_pyramid = vk14::image_descriptor(ctx.sampling.clamp_nearest, scene.depth_empty->view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
if (false == cull.main.init(environment, device, ctx.pipelines, ctx.shaders, scene.pass_layout))
{
return false;
}
for (auto &cascade : cull.cascades)
{
if (false == cascade.init(environment, device, ctx.pipelines, ctx.shaders, scene.pass_layout, &cull.main))
{
return false;
}
}
if (false == cull.hiz.init(device, ctx.pipelines, ctx.shaders, scene.pass_layout))
{
return false;
}
cull.main.begin_frame(ctx.slot, ctx.timelinevalue + 1, ctx.completed());
cull.ready = true;
return true;
}
void vulkan14_renderer::destroy_cull()
{
if (!m_context)
{
return;
}
auto &cull{m_context->scene.cull};
auto const device{m_context->device.handle()};
// the ones which share the kernels of the main view first
for (auto &cascade : cull.cascades)
{
cascade.destroy(device);
}
for (auto *culls : {&cull.headlights, &cull.lamps})
{
for (auto &culling : *culls)
{
if (culling)
{
culling->destroy(device);
}
}
culls->clear();
}
cull.main.destroy(device);
cull.hiz.destroy(device);
cull.shadowfit.destroy(device);
cull.ready = false;
}
vk14::instance_cull &vulkan14_renderer::pass_cull()
{
return static_cast<vk14::instance_cull &>(*m_passcull);
}
// the gpu culling runs along the first color pass of the main viewport of a frame, and the passes of the cascades of the sun
// shadows and of the faces of the environment map drawn for it, each with a culling of its own (opengl46_renderer::walk_region_begin())
// the cullings made the first time they're wanted, by the main renderer (before it gives the passes to the walkers)
void vulkan14_renderer::ensure_cull()
{
auto &cull{m_context->scene.cull};
if ((m_main == nullptr) && (Global.gfx_gpucull != 0) && (false == cull.ready) && (false == cull.tried))
{
cull.tried = true;
if (false == init_cull())
{
ErrorLog("vulkan: the gpu culling of the instances can't be used, the cpu culls them (gfx.gpucull)");
destroy_cull();
}
}
}
void vulkan14_renderer::walk_region_begin()
{
vk14::instance_cull *passcull{nullptr};
auto &cull{m_context->scene.cull};
if ((Global.gfx_gpucull != 0) && (m_current_viewport != nullptr) && (m_current_viewport->main) && m_draw->rendering)
{
ensure_cull();
if (cull.ready)
{
if ((m_renderpass.draw_mode == rendermode::color) && (m_instancecullframe != m_framestamp))
{
m_instancecullframe = m_framestamp;
passcull = &cull.main;
}
else if ((m_renderpass.draw_mode == rendermode::shadows) && (m_lightshadowcull != nullptr))
{
// the map of a set of headlights or the scenery part of the map of a lamp, which picked its culling
passcull = m_lightshadowcull;
m_shadowcasters.clear();
}
else if ((m_renderpass.draw_mode == rendermode::shadows) && Global.gfx_gpucull_shadows && (m_shadowcascade >= 0) && (m_shadowcascade < static_cast<int>(cull.cascades.size())) &&
(false == m_cubeshadows))
{
// the cascades of the sun
passcull = &cull.cascades[m_shadowcascade];
m_shadowcasters.clear();
}
}
}
Begin_Pass_Cull(passcull, (passcull == &cull.main));
}
bool vulkan14_renderer::walk_instances_begin()
{
if (m_indirectcolourpending)
{
// the instances culled and recorded by the depth pass of the whole scene, the buckets weren't queued for the cpu
render_instances_indirect_pending();
}
if (m_instancecullpass)
{
if (m_passcull->drawing())
{
// the buckets of the pass weren't queued for the cpu, which leaves the loop of the walk empty
if (m_passcull == &m_context->scene.cull.main)
{
render_instances_indirect();
}
else
{
render_instances_indirect_pass();
}
}
else
{
// the view of the pass is in place here, the one Render_Instanced() takes
suspend_rendering();
pass_cull().dispatch(m_commands, OpenGLMatrices.data(GL_MODELVIEW));
resume_rendering();
// what the cpu drew of the sleepers and the models drawn on their own
Compare_Pass_Cull();
}
}
return m_instancecullpass;
}
void vulkan14_renderer::walk_instances_end()
{
End_Pass_Cull();
}
// the rendering ended and begun again around the compute passes
void vulkan14_renderer::remember_rendering(VkRenderingInfo const &Info, std::vector<vk14::image_use> const &Uses)
{
auto &resume{m_draw->resume};
resume.info = Info;
resume.info.pNext = nullptr;
resume.info.flags = 0;
auto const colours{std::min<std::uint32_t>(Info.colorAttachmentCount, static_cast<std::uint32_t>(resume.colours.size()))};
for (std::uint32_t i = 0; i < colours; ++i)
{
resume.colours[i] = Info.pColorAttachments[i];
// what was drawn is drawn over
resume.colours[i].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
}
resume.info.colorAttachmentCount = colours;
resume.info.pColorAttachments = (colours > 0 ? resume.colours.data() : nullptr);
resume.has_depth = (Info.pDepthAttachment != nullptr);
if (resume.has_depth)
{
resume.depth = *Info.pDepthAttachment;
resume.depth.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
}
resume.info.pDepthAttachment = (resume.has_depth ? &resume.depth : nullptr);
resume.info.pStencilAttachment = nullptr;
resume.uses = Uses;
resume.valid = true;
}
void vulkan14_renderer::suspend_rendering()
{
auto &draw{*m_draw};
if (false == draw.rendering)
{
return;
}
m_commands->end_rendering();
draw.rendering = false;
}
void vulkan14_renderer::resume_rendering()
{
auto &draw{*m_draw};
auto &resume{draw.resume};
if (draw.rendering || (false == resume.valid) || (draw.targets == nullptr))
{
return;
}
auto const commands{m_commands};
// what the rendering stored before is loaded again. a walker leaves the states of the images the main thread tracks as they were:
// the rendering left them as it found them
if (m_main != nullptr)
{
vk14::barriers_again(commands, resume.uses);
}
else
{
vk14::barriers(commands, resume.uses);
}
commands->begin_rendering(&resume.info);
// the dynamic state starts anew with the rendering, the state of the walk goes on
auto const set{draw.set};
auto const shadows{draw.shadows};
auto const blending{draw.blending};
auto const cullface{draw.cullface};
auto const glare{draw.glare};
auto const depthmask_off{draw.depthmask_off};
auto *program{draw.program};
auto const textures{draw.textures};
start_draws(*draw.targets, static_cast<int>(draw.extent.width), static_cast<int>(draw.extent.height), set, shadows);
draw.blending = blending;
draw.cullface = cullface;
draw.glare = glare;
draw.depthmask_off = depthmask_off;
draw.program = program;
draw.textures = textures;
if (draw.depthpass)
{
// the bias of the depth pass of the scene (render_scene_depth())
commands->set_depth_bias_enable(VK_TRUE);
commands->set_depth_bias(-1.f, 0.f, -1.f);
}
}
// the draws
namespace
{
// a variant of a program of the scene, made of its shaders with a definition more: the multi draws (MODEL_DRAWS, of the same
// vertices), or the chunks of the compact layout (COMPACT_VERTEX, vertex_kind::scene_compact). null when its shaders can't be made.
// any thread
struct variant_kind
{
char const *define;
char const *label;
bool compact;
std::shared_ptr<vk14::scene_program> vk14::scene_program::*slot;
bool vk14::scene_program::*tried;
bool vk14::scene_program::*pending;
};
variant_kind const multidraw_kind{"#define MODEL_DRAWS\n", " (multi draws)", false, &vk14::scene_program::multidraw, &vk14::scene_program::multidraw_tried, &vk14::scene_program::multidraw_pending};
variant_kind const compact_kind{"#define COMPACT_VERTEX\n", " (compact)", true, &vk14::scene_program::compact, &vk14::scene_program::compact_tried, &vk14::scene_program::compact_pending};
std::shared_ptr<vk14::scene_program> make_variant(vk14::shader_library &Shaders, std::string const &Defines, std::string const &Vertex, std::string const &Fragment, std::string const &Name,
vk14::vertex_kind const Vertices, variant_kind const &Kind)
{
try
{
auto variant{std::make_shared<vk14::scene_program>()};
variant->defines = Defines + Kind.define;
variant->vertex = Shaders.get(Vertex, variant->defines);
variant->fragment = Shaders.get(Fragment, variant->defines);
variant->vertex_name = Vertex;
variant->fragment_name = Fragment;
variant->vertices = Kind.compact ? vk14::vertex_kind::scene_compact : Vertices;
variant->name = Name + Kind.label;
// a variant has no variant of its own kind. the one of the multi draws makes a compact one of its own (both definitions), which
// the multi draws of the chunks of the compact layout take; the compact one makes none more
(*variant).*Kind.tried = true;
if (Kind.compact)
{
variant->multidraw_tried = true;
}
return variant;
}
catch (gfx::shader_exception const &Exception)
{
vk14::log_error(Exception.what());
if (Kind.compact)
{
// without the variant the chunks of the compact layout would be left out for good: their banks go back to basic_vertex
vk14::compact_layout_failed(Name);
}
}
return nullptr;
}
} // namespace
vk14::scene_program *vulkan14_renderer::program_variant(vk14::scene_program &Program, bool &Pending, bool const Compact)
{
auto const &kind{Compact ? compact_kind : multidraw_kind};
// made by one of the threads walking the passes (M2)
std::scoped_lock lock(Program.mutex);
Pending = false;
if (Program.*kind.tried)
{
Pending = Program.*kind.pending;
return (Program.*kind.slot).get();
}
Program.*kind.tried = true;
auto const scene{(Program.vertices == vk14::vertex_kind::scene) || ((false == Compact) && (Program.vertices == vk14::vertex_kind::scene_compact))};
if (Program.vertex_name.empty() || Program.fragment_name.empty() || (false == scene))
{
return nullptr;
}
auto &ctx{*m_context};
if (Global.gfx_vulkan_asyncresources && ctx.background.running())
{
// its shaders are compiled by the background threads (gfx.vulkan.asyncresources), the draws left out till they are, as the
// ones without their pipeline are: the walk doesn't wait for the compiler, nor do the other threads binding the program for
// its lock. the program outlives the threads (stopped first when the scene goes)
Program.*kind.pending = true;
Pending = true;
ctx.background.add([&shaders = ctx.shaders, program = &Program, defines = Program.defines, vertex = Program.vertex_name, fragment = Program.fragment_name, name = Program.name,
vertices = Program.vertices, &kind]() {
auto variant{make_variant(shaders, defines, vertex, fragment, name, vertices, kind)};
std::scoped_lock const made{program->mutex};
(*program).*kind.slot = std::move(variant);
(*program).*kind.pending = false;
});
return nullptr;
}
Program.*kind.slot = make_variant(ctx.shaders, Program.defines, Program.vertex_name, Program.fragment_name, Program.name, Program.vertices, kind);
return (Program.*kind.slot).get();
}
vk14::scene_program *vulkan14_renderer::multidraw_program(vk14::scene_program &Program, bool &Pending)
{
return program_variant(Program, Pending, false);
}
vk14::scene_program *vulkan14_renderer::compact_program(vk14::scene_program &Program, bool &Pending)
{
return program_variant(Program, Pending, true);
}
bool vulkan14_renderer::draw_indirect(vk14::instance_cull &Cull, command_set const Set, std::size_t const First, std::uint32_t const Count, vk14::scene_program &Program, vk14::blend_mode const Blend)
{
auto &ctx{*m_context};
auto &scene{ctx.scene};
auto &draw{*m_draw};
auto const &first{m_indirectdraws[First]};
if ((false == draw.rendering) || (Count == 0) || (Program.vertices != vk14::vertex_kind::scene) || (Cull.command_buffer() == VK_NULL_HANDLE))
{
return false;
}
auto const &geometry{first.geometry};
if ((geometry.bank == 0) || (geometry.bank > m_banks.size()) || (m_banks[geometry.bank - 1] == nullptr))
{
return false;
}
// the buffers of the bank of the chunks, with their data on the gpu (the chunks of a multi draw are of one bank). what the prepass
// of the scene leaves out has its depth written by the colour pass (keepdepth)
vk14::chunk_source chunk;
if (false == chunk_of(geometry, chunk))
{
m_prepassincomplete = (m_prepassincomplete || draw.depthpass);
return false;
}
auto *program{program_for(Program, chunk)};
if ((program == nullptr) || (false == bind_scene_pipeline(*program, *draw.targets, Blend, static_cast<int>(chunk.topology))))
{
m_prepassincomplete = (m_prepassincomplete || draw.depthpass);
return false;
}
// the prepass draws the triangles of the draws (draw_indirect_depth())
auto const triangles{(first.type == GL_TRIANGLES) || (first.type == GL_TRIANGLE_STRIP) || (first.type == GL_TRIANGLE_FAN)};
apply_draw_state(triangles);
// the blocks of the draws, one after another in the window of the blocks: the multi draw picks them by the number of the draw
std::uint32_t block{~0u};
if (Count == 1)
{
// the block of the recorded draw; the walk goes on with its own model block. what a recorded draw leaves in it (the
// emission of a lit lamp) would light the tracks and the shapes the walk draws next
auto const walkubs{model_ubs};
model_ubs = first.ubs;
block = push_draw_block();
model_ubs = walkubs;
}
else
{
auto &draws{scene.draws};
auto &window{draw.draws_window};
auto const size{static_cast<VkDeviceSize>(Count) * draws.stride};
auto slice{(window.open && (size <= draws.window)) ? draw.draws_cache.allocate_before(size, draws.stride, window.base, window.base + draws.window) : vk14::ring_slice{}};
if ((false == bool(slice)) && (size <= draws.window))
{
// further on in the ring; the window stays where it is while the blocks lie within it (push_draw_block())
slice = (window.open ? draw.draws_cache.allocate_after(size, draws.stride, window.base) : draw.draws_cache.allocate(size, draws.stride));
if (slice && ((false == window.open) || (slice.offset + slice.size > window.base + draws.window)))
{
window.base = slice.offset;
window.open = true;
draw.set_dirty = true;
}
}
if (slice)
{
for (std::uint32_t i = 0; i < Count; ++i)
{
std::memcpy(slice.data + i * draws.stride, &m_indirectdraws[First + i].ubs, sizeof(gl::model_ubs));
}
block = static_cast<std::uint32_t>((slice.offset - window.base) / draws.stride);
}
}
// the next draw of the walk pushes a block of its own
draw.block = ~0u;
if (block == ~0u)
{
m_prepassincomplete = (m_prepassincomplete || draw.depthpass);
return false;
}
if (draw.set_dirty)
{
bind_pass_set();
}
if (draw.textures_dirty)
{
push_textures();
}
auto const commands{draw.commands};
if (draw.pushed_draw != block)
{
vk14::draw_constants constants;
constants.draw = block;
VkPushConstantsInfo info{};
info.sType = VK_STRUCTURE_TYPE_PUSH_CONSTANTS_INFO;
info.layout = scene.scene_layout;
info.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
info.offset = 0;
info.size = sizeof(constants);
info.pValues = &constants;
commands->push_constants2(&info);
draw.pushed_draw = block;
}
bind_chunk_buffers(chunk);
auto const offset{static_cast<VkDeviceSize>(Cull.draw_command_offset(Set, First))};
if (chunk.indices != VK_NULL_HANDLE)
{
if (chunk.indices != draw.indices)
{
commands->bind_index_buffer(chunk.indices, 0, VK_INDEX_TYPE_UINT32);
draw.indices = chunk.indices;
}
commands->draw_indexed_indirect(Cull.command_buffer(), offset, Count, sizeof(gl::instance_draw_command));
}
else
{
commands->draw_indirect(Cull.command_buffer(), offset, Count, sizeof(gl::instance_draw_command));
}
draw.draws += Count;
return true;
}
vulkan14_renderer::indirect_counts vulkan14_renderer::draw_indirect_colour(vk14::instance_cull &Cull, command_set const Set)
{
auto &draw{*m_draw};
auto const &scene{m_context->scene};
// the draws were recorded with the seed of scenery models and sleepers, 0 (TAnimModel::RaPrepare()); vehicles drawn since
// leave their own, which would pick other texture variants than those the draws were recorded and ordered with
TSubModel::iVariantSeed = 0;
indirect_counts counts;
auto const count{m_indirectdraws.size()};
auto const longest{static_cast<std::size_t>(scene.draws.window / scene.draws.stride)};
draw.replaying = true;
for (std::size_t index = 0; index < count;)
{
auto const blending{m_indirectdraws[index].blending};
if (blending)
{
walk_blending(true);
}
walk_material(m_indirectdraws[index].material, m_indirectdraws[index].submodel);
auto *program{draw.program};
auto pending{false};
auto *multidraw{(m_indirectmultidraw && (program != nullptr)) ? multidraw_program(*program, pending) : nullptr};
auto last{index + 1};
if (multidraw != nullptr)
{
// the draws which follow with the same state, in one call
while ((last < count) && (last - index < longest) && Indirect_Mergeable(index, last))
{
++last;
}
}
if ((program != nullptr) && (false == pending))
{
auto const drawcount{static_cast<std::uint32_t>(last - index)};
if (draw_indirect(Cull, Set, index, drawcount, (multidraw != nullptr ? *multidraw : *program), (draw.blending ? vk14::blend_mode::scene_alpha : vk14::blend_mode::none)))
{
counts.draws += drawcount;
}
++counts.calls;
}
index = last;
if (blending)
{
walk_blending(false);
}
}
draw.replaying = false;
if (count > 0)
{
// the model block the draws made one by one leave behind
model_ubs = m_indirectdraws.back().ubs;
}
model_ubs.emission = 0.0f;
return counts;
}
vulkan14_renderer::indirect_counts vulkan14_renderer::draw_indirect_depth(vk14::instance_cull &Cull, command_set const Set)
{
auto &draw{*m_draw};
auto &scene{m_context->scene};
auto const commands{m_commands};
// as in draw_indirect_colour(): the seed the draws were recorded with
TSubModel::iVariantSeed = 0;
indirect_counts counts;
auto const count{m_indirectdraws.size()};
auto const longest{static_cast<std::size_t>(scene.draws.window / scene.draws.stride)};
// whether the prepass of the class draws the draw, and with which diffuse texture (class 2)
auto const prepass_texture = [this](std::size_t const Index, int const Prepassclass, texture_handle &Texture) -> bool {
auto const &entry{m_indirectdraws[Index]};
if ((entry.blending) || (entry.ubs.opacity < 0.f))
{
return false;
}
if ((entry.type != GL_TRIANGLES) && (entry.type != GL_TRIANGLE_STRIP) && (entry.type != GL_TRIANGLE_FAN))
{
return false;
}
if (prepass_class(entry.material) != Prepassclass)
{
return false;
}
Texture = null_handle;
if (Prepassclass == 2)
{
// the diffuse texture of the colour pass, the same choice as walk_material() makes
auto const &material{m_materials.material(entry.material)};
auto const &textures{material.texture_variants.empty() ? material.textures : material.GetTextures(TSubModel::VariantId(entry.submodel))};
if (textures[0] == null_handle)
{
return false;
}
Texture = textures[0];
}
return true;
};
// inside the colour pass the depth goes a little farther than the surface, without the colour (the depth pass of the scene has
// its own state)
auto const insidecolour{false == draw.depthpass};
if (insidecolour)
{
commands->set_depth_bias_enable(VK_TRUE);
commands->set_depth_bias(-1.f, 0.f, -1.f);
}
walk_blending(false);
// the draws without the alpha test first, then the ones with it, one program each
for (auto prepassclass = 1; prepassclass <= 2; ++prepassclass)
{
auto &program{prepassclass == 1 ? *scene.prepass : *scene.prepass_alpha};
auto pending{false};
auto *multidraw{m_indirectmultidraw ? multidraw_program(program, pending) : nullptr};
if (pending)
{
// the shaders of its multi draws are being compiled: its draws are left out, the colour pass writes their depth
m_prepassincomplete = (m_prepassincomplete || draw.depthpass);
continue;
}
for (std::size_t index = 0; index < count;)
{
texture_handle texture;
if (false == prepass_texture(index, prepassclass, texture))
{
++index;
continue;
}
if (prepassclass == 2)
{
walk_texture(0, texture);
}
auto const &entry{m_indirectdraws[index]};
auto last{index + 1};
if (multidraw != nullptr)
{
// the draws which follow in the same bank and of the same primitive, with the same texture for the alpha test. the
// material doesn't matter to the depth
texture_handle othertexture;
while ((last < count) && (last - index < longest) && (m_indirectdraws[last].group == entry.group) && (m_indirectdraws[last].type == entry.type) &&
(m_indirectcommands[last].indexed == m_indirectcommands[index].indexed) && (prepass_texture(last, prepassclass, othertexture)) && (othertexture == texture))
{
++last;
}
}
auto const drawcount{static_cast<std::uint32_t>(last - index)};
if (draw_indirect(Cull, Set, index, drawcount, (multidraw != nullptr ? *multidraw : program), (insidecolour ? vk14::blend_mode::no_colour : vk14::blend_mode::none)))
{
counts.draws += drawcount;
}
++counts.calls;
index = last;
}
}
if (insidecolour)
{
commands->set_depth_bias_enable(VK_FALSE);
}
return counts;
}
vulkan14_renderer::indirect_counts vulkan14_renderer::draw_indirect_shadow(vk14::instance_cull &Cull, command_set const Set)
{
auto &scene{m_context->scene};
indirect_counts counts;
auto const count{m_indirectdraws.size()};
auto const longest{static_cast<std::size_t>(scene.draws.window / scene.draws.stride)};
for (std::size_t index = 0; index < count;)
{
auto const &entry{m_indirectdraws[index]};
auto const texture{Indirect_Shadow_Texture(entry.material)};
// a material which moves its vertices itself: its own program, with its textures; the draws of it alone go together
auto *own{own_shadow_program(entry.material)};
auto &program{own != nullptr ? *own :
m_cubeshadows ? (texture != null_handle ? *scene.cube_alpha_shadow : *scene.cube_shadow) :
(texture != null_handle ? *scene.alpha_shadow : *scene.shadow)};
auto pending{false};
auto *multidraw{m_indirectmultidraw ? multidraw_program(program, pending) : nullptr};
if (pending)
{
// the shaders of its multi draws are being compiled: the draw is left out till they're there
++index;
continue;
}
auto last{index + 1};
if (multidraw != nullptr)
{
// the draws which follow in the same bank and of the same primitive, with the same texture for the alpha test
while ((last < count) && (last - index < longest) && (m_indirectdraws[last].group == entry.group) && (m_indirectdraws[last].type == entry.type) &&
(m_indirectcommands[last].indexed == m_indirectcommands[index].indexed) && (Indirect_Shadow_Texture(m_indirectdraws[last].material) == texture) &&
(own_shadow_program(m_indirectdraws[last].material) == own) && ((own == nullptr) || (m_indirectdraws[last].material == entry.material)))
{
++last;
}
}
if (own != nullptr)
{
auto const &textures{m_materials.material(entry.material).textures};
for (std::size_t unit = 0; unit < gl::MAX_TEXTURES; ++unit)
{
if (textures[unit] != null_handle)
{
walk_texture(unit, textures[unit]);
}
}
}
else if (texture != null_handle)
{
walk_texture(0, texture);
}
auto const drawcount{static_cast<std::uint32_t>(last - index)};
if (draw_indirect(Cull, Set, index, drawcount, (multidraw != nullptr ? *multidraw : program), vk14::blend_mode::none))
{
counts.draws += drawcount;
}
++counts.calls;
index = last;
}
return counts;
}
void vulkan14_renderer::record_indirect_draws(vk14::instance_cull &Cull, bool const Cascade)
{
m_indirectcull = &Cull;
Walk_Indirect_Draws(Cull, Cascade);
if (false == Cull.dispatched())
{
// the culling didn't run (its buffers or its data of the frame couldn't be had): there are no results to draw its instances
// from, and the commands of an earlier frame aren't theirs
m_indirectdraws.clear();
m_indirectcommands.clear();
}
auto const orderstart{std::chrono::steady_clock::now()};
// with multi draws the draws with the same state follow one another; the order goes into the commands, so it comes first
m_indirectmultidraw = Global.gfx_gpucull_multidraw;
if (m_indirectmultidraw && (false == m_indirectdraws.empty()))
{
Order_Indirect_Draws();
}
Cull.fill_commands(m_commands, m_indirectcommands);
m_walkcounts.ordertime += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - orderstart).count();
}
// the instances of the main view: the depth pyramid of what the pass drew so far, the culling, the walks, the depth first (the
// depth pass of the scene, gfx.depthprepass), the colour; in two phases the instances seen in the previous frame first
void vulkan14_renderer::render_instances_indirect()
{
auto const cpustart{std::chrono::steady_clock::now()};
auto &ctx{*m_context};
auto &scene{ctx.scene};
auto &cull{scene.cull.main};
auto &draw{*m_draw};
auto const commands{m_commands};
auto const viewport{static_cast<std::size_t>(m_current_viewport - m_viewports.data())};
auto *targets{viewport < scene.targets.size() ? &scene.targets[viewport] : nullptr};
auto const projection{OpenGLMatrices.data(GL_PROJECTION)};
suspend_rendering();
// the occlusion test against the depth drawn so far in the pass: cab, terrain, shapes, tracks, vehicles, other models
vk14::cull_occlusion occlusion;
auto const build = [&]() {
vk14::barriers(commands, {vk14::use_of(*targets->depth, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, compute_stage, VK_ACCESS_2_SHADER_SAMPLED_READ_BIT)});
if (scene.cull.hiz.build(commands, ctx.device.handle(), ctx.memory, ctx.deletions, ctx.uploads.current_value(), targets->depth->view(), ctx.sampling.clamp_nearest, static_cast<int>(targets->extent.width),
static_cast<int>(targets->extent.height)))
{
occlusion = scene.cull.hiz.occlusion(ctx.sampling.clamp_nearest, projection);
// the weather of the next frame asks it what the scenery hid, of the main view
auto const mainview{(m_current_viewport != nullptr) && m_current_viewport->main && (m_renderpass.draw_mode == rendermode::color)};
scene.cull.hiz.made_in(mainview ? ctx.framecount : vk14::hiz_pyramid::no_frame, projection * glm::mat4{glm::mat3{OpenGLMatrices.data(GL_MODELVIEW)}});
}
else
{
scene.cull.hiz.made_in(vk14::hiz_pyramid::no_frame, glm::mat4{1.f});
}
};
auto const useocclusion{Global.gfx_gpucull_occlusion && (false == Global.gfx_skippipeline) && (targets != nullptr) && targets->depth};
if (useocclusion)
{
build();
}
// in two phases the instances seen in the previous frame are drawn first, and the others are tested against a pyramid made
// again with their depth in it (not in wireframe, which doesn't draw surfaces)
cull.dispatch(commands, OpenGLMatrices.data(GL_MODELVIEW), (occlusion.pyramid != VK_NULL_HANDLE ? &occlusion : nullptr), (Global.gfx_gpucull_occlusion_instances && (false == Global.bWireFrame)));
// the draws, recorded while the gpu culls
record_indirect_draws(cull, false);
// two phases only with the occlusion test, which needs the targets
auto const twophase{useocclusion && cull.two_phase()};
auto const draws{false == m_indirectdraws.empty()};
// the depth comes first in the depth pass of the whole scene, which leaves the colour to the colour pass, and with the depth
// prepass of the instances
auto const depthfirst{draw.depthpass || (Global.gfx_depthprepass && (false == Global.bWireFrame))};
indirect_counts prepasscounts;
indirect_counts colourcounts;
auto colourset{command_set::first};
resume_rendering();
if (twophase)
{
if (draws)
{
// the instances seen in the previous frame, then the pyramid again, with them in it
use_cull_pass_sets(&cull);
if (depthfirst)
{
prepasscounts += draw_indirect_depth(cull, command_set::first);
}
else
{
colourcounts += draw_indirect_colour(cull, command_set::first);
}
use_cull_pass_sets(nullptr);
}
suspend_rendering();
if (draws)
{
build();
}
// the others are tested against it; with no draws the instances are only marked for the next frame
cull.dispatch_second(commands, occlusion);
resume_rendering();
colourset = (depthfirst ? command_set::all : command_set::second);
}
if (draws)
{
use_cull_pass_sets(&cull);
if (depthfirst)
{
prepasscounts += draw_indirect_depth(cull, (twophase ? command_set::second : command_set::first));
}
if (false == draw.depthpass)
{
colourcounts += draw_indirect_colour(cull, colourset);
}
use_cull_pass_sets(nullptr);
}
if (draw.depthpass)
{
// the colour pass takes the draws from here, and counts them
m_indirectcolourpending = true;
m_indirectcolourset = colourset;
m_indirectcoloursleepers = m_sleeperspass;
m_indirectcolourmodels = m_modelspass;
m_renderpass.draw_stats.drawcalls += static_cast<int>(prepasscounts.calls);
colourcounts.draws = static_cast<std::uint32_t>(m_indirectdraws.size());
}
else
{
m_renderpass.draw_stats.instanced_drawcalls += static_cast<int>(colourcounts.calls);
m_renderpass.draw_stats.drawcalls += static_cast<int>(prepasscounts.calls);
// the count of the frame the gpu reported last
m_renderpass.draw_stats.instances += static_cast<int>(cull.last_count());
m_renderpass.draw_stats.models += static_cast<int>(cull.last_count());
}
cull.draw_result(colourcounts.draws, prepasscounts.draws, colourcounts.calls, prepasscounts.calls, std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - cpustart).count());
}
// the instances of a pass other than the main view: the culling of the pass with its own camera, no occlusion test, the draws
// recorded the same way, then drawn once with the programs the pass draws with on the cpu
void vulkan14_renderer::render_instances_indirect_pass()
{
auto const cpustart{std::chrono::steady_clock::now()};
auto &cull{pass_cull()};
auto &cascades{m_context->scene.cull.cascades};
suspend_rendering();
cull.dispatch(m_commands, OpenGLMatrices.data(GL_MODELVIEW));
record_indirect_draws(cull, (&cull >= cascades.data()) && (&cull < cascades.data() + cascades.size()));
resume_rendering();
indirect_counts counts;
if (false == m_indirectdraws.empty())
{
use_cull_pass_sets(&cull);
counts = (m_renderpass.draw_mode == rendermode::shadows ? draw_indirect_shadow(cull, command_set::first) : draw_indirect_colour(cull, command_set::first));
use_cull_pass_sets(nullptr);
}
m_renderpass.draw_stats.instanced_drawcalls += static_cast<int>(counts.calls);
// the count of the frame the gpu reported last
m_renderpass.draw_stats.instances += static_cast<int>(cull.last_count());
m_renderpass.draw_stats.models += static_cast<int>(cull.last_count());
cull.draw_result(counts.draws, 0, counts.calls, 0, std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - cpustart).count());
}
// the colour of the instances culled and recorded by the depth pass of the whole scene
void vulkan14_renderer::render_instances_indirect_pending()
{
m_indirectcolourpending = false;
auto &cull{m_context->scene.cull.main};
indirect_counts colourcounts;
if (false == m_indirectdraws.empty())
{
auto const cpustart{std::chrono::steady_clock::now()};
use_cull_pass_sets(&cull);
colourcounts = draw_indirect_colour(cull, m_indirectcolourset);
use_cull_pass_sets(nullptr);
cull.draw_result(colourcounts.draws, cull.prepass_draws(), colourcounts.calls, cull.prepass_calls(), cull.cpu_time() + std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - cpustart).count());
}
m_renderpass.draw_stats.instanced_drawcalls += static_cast<int>(colourcounts.calls);
m_renderpass.draw_stats.instances += static_cast<int>(cull.last_count());
m_renderpass.draw_stats.models += static_cast<int>(cull.last_count());
}
// the cascades of the sun shadows fitted to the depth of the main view (opengl46_renderer::Shadow_Fit())
char const *vulkan14_renderer::shadow_fit_unavailable(viewport_config const &Viewport) const
{
auto const shadowviews{std::count_if(m_viewports.begin(), m_viewports.end(), [](viewport_config const &View) { return View.shadow; })};
return gfx::shadow_fit::unavailable(false == Global.gfx_skippipeline, Viewport.shadow, static_cast<std::size_t>(shadowviews));
}
bool vulkan14_renderer::shadow_fit(viewport_config const &Viewport, std::array<float, gl::MAX_CASCADES + 1> &Splits, gfx::shadow_fit::areas &Areas)
{
auto &ctx{*m_context};
auto &cull{ctx.scene.cull};
auto const *unavailable{Global.gfx_shadow_sdsm ? shadow_fit_unavailable(Viewport) : nullptr};
if (Global.gfx_shadow_sdsm && (unavailable == nullptr))
{
if (cull.shadowfit)
{
// takes in what came back
cull.shadowfit.latest(ctx.completed());
}
else
{
unavailable = "no compute passes";
}
}
return cull.shadowfit.fit(m_framestamp, m_colorpass.pass_camera, unavailable, Splits, Areas, m_shadowfitstate);
}
void vulkan14_renderer::measure_shadow_fit(std::size_t const Viewport)
{
auto &ctx{*m_context};
auto &scene{ctx.scene};
auto &cull{scene.cull};
auto const &vp{m_viewports[Viewport]};
if ((false == vp.main) || (false == Global.gfx_shadow_sdsm) || (shadow_fit_unavailable(vp) != nullptr) || (Viewport >= scene.targets.size()) || (false == bool(scene.targets[Viewport].depth)))
{
return;
}
if ((false == bool(cull.shadowfit)) && (false == cull.shadowfit_tried))
{
cull.shadowfit_tried = true;
if (false == cull.shadowfit.init(ctx.device.handle(), ctx.pipelines, ctx.shaders, scene.pass_layout, ctx.memory, static_cast<std::uint32_t>(ctx.frames.size())))
{
ErrorLog("vulkan: the cascades of the sun shadows can't be fitted to the depth of the view (gfx.shadow.sdsm)");
cull.shadowfit.destroy(ctx.device.handle());
}
}
if (false == bool(cull.shadowfit))
{
return;
}
auto &targets{scene.targets[Viewport]};
auto const commands{m_commands};
vk14::barriers(commands, {vk14::use_of(*targets.depth, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, compute_stage, VK_ACCESS_2_SHADER_SAMPLED_READ_BIT)});
gfx::shadow_fit::view const view{m_colorpass.pass_camera.position(), m_colorpass.pass_camera.projection(), glm::mat3{m_colorpass.pass_camera.modelview()},
gfx::shadow_fit::light_rotation(m_sunlight.direction), gfx::shadow_fit::fixed_splits().back()};
ctx.passes.begin_pass("sun shadows fitted to the depth");
cull.shadowfit.measure(commands, targets.depth->view(), ctx.sampling.clamp_nearest, static_cast<int>(targets.extent.width), static_cast<int>(targets.extent.height), view, m_framestamp, ctx.slot, ctx.timelinevalue + 1);
ctx.passes.end_pass();
}