Skip to content

Commit

Permalink
Issue AcademySoftwareFoundation#2116 : Fixes Metal backend's generate…
Browse files Browse the repository at this point in the history
…d shaders with float/int constant Array Performance (AcademySoftwareFoundation#2117)

* Issue AcademySoftwareFoundation#2116 : Improves Metal Backend Perf. moves the constant float/int declaration to constant space so it doesnt get initialized per thread. This improved color correction performance on M4 Max 3-4 times better.

Signed-off-by: Morteza <smostajabodaveh@apple.com>

* Tiny refactoring to improve code maintainability

Signed-off-by: Morteza <smostajabodaveh@apple.com>

---------

Signed-off-by: Morteza <smostajabodaveh@apple.com>
  • Loading branch information
Morteeza authored Feb 10, 2025
1 parent c5c85b0 commit d807b38
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
91 changes: 49 additions & 42 deletions src/OpenColorIO/GpuShaderUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ void GpuShaderText::declareFloatArrayConst(const std::string & name, int size, c
}

auto nl = newLine();

auto emitArrayValues = [&]()
{
for (int i = 0; i < size; ++i)
{
nl << getFloatString(v[i], m_lang);
if (i + 1 != size)
{
nl << ", ";
}
}
};

switch (m_lang)
{
Expand All @@ -524,34 +536,30 @@ void GpuShaderText::declareFloatArrayConst(const std::string & name, int size, c
{
nl << floatKeywordConst() << " " << name << "[" << size << "] = ";
nl << floatKeyword() << "[" << size << "](";
for (int i = 0; i < size; ++i)
{
nl << getFloatString(v[i], m_lang);
if (i + 1 != size)
{
nl << ", ";
}
}
emitArrayValues();
nl << ");";
break;
}
case LANGUAGE_OSL_1:
case GPU_LANGUAGE_CG:
case GPU_LANGUAGE_HLSL_SM_5_0:
{
nl << floatKeywordConst();
nl << " " << name << "[" << size << "] = {";
emitArrayValues();
nl << "};";
break;
}

case GPU_LANGUAGE_MSL_2_0:
{
nl << floatKeywordConst() << " " << name << "[" << size << "] = {";
for (int i = 0; i < size; ++i)
{
nl << getFloatString(v[i], m_lang);
if (i + 1 != size)
{
nl << ", ";
}
}
nl << "constant constexpr static float";
nl << " " << name << "[" << size << "] = {";
emitArrayValues();
nl << "};";
break;
}

}
}

Expand All @@ -567,6 +575,18 @@ void GpuShaderText::declareIntArrayConst(const std::string & name, int size, con
}

auto nl = newLine();

auto emitArrayValues = [&]()
{
for (int i = 0; i < size; ++i)
{
nl << v[i];
if (i + 1 != size)
{
nl << ", ";
}
}
};

switch (m_lang)
{
Expand All @@ -578,44 +598,31 @@ void GpuShaderText::declareIntArrayConst(const std::string & name, int size, con
{
nl << intKeywordConst() << " " << name << "[" << size << "] = "
<< intKeyword() << "[" << size << "](";
for (int i = 0; i < size; ++i)
{
nl << v[i];
if (i + 1 != size)
{
nl << ", ";
}
}
emitArrayValues();
nl << ");";
break;
}
case GPU_LANGUAGE_HLSL_SM_5_0:
{
nl << intKeywordConst();
nl << " " << name << "[" << size << "] = {";
emitArrayValues();
nl << "};";
break;
}
case GPU_LANGUAGE_MSL_2_0:
{
nl << intKeywordConst() << " " << name << "[" << size << "] = {";
for (int i = 0; i < size; ++i)
{
nl << v[i];
if (i + 1 != size)
{
nl << ", ";
}
}
nl << "constant constexpr static int";
nl << " " << name << "[" << size << "] = {";
emitArrayValues();
nl << "};";
break;
}
case LANGUAGE_OSL_1:
case GPU_LANGUAGE_CG:
{
nl << intKeyword() << " " << name << "[" << size << "] = {";
for (int i = 0; i < size; ++i)
{
nl << v[i];
if (i + 1 != size)
{
nl << ", ";
}
}
emitArrayValues();
nl << "};";
break;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/cpu/GpuShader_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,10 +1032,10 @@ ocioOCIOMain(
// Declaration of all helper methods
const int ocio_grading_rgbcurve_knotsOffsets_0[8] = {0, 5, -1, 0, -1, 0, -1, 0};
const float ocio_grading_rgbcurve_knots_0[5] = {0., 0.333333343, 0.5, 0.666666508, 1.};
const int ocio_grading_rgbcurve_coefsOffsets_0[8] = {0, 12, -1, 0, -1, 0, -1, 0};
const float ocio_grading_rgbcurve_coefs_0[12] = {0.0982520878, 0.393008381, 0.347727984, 0.08693178, 0.934498608, 1., 1.13100278, 1.246912, 0., 0.322416425, 0.5, 0.698159397};
constant constexpr static int ocio_grading_rgbcurve_knotsOffsets_0[8] = {0, 5, -1, 0, -1, 0, -1, 0};
constant constexpr static float ocio_grading_rgbcurve_knots_0[5] = {0., 0.333333343, 0.5, 0.666666508, 1.};
constant constexpr static int ocio_grading_rgbcurve_coefsOffsets_0[8] = {0, 12, -1, 0, -1, 0, -1, 0};
constant constexpr static float ocio_grading_rgbcurve_coefs_0[12] = {0.0982520878, 0.393008381, 0.347727984, 0.08693178, 0.934498608, 1., 1.13100278, 1.246912, 0., 0.322416425, 0.5, 0.698159397};
float ocio_grading_rgbcurve_evalBSplineCurve_0(int curveIdx, float x)
{
Expand Down

0 comments on commit d807b38

Please sign in to comment.