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)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 case kSampler_GrSLType: 59 case kTexture2D_GrSLType: 60 case kInput_GrSLType: 61 return 0; 62 case kTextureExternalSampler_GrSLType: 63 return 0; 64 case kTexture2DRectSampler_GrSLType: 65 return 0; 66 case kBool_GrSLType: 67 return 1; 68 case kInt_GrSLType: // fall through 69 case kShort_GrSLType: 70 case kByte_GrSLType: 71 return 1; 72 case kUint_GrSLType: // fall through 73 case kUShort_GrSLType: 74 case kUByte_GrSLType: 75 return 1; 76 } 77 SK_ABORT("Unexpected type"); 78 } 79 finalize_helper(GrVkVaryingHandler::VarArray & vars)80static void finalize_helper(GrVkVaryingHandler::VarArray& vars) { 81 int locationIndex = 0; 82 for (GrShaderVar& var : vars.items()) { 83 SkString location; 84 location.appendf("location = %d", locationIndex); 85 var.addLayoutQualifier(location.c_str()); 86 87 int elementSize = grsltype_to_location_size(var.getType()); 88 SkASSERT(elementSize > 0); 89 int numElements = 1; 90 if (var.isArray() && !var.isUnsizedArray()) { 91 numElements = var.getArrayCount(); 92 } 93 SkASSERT(numElements > 0); 94 locationIndex += elementSize * numElements; 95 } 96 // Vulkan requires at least 64 locations to be supported for both vertex output and fragment 97 // input. If we ever hit this assert, then we'll need to add a cap to actually check the 98 // supported input and output values and adjust our supported shaders based on those values. 99 SkASSERT(locationIndex <= 64); 100 } 101 onFinalize()102void GrVkVaryingHandler::onFinalize() { 103 finalize_helper(fVertexInputs); 104 finalize_helper(fVertexOutputs); 105 finalize_helper(fGeomInputs); 106 finalize_helper(fGeomOutputs); 107 finalize_helper(fFragInputs); 108 finalize_helper(fFragOutputs); 109 } 110