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