Skip to content

Commit

Permalink
Merge branch 'dx12-mesh-flip-y' of https://github.com/Try/SPIRV-Cross
Browse files Browse the repository at this point in the history
…into pr-2168
  • Loading branch information
HansKristian-Work committed Jul 3, 2023
2 parents aafcc20 + 6837650 commit a065c3b
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
static const uint3 gl_WorkGroupSize = uint3(2u, 3u, 4u);

struct BlockOut
{
float4 a;
float4 b;
};

struct TaskPayload
{
float a;
float b;
int c;
};

static uint3 gl_WorkGroupID;
static uint3 gl_GlobalInvocationID;
static uint gl_LocalInvocationIndex;
struct SPIRV_Cross_Input
{
uint3 gl_WorkGroupID : SV_GroupID;
uint3 gl_GlobalInvocationID : SV_DispatchThreadID;
uint gl_LocalInvocationIndex : SV_GroupIndex;
};

struct gl_MeshPerVertexEXT
{
float4 vOut : TEXCOORD0;
BlockOut outputs : TEXCOORD2;
float4 gl_Position : SV_Position;
float gl_ClipDistance[1] : SV_ClipDistance;
float2 gl_CullDistance : SV_CullDistance;
};

struct gl_MeshPerPrimitiveEXT
{
};

groupshared float shared_float[16];

float4 spvFlipVertY(float4 v)
{
return float4(v.x, -v.y, v.z, v.w);
}

float spvFlipVertY(float v)
{
return -v;
}

void mesh_main(inout gl_MeshPerVertexEXT gl_MeshVerticesEXT[24], inout uint3 gl_PrimitiveTriangleIndicesEXT[22])
{
SetMeshOutputCounts(24u, 22u);
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = spvFlipVertY(float4(float3(gl_GlobalInvocationID), 1.0f));
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.y = spvFlipVertY(float3(gl_WorkGroupID).x);
float3 _46 = float3(gl_GlobalInvocationID);
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.x = _46.x;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.z = _46.y;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.w = _46.z;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0f;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_CullDistance[0] = 3.0f;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_CullDistance[1] = 5.0f;
GroupMemoryBarrierWithGroupSync();
if (gl_LocalInvocationIndex < 22u)
{
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex] = uint3(0u, 1u, 2u) + gl_LocalInvocationIndex.xxx;
}
}

[outputtopology("triangle")]
[numthreads(2, 3, 4)]
void main(SPIRV_Cross_Input stage_input, out vertices gl_MeshPerVertexEXT gl_MeshVerticesEXT[24], out indices uint3 gl_PrimitiveTriangleIndicesEXT[22])
{
gl_WorkGroupID = stage_input.gl_WorkGroupID;
gl_GlobalInvocationID = stage_input.gl_GlobalInvocationID;
gl_LocalInvocationIndex = stage_input.gl_LocalInvocationIndex;
mesh_main(gl_MeshVerticesEXT, gl_PrimitiveTriangleIndicesEXT);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#version 450
#extension GL_EXT_mesh_shader : require
layout(local_size_x = 2, local_size_y = 3, local_size_z = 4) in;
layout(triangles, max_vertices = 24, max_primitives = 22) out;

out gl_MeshPerVertexEXT
{
vec4 gl_Position;
float gl_PointSize;
float gl_ClipDistance[1];
float gl_CullDistance[2];
} gl_MeshVerticesEXT[];

layout(location = 0) out vec4 vOut[];

layout(location = 2) out BlockOut
{
vec4 a;
vec4 b;
} outputs[];

shared float shared_float[16];

struct TaskPayload
{
float a;
float b;
int c;
};

taskPayloadSharedEXT TaskPayload payload;

void main()
{
SetMeshOutputsEXT(24, 22);

gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = vec4(gl_GlobalInvocationID, 1.0);
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.y = float(gl_WorkGroupID);
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position.xzw = vec3(gl_GlobalInvocationID);

gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_CullDistance[0] = 3.0;
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_CullDistance[1] = 5.0;

barrier();
if (gl_LocalInvocationIndex < 22)
{
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex] = uvec3(0, 1, 2) + gl_LocalInvocationIndex;
}
}
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 @@ -9942,6 +9942,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 @@ -10149,6 +10155,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 @@ -10293,6 +10304,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 @@ -10319,6 +10345,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 @@ -11891,6 +11918,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
27 changes: 27 additions & 0 deletions spirv_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,20 @@ void CompilerHLSL::emit_resources()
end_scope();
statement("");
}

if (is_mesh_shader && options.vertex.flip_vert_y)
{
statement("float4 spvFlipVertY(float4 v)");
begin_scope();
statement("return float4(v.x, -v.y, v.z, v.w);");
end_scope();
statement("");
statement("float spvFlipVertY(float v)");
begin_scope();
statement("return -v;");
end_scope();
statement("");
}
}

void CompilerHLSL::emit_texture_size_variants(uint64_t variant_mask, const char *vecsize_qualifier, bool uav,
Expand Down Expand Up @@ -4950,6 +4964,19 @@ 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)
{
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]);
if (chain)
write_access_chain(*chain, ops[1], {});
Expand Down
2 changes: 2 additions & 0 deletions test_shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ def cross_compile_hlsl(shader, spirv, opt, force_no_external_validation, iterati
hlsl_args.append('--relax-nan-checks')
if '.structured.' in shader:
hlsl_args.append('--hlsl-preserve-structured-buffers')
if '.flip-vert-y.' in shader:
hlsl_args.append('--flip-vert-y')

subprocess.check_call(hlsl_args)

Expand Down

0 comments on commit a065c3b

Please sign in to comment.