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