1
0
Fork 0
forked from suyu/suyu

gl_shader_decompiler: Partially implement several non-2D texture types (Subv).

This commit is contained in:
bunnei 2018-09-06 18:45:49 -04:00
parent 05f6f59ffb
commit 0731383124
2 changed files with 143 additions and 32 deletions

View file

@ -443,13 +443,12 @@ public:
} }
declarations.AddNewLine(); declarations.AddNewLine();
// Append the sampler2D array for the used textures. const auto& samplers = GetSamplers();
const size_t num_samplers = used_samplers.size(); for (const auto& sampler : samplers) {
if (num_samplers > 0) { declarations.AddLine("uniform " + sampler.GetTypeString() + ' ' + sampler.GetName() +
declarations.AddLine("uniform sampler2D " + SamplerEntry::GetArrayName(stage) + '[' + ';');
std::to_string(num_samplers) + "];");
declarations.AddNewLine();
} }
declarations.AddNewLine();
} }
/// Returns a list of constant buffer declarations /// Returns a list of constant buffer declarations
@ -461,13 +460,14 @@ public:
} }
/// Returns a list of samplers used in the shader /// Returns a list of samplers used in the shader
std::vector<SamplerEntry> GetSamplers() const { const std::vector<SamplerEntry>& GetSamplers() const {
return used_samplers; return used_samplers;
} }
/// Returns the GLSL sampler used for the input shader sampler, and creates a new one if /// Returns the GLSL sampler used for the input shader sampler, and creates a new one if
/// necessary. /// necessary.
std::string AccessSampler(const Sampler& sampler) { std::string AccessSampler(const Sampler& sampler, Tegra::Shader::TextureType type,
bool is_array) {
size_t offset = static_cast<size_t>(sampler.index.Value()); size_t offset = static_cast<size_t>(sampler.index.Value());
// If this sampler has already been used, return the existing mapping. // If this sampler has already been used, return the existing mapping.
@ -476,12 +476,13 @@ public:
[&](const SamplerEntry& entry) { return entry.GetOffset() == offset; }); [&](const SamplerEntry& entry) { return entry.GetOffset() == offset; });
if (itr != used_samplers.end()) { if (itr != used_samplers.end()) {
ASSERT(itr->GetType() == type && itr->IsArray() == is_array);
return itr->GetName(); return itr->GetName();
} }
// Otherwise create a new mapping for this sampler // Otherwise create a new mapping for this sampler
size_t next_index = used_samplers.size(); size_t next_index = used_samplers.size();
SamplerEntry entry{stage, offset, next_index}; SamplerEntry entry{stage, offset, next_index, type, is_array};
used_samplers.emplace_back(entry); used_samplers.emplace_back(entry);
return entry.GetName(); return entry.GetName();
} }
@ -722,8 +723,8 @@ private:
} }
/// Generates code representing a texture sampler. /// Generates code representing a texture sampler.
std::string GetSampler(const Sampler& sampler) { std::string GetSampler(const Sampler& sampler, Tegra::Shader::TextureType type, bool is_array) {
return regs.AccessSampler(sampler); return regs.AccessSampler(sampler, type, is_array);
} }
/** /**
@ -1753,10 +1754,35 @@ private:
break; break;
} }
case OpCode::Id::TEX: { case OpCode::Id::TEX: {
const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8); ASSERT_MSG(instr.tex.array == 0, "TEX arrays unimplemented");
const std::string op_b = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1); Tegra::Shader::TextureType texture_type{instr.tex.texture_type};
const std::string sampler = GetSampler(instr.sampler); std::string coord;
const std::string coord = "vec2 coords = vec2(" + op_a + ", " + op_b + ");";
switch (texture_type) {
case Tegra::Shader::TextureType::Texture1D: {
std::string x = regs.GetRegisterAsFloat(instr.gpr8);
coord = "float coords = " + x + ';';
break;
}
case Tegra::Shader::TextureType::Texture2D: {
std::string x = regs.GetRegisterAsFloat(instr.gpr8);
std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
coord = "vec2 coords = vec2(" + x + ", " + y + ");";
break;
}
default:
LOG_CRITICAL(HW_GPU, "Unhandled texture type {}",
static_cast<u32>(texture_type));
UNREACHABLE();
// Fallback to interpreting as a 2D texture for now
std::string x = regs.GetRegisterAsFloat(instr.gpr8);
std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
coord = "vec2 coords = vec2(" + x + ", " + y + ");";
texture_type = Tegra::Shader::TextureType::Texture2D;
}
const std::string sampler = GetSampler(instr.sampler, texture_type, false);
// Add an extra scope and declare the texture coords inside to prevent // Add an extra scope and declare the texture coords inside to prevent
// overwriting them in case they are used as outputs of the texs instruction. // overwriting them in case they are used as outputs of the texs instruction.
shader.AddLine("{"); shader.AddLine("{");
@ -1778,20 +1804,65 @@ private:
break; break;
} }
case OpCode::Id::TEXS: { case OpCode::Id::TEXS: {
const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8); std::string coord;
const std::string op_b = regs.GetRegisterAsFloat(instr.gpr20); Tegra::Shader::TextureType texture_type{instr.texs.GetTextureType()};
const std::string sampler = GetSampler(instr.sampler); bool is_array{instr.texs.IsArrayTexture()};
const std::string coord = "vec2 coords = vec2(" + op_a + ", " + op_b + ");";
switch (texture_type) {
case Tegra::Shader::TextureType::Texture2D: {
if (is_array) {
std::string index = regs.GetRegisterAsInteger(instr.gpr8);
std::string x = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
std::string y = regs.GetRegisterAsFloat(instr.gpr20);
coord = "vec3 coords = vec3(" + x + ", " + y + ", " + index + ");";
} else {
std::string x = regs.GetRegisterAsFloat(instr.gpr8);
std::string y = regs.GetRegisterAsFloat(instr.gpr20);
coord = "vec2 coords = vec2(" + x + ", " + y + ");";
}
break;
}
default:
LOG_CRITICAL(HW_GPU, "Unhandled texture type {}",
static_cast<u32>(texture_type));
UNREACHABLE();
// Fallback to interpreting as a 2D texture for now
std::string x = regs.GetRegisterAsFloat(instr.gpr8);
std::string y = regs.GetRegisterAsFloat(instr.gpr20);
coord = "vec2 coords = vec2(" + x + ", " + y + ");";
texture_type = Tegra::Shader::TextureType::Texture2D;
is_array = false;
}
const std::string sampler = GetSampler(instr.sampler, texture_type, is_array);
const std::string texture = "texture(" + sampler + ", coords)"; const std::string texture = "texture(" + sampler + ", coords)";
WriteTexsInstruction(instr, coord, texture); WriteTexsInstruction(instr, coord, texture);
break; break;
} }
case OpCode::Id::TLDS: { case OpCode::Id::TLDS: {
const std::string op_a = regs.GetRegisterAsInteger(instr.gpr8); ASSERT(instr.tlds.GetTextureType() == Tegra::Shader::TextureType::Texture2D);
const std::string op_b = regs.GetRegisterAsInteger(instr.gpr20); ASSERT(instr.tlds.IsArrayTexture() == false);
const std::string sampler = GetSampler(instr.sampler); std::string coord;
const std::string coord = "ivec2 coords = ivec2(" + op_a + ", " + op_b + ");";
switch (instr.tlds.GetTextureType()) {
case Tegra::Shader::TextureType::Texture2D: {
if (instr.tlds.IsArrayTexture()) {
LOG_CRITICAL(HW_GPU, "Unhandled 2d array texture");
UNREACHABLE();
} else {
std::string x = regs.GetRegisterAsInteger(instr.gpr8);
std::string y = regs.GetRegisterAsInteger(instr.gpr20);
coord = "ivec2 coords = ivec2(" + x + ", " + y + ");";
}
break;
}
default:
LOG_CRITICAL(HW_GPU, "Unhandled texture type {}",
static_cast<u32>(instr.tlds.GetTextureType()));
UNREACHABLE();
}
const std::string sampler = GetSampler(instr.sampler, instr.tlds.GetTextureType(),
instr.tlds.IsArrayTexture());
const std::string texture = "texelFetch(" + sampler + ", coords, 0)"; const std::string texture = "texelFetch(" + sampler + ", coords, 0)";
WriteTexsInstruction(instr, coord, texture); WriteTexsInstruction(instr, coord, texture);
break; break;
@ -1799,7 +1870,7 @@ private:
case OpCode::Id::TLD4: { case OpCode::Id::TLD4: {
ASSERT(instr.tld4.texture_type == Tegra::Shader::TextureType::Texture2D); ASSERT(instr.tld4.texture_type == Tegra::Shader::TextureType::Texture2D);
ASSERT(instr.tld4.array == 0); ASSERT(instr.tld4.array == 0);
std::string coord{}; std::string coord;
switch (instr.tld4.texture_type) { switch (instr.tld4.texture_type) {
case Tegra::Shader::TextureType::Texture2D: { case Tegra::Shader::TextureType::Texture2D: {
@ -1814,7 +1885,8 @@ private:
UNREACHABLE(); UNREACHABLE();
} }
const std::string sampler = GetSampler(instr.sampler); const std::string sampler =
GetSampler(instr.sampler, instr.tld4.texture_type, false);
// Add an extra scope and declare the texture coords inside to prevent // Add an extra scope and declare the texture coords inside to prevent
// overwriting them in case they are used as outputs of the texs instruction. // overwriting them in case they are used as outputs of the texs instruction.
shader.AddLine("{"); shader.AddLine("{");
@ -1840,7 +1912,8 @@ private:
const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8); const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
const std::string op_b = regs.GetRegisterAsFloat(instr.gpr20); const std::string op_b = regs.GetRegisterAsFloat(instr.gpr20);
// TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction. // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
const std::string sampler = GetSampler(instr.sampler); const std::string sampler =
GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false);
const std::string coord = "vec2 coords = vec2(" + op_a + ", " + op_b + ");"; const std::string coord = "vec2 coords = vec2(" + op_a + ", " + op_b + ");";
const std::string texture = "textureGather(" + sampler + ", coords, " + const std::string texture = "textureGather(" + sampler + ", coords, " +
std::to_string(instr.tld4s.component) + ')'; std::to_string(instr.tld4s.component) + ')';

View file

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "video_core/engines/shader_bytecode.h"
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
@ -73,8 +74,9 @@ class SamplerEntry {
using Maxwell = Tegra::Engines::Maxwell3D::Regs; using Maxwell = Tegra::Engines::Maxwell3D::Regs;
public: public:
SamplerEntry(Maxwell::ShaderStage stage, size_t offset, size_t index) SamplerEntry(Maxwell::ShaderStage stage, size_t offset, size_t index,
: offset(offset), stage(stage), sampler_index(index) {} Tegra::Shader::TextureType type, bool is_array)
: offset(offset), stage(stage), sampler_index(index), type(type), is_array(is_array) {}
size_t GetOffset() const { size_t GetOffset() const {
return offset; return offset;
@ -89,8 +91,41 @@ public:
} }
std::string GetName() const { std::string GetName() const {
return std::string(TextureSamplerNames[static_cast<size_t>(stage)]) + '[' + return std::string(TextureSamplerNames[static_cast<size_t>(stage)]) + '_' +
std::to_string(sampler_index) + ']'; std::to_string(sampler_index);
}
std::string GetTypeString() const {
using Tegra::Shader::TextureType;
std::string glsl_type;
switch (type) {
case TextureType::Texture1D:
glsl_type = "sampler1D";
break;
case TextureType::Texture2D:
glsl_type = "sampler2D";
break;
case TextureType::Texture3D:
glsl_type = "sampler3D";
break;
case TextureType::TextureCube:
glsl_type = "samplerCube";
break;
default:
UNIMPLEMENTED();
}
if (is_array)
glsl_type += "Array";
return glsl_type;
}
Tegra::Shader::TextureType GetType() const {
return type;
}
bool IsArray() const {
return is_array;
} }
u32 GetHash() const { u32 GetHash() const {
@ -105,11 +140,14 @@ private:
static constexpr std::array<const char*, Maxwell::MaxShaderStage> TextureSamplerNames = { static constexpr std::array<const char*, Maxwell::MaxShaderStage> TextureSamplerNames = {
"tex_vs", "tex_tessc", "tex_tesse", "tex_gs", "tex_fs", "tex_vs", "tex_tessc", "tex_tesse", "tex_gs", "tex_fs",
}; };
/// Offset in TSC memory from which to read the sampler object, as specified by the sampling /// Offset in TSC memory from which to read the sampler object, as specified by the sampling
/// instruction. /// instruction.
size_t offset; size_t offset;
Maxwell::ShaderStage stage; ///< Shader stage where this sampler was used. Maxwell::ShaderStage stage; ///< Shader stage where this sampler was used.
size_t sampler_index; ///< Value used to index into the generated GLSL sampler array. size_t sampler_index; ///< Value used to index into the generated GLSL sampler array.
Tegra::Shader::TextureType type; ///< The type used to sample this texture (Texture2D, etc)
bool is_array; ///< Whether the texture is being sampled as an array texture or not.
}; };
struct ShaderEntries { struct ShaderEntries {