early-access version 2561

This commit is contained in:
pineappleEA 2022-03-17 23:26:23 +01:00
parent f850a54bac
commit 8976d3f67b
6 changed files with 121 additions and 82 deletions

View File

@ -1,7 +1,7 @@
yuzu emulator early access yuzu emulator early access
============= =============
This is the source code for early-access 2559. This is the source code for early-access 2561.
## Legal Notice ## Legal Notice

View File

@ -123,9 +123,8 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
} }
Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr, u32 element_size, Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr, u32 element_size,
const IR::Value& binding, const IR::Value& offset) { const IR::Value& binding, const IR::Value& offset, const Id indirect_func) {
Id buffer_offset; Id buffer_offset;
const Id uniform_type{ctx.uniform_types.*member_ptr}; const Id uniform_type{ctx.uniform_types.*member_ptr};
if (offset.IsImmediate()) { if (offset.IsImmediate()) {
// Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4) // Hardware been proved to read the aligned offset (e.g. LDC.U32 at 6 will read offset 4)
@ -138,49 +137,22 @@ Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr,
} else { } else {
buffer_offset = ctx.Def(offset); buffer_offset = ctx.Def(offset);
} }
if (!binding.IsImmediate()) {
if (binding.IsImmediate()) { return ctx.OpFunctionCall(result_type, indirect_func, ctx.Def(binding), buffer_offset);
}
const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr}; const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
const Id access_chain{ const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)};
ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)};
return ctx.OpLoad(result_type, access_chain); return ctx.OpLoad(result_type, access_chain);
} else {
const Id index{ctx.Def(binding)};
const Id merge_label = ctx.OpLabel();
std::array<Id, Info::MAX_CBUFS> buf_labels;
std::array<Sirit::Literal, Info::MAX_CBUFS> buf_literals;
for (u32 i = 0; i < Info::MAX_CBUFS; i++) {
buf_labels[i] = ctx.OpLabel();
buf_literals[i] = Sirit::Literal{i};
}
ctx.OpSelectionMerge(merge_label, spv::SelectionControlMask::MaskNone);
ctx.OpSwitch(index, buf_labels[0], buf_literals, buf_labels);
std::array<Id, Info::MAX_CBUFS * 2> phi_targets;
for (u32 i = 0; i < Info::MAX_CBUFS; i++) {
ctx.AddLabel(buf_labels[i]);
const Id cbuf{ctx.cbufs[i].*member_ptr};
const Id access_chain{
ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, buffer_offset)};
phi_targets[2 * i + 0] = ctx.OpLoad(result_type, access_chain);
phi_targets[2 * i + 1] = buf_labels[i];
ctx.OpBranch(merge_label);
}
ctx.AddLabel(merge_label);
return ctx.OpPhi(result_type, phi_targets);
}
} }
Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id GetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset); return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset,
ctx.load_const_func_u32);
} }
Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id GetCbufU32x4(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset); return GetCbuf(ctx, ctx.U32[4], &UniformDefinitions::U32x4, sizeof(u32[4]), binding, offset,
ctx.load_const_func_u32x4);
} }
Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) { Id GetCbufElement(EmitContext& ctx, Id vector, const IR::Value& offset, u32 index_offset) {
@ -231,7 +203,8 @@ void EmitGetIndirectBranchVariable(EmitContext&) {
Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)}; const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset,
ctx.load_const_func_u8)};
return ctx.OpUConvert(ctx.U32[1], load); return ctx.OpUConvert(ctx.U32[1], load);
} }
Id element{}; Id element{};
@ -247,7 +220,8 @@ Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& of
Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) { if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int8) {
const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset)}; const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset,
ctx.load_const_func_u8)};
return ctx.OpSConvert(ctx.U32[1], load); return ctx.OpSConvert(ctx.U32[1], load);
} }
Id element{}; Id element{};
@ -263,8 +237,8 @@ Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& of
Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
const Id load{ const Id load{GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset,
GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset)}; ctx.load_const_func_u16)};
return ctx.OpUConvert(ctx.U32[1], load); return ctx.OpUConvert(ctx.U32[1], load);
} }
Id element{}; Id element{};
@ -280,8 +254,8 @@ Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& o
Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) { if (ctx.profile.support_descriptor_aliasing && ctx.profile.support_int16) {
const Id load{ const Id load{GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset,
GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset)}; ctx.load_const_func_u16)};
return ctx.OpSConvert(ctx.U32[1], load); return ctx.OpSConvert(ctx.U32[1], load);
} }
Id element{}; Id element{};
@ -306,7 +280,8 @@ Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& o
Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing) { if (ctx.profile.support_descriptor_aliasing) {
return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset); return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset,
ctx.load_const_func_f32);
} else { } else {
const Id vector{GetCbufU32x4(ctx, binding, offset)}; const Id vector{GetCbufU32x4(ctx, binding, offset)};
return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u)); return ctx.OpBitcast(ctx.F32[1], GetCbufElement(ctx, vector, offset, 0u));
@ -315,8 +290,8 @@ Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& o
Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) { Id EmitGetCbufU32x2(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
if (ctx.profile.support_descriptor_aliasing) { if (ctx.profile.support_descriptor_aliasing) {
return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding, return GetCbuf(ctx, ctx.U32[2], &UniformDefinitions::U32x2, sizeof(u32[2]), binding, offset,
offset); ctx.load_const_func_u32x2);
} else { } else {
const Id vector{GetCbufU32x4(ctx, binding, offset)}; const Id vector{GetCbufU32x4(ctx, binding, offset)};
return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u), return ctx.OpCompositeConstruct(ctx.U32[2], GetCbufElement(ctx, vector, offset, 0u),

View File

@ -464,6 +464,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
DefineSharedMemory(program); DefineSharedMemory(program);
DefineSharedMemoryFunctions(program); DefineSharedMemoryFunctions(program);
DefineConstantBuffers(program.info, uniform_binding); DefineConstantBuffers(program.info, uniform_binding);
DefineConstantBufferIndirectFunctions(program.info);
DefineStorageBuffers(program.info, storage_binding); DefineStorageBuffers(program.info, storage_binding);
DefineTextureBuffers(program.info, texture_binding); DefineTextureBuffers(program.info, texture_binding);
DefineImageBuffers(program.info, image_binding); DefineImageBuffers(program.info, image_binding);
@ -993,7 +994,7 @@ void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
} }
return; return;
} }
IR::Type types{info.used_constant_buffer_types}; IR::Type types{info.used_constant_buffer_types | info.used_indirect_cbuf_types};
if (True(types & IR::Type::U8)) { if (True(types & IR::Type::U8)) {
if (profile.support_int8) { if (profile.support_int8) {
DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8)); DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8));
@ -1027,6 +1028,62 @@ void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
binding += static_cast<u32>(info.constant_buffer_descriptors.size()); binding += static_cast<u32>(info.constant_buffer_descriptors.size());
} }
void EmitContext::DefineConstantBufferIndirectFunctions(const Info& info) {
if (!info.uses_cbuf_indirect) {
return;
}
const auto make_accessor{[&](Id buffer_type, Id UniformDefinitions::*member_ptr) {
const Id func_type{TypeFunction(buffer_type, U32[1], U32[1])};
const Id func{OpFunction(buffer_type, spv::FunctionControlMask::MaskNone, func_type)};
const Id binding{OpFunctionParameter(U32[1])};
const Id offset{OpFunctionParameter(U32[1])};
AddLabel();
const Id merge_label{OpLabel()};
const Id uniform_type{uniform_types.*member_ptr};
std::array<Id, Info::MAX_CBUFS> buf_labels;
std::array<Sirit::Literal, Info::MAX_CBUFS> buf_literals;
for (u32 i = 0; i < Info::MAX_CBUFS; i++) {
buf_labels[i] = OpLabel();
buf_literals[i] = Sirit::Literal{i};
}
OpSelectionMerge(merge_label, spv::SelectionControlMask::MaskNone);
OpSwitch(binding, buf_labels[0], buf_literals, buf_labels);
for (u32 i = 0; i < Info::MAX_CBUFS; i++) {
AddLabel(buf_labels[i]);
const Id cbuf{cbufs[i].*member_ptr};
const Id access_chain{OpAccessChain(uniform_type, cbuf, u32_zero_value, offset)};
const Id result{OpLoad(buffer_type, access_chain)};
OpReturnValue(result);
}
AddLabel(merge_label);
OpUnreachable();
OpFunctionEnd();
return func;
}};
IR::Type types{info.used_indirect_cbuf_types};
if (True(types & IR::Type::U8)) {
load_const_func_u8 = make_accessor(U8, &UniformDefinitions::U8);
}
if (True(types & IR::Type::U16)) {
load_const_func_u16 = make_accessor(U16, &UniformDefinitions::U16);
}
if (True(types & IR::Type::F32)) {
load_const_func_f32 = make_accessor(F32[1], &UniformDefinitions::F32);
}
if (True(types & IR::Type::U32)) {
load_const_func_u32 = make_accessor(U32[1], &UniformDefinitions::U32);
}
if (True(types & IR::Type::U32x2)) {
load_const_func_u32x2 = make_accessor(U32[2], &UniformDefinitions::U32x2);
}
if (True(types & IR::Type::U32x4)) {
load_const_func_u32x4 = make_accessor(U32[4], &UniformDefinitions::U32x4);
}
}
void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) { void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
if (info.storage_buffers_descriptors.empty()) { if (info.storage_buffers_descriptors.empty()) {
return; return;

View File

@ -294,6 +294,13 @@ public:
std::vector<Id> interfaces; std::vector<Id> interfaces;
Id load_const_func_u8{};
Id load_const_func_u16{};
Id load_const_func_u32{};
Id load_const_func_f32{};
Id load_const_func_u32x2{};
Id load_const_func_u32x4{};
private: private:
void DefineCommonTypes(const Info& info); void DefineCommonTypes(const Info& info);
void DefineCommonConstants(); void DefineCommonConstants();
@ -302,6 +309,7 @@ private:
void DefineSharedMemory(const IR::Program& program); void DefineSharedMemory(const IR::Program& program);
void DefineSharedMemoryFunctions(const IR::Program& program); void DefineSharedMemoryFunctions(const IR::Program& program);
void DefineConstantBuffers(const Info& info, u32& binding); void DefineConstantBuffers(const Info& info, u32& binding);
void DefineConstantBufferIndirectFunctions(const Info& info);
void DefineStorageBuffers(const Info& info, u32& binding); void DefineStorageBuffers(const Info& info, u32& binding);
void DefineTextureBuffers(const Info& info, u32& binding); void DefineTextureBuffers(const Info& info, u32& binding);
void DefineImageBuffers(const Info& info, u32& binding); void DefineImageBuffers(const Info& info, u32& binding);

View File

@ -30,6 +30,8 @@ void AddConstantBufferDescriptor(Info& info, u32 index, u32 count) {
} }
void AddRegisterIndexedLdc(Info& info) { void AddRegisterIndexedLdc(Info& info) {
info.uses_cbuf_indirect = true;
// The shader can use any possible constant buffer // The shader can use any possible constant buffer
info.constant_buffer_mask = (1 << Info::MAX_CBUFS) - 1; info.constant_buffer_mask = (1 << Info::MAX_CBUFS) - 1;
@ -43,6 +45,30 @@ void AddRegisterIndexedLdc(Info& info) {
} }
} }
u32 GetElementSize(IR::Type& used_type, Shader::IR::Opcode opcode) {
switch (opcode) {
case IR::Opcode::GetCbufU8:
case IR::Opcode::GetCbufS8:
used_type |= IR::Type::U8;
return 1;
case IR::Opcode::GetCbufU16:
case IR::Opcode::GetCbufS16:
used_type |= IR::Type::U16;
return 2;
case IR::Opcode::GetCbufU32:
used_type |= IR::Type::U32;
return 4;
case IR::Opcode::GetCbufF32:
used_type |= IR::Type::F32;
return 4;
case IR::Opcode::GetCbufU32x2:
used_type |= IR::Type::U32x2;
return 8;
default:
throw InvalidArgument("Invalid opcode {}", opcode);
}
}
void GetPatch(Info& info, IR::Patch patch) { void GetPatch(Info& info, IR::Patch patch) {
if (!IR::IsGeneric(patch)) { if (!IR::IsGeneric(patch)) {
throw NotImplementedException("Reading non-generic patch {}", patch); throw NotImplementedException("Reading non-generic patch {}", patch);
@ -479,45 +505,16 @@ void VisitUsages(Info& info, IR::Inst& inst) {
const IR::Value offset{inst.Arg(1)}; const IR::Value offset{inst.Arg(1)};
if (index.IsImmediate()) { if (index.IsImmediate()) {
AddConstantBufferDescriptor(info, index.U32(), 1); AddConstantBufferDescriptor(info, index.U32(), 1);
} else { u32 element_size = GetElementSize(info.used_constant_buffer_types, inst.GetOpcode());
AddRegisterIndexedLdc(info);
}
u32 element_size{};
switch (inst.GetOpcode()) {
case IR::Opcode::GetCbufU8:
case IR::Opcode::GetCbufS8:
info.used_constant_buffer_types |= IR::Type::U8;
element_size = 1;
break;
case IR::Opcode::GetCbufU16:
case IR::Opcode::GetCbufS16:
info.used_constant_buffer_types |= IR::Type::U16;
element_size = 2;
break;
case IR::Opcode::GetCbufU32:
info.used_constant_buffer_types |= IR::Type::U32;
element_size = 4;
break;
case IR::Opcode::GetCbufF32:
info.used_constant_buffer_types |= IR::Type::F32;
element_size = 4;
break;
case IR::Opcode::GetCbufU32x2:
info.used_constant_buffer_types |= IR::Type::U32x2;
element_size = 8;
break;
default:
break;
}
if (index.IsImmediate()) {
u32& size{info.constant_buffer_used_sizes[index.U32()]}; u32& size{info.constant_buffer_used_sizes[index.U32()]};
if (offset.IsImmediate()) { if (offset.IsImmediate()) {
size = Common::AlignUp(std::max(size, offset.U32() + element_size), 16u); size = Common::AlignUp(std::max(size, offset.U32() + element_size), 16u);
} else { } else {
size = 0x10'000; size = 0x10'000;
} }
} else {
AddRegisterIndexedLdc(info);
GetElementSize(info.used_indirect_cbuf_types, inst.GetOpcode());
} }
break; break;
} }

View File

@ -173,9 +173,11 @@ struct Info {
bool uses_atomic_image_u32{}; bool uses_atomic_image_u32{};
bool uses_shadow_lod{}; bool uses_shadow_lod{};
bool uses_rescaling_uniform{}; bool uses_rescaling_uniform{};
bool uses_cbuf_indirect{};
IR::Type used_constant_buffer_types{}; IR::Type used_constant_buffer_types{};
IR::Type used_storage_buffer_types{}; IR::Type used_storage_buffer_types{};
IR::Type used_indirect_cbuf_types{};
u32 constant_buffer_mask{}; u32 constant_buffer_mask{};
std::array<u32, MAX_CBUFS> constant_buffer_used_sizes{}; std::array<u32, MAX_CBUFS> constant_buffer_used_sizes{};