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 SkSLType. We assume that all 11 scalar values are 32 bits. */ sksltype_to_location_size(SkSLType type)12static inline int sksltype_to_location_size(SkSLType type) { 13 switch(type) { 14 case SkSLType::kVoid: 15 return 0; 16 case SkSLType::kFloat: // fall through 17 case SkSLType::kHalf: 18 return 1; 19 case SkSLType::kFloat2: // fall through 20 case SkSLType::kHalf2: 21 return 1; 22 case SkSLType::kFloat3: 23 case SkSLType::kHalf3: 24 return 1; 25 case SkSLType::kFloat4: 26 case SkSLType::kHalf4: 27 return 1; 28 case SkSLType::kInt2: 29 case SkSLType::kUInt2: 30 case SkSLType::kShort2: 31 case SkSLType::kUShort2: 32 return 1; 33 case SkSLType::kInt3: 34 case SkSLType::kUInt3: 35 case SkSLType::kShort3: 36 case SkSLType::kUShort3: 37 return 1; 38 case SkSLType::kInt4: 39 case SkSLType::kUInt4: 40 case SkSLType::kShort4: 41 case SkSLType::kUShort4: 42 return 1; 43 case SkSLType::kFloat2x2: 44 case SkSLType::kHalf2x2: 45 return 2; 46 case SkSLType::kFloat3x3: 47 case SkSLType::kHalf3x3: 48 return 3; 49 case SkSLType::kFloat4x4: 50 case SkSLType::kHalf4x4: 51 return 4; 52 case SkSLType::kTexture2DSampler: 53 case SkSLType::kSampler: 54 case SkSLType::kTexture2D: 55 case SkSLType::kInput: 56 return 0; 57 case SkSLType::kTextureExternalSampler: 58 return 0; 59 case SkSLType::kTexture2DRectSampler: 60 return 0; 61 case SkSLType::kBool: 62 case SkSLType::kBool2: 63 case SkSLType::kBool3: 64 case SkSLType::kBool4: 65 return 1; 66 case SkSLType::kInt: // fall through 67 case SkSLType::kShort: 68 return 1; 69 case SkSLType::kUInt: // fall through 70 case SkSLType::kUShort: 71 return 1; 72 } 73 SK_ABORT("Unexpected type"); 74 } 75 finalize_helper(GrVkVaryingHandler::VarArray & vars)76static 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 = sksltype_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()95void GrVkVaryingHandler::onFinalize() { 96 finalize_helper(fVertexInputs); 97 finalize_helper(fVertexOutputs); 98 finalize_helper(fFragInputs); 99 finalize_helper(fFragOutputs); 100 } 101