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"
10 #include "GrShaderCaps.h"
11
type_modifier_string(GrShaderVar::TypeModifier t)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 }
20 SkFAIL("Unknown shader variable type modifier.");
21 return "";
22 }
23
setImageStorageFormat(GrImageStorageFormat format)24 void GrShaderVar::setImageStorageFormat(GrImageStorageFormat format) {
25 const char* formatStr = nullptr;
26 switch (format) {
27 case GrImageStorageFormat::kRGBA8:
28 formatStr = "rgba8";
29 break;
30 case GrImageStorageFormat::kRGBA8i:
31 formatStr = "rgba8i";
32 break;
33 case GrImageStorageFormat::kRGBA16f:
34 formatStr = "rgba16f";
35 break;
36 case GrImageStorageFormat::kRGBA32f:
37 formatStr = "rgba32f";
38 break;
39 }
40 this->addLayoutQualifier(formatStr);
41 SkASSERT(formatStr);
42 }
43
setMemoryModel(GrSLMemoryModel model)44 void GrShaderVar::setMemoryModel(GrSLMemoryModel model) {
45 switch (model) {
46 case GrSLMemoryModel::kNone:
47 return;
48 case GrSLMemoryModel::kCoherent:
49 this->addModifier("coherent");
50 return;
51 case GrSLMemoryModel::kVolatile:
52 this->addModifier("volatile");
53 return;
54 }
55 SkFAIL("Unknown memory model.");
56 }
57
setRestrict(GrSLRestrict restrict)58 void GrShaderVar::setRestrict(GrSLRestrict restrict) {
59 switch (restrict) {
60 case GrSLRestrict::kNo:
61 return;
62 case GrSLRestrict::kYes:
63 this->addModifier("restrict");
64 return;
65 }
66 SkFAIL("Unknown restrict.");
67 }
68
setIOType(GrIOType ioType)69 void GrShaderVar::setIOType(GrIOType ioType) {
70 switch (ioType) {
71 case kRW_GrIOType:
72 return;
73 case kRead_GrIOType:
74 this->addModifier("readonly");
75 return;
76 case kWrite_GrIOType:
77 this->addModifier("writeonly");
78 return;
79 }
80 SkFAIL("Unknown io type.");
81 }
82
appendDecl(const GrShaderCaps * shaderCaps,SkString * out) const83 void GrShaderVar::appendDecl(const GrShaderCaps* shaderCaps, SkString* out) const {
84 SkASSERT(kDefault_GrSLPrecision == fPrecision || GrSLTypeAcceptsPrecision(fType));
85 SkString layout = fLayoutQualifier;
86 if (!fLayoutQualifier.isEmpty()) {
87 out->appendf("layout(%s) ", fLayoutQualifier.c_str());
88 }
89 out->append(fExtraModifiers);
90 if (this->getTypeModifier() != kNone_TypeModifier) {
91 out->append(type_modifier_string(this->getTypeModifier()));
92 out->append(" ");
93 }
94 GrSLType effectiveType = this->getType();
95 if (shaderCaps->usesPrecisionModifiers() && GrSLTypeAcceptsPrecision(effectiveType)) {
96 // Desktop GLSL has added precision qualifiers but they don't do anything.
97 out->appendf("%s ", GrGLSLPrecisionString(fPrecision));
98 }
99 if (this->isArray()) {
100 if (this->isUnsizedArray()) {
101 out->appendf("%s %s[]",
102 GrGLSLTypeString(effectiveType),
103 this->getName().c_str());
104 } else {
105 SkASSERT(this->getArrayCount() > 0);
106 out->appendf("%s %s[%d]",
107 GrGLSLTypeString(effectiveType),
108 this->getName().c_str(),
109 this->getArrayCount());
110 }
111 } else {
112 out->appendf("%s %s",
113 GrGLSLTypeString(effectiveType),
114 this->getName().c_str());
115 }
116 }
117