spirv: Implement rescaling patching

This commit is contained in:
ReinUsesLisp 2021-07-25 22:26:23 -03:00 committed by Fernando Sahmkow
parent 01379c5e3c
commit 656adee630
8 changed files with 86 additions and 5 deletions

View File

@ -7,6 +7,8 @@
#include <climits>
#include <string_view>
#include <boost/container/static_vector.hpp>
#include <fmt/format.h>
#include "common/common_types.h"
@ -496,6 +498,7 @@ EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_inf
DefineImages(program.info, image_binding);
DefineAttributeMemAccess(program.info);
DefineGlobalMemoryFunctions(program.info);
DefineRescalingInput(program.info);
}
EmitContext::~EmitContext() = default;
@ -996,6 +999,38 @@ void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4]));
}
void EmitContext::DefineRescalingInput(const Info& info) {
if (!info.uses_rescaling_uniform) {
return;
}
boost::container::static_vector<Id, 2> members{F32[1]};
u32 member_index{0};
const u32 num_texture_words{Common::DivCeil(runtime_info.num_textures, 32u)};
if (runtime_info.num_textures > 0) {
rescaling_textures_type = TypeArray(U32[1], Const(num_texture_words));
Decorate(rescaling_textures_type, spv::Decoration::ArrayStride, 4u);
members.push_back(rescaling_textures_type);
rescaling_textures_member_index = ++member_index;
}
const Id push_constant_struct{TypeStruct(std::span(members.data(), members.size()))};
Decorate(push_constant_struct, spv::Decoration::Block);
Name(push_constant_struct, "ResolutionInfo");
MemberDecorate(push_constant_struct, 0u, spv::Decoration::Offset, 0u);
MemberName(push_constant_struct, 0u, "down_factor");
if (runtime_info.num_textures > 0) {
MemberDecorate(push_constant_struct, rescaling_textures_member_index,
spv::Decoration::Offset, 4u);
MemberName(push_constant_struct, rescaling_textures_member_index, "rescaling_textures");
}
const Id pointer_type{TypePointer(spv::StorageClass::PushConstant, push_constant_struct)};
rescaling_push_constants = AddGlobalVariable(pointer_type, spv::StorageClass::PushConstant);
Name(rescaling_push_constants, "rescaling_push_constants");
if (profile.supported_spirv >= 0x00010400) {
interfaces.push_back(rescaling_push_constants);
}
}
void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
if (info.constant_buffer_descriptors.empty()) {
return;

View File

@ -238,6 +238,10 @@ public:
Id indexed_load_func{};
Id indexed_store_func{};
Id rescaling_push_constants{};
Id rescaling_textures_type{};
u32 rescaling_textures_member_index{};
Id local_memory{};
Id shared_memory_u8{};
@ -314,6 +318,7 @@ private:
void DefineImages(const Info& info, u32& binding);
void DefineAttributeMemAccess(const Info& info);
void DefineGlobalMemoryFunctions(const Info& info);
void DefineRescalingInput(const Info& info);
void DefineInputs(const IR::Program& program);
void DefineOutputs(const IR::Program& program);

View File

@ -20,8 +20,11 @@ namespace Shader::Backend::SPIRV {
IR::Program& program, Bindings& bindings);
[[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;
return EmitSPIRV(profile, {}, program, binding);
return EmitSPIRV(profile, runtime_info, program, binding);
}
} // namespace Shader::Backend::SPIRV

View File

@ -527,8 +527,10 @@ Id EmitYDirection(EmitContext& ctx) {
}
Id EmitResolutionDownFactor(EmitContext& ctx) {
UNIMPLEMENTED();
return ctx.Const(1.0f);
const Id pointer_type{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.F32[1])};
const Id pointer{
ctx.OpAccessChain(pointer_type, ctx.rescaling_push_constants, ctx.u32_zero_value)};
return ctx.OpLoad(ctx.F32[1], pointer);
}
Id EmitLoadLocal(EmitContext& ctx, Id word_offset) {

View File

@ -470,8 +470,30 @@ void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id
ctx.OpImageWrite(Image(ctx, index, info), coords, color);
}
Id EmitIsTextureScaled([[maybe_unused]] EmitContext& ctx, [[maybe_unused]] const IR::Value& index) {
return ctx.false_value;
Id EmitIsTextureScaled(EmitContext& ctx, const IR::Value& index) {
const Id push_constant_u32{ctx.TypePointer(spv::StorageClass::PushConstant, ctx.U32[1])};
const Id member_index{ctx.Const(ctx.rescaling_textures_member_index)};
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()};
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 {
const Id index_value{ctx.Def(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);
}
} // namespace Shader::Backend::SPIRV

View File

@ -178,6 +178,9 @@ IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Blo
Optimization::GlobalMemoryToStorageBufferPass(program);
Optimization::TexturePass(env, program);
if (Settings::values.resolution_info.active) {
Optimization::RescalingPass(program);
}
Optimization::ConstantPropagationPass(program);
Optimization::DeadCodeEliminationPass(program);
if (Settings::values.renderer_debug) {

View File

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

View File

@ -191,4 +191,13 @@ struct Info {
ImageDescriptors image_descriptors;
};
template <typename Descriptors>
u32 NumDescriptors(const Descriptors& descriptors) {
u32 num{};
for (const auto& desc : descriptors) {
num += desc.count;
}
return num;
}
} // namespace Shader