codex-5.2: ресурсы
@@ -35,11 +35,11 @@ struct VoxelVertexPoint {
|
|||||||
|
|
||||||
struct NodeVertexStatic {
|
struct NodeVertexStatic {
|
||||||
uint32_t
|
uint32_t
|
||||||
FX : 9, FY : 9, FZ : 9, // Позиция -224 ~ 288; 64 позиций в одной ноде, 7.5 метров в ряд
|
FX : 11, FY : 11, N1 : 10, // Позиция, 64 позиции на метр, +3.5м запас
|
||||||
N1 : 4, // Не занято
|
FZ : 11, // Позиция
|
||||||
LS : 1, // Масштаб карты освещения (1м/16 или 1м)
|
LS : 1, // Масштаб карты освещения (1м/16 или 1м)
|
||||||
Tex : 18, // Текстура
|
Tex : 18, // Текстура
|
||||||
N2 : 14, // Не занято
|
N2 : 2, // Не занято
|
||||||
TU : 16, TV : 16; // UV на текстуре
|
TU : 16, TV : 16; // UV на текстуре
|
||||||
|
|
||||||
bool operator==(const NodeVertexStatic& other) const {
|
bool operator==(const NodeVertexStatic& other) const {
|
||||||
|
|||||||
198
Src/Client/Vulkan/AtlasPipeline/PipelinedTextureAtlas.cpp
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
#include "PipelinedTextureAtlas.hpp"
|
||||||
|
|
||||||
|
PipelinedTextureAtlas::PipelinedTextureAtlas(TextureAtlas&& tk)
|
||||||
|
: Super(std::move(tk)) {}
|
||||||
|
|
||||||
|
PipelinedTextureAtlas::AtlasTextureId PipelinedTextureAtlas::getByPipeline(const HashedPipeline& pipeline) {
|
||||||
|
auto iter = _PipeToTexId.find(pipeline);
|
||||||
|
if (iter == _PipeToTexId.end()) {
|
||||||
|
AtlasTextureId atlasTexId = Super.registerTexture();
|
||||||
|
_PipeToTexId.insert({pipeline, atlasTexId});
|
||||||
|
_ChangedPipelines.push_back(pipeline);
|
||||||
|
|
||||||
|
for (uint32_t texId : pipeline.getDependencedTextures()) {
|
||||||
|
_AddictedTextures[texId].push_back(pipeline);
|
||||||
|
}
|
||||||
|
|
||||||
|
return atlasTexId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::freeByPipeline(const HashedPipeline& pipeline) {
|
||||||
|
auto iter = _PipeToTexId.find(pipeline);
|
||||||
|
if (iter == _PipeToTexId.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint32_t texId : pipeline.getDependencedTextures()) {
|
||||||
|
auto iterAT = _AddictedTextures.find(texId);
|
||||||
|
assert(iterAT != _AddictedTextures.end());
|
||||||
|
auto iterATSub = std::find(iterAT->second.begin(), iterAT->second.end(), pipeline);
|
||||||
|
assert(iterATSub != iterAT->second.end());
|
||||||
|
iterAT->second.erase(iterATSub);
|
||||||
|
}
|
||||||
|
|
||||||
|
Super.removeTexture(iter->second);
|
||||||
|
_AtlasCpuTextures.erase(iter->second);
|
||||||
|
_PipeToTexId.erase(iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::updateTexture(uint32_t texId, const StoredTexture& texture) {
|
||||||
|
_ResToTexture[texId] = texture;
|
||||||
|
_ChangedTextures.push_back(texId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::updateTexture(uint32_t texId, StoredTexture&& texture) {
|
||||||
|
_ResToTexture[texId] = std::move(texture);
|
||||||
|
_ChangedTextures.push_back(texId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::freeTexture(uint32_t texId) {
|
||||||
|
auto iter = _ResToTexture.find(texId);
|
||||||
|
if (iter != _ResToTexture.end()) {
|
||||||
|
_ResToTexture.erase(iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PipelinedTextureAtlas::getHostTexture(TextureId texId, HostTextureView& out) const {
|
||||||
|
auto fill = [&](const StoredTexture& tex) -> bool {
|
||||||
|
if (tex._Pixels.empty() || tex._Widht == 0 || tex._Height == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.width = tex._Widht;
|
||||||
|
out.height = tex._Height;
|
||||||
|
out.rowPitchBytes = static_cast<uint32_t>(tex._Widht) * 4u;
|
||||||
|
out.pixelsRGBA8 = reinterpret_cast<const uint8_t*>(tex._Pixels.data());
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto it = _ResToTexture.find(texId);
|
||||||
|
if (it != _ResToTexture.end() && fill(it->second)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto itAtlas = _AtlasCpuTextures.find(texId);
|
||||||
|
if (itAtlas != _AtlasCpuTextures.end() && fill(itAtlas->second)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
StoredTexture PipelinedTextureAtlas::_generatePipelineTexture(const HashedPipeline& pipeline) {
|
||||||
|
std::vector<detail::Word> words(pipeline._Pipeline.begin(), pipeline._Pipeline.end());
|
||||||
|
if (words.empty()) {
|
||||||
|
if (auto tex = tryCopyFirstDependencyTexture(pipeline)) {
|
||||||
|
return *tex;
|
||||||
|
}
|
||||||
|
return makeSolidColorTexture(0xFFFF00FFu);
|
||||||
|
}
|
||||||
|
|
||||||
|
TexturePipelineProgram program;
|
||||||
|
program.fromWords(std::move(words));
|
||||||
|
|
||||||
|
TexturePipelineProgram::OwnedTexture baked;
|
||||||
|
auto provider = [this](uint32_t texId) -> std::optional<Texture> {
|
||||||
|
auto iter = _ResToTexture.find(texId);
|
||||||
|
if (iter == _ResToTexture.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
const StoredTexture& stored = iter->second;
|
||||||
|
if (stored._Pixels.empty() || stored._Widht == 0 || stored._Height == 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
Texture tex{};
|
||||||
|
tex.Width = stored._Widht;
|
||||||
|
tex.Height = stored._Height;
|
||||||
|
tex.Pixels = stored._Pixels.data();
|
||||||
|
return tex;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!program.bake(provider, baked, nullptr)) {
|
||||||
|
if (auto tex = tryCopyFirstDependencyTexture(pipeline)) {
|
||||||
|
return *tex;
|
||||||
|
}
|
||||||
|
return makeSolidColorTexture(0xFFFF00FFu);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t width = baked.Width;
|
||||||
|
const uint32_t height = baked.Height;
|
||||||
|
if (width == 0 || height == 0 ||
|
||||||
|
width > std::numeric_limits<uint16_t>::max() ||
|
||||||
|
height > std::numeric_limits<uint16_t>::max() ||
|
||||||
|
baked.Pixels.size() != static_cast<size_t>(width) * static_cast<size_t>(height)) {
|
||||||
|
if (auto tex = tryCopyFirstDependencyTexture(pipeline)) {
|
||||||
|
return *tex;
|
||||||
|
}
|
||||||
|
return makeSolidColorTexture(0xFFFF00FFu);
|
||||||
|
}
|
||||||
|
|
||||||
|
return StoredTexture(static_cast<uint16_t>(width),
|
||||||
|
static_cast<uint16_t>(height),
|
||||||
|
std::move(baked.Pixels));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::flushNewPipelines() {
|
||||||
|
std::vector<uint32_t> changedTextures = std::move(_ChangedTextures);
|
||||||
|
|
||||||
|
std::sort(changedTextures.begin(), changedTextures.end());
|
||||||
|
changedTextures.erase(std::unique(changedTextures.begin(), changedTextures.end()), changedTextures.end());
|
||||||
|
|
||||||
|
std::vector<HashedPipeline> changedPipelineTextures;
|
||||||
|
for (uint32_t texId : changedTextures) {
|
||||||
|
auto iter = _AddictedTextures.find(texId);
|
||||||
|
if (iter == _AddictedTextures.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
changedPipelineTextures.append_range(iter->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
changedPipelineTextures.append_range(std::move(_ChangedPipelines));
|
||||||
|
changedTextures.clear();
|
||||||
|
|
||||||
|
std::sort(changedPipelineTextures.begin(), changedPipelineTextures.end());
|
||||||
|
changedPipelineTextures.erase(std::unique(changedPipelineTextures.begin(), changedPipelineTextures.end()),
|
||||||
|
changedPipelineTextures.end());
|
||||||
|
|
||||||
|
for (const HashedPipeline& pipeline : changedPipelineTextures) {
|
||||||
|
auto iterPTTI = _PipeToTexId.find(pipeline);
|
||||||
|
assert(iterPTTI != _PipeToTexId.end());
|
||||||
|
|
||||||
|
StoredTexture texture = _generatePipelineTexture(pipeline);
|
||||||
|
AtlasTextureId atlasTexId = iterPTTI->second;
|
||||||
|
auto& stored = _AtlasCpuTextures[atlasTexId];
|
||||||
|
stored = std::move(texture);
|
||||||
|
if (!stored._Pixels.empty()) {
|
||||||
|
Super.setTextureData(atlasTexId,
|
||||||
|
stored._Widht,
|
||||||
|
stored._Height,
|
||||||
|
stored._Pixels.data(),
|
||||||
|
stored._Widht * 4u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas::DescriptorOut PipelinedTextureAtlas::flushUploadsAndBarriers(VkCommandBuffer cmdBuffer) {
|
||||||
|
return Super.flushUploadsAndBarriers(cmdBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PipelinedTextureAtlas::notifyGpuFinished() {
|
||||||
|
Super.notifyGpuFinished();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<StoredTexture> PipelinedTextureAtlas::tryCopyFirstDependencyTexture(const HashedPipeline& pipeline) const {
|
||||||
|
auto deps = pipeline.getDependencedTextures();
|
||||||
|
if (!deps.empty()) {
|
||||||
|
auto iter = _ResToTexture.find(deps.front());
|
||||||
|
if (iter != _ResToTexture.end()) {
|
||||||
|
return iter->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
StoredTexture PipelinedTextureAtlas::makeSolidColorTexture(uint32_t rgba) {
|
||||||
|
return StoredTexture(1, 1, std::vector<uint32_t>{rgba});
|
||||||
|
}
|
||||||
380
Src/Client/Vulkan/AtlasPipeline/PipelinedTextureAtlas.hpp
Normal file
@@ -0,0 +1,380 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "TextureAtlas.hpp"
|
||||||
|
#include "TexturePipelineProgram.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#include "boost/container/small_vector.hpp"
|
||||||
|
|
||||||
|
using TextureId = uint32_t;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
using Word = TexturePipelineProgram::Word;
|
||||||
|
|
||||||
|
enum class Op16 : Word {
|
||||||
|
End = 0,
|
||||||
|
Base_Tex = 1,
|
||||||
|
Base_Fill = 2,
|
||||||
|
Resize = 10,
|
||||||
|
Transform = 11,
|
||||||
|
Opacity = 12,
|
||||||
|
NoAlpha = 13,
|
||||||
|
MakeAlpha = 14,
|
||||||
|
Invert = 15,
|
||||||
|
Brighten = 16,
|
||||||
|
Contrast = 17,
|
||||||
|
Multiply = 18,
|
||||||
|
Screen = 19,
|
||||||
|
Colorize = 20,
|
||||||
|
Overlay = 30,
|
||||||
|
Mask = 31,
|
||||||
|
LowPart = 32,
|
||||||
|
Combine = 40
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SrcKind16 : Word { TexId = 0, Sub = 1 };
|
||||||
|
|
||||||
|
struct SrcRef16 {
|
||||||
|
SrcKind16 kind{SrcKind16::TexId};
|
||||||
|
Word a = 0;
|
||||||
|
Word b = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline uint32_t makeU32(Word lo, Word hi) {
|
||||||
|
return uint32_t(lo) | (uint32_t(hi) << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void addUniqueDep(boost::container::small_vector<uint32_t, 8>& deps, uint32_t id) {
|
||||||
|
if (id == TextureAtlas::kOverflowId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (std::find(deps.begin(), deps.end(), id) == deps.end()) {
|
||||||
|
deps.push_back(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool readSrc(const std::vector<Word>& words, size_t end, size_t& ip, SrcRef16& out) {
|
||||||
|
if (ip + 2 >= end) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.kind = static_cast<SrcKind16>(words[ip++]);
|
||||||
|
out.a = words[ip++];
|
||||||
|
out.b = words[ip++];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void extractPipelineDependencies(const std::vector<Word>& words,
|
||||||
|
size_t start,
|
||||||
|
size_t end,
|
||||||
|
boost::container::small_vector<uint32_t, 8>& deps,
|
||||||
|
std::vector<std::pair<size_t, size_t>>& visited) {
|
||||||
|
if (start >= end || end > words.size()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const std::pair<size_t, size_t> key{start, end};
|
||||||
|
if (std::find(visited.begin(), visited.end(), key) != visited.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
visited.push_back(key);
|
||||||
|
|
||||||
|
size_t ip = start;
|
||||||
|
auto need = [&](size_t n) { return ip + n <= end; };
|
||||||
|
auto handleSrc = [&](const SrcRef16& src) {
|
||||||
|
if (src.kind == SrcKind16::TexId) {
|
||||||
|
addUniqueDep(deps, makeU32(src.a, src.b));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (src.kind == SrcKind16::Sub) {
|
||||||
|
size_t subStart = static_cast<size_t>(src.a);
|
||||||
|
size_t subEnd = subStart + static_cast<size_t>(src.b);
|
||||||
|
if (subStart < subEnd && subEnd <= words.size()) {
|
||||||
|
extractPipelineDependencies(words, subStart, subEnd, deps, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while (ip < end) {
|
||||||
|
if (!need(1)) break;
|
||||||
|
Op16 op = static_cast<Op16>(words[ip++]);
|
||||||
|
switch (op) {
|
||||||
|
case Op16::End:
|
||||||
|
return;
|
||||||
|
|
||||||
|
case Op16::Base_Tex: {
|
||||||
|
if (!need(3)) return;
|
||||||
|
SrcRef16 src{};
|
||||||
|
if (!readSrc(words, end, ip, src)) return;
|
||||||
|
handleSrc(src);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case Op16::Base_Fill:
|
||||||
|
if (!need(4)) return;
|
||||||
|
ip += 4;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Overlay:
|
||||||
|
case Op16::Mask: {
|
||||||
|
if (!need(3)) return;
|
||||||
|
SrcRef16 src{};
|
||||||
|
if (!readSrc(words, end, ip, src)) return;
|
||||||
|
handleSrc(src);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case Op16::LowPart: {
|
||||||
|
if (!need(1 + 3)) return;
|
||||||
|
ip += 1; // percent
|
||||||
|
SrcRef16 src{};
|
||||||
|
if (!readSrc(words, end, ip, src)) return;
|
||||||
|
handleSrc(src);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case Op16::Resize:
|
||||||
|
if (!need(2)) return;
|
||||||
|
ip += 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Transform:
|
||||||
|
case Op16::Opacity:
|
||||||
|
if (!need(1)) return;
|
||||||
|
ip += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::NoAlpha:
|
||||||
|
case Op16::Brighten:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::MakeAlpha:
|
||||||
|
if (!need(2)) return;
|
||||||
|
ip += 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Invert:
|
||||||
|
if (!need(1)) return;
|
||||||
|
ip += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Contrast:
|
||||||
|
if (!need(2)) return;
|
||||||
|
ip += 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Multiply:
|
||||||
|
case Op16::Screen:
|
||||||
|
if (!need(2)) return;
|
||||||
|
ip += 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Colorize:
|
||||||
|
if (!need(3)) return;
|
||||||
|
ip += 3;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Op16::Combine: {
|
||||||
|
if (!need(3)) return;
|
||||||
|
ip += 2; // skip w,h
|
||||||
|
uint32_t n = words[ip++];
|
||||||
|
for (uint32_t i = 0; i < n; ++i) {
|
||||||
|
if (!need(2 + 3)) return;
|
||||||
|
ip += 2; // x, y
|
||||||
|
SrcRef16 src{};
|
||||||
|
if (!readSrc(words, end, ip, src)) return;
|
||||||
|
handleSrc(src);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline boost::container::small_vector<uint32_t, 8> extractPipelineDependencies(const std::vector<Word>& words) {
|
||||||
|
boost::container::small_vector<uint32_t, 8> deps;
|
||||||
|
std::vector<std::pair<size_t, size_t>> visited;
|
||||||
|
extractPipelineDependencies(words, 0, words.size(), deps, visited);
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline boost::container::small_vector<uint32_t, 8> extractPipelineDependencies(const boost::container::small_vector<Word, 32>& words) {
|
||||||
|
boost::container::small_vector<uint32_t, 8> deps;
|
||||||
|
std::vector<std::pair<size_t, size_t>> visited;
|
||||||
|
std::vector<Word> copy(words.begin(), words.end());
|
||||||
|
extractPipelineDependencies(copy, 0, copy.size(), deps, visited);
|
||||||
|
return deps;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// Структура нехешированного пайплайна
|
||||||
|
struct Pipeline {
|
||||||
|
std::vector<detail::Word> _Pipeline;
|
||||||
|
|
||||||
|
Pipeline() = default;
|
||||||
|
|
||||||
|
explicit Pipeline(const TexturePipelineProgram& program)
|
||||||
|
: _Pipeline(program.words().begin(), program.words().end())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Pipeline(TextureId texId) {
|
||||||
|
_Pipeline = {
|
||||||
|
static_cast<detail::Word>(detail::Op16::Base_Tex),
|
||||||
|
static_cast<detail::Word>(detail::SrcKind16::TexId),
|
||||||
|
static_cast<detail::Word>(texId & 0xFFFFu),
|
||||||
|
static_cast<detail::Word>((texId >> 16) & 0xFFFFu),
|
||||||
|
static_cast<detail::Word>(detail::Op16::End)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Структура хешированного текстурного пайплайна
|
||||||
|
struct HashedPipeline {
|
||||||
|
// Предвычисленный хеш
|
||||||
|
std::size_t _Hash;
|
||||||
|
boost::container::small_vector<detail::Word, 32> _Pipeline;
|
||||||
|
|
||||||
|
HashedPipeline() = default;
|
||||||
|
HashedPipeline(const Pipeline& pipeline) noexcept
|
||||||
|
: _Pipeline(pipeline._Pipeline.begin(), pipeline._Pipeline.end())
|
||||||
|
{
|
||||||
|
reComputeHash();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Перевычисляет хеш
|
||||||
|
void reComputeHash() noexcept {
|
||||||
|
std::size_t hash = 14695981039346656037ull;
|
||||||
|
constexpr std::size_t prime = 1099511628211ull;
|
||||||
|
|
||||||
|
for(detail::Word w : _Pipeline) {
|
||||||
|
hash ^= static_cast<uint8_t>(w & 0xFF);
|
||||||
|
hash *= prime;
|
||||||
|
hash ^= static_cast<uint8_t>((w >> 8) & 0xFF);
|
||||||
|
hash *= prime;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Hash = hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Выдаёт список зависимых текстур, на основе которых строится эта
|
||||||
|
boost::container::small_vector<uint32_t, 8> getDependencedTextures() const {
|
||||||
|
return detail::extractPipelineDependencies(_Pipeline);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const HashedPipeline& obj) const noexcept {
|
||||||
|
return _Hash == obj._Hash && _Pipeline == obj._Pipeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const HashedPipeline& obj) const noexcept {
|
||||||
|
return _Hash < obj._Hash || (_Hash == obj._Hash && _Pipeline < obj._Pipeline);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct StoredTexture {
|
||||||
|
uint16_t _Widht = 0;
|
||||||
|
uint16_t _Height = 0;
|
||||||
|
std::vector<uint32_t> _Pixels;
|
||||||
|
|
||||||
|
StoredTexture() = default;
|
||||||
|
StoredTexture(uint16_t w, uint16_t h, std::vector<uint32_t> pixels)
|
||||||
|
: _Widht(w), _Height(h), _Pixels(std::move(pixels))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Пайплайновый текстурный атлас
|
||||||
|
class PipelinedTextureAtlas {
|
||||||
|
public:
|
||||||
|
using AtlasTextureId = uint32_t;
|
||||||
|
struct HostTextureView {
|
||||||
|
uint32_t width = 0;
|
||||||
|
uint32_t height = 0;
|
||||||
|
uint32_t rowPitchBytes = 0;
|
||||||
|
const uint8_t* pixelsRGBA8 = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Функтор хеша
|
||||||
|
struct HashedPipelineKeyHash {
|
||||||
|
std::size_t operator()(const HashedPipeline& k) const noexcept {
|
||||||
|
return k._Hash;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Функтор равенства
|
||||||
|
struct HashedPipelineKeyEqual {
|
||||||
|
bool operator()(const HashedPipeline& a, const HashedPipeline& b) const noexcept {
|
||||||
|
return a._Pipeline == b._Pipeline;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Текстурный атлас
|
||||||
|
TextureAtlas Super;
|
||||||
|
// Пустой пайплайн (указывающий на одну текстуру) ссылается на простой идентификатор (ResToAtlas)
|
||||||
|
std::unordered_map<HashedPipeline, AtlasTextureId, HashedPipelineKeyHash, HashedPipelineKeyEqual> _PipeToTexId;
|
||||||
|
// Загруженные текстуры
|
||||||
|
std::unordered_map<TextureId, StoredTexture> _ResToTexture;
|
||||||
|
std::unordered_map<AtlasTextureId, StoredTexture> _AtlasCpuTextures;
|
||||||
|
// Список зависимых пайплайнов от текстур (при изменении текстуры, нужно перерисовать пайплайны)
|
||||||
|
std::unordered_map<TextureId, boost::container::small_vector<HashedPipeline, 8>> _AddictedTextures;
|
||||||
|
// Изменённые простые текстуры (для последующего массового обновление пайплайнов)
|
||||||
|
std::vector<uint32_t> _ChangedTextures;
|
||||||
|
// Необходимые к созданию/обновлению пайплайны
|
||||||
|
std::vector<HashedPipeline> _ChangedPipelines;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PipelinedTextureAtlas(TextureAtlas&& tk);
|
||||||
|
|
||||||
|
uint32_t atlasSide() const {
|
||||||
|
return Super.atlasSide();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t atlasLayers() const {
|
||||||
|
return Super.atlasLayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t AtlasSide() const {
|
||||||
|
return atlasSide();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t AtlasLayers() const {
|
||||||
|
return atlasLayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Должны всегда бронировать идентификатор, либо отдавать kOverflowId. При этом запись tex+pipeline остаётся
|
||||||
|
// Выдаёт стабильный идентификатор, привязанный к пайплайну
|
||||||
|
AtlasTextureId getByPipeline(const HashedPipeline& pipeline);
|
||||||
|
|
||||||
|
// Уведомить что текстура+pipeline более не используются (идентификатор будет освобождён)
|
||||||
|
// Освобождать можно при потере ресурсов
|
||||||
|
void freeByPipeline(const HashedPipeline& pipeline);
|
||||||
|
|
||||||
|
void updateTexture(uint32_t texId, const StoredTexture& texture);
|
||||||
|
void updateTexture(uint32_t texId, StoredTexture&& texture);
|
||||||
|
|
||||||
|
void freeTexture(uint32_t texId);
|
||||||
|
|
||||||
|
bool getHostTexture(TextureId texId, HostTextureView& out) const;
|
||||||
|
|
||||||
|
// Генерация текстуры пайплайна
|
||||||
|
StoredTexture _generatePipelineTexture(const HashedPipeline& pipeline);
|
||||||
|
|
||||||
|
// Обновляет пайплайны по необходимости
|
||||||
|
void flushNewPipelines();
|
||||||
|
|
||||||
|
TextureAtlas::DescriptorOut flushUploadsAndBarriers(VkCommandBuffer cmdBuffer);
|
||||||
|
|
||||||
|
void notifyGpuFinished();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::optional<StoredTexture> tryCopyFirstDependencyTexture(const HashedPipeline& pipeline) const;
|
||||||
|
|
||||||
|
static StoredTexture makeSolidColorTexture(uint32_t rgba);
|
||||||
|
};
|
||||||
169
Src/Client/Vulkan/AtlasPipeline/SharedStagingBuffer.hpp
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <optional>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
/*
|
||||||
|
Межкадровый промежуточный буфер.
|
||||||
|
Для модели рендера Один за одним.
|
||||||
|
После окончания рендера кадра считается синхронизированным
|
||||||
|
и может заполняться по новой.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class SharedStagingBuffer {
|
||||||
|
public:
|
||||||
|
static constexpr VkDeviceSize kDefaultSize = 64ull * 1024ull * 1024ull;
|
||||||
|
|
||||||
|
SharedStagingBuffer(VkDevice device,
|
||||||
|
VkPhysicalDevice physicalDevice,
|
||||||
|
VkDeviceSize sizeBytes = kDefaultSize)
|
||||||
|
: device_(device),
|
||||||
|
physicalDevice_(physicalDevice),
|
||||||
|
size_(sizeBytes) {
|
||||||
|
if (!device_ || !physicalDevice_) {
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: null device/physicalDevice");
|
||||||
|
}
|
||||||
|
if (size_ == 0) {
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: size must be > 0");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkBufferCreateInfo bi{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||||
|
.pNext = nullptr,
|
||||||
|
.flags = 0,
|
||||||
|
.size = size_,
|
||||||
|
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||||
|
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||||
|
.queueFamilyIndexCount = 0,
|
||||||
|
.pQueueFamilyIndices = nullptr
|
||||||
|
};
|
||||||
|
|
||||||
|
if (vkCreateBuffer(device_, &bi, nullptr, &buffer_) != VK_SUCCESS) {
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: vkCreateBuffer failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryRequirements mr{};
|
||||||
|
vkGetBufferMemoryRequirements(device_, buffer_, &mr);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo ai{};
|
||||||
|
ai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
ai.allocationSize = mr.size;
|
||||||
|
ai.memoryTypeIndex = FindMemoryType_(mr.memoryTypeBits,
|
||||||
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
||||||
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||||
|
|
||||||
|
if (vkAllocateMemory(device_, &ai, nullptr, &memory_) != VK_SUCCESS) {
|
||||||
|
vkDestroyBuffer(device_, buffer_, nullptr);
|
||||||
|
buffer_ = VK_NULL_HANDLE;
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: vkAllocateMemory failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
vkBindBufferMemory(device_, buffer_, memory_, 0);
|
||||||
|
|
||||||
|
if (vkMapMemory(device_, memory_, 0, VK_WHOLE_SIZE, 0, &mapped_) != VK_SUCCESS) {
|
||||||
|
vkFreeMemory(device_, memory_, nullptr);
|
||||||
|
vkDestroyBuffer(device_, buffer_, nullptr);
|
||||||
|
buffer_ = VK_NULL_HANDLE;
|
||||||
|
memory_ = VK_NULL_HANDLE;
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: vkMapMemory failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~SharedStagingBuffer() { Destroy_(); }
|
||||||
|
|
||||||
|
SharedStagingBuffer(const SharedStagingBuffer&) = delete;
|
||||||
|
SharedStagingBuffer& operator=(const SharedStagingBuffer&) = delete;
|
||||||
|
|
||||||
|
SharedStagingBuffer(SharedStagingBuffer&& other) noexcept {
|
||||||
|
*this = std::move(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
SharedStagingBuffer& operator=(SharedStagingBuffer&& other) noexcept {
|
||||||
|
if (this != &other) {
|
||||||
|
Destroy_();
|
||||||
|
device_ = other.device_;
|
||||||
|
physicalDevice_ = other.physicalDevice_;
|
||||||
|
buffer_ = other.buffer_;
|
||||||
|
memory_ = other.memory_;
|
||||||
|
mapped_ = other.mapped_;
|
||||||
|
size_ = other.size_;
|
||||||
|
offset_ = other.offset_;
|
||||||
|
|
||||||
|
other.device_ = VK_NULL_HANDLE;
|
||||||
|
other.physicalDevice_ = VK_NULL_HANDLE;
|
||||||
|
other.buffer_ = VK_NULL_HANDLE;
|
||||||
|
other.memory_ = VK_NULL_HANDLE;
|
||||||
|
other.mapped_ = nullptr;
|
||||||
|
other.size_ = 0;
|
||||||
|
other.offset_ = 0;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkBuffer Buffer() const { return buffer_; }
|
||||||
|
void* Mapped() const { return mapped_; }
|
||||||
|
VkDeviceSize Size() const { return size_; }
|
||||||
|
|
||||||
|
std::optional<VkDeviceSize> Allocate(VkDeviceSize bytes, VkDeviceSize alignment) {
|
||||||
|
VkDeviceSize off = Align_(offset_, alignment);
|
||||||
|
if (off + bytes > size_) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
offset_ = off + bytes;
|
||||||
|
return off;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reset() { offset_ = 0; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint32_t FindMemoryType_(uint32_t typeBits, VkMemoryPropertyFlags properties) const {
|
||||||
|
VkPhysicalDeviceMemoryProperties mp{};
|
||||||
|
vkGetPhysicalDeviceMemoryProperties(physicalDevice_, &mp);
|
||||||
|
for (uint32_t i = 0; i < mp.memoryTypeCount; ++i) {
|
||||||
|
if ((typeBits & (1u << i)) &&
|
||||||
|
(mp.memoryTypes[i].propertyFlags & properties) == properties) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw std::runtime_error("SharedStagingBuffer: no suitable memory type");
|
||||||
|
}
|
||||||
|
|
||||||
|
static VkDeviceSize Align_(VkDeviceSize value, VkDeviceSize alignment) {
|
||||||
|
if (alignment == 0) return value;
|
||||||
|
return (value + alignment - 1) & ~(alignment - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Destroy_() {
|
||||||
|
if (device_ == VK_NULL_HANDLE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mapped_) {
|
||||||
|
vkUnmapMemory(device_, memory_);
|
||||||
|
mapped_ = nullptr;
|
||||||
|
}
|
||||||
|
if (buffer_) {
|
||||||
|
vkDestroyBuffer(device_, buffer_, nullptr);
|
||||||
|
buffer_ = VK_NULL_HANDLE;
|
||||||
|
}
|
||||||
|
if (memory_) {
|
||||||
|
vkFreeMemory(device_, memory_, nullptr);
|
||||||
|
memory_ = VK_NULL_HANDLE;
|
||||||
|
}
|
||||||
|
size_ = 0;
|
||||||
|
offset_ = 0;
|
||||||
|
device_ = VK_NULL_HANDLE;
|
||||||
|
physicalDevice_ = VK_NULL_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
VkDevice device_ = VK_NULL_HANDLE;
|
||||||
|
VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE;
|
||||||
|
VkBuffer buffer_ = VK_NULL_HANDLE;
|
||||||
|
VkDeviceMemory memory_ = VK_NULL_HANDLE;
|
||||||
|
void* mapped_ = nullptr;
|
||||||
|
VkDeviceSize size_ = 0;
|
||||||
|
VkDeviceSize offset_ = 0;
|
||||||
|
};
|
||||||
477
Src/Client/Vulkan/AtlasPipeline/TextureAtlas.cpp
Normal file
@@ -0,0 +1,477 @@
|
|||||||
|
#include "TextureAtlas.hpp"
|
||||||
|
|
||||||
|
TextureAtlas::TextureAtlas(VkDevice device,
|
||||||
|
VkPhysicalDevice physicalDevice,
|
||||||
|
const Config& cfg,
|
||||||
|
EventCallback cb,
|
||||||
|
std::shared_ptr<SharedStagingBuffer> staging)
|
||||||
|
: Device_(device),
|
||||||
|
Phys_(physicalDevice),
|
||||||
|
Cfg_(cfg),
|
||||||
|
OnEvent_(std::move(cb)),
|
||||||
|
Staging_(std::move(staging)) {
|
||||||
|
if(!Device_ || !Phys_) {
|
||||||
|
throw std::runtime_error("TextureAtlas: device/physicalDevice == null");
|
||||||
|
}
|
||||||
|
_validateConfigOrThrow();
|
||||||
|
|
||||||
|
VkPhysicalDeviceProperties props{};
|
||||||
|
vkGetPhysicalDeviceProperties(Phys_, &props);
|
||||||
|
CopyOffsetAlignment_ = std::max<VkDeviceSize>(4, props.limits.optimalBufferCopyOffsetAlignment);
|
||||||
|
|
||||||
|
if(!Staging_) {
|
||||||
|
Staging_ = std::make_shared<SharedStagingBuffer>(Device_, Phys_, kStagingSizeBytes);
|
||||||
|
}
|
||||||
|
_validateStagingCapacityOrThrow();
|
||||||
|
|
||||||
|
_createEntriesBufferOrThrow();
|
||||||
|
_createAtlasOrThrow(Cfg_.InitialSide, 1);
|
||||||
|
|
||||||
|
EntriesCpu_.resize(Cfg_.MaxTextureId);
|
||||||
|
std::memset(EntriesCpu_.data(), 0, EntriesCpu_.size() * sizeof(Entry));
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
|
||||||
|
Slots_.resize(Cfg_.MaxTextureId);
|
||||||
|
FreeIds_.reserve(Cfg_.MaxTextureId);
|
||||||
|
PendingInQueue_.assign(Cfg_.MaxTextureId, false);
|
||||||
|
|
||||||
|
if(Cfg_.ExternalSampler != VK_NULL_HANDLE) {
|
||||||
|
Sampler_ = Cfg_.ExternalSampler;
|
||||||
|
OwnsSampler_ = false;
|
||||||
|
} else {
|
||||||
|
_createSamplerOrThrow();
|
||||||
|
OwnsSampler_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
_rebuildPackersFromPlacements();
|
||||||
|
Alive_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas::~TextureAtlas() { _shutdownNoThrow(); }
|
||||||
|
|
||||||
|
TextureAtlas::TextureAtlas(TextureAtlas&& other) noexcept {
|
||||||
|
_moveFrom(std::move(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas& TextureAtlas::operator=(TextureAtlas&& other) noexcept {
|
||||||
|
if(this != &other) {
|
||||||
|
_shutdownNoThrow();
|
||||||
|
_moveFrom(std::move(other));
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::shutdown() {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
_shutdownNoThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas::TextureId TextureAtlas::registerTexture() {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
|
||||||
|
TextureId id = kOverflowId;
|
||||||
|
if(!FreeIds_.empty()) {
|
||||||
|
id = FreeIds_.back();
|
||||||
|
FreeIds_.pop_back();
|
||||||
|
} else if(NextId_ < Cfg_.MaxTextureId) {
|
||||||
|
id = NextId_++;
|
||||||
|
} else {
|
||||||
|
return kOverflowId;
|
||||||
|
}
|
||||||
|
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
s = Slot{};
|
||||||
|
s.InUse = true;
|
||||||
|
s.StateValue = State::REGISTERED;
|
||||||
|
s.Generation = 1;
|
||||||
|
|
||||||
|
_setEntryInvalid(id, /*diagPending*/false, /*diagTooLarge*/false);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::setTextureData(TextureId id,
|
||||||
|
uint32_t w,
|
||||||
|
uint32_t h,
|
||||||
|
const void* pixelsRGBA8,
|
||||||
|
uint32_t rowPitchBytes) {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
if(id == kOverflowId) return;
|
||||||
|
_ensureRegisteredIdOrThrow(id);
|
||||||
|
|
||||||
|
if(w == 0 || h == 0) {
|
||||||
|
throw _inputError("setTextureData: w/h must be > 0");
|
||||||
|
}
|
||||||
|
if(w > Cfg_.MaxTextureSize || h > Cfg_.MaxTextureSize) {
|
||||||
|
_handleTooLarge(id);
|
||||||
|
throw _inputError("setTextureData: texture is TOO_LARGE (>2048)");
|
||||||
|
}
|
||||||
|
if(!pixelsRGBA8) {
|
||||||
|
throw _inputError("setTextureData: pixelsRGBA8 == null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rowPitchBytes == 0) {
|
||||||
|
rowPitchBytes = w * 4;
|
||||||
|
}
|
||||||
|
if(rowPitchBytes < w * 4) {
|
||||||
|
throw _inputError("setTextureData: rowPitchBytes < w*4");
|
||||||
|
}
|
||||||
|
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
|
||||||
|
const bool sizeChanged = (s.HasCpuData && (s.W != w || s.H != h));
|
||||||
|
if(sizeChanged) {
|
||||||
|
_freePlacement(id);
|
||||||
|
_setEntryInvalid(id, /*diagPending*/true, /*diagTooLarge*/false);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
s.W = w;
|
||||||
|
s.H = h;
|
||||||
|
|
||||||
|
s.CpuPixels = static_cast<const uint8_t*>(pixelsRGBA8);
|
||||||
|
s.CpuRowPitchBytes = rowPitchBytes;
|
||||||
|
s.HasCpuData = true;
|
||||||
|
s.StateValue = State::PENDING_UPLOAD;
|
||||||
|
s.Generation++;
|
||||||
|
|
||||||
|
if(!sizeChanged && s.HasPlacement && s.StateWasValid) {
|
||||||
|
// keep entry valid
|
||||||
|
} else if(!s.HasPlacement) {
|
||||||
|
_setEntryInvalid(id, /*diagPending*/true, /*diagTooLarge*/false);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
_enqueuePending(id);
|
||||||
|
|
||||||
|
if(Repack_.Active && Repack_.Plan.count(id) != 0) {
|
||||||
|
_enqueueRepackPending(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::clearTextureData(TextureId id) {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
if(id == kOverflowId) return;
|
||||||
|
_ensureRegisteredIdOrThrow(id);
|
||||||
|
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
s.CpuPixels = nullptr;
|
||||||
|
s.CpuRowPitchBytes = 0;
|
||||||
|
s.HasCpuData = false;
|
||||||
|
|
||||||
|
_freePlacement(id);
|
||||||
|
s.StateValue = State::REGISTERED;
|
||||||
|
s.StateWasValid = false;
|
||||||
|
|
||||||
|
_removeFromPending(id);
|
||||||
|
_removeFromRepackPending(id);
|
||||||
|
|
||||||
|
_setEntryInvalid(id, /*diagPending*/false, /*diagTooLarge*/false);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::removeTexture(TextureId id) {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
if(id == kOverflowId) return;
|
||||||
|
_ensureRegisteredIdOrThrow(id);
|
||||||
|
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
|
||||||
|
clearTextureData(id);
|
||||||
|
|
||||||
|
s.InUse = false;
|
||||||
|
s.StateValue = State::REMOVED;
|
||||||
|
|
||||||
|
FreeIds_.push_back(id);
|
||||||
|
|
||||||
|
_setEntryInvalid(id, /*diagPending*/false, /*diagTooLarge*/false);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::requestFullRepack(RepackMode mode) {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
Repack_.Requested = true;
|
||||||
|
Repack_.Mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureAtlas::DescriptorOut TextureAtlas::flushUploadsAndBarriers(VkCommandBuffer cmdBuffer) {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
if(cmdBuffer == VK_NULL_HANDLE) {
|
||||||
|
throw _inputError("flushUploadsAndBarriers: cmdBuffer == null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Repack_.SwapReady) {
|
||||||
|
_swapToRepackedAtlas();
|
||||||
|
}
|
||||||
|
if(Repack_.Requested && !Repack_.Active) {
|
||||||
|
_startRepackIfPossible();
|
||||||
|
}
|
||||||
|
|
||||||
|
_processPendingLayerGrow(cmdBuffer);
|
||||||
|
|
||||||
|
bool willTouchEntries = EntriesDirty_;
|
||||||
|
|
||||||
|
auto collectQueue = [this](std::deque<TextureId>& queue,
|
||||||
|
std::vector<bool>& inQueue,
|
||||||
|
std::vector<TextureId>& out) {
|
||||||
|
while (!queue.empty()) {
|
||||||
|
TextureId id = queue.front();
|
||||||
|
queue.pop_front();
|
||||||
|
if(id == kOverflowId || id >= inQueue.size()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(!inQueue[id]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
inQueue[id] = false;
|
||||||
|
out.push_back(id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<TextureId> pendingNow;
|
||||||
|
pendingNow.reserve(Pending_.size());
|
||||||
|
collectQueue(Pending_, PendingInQueue_, pendingNow);
|
||||||
|
|
||||||
|
std::vector<TextureId> repackPending;
|
||||||
|
if(Repack_.Active) {
|
||||||
|
if(Repack_.InPending.empty()) {
|
||||||
|
Repack_.InPending.assign(Cfg_.MaxTextureId, false);
|
||||||
|
}
|
||||||
|
collectQueue(Repack_.Pending, Repack_.InPending, repackPending);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto processPlacement = [&](TextureId id, Slot& s) -> bool {
|
||||||
|
if(s.HasPlacement) return true;
|
||||||
|
const uint32_t wP = s.W + 2u * Cfg_.PaddingPx;
|
||||||
|
const uint32_t hP = s.H + 2u * Cfg_.PaddingPx;
|
||||||
|
if(!_tryPlaceWithGrow(id, wP, hP, cmdBuffer)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
willTouchEntries = true;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool outOfSpace = false;
|
||||||
|
for(TextureId id : pendingNow) {
|
||||||
|
if(id == kOverflowId) continue;
|
||||||
|
if(id >= Slots_.size()) continue;
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
if(!s.InUse || !s.HasCpuData) continue;
|
||||||
|
if(!processPlacement(id, s)) {
|
||||||
|
outOfSpace = true;
|
||||||
|
_enqueuePending(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(outOfSpace) {
|
||||||
|
_emitEventOncePerFlush(AtlasEvent::AtlasOutOfSpace);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool anyAtlasWrites = false;
|
||||||
|
bool anyRepackWrites = false;
|
||||||
|
|
||||||
|
auto uploadTextureIntoAtlas = [&](Slot& s,
|
||||||
|
const Placement& pp,
|
||||||
|
ImageRes& targetAtlas,
|
||||||
|
bool isRepackTarget) {
|
||||||
|
const uint32_t wP = pp.WP;
|
||||||
|
const uint32_t hP = pp.HP;
|
||||||
|
const VkDeviceSize bytes = static_cast<VkDeviceSize>(wP) * hP * 4u;
|
||||||
|
auto stagingOff = Staging_->Allocate(bytes, CopyOffsetAlignment_);
|
||||||
|
if(!stagingOff) {
|
||||||
|
_emitEventOncePerFlush(AtlasEvent::StagingOverflow);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* dst = static_cast<uint8_t*>(Staging_->Mapped()) + *stagingOff;
|
||||||
|
if(!s.CpuPixels) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_writePaddedRGBA8(dst, wP * 4u, s.W, s.H, Cfg_.PaddingPx,
|
||||||
|
s.CpuPixels, s.CpuRowPitchBytes);
|
||||||
|
|
||||||
|
_ensureImageLayoutForTransferDst(cmdBuffer, targetAtlas,
|
||||||
|
isRepackTarget ? anyRepackWrites : anyAtlasWrites);
|
||||||
|
|
||||||
|
VkBufferImageCopy region{};
|
||||||
|
region.bufferOffset = *stagingOff;
|
||||||
|
region.bufferRowLength = wP;
|
||||||
|
region.bufferImageHeight = hP;
|
||||||
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
region.imageSubresource.mipLevel = 0;
|
||||||
|
region.imageSubresource.baseArrayLayer = pp.Layer;
|
||||||
|
region.imageSubresource.layerCount = 1;
|
||||||
|
region.imageOffset = { static_cast<int32_t>(pp.X),
|
||||||
|
static_cast<int32_t>(pp.Y), 0 };
|
||||||
|
region.imageExtent = { wP, hP, 1 };
|
||||||
|
|
||||||
|
vkCmdCopyBufferToImage(cmdBuffer, Staging_->Buffer(), targetAtlas.Image,
|
||||||
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
for(TextureId id : pendingNow) {
|
||||||
|
if(id == kOverflowId) continue;
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
if(!s.InUse || !s.HasCpuData || !s.HasPlacement) continue;
|
||||||
|
if(!uploadTextureIntoAtlas(s, s.Place, Atlas_, false)) {
|
||||||
|
_enqueuePending(id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
s.StateValue = State::VALID;
|
||||||
|
s.StateWasValid = true;
|
||||||
|
_setEntryValid(id);
|
||||||
|
EntriesDirty_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Repack_.Active) {
|
||||||
|
for(TextureId id : repackPending) {
|
||||||
|
if(Repack_.Plan.count(id) == 0) continue;
|
||||||
|
Slot& s = Slots_[id];
|
||||||
|
if(!s.InUse || !s.HasCpuData) continue;
|
||||||
|
const PlannedPlacement& pp = Repack_.Plan[id];
|
||||||
|
Placement place{pp.X, pp.Y, pp.WP, pp.HP, pp.Layer};
|
||||||
|
if(!uploadTextureIntoAtlas(s, place, Repack_.Atlas, true)) {
|
||||||
|
_enqueueRepackPending(id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Repack_.WroteSomethingThisFlush = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(willTouchEntries || EntriesDirty_) {
|
||||||
|
const VkDeviceSize entriesBytes = static_cast<VkDeviceSize>(EntriesCpu_.size()) * sizeof(Entry);
|
||||||
|
auto off = Staging_->Allocate(entriesBytes, CopyOffsetAlignment_);
|
||||||
|
if(!off) {
|
||||||
|
_emitEventOncePerFlush(AtlasEvent::StagingOverflow);
|
||||||
|
} else {
|
||||||
|
std::memcpy(static_cast<uint8_t*>(Staging_->Mapped()) + *off,
|
||||||
|
EntriesCpu_.data(),
|
||||||
|
static_cast<size_t>(entriesBytes));
|
||||||
|
|
||||||
|
VkBufferCopy c{};
|
||||||
|
c.srcOffset = *off;
|
||||||
|
c.dstOffset = 0;
|
||||||
|
c.size = entriesBytes;
|
||||||
|
vkCmdCopyBuffer(cmdBuffer, Staging_->Buffer(), Entries_.Buffer, 1, &c);
|
||||||
|
|
||||||
|
VkBufferMemoryBarrier b{};
|
||||||
|
b.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||||
|
b.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
||||||
|
b.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||||
|
b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||||
|
b.buffer = Entries_.Buffer;
|
||||||
|
b.offset = 0;
|
||||||
|
b.size = VK_WHOLE_SIZE;
|
||||||
|
|
||||||
|
vkCmdPipelineBarrier(cmdBuffer,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_VERTEX_SHADER_BIT |
|
||||||
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT |
|
||||||
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||||
|
0, 0, nullptr, 1, &b, 0, nullptr);
|
||||||
|
EntriesDirty_ = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(anyAtlasWrites) {
|
||||||
|
_transitionImage(cmdBuffer, Atlas_,
|
||||||
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||||
|
VK_ACCESS_SHADER_READ_BIT,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
||||||
|
} else if(Atlas_.Layout == VK_IMAGE_LAYOUT_UNDEFINED) {
|
||||||
|
_transitionImage(cmdBuffer, Atlas_,
|
||||||
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
|
0, VK_ACCESS_SHADER_READ_BIT,
|
||||||
|
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
||||||
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(anyRepackWrites) {
|
||||||
|
_transitionImage(cmdBuffer, Repack_.Atlas,
|
||||||
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||||
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||||
|
VK_ACCESS_SHADER_READ_BIT,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Repack_.Active) {
|
||||||
|
if(Repack_.Pending.empty()) {
|
||||||
|
Repack_.WaitingGpuForReady = true;
|
||||||
|
}
|
||||||
|
Repack_.WroteSomethingThisFlush = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _buildDescriptorOut();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::notifyGpuFinished() {
|
||||||
|
_ensureAliveOrThrow();
|
||||||
|
|
||||||
|
for(auto& img : DeferredImages_) {
|
||||||
|
_destroyImage(img);
|
||||||
|
}
|
||||||
|
DeferredImages_.clear();
|
||||||
|
|
||||||
|
if(Staging_) {
|
||||||
|
Staging_->Reset();
|
||||||
|
}
|
||||||
|
FlushEventMask_ = 0;
|
||||||
|
|
||||||
|
if(Repack_.Active && Repack_.WaitingGpuForReady && Repack_.Pending.empty()) {
|
||||||
|
Repack_.SwapReady = true;
|
||||||
|
Repack_.WaitingGpuForReady = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextureAtlas::_moveFrom(TextureAtlas&& other) noexcept {
|
||||||
|
Device_ = other.Device_;
|
||||||
|
Phys_ = other.Phys_;
|
||||||
|
Cfg_ = other.Cfg_;
|
||||||
|
OnEvent_ = std::move(other.OnEvent_);
|
||||||
|
Alive_ = other.Alive_;
|
||||||
|
CopyOffsetAlignment_ = other.CopyOffsetAlignment_;
|
||||||
|
Staging_ = std::move(other.Staging_);
|
||||||
|
Entries_ = other.Entries_;
|
||||||
|
Atlas_ = other.Atlas_;
|
||||||
|
Sampler_ = other.Sampler_;
|
||||||
|
OwnsSampler_ = other.OwnsSampler_;
|
||||||
|
EntriesCpu_ = std::move(other.EntriesCpu_);
|
||||||
|
EntriesDirty_ = other.EntriesDirty_;
|
||||||
|
Slots_ = std::move(other.Slots_);
|
||||||
|
FreeIds_ = std::move(other.FreeIds_);
|
||||||
|
NextId_ = other.NextId_;
|
||||||
|
Pending_ = std::move(other.Pending_);
|
||||||
|
PendingInQueue_ = std::move(other.PendingInQueue_);
|
||||||
|
Packers_ = std::move(other.Packers_);
|
||||||
|
DeferredImages_ = std::move(other.DeferredImages_);
|
||||||
|
FlushEventMask_ = other.FlushEventMask_;
|
||||||
|
GrewThisFlush_ = other.GrewThisFlush_;
|
||||||
|
Repack_ = std::move(other.Repack_);
|
||||||
|
|
||||||
|
other.Device_ = VK_NULL_HANDLE;
|
||||||
|
other.Phys_ = VK_NULL_HANDLE;
|
||||||
|
other.OnEvent_ = {};
|
||||||
|
other.Alive_ = false;
|
||||||
|
other.CopyOffsetAlignment_ = 0;
|
||||||
|
other.Staging_.reset();
|
||||||
|
other.Entries_ = {};
|
||||||
|
other.Atlas_ = {};
|
||||||
|
other.Sampler_ = VK_NULL_HANDLE;
|
||||||
|
other.OwnsSampler_ = false;
|
||||||
|
other.EntriesCpu_.clear();
|
||||||
|
other.EntriesDirty_ = false;
|
||||||
|
other.Slots_.clear();
|
||||||
|
other.FreeIds_.clear();
|
||||||
|
other.NextId_ = 0;
|
||||||
|
other.Pending_.clear();
|
||||||
|
other.PendingInQueue_.clear();
|
||||||
|
other.Packers_.clear();
|
||||||
|
other.DeferredImages_.clear();
|
||||||
|
other.FlushEventMask_ = 0;
|
||||||
|
other.GrewThisFlush_ = false;
|
||||||
|
other.Repack_ = RepackState{};
|
||||||
|
}
|
||||||
1394
Src/Client/Vulkan/AtlasPipeline/TextureAtlas.hpp
Normal file
1271
Src/Client/Vulkan/AtlasPipeline/TexturePipelineProgram.hpp
Normal file
@@ -1,12 +1,43 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Vulkan.hpp"
|
#include "Vulkan.hpp"
|
||||||
|
#include "Client/Vulkan/AtlasPipeline/SharedStagingBuffer.hpp"
|
||||||
|
#include <algorithm>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
|
|
||||||
|
|
||||||
namespace LV::Client::VK {
|
namespace LV::Client::VK {
|
||||||
|
|
||||||
|
inline std::weak_ptr<SharedStagingBuffer>& globalVertexStaging() {
|
||||||
|
static std::weak_ptr<SharedStagingBuffer> staging;
|
||||||
|
return staging;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::shared_ptr<SharedStagingBuffer> getOrCreateVertexStaging(Vulkan* inst) {
|
||||||
|
auto& staging = globalVertexStaging();
|
||||||
|
std::shared_ptr<SharedStagingBuffer> shared = staging.lock();
|
||||||
|
if(!shared) {
|
||||||
|
shared = std::make_shared<SharedStagingBuffer>(
|
||||||
|
inst->Graphics.Device,
|
||||||
|
inst->Graphics.PhysicalDevice
|
||||||
|
);
|
||||||
|
staging = shared;
|
||||||
|
}
|
||||||
|
return shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void resetVertexStaging() {
|
||||||
|
auto& staging = globalVertexStaging();
|
||||||
|
if(auto shared = staging.lock())
|
||||||
|
shared->Reset();
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Память на устройстве выделяется пулами
|
Память на устройстве выделяется пулами
|
||||||
Для массивов вершин память выделяется блоками по PerBlock вершин в каждом
|
Для массивов вершин память выделяется блоками по PerBlock вершин в каждом
|
||||||
@@ -22,10 +53,8 @@ class VertexPool {
|
|||||||
Vulkan *Inst;
|
Vulkan *Inst;
|
||||||
|
|
||||||
// Память, доступная для обмена с устройством
|
// Память, доступная для обмена с устройством
|
||||||
Buffer HostCoherent;
|
std::shared_ptr<SharedStagingBuffer> Staging;
|
||||||
Vertex *HCPtr = nullptr;
|
VkDeviceSize CopyOffsetAlignment = 4;
|
||||||
VkFence Fence = nullptr;
|
|
||||||
size_t WritePos = 0;
|
|
||||||
|
|
||||||
struct Pool {
|
struct Pool {
|
||||||
// Память на устройстве
|
// Память на устройстве
|
||||||
@@ -47,7 +76,6 @@ class VertexPool {
|
|||||||
|
|
||||||
struct Task {
|
struct Task {
|
||||||
std::vector<Vertex> Data;
|
std::vector<Vertex> Data;
|
||||||
size_t Pos = -1; // Если данные уже записаны, то будет указана позиция в буфере общения
|
|
||||||
uint8_t PoolId; // Куда потом направить
|
uint8_t PoolId; // Куда потом направить
|
||||||
uint16_t BlockId; // И в какой блок
|
uint16_t BlockId; // И в какой блок
|
||||||
};
|
};
|
||||||
@@ -61,46 +89,21 @@ class VertexPool {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void pushData(std::vector<Vertex>&& data, uint8_t poolId, uint16_t blockId) {
|
void pushData(std::vector<Vertex>&& data, uint8_t poolId, uint16_t blockId) {
|
||||||
if(HC_Buffer_Size-WritePos >= data.size()) {
|
TasksWait.push({std::move(data), poolId, blockId});
|
||||||
// Пишем в общий буфер, TasksWait
|
|
||||||
Vertex *ptr = HCPtr+WritePos;
|
|
||||||
std::copy(data.begin(), data.end(), ptr);
|
|
||||||
size_t count = data.size();
|
|
||||||
TasksWait.push({std::move(data), WritePos, poolId, blockId});
|
|
||||||
WritePos += count;
|
|
||||||
} else {
|
|
||||||
// Отложим запись на следующий такт
|
|
||||||
TasksPostponed.push(Task(std::move(data), -1, poolId, blockId));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VertexPool(Vulkan* inst)
|
VertexPool(Vulkan* inst)
|
||||||
: Inst(inst),
|
: Inst(inst)
|
||||||
HostCoherent(inst,
|
|
||||||
sizeof(Vertex)*HC_Buffer_Size+4 /* Для vkCmdFillBuffer */,
|
|
||||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
|
|
||||||
{
|
{
|
||||||
Pools.reserve(16);
|
Pools.reserve(16);
|
||||||
HCPtr = (Vertex*) HostCoherent.mapMemory();
|
Staging = getOrCreateVertexStaging(inst);
|
||||||
|
VkPhysicalDeviceProperties props{};
|
||||||
const VkFenceCreateInfo info = {
|
vkGetPhysicalDeviceProperties(inst->Graphics.PhysicalDevice, &props);
|
||||||
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
CopyOffsetAlignment = std::max<VkDeviceSize>(4, props.limits.optimalBufferCopyOffsetAlignment);
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkCreateFence(inst->Graphics.Device, &info, nullptr, &Fence));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~VertexPool() {
|
~VertexPool() {
|
||||||
if(HCPtr)
|
|
||||||
HostCoherent.unMapMemory();
|
|
||||||
|
|
||||||
if(Fence) {
|
|
||||||
vkDestroyFence(Inst->Graphics.Device, Fence, nullptr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -229,44 +232,65 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Должно вызываться после приёма всех данных и перед рендером
|
Должно вызываться после приёма всех данных, до начала рендера в командном буфере
|
||||||
*/
|
*/
|
||||||
void update(VkCommandPool commandPool) {
|
void flushUploadsAndBarriers(VkCommandBuffer commandBuffer) {
|
||||||
if(TasksWait.empty())
|
if(TasksWait.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
assert(WritePos);
|
struct CopyTask {
|
||||||
|
VkBuffer DstBuffer;
|
||||||
VkCommandBufferAllocateInfo allocInfo {
|
VkDeviceSize SrcOffset;
|
||||||
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
VkDeviceSize DstOffset;
|
||||||
nullptr,
|
VkDeviceSize Size;
|
||||||
commandPool,
|
uint8_t PoolId;
|
||||||
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
||||||
1
|
|
||||||
};
|
};
|
||||||
|
|
||||||
VkCommandBuffer commandBuffer;
|
std::vector<CopyTask> copies;
|
||||||
vkAllocateCommandBuffers(Inst->Graphics.Device, &allocInfo, &commandBuffer);
|
copies.reserve(TasksWait.size());
|
||||||
|
std::vector<uint8_t> touchedPools(Pools.size(), 0);
|
||||||
|
|
||||||
VkCommandBufferBeginInfo beginInfo {
|
while(!TasksWait.empty()) {
|
||||||
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
Task task = std::move(TasksWait.front());
|
||||||
nullptr,
|
TasksWait.pop();
|
||||||
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
|
||||||
nullptr
|
|
||||||
};
|
|
||||||
|
|
||||||
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
VkDeviceSize bytes = task.Data.size()*sizeof(Vertex);
|
||||||
|
std::optional<VkDeviceSize> stagingOffset = Staging->Allocate(bytes, CopyOffsetAlignment);
|
||||||
|
if(!stagingOffset) {
|
||||||
|
TasksPostponed.push(std::move(task));
|
||||||
|
while(!TasksWait.empty()) {
|
||||||
|
TasksPostponed.push(std::move(TasksWait.front()));
|
||||||
|
TasksWait.pop();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
VkBufferMemoryBarrier barrier = {
|
std::memcpy(static_cast<uint8_t*>(Staging->Mapped()) + *stagingOffset,
|
||||||
|
task.Data.data(), bytes);
|
||||||
|
|
||||||
|
copies.push_back({
|
||||||
|
Pools[task.PoolId].DeviceBuff.getBuffer(),
|
||||||
|
*stagingOffset,
|
||||||
|
task.BlockId*sizeof(Vertex)*size_t(PerBlock),
|
||||||
|
bytes,
|
||||||
|
task.PoolId
|
||||||
|
});
|
||||||
|
touchedPools[task.PoolId] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(copies.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
VkBufferMemoryBarrier stagingBarrier = {
|
||||||
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||||
nullptr,
|
nullptr,
|
||||||
VK_ACCESS_HOST_WRITE_BIT,
|
VK_ACCESS_HOST_WRITE_BIT,
|
||||||
VK_ACCESS_TRANSFER_READ_BIT,
|
VK_ACCESS_TRANSFER_READ_BIT,
|
||||||
VK_QUEUE_FAMILY_IGNORED,
|
VK_QUEUE_FAMILY_IGNORED,
|
||||||
VK_QUEUE_FAMILY_IGNORED,
|
VK_QUEUE_FAMILY_IGNORED,
|
||||||
HostCoherent.getBuffer(),
|
Staging->Buffer(),
|
||||||
0,
|
0,
|
||||||
WritePos*sizeof(Vertex)
|
Staging->Size()
|
||||||
};
|
};
|
||||||
|
|
||||||
vkCmdPipelineBarrier(
|
vkCmdPipelineBarrier(
|
||||||
@@ -275,53 +299,60 @@ public:
|
|||||||
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
0,
|
0,
|
||||||
0, nullptr,
|
0, nullptr,
|
||||||
1, &barrier,
|
1, &stagingBarrier,
|
||||||
0, nullptr
|
0, nullptr
|
||||||
);
|
);
|
||||||
|
|
||||||
while(!TasksWait.empty()) {
|
for(const CopyTask& copy : copies) {
|
||||||
Task& task = TasksWait.front();
|
|
||||||
|
|
||||||
VkBufferCopy copyRegion {
|
VkBufferCopy copyRegion {
|
||||||
task.Pos*sizeof(Vertex),
|
copy.SrcOffset,
|
||||||
task.BlockId*sizeof(Vertex)*size_t(PerBlock),
|
copy.DstOffset,
|
||||||
task.Data.size()*sizeof(Vertex)
|
copy.Size
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(copyRegion.dstOffset+copyRegion.size < sizeof(Vertex)*PerBlock*PerPool);
|
assert(copyRegion.dstOffset+copyRegion.size <= Pools[copy.PoolId].DeviceBuff.getSize());
|
||||||
|
|
||||||
vkCmdCopyBuffer(commandBuffer, HostCoherent.getBuffer(), Pools[task.PoolId].DeviceBuff.getBuffer(),
|
vkCmdCopyBuffer(commandBuffer, Staging->Buffer(), copy.DstBuffer, 1, ©Region);
|
||||||
1, ©Region);
|
|
||||||
|
|
||||||
TasksWait.pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vkEndCommandBuffer(commandBuffer);
|
std::vector<VkBufferMemoryBarrier> dstBarriers;
|
||||||
|
dstBarriers.reserve(Pools.size());
|
||||||
|
for(size_t poolId = 0; poolId < Pools.size(); poolId++) {
|
||||||
|
if(!touchedPools[poolId])
|
||||||
|
continue;
|
||||||
|
|
||||||
VkSubmitInfo submitInfo {
|
VkBufferMemoryBarrier barrier = {
|
||||||
VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||||
nullptr,
|
nullptr,
|
||||||
0, nullptr,
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||||
nullptr,
|
IsIndex ? VK_ACCESS_INDEX_READ_BIT : VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
|
||||||
1,
|
VK_QUEUE_FAMILY_IGNORED,
|
||||||
&commandBuffer,
|
VK_QUEUE_FAMILY_IGNORED,
|
||||||
|
Pools[poolId].DeviceBuff.getBuffer(),
|
||||||
0,
|
0,
|
||||||
nullptr
|
Pools[poolId].DeviceBuff.getSize()
|
||||||
};
|
};
|
||||||
{
|
dstBarriers.push_back(barrier);
|
||||||
auto lockQueue = Inst->Graphics.DeviceQueueGraphic.lock();
|
|
||||||
vkAssert(!vkQueueSubmit(*lockQueue, 1, &submitInfo, Fence));
|
|
||||||
}
|
}
|
||||||
vkAssert(!vkWaitForFences(Inst->Graphics.Device, 1, &Fence, VK_TRUE, UINT64_MAX));
|
|
||||||
vkAssert(!vkResetFences(Inst->Graphics.Device, 1, &Fence));
|
|
||||||
vkFreeCommandBuffers(Inst->Graphics.Device, commandPool, 1, &commandBuffer);
|
|
||||||
|
|
||||||
|
if(!dstBarriers.empty()) {
|
||||||
|
vkCmdPipelineBarrier(
|
||||||
|
commandBuffer,
|
||||||
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
||||||
|
VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
|
||||||
|
0,
|
||||||
|
0, nullptr,
|
||||||
|
static_cast<uint32_t>(dstBarriers.size()),
|
||||||
|
dstBarriers.data(),
|
||||||
|
0, nullptr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void notifyGpuFinished() {
|
||||||
std::queue<Task> postponed = std::move(TasksPostponed);
|
std::queue<Task> postponed = std::move(TasksPostponed);
|
||||||
WritePos = 0;
|
|
||||||
|
|
||||||
while(!postponed.empty()) {
|
while(!postponed.empty()) {
|
||||||
Task& task = postponed.front();
|
TasksWait.push(std::move(postponed.front()));
|
||||||
pushData(std::move(task.Data), task.PoolId, task.BlockId);
|
|
||||||
postponed.pop();
|
postponed.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -275,10 +275,6 @@ void Vulkan::run()
|
|||||||
// if(CallBeforeDraw)
|
// if(CallBeforeDraw)
|
||||||
// CallBeforeDraw(this);
|
// CallBeforeDraw(this);
|
||||||
|
|
||||||
if(Game.RSession) {
|
|
||||||
Game.RSession->beforeDraw();
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
VkResult err;
|
VkResult err;
|
||||||
@@ -314,6 +310,10 @@ void Vulkan::run()
|
|||||||
vkAssert(!vkBeginCommandBuffer(Graphics.CommandBufferRender, &cmd_buf_info));
|
vkAssert(!vkBeginCommandBuffer(Graphics.CommandBufferRender, &cmd_buf_info));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(Game.RSession) {
|
||||||
|
Game.RSession->beforeDraw();
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
VkImageMemoryBarrier image_memory_barrier =
|
VkImageMemoryBarrier image_memory_barrier =
|
||||||
{
|
{
|
||||||
@@ -602,6 +602,8 @@ void Vulkan::run()
|
|||||||
// Насильно ожидаем завершения рендера кадра
|
// Насильно ожидаем завершения рендера кадра
|
||||||
vkWaitForFences(Graphics.Device, 1, &drawEndFence, true, -1);
|
vkWaitForFences(Graphics.Device, 1, &drawEndFence, true, -1);
|
||||||
vkResetFences(Graphics.Device, 1, &drawEndFence);
|
vkResetFences(Graphics.Device, 1, &drawEndFence);
|
||||||
|
if(Game.RSession)
|
||||||
|
Game.RSession->onGpuFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 16; y++)
|
||||||
fullNodes[17][y+1][z+1] = 1;
|
fullNodes[17][y+1][z+1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunks[1]) {
|
if(chunks[1]) {
|
||||||
@@ -223,7 +223,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 16; y++)
|
||||||
fullNodes[0][y+1][z+1] = 1;
|
fullNodes[0][y+1][z+1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunks[2]) {
|
if(chunks[2]) {
|
||||||
@@ -235,7 +235,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
for(int x = 0; x < 16; x++)
|
for(int x = 0; x < 16; x++)
|
||||||
fullNodes[x+1][17][z+1] = 1;
|
fullNodes[x+1][17][z+1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunks[3]) {
|
if(chunks[3]) {
|
||||||
@@ -247,7 +247,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
for(int x = 0; x < 16; x++)
|
for(int x = 0; x < 16; x++)
|
||||||
fullNodes[x+1][0][z+1] = 1;
|
fullNodes[x+1][0][z+1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunks[4]) {
|
if(chunks[4]) {
|
||||||
@@ -259,7 +259,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 16; y++)
|
||||||
for(int x = 0; x < 16; x++)
|
for(int x = 0; x < 16; x++)
|
||||||
fullNodes[x+1][y+1][17] = 1;
|
fullNodes[x+1][y+1][17] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(chunks[5]) {
|
if(chunks[5]) {
|
||||||
@@ -271,7 +271,7 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
} else {
|
} else {
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 16; y++)
|
||||||
for(int x = 0; x < 16; x++)
|
for(int x = 0; x < 16; x++)
|
||||||
fullNodes[x+0][y+1][0] = 1;
|
fullNodes[x+0][y+1][0] = 0;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
goto end;
|
goto end;
|
||||||
@@ -307,6 +307,72 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
NodeVertexStatic v;
|
NodeVertexStatic v;
|
||||||
std::memset(&v, 0, sizeof(v));
|
std::memset(&v, 0, sizeof(v));
|
||||||
|
|
||||||
|
struct ModelCacheEntry {
|
||||||
|
std::vector<std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>>> Routes;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<uint32_t, ModelCacheEntry> modelCache;
|
||||||
|
std::unordered_map<AssetsTexture, uint16_t> baseTextureCache;
|
||||||
|
|
||||||
|
std::vector<NodeStateInfo> metaStatesInfo;
|
||||||
|
{
|
||||||
|
NodeStateInfo info;
|
||||||
|
info.Name = "meta";
|
||||||
|
info.Variations = 256;
|
||||||
|
metaStatesInfo.push_back(std::move(info));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto isFaceCovered = [&](EnumFace face, int covered) -> bool {
|
||||||
|
switch(face) {
|
||||||
|
case EnumFace::Up: return covered & (1 << 2);
|
||||||
|
case EnumFace::Down: return covered & (1 << 3);
|
||||||
|
case EnumFace::East: return covered & (1 << 0);
|
||||||
|
case EnumFace::West: return covered & (1 << 1);
|
||||||
|
case EnumFace::South: return covered & (1 << 4);
|
||||||
|
case EnumFace::North: return covered & (1 << 5);
|
||||||
|
default: return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto pickVariant = [&](const std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>>& variants, uint32_t seed)
|
||||||
|
-> const std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>*
|
||||||
|
{
|
||||||
|
if(variants.empty())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
float total = 0.0f;
|
||||||
|
for(const auto& entry : variants)
|
||||||
|
total += std::max(0.0f, entry.first);
|
||||||
|
|
||||||
|
if(total <= 0.0f)
|
||||||
|
return &variants.front().second;
|
||||||
|
|
||||||
|
float r = (seed % 10000u) / 10000.0f * total;
|
||||||
|
float accum = 0.0f;
|
||||||
|
for(const auto& entry : variants) {
|
||||||
|
accum += std::max(0.0f, entry.first);
|
||||||
|
if(r <= accum)
|
||||||
|
return &entry.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &variants.back().second;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto appendModel = [&](const std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>& faces, int covered, int x, int y, int z) {
|
||||||
|
for(const auto& [face, verts] : faces) {
|
||||||
|
if(face != EnumFace::None && isFaceCovered(face, covered))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for(const NodeVertexStatic& baseVert : verts) {
|
||||||
|
NodeVertexStatic vert = baseVert;
|
||||||
|
vert.FX = uint32_t(vert.FX + x * 64);
|
||||||
|
vert.FY = uint32_t(vert.FY + y * 64);
|
||||||
|
vert.FZ = uint32_t(vert.FZ + z * 64);
|
||||||
|
result.NodeVertexs.push_back(vert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Сбор вершин
|
// Сбор вершин
|
||||||
for(int z = 0; z < 16; z++)
|
for(int z = 0; z < 16; z++)
|
||||||
for(int y = 0; y < 16; y++)
|
for(int y = 0; y < 16; y++)
|
||||||
@@ -323,208 +389,259 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
if(fullCovered == 0b111111)
|
if(fullCovered == 0b111111)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const DefNode_t* node = getNodeProfile((*chunk)[x+y*16+z*16*16].NodeId);
|
const Node& nodeData = (*chunk)[x+y*16+z*16*16];
|
||||||
|
const DefNode_t* node = getNodeProfile(nodeData.NodeId);
|
||||||
|
|
||||||
|
bool usedModel = false;
|
||||||
|
|
||||||
|
if(NSP && node->NodestateId != 0) {
|
||||||
|
auto iterCache = modelCache.find(nodeData.Data);
|
||||||
|
if(iterCache == modelCache.end()) {
|
||||||
|
std::unordered_map<std::string, int32_t> states;
|
||||||
|
states.emplace("meta", nodeData.Meta);
|
||||||
|
|
||||||
|
ModelCacheEntry entry;
|
||||||
|
entry.Routes = NSP->getModelsForNode(node->NodestateId, metaStatesInfo, states);
|
||||||
|
iterCache = modelCache.emplace(nodeData.Data, std::move(entry)).first;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!iterCache->second.Routes.empty()) {
|
||||||
|
uint32_t seed = uint32_t(nodeData.Data) * 2654435761u;
|
||||||
|
seed ^= uint32_t(x) * 73856093u;
|
||||||
|
seed ^= uint32_t(y) * 19349663u;
|
||||||
|
seed ^= uint32_t(z) * 83492791u;
|
||||||
|
|
||||||
|
for(size_t routeIndex = 0; routeIndex < iterCache->second.Routes.size(); routeIndex++) {
|
||||||
|
const auto& variants = iterCache->second.Routes[routeIndex];
|
||||||
|
const auto* faces = pickVariant(variants, seed + uint32_t(routeIndex) * 374761393u);
|
||||||
|
if(faces)
|
||||||
|
appendModel(*faces, fullCovered, x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
usedModel = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(usedModel)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(NSP && node->TexId != 0) {
|
||||||
|
auto iterTex = baseTextureCache.find(node->TexId);
|
||||||
|
if(iterTex != baseTextureCache.end()) {
|
||||||
|
v.Tex = iterTex->second;
|
||||||
|
} else {
|
||||||
|
uint16_t resolvedTex = NSP->getTextureId(node->TexId);
|
||||||
|
v.Tex = resolvedTex;
|
||||||
|
baseTextureCache.emplace(node->TexId, resolvedTex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
v.Tex = node->TexId;
|
v.Tex = node->TexId;
|
||||||
|
}
|
||||||
|
|
||||||
if(v.Tex == 0)
|
if(v.Tex == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Рендерим обычный кубоид
|
// Рендерим обычный кубоид
|
||||||
|
// XZ+Y
|
||||||
if(!(fullCovered & 0b000100)) {
|
if(!(fullCovered & 0b000100)) {
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16+16;
|
v.FY = 224+y*64+64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX -= 16;
|
v.FX -= 64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XZ-Y
|
||||||
if(!(fullCovered & 0b001000)) {
|
if(!(fullCovered & 0b001000)) {
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ += 16;
|
v.FZ += 64;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//YZ+X
|
||||||
if(!(fullCovered & 0b000001)) {
|
if(!(fullCovered & 0b000001)) {
|
||||||
v.FX = 224+x*16+16;
|
v.FX = 224+x*64+64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ += 16;
|
v.FZ += 64;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//YZ-X
|
||||||
if(!(fullCovered & 0b000010)) {
|
if(!(fullCovered & 0b000010)) {
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.FZ -= 16;
|
v.FZ -= 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY -= 16;
|
v.FY -= 64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//XY+Z
|
||||||
if(!(fullCovered & 0b010000)) {
|
if(!(fullCovered & 0b010000)) {
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16+16;
|
v.FZ = 224+z*64+64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX -= 16;
|
v.FX -= 64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XY-Z
|
||||||
if(!(fullCovered & 0b100000)) {
|
if(!(fullCovered & 0b100000)) {
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.FZ = 224+z*16;
|
v.FZ = 224+z*64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX = 224+x*16;
|
v.FX = 224+x*64;
|
||||||
v.FY = 224+y*16;
|
v.FY = 224+y*64;
|
||||||
v.TU = 0;
|
v.TU = 0;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FX += 16;
|
v.FX += 64;
|
||||||
v.FY += 16;
|
v.FY += 64;
|
||||||
v.TV = 65535;
|
v.TV = 65535;
|
||||||
v.TU = 65535;
|
v.TU = 65535;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
|
|
||||||
v.FY -= 16;
|
v.FY -= 64;
|
||||||
v.TV = 0;
|
v.TV = 0;
|
||||||
result.NodeVertexs.push_back(v);
|
result.NodeVertexs.push_back(v);
|
||||||
}
|
}
|
||||||
@@ -563,7 +680,6 @@ void ChunkMeshGenerator::run(uint8_t id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
Output.lock()->emplace_back(std::move(result));
|
Output.lock()->emplace_back(std::move(result));
|
||||||
|
|
||||||
@@ -588,10 +704,74 @@ void ChunkPreparator::tickSync(const TickSyncData& data) {
|
|||||||
// Пересчёт соседних чанков
|
// Пересчёт соседних чанков
|
||||||
// Проверить необходимость пересчёта чанков при изменении профилей
|
// Проверить необходимость пересчёта чанков при изменении профилей
|
||||||
|
|
||||||
|
std::unordered_map<WorldId_t, std::vector<Pos::GlobalChunk>> changedChunks = data.ChangedChunks;
|
||||||
|
|
||||||
|
if(!data.ChangedNodes.empty()) {
|
||||||
|
std::unordered_set<DefNodeId> changedNodes(data.ChangedNodes.begin(), data.ChangedNodes.end());
|
||||||
|
|
||||||
|
for(const auto& [wId, regions] : ChunksMesh) {
|
||||||
|
for(const auto& [rPos, chunks] : regions) {
|
||||||
|
Pos::GlobalChunk base = Pos::GlobalChunk(rPos) << 2;
|
||||||
|
|
||||||
|
for(size_t index = 0; index < chunks.size(); index++) {
|
||||||
|
const ChunkObj_t& chunk = chunks[index];
|
||||||
|
if(chunk.Nodes.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
bool hit = false;
|
||||||
|
for(DefNodeId nodeId : chunk.Nodes) {
|
||||||
|
if(changedNodes.contains(nodeId)) {
|
||||||
|
hit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!hit)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Pos::bvec4u localPos;
|
||||||
|
localPos.unpack(index);
|
||||||
|
changedChunks[wId].push_back(base + Pos::GlobalChunk(localPos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!data.ChangedVoxels.empty()) {
|
||||||
|
std::unordered_set<DefVoxelId> changedVoxels(data.ChangedVoxels.begin(), data.ChangedVoxels.end());
|
||||||
|
|
||||||
|
for(const auto& [wId, regions] : ChunksMesh) {
|
||||||
|
for(const auto& [rPos, chunks] : regions) {
|
||||||
|
Pos::GlobalChunk base = Pos::GlobalChunk(rPos) << 2;
|
||||||
|
|
||||||
|
for(size_t index = 0; index < chunks.size(); index++) {
|
||||||
|
const ChunkObj_t& chunk = chunks[index];
|
||||||
|
if(chunk.Voxels.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
bool hit = false;
|
||||||
|
for(DefVoxelId voxelId : chunk.Voxels) {
|
||||||
|
if(changedVoxels.contains(voxelId)) {
|
||||||
|
hit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!hit)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Pos::bvec4u localPos;
|
||||||
|
localPos.unpack(index);
|
||||||
|
changedChunks[wId].push_back(base + Pos::GlobalChunk(localPos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Добавляем к изменёным чанкам пересчёт соседей
|
// Добавляем к изменёным чанкам пересчёт соседей
|
||||||
{
|
{
|
||||||
std::vector<std::tuple<WorldId_t, Pos::GlobalChunk, uint32_t>> toBuild;
|
std::vector<std::tuple<WorldId_t, Pos::GlobalChunk, uint32_t>> toBuild;
|
||||||
for(auto& [wId, chunks] : data.ChangedChunks) {
|
for(auto& [wId, chunks] : changedChunks) {
|
||||||
std::vector<Pos::GlobalChunk> list;
|
std::vector<Pos::GlobalChunk> list;
|
||||||
for(const Pos::GlobalChunk& pos : chunks) {
|
for(const Pos::GlobalChunk& pos : chunks) {
|
||||||
list.push_back(pos);
|
list.push_back(pos);
|
||||||
@@ -683,11 +863,6 @@ void ChunkPreparator::tickSync(const TickSyncData& data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexPool_Voxels.update(CMDPool);
|
|
||||||
VertexPool_Nodes.update(CMDPool);
|
|
||||||
IndexPool_Nodes_16.update(CMDPool);
|
|
||||||
IndexPool_Nodes_32.update(CMDPool);
|
|
||||||
|
|
||||||
CMG.endTickSync();
|
CMG.endTickSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -824,7 +999,7 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
: VkInst(vkInst),
|
: VkInst(vkInst),
|
||||||
ServerSession(serverSession),
|
ServerSession(serverSession),
|
||||||
CP(vkInst, serverSession),
|
CP(vkInst, serverSession),
|
||||||
MainTest(vkInst), LightDummy(vkInst),
|
LightDummy(vkInst),
|
||||||
TestQuad(vkInst, sizeof(NodeVertexStatic)*6*3*2)
|
TestQuad(vkInst, sizeof(NodeVertexStatic)*6*3*2)
|
||||||
{
|
{
|
||||||
assert(vkInst);
|
assert(vkInst);
|
||||||
@@ -849,49 +1024,9 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
&DescriptorPool));
|
&DescriptorPool));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
TP = std::make_unique<TextureProvider>(VkInst, DescriptorPool);
|
||||||
std::vector<VkDescriptorSetLayoutBinding> shaderLayoutBindings =
|
NSP = std::make_unique<NodestateProvider>(MP, *TP);
|
||||||
{
|
CP.setNodestateProvider(NSP.get());
|
||||||
{
|
|
||||||
.binding = 0,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
.pImmutableSamplers = nullptr
|
|
||||||
}, {
|
|
||||||
.binding = 1,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
|
|
||||||
.pImmutableSamplers = nullptr
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.bindingCount = (uint32_t) shaderLayoutBindings.size(),
|
|
||||||
.pBindings = shaderLayoutBindings.data()
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkCreateDescriptorSetLayout(
|
|
||||||
VkInst->Graphics.Device, &descriptorLayout, nullptr, &MainAtlasDescLayout));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
VkDescriptorSetAllocateInfo ciAllocInfo =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.descriptorPool = DescriptorPool,
|
|
||||||
.descriptorSetCount = 1,
|
|
||||||
.pSetLayouts = &MainAtlasDescLayout
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkAllocateDescriptorSets(VkInst->Graphics.Device, &ciAllocInfo, &MainAtlasDescriptor));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::vector<VkDescriptorSetLayoutBinding> shaderLayoutBindings =
|
std::vector<VkDescriptorSetLayoutBinding> shaderLayoutBindings =
|
||||||
@@ -937,39 +1072,11 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MainTest.atlasAddCallbackOnUniformChange([this]() -> bool {
|
|
||||||
updateDescriptor_MainAtlas();
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
LightDummy.atlasAddCallbackOnUniformChange([this]() -> bool {
|
LightDummy.atlasAddCallbackOnUniformChange([this]() -> bool {
|
||||||
updateDescriptor_VoxelsLight();
|
updateDescriptor_VoxelsLight();
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
{
|
|
||||||
uint16_t texId = MainTest.atlasAddTexture(2, 2);
|
|
||||||
uint32_t colors[4] = {0xfffffffful, 0x00fffffful, 0xffffff00ul, 0xff00fffful};
|
|
||||||
MainTest.atlasChangeTextureData(texId, (const uint32_t*) colors);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
int width, height;
|
|
||||||
bool hasAlpha;
|
|
||||||
for(const char *path : {
|
|
||||||
"grass.png",
|
|
||||||
"willow_wood.png",
|
|
||||||
"tropical_rainforest_wood.png",
|
|
||||||
"xnether_blue_wood.png",
|
|
||||||
"xnether_purple_wood.png",
|
|
||||||
"frame.png"
|
|
||||||
}) {
|
|
||||||
ByteBuffer image = VK::loadPNG(getResource(std::string("textures/") + path)->makeStream().Stream, width, height, hasAlpha);
|
|
||||||
uint16_t texId = MainTest.atlasAddTexture(width, height);
|
|
||||||
MainTest.atlasChangeTextureData(texId, (const uint32_t*) image.data());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
x left -1 ~ right 1
|
x left -1 ~ right 1
|
||||||
y up 1 ~ down -1
|
y up 1 ~ down -1
|
||||||
@@ -981,47 +1088,47 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
|
|
||||||
{
|
{
|
||||||
NodeVertexStatic *array = (NodeVertexStatic*) TestQuad.mapMemory();
|
NodeVertexStatic *array = (NodeVertexStatic*) TestQuad.mapMemory();
|
||||||
array[0] = {135, 135, 135, 0, 0, 0, 0, 65535, 0};
|
array[0] = {224, 224, 0, 224, 0, 0, 0, 65535, 0};
|
||||||
array[1] = {135, 135+16, 135, 0, 0, 0, 0, 0, 65535};
|
array[1] = {224, 224+64, 0, 224, 0, 0, 0, 0, 65535};
|
||||||
array[2] = {135+16, 135+16, 135, 0, 0, 0, 0, 0, 65535};
|
array[2] = {224+64, 224+64, 0, 224, 0, 0, 0, 0, 65535};
|
||||||
array[3] = {135, 135, 135, 0, 0, 0, 0, 65535, 0};
|
array[3] = {224, 224, 0, 224, 0, 0, 0, 65535, 0};
|
||||||
array[4] = {135+16, 135+16, 135, 0, 0, 0, 0, 0, 65535};
|
array[4] = {224+64, 224+64, 0, 224, 0, 0, 0, 0, 65535};
|
||||||
array[5] = {135+16, 135, 135, 0, 0, 0, 0, 0, 0};
|
array[5] = {224+64, 224, 0, 224, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
array[6] = {135, 135, 135+16, 0, 0, 0, 0, 0, 0};
|
array[6] = {224, 224, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[7] = {135+16, 135, 135+16, 0, 0, 0, 0, 65535, 0};
|
array[7] = {224+64, 224, 0, 224+64, 0, 0, 0, 65535, 0};
|
||||||
array[8] = {135+16, 135+16, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[8] = {224+64, 224+64, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[9] = {135, 135, 135+16, 0, 0, 0, 0, 0, 0};
|
array[9] = {224, 224, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[10] = {135+16, 135+16, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[10] = {224+64, 224+64, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[11] = {135, 135+16, 135+16, 0, 0, 0, 0, 0, 65535};
|
array[11] = {224, 224+64, 0, 224+64, 0, 0, 0, 0, 65535};
|
||||||
|
|
||||||
array[12] = {135, 135, 135, 0, 0, 0, 0, 0, 0};
|
array[12] = {224, 224, 0, 224, 0, 0, 0, 0, 0};
|
||||||
array[13] = {135, 135, 135+16, 0, 0, 0, 0, 65535, 0};
|
array[13] = {224, 224, 0, 224+64, 0, 0, 0, 65535, 0};
|
||||||
array[14] = {135, 135+16, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[14] = {224, 224+64, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[15] = {135, 135, 135, 0, 0, 0, 0, 0, 0};
|
array[15] = {224, 224, 0, 224, 0, 0, 0, 0, 0};
|
||||||
array[16] = {135, 135+16, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[16] = {224, 224+64, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[17] = {135, 135+16, 135, 0, 0, 0, 0, 0, 65535};
|
array[17] = {224, 224+64, 0, 224, 0, 0, 0, 0, 65535};
|
||||||
|
|
||||||
array[18] = {135+16, 135, 135+16, 0, 0, 0, 0, 0, 0};
|
array[18] = {224+64, 224, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[19] = {135+16, 135, 135, 0, 0, 0, 0, 65535, 0};
|
array[19] = {224+64, 224, 0, 224, 0, 0, 0, 65535, 0};
|
||||||
array[20] = {135+16, 135+16, 135, 0, 0, 0, 0, 65535, 65535};
|
array[20] = {224+64, 224+64, 0, 224, 0, 0, 0, 65535, 65535};
|
||||||
array[21] = {135+16, 135, 135+16, 0, 0, 0, 0, 0, 0};
|
array[21] = {224+64, 224, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[22] = {135+16, 135+16, 135, 0, 0, 0, 0, 65535, 65535};
|
array[22] = {224+64, 224+64, 0, 224, 0, 0, 0, 65535, 65535};
|
||||||
array[23] = {135+16, 135+16, 135+16, 0, 0, 0, 0, 0, 65535};
|
array[23] = {224+64, 224+64, 0, 224+64, 0, 0, 0, 0, 65535};
|
||||||
|
|
||||||
array[24] = {135, 135, 135, 0, 0, 0, 0, 0, 0};
|
array[24] = {224, 224, 0, 224, 0, 0, 0, 0, 0};
|
||||||
array[25] = {135+16, 135, 135, 0, 0, 0, 0, 65535, 0};
|
array[25] = {224+64, 224, 0, 224, 0, 0, 0, 65535, 0};
|
||||||
array[26] = {135+16, 135, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[26] = {224+64, 224, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[27] = {135, 135, 135, 0, 0, 0, 0, 0, 0};
|
array[27] = {224, 224, 0, 224, 0, 0, 0, 0, 0};
|
||||||
array[28] = {135+16, 135, 135+16, 0, 0, 0, 0, 65535, 65535};
|
array[28] = {224+64, 224, 0, 224+64, 0, 0, 0, 65535, 65535};
|
||||||
array[29] = {135, 135, 135+16, 0, 0, 0, 0, 0, 65535};
|
array[29] = {224, 224, 0, 224+64, 0, 0, 0, 0, 65535};
|
||||||
|
|
||||||
array[30] = {135, 135+16, 135+16, 0, 0, 0, 0, 0, 0};
|
array[30] = {224, 224+64, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[31] = {135+16, 135+16, 135+16, 0, 0, 0, 0, 65535, 0};
|
array[31] = {224+64, 224+64, 0, 224+64, 0, 0, 0, 65535, 0};
|
||||||
array[32] = {135+16, 135+16, 135, 0, 0, 0, 0, 65535, 65535};
|
array[32] = {224+64, 224+64, 0, 224, 0, 0, 0, 65535, 65535};
|
||||||
array[33] = {135, 135+16, 135+16, 0, 0, 0, 0, 0, 0};
|
array[33] = {224, 224+64, 0, 224+64, 0, 0, 0, 0, 0};
|
||||||
array[34] = {135+16, 135+16, 135, 0, 0, 0, 0, 65535, 65535};
|
array[34] = {224+64, 224+64, 0, 224, 0, 0, 0, 65535, 65535};
|
||||||
array[35] = {135, 135+16, 135, 0, 0, 0, 0, 0, 65535};
|
array[35] = {224, 224+64, 0, 224, 0, 0, 0, 0, 65535};
|
||||||
|
|
||||||
for(int iter = 0; iter < 36; iter++) {
|
for(int iter = 0; iter < 36; iter++) {
|
||||||
array[iter].Tex = 6;
|
array[iter].Tex = 6;
|
||||||
@@ -1067,7 +1174,6 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDescriptor_MainAtlas();
|
|
||||||
updateDescriptor_VoxelsLight();
|
updateDescriptor_VoxelsLight();
|
||||||
updateDescriptor_ChunksLight();
|
updateDescriptor_ChunksLight();
|
||||||
|
|
||||||
@@ -1084,7 +1190,7 @@ VulkanRenderSession::VulkanRenderSession(Vulkan *vkInst, IServerSession *serverS
|
|||||||
|
|
||||||
std::vector<VkDescriptorSetLayout> layouts =
|
std::vector<VkDescriptorSetLayout> layouts =
|
||||||
{
|
{
|
||||||
MainAtlasDescLayout,
|
TP ? TP->getDescriptorLayout() : VK_NULL_HANDLE,
|
||||||
VoxelLightMapDescLayout
|
VoxelLightMapDescLayout
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1434,8 +1540,6 @@ VulkanRenderSession::~VulkanRenderSession() {
|
|||||||
if(MainAtlas_LightMap_PipelineLayout)
|
if(MainAtlas_LightMap_PipelineLayout)
|
||||||
vkDestroyPipelineLayout(VkInst->Graphics.Device, MainAtlas_LightMap_PipelineLayout, nullptr);
|
vkDestroyPipelineLayout(VkInst->Graphics.Device, MainAtlas_LightMap_PipelineLayout, nullptr);
|
||||||
|
|
||||||
if(MainAtlasDescLayout)
|
|
||||||
vkDestroyDescriptorSetLayout(VkInst->Graphics.Device, MainAtlasDescLayout, nullptr);
|
|
||||||
if(VoxelLightMapDescLayout)
|
if(VoxelLightMapDescLayout)
|
||||||
vkDestroyDescriptorSetLayout(VkInst->Graphics.Device, VoxelLightMapDescLayout, nullptr);
|
vkDestroyDescriptorSetLayout(VkInst->Graphics.Device, VoxelLightMapDescLayout, nullptr);
|
||||||
|
|
||||||
@@ -1459,36 +1563,93 @@ void VulkanRenderSession::tickSync(const TickSyncData& data) {
|
|||||||
ChunkPreparator::TickSyncData mcpData;
|
ChunkPreparator::TickSyncData mcpData;
|
||||||
mcpData.ChangedChunks = data.Chunks_ChangeOrAdd;
|
mcpData.ChangedChunks = data.Chunks_ChangeOrAdd;
|
||||||
mcpData.LostRegions = data.Chunks_Lost;
|
mcpData.LostRegions = data.Chunks_Lost;
|
||||||
|
|
||||||
|
if(auto iter = data.Profiles_ChangeOrAdd.find(EnumDefContent::Node); iter != data.Profiles_ChangeOrAdd.end())
|
||||||
|
mcpData.ChangedNodes.insert(mcpData.ChangedNodes.end(), iter->second.begin(), iter->second.end());
|
||||||
|
if(auto iter = data.Profiles_Lost.find(EnumDefContent::Node); iter != data.Profiles_Lost.end())
|
||||||
|
mcpData.ChangedNodes.insert(mcpData.ChangedNodes.end(), iter->second.begin(), iter->second.end());
|
||||||
|
if(auto iter = data.Profiles_ChangeOrAdd.find(EnumDefContent::Voxel); iter != data.Profiles_ChangeOrAdd.end())
|
||||||
|
mcpData.ChangedVoxels.insert(mcpData.ChangedVoxels.end(), iter->second.begin(), iter->second.end());
|
||||||
|
if(auto iter = data.Profiles_Lost.find(EnumDefContent::Voxel); iter != data.Profiles_Lost.end())
|
||||||
|
mcpData.ChangedVoxels.insert(mcpData.ChangedVoxels.end(), iter->second.begin(), iter->second.end());
|
||||||
|
|
||||||
|
std::vector<std::tuple<AssetsModel, Resource>> modelResources;
|
||||||
|
std::vector<AssetsModel> modelLost;
|
||||||
|
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Model); iter != data.Assets_ChangeOrAdd.end()) {
|
||||||
|
const auto& list = ServerSession->Assets[EnumAssets::Model];
|
||||||
|
for(ResourceId id : iter->second) {
|
||||||
|
auto entryIter = list.find(id);
|
||||||
|
if(entryIter == list.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
modelResources.emplace_back(id, entryIter->second.Res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(auto iter = data.Assets_Lost.find(EnumAssets::Model); iter != data.Assets_Lost.end())
|
||||||
|
modelLost.insert(modelLost.end(), iter->second.begin(), iter->second.end());
|
||||||
|
|
||||||
|
std::vector<AssetsModel> changedModels;
|
||||||
|
if(!modelResources.empty() || !modelLost.empty())
|
||||||
|
changedModels = MP.onModelChanges(std::move(modelResources), std::move(modelLost));
|
||||||
|
|
||||||
|
if(TP) {
|
||||||
|
std::vector<std::tuple<AssetsTexture, Resource>> textureResources;
|
||||||
|
std::vector<AssetsTexture> textureLost;
|
||||||
|
|
||||||
|
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Texture); iter != data.Assets_ChangeOrAdd.end()) {
|
||||||
|
const auto& list = ServerSession->Assets[EnumAssets::Texture];
|
||||||
|
for(ResourceId id : iter->second) {
|
||||||
|
auto entryIter = list.find(id);
|
||||||
|
if(entryIter == list.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
textureResources.emplace_back(id, entryIter->second.Res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(auto iter = data.Assets_Lost.find(EnumAssets::Texture); iter != data.Assets_Lost.end())
|
||||||
|
textureLost.insert(textureLost.end(), iter->second.begin(), iter->second.end());
|
||||||
|
|
||||||
|
if(!textureResources.empty() || !textureLost.empty())
|
||||||
|
TP->onTexturesChanges(std::move(textureResources), std::move(textureLost));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<AssetsNodestate> changedNodestates;
|
||||||
|
if(NSP) {
|
||||||
|
std::vector<std::tuple<AssetsNodestate, Resource>> nodestateResources;
|
||||||
|
std::vector<AssetsNodestate> nodestateLost;
|
||||||
|
|
||||||
|
if(auto iter = data.Assets_ChangeOrAdd.find(EnumAssets::Nodestate); iter != data.Assets_ChangeOrAdd.end()) {
|
||||||
|
const auto& list = ServerSession->Assets[EnumAssets::Nodestate];
|
||||||
|
for(ResourceId id : iter->second) {
|
||||||
|
auto entryIter = list.find(id);
|
||||||
|
if(entryIter == list.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
nodestateResources.emplace_back(id, entryIter->second.Res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(auto iter = data.Assets_Lost.find(EnumAssets::Nodestate); iter != data.Assets_Lost.end())
|
||||||
|
nodestateLost.insert(nodestateLost.end(), iter->second.begin(), iter->second.end());
|
||||||
|
|
||||||
|
if(!nodestateResources.empty() || !nodestateLost.empty() || !changedModels.empty())
|
||||||
|
changedNodestates = NSP->onNodestateChanges(std::move(nodestateResources), std::move(nodestateLost), changedModels);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!changedNodestates.empty()) {
|
||||||
|
std::unordered_set<AssetsNodestate> changed;
|
||||||
|
changed.reserve(changedNodestates.size());
|
||||||
|
for(AssetsNodestate id : changedNodestates)
|
||||||
|
changed.insert(id);
|
||||||
|
|
||||||
|
for(const auto& [nodeId, def] : ServerSession->Profiles.DefNode) {
|
||||||
|
if(changed.contains(def.NodestateId))
|
||||||
|
mcpData.ChangedNodes.push_back(nodeId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CP.tickSync(mcpData);
|
CP.tickSync(mcpData);
|
||||||
|
|
||||||
{
|
|
||||||
std::vector<std::tuple<ResourceId, Resource>> resources;
|
|
||||||
std::vector<ResourceId> lost;
|
|
||||||
|
|
||||||
for(const auto& [type, ids] : data.Assets_ChangeOrAdd) {
|
|
||||||
if(type != EnumAssets::Model)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
const auto& list = ServerSession->Assets[type];
|
|
||||||
for(ResourceId id : ids) {
|
|
||||||
auto iter = list.find(id);
|
|
||||||
if(iter == list.end())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
resources.emplace_back(id, iter->second.Res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(const auto& [type, ids] : data.Assets_Lost) {
|
|
||||||
if(type != EnumAssets::Model)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
lost.append_range(ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!resources.empty() || !lost.empty())
|
|
||||||
MP.onModelChanges(std::move(resources), std::move(lost));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanRenderSession::setCameraPos(WorldId_t worldId, Pos::Object pos, glm::quat quat) {
|
void VulkanRenderSession::setCameraPos(WorldId_t worldId, Pos::Object pos, glm::quat quat) {
|
||||||
@@ -1502,8 +1663,14 @@ void VulkanRenderSession::setCameraPos(WorldId_t worldId, Pos::Object pos, glm::
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VulkanRenderSession::beforeDraw() {
|
void VulkanRenderSession::beforeDraw() {
|
||||||
MainTest.atlasUpdateDynamicData();
|
if(TP)
|
||||||
|
TP->update();
|
||||||
LightDummy.atlasUpdateDynamicData();
|
LightDummy.atlasUpdateDynamicData();
|
||||||
|
CP.flushUploadsAndBarriers(VkInst->Graphics.CommandBufferRender);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VulkanRenderSession::onGpuFinished() {
|
||||||
|
CP.notifyGpuFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanRenderSession::drawWorld(GlobalTime gTime, float dTime, VkCommandBuffer drawCmd) {
|
void VulkanRenderSession::drawWorld(GlobalTime gTime, float dTime, VkCommandBuffer drawCmd) {
|
||||||
@@ -1643,7 +1810,7 @@ void VulkanRenderSession::drawWorld(GlobalTime gTime, float dTime, VkCommandBuff
|
|||||||
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, 0, sizeof(WorldPCO), &PCO);
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT, 0, sizeof(WorldPCO), &PCO);
|
||||||
vkCmdBindDescriptorSets(drawCmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
vkCmdBindDescriptorSets(drawCmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||||
MainAtlas_LightMap_PipelineLayout, 0, 2,
|
MainAtlas_LightMap_PipelineLayout, 0, 2,
|
||||||
(const VkDescriptorSet[]) {MainAtlasDescriptor, VoxelLightMapDescriptor}, 0, nullptr);
|
(const VkDescriptorSet[]) {TP ? TP->getDescriptorSet() : VK_NULL_HANDLE, VoxelLightMapDescriptor}, 0, nullptr);
|
||||||
|
|
||||||
{
|
{
|
||||||
// glm::vec4 offset = glm::inverse(Quat)*glm::vec4(0, 0, -64, 1);
|
// glm::vec4 offset = glm::inverse(Quat)*glm::vec4(0, 0, -64, 1);
|
||||||
@@ -1662,13 +1829,13 @@ void VulkanRenderSession::drawWorld(GlobalTime gTime, float dTime, VkCommandBuff
|
|||||||
|
|
||||||
auto [voxelVertexs, nodeVertexs] = CP.getChunksForRender(WorldId, Pos, 1, PCO.ProjView, x64offset_region);
|
auto [voxelVertexs, nodeVertexs] = CP.getChunksForRender(WorldId, Pos, 1, PCO.ProjView, x64offset_region);
|
||||||
|
|
||||||
{
|
// {
|
||||||
static uint32_t l = TOS::Time::getSeconds();
|
// static uint32_t l = TOS::Time::getSeconds();
|
||||||
if(l != TOS::Time::getSeconds()) {
|
// if(l != TOS::Time::getSeconds()) {
|
||||||
l = TOS::Time::getSeconds();
|
// l = TOS::Time::getSeconds();
|
||||||
TOS::Logger("Test").debug() << nodeVertexs.size();
|
// TOS::Logger("Test").debug() << nodeVertexs.size();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
|
|
||||||
@@ -1789,36 +1956,6 @@ std::vector<VoxelVertexPoint> VulkanRenderSession::generateMeshForVoxelChunks(co
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanRenderSession::updateDescriptor_MainAtlas() {
|
|
||||||
VkDescriptorBufferInfo bufferInfo = MainTest;
|
|
||||||
VkDescriptorImageInfo imageInfo = MainTest;
|
|
||||||
|
|
||||||
std::vector<VkWriteDescriptorSet> ciDescriptorSet =
|
|
||||||
{
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.dstSet = MainAtlasDescriptor,
|
|
||||||
.dstBinding = 0,
|
|
||||||
.dstArrayElement = 0,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
||||||
.pImageInfo = &imageInfo
|
|
||||||
}, {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.dstSet = MainAtlasDescriptor,
|
|
||||||
.dstBinding = 1,
|
|
||||||
.dstArrayElement = 0,
|
|
||||||
.descriptorCount = 1,
|
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
|
||||||
.pBufferInfo = &bufferInfo
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
vkUpdateDescriptorSets(VkInst->Graphics.Device, ciDescriptorSet.size(), ciDescriptorSet.data(), 0, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanRenderSession::updateDescriptor_VoxelsLight() {
|
void VulkanRenderSession::updateDescriptor_VoxelsLight() {
|
||||||
VkDescriptorBufferInfo bufferInfo = LightDummy;
|
VkDescriptorBufferInfo bufferInfo = LightDummy;
|
||||||
VkDescriptorImageInfo imageInfo = LightDummy;
|
VkDescriptorImageInfo imageInfo = LightDummy;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -18,6 +19,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
|
#include "Client/Vulkan/AtlasPipeline/PipelinedTextureAtlas.hpp"
|
||||||
#include "Abstract.hpp"
|
#include "Abstract.hpp"
|
||||||
#include "TOSLib.hpp"
|
#include "TOSLib.hpp"
|
||||||
#include "VertexPool.hpp"
|
#include "VertexPool.hpp"
|
||||||
@@ -243,7 +245,7 @@ public:
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Models.insert({key, std::move(model)});
|
Models.insert_or_assign(key, std::move(model));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(result.begin(), result.end());
|
std::sort(result.begin(), result.end());
|
||||||
@@ -388,63 +390,11 @@ private:
|
|||||||
Хранить все текстуры в оперативке
|
Хранить все текстуры в оперативке
|
||||||
*/
|
*/
|
||||||
class TextureProvider {
|
class TextureProvider {
|
||||||
// Хедер для атласа перед описанием текстур
|
|
||||||
struct alignas(16) UniformInfo {
|
|
||||||
uint32_t
|
|
||||||
// Количество текстур
|
|
||||||
SubsCount,
|
|
||||||
// Счётчик времени с разрешением 8 бит в секунду
|
|
||||||
Counter,
|
|
||||||
// Размер атласа
|
|
||||||
Size;
|
|
||||||
|
|
||||||
// Дальше в шейдере массив на описания текстур
|
|
||||||
// std::vector<InfoSubTexture> SubsInfo;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Описание текстуры на стороне шейдера
|
|
||||||
struct alignas(16) InfoSubTexture {
|
|
||||||
uint32_t isExist = 0;
|
|
||||||
uint32_t
|
|
||||||
// Точная позиция в атласе
|
|
||||||
PosX = 0, PosY = 0, PosZ = 0,
|
|
||||||
// Размер текстуры в атласе
|
|
||||||
Width = 0, Height = 0;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
uint16_t Enabled : 1 = 0, Frames : 15 = 0;
|
|
||||||
uint16_t TimePerFrame = 0;
|
|
||||||
} Animation;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TextureProvider(Vulkan* inst, VkDescriptorPool descPool)
|
TextureProvider(Vulkan* inst, VkDescriptorPool descPool)
|
||||||
: Inst(inst), DescPool(descPool)
|
: Inst(inst), DescPool(descPool)
|
||||||
{
|
{
|
||||||
{
|
assert(inst);
|
||||||
const VkSamplerCreateInfo ciSampler =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.magFilter = VK_FILTER_NEAREST,
|
|
||||||
.minFilter = VK_FILTER_NEAREST,
|
|
||||||
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
|
|
||||||
.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
|
||||||
.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
|
||||||
.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
|
||||||
.mipLodBias = 0.0f,
|
|
||||||
.anisotropyEnable = VK_FALSE,
|
|
||||||
.maxAnisotropy = 1,
|
|
||||||
.compareEnable = 0,
|
|
||||||
.compareOp = VK_COMPARE_OP_NEVER,
|
|
||||||
.minLod = 0.0f,
|
|
||||||
.maxLod = 0.0f,
|
|
||||||
.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
|
|
||||||
.unnormalizedCoordinates = VK_FALSE
|
|
||||||
};
|
|
||||||
vkAssert(!vkCreateSampler(inst->Graphics.Device, &ciSampler, nullptr, &Sampler));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::vector<VkDescriptorSetLayoutBinding> shaderLayoutBindings =
|
std::vector<VkDescriptorSetLayoutBinding> shaderLayoutBindings =
|
||||||
@@ -478,388 +428,248 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
Atlases.resize(BackupAtlasCount);
|
|
||||||
|
|
||||||
VkDescriptorSetAllocateInfo ciAllocInfo =
|
VkDescriptorSetAllocateInfo ciAllocInfo =
|
||||||
{
|
{
|
||||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.descriptorPool = descPool,
|
.descriptorPool = DescPool,
|
||||||
.descriptorSetCount = (uint32_t) Atlases.size(),
|
.descriptorSetCount = 1,
|
||||||
.pSetLayouts = &DescLayout
|
.pSetLayouts = &DescLayout
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<VkDescriptorSet> descriptors;
|
vkAssert(!vkAllocateDescriptorSets(Inst->Graphics.Device, &ciAllocInfo, &Descriptor));
|
||||||
descriptors.resize(Atlases.size());
|
|
||||||
vkAssert(!vkAllocateDescriptorSets(inst->Graphics.Device, &ciAllocInfo, descriptors.data()));
|
|
||||||
|
|
||||||
for(auto& atlas : Atlases) {
|
|
||||||
atlas.recreate(Inst, true);
|
|
||||||
atlas.Descriptor = descriptors.back();
|
|
||||||
descriptors.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
VkSemaphoreCreateInfo semaphoreCreateInfo = {
|
TextureAtlas::Config cfg;
|
||||||
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
|
cfg.MaxTextureId = 1 << 18;
|
||||||
|
AtlasStaging = std::make_shared<SharedStagingBuffer>(
|
||||||
|
Inst->Graphics.Device,
|
||||||
|
Inst->Graphics.PhysicalDevice
|
||||||
|
);
|
||||||
|
Atlas = std::make_unique<PipelinedTextureAtlas>(
|
||||||
|
TextureAtlas(Inst->Graphics.Device, Inst->Graphics.PhysicalDevice, cfg, {}, AtlasStaging)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const VkFenceCreateInfo info = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.flags = 0
|
.flags = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
vkAssert(!vkCreateSemaphore(Inst->Graphics.Device, &semaphoreCreateInfo, nullptr, &SendChanges));
|
vkAssert(!vkCreateFence(Inst->Graphics.Device, &info, nullptr, &UpdateFence));
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
NeedsUpload = true;
|
||||||
const VkCommandBufferAllocateInfo infoCmd =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.commandPool = Inst->Graphics.Pool,
|
|
||||||
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
||||||
.commandBufferCount = 1
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkAllocateCommandBuffers(Inst->Graphics.Device, &infoCmd, &CMD));
|
|
||||||
}
|
|
||||||
|
|
||||||
Cache.recreate(Inst, false);
|
|
||||||
|
|
||||||
AtlasTextureUnusedId.all();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~TextureProvider() {
|
~TextureProvider() {
|
||||||
|
if(UpdateFence)
|
||||||
|
vkDestroyFence(Inst->Graphics.Device, UpdateFence, nullptr);
|
||||||
|
|
||||||
if(DescLayout)
|
if(DescLayout)
|
||||||
vkDestroyDescriptorSetLayout(Inst->Graphics.Device, DescLayout, nullptr);
|
vkDestroyDescriptorSetLayout(Inst->Graphics.Device, DescLayout, nullptr);
|
||||||
|
|
||||||
if(Sampler) {
|
|
||||||
vkDestroySampler(Inst->Graphics.Device, Sampler, nullptr);
|
|
||||||
Sampler = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& atlas : Atlases) {
|
VkDescriptorSetLayout getDescriptorLayout() const {
|
||||||
atlas.destroy(Inst);
|
return DescLayout;
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlases.clear();
|
VkDescriptorSet getDescriptorSet() const {
|
||||||
|
return Descriptor;
|
||||||
Cache.destroy(Inst);
|
|
||||||
Cache.unMap(Inst);
|
|
||||||
|
|
||||||
if(SendChanges) {
|
|
||||||
vkDestroySemaphore(Inst->Graphics.Device, SendChanges, nullptr);
|
|
||||||
SendChanges = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(CMD) {
|
|
||||||
vkFreeCommandBuffers(Inst->Graphics.Device, Inst->Graphics.Pool, 1, &CMD);
|
|
||||||
CMD = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t getTextureId(const TexturePipeline& pipe) {
|
uint16_t getTextureId(const TexturePipeline& pipe) {
|
||||||
return 0;
|
std::lock_guard lock(Mutex);
|
||||||
}
|
auto iter = PipelineToAtlas.find(pipe);
|
||||||
|
if(iter != PipelineToAtlas.end())
|
||||||
|
return iter->second;
|
||||||
|
|
||||||
// Устанавливает новый размер единицы в массиве текстур атласа
|
::HashedPipeline hashed = makeHashedPipeline(pipe);
|
||||||
enum class EnumAtlasSize {
|
uint32_t atlasId = Atlas->getByPipeline(hashed);
|
||||||
_2048 = 2048, _4096 = 4096, _8192 = 8192, _16_384 = 16'384
|
|
||||||
};
|
|
||||||
void setAtlasSize(EnumAtlasSize size) {
|
|
||||||
ReferenceSize = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Максимальный размер выделенный под атласы в памяти устройства
|
uint16_t result = 0;
|
||||||
void setDeviceMemorySize(size_t size) {
|
if(atlasId <= std::numeric_limits<uint16_t>::max())
|
||||||
std::unreachable();
|
result = static_cast<uint16_t>(atlasId);
|
||||||
|
else
|
||||||
|
LOG.warn() << "Atlas texture id overflow: " << atlasId;
|
||||||
|
|
||||||
|
PipelineToAtlas.emplace(pipe, result);
|
||||||
|
NeedsUpload = true;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Применяет изменения, возвращая все затронутые модели
|
// Применяет изменения, возвращая все затронутые модели
|
||||||
std::vector<AssetsTexture> onTexturesChanges(std::vector<std::tuple<AssetsTexture, Resource>> newOrChanged, std::vector<AssetsTexture> lost) {
|
std::vector<AssetsTexture> onTexturesChanges(std::vector<std::tuple<AssetsTexture, Resource>> newOrChanged, std::vector<AssetsTexture> lost) {
|
||||||
|
std::lock_guard lock(Mutex);
|
||||||
std::vector<AssetsTexture> result;
|
std::vector<AssetsTexture> result;
|
||||||
|
|
||||||
for(const auto& [key, res] : newOrChanged) {
|
for(const auto& [key, res] : newOrChanged) {
|
||||||
result.push_back(key);
|
result.push_back(key);
|
||||||
ChangedOrAdded.push_back(key);
|
|
||||||
|
|
||||||
TextureEntry entry;
|
|
||||||
iResource sres((const uint8_t*) res.data(), res.size());
|
iResource sres((const uint8_t*) res.data(), res.size());
|
||||||
iBinaryStream stream = sres.makeStream();
|
iBinaryStream stream = sres.makeStream();
|
||||||
png::image<png::rgba_pixel> img(stream.Stream);
|
png::image<png::rgba_pixel> img(stream.Stream);
|
||||||
entry.Width = img.get_width();
|
uint32_t width = img.get_width();
|
||||||
entry.Height = img.get_height();
|
uint32_t height = img.get_height();
|
||||||
entry.RGBA.resize(4*entry.Width*entry.Height);
|
|
||||||
|
|
||||||
for(int i = 0; i < entry.Height; i++) {
|
std::vector<uint32_t> pixels;
|
||||||
std::copy(
|
pixels.resize(width*height);
|
||||||
((const uint32_t*) &img.get_pixbuf().operator [](i)[0]),
|
|
||||||
((const uint32_t*) &img.get_pixbuf().operator [](i)[0])+entry.Width,
|
for(uint32_t y = 0; y < height; y++) {
|
||||||
((uint32_t*) entry.RGBA.data())+entry.Width*(false ? entry.Height-i-1 : i)
|
const auto& row = img.get_pixbuf().operator [](y);
|
||||||
);
|
for(uint32_t x = 0; x < width; x++) {
|
||||||
|
const auto& px = row[x];
|
||||||
|
uint32_t rgba = (uint32_t(px.alpha) << 24)
|
||||||
|
| (uint32_t(px.red) << 16)
|
||||||
|
| (uint32_t(px.green) << 8)
|
||||||
|
| uint32_t(px.blue);
|
||||||
|
pixels[x + y * width] = rgba;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Textures[key] = std::move(entry);
|
Atlas->updateTexture(key, StoredTexture(
|
||||||
|
static_cast<uint16_t>(width),
|
||||||
|
static_cast<uint16_t>(height),
|
||||||
|
std::move(pixels)
|
||||||
|
));
|
||||||
|
|
||||||
|
NeedsUpload = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(AssetsTexture key : lost) {
|
for(AssetsTexture key : lost) {
|
||||||
result.push_back(key);
|
result.push_back(key);
|
||||||
Lost.push_back(key);
|
Atlas->freeTexture(key);
|
||||||
|
NeedsUpload = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
std::sort(result.begin(), result.end());
|
std::sort(result.begin(), result.end());
|
||||||
auto eraseIter = std::unique(result.begin(), result.end());
|
auto eraseIter = std::unique(result.begin(), result.end());
|
||||||
result.erase(eraseIter, result.end());
|
result.erase(eraseIter, result.end());
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
std::sort(ChangedOrAdded.begin(), ChangedOrAdded.end());
|
|
||||||
auto eraseIter = std::unique(ChangedOrAdded.begin(), ChangedOrAdded.end());
|
|
||||||
ChangedOrAdded.erase(eraseIter, ChangedOrAdded.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
std::sort(Lost.begin(), Lost.end());
|
|
||||||
auto eraseIter = std::unique(Lost.begin(), Lost.end());
|
|
||||||
Lost.erase(eraseIter, Lost.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
// Подготовить обновления атласа
|
std::lock_guard lock(Mutex);
|
||||||
// Если предыдущий освободился, то записать изменения в него
|
if(!NeedsUpload || !Atlas)
|
||||||
|
return;
|
||||||
|
|
||||||
// Держать на стороне хоста полную версию атласа и все изменения писать туда
|
Atlas->flushNewPipelines();
|
||||||
// Когерентная память сама разберётся что отсылать на устройство
|
|
||||||
// Синхронизировать всё из внутреннего буфера в атлас
|
|
||||||
// При пересоздании хостового буфера, скопировать всё из старого.
|
|
||||||
|
|
||||||
// Оптимизации копирования при указании конкретных изменённых слоёв?
|
VkCommandBufferAllocateInfo allocInfo {
|
||||||
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
||||||
|
nullptr,
|
||||||
|
Inst->Graphics.Pool,
|
||||||
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
||||||
|
1
|
||||||
|
};
|
||||||
|
|
||||||
|
VkCommandBuffer commandBuffer;
|
||||||
|
vkAssert(!vkAllocateCommandBuffers(Inst->Graphics.Device, &allocInfo, &commandBuffer));
|
||||||
|
|
||||||
|
VkCommandBufferBeginInfo beginInfo {
|
||||||
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
||||||
|
nullptr,
|
||||||
|
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
||||||
|
nullptr
|
||||||
|
};
|
||||||
|
|
||||||
|
vkAssert(!vkBeginCommandBuffer(commandBuffer, &beginInfo));
|
||||||
|
|
||||||
|
TextureAtlas::DescriptorOut desc = Atlas->flushUploadsAndBarriers(commandBuffer);
|
||||||
|
|
||||||
|
vkAssert(!vkEndCommandBuffer(commandBuffer));
|
||||||
|
|
||||||
|
VkSubmitInfo submitInfo {
|
||||||
|
VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
||||||
|
nullptr,
|
||||||
|
0, nullptr,
|
||||||
|
nullptr,
|
||||||
|
1,
|
||||||
|
&commandBuffer,
|
||||||
|
0,
|
||||||
|
nullptr
|
||||||
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
auto lockQueue = Inst->Graphics.DeviceQueueGraphic.lock();
|
||||||
|
vkAssert(!vkQueueSubmit(*lockQueue, 1, &submitInfo, UpdateFence));
|
||||||
}
|
}
|
||||||
|
|
||||||
VkDescriptorSet getDescriptor() {
|
vkAssert(!vkWaitForFences(Inst->Graphics.Device, 1, &UpdateFence, VK_TRUE, UINT64_MAX));
|
||||||
return Atlases[ActiveAtlas].Descriptor;
|
vkAssert(!vkResetFences(Inst->Graphics.Device, 1, &UpdateFence));
|
||||||
|
|
||||||
|
vkFreeCommandBuffers(Inst->Graphics.Device, Inst->Graphics.Pool, 1, &commandBuffer);
|
||||||
|
|
||||||
|
Atlas->notifyGpuFinished();
|
||||||
|
updateDescriptor(desc);
|
||||||
|
|
||||||
|
NeedsUpload = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pushFrame() {
|
private:
|
||||||
for(auto& atlas : Atlases)
|
::HashedPipeline makeHashedPipeline(const TexturePipeline& pipe) const {
|
||||||
if(atlas.NotUsedFrames < 100)
|
::Pipeline pipeline;
|
||||||
atlas.NotUsedFrames++;
|
|
||||||
|
|
||||||
Atlases[ActiveAtlas].NotUsedFrames = 0;
|
if(!pipe.Pipeline.empty() && (pipe.Pipeline.size() % 2u) == 0u) {
|
||||||
|
std::vector<TexturePipelineProgram::Word> words;
|
||||||
|
words.reserve(pipe.Pipeline.size() / 2u);
|
||||||
|
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(pipe.Pipeline.data());
|
||||||
|
for(size_t i = 0; i < pipe.Pipeline.size(); i += 2) {
|
||||||
|
uint16_t lo = bytes[i];
|
||||||
|
uint16_t hi = bytes[i + 1];
|
||||||
|
words.push_back(static_cast<TexturePipelineProgram::Word>(lo | (hi << 8)));
|
||||||
|
}
|
||||||
|
pipeline._Pipeline.assign(words.begin(), words.end());
|
||||||
|
}
|
||||||
|
|
||||||
// Если есть новые текстуры или они поменялись
|
if(pipeline._Pipeline.empty()) {
|
||||||
//
|
if(!pipe.BinTextures.empty())
|
||||||
|
pipeline = ::Pipeline(pipe.BinTextures.front());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ::HashedPipeline(pipeline);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateDescriptor(const TextureAtlas::DescriptorOut& desc) {
|
||||||
|
VkWriteDescriptorSet writes[2] = {};
|
||||||
|
|
||||||
|
writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||||
|
writes[0].dstSet = Descriptor;
|
||||||
|
writes[0].dstBinding = 0;
|
||||||
|
writes[0].descriptorCount = 1;
|
||||||
|
writes[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||||
|
writes[0].pImageInfo = &desc.ImageInfo;
|
||||||
|
|
||||||
|
writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||||
|
writes[1].dstSet = Descriptor;
|
||||||
|
writes[1].dstBinding = 1;
|
||||||
|
writes[1].descriptorCount = 1;
|
||||||
|
writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
||||||
|
writes[1].pBufferInfo = &desc.EntriesInfo;
|
||||||
|
|
||||||
|
vkUpdateDescriptorSets(Inst->Graphics.Device, 2, writes, 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vulkan* Inst = nullptr;
|
Vulkan* Inst = nullptr;
|
||||||
VkDescriptorPool DescPool = VK_NULL_HANDLE;
|
VkDescriptorPool DescPool = VK_NULL_HANDLE;
|
||||||
VkDescriptorSetLayout DescLayout = VK_NULL_HANDLE;
|
VkDescriptorSetLayout DescLayout = VK_NULL_HANDLE;
|
||||||
// Для всех атласов
|
VkDescriptorSet Descriptor = VK_NULL_HANDLE;
|
||||||
VkSampler Sampler = VK_NULL_HANDLE;
|
VkFence UpdateFence = VK_NULL_HANDLE;
|
||||||
// Ожидание завершения работы с хостовым буфером
|
|
||||||
VkSemaphore SendChanges = VK_NULL_HANDLE;
|
|
||||||
//
|
|
||||||
VkCommandBuffer CMD = VK_NULL_HANDLE;
|
|
||||||
|
|
||||||
// Размер, которому должны соответствовать все атласы
|
std::shared_ptr<SharedStagingBuffer> AtlasStaging;
|
||||||
EnumAtlasSize ReferenceSize = EnumAtlasSize::_2048;
|
std::unique_ptr<PipelinedTextureAtlas> Atlas;
|
||||||
|
std::unordered_map<TexturePipeline, uint16_t> PipelineToAtlas;
|
||||||
|
|
||||||
struct TextureEntry {
|
bool NeedsUpload = false;
|
||||||
uint16_t Width, Height;
|
Logger LOG = "Client>TextureProvider";
|
||||||
std::vector<glm::i8vec4> RGBA;
|
mutable std::mutex Mutex;
|
||||||
|
|
||||||
// Идентификатор текстуры в атласе
|
|
||||||
uint16_t InAtlasId = uint16_t(-1);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Текстуры, загруженные с файлов
|
|
||||||
std::unordered_map<AssetsTexture, TextureEntry> Textures;
|
|
||||||
|
|
||||||
struct TextureFromPipeline {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unordered_map<TexturePipeline, TextureFromPipeline> Pipelines;
|
|
||||||
|
|
||||||
struct AtlasTextureEntry {
|
|
||||||
uint16_t PosX, PosY, PosZ, Width, Height;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
std::bitset<1 << 16> AtlasTextureUnusedId;
|
|
||||||
std::unordered_map<uint16_t, AtlasTextureEntry> AtlasTextureInfo;
|
|
||||||
|
|
||||||
std::vector<AssetsTexture> ChangedOrAdded, Lost;
|
|
||||||
|
|
||||||
struct VkAtlasInfo {
|
|
||||||
VkImage Image = VK_NULL_HANDLE;
|
|
||||||
VkImageLayout ImageLayout = VK_IMAGE_LAYOUT_MAX_ENUM;
|
|
||||||
|
|
||||||
VkDeviceMemory Memory = VK_NULL_HANDLE;
|
|
||||||
VkImageView View = VK_NULL_HANDLE;
|
|
||||||
|
|
||||||
VkDescriptorSet Descriptor;
|
|
||||||
EnumAtlasSize Size = EnumAtlasSize::_2048;
|
|
||||||
uint16_t Depth = 1;
|
|
||||||
|
|
||||||
// Сколько кадров уже не используется атлас
|
|
||||||
int NotUsedFrames = 0;
|
|
||||||
|
|
||||||
void destroy(Vulkan* inst) {
|
|
||||||
if(View) {
|
|
||||||
vkDestroyImageView(inst->Graphics.Device, View, nullptr);
|
|
||||||
View = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Image) {
|
|
||||||
vkDestroyImage(inst->Graphics.Device, Image, nullptr);
|
|
||||||
Image = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Memory) {
|
|
||||||
vkFreeMemory(inst->Graphics.Device, Memory, nullptr);
|
|
||||||
Memory = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void recreate(Vulkan* inst, bool deviceLocal) {
|
|
||||||
// Уничтожаем то, что не понадобится
|
|
||||||
if(View) {
|
|
||||||
vkDestroyImageView(inst->Graphics.Device, View, nullptr);
|
|
||||||
View = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Image) {
|
|
||||||
vkDestroyImage(inst->Graphics.Device, Image, nullptr);
|
|
||||||
Image = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Memory) {
|
|
||||||
vkFreeMemory(inst->Graphics.Device, Memory, nullptr);
|
|
||||||
Memory = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создаём атлас
|
|
||||||
uint32_t size = uint32_t(Size);
|
|
||||||
|
|
||||||
VkImageCreateInfo infoImageCreate =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.imageType = VK_IMAGE_TYPE_2D,
|
|
||||||
.format = VK_FORMAT_B8G8R8A8_UNORM,
|
|
||||||
.extent = { size, size, 1 },
|
|
||||||
.mipLevels = 1,
|
|
||||||
.arrayLayers = Depth,
|
|
||||||
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
||||||
.tiling = VK_IMAGE_TILING_MAX_ENUM,
|
|
||||||
.usage =
|
|
||||||
static_cast<VkImageUsageFlags>(deviceLocal
|
|
||||||
? VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT
|
|
||||||
: VK_IMAGE_USAGE_TRANSFER_SRC_BIT),
|
|
||||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
|
||||||
.queueFamilyIndexCount = 0,
|
|
||||||
.pQueueFamilyIndices = 0,
|
|
||||||
.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED
|
|
||||||
};
|
|
||||||
|
|
||||||
VkFormatProperties props;
|
|
||||||
vkGetPhysicalDeviceFormatProperties(inst->Graphics.PhysicalDevice, infoImageCreate.format, &props);
|
|
||||||
|
|
||||||
if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
|
|
||||||
infoImageCreate.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
||||||
else if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
|
|
||||||
infoImageCreate.tiling = VK_IMAGE_TILING_LINEAR;
|
|
||||||
else
|
|
||||||
vkAssert(!"No support for B8G8R8A8_UNORM as texture image format");
|
|
||||||
|
|
||||||
vkAssert(!vkCreateImage(inst->Graphics.Device, &infoImageCreate, nullptr, &Image));
|
|
||||||
|
|
||||||
// Выделяем память
|
|
||||||
VkMemoryRequirements memoryReqs;
|
|
||||||
vkGetImageMemoryRequirements(inst->Graphics.Device, Image, &memoryReqs);
|
|
||||||
|
|
||||||
VkMemoryAllocateInfo memoryAlloc
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.allocationSize = memoryReqs.size,
|
|
||||||
.memoryTypeIndex = inst->memoryTypeFromProperties(memoryReqs.memoryTypeBits,
|
|
||||||
deviceLocal
|
|
||||||
? VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
|
||||||
: VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkAllocateMemory(inst->Graphics.Device, &memoryAlloc, nullptr, &Memory));
|
|
||||||
vkAssert(!vkBindImageMemory(inst->Graphics.Device, Image, Memory, 0));
|
|
||||||
|
|
||||||
// Порядок пикселей и привязка к картинке
|
|
||||||
VkImageViewCreateInfo ciView =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = 0,
|
|
||||||
.image = Image,
|
|
||||||
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
|
|
||||||
.format = infoImageCreate.format,
|
|
||||||
.components =
|
|
||||||
{
|
|
||||||
VK_COMPONENT_SWIZZLE_B,
|
|
||||||
VK_COMPONENT_SWIZZLE_G,
|
|
||||||
VK_COMPONENT_SWIZZLE_R,
|
|
||||||
VK_COMPONENT_SWIZZLE_A
|
|
||||||
},
|
|
||||||
.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkCreateImageView(inst->Graphics.Device, &ciView, nullptr, &View));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct HostCache : public VkAtlasInfo {
|
|
||||||
std::vector<uint32_t*> Layers;
|
|
||||||
std::vector<VkSubresourceLayout> Layouts;
|
|
||||||
std::vector<rbp::MaxRectsBinPack> Packs;
|
|
||||||
|
|
||||||
void map(Vulkan* inst) {
|
|
||||||
Layers.resize(Depth);
|
|
||||||
Layouts.resize(Depth);
|
|
||||||
|
|
||||||
for(uint32_t layer = 0; layer < Depth; layer++) {
|
|
||||||
const VkImageSubresource memorySubres = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .mipLevel = 0, .arrayLayer = layer, };
|
|
||||||
vkGetImageSubresourceLayout(inst->Graphics.Device, Image, &memorySubres, &Layouts[layer]);
|
|
||||||
|
|
||||||
vkAssert(!vkMapMemory(inst->Graphics.Device, Memory, Layouts[layer].offset, Layouts[layer].size, 0, (void**) &Layers[layer]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void unMap(Vulkan* inst) {
|
|
||||||
vkUnmapMemory(inst->Graphics.Device, Memory);
|
|
||||||
|
|
||||||
Layers.clear();
|
|
||||||
Layouts.clear();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
HostCache Cache;
|
|
||||||
|
|
||||||
static constexpr size_t BackupAtlasCount = 2;
|
|
||||||
|
|
||||||
// Атласы, используемые в кадре.
|
|
||||||
// Изменения пишутся в не используемый в данный момент атлас
|
|
||||||
// и изменённый атлас становится активным. Новые изменения
|
|
||||||
// можно писать по прошествии нескольких кадров.
|
|
||||||
std::vector<VkAtlasInfo> Atlases;
|
|
||||||
int ActiveAtlas = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Хранит информацию о моделях при различных состояниях нод
|
Хранит информацию о моделях при различных состояниях нод
|
||||||
@@ -904,7 +714,23 @@ public:
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Nodestates.insert({key, std::move(nodestate)});
|
Nodestates.insert_or_assign(key, std::move(nodestate));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!changedModels.empty()) {
|
||||||
|
std::unordered_set<AssetsModel> changed;
|
||||||
|
changed.reserve(changedModels.size());
|
||||||
|
for(AssetsModel modelId : changedModels)
|
||||||
|
changed.insert(modelId);
|
||||||
|
|
||||||
|
for(const auto& [nodestateId, nodestate] : Nodestates) {
|
||||||
|
for(AssetsModel modelId : nodestate.LocalToModel) {
|
||||||
|
if(changed.contains(modelId)) {
|
||||||
|
result.push_back(nodestateId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(result.begin(), result.end());
|
std::sort(result.begin(), result.end());
|
||||||
@@ -922,19 +748,15 @@ public:
|
|||||||
if(iterNodestate == Nodestates.end())
|
if(iterNodestate == Nodestates.end())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
std::vector<uint16_t> routes = iterNodestate->second.getModelsForState(statesInfo, states);
|
PreparedNodeState& nodestate = iterNodestate->second;
|
||||||
|
std::vector<uint16_t> routes = nodestate.getModelsForState(statesInfo, states);
|
||||||
std::vector<std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>>> result;
|
std::vector<std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>>> result;
|
||||||
|
|
||||||
std::unordered_map<TexturePipeline, uint16_t> pipelineResolveCache;
|
std::unordered_map<TexturePipeline, uint16_t> pipelineResolveCache;
|
||||||
|
|
||||||
for(uint16_t routeId : routes) {
|
auto appendModel = [&](AssetsModel modelId, const std::vector<Transformation>& transforms, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>& out) {
|
||||||
std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>> routeModels;
|
ModelProvider::Model model = MP.getModel(modelId);
|
||||||
const auto& route = iterNodestate->second.Routes[routeId];
|
Transformations trf{transforms};
|
||||||
for(const auto& [w, m] : route.second) {
|
|
||||||
if(const PreparedNodeState::Model* ptr = std::get_if<PreparedNodeState::Model>(&m)) {
|
|
||||||
ModelProvider::Model model = MP.getModel(ptr->Id);
|
|
||||||
Transformations trf(ptr->Transforms);
|
|
||||||
std::unordered_map<EnumFace, std::vector<NodeVertexStatic>> out;
|
|
||||||
|
|
||||||
for(auto& [l, r] : model.Vertecies) {
|
for(auto& [l, r] : model.Vertecies) {
|
||||||
trf.apply(r);
|
trf.apply(r);
|
||||||
@@ -943,12 +765,12 @@ public:
|
|||||||
for(const Vertex& v : r) {
|
for(const Vertex& v : r) {
|
||||||
NodeVertexStatic vert;
|
NodeVertexStatic vert;
|
||||||
|
|
||||||
vert.FX = (v.Pos.x+0.5)*64+224;
|
vert.FX = (v.Pos.x+0.5f)*64+224;
|
||||||
vert.FY = (v.Pos.y+0.5)*64+224;
|
vert.FY = (v.Pos.y+0.5f)*64+224;
|
||||||
vert.FZ = (v.Pos.z+0.5)*64+224;
|
vert.FZ = (v.Pos.z+0.5f)*64+224;
|
||||||
|
|
||||||
vert.TU = std::clamp<int32_t>(v.UV.x * (1 << 16), 0, (1 << 16));
|
vert.TU = std::clamp<int32_t>(v.UV.x * (1 << 16), 0, (1 << 16) - 1);
|
||||||
vert.TV = std::clamp<int32_t>(v.UV.y * (1 << 16), 0, (1 << 16));
|
vert.TV = std::clamp<int32_t>(v.UV.y * (1 << 16), 0, (1 << 16) - 1);
|
||||||
|
|
||||||
const TexturePipeline& pipe = model.TextureMap[model.TextureKeys[v.TexId]];
|
const TexturePipeline& pipe = model.TextureMap[model.TextureKeys[v.TexId]];
|
||||||
if(auto iterPipe = pipelineResolveCache.find(pipe); iterPipe != pipelineResolveCache.end()) {
|
if(auto iterPipe = pipelineResolveCache.find(pipe); iterPipe != pipelineResolveCache.end()) {
|
||||||
@@ -961,12 +783,43 @@ public:
|
|||||||
out[l].push_back(vert);
|
out[l].push_back(vert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
auto resolveModelId = [&](uint16_t localId, AssetsModel& outId) -> bool {
|
||||||
|
if(localId >= nodestate.LocalToModel.size())
|
||||||
|
return false;
|
||||||
|
outId = nodestate.LocalToModel[localId];
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
for(uint16_t routeId : routes) {
|
||||||
|
if(routeId >= nodestate.Routes.size())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::vector<std::pair<float, std::unordered_map<EnumFace, std::vector<NodeVertexStatic>>>> routeModels;
|
||||||
|
const auto& route = nodestate.Routes[routeId];
|
||||||
|
for(const auto& [w, m] : route.second) {
|
||||||
|
std::unordered_map<EnumFace, std::vector<NodeVertexStatic>> out;
|
||||||
|
|
||||||
|
if(const PreparedNodeState::Model* ptr = std::get_if<PreparedNodeState::Model>(&m)) {
|
||||||
|
AssetsModel modelId;
|
||||||
|
if(resolveModelId(ptr->Id, modelId))
|
||||||
|
appendModel(modelId, ptr->Transforms, out);
|
||||||
|
} else if(const PreparedNodeState::VectorModel* ptr = std::get_if<PreparedNodeState::VectorModel>(&m)) {
|
||||||
|
for(const auto& sub : ptr->Models) {
|
||||||
|
AssetsModel modelId;
|
||||||
|
if(!resolveModelId(sub.Id, modelId))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::vector<Transformation> transforms = sub.Transforms;
|
||||||
|
transforms.insert(transforms.end(), ptr->Transforms.begin(), ptr->Transforms.end());
|
||||||
|
appendModel(modelId, transforms, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// TODO: uvlock
|
/// TODO: uvlock
|
||||||
|
|
||||||
routeModels.emplace_back(w, std::move(out));
|
routeModels.emplace_back(w, std::move(out));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
result.push_back(std::move(routeModels));
|
result.push_back(std::move(routeModels));
|
||||||
}
|
}
|
||||||
@@ -974,6 +827,15 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t getTextureId(AssetsTexture texId) {
|
||||||
|
if(texId == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
TexturePipeline pipe;
|
||||||
|
pipe.BinTextures.push_back(texId);
|
||||||
|
return TP.getTextureId(pipe);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Logger LOG = "Client>NodestateProvider";
|
Logger LOG = "Client>NodestateProvider";
|
||||||
ModelProvider& MP;
|
ModelProvider& MP;
|
||||||
@@ -1027,6 +889,10 @@ public:
|
|||||||
// Меняет количество обрабатывающих потоков
|
// Меняет количество обрабатывающих потоков
|
||||||
void changeThreadsCount(uint8_t threads);
|
void changeThreadsCount(uint8_t threads);
|
||||||
|
|
||||||
|
void setNodestateProvider(NodestateProvider* provider) {
|
||||||
|
NSP = provider;
|
||||||
|
}
|
||||||
|
|
||||||
void prepareTickSync() {
|
void prepareTickSync() {
|
||||||
Sync.Stop = true;
|
Sync.Stop = true;
|
||||||
}
|
}
|
||||||
@@ -1051,6 +917,7 @@ private:
|
|||||||
} Sync;
|
} Sync;
|
||||||
|
|
||||||
IServerSession *SS;
|
IServerSession *SS;
|
||||||
|
NodestateProvider* NSP = nullptr;
|
||||||
std::vector<std::thread> Threads;
|
std::vector<std::thread> Threads;
|
||||||
|
|
||||||
void run(uint8_t id);
|
void run(uint8_t id);
|
||||||
@@ -1083,23 +950,10 @@ public:
|
|||||||
assert(serverSession);
|
assert(serverSession);
|
||||||
|
|
||||||
CMG.changeThreadsCount(1);
|
CMG.changeThreadsCount(1);
|
||||||
|
|
||||||
const VkCommandPoolCreateInfo infoCmdPool =
|
|
||||||
{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
|
|
||||||
.pNext = nullptr,
|
|
||||||
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
|
|
||||||
.queueFamilyIndex = VkInst->getSettings().QueueGraphics
|
|
||||||
};
|
|
||||||
|
|
||||||
vkAssert(!vkCreateCommandPool(VkInst->Graphics.Device, &infoCmdPool, nullptr, &CMDPool));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~ChunkPreparator() {
|
~ChunkPreparator() {
|
||||||
CMG.changeThreadsCount(0);
|
CMG.changeThreadsCount(0);
|
||||||
|
|
||||||
if(CMDPool)
|
|
||||||
vkDestroyCommandPool(VkInst->Graphics.Device, CMDPool, nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1111,7 +965,24 @@ public:
|
|||||||
CMG.pushStageTickSync();
|
CMG.pushStageTickSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setNodestateProvider(NodestateProvider* provider) {
|
||||||
|
CMG.setNodestateProvider(provider);
|
||||||
|
}
|
||||||
|
|
||||||
void tickSync(const TickSyncData& data);
|
void tickSync(const TickSyncData& data);
|
||||||
|
void notifyGpuFinished() {
|
||||||
|
resetVertexStaging();
|
||||||
|
VertexPool_Voxels.notifyGpuFinished();
|
||||||
|
VertexPool_Nodes.notifyGpuFinished();
|
||||||
|
IndexPool_Nodes_16.notifyGpuFinished();
|
||||||
|
IndexPool_Nodes_32.notifyGpuFinished();
|
||||||
|
}
|
||||||
|
void flushUploadsAndBarriers(VkCommandBuffer commandBuffer) {
|
||||||
|
VertexPool_Voxels.flushUploadsAndBarriers(commandBuffer);
|
||||||
|
VertexPool_Nodes.flushUploadsAndBarriers(commandBuffer);
|
||||||
|
IndexPool_Nodes_16.flushUploadsAndBarriers(commandBuffer);
|
||||||
|
IndexPool_Nodes_32.flushUploadsAndBarriers(commandBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Готовность кадров определяет когда можно удалять ненужные ресурсы, которые ещё используются в рендере
|
// Готовность кадров определяет когда можно удалять ненужные ресурсы, которые ещё используются в рендере
|
||||||
void pushFrame();
|
void pushFrame();
|
||||||
@@ -1126,7 +997,6 @@ private:
|
|||||||
static constexpr uint8_t FRAME_COUNT_RESOURCE_LATENCY = 6;
|
static constexpr uint8_t FRAME_COUNT_RESOURCE_LATENCY = 6;
|
||||||
|
|
||||||
Vulkan* VkInst;
|
Vulkan* VkInst;
|
||||||
VkCommandPool CMDPool = nullptr;
|
|
||||||
|
|
||||||
// Генератор вершин чанков
|
// Генератор вершин чанков
|
||||||
ChunkMeshGenerator CMG;
|
ChunkMeshGenerator CMG;
|
||||||
@@ -1193,8 +1063,10 @@ class VulkanRenderSession : public IRenderSession {
|
|||||||
|
|
||||||
ChunkPreparator CP;
|
ChunkPreparator CP;
|
||||||
ModelProvider MP;
|
ModelProvider MP;
|
||||||
|
std::unique_ptr<TextureProvider> TP;
|
||||||
|
std::unique_ptr<NodestateProvider> NSP;
|
||||||
|
|
||||||
AtlasImage MainTest, LightDummy;
|
AtlasImage LightDummy;
|
||||||
Buffer TestQuad;
|
Buffer TestQuad;
|
||||||
std::optional<Buffer> TestVoxel;
|
std::optional<Buffer> TestVoxel;
|
||||||
|
|
||||||
@@ -1206,8 +1078,6 @@ class VulkanRenderSession : public IRenderSession {
|
|||||||
.binding = 1,
|
.binding = 1,
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, Данные к атласу
|
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, Данные к атласу
|
||||||
*/
|
*/
|
||||||
VkDescriptorSetLayout MainAtlasDescLayout = VK_NULL_HANDLE;
|
|
||||||
VkDescriptorSet MainAtlasDescriptor = VK_NULL_HANDLE;
|
|
||||||
/*
|
/*
|
||||||
.binding = 2,
|
.binding = 2,
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, Воксельная карта освещения
|
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, Воксельная карта освещения
|
||||||
@@ -1232,7 +1102,6 @@ class VulkanRenderSession : public IRenderSession {
|
|||||||
NodeStaticOpaquePipeline = VK_NULL_HANDLE,
|
NodeStaticOpaquePipeline = VK_NULL_HANDLE,
|
||||||
NodeStaticTransparentPipeline = VK_NULL_HANDLE;
|
NodeStaticTransparentPipeline = VK_NULL_HANDLE;
|
||||||
|
|
||||||
std::map<AssetsTexture, uint16_t> ServerToAtlas;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WorldPCO PCO;
|
WorldPCO PCO;
|
||||||
@@ -1254,13 +1123,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void beforeDraw();
|
void beforeDraw();
|
||||||
|
void onGpuFinished();
|
||||||
void drawWorld(GlobalTime gTime, float dTime, VkCommandBuffer drawCmd);
|
void drawWorld(GlobalTime gTime, float dTime, VkCommandBuffer drawCmd);
|
||||||
void pushStage(EnumRenderStage stage);
|
void pushStage(EnumRenderStage stage);
|
||||||
|
|
||||||
static std::vector<VoxelVertexPoint> generateMeshForVoxelChunks(const std::vector<VoxelCube>& cubes);
|
static std::vector<VoxelVertexPoint> generateMeshForVoxelChunks(const std::vector<VoxelCube>& cubes);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateDescriptor_MainAtlas();
|
|
||||||
void updateDescriptor_VoxelsLight();
|
void updateDescriptor_VoxelsLight();
|
||||||
void updateDescriptor_ChunksLight();
|
void updateDescriptor_ChunksLight();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -948,8 +948,11 @@ PreparedNodeState::PreparedNodeState(const std::u8string_view data) {
|
|||||||
for(int counter4 = 0; counter4 < transformsSize; counter4++) {
|
for(int counter4 = 0; counter4 < transformsSize; counter4++) {
|
||||||
Transformation tr;
|
Transformation tr;
|
||||||
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
||||||
|
tr.Value = lr.read<float>();
|
||||||
mod2.Transforms.push_back(tr);
|
mod2.Transforms.push_back(tr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod.Models.push_back(std::move(mod2));
|
||||||
}
|
}
|
||||||
|
|
||||||
mod.UVLock = lr.read<uint8_t>();
|
mod.UVLock = lr.read<uint8_t>();
|
||||||
@@ -961,6 +964,7 @@ PreparedNodeState::PreparedNodeState(const std::u8string_view data) {
|
|||||||
for(int counter3 = 0; counter3 < transformsSize; counter3++) {
|
for(int counter3 = 0; counter3 < transformsSize; counter3++) {
|
||||||
Transformation tr;
|
Transformation tr;
|
||||||
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
||||||
|
tr.Value = lr.read<float>();
|
||||||
mod.Transforms.push_back(tr);
|
mod.Transforms.push_back(tr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -976,12 +980,15 @@ PreparedNodeState::PreparedNodeState(const std::u8string_view data) {
|
|||||||
for(int counter3 = 0; counter3 < transformsSize; counter3++) {
|
for(int counter3 = 0; counter3 < transformsSize; counter3++) {
|
||||||
Transformation tr;
|
Transformation tr;
|
||||||
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
tr.Op = Transformation::EnumTransform(lr.read<uint8_t>());
|
||||||
|
tr.Value = lr.read<float>();
|
||||||
mod.Transforms.push_back(tr);
|
mod.Transforms.push_back(tr);
|
||||||
}
|
}
|
||||||
|
|
||||||
variants.emplace_back(weight, std::move(mod));
|
variants.emplace_back(weight, std::move(mod));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Routes.emplace_back(nodeId, std::move(variants));
|
||||||
}
|
}
|
||||||
|
|
||||||
lr.checkUnreaded();
|
lr.checkUnreaded();
|
||||||
@@ -1088,9 +1095,9 @@ uint16_t PreparedNodeState::parseCondition(const std::string_view expression) {
|
|||||||
char c = expression[pos];
|
char c = expression[pos];
|
||||||
|
|
||||||
// Числа
|
// Числа
|
||||||
if(std::isdigit(c)) {
|
if(std::isdigit(static_cast<unsigned char>(c))) {
|
||||||
ssize_t npos = pos;
|
ssize_t npos = pos;
|
||||||
for(; npos < expression.size() && std::isdigit(expression[npos]); npos++);
|
for(; npos < expression.size() && std::isdigit(static_cast<unsigned char>(expression[npos])); npos++);
|
||||||
int value;
|
int value;
|
||||||
std::string_view value_view = expression.substr(pos, npos-pos);
|
std::string_view value_view = expression.substr(pos, npos-pos);
|
||||||
auto [partial_ptr, partial_ec] = std::from_chars(value_view.data(), value_view.data() + value_view.size(), value);
|
auto [partial_ptr, partial_ec] = std::from_chars(value_view.data(), value_view.data() + value_view.size(), value);
|
||||||
@@ -1102,15 +1109,20 @@ uint16_t PreparedNodeState::parseCondition(const std::string_view expression) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tokens.push_back(value);
|
tokens.push_back(value);
|
||||||
|
pos = npos - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Переменные
|
// Переменные
|
||||||
if(std::isalpha(c) || c == ':') {
|
if(std::isalpha(static_cast<unsigned char>(c)) || c == '_' || c == ':') {
|
||||||
ssize_t npos = pos;
|
ssize_t npos = pos;
|
||||||
for(; npos < expression.size() && std::isalpha(expression[npos]); npos++);
|
for(; npos < expression.size(); npos++) {
|
||||||
|
char ch = expression[npos];
|
||||||
|
if(!std::isalnum(static_cast<unsigned char>(ch)) && ch != '_' && ch != ':')
|
||||||
|
break;
|
||||||
|
}
|
||||||
std::string_view value = expression.substr(pos, npos-pos);
|
std::string_view value = expression.substr(pos, npos-pos);
|
||||||
pos += value.size();
|
pos = npos - 1;
|
||||||
if(value == "true")
|
if(value == "true")
|
||||||
tokens.push_back(1);
|
tokens.push_back(1);
|
||||||
else if(value == "false")
|
else if(value == "false")
|
||||||
@@ -1121,7 +1133,7 @@ uint16_t PreparedNodeState::parseCondition(const std::string_view expression) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Двойные операторы
|
// Двойные операторы
|
||||||
if(pos-1 < expression.size()) {
|
if(pos + 1 < expression.size()) {
|
||||||
char n = expression[pos+1];
|
char n = expression[pos+1];
|
||||||
|
|
||||||
if(c == '<' && n == '=') {
|
if(c == '<' && n == '=') {
|
||||||
@@ -1145,22 +1157,23 @@ uint16_t PreparedNodeState::parseCondition(const std::string_view expression) {
|
|||||||
|
|
||||||
// Операторы
|
// Операторы
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case '(': tokens.push_back(EnumTokenKind::LParen);
|
case '(': tokens.push_back(EnumTokenKind::LParen); break;
|
||||||
case ')': tokens.push_back(EnumTokenKind::RParen);
|
case ')': tokens.push_back(EnumTokenKind::RParen); break;
|
||||||
case '+': tokens.push_back(EnumTokenKind::Plus);
|
case '+': tokens.push_back(EnumTokenKind::Plus); break;
|
||||||
case '-': tokens.push_back(EnumTokenKind::Minus);
|
case '-': tokens.push_back(EnumTokenKind::Minus); break;
|
||||||
case '*': tokens.push_back(EnumTokenKind::Star);
|
case '*': tokens.push_back(EnumTokenKind::Star); break;
|
||||||
case '/': tokens.push_back(EnumTokenKind::Slash);
|
case '/': tokens.push_back(EnumTokenKind::Slash); break;
|
||||||
case '%': tokens.push_back(EnumTokenKind::Percent);
|
case '%': tokens.push_back(EnumTokenKind::Percent); break;
|
||||||
case '!': tokens.push_back(EnumTokenKind::Not);
|
case '!': tokens.push_back(EnumTokenKind::Not); break;
|
||||||
case '&': tokens.push_back(EnumTokenKind::And);
|
case '&': tokens.push_back(EnumTokenKind::And); break;
|
||||||
case '|': tokens.push_back(EnumTokenKind::Or);
|
case '|': tokens.push_back(EnumTokenKind::Or); break;
|
||||||
case '<': tokens.push_back(EnumTokenKind::LT);
|
case '<': tokens.push_back(EnumTokenKind::LT); break;
|
||||||
case '>': tokens.push_back(EnumTokenKind::GT);
|
case '>': tokens.push_back(EnumTokenKind::GT); break;
|
||||||
}
|
default:
|
||||||
|
|
||||||
MAKE_ERROR("Недопустимый символ: " << c);
|
MAKE_ERROR("Недопустимый символ: " << c);
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for(size_t index = 0; index < tokens.size(); index++) {
|
for(size_t index = 0; index < tokens.size(); index++) {
|
||||||
|
|||||||
@@ -499,15 +499,15 @@ AssetsManager::Out_applyResourceChange AssetsManager::applyResourceChange(const
|
|||||||
PreparedNodeState nodestate = _nodestate;
|
PreparedNodeState nodestate = _nodestate;
|
||||||
|
|
||||||
// Ресолвим модели
|
// Ресолвим модели
|
||||||
for(const auto& [lKey, lDomain] : nodestate.LocalToModelKD) {
|
for(const auto& [lDomain, lKey] : nodestate.LocalToModelKD) {
|
||||||
nodestate.LocalToModel.push_back(lock->getId(EnumAssets::Nodestate, lDomain, lKey));
|
nodestate.LocalToModel.push_back(lock->getId(EnumAssets::Model, lDomain, lKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Сдампим для отправки клиенту (Кеш в пролёте?)
|
// Сдампим для отправки клиенту (Кеш в пролёте?)
|
||||||
Resource res(nodestate.dump());
|
Resource res(nodestate.dump());
|
||||||
|
|
||||||
// На оповещение
|
// На оповещение
|
||||||
result.NewOrChange[(int) EnumAssets::Model].push_back({resId, res});
|
result.NewOrChange[(int) EnumAssets::Nodestate].push_back({resId, res});
|
||||||
|
|
||||||
// Запись в таблице ресурсов
|
// Запись в таблице ресурсов
|
||||||
data.emplace(ftt, res, domain, key);
|
data.emplace(ftt, res, domain, key);
|
||||||
|
|||||||
@@ -258,6 +258,10 @@ public:
|
|||||||
std::tuple<AssetsNodestate, std::vector<AssetsModel>, std::vector<AssetsTexture>>
|
std::tuple<AssetsNodestate, std::vector<AssetsModel>, std::vector<AssetsTexture>>
|
||||||
getNodeDependency(const std::string& domain, const std::string& key)
|
getNodeDependency(const std::string& domain, const std::string& key)
|
||||||
{
|
{
|
||||||
|
if(domain == "core" && key == "none") {
|
||||||
|
return {0, {}, {}};
|
||||||
|
}
|
||||||
|
|
||||||
auto lock = LocalObj.lock();
|
auto lock = LocalObj.lock();
|
||||||
AssetsNodestate nodestateId = lock->getId(EnumAssets::Nodestate, domain, key+".json");
|
AssetsNodestate nodestateId = lock->getId(EnumAssets::Nodestate, domain, key+".json");
|
||||||
|
|
||||||
|
|||||||
@@ -862,7 +862,6 @@ void GameServer::BackingAsyncLua_t::run(int id) {
|
|||||||
Pos::bvec64u nodePos(x, y, z);
|
Pos::bvec64u nodePos(x, y, z);
|
||||||
auto &node = out.Nodes[Pos::bvec4u(nodePos >> 4).pack()][Pos::bvec16u(nodePos & 0xf).pack()];
|
auto &node = out.Nodes[Pos::bvec4u(nodePos >> 4).pack()][Pos::bvec16u(nodePos & 0xf).pack()];
|
||||||
node.NodeId = id;
|
node.NodeId = id;
|
||||||
node.Meta = 0;
|
|
||||||
|
|
||||||
if(x == 0 && z == 0)
|
if(x == 0 && z == 0)
|
||||||
node.NodeId = 1;
|
node.NodeId = 1;
|
||||||
@@ -876,7 +875,7 @@ void GameServer::BackingAsyncLua_t::run(int id) {
|
|||||||
else if(x == 0 && y == 1)
|
else if(x == 0 && y == 1)
|
||||||
node.NodeId = 0;
|
node.NodeId = 0;
|
||||||
|
|
||||||
// node.Meta = 0;
|
node.Meta = uint8_t((x + y + z + int(node.NodeId)) & 0x3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// else {
|
// else {
|
||||||
@@ -1445,12 +1444,7 @@ void GameServer::init(fs::path worldPath) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
sol::table t = LuaMainState.create_table();
|
sol::table t = LuaMainState.create_table();
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test0", t);
|
Content.CM.registerBase(EnumDefContent::Node, "core", "none", t);
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test1", t);
|
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test2", t);
|
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test3", t);
|
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test4", t);
|
|
||||||
Content.CM.registerBase(EnumDefContent::Node, "test", "test5", t);
|
|
||||||
Content.CM.registerBase(EnumDefContent::World, "test", "devel_world", t);
|
Content.CM.registerBase(EnumDefContent::World, "test", "devel_world", t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2364,7 +2358,9 @@ void GameServer::stepSyncContent() {
|
|||||||
|
|
||||||
auto region = Expanse.Worlds[0]->Regions.find(rPos);
|
auto region = Expanse.Worlds[0]->Regions.find(rPos);
|
||||||
if(region != Expanse.Worlds[0]->Regions.end()) {
|
if(region != Expanse.Worlds[0]->Regions.end()) {
|
||||||
region->second->Nodes[cPos.pack()][nPos.pack()].NodeId = 4;
|
Node& n = region->second->Nodes[cPos.pack()][nPos.pack()];
|
||||||
|
n.NodeId = 4;
|
||||||
|
n.Meta = uint8_t((int(nPos.x) + int(nPos.y) + int(nPos.z)) & 0x3);
|
||||||
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2379,7 +2375,9 @@ void GameServer::stepSyncContent() {
|
|||||||
|
|
||||||
auto region = Expanse.Worlds[0]->Regions.find(rPos);
|
auto region = Expanse.Worlds[0]->Regions.find(rPos);
|
||||||
if(region != Expanse.Worlds[0]->Regions.end()) {
|
if(region != Expanse.Worlds[0]->Regions.end()) {
|
||||||
region->second->Nodes[cPos.pack()][nPos.pack()].NodeId = 0;
|
Node& n = region->second->Nodes[cPos.pack()][nPos.pack()];
|
||||||
|
n.NodeId = 0;
|
||||||
|
n.Meta = 0;
|
||||||
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
region->second->IsChunkChanged_Nodes |= 1ull << cPos.pack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,25 +16,29 @@ layout(push_constant) uniform UniformBufferObject {
|
|||||||
|
|
||||||
// struct NodeVertexStatic {
|
// struct NodeVertexStatic {
|
||||||
// uint32_t
|
// uint32_t
|
||||||
// FX : 9, FY : 9, FZ : 9, // Позиция -224 ~ 288; 64 позиций в одной ноде, 7.5 метров в ряд
|
// FX : 11, FY : 11, N1 : 10, // Позиция, 64 позиции на метр, +3.5м запас
|
||||||
// N1 : 4, // Не занято
|
// FZ : 11, // Позиция
|
||||||
// LS : 1, // Масштаб карты освещения (1м/16 или 1м)
|
// LS : 1, // Масштаб карты освещения (1м/16 или 1м)
|
||||||
// Tex : 18, // Текстура
|
// Tex : 18, // Текстура
|
||||||
// N2 : 14, // Не занято
|
// N2 : 2, // Не занято
|
||||||
// TU : 16, TV : 16; // UV на текстуре
|
// TU : 16, TV : 16; // UV на текстуре
|
||||||
// };
|
// };
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
uint fx = Vertex.x & 0x7ffu;
|
||||||
|
uint fy = (Vertex.x >> 11) & 0x7ffu;
|
||||||
|
uint fz = Vertex.y & 0x7ffu;
|
||||||
|
|
||||||
vec4 baseVec = ubo.model*vec4(
|
vec4 baseVec = ubo.model*vec4(
|
||||||
float(Vertex.x & 0x1ff) / 64.f - 3.5f,
|
float(fx) / 64.f - 3.5f,
|
||||||
float((Vertex.x >> 9) & 0x1ff) / 64.f - 3.5f,
|
float(fy) / 64.f - 3.5f,
|
||||||
float((Vertex.x >> 18) & 0x1ff) / 64.f - 3.5f,
|
float(fz) / 64.f - 3.5f,
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
|
|
||||||
Geometry.GeoPos = baseVec.xyz;
|
Geometry.GeoPos = baseVec.xyz;
|
||||||
Geometry.Texture = Vertex.y & 0x3ffff;
|
Geometry.Texture = (Vertex.y >> 12) & 0x3ffffu;
|
||||||
Geometry.UV = vec2(
|
Geometry.UV = vec2(
|
||||||
float(Vertex.z & 0xffff) / pow(2, 16),
|
float(Vertex.z & 0xffff) / pow(2, 16),
|
||||||
float((Vertex.z >> 16) & 0xffff) / pow(2, 16)
|
float((Vertex.z >> 16) & 0xffff) / pow(2, 16)
|
||||||
|
|||||||
@@ -11,65 +11,36 @@ layout(location = 0) in FragmentObj {
|
|||||||
|
|
||||||
layout(location = 0) out vec4 Frame;
|
layout(location = 0) out vec4 Frame;
|
||||||
|
|
||||||
struct InfoSubTexture {
|
struct AtlasEntry {
|
||||||
uint Flags; // 1 isExist
|
vec4 UVMinMax;
|
||||||
uint PosXY, WidthHeight;
|
uint Layer;
|
||||||
|
uint Flags;
|
||||||
uint AnimationFrames_AnimationTimePerFrame;
|
uint _Pad0;
|
||||||
|
uint _Pad1;
|
||||||
};
|
};
|
||||||
|
|
||||||
uniform layout(set = 0, binding = 0) sampler2D MainAtlas;
|
const uint ATLAS_ENTRY_VALID = 1u;
|
||||||
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
|
||||||
uint SubsCount;
|
|
||||||
uint Counter;
|
|
||||||
uint WidthHeight;
|
|
||||||
|
|
||||||
InfoSubTexture SubTextures[];
|
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
||||||
|
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
||||||
|
AtlasEntry Entries[];
|
||||||
} MainAtlasLayout;
|
} MainAtlasLayout;
|
||||||
|
|
||||||
uniform layout(set = 1, binding = 0) sampler2D LightMap;
|
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
||||||
layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
||||||
vec3 Color;
|
vec3 Color;
|
||||||
} LightMapLayout;
|
} LightMapLayout;
|
||||||
|
|
||||||
vec4 atlasColor(uint texId, vec2 uv)
|
vec4 atlasColor(uint texId, vec2 uv)
|
||||||
{
|
{
|
||||||
uint flags = (texId & 0xffff0000) >> 16;
|
AtlasEntry entry = MainAtlasLayout.Entries[texId];
|
||||||
texId &= 0xffff;
|
if((entry.Flags & ATLAS_ENTRY_VALID) == 0u)
|
||||||
vec4 color = vec4(uv, 0, 1);
|
|
||||||
|
|
||||||
if((flags & (2 | 4)) > 0)
|
|
||||||
{
|
|
||||||
if((flags & 2) > 0)
|
|
||||||
color = vec4(1, 1, 1, 1);
|
|
||||||
else if((flags & 4) > 0)
|
|
||||||
{
|
|
||||||
color = vec4(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else if(texId >= uint(MainAtlasLayout.SubsCount))
|
|
||||||
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2 ) * vec3(0, 1, 1), 1);
|
|
||||||
else {
|
|
||||||
InfoSubTexture texInfo = MainAtlasLayout.SubTextures[texId];
|
|
||||||
if(texInfo.Flags == 0)
|
|
||||||
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
||||||
|
|
||||||
uint posX = texInfo.PosXY & 0xffff;
|
vec2 baseUV = vec2(uv.x, 1.0f - uv.y);
|
||||||
uint posY = (texInfo.PosXY >> 16) & 0xffff;
|
vec2 atlasUV = mix(entry.UVMinMax.xy, entry.UVMinMax.zw, baseUV);
|
||||||
uint width = texInfo.WidthHeight & 0xffff;
|
atlasUV = clamp(atlasUV, entry.UVMinMax.xy, entry.UVMinMax.zw);
|
||||||
uint height = (texInfo.WidthHeight >> 16) & 0xffff;
|
return texture(MainAtlas, vec3(atlasUV, entry.Layer));
|
||||||
uint awidth = MainAtlasLayout.WidthHeight & 0xffff;
|
|
||||||
uint aheight = (MainAtlasLayout.WidthHeight >> 16) & 0xffff;
|
|
||||||
|
|
||||||
if((flags & 1) > 0)
|
|
||||||
color = texture(MainAtlas, vec2((posX+0.5f+uv.x*(width-1))/awidth, (posY+0.5f+(1-uv.y)*(height-1))/aheight));
|
|
||||||
else
|
|
||||||
color = texture(MainAtlas, vec2((posX+uv.x*width)/awidth, (posY+(1-uv.y)*height)/aheight));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return color;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 blendOverlay(vec3 base, vec3 blend) {
|
vec3 blendOverlay(vec3 base, vec3 blend) {
|
||||||
|
|||||||
@@ -9,9 +9,19 @@ layout(location = 0) in FragmentObj {
|
|||||||
|
|
||||||
layout(location = 0) out vec4 Frame;
|
layout(location = 0) out vec4 Frame;
|
||||||
|
|
||||||
|
struct AtlasEntry {
|
||||||
|
vec4 UVMinMax;
|
||||||
|
uint Layer;
|
||||||
|
uint Flags;
|
||||||
|
uint _Pad0;
|
||||||
|
uint _Pad1;
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint ATLAS_ENTRY_VALID = 1u;
|
||||||
|
|
||||||
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
||||||
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
||||||
vec3 Color;
|
AtlasEntry Entries[];
|
||||||
} MainAtlasLayout;
|
} MainAtlasLayout;
|
||||||
|
|
||||||
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
||||||
@@ -19,6 +29,19 @@ layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
|||||||
vec3 Color;
|
vec3 Color;
|
||||||
} LightMapLayout;
|
} LightMapLayout;
|
||||||
|
|
||||||
void main() {
|
vec4 atlasColor(uint texId, vec2 uv)
|
||||||
Frame = vec4(Fragment.GeoPos, 1);
|
{
|
||||||
|
AtlasEntry entry = MainAtlasLayout.Entries[texId];
|
||||||
|
if((entry.Flags & ATLAS_ENTRY_VALID) == 0u)
|
||||||
|
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
||||||
|
|
||||||
|
vec2 baseUV = vec2(uv.x, 1.0f - uv.y);
|
||||||
|
vec2 atlasUV = mix(entry.UVMinMax.xy, entry.UVMinMax.zw, baseUV);
|
||||||
|
atlasUV = clamp(atlasUV, entry.UVMinMax.xy, entry.UVMinMax.zw);
|
||||||
|
return texture(MainAtlas, vec3(atlasUV, entry.Layer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Frame = atlasColor(Fragment.Texture, Fragment.UV);
|
||||||
|
Frame.xyz *= max(0.2f, dot(Fragment.Normal, normalize(vec3(0.5, 1, 0.8))));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,23 +9,22 @@ layout(location = 0) in FragmentObj {
|
|||||||
|
|
||||||
layout(location = 0) out vec4 Frame;
|
layout(location = 0) out vec4 Frame;
|
||||||
|
|
||||||
struct InfoSubTexture {
|
struct AtlasEntry {
|
||||||
uint Flags; // 1 isExist
|
vec4 UVMinMax;
|
||||||
uint PosXY, WidthHeight;
|
uint Layer;
|
||||||
|
uint Flags;
|
||||||
uint AnimationFrames_AnimationTimePerFrame;
|
uint _Pad0;
|
||||||
|
uint _Pad1;
|
||||||
};
|
};
|
||||||
|
|
||||||
uniform layout(set = 0, binding = 0) sampler2D MainAtlas;
|
const uint ATLAS_ENTRY_VALID = 1u;
|
||||||
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
|
||||||
uint SubsCount;
|
|
||||||
uint Counter;
|
|
||||||
uint WidthHeight;
|
|
||||||
|
|
||||||
InfoSubTexture SubTextures[];
|
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
||||||
|
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
||||||
|
AtlasEntry Entries[];
|
||||||
} MainAtlasLayout;
|
} MainAtlasLayout;
|
||||||
|
|
||||||
uniform layout(set = 1, binding = 0) sampler2D LightMap;
|
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
||||||
layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
||||||
vec3 Color;
|
vec3 Color;
|
||||||
} LightMapLayout;
|
} LightMapLayout;
|
||||||
@@ -35,42 +34,14 @@ vec4 atlasColor(uint texId, vec2 uv)
|
|||||||
{
|
{
|
||||||
uv = mod(uv, 1);
|
uv = mod(uv, 1);
|
||||||
|
|
||||||
uint flags = (texId & 0xffff0000) >> 16;
|
AtlasEntry entry = MainAtlasLayout.Entries[texId];
|
||||||
texId &= 0xffff;
|
if((entry.Flags & ATLAS_ENTRY_VALID) == 0u)
|
||||||
vec4 color = vec4(uv, 0, 1);
|
|
||||||
|
|
||||||
if((flags & (2 | 4)) > 0)
|
|
||||||
{
|
|
||||||
if((flags & 2) > 0)
|
|
||||||
color = vec4(1, 1, 1, 1);
|
|
||||||
else if((flags & 4) > 0)
|
|
||||||
{
|
|
||||||
color = vec4(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else if(texId >= uint(MainAtlasLayout.SubsCount))
|
|
||||||
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2 ) * vec3(0, 1, 1), 1);
|
|
||||||
else {
|
|
||||||
InfoSubTexture texInfo = MainAtlasLayout.SubTextures[texId];
|
|
||||||
if(texInfo.Flags == 0)
|
|
||||||
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
||||||
|
|
||||||
uint posX = texInfo.PosXY & 0xffff;
|
vec2 baseUV = vec2(uv.x, 1.0f - uv.y);
|
||||||
uint posY = (texInfo.PosXY >> 16) & 0xffff;
|
vec2 atlasUV = mix(entry.UVMinMax.xy, entry.UVMinMax.zw, baseUV);
|
||||||
uint width = texInfo.WidthHeight & 0xffff;
|
atlasUV = clamp(atlasUV, entry.UVMinMax.xy, entry.UVMinMax.zw);
|
||||||
uint height = (texInfo.WidthHeight >> 16) & 0xffff;
|
return texture(MainAtlas, vec3(atlasUV, entry.Layer));
|
||||||
uint awidth = MainAtlasLayout.WidthHeight & 0xffff;
|
|
||||||
uint aheight = (MainAtlasLayout.WidthHeight >> 16) & 0xffff;
|
|
||||||
|
|
||||||
if((flags & 1) > 0)
|
|
||||||
color = texture(MainAtlas, vec2((posX+0.5f+uv.x*(width-1))/awidth, (posY+0.5f+(1-uv.y)*(height-1))/aheight));
|
|
||||||
else
|
|
||||||
color = texture(MainAtlas, vec2((posX+uv.x*width)/awidth, (posY+(1-uv.y)*height)/aheight));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return color;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|||||||
@@ -9,9 +9,19 @@ layout(location = 0) in Fragment {
|
|||||||
|
|
||||||
layout(location = 0) out vec4 Frame;
|
layout(location = 0) out vec4 Frame;
|
||||||
|
|
||||||
|
struct AtlasEntry {
|
||||||
|
vec4 UVMinMax;
|
||||||
|
uint Layer;
|
||||||
|
uint Flags;
|
||||||
|
uint _Pad0;
|
||||||
|
uint _Pad1;
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint ATLAS_ENTRY_VALID = 1u;
|
||||||
|
|
||||||
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
uniform layout(set = 0, binding = 0) sampler2DArray MainAtlas;
|
||||||
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
layout(set = 0, binding = 1) readonly buffer MainAtlasLayoutObj {
|
||||||
vec3 Color;
|
AtlasEntry Entries[];
|
||||||
} MainAtlasLayout;
|
} MainAtlasLayout;
|
||||||
|
|
||||||
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
uniform layout(set = 1, binding = 0) sampler2DArray LightMap;
|
||||||
@@ -19,6 +29,39 @@ layout(set = 1, binding = 1) readonly buffer LightMapLayoutObj {
|
|||||||
vec3 Color;
|
vec3 Color;
|
||||||
} LightMapLayout;
|
} LightMapLayout;
|
||||||
|
|
||||||
void main() {
|
vec4 atlasColor(uint texId, vec2 uv)
|
||||||
Frame = vec4(1);
|
{
|
||||||
|
uv = mod(uv, 1);
|
||||||
|
|
||||||
|
AtlasEntry entry = MainAtlasLayout.Entries[texId];
|
||||||
|
if((entry.Flags & ATLAS_ENTRY_VALID) == 0u)
|
||||||
|
return vec4(((int(gl_FragCoord.x / 128) + int(gl_FragCoord.y / 128)) % 2) * vec3(1, 0, 1), 1);
|
||||||
|
|
||||||
|
vec2 baseUV = vec2(uv.x, 1.0f - uv.y);
|
||||||
|
vec2 atlasUV = mix(entry.UVMinMax.xy, entry.UVMinMax.zw, baseUV);
|
||||||
|
atlasUV = clamp(atlasUV, entry.UVMinMax.xy, entry.UVMinMax.zw);
|
||||||
|
return texture(MainAtlas, vec3(atlasUV, entry.Layer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec2 uv;
|
||||||
|
|
||||||
|
switch(fragment.Place) {
|
||||||
|
case 0:
|
||||||
|
uv = fragment.GeoPos.xz; break;
|
||||||
|
case 1:
|
||||||
|
uv = fragment.GeoPos.xy; break;
|
||||||
|
case 2:
|
||||||
|
uv = fragment.GeoPos.zy; break;
|
||||||
|
case 3:
|
||||||
|
uv = fragment.GeoPos.xz*vec2(-1, -1); break;
|
||||||
|
case 4:
|
||||||
|
uv = fragment.GeoPos.xy*vec2(-1, 1); break;
|
||||||
|
case 5:
|
||||||
|
uv = fragment.GeoPos.zy*vec2(-1, 1); break;
|
||||||
|
default:
|
||||||
|
uv = vec2(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame = atlasColor(fragment.VoxMTL, uv);
|
||||||
}
|
}
|
||||||
|
|||||||
81
mods/test/assets/test/model/node/acacia_planks.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "acacia_planks.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/frame.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "frame.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/grass.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "grass.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/jungle_planks.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "jungle_planks.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/oak_planks.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "oak_planks.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "tropical_rainforest_wood.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/willow_wood.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "willow_wood.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/xnether_blue_wood.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "xnether_blue_wood.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
81
mods/test/assets/test/model/node/xnether_purple_wood.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"textures": {
|
||||||
|
"default": "xnether_purple_wood.png"
|
||||||
|
},
|
||||||
|
"cuboids": [
|
||||||
|
{
|
||||||
|
"from": [
|
||||||
|
-0.5,
|
||||||
|
-0.5,
|
||||||
|
-0.5
|
||||||
|
],
|
||||||
|
"to": [
|
||||||
|
0.5,
|
||||||
|
0.5,
|
||||||
|
0.5
|
||||||
|
],
|
||||||
|
"faces": {
|
||||||
|
"down": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "down"
|
||||||
|
},
|
||||||
|
"up": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "up"
|
||||||
|
},
|
||||||
|
"north": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "north"
|
||||||
|
},
|
||||||
|
"south": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "south"
|
||||||
|
},
|
||||||
|
"west": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "west"
|
||||||
|
},
|
||||||
|
"east": {
|
||||||
|
"uv": [
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"texture": "default",
|
||||||
|
"cullface": "east"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test0.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/grass.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/oak_planks.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/jungle_planks.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/acacia_planks.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test1.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/tropical_rainforest_wood.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/willow_wood.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/xnether_blue_wood.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/xnether_purple_wood.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test2.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/frame.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/grass.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/oak_planks.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/acacia_planks.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test3.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/jungle_planks.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/tropical_rainforest_wood.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/willow_wood.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/xnether_blue_wood.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test4.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/oak_planks.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/jungle_planks.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/acacia_planks.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/willow_wood.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
14
mods/test/assets/test/nodestate/test5.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"meta==0": {
|
||||||
|
"model": "node/grass.json"
|
||||||
|
},
|
||||||
|
"meta==1": {
|
||||||
|
"model": "node/frame.json"
|
||||||
|
},
|
||||||
|
"meta==2": {
|
||||||
|
"model": "node/xnether_purple_wood.json"
|
||||||
|
},
|
||||||
|
"meta==3": {
|
||||||
|
"model": "node/tropical_rainforest_wood.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
mods/test/assets/test/texture/0.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
mods/test/assets/test/texture/acacia_planks.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
mods/test/assets/test/texture/frame.png
Normal file
|
After Width: | Height: | Size: 138 B |
BIN
mods/test/assets/test/texture/grass.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
mods/test/assets/test/texture/jungle_planks.png
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
mods/test/assets/test/texture/oak_planks.png
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
mods/test/assets/test/texture/tropical_rainforest_wood.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
mods/test/assets/test/texture/willow_wood.png
Normal file
|
After Width: | Height: | Size: 2.0 KiB |
BIN
mods/test/assets/test/texture/xnether_blue_wood.png
Normal file
|
After Width: | Height: | Size: 6.0 KiB |
BIN
mods/test/assets/test/texture/xnether_purple_wood.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
98
mods/test/init.lua
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
-- parent = default:air
|
||||||
|
--
|
||||||
|
-- hasHalfTransparency
|
||||||
|
-- collideBox = {}
|
||||||
|
-- plantLike = {}
|
||||||
|
-- nodebox = {}
|
||||||
|
|
||||||
|
local node_template = {
|
||||||
|
parent = "default:normal" or node_template,
|
||||||
|
render = {
|
||||||
|
has_half_transparency = false
|
||||||
|
},
|
||||||
|
collision = {
|
||||||
|
|
||||||
|
},
|
||||||
|
events = {
|
||||||
|
|
||||||
|
},
|
||||||
|
node_advancement_factory = function(world_id, node_pos)
|
||||||
|
local node_advancement = {
|
||||||
|
onLoad = function(data)
|
||||||
|
|
||||||
|
end,
|
||||||
|
onSave = function()
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
return node_advancement
|
||||||
|
end
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
local instance = {}
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Движок автоматически подгружает ассеты из папки assets
|
||||||
|
В этом методе можно зарегистрировать ассеты из иных источников
|
||||||
|
Состояния нод, частицы, анимации, модели, текстуры, звуки, шрифты
|
||||||
|
]]--
|
||||||
|
function instance.assetsInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
*preInit. События для регистрации определений игрового контента
|
||||||
|
Ноды, воксели, миры, порталы, сущности, предметы
|
||||||
|
]]--
|
||||||
|
function instance.lowPreInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
До вызова preInit будет выполнена регистрация
|
||||||
|
контента из файлов в папке content
|
||||||
|
]]--
|
||||||
|
function instance.preInit()
|
||||||
|
local node_air = {}
|
||||||
|
|
||||||
|
node_air.hasHalfTransparency = false
|
||||||
|
node_air.collideBox = nil
|
||||||
|
node_air.render = nil
|
||||||
|
|
||||||
|
core.register_node('test0', {})
|
||||||
|
core.register_node('test1', {})
|
||||||
|
core.register_node('test2', {})
|
||||||
|
core.register_node('test3', {})
|
||||||
|
core.register_node('test4', {})
|
||||||
|
core.register_node('test5', {})
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance.highPreInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
На этом этапе можно наложить изменения
|
||||||
|
на зарегистрированный другими модами контент
|
||||||
|
]]--
|
||||||
|
function instance.init()
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance.postInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance.preDeInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance.deInit()
|
||||||
|
end
|
||||||
|
|
||||||
|
function instance.postDeInit()
|
||||||
|
core.unregister_node('test0')
|
||||||
|
core.unregister_node('test1')
|
||||||
|
core.unregister_node('test2')
|
||||||
|
core.unregister_node('test3')
|
||||||
|
core.unregister_node('test4')
|
||||||
|
core.unregister_node('test5')
|
||||||
|
end
|
||||||
|
|
||||||
|
return instance
|
||||||
9
mods/test/mod.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"id": "test",
|
||||||
|
"name": "Test Mod",
|
||||||
|
"description": "Это тестовый мод",
|
||||||
|
"depends": [],
|
||||||
|
"optional_depends": [],
|
||||||
|
"author": "DrSocalkwe3n",
|
||||||
|
"version": [0, 0, 0, 1]
|
||||||
|
}
|
||||||