mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 08:04:57 +01:00
Optimise raytracing and buffer building
This commit is contained in:
@@ -15,6 +15,8 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/Timer.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace gfx
|
||||
{
|
||||
|
||||
@@ -246,6 +248,7 @@ bool grass_field::prepare(glm::dvec3 const &Camera, std::function<bool(scene::bo
|
||||
draw.share = share;
|
||||
draw.dry = dry;
|
||||
draw.first = m_tiledraws.size();
|
||||
draw.nearest = distance;
|
||||
m_chunkdraws.push_back(draw);
|
||||
}
|
||||
// fewer segments further away
|
||||
@@ -258,11 +261,20 @@ bool grass_field::prepare(glm::dvec3 const &Camera, std::function<bool(scene::bo
|
||||
draw.part = glm::vec4{fill, static_cast<float>(segments), seedx, seedz};
|
||||
draw.count = count;
|
||||
draw.segments = segments;
|
||||
draw.distance = distance;
|
||||
m_tiledraws.push_back(draw);
|
||||
++m_chunkdraws.back().count;
|
||||
m_chunkdraws.back().nearest = std::min(m_chunkdraws.back().nearest, distance);
|
||||
m_triangles += static_cast<std::size_t>(count) * (segments * 2 - 1);
|
||||
}
|
||||
}
|
||||
// from the nearest: the blades in front are drawn first, and those they hide fail the depth test before they're shaded
|
||||
for (auto const &chunk : m_chunkdraws)
|
||||
{
|
||||
auto const first{m_tiledraws.begin() + static_cast<std::ptrdiff_t>(chunk.first)};
|
||||
std::ranges::sort(first, first + static_cast<std::ptrdiff_t>(chunk.count), {}, &tile_draw::distance);
|
||||
}
|
||||
std::ranges::sort(m_chunkdraws, {}, &chunk_draw::nearest);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ class grass_field
|
||||
glm::vec4 share{0.f};
|
||||
std::size_t first{0}; // tiles drawn, in tile_draws()
|
||||
std::size_t count{0};
|
||||
float nearest{0.f}; // distance to the nearest of its tiles; the chunks are drawn from the nearest one
|
||||
};
|
||||
// a tile drawn: Count instances of a strip of 2 * Segments + 1 vertices
|
||||
struct tile_draw
|
||||
@@ -71,6 +72,7 @@ class grass_field
|
||||
glm::vec4 part{0.f}; // grass_part
|
||||
int count{0};
|
||||
int segments{0};
|
||||
float distance{0.f}; // to the box of the tile; the tiles of a chunk are drawn from the nearest one
|
||||
};
|
||||
// the draws of the grass seen from Camera: false when there is nothing to draw (grass off, no chunks or kinds), the draws of
|
||||
// the last call are left then. Visible: whether the bounds of a tile are seen
|
||||
|
||||
@@ -184,7 +184,7 @@ std::string vulkan14_renderer::scene_info()
|
||||
}
|
||||
if (scene.raytracing && m_raytracingon)
|
||||
{
|
||||
text += scene.raytracing->info() + "\n";
|
||||
text += scene.raytracing->info() + "; processor: walk " + to_string(m_rtwalkms, 2) + " ms, build " + to_string(m_rtbuildms, 2) + " ms, " + std::to_string(m_rtdraws.size()) + " draws\n";
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
@@ -2819,10 +2819,6 @@ void vulkan14_renderer::render_grass(glm::mat4 const &Future)
|
||||
auto const cullface{draw.cullface};
|
||||
draw.cullface = false;
|
||||
apply_draw_state();
|
||||
if (draw.set_dirty)
|
||||
{
|
||||
bind_pass_set();
|
||||
}
|
||||
glm::mat4 const view{OpenGLMatrices.data(GL_MODELVIEW)};
|
||||
auto const &frame{grass.frame()};
|
||||
auto const &tiles{grass.tile_draws()};
|
||||
@@ -2840,6 +2836,12 @@ void vulkan14_renderer::render_grass(glm::mat4 const &Future)
|
||||
{
|
||||
break;
|
||||
}
|
||||
// the block may have opened a new window of the blocks, which the pass set is bound with: without binding it again the
|
||||
// chunk (and those after it) would read the blocks of other draws, and the grass would flicker
|
||||
if (draw.set_dirty)
|
||||
{
|
||||
bind_pass_set();
|
||||
}
|
||||
std::array<VkDescriptorImageInfo, gl::MAX_TEXTURES> descriptors;
|
||||
for (std::size_t unit = 0; unit < gl::MAX_TEXTURES; ++unit)
|
||||
{
|
||||
|
||||
@@ -431,12 +431,16 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
std::uint32_t waiting{0};
|
||||
VkDeviceSize scratchsize{0};
|
||||
auto const scratchalignment{static_cast<VkDeviceSize>(m_scratchalignment)};
|
||||
for (auto const &draw : Draws)
|
||||
// the structure of each draw, looked up once (null: none, the draw is left out); the map keeps its entries in place
|
||||
std::vector<bottom const *> found(Draws.size(), nullptr);
|
||||
for (std::size_t drawindex = 0; drawindex < Draws.size(); ++drawindex)
|
||||
{
|
||||
auto const &draw{Draws[drawindex]};
|
||||
auto lookup{m_bottoms.find(draw.key)};
|
||||
if ((lookup != m_bottoms.end()) && (lookup->second.generation == draw.source.generation))
|
||||
{
|
||||
lookup->second.used = Frame;
|
||||
found[drawindex] = &lookup->second;
|
||||
continue;
|
||||
}
|
||||
if ((builds.size() >= builds_per_frame) || (triangles >= triangles_per_frame))
|
||||
@@ -533,6 +537,7 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
entry.index_address = (generated ? generated->address() : 0);
|
||||
entry.indices = std::move(generated);
|
||||
m_bottombytes += entry.bytes;
|
||||
found[drawindex] = &entry;
|
||||
build.info.dstAccelerationStructure = structure;
|
||||
build.scratch = scratchsize;
|
||||
scratchsize += (sizes.buildScratchSize + scratchalignment - 1) / scratchalignment * scratchalignment;
|
||||
@@ -555,6 +560,7 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
{
|
||||
m_bottombytes -= it->second.bytes;
|
||||
m_deletions->push(Value, [handle, old = it->second.handle, oldstorage = it->second.storage, oldindices = it->second.indices]() { destroy_structure(handle, old); });
|
||||
std::ranges::replace(found, static_cast<bottom const *>(&it->second), static_cast<bottom const *>(nullptr));
|
||||
m_bottoms.erase(it);
|
||||
break;
|
||||
}
|
||||
@@ -584,15 +590,7 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
}
|
||||
|
||||
// the records and the instances of the draws with their structures
|
||||
std::size_t instancecount{0};
|
||||
for (auto const &draw : Draws)
|
||||
{
|
||||
auto const lookup{m_bottoms.find(draw.key)};
|
||||
if ((lookup != m_bottoms.end()) && (lookup->second.generation == draw.source.generation))
|
||||
{
|
||||
++instancecount;
|
||||
}
|
||||
}
|
||||
auto instancecount{static_cast<std::size_t>(std::ranges::count_if(found, [](bottom const *Built) { return Built != nullptr; }))};
|
||||
instancecount = std::min<std::size_t>(instancecount, max_instances);
|
||||
if ((false == ensure(frame.records, sizeof(record) * std::max<std::size_t>(instancecount, 1), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, memory_kind::upload, "ray tracing records", Value)) ||
|
||||
(false == ensure(frame.instances, sizeof(VkAccelerationStructureInstanceKHR) * std::max<std::size_t>(instancecount, 1),
|
||||
@@ -621,17 +619,18 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
return (layersentry != nullptr) && ((terrainlayers == 0) || (terrainlayers == Id));
|
||||
};
|
||||
std::uint32_t written{0};
|
||||
for (auto const &draw : Draws)
|
||||
if ((records == nullptr) || (instances == nullptr))
|
||||
{
|
||||
if ((written >= instancecount) || (records == nullptr) || (instances == nullptr))
|
||||
{
|
||||
break;
|
||||
}
|
||||
auto const lookup{m_bottoms.find(draw.key)};
|
||||
if ((lookup == m_bottoms.end()) || (lookup->second.generation != draw.source.generation))
|
||||
instancecount = 0;
|
||||
}
|
||||
for (std::size_t drawindex = 0; (drawindex < Draws.size()) && (written < instancecount); ++drawindex)
|
||||
{
|
||||
auto const *built{found[drawindex]};
|
||||
if (built == nullptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
auto const &draw{Draws[drawindex]};
|
||||
auto const &source{draw.source};
|
||||
auto const terrain{draw.terrain_table != 0};
|
||||
auto const texture{texture_index(terrain ? draw.terrain_weights[0] : draw.texture, Frame)};
|
||||
@@ -639,9 +638,11 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
{
|
||||
usedtextures.push_back(texture);
|
||||
}
|
||||
auto &entry{records[written]};
|
||||
// made here and copied whole: the buffers are write-combined memory of the upload heap, where setting the bit fields of
|
||||
// an instance in place reads the memory back, which takes a few microseconds for each draw
|
||||
record entry{};
|
||||
entry.vertices = source.vertex_address + vertex_stride * source.vertexfirst;
|
||||
entry.indices = (lookup->second.index_address != 0 ? lookup->second.index_address : source.indices != VK_NULL_HANDLE ? source.index_address + sizeof(std::uint32_t) * source.first : 0);
|
||||
entry.indices = (built->index_address != 0 ? built->index_address : source.indices != VK_NULL_HANDLE ? source.index_address + sizeof(std::uint32_t) * source.first : 0);
|
||||
entry.texture = texture;
|
||||
auto const alpha{(false == terrain) && (texture != 0) && (draw.cutoff > 0.f)};
|
||||
entry.flags = (alpha ? record_alpha : 0);
|
||||
@@ -673,7 +674,7 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
entry.texture = 0;
|
||||
}
|
||||
}
|
||||
auto &instance{instances[written]};
|
||||
VkAccelerationStructureInstanceKHR instance{};
|
||||
// the rows of the transform, which glm keeps by the columns
|
||||
for (int row = 0; row < 3; ++row)
|
||||
{
|
||||
@@ -688,7 +689,9 @@ void ray_tracing::build(command_stream *Commands, std::uint32_t const Slot, std:
|
||||
instance.instanceShaderBindingTableRecordOffset = 0;
|
||||
// the models have faces of one side, seen from both in the reflections; an alpha tested texture leaves holes in its faces
|
||||
instance.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR | (alpha ? VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR : VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR);
|
||||
instance.accelerationStructureReference = lookup->second.address;
|
||||
instance.accelerationStructureReference = built->address;
|
||||
records[written] = entry;
|
||||
instances[written] = instance;
|
||||
++written;
|
||||
}
|
||||
if (written > 0)
|
||||
|
||||
@@ -722,13 +722,14 @@ bool vulkan14_renderer::init_scene(std::string &Reason)
|
||||
auto const cullface{draw.cullface};
|
||||
draw.cullface = false;
|
||||
apply_draw_state();
|
||||
if (draw.set_dirty)
|
||||
{
|
||||
bind_pass_set();
|
||||
}
|
||||
auto const block{push_draw_block()};
|
||||
if (block != ~0u)
|
||||
{
|
||||
// after the block, which may have opened a new window of the blocks the pass set is bound with
|
||||
if (draw.set_dirty)
|
||||
{
|
||||
bind_pass_set();
|
||||
}
|
||||
if (draw.textures_dirty)
|
||||
{
|
||||
push_textures();
|
||||
|
||||
@@ -37,22 +37,12 @@ layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 2) uniform sampler
|
||||
// grid along a side, grass samples along a side less one)
|
||||
layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 3) uniform sampler2D grass_kinds;
|
||||
|
||||
vec4 grass_base[4];
|
||||
vec4 grass_tip[4];
|
||||
vec4 grass_shape[4];
|
||||
vec4 grass_dry[4];
|
||||
vec4 grass_share;
|
||||
vec4 grass_chunk;
|
||||
|
||||
void read_kinds()
|
||||
// what every vertex needs, the blades left out too; the column of the kind is read once the blade is known to be drawn
|
||||
void read_chunk()
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
grass_base[i] = texelFetch(grass_kinds, ivec2(i, 0), 0);
|
||||
grass_tip[i] = texelFetch(grass_kinds, ivec2(i, 1), 0);
|
||||
grass_shape[i] = texelFetch(grass_kinds, ivec2(i, 2), 0);
|
||||
grass_dry[i] = texelFetch(grass_kinds, ivec2(i, 3), 0);
|
||||
}
|
||||
grass_share = texelFetch(grass_kinds, ivec2(0, 4), 0);
|
||||
grass_chunk = texelFetch(grass_kinds, ivec2(1, 4), 0);
|
||||
}
|
||||
@@ -128,7 +118,7 @@ void collapse()
|
||||
|
||||
void main()
|
||||
{
|
||||
read_kinds();
|
||||
read_chunk();
|
||||
int blade = gl_InstanceIndex;
|
||||
// the numbers of the blade, drawn in the same order whatever becomes of them
|
||||
uint state = hash(uint(blade) * 0x9E3779B9U ^ hash(uint(int(grass_part.z)) * 0x85EBCA6BU ^ hash(uint(int(grass_part.w)) + 0x27D4EB2FU)));
|
||||
@@ -153,6 +143,15 @@ void main()
|
||||
collapse();
|
||||
return;
|
||||
}
|
||||
// a blade past the share drawn at its distance along the ground is left out before its paint and its ground are read: its
|
||||
// root is no nearer than that, and the share only falls with the distance
|
||||
float rank = (float(blade) + 0.5) / grass_tile.w;
|
||||
float flatdistance = length(point - grass_camera.xz);
|
||||
if (flatdistance > grass_lod.z || rank >= lod_share(flatdistance))
|
||||
{
|
||||
collapse();
|
||||
return;
|
||||
}
|
||||
vec2 local = point / grass_chunk.x;
|
||||
float samples = grass_chunk.z;
|
||||
vec4 density = texture(grass_density, (local * samples + 0.5) / (samples + 1.0));
|
||||
@@ -180,19 +179,21 @@ void main()
|
||||
vec3 root = vec3(point.x, ground(point, groundnormal), point.y);
|
||||
float distance = length(root - grass_camera.xyz);
|
||||
float share = lod_share(distance);
|
||||
float rank = (float(blade) + 0.5) / grass_tile.w;
|
||||
if (kind < 0 || hole > 0.5 || rank >= share || distance > grass_lod.z)
|
||||
{
|
||||
collapse();
|
||||
return;
|
||||
}
|
||||
|
||||
vec4 shape = grass_shape[kind];
|
||||
vec4 kindbase = texelFetch(grass_kinds, ivec2(kind, 0), 0);
|
||||
vec4 kindtip = texelFetch(grass_kinds, ivec2(kind, 1), 0);
|
||||
vec4 shape = texelFetch(grass_kinds, ivec2(kind, 2), 0);
|
||||
vec4 kinddry = texelFetch(grass_kinds, ivec2(kind, 3), 0);
|
||||
float height = shape.x * (1.0 + shape.y * heightrandom);
|
||||
// the last of the blades drawn at the distance grow in, and the grass fades out at the end of its reach
|
||||
height *= 1.0 - smoothstep(share * 0.8, share, rank);
|
||||
// lower in winter, growing in spring
|
||||
float seasonal = grass_dry[kind].a;
|
||||
float seasonal = kinddry.a;
|
||||
height *= mix(1.0, grass_season.z, seasonal);
|
||||
// where the paint thins out the blades get shorter
|
||||
height *= mix(0.5, 1.0, clamp(density[kind] * 1.5, 0.0, 1.0));
|
||||
@@ -230,8 +231,8 @@ void main()
|
||||
// the seasons: some blades dry out (and green up) earlier than others, so a field turns gradually; a dry blade takes the
|
||||
// dry colour, darker at its root. in spring the green is fresher
|
||||
float dry = clamp(grass_season.x * seasonal * (0.6 + 0.8 * early), 0.0, 1.0);
|
||||
vec3 bottom = mix(grass_base[kind].rgb, grass_dry[kind].rgb * 0.45, dry);
|
||||
vec3 top = mix(grass_tip[kind].rgb, grass_dry[kind].rgb, dry);
|
||||
vec3 bottom = mix(kindbase.rgb, kinddry.rgb * 0.45, dry);
|
||||
vec3 top = mix(kindtip.rgb, kinddry.rgb, dry);
|
||||
top *= mix(vec3(1.0), vec3(1.08, 1.22, 0.85), grass_season.y * seasonal * (1.0 - dry));
|
||||
vec3 albedo = mix(bottom, top, t) * (0.85 + 0.3 * shade);
|
||||
// the lower part of the blades is shaded by the others
|
||||
|
||||
Reference in New Issue
Block a user