Support gl_HelperInvocation on GLSL and MSL.

There is no obvious builtin for this on HLSL.
This commit is contained in:
Hans-Kristian Arntzen 2018-11-28 15:18:43 +01:00
parent fbae10db15
commit 61f1d8b2cf
8 changed files with 190 additions and 9 deletions

View File

@ -0,0 +1,32 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
struct main0_in
{
float2 vUV [[user(locn0)]];
};
fragment main0_out main0(main0_in in [[stage_in]], texture2d<float> uSampler [[texture(0)]], sampler uSamplerSmplr [[sampler(0)]])
{
main0_out out = {};
bool gl_HelperInvocation = simd_is_helper_thread();
float4 _51;
if (!gl_HelperInvocation)
{
_51 = uSampler.sample(uSamplerSmplr, in.vUV, level(0.0));
}
else
{
_51 = float4(1.0);
}
out.FragColor = _51;
return out;
}

View File

@ -0,0 +1,23 @@
#version 310 es
precision mediump float;
precision highp int;
layout(binding = 0) uniform mediump sampler2D uSampler;
layout(location = 0) in vec2 vUV;
layout(location = 0) out vec4 FragColor;
void main()
{
vec4 _51;
if (!gl_HelperInvocation)
{
_51 = textureLod(uSampler, vUV, 0.0);
}
else
{
_51 = vec4(1.0);
}
FragColor = _51;
}

View File

@ -0,0 +1,39 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
struct main0_in
{
float2 vUV [[user(locn0)]];
};
float4 foo(thread bool& gl_HelperInvocation, thread texture2d<float> uSampler, thread const sampler uSamplerSmplr, thread float2& vUV)
{
float4 color;
if (!gl_HelperInvocation)
{
color = uSampler.sample(uSamplerSmplr, vUV, level(0.0));
}
else
{
color = float4(1.0);
}
return color;
}
fragment main0_out main0(main0_in in [[stage_in]], texture2d<float> uSampler [[texture(0)]], sampler uSamplerSmplr [[sampler(0)]])
{
main0_out out = {};
bool gl_HelperInvocation = simd_is_helper_thread();
out.FragColor = foo(gl_HelperInvocation, uSampler, uSamplerSmplr, in.vUV);
return out;
}

View File

@ -0,0 +1,28 @@
#version 310 es
precision mediump float;
precision highp int;
layout(binding = 0) uniform mediump sampler2D uSampler;
layout(location = 0) in vec2 vUV;
layout(location = 0) out vec4 FragColor;
vec4 foo()
{
vec4 color;
if (!gl_HelperInvocation)
{
color = textureLod(uSampler, vUV, 0.0);
}
else
{
color = vec4(1.0);
}
return color;
}
void main()
{
FragColor = foo();
}

View File

@ -0,0 +1,21 @@
#version 310 es
precision mediump float;
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec2 vUV;
layout(binding = 0) uniform sampler2D uSampler;
vec4 foo()
{
vec4 color;
if (!gl_HelperInvocation)
color = textureLod(uSampler, vUV, 0.0);
else
color = vec4(1.0);
return color;
}
void main()
{
FragColor = foo();
}

View File

@ -0,0 +1,21 @@
#version 310 es
precision mediump float;
layout(location = 0) out vec4 FragColor;
layout(location = 0) in vec2 vUV;
layout(binding = 0) uniform sampler2D uSampler;
vec4 foo()
{
vec4 color;
if (!gl_HelperInvocation)
color = textureLod(uSampler, vUV, 0.0);
else
color = vec4(1.0);
return color;
}
void main()
{
FragColor = foo();
}

View File

@ -5320,6 +5320,8 @@ string CompilerGLSL::builtin_to_glsl(BuiltIn builtin, StorageClass storage)
return "gl_GlobalInvocationID";
case BuiltInLocalInvocationIndex:
return "gl_LocalInvocationIndex";
case BuiltInHelperInvocation:
return "gl_HelperInvocation";
case BuiltInBaseVertex:
if (options.es)
SPIRV_CROSS_THROW("BaseVertex not supported in ES profile.");

View File

@ -4023,15 +4023,7 @@ string CompilerMSL::entry_point_args(bool append_comma)
// point, we get that by calling get_sample_position() on the sample ID.
if (var.storage == StorageClassInput && is_builtin_variable(var))
{
if (bi_type != BuiltInSamplePosition)
{
if (!ep_args.empty())
ep_args += ", ";
ep_args += builtin_type_decl(bi_type) + " " + to_expression(var_id);
ep_args += " [[" + builtin_qualifier(bi_type) + "]]";
}
else
if (bi_type == BuiltInSamplePosition)
{
auto &entry_func = get<SPIRFunction>(ir.default_entry_point);
entry_func.fixup_hooks_in.push_back([=]() {
@ -4039,6 +4031,26 @@ string CompilerMSL::entry_point_args(bool append_comma)
to_expression(builtin_sample_id_id), ");");
});
}
else if (bi_type == BuiltInHelperInvocation)
{
if (msl_options.is_ios())
SPIRV_CROSS_THROW("simd_is_helper_thread() is only supported on macOS.");
else if (msl_options.is_macos() && !msl_options.supports_msl_version(2, 1))
SPIRV_CROSS_THROW("simd_is_helper_thread() requires version 2.1 on macOS.");
auto &entry_func = get<SPIRFunction>(ir.default_entry_point);
entry_func.fixup_hooks_in.push_back([=]() {
statement(builtin_type_decl(bi_type), " ", to_expression(var_id), " = simd_is_helper_thread();");
});
}
else
{
if (!ep_args.empty())
ep_args += ", ";
ep_args += builtin_type_decl(bi_type) + " " + to_expression(var_id);
ep_args += " [[" + builtin_qualifier(bi_type) + "]]";
}
}
}
}
@ -4794,6 +4806,9 @@ string CompilerMSL::builtin_type_decl(BuiltIn builtin)
case BuiltInLocalInvocationIndex:
return "uint";
case BuiltInHelperInvocation:
return "bool";
default:
return "unsupported-built-in-type";
}