mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #790 from CnRJay/feat/image-msaa-load
feat(shader): implement image_msaa_load
This commit is contained in:
@@ -831,3 +831,14 @@ if(BUILD_TESTING)
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_test_executable(image_msaa_load_tests ${LIBS_INCLUDE_DIR}/tests/ImageMsaaLoad.cpp)
|
||||
target_link_libraries(image_msaa_load_tests PRIVATE shader_recompiler_agc_driver libc)
|
||||
configure_windows_unwind(image_msaa_load_tests)
|
||||
add_test(NAME image_msaa_load COMMAND image_msaa_load_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(image_msaa_load PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "Translation/TranslationContext.hpp"
|
||||
#include "RdnaDecoder/RdnaImageOpDecoder.hpp"
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
using namespace ShaderRecompiler;
|
||||
static void Require(bool value) { if (!value) throw std::runtime_error("image msaa load regression"); }
|
||||
static std::uint32_t Word0(std::uint32_t dmask, std::uint32_t dimension) {
|
||||
return (0x3cu << 26u) | (dmask << 8u) | (dimension << 3u) | 1u;
|
||||
}
|
||||
static RdnaInstruction Decode(std::uint32_t word0, std::uint32_t word1) {
|
||||
const std::array<std::uint32_t, 2> code{word0, word1};
|
||||
return DecodeRdnaMimg(0u, code, 0u);
|
||||
}
|
||||
static bool Rejects(std::uint32_t word0, std::uint32_t word1) {
|
||||
try {
|
||||
(void)Decode(word0, word1);
|
||||
} catch (const std::runtime_error&) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static void Check(std::uint32_t dimension, bool a16, std::uint32_t components, std::uint32_t fragmentDword, std::uint32_t fragmentShift) {
|
||||
const RdnaInstruction instruction = Decode(Word0(0x4u, dimension), (a16 ? 0x40000000u : 0u) | (8u << 8u));
|
||||
Require(instruction.op == RdnaOpcode::ImageMsaaLoad);
|
||||
Require(instruction.family == RdnaInstructionFamily::MIMG);
|
||||
Require(IsImageOpcode(instruction.op));
|
||||
Require(instruction.imageOpcodeId == 0x80u);
|
||||
Require(instruction.imageAddressComponents == components);
|
||||
Require(instruction.dataComponents == 4u);
|
||||
Require(instruction.dataDwordCount == 4u);
|
||||
IrProgram program;
|
||||
auto& block = program.CreateBlock();
|
||||
program.SetEntryBlock(block);
|
||||
TranslationContext context(program, block, 256);
|
||||
context.TranslateInstruction(instruction);
|
||||
std::vector<IrValue*> reads;
|
||||
for (auto* value : block.Instructions()) {
|
||||
if (value->Opcode() == IrOpcode::ImageRead) {
|
||||
reads.push_back(value);
|
||||
}
|
||||
}
|
||||
Require(reads.size() == 4u);
|
||||
for (std::uint32_t fragment = 0u; fragment < 4u; ++fragment) {
|
||||
IrValue* address = reads[fragment]->Argument(1)->Resolve();
|
||||
Require(address->Opcode() == IrOpcode::MakeImageAddress);
|
||||
IrValue* word = address->Argument(fragmentDword)->Resolve();
|
||||
if (fragment == 0u) {
|
||||
Require(word->Opcode() != IrOpcode::IAdd32);
|
||||
continue;
|
||||
}
|
||||
Require(word->Opcode() == IrOpcode::IAdd32);
|
||||
IrValue* offset = word->Argument(1)->Resolve();
|
||||
Require(offset->HasImmediate() && offset->ImmediateU32() == fragment << fragmentShift);
|
||||
const auto& memory = program.Resources().memoryInfo[reads[fragment]->Flags<MemoryFlags>().index];
|
||||
Require(memory.dmask == 0x4u && memory.dataDwords == 1u);
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
Check(6u, false, 3u, 2u, 0u);
|
||||
Check(7u, false, 4u, 3u, 0u);
|
||||
Check(6u, true, 3u, 1u, 0u);
|
||||
Check(7u, true, 4u, 1u, 16u);
|
||||
Require(Rejects(Word0(0x1u, 1u), 0u));
|
||||
Require(Rejects(Word0(0x3u, 6u), 0u));
|
||||
Require(Rejects(Word0(0x1u, 6u), 0x80000000u));
|
||||
Require(Rejects(Word0(0x1u, 6u) | 0x00010000u, 0u));
|
||||
}
|
||||
@@ -955,6 +955,7 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
ImageStoreMip,
|
||||
ImageStorePck,
|
||||
ImageStoreMipPck,
|
||||
ImageMsaaLoad,
|
||||
ImageAtomicSwap,
|
||||
ImageAtomicAdd,
|
||||
ImageAtomicUmin,
|
||||
|
||||
@@ -132,6 +132,7 @@ constexpr ImageOpcodeInfo imageOpcodes[] = {
|
||||
{0x0au, RdnaOpcode::ImageStorePck, nullptr, 0, false, false, false},
|
||||
{0x0bu, RdnaOpcode::ImageStoreMipPck, nullptr, 0, false, false, false},
|
||||
{0x0eu, RdnaOpcode::ImageGetResinfo, nullptr, 0, false, false, false},
|
||||
{0x80u, RdnaOpcode::ImageMsaaLoad, nullptr, 0, false, false, false},
|
||||
{0x60u, RdnaOpcode::ImageGetLod, nullptr, 0, false, false, false},
|
||||
{0xe6u, RdnaOpcode::ImageBvhIntersectRay, "image_bvh_intersect_ray", 0, false, false, false},
|
||||
{0xe7u, RdnaOpcode::ImageBvh64IntersectRay, "image_bvh64_intersect_ray", 0, false, false, false},
|
||||
@@ -336,6 +337,10 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span<const std
|
||||
if (multisampled && (info.sample || info.gather || opcode == 0x60u || (opcode >= 1u && opcode <= 5u) || opcode == 9u || opcode == 0x0bu)) {
|
||||
throw std::runtime_error("unsupported multisampled MIMG operation");
|
||||
}
|
||||
const bool msaaLoad = info.opcode == RdnaOpcode::ImageMsaaLoad;
|
||||
if (msaaLoad && !multisampled) {
|
||||
throw std::runtime_error("image_msaa_load requires a multisampled dimension");
|
||||
}
|
||||
auto flags = info.flags | (a16 ? RdnaImageSampleFlagA16 : 0u);
|
||||
if ((info.flags & RdnaImageSampleFlagDerivative) != 0u) {
|
||||
flags |= gradientCount(dimension) << RdnaImageSampleGradientCountShift;
|
||||
@@ -343,7 +348,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span<const std
|
||||
validateFlags(flags);
|
||||
const auto dmask = (word0 >> 8u) & 15u;
|
||||
const bool compareSwap = info.opcode == RdnaOpcode::ImageAtomicCmpswap || info.opcode == RdnaOpcode::ImageAtomicFcmpswap;
|
||||
if (dmask == 0u || (compareSwap ? dmask != 3u : (info.gather || info.atomic) && !std::has_single_bit(dmask))) {
|
||||
if (dmask == 0u || (compareSwap ? dmask != 3u : (info.gather || info.atomic || msaaLoad) && !std::has_single_bit(dmask))) {
|
||||
throw std::runtime_error("invalid MIMG data mask");
|
||||
}
|
||||
if (d16 && !(info.sample || info.gather || opcode == 0u || opcode == 1u || opcode == 8u || opcode == 9u)) {
|
||||
@@ -371,7 +376,7 @@ RdnaInstruction DecodeRdnaMimg(std::uint32_t programCounter, std::span<const std
|
||||
if (nsa == 0u && addressDwords > 256u - vaddr) {
|
||||
throw std::runtime_error("MIMG address register range overflow");
|
||||
}
|
||||
const auto dataComponents = info.gather ? 4u : static_cast<std::uint32_t>(std::popcount(dmask));
|
||||
const auto dataComponents = info.gather || msaaLoad ? 4u : static_cast<std::uint32_t>(std::popcount(dmask));
|
||||
const auto dataDwords = d16 ? (dataComponents + 1u) / 2u : dataComponents;
|
||||
const auto statusDwords = (word0 & 0x00010000u) != 0u ? 1u : 0u;
|
||||
if (dataDwords + statusDwords > 256u - vdata) {
|
||||
|
||||
@@ -822,6 +822,7 @@ bool IsImageOpcode(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::ImageStoreMip:
|
||||
case RdnaOpcode::ImageStorePck:
|
||||
case RdnaOpcode::ImageStoreMipPck:
|
||||
case RdnaOpcode::ImageMsaaLoad:
|
||||
case RdnaOpcode::ImageAtomicSwap:
|
||||
case RdnaOpcode::ImageAtomicAdd:
|
||||
case RdnaOpcode::ImageAtomicUmin:
|
||||
|
||||
@@ -79,7 +79,7 @@ private:
|
||||
IrValue* getScalarAddressResource(std::uint32_t base);
|
||||
IrValue* getImageResource(const MemoryInfo& memory);
|
||||
IrValue* getSamplerResource(const MemoryInfo& memory);
|
||||
IrValue* makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base);
|
||||
IrValue* makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base, std::uint32_t fragmentOffset = 0u);
|
||||
IrValue* constructU32x4(const RdnaOperand& base, std::uint32_t count);
|
||||
void writeImageComponents(const RdnaOperand& dst, IrValue* value, const MemoryInfo& memory, std::uint32_t componentLimit);
|
||||
BufferAddress readBufferAddress(const RdnaInstruction& inst);
|
||||
@@ -100,6 +100,7 @@ private:
|
||||
bool imageGetResinfo(const RdnaInstruction& inst);
|
||||
bool imageGetLod(const RdnaInstruction& inst);
|
||||
bool imageLoad(const RdnaInstruction& inst);
|
||||
bool imageMsaaLoad(const RdnaInstruction& inst);
|
||||
bool imageStore(const RdnaInstruction& inst);
|
||||
bool imageSample(const RdnaInstruction& inst);
|
||||
bool imageGather(const RdnaInstruction& inst);
|
||||
|
||||
@@ -120,6 +120,20 @@ bool TranslationContext::imageLoad(const RdnaInstruction& inst) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::imageMsaaLoad(const RdnaInstruction& inst) {
|
||||
MemoryInfo memory = imageMemoryInfoFromInstruction(inst);
|
||||
memory.dataDwords = 1u;
|
||||
memory.componentCount = 1u;
|
||||
IrValue* resource = getImageResource(memory);
|
||||
IrValue& exec = ir.GetExec();
|
||||
for (std::uint32_t fragment = 0u; fragment < 4u; ++fragment) {
|
||||
IrValue* address = makeImageAddress(inst, inst.source0, fragment);
|
||||
IrValue& result = ir.Emit(IrOpcode::ImageRead, IrOpcodeType(IrOpcode::ImageRead), {resource, address, &exec}, addMemoryInfo(memory, inst.programCounter));
|
||||
writeImageComponents(offsetOperand(inst.destination, fragment), &result, memory, 4u);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::imageStore(const RdnaInstruction& inst) {
|
||||
const MemoryInfo memory = imageMemoryInfoFromInstruction(inst);
|
||||
IrValue* resource = getImageResource(memory);
|
||||
|
||||
+2
@@ -600,6 +600,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) {
|
||||
case RdnaOpcode::ImageLoadMipPck:
|
||||
case RdnaOpcode::ImageLoadMipPckSgn:
|
||||
return imageLoad(inst);
|
||||
case RdnaOpcode::ImageMsaaLoad:
|
||||
return imageMsaaLoad(inst);
|
||||
case RdnaOpcode::ImageStore:
|
||||
case RdnaOpcode::ImageStoreMip:
|
||||
case RdnaOpcode::ImageStorePck:
|
||||
|
||||
@@ -93,7 +93,7 @@ IrValue* TranslationContext::getSamplerResource(const MemoryInfo& memory) {
|
||||
return &ir.Emit(IrOpcode::GetSamplerResource, IrOpcodeType(IrOpcode::GetSamplerResource), {&dword0.Value(), &dword1.Value(), &dword2.Value(), &dword3.Value()});
|
||||
}
|
||||
|
||||
IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base) {
|
||||
IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const RdnaOperand& base, std::uint32_t fragmentOffset) {
|
||||
std::array<IrValue*, 13> components{};
|
||||
IrValue& zero = ir.Constant(0u);
|
||||
components.fill(&zero);
|
||||
@@ -110,6 +110,11 @@ IrValue* TranslationContext::makeImageAddress(const RdnaInstruction& inst, const
|
||||
components[index] = &readRawU32(offsetOperand(plainBase, index)).Value();
|
||||
}
|
||||
}
|
||||
if (fragmentOffset != 0u) {
|
||||
const RdnaImageAddressComponent fragment = GetRdnaImageAddressComponentLayout(inst.imageSampleFlags, inst.imageAddressComponents - 1u);
|
||||
IrValue*& word = components[fragment.bitOffset / 32u];
|
||||
word = &ir.IAdd(*word, ir.Constant(fragmentOffset << (fragment.bitOffset % 32u)));
|
||||
}
|
||||
if ((inst.imageSampleFlags & RdnaImageSampleFlagCd) != 0u) {
|
||||
const auto mask = ballotMask(IrU1(ir.GetExec()));
|
||||
IrValue& lane = ir.Emit(IrOpcode::LaneId, IrType::U32, {});
|
||||
|
||||
Reference in New Issue
Block a user