Simply legacy attribute implement

This commit is contained in:
Feng Chen 2021-11-01 12:00:13 +08:00
parent 49f9a44235
commit f2a4204245
3 changed files with 125 additions and 152 deletions

View file

@ -441,6 +441,22 @@ size_t FindAndSetNextUnusedLocation(std::bitset<IR::NUM_GENERICS>& used_location
} }
throw RuntimeError("Unable to get an unused location for legacy attribute"); throw RuntimeError("Unable to get an unused location for legacy attribute");
} }
Id DefineLegacyInput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
size_t& start_offset) {
const Id id{DefineInput(ctx, ctx.F32[4], true)};
const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
ctx.Decorate(id, spv::Decoration::Location, location);
return id;
}
Id DefineLegacyOutput(EmitContext& ctx, std::bitset<IR::NUM_GENERICS>& used_locations,
size_t& start_offset, std::optional<u32> invocations) {
const Id id{DefineOutput(ctx, ctx.F32[4], invocations)};
const size_t location = FindAndSetNextUnusedLocation(used_locations, start_offset);
ctx.Decorate(id, spv::Decoration::Location, location);
return id;
}
} // Anonymous namespace } // Anonymous namespace
void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) { void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
@ -522,6 +538,64 @@ Id EmitContext::BitOffset16(const IR::Value& offset) {
return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u)); return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
} }
Id EmitContext::InputLegacyAttribute(IR::Attribute attribute) {
if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
attribute <= IR::Attribute::ColorFrontDiffuseA) {
return input_front_color;
}
if (attribute >= IR::Attribute::ColorFrontSpecularR &&
attribute <= IR::Attribute::ColorFrontSpecularA) {
return input_front_secondary_color;
}
if (attribute >= IR::Attribute::ColorBackDiffuseR &&
attribute <= IR::Attribute::ColorBackDiffuseA) {
return input_back_color;
}
if (attribute >= IR::Attribute::ColorBackSpecularR &&
attribute <= IR::Attribute::ColorBackSpecularA) {
return input_back_secondary_color;
}
if (attribute == IR::Attribute::FogCoordinate) {
return input_fog_frag_coord;
}
if (attribute >= IR::Attribute::FixedFncTexture0S &&
attribute <= IR::Attribute::FixedFncTexture9Q) {
u32 index =
(static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
return input_fixed_fnc_textures[index];
}
throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
}
Id EmitContext::OutputLegacyAttribute(IR::Attribute attribute) {
if (attribute >= IR::Attribute::ColorFrontDiffuseR &&
attribute <= IR::Attribute::ColorFrontDiffuseA) {
return output_front_color;
}
if (attribute >= IR::Attribute::ColorFrontSpecularR &&
attribute <= IR::Attribute::ColorFrontSpecularA) {
return output_front_secondary_color;
}
if (attribute >= IR::Attribute::ColorBackDiffuseR &&
attribute <= IR::Attribute::ColorBackDiffuseA) {
return output_back_color;
}
if (attribute >= IR::Attribute::ColorBackSpecularR &&
attribute <= IR::Attribute::ColorBackSpecularA) {
return output_back_secondary_color;
}
if (attribute == IR::Attribute::FogCoordinate) {
return output_fog_frag_coord;
}
if (attribute >= IR::Attribute::FixedFncTexture0S &&
attribute <= IR::Attribute::FixedFncTexture9Q) {
u32 index =
(static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4;
return output_fixed_fnc_textures[index];
}
throw InvalidArgument("Attribute is not legacy attribute {}", attribute);
}
void EmitContext::DefineCommonTypes(const Info& info) { void EmitContext::DefineCommonTypes(const Info& info) {
void_id = TypeVoid(); void_id = TypeVoid();
@ -1281,41 +1355,26 @@ void EmitContext::DefineInputs(const IR::Program& program) {
} }
size_t previous_unused_location = 0; size_t previous_unused_location = 0;
if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
const Id id{DefineInput(*this, F32[4], true)}; input_front_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
Decorate(id, spv::Decoration::Location,
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_front_color = id;
} }
if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { if (loads.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
const Id id{DefineInput(*this, F32[4], true)}; input_front_secondary_color =
Decorate(id, spv::Decoration::Location, DefineLegacyInput(*this, used_locations, previous_unused_location);
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_front_secondary_color = id;
} }
if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { if (loads.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
const Id id{DefineInput(*this, F32[4], true)}; input_back_color = DefineLegacyInput(*this, used_locations, previous_unused_location);
Decorate(id, spv::Decoration::Location,
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_back_color = id;
} }
if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) { if (loads.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
const Id id{DefineInput(*this, F32[4], true)}; input_back_secondary_color =
Decorate(id, spv::Decoration::Location, DefineLegacyInput(*this, used_locations, previous_unused_location);
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_back_secondary_color = id;
} }
if (loads.AnyComponent(IR::Attribute::FogCoordinate)) { if (loads.AnyComponent(IR::Attribute::FogCoordinate)) {
const Id id{DefineInput(*this, F32[4], true)}; input_fog_frag_coord = DefineLegacyInput(*this, used_locations, previous_unused_location);
Decorate(id, spv::Decoration::Location,
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_fog_frag_coord = id;
} }
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
const Id id{DefineInput(*this, F32[4], true)}; input_fixed_fnc_textures[index] =
Decorate(id, spv::Decoration::Location, DefineLegacyInput(*this, used_locations, previous_unused_location);
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
input_fixed_fnc_textures[index] = id;
} }
} }
if (stage == Stage::TessellationEval) { if (stage == Stage::TessellationEval) {
@ -1377,46 +1436,29 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
} }
size_t previous_unused_location = 0; size_t previous_unused_location = 0;
if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) { if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_front_color =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
static_cast<u32>(
FindAndSetNextUnusedLocation(used_locations, previous_unused_location)));
output_front_color = id;
} }
if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) { if (info.stores.AnyComponent(IR::Attribute::ColorFrontSpecularR)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_front_secondary_color =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
static_cast<u32>(
FindAndSetNextUnusedLocation(used_locations, previous_unused_location)));
output_front_secondary_color = id;
} }
if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) { if (info.stores.AnyComponent(IR::Attribute::ColorBackDiffuseR)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_back_color =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
static_cast<u32>(
FindAndSetNextUnusedLocation(used_locations, previous_unused_location)));
output_back_color = id;
} }
if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) { if (info.stores.AnyComponent(IR::Attribute::ColorBackSpecularR)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_back_secondary_color =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
static_cast<u32>(
FindAndSetNextUnusedLocation(used_locations, previous_unused_location)));
output_back_secondary_color = id;
} }
if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) { if (info.stores.AnyComponent(IR::Attribute::FogCoordinate)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_fog_frag_coord =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
static_cast<u32>(
FindAndSetNextUnusedLocation(used_locations, previous_unused_location)));
output_fog_frag_coord = id;
} }
for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) { for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) { if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
const Id id{DefineOutput(*this, F32[4], invocations)}; output_fixed_fnc_textures[index] =
Decorate(id, spv::Decoration::Location, DefineLegacyOutput(*this, used_locations, previous_unused_location, invocations);
FindAndSetNextUnusedLocation(used_locations, previous_unused_location));
output_fixed_fnc_textures[index] = id;
} }
} }
switch (stage) { switch (stage) {

View file

@ -113,6 +113,9 @@ public:
[[nodiscard]] Id BitOffset8(const IR::Value& offset); [[nodiscard]] Id BitOffset8(const IR::Value& offset);
[[nodiscard]] Id BitOffset16(const IR::Value& offset); [[nodiscard]] Id BitOffset16(const IR::Value& offset);
Id InputLegacyAttribute(IR::Attribute attribute);
Id OutputLegacyAttribute(IR::Attribute attribute);
Id Const(u32 value) { Id Const(u32 value) {
return Constant(U32[1], value); return Constant(U32[1], value);
} }

View file

@ -43,23 +43,12 @@ Id AttrPointer(EmitContext& ctx, Id pointer_type, Id vertex, Id base, Args&&...
} }
} }
bool IsFixedFncTexture(IR::Attribute attribute) { bool IsLegacyAttribute(IR::Attribute attribute) {
return attribute >= IR::Attribute::FixedFncTexture0S && return (attribute >= IR::Attribute::ColorFrontDiffuseR &&
attribute <= IR::Attribute::FixedFncTexture9Q; attribute <= IR::Attribute::ColorBackSpecularA) ||
} attribute == IR::Attribute::FogCoordinate ||
(attribute >= IR::Attribute::FixedFncTexture0S &&
u32 FixedFncTextureAttributeIndex(IR::Attribute attribute) { attribute <= IR::Attribute::FixedFncTexture9Q);
if (!IsFixedFncTexture(attribute)) {
throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute);
}
return (static_cast<u32>(attribute) - static_cast<u32>(IR::Attribute::FixedFncTexture0S)) / 4u;
}
u32 FixedFncTextureAttributeElement(IR::Attribute attribute) {
if (!IsFixedFncTexture(attribute)) {
throw InvalidArgument("Attribute {} is not a FixedFncTexture", attribute);
}
return static_cast<u32>(attribute) % 4u;
} }
template <typename... Args> template <typename... Args>
@ -93,12 +82,16 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id); return OutputAccessChain(ctx, ctx.output_f32, info.id, index_id);
} }
} }
if (IsFixedFncTexture(attr)) { if (IsLegacyAttribute(attr)) {
const u32 index{FixedFncTextureAttributeIndex(attr)}; if (attr == IR::Attribute::FogCoordinate) {
const u32 element{FixedFncTextureAttributeElement(attr)}; return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
const Id element_id{ctx.Const(element)}; ctx.Const(0u));
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fixed_fnc_textures[index], } else {
element_id); const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.OutputLegacyAttribute(attr),
element_id);
}
} }
switch (attr) { switch (attr) {
case IR::Attribute::PointSize: case IR::Attribute::PointSize:
@ -111,43 +104,6 @@ std::optional<OutAttr> OutputAttrPointer(EmitContext& ctx, IR::Attribute attr) {
const Id element_id{ctx.Const(element)}; const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id); return OutputAccessChain(ctx, ctx.output_f32, ctx.output_position, element_id);
} }
case IR::Attribute::ColorFrontDiffuseR:
case IR::Attribute::ColorFrontDiffuseG:
case IR::Attribute::ColorFrontDiffuseB:
case IR::Attribute::ColorFrontDiffuseA: {
const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_color, element_id);
}
case IR::Attribute::ColorFrontSpecularR:
case IR::Attribute::ColorFrontSpecularG:
case IR::Attribute::ColorFrontSpecularB:
case IR::Attribute::ColorFrontSpecularA: {
const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_front_secondary_color, element_id);
}
case IR::Attribute::ColorBackDiffuseR:
case IR::Attribute::ColorBackDiffuseG:
case IR::Attribute::ColorBackDiffuseB:
case IR::Attribute::ColorBackDiffuseA: {
const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_color, element_id);
}
case IR::Attribute::ColorBackSpecularR:
case IR::Attribute::ColorBackSpecularG:
case IR::Attribute::ColorBackSpecularB:
case IR::Attribute::ColorBackSpecularA: {
const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_back_secondary_color, element_id);
}
case IR::Attribute::FogCoordinate: {
const u32 element{static_cast<u32>(attr) % 4};
const Id element_id{ctx.Const(element)};
return OutputAccessChain(ctx, ctx.output_f32, ctx.output_fog_frag_coord, element_id);
}
case IR::Attribute::ClipDistance0: case IR::Attribute::ClipDistance0:
case IR::Attribute::ClipDistance1: case IR::Attribute::ClipDistance1:
case IR::Attribute::ClipDistance2: case IR::Attribute::ClipDistance2:
@ -370,11 +326,17 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
const Id value{ctx.OpLoad(type->id, pointer)}; const Id value{ctx.OpLoad(type->id, pointer)};
return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value; return type->needs_cast ? ctx.OpBitcast(ctx.F32[1], value) : value;
} }
if (IsFixedFncTexture(attr)) { if (IsLegacyAttribute(attr)) {
const u32 index{FixedFncTextureAttributeIndex(attr)}; if (attr == IR::Attribute::FogCoordinate) {
const Id attr_id{ctx.input_fixed_fnc_textures[index]}; const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex, attr_id, ctx.Const(element))}; ctx.InputLegacyAttribute(attr), ctx.Const(0u))};
return ctx.OpLoad(ctx.F32[1], attr_ptr); return ctx.OpLoad(ctx.F32[1], attr_ptr);
} else {
const Id element_id{ctx.Const(element)};
const Id attr_ptr{AttrPointer(ctx, ctx.input_f32, vertex,
ctx.InputLegacyAttribute(attr), element_id)};
return ctx.OpLoad(ctx.F32[1], attr_ptr);
}
} }
switch (attr) { switch (attr) {
case IR::Attribute::PrimitiveId: case IR::Attribute::PrimitiveId:
@ -385,40 +347,6 @@ Id EmitGetAttribute(EmitContext& ctx, IR::Attribute attr, Id vertex) {
case IR::Attribute::PositionW: case IR::Attribute::PositionW:
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position, return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_position,
ctx.Const(element))); ctx.Const(element)));
case IR::Attribute::ColorFrontDiffuseR:
case IR::Attribute::ColorFrontDiffuseG:
case IR::Attribute::ColorFrontDiffuseB:
case IR::Attribute::ColorFrontDiffuseA: {
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_color,
ctx.Const(element)));
}
case IR::Attribute::ColorFrontSpecularR:
case IR::Attribute::ColorFrontSpecularG:
case IR::Attribute::ColorFrontSpecularB:
case IR::Attribute::ColorFrontSpecularA: {
return ctx.OpLoad(ctx.F32[1],
AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_front_secondary_color,
ctx.Const(element)));
}
case IR::Attribute::ColorBackDiffuseR:
case IR::Attribute::ColorBackDiffuseG:
case IR::Attribute::ColorBackDiffuseB:
case IR::Attribute::ColorBackDiffuseA: {
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_color,
ctx.Const(element)));
}
case IR::Attribute::ColorBackSpecularR:
case IR::Attribute::ColorBackSpecularG:
case IR::Attribute::ColorBackSpecularB:
case IR::Attribute::ColorBackSpecularA: {
return ctx.OpLoad(ctx.F32[1],
AttrPointer(ctx, ctx.input_f32, vertex, ctx.input_back_secondary_color,
ctx.Const(element)));
}
case IR::Attribute::FogCoordinate: {
return ctx.OpLoad(ctx.F32[1], AttrPointer(ctx, ctx.input_f32, vertex,
ctx.input_fog_frag_coord, ctx.Const(element)));
}
case IR::Attribute::InstanceId: case IR::Attribute::InstanceId:
if (ctx.profile.support_vertex_instance_id) { if (ctx.profile.support_vertex_instance_id) {
return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id)); return ctx.OpBitcast(ctx.F32[1], ctx.OpLoad(ctx.U32[1], ctx.instance_id));