spv_shader_gen: Remove _id suffix within EmitterIDs-type

This commit is contained in:
Wunkolo 2024-03-03 11:27:16 -08:00
parent 77eb142304
commit eaa5b74f5e
2 changed files with 118 additions and 128 deletions

View File

@ -26,18 +26,18 @@ VertexModule::VertexModule() : Sirit::Module{SPIRV_VERSION_1_3} {
VertexModule::~VertexModule() = default;
void VertexModule::DefineArithmeticTypes() {
ids.void_id = Name(TypeVoid(), "void_id");
ids.bool_id = Name(TypeBool(), "bool_id");
ids.f32_id = Name(TypeFloat(32), "f32_id");
ids.i32_id = Name(TypeSInt(32), "i32_id");
ids.u32_id = Name(TypeUInt(32), "u32_id");
ids.void_ = Name(TypeVoid(), "void_id");
ids.bool_ = Name(TypeBool(), "bool_id");
ids.f32 = Name(TypeFloat(32), "f32_id");
ids.i32 = Name(TypeSInt(32), "i32_id");
ids.u32 = Name(TypeUInt(32), "u32_id");
for (u32 size = 2; size <= 4; size++) {
const u32 i = size - 2;
ids.bvec_ids.ids[i] = Name(TypeVector(ids.bool_id, size), fmt::format("bvec{}_id", size));
ids.vec_ids.ids[i] = Name(TypeVector(ids.f32_id, size), fmt::format("vec{}_id", size));
ids.ivec_ids.ids[i] = Name(TypeVector(ids.i32_id, size), fmt::format("ivec{}_id", size));
ids.uvec_ids.ids[i] = Name(TypeVector(ids.u32_id, size), fmt::format("uvec{}_id", size));
ids.bvec.ids[i] = Name(TypeVector(ids.bool_, size), fmt::format("bvec{}_id", size));
ids.vec.ids[i] = Name(TypeVector(ids.f32, size), fmt::format("vec{}_id", size));
ids.ivec.ids[i] = Name(TypeVector(ids.i32, size), fmt::format("ivec{}_id", size));
ids.uvec.ids[i] = Name(TypeVector(ids.u32, size), fmt::format("uvec{}_id", size));
}
}
@ -50,24 +50,24 @@ void VertexModule::DefineEntryPoint() {
const Id interface_ids[] = {
// Inputs
ids.vert_in_position_id,
ids.vert_in_color_id,
ids.vert_in_texcoord0_id,
ids.vert_in_texcoord1_id,
ids.vert_in_texcoord2_id,
ids.vert_in_texcoord0_w_id,
ids.vert_in_normquat_id,
ids.vert_in_view_id,
ids.vert_in_position,
ids.vert_in_color,
ids.vert_in_texcoord0,
ids.vert_in_texcoord1,
ids.vert_in_texcoord2,
ids.vert_in_texcoord0_w,
ids.vert_in_normquat,
ids.vert_in_view,
// Outputs
ids.gl_position,
ids.gl_clip_distance,
ids.vert_out_color_id,
ids.vert_out_texcoord0_id,
ids.vert_out_texcoord1_id,
ids.vert_out_texcoord2_id,
ids.vert_out_texcoord0_w_id,
ids.vert_out_normquat_id,
ids.vert_out_view_id,
ids.vert_out_color,
ids.vert_out_texcoord0,
ids.vert_out_texcoord1,
ids.vert_out_texcoord2,
ids.vert_out_texcoord0_w,
ids.vert_out_normquat,
ids.vert_out_view,
};
AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", interface_ids);
@ -77,40 +77,39 @@ void VertexModule::DefineInterface() {
// Define interface block
/// Inputs
ids.vert_in_position_id =
Name(DefineInput(ids.vec_ids.Get(4), ATTRIBUTE_POSITION), "vert_in_position");
ids.vert_in_color_id = Name(DefineInput(ids.vec_ids.Get(4), ATTRIBUTE_COLOR), "vert_in_color");
ids.vert_in_texcoord0_id =
Name(DefineInput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD0), "vert_in_texcoord0");
ids.vert_in_texcoord1_id =
Name(DefineInput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD1), "vert_in_texcoord1");
ids.vert_in_texcoord2_id =
Name(DefineInput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD2), "vert_in_texcoord2");
ids.vert_in_texcoord0_w_id =
Name(DefineInput(ids.f32_id, ATTRIBUTE_TEXCOORD0_W), "vert_in_texcoord0_w");
ids.vert_in_normquat_id =
Name(DefineInput(ids.vec_ids.Get(4), ATTRIBUTE_NORMQUAT), "vert_in_normquat");
ids.vert_in_view_id = Name(DefineInput(ids.vec_ids.Get(3), ATTRIBUTE_VIEW), "vert_in_view");
ids.vert_in_position =
Name(DefineInput(ids.vec.Get(4), ATTRIBUTE_POSITION), "vert_in_position");
ids.vert_in_color = Name(DefineInput(ids.vec.Get(4), ATTRIBUTE_COLOR), "vert_in_color");
ids.vert_in_texcoord0 =
Name(DefineInput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD0), "vert_in_texcoord0");
ids.vert_in_texcoord1 =
Name(DefineInput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD1), "vert_in_texcoord1");
ids.vert_in_texcoord2 =
Name(DefineInput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD2), "vert_in_texcoord2");
ids.vert_in_texcoord0_w =
Name(DefineInput(ids.f32, ATTRIBUTE_TEXCOORD0_W), "vert_in_texcoord0_w");
ids.vert_in_normquat =
Name(DefineInput(ids.vec.Get(4), ATTRIBUTE_NORMQUAT), "vert_in_normquat");
ids.vert_in_view = Name(DefineInput(ids.vec.Get(3), ATTRIBUTE_VIEW), "vert_in_view");
/// Outputs
ids.vert_out_color_id =
Name(DefineOutput(ids.vec_ids.Get(4), ATTRIBUTE_COLOR), "vert_out_color");
ids.vert_out_texcoord0_id =
Name(DefineOutput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD0), "vert_out_texcoord0");
ids.vert_out_texcoord1_id =
Name(DefineOutput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD1), "vert_out_texcoord1");
ids.vert_out_texcoord2_id =
Name(DefineOutput(ids.vec_ids.Get(2), ATTRIBUTE_TEXCOORD2), "vert_out_texcoord2");
ids.vert_out_texcoord0_w_id =
Name(DefineOutput(ids.f32_id, ATTRIBUTE_TEXCOORD0_W), "vert_out_texcoord0_w");
ids.vert_out_normquat_id =
Name(DefineOutput(ids.vec_ids.Get(4), ATTRIBUTE_NORMQUAT), "vert_out_normquat");
ids.vert_out_view_id = Name(DefineOutput(ids.vec_ids.Get(3), ATTRIBUTE_VIEW), "vert_out_view");
ids.vert_out_color = Name(DefineOutput(ids.vec.Get(4), ATTRIBUTE_COLOR), "vert_out_color");
ids.vert_out_texcoord0 =
Name(DefineOutput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD0), "vert_out_texcoord0");
ids.vert_out_texcoord1 =
Name(DefineOutput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD1), "vert_out_texcoord1");
ids.vert_out_texcoord2 =
Name(DefineOutput(ids.vec.Get(2), ATTRIBUTE_TEXCOORD2), "vert_out_texcoord2");
ids.vert_out_texcoord0_w =
Name(DefineOutput(ids.f32, ATTRIBUTE_TEXCOORD0_W), "vert_out_texcoord0_w");
ids.vert_out_normquat =
Name(DefineOutput(ids.vec.Get(4), ATTRIBUTE_NORMQUAT), "vert_out_normquat");
ids.vert_out_view = Name(DefineOutput(ids.vec.Get(3), ATTRIBUTE_VIEW), "vert_out_view");
/// Uniforms
// vs_data
const Id type_vs_data = Name(TypeStruct(ids.u32_id, ids.vec_ids.Get(4)), "vs_data");
const Id type_vs_data = Name(TypeStruct(ids.u32, ids.vec.Get(4)), "vs_data");
Decorate(type_vs_data, spv::Decoration::Block);
ids.ptr_vs_data = AddGlobalVariable(TypePointer(spv::StorageClass::Uniform, type_vs_data),
@ -126,36 +125,35 @@ void VertexModule::DefineInterface() {
MemberDecorate(type_vs_data, 1, spv::Decoration::Offset, 16);
/// Built-ins
ids.gl_position = DefineVar(ids.vec_ids.Get(4), spv::StorageClass::Output);
ids.gl_position = DefineVar(ids.vec.Get(4), spv::StorageClass::Output);
Decorate(ids.gl_position, spv::Decoration::BuiltIn, spv::BuiltIn::Position);
ids.gl_clip_distance =
DefineVar(TypeArray(ids.f32_id, Constant(ids.u32_id, 2)), spv::StorageClass::Output);
DefineVar(TypeArray(ids.f32, Constant(ids.u32, 2)), spv::StorageClass::Output);
Decorate(ids.gl_clip_distance, spv::Decoration::BuiltIn, spv::BuiltIn::ClipDistance);
}
Id VertexModule::WriteFuncSanitizeVertex() {
const Id func_type = TypeFunction(ids.vec_ids.Get(4), ids.vec_ids.Get(4));
const Id func =
Name(OpFunction(ids.vec_ids.Get(4), spv::FunctionControlMask::MaskNone, func_type),
const Id func_type = TypeFunction(ids.vec.Get(4), ids.vec.Get(4));
const Id func = Name(OpFunction(ids.vec.Get(4), spv::FunctionControlMask::MaskNone, func_type),
"SanitizeVertex");
const Id arg_pos = OpFunctionParameter(ids.vec_ids.Get(4));
const Id arg_pos = OpFunctionParameter(ids.vec.Get(4));
AddLabel(OpLabel());
const Id result = AddLocalVariable(TypePointer(spv::StorageClass::Function, ids.vec_ids.Get(4)),
const Id result = AddLocalVariable(TypePointer(spv::StorageClass::Function, ids.vec.Get(4)),
spv::StorageClass::Function);
OpStore(result, arg_pos);
const Id pos_z = OpCompositeExtract(ids.f32_id, arg_pos, 2);
const Id pos_w = OpCompositeExtract(ids.f32_id, arg_pos, 3);
const Id pos_z = OpCompositeExtract(ids.f32, arg_pos, 2);
const Id pos_w = OpCompositeExtract(ids.f32, arg_pos, 3);
const Id ndc_z = OpFDiv(ids.f32_id, pos_z, pos_w);
const Id ndc_z = OpFDiv(ids.f32, pos_z, pos_w);
// if (ndc_z > 0.f && ndc_z < 0.000001f)
const Id test_1 =
OpLogicalAnd(ids.bool_id, OpFOrdGreaterThan(ids.bool_id, ndc_z, Constant(ids.f32_id, 0.0f)),
OpFOrdLessThan(ids.bool_id, ndc_z, Constant(ids.f32_id, 0.000001f)));
OpLogicalAnd(ids.bool_, OpFOrdGreaterThan(ids.bool_, ndc_z, Constant(ids.f32, 0.0f)),
OpFOrdLessThan(ids.bool_, ndc_z, Constant(ids.f32, 0.000001f)));
{
const Id true_label = OpLabel();
@ -166,8 +164,7 @@ Id VertexModule::WriteFuncSanitizeVertex() {
AddLabel(true_label);
// .z = 0.0f;
OpStore(result,
OpCompositeInsert(ids.vec_ids.Get(4), ConstantNull(ids.f32_id), arg_pos, 2));
OpStore(result, OpCompositeInsert(ids.vec.Get(4), ConstantNull(ids.f32), arg_pos, 2));
OpBranch(end_label);
AddLabel(end_label);
@ -175,8 +172,8 @@ Id VertexModule::WriteFuncSanitizeVertex() {
// if (ndc_z < -1.f && ndc_z > -1.00001f)
const Id test_2 =
OpLogicalAnd(ids.bool_id, OpFOrdLessThan(ids.bool_id, ndc_z, Constant(ids.f32_id, -1.0f)),
OpFOrdGreaterThan(ids.bool_id, ndc_z, Constant(ids.f32_id, -1.00001f)));
OpLogicalAnd(ids.bool_, OpFOrdLessThan(ids.bool_, ndc_z, Constant(ids.f32, -1.0f)),
OpFOrdGreaterThan(ids.bool_, ndc_z, Constant(ids.f32, -1.00001f)));
{
const Id true_label = OpLabel();
const Id end_label = OpLabel();
@ -186,14 +183,14 @@ Id VertexModule::WriteFuncSanitizeVertex() {
AddLabel(true_label);
// .z = -.w;
const Id neg_w = OpFNegate(ids.f32_id, OpCompositeExtract(ids.f32_id, arg_pos, 3));
OpStore(result, OpCompositeInsert(ids.vec_ids.Get(4), neg_w, arg_pos, 2));
const Id neg_w = OpFNegate(ids.f32, OpCompositeExtract(ids.f32, arg_pos, 3));
OpStore(result, OpCompositeInsert(ids.vec.Get(4), neg_w, arg_pos, 2));
OpBranch(end_label);
AddLabel(end_label);
}
OpReturnValue(OpLoad(ids.vec_ids.Get(4), result));
OpReturnValue(OpLoad(ids.vec.Get(4), result));
OpFunctionEnd();
return func;
}
@ -201,11 +198,11 @@ Id VertexModule::WriteFuncSanitizeVertex() {
void VertexModule::Generate(Common::UniqueFunction<void, Sirit::Module&, const EmitterIDs&> proc) {
AddLabel(OpLabel());
ids.ptr_enable_clip1 = OpAccessChain(TypePointer(spv::StorageClass::Uniform, ids.u32_id),
ids.ptr_vs_data, Constant(ids.u32_id, 0));
ids.ptr_enable_clip1 = OpAccessChain(TypePointer(spv::StorageClass::Uniform, ids.u32),
ids.ptr_vs_data, Constant(ids.u32, 0));
ids.ptr_clip_coef = OpAccessChain(TypePointer(spv::StorageClass::Uniform, ids.vec_ids.Get(4)),
ids.ptr_vs_data, Constant(ids.u32_id, 1));
ids.ptr_clip_coef = OpAccessChain(TypePointer(spv::StorageClass::Uniform, ids.vec.Get(4)),
ids.ptr_vs_data, Constant(ids.u32, 1));
proc(*this, ids);
OpReturn();
@ -222,39 +219,32 @@ std::vector<u32> GenerateTrivialVertexShader(bool use_clip_planes) {
VertexModule module;
module.Generate([use_clip_planes](Sirit::Module& code,
const VertexModule::EmitterIDs& ids) -> void {
const Id pos_sanitized =
code.OpFunctionCall(ids.vec_ids.Get(4), ids.sanitize_vertex,
code.OpLoad(ids.vec_ids.Get(4), ids.vert_in_position_id));
const Id pos_sanitized = code.OpFunctionCall(
ids.vec.Get(4), ids.sanitize_vertex, code.OpLoad(ids.vec.Get(4), ids.vert_in_position));
// Negate Z
const Id neg_z =
code.OpFNegate(ids.f32_id, code.OpCompositeExtract(ids.f32_id, pos_sanitized, 2));
const Id negated_z = code.OpCompositeInsert(ids.vec_ids.Get(4), neg_z, pos_sanitized, 2);
code.OpFNegate(ids.f32, code.OpCompositeExtract(ids.f32, pos_sanitized, 2));
const Id negated_z = code.OpCompositeInsert(ids.vec.Get(4), neg_z, pos_sanitized, 2);
code.OpStore(ids.gl_position, negated_z);
// Pass-through
code.OpStore(ids.vert_out_color_id, code.OpLoad(ids.vec_ids.Get(4), ids.vert_in_color_id));
code.OpStore(ids.vert_out_texcoord0_id,
code.OpLoad(ids.vec_ids.Get(2), ids.vert_in_texcoord0_id));
code.OpStore(ids.vert_out_texcoord1_id,
code.OpLoad(ids.vec_ids.Get(2), ids.vert_in_texcoord1_id));
code.OpStore(ids.vert_out_texcoord2_id,
code.OpLoad(ids.vec_ids.Get(2), ids.vert_in_texcoord2_id));
code.OpStore(ids.vert_out_texcoord0_w_id,
code.OpLoad(ids.f32_id, ids.vert_in_texcoord0_w_id));
code.OpStore(ids.vert_out_normquat_id,
code.OpLoad(ids.vec_ids.Get(4), ids.vert_in_normquat_id));
code.OpStore(ids.vert_out_view_id, code.OpLoad(ids.vec_ids.Get(3), ids.vert_in_view_id));
code.OpStore(ids.vert_out_color, code.OpLoad(ids.vec.Get(4), ids.vert_in_color));
code.OpStore(ids.vert_out_texcoord0, code.OpLoad(ids.vec.Get(2), ids.vert_in_texcoord0));
code.OpStore(ids.vert_out_texcoord1, code.OpLoad(ids.vec.Get(2), ids.vert_in_texcoord1));
code.OpStore(ids.vert_out_texcoord2, code.OpLoad(ids.vec.Get(2), ids.vert_in_texcoord2));
code.OpStore(ids.vert_out_texcoord0_w, code.OpLoad(ids.f32, ids.vert_in_texcoord0_w));
code.OpStore(ids.vert_out_normquat, code.OpLoad(ids.vec.Get(4), ids.vert_in_normquat));
code.OpStore(ids.vert_out_view, code.OpLoad(ids.vec.Get(3), ids.vert_in_view));
if (use_clip_planes) {
code.OpStore(code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32_id),
ids.gl_clip_distance, code.Constant(ids.u32_id, 0)),
code.OpStore(code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32),
ids.gl_clip_distance, code.Constant(ids.u32, 0)),
neg_z);
const Id enable_clip1 =
code.OpINotEqual(ids.bool_id, code.OpLoad(ids.u32_id, ids.ptr_enable_clip1),
code.Constant(ids.u32_id, 0));
const Id enable_clip1 = code.OpINotEqual(
ids.bool_, code.OpLoad(ids.u32, ids.ptr_enable_clip1), code.Constant(ids.u32, 0));
{
const Id true_label = code.OpLabel();
@ -267,9 +257,9 @@ std::vector<u32> GenerateTrivialVertexShader(bool use_clip_planes) {
code.AddLabel(true_label);
code.OpStore(
code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32_id),
ids.gl_clip_distance, code.Constant(ids.u32_id, 1)),
code.OpDot(ids.f32_id, code.OpLoad(ids.vec_ids.Get(4), ids.ptr_clip_coef),
code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32),
ids.gl_clip_distance, code.Constant(ids.u32, 1)),
code.OpDot(ids.f32, code.OpLoad(ids.vec.Get(4), ids.ptr_clip_coef),
pos_sanitized));
code.OpBranch(end_label);
@ -278,9 +268,9 @@ std::vector<u32> GenerateTrivialVertexShader(bool use_clip_planes) {
code.AddLabel(false_label);
code.OpStore(
code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32_id),
ids.gl_clip_distance, code.Constant(ids.u32_id, 1)),
code.ConstantNull(ids.f32_id));
code.OpAccessChain(code.TypePointer(spv::StorageClass::Output, ids.f32),
ids.gl_clip_distance, code.Constant(ids.u32, 1)),
code.ConstantNull(ids.f32));
code.OpBranch(end_label);
}

View File

@ -70,35 +70,35 @@ private:
public:
struct EmitterIDs {
Id void_id{};
Id bool_id{};
Id f32_id{};
Id i32_id{};
Id u32_id{};
Id void_{};
Id bool_{};
Id f32{};
Id i32{};
Id u32{};
VectorIds vec_ids{};
VectorIds ivec_ids{};
VectorIds uvec_ids{};
VectorIds bvec_ids{};
VectorIds vec{};
VectorIds ivec{};
VectorIds uvec{};
VectorIds bvec{};
// Input vertex attributes
Id vert_in_position_id{};
Id vert_in_color_id{};
Id vert_in_texcoord0_id{};
Id vert_in_texcoord1_id{};
Id vert_in_texcoord2_id{};
Id vert_in_texcoord0_w_id{};
Id vert_in_normquat_id{};
Id vert_in_view_id{};
Id vert_in_position{};
Id vert_in_color{};
Id vert_in_texcoord0{};
Id vert_in_texcoord1{};
Id vert_in_texcoord2{};
Id vert_in_texcoord0_w{};
Id vert_in_normquat{};
Id vert_in_view{};
// Output vertex attributes
Id vert_out_color_id{};
Id vert_out_texcoord0_id{};
Id vert_out_texcoord1_id{};
Id vert_out_texcoord2_id{};
Id vert_out_texcoord0_w_id{};
Id vert_out_normquat_id{};
Id vert_out_view_id{};
Id vert_out_color{};
Id vert_out_texcoord0{};
Id vert_out_texcoord1{};
Id vert_out_texcoord2{};
Id vert_out_texcoord0_w{};
Id vert_out_normquat{};
Id vert_out_view{};
// Uniforms