Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Greg Daniel | d59a91d | 2020-04-23 13:22:47 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrSPIRVVaryingHandler.h" |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 9 | |
| 10 | /** Returns the number of locations take up by a given GrSLType. We assume that all |
| 11 | scalar values are 32 bits. */ |
| 12 | static inline int grsltype_to_location_size(GrSLType type) { |
John Stiles | 9fc9b87 | 2021-02-01 15:50:32 -0500 | [diff] [blame] | 13 | // If a new GrSL type is added, this function will need to be updated. |
| 14 | static_assert(kGrSLTypeCount == 49); |
| 15 | |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 16 | switch(type) { |
| 17 | case kVoid_GrSLType: |
| 18 | return 0; |
| 19 | case kFloat_GrSLType: // fall through |
| 20 | case kHalf_GrSLType: |
| 21 | return 1; |
| 22 | case kFloat2_GrSLType: // fall through |
| 23 | case kHalf2_GrSLType: |
| 24 | return 1; |
| 25 | case kFloat3_GrSLType: |
| 26 | case kHalf3_GrSLType: |
| 27 | return 1; |
| 28 | case kFloat4_GrSLType: |
| 29 | case kHalf4_GrSLType: |
| 30 | return 1; |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 31 | case kInt2_GrSLType: |
John Stiles | 148224e | 2021-01-19 17:35:33 -0500 | [diff] [blame] | 32 | case kUint2_GrSLType: |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 33 | case kShort2_GrSLType: |
| 34 | case kUShort2_GrSLType: |
| 35 | case kByte2_GrSLType: |
| 36 | case kUByte2_GrSLType: |
| 37 | return 1; |
| 38 | case kInt3_GrSLType: |
John Stiles | 148224e | 2021-01-19 17:35:33 -0500 | [diff] [blame] | 39 | case kUint3_GrSLType: |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 40 | case kShort3_GrSLType: |
| 41 | case kUShort3_GrSLType: |
| 42 | case kByte3_GrSLType: |
| 43 | case kUByte3_GrSLType: |
| 44 | return 1; |
| 45 | case kInt4_GrSLType: |
John Stiles | 148224e | 2021-01-19 17:35:33 -0500 | [diff] [blame] | 46 | case kUint4_GrSLType: |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 47 | case kShort4_GrSLType: |
| 48 | case kUShort4_GrSLType: |
| 49 | case kByte4_GrSLType: |
| 50 | case kUByte4_GrSLType: |
| 51 | return 1; |
| 52 | case kFloat2x2_GrSLType: |
| 53 | case kHalf2x2_GrSLType: |
| 54 | return 2; |
| 55 | case kFloat3x3_GrSLType: |
| 56 | case kHalf3x3_GrSLType: |
| 57 | return 3; |
| 58 | case kFloat4x4_GrSLType: |
| 59 | case kHalf4x4_GrSLType: |
| 60 | return 4; |
| 61 | case kTexture2DSampler_GrSLType: |
| 62 | return 0; |
| 63 | case kTextureExternalSampler_GrSLType: |
| 64 | return 0; |
| 65 | case kTexture2DRectSampler_GrSLType: |
| 66 | return 0; |
| 67 | case kBool_GrSLType: |
John Stiles | 9fc9b87 | 2021-02-01 15:50:32 -0500 | [diff] [blame] | 68 | case kBool2_GrSLType: |
| 69 | case kBool3_GrSLType: |
| 70 | case kBool4_GrSLType: |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 71 | return 1; |
| 72 | case kInt_GrSLType: // fall through |
| 73 | case kShort_GrSLType: |
| 74 | case kByte_GrSLType: |
| 75 | return 1; |
| 76 | case kUint_GrSLType: // fall through |
| 77 | case kUShort_GrSLType: |
| 78 | case kUByte_GrSLType: |
| 79 | return 1; |
| 80 | case kTexture2D_GrSLType: |
| 81 | return 0; |
| 82 | case kSampler_GrSLType: |
| 83 | return 0; |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 84 | case kInput_GrSLType: |
| 85 | return 0; |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 86 | } |
| 87 | SK_ABORT("Unexpected type"); |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Greg Daniel | d59a91d | 2020-04-23 13:22:47 -0400 | [diff] [blame] | 90 | static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) { |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 91 | int locationIndex = 0; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 92 | for (GrShaderVar& var : vars.items()) { |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 93 | SkString location; |
| 94 | location.appendf("location = %d", locationIndex); |
| 95 | var.addLayoutQualifier(location.c_str()); |
| 96 | |
| 97 | int elementSize = grsltype_to_location_size(var.getType()); |
| 98 | SkASSERT(elementSize > 0); |
| 99 | int numElements = 1; |
| 100 | if (var.isArray() && !var.isUnsizedArray()) { |
| 101 | numElements = var.getArrayCount(); |
| 102 | } |
| 103 | SkASSERT(numElements > 0); |
| 104 | locationIndex += elementSize * numElements; |
| 105 | } |
Greg Daniel | d59a91d | 2020-04-23 13:22:47 -0400 | [diff] [blame] | 106 | // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here. |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Greg Daniel | d59a91d | 2020-04-23 13:22:47 -0400 | [diff] [blame] | 109 | void GrSPIRVVaryingHandler::onFinalize() { |
Stephen White | 83d8a94 | 2019-07-31 15:03:34 -0400 | [diff] [blame] | 110 | finalize_helper(fVertexInputs); |
| 111 | finalize_helper(fVertexOutputs); |
| 112 | finalize_helper(fGeomInputs); |
| 113 | finalize_helper(fGeomOutputs); |
| 114 | finalize_helper(fFragInputs); |
| 115 | finalize_helper(fFragOutputs); |
| 116 | } |