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 "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)12static 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 kUint2_GrSLType: 29 return 1; 30 case kInt2_GrSLType: 31 case kShort2_GrSLType: 32 case kUShort2_GrSLType: 33 case kByte2_GrSLType: 34 case kUByte2_GrSLType: 35 return 1; 36 case kInt3_GrSLType: 37 case kShort3_GrSLType: 38 case kUShort3_GrSLType: 39 case kByte3_GrSLType: 40 case kUByte3_GrSLType: 41 return 1; 42 case kInt4_GrSLType: 43 case kShort4_GrSLType: 44 case kUShort4_GrSLType: 45 case kByte4_GrSLType: 46 case kUByte4_GrSLType: 47 return 1; 48 case kFloat2x2_GrSLType: 49 case kHalf2x2_GrSLType: 50 return 2; 51 case kFloat3x3_GrSLType: 52 case kHalf3x3_GrSLType: 53 return 3; 54 case kFloat4x4_GrSLType: 55 case kHalf4x4_GrSLType: 56 return 4; 57 case kTexture2DSampler_GrSLType: 58 return 0; 59 case kTextureExternalSampler_GrSLType: 60 return 0; 61 case kTexture2DRectSampler_GrSLType: 62 return 0; 63 case kBool_GrSLType: 64 return 1; 65 case kInt_GrSLType: // fall through 66 case kShort_GrSLType: 67 case kByte_GrSLType: 68 return 1; 69 case kUint_GrSLType: // fall through 70 case kUShort_GrSLType: 71 case kUByte_GrSLType: 72 return 1; 73 } 74 SK_ABORT("Unexpected type"); 75 return -1; 76 } 77 finalize_helper(GrVkVaryingHandler::VarArray & vars)78static void finalize_helper(GrVkVaryingHandler::VarArray& vars) { 79 int locationIndex = 0; 80 for (int i = 0; i < vars.count(); ++i) { 81 GrShaderVar& var = vars[i]; 82 SkString location; 83 location.appendf("location = %d", locationIndex); 84 var.addLayoutQualifier(location.c_str()); 85 86 int elementSize = grsltype_to_location_size(var.getType()); 87 SkASSERT(elementSize > 0); 88 int numElements = 1; 89 if (var.isArray() && !var.isUnsizedArray()) { 90 numElements = var.getArrayCount(); 91 } 92 SkASSERT(numElements > 0); 93 locationIndex += elementSize * numElements; 94 } 95 // Vulkan requires at least 64 locations to be supported for both vertex output and fragment 96 // input. If we ever hit this assert, then we'll need to add a cap to actually check the 97 // supported input and output values and adjust our supported shaders based on those values. 98 SkASSERT(locationIndex <= 64); 99 } 100 onFinalize()101void GrVkVaryingHandler::onFinalize() { 102 finalize_helper(fVertexInputs); 103 finalize_helper(fVertexOutputs); 104 finalize_helper(fGeomInputs); 105 finalize_helper(fGeomOutputs); 106 finalize_helper(fFragInputs); 107 finalize_helper(fFragOutputs); 108 } 109