1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2018 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlVaryingHandler.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/private/GrMtlTypesPriv.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#if !__has_feature(objc_arc)
13cb93a386Sopenharmony_ci#error This file must be compiled with Arc. Use -fobjc-arc flag
14cb93a386Sopenharmony_ci#endif
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ciGR_NORETAIN_BEGIN
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_cistatic void finalize_helper(GrMtlVaryingHandler::VarArray& vars) {
19cb93a386Sopenharmony_ci    int locationIndex = 0;
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci    SkDEBUGCODE(int componentCount = 0);
22cb93a386Sopenharmony_ci    for (GrShaderVar& var : vars.items()) {
23cb93a386Sopenharmony_ci        // Metal only allows scalars (including bool and char) and vectors as varyings
24cb93a386Sopenharmony_ci        SkASSERT(GrSLTypeVecLength(var.getType()) != -1);
25cb93a386Sopenharmony_ci        SkDEBUGCODE(componentCount += GrSLTypeVecLength(var.getType()));
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ci        SkString location;
28cb93a386Sopenharmony_ci        location.appendf("location = %d", locationIndex);
29cb93a386Sopenharmony_ci        var.addLayoutQualifier(location.c_str());
30cb93a386Sopenharmony_ci        ++locationIndex;
31cb93a386Sopenharmony_ci    }
32cb93a386Sopenharmony_ci    // The max number of inputs is 60 for iOS and 32 for macOS. The max number of components is 60
33cb93a386Sopenharmony_ci    // for iOS and 128 for macOS. To be conservative, we are going to assert that we have less than
34cb93a386Sopenharmony_ci    // 32 varyings and less than 60 components across all varyings. If we hit this assert, we can
35cb93a386Sopenharmony_ci    // implement a function in GrMtlCaps to be less conservative.
36cb93a386Sopenharmony_ci    SkASSERT(locationIndex <= 32);
37cb93a386Sopenharmony_ci    SkASSERT(componentCount <= 60);
38cb93a386Sopenharmony_ci}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_civoid GrMtlVaryingHandler::onFinalize() {
41cb93a386Sopenharmony_ci    finalize_helper(fVertexInputs);
42cb93a386Sopenharmony_ci    finalize_helper(fVertexOutputs);
43cb93a386Sopenharmony_ci    finalize_helper(fFragInputs);
44cb93a386Sopenharmony_ci    finalize_helper(fFragOutputs);
45cb93a386Sopenharmony_ci}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ciGR_NORETAIN_END
48