Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 7 | |
| 8 | |
| 9 | #include "GrShaderVar.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 10 | #include "GrShaderCaps.h" |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 11 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 12 | static const char* type_modifier_string(GrShaderVar::TypeModifier t) { |
| 13 | switch (t) { |
| 14 | case GrShaderVar::kNone_TypeModifier: return ""; |
| 15 | case GrShaderVar::kIn_TypeModifier: return "in"; |
| 16 | case GrShaderVar::kInOut_TypeModifier: return "inout"; |
| 17 | case GrShaderVar::kOut_TypeModifier: return "out"; |
| 18 | case GrShaderVar::kUniform_TypeModifier: return "uniform"; |
| 19 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 20 | SK_ABORT("Unknown shader variable type modifier."); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 21 | return ""; |
| 22 | } |
| 23 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 24 | void GrShaderVar::setIOType(GrIOType ioType) { |
| 25 | switch (ioType) { |
| 26 | case kRW_GrIOType: |
| 27 | return; |
| 28 | case kRead_GrIOType: |
| 29 | this->addModifier("readonly"); |
| 30 | return; |
| 31 | case kWrite_GrIOType: |
| 32 | this->addModifier("writeonly"); |
| 33 | return; |
| 34 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 35 | SK_ABORT("Unknown io type."); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Brian Salomon | 23c55b6 | 2018-06-19 16:28:41 -0400 | [diff] [blame] | 38 | // Converts a GrSLPrecision to its corresponding GLSL precision qualifier. TODO: Remove this as we |
| 39 | // shouldn't need it with SkSL. |
| 40 | static inline const char* glsl_precision_string(GrSLPrecision p) { |
| 41 | switch (p) { |
| 42 | case kLow_GrSLPrecision: |
| 43 | return "lowp"; |
| 44 | case kMedium_GrSLPrecision: |
| 45 | return "mediump"; |
| 46 | case kHigh_GrSLPrecision: |
| 47 | return "highp"; |
| 48 | case kDefault_GrSLPrecision: |
| 49 | return ""; |
| 50 | } |
| 51 | SK_ABORT("Unexpected precision type."); |
| 52 | return ""; |
| 53 | } |
| 54 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 55 | void GrShaderVar::appendDecl(const GrShaderCaps* shaderCaps, SkString* out) const { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 56 | SkASSERT(kDefault_GrSLPrecision == fPrecision || GrSLTypeTemporarilyAcceptsPrecision(fType)); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 57 | SkString layout = fLayoutQualifier; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 58 | if (!fLayoutQualifier.isEmpty()) { |
| 59 | out->appendf("layout(%s) ", fLayoutQualifier.c_str()); |
| 60 | } |
| 61 | out->append(fExtraModifiers); |
| 62 | if (this->getTypeModifier() != kNone_TypeModifier) { |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 63 | out->append(type_modifier_string(this->getTypeModifier())); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 64 | out->append(" "); |
| 65 | } |
| 66 | GrSLType effectiveType = this->getType(); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 67 | if (shaderCaps->usesPrecisionModifiers() && GrSLTypeAcceptsPrecision(effectiveType)) { |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 68 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
Brian Salomon | 23c55b6 | 2018-06-19 16:28:41 -0400 | [diff] [blame] | 69 | out->appendf("%s ", glsl_precision_string(fPrecision)); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 70 | } |
| 71 | if (this->isArray()) { |
| 72 | if (this->isUnsizedArray()) { |
| 73 | out->appendf("%s %s[]", |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 74 | GrGLSLTypeString(shaderCaps, effectiveType), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 75 | this->getName().c_str()); |
| 76 | } else { |
| 77 | SkASSERT(this->getArrayCount() > 0); |
| 78 | out->appendf("%s %s[%d]", |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 79 | GrGLSLTypeString(shaderCaps, effectiveType), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 80 | this->getName().c_str(), |
| 81 | this->getArrayCount()); |
| 82 | } |
| 83 | } else { |
| 84 | out->appendf("%s %s", |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 85 | GrGLSLTypeString(shaderCaps, effectiveType), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 86 | this->getName().c_str()); |
| 87 | } |
| 88 | } |