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

View File

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