Overload on constant storage.

This commit is contained in:
Hans-Kristian Arntzen 2018-02-08 17:58:46 +01:00
parent b2c9487b0f
commit c9db3e5521
7 changed files with 69 additions and 16 deletions

View File

@ -28,8 +28,15 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }

View File

@ -25,8 +25,15 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }

View File

@ -19,8 +19,15 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }

View File

@ -28,8 +28,15 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }

View File

@ -25,8 +25,15 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }

View File

@ -19,15 +19,22 @@ struct main0_out
}; };
// Implementation of an array copy function to cover GLSL's ability to copy an array via assignment. // Implementation of an array copy function to cover GLSL's ability to copy an array via assignment.
template<typename T, typename U, uint N> template<typename T, uint N>
void spvArrayCopy(thread T (&dst)[N], U (&src)[N]) void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])
{
for (uint i = 0; i < N; dst[i] = src[i], i++);
}
// An overload for constant arrays.
template<typename T, uint N>
void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])
{ {
for (uint i = 0; i < N; dst[i] = src[i], i++); for (uint i = 0; i < N; dst[i] = src[i], i++);
} }
void test(thread float4 (&SPIRV_Cross_return_value)[2]) void test(thread float4 (&SPIRV_Cross_return_value)[2])
{ {
spvArrayCopy(SPIRV_Cross_return_value, _20); spvArrayCopyConstant(SPIRV_Cross_return_value, _20);
} }
void test2(thread float4 (&SPIRV_Cross_return_value)[2], thread float4& vInput0, thread float4& vInput1) void test2(thread float4 (&SPIRV_Cross_return_value)[2], thread float4& vInput0, thread float4& vInput1)

View File

@ -915,8 +915,16 @@ void CompilerMSL::emit_custom_functions()
case SPVFuncImplArrayCopy: case SPVFuncImplArrayCopy:
statement("// Implementation of an array copy function to cover GLSL's ability to copy an array via " statement("// Implementation of an array copy function to cover GLSL's ability to copy an array via "
"assignment."); "assignment.");
statement("template<typename T, typename U, uint N>"); statement("template<typename T, uint N>");
statement("void spvArrayCopy(thread T (&dst)[N], U (&src)[N])"); statement("void spvArrayCopy(thread T (&dst)[N], thread const T (&src)[N])");
begin_scope();
statement("for (uint i = 0; i < N; dst[i] = src[i], i++);");
end_scope();
statement("");
statement("// An overload for constant arrays.");
statement("template<typename T, uint N>");
statement("void spvArrayCopyConstant(thread T (&dst)[N], constant T (&src)[N])");
begin_scope(); begin_scope();
statement("for (uint i = 0; i < N; dst[i] = src[i], i++);"); statement("for (uint i = 0; i < N; dst[i] = src[i], i++);");
end_scope(); end_scope();
@ -1777,7 +1785,10 @@ bool CompilerMSL::maybe_emit_input_struct_assignment(uint32_t id_lhs, uint32_t i
void CompilerMSL::emit_array_copy(const string &lhs, uint32_t rhs_id) void CompilerMSL::emit_array_copy(const string &lhs, uint32_t rhs_id)
{ {
// Assignment from an array initializer is fine. // Assignment from an array initializer is fine.
statement("spvArrayCopy(", lhs, ", ", to_expression(rhs_id), ");"); if (ids[rhs_id].get_type() == TypeConstant)
statement("spvArrayCopyConstant(", lhs, ", ", to_expression(rhs_id), ");");
else
statement("spvArrayCopy(", lhs, ", ", to_expression(rhs_id), ");");
} }
// Since MSL does not allow arrays to be copied via simple variable assignment, // Since MSL does not allow arrays to be copied via simple variable assignment,