VERY experimental Nintendo Switch 2 emulator
C++
shader/node: Unpack bindless texture encoding
ReinUsesLisp1589a146eddf
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cppindex 43032e9a73..35fc094a8f 100644--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp@@ -973,10 +973,9 @@ TextureBufferUsage RasterizerOpenGL::SetupDrawTextures(Maxwell::ShaderStage stagif (!entry.IsBindless()) {return maxwell3d.GetStageTexture(stage, entry.GetOffset());}- const auto cbuf = entry.GetBindlessCBuf();- Tegra::Texture::TextureHandle tex_handle;- Tegra::Engines::ShaderType shader_type = static_cast<Tegra::Engines::ShaderType>(stage);- tex_handle.raw = maxwell3d.AccessConstBuffer32(shader_type, cbuf.first, cbuf.second);+ const auto shader_type = static_cast<Tegra::Engines::ShaderType>(stage);+ const Tegra::Texture::TextureHandle tex_handle =+ maxwell3d.AccessConstBuffer32(shader_type, entry.GetBuffer(), entry.GetOffset());return maxwell3d.GetTextureInfo(tex_handle);}();@@ -1004,10 +1003,8 @@ TextureBufferUsage RasterizerOpenGL::SetupComputeTextures(const Shader& kernel)if (!entry.IsBindless()) {return compute.GetTexture(entry.GetOffset());}- const auto cbuf = entry.GetBindlessCBuf();- Tegra::Texture::TextureHandle tex_handle;- tex_handle.raw = compute.AccessConstBuffer32(Tegra::Engines::ShaderType::Compute,- cbuf.first, cbuf.second);+ const Tegra::Texture::TextureHandle tex_handle = compute.AccessConstBuffer32(+ Tegra::Engines::ShaderType::Compute, entry.GetBuffer(), entry.GetOffset());return compute.GetTextureInfo(tex_handle);}();@@ -1050,10 +1047,8 @@ void RasterizerOpenGL::SetupComputeImages(const Shader& shader) {if (!entry.IsBindless()) {return compute.GetTexture(entry.GetOffset()).tic;}- const auto cbuf = entry.GetBindlessCBuf();- Tegra::Texture::TextureHandle tex_handle;- tex_handle.raw = compute.AccessConstBuffer32(Tegra::Engines::ShaderType::Compute,- cbuf.first, cbuf.second);+ const Tegra::Texture::TextureHandle tex_handle = compute.AccessConstBuffer32(+ Tegra::Engines::ShaderType::Compute, entry.GetBuffer(), entry.GetOffset());return compute.GetTextureInfo(tex_handle).tic;}();SetupImage(bindpoint, tic, entry);diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cppindex 030550c533..92ee8459e4 100644--- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp@@ -735,7 +735,7 @@ private:void DeclareImages() {const auto& images{ir.GetImages()};- for (const auto& [offset, image] : images) {+ for (const auto& image : images) {std::string qualifier = "coherent volatile";if (image.IsRead() && !image.IsWritten()) {qualifier += " readonly";@@ -2466,16 +2466,16 @@ ShaderEntries GetEntries(const VideoCommon::Shader::ShaderIR& ir) {entries.const_buffers.emplace_back(cbuf.second.GetMaxOffset(), cbuf.second.IsIndirect(),cbuf.first);}+ for (const auto& [base, usage] : ir.GetGlobalMemory()) {+ entries.global_memory_entries.emplace_back(base.cbuf_index, base.cbuf_offset, usage.is_read,+ usage.is_written);+ }for (const auto& sampler : ir.GetSamplers()) {entries.samplers.emplace_back(sampler);}- for (const auto& [offset, image] : ir.GetImages()) {+ for (const auto& image : ir.GetImages()) {entries.images.emplace_back(image);}- for (const auto& [base, usage] : ir.GetGlobalMemory()) {- entries.global_memory_entries.emplace_back(base.cbuf_index, base.cbuf_offset, usage.is_read,- usage.is_written);- }entries.clip_distances = ir.GetClipDistances();entries.shader_length = ir.GetLength();return entries;diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.h b/src/video_core/renderer_opengl/gl_shader_decompiler.hindex fead2a51ef..b1e75e6cc7 100644--- a/src/video_core/renderer_opengl/gl_shader_decompiler.h+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.h@@ -82,10 +82,9 @@ private:struct ShaderEntries {std::vector<ConstBufferEntry> const_buffers;+ std::vector<GlobalMemoryEntry> global_memory_entries;std::vector<SamplerEntry> samplers;- std::vector<SamplerEntry> bindless_samplers;std::vector<ImageEntry> images;- std::vector<GlobalMemoryEntry> global_memory_entries;std::array<bool, Maxwell::NumClipDistances> clip_distances{};std::size_t shader_length{};};diff --git a/src/video_core/shader/decode/image.cpp b/src/video_core/shader/decode/image.cppindex b02d2cb952..d2fe4ec5dd 100644--- a/src/video_core/shader/decode/image.cpp+++ b/src/video_core/shader/decode/image.cpp@@ -143,39 +143,37 @@ u32 ShaderIR::DecodeImage(NodeBlock& bb, u32 pc) {}Image& ShaderIR::GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type) {- const auto offset{static_cast<std::size_t>(image.index.Value())};- if (const auto existing_image = TryUseExistingImage(offset, type)) {- return *existing_image;+ const auto offset = static_cast<u32>(image.index.Value());++ const auto it =+ std::find_if(std::begin(used_images), std::end(used_images),+ [offset](const Image& entry) { return entry.GetOffset() == offset; });+ if (it != std::end(used_images)) {+ ASSERT(!it->IsBindless() && it->GetType() == it->GetType());+ return *it;}- const std::size_t next_index{used_images.size()};- return used_images.emplace(offset, Image{offset, next_index, type}).first->second;+ const auto next_index = static_cast<u32>(used_images.size());+ return used_images.emplace_back(next_index, offset, type);}Image& ShaderIR::GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type) {- const Node image_register{GetRegister(reg)};- const auto [base_image, cbuf_index, cbuf_offset]{- TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()))};- const auto cbuf_key{(static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset)};-- if (const auto image = TryUseExistingImage(cbuf_key, type)) {- return *image;- }-- const std::size_t next_index{used_images.size()};- return used_images.emplace(cbuf_key, Image{cbuf_index, cbuf_offset, next_index, type})- .first->second;-}--Image* ShaderIR::TryUseExistingImage(u64 offset, Tegra::Shader::ImageType type) {- auto it = used_images.find(offset);- if (it == used_images.end()) {- return nullptr;+ const Node image_register = GetRegister(reg);+ const auto [base_image, buffer, offset] =+ TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()));++ const auto it =+ std::find_if(std::begin(used_images), std::end(used_images),+ [buffer = buffer, offset = offset](const Image& entry) {+ return entry.GetBuffer() == buffer && entry.GetOffset() == offset;+ });+ if (it != std::end(used_images)) {+ ASSERT(it->IsBindless() && it->GetType() == it->GetType());+ return *it;}- auto& image = it->second;- ASSERT(image.GetType() == type);- return ℑ+ const auto next_index = static_cast<u32>(used_images.size());+ return used_images.emplace_back(next_index, offset, buffer, type);}} // namespace VideoCommon::Shaderdiff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cppindex d61e656b75..8d3d962c7e 100644--- a/src/video_core/shader/decode/texture.cpp+++ b/src/video_core/shader/decode/texture.cpp@@ -255,7 +255,7 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {break;}case OpCode::Id::TLDS: {- const Tegra::Shader::TextureType texture_type{instr.tlds.GetTextureType()};+ const TextureType texture_type{instr.tlds.GetTextureType()};const bool is_array{instr.tlds.IsArrayTexture()};UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI),@@ -286,77 +286,80 @@ const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler,std::optional<SamplerInfo> sampler_info) {const auto offset = static_cast<u32>(sampler.index.Value());- Tegra::Shader::TextureType type;+ TextureType type;bool is_array;bool is_shadow;if (sampler_info) {type = sampler_info->type;is_array = sampler_info->is_array;is_shadow = sampler_info->is_shadow;- } else if (auto sampler = locker.ObtainBoundSampler(offset); sampler) {+ } else if (const auto sampler = locker.ObtainBoundSampler(offset)) {type = sampler->texture_type.Value();is_array = sampler->is_array.Value() != 0;is_shadow = sampler->is_shadow.Value() != 0;} else {- type = Tegra::Shader::TextureType::Texture2D;+ LOG_WARNING(HW_GPU, "Unknown sampler info");+ type = TextureType::Texture2D;is_array = false;is_shadow = false;}// If this sampler has already been used, return the existing mapping.- const auto itr =+ const auto it =std::find_if(used_samplers.begin(), used_samplers.end(),- [&](const Sampler& entry) { return entry.GetOffset() == offset; });- if (itr != used_samplers.end()) {- ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&- itr->IsShadow() == is_shadow);- return *itr;+ [offset](const Sampler& entry) { return entry.GetOffset() == offset; });+ if (it != used_samplers.end()) {+ ASSERT(!it->IsBindless() && it->GetType() == type && it->IsArray() == is_array &&+ it->IsShadow() == is_shadow);+ return *it;}// Otherwise create a new mapping for this sampler- const std::size_t next_index = used_samplers.size();- const Sampler entry{offset, next_index, type, is_array, is_shadow};- return *used_samplers.emplace(entry).first;-} // namespace VideoCommon::Shader+ const auto next_index = static_cast<u32>(used_samplers.size());+ return used_samplers.emplace_back(Sampler(next_index, offset, type, is_array, is_shadow));+}const Sampler& ShaderIR::GetBindlessSampler(const Tegra::Shader::Register& reg,std::optional<SamplerInfo> sampler_info) {const Node sampler_register = GetRegister(reg);- const auto [base_sampler, cbuf_index, cbuf_offset] =+ const auto [base_sampler, buffer, offset] =TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size()));ASSERT(base_sampler != nullptr);- const auto cbuf_key = (static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset);- Tegra::Shader::TextureType type;++ TextureType type;bool is_array;bool is_shadow;if (sampler_info) {type = sampler_info->type;is_array = sampler_info->is_array;is_shadow = sampler_info->is_shadow;- } else if (auto sampler = locker.ObtainBindlessSampler(cbuf_index, cbuf_offset); sampler) {+ } else if (const auto sampler = locker.ObtainBindlessSampler(buffer, offset)) {type = sampler->texture_type.Value();is_array = sampler->is_array.Value() != 0;is_shadow = sampler->is_shadow.Value() != 0;} else {- type = Tegra::Shader::TextureType::Texture2D;+ LOG_WARNING(HW_GPU, "Unknown sampler info");+ type = TextureType::Texture2D;is_array = false;is_shadow = false;}// If this sampler has already been used, return the existing mapping.- const auto itr =+ const auto it =std::find_if(used_samplers.begin(), used_samplers.end(),- [&](const Sampler& entry) { return entry.GetOffset() == cbuf_key; });- if (itr != used_samplers.end()) {- ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&- itr->IsShadow() == is_shadow);- return *itr;+ [buffer = buffer, offset = offset](const Sampler& entry) {+ return entry.GetBuffer() == buffer && entry.GetOffset() == offset;+ });+ if (it != used_samplers.end()) {+ ASSERT(it->IsBindless() && it->GetType() == type && it->IsArray() == is_array &&+ it->IsShadow() == is_shadow);+ return *it;}// Otherwise create a new mapping for this sampler- const std::size_t next_index = used_samplers.size();- const Sampler entry{cbuf_index, cbuf_offset, next_index, type, is_array, is_shadow};- return *used_samplers.emplace(entry).first;+ const auto next_index = static_cast<u32>(used_samplers.size());+ return used_samplers.emplace_back(+ Sampler(next_index, offset, buffer, type, is_array, is_shadow));}void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) {diff --git a/src/video_core/shader/node.h b/src/video_core/shader/node.hindex 447fb5c1d3..4300d9ff4d 100644--- a/src/video_core/shader/node.h+++ b/src/video_core/shader/node.h@@ -230,62 +230,49 @@ using NodeBlock = std::vector<Node>;class Sampler {public:/// This constructor is for bound samplers- explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,- bool is_array, bool is_shadow)- : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},- is_bindless{false} {}+ constexpr explicit Sampler(u32 index, u32 offset, Tegra::Shader::TextureType type,+ bool is_array, bool is_shadow)+ : index{index}, offset{offset}, type{type}, is_array{is_array}, is_shadow{is_shadow} {}/// This constructor is for bindless samplers- explicit Sampler(u32 cbuf_index, u32 cbuf_offset, std::size_t index,- Tegra::Shader::TextureType type, bool is_array, bool is_shadow)- : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type},- is_array{is_array}, is_shadow{is_shadow}, is_bindless{true} {}-- /// This constructor is for serialization/deserialization- explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,- bool is_array, bool is_shadow, bool is_bindless)- : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},- is_bindless{is_bindless} {}-- std::size_t GetOffset() const {+ constexpr explicit Sampler(u32 index, u32 offset, u32 buffer, Tegra::Shader::TextureType type,+ bool is_array, bool is_shadow)+ : index{index}, offset{offset}, buffer{buffer}, type{type}, is_array{is_array},+ is_shadow{is_shadow}, is_bindless{true} {}++ constexpr u32 GetIndex() const {+ return index;+ }++ constexpr u32 GetOffset() const {return offset;}- std::size_t GetIndex() const {- return index;+ constexpr u32 GetBuffer() const {+ return buffer;}- Tegra::Shader::TextureType GetType() const {+ constexpr Tegra::Shader::TextureType GetType() const {return type;}- bool IsArray() const {+ constexpr bool IsArray() const {return is_array;}- bool IsShadow() const {+ constexpr bool IsShadow() const {return is_shadow;}- bool IsBindless() const {+ constexpr bool IsBindless() const {return is_bindless;}- std::pair<u32, u32> GetBindlessCBuf() const {- return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};- }-- bool operator<(const Sampler& rhs) const {- return std::tie(index, offset, type, is_array, is_shadow, is_bindless) <- std::tie(rhs.index, rhs.offset, rhs.type, rhs.is_array, rhs.is_shadow,- rhs.is_bindless);- }-private:- /// Offset in TSC memory from which to read the sampler object, as specified by the sampling- /// instruction.- std::size_t offset{};- std::size_t index{}; ///< Value used to index into the generated GLSL sampler array.+ u32 index{}; ///< Emulated index given for the this sampler.+ u32 offset{}; ///< Offset in the const buffer from where the sampler is being read.+ u32 buffer{}; ///< Buffer where the bindless sampler is being read (unused on bound samplers).+Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc)bool is_array{}; ///< Whether the texture is being sampled as an array texture or not.bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not.@@ -294,18 +281,13 @@ private:class Image final {public:- constexpr explicit Image(std::size_t offset, std::size_t index, Tegra::Shader::ImageType type)- : offset{offset}, index{index}, type{type}, is_bindless{false} {}-- constexpr explicit Image(u32 cbuf_index, u32 cbuf_offset, std::size_t index,- Tegra::Shader::ImageType type)- : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type},- is_bindless{true} {}+ /// This constructor is for bound images+ constexpr explicit Image(u32 index, u32 offset, Tegra::Shader::ImageType type)+ : index{index}, offset{offset}, type{type} {}- constexpr explicit Image(std::size_t offset, std::size_t index, Tegra::Shader::ImageType type,- bool is_bindless, bool is_written, bool is_read, bool is_atomic)- : offset{offset}, index{index}, type{type}, is_bindless{is_bindless},- is_written{is_written}, is_read{is_read}, is_atomic{is_atomic} {}+ /// This constructor is for bindless samplers+ constexpr explicit Image(u32 index, u32 offset, u32 buffer, Tegra::Shader::ImageType type)+ : index{index}, offset{offset}, buffer{buffer}, type{type}, is_bindless{true} {}void MarkWrite() {is_written = true;@@ -321,12 +303,16 @@ public:is_atomic = true;}- constexpr std::size_t GetOffset() const {+ constexpr u32 GetIndex() const {+ return index;+ }++ constexpr u32 GetOffset() const {return offset;}- constexpr std::size_t GetIndex() const {- return index;+ constexpr u32 GetBuffer() const {+ return buffer;}constexpr Tegra::Shader::ImageType GetType() const {@@ -349,18 +335,11 @@ public:return is_atomic;}- constexpr std::pair<u32, u32> GetBindlessCBuf() const {- return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};- }-- constexpr bool operator<(const Image& rhs) const {- return std::tie(offset, index, type, is_bindless) <- std::tie(rhs.offset, rhs.index, rhs.type, rhs.is_bindless);- }-private:- u64 offset{};- std::size_t index{};+ u32 index{};+ u32 offset{};+ u32 buffer{};+Tegra::Shader::ImageType type{};bool is_bindless{};bool is_written{};diff --git a/src/video_core/shader/shader_ir.h b/src/video_core/shader/shader_ir.hindex 1fd44bde1d..5243644e8b 100644--- a/src/video_core/shader/shader_ir.h+++ b/src/video_core/shader/shader_ir.h@@ -5,6 +5,7 @@#pragma once#include <array>+#include <list>#include <map>#include <optional>#include <set>@@ -95,11 +96,11 @@ public:return used_cbufs;}- const std::set<Sampler>& GetSamplers() const {+ const std::list<Sampler>& GetSamplers() const {return used_samplers;}- const std::map<u64, Image>& GetImages() const {+ const std::list<Image>& GetImages() const {return used_images;}@@ -316,9 +317,6 @@ private:/// Access a bindless image sampler.Image& GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type);- /// Tries to access an existing image, updating it's state as needed- Image* TryUseExistingImage(u64 offset, Tegra::Shader::ImageType type);-/// Extracts a sequence of bits from a nodeNode BitfieldExtract(Node value, u32 offset, u32 bits);@@ -402,8 +400,8 @@ private:std::set<Tegra::Shader::Attribute::Index> used_input_attributes;std::set<Tegra::Shader::Attribute::Index> used_output_attributes;std::map<u32, ConstBuffer> used_cbufs;- std::set<Sampler> used_samplers;- std::map<u64, Image> used_images;+ std::list<Sampler> used_samplers;+ std::list<Image> used_images;std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};std::map<GlobalMemoryBase, GlobalMemoryUsage> used_global_memory;bool uses_layer{};diff --git a/src/video_core/textures/texture.h b/src/video_core/textures/texture.hindex 0429af9c12..27c8ce9757 100644--- a/src/video_core/textures/texture.h+++ b/src/video_core/textures/texture.h@@ -132,6 +132,8 @@ enum class SwizzleSource : u32 {};union TextureHandle {+ TextureHandle(u32 raw) : raw{raw} {}+u32 raw;BitField<0, 20, u32> tic_id;BitField<20, 12, u32> tsc_id;