jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 1 | /* |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 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 | */ |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 7 | |
| 8 | #include "GrVkVaryingHandler.h" |
| 9 | |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 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) { |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 13 | switch(type) { |
| 14 | case kVoid_GrSLType: |
| 15 | return 0; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 16 | case kFloat_GrSLType: // fall through |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 17 | case kHalf_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 18 | return 1; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 19 | case kFloat2_GrSLType: // fall through |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 20 | case kHalf2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 21 | return 1; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 22 | case kFloat3_GrSLType: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 23 | case kHalf3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 24 | return 1; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 25 | case kFloat4_GrSLType: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 26 | case kHalf4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 27 | return 1; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 28 | case kUint2_GrSLType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 29 | return 1; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 30 | case kInt2_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 31 | case kShort2_GrSLType: |
| 32 | case kUShort2_GrSLType: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 33 | case kByte2_GrSLType: |
| 34 | case kUByte2_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 35 | return 1; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 36 | case kInt3_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 37 | case kShort3_GrSLType: |
| 38 | case kUShort3_GrSLType: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 39 | case kByte3_GrSLType: |
| 40 | case kUByte3_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 41 | return 1; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 42 | case kInt4_GrSLType: |
Chris Dalton | 51ebd66 | 2017-10-24 16:10:48 -0600 | [diff] [blame] | 43 | case kShort4_GrSLType: |
| 44 | case kUShort4_GrSLType: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 45 | case kByte4_GrSLType: |
| 46 | case kUByte4_GrSLType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 47 | return 1; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 48 | case kFloat2x2_GrSLType: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 49 | case kHalf2x2_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 50 | return 2; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 51 | case kFloat3x3_GrSLType: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 52 | case kHalf3x3_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 53 | return 3; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 54 | case kFloat4x4_GrSLType: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 55 | case kHalf4x4_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 56 | return 4; |
| 57 | case kTexture2DSampler_GrSLType: |
| 58 | return 0; |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 59 | case kTextureExternalSampler_GrSLType: |
| 60 | return 0; |
| 61 | case kTexture2DRectSampler_GrSLType: |
| 62 | return 0; |
csmartdalton | 2245803 | 2016-11-16 11:28:16 -0700 | [diff] [blame] | 63 | case kBufferSampler_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 64 | return 0; |
| 65 | case kBool_GrSLType: |
| 66 | return 1; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 67 | case kInt_GrSLType: // fall through |
| 68 | case kShort_GrSLType: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 69 | case kByte_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 70 | return 1; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 71 | case kUint_GrSLType: // fall through |
| 72 | case kUShort_GrSLType: |
| 73 | case kUByte_GrSLType: |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 74 | return 1; |
| 75 | case kTexture2D_GrSLType: |
| 76 | return 0; |
| 77 | case kSampler_GrSLType: |
| 78 | return 0; |
| 79 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 80 | SK_ABORT("Unexpected type"); |
Brian Salomon | fa26e66 | 2016-11-14 11:27:00 -0500 | [diff] [blame] | 81 | return -1; |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 82 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 83 | |
| 84 | void finalize_helper(GrVkVaryingHandler::VarArray& vars) { |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 85 | int locationIndex = 0; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 86 | for (int i = 0; i < vars.count(); ++i) { |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 87 | GrShaderVar& var = vars[i]; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 88 | SkString location; |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 89 | location.appendf("location = %d", locationIndex); |
Brian Salomon | 6039768 | 2016-11-22 15:06:46 -0500 | [diff] [blame] | 90 | var.addLayoutQualifier(location.c_str()); |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 91 | |
| 92 | int elementSize = grsltype_to_location_size(var.getType()); |
Chris Dalton | 135e446 | 2017-07-18 11:39:14 -0600 | [diff] [blame] | 93 | SkASSERT(elementSize > 0); |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 94 | int numElements = 1; |
Chris Dalton | 135e446 | 2017-07-18 11:39:14 -0600 | [diff] [blame] | 95 | if (var.isArray() && !var.isUnsizedArray()) { |
| 96 | numElements = var.getArrayCount(); |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 97 | } |
Chris Dalton | 135e446 | 2017-07-18 11:39:14 -0600 | [diff] [blame] | 98 | SkASSERT(numElements > 0); |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 99 | locationIndex += elementSize * numElements; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 100 | } |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 101 | // Vulkan requires at least 64 locations to be supported for both vertex output and fragment |
| 102 | // input. If we ever hit this assert, then we'll need to add a cap to actually check the |
| 103 | // supported input and output values and adjust our supported shaders based on those values. |
| 104 | SkASSERT(locationIndex <= 64); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void GrVkVaryingHandler::onFinalize() { |
| 108 | finalize_helper(fVertexInputs); |
| 109 | finalize_helper(fVertexOutputs); |
| 110 | finalize_helper(fGeomInputs); |
| 111 | finalize_helper(fGeomOutputs); |
| 112 | finalize_helper(fFragInputs); |
| 113 | finalize_helper(fFragOutputs); |
| 114 | } |