Support passing implicit frag_coord arguments down to functions.

This commit is contained in:
Hans-Kristian Arntzen 2018-02-09 12:13:33 +01:00
parent 0912427046
commit 702e08671b
8 changed files with 130 additions and 4 deletions

View File

@ -0,0 +1,17 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
fragment main0_out main0(texture2d_ms<float> uSubpass0 [[texture(0)]], texture2d_ms<float> uSubpass1 [[texture(1)]], uint gl_SampleID [[sample_id]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = (uSubpass0.read(uint2(gl_FragCoord.xy), 1) + uSubpass1.read(uint2(gl_FragCoord.xy), 2)) + uSubpass0.read(uint2(gl_FragCoord.xy), gl_SampleID);
return out;
}

View File

@ -0,0 +1,17 @@
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
fragment main0_out main0(texture2d<float> uSubpass0 [[texture(0)]], texture2d<float> uSubpass1 [[texture(1)]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = uSubpass0.read(uint2(gl_FragCoord.xy), 0) + uSubpass1.read(uint2(gl_FragCoord.xy), 0);
return out;
}

View File

@ -0,0 +1,24 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
float4 load_subpasses(thread const texture2d_ms<float> uInput, thread uint& gl_SampleID, thread float4& gl_FragCoord)
{
return uInput.read(uint2(gl_FragCoord.xy), gl_SampleID);
}
fragment main0_out main0(texture2d_ms<float> uSubpass0 [[texture(0)]], texture2d_ms<float> uSubpass1 [[texture(1)]], uint gl_SampleID [[sample_id]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = (uSubpass0.read(uint2(gl_FragCoord.xy), 1) + uSubpass1.read(uint2(gl_FragCoord.xy), 2)) + load_subpasses(uSubpass0, gl_SampleID, gl_FragCoord);
return out;
}

View File

@ -0,0 +1,24 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct main0_out
{
float4 FragColor [[color(0)]];
};
float4 load_subpasses(thread const texture2d<float> uInput, thread float4& gl_FragCoord)
{
return uInput.read(uint2(gl_FragCoord.xy), 0);
}
fragment main0_out main0(texture2d<float> uSubpass0 [[texture(0)]], texture2d<float> uSubpass1 [[texture(1)]], float4 gl_FragCoord [[position]])
{
main0_out out = {};
out.FragColor = uSubpass0.read(uint2(gl_FragCoord.xy), 0) + load_subpasses(uSubpass1, gl_FragCoord);
return out;
}

View File

@ -0,0 +1,15 @@
#version 450
layout(input_attachment_index = 0, set = 0, binding = 0) uniform subpassInputMS uSubpass0;
layout(input_attachment_index = 1, set = 0, binding = 1) uniform subpassInputMS uSubpass1;
layout(location = 0) out vec4 FragColor;
vec4 load_subpasses(mediump subpassInputMS uInput)
{
return subpassLoad(uInput, gl_SampleID);
}
void main()
{
FragColor = subpassLoad(uSubpass0, 1) + subpassLoad(uSubpass1, 2) + load_subpasses(uSubpass0);
}

View File

@ -0,0 +1,16 @@
#version 310 es
precision mediump float;
layout(input_attachment_index = 0, set = 0, binding = 0) uniform mediump subpassInput uSubpass0;
layout(input_attachment_index = 1, set = 0, binding = 1) uniform mediump subpassInput uSubpass1;
layout(location = 0) out vec4 FragColor;
vec4 load_subpasses(mediump subpassInput uInput)
{
return subpassLoad(uInput);
}
void main()
{
FragColor = subpassLoad(uSubpass0) + load_subpasses(uSubpass1);
}

View File

@ -19,6 +19,7 @@
#include <algorithm>
#include <numeric>
#include <assert.h>
using namespace spv;
using namespace spirv_cross;
@ -69,6 +70,7 @@ void CompilerMSL::build_implicit_builtins()
meta[var.self].decoration.builtin &&
meta[var.self].decoration.builtin_type == BuiltInFragCoord)
{
builtin_frag_coord_id = var.self;
has_frag_coord = true;
break;
}
@ -93,10 +95,11 @@ void CompilerMSL::build_implicit_builtins()
vec4_type_ptr.parent_type = type_id;
vec4_type_ptr.storage = StorageClassInput;
auto &ptr_type = set<SPIRType>(type_ptr_id, vec4_type_ptr);
ptr_type.self = offset;
ptr_type.self = type_id;
set<SPIRVariable>(var_id, type_ptr_id, StorageClassInput);
set_decoration(var_id, DecorationBuiltIn, BuiltInFragCoord);
builtin_frag_coord_id = var_id;
}
}
}
@ -123,6 +126,7 @@ string CompilerMSL::compile()
backend.flexible_member_array_supported = false;
backend.can_declare_arrays_inline = false;
backend.can_return_array = false;
backend.boolean_mix_support = false;
replace_illegal_names();
@ -332,6 +336,14 @@ void CompilerMSL::extract_global_variables_from_function(uint32_t func_id, std::
if (global_var_ids.find(base_id) != global_var_ids.end())
added_arg_ids.insert(base_id);
auto &type = get<SPIRType>(ops[0]);
if (type.image.dim == DimSubpassData)
{
// Implicitly reads gl_FragCoord.
assert(builtin_frag_coord_id != 0);
added_arg_ids.insert(builtin_frag_coord_id);
}
break;
}
case OpFunctionCall:
@ -3023,7 +3035,7 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg)
if (is_array(type))
{
decl += " (&";
decl += to_name(var.self);
decl += to_expression(var.self);
decl += ")";
decl += type_to_array_glsl(type);
}
@ -3031,12 +3043,12 @@ string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg)
{
decl += "&";
decl += " ";
decl += to_name(var.self);
decl += to_expression(var.self);
}
else
{
decl += " ";
decl += to_name(var.self);
decl += to_expression(var.self);
}
return decl;

View File

@ -268,6 +268,7 @@ protected:
void emit_barrier(uint32_t id_exe_scope, uint32_t id_mem_scope, uint32_t id_mem_sem);
void emit_array_copy(const std::string &lhs, uint32_t rhs_id) override;
void build_implicit_builtins();
uint32_t builtin_frag_coord_id = 0;
Options options;
std::set<SPVFuncImpl> spv_function_implementations;