blob: 8e43b76341176d5f5e907dcfd4f15dc57c179b62 [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
8#include "GrVkVaryingHandler.h"
9
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;
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;
csmartdaltonb37cb232017-02-08 14:56:27 -050024 case kVec2i_GrSLType:
25 return 1;
26 case kVec3i_GrSLType:
27 return 1;
28 case kVec4i_GrSLType:
29 return 1;
Brian Salomonfa26e662016-11-14 11:27:00 -050030 case kMat22f_GrSLType:
31 return 2;
32 case kMat33f_GrSLType:
33 return 3;
34 case kMat44f_GrSLType:
35 return 4;
36 case kTexture2DSampler_GrSLType:
37 return 0;
Brian Salomona8f00022016-11-16 12:55:57 -050038 case kITexture2DSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050039 return 0;
40 case kTextureExternalSampler_GrSLType:
41 return 0;
42 case kTexture2DRectSampler_GrSLType:
43 return 0;
csmartdalton22458032016-11-16 11:28:16 -070044 case kBufferSampler_GrSLType:
Brian Salomonfa26e662016-11-14 11:27:00 -050045 return 0;
46 case kBool_GrSLType:
47 return 1;
48 case kInt_GrSLType:
49 return 1;
50 case kUint_GrSLType:
51 return 1;
52 case kTexture2D_GrSLType:
53 return 0;
54 case kSampler_GrSLType:
55 return 0;
Brian Salomonf9f45122016-11-29 11:59:17 -050056 case kImageStorage2D_GrSLType:
57 return 0;
58 case kIImageStorage2D_GrSLType:
59 return 0;
Brian Salomonfa26e662016-11-14 11:27:00 -050060 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040061 SK_ABORT("Unexpected type");
Brian Salomonfa26e662016-11-14 11:27:00 -050062 return -1;
egdaniel5b20c462016-09-27 09:09:44 -070063}
jvanverth992ad362016-02-26 09:21:02 -080064
65void finalize_helper(GrVkVaryingHandler::VarArray& vars) {
egdaniel5b20c462016-09-27 09:09:44 -070066 int locationIndex = 0;
jvanverth992ad362016-02-26 09:21:02 -080067 for (int i = 0; i < vars.count(); ++i) {
Brian Salomon99938a82016-11-21 13:41:08 -050068 GrShaderVar& var = vars[i];
jvanverth992ad362016-02-26 09:21:02 -080069 SkString location;
egdaniel5b20c462016-09-27 09:09:44 -070070 location.appendf("location = %d", locationIndex);
Brian Salomon60397682016-11-22 15:06:46 -050071 var.addLayoutQualifier(location.c_str());
egdaniel5b20c462016-09-27 09:09:44 -070072
73 int elementSize = grsltype_to_location_size(var.getType());
Chris Dalton135e4462017-07-18 11:39:14 -060074 SkASSERT(elementSize > 0);
egdaniel5b20c462016-09-27 09:09:44 -070075 int numElements = 1;
Chris Dalton135e4462017-07-18 11:39:14 -060076 if (var.isArray() && !var.isUnsizedArray()) {
77 numElements = var.getArrayCount();
egdaniel5b20c462016-09-27 09:09:44 -070078 }
Chris Dalton135e4462017-07-18 11:39:14 -060079 SkASSERT(numElements > 0);
egdaniel5b20c462016-09-27 09:09:44 -070080 locationIndex += elementSize * numElements;
jvanverth992ad362016-02-26 09:21:02 -080081 }
egdaniel5b20c462016-09-27 09:09:44 -070082 // Vulkan requires at least 64 locations to be supported for both vertex output and fragment
83 // input. If we ever hit this assert, then we'll need to add a cap to actually check the
84 // supported input and output values and adjust our supported shaders based on those values.
85 SkASSERT(locationIndex <= 64);
jvanverth992ad362016-02-26 09:21:02 -080086}
87
88void GrVkVaryingHandler::onFinalize() {
89 finalize_helper(fVertexInputs);
90 finalize_helper(fVertexOutputs);
91 finalize_helper(fGeomInputs);
92 finalize_helper(fGeomOutputs);
93 finalize_helper(fFragInputs);
94 finalize_helper(fFragOutputs);
95}