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