| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | // Copyright 2019 yuzu Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2 or any later version
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <fmt/format.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "common/assert.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  | #include "common/bit_field.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | #include "common/common_types.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  | #include "common/logging/log.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | #include "video_core/engines/shader_bytecode.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-04 22:44:06 -03:00
										 |  |  | #include "video_core/shader/node_helper.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | #include "video_core/shader/shader_ir.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace VideoCommon::Shader { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using Tegra::Shader::Instruction; | 
					
						
							|  |  |  | using Tegra::Shader::OpCode; | 
					
						
							|  |  |  | using Tegra::Shader::Register; | 
					
						
							|  |  |  | using Tegra::Shader::TextureMiscMode; | 
					
						
							|  |  |  | using Tegra::Shader::TextureProcessMode; | 
					
						
							|  |  |  | using Tegra::Shader::TextureType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static std::size_t GetCoordCount(TextureType texture_type) { | 
					
						
							|  |  |  |     switch (texture_type) { | 
					
						
							|  |  |  |     case TextureType::Texture1D: | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     case TextureType::Texture2D: | 
					
						
							|  |  |  |         return 2; | 
					
						
							|  |  |  |     case TextureType::Texture3D: | 
					
						
							|  |  |  |     case TextureType::TextureCube: | 
					
						
							|  |  |  |         return 3; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type)); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) { | 
					
						
							|  |  |  |     const Instruction instr = {program_code[pc]}; | 
					
						
							|  |  |  |     const auto opcode = OpCode::Decode(instr); | 
					
						
							| 
									
										
										
										
											2019-03-26 19:13:53 -04:00
										 |  |  |     bool is_bindless = false; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     switch (opcode->get().GetId()) { | 
					
						
							|  |  |  |     case OpCode::Id::TEX: { | 
					
						
							|  |  |  |         const TextureType texture_type{instr.tex.texture_type}; | 
					
						
							|  |  |  |         const bool is_array = instr.tex.array != 0; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |         const bool is_aoffi = instr.tex.UsesMiscMode(TextureMiscMode::AOFFI); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         const bool depth_compare = instr.tex.UsesMiscMode(TextureMiscMode::DC); | 
					
						
							|  |  |  |         const auto process_mode = instr.tex.GetTextureProcessMode(); | 
					
						
							|  |  |  |         WriteTexInstructionFloat( | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |             bb, instr, | 
					
						
							| 
									
										
										
										
											2019-03-27 07:11:50 -04:00
										 |  |  |             GetTexCode(instr, texture_type, process_mode, depth_compare, is_array, is_aoffi, {})); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  |     case OpCode::Id::TEX_B: { | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(TextureMiscMode::AOFFI), | 
					
						
							|  |  |  |                              "AOFFI is not implemented"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 19:02:24 -04:00
										 |  |  |         const TextureType texture_type{instr.tex_b.texture_type}; | 
					
						
							|  |  |  |         const bool is_array = instr.tex_b.array != 0; | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |         const bool is_aoffi = instr.tex.UsesMiscMode(TextureMiscMode::AOFFI); | 
					
						
							| 
									
										
										
										
											2019-03-26 19:02:24 -04:00
										 |  |  |         const bool depth_compare = instr.tex_b.UsesMiscMode(TextureMiscMode::DC); | 
					
						
							|  |  |  |         const auto process_mode = instr.tex_b.GetTextureProcessMode(); | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |         WriteTexInstructionFloat(bb, instr, | 
					
						
							|  |  |  |                                  GetTexCode(instr, texture_type, process_mode, depth_compare, | 
					
						
							|  |  |  |                                             is_array, is_aoffi, {instr.gpr20})); | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     case OpCode::Id::TEXS: { | 
					
						
							|  |  |  |         const TextureType texture_type{instr.texs.GetTextureType()}; | 
					
						
							|  |  |  |         const bool is_array{instr.texs.IsArrayTexture()}; | 
					
						
							|  |  |  |         const bool depth_compare = instr.texs.UsesMiscMode(TextureMiscMode::DC); | 
					
						
							|  |  |  |         const auto process_mode = instr.texs.GetTextureProcessMode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const Node4 components = | 
					
						
							|  |  |  |             GetTexsCode(instr, texture_type, process_mode, depth_compare, is_array); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (instr.texs.fp32_flag) { | 
					
						
							|  |  |  |             WriteTexsInstructionFloat(bb, instr, components); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             WriteTexsInstructionHalfFloat(bb, instr, components); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |     case OpCode::Id::TLD4_B: { | 
					
						
							|  |  |  |         is_bindless = true; | 
					
						
							|  |  |  |         [[fallthrough]]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     case OpCode::Id::TLD4: { | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::NDV), | 
					
						
							|  |  |  |                              "NDV is not implemented"); | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(TextureMiscMode::PTP), | 
					
						
							|  |  |  |                              "PTP is not implemented"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const auto texture_type = instr.tld4.texture_type.Value(); | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |         const bool depth_compare = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::DC) | 
					
						
							|  |  |  |                                                : instr.tld4.UsesMiscMode(TextureMiscMode::DC); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         const bool is_array = instr.tld4.array != 0; | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |         const bool is_aoffi = is_bindless ? instr.tld4_b.UsesMiscMode(TextureMiscMode::AOFFI) | 
					
						
							|  |  |  |                                           : instr.tld4.UsesMiscMode(TextureMiscMode::AOFFI); | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |         WriteTexInstructionFloat( | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |             bb, instr, | 
					
						
							| 
									
										
										
										
											2019-10-30 21:14:57 -04:00
										 |  |  |             GetTld4Code(instr, texture_type, depth_compare, is_array, is_aoffi, is_bindless)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     case OpCode::Id::TLD4S: { | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |         constexpr std::size_t num_coords = 2; | 
					
						
							|  |  |  |         const bool is_aoffi = instr.tld4s.UsesMiscMode(TextureMiscMode::AOFFI); | 
					
						
							|  |  |  |         const bool is_depth_compare = instr.tld4s.UsesMiscMode(TextureMiscMode::DC); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         const Node op_a = GetRegister(instr.gpr8); | 
					
						
							|  |  |  |         const Node op_b = GetRegister(instr.gpr20); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
 | 
					
						
							|  |  |  |         std::vector<Node> coords; | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |         std::vector<Node> aoffi; | 
					
						
							|  |  |  |         Node depth_compare; | 
					
						
							|  |  |  |         if (is_depth_compare) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             // Note: TLD4S coordinate encoding works just like TEXS's
 | 
					
						
							|  |  |  |             const Node op_y = GetRegister(instr.gpr8.Value() + 1); | 
					
						
							|  |  |  |             coords.push_back(op_a); | 
					
						
							|  |  |  |             coords.push_back(op_y); | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |             if (is_aoffi) { | 
					
						
							|  |  |  |                 aoffi = GetAoffiCoordinates(op_b, num_coords, true); | 
					
						
							|  |  |  |                 depth_compare = GetRegister(instr.gpr20.Value() + 1); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 depth_compare = op_b; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |             // There's no depth compare
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             coords.push_back(op_a); | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |             if (is_aoffi) { | 
					
						
							|  |  |  |                 coords.push_back(GetRegister(instr.gpr8.Value() + 1)); | 
					
						
							|  |  |  |                 aoffi = GetAoffiCoordinates(op_b, num_coords, true); | 
					
						
							| 
									
										
										
										
											2019-12-11 19:43:43 -04:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 coords.push_back(op_b); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-22 03:30:12 -03:00
										 |  |  |         const Node component = Immediate(static_cast<u32>(instr.tld4s.component)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |         const SamplerInfo info{TextureType::Texture2D, false, is_depth_compare}; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         const Sampler& sampler = *GetSampler(instr.sampler, info); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Node4 values; | 
					
						
							|  |  |  |         for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |             auto coords_copy = coords; | 
					
						
							| 
									
										
										
										
											2019-12-16 02:06:42 -03:00
										 |  |  |             MetaTexture meta{sampler, {}, depth_compare, aoffi, {}, {}, {}, component, element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |         if (instr.tld4s.fp16_flag) { | 
					
						
							|  |  |  |             WriteTexsInstructionHalfFloat(bb, instr, values, true); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             WriteTexsInstructionFloat(bb, instr, values, true); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  |     case OpCode::Id::TXD_B: | 
					
						
							|  |  |  |         is_bindless = true; | 
					
						
							|  |  |  |         [[fallthrough]]; | 
					
						
							|  |  |  |     case OpCode::Id::TXD: { | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.txd.UsesMiscMode(TextureMiscMode::AOFFI), | 
					
						
							|  |  |  |                              "AOFFI is not implemented"); | 
					
						
							| 
									
										
										
										
											2019-11-18 07:34:34 -04:00
										 |  |  |         UNIMPLEMENTED_IF_MSG(instr.txd.is_array != 0, "TXD Array is not implemented"); | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         u64 base_reg = instr.gpr8.Value(); | 
					
						
							|  |  |  |         const auto derivate_reg = instr.gpr20.Value(); | 
					
						
							|  |  |  |         const auto texture_type = instr.txd.texture_type.Value(); | 
					
						
							|  |  |  |         const auto coord_count = GetCoordCount(texture_type); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         const Sampler* sampler = is_bindless | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |                                      ? GetBindlessSampler(base_reg, {{texture_type, false, false}}) | 
					
						
							|  |  |  |                                      : GetSampler(instr.sampler, {{texture_type, false, false}}); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         Node4 values; | 
					
						
							|  |  |  |         if (sampler == nullptr) { | 
					
						
							|  |  |  |             for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |                 values[element] = Immediate(0); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             WriteTexInstructionFloat(bb, instr, values); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  |         if (is_bindless) { | 
					
						
							|  |  |  |             base_reg++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         std::vector<Node> coords; | 
					
						
							|  |  |  |         std::vector<Node> derivates; | 
					
						
							|  |  |  |         for (std::size_t i = 0; i < coord_count; ++i) { | 
					
						
							|  |  |  |             coords.push_back(GetRegister(base_reg + i)); | 
					
						
							|  |  |  |             const std::size_t derivate = i * 2; | 
					
						
							|  |  |  |             derivates.push_back(GetRegister(derivate_reg + derivate)); | 
					
						
							|  |  |  |             derivates.push_back(GetRegister(derivate_reg + derivate + 1)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |             MetaTexture meta{*sampler, {}, {}, {}, derivates, {}, {}, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-11-18 07:34:34 -04:00
										 |  |  |             values[element] = Operation(OperationCode::TextureGradient, std::move(meta), coords); | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         WriteTexInstructionFloat(bb, instr, values); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-26 19:46:11 -04:00
										 |  |  |     case OpCode::Id::TXQ_B: | 
					
						
							|  |  |  |         is_bindless = true; | 
					
						
							| 
									
										
										
										
											2019-04-07 08:30:26 -04:00
										 |  |  |         [[fallthrough]]; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     case OpCode::Id::TXQ: { | 
					
						
							|  |  |  |         // TODO: The new commits on the texture refactor, change the way samplers work.
 | 
					
						
							|  |  |  |         // Sadly, not all texture instructions specify the type of texture their sampler
 | 
					
						
							|  |  |  |         // uses. This must be fixed at a later instance.
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         const Sampler* sampler = | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |             is_bindless ? GetBindlessSampler(instr.gpr8) : GetSampler(instr.sampler); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         if (sampler == nullptr) { | 
					
						
							|  |  |  |             u32 indexer = 0; | 
					
						
							|  |  |  |             for (u32 element = 0; element < 4; ++element) { | 
					
						
							|  |  |  |                 if (!instr.txq.IsComponentEnabled(element)) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const Node value = Immediate(0); | 
					
						
							|  |  |  |                 SetTemporary(bb, indexer++, value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for (u32 i = 0; i < indexer; ++i) { | 
					
						
							|  |  |  |                 SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         u32 indexer = 0; | 
					
						
							|  |  |  |         switch (instr.txq.query_type) { | 
					
						
							|  |  |  |         case Tegra::Shader::TextureQueryType::Dimension: { | 
					
						
							|  |  |  |             for (u32 element = 0; element < 4; ++element) { | 
					
						
							|  |  |  |                 if (!instr.txq.IsComponentEnabled(element)) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |                 MetaTexture meta{*sampler, {}, {}, {}, {}, {}, {}, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |                 const Node value = | 
					
						
							| 
									
										
										
										
											2019-03-26 19:46:11 -04:00
										 |  |  |                     Operation(OperationCode::TextureQueryDimensions, meta, | 
					
						
							|  |  |  |                               GetRegister(instr.gpr8.Value() + (is_bindless ? 1 : 0))); | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |                 SetTemporary(bb, indexer++, value); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             } | 
					
						
							|  |  |  |             for (u32 i = 0; i < indexer; ++i) { | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |                 SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             UNIMPLEMENTED_MSG("Unhandled texture query type: {}", | 
					
						
							|  |  |  |                               static_cast<u32>(instr.txq.query_type.Value())); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-26 19:13:53 -04:00
										 |  |  |     case OpCode::Id::TMML_B: | 
					
						
							|  |  |  |         is_bindless = true; | 
					
						
							| 
									
										
										
										
											2019-04-07 08:30:26 -04:00
										 |  |  |         [[fallthrough]]; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     case OpCode::Id::TMML: { | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV), | 
					
						
							|  |  |  |                              "NDV is not implemented"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto texture_type = instr.tmml.texture_type.Value(); | 
					
						
							|  |  |  |         const bool is_array = instr.tmml.array != 0; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         const Sampler* sampler = | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |             is_bindless ? GetBindlessSampler(instr.gpr20) : GetSampler(instr.sampler); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         if (sampler == nullptr) { | 
					
						
							|  |  |  |             u32 indexer = 0; | 
					
						
							|  |  |  |             for (u32 element = 0; element < 2; ++element) { | 
					
						
							|  |  |  |                 if (!instr.tmml.IsComponentEnabled(element)) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const Node value = Immediate(0); | 
					
						
							|  |  |  |                 SetTemporary(bb, indexer++, value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for (u32 i = 0; i < indexer; ++i) { | 
					
						
							|  |  |  |                 SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         std::vector<Node> coords; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO: Add coordinates for different samplers once other texture types are implemented.
 | 
					
						
							|  |  |  |         switch (texture_type) { | 
					
						
							|  |  |  |         case TextureType::Texture1D: | 
					
						
							|  |  |  |             coords.push_back(GetRegister(instr.gpr8)); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextureType::Texture2D: | 
					
						
							|  |  |  |             coords.push_back(GetRegister(instr.gpr8.Value() + 0)); | 
					
						
							|  |  |  |             coords.push_back(GetRegister(instr.gpr8.Value() + 1)); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Fallback to interpreting as a 2D texture for now
 | 
					
						
							|  |  |  |             coords.push_back(GetRegister(instr.gpr8.Value() + 0)); | 
					
						
							|  |  |  |             coords.push_back(GetRegister(instr.gpr8.Value() + 1)); | 
					
						
							|  |  |  |             texture_type = TextureType::Texture2D; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-28 19:45:19 -04:00
										 |  |  |         u32 indexer = 0; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         for (u32 element = 0; element < 2; ++element) { | 
					
						
							| 
									
										
										
										
											2019-03-28 19:45:19 -04:00
										 |  |  |             if (!instr.tmml.IsComponentEnabled(element)) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             auto params = coords; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |             MetaTexture meta{*sampler, {}, {}, {}, {}, {}, {}, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             const Node value = Operation(OperationCode::TextureQueryLod, meta, std::move(params)); | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |             SetTemporary(bb, indexer++, value); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-28 19:45:19 -04:00
										 |  |  |         for (u32 i = 0; i < indexer; ++i) { | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |             SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-27 20:50:35 -03:00
										 |  |  |     case OpCode::Id::TLD: { | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tld.aoffi, "AOFFI is not implemented"); | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tld.ms, "MS is not implemented"); | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tld.cl, "CL is not implemented"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         WriteTexInstructionFloat(bb, instr, GetTldCode(instr)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     case OpCode::Id::TLDS: { | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |         const TextureType texture_type{instr.tlds.GetTextureType()}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         const bool is_array{instr.tlds.IsArrayTexture()}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI), | 
					
						
							|  |  |  |                              "AOFFI is not implemented"); | 
					
						
							|  |  |  |         UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::MZ), "MZ is not implemented"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 15:47:58 -03:00
										 |  |  |         const Node4 components = GetTldsCode(instr, texture_type, is_array); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (instr.tlds.fp32_flag) { | 
					
						
							|  |  |  |             WriteTexsInstructionFloat(bb, instr, components); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             WriteTexsInstructionHalfFloat(bb, instr, components); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return pc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  | ShaderIR::SamplerInfo ShaderIR::GetSamplerInfo(std::optional<SamplerInfo> sampler_info, u32 offset, | 
					
						
							|  |  |  |                                                std::optional<u32> buffer) { | 
					
						
							| 
									
										
										
										
											2019-09-25 09:53:18 -04:00
										 |  |  |     if (sampler_info) { | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |         return *sampler_info; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const auto sampler = | 
					
						
							|  |  |  |         buffer ? locker.ObtainBindlessSampler(*buffer, offset) : locker.ObtainBoundSampler(offset); | 
					
						
							|  |  |  |     if (!sampler) { | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |         LOG_WARNING(HW_GPU, "Unknown sampler info"); | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |         return SamplerInfo{TextureType::Texture2D, false, false, false}; | 
					
						
							| 
									
										
										
										
											2019-09-25 09:53:18 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     return SamplerInfo{sampler->texture_type, sampler->is_array != 0, sampler->is_shadow != 0, | 
					
						
							|  |  |  |                        sampler->is_buffer != 0}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  | const Sampler* ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler, | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |                                     std::optional<SamplerInfo> sampler_info) { | 
					
						
							|  |  |  |     const auto offset = static_cast<u32>(sampler.index.Value()); | 
					
						
							|  |  |  |     const auto info = GetSamplerInfo(sampler_info, offset); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // If this sampler has already been used, return the existing mapping.
 | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |     const auto it = | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         std::find_if(used_samplers.begin(), used_samplers.end(), | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |                      [offset](const Sampler& entry) { return entry.GetOffset() == offset; }); | 
					
						
							|  |  |  |     if (it != used_samplers.end()) { | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |         ASSERT(!it->IsBindless() && it->GetType() == info.type && it->IsArray() == info.is_array && | 
					
						
							|  |  |  |                it->IsShadow() == info.is_shadow && it->IsBuffer() == info.is_buffer); | 
					
						
							| 
									
										
										
										
											2019-12-16 01:52:33 -03:00
										 |  |  |         return &*it; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Otherwise create a new mapping for this sampler
 | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |     const auto next_index = static_cast<u32>(used_samplers.size()); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     return &used_samplers.emplace_back(next_index, offset, info.type, info.is_array, info.is_shadow, | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |                                        info.is_buffer); | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  | const Sampler* ShaderIR::GetBindlessSampler(Tegra::Shader::Register reg, | 
					
						
							| 
									
										
										
										
											2019-09-25 09:53:18 -04:00
										 |  |  |                                             std::optional<SamplerInfo> sampler_info) { | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  |     const Node sampler_register = GetRegister(reg); | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |     const auto [base_sampler, buffer, offset] = | 
					
						
							| 
									
										
										
										
											2019-03-26 19:13:53 -04:00
										 |  |  |         TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size())); | 
					
						
							| 
									
										
										
										
											2019-05-31 19:14:34 -03:00
										 |  |  |     ASSERT(base_sampler != nullptr); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     if (base_sampler == nullptr) { | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     const auto info = GetSamplerInfo(sampler_info, offset, buffer); | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // If this sampler has already been used, return the existing mapping.
 | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |     const auto it = | 
					
						
							| 
									
										
										
										
											2019-03-26 17:56:16 -04:00
										 |  |  |         std::find_if(used_samplers.begin(), used_samplers.end(), | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |                      [buffer = buffer, offset = offset](const Sampler& entry) { | 
					
						
							|  |  |  |                          return entry.GetBuffer() == buffer && entry.GetOffset() == offset; | 
					
						
							|  |  |  |                      }); | 
					
						
							|  |  |  |     if (it != used_samplers.end()) { | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |         ASSERT(it->IsBindless() && it->GetType() == info.type && it->IsArray() == info.is_array && | 
					
						
							|  |  |  |                it->IsShadow() == info.is_shadow); | 
					
						
							| 
									
										
										
										
											2019-12-16 01:52:33 -03:00
										 |  |  |         return &*it; | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Otherwise create a new mapping for this sampler
 | 
					
						
							| 
									
										
										
										
											2019-10-28 02:31:05 -03:00
										 |  |  |     const auto next_index = static_cast<u32>(used_samplers.size()); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     return &used_samplers.emplace_back(next_index, offset, buffer, info.type, info.is_array, | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |                                        info.is_shadow, info.is_buffer); | 
					
						
							| 
									
										
										
										
											2019-03-26 17:05:23 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 21:14:57 -04:00
										 |  |  | void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     u32 dest_elem = 0; | 
					
						
							|  |  |  |     for (u32 elem = 0; elem < 4; ++elem) { | 
					
						
							| 
									
										
										
										
											2019-10-30 21:14:57 -04:00
										 |  |  |         if (!instr.tex.IsComponentEnabled(elem)) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             // Skip disabled components
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |         SetTemporary(bb, dest_elem++, components[elem]); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     } | 
					
						
							|  |  |  |     // After writing values in temporals, move them to the real registers
 | 
					
						
							|  |  |  |     for (u32 i = 0; i < dest_elem; ++i) { | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |         SetRegister(bb, instr.gpr0.Value() + i, GetTemporary(i)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 10:59:07 -04:00
										 |  |  | void ShaderIR::WriteTexsInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components, | 
					
						
							|  |  |  |                                          bool ignore_mask) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
 | 
					
						
							|  |  |  |     // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u32 dest_elem = 0; | 
					
						
							|  |  |  |     for (u32 component = 0; component < 4; ++component) { | 
					
						
							| 
									
										
										
										
											2019-10-22 10:59:07 -04:00
										 |  |  |         if (!instr.texs.IsComponentEnabled(component) && !ignore_mask) | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |         SetTemporary(bb, dest_elem++, components[component]); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (u32 i = 0; i < dest_elem; ++i) { | 
					
						
							|  |  |  |         if (i < 2) { | 
					
						
							|  |  |  |             // Write the first two swizzle components to gpr0 and gpr0+1
 | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |             SetRegister(bb, instr.gpr0.Value() + i % 2, GetTemporary(i)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             ASSERT(instr.texs.HasTwoDestinations()); | 
					
						
							|  |  |  |             // Write the rest of the swizzle components to gpr28 and gpr28+1
 | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |             SetRegister(bb, instr.gpr28.Value() + i % 2, GetTemporary(i)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ShaderIR::WriteTexsInstructionHalfFloat(NodeBlock& bb, Instruction instr, | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |                                              const Node4& components, bool ignore_mask) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     // TEXS.F16 destionation registers are packed in two registers in pairs (just like any half
 | 
					
						
							|  |  |  |     // float instruction).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Node4 values; | 
					
						
							|  |  |  |     u32 dest_elem = 0; | 
					
						
							|  |  |  |     for (u32 component = 0; component < 4; ++component) { | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |         if (!instr.texs.IsComponentEnabled(component) && !ignore_mask) | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         values[dest_elem++] = components[component]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dest_elem == 0) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::generate(values.begin() + dest_elem, values.end(), [&]() { return Immediate(0); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Node first_value = Operation(OperationCode::HPack2, values[0], values[1]); | 
					
						
							|  |  |  |     if (dest_elem <= 2) { | 
					
						
							|  |  |  |         SetRegister(bb, instr.gpr0, first_value); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |     SetTemporary(bb, 0, first_value); | 
					
						
							|  |  |  |     SetTemporary(bb, 1, Operation(OperationCode::HPack2, values[2], values[3])); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 10:31:17 -04:00
										 |  |  |     SetRegister(bb, instr.gpr0, GetTemporary(0)); | 
					
						
							|  |  |  |     SetRegister(bb, instr.gpr28, GetTemporary(1)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type, | 
					
						
							|  |  |  |                                TextureProcessMode process_mode, std::vector<Node> coords, | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |                                Node array, Node depth_compare, u32 bias_offset, | 
					
						
							|  |  |  |                                std::vector<Node> aoffi, | 
					
						
							|  |  |  |                                std::optional<Tegra::Shader::Register> bindless_reg) { | 
					
						
							| 
									
										
										
										
											2019-06-04 22:44:06 -03:00
										 |  |  |     const auto is_array = static_cast<bool>(array); | 
					
						
							|  |  |  |     const auto is_shadow = static_cast<bool>(depth_compare); | 
					
						
							| 
									
										
										
										
											2019-03-27 07:11:50 -04:00
										 |  |  |     const bool is_bindless = bindless_reg.has_value(); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     UNIMPLEMENTED_IF_MSG((texture_type == TextureType::Texture3D && (is_array || is_shadow)) || | 
					
						
							|  |  |  |                              (texture_type == TextureType::TextureCube && is_array && is_shadow), | 
					
						
							|  |  |  |                          "This method is not supported."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     const SamplerInfo info{texture_type, is_array, is_shadow, false}; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     const Sampler* sampler = | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |         is_bindless ? GetBindlessSampler(*bindless_reg, info) : GetSampler(instr.sampler, info); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     Node4 values; | 
					
						
							|  |  |  |     if (sampler == nullptr) { | 
					
						
							|  |  |  |         for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |             values[element] = Immediate(0); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return values; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const bool lod_needed = process_mode == TextureProcessMode::LZ || | 
					
						
							|  |  |  |                             process_mode == TextureProcessMode::LL || | 
					
						
							|  |  |  |                             process_mode == TextureProcessMode::LLA; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     // LOD selection (either via bias or explicit textureLod) not supported in GL for
 | 
					
						
							|  |  |  |     // sampler2DArrayShadow and samplerCubeArrayShadow.
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     const bool gl_lod_supported = | 
					
						
							|  |  |  |         !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && is_shadow) || | 
					
						
							|  |  |  |           (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && is_shadow)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const OperationCode read_method = | 
					
						
							| 
									
										
										
										
											2019-02-22 03:30:12 -03:00
										 |  |  |         (lod_needed && gl_lod_supported) ? OperationCode::TextureLod : OperationCode::Texture; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     Node bias; | 
					
						
							|  |  |  |     Node lod; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     if (process_mode != TextureProcessMode::None && gl_lod_supported) { | 
					
						
							| 
									
										
										
										
											2019-02-22 03:30:12 -03:00
										 |  |  |         switch (process_mode) { | 
					
						
							|  |  |  |         case TextureProcessMode::LZ: | 
					
						
							|  |  |  |             lod = Immediate(0.0f); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextureProcessMode::LB: | 
					
						
							| 
									
										
										
										
											2019-03-27 07:11:50 -04:00
										 |  |  |             // If present, lod or bias are always stored in the register
 | 
					
						
							|  |  |  |             // indexed by the gpr20 field with an offset depending on the
 | 
					
						
							|  |  |  |             // usage of the other registers
 | 
					
						
							| 
									
										
										
										
											2019-02-22 03:30:12 -03:00
										 |  |  |             bias = GetRegister(instr.gpr20.Value() + bias_offset); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextureProcessMode::LL: | 
					
						
							|  |  |  |             lod = GetRegister(instr.gpr20.Value() + bias_offset); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode)); | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |         auto copy_coords = coords; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, bias, lod, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         values[element] = Operation(read_method, meta, std::move(copy_coords)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return values; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Node4 ShaderIR::GetTexCode(Instruction instr, TextureType texture_type, | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |                            TextureProcessMode process_mode, bool depth_compare, bool is_array, | 
					
						
							| 
									
										
										
										
											2019-03-27 07:11:50 -04:00
										 |  |  |                            bool is_aoffi, std::optional<Tegra::Shader::Register> bindless_reg) { | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     const bool lod_bias_enabled{ | 
					
						
							|  |  |  |         (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ)}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |     const bool is_bindless = bindless_reg.has_value(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     u64 parameter_register = instr.gpr20.Value(); | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |     if (is_bindless) { | 
					
						
							|  |  |  |         ++parameter_register; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const u32 bias_lod_offset = (is_bindless ? 1 : 0); | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     if (lod_bias_enabled) { | 
					
						
							|  |  |  |         ++parameter_register; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement( | 
					
						
							|  |  |  |         texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5); | 
					
						
							|  |  |  |     // If enabled arrays index is always stored in the gpr8 field
 | 
					
						
							|  |  |  |     const u64 array_register = instr.gpr8.Value(); | 
					
						
							|  |  |  |     // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
 | 
					
						
							|  |  |  |     const u64 coord_register = array_register + (is_array ? 1 : 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> coords; | 
					
						
							|  |  |  |     for (std::size_t i = 0; i < coord_count; ++i) { | 
					
						
							|  |  |  |         coords.push_back(GetRegister(coord_register + i)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // 1D.DC in OpenGL the 2nd component is ignored.
 | 
					
						
							|  |  |  |     if (depth_compare && !is_array && texture_type == TextureType::Texture1D) { | 
					
						
							|  |  |  |         coords.push_back(Immediate(0.0f)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Node array = is_array ? GetRegister(array_register) : nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     std::vector<Node> aoffi; | 
					
						
							|  |  |  |     if (is_aoffi) { | 
					
						
							|  |  |  |         aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     Node dc{}; | 
					
						
							|  |  |  |     if (depth_compare) { | 
					
						
							|  |  |  |         // Depth is always stored in the register signaled by gpr20 or in the next register if lod
 | 
					
						
							|  |  |  |         // or bias are used
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |         dc = GetRegister(parameter_register++); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |     return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_lod_offset, | 
					
						
							|  |  |  |                           aoffi, bindless_reg); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Node4 ShaderIR::GetTexsCode(Instruction instr, TextureType texture_type, | 
					
						
							|  |  |  |                             TextureProcessMode process_mode, bool depth_compare, bool is_array) { | 
					
						
							|  |  |  |     const bool lod_bias_enabled = | 
					
						
							|  |  |  |         (process_mode != TextureProcessMode::None && process_mode != TextureProcessMode::LZ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const auto [coord_count, total_coord_count] = ValidateAndGetCoordinateElement( | 
					
						
							|  |  |  |         texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4); | 
					
						
							|  |  |  |     // If enabled arrays index is always stored in the gpr8 field
 | 
					
						
							|  |  |  |     const u64 array_register = instr.gpr8.Value(); | 
					
						
							|  |  |  |     // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
 | 
					
						
							|  |  |  |     const u64 coord_register = array_register + (is_array ? 1 : 0); | 
					
						
							|  |  |  |     const u64 last_coord_register = | 
					
						
							|  |  |  |         (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2)) | 
					
						
							|  |  |  |             ? static_cast<u64>(instr.gpr20.Value()) | 
					
						
							|  |  |  |             : coord_register + 1; | 
					
						
							|  |  |  |     const u32 bias_offset = coord_count > 2 ? 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> coords; | 
					
						
							|  |  |  |     for (std::size_t i = 0; i < coord_count; ++i) { | 
					
						
							|  |  |  |         const bool last = (i == (coord_count - 1)) && (coord_count > 1); | 
					
						
							|  |  |  |         coords.push_back(GetRegister(last ? last_coord_register : coord_register + i)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Node array = is_array ? GetRegister(array_register) : nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Node dc{}; | 
					
						
							|  |  |  |     if (depth_compare) { | 
					
						
							|  |  |  |         // Depth is always stored in the register signaled by gpr20 or in the next register if lod
 | 
					
						
							|  |  |  |         // or bias are used
 | 
					
						
							|  |  |  |         const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0); | 
					
						
							|  |  |  |         dc = GetRegister(depth_register); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 12:07:56 -04:00
										 |  |  |     return GetTextureCode(instr, texture_type, process_mode, coords, array, dc, bias_offset, {}, | 
					
						
							|  |  |  |                           {}); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Node4 ShaderIR::GetTld4Code(Instruction instr, TextureType texture_type, bool depth_compare, | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |                             bool is_array, bool is_aoffi, bool is_bindless) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     const std::size_t coord_count = GetCoordCount(texture_type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If enabled arrays index is always stored in the gpr8 field
 | 
					
						
							|  |  |  |     const u64 array_register = instr.gpr8.Value(); | 
					
						
							|  |  |  |     // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
 | 
					
						
							|  |  |  |     const u64 coord_register = array_register + (is_array ? 1 : 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> coords; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     for (std::size_t i = 0; i < coord_count; ++i) { | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         coords.push_back(GetRegister(coord_register + i)); | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u64 parameter_register = instr.gpr20.Value(); | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 04:32:43 -03:00
										 |  |  |     const SamplerInfo info{texture_type, is_array, depth_compare, false}; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     const Sampler* sampler = is_bindless ? GetBindlessSampler(parameter_register++, info) | 
					
						
							| 
									
										
										
										
											2019-12-11 18:12:29 -04:00
										 |  |  |                                          : GetSampler(instr.sampler, info); | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     Node4 values; | 
					
						
							|  |  |  |     if (sampler == nullptr) { | 
					
						
							|  |  |  |         for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |             values[element] = Immediate(0); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return values; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  |     std::vector<Node> aoffi; | 
					
						
							|  |  |  |     if (is_aoffi) { | 
					
						
							|  |  |  |         aoffi = GetAoffiCoordinates(GetRegister(parameter_register++), coord_count, true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Node dc{}; | 
					
						
							|  |  |  |     if (depth_compare) { | 
					
						
							|  |  |  |         dc = GetRegister(parameter_register++); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |     const Node component = is_bindless ? Immediate(static_cast<u32>(instr.tld4_b.component)) | 
					
						
							|  |  |  |                                        : Immediate(static_cast<u32>(instr.tld4.component)); | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |         auto coords_copy = coords; | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |         MetaTexture meta{*sampler, GetRegister(array_register), dc, aoffi, {}, {}, {}, component, | 
					
						
							| 
									
										
										
										
											2019-10-29 20:48:18 -04:00
										 |  |  |                          element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         values[element] = Operation(OperationCode::TextureGather, meta, std::move(coords_copy)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return values; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-27 20:50:35 -03:00
										 |  |  | Node4 ShaderIR::GetTldCode(Tegra::Shader::Instruction instr) { | 
					
						
							|  |  |  |     const auto texture_type{instr.tld.texture_type}; | 
					
						
							|  |  |  |     const bool is_array{instr.tld.is_array}; | 
					
						
							|  |  |  |     const bool lod_enabled{instr.tld.GetTextureProcessMode() == TextureProcessMode::LL}; | 
					
						
							|  |  |  |     const std::size_t coord_count{GetCoordCount(texture_type)}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u64 gpr8_cursor{instr.gpr8.Value()}; | 
					
						
							|  |  |  |     const Node array_register{is_array ? GetRegister(gpr8_cursor++) : nullptr}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> coords; | 
					
						
							| 
									
										
										
										
											2019-06-24 02:05:05 -03:00
										 |  |  |     coords.reserve(coord_count); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:50:35 -03:00
										 |  |  |     for (std::size_t i = 0; i < coord_count; ++i) { | 
					
						
							|  |  |  |         coords.push_back(GetRegister(gpr8_cursor++)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u64 gpr20_cursor{instr.gpr20.Value()}; | 
					
						
							|  |  |  |     // const Node bindless_register{is_bindless ? GetRegister(gpr20_cursor++) : nullptr};
 | 
					
						
							|  |  |  |     const Node lod{lod_enabled ? GetRegister(gpr20_cursor++) : Immediate(0u)}; | 
					
						
							|  |  |  |     // const Node aoffi_register{is_aoffi ? GetRegister(gpr20_cursor++) : nullptr};
 | 
					
						
							|  |  |  |     // const Node multisample{is_multisample ? GetRegister(gpr20_cursor++) : nullptr};
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     const auto& sampler = *GetSampler(instr.sampler); | 
					
						
							| 
									
										
										
										
											2019-04-27 20:50:35 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Node4 values; | 
					
						
							|  |  |  |     for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |         auto coords_copy = coords; | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  |         MetaTexture meta{sampler, array_register, {}, {}, {}, {}, lod, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-04-27 20:50:35 -03:00
										 |  |  |         values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return values; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | Node4 ShaderIR::GetTldsCode(Instruction instr, TextureType texture_type, bool is_array) { | 
					
						
							| 
									
										
										
										
											2019-12-11 13:07:30 -04:00
										 |  |  |     const Sampler& sampler = *GetSampler(instr.sampler); | 
					
						
							| 
									
										
										
										
											2019-11-06 22:43:02 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |     const std::size_t type_coord_count = GetCoordCount(texture_type); | 
					
						
							|  |  |  |     const bool lod_enabled = instr.tlds.GetTextureProcessMode() == TextureProcessMode::LL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If enabled arrays index is always stored in the gpr8 field
 | 
					
						
							|  |  |  |     const u64 array_register = instr.gpr8.Value(); | 
					
						
							|  |  |  |     // if is array gpr20 is used
 | 
					
						
							|  |  |  |     const u64 coord_register = is_array ? instr.gpr20.Value() : instr.gpr8.Value(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const u64 last_coord_register = | 
					
						
							|  |  |  |         ((type_coord_count > 2) || (type_coord_count == 2 && !lod_enabled)) && !is_array | 
					
						
							|  |  |  |             ? static_cast<u64>(instr.gpr20.Value()) | 
					
						
							|  |  |  |             : coord_register + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> coords; | 
					
						
							|  |  |  |     for (std::size_t i = 0; i < type_coord_count; ++i) { | 
					
						
							|  |  |  |         const bool last = (i == (type_coord_count - 1)) && (type_coord_count > 1); | 
					
						
							|  |  |  |         coords.push_back(GetRegister(last ? last_coord_register : coord_register + i)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const Node array = is_array ? GetRegister(array_register) : nullptr; | 
					
						
							|  |  |  |     // When lod is used always is in gpr20
 | 
					
						
							|  |  |  |     const Node lod = lod_enabled ? GetRegister(instr.gpr20) : Immediate(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 22:43:02 -03:00
										 |  |  |     // Fill empty entries from the guest sampler.
 | 
					
						
							|  |  |  |     const std::size_t entry_coord_count = GetCoordCount(sampler.GetType()); | 
					
						
							|  |  |  |     if (type_coord_count != entry_coord_count) { | 
					
						
							|  |  |  |         LOG_WARNING(HW_GPU, "Bound and built texture types mismatch"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (std::size_t i = type_coord_count; i < entry_coord_count; ++i) { | 
					
						
							|  |  |  |         coords.push_back(GetRegister(Register::ZeroIndex)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Node4 values; | 
					
						
							|  |  |  |     for (u32 element = 0; element < values.size(); ++element) { | 
					
						
							|  |  |  |         auto coords_copy = coords; | 
					
						
							| 
									
										
										
										
											2019-11-12 13:43:08 -04:00
										 |  |  |         MetaTexture meta{sampler, array, {}, {}, {}, {}, lod, {}, element}; | 
					
						
							| 
									
										
										
										
											2019-02-22 02:19:45 -03:00
										 |  |  |         values[element] = Operation(OperationCode::TexelFetch, meta, std::move(coords_copy)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return values; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::tuple<std::size_t, std::size_t> ShaderIR::ValidateAndGetCoordinateElement( | 
					
						
							|  |  |  |     TextureType texture_type, bool depth_compare, bool is_array, bool lod_bias_enabled, | 
					
						
							|  |  |  |     std::size_t max_coords, std::size_t max_inputs) { | 
					
						
							|  |  |  |     const std::size_t coord_count = GetCoordCount(texture_type); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0); | 
					
						
							|  |  |  |     const std::size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0); | 
					
						
							|  |  |  |     if (total_coord_count > max_coords || total_reg_count > max_inputs) { | 
					
						
							|  |  |  |         UNIMPLEMENTED_MSG("Unsupported Texture operation"); | 
					
						
							|  |  |  |         total_coord_count = std::min(total_coord_count, max_coords); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // 1D.DC OpenGL is using a vec3 but 2nd component is ignored later.
 | 
					
						
							|  |  |  |     total_coord_count += | 
					
						
							|  |  |  |         (depth_compare && !is_array && texture_type == TextureType::Texture1D) ? 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {coord_count, total_coord_count}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  | std::vector<Node> ShaderIR::GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count, | 
					
						
							|  |  |  |                                                 bool is_tld4) { | 
					
						
							|  |  |  |     const auto [coord_offsets, size, wrap_value, | 
					
						
							|  |  |  |                 diff_value] = [is_tld4]() -> std::tuple<std::array<u32, 3>, u32, s32, s32> { | 
					
						
							|  |  |  |         if (is_tld4) { | 
					
						
							|  |  |  |             return {{0, 8, 16}, 6, 32, 64}; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return {{0, 4, 8}, 4, 8, 16}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }(); | 
					
						
							| 
									
										
										
										
											2019-03-31 00:12:54 -03:00
										 |  |  |     const u32 mask = (1U << size) - 1; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:36:54 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Node> aoffi; | 
					
						
							|  |  |  |     aoffi.reserve(coord_count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const auto aoffi_immediate{ | 
					
						
							|  |  |  |         TrackImmediate(aoffi_reg, global_code, static_cast<s64>(global_code.size()))}; | 
					
						
							|  |  |  |     if (!aoffi_immediate) { | 
					
						
							|  |  |  |         // Variable access, not supported on AMD.
 | 
					
						
							|  |  |  |         LOG_WARNING(HW_GPU, | 
					
						
							|  |  |  |                     "AOFFI constant folding failed, some hardware might have graphical issues"); | 
					
						
							|  |  |  |         for (std::size_t coord = 0; coord < coord_count; ++coord) { | 
					
						
							|  |  |  |             const Node value = BitfieldExtract(aoffi_reg, coord_offsets.at(coord), size); | 
					
						
							|  |  |  |             const Node condition = | 
					
						
							|  |  |  |                 Operation(OperationCode::LogicalIGreaterEqual, value, Immediate(wrap_value)); | 
					
						
							|  |  |  |             const Node negative = Operation(OperationCode::IAdd, value, Immediate(-diff_value)); | 
					
						
							|  |  |  |             aoffi.push_back(Operation(OperationCode::Select, condition, negative, value)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return aoffi; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (std::size_t coord = 0; coord < coord_count; ++coord) { | 
					
						
							|  |  |  |         s32 value = (*aoffi_immediate >> coord_offsets.at(coord)) & mask; | 
					
						
							|  |  |  |         if (value >= wrap_value) { | 
					
						
							|  |  |  |             value -= diff_value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         aoffi.push_back(Immediate(value)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return aoffi; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 00:12:54 -03:00
										 |  |  | } // namespace VideoCommon::Shader
 |