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; |
| 16 | case kFloat_GrSLType: |
| 17 | return 1; |
| 18 | case kVec2f_GrSLType: |
| 19 | return 1; |
| 20 | case kVec3f_GrSLType: |
| 21 | return 1; |
| 22 | case kVec4f_GrSLType: |
| 23 | return 1; |
| 24 | case kMat22f_GrSLType: |
| 25 | return 2; |
| 26 | case kMat33f_GrSLType: |
| 27 | return 3; |
| 28 | case kMat44f_GrSLType: |
| 29 | return 4; |
| 30 | case kTexture2DSampler_GrSLType: |
| 31 | return 0; |
| 32 | case kTexture2DISampler_GrSLType: |
| 33 | return 0; |
| 34 | case kTextureExternalSampler_GrSLType: |
| 35 | return 0; |
| 36 | case kTexture2DRectSampler_GrSLType: |
| 37 | return 0; |
| 38 | case kTextureBufferSampler_GrSLType: |
| 39 | return 0; |
| 40 | case kBool_GrSLType: |
| 41 | return 1; |
| 42 | case kInt_GrSLType: |
| 43 | return 1; |
| 44 | case kUint_GrSLType: |
| 45 | return 1; |
| 46 | case kTexture2D_GrSLType: |
| 47 | return 0; |
| 48 | case kSampler_GrSLType: |
| 49 | return 0; |
| 50 | } |
| 51 | SkFAIL("Unexpected type"); |
| 52 | return -1; |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 53 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 54 | |
| 55 | void finalize_helper(GrVkVaryingHandler::VarArray& vars) { |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 56 | int locationIndex = 0; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 57 | for (int i = 0; i < vars.count(); ++i) { |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 58 | GrGLSLShaderVar& var = vars[i]; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 59 | SkString location; |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 60 | location.appendf("location = %d", locationIndex); |
| 61 | var.setLayoutQualifier(location.c_str()); |
| 62 | |
| 63 | int elementSize = grsltype_to_location_size(var.getType()); |
| 64 | SkASSERT(elementSize); |
| 65 | int numElements = 1; |
| 66 | if (var.isArray()) { |
| 67 | numElements = var.getArrayCount(); |
| 68 | } |
| 69 | locationIndex += elementSize * numElements; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 70 | } |
egdaniel | 5b20c46 | 2016-09-27 09:09:44 -0700 | [diff] [blame] | 71 | // Vulkan requires at least 64 locations to be supported for both vertex output and fragment |
| 72 | // input. If we ever hit this assert, then we'll need to add a cap to actually check the |
| 73 | // supported input and output values and adjust our supported shaders based on those values. |
| 74 | SkASSERT(locationIndex <= 64); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void GrVkVaryingHandler::onFinalize() { |
| 78 | finalize_helper(fVertexInputs); |
| 79 | finalize_helper(fVertexOutputs); |
| 80 | finalize_helper(fGeomInputs); |
| 81 | finalize_helper(fGeomOutputs); |
| 82 | finalize_helper(fFragInputs); |
| 83 | finalize_helper(fFragOutputs); |
| 84 | } |