Repository: hyprwm/Hyprland
File: src/render/OpenGL.cpp (C++)

Code before:
    glUniform2f(shader.srcTFRange, imageDescription.getTFMinLuminance(), imageDescription.getTFMaxLuminance());
    glUniform2f(shader.dstTFRange, targetImageDescription.getTFMinLuminance(), targetImageDescription.getTFMaxLuminance());

    const float maxLuminance = imageDescription.luminances.max > 0 ? imageDescription.luminances.max : imageDescription.luminances.reference;
    glUniform1f(shader.maxLuminance, maxLuminance * targetImageDescription.luminances.reference / imageDescription.luminances.reference);
    glUniform1f(shader.dstMaxLuminance, targetImageDescription.luminances.max > 0 ? targetImageDescription.luminances.max : 10000);
    glUniform1f(shader.dstRefLuminance, targetImageDescription.luminances.reference);
    glUniform1f(shader.sdrSaturation,
                modifySDR && m_RenderData.pMonitor->sdrSaturation > 0 && targetImageDescription.transferFunction == NColorManagement::CM_TRANSFER_FUNCTION_ST2084_PQ ?
                    m_RenderData.pMonitor->sdrSaturation :
                    1.0f);
    glUniform1f(shader.sdrBrightness,
                modifySDR && m_RenderData.pMonitor->sdrBrightness > 0 && targetImageDescription.transferFunction == NColorManagement::CM_TRANSFER_FUNCTION_ST2084_PQ ?
                    m_RenderData.pMonitor->sdrBrightness :

                    1.0f);
    const auto cacheKey = std::make_pair(imageDescription.getId(), targetImageDescription.getId());
    if (!primariesConversionCache.contains(cacheKey)) {
        const auto                   mat             = imageDescription.getPrimaries().convertMatrix(targetImageDescription.getPrimaries()).mat();
        const std::array<GLfloat, 9> glConvertMatrix = {
            mat[0][0], mat[1][0], mat[2][0], //
            mat[0][1], mat[1][1], mat[2][1], //
            mat[0][2], mat[1][2], mat[2][2], //
        };
        primariesConversionCache.insert(std::make_pair(cacheKey, glConvertMatrix));
    }
    glUniformMatrix3fv(shader.convertMatrix, 1, false, &primariesConversionCache[cacheKey][0]);
}

void CHyprOpenGLImpl::passCMUniforms(const CShader& shader, const SImageDescription& imageDescription) {
    passCMUniforms(shader, imageDescription, m_RenderData.pMonitor->imageDescription, true);
}

void CHyprOpenGLImpl::renderTextureInternalWithDamage(SP<CTexture> tex, const CBox& box, float alpha, const CRegion& damage, int round, float roundingPower, bool discardActive,
                                                      bool noAA, bool allowCustomUV, bool allowDim) {
    RASSERT(m_RenderData.pMonitor, "Tried to render texture without begin()!");
    RASSERT((tex->m_iTexID > 0), "Attempted to draw nullptr texture!");

    TRACY_GPU_ZONE("RenderTextureInternalWithDamage");

    alpha = std::clamp(alpha, 0.f, 1.f);

    if (damage.empty())
        return;

    CBox newBox = box;
    m_RenderData.renderModif.applyToBox(newBox);

    static const auto PDT       = CConfigValue<Hyprlang::INT>("debug:damage_tracking");
    static const auto PPASS     = CConfigValue<Hyprlang::INT>("render:cm_fs_passthrough");
    static const auto PENABLECM = CConfigValue<Hyprlang::INT>("render:cm_enabled");

Review comment (performance):
nitpick: is there a reason you're doing `insert` and `std::make_pair` instead of just `primariesConversionCache[cacheKey] = glConvertMatrix;`?

Code after:

    glUniform2f(shader.srcTFRange, imageDescription.getTFMinLuminance(), imageDescription.getTFMaxLuminance());
    glUniform2f(shader.dstTFRange, targetImageDescription.getTFMinLuminance(), targetImageDescription.getTFMaxLuminance());

    const float maxLuminance = imageDescription.luminances.max > 0 ? imageDescription.luminances.max : imageDescription.luminances.reference;
    glUniform1f(shader.maxLuminance, maxLuminance * targetImageDescription.luminances.reference / imageDescription.luminances.reference);
    glUniform1f(shader.dstMaxLuminance, targetImageDescription.luminances.max > 0 ? targetImageDescription.luminances.max : 10000);
    glUniform1f(shader.dstRefLuminance, targetImageDescription.luminances.reference);
    glUniform1f(shader.sdrSaturation,
                modifySDR && m_RenderData.pMonitor->sdrSaturation > 0 && targetImageDescription.transferFunction == NColorManagement::CM_TRANSFER_FUNCTION_ST2084_PQ ?
                    m_RenderData.pMonitor->sdrSaturation :
                    1.0f);
    glUniform1f(shader.sdrBrightness,
                modifySDR && m_RenderData.pMonitor->sdrBrightness > 0 && targetImageDescription.transferFunction == NColorManagement::CM_TRANSFER_FUNCTION_ST2084_PQ ?
                    m_RenderData.pMonitor->sdrBrightness :

                    1.0f);
    const auto cacheKey = std::make_pair(imageDescription.getId(), targetImageDescription.getId());
    if (!primariesConversionCache.contains(cacheKey)) {
        const auto                   mat             = imageDescription.getPrimaries().convertMatrix(targetImageDescription.getPrimaries()).mat();
        const std::array<GLfloat, 9> glConvertMatrix = {
            mat[0][0], mat[1][0], mat[2][0], //
            mat[0][1], mat[1][1], mat[2][1], //
            mat[0][2], mat[1][2], mat[2][2], //
        };
        primariesConversionCache.insert(std::make_pair(cacheKey, glConvertMatrix));
    }
    glUniformMatrix3fv(shader.convertMatrix, 1, false, &primariesConversionCache[cacheKey][0]);
}

void CHyprOpenGLImpl::passCMUniforms(const CShader& shader, const SImageDescription& imageDescription) {
    passCMUniforms(shader, imageDescription, m_RenderData.pMonitor->imageDescription, true);
}

void CHyprOpenGLImpl::renderTextureInternalWithDamage(SP<CTexture> tex, const CBox& box, float alpha, const CRegion& damage, int round, float roundingPower, bool discardActive,
                                                      bool noAA, bool allowCustomUV, bool allowDim) {
    RASSERT(m_RenderData.pMonitor, "Tried to render texture without begin()!");
    RASSERT((tex->m_iTexID > 0), "Attempted to draw nullptr texture!");

    TRACY_GPU_ZONE("RenderTextureInternalWithDamage");

    alpha = std::clamp(alpha, 0.f, 1.f);

    if (damage.empty())
        return;

    CBox newBox = box;
    m_RenderData.renderModif.applyToBox(newBox);

    static const auto PDT       = CConfigValue<Hyprlang::INT>("debug:damage_tracking");
    static const auto PPASS     = CConfigValue<Hyprlang::INT>("render:cm_fs_passthrough");