shader: Properly scale image reads and add GL SPIR-V support

Thanks for everything!
This commit is contained in:
ReinUsesLisp 2021-08-01 18:57:45 -03:00 committed by Fernando Sahmkow
parent fc9bb3c3fe
commit e66d5b88a6
25 changed files with 228 additions and 77 deletions

View File

@ -14,6 +14,8 @@ struct Bindings {
u32 storage_buffer{}; u32 storage_buffer{};
u32 texture{}; u32 texture{};
u32 image{}; u32 image{};
u32 texture_scaling_index{};
u32 image_scaling_index{};
}; };
} // namespace Shader::Backend } // namespace Shader::Backend

View File

@ -6,6 +6,7 @@
#include "shader_recompiler/backend/bindings.h" #include "shader_recompiler/backend/bindings.h"
#include "shader_recompiler/backend/glasm/emit_context.h" #include "shader_recompiler/backend/glasm/emit_context.h"
#include "shader_recompiler/backend/glasm/emit_glasm.h"
#include "shader_recompiler/frontend/ir/program.h" #include "shader_recompiler/frontend/ir/program.h"
#include "shader_recompiler/profile.h" #include "shader_recompiler/profile.h"
#include "shader_recompiler/runtime_info.h" #include "shader_recompiler/runtime_info.h"
@ -55,7 +56,8 @@ EmitContext::EmitContext(IR::Program& program, Bindings& bindings, const Profile
} }
if (!runtime_info.glasm_use_storage_buffers) { if (!runtime_info.glasm_use_storage_buffers) {
if (const size_t num = info.storage_buffers_descriptors.size(); num > 0) { if (const size_t num = info.storage_buffers_descriptors.size(); num > 0) {
Add("PARAM c[{}]={{program.local[0..{}]}};", num, num - 1); const size_t index{num + PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE};
Add("PARAM c[{}]={{program.local[0..{}]}};", index, index - 1);
} }
} }
stage = program.stage; stage = program.stage;

View File

@ -13,6 +13,8 @@
namespace Shader::Backend::GLASM { namespace Shader::Backend::GLASM {
constexpr u32 PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE = 1;
[[nodiscard]] std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info, [[nodiscard]] std::string EmitGLASM(const Profile& profile, const RuntimeInfo& runtime_info,
IR::Program& program, Bindings& bindings); IR::Program& program, Bindings& bindings);

View File

@ -617,6 +617,15 @@ void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& inde
1u << index.U32(), ctx.reg_alloc.Define(inst)); 1u << index.U32(), ctx.reg_alloc.Define(inst));
} }
void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) {
if (!index.IsImmediate()) {
throw NotImplementedException("Non-constant texture rescaling");
}
ctx.Add("AND.U RC.x,scaling[0].y,{};"
"SNE.S {},RC.x,0;",
1u << index.U32(), ctx.reg_alloc.Define(inst));
}
void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, void EmitImageAtomicIAdd32(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord,
ScalarU32 value) { ScalarU32 value) {
ImageAtomic(ctx, inst, index, coord, value, "ADD.U32"); ImageAtomic(ctx, inst, index, coord, value, "ADD.U32");

View File

@ -557,6 +557,7 @@ void EmitImageRead(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Reg
void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord, void EmitImageWrite(EmitContext& ctx, IR::Inst& inst, const IR::Value& index, Register coord,
Register color); Register color);
void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index); void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index);
void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index);
void EmitBindlessImageAtomicIAdd32(EmitContext&); void EmitBindlessImageAtomicIAdd32(EmitContext&);
void EmitBindlessImageAtomicSMin32(EmitContext&); void EmitBindlessImageAtomicSMin32(EmitContext&);
void EmitBindlessImageAtomicUMin32(EmitContext&); void EmitBindlessImageAtomicUMin32(EmitContext&);

View File

@ -211,7 +211,7 @@ void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
} }
void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) {
ctx.Add("MOV.F {}.x,scaling[0].y;", inst); ctx.Add("MOV.F {}.x,scaling[0].z;", inst);
} }
void EmitUndefU1(EmitContext& ctx, IR::Inst& inst) { void EmitUndefU1(EmitContext& ctx, IR::Inst& inst) {

View File

@ -446,7 +446,7 @@ void EmitYDirection(EmitContext& ctx, IR::Inst& inst) {
} }
void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) { void EmitResolutionDownFactor(EmitContext& ctx, IR::Inst& inst) {
ctx.AddF32("{}=scaling.y;", inst); ctx.AddF32("{}=scaling.z;", inst);
} }
void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) { void EmitLoadLocal(EmitContext& ctx, IR::Inst& inst, std::string_view word_offset) {

View File

@ -620,6 +620,14 @@ void EmitIsTextureScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& inde
ctx.AddU1("{}=(ftou(scaling.x)&{})!=0;", inst, 1u << image_index); ctx.AddU1("{}=(ftou(scaling.x)&{})!=0;", inst, 1u << image_index);
} }
void EmitIsImageScaled(EmitContext& ctx, IR::Inst& inst, const IR::Value& index) {
if (!index.IsImmediate()) {
throw NotImplementedException("Non-constant texture rescaling");
}
const u32 image_index{index.U32()};
ctx.AddU1("{}=(ftou(scaling.y)&{})!=0;", inst, 1u << image_index);
}
void EmitBindlessImageSampleImplicitLod(EmitContext&) { void EmitBindlessImageSampleImplicitLod(EmitContext&) {
NotImplemented(); NotImplemented();
} }

View File

@ -14,6 +14,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/div_ceil.h" #include "common/div_ceil.h"
#include "shader_recompiler/backend/spirv/emit_context.h" #include "shader_recompiler/backend/spirv/emit_context.h"
#include "shader_recompiler/backend/spirv/emit_spirv.h"
namespace Shader::Backend::SPIRV { namespace Shader::Backend::SPIRV {
namespace { namespace {
@ -476,8 +477,9 @@ void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_vie
EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_, EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_,
IR::Program& program, Bindings& bindings) IR::Program& program, Bindings& bindings)
: Sirit::Module(profile_.supported_spirv), profile{profile_}, : Sirit::Module(profile_.supported_spirv), profile{profile_}, runtime_info{runtime_info_},
runtime_info{runtime_info_}, stage{program.stage} { stage{program.stage}, texture_rescaling_index{bindings.texture_scaling_index},
image_rescaling_index{bindings.image_scaling_index} {
const bool is_unified{profile.unified_descriptor_binding}; const bool is_unified{profile.unified_descriptor_binding};
u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer}; u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer};
u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer}; u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer};
@ -494,8 +496,8 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
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);
DefineTextures(program.info, texture_binding); DefineTextures(program.info, texture_binding, bindings.texture_scaling_index);
DefineImages(program.info, image_binding); DefineImages(program.info, image_binding, bindings.image_scaling_index);
DefineAttributeMemAccess(program.info); DefineAttributeMemAccess(program.info);
DefineGlobalMemoryFunctions(program.info); DefineGlobalMemoryFunctions(program.info);
DefineRescalingInput(program.info); DefineRescalingInput(program.info);
@ -1003,25 +1005,49 @@ void EmitContext::DefineRescalingInput(const Info& info) {
if (!info.uses_rescaling_uniform) { if (!info.uses_rescaling_uniform) {
return; return;
} }
boost::container::static_vector<Id, 2> members{F32[1]}; if (profile.unified_descriptor_binding) {
DefineRescalingInputPushConstant(info);
} else {
DefineRescalingInputUniformConstant();
}
}
void EmitContext::DefineRescalingInputPushConstant(const Info& info) {
boost::container::static_vector<Id, 3> members{F32[1]};
u32 member_index{0}; u32 member_index{0};
const u32 num_texture_words{Common::DivCeil(runtime_info.num_textures, 32u)}; if (!info.texture_descriptors.empty()) {
if (runtime_info.num_textures > 0) { rescaling_textures_type = TypeArray(U32[1], Const(4u));
rescaling_textures_type = TypeArray(U32[1], Const(num_texture_words));
Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u); Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u);
members.push_back(rescaling_textures_type); members.push_back(rescaling_textures_type);
rescaling_textures_member_index = ++member_index; rescaling_textures_member_index = ++member_index;
} }
if (!info.image_descriptors.empty()) {
rescaling_images_type = TypeArray(U32[1], Const(NUM_IMAGE_SCALING_WORDS));
if (rescaling_textures_type.value != rescaling_images_type.value) {
Decorate(rescaling_images_type, spv::Decoration::ArrayStride, 4u);
}
members.push_back(rescaling_images_type);
rescaling_images_member_index = ++member_index;
}
const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))}; const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))};
Decorate(push_constant_struct, spv::Decoration::Block); Decorate(push_constant_struct, spv::Decoration::Block);
Name(push_constant_struct, "ResolutionInfo"); Name(push_constant_struct, "ResolutionInfo");
MemberDecorate(push_constant_struct, 0u, spv::Decoration::Offset, 0u); MemberDecorate(push_constant_struct, 0u, spv::Decoration::Offset, 0u);
MemberName(push_constant_struct, 0u, "down_factor"); MemberName(push_constant_struct, 0u, "down_factor");
if (runtime_info.num_textures > 0) {
MemberDecorate(push_constant_struct, rescaling_textures_member_index, const u32 offset_bias = stage == Stage::Compute ? sizeof(u32) : 0;
spv::Decoration::Offset, 4u); if (!info.texture_descriptors.empty()) {
MemberDecorate(
push_constant_struct, rescaling_textures_member_index, spv::Decoration::Offset,
static_cast<u32>(offsetof(RescalingLayout, rescaling_textures) - offset_bias));
MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures"); MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures");
} }
if (!info.image_descriptors.empty()) {
MemberDecorate(push_constant_struct, rescaling_images_member_index, spv::Decoration::Offset,
static_cast<u32>(offsetof(RescalingLayout, rescaling_images) - offset_bias));
MemberName(push_constant_struct, rescaling_images_member_index, "rescaling_images");
}
const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)}; const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)};
rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant); rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant);
Name(rescaling_push_constants, "rescaling_push_constants"); Name(rescaling_push_constants, "rescaling_push_constants");
@ -1031,6 +1057,17 @@ void EmitContext::DefineRescalingInput(const Info& info) {
} }
} }
void EmitContext::DefineRescalingInputUniformConstant() {
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, F32[4])};
rescaling_uniform_constant =
AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant);
Decorate(rescaling_uniform_constant, spv::Decoration::Location, 0u);
if (profile.supported_spirv >= 0x00010400) {
interfaces.push_back(rescaling_uniform_constant);
}
}
void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) { void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
if (info.constant_buffer_descriptors.empty()) { if (info.constant_buffer_descriptors.empty()) {
return; return;
@ -1219,7 +1256,7 @@ void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
} }
} }
void EmitContext::DefineTextures(const Info& info, u32& binding) { void EmitContext::DefineTextures(const Info& info, u32& binding, u32& scaling_index) {
textures.reserve(info.texture_descriptors.size()); textures.reserve(info.texture_descriptors.size());
for (const TextureDescriptor& desc : info.texture_descriptors) { for (const TextureDescriptor& desc : info.texture_descriptors) {
const Id image_type{ImageType(*this, desc)}; const Id image_type{ImageType(*this, desc)};
@ -1241,13 +1278,14 @@ void EmitContext::DefineTextures(const Info& info, u32& binding) {
interfaces.push_back(id); interfaces.push_back(id);
} }
++binding; ++binding;
++scaling_index;
} }
if (info.uses_atomic_image_u32) { if (info.uses_atomic_image_u32) {
image_u32 = TypePointer(spv::StorageClass::Image, U32[1]); image_u32 = TypePointer(spv::StorageClass::Image, U32[1]);
} }
} }
void EmitContext::DefineImages(const Info& info, u32& binding) { void EmitContext::DefineImages(const Info& info, u32& binding, u32& scaling_index) {
images.reserve(info.image_descriptors.size()); images.reserve(info.image_descriptors.size());
for (const ImageDescriptor& desc : info.image_descriptors) { for (const ImageDescriptor& desc : info.image_descriptors) {
if (desc.count != 1) { if (desc.count != 1) {
@ -1268,6 +1306,7 @@ void EmitContext::DefineImages(const Info& info, u32& binding) {
interfaces.push_back(id); interfaces.push_back(id);
} }
++binding; ++binding;
++scaling_index;
} }
} }

View File

@ -238,9 +238,14 @@ public:
Id indexed_load_func{}; Id indexed_load_func{};
Id indexed_store_func{}; Id indexed_store_func{};
Id rescaling_uniform_constant{};
Id rescaling_push_constants{}; Id rescaling_push_constants{};
Id rescaling_textures_type{}; Id rescaling_textures_type{};
Id rescaling_images_type{};
u32 rescaling_textures_member_index{}; u32 rescaling_textures_member_index{};
u32 rescaling_images_member_index{};
u32 texture_rescaling_index{};
u32 image_rescaling_index{};
Id local_memory{}; Id local_memory{};
@ -314,11 +319,13 @@ private:
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);
void DefineTextures(const Info& info, u32& binding); void DefineTextures(const Info& info, u32& binding, u32& scaling_index);
void DefineImages(const Info& info, u32& binding); void DefineImages(const Info& info, u32& binding, u32& scaling_index);
void DefineAttributeMemAccess(const Info& info); void DefineAttributeMemAccess(const Info& info);
void DefineGlobalMemoryFunctions(const Info& info); void DefineGlobalMemoryFunctions(const Info& info);
void DefineRescalingInput(const Info& info); void DefineRescalingInput(const Info& info);
void DefineRescalingInputPushConstant(const Info& info);
void DefineRescalingInputUniformConstant();
void DefineInputs(const IR::Program& program); void DefineInputs(const IR::Program& program);
void DefineOutputs(const IR::Program& program); void DefineOutputs(const IR::Program& program);

View File

@ -16,15 +16,23 @@
namespace Shader::Backend::SPIRV { namespace Shader::Backend::SPIRV {
constexpr u32 NUM_TEXTURE_SCALING_WORDS = 4;
constexpr u32 NUM_IMAGE_SCALING_WORDS = 2;
constexpr u32 NUM_TEXTURE_AND_IMAGE_SCALING_WORDS =
NUM_TEXTURE_SCALING_WORDS + NUM_IMAGE_SCALING_WORDS;
struct RescalingLayout {
u32 down_factor;
std::array<u32, NUM_TEXTURE_SCALING_WORDS> rescaling_textures;
std::array<u32, NUM_IMAGE_SCALING_WORDS> rescaling_images;
};
[[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info, [[nodiscard]] std::vector<u32> EmitSPIRV(const Profile& profile, const RuntimeInfo& runtime_info,
IR::Program& program, Bindings& bindings); IR::Program& program, Bindings& bindings);
[[nodiscard]] inline std::vector<u32> EmitSPIRV(const Profile& profile, IR::Program& program) { [[nodiscard]] inline std::vector<u32> EmitSPIRV(const Profile& profile, IR::Program& program) {
RuntimeInfo runtime_info{};
runtime_info.num_textures = Shader::NumDescriptors(program.info.texture_descriptors);
Bindings binding; Bindings binding;
return EmitSPIRV(profile, runtime_info, program, binding); return EmitSPIRV(profile, {}, program, binding);
} }
} // namespace Shader::Backend::SPIRV } // namespace Shader::Backend::SPIRV

View File

@ -527,10 +527,15 @@ Id EmitYDirection(EmitContext& ctx) {
} }
Id EmitResolutionDownFactor(EmitContext& ctx) { Id EmitResolutionDownFactor(EmitContext& ctx) {
const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])}; if (ctx.profile.unified_descriptor_binding) {
const Id pointer{ const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])};
ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, ctx.u32_zero_value)}; const Id pointer{
return ctx.OpLoad(ctx.F32[1], pointer); ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, ctx.u32_zero_value)};
return ctx.OpLoad(ctx.F32[1], pointer);
} else {
const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)};
return ctx.OpCompositeExtract(ctx.F32[1], composite, 2u);
}
} }
Id EmitLoadLocal(EmitContext& ctx, Id word_offset) { Id EmitLoadLocal(EmitContext& ctx, Id word_offset) {

View File

@ -224,6 +224,40 @@ Id Emit(MethodPtrType sparse_ptr, MethodPtrType non_sparse_ptr, EmitContext& ctx
Decorate(ctx, inst, sample); Decorate(ctx, inst, sample);
return ctx.OpCompositeExtract(result_type, sample, 1U); return ctx.OpCompositeExtract(result_type, sample, 1U);
} }
Id IsScaled(EmitContext& ctx, const IR::Value& index, Id member_index, u32 base_index) {
const Id push_constant_u32{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.U32[1])};
Id bit{};
if (index.IsImmediate()) {
// Use BitwiseAnd instead of BitfieldExtract for better codegen on Nvidia OpenGL.
// LOP32I.NZ is used to set the predicate rather than BFE+ISETP.
const u32 index_value{index.U32() + base_index};
const Id word_index{ctx.Const(index_value / 32)};
const Id bit_index_mask{ctx.Const(1u << (index_value % 32))};
const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants,
member_index, word_index)};
const Id word{ctx.OpLoad(ctx.U32[1], pointer)};
bit = ctx.OpBitwiseAnd(ctx.U32[1], word, bit_index_mask);
} else {
Id index_value{ctx.Def(index)};
if (base_index != 0) {
index_value = ctx.OpIAdd(ctx.U32[1], index_value, ctx.Const(base_index));
}
const Id word_index{ctx.OpShiftRightArithmetic(ctx.U32[1], index_value, ctx.Const(5u))};
const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants,
member_index, word_index)};
const Id word{ctx.OpLoad(ctx.U32[1], pointer)};
const Id bit_index{ctx.OpBitwiseAnd(ctx.U32[1], index_value, ctx.Const(31u))};
bit = ctx.OpBitFieldUExtract(ctx.U32[1], index_value, bit_index, ctx.Const(1u));
}
return ctx.OpINotEqual(ctx.U1, bit, ctx.u32_zero_value);
}
Id BitTest(EmitContext& ctx, Id mask, Id bit) {
const Id shifted{ctx.OpShiftRightLogical(ctx.U32[1], mask, bit)};
const Id bit_value{ctx.OpBitwiseAnd(ctx.U32[1], shifted, ctx.Const(1u))};
return ctx.OpINotEqual(ctx.U1, bit_value, ctx.u32_zero_value);
}
} // Anonymous namespace } // Anonymous namespace
Id EmitBindlessImageSampleImplicitLod(EmitContext&) { Id EmitBindlessImageSampleImplicitLod(EmitContext&) {
@ -471,29 +505,27 @@ void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id
} }
Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) { Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) {
const Id push_constant_u32{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.U32[1])}; if (ctx.profile.unified_descriptor_binding) {
const Id member_index{ctx.Const(ctx.rescaling_textures_member_index)}; const Id member_index{ctx.Const(ctx.rescaling_textures_member_index)};
Id bit{}; return IsScaled(ctx, index, member_index, ctx.texture_rescaling_index);
if (index.IsImmediate()) {
// Use BitwiseAnd instead of BitfieldExtract for better codegen on Nvidia OpenGL.
// LOP32I.NZ is used to set the predicate rather than BFE+ISETP.
const u32 index_value{index.U32()};
const Id word_index{ctx.Const(index_value / 32)};
const Id bit_index_mask{ctx.Const(1u << (index_value % 32))};
const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants,
member_index, word_index)};
const Id word{ctx.OpLoad(ctx.U32[1], pointer)};
bit = ctx.OpBitwiseAnd(ctx.U32[1], word, bit_index_mask);
} else { } else {
const Id index_value{ctx.Def(index)}; const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)};
const Id word_index{ctx.OpShiftRightArithmetic(ctx.U32[1], index_value, ctx.Const(5u))}; const Id mask_f32{ctx.OpCompositeExtract(ctx.F32[1], composite, 0u)};
const Id pointer{ctx.OpAccessChain(push_constant_u32, ctx.rescaling_push_constants, const Id mask{ctx.OpBitcast(ctx.U32[1], mask_f32)};
member_index, word_index)}; return BitTest(ctx, mask, ctx.Def(index));
const Id word{ctx.OpLoad(ctx.U32[1], pointer)}; }
const Id bit_index{ctx.OpBitwiseAnd(ctx.U32[1], index_value, ctx.Const(31u))}; }
bit = ctx.OpBitFieldUExtract(ctx.U32[1], index_value, bit_index, ctx.Const(1u));
Id EmitIsImageScaled(EmitContext& ctx, const IR::Value& index) {
if (ctx.profile.unified_descriptor_binding) {
const Id member_index{ctx.Const(ctx.rescaling_images_member_index)};
return IsScaled(ctx, index, member_index, ctx.image_rescaling_index);
} else {
const Id composite{ctx.OpLoad(ctx.F32[4], ctx.rescaling_uniform_constant)};
const Id mask_f32{ctx.OpCompositeExtract(ctx.F32[1], composite, 1u)};
const Id mask{ctx.OpBitcast(ctx.U32[1], mask_f32)};
return BitTest(ctx, mask, ctx.Def(index));
} }
return ctx.OpINotEqual(ctx.U1, bit, ctx.u32_zero_value);
} }
} // namespace Shader::Backend::SPIRV } // namespace Shader::Backend::SPIRV

View File

@ -514,6 +514,7 @@ Id EmitImageGradient(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, I
Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords); Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords);
void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color); void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color);
Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index); Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index);
Id EmitIsImageScaled(EmitContext& ctx, const IR::Value& index);
Id EmitBindlessImageAtomicIAdd32(EmitContext&); Id EmitBindlessImageAtomicIAdd32(EmitContext&);
Id EmitBindlessImageAtomicSMin32(EmitContext&); Id EmitBindlessImageAtomicSMin32(EmitContext&);
Id EmitBindlessImageAtomicUMin32(EmitContext&); Id EmitBindlessImageAtomicUMin32(EmitContext&);

View File

@ -1950,6 +1950,10 @@ U1 IREmitter::IsTextureScaled(const U32& index) {
return Inst<U1>(Opcode::IsTextureScaled, index); return Inst<U1>(Opcode::IsTextureScaled, index);
} }
U1 IREmitter::IsImageScaled(const U32& index) {
return Inst<U1>(Opcode::IsImageScaled, index);
}
U1 IREmitter::VoteAll(const U1& value) { U1 IREmitter::VoteAll(const U1& value) {
return Inst<U1>(Opcode::VoteAll, value); return Inst<U1>(Opcode::VoteAll, value);
} }

View File

@ -361,6 +361,7 @@ public:
const Value& value, TextureInstInfo info); const Value& value, TextureInstInfo info);
[[nodiscard]] U1 IsTextureScaled(const U32& index); [[nodiscard]] U1 IsTextureScaled(const U32& index);
[[nodiscard]] U1 IsImageScaled(const U32& index);
[[nodiscard]] U1 VoteAll(const U1& value); [[nodiscard]] U1 VoteAll(const U1& value);
[[nodiscard]] U1 VoteAny(const U1& value); [[nodiscard]] U1 VoteAny(const U1& value);

View File

@ -494,6 +494,7 @@ OPCODE(ImageRead, U32x4, Opaq
OPCODE(ImageWrite, Void, Opaque, Opaque, U32x4, ) OPCODE(ImageWrite, Void, Opaque, Opaque, U32x4, )
OPCODE(IsTextureScaled, U1, U32, ) OPCODE(IsTextureScaled, U1, U32, )
OPCODE(IsImageScaled, U1, U32, )
// Atomic Image operations // Atomic Image operations

View File

@ -432,6 +432,7 @@ void VisitUsages(Info& info, IR::Inst& inst) {
break; break;
case IR::Opcode::ResolutionDownFactor: case IR::Opcode::ResolutionDownFactor:
case IR::Opcode::IsTextureScaled: case IR::Opcode::IsTextureScaled:
case IR::Opcode::IsImageScaled:
info.uses_rescaling_uniform = true; info.uses_rescaling_uniform = true;
break; break;
case IR::Opcode::LaneId: case IR::Opcode::LaneId:

View File

@ -129,8 +129,7 @@ void PatchImageFetch(IR::Block& block, IR::Inst& inst) {
void PatchImageRead(IR::Block& block, IR::Inst& inst) { void PatchImageRead(IR::Block& block, IR::Inst& inst) {
IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)}; IR::IREmitter ir{block, IR::Block::InstructionList::s_iterator_to(inst)};
const auto info{inst.Flags<IR::TextureInstInfo>()}; const auto info{inst.Flags<IR::TextureInstInfo>()};
// TODO: Scale conditionally const IR::U1 is_scaled{ir.IsImageScaled(ir.Imm32(info.descriptor_index))};
const IR::U1 is_scaled{IR::Value{true}};
ScaleIntegerCoord(ir, inst, is_scaled); ScaleIntegerCoord(ir, inst, is_scaled);
} }

View File

@ -63,8 +63,6 @@ struct RuntimeInfo {
std::array<AttributeType, 32> generic_input_types{}; std::array<AttributeType, 32> generic_input_types{};
VaryingState previous_stage_stores; VaryingState previous_stage_stores;
u32 num_textures{};
bool convert_depth_mode{}; bool convert_depth_mode{};
bool force_early_z{}; bool force_early_z{};

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <span> #include <span>
#include "shader_recompiler/backend/glasm/emit_glasm.h"
#include "video_core/buffer_cache/buffer_cache.h" #include "video_core/buffer_cache/buffer_cache.h"
#include "video_core/renderer_opengl/gl_buffer_cache.h" #include "video_core/renderer_opengl/gl_buffer_cache.h"
#include "video_core/renderer_opengl/gl_device.h" #include "video_core/renderer_opengl/gl_device.h"
@ -229,8 +230,10 @@ void BufferCacheRuntime::BindStorageBuffer(size_t stage, u32 binding_index, Buff
.padding = 0, .padding = 0,
}; };
buffer.MakeResident(is_written ? GL_READ_WRITE : GL_READ_ONLY); buffer.MakeResident(is_written ? GL_READ_WRITE : GL_READ_ONLY);
glProgramLocalParametersI4uivNV(PROGRAM_LUT[stage], binding_index, 1, glProgramLocalParametersI4uivNV(
reinterpret_cast<const GLuint*>(&ssbo)); PROGRAM_LUT[stage],
Shader::Backend::GLASM::PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE + binding_index, 1,
reinterpret_cast<const GLuint*>(&ssbo));
} }
} }
@ -250,8 +253,10 @@ void BufferCacheRuntime::BindComputeStorageBuffer(u32 binding_index, Buffer& buf
.padding = 0, .padding = 0,
}; };
buffer.MakeResident(is_written ? GL_READ_WRITE : GL_READ_ONLY); buffer.MakeResident(is_written ? GL_READ_WRITE : GL_READ_ONLY);
glProgramLocalParametersI4uivNV(GL_COMPUTE_PROGRAM_NV, binding_index, 1, glProgramLocalParametersI4uivNV(
reinterpret_cast<const GLuint*>(&ssbo)); GL_COMPUTE_PROGRAM_NV,
Shader::Backend::GLASM::PROGRAM_LOCAL_PARAMETER_STORAGE_BUFFER_BASE + binding_index, 1,
reinterpret_cast<const GLuint*>(&ssbo));
} }
} }

View File

@ -181,33 +181,40 @@ void ComputePipeline::Configure() {
texture_binding += num_texture_buffers; texture_binding += num_texture_buffers;
image_binding += num_image_buffers; image_binding += num_image_buffers;
u32 scaling_mask{}; u32 texture_scaling_mask{};
for (const auto& desc : info.texture_descriptors) { for (const auto& desc : info.texture_descriptors) {
for (u32 index = 0; index < desc.count; ++index) { for (u32 index = 0; index < desc.count; ++index) {
ImageView& image_view{texture_cache.GetImageView((views_it++)->id)}; ImageView& image_view{texture_cache.GetImageView((views_it++)->id)};
textures[texture_binding] = image_view.Handle(desc.type); textures[texture_binding] = image_view.Handle(desc.type);
if (texture_cache.IsRescaling(image_view)) { if (texture_cache.IsRescaling(image_view)) {
scaling_mask |= 1u << texture_binding; texture_scaling_mask |= 1u << texture_binding;
} }
++texture_binding; ++texture_binding;
} }
} }
u32 image_scaling_mask{};
for (const auto& desc : info.image_descriptors) { for (const auto& desc : info.image_descriptors) {
for (u32 index = 0; index < desc.count; ++index) { for (u32 index = 0; index < desc.count; ++index) {
ImageView& image_view{texture_cache.GetImageView((views_it++)->id)}; ImageView& image_view{texture_cache.GetImageView((views_it++)->id)};
if (desc.is_written) { if (desc.is_written) {
texture_cache.MarkModification(image_view.image_id); texture_cache.MarkModification(image_view.image_id);
} }
images[image_binding++] = image_view.StorageView(desc.type, desc.format); images[image_binding] = image_view.StorageView(desc.type, desc.format);
if (texture_cache.IsRescaling(image_view)) {
image_scaling_mask |= 1u << image_binding;
}
++image_binding;
} }
} }
if (info.uses_rescaling_uniform) { if (info.uses_rescaling_uniform) {
const f32 float_scaling_mask{Common::BitCast<f32>(scaling_mask)}; const f32 float_texture_scaling_mask{Common::BitCast<f32>(texture_scaling_mask)};
const f32 float_image_scaling_mask{Common::BitCast<f32>(image_scaling_mask)};
if (assembly_program.handle != 0) { if (assembly_program.handle != 0) {
glProgramLocalParameter4fARB(GL_COMPUTE_PROGRAM_NV, 0, float_scaling_mask, 0.0f, 0.0f, glProgramLocalParameter4fARB(GL_COMPUTE_PROGRAM_NV, 0, float_texture_scaling_mask,
0.0f); float_image_scaling_mask, 0.0f, 0.0f);
} else { } else {
glProgramUniform4f(source_program.handle, 0, float_scaling_mask, 0.0f, 0.0f, 0.0f); glProgramUniform4f(source_program.handle, 0, float_texture_scaling_mask,
float_image_scaling_mask, 0.0f, 0.0f);
} }
} }
if (texture_binding != 0) { if (texture_binding != 0) {

View File

@ -464,8 +464,10 @@ void GraphicsPipeline::ConfigureImpl(bool is_indexed) {
views_it += num_texture_buffers[stage]; views_it += num_texture_buffers[stage];
views_it += num_image_buffers[stage]; views_it += num_image_buffers[stage];
u32 scaling_mask{}; u32 texture_scaling_mask{};
u32 image_scaling_mask{};
u32 stage_texture_binding{}; u32 stage_texture_binding{};
u32 stage_image_binding{};
const auto& info{stage_infos[stage]}; const auto& info{stage_infos[stage]};
for (const auto& desc : info.texture_descriptors) { for (const auto& desc : info.texture_descriptors) {
@ -473,7 +475,7 @@ void GraphicsPipeline::ConfigureImpl(bool is_indexed) {
ImageView& image_view{texture_cache.GetImageView((views_it++)->id)}; ImageView& image_view{texture_cache.GetImageView((views_it++)->id)};
textures[texture_binding] = image_view.Handle(desc.type); textures[texture_binding] = image_view.Handle(desc.type);
if (texture_cache.IsRescaling(image_view)) { if (texture_cache.IsRescaling(image_view)) {
scaling_mask |= 1u << stage_texture_binding; texture_scaling_mask |= 1u << stage_texture_binding;
} }
++texture_binding; ++texture_binding;
++stage_texture_binding; ++stage_texture_binding;
@ -485,20 +487,26 @@ void GraphicsPipeline::ConfigureImpl(bool is_indexed) {
if (desc.is_written) { if (desc.is_written) {
texture_cache.MarkModification(image_view.image_id); texture_cache.MarkModification(image_view.image_id);
} }
images[image_binding++] = image_view.StorageView(desc.type, desc.format); images[image_binding] = image_view.StorageView(desc.type, desc.format);
if (texture_cache.IsRescaling(image_view)) {
image_scaling_mask |= 1u << stage_image_binding;
}
++image_binding;
++stage_image_binding;
} }
} }
if (info.uses_rescaling_uniform) { if (info.uses_rescaling_uniform) {
const f32 float_scaling_mask{Common::BitCast<f32>(scaling_mask)}; const f32 float_texture_scaling_mask{Common::BitCast<f32>(texture_scaling_mask)};
const f32 float_image_scaling_mask{Common::BitCast<f32>(image_scaling_mask)};
const bool is_rescaling{texture_cache.IsRescaling()}; const bool is_rescaling{texture_cache.IsRescaling()};
const f32 config_down_factor{Settings::values.resolution_info.down_factor}; const f32 config_down_factor{Settings::values.resolution_info.down_factor};
const f32 down_factor{is_rescaling ? config_down_factor : 1.0f}; const f32 down_factor{is_rescaling ? config_down_factor : 1.0f};
if (use_assembly) { if (use_assembly) {
glProgramLocalParameter4fARB(AssemblyStage(stage), 0, float_scaling_mask, glProgramLocalParameter4fARB(AssemblyStage(stage), 0, float_texture_scaling_mask,
down_factor, 0.0f, 0.0f); float_image_scaling_mask, down_factor, 0.0f);
} else { } else {
glProgramUniform4f(source_programs[stage].handle, 0, float_scaling_mask, glProgramUniform4f(source_programs[stage].handle, 0, float_texture_scaling_mask,
down_factor, 0.0f, 0.0f); float_image_scaling_mask, down_factor, 0.0f);
} }
} }
}}; }};

View File

@ -10,6 +10,7 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/shader_info.h" #include "shader_recompiler/shader_info.h"
#include "video_core/renderer_vulkan/vk_texture_cache.h" #include "video_core/renderer_vulkan/vk_texture_cache.h"
#include "video_core/renderer_vulkan/vk_update_descriptor.h" #include "video_core/renderer_vulkan/vk_update_descriptor.h"
@ -20,7 +21,7 @@
namespace Vulkan { namespace Vulkan {
constexpr size_t MAX_RESCALING_WORDS = 4; using Shader::Backend::SPIRV::NUM_TEXTURE_AND_IMAGE_SCALING_WORDS;
class DescriptorLayoutBuilder { class DescriptorLayoutBuilder {
public: public:
@ -74,7 +75,8 @@ public:
.stageFlags = static_cast<VkShaderStageFlags>( .stageFlags = static_cast<VkShaderStageFlags>(
is_compute ? VK_SHADER_STAGE_COMPUTE_BIT : VK_SHADER_STAGE_ALL_GRAPHICS), is_compute ? VK_SHADER_STAGE_COMPUTE_BIT : VK_SHADER_STAGE_ALL_GRAPHICS),
.offset = 0, .offset = 0,
.size = (is_compute ? 0 : sizeof(f32)) + sizeof(std::array<u32, MAX_RESCALING_WORDS>), .size = (is_compute ? 0 : sizeof(f32)) +
sizeof(std::array<u32, NUM_TEXTURE_AND_IMAGE_SCALING_WORDS>),
}; };
return device->GetLogical().CreatePipelineLayout({ return device->GetLogical().CreatePipelineLayout({
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
@ -146,14 +148,25 @@ public:
} }
} }
const std::array<u32, MAX_RESCALING_WORDS>& Data() const noexcept { void PushImage(bool is_rescaled) noexcept {
*image_ptr |= is_rescaled ? image_bit : 0;
image_bit <<= 1;
if (image_bit == 0) {
image_bit = 1u;
++image_ptr;
}
}
const std::array<u32, NUM_TEXTURE_AND_IMAGE_SCALING_WORDS>& Data() const noexcept {
return words; return words;
} }
private: private:
std::array<u32, MAX_RESCALING_WORDS> words{}; std::array<u32, NUM_TEXTURE_AND_IMAGE_SCALING_WORDS> words{};
u32* texture_ptr{words.data()}; u32* texture_ptr{words.data()};
u32* image_ptr{words.data() + Shader::Backend::SPIRV::NUM_TEXTURE_SCALING_WORDS};
u32 texture_bit{1u}; u32 texture_bit{1u};
u32 image_bit{1u};
}; };
inline void PushImageDescriptors(TextureCache& texture_cache, inline void PushImageDescriptors(TextureCache& texture_cache,
@ -181,6 +194,7 @@ inline void PushImageDescriptors(TextureCache& texture_cache,
} }
const VkImageView vk_image_view{image_view.StorageView(desc.type, desc.format)}; const VkImageView vk_image_view{image_view.StorageView(desc.type, desc.format)};
update_descriptor_queue.AddImage(vk_image_view); update_descriptor_queue.AddImage(vk_image_view);
rescaling.PushImage(texture_cache.IsRescaling(image_view));
} }
} }
} }

View File

@ -139,9 +139,6 @@ Shader::RuntimeInfo MakeRuntimeInfo(std::span<const Shader::IR::Program> program
} else { } else {
info.previous_stage_stores.mask.set(); info.previous_stage_stores.mask.set();
} }
for (const auto& stage : programs) {
info.num_textures += Shader::NumDescriptors(stage.info.texture_descriptors);
}
const Shader::Stage stage{program.stage}; const Shader::Stage stage{program.stage};
const bool has_geometry{key.unique_hashes[4] != 0 && !programs[4].is_geometry_passthrough}; const bool has_geometry{key.unique_hashes[4] != 0 && !programs[4].is_geometry_passthrough};
const bool gl_ndc{key.state.ndc_minus_one_to_one != 0}; const bool gl_ndc{key.state.ndc_minus_one_to_one != 0};