Skip to content

Commit

Permalink
track access to meshlet position.y, via SPIRExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Jul 1, 2023
1 parent 34f7bd0 commit 6837650
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
4 changes: 4 additions & 0 deletions spirv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ struct SPIRExpression : IVariant
// Whether or not this is an access chain expression.
bool access_chain = false;

// Whether or not gl_MeshVerticesEXT[].gl_Position (as a whole or .y) is referenced
bool access_meshlet_position_y = false;

// A list of expressions which this expression depends on.
SmallVector<ID> expression_dependencies;

Expand Down Expand Up @@ -1580,6 +1583,7 @@ struct AccessChainMeta
bool storage_is_invariant = false;
bool flattened_struct = false;
bool relaxed_precision = false;
bool access_meshlet_position_y = false;
};

enum ExtendedDecorations
Expand Down
28 changes: 28 additions & 0 deletions spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9857,6 +9857,12 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice
bool relaxed_precision = has_decoration(base, DecorationRelaxedPrecision);
bool pending_array_enclose = false;
bool dimension_flatten = false;
bool access_meshlet_position_y = false;

if (auto *base_expr = maybe_get<SPIRExpression>(base))
{
access_meshlet_position_y = base_expr->access_meshlet_position_y;
}

// If we are translating access to a structured buffer, the first subscript '._m0' must be hidden
bool hide_first_subscript = count > 1 && is_user_type_structured(base);
Expand Down Expand Up @@ -10064,6 +10070,11 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice
}
else
expr = builtin_to_glsl(builtin, type->storage);

if (builtin == BuiltInPosition && get_execution_model() == ExecutionModelMeshEXT)
{
access_meshlet_position_y = true;
}
}
else
{
Expand Down Expand Up @@ -10208,6 +10219,21 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice
is_packed);
}

if (access_meshlet_position_y)
{
uint32_t id = 0;
if (is_literal)
{
id = index;
}
else
{
auto &c = get<SPIRConstant>(index);
id = c.scalar();
}
access_meshlet_position_y = (id == 1);
}

expr += deferred_index;
row_major_matrix_needs_conversion = false;

Expand All @@ -10234,6 +10260,7 @@ string CompilerGLSL::access_chain_internal(uint32_t base, const uint32_t *indice
meta->storage_is_invariant = is_invariant;
meta->storage_physical_type = physical_type;
meta->relaxed_precision = relaxed_precision;
meta->access_meshlet_position_y = access_meshlet_position_y;
}

return expr;
Expand Down Expand Up @@ -11799,6 +11826,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction)
expr.loaded_from = backing_variable ? backing_variable->self : ID(ops[2]);
expr.need_transpose = meta.need_transpose;
expr.access_chain = true;
expr.access_meshlet_position_y = meta.access_meshlet_position_y;

// Mark the result as being packed. Some platforms handled packed vectors differently than non-packed.
if (meta.storage_is_packed)
Expand Down
40 changes: 10 additions & 30 deletions spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2497,30 +2497,6 @@ void CompilerHLSL::analyze_meshlet_writes(uint32_t func_id, uint32_t id_per_vert
bool already_declared = false;
auto builtin_type = BuiltIn(get_decoration(var->self, DecorationBuiltIn));

if (op == OpAccessChain && i.length > 4)
{
auto &type = get_variable_element_type(*var);
auto *c = maybe_get<SPIRConstant>(ops[4]);
if (c != nullptr && c->scalar() == 0 &&
has_member_decoration(type.self, 0, DecorationBuiltIn) &&
get_member_decoration(type.self, 0, DecorationBuiltIn) == spv::BuiltInPosition)
{
const SPIRType &dst_type = get_pointee_type(ops[0]);
if (dst_type.vecsize == 4)
{
// full vec4 write
access_to_meshlet_position.insert(ops[1]);
}
else if (i.length > 5)
{
// gl_Position.y
auto *cY = maybe_get<SPIRConstant>(ops[5]);
if (cY != nullptr && cY->scalar() == 1)
access_to_meshlet_position.insert(ops[1]);
}
}
}

uint32_t var_id = var->self;
if (var->storage != StorageClassTaskPayloadWorkgroupEXT &&
builtin_type != BuiltInPrimitivePointIndicesEXT &&
Expand Down Expand Up @@ -4988,13 +4964,17 @@ void CompilerHLSL::write_access_chain(const SPIRAccessChain &chain, uint32_t val
void CompilerHLSL::emit_store(const Instruction &instruction)
{
auto ops = stream(instruction);
if (options.vertex.flip_vert_y && access_to_meshlet_position.count(ops[0]) != 0)
if (options.vertex.flip_vert_y)
{
auto lhs = to_dereferenced_expression(ops[0]);
auto rhs = to_pointer_expression(ops[1]);
statement(lhs, " = spvFlipVertY(", rhs, ");");
register_write(ops[0]);
return;
auto *expr = maybe_get<SPIRExpression>(ops[0]);
if (expr != nullptr && expr->access_meshlet_position_y)
{
auto lhs = to_dereferenced_expression(ops[0]);
auto rhs = to_unpacked_expression(ops[1]);
statement(lhs, " = spvFlipVertY(", rhs, ");");
register_write(ops[0]);
return;
}
}

auto *chain = maybe_get<SPIRAccessChain>(ops[0]);
Expand Down
1 change: 0 additions & 1 deletion spirv_hlsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ class CompilerHLSL : public CompilerGLSL
void analyze_meshlet_writes();
void analyze_meshlet_writes(uint32_t func_id, uint32_t id_per_vertex, uint32_t id_per_primitive,
std::unordered_set<uint32_t> &processed_func_ids);
std::unordered_set<uint32_t> access_to_meshlet_position;

BitcastType get_bitcast_type(uint32_t result_type, uint32_t op0);

Expand Down

0 comments on commit 6837650

Please sign in to comment.