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