2021-02-08 06:54:35 +01:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
|
|
|
|
|
|
|
namespace Shader::Backend::SPIRV {
|
2021-03-19 23:28:31 +01:00
|
|
|
namespace {
|
|
|
|
Id InputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
|
|
|
|
const u32 element{static_cast<u32>(attr) % 4};
|
|
|
|
const auto element_id{[&] { return ctx.Constant(ctx.U32[1], element); }};
|
|
|
|
if (IR::IsGeneric(attr)) {
|
|
|
|
const u32 index{IR::GenericAttributeIndex(attr)};
|
|
|
|
return ctx.OpAccessChain(ctx.input_f32, ctx.input_generics.at(index), element_id());
|
|
|
|
}
|
|
|
|
switch (attr) {
|
|
|
|
case IR::Attribute::PositionX:
|
|
|
|
case IR::Attribute::PositionY:
|
|
|
|
case IR::Attribute::PositionZ:
|
|
|
|
case IR::Attribute::PositionW:
|
|
|
|
return ctx.OpAccessChain(ctx.input_f32, ctx.input_position, element_id());
|
2021-03-20 23:11:56 +01:00
|
|
|
case IR::Attribute::InstanceId:
|
|
|
|
return ctx.OpLoad(ctx.U32[1], ctx.instance_id);
|
|
|
|
case IR::Attribute::VertexId:
|
|
|
|
return ctx.OpLoad(ctx.U32[1], ctx.vertex_id);
|
2021-03-19 23:28:31 +01:00
|
|
|
default:
|
|
|
|
throw NotImplementedException("Read attribute {}", attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Id OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
|
|
|
|
const u32 element{static_cast<u32>(attr) % 4};
|
|
|
|
const auto element_id{[&] { return ctx.Constant(ctx.U32[1], element); }};
|
|
|
|
if (IR::IsGeneric(attr)) {
|
|
|
|
const u32 index{IR::GenericAttributeIndex(attr)};
|
|
|
|
return ctx.OpAccessChain(ctx.output_f32, ctx.output_generics.at(index), element_id());
|
|
|
|
}
|
|
|
|
switch (attr) {
|
|
|
|
case IR::Attribute::PositionX:
|
|
|
|
case IR::Attribute::PositionY:
|
|
|
|
case IR::Attribute::PositionZ:
|
|
|
|
case IR::Attribute::PositionW:
|
|
|
|
return ctx.OpAccessChain(ctx.output_f32, ctx.output_position, element_id());
|
|
|
|
default:
|
|
|
|
throw NotImplementedException("Read attribute {}", attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2021-02-08 06:54:35 +01:00
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetRegister(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetRegister(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetPred(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetPred(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetGotoVariable(EmitContext&) {
|
2021-02-11 20:39:06 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetGotoVariable(EmitContext&) {
|
2021-02-11 20:39:06 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-09 21:14:57 +01:00
|
|
|
static Id GetCbuf(EmitContext& ctx, Id result_type, Id UniformDefinitions::*member_ptr,
|
|
|
|
u32 element_size, const IR::Value& binding, const IR::Value& offset) {
|
2021-02-08 06:54:35 +01:00
|
|
|
if (!binding.IsImmediate()) {
|
|
|
|
throw NotImplementedException("Constant buffer indexing");
|
|
|
|
}
|
2021-03-09 21:14:57 +01:00
|
|
|
const Id cbuf{ctx.cbufs[binding.U32()].*member_ptr};
|
|
|
|
const Id uniform_type{ctx.uniform_types.*member_ptr};
|
2021-02-08 06:54:35 +01:00
|
|
|
if (!offset.IsImmediate()) {
|
2021-03-09 21:14:57 +01:00
|
|
|
Id index{ctx.Def(offset)};
|
|
|
|
if (element_size > 1) {
|
|
|
|
const u32 log2_element_size{static_cast<u32>(std::countr_zero(element_size))};
|
|
|
|
const Id shift{ctx.Constant(ctx.U32[1], log2_element_size)};
|
|
|
|
index = ctx.OpShiftRightArithmetic(ctx.U32[1], ctx.Def(offset), shift);
|
|
|
|
}
|
|
|
|
const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, index)};
|
|
|
|
return ctx.OpLoad(result_type, access_chain);
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
2021-03-09 21:14:57 +01:00
|
|
|
if (offset.U32() % element_size != 0) {
|
|
|
|
throw NotImplementedException("Unaligned immediate constant buffer load");
|
|
|
|
}
|
|
|
|
const Id imm_offset{ctx.Constant(ctx.U32[1], offset.U32() / element_size)};
|
|
|
|
const Id access_chain{ctx.OpAccessChain(uniform_type, cbuf, ctx.u32_zero_value, imm_offset)};
|
|
|
|
return ctx.OpLoad(result_type, access_chain);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufU8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
const Id load{GetCbuf(ctx, ctx.U8, &UniformDefinitions::U8, sizeof(u8), binding, offset)};
|
|
|
|
return ctx.OpUConvert(ctx.U32[1], load);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufS8(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
const Id load{GetCbuf(ctx, ctx.S8, &UniformDefinitions::S8, sizeof(s8), binding, offset)};
|
|
|
|
return ctx.OpSConvert(ctx.U32[1], load);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufU16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
const Id load{GetCbuf(ctx, ctx.U16, &UniformDefinitions::U16, sizeof(u16), binding, offset)};
|
|
|
|
return ctx.OpUConvert(ctx.U32[1], load);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufS16(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
const Id load{GetCbuf(ctx, ctx.S16, &UniformDefinitions::S16, sizeof(s16), binding, offset)};
|
|
|
|
return ctx.OpSConvert(ctx.U32[1], load);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufU32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
return GetCbuf(ctx, ctx.U32[1], &UniformDefinitions::U32, sizeof(u32), binding, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufF32(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
return GetCbuf(ctx, ctx.F32[1], &UniformDefinitions::F32, sizeof(f32), binding, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Id EmitGetCbufU64(EmitContext& ctx, const IR::Value& binding, const IR::Value& offset) {
|
|
|
|
return GetCbuf(ctx, ctx.U64, &UniformDefinitions::U64, sizeof(u64), binding, offset);
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:28:31 +01:00
|
|
|
Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr) {
|
2021-03-20 23:11:56 +01:00
|
|
|
if (!ctx.profile.support_vertex_instance_id) {
|
|
|
|
switch (attr) {
|
|
|
|
case IR::Attribute::InstanceId:
|
|
|
|
return ctx.OpISub(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_index),
|
|
|
|
ctx.OpLoad(ctx.U32[1], ctx.base_instance));
|
|
|
|
case IR::Attribute::VertexId:
|
|
|
|
return ctx.OpISub(ctx.U32[1], ctx.OpLoad(ctx.U32[1], ctx.vertex_index),
|
|
|
|
ctx.OpLoad(ctx.U32[1], ctx.base_vertex));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 23:28:31 +01:00
|
|
|
return ctx.OpLoad(ctx.F32[1], InputAttrPointer(ctx, attr));
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 23:28:31 +01:00
|
|
|
void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value) {
|
|
|
|
ctx.OpStore(OutputAttrPointer(ctx, attr), value);
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetAttributeIndexed(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetAttributeIndexed(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:28:31 +01:00
|
|
|
void EmitSetFragColor(EmitContext& ctx, u32 index, u32 component, Id value) {
|
|
|
|
const Id component_id{ctx.Constant(ctx.U32[1], component)};
|
|
|
|
const Id pointer{ctx.OpAccessChain(ctx.output_f32, ctx.frag_color.at(index), component_id)};
|
|
|
|
ctx.OpStore(pointer, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitSetFragDepth(EmitContext& ctx, Id value) {
|
|
|
|
ctx.OpStore(ctx.frag_depth, value);
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetZFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetSFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetCFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitGetOFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetZFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetSFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetCFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
void EmitSetOFlag(EmitContext&) {
|
2021-02-08 06:54:35 +01:00
|
|
|
throw NotImplementedException("SPIR-V Instruction");
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
Id EmitWorkgroupId(EmitContext& ctx) {
|
2021-02-16 08:10:22 +01:00
|
|
|
return ctx.OpLoad(ctx.U32[3], ctx.workgroup_id);
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
Id EmitLocalInvocationId(EmitContext& ctx) {
|
2021-02-16 08:10:22 +01:00
|
|
|
return ctx.OpLoad(ctx.U32[3], ctx.local_invocation_id);
|
2021-02-08 06:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Backend::SPIRV
|