1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/gpu/vk/GrVkVaryingHandler.h"
9 
10 /** Returns the number of locations take up by a given GrSLType. We assume that all
11     scalar values are 32 bits. */
grsltype_to_location_size(GrSLType type)12 static inline int grsltype_to_location_size(GrSLType type) {
13     switch(type) {
14         case kVoid_GrSLType:
15             return 0;
16         case kFloat_GrSLType: // fall through
17         case kHalf_GrSLType:
18             return 1;
19         case kFloat2_GrSLType: // fall through
20         case kHalf2_GrSLType:
21             return 1;
22         case kFloat3_GrSLType:
23         case kHalf3_GrSLType:
24             return 1;
25         case kFloat4_GrSLType:
26         case kHalf4_GrSLType:
27             return 1;
28         case kInt2_GrSLType:
29         case kUInt2_GrSLType:
30         case kShort2_GrSLType:
31         case kUShort2_GrSLType:
32             return 1;
33         case kInt3_GrSLType:
34         case kUInt3_GrSLType:
35         case kShort3_GrSLType:
36         case kUShort3_GrSLType:
37             return 1;
38         case kInt4_GrSLType:
39         case kUInt4_GrSLType:
40         case kShort4_GrSLType:
41         case kUShort4_GrSLType:
42             return 1;
43         case kFloat2x2_GrSLType:
44         case kHalf2x2_GrSLType:
45             return 2;
46         case kFloat3x3_GrSLType:
47         case kHalf3x3_GrSLType:
48             return 3;
49         case kFloat4x4_GrSLType:
50         case kHalf4x4_GrSLType:
51             return 4;
52         case kTexture2DSampler_GrSLType:
53         case kSampler_GrSLType:
54         case kTexture2D_GrSLType:
55         case kInput_GrSLType:
56             return 0;
57         case kTextureExternalSampler_GrSLType:
58              return 0;
59         case kTexture2DRectSampler_GrSLType:
60              return 0;
61         case kBool_GrSLType:
62         case kBool2_GrSLType:
63         case kBool3_GrSLType:
64         case kBool4_GrSLType:
65              return 1;
66         case kInt_GrSLType: // fall through
67         case kShort_GrSLType:
68              return 1;
69         case kUInt_GrSLType: // fall through
70         case kUShort_GrSLType:
71              return 1;
72     }
73     SK_ABORT("Unexpected type");
74 }
75 
finalize_helper(GrVkVaryingHandler::VarArray& vars)76 static void finalize_helper(GrVkVaryingHandler::VarArray& vars) {
77     int locationIndex = 0;
78     for (GrShaderVar& var : vars.items()) {
79         SkString location;
80         location.appendf("location = %d", locationIndex);
81         var.addLayoutQualifier(location.c_str());
82 
83         int elementSize = grsltype_to_location_size(var.getType());
84         SkASSERT(elementSize > 0);
85         int numElements = var.isArray() ? var.getArrayCount() : 1;
86         SkASSERT(numElements > 0);
87         locationIndex += elementSize * numElements;
88     }
89     // Vulkan requires at least 64 locations to be supported for both vertex output and fragment
90     // input. If we ever hit this assert, then we'll need to add a cap to actually check the
91     // supported input and output values and adjust our supported shaders based on those values.
92     SkASSERT(locationIndex <= 64);
93 }
94 
onFinalize()95 void GrVkVaryingHandler::onFinalize() {
96     finalize_helper(fVertexInputs);
97     finalize_helper(fVertexOutputs);
98     finalize_helper(fFragInputs);
99     finalize_helper(fFragOutputs);
100 }
101