blob: 5fe2f45373674abfc750720ba43ceef25347e7a1 [file] [log] [blame]
jvanverth992ad362016-02-26 09:21:02 -08001/*
egdanielfd016d72016-09-27 12:13:05 -07002 * 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 */
jvanverth992ad362016-02-26 09:21:02 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkVaryingHandler.h"
jvanverth992ad362016-02-26 09:21:02 -08009
egdaniel5b20c462016-09-27 09:09:44 -070010/** Returns the number of locations take up by a given GrSLType. We assume that all
11 scalar values are 32 bits. */
12static inline int grsltype_to_location_size(GrSLType type) {
Brian Salomonfa26e662016-11-14 11:27:00 -050013 switch(type) {
14 case kVoid_GrSLType:
15 return 0;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040016 case kFloat_GrSLType: // fall through
Ethan Nicholasf7b88202017-09-18 14:10:39 -040017 case kHalf_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050018 return 1;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040019 case kFloat2_GrSLType: // fall through
Ethan Nicholasf7b88202017-09-18 14:10:39 -040020 case kHalf2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050021 return 1;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040022 case kFloat3_GrSLType:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040023 case kHalf3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050024 return 1;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040025 case kFloat4_GrSLType:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040026 case kHalf4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050027 return 1;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040028 case kUint2_GrSLType:
Robert Phillips8296e752017-08-25 08:45:21 -040029 return 1;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040030 case kInt2_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -060031 case kShort2_GrSLType:
32 case kUShort2_GrSLType:
Ruiqi Maob609e6d2018-07-17 10:19:38 -040033 case kByte2_GrSLType:
34 case kUByte2_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050035 return 1;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040036 case kInt3_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -060037 case kShort3_GrSLType:
38 case kUShort3_GrSLType:
Ruiqi Maob609e6d2018-07-17 10:19:38 -040039 case kByte3_GrSLType:
40 case kUByte3_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050041 return 1;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040042 case kInt4_GrSLType:
Chris Dalton51ebd662017-10-24 16:10:48 -060043 case kShort4_GrSLType:
44 case kUShort4_GrSLType:
Ruiqi Maob609e6d2018-07-17 10:19:38 -040045 case kByte4_GrSLType:
46 case kUByte4_GrSLType:
csmartdaltonb37cb232017-02-08 14:56:27 -050047 return 1;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040048 case kFloat2x2_GrSLType:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040049 case kHalf2x2_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050050 return 2;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040051 case kFloat3x3_GrSLType:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040052 case kHalf3x3_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050053 return 3;
Ethan Nicholas8aa45692017-09-20 11:24:15 -040054 case kFloat4x4_GrSLType:
Ethan Nicholasf7b88202017-09-18 14:10:39 -040055 case kHalf4x4_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050056 return 4;
57 case kTexture2DSampler_GrSLType:
Stephen Whiteff5d7a22019-07-26 17:42:06 -040058 case kSampler_GrSLType:
59 case kTexture2D_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050060 return 0;
Brian Salomonfa26e662016-11-14 11:27:00 -050061 case kTextureExternalSampler_GrSLType:
62 return 0;
63 case kTexture2DRectSampler_GrSLType:
64 return 0;
Brian Salomonfa26e662016-11-14 11:27:00 -050065 case kBool_GrSLType:
66 return 1;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040067 case kInt_GrSLType: // fall through
68 case kShort_GrSLType:
Ruiqi Maob609e6d2018-07-17 10:19:38 -040069 case kByte_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050070 return 1;
Ruiqi Maob609e6d2018-07-17 10:19:38 -040071 case kUint_GrSLType: // fall through
72 case kUShort_GrSLType:
73 case kUByte_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050074 return 1;
Brian Salomonfa26e662016-11-14 11:27:00 -050075 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040076 SK_ABORT("Unexpected type");
egdaniel5b20c462016-09-27 09:09:44 -070077}
jvanverth992ad362016-02-26 09:21:02 -080078
Mike Kleindc735592018-10-11 14:26:30 -040079static void finalize_helper(GrVkVaryingHandler::VarArray& vars) {
egdaniel5b20c462016-09-27 09:09:44 -070080 int locationIndex = 0;
jvanverth992ad362016-02-26 09:21:02 -080081 for (int i = 0; i < vars.count(); ++i) {
Brian Salomon99938a82016-11-21 13:41:08 -050082 GrShaderVar& var = vars[i];
jvanverth992ad362016-02-26 09:21:02 -080083 SkString location;
egdaniel5b20c462016-09-27 09:09:44 -070084 location.appendf("location = %d", locationIndex);
Brian Salomon60397682016-11-22 15:06:46 -050085 var.addLayoutQualifier(location.c_str());
egdaniel5b20c462016-09-27 09:09:44 -070086
87 int elementSize = grsltype_to_location_size(var.getType());
Chris Dalton135e4462017-07-18 11:39:14 -060088 SkASSERT(elementSize > 0);
egdaniel5b20c462016-09-27 09:09:44 -070089 int numElements = 1;
Chris Dalton135e4462017-07-18 11:39:14 -060090 if (var.isArray() && !var.isUnsizedArray()) {
91 numElements = var.getArrayCount();
egdaniel5b20c462016-09-27 09:09:44 -070092 }
Chris Dalton135e4462017-07-18 11:39:14 -060093 SkASSERT(numElements > 0);
egdaniel5b20c462016-09-27 09:09:44 -070094 locationIndex += elementSize * numElements;
jvanverth992ad362016-02-26 09:21:02 -080095 }
egdaniel5b20c462016-09-27 09:09:44 -070096 // Vulkan requires at least 64 locations to be supported for both vertex output and fragment
97 // input. If we ever hit this assert, then we'll need to add a cap to actually check the
98 // supported input and output values and adjust our supported shaders based on those values.
99 SkASSERT(locationIndex <= 64);
jvanverth992ad362016-02-26 09:21:02 -0800100}
101
102void GrVkVaryingHandler::onFinalize() {
103 finalize_helper(fVertexInputs);
104 finalize_helper(fVertexOutputs);
105 finalize_helper(fGeomInputs);
106 finalize_helper(fGeomOutputs);
107 finalize_helper(fFragInputs);
108 finalize_helper(fFragOutputs);
109}