summaryrefslogtreecommitdiff
path: root/src/video_core/shader
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2019-09-25 09:53:18 -0400
committerGravatar FernandoS272019-10-25 09:01:30 -0400
commit33fcec3502f5dd5a99b7a8337128b7c99bfba908 (patch)
tree4f41d09678600fc3e12708f8a4f8ae2f05c37ad1 /src/video_core/shader
parentShader_IR: Implement Fast BRX and allow multi-branches in the CFG. (diff)
downloadyuzu-33fcec3502f5dd5a99b7a8337128b7c99bfba908.tar.gz
yuzu-33fcec3502f5dd5a99b7a8337128b7c99bfba908.tar.xz
yuzu-33fcec3502f5dd5a99b7a8337128b7c99bfba908.zip
Shader_IR: allow lookup of texture samplers within the shader_ir for instructions that don't provide it
Diffstat (limited to 'src/video_core/shader')
-rw-r--r--src/video_core/shader/const_buffer_locker.cpp110
-rw-r--r--src/video_core/shader/const_buffer_locker.h60
-rw-r--r--src/video_core/shader/decode/texture.cpp72
-rw-r--r--src/video_core/shader/shader_ir.h12
4 files changed, 212 insertions, 42 deletions
diff --git a/src/video_core/shader/const_buffer_locker.cpp b/src/video_core/shader/const_buffer_locker.cpp
index 6a9e0ed5e..4f5de8ae9 100644
--- a/src/video_core/shader/const_buffer_locker.cpp
+++ b/src/video_core/shader/const_buffer_locker.cpp
@@ -27,43 +27,121 @@ void ConstBufferLocker::SetEngine(Tegra::Engines::ConstBufferEngineInterface* en
27} 27}
28 28
29std::optional<u32> ConstBufferLocker::ObtainKey(u32 buffer, u32 offset) { 29std::optional<u32> ConstBufferLocker::ObtainKey(u32 buffer, u32 offset) {
30 if (!keys) {
31 keys = std::make_shared<KeyMap>();
32 }
33 auto& key_map = *keys;
30 const std::pair<u32, u32> key = {buffer, offset}; 34 const std::pair<u32, u32> key = {buffer, offset};
31 const auto iter = keys.find(key); 35 const auto iter = key_map.find(key);
32 if (iter != keys.end()) { 36 if (iter != key_map.end()) {
33 return {iter->second}; 37 return {iter->second};
34 } 38 }
35 if (!IsEngineSet()) { 39 if (!IsEngineSet()) {
36 return {}; 40 return {};
37 } 41 }
38 const u32 value = engine->AccessConstBuffer32(shader_stage, buffer, offset); 42 const u32 value = engine->AccessConstBuffer32(shader_stage, buffer, offset);
39 keys.emplace(key, value); 43 key_map.emplace(key, value);
44 return {value};
45}
46
47std::optional<Tegra::Engines::SamplerDescriptor> ConstBufferLocker::ObtainBoundSampler(u32 offset) {
48 if (!bound_samplers) {
49 bound_samplers = std::make_shared<BoundSamplerMap>();
50 }
51 auto& key_map = *bound_samplers;
52 const u32 key = offset;
53 const auto iter = key_map.find(key);
54 if (iter != key_map.end()) {
55 return {iter->second};
56 }
57 if (!IsEngineSet()) {
58 return {};
59 }
60 const Tegra::Engines::SamplerDescriptor value =
61 engine->AccessBoundSampler(shader_stage, offset);
62 key_map.emplace(key, value);
63 return {value};
64}
65
66std::optional<Tegra::Engines::SamplerDescriptor> ConstBufferLocker::ObtainBindlessSampler(
67 u32 buffer, u32 offset) {
68 if (!bindless_samplers) {
69 bindless_samplers = std::make_shared<BindlessSamplerMap>();
70 }
71 auto& key_map = *bindless_samplers;
72 const std::pair<u32, u32> key = {buffer, offset};
73 const auto iter = key_map.find(key);
74 if (iter != key_map.end()) {
75 return {iter->second};
76 }
77 if (!IsEngineSet()) {
78 return {};
79 }
80 const Tegra::Engines::SamplerDescriptor value =
81 engine->AccessBindlessSampler(shader_stage, buffer, offset);
82 key_map.emplace(key, value);
40 return {value}; 83 return {value};
41} 84}
42 85
43void ConstBufferLocker::InsertKey(u32 buffer, u32 offset, u32 value) { 86void ConstBufferLocker::InsertKey(u32 buffer, u32 offset, u32 value) {
87 if (!keys) {
88 keys = std::make_shared<KeyMap>();
89 }
44 const std::pair<u32, u32> key = {buffer, offset}; 90 const std::pair<u32, u32> key = {buffer, offset};
45 keys[key] = value; 91 (*keys)[key] = value;
46} 92}
47 93
48u32 ConstBufferLocker::NumKeys() const { 94void ConstBufferLocker::InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler) {
49 return keys.size(); 95 if (!bound_samplers) {
96 bound_samplers = std::make_shared<BoundSamplerMap>();
97 }
98 (*bound_samplers)[offset] = sampler;
50} 99}
51 100
52const std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>& 101void ConstBufferLocker::InsertBindlessSampler(u32 buffer, u32 offset,
53ConstBufferLocker::AccessKeys() const { 102 Tegra::Engines::SamplerDescriptor sampler) {
54 return keys; 103 if (!bindless_samplers) {
104 bindless_samplers = std::make_shared<BindlessSamplerMap>();
105 }
106 const std::pair<u32, u32> key = {buffer, offset};
107 (*bindless_samplers)[key] = sampler;
55} 108}
56 109
57bool ConstBufferLocker::AreKeysConsistant() const { 110bool ConstBufferLocker::IsConsistant() const {
58 if (!IsEngineSet()) { 111 if (!IsEngineSet()) {
59 return false; 112 return false;
60 } 113 }
61 for (const auto& key_val : keys) { 114 if (keys) {
62 const std::pair<u32, u32> key = key_val.first; 115 for (const auto& key_val : *keys) {
63 const u32 value = key_val.second; 116 const std::pair<u32, u32> key = key_val.first;
64 const u32 other_value = engine->AccessConstBuffer32(shader_stage, key.first, key.second); 117 const u32 value = key_val.second;
65 if (other_value != value) { 118 const u32 other_value =
66 return false; 119 engine->AccessConstBuffer32(shader_stage, key.first, key.second);
120 if (other_value != value) {
121 return false;
122 }
123 }
124 }
125 if (bound_samplers) {
126 for (const auto& sampler_val : *bound_samplers) {
127 const u32 key = sampler_val.first;
128 const Tegra::Engines::SamplerDescriptor value = sampler_val.second;
129 const Tegra::Engines::SamplerDescriptor other_value =
130 engine->AccessBoundSampler(shader_stage, key);
131 if (other_value.raw != value.raw) {
132 return false;
133 }
134 }
135 }
136 if (bindless_samplers) {
137 for (const auto& sampler_val : *bindless_samplers) {
138 const std::pair<u32, u32> key = sampler_val.first;
139 const Tegra::Engines::SamplerDescriptor value = sampler_val.second;
140 const Tegra::Engines::SamplerDescriptor other_value =
141 engine->AccessBindlessSampler(shader_stage, key.first, key.second);
142 if (other_value.raw != value.raw) {
143 return false;
144 }
67 } 145 }
68 } 146 }
69 return true; 147 return true;
diff --git a/src/video_core/shader/const_buffer_locker.h b/src/video_core/shader/const_buffer_locker.h
index 39e62584d..0bc257781 100644
--- a/src/video_core/shader/const_buffer_locker.h
+++ b/src/video_core/shader/const_buffer_locker.h
@@ -11,6 +11,11 @@
11 11
12namespace VideoCommon::Shader { 12namespace VideoCommon::Shader {
13 13
14using KeyMap = std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>;
15using BoundSamplerMap = std::unordered_map<u32, Tegra::Engines::SamplerDescriptor>;
16using BindlessSamplerMap =
17 std::unordered_map<std::pair<u32, u32>, Tegra::Engines::SamplerDescriptor, Common::PairHash>;
18
14class ConstBufferLocker { 19class ConstBufferLocker {
15public: 20public:
16 explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage); 21 explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage);
@@ -29,22 +34,67 @@ public:
29 // registered value, if not it will obtain it from maxwell3d and register it. 34 // registered value, if not it will obtain it from maxwell3d and register it.
30 std::optional<u32> ObtainKey(u32 buffer, u32 offset); 35 std::optional<u32> ObtainKey(u32 buffer, u32 offset);
31 36
37 std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
38
39 std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
40
32 // Manually inserts a key. 41 // Manually inserts a key.
33 void InsertKey(u32 buffer, u32 offset, u32 value); 42 void InsertKey(u32 buffer, u32 offset, u32 value);
34 43
44 void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
45
46 void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
47
35 // Retrieves the number of keys registered. 48 // Retrieves the number of keys registered.
36 u32 NumKeys() const; 49 std::size_t NumKeys() const {
50 if (!keys) {
51 return 0;
52 }
53 return keys->size();
54 }
55
56 std::size_t NumBoundSamplers() const {
57 if (!bound_samplers) {
58 return 0;
59 }
60 return bound_samplers->size();
61 }
62
63 std::size_t NumBindlessSamplers() const {
64 if (!bindless_samplers) {
65 return 0;
66 }
67 return bindless_samplers->size();
68 }
37 69
38 // Gives an accessor to the key's database. 70 // Gives an accessor to the key's database.
39 const std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash>& AccessKeys() const; 71 // Pre: NumKeys > 0
72 const KeyMap& AccessKeys() const {
73 return *keys;
74 }
75
76 // Gives an accessor to the sampler's database.
77 // Pre: NumBindlessSamplers > 0
78 const BoundSamplerMap& AccessBoundSamplers() const {
79 return *bound_samplers;
80 }
81
82 // Gives an accessor to the sampler's database.
83 // Pre: NumBindlessSamplers > 0
84 const BindlessSamplerMap& AccessBindlessSamplers() const {
85 return *bindless_samplers;
86 }
40 87
41 // Checks keys against maxwell3d's current const buffers. Returns true if they 88 // Checks keys & samplers against engine's current const buffers. Returns true if they
42 // are the same value, false otherwise; 89 // are the same value, false otherwise;
43 bool AreKeysConsistant() const; 90 bool IsConsistant() const;
44 91
45private: 92private:
46 Tegra::Engines::ConstBufferEngineInterface* engine; 93 Tegra::Engines::ConstBufferEngineInterface* engine;
47 Tegra::Engines::ShaderType shader_stage; 94 Tegra::Engines::ShaderType shader_stage;
48 std::unordered_map<std::pair<u32, u32>, u32, Common::PairHash> keys{}; 95 // All containers are lazy initialized as most shaders don't use them.
96 std::shared_ptr<KeyMap> keys{};
97 std::shared_ptr<BoundSamplerMap> bound_samplers{};
98 std::shared_ptr<BindlessSamplerMap> bindless_samplers{};
49}; 99};
50} // namespace VideoCommon::Shader 100} // namespace VideoCommon::Shader
diff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cpp
index 0b934a069..c369e23ad 100644
--- a/src/video_core/shader/decode/texture.cpp
+++ b/src/video_core/shader/decode/texture.cpp
@@ -141,7 +141,7 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
141 const Node component = Immediate(static_cast<u32>(instr.tld4s.component)); 141 const Node component = Immediate(static_cast<u32>(instr.tld4s.component));
142 142
143 const auto& sampler = 143 const auto& sampler =
144 GetSampler(instr.sampler, TextureType::Texture2D, false, depth_compare); 144 GetSampler(instr.sampler, {{TextureType::Texture2D, false, depth_compare}});
145 145
146 Node4 values; 146 Node4 values;
147 for (u32 element = 0; element < values.size(); ++element) { 147 for (u32 element = 0; element < values.size(); ++element) {
@@ -165,10 +165,7 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
165 // Sadly, not all texture instructions specify the type of texture their sampler 165 // Sadly, not all texture instructions specify the type of texture their sampler
166 // uses. This must be fixed at a later instance. 166 // uses. This must be fixed at a later instance.
167 const auto& sampler = 167 const auto& sampler =
168 is_bindless 168 is_bindless ? GetBindlessSampler(instr.gpr8, {}) : GetSampler(instr.sampler, {});
169 ? GetBindlessSampler(instr.gpr8, Tegra::Shader::TextureType::Texture2D, false,
170 false)
171 : GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false, false);
172 169
173 u32 indexer = 0; 170 u32 indexer = 0;
174 switch (instr.txq.query_type) { 171 switch (instr.txq.query_type) {
@@ -207,9 +204,9 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
207 204
208 auto texture_type = instr.tmml.texture_type.Value(); 205 auto texture_type = instr.tmml.texture_type.Value();
209 const bool is_array = instr.tmml.array != 0; 206 const bool is_array = instr.tmml.array != 0;
210 const auto& sampler = is_bindless 207 const auto& sampler =
211 ? GetBindlessSampler(instr.gpr20, texture_type, is_array, false) 208 is_bindless ? GetBindlessSampler(instr.gpr20, {{texture_type, is_array, false}})
212 : GetSampler(instr.sampler, texture_type, is_array, false); 209 : GetSampler(instr.sampler, {{texture_type, is_array, false}});
213 210
214 std::vector<Node> coords; 211 std::vector<Node> coords;
215 212
@@ -285,10 +282,30 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
285 return pc; 282 return pc;
286} 283}
287 284
288const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler, TextureType type, 285const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler,
289 bool is_array, bool is_shadow) { 286 std::optional<SamplerInfo> sampler_info) {
290 const auto offset = static_cast<std::size_t>(sampler.index.Value()); 287 const auto offset = static_cast<std::size_t>(sampler.index.Value());
291 288
289 Tegra::Shader::TextureType type;
290 bool is_array;
291 bool is_shadow;
292 if (sampler_info) {
293 type = sampler_info->type;
294 is_array = sampler_info->is_array;
295 is_shadow = sampler_info->is_shadow;
296 } else {
297 auto sampler = locker.ObtainBoundSampler(offset);
298 if (sampler) {
299 type = sampler->texture_type.Value();
300 is_array = sampler->is_array.Value() != 0;
301 is_shadow = sampler->is_shadow.Value() != 0;
302 } else {
303 type = Tegra::Shader::TextureType::Texture2D;
304 is_array = false;
305 is_shadow = false;
306 }
307 }
308
292 // If this sampler has already been used, return the existing mapping. 309 // If this sampler has already been used, return the existing mapping.
293 const auto itr = 310 const auto itr =
294 std::find_if(used_samplers.begin(), used_samplers.end(), 311 std::find_if(used_samplers.begin(), used_samplers.end(),
@@ -305,13 +322,32 @@ const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler, Textu
305 return *used_samplers.emplace(entry).first; 322 return *used_samplers.emplace(entry).first;
306} 323}
307 324
308const Sampler& ShaderIR::GetBindlessSampler(const Tegra::Shader::Register& reg, TextureType type, 325const Sampler& ShaderIR::GetBindlessSampler(const Tegra::Shader::Register& reg,
309 bool is_array, bool is_shadow) { 326 std::optional<SamplerInfo> sampler_info) {
310 const Node sampler_register = GetRegister(reg); 327 const Node sampler_register = GetRegister(reg);
311 const auto [base_sampler, cbuf_index, cbuf_offset] = 328 const auto [base_sampler, cbuf_index, cbuf_offset] =
312 TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size())); 329 TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size()));
313 ASSERT(base_sampler != nullptr); 330 ASSERT(base_sampler != nullptr);
314 const auto cbuf_key = (static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset); 331 const auto cbuf_key = (static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset);
332 Tegra::Shader::TextureType type;
333 bool is_array;
334 bool is_shadow;
335 if (sampler_info) {
336 type = sampler_info->type;
337 is_array = sampler_info->is_array;
338 is_shadow = sampler_info->is_shadow;
339 } else {
340 auto sampler = locker.ObtainBindlessSampler(cbuf_index, cbuf_offset);
341 if (sampler) {
342 type = sampler->texture_type.Value();
343 is_array = sampler->is_array.Value() != 0;
344 is_shadow = sampler->is_shadow.Value() != 0;
345 } else {
346 type = Tegra::Shader::TextureType::Texture2D;
347 is_array = false;
348 is_shadow = false;
349 }
350 }
315 351
316 // If this sampler has already been used, return the existing mapping. 352 // If this sampler has already been used, return the existing mapping.
317 const auto itr = 353 const auto itr =
@@ -411,9 +447,9 @@ Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
411 (texture_type == TextureType::TextureCube && is_array && is_shadow), 447 (texture_type == TextureType::TextureCube && is_array && is_shadow),
412 "This method is not supported."); 448 "This method is not supported.");
413 449
414 const auto& sampler = is_bindless 450 const auto& sampler =
415 ? GetBindlessSampler(*bindless_reg, texture_type, is_array, is_shadow) 451 is_bindless ? GetBindlessSampler(*bindless_reg, {{texture_type, is_array, is_shadow}})
416 : GetSampler(instr.sampler, texture_type, is_array, is_shadow); 452 : GetSampler(instr.sampler, {{texture_type, is_array, is_shadow}});
417 453
418 const bool lod_needed = process_mode == TextureProcessMode::LZ || 454 const bool lod_needed = process_mode == TextureProcessMode::LZ ||
419 process_mode == TextureProcessMode::LL || 455 process_mode == TextureProcessMode::LL ||
@@ -577,7 +613,7 @@ Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool de
577 dc = GetRegister(parameter_register++); 613 dc = GetRegister(parameter_register++);
578 } 614 }
579 615
580 const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, depth_compare); 616 const auto& sampler = GetSampler(instr.sampler, {{texture_type, is_array, depth_compare}});
581 617
582 Node4 values; 618 Node4 values;
583 for (u32 element = 0; element < values.size(); ++element) { 619 for (u32 element = 0; element < values.size(); ++element) {
@@ -610,7 +646,7 @@ Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) {
610 // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr}; 646 // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr};
611 // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr}; 647 // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr};
612 648
613 const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, false); 649 const auto& sampler = GetSampler(instr.sampler, {{texture_type, is_array, false}});
614 650
615 Node4 values; 651 Node4 values;
616 for (u32 element = 0; element < values.size(); ++element) { 652 for (u32 element = 0; element < values.size(); ++element) {
@@ -646,7 +682,7 @@ Node4 ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is
646 // When lod is used always is in gpr20 682 // When lod is used always is in gpr20
647 const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0); 683 const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0);
648 684
649 const auto& sampler = GetSampler(instr.sampler, texture_type, is_array, false); 685 const auto& sampler = GetSampler(instr.sampler, {{texture_type, is_array, false}});
650 686
651 Node4 values; 687 Node4 values;
652 for (u32 element = 0; element < values.size(); ++element) { 688 for (u32 element = 0; element < values.size(); ++element) {
diff --git a/src/video_core/shader/shader_ir.h b/src/video_core/shader/shader_ir.h
index e3b568d3e..3a3e381d2 100644
--- a/src/video_core/shader/shader_ir.h
+++ b/src/video_core/shader/shader_ir.h
@@ -173,6 +173,13 @@ public:
173 173
174private: 174private:
175 friend class ASTDecoder; 175 friend class ASTDecoder;
176
177 struct SamplerInfo {
178 Tegra::Shader::TextureType type;
179 bool is_array;
180 bool is_shadow;
181 };
182
176 void Decode(); 183 void Decode();
177 184
178 NodeBlock DecodeRange(u32 begin, u32 end); 185 NodeBlock DecodeRange(u32 begin, u32 end);
@@ -297,12 +304,11 @@ private:
297 304
298 /// Accesses a texture sampler 305 /// Accesses a texture sampler
299 const Sampler& GetSampler(const Tegra::Shader::Sampler& sampler, 306 const Sampler& GetSampler(const Tegra::Shader::Sampler& sampler,
300 Tegra::Shader::TextureType type, bool is_array, bool is_shadow); 307 std::optional<SamplerInfo> sampler_info);
301 308
302 // Accesses a texture sampler for a bindless texture. 309 // Accesses a texture sampler for a bindless texture.
303 const Sampler& GetBindlessSampler(const Tegra::Shader::Register& reg, 310 const Sampler& GetBindlessSampler(const Tegra::Shader::Register& reg,
304 Tegra::Shader::TextureType type, bool is_array, 311 std::optional<SamplerInfo> sampler_info);
305 bool is_shadow);
306 312
307 /// Accesses an image. 313 /// Accesses an image.
308 Image& GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type); 314 Image& GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type);