mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 09:15:01 +01:00
Some optimalisations
This commit is contained in:
@@ -643,9 +643,9 @@ void scene_walk::Render(cell_sequence::iterator First, cell_sequence::iterator L
|
||||
}
|
||||
// remaining (non-instanceable) opaque instance nodes go through the per-node path, or with the instances
|
||||
// to the gpu culling, each on its own
|
||||
for (auto *instance : cell->m_instancesopaque)
|
||||
// (the instanceable ones are handled via bucket)
|
||||
for (auto *instance : Single_Instances(*cell))
|
||||
{
|
||||
if( instance->m_instanceable ) { continue; } // already handled via bucket
|
||||
// the model of a lamp would shadow everything in the map of the lamp
|
||||
if( instance == m_cubeskipinstance ) { continue; }
|
||||
if( walk_queue_model( instance, cellinrange ) ) { continue; }
|
||||
@@ -680,8 +680,7 @@ void scene_walk::Render(cell_sequence::iterator First, cell_sequence::iterator L
|
||||
// opaque parts of instanced models -- accumulate into the
|
||||
// frame-level map; flushed once per unique model after the loop.
|
||||
Queue_Instances( cell );
|
||||
for( auto *instance : cell->m_instancesopaque ) {
|
||||
if( instance->m_instanceable ) { continue; }
|
||||
for( auto *instance : Single_Instances( *cell ) ) {
|
||||
if( walk_queue_model( instance, true ) ) { continue; }
|
||||
Render( instance );
|
||||
m_modelcomparekey = nullptr;
|
||||
@@ -1074,6 +1073,32 @@ void scene_walk::Update_Instance_Records( TModel3d const *Model, scene::basic_ce
|
||||
Bucket.gpustale = true;
|
||||
}
|
||||
|
||||
// the opaque instances of a scene cell drawn each on its own. an instance stops being drawn with the others of its model by an event
|
||||
// (TAnimModel::AddContainer()), which changes scene::node_state_version and with it the version of the instance records; the editor
|
||||
// makes the records anew in every frame. the list is made once for a version, before any walk of the frame reads it, so a walk going
|
||||
// through it doesn't see it change
|
||||
scene::basic_cell::instance_sequence const &scene_walk::Single_Instances( scene::basic_cell const &Cell ) const
|
||||
{
|
||||
auto const stale = [ this, &Cell ]() {
|
||||
return ( Cell.m_instancessingleversion != m_instancerecordsversion )
|
||||
|| ( Cell.m_instancessinglesource != Cell.m_instancesopaque.size() ); };
|
||||
if( stale() ) {
|
||||
// the lists the passes share are made by one walk at a time (walk_sync)
|
||||
gfx::walk_sync::change change;
|
||||
if( stale() ) {
|
||||
Cell.m_instancessingle.clear();
|
||||
for( auto *instance : Cell.m_instancesopaque ) {
|
||||
if( false == instance->m_instanceable ) {
|
||||
Cell.m_instancessingle.emplace_back( instance );
|
||||
}
|
||||
}
|
||||
Cell.m_instancessinglesource = Cell.m_instancesopaque.size();
|
||||
Cell.m_instancessingleversion = m_instancerecordsversion;
|
||||
}
|
||||
}
|
||||
return Cell.m_instancessingle;
|
||||
}
|
||||
|
||||
// whether none of the instances of a bucket can pass the tests of the culling of the pass (Render_Instanced(), on the gpu
|
||||
// instance_cull.comp): by the bounds of their records the visible ones lie wholly out of the frustum of the pass, beyond their
|
||||
// range or the draw range, or are too small on the screen. the tests are those of the culling with the nearest location and
|
||||
|
||||
@@ -153,6 +153,8 @@ class scene_walk
|
||||
void Queue_Instances(scene::basic_cell const *Cell);
|
||||
// makes the records of the instances held by a bucket of a scene cell, with locations relative to Origin
|
||||
void Update_Instance_Records(TModel3d const *Model, scene::basic_cell::instance_bucket const &Bucket, glm::dvec3 const &Origin);
|
||||
// the opaque instances of a scene cell which aren't drawn with the others of their model, made anew with the instance records
|
||||
scene::basic_cell::instance_sequence const &Single_Instances(scene::basic_cell const &Cell) const;
|
||||
// whether none of the instances of a bucket can pass the culling of the current pass, by the bounds of their records
|
||||
bool Bucket_Culled(scene::basic_cell::instance_bucket const &Bucket) const;
|
||||
// the sphere around the locations of the visible instances of a bucket, with a margin for the precision of the records
|
||||
@@ -344,8 +346,9 @@ class scene_walk
|
||||
// whether two recorded draws can go in one multi draw of the colour pass: the same material and texture variant, geometry
|
||||
// bank, primitive and blending
|
||||
bool Indirect_Mergeable(std::size_t const First, std::size_t const Other) const;
|
||||
// the texture the shadow material binds for the alpha test of a material, or null_handle (no test)
|
||||
texture_handle Indirect_Shadow_Texture(material_handle const Material) const;
|
||||
// the texture the shadow material binds for the alpha test of a material, or null_handle (no test). a renderer which knows a
|
||||
// texture passes the test everywhere draws its shadows without one
|
||||
virtual texture_handle Indirect_Shadow_Texture(material_handle const Material) const;
|
||||
// the records of the sleepers and of the models drawn on their own not queued for a while are let go
|
||||
void Expire_Indirect_Records();
|
||||
// a scenery is being loaded: the tracks and the models the records were made for may be gone
|
||||
|
||||
@@ -314,6 +314,8 @@ void sky_occlusion::stop()
|
||||
|
||||
void sky_occlusion::clear()
|
||||
{
|
||||
// the cells go with the scenery
|
||||
m_cellsignatures.clear();
|
||||
// once, until a scenery is there again
|
||||
if ((m_side <= 0) || (m_region == nullptr))
|
||||
return;
|
||||
@@ -628,20 +630,39 @@ std::uint64_t sky_occlusion::signature(scene::basic_region *Region, glm::ivec2 c
|
||||
if (overlaps(data.area, shape_reach(shape)))
|
||||
hash = hash_mix(hash, static_cast<std::uint64_t>(data.geometry) ^ static_cast<std::uint64_t>(data.visible ? 1 : 0));
|
||||
}
|
||||
for (auto const *instance : cell.m_instancesopaque)
|
||||
{
|
||||
hash = hash_mix(hash, static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(instance)) ^ static_cast<std::uint64_t>(instance->visible() ? 1 : 0));
|
||||
auto const location{instance->location()};
|
||||
hash = hash_mix_double(hash, location.x);
|
||||
hash = hash_mix_double(hash, location.y);
|
||||
hash = hash_mix_double(hash, location.z);
|
||||
hash = hash_mix_double(hash, static_cast<double>(instance->vAngle.y));
|
||||
}
|
||||
hash = hash_mix(hash, instances_signature(cell));
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
std::uint64_t sky_occlusion::instances_signature(scene::basic_cell const &Cell)
|
||||
{
|
||||
auto const make = [&Cell]() {
|
||||
std::uint64_t hash{0x1a57a2ce};
|
||||
for (auto const *instance : Cell.m_instancesopaque)
|
||||
{
|
||||
hash = hash_mix(hash, static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(instance)) ^ static_cast<std::uint64_t>(instance->visible() ? 1 : 0));
|
||||
auto const location{instance->location()};
|
||||
hash = hash_mix_double(hash, location.x);
|
||||
hash = hash_mix_double(hash, location.y);
|
||||
hash = hash_mix_double(hash, location.z);
|
||||
hash = hash_mix_double(hash, static_cast<double>(instance->vAngle.y));
|
||||
}
|
||||
return hash;
|
||||
};
|
||||
// the editor moves the instances and hides them with their layers without the version changing: read anew each time there, and
|
||||
// kept for when it's left
|
||||
auto &kept{m_cellsignatures[&Cell]};
|
||||
if (EditorModeFlag || (kept.version != scene::node_state_version) || (kept.count != Cell.m_instancesopaque.size()))
|
||||
{
|
||||
kept.hash = make();
|
||||
kept.version = scene::node_state_version;
|
||||
kept.count = Cell.m_instancesopaque.size();
|
||||
}
|
||||
return kept.hash;
|
||||
}
|
||||
|
||||
void sky_occlusion::gather(scene::basic_region *Region, glm::ivec2 const &Tile, std::vector<triangle> &Triangles)
|
||||
{
|
||||
auto const tilesize{static_cast<double>(m_layout.tile)};
|
||||
|
||||
@@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "gl/ubo.h"
|
||||
@@ -29,7 +30,8 @@ class TSubModel;
|
||||
namespace scene
|
||||
{
|
||||
class basic_region;
|
||||
}
|
||||
class basic_cell;
|
||||
} // namespace scene
|
||||
|
||||
namespace gfx
|
||||
{
|
||||
@@ -181,6 +183,8 @@ class sky_occlusion
|
||||
void gather_tunnels(scene::basic_region *Region, glm::ivec2 const &Tile, std::vector<glm::vec2> &Points);
|
||||
// what the scenery over a tile is made of, cheap to work out: a change of it means the tile is gathered again
|
||||
std::uint64_t signature(scene::basic_region *Region, glm::ivec2 const &Tile);
|
||||
// the part of the signature made of the opaque instances of a cell: their places and whether they're shown
|
||||
std::uint64_t instances_signature(scene::basic_cell const &Cell);
|
||||
// the values of a tile nothing is known of yet, in the textures and in the copy of the cpu
|
||||
void neutral(int const Slot);
|
||||
void apply(tile_result &Result);
|
||||
@@ -208,6 +212,16 @@ class sky_occlusion
|
||||
std::vector<glm::vec4> m_neutralheights;
|
||||
std::vector<std::uint16_t> m_neutralocclusion;
|
||||
std::size_t m_checkslot{0}; // the slot whose signature is checked next
|
||||
// the part of the signatures made of the instances of a cell, worked out for a scene::node_state_version and a count of its
|
||||
// instances. outside the editor an instance is shown or hidden only with the version changing, and isn't moved; reading every
|
||||
// instance of the cells around for each check took a large part of the frame in the forests
|
||||
struct cell_signature
|
||||
{
|
||||
unsigned int version{0};
|
||||
std::size_t count{0};
|
||||
std::uint64_t hash{0};
|
||||
};
|
||||
std::unordered_map<scene::basic_cell const *, cell_signature> m_cellsignatures;
|
||||
scene::basic_region const *m_region{nullptr};
|
||||
|
||||
// shared with the thread, under m_mutex
|
||||
|
||||
219
rendering/common/texturealpha.cpp
Normal file
219
rendering/common/texturealpha.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
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/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "rendering/common/texturealpha.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
|
||||
namespace gfx
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using block_check = bool (*)(std::uint8_t const *Block);
|
||||
|
||||
bool rgba8_kept(std::span<std::uint8_t const> const Data)
|
||||
{
|
||||
for (std::size_t alpha = 3; alpha < Data.size(); alpha += 4)
|
||||
{
|
||||
if (Data[alpha] < alpha_test_kept)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// bc1: with the first colour above the second the block has four colours and no alpha; otherwise the index 3 is transparent black
|
||||
bool bc1_kept(std::uint8_t const *Block)
|
||||
{
|
||||
auto const colour0{static_cast<unsigned>(Block[0]) | (static_cast<unsigned>(Block[1]) << 8)};
|
||||
auto const colour1{static_cast<unsigned>(Block[2]) | (static_cast<unsigned>(Block[3]) << 8)};
|
||||
if (colour0 > colour1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
for (std::size_t row = 4; row < 8; ++row)
|
||||
{
|
||||
// both bits of a 2 bit index set: the index 3
|
||||
if ((Block[row] & (Block[row] >> 1) & 0x55u) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// bc2: 4 bits of alpha per texel, kept from 8 up (the top bit of each half of a byte)
|
||||
bool bc2_kept(std::uint8_t const *Block)
|
||||
{
|
||||
for (std::size_t byte = 0; byte < 8; ++byte)
|
||||
{
|
||||
if ((Block[byte] & 0x88u) != 0x88u)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// bc3: two alpha endpoints and 3 bit indices into the palette they make
|
||||
bool bc3_kept(std::uint8_t const *Block)
|
||||
{
|
||||
auto const alpha0{static_cast<unsigned>(Block[0])};
|
||||
auto const alpha1{static_cast<unsigned>(Block[1])};
|
||||
std::array<unsigned, 8> palette{alpha0, alpha1, 0u, 0u, 0u, 0u, 0u, 0u};
|
||||
if (alpha0 > alpha1)
|
||||
{
|
||||
for (unsigned index = 2; index < 8; ++index)
|
||||
{
|
||||
palette[index] = ((8 - index) * alpha0 + (index - 1) * alpha1) / 7;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned index = 2; index < 6; ++index)
|
||||
{
|
||||
palette[index] = ((6 - index) * alpha0 + (index - 1) * alpha1) / 5;
|
||||
}
|
||||
palette[6] = 0;
|
||||
palette[7] = 255;
|
||||
}
|
||||
std::uint64_t indices{0};
|
||||
for (std::size_t byte = 0; byte < 6; ++byte)
|
||||
{
|
||||
indices |= static_cast<std::uint64_t>(Block[2 + byte]) << (8 * byte);
|
||||
}
|
||||
for (unsigned texel = 0; texel < 16; ++texel)
|
||||
{
|
||||
if (palette[(indices >> (3 * texel)) & 7u] < alpha_test_kept)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Count bits of a bc7 block from bit Offset, the lowest first
|
||||
unsigned bc7_bits(std::uint8_t const *Block, unsigned const Offset, unsigned const Count)
|
||||
{
|
||||
unsigned result{0};
|
||||
for (unsigned bit = 0; bit < Count; ++bit)
|
||||
{
|
||||
auto const index{Offset + bit};
|
||||
result |= ((static_cast<unsigned>(Block[index >> 3]) >> (index & 7u)) & 1u) << bit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// an endpoint of Bits bits as the decoder makes it 8 bits wide
|
||||
unsigned bc7_expand(unsigned const Value, unsigned const Bits)
|
||||
{
|
||||
return (Value << (8 - Bits)) | (Value >> (2 * Bits - 8));
|
||||
}
|
||||
|
||||
// modes 4 and 5: one subset with alpha endpoints of their own, which the rotation swaps with one of the colour channels
|
||||
bool bc7_rotated_kept(std::uint8_t const *Block, unsigned const Mode)
|
||||
{
|
||||
auto const rotation{bc7_bits(Block, Mode + 1, 2)};
|
||||
auto const colourbits{Mode == 4 ? 5u : 7u};
|
||||
auto const alphabits{Mode == 4 ? 6u : 8u};
|
||||
auto const bits{rotation == 0 ? alphabits : colourbits};
|
||||
auto const start{rotation == 0 ? 8 + 6 * colourbits : 8 + 2 * colourbits * (rotation - 1)};
|
||||
for (unsigned endpoint = 0; endpoint < 2; ++endpoint)
|
||||
{
|
||||
if (bc7_expand(bc7_bits(Block, start + endpoint * bits, bits), bits) < alpha_test_kept)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// bc7: the alpha of a texel lies between the alpha endpoints of its subset, so the endpoints decide
|
||||
bool bc7_kept(std::uint8_t const *Block)
|
||||
{
|
||||
if (Block[0] == 0)
|
||||
{
|
||||
// the reserved mode decodes to transparent black
|
||||
return false;
|
||||
}
|
||||
unsigned mode{0};
|
||||
while ((Block[0] & (1u << mode)) == 0)
|
||||
{
|
||||
++mode;
|
||||
}
|
||||
switch (mode)
|
||||
{
|
||||
case 4:
|
||||
case 5:
|
||||
return bc7_rotated_kept(Block, mode);
|
||||
case 6:
|
||||
// 7 bit alpha endpoints, each with its own p-bit
|
||||
for (unsigned endpoint = 0; endpoint < 2; ++endpoint)
|
||||
{
|
||||
if (((bc7_bits(Block, 49 + endpoint * 7, 7) << 1) | bc7_bits(Block, 63 + endpoint, 1)) < alpha_test_kept)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
case 7:
|
||||
// two subsets, 5 bit alpha endpoints made 6 bits by their p-bits
|
||||
for (unsigned endpoint = 0; endpoint < 4; ++endpoint)
|
||||
{
|
||||
if (bc7_expand((bc7_bits(Block, 74 + endpoint * 5, 5) << 1) | bc7_bits(Block, 94 + endpoint, 1), 6) < alpha_test_kept)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
// modes 0-3 hold no alpha: opaque
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool blocks_kept(std::span<std::uint8_t const> const Data, std::size_t const Size, block_check const Check)
|
||||
{
|
||||
for (std::size_t offset = 0; offset + Size <= Data.size(); offset += Size)
|
||||
{
|
||||
if (false == Check(Data.data() + offset))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool texels_pass_alpha_test(alpha_layout const Layout, std::span<std::uint8_t const> const Data)
|
||||
{
|
||||
switch (Layout)
|
||||
{
|
||||
case alpha_layout::rgba8:
|
||||
return rgba8_kept(Data);
|
||||
case alpha_layout::bc1:
|
||||
return blocks_kept(Data, 8, bc1_kept);
|
||||
case alpha_layout::bc2:
|
||||
return blocks_kept(Data, 16, bc2_kept);
|
||||
case alpha_layout::bc3:
|
||||
return blocks_kept(Data, 16, bc3_kept);
|
||||
case alpha_layout::bc7:
|
||||
return blocks_kept(Data, 16, bc7_kept);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gfx
|
||||
38
rendering/common/texturealpha.h
Normal file
38
rendering/common/texturealpha.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
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/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
namespace gfx
|
||||
{
|
||||
|
||||
// how the texels of a texture's data are laid out: rgba (or bgra) bytes, or the 4x4 blocks of the compressed formats
|
||||
enum class alpha_layout
|
||||
{
|
||||
rgba8,
|
||||
bc1,
|
||||
bc2,
|
||||
bc3,
|
||||
bc7
|
||||
};
|
||||
|
||||
// the alpha the alpha test of the shadow maps (alphashadowmap.frag) keeps for sure: it discards below a half, and this leaves a
|
||||
// margin for the decoders and the filtering of the gpus (136/255, the 8/15 of the 4 bit alpha of bc2)
|
||||
constexpr std::uint8_t alpha_test_kept{136};
|
||||
|
||||
// whether every texel of Data (the levels of the texture one after another, whole blocks) decodes to an alpha of at least
|
||||
// alpha_test_kept. the filtering only mixes texels, so the alpha test then keeps every fragment, and a program without the test
|
||||
// draws the same depth. the compressed blocks are checked by what they can decode to, so the answer errs on the side of a
|
||||
// translucent texture
|
||||
bool texels_pass_alpha_test(alpha_layout Layout, std::span<std::uint8_t const> Data);
|
||||
|
||||
} // namespace gfx
|
||||
Reference in New Issue
Block a user