mirror of
https://github.com/Eglantine-Community/Eglantine.git
synced 2026-11-23 05:39:37 -10:00
nvn: bindless texture table for handles a shader reads at run time
A shader that samples a texture whose handle it reads from a buffer the game fills (per pixel or per light) has no per-draw handle program that could name the texture. The replay now keeps one descriptor set (set 1) with every texture of the game's texture pool, one array per shader image type, and every sampler of the sampler pool; such texture ops index it with the handle, rebased by the pool bases the host puts in the driver bank (shader_recompiler/bindless_table.h, nvn_bindless.cpp). Needs descriptor indexing with update-after-bind (Device::IsBindlessTableSupported); the pipeline cache version goes to 18. Also: - a compute handle word resolves to the table that holds it, whichever kind of op uses it (a texture op on an image-table handle and the reverse); - a texture view with a level or layer count of 0 covers the full range from its base instead of one level/layer. Found on Fire Emblem: Fortune's Weave; none of it is specific to the game. VTX-HLE by Vortex727 (https://github.com/Vortex727/FUKKO) Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
8449ce0590
commit
359335ad98
27 files changed
+951
-32
No files matched your search
@@ -10,6 +10,7 @@
|
||||
#include <boost/container/static_vector.hpp>
|
||||
|
||||
#include "common/settings.h"
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
|
||||
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
|
||||
@@ -242,8 +243,49 @@ private:
|
||||
spv::ImageOperandsMask mask{};
|
||||
};
|
||||
|
||||
// The bindless table's image of a rebased handle word (texture id in bits 0..19) and, for a
|
||||
// sampled access, the sampler of its sampler id (bits 20..): the ids index arrays the host fills
|
||||
// from the game's pool registrations. An id past the capacity reads the last entry.
|
||||
Id BindlessIndex(EmitContext& ctx, Id handle, bool sampler) {
|
||||
const Id id{sampler ? ctx.OpShiftRightLogical(ctx.U32[1], handle, ctx.Const(20U))
|
||||
: ctx.OpBitwiseAnd(ctx.U32[1], handle, ctx.Const(0xFFFFFU))};
|
||||
const Id limit{ctx.Const((sampler ? BINDLESS_SAMPLER_CAPACITY : BINDLESS_TEXTURE_CAPACITY) -
|
||||
1)};
|
||||
const Id clamped{ctx.OpUMin(ctx.U32[1], id, limit)};
|
||||
DecorateNonUniform(ctx, clamped);
|
||||
ctx.uses_nonuniform_sampled_image = true;
|
||||
return clamped;
|
||||
}
|
||||
|
||||
Id BindlessImage(EmitContext& ctx, const TextureDefinition& def, const IR::Value& handle_value) {
|
||||
const Id handle{ctx.Def(handle_value)};
|
||||
const Id pointer{
|
||||
ctx.OpAccessChain(def.pointer_type, def.id, BindlessIndex(ctx, handle, false))};
|
||||
const Id image{ctx.OpLoad(def.image_type, pointer)};
|
||||
DecorateNonUniform(ctx, pointer);
|
||||
DecorateNonUniform(ctx, image);
|
||||
return image;
|
||||
}
|
||||
|
||||
Id BindlessSampled(EmitContext& ctx, const TextureDefinition& def, const IR::Value& handle_value) {
|
||||
const Id handle{ctx.Def(handle_value)};
|
||||
const Id image{BindlessImage(ctx, def, handle_value)};
|
||||
const Id sampler_pointer{ctx.OpAccessChain(ctx.bindless_sampler_pointer_type,
|
||||
ctx.bindless_samplers,
|
||||
BindlessIndex(ctx, handle, true))};
|
||||
const Id sampler{ctx.OpLoad(ctx.bindless_sampler_type, sampler_pointer)};
|
||||
DecorateNonUniform(ctx, sampler_pointer);
|
||||
DecorateNonUniform(ctx, sampler);
|
||||
const Id combined{ctx.OpSampledImage(def.sampled_type, image, sampler)};
|
||||
DecorateNonUniform(ctx, combined);
|
||||
return combined;
|
||||
}
|
||||
|
||||
Id Texture(EmitContext& ctx, IR::TextureInstInfo info, [[maybe_unused]] const IR::Value& index) {
|
||||
const TextureDefinition& def{ctx.textures.at(info.descriptor_index)};
|
||||
if (def.bindless) {
|
||||
return BindlessSampled(ctx, def, index);
|
||||
}
|
||||
if (def.count > 1) {
|
||||
auto const idx = index.IsImmediate() ? ctx.Const(index.U32()) : ctx.Def(index);
|
||||
const bool non_uniform{MarkNonUniform(ctx, idx, index, NonUniformKind::SampledImage)};
|
||||
@@ -277,6 +319,9 @@ Id TextureImage(EmitContext& ctx, IR::TextureInstInfo info, const IR::Value& ind
|
||||
return ctx.OpLoad(ctx.image_buffer_type, def.id);
|
||||
} else {
|
||||
const TextureDefinition& def{ctx.textures.at(info.descriptor_index)};
|
||||
if (def.bindless) {
|
||||
return BindlessImage(ctx, def, index);
|
||||
}
|
||||
if (def.count > 1) {
|
||||
auto const idx = index.IsImmediate() ? ctx.Const(index.U32()) : ctx.Def(index);
|
||||
const bool non_uniform{MarkNonUniform(ctx, idx, index, NonUniformKind::SampledImage)};
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "common/div_ceil.h"
|
||||
#include "common/env_gate.h"
|
||||
#include "shader_recompiler/backend/nvn_storage_descriptor.h"
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv_instructions.h"
|
||||
#include "shader_recompiler/backend/spirv/spirv_emit_context.h"
|
||||
@@ -2385,6 +2386,50 @@ void EmitContext::DefineTextures(const Info& info, u32& binding, u32& scaling_in
|
||||
for (const TextureDescriptor& desc : info.texture_descriptors) {
|
||||
const Id image_type{ImageType(*this, desc)};
|
||||
const Id sampled_type{TypeSampledImage(image_type)};
|
||||
if (desc.bindless) {
|
||||
// The replay's table of this type, in its own set (bindless_table.h): the descriptors
|
||||
// of the draw (set 0) do not number it.
|
||||
if (!bindless_samplers.value) {
|
||||
bindless_sampler_type = TypeSampler();
|
||||
const Id array_type{
|
||||
TypeArray(bindless_sampler_type, Const(BINDLESS_SAMPLER_CAPACITY))};
|
||||
bindless_sampler_pointer_type = TypePointer(spv::StorageClass::UniformConstant,
|
||||
bindless_sampler_type);
|
||||
bindless_samplers = AddGlobalVariable(
|
||||
TypePointer(spv::StorageClass::UniformConstant, array_type),
|
||||
spv::StorageClass::UniformConstant);
|
||||
Decorate(bindless_samplers, spv::Decoration::Binding, BINDLESS_SAMPLER_BINDING);
|
||||
Decorate(bindless_samplers, spv::Decoration::DescriptorSet, BINDLESS_SET);
|
||||
Name(bindless_samplers, "bindless_samplers");
|
||||
if (profile.supported_spirv >= 0x00010400) {
|
||||
interfaces.push_back(bindless_samplers);
|
||||
}
|
||||
}
|
||||
const Id array_type{TypeArray(image_type, Const(BINDLESS_TEXTURE_CAPACITY))};
|
||||
const Id id{AddGlobalVariable(TypePointer(spv::StorageClass::UniformConstant,
|
||||
array_type),
|
||||
spv::StorageClass::UniformConstant)};
|
||||
Decorate(id, spv::Decoration::Binding,
|
||||
BindlessTextureBinding(desc.type,
|
||||
BindlessClass(desc.is_integer, desc.is_depth)));
|
||||
Decorate(id, spv::Decoration::DescriptorSet, BINDLESS_SET);
|
||||
Name(id, NameOf(stage, desc, "bindless_tex"));
|
||||
textures.push_back({
|
||||
.id = id,
|
||||
.sampled_type = sampled_type,
|
||||
.pointer_type = TypePointer(spv::StorageClass::UniformConstant, image_type),
|
||||
.image_type = image_type,
|
||||
.count = BINDLESS_TEXTURE_CAPACITY,
|
||||
.is_multisample = desc.is_multisample,
|
||||
.is_integer = desc.is_integer,
|
||||
.bindless = true,
|
||||
});
|
||||
if (profile.supported_spirv >= 0x00010400) {
|
||||
interfaces.push_back(id);
|
||||
}
|
||||
++scaling_index;
|
||||
continue;
|
||||
}
|
||||
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, sampled_type)};
|
||||
const Id desc_type{DescType(*this, sampled_type, pointer_type, desc.count)};
|
||||
const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
|
||||
|
||||
@@ -47,6 +47,10 @@ struct TextureDefinition {
|
||||
u32 count;
|
||||
bool is_multisample;
|
||||
bool is_integer;
|
||||
/// The array is the replay's bindless table of this type (bindless_table.h): `id` is the
|
||||
/// array of images, indexed with the rebased handle word; `sampled_type` combines one with a
|
||||
/// sampler of `bindless_samplers`.
|
||||
bool bindless{};
|
||||
};
|
||||
|
||||
struct TextureBufferDefinition {
|
||||
@@ -463,6 +467,12 @@ public:
|
||||
// Sirit::Id doesn't play nice with *::set<>
|
||||
::Common::unordered_set<u32> non_uniform_ids;
|
||||
|
||||
/// The sampler array of the bindless table (set BINDLESS_SET, binding 0); unset until a
|
||||
/// bindless texture is declared.
|
||||
Id bindless_samplers{};
|
||||
Id bindless_sampler_type{};
|
||||
Id bindless_sampler_pointer_type{};
|
||||
|
||||
bool uses_nonuniform_sampled_image{};
|
||||
bool uses_nonuniform_storage_image{};
|
||||
bool uses_nonuniform_uniform_texel_buffer{};
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Vortex727 (FUKKO)
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "shader_recompiler/shader_info.h"
|
||||
|
||||
// The bindless texture table of VTX-HLE: what a shader needs when it samples a texture whose
|
||||
// handle it reads at run time (a handle stored in a buffer the game fills, indexed per pixel or
|
||||
// per light), where no per-draw handle program can name the texture. The replay keeps one
|
||||
// descriptor set (set BINDLESS_SET) holding every texture the game registered in its texture
|
||||
// pool, one array per shader image type, and every sampler of the sampler pool. A handle is the
|
||||
// game's: texture id in bits 0..19 and sampler id from bit 20, rebased into the replay's global
|
||||
// id space (RebaseHandleWord) with the pool bases the host puts in the driver bank (c0).
|
||||
|
||||
namespace Shader {
|
||||
|
||||
/// Descriptor set of the tables (set 0 holds the draw's own descriptors).
|
||||
constexpr u32 BINDLESS_SET = 1;
|
||||
|
||||
/// Entries of every texture array / of the sampler array. Ids past the capacity read the last
|
||||
/// entry, which stays a stand-in. The sampler id is 12 bits wide.
|
||||
constexpr u32 BINDLESS_TEXTURE_CAPACITY = 1u << 15;
|
||||
constexpr u32 BINDLESS_SAMPLER_CAPACITY = 1u << 12;
|
||||
|
||||
/// Binding of the sampler array.
|
||||
constexpr u32 BINDLESS_SAMPLER_BINDING = 0;
|
||||
|
||||
/// Bindings of the texture arrays: one per shader image type and sampled class (float, integer,
|
||||
/// depth: a depth-compare sampler needs a depth-typed image).
|
||||
constexpr u32 BINDLESS_TYPE_COUNT = 8; ///< Color1D .. ColorArrayCube, Color2DRect
|
||||
constexpr u32 BINDLESS_CLASS_COUNT = 3;
|
||||
constexpr u32 BINDLESS_BINDING_COUNT = 1 + BINDLESS_CLASS_COUNT * BINDLESS_TYPE_COUNT;
|
||||
|
||||
[[nodiscard]] constexpr bool BindlessSupportsType(TextureType type) noexcept {
|
||||
return static_cast<u32>(type) < BINDLESS_TYPE_COUNT;
|
||||
}
|
||||
|
||||
/// Class 0 float, 1 integer, 2 depth.
|
||||
[[nodiscard]] constexpr u32 BindlessClass(bool is_integer, bool is_depth) noexcept {
|
||||
return is_depth ? 2u : (is_integer ? 1u : 0u);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr u32 BindlessTextureBinding(TextureType type, u32 klass) noexcept {
|
||||
return 1 + static_cast<u32>(type) + klass * BINDLESS_TYPE_COUNT;
|
||||
}
|
||||
|
||||
/// Where the host puts the bases of the bound pools in the driver bank (c0), u32 each. Past
|
||||
/// every offset the guest's shaders and the stage tables use.
|
||||
constexpr u32 BINDLESS_DRIVER_TEXTURE_BASE = 0xf00;
|
||||
constexpr u32 BINDLESS_DRIVER_SAMPLER_BASE = 0xf04;
|
||||
|
||||
} // namespace Shader
|
||||
@@ -72,6 +72,8 @@ struct ConstBufferAddr {
|
||||
u32 count;
|
||||
bool has_secondary;
|
||||
u32 handle_program{~0U}; ///< index in Info::handle_programs for a handle computed per draw
|
||||
bool bindless{}; ///< the handle (`handle`) is only known at run time
|
||||
IR::U32 handle; ///< with `bindless`: the guest handle word, as the shader computes it
|
||||
};
|
||||
|
||||
/// Window of 16-byte {u64 address, u32 size, u32 pad} buffer descriptors in the driver bank (c0)
|
||||
|
||||
@@ -44,6 +44,9 @@ struct HostTranslateInfo {
|
||||
bool support_conditional_barrier{}; ///< True when the device supports barriers in conditional
|
||||
///< control flow
|
||||
bool single_lane_geometry_subgroups{};
|
||||
/// VTX-HLE graphics stages: a bindless texture handle that no per-draw handle program can
|
||||
/// compute is looked up in the replay's bindless table at run time (bindless_table.h).
|
||||
bool nvn_bindless_table{};
|
||||
/// VTX-HLE only: a global memory access whose address comes out of a memory load is never
|
||||
/// tied to a storage buffer (the search stops at loads); such accesses stay raw global memory
|
||||
/// (Profile::support_raw_global_memory).
|
||||
|
||||
@@ -19,9 +19,11 @@
|
||||
#include <limits>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
#include "common/logging.h"
|
||||
#include "common/scope_exit.h"
|
||||
#include "common/settings.h"
|
||||
#include "common/env_gate.h"
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "shader_recompiler/environment.h"
|
||||
#include "shader_recompiler/frontend/ir/basic_block.h"
|
||||
#include "shader_recompiler/frontend/ir/breadth_first_search.h"
|
||||
@@ -774,6 +776,8 @@ bool BuildHandleNode(const IR::Value& value, std::vector<u32>& code, int& budget
|
||||
|
||||
// Programs built while making the texture instructions of the pass running on this thread.
|
||||
thread_local std::vector<std::vector<u32>>* g_handle_programs{};
|
||||
// The program being made may look run-time handles up in the host's bindless table.
|
||||
thread_local bool g_bindless_allowed{};
|
||||
|
||||
std::optional<u32> TryGetConstant(IR::Value& value, Environment& env) {
|
||||
const IR::Inst* inst = value.InstRecursive();
|
||||
@@ -936,6 +940,27 @@ TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst, const H
|
||||
int budget = 4096;
|
||||
g_handle_failure = IR::Opcode::Void;
|
||||
if (!g_handle_programs || !BuildHandleProgram(inst.Arg(0), code, budget, block)) {
|
||||
if (g_bindless_allowed && !IsStorageImageOpcode(IndexedInstruction(inst))) {
|
||||
// The handle depends on more than one draw can name (a light or material
|
||||
// record read per pixel): the host's bindless table is indexed with it.
|
||||
return TextureInst{
|
||||
.cbuf = ConstBufferAddr{
|
||||
.index = 0,
|
||||
.offset = 0,
|
||||
.shift_left = 0,
|
||||
.secondary_index = 0,
|
||||
.secondary_offset = 0,
|
||||
.secondary_shift_left = 0,
|
||||
.dynamic_offset = {},
|
||||
.count = 1,
|
||||
.has_secondary = false,
|
||||
.bindless = true,
|
||||
.handle = IR::U32{inst.Arg(0)},
|
||||
},
|
||||
.inst = &inst,
|
||||
.block = block,
|
||||
};
|
||||
}
|
||||
// Not computable for a whole draw (a handle that changes in a loop): keep the
|
||||
// earlier search through loads, which at least gives the shader a texture.
|
||||
const std::optional<ConstBufferAddr> legacy{IR::BreadthFirstSearch(inst.Arg(0),
|
||||
@@ -946,6 +971,9 @@ TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst, const H
|
||||
throw NotImplementedException("Failed to track bindless texture constant buffer (stopped at {}, budget {})",
|
||||
g_handle_failure, budget);
|
||||
}
|
||||
LOG_WARNING(Shader, "Bindless texture handle is not computable per draw (stopped at {}, budget {}); "
|
||||
"falling back to constant buffer c{}[{:#x}]",
|
||||
g_handle_failure, budget, legacy->index, legacy->offset);
|
||||
return TextureInst{.cbuf = *legacy, .inst = &inst, .block = block};
|
||||
}
|
||||
auto& programs{*g_handle_programs};
|
||||
@@ -1056,7 +1084,8 @@ public:
|
||||
desc.secondary_cbuf_offset == existing.secondary_cbuf_offset &&
|
||||
desc.secondary_shift_left == existing.secondary_shift_left &&
|
||||
desc.count == existing.count && desc.size_shift == existing.size_shift &&
|
||||
desc.handle_program == existing.handle_program;
|
||||
desc.handle_program == existing.handle_program &&
|
||||
desc.bindless == existing.bindless;
|
||||
})};
|
||||
// TODO: Read this from TIC
|
||||
texture_descriptors[index].is_multisample |= desc.is_multisample;
|
||||
@@ -1168,6 +1197,7 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
env.track_cache.clear();
|
||||
TextureInstVector to_replace;
|
||||
g_handle_programs = &program.info.handle_programs;
|
||||
g_bindless_allowed = host_info.nvn_bindless_table && program.stage != Stage::Compute;
|
||||
for (IR::Block* const block : program.post_order_blocks) {
|
||||
env.track_cache.clear();
|
||||
for (IR::Inst& inst : block->Instructions()) {
|
||||
@@ -1211,8 +1241,15 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
// A handle program has no descriptor to read a type from, and ImageQueryDimensions carries
|
||||
// none: it takes the type of another instruction using the same handle.
|
||||
std::unordered_map<u32, TextureType> handle_program_types;
|
||||
std::unordered_map<const IR::Inst*, TextureType> bindless_types;
|
||||
for (const TextureInst& texture_inst : to_replace) {
|
||||
const IR::Opcode opcode{IndexedInstruction(*texture_inst.inst)};
|
||||
if (texture_inst.cbuf.bindless && opcode != IR::Opcode::ImageQueryDimensions &&
|
||||
!texture_inst.cbuf.handle.IsImmediate()) {
|
||||
bindless_types.try_emplace(
|
||||
texture_inst.cbuf.handle.InstRecursive(),
|
||||
texture_inst.inst->Flags<IR::TextureInstInfo>().type.Value());
|
||||
}
|
||||
if (texture_inst.cbuf.handle_program != ~0U && opcode != IR::Opcode::ImageQueryDimensions &&
|
||||
!IsStorageImageOpcode(opcode)) {
|
||||
handle_program_types.try_emplace(
|
||||
@@ -1231,7 +1268,13 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
bool is_rect_sample{false};
|
||||
switch (inst->GetOpcode()) {
|
||||
case IR::Opcode::ImageQueryDimensions:
|
||||
if (const auto it{handle_program_types.find(cbuf.handle_program)};
|
||||
if (cbuf.bindless) {
|
||||
const auto it{cbuf.handle.IsImmediate() ? bindless_types.end()
|
||||
: bindless_types.find(
|
||||
cbuf.handle.InstRecursive())};
|
||||
flags.type.Assign(it != bindless_types.end() ? it->second
|
||||
: TextureType::Color2D);
|
||||
} else if (const auto it{handle_program_types.find(cbuf.handle_program)};
|
||||
it != handle_program_types.end()) {
|
||||
flags.type.Assign(it->second);
|
||||
} else {
|
||||
@@ -1244,7 +1287,8 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
break;
|
||||
}
|
||||
// Normalized once the descriptor is known (below).
|
||||
is_rect_sample = ReadTextureTypeCached(env, cbuf) == TextureType::Color2DRect;
|
||||
is_rect_sample =
|
||||
!cbuf.bindless && ReadTextureTypeCached(env, cbuf) == TextureType::Color2DRect;
|
||||
break;
|
||||
case IR::Opcode::ImageFetch:
|
||||
if (flags.type == TextureType::Color2D || flags.type == TextureType::Color2DRect ||
|
||||
@@ -1256,7 +1300,7 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
if (flags.type != TextureType::Color1D) {
|
||||
break;
|
||||
}
|
||||
if (ReadTextureTypeCached(env, cbuf) == TextureType::Buffer) {
|
||||
if (!cbuf.bindless && ReadTextureTypeCached(env, cbuf) == TextureType::Buffer) {
|
||||
// Replace with the bound texture type only when it's a texture buffer
|
||||
// If the instruction is 1D and the bound type is 2D, don't change the code and let
|
||||
// the rasterizer robustness handle it
|
||||
@@ -1337,7 +1381,7 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
// Depth-compare sampling returns a float in Vulkan, so its image must be float
|
||||
// even when a garbage or unknown descriptor reads as an integer format.
|
||||
static const bool depth_float_fix{Common::EnvGate("EGLANTINE_DEPTH_FLOAT_FIX")};
|
||||
const bool is_integer{(!depth_float_fix || flags.is_depth == 0) &&
|
||||
const bool is_integer{!cbuf.bindless && (!depth_float_fix || flags.is_depth == 0) &&
|
||||
is_texture_pixel_format_integer(cbuf)};
|
||||
index = descriptors.Add(TextureDescriptor{
|
||||
.type = flags.type,
|
||||
@@ -1354,6 +1398,7 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
.count = count,
|
||||
.size_shift = size_shift,
|
||||
.handle_program = cbuf.handle_program,
|
||||
.bindless = cbuf.bindless,
|
||||
});
|
||||
flags.is_integer.Assign(is_integer ? 1 : 0);
|
||||
}
|
||||
@@ -1362,7 +1407,25 @@ void TexturePass(Environment& env, IR::Program& program, const HostTranslateInfo
|
||||
flags.descriptor_index.Assign(index);
|
||||
inst->SetFlags(flags);
|
||||
|
||||
if (count > 1) {
|
||||
if (cbuf.bindless) {
|
||||
// The table's index: the game's handle moved into the replay's global id space with
|
||||
// the bases of the bound pools (an id of 0 stays 0, as RebaseHandleWord).
|
||||
const auto insert_point{IR::Block::InstructionList::s_iterator_to(*inst)};
|
||||
IR::IREmitter ir{*texture_inst.block, insert_point};
|
||||
const IR::U32 texture_id{ir.BitwiseAnd(cbuf.handle, ir.Imm32(0xFFFFF))};
|
||||
const IR::U32 sampler_id{ir.ShiftRightLogical(cbuf.handle, ir.Imm32(20))};
|
||||
const IR::U32 texture_base{
|
||||
ir.GetCbuf(ir.Imm32(0), ir.Imm32(BINDLESS_DRIVER_TEXTURE_BASE))};
|
||||
const IR::U32 sampler_base{
|
||||
ir.GetCbuf(ir.Imm32(0), ir.Imm32(BINDLESS_DRIVER_SAMPLER_BASE))};
|
||||
const IR::U32 zero{ir.Imm32(0)};
|
||||
const IR::U32 rebased_texture{ir.Select(ir.IEqual(texture_id, zero), zero,
|
||||
ir.IAdd(texture_id, texture_base))};
|
||||
const IR::U32 rebased_sampler{ir.Select(ir.IEqual(sampler_id, zero), zero,
|
||||
ir.IAdd(sampler_id, sampler_base))};
|
||||
inst->SetArg(0, ir.BitwiseOr(ir.BitwiseAnd(rebased_texture, ir.Imm32(0xFFFFF)),
|
||||
ir.ShiftLeftLogical(rebased_sampler, ir.Imm32(20))));
|
||||
} else if (count > 1) {
|
||||
const auto insert_point{IR::Block::InstructionList::s_iterator_to(*inst)};
|
||||
IR::IREmitter ir{*texture_inst.block, insert_point};
|
||||
const IR::U32 shift{ir.Imm32(size_shift)};
|
||||
|
||||
@@ -257,6 +257,9 @@ struct TextureDescriptor {
|
||||
/// Index in Info::handle_programs when the handle is computed at draw time (read from a
|
||||
/// storage buffer), ~0 when it is the constant buffer word above.
|
||||
u32 handle_program{~0U};
|
||||
/// The shader reads the handle at run time (bindless_table.h): the descriptor names the
|
||||
/// type's table of the replay's bindless set, not a texture of this draw.
|
||||
bool bindless{};
|
||||
|
||||
auto operator<=>(const TextureDescriptor&) const = default;
|
||||
};
|
||||
|
||||
@@ -328,6 +328,11 @@ TEST_CASE("NVN handle word classification", "[nvn][shaders]") {
|
||||
CHECK(ClassifyHandleWord(0, 0x80, T, &tables).source == S::Unsupported); // misaligned in table
|
||||
CHECK(ClassifyHandleWord(0, 0x94, I, &tables).source == S::ComputeImage);
|
||||
CHECK(ClassifyHandleWord(0, 0x94, I, &tables).slot == 1);
|
||||
// A handle is a pool index whichever table holds it: texture ops on an image-table word and
|
||||
// image ops on a texture-table word resolve to the table that holds the word.
|
||||
CHECK(ClassifyHandleWord(0, 0x94, T, &tables).source == S::ComputeImage);
|
||||
CHECK(ClassifyHandleWord(0, 0x94, T, &tables).slot == 1);
|
||||
CHECK(ClassifyHandleWord(0, 0x84, I, &tables).source == S::ComputeTexture);
|
||||
// Array elements: the next slot of a handle table, the next word of the same uniform buffer.
|
||||
CHECK(HandleElement({S::Combined, 2, HandleBank, 0x30}, 3) ==
|
||||
HandleRef{S::Combined, 5, HandleBank, 0x48});
|
||||
|
||||
@@ -265,6 +265,7 @@ add_library(video_core STATIC
|
||||
renderer_vulkan/nvn/nvn_draw_clear.cpp
|
||||
renderer_vulkan/nvn/nvn_draw_debug.cpp
|
||||
renderer_vulkan/nvn/nvn_draw_probe.cpp
|
||||
renderer_vulkan/nvn/nvn_bindless.cpp
|
||||
renderer_vulkan/nvn/nvn_draw_descriptors.cpp
|
||||
renderer_vulkan/nvn/nvn_draw_indirect.cpp
|
||||
renderer_vulkan/nvn/nvn_draw_log.cpp
|
||||
|
||||
@@ -304,6 +304,11 @@ DescriptorLayout BuildDescriptorLayout(std::span<const StageInfo> stages,
|
||||
|
||||
// 5. Textures.
|
||||
for (const Shader::TextureDescriptor& desc : info.texture_descriptors) {
|
||||
if (desc.bindless) {
|
||||
// In the table's set, not numbered with the draw's descriptors (as EmitSPIRV).
|
||||
layout.uses_bindless_table = true;
|
||||
continue;
|
||||
}
|
||||
auto entry = Make(binding, DescriptorKind::CombinedImageSampler,
|
||||
DescriptorRole::Texture, stage, desc.count);
|
||||
entry.type = desc.type;
|
||||
|
||||
@@ -140,6 +140,9 @@ struct DescriptorLayout {
|
||||
/// A compute program that must run 32 threads per subgroup (the compact tensor helper
|
||||
/// shuffles across the warp): its pipeline asks for a required subgroup size of 32.
|
||||
bool requires_guest_warp{};
|
||||
/// A stage samples through the replay's bindless table (set Shader::BINDLESS_SET): the
|
||||
/// pipeline layout carries that set and the draw binds it.
|
||||
bool uses_bindless_table{};
|
||||
u32 unsupported_count{};
|
||||
|
||||
/// True when the pipeline needs push constants (rescaling or render area).
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "common/common_types.h"
|
||||
#include "shader_recompiler/stage.h"
|
||||
|
||||
@@ -307,21 +309,23 @@ struct HandleClassification {
|
||||
};
|
||||
const bool in_texture = in_table(compute->texture_offset, compute->texture_count);
|
||||
const bool in_image = in_table(compute->image_offset, compute->image_count);
|
||||
if (want == HandleWant::Texture && in_texture) {
|
||||
if ((offset - compute->texture_offset) % HandleStride != 0) {
|
||||
// A handle word is an index into the pool whichever table holds it: a shader may sample
|
||||
// (texture op) a handle of its image table, or run image ops on one of its texture table.
|
||||
const bool texture_table = in_texture;
|
||||
if (in_texture || in_image) {
|
||||
const u32 base = texture_table ? compute->texture_offset : compute->image_offset;
|
||||
if ((offset - base) % HandleStride != 0) {
|
||||
return unsupported("misaligned compute handle offset");
|
||||
}
|
||||
return {HandleSource::ComputeTexture, (offset - compute->texture_offset) / HandleStride,
|
||||
return {texture_table ? HandleSource::ComputeTexture : HandleSource::ComputeImage,
|
||||
(offset - base) / HandleStride,
|
||||
{}};
|
||||
}
|
||||
if (want == HandleWant::Image && in_image) {
|
||||
if ((offset - compute->image_offset) % HandleStride != 0) {
|
||||
return unsupported("misaligned compute handle offset");
|
||||
}
|
||||
return {HandleSource::ComputeImage, (offset - compute->image_offset) / HandleStride,
|
||||
{}};
|
||||
}
|
||||
return unsupported("compute handle outside the texture/image tables");
|
||||
return unsupported(fmt::format(
|
||||
"compute handle outside the texture/image tables (textures {}x at {:#x}, images {}x at "
|
||||
"{:#x})",
|
||||
compute->texture_count, compute->texture_offset, compute->image_count,
|
||||
compute->image_offset));
|
||||
}
|
||||
if (bank == ProgramBank) {
|
||||
return unsupported("handle read from the program constant bank");
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace VideoCommon {
|
||||
|
||||
// VTX-HLE's disk caches: the NVN pipeline records and the driver's VkPipelineCache data. Bump
|
||||
// NVN_PIPELINE_CACHE_VERSION when the record format changes (nvn_pipeline_disk.h).
|
||||
inline constexpr u32 NVN_PIPELINE_CACHE_VERSION = 17;
|
||||
inline constexpr u32 NVN_PIPELINE_CACHE_VERSION = 18;
|
||||
inline constexpr u32 NVN_DRIVER_CACHE_VERSION = 1;
|
||||
|
||||
inline constexpr std::array<char, 8> NVN_PIPELINE_CACHE_MAGIC{'N', 'V', 'N', 'P',
|
||||
|
||||
@@ -0,0 +1,513 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Vortex727 (FUKKO)
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// The bindless texture table of the replay (shader_recompiler/bindless_table.h). A shader that
|
||||
// reads a texture handle at run time (a light or material record in a buffer the game fills)
|
||||
// indexes arrays of every texture and sampler the game registered in its pools. The arrays are
|
||||
// filled from those registrations, not guessed: entry `id` of an array is the image view of the
|
||||
// texture registered at pool slot `id`, made with the view the game described (target, format,
|
||||
// level and layer range), for the shader image type that target gives, and entry `id` of the
|
||||
// sampler array is the sampler registered at that slot.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
|
||||
#include "common/logging.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_bindless.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_draw.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_texture_layout.h"
|
||||
#include "video_core/surface.h"
|
||||
|
||||
namespace Vulkan::Nvn {
|
||||
namespace {
|
||||
|
||||
constexpr u32 TEXTURE_BINDINGS = Shader::BINDLESS_BINDING_COUNT - 1;
|
||||
|
||||
[[nodiscard]] Shader::TextureType TypeOfBinding(u32 binding) {
|
||||
return static_cast<Shader::TextureType>((binding - 1) % Shader::BINDLESS_TYPE_COUNT);
|
||||
}
|
||||
|
||||
[[nodiscard]] VkImageLayout ReadLayoutOf(const Storage& s) {
|
||||
const bool depth = s.host.depth || s.host.stencil;
|
||||
return depth && (s.usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) != 0
|
||||
? VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
|
||||
: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
}
|
||||
|
||||
/// The shader image types a texture registered with `target` is sampled as (what a shader declares
|
||||
/// for it): its own, and the one-layer array / array-less twin where the view allows it.
|
||||
[[nodiscard]] std::array<std::optional<Shader::TextureType>, 2> TypesOfTarget(s32 target) {
|
||||
using T = Shader::TextureType;
|
||||
switch (static_cast<TexTarget>(target)) {
|
||||
case TexTarget::Dim1D:
|
||||
return {T::Color1D, std::nullopt};
|
||||
case TexTarget::Dim1DArray:
|
||||
return {T::ColorArray1D, std::nullopt};
|
||||
case TexTarget::Dim2D:
|
||||
return {T::Color2D, T::ColorArray2D};
|
||||
case TexTarget::Dim2DArray:
|
||||
return {T::ColorArray2D, T::Color2D};
|
||||
case TexTarget::Rectangle:
|
||||
return {T::Color2DRect, T::Color2D};
|
||||
case TexTarget::Dim3D:
|
||||
return {T::Color3D, std::nullopt};
|
||||
case TexTarget::Cubemap:
|
||||
return {T::ColorCube, std::nullopt};
|
||||
case TexTarget::CubemapArray:
|
||||
return {T::ColorArrayCube, std::nullopt};
|
||||
default: // multisampled and buffer textures have no table
|
||||
return {std::nullopt, std::nullopt};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
VkImageViewType BindlessViewType(Shader::TextureType type) {
|
||||
switch (type) {
|
||||
case Shader::TextureType::Color1D:
|
||||
return VK_IMAGE_VIEW_TYPE_1D;
|
||||
case Shader::TextureType::ColorArray1D:
|
||||
return VK_IMAGE_VIEW_TYPE_1D_ARRAY;
|
||||
case Shader::TextureType::ColorArray2D:
|
||||
return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
||||
case Shader::TextureType::Color3D:
|
||||
return VK_IMAGE_VIEW_TYPE_3D;
|
||||
case Shader::TextureType::ColorCube:
|
||||
return VK_IMAGE_VIEW_TYPE_CUBE;
|
||||
case Shader::TextureType::ColorArrayCube:
|
||||
return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
|
||||
default:
|
||||
return VK_IMAGE_VIEW_TYPE_2D;
|
||||
}
|
||||
}
|
||||
|
||||
vk::DescriptorSetLayout MakeBindlessSetLayout(const Device& device) {
|
||||
constexpr VkShaderStageFlags stages = VK_SHADER_STAGE_ALL_GRAPHICS;
|
||||
std::array<VkDescriptorSetLayoutBinding, Shader::BINDLESS_BINDING_COUNT> bindings{};
|
||||
std::array<VkDescriptorBindingFlags, Shader::BINDLESS_BINDING_COUNT> flags{};
|
||||
constexpr VkDescriptorBindingFlags update_flags =
|
||||
VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
|
||||
VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT;
|
||||
bindings[0] = {
|
||||
.binding = Shader::BINDLESS_SAMPLER_BINDING,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
.descriptorCount = Shader::BINDLESS_SAMPLER_CAPACITY,
|
||||
.stageFlags = stages,
|
||||
.pImmutableSamplers = nullptr,
|
||||
};
|
||||
flags[0] = update_flags;
|
||||
for (u32 binding = 1; binding < Shader::BINDLESS_BINDING_COUNT; ++binding) {
|
||||
bindings[binding] = {
|
||||
.binding = binding,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
.descriptorCount = Shader::BINDLESS_TEXTURE_CAPACITY,
|
||||
.stageFlags = stages,
|
||||
.pImmutableSamplers = nullptr,
|
||||
};
|
||||
flags[binding] = update_flags;
|
||||
}
|
||||
const VkDescriptorSetLayoutBindingFlagsCreateInfo flags_ci{
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.bindingCount = static_cast<u32>(flags.size()),
|
||||
.pBindingFlags = flags.data(),
|
||||
};
|
||||
return device.GetLogical().CreateDescriptorSetLayout({
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
||||
.pNext = &flags_ci,
|
||||
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT,
|
||||
.bindingCount = static_cast<u32>(bindings.size()),
|
||||
.pBindings = bindings.data(),
|
||||
});
|
||||
}
|
||||
|
||||
bool DrawContext::BindlessReady() {
|
||||
BindlessState& state = bindless;
|
||||
if (state.set != VK_NULL_HANDLE) {
|
||||
return true;
|
||||
}
|
||||
if (state.failed) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const Device& device = impl.device;
|
||||
state.layout = MakeBindlessSetLayout(device);
|
||||
const std::array<VkDescriptorPoolSize, 2> sizes{{
|
||||
{VK_DESCRIPTOR_TYPE_SAMPLER, Shader::BINDLESS_SAMPLER_CAPACITY},
|
||||
{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
Shader::BINDLESS_TEXTURE_CAPACITY * TEXTURE_BINDINGS},
|
||||
}};
|
||||
state.pool = device.GetLogical().CreateDescriptorPool({
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT,
|
||||
.maxSets = 1,
|
||||
.poolSizeCount = static_cast<u32>(sizes.size()),
|
||||
.pPoolSizes = sizes.data(),
|
||||
});
|
||||
const VkDescriptorSetLayout layout_handle = *state.layout;
|
||||
const VkDescriptorSetAllocateInfo allocate{
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.descriptorPool = *state.pool,
|
||||
.descriptorSetCount = 1,
|
||||
.pSetLayouts = &layout_handle,
|
||||
};
|
||||
if (impl.dld.vkAllocateDescriptorSets(impl.logical, &allocate, &state.set) != VK_SUCCESS) {
|
||||
throw vk::Exception(VK_ERROR_OUT_OF_POOL_MEMORY);
|
||||
}
|
||||
// Every entry starts as the stand-in of its type, so a handle no registration names reads
|
||||
// black (partially bound arrays must not be read unwritten).
|
||||
std::vector<VkDescriptorImageInfo> infos(Shader::BINDLESS_TEXTURE_CAPACITY);
|
||||
std::vector<VkWriteDescriptorSet> writes;
|
||||
writes.reserve(Shader::BINDLESS_BINDING_COUNT);
|
||||
for (u32 binding = 1; binding < Shader::BINDLESS_BINDING_COUNT; ++binding) {
|
||||
const VkImageView dummy =
|
||||
resources.DummyView(BindlessViewType(TypeOfBinding(binding)));
|
||||
state.dummy[binding] = dummy;
|
||||
state.written[binding].assign(Shader::BINDLESS_TEXTURE_CAPACITY, dummy);
|
||||
}
|
||||
// One binding at a time: the infos are a copy of the binding's stand-in.
|
||||
for (u32 binding = 1; binding < Shader::BINDLESS_BINDING_COUNT; ++binding) {
|
||||
std::fill(infos.begin(), infos.end(),
|
||||
VkDescriptorImageInfo{VK_NULL_HANDLE, state.dummy[binding],
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL});
|
||||
const VkWriteDescriptorSet write{
|
||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||
.pNext = nullptr,
|
||||
.dstSet = state.set,
|
||||
.dstBinding = binding,
|
||||
.dstArrayElement = 0,
|
||||
.descriptorCount = Shader::BINDLESS_TEXTURE_CAPACITY,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
.pImageInfo = infos.data(),
|
||||
.pBufferInfo = nullptr,
|
||||
.pTexelBufferView = nullptr,
|
||||
};
|
||||
device.GetLogical().UpdateDescriptorSets(std::span{&write, 1}, {});
|
||||
}
|
||||
state.written_samplers.assign(Shader::BINDLESS_SAMPLER_CAPACITY,
|
||||
*resources.fallback_sampler);
|
||||
std::vector<VkDescriptorImageInfo> sampler_infos(
|
||||
Shader::BINDLESS_SAMPLER_CAPACITY,
|
||||
VkDescriptorImageInfo{*resources.fallback_sampler, VK_NULL_HANDLE,
|
||||
VK_IMAGE_LAYOUT_UNDEFINED});
|
||||
const VkWriteDescriptorSet sampler_write{
|
||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||
.pNext = nullptr,
|
||||
.dstSet = state.set,
|
||||
.dstBinding = Shader::BINDLESS_SAMPLER_BINDING,
|
||||
.dstArrayElement = 0,
|
||||
.descriptorCount = Shader::BINDLESS_SAMPLER_CAPACITY,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
.pImageInfo = sampler_infos.data(),
|
||||
.pBufferInfo = nullptr,
|
||||
.pTexelBufferView = nullptr,
|
||||
};
|
||||
device.GetLogical().UpdateDescriptorSets(std::span{&sampler_write, 1}, {});
|
||||
LOG_INFO(Render_Vulkan,
|
||||
"[nvn] bindless table created: {} texture arrays of {} entries and {} samplers",
|
||||
TEXTURE_BINDINGS, Shader::BINDLESS_TEXTURE_CAPACITY,
|
||||
Shader::BINDLESS_SAMPLER_CAPACITY);
|
||||
return true;
|
||||
} catch (const vk::Exception& exception) {
|
||||
LOG_ERROR(Render_Vulkan, "[nvn] bindless table creation failed: {}", exception.what());
|
||||
state.failed = true;
|
||||
state.set = VK_NULL_HANDLE;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawContext::BindlessSync(bool force) {
|
||||
BindlessState& state = bindless;
|
||||
const u64 seq = host->HostFlight().seq;
|
||||
const std::vector<u64>& texture_ids = submission->texture_pool_snapshot.Values();
|
||||
const std::vector<u64>& sampler_ids = submission->sampler_pool_snapshot.Values();
|
||||
// The registrations only change between submissions; what the table holds follows them (and
|
||||
// the images the replay created meanwhile) once per submission.
|
||||
if (!force && state.synced_seq == seq && state.synced_textures == texture_ids.data() &&
|
||||
state.synced_samplers == sampler_ids.data()) {
|
||||
return;
|
||||
}
|
||||
state.synced_seq = seq;
|
||||
state.synced_textures = texture_ids.data();
|
||||
state.synced_samplers = sampler_ids.data();
|
||||
|
||||
const u32 count = static_cast<u32>(
|
||||
std::min<size_t>(std::max(texture_ids.size(), size_t{state.synced_count}),
|
||||
Shader::BINDLESS_TEXTURE_CAPACITY));
|
||||
struct Run {
|
||||
u32 binding;
|
||||
u32 first_id;
|
||||
u32 first_info;
|
||||
u32 count;
|
||||
};
|
||||
std::vector<Run> runs;
|
||||
std::vector<VkDescriptorImageInfo> infos;
|
||||
std::vector<std::shared_ptr<Storage>> storages;
|
||||
std::array<VkImageView, TEXTURE_BINDINGS + 1> want{};
|
||||
std::array<VkImageLayout, TEXTURE_BINDINGS + 1> want_layout{};
|
||||
for (u32 id = 0; id < count; ++id) {
|
||||
for (u32 binding = 1; binding <= TEXTURE_BINDINGS; ++binding) {
|
||||
want[binding] = state.dummy[binding];
|
||||
want_layout[binding] = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
}
|
||||
// Pool slot 0 is "no texture" (the rebased handle keeps id 0 at 0).
|
||||
if (id != 0 && id < texture_ids.size() && texture_ids[id] != 0) {
|
||||
const std::shared_ptr<Texture>& texture = TextureOfId(id);
|
||||
if (texture && texture->storage) {
|
||||
Storage& s = *texture->storage;
|
||||
// Only what has an image holding the game's data: the table never creates one
|
||||
// (a texture the game registers and never uses would cost its memory).
|
||||
if (s.image && !s.image_failed && s.contents_valid &&
|
||||
s.samples == VK_SAMPLE_COUNT_1_BIT) {
|
||||
const s32 target = texture->desc.view.set_target ? texture->desc.view.target
|
||||
: texture->desc.target;
|
||||
const bool is_integer =
|
||||
VideoCore::Surface::IsPixelFormatInteger(texture->host.pixel_format);
|
||||
bool any = false;
|
||||
for (const std::optional<Shader::TextureType>& type : TypesOfTarget(target)) {
|
||||
if (!type) {
|
||||
continue;
|
||||
}
|
||||
ViewKey key{};
|
||||
if (!ViewKeyOf(*texture, s, *type, false, key)) {
|
||||
continue;
|
||||
}
|
||||
const VkImageView view = resources.GetView(*texture, key);
|
||||
if (!view) {
|
||||
continue;
|
||||
}
|
||||
const VkImageLayout layout = ReadLayoutOf(s);
|
||||
for (const u32 klass : {0u, 1u, 2u}) {
|
||||
// Float and depth-compare samplers share the views; an integer
|
||||
// sampler takes only an integer format.
|
||||
if ((klass == 1) != is_integer) {
|
||||
continue;
|
||||
}
|
||||
const u32 binding = Shader::BindlessTextureBinding(*type, klass);
|
||||
want[binding] = view;
|
||||
want_layout[binding] = layout;
|
||||
}
|
||||
any = true;
|
||||
}
|
||||
if (any) {
|
||||
storages.push_back(texture->storage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (u32 binding = 1; binding <= TEXTURE_BINDINGS; ++binding) {
|
||||
if (state.written[binding][id] == want[binding]) {
|
||||
continue;
|
||||
}
|
||||
state.written[binding][id] = want[binding];
|
||||
infos.push_back({VK_NULL_HANDLE, want[binding], want_layout[binding]});
|
||||
runs.push_back({binding, id, static_cast<u32>(infos.size() - 1), 1});
|
||||
}
|
||||
}
|
||||
// Merge consecutive entries of one binding into one write each.
|
||||
std::stable_sort(runs.begin(), runs.end(), [](const Run& a, const Run& b) {
|
||||
return a.binding != b.binding ? a.binding < b.binding : a.first_id < b.first_id;
|
||||
});
|
||||
std::vector<VkDescriptorImageInfo> sorted;
|
||||
sorted.reserve(infos.size());
|
||||
std::vector<VkWriteDescriptorSet> writes;
|
||||
std::vector<Run> merged;
|
||||
for (const Run& run : runs) {
|
||||
if (!merged.empty() && merged.back().binding == run.binding &&
|
||||
merged.back().first_id + merged.back().count == run.first_id) {
|
||||
++merged.back().count;
|
||||
} else {
|
||||
merged.push_back({run.binding, run.first_id, static_cast<u32>(sorted.size()), 1});
|
||||
}
|
||||
sorted.push_back(infos[run.first_info]);
|
||||
}
|
||||
for (const Run& run : merged) {
|
||||
writes.push_back({
|
||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||
.pNext = nullptr,
|
||||
.dstSet = state.set,
|
||||
.dstBinding = run.binding,
|
||||
.dstArrayElement = run.first_id,
|
||||
.descriptorCount = run.count,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
.pImageInfo = sorted.data() + run.first_info,
|
||||
.pBufferInfo = nullptr,
|
||||
.pTexelBufferView = nullptr,
|
||||
});
|
||||
}
|
||||
state.synced_count = static_cast<u32>(std::min<size_t>(texture_ids.size(),
|
||||
Shader::BINDLESS_TEXTURE_CAPACITY));
|
||||
state.storages = std::move(storages);
|
||||
if (state.sync_logs++ % 600 == 0) {
|
||||
u32 registered = 0;
|
||||
for (u32 id = 1; id < texture_ids.size() && id < Shader::BINDLESS_TEXTURE_CAPACITY; ++id) {
|
||||
registered += texture_ids[id] != 0;
|
||||
}
|
||||
u32 cube = 0;
|
||||
u32 d2 = 0;
|
||||
for (u32 id = 0; id < state.synced_count; ++id) {
|
||||
const u32 cube_binding = Shader::BindlessTextureBinding(Shader::TextureType::ColorCube, 0);
|
||||
const u32 d2_binding = Shader::BindlessTextureBinding(Shader::TextureType::Color2D, 0);
|
||||
cube += state.written[cube_binding][id] != state.dummy[cube_binding];
|
||||
d2 += state.written[d2_binding][id] != state.dummy[d2_binding];
|
||||
}
|
||||
LOG_INFO(Render_Vulkan,
|
||||
"[nvn] bindless table: {} textures registered, {} 2D and {} cube entries hold an "
|
||||
"image ({} created for it so far), {} samplers registered, {} writes",
|
||||
registered, d2, cube, state.primed_total,
|
||||
static_cast<u32>(std::count_if(sampler_ids.begin(), sampler_ids.end(),
|
||||
[](u64 serial) { return serial != 0; })),
|
||||
writes.size());
|
||||
}
|
||||
|
||||
// Samplers: entry `id` is the sampler the game registered at that slot.
|
||||
const u32 sampler_count = static_cast<u32>(
|
||||
std::min<size_t>(std::max(sampler_ids.size(), size_t{state.synced_sampler_count}),
|
||||
Shader::BINDLESS_SAMPLER_CAPACITY));
|
||||
std::vector<VkDescriptorImageInfo> sampler_infos;
|
||||
std::vector<std::pair<u32, u32>> sampler_runs; // first id, info index
|
||||
for (u32 id = 0; id < sampler_count; ++id) {
|
||||
VkSampler sampler = *resources.fallback_sampler;
|
||||
if (id != 0 && id < sampler_ids.size() && sampler_ids[id] != 0) {
|
||||
sampler = resources.GetSampler(sampler_ids[id], false);
|
||||
}
|
||||
if (state.written_samplers[id] == sampler) {
|
||||
continue;
|
||||
}
|
||||
state.written_samplers[id] = sampler;
|
||||
sampler_infos.push_back({sampler, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED});
|
||||
sampler_runs.emplace_back(id, static_cast<u32>(sampler_infos.size() - 1));
|
||||
}
|
||||
std::vector<std::pair<u32, u32>> sampler_writes; // first id, count
|
||||
for (size_t i = 0; i < sampler_runs.size(); ++i) {
|
||||
if (!sampler_writes.empty() &&
|
||||
sampler_writes.back().first + sampler_writes.back().second == sampler_runs[i].first) {
|
||||
++sampler_writes.back().second;
|
||||
} else {
|
||||
sampler_writes.emplace_back(sampler_runs[i].first, 1u);
|
||||
}
|
||||
}
|
||||
size_t info_index = 0;
|
||||
for (const auto& [first, run_count] : sampler_writes) {
|
||||
writes.push_back({
|
||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||
.pNext = nullptr,
|
||||
.dstSet = state.set,
|
||||
.dstBinding = Shader::BINDLESS_SAMPLER_BINDING,
|
||||
.dstArrayElement = first,
|
||||
.descriptorCount = run_count,
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
.pImageInfo = sampler_infos.data() + info_index,
|
||||
.pBufferInfo = nullptr,
|
||||
.pTexelBufferView = nullptr,
|
||||
});
|
||||
info_index += run_count;
|
||||
}
|
||||
state.synced_sampler_count = static_cast<u32>(
|
||||
std::min<size_t>(sampler_ids.size(), Shader::BINDLESS_SAMPLER_CAPACITY));
|
||||
if (!writes.empty()) {
|
||||
impl.device.GetLogical().UpdateDescriptorSets(writes, {});
|
||||
}
|
||||
}
|
||||
|
||||
void DrawContext::BindlessPrime(DrawPlan& plan) {
|
||||
BindlessState& state = bindless;
|
||||
// What a submission may create: the budget is memory and a count (a create is a pass break
|
||||
// and an upload), so a pool of thousands of textures fills over a few frames, not at once.
|
||||
static const u64 budget_bytes = [] {
|
||||
const char* text = std::getenv("EDEN_NX2_NVN_BINDLESS_PRIME_MB");
|
||||
return (text ? std::strtoull(text, nullptr, 10) : 64ull) << 20;
|
||||
}();
|
||||
constexpr u32 BUDGET_COUNT = 96;
|
||||
const u64 seq = host->HostFlight().seq;
|
||||
if (state.prime_seq != seq) {
|
||||
state.prime_seq = seq;
|
||||
state.prime_bytes = 0;
|
||||
state.prime_count = 0;
|
||||
}
|
||||
if (budget_bytes == 0 || state.prime_count >= BUDGET_COUNT || state.prime_bytes >= budget_bytes) {
|
||||
return;
|
||||
}
|
||||
const std::vector<u64>& texture_ids = submission->texture_pool_snapshot.Values();
|
||||
const u32 count = static_cast<u32>(
|
||||
std::min<size_t>(texture_ids.size(), Shader::BINDLESS_TEXTURE_CAPACITY));
|
||||
if (count < 2) {
|
||||
return;
|
||||
}
|
||||
u32 id = state.prime_cursor < count ? state.prime_cursor : 1;
|
||||
for (u32 visited = 1; visited < count; ++visited, id = id + 1 < count ? id + 1 : 1) {
|
||||
if (texture_ids[id] == 0) {
|
||||
continue;
|
||||
}
|
||||
const std::shared_ptr<Texture>& texture = TextureOfId(id);
|
||||
if (!texture || !texture->storage) {
|
||||
continue;
|
||||
}
|
||||
Storage& s = *texture->storage;
|
||||
// Plain sampled textures only: what the game made as a render target gets its image and
|
||||
// contents from rendering, and one without guest data (image failed) is not retried.
|
||||
const VkImageUsageFlags attachment = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
|
||||
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
||||
if (s.image || s.image_failed || (s.usage & attachment) != 0 ||
|
||||
s.samples != VK_SAMPLE_COUNT_1_BIT || s.scale_owner != nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (state.prime_bytes + s.vram_bytes > budget_bytes && state.prime_count != 0) {
|
||||
continue;
|
||||
}
|
||||
plan.UseImage(texture->storage, ReadLayoutOf(s), true);
|
||||
state.prime_bytes += s.vram_bytes;
|
||||
++state.primed_total;
|
||||
state.dirty = true;
|
||||
if (++state.prime_count >= BUDGET_COUNT || state.prime_bytes >= budget_bytes) {
|
||||
id = id + 1 < count ? id + 1 : 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
state.prime_cursor = id;
|
||||
}
|
||||
|
||||
bool DrawContext::BindlessPrepare(DrawPlan& plan) {
|
||||
if (!BindlessReady()) {
|
||||
return false;
|
||||
}
|
||||
BindlessSync();
|
||||
BindlessPrime(plan);
|
||||
// A shader may read any entry: the images of the table must be in the layout the descriptors
|
||||
// name. Those a render pass or a copy left in another layout are moved like any sampled image
|
||||
// (a render target of this very draw stays: it cannot be both).
|
||||
for (const std::shared_ptr<Storage>& storage : bindless.storages) {
|
||||
Storage& s = *storage;
|
||||
if (!s.image) {
|
||||
continue;
|
||||
}
|
||||
const VkImageLayout wanted = ReadLayoutOf(s);
|
||||
if (s.current_layout == wanted || IsBoundTarget(&s)) {
|
||||
continue;
|
||||
}
|
||||
plan.UseImage(storage, wanted, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DrawContext::BindlessBind(PipelineBase& pipe, VkPipelineBindPoint bind_point) {
|
||||
if (!pipe.layout || bindless.set == VK_NULL_HANDLE) {
|
||||
return;
|
||||
}
|
||||
if (bindless.dirty) {
|
||||
// The plan created images before this draw recorded: their entries are written before
|
||||
// the set is bound (update after bind), so this very draw reads them.
|
||||
bindless.dirty = false;
|
||||
BindlessSync(true);
|
||||
}
|
||||
const VkDescriptorSet set = bindless.set;
|
||||
host->HostCmd().BindDescriptorSets(bind_point, *pipe.layout, Shader::BINDLESS_SET,
|
||||
std::span{&set, 1}, {});
|
||||
}
|
||||
|
||||
} // namespace Vulkan::Nvn
|
||||
@@ -0,0 +1,22 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2026 Vortex727 (FUKKO)
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "video_core/vulkan_common/vulkan_device.h"
|
||||
#include "video_core/vulkan_common/vulkan_wrapper.h"
|
||||
|
||||
namespace Vulkan::Nvn {
|
||||
|
||||
/// The set layout of the bindless table (shader_recompiler/bindless_table.h): a sampler array at
|
||||
/// binding 0 and a sampled image array per shader image type and class, each updatable after
|
||||
/// binding and partially bound. Every graphics pipeline that samples through the table has a
|
||||
/// layout holding it as set BINDLESS_SET; the replay's own descriptor set is made from the same
|
||||
/// definition.
|
||||
[[nodiscard]] vk::DescriptorSetLayout MakeBindlessSetLayout(const Device& device);
|
||||
|
||||
/// The view type a table binding's images are made as.
|
||||
[[nodiscard]] VkImageViewType BindlessViewType(Shader::TextureType type);
|
||||
|
||||
} // namespace Vulkan::Nvn
|
||||
@@ -2556,6 +2556,11 @@ bool DrawContext::PrepareDraw(u32 mode, u32 element_count, const IndexRange* ind
|
||||
++counters.draws_skipped;
|
||||
return false;
|
||||
}
|
||||
if (active->descriptors.uses_bindless_table && !BindlessPrepare(plan)) {
|
||||
++counters.draws_skipped;
|
||||
host->HostUnsupported("Draw", "the bindless texture table cannot be created");
|
||||
return false;
|
||||
}
|
||||
// Vertex layout of this draw (rebuilt only when the words or the pipeline's attribute reads
|
||||
// changed) and the streams whose buffers it binds. The streams are exactly the bindings the
|
||||
// pipeline (static) or the dynamic vertex input (below) declares. Task/mesh pipelines have
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_barrier_track.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_debug.h"
|
||||
#include "video_core/nvn/record.h"
|
||||
@@ -728,6 +729,47 @@ private:
|
||||
// ---- descriptors (nvn_draw_descriptors.cpp) ----
|
||||
bool BuildDescriptors(PipelineBase& pipeline, DrawPlan& plan, bool graphics);
|
||||
bool PrepareOutsidePass(DrawPlan& plan);
|
||||
// ---- bindless texture table (nvn_bindless.cpp) ----
|
||||
/// The replay's table of every texture and sampler the game registered, for shaders that
|
||||
/// read a handle at run time (shader_recompiler/bindless_table.h).
|
||||
struct BindlessState {
|
||||
bool failed{false};
|
||||
vk::DescriptorSetLayout layout;
|
||||
vk::DescriptorPool pool;
|
||||
VkDescriptorSet set{VK_NULL_HANDLE};
|
||||
/// The stand-in of each texture array and what each entry holds now.
|
||||
std::array<VkImageView, Shader::BINDLESS_BINDING_COUNT> dummy{};
|
||||
std::array<std::vector<VkImageView>, Shader::BINDLESS_BINDING_COUNT> written;
|
||||
std::vector<VkSampler> written_samplers;
|
||||
/// The storages of the entries that hold an image (their layouts are checked per draw).
|
||||
std::vector<std::shared_ptr<Storage>> storages;
|
||||
u64 synced_seq{~u64{0}};
|
||||
const void* synced_textures{};
|
||||
const void* synced_samplers{};
|
||||
u32 synced_count{};
|
||||
u32 synced_sampler_count{};
|
||||
u32 sync_logs{};
|
||||
/// Textures registered without an image yet are created (and their data loaded) as draws
|
||||
/// need the table, within a budget per submission; a change makes the entries refresh
|
||||
/// before the set is bound.
|
||||
bool dirty{false};
|
||||
u64 prime_seq{~u64{0}};
|
||||
u64 prime_bytes{};
|
||||
u32 prime_count{};
|
||||
u32 prime_cursor{1};
|
||||
u32 primed_total{};
|
||||
} bindless;
|
||||
/// Creates the table on first use; false when the device cannot hold it.
|
||||
bool BindlessReady();
|
||||
/// Brings the table up to the submission's pool registrations (once per submission).
|
||||
void BindlessSync(bool force = false);
|
||||
/// Adds to `plan` the registered textures that have no image yet, up to the submission's
|
||||
/// budget (creating one needs a pass break, and its memory counts).
|
||||
void BindlessPrime(DrawPlan& plan);
|
||||
/// Syncs the table and adds to `plan` the layout changes its images need.
|
||||
bool BindlessPrepare(DrawPlan& plan);
|
||||
/// Binds the table's set for the pipeline's layout.
|
||||
void BindlessBind(PipelineBase& pipeline, VkPipelineBindPoint bind_point);
|
||||
/// Phase B of the descriptors: creates the image views the plan resolved and patches the
|
||||
/// image infos (the images exist and are in layout by now).
|
||||
void FinishImages(DrawPlan& plan);
|
||||
@@ -998,6 +1040,7 @@ private:
|
||||
u64 e{};
|
||||
u64 f{}; ///< the raw coverage ring written into the bank
|
||||
u64 g{}; ///< the read-only mirror table written into the bank (generation + 1, 0: none)
|
||||
u64 h{}; ///< the bases of the bound texture and sampler pools (bindless_table.h)
|
||||
BufferRef ref;
|
||||
};
|
||||
std::array<BankCache, BIND_STAGES> driver_banks;
|
||||
|
||||
@@ -630,7 +630,8 @@ namespace {
|
||||
/// Fills the c0 driver bank of a graphics stage. `mesh_dims`: the workgroup counts of the last
|
||||
/// 3D mesh-task draw, which the draw macro leaves in the bank (VC::DriverMeshDimsOffset).
|
||||
void FillDriverBank(u32 stage, const StageBindings& bindings, std::span<const u8> push_constants,
|
||||
const std::array<u32, 3>& mesh_dims, u8* out) {
|
||||
const std::array<u32, 3>& mesh_dims, u32 texture_pool_base,
|
||||
u32 sampler_pool_base, u8* out) {
|
||||
std::memset(out, 0, DRIVER_BANK_SIZE);
|
||||
if (stage < VC::ShaderStageCount) {
|
||||
const u32 base = VC::StorageBufferBase(static_cast<VC::ShaderStage>(stage));
|
||||
@@ -652,6 +653,11 @@ void FillDriverBank(u32 stage, const StageBindings& bindings, std::span<const u8
|
||||
const float largest_point_size = 2047.0f;
|
||||
std::memcpy(out + 0x610, &largest_point_size, sizeof(largest_point_size));
|
||||
std::memcpy(out + VC::DriverMeshDimsOffset, mesh_dims.data(), sizeof(mesh_dims));
|
||||
// The bases a shader adds to the handles it reads from memory (bindless_table.h).
|
||||
std::memcpy(out + Shader::BINDLESS_DRIVER_TEXTURE_BASE, &texture_pool_base,
|
||||
sizeof(texture_pool_base));
|
||||
std::memcpy(out + Shader::BINDLESS_DRIVER_SAMPLER_BASE, &sampler_pool_base,
|
||||
sizeof(sampler_pool_base));
|
||||
}
|
||||
|
||||
/// Fills the c2 handle tables.
|
||||
@@ -763,7 +769,9 @@ bool DrawContext::ViewKeyOf(const Texture& texture, const Storage& s, Shader::Te
|
||||
key.aspect = want_stencil ? VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT;
|
||||
}
|
||||
key.base_level = (std::min)(level_base, s.levels - 1);
|
||||
key.levels = texture.desc.view.set_levels
|
||||
// A view cannot address zero levels or layers: a count of 0 (SetLevels / SetLayers with no
|
||||
// range) is the full range from the base on.
|
||||
key.levels = texture.desc.view.set_levels && texture.desc.view.nlevels != 0
|
||||
? (std::max<u32>)(1u, (std::min<u32>)(texture.desc.view.nlevels,
|
||||
s.levels - key.base_level))
|
||||
: s.levels - key.base_level;
|
||||
@@ -774,7 +782,9 @@ bool DrawContext::ViewKeyOf(const Texture& texture, const Storage& s, Shader::Te
|
||||
(s.create_flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT) != 0;
|
||||
const u32 total_layers =
|
||||
s.type == VK_IMAGE_TYPE_3D ? (std::max)(s.extent.depth >> key.base_level, 1u) : s.layers;
|
||||
const u32 layer_total = texture.desc.view.set_layers ? texture.desc.view.nlayers : total_layers;
|
||||
const u32 layer_total = texture.desc.view.set_layers && texture.desc.view.nlayers != 0
|
||||
? texture.desc.view.nlayers
|
||||
: total_layers;
|
||||
key.base_layer = (std::min)(layer_base, total_layers - 1);
|
||||
const u32 layer_count = (std::max)(1u, (std::min)(layer_total, total_layers - key.base_layer));
|
||||
// A storage image view must have the identity swizzle (0 = IDENTITY). A BC4/BC5 stand-in holds
|
||||
@@ -1221,16 +1231,18 @@ bool DrawContext::BuildDescriptors(PipelineBase& pipe, DrawPlan& plan, bool grap
|
||||
BankCache& cache = driver_banks[stage];
|
||||
const u64 seq = host->HostFlight().seq;
|
||||
const u64 raw_generation = raw_table ? raw_table->generation + 1 : 0;
|
||||
const u64 pool_bases = u64{texture_pool_base} << 32 | sampler_pool_base;
|
||||
const void* raw_device = raw_table ? raw_table->device.get() : nullptr;
|
||||
if (cache.seq != seq || cache.a != storage_gen[stage] ||
|
||||
cache.b != push_gen[0] || cache.c != raw_generation ||
|
||||
cache.d != raw_device || cache.e != mesh_dims_gen ||
|
||||
cache.f != coverage_ring || cache.g != alias_generation) {
|
||||
cache.f != coverage_ring || cache.g != alias_generation ||
|
||||
cache.h != pool_bases) {
|
||||
u8* pointer = nullptr;
|
||||
cache.ref = AllocArena(DRIVER_BANK_SIZE, 256, &pointer);
|
||||
++counters.driver_bank_uploads;
|
||||
FillDriverBank(stage, stages[stage], push_constants[0], mesh_dims,
|
||||
pointer);
|
||||
texture_pool_base, sampler_pool_base, pointer);
|
||||
if (raw_table != nullptr) {
|
||||
WriteRawGlobalTable(raw_table->device.get(), coverage_ring,
|
||||
std::span<u8>{pointer, DRIVER_BANK_SIZE},
|
||||
@@ -1244,6 +1256,7 @@ bool DrawContext::BuildDescriptors(PipelineBase& pipe, DrawPlan& plan, bool grap
|
||||
cache.e = mesh_dims_gen;
|
||||
cache.f = coverage_ring;
|
||||
cache.g = alias_generation;
|
||||
cache.h = pool_bases;
|
||||
}
|
||||
ref = cache.ref;
|
||||
}
|
||||
@@ -2143,6 +2156,9 @@ void DrawContext::FinishImages(DrawPlan& plan) {
|
||||
bool DrawContext::PushPlan(PipelineBase& pipe, DrawPlan& plan, VkPipelineBindPoint bind_point) {
|
||||
FinishImages(plan);
|
||||
vk::CommandBuffer cmd = host->HostCmd();
|
||||
if (pipe.descriptors.uses_bindless_table) {
|
||||
BindlessBind(pipe, bind_point);
|
||||
}
|
||||
if (cs_mode == CostSplit::Mode::NoBind && bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS &&
|
||||
pipe.layout) [[unlikely]] {
|
||||
// The pass's first class draw pushed for this layout: its descriptors and constants stay.
|
||||
|
||||
@@ -1101,6 +1101,9 @@ bool PipelineCache::Impl::Translate(const TranslateInput& in, TranslateOutput& o
|
||||
record.layout.uses_raw_global_memory = programs[stage].info.uses_raw_global_memory;
|
||||
record.layout.stores_raw_global_memory = programs[stage].info.stores_raw_global_memory;
|
||||
record.layout.raw_stores_written_through = VC::RawStoresWrittenThrough(programs[stage].info);
|
||||
record.layout.uses_bindless_table = std::ranges::any_of(
|
||||
programs[stage].info.texture_descriptors,
|
||||
[](const Shader::TextureDescriptor& desc) { return desc.bindless; });
|
||||
record.layout.requires_guest_warp =
|
||||
is_compute && tensor_lowering == VideoCommon::NativeTensorLowering::Compact &&
|
||||
programs[stage].info.UsesWarpOps();
|
||||
|
||||
@@ -307,6 +307,8 @@ struct PipelineBase {
|
||||
vk::Pipeline pipeline;
|
||||
vk::PipelineLayout layout;
|
||||
vk::DescriptorSetLayout set_layout; ///< null when the pipeline has no descriptors
|
||||
/// Set BINDLESS_SET of the layout when a stage samples through the bindless table.
|
||||
vk::DescriptorSetLayout bindless_layout;
|
||||
/// The descriptor set is pushed with vkCmdPushDescriptorSetKHR; otherwise it is allocated.
|
||||
bool push_descriptors{};
|
||||
/// Descriptor bindings of every stage in pipeline order, numbered as in the SPIR-V.
|
||||
|
||||
@@ -179,7 +179,8 @@ void WriteLayout(Writer& w, const VC::DescriptorLayout& layout) {
|
||||
// 4: raw_stores_written_through (records from before it read 0: the stores are not).
|
||||
// 8: requires_guest_warp (records from before it read 0).
|
||||
w.U8((layout.uses_raw_global_memory ? 1 : 0) | (layout.stores_raw_global_memory ? 2 : 0) |
|
||||
(layout.raw_stores_written_through ? 4 : 0) | (layout.requires_guest_warp ? 8 : 0));
|
||||
(layout.raw_stores_written_through ? 4 : 0) | (layout.requires_guest_warp ? 8 : 0) |
|
||||
(layout.uses_bindless_table ? 16 : 0));
|
||||
w.U32(layout.unsupported_count);
|
||||
w.U32(static_cast<u32>(layout.bindings.size()));
|
||||
for (const VC::DescriptorBinding& b : layout.bindings) {
|
||||
@@ -218,6 +219,7 @@ bool ReadLayout(Reader& r, VC::DescriptorLayout& out) {
|
||||
out.stores_raw_global_memory = (raw_global & 2) != 0;
|
||||
out.raw_stores_written_through = (raw_global & 4) != 0;
|
||||
out.requires_guest_warp = (raw_global & 8) != 0;
|
||||
out.uses_bindless_table = (raw_global & 16) != 0;
|
||||
out.unsupported_count = r.U32();
|
||||
const u32 count = r.U32();
|
||||
// Every binding takes at least 78 bytes: a count the input cannot hold is garbage.
|
||||
|
||||
@@ -371,6 +371,8 @@ struct PipelineCache::Impl {
|
||||
// --- Vulkan objects -----------------------------------------------------------------------
|
||||
struct LayoutObjects {
|
||||
vk::DescriptorSetLayout set_layout;
|
||||
/// Set BINDLESS_SET when a stage samples through the table (nvn_bindless.h).
|
||||
vk::DescriptorSetLayout bindless_layout;
|
||||
vk::PipelineLayout layout;
|
||||
bool push_descriptors{};
|
||||
u32 push_constant_size{};
|
||||
|
||||
@@ -973,6 +973,7 @@ Shader::HostTranslateInfo MakeHostInfo(const Device& device, const Shader::Profi
|
||||
.support_geometry_shader_passthrough = device.IsNvGeometryShaderPassthroughSupported(),
|
||||
.support_conditional_barrier = device.SupportsConditionalBarriers(),
|
||||
.single_lane_geometry_subgroups = !profile.SupportsSubgroupStage(Shader::Stage::Geometry),
|
||||
.nvn_bindless_table = device.IsBindlessTableSupported(),
|
||||
.nvn_raw_global_memory = profile.support_raw_global_memory,
|
||||
.raw_coverage = profile.support_raw_global_memory && VideoCommon::RawCoverageEnabled(),
|
||||
.raw_global_last_hit =
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_bindless.h"
|
||||
#include "video_core/renderer_vulkan/nvn/nvn_pipeline_impl.h"
|
||||
|
||||
namespace Vulkan::Nvn {
|
||||
@@ -203,6 +204,19 @@ PipelineCache::Impl::LayoutObjects PipelineCache::Impl::CreateLayoutObjects(
|
||||
.pBindings = bindings.data(),
|
||||
});
|
||||
}
|
||||
if (descriptors.uses_bindless_table) {
|
||||
// The table is set BINDLESS_SET: set 0 must exist even when the draw has no descriptors.
|
||||
if (bindings.empty()) {
|
||||
out.set_layout = device.GetLogical().CreateDescriptorSetLayout({
|
||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
.bindingCount = 0,
|
||||
.pBindings = nullptr,
|
||||
});
|
||||
}
|
||||
out.bindless_layout = MakeBindlessSetLayout(device);
|
||||
}
|
||||
VkPushConstantRange range{
|
||||
.stageFlags = push_stages,
|
||||
.offset = 0,
|
||||
@@ -215,13 +229,15 @@ PipelineCache::Impl::LayoutObjects PipelineCache::Impl::CreateLayoutObjects(
|
||||
static_cast<u32>(sizeof(RenderAreaLayout));
|
||||
out.push_constant_size = range.size;
|
||||
}
|
||||
const VkDescriptorSetLayout set_handle = *out.set_layout;
|
||||
const std::array<VkDescriptorSetLayout, 2> set_handles{
|
||||
*out.set_layout, out.bindless_layout ? *out.bindless_layout : VK_NULL_HANDLE};
|
||||
const u32 set_count = out.bindless_layout ? 2u : (set_handles[0] != VK_NULL_HANDLE ? 1u : 0u);
|
||||
out.layout = device.GetLogical().CreatePipelineLayout({
|
||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
.setLayoutCount = set_handle != VK_NULL_HANDLE ? 1u : 0u,
|
||||
.pSetLayouts = set_handle != VK_NULL_HANDLE ? &set_handle : nullptr,
|
||||
.setLayoutCount = set_count,
|
||||
.pSetLayouts = set_count != 0 ? set_handles.data() : nullptr,
|
||||
.pushConstantRangeCount = range.size != 0 ? 1u : 0u,
|
||||
.pPushConstantRanges = range.size != 0 ? &range : nullptr,
|
||||
});
|
||||
@@ -243,6 +259,7 @@ VC::DescriptorLayout PipelineCache::Impl::MergeLayouts(
|
||||
merged.uses_raw_global_memory |= slice.uses_raw_global_memory;
|
||||
merged.stores_raw_global_memory |= slice.stores_raw_global_memory;
|
||||
merged.requires_guest_warp |= slice.requires_guest_warp;
|
||||
merged.uses_bindless_table |= slice.uses_bindless_table;
|
||||
raw_stores_not_through |=
|
||||
slice.stores_raw_global_memory && !slice.raw_stores_written_through;
|
||||
merged.unsupported_count += slice.unsupported_count;
|
||||
@@ -251,9 +268,16 @@ VC::DescriptorLayout PipelineCache::Impl::MergeLayouts(
|
||||
merged.raw_stores_written_through = merged.stores_raw_global_memory && !raw_stores_not_through;
|
||||
if (!merged.AllSupported()) {
|
||||
std::set<std::string> issues;
|
||||
for (const VC::DescriptorBinding& binding : merged.bindings) {
|
||||
if (!binding.supported) {
|
||||
issues.insert(binding.issue);
|
||||
for (const StageArtifact* stage : stages) {
|
||||
if (stage == nullptr) {
|
||||
continue;
|
||||
}
|
||||
for (const VC::DescriptorBinding& binding : stage->record->layout.bindings) {
|
||||
if (!binding.supported) {
|
||||
issues.insert(fmt::format("{} [{} program {:016x}]", binding.issue,
|
||||
StageNameOf(stage->record->stage),
|
||||
stage->record->program_hash));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const std::string& issue : issues) {
|
||||
@@ -424,6 +448,7 @@ std::shared_ptr<GraphicsPipeline> PipelineCache::Impl::AssembleGraphics(
|
||||
}
|
||||
pipeline->layout = std::move(layout.layout);
|
||||
pipeline->set_layout = std::move(layout.set_layout);
|
||||
pipeline->bindless_layout = std::move(layout.bindless_layout);
|
||||
pipeline->push_descriptors = layout.push_descriptors;
|
||||
pipeline->push_constant_size = layout.push_constant_size;
|
||||
pipeline->push_constant_stages = push_stages;
|
||||
@@ -489,6 +514,7 @@ std::shared_ptr<ComputePipeline> PipelineCache::Impl::AssembleCompute(
|
||||
*vk_cache);
|
||||
pipeline->layout = std::move(layout.layout);
|
||||
pipeline->set_layout = std::move(layout.set_layout);
|
||||
pipeline->bindless_layout = std::move(layout.bindless_layout);
|
||||
pipeline->push_descriptors = layout.push_descriptors;
|
||||
pipeline->push_constant_size = layout.push_constant_size;
|
||||
pipeline->push_constant_stages = VK_SHADER_STAGE_COMPUTE_BIT;
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <ranges>
|
||||
#include "common/settings.h"
|
||||
#include "common/settings_enums.h"
|
||||
#include "shader_recompiler/bindless_table.h"
|
||||
#include "video_core/vulkan_common/nsight_aftermath_tracker.h"
|
||||
#include "video_core/vulkan_common/vma.h"
|
||||
#include "video_core/vulkan_common/vulkan_device.h"
|
||||
@@ -734,12 +735,17 @@ Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR
|
||||
descriptor_indexing.shaderStorageBufferArrayNonUniformIndexing = false;
|
||||
descriptor_indexing.shaderInputAttachmentArrayNonUniformIndexing = false;
|
||||
descriptor_indexing.descriptorBindingUniformBufferUpdateAfterBind = false;
|
||||
descriptor_indexing.descriptorBindingSampledImageUpdateAfterBind = false;
|
||||
// VTX-HLE's bindless texture table is updated while earlier frames may still be in
|
||||
// flight (bindless_table.h); nothing else uses the update-after-bind bits.
|
||||
const bool bindless_table = Settings::ResolveNvnHle().hle;
|
||||
descriptor_indexing.descriptorBindingSampledImageUpdateAfterBind =
|
||||
bindless_table && descriptor_indexing.descriptorBindingSampledImageUpdateAfterBind;
|
||||
descriptor_indexing.descriptorBindingStorageImageUpdateAfterBind = false;
|
||||
descriptor_indexing.descriptorBindingStorageBufferUpdateAfterBind = false;
|
||||
descriptor_indexing.descriptorBindingUniformTexelBufferUpdateAfterBind = false;
|
||||
descriptor_indexing.descriptorBindingStorageTexelBufferUpdateAfterBind = false;
|
||||
descriptor_indexing.descriptorBindingUpdateUnusedWhilePending = false;
|
||||
descriptor_indexing.descriptorBindingUpdateUnusedWhilePending =
|
||||
bindless_table && descriptor_indexing.descriptorBindingUpdateUnusedWhilePending;
|
||||
descriptor_indexing.descriptorBindingVariableDescriptorCount = false;
|
||||
descriptor_indexing.runtimeDescriptorArray = false;
|
||||
}
|
||||
@@ -1374,6 +1380,11 @@ bool Device::GetSuitability(bool requires_swapchain) {
|
||||
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_PROPERTIES_EXT;
|
||||
SetNext(next, properties.descriptor_buffer);
|
||||
}
|
||||
if (extensions.descriptor_indexing || instance_version >= VK_API_VERSION_1_2) {
|
||||
properties.descriptor_indexing.sType =
|
||||
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES;
|
||||
SetNext(next, properties.descriptor_indexing);
|
||||
}
|
||||
if (extensions.subgroup_size_control || features.subgroup_size_control.subgroupSizeControl) {
|
||||
properties.subgroup_size_control.sType =
|
||||
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
|
||||
@@ -2137,3 +2148,27 @@ void Device::ShutdownGPULogging() {
|
||||
}
|
||||
|
||||
} // namespace Vulkan
|
||||
|
||||
namespace Vulkan {
|
||||
|
||||
bool Device::IsBindlessTableSupported() const {
|
||||
const auto& indexing = features.descriptor_indexing;
|
||||
const auto& limits = properties.descriptor_indexing;
|
||||
// One set holds a texture array per image type and class plus the sampler array: the update
|
||||
// after bind limits count the whole set and each stage.
|
||||
constexpr u32 images = Shader::BINDLESS_TEXTURE_CAPACITY * Shader::BINDLESS_CLASS_COUNT *
|
||||
Shader::BINDLESS_TYPE_COUNT;
|
||||
return Settings::ResolveNvnHle().hle && indexing.shaderSampledImageArrayNonUniformIndexing &&
|
||||
indexing.descriptorBindingPartiallyBound &&
|
||||
indexing.descriptorBindingSampledImageUpdateAfterBind &&
|
||||
indexing.descriptorBindingUpdateUnusedWhilePending &&
|
||||
features.features.shaderSampledImageArrayDynamicIndexing &&
|
||||
limits.maxPerStageDescriptorUpdateAfterBindSampledImages >= images &&
|
||||
limits.maxDescriptorSetUpdateAfterBindSampledImages >= images &&
|
||||
limits.maxPerStageDescriptorUpdateAfterBindSamplers >=
|
||||
Shader::BINDLESS_SAMPLER_CAPACITY &&
|
||||
limits.maxDescriptorSetUpdateAfterBindSamplers >= Shader::BINDLESS_SAMPLER_CAPACITY &&
|
||||
limits.maxPerStageUpdateAfterBindResources >= images + Shader::BINDLESS_SAMPLER_CAPACITY;
|
||||
}
|
||||
|
||||
} // namespace Vulkan
|
||||
@@ -1062,6 +1062,11 @@ FN_MAX_LIMIT_LIST
|
||||
return extensions.astc_decode_mode;
|
||||
}
|
||||
|
||||
/// VTX-HLE's bindless texture table (shader_recompiler/bindless_table.h): the device updates
|
||||
/// sampled-image and sampler descriptors after binding while unused, indexes them with
|
||||
/// non-uniform values, and holds the table's descriptors in one set.
|
||||
bool IsBindlessTableSupported() const;
|
||||
|
||||
/// Returns true if descriptor bindings is partially bound.
|
||||
bool IsDescriptorBindingPartiallyBoundSupported() const {
|
||||
return features.descriptor_indexing.descriptorBindingPartiallyBound;
|
||||
@@ -1393,6 +1398,7 @@ private:
|
||||
VkPhysicalDeviceDepthStencilResolveProperties depth_stencil_resolve{};
|
||||
VkPhysicalDeviceCustomBorderColorPropertiesEXT custom_border_color{};
|
||||
VkPhysicalDeviceCooperativeMatrixPropertiesKHR cooperative_matrix{};
|
||||
VkPhysicalDeviceDescriptorIndexingProperties descriptor_indexing{};
|
||||
|
||||
VkPhysicalDeviceProperties properties{};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user