Files
eden/src/shader_recompiler/frontend/maxwell/translate/impl/texture_gradient.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

184 lines
5.9 KiB
C++
Raw Normal View History

2026-03-17 08:42:34 +00:00
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2021-03-29 02:52:52 +02:00
#include "common/bit_field.h"
#include "common/common_types.h"
#include "shader_recompiler/frontend/ir/modifiers.h"
#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
namespace Shader::Maxwell {
namespace {
2026-03-17 08:42:34 +00:00
enum class TextureGradientType : u64 {
2021-03-29 02:52:52 +02:00
_1D,
ARRAY_1D,
_2D,
ARRAY_2D,
_3D,
ARRAY_3D,
CUBE,
ARRAY_CUBE,
};
2026-03-17 08:42:34 +00:00
Shader::TextureType GetType(TextureGradientType type) {
2021-03-29 02:52:52 +02:00
switch (type) {
2026-03-17 08:42:34 +00:00
case TextureGradientType::_1D:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::Color1D;
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_1D:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::ColorArray1D;
2026-03-17 08:42:34 +00:00
case TextureGradientType::_2D:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::Color2D;
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_2D:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::ColorArray2D;
2026-03-17 08:42:34 +00:00
case TextureGradientType::_3D:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::Color3D;
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_3D:
2021-03-29 02:52:52 +02:00
throw NotImplementedException("3D array texture type");
2026-03-17 08:42:34 +00:00
case TextureGradientType::CUBE:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::ColorCube;
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_CUBE:
2021-04-09 01:45:39 -03:00
return Shader::TextureType::ColorArrayCube;
2021-03-29 02:52:52 +02:00
}
throw NotImplementedException("Invalid texture type {}", type);
}
IR::Value MakeOffset(TranslatorVisitor& v, IR::Reg reg, bool has_lod_clamp) {
const IR::U32 value{v.X(reg)};
2021-03-30 08:41:21 +02:00
const u32 base{has_lod_clamp ? 12U : 16U};
2021-03-29 02:52:52 +02:00
return v.ir.CompositeConstruct(
v.ir.BitFieldExtract(value, v.ir.Imm32(base), v.ir.Imm32(4), true),
v.ir.BitFieldExtract(value, v.ir.Imm32(base + 4), v.ir.Imm32(4), true));
}
2026-03-17 08:42:34 +00:00
void TextureGatherImpl(TranslatorVisitor& v, u64 insn, bool is_bindless) {
2021-03-29 02:52:52 +02:00
union {
u64 raw;
BitField<49, 1, u64> nodep;
BitField<35, 1, u64> aoffi;
BitField<50, 1, u64> lc;
BitField<51, 3, IR::Pred> sparse_pred;
BitField<0, 8, IR::Reg> dest_reg;
BitField<8, 8, IR::Reg> coord_reg;
BitField<20, 8, IR::Reg> derivative_reg;
2026-03-17 08:42:34 +00:00
BitField<28, 3, TextureGradientType> type;
2021-03-29 02:52:52 +02:00
BitField<31, 4, u64> mask;
BitField<36, 13, u64> cbuf_offset;
} const txd{insn};
const bool has_lod_clamp = txd.lc != 0;
if (has_lod_clamp) {
throw NotImplementedException("TXD.LC - CLAMP is not implemented");
}
IR::Value coords;
u32 num_derivatives{};
2021-03-30 08:41:21 +02:00
IR::Reg base_reg{txd.coord_reg};
2021-03-29 02:52:52 +02:00
IR::Reg last_reg;
IR::Value handle;
2021-03-30 08:41:21 +02:00
if (is_bindless) {
2021-03-29 02:52:52 +02:00
handle = v.X(base_reg++);
2021-03-30 08:41:21 +02:00
} else {
handle = v.ir.Imm32(static_cast<u32>(txd.cbuf_offset.Value() * 4));
2021-03-29 02:52:52 +02:00
}
const auto read_array{[&]() -> IR::F32 {
2021-03-30 08:41:21 +02:00
const IR::U32 base{v.ir.Imm32(0)};
const IR::U32 count{v.ir.Imm32(has_lod_clamp ? 12 : 16)};
const IR::U32 array_index{v.ir.BitFieldExtract(v.X(last_reg), base, count)};
return v.ir.ConvertUToF(32, 16, array_index);
2021-03-29 02:52:52 +02:00
}};
switch (txd.type) {
2026-03-17 08:42:34 +00:00
case TextureGradientType::_1D: {
2021-03-29 02:52:52 +02:00
coords = v.F(base_reg);
num_derivatives = 1;
2021-03-29 02:52:52 +02:00
last_reg = base_reg + 1;
break;
}
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_1D: {
2021-03-29 02:52:52 +02:00
last_reg = base_reg + 1;
coords = v.ir.CompositeConstruct(v.F(base_reg), read_array());
num_derivatives = 1;
2021-03-29 02:52:52 +02:00
break;
}
2026-03-17 08:42:34 +00:00
case TextureGradientType::_2D: {
2021-03-29 02:52:52 +02:00
last_reg = base_reg + 2;
coords = v.ir.CompositeConstruct(v.F(base_reg), v.F(base_reg + 1));
num_derivatives = 2;
2021-03-29 02:52:52 +02:00
break;
}
2026-03-17 08:42:34 +00:00
case TextureGradientType::ARRAY_2D: {
2021-03-29 02:52:52 +02:00
last_reg = base_reg + 2;
coords = v.ir.CompositeConstruct(v.F(base_reg), v.F(base_reg + 1), read_array());
num_derivatives = 2;
2021-03-29 02:52:52 +02:00
break;
}
default:
throw NotImplementedException("Invalid texture type");
}
const IR::Reg derivative_reg{txd.derivative_reg};
IR::Value derivatives;
switch (num_derivatives) {
2021-03-29 02:52:52 +02:00
case 1: {
derivatives = v.ir.CompositeConstruct(v.F(derivative_reg), v.F(derivative_reg + 1));
2021-03-29 02:52:52 +02:00
break;
}
case 2: {
derivatives = v.ir.CompositeConstruct(v.F(derivative_reg), v.F(derivative_reg + 1),
v.F(derivative_reg + 2), v.F(derivative_reg + 3));
2021-03-29 02:52:52 +02:00
break;
}
default:
throw NotImplementedException("Invalid texture type");
}
IR::Value offset;
if (txd.aoffi != 0) {
offset = MakeOffset(v, last_reg, has_lod_clamp);
}
IR::F32 lod_clamp;
if (has_lod_clamp) {
2021-03-30 08:41:21 +02:00
// Lod Clamp is a Fixed Point 4.8, we need to transform it to float.
// to convert a fixed point, float(value) / float(1 << fixed_point)
// in this case the fixed_point is 8.
const IR::F32 conv4_8fixp_f{v.ir.Imm32(static_cast<f32>(1U << 8))};
const IR::F32 fixp_lc{v.ir.ConvertUToF(
32, 16, v.ir.BitFieldExtract(v.X(last_reg), v.ir.Imm32(20), v.ir.Imm32(12)))};
lod_clamp = v.ir.FPMul(fixp_lc, conv4_8fixp_f);
2021-03-29 02:52:52 +02:00
}
IR::TextureInstInfo info{};
2021-04-09 01:45:39 -03:00
info.type.Assign(GetType(txd.type));
info.num_derivatives.Assign(num_derivatives);
2021-03-29 02:52:52 +02:00
info.has_lod_clamp.Assign(has_lod_clamp ? 1 : 0);
const IR::Value sample{
v.ir.ImageGradient(handle, coords, derivatives, offset, lod_clamp, info)};
2021-03-29 02:52:52 +02:00
IR::Reg dest_reg{txd.dest_reg};
for (size_t element = 0; element < 4; ++element) {
if (((txd.mask >> element) & 1) == 0) {
continue;
}
v.F(dest_reg, IR::F32{v.ir.CompositeExtract(sample, element)});
++dest_reg;
}
if (txd.sparse_pred != IR::Pred::PT) {
v.ir.SetPred(txd.sparse_pred, v.ir.LogicalNot(v.ir.GetSparseFromOp(sample)));
}
}
} // Anonymous namespace
void TranslatorVisitor::TXD(u64 insn) {
2026-03-17 08:42:34 +00:00
TextureGatherImpl(*this, insn, false);
2021-03-29 02:52:52 +02:00
}
void TranslatorVisitor::TXD_b(u64 insn) {
2026-03-17 08:42:34 +00:00
TextureGatherImpl(*this, insn, true);
2021-03-29 02:52:52 +02:00
}
} // namespace Shader::Maxwell