blob: 4d6c6581a13d3ed73af419cacdffd9cf0245332c [file] [log] [blame]
Brian Salomon99938a82016-11-21 13:41:08 -05001/*
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 Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
Brian Salomon99938a82016-11-21 13:41:08 -050011
Brian Salomonf9f45122016-11-29 11:59:17 -050012static 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 Wagnerb4aab9a2017-08-16 10:53:04 -040020 SK_ABORT("Unknown shader variable type modifier.");
Brian Salomonf9f45122016-11-29 11:59:17 -050021 return "";
22}
23
Brian Salomonf9f45122016-11-29 11:59:17 -050024void 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 Wagnerb4aab9a2017-08-16 10:53:04 -040035 SK_ABORT("Unknown io type.");
Brian Salomonf9f45122016-11-29 11:59:17 -050036}
37
Brian Salomon1edc5b92016-11-29 13:43:46 -050038void GrShaderVar::appendDecl(const GrShaderCaps* shaderCaps, SkString* out) const {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040039 SkASSERT(kDefault_GrSLPrecision == fPrecision || GrSLTypeTemporarilyAcceptsPrecision(fType));
Brian Salomonf9f45122016-11-29 11:59:17 -050040 SkString layout = fLayoutQualifier;
Brian Salomon99938a82016-11-21 13:41:08 -050041 if (!fLayoutQualifier.isEmpty()) {
42 out->appendf("layout(%s) ", fLayoutQualifier.c_str());
43 }
44 out->append(fExtraModifiers);
45 if (this->getTypeModifier() != kNone_TypeModifier) {
Brian Salomonf9f45122016-11-29 11:59:17 -050046 out->append(type_modifier_string(this->getTypeModifier()));
Brian Salomon99938a82016-11-21 13:41:08 -050047 out->append(" ");
48 }
49 GrSLType effectiveType = this->getType();
Brian Salomon1edc5b92016-11-29 13:43:46 -050050 if (shaderCaps->usesPrecisionModifiers() && GrSLTypeAcceptsPrecision(effectiveType)) {
Brian Salomon99938a82016-11-21 13:41:08 -050051 // Desktop GLSL has added precision qualifiers but they don't do anything.
52 out->appendf("%s ", GrGLSLPrecisionString(fPrecision));
53 }
54 if (this->isArray()) {
55 if (this->isUnsizedArray()) {
56 out->appendf("%s %s[]",
Robert Phillips8296e752017-08-25 08:45:21 -040057 GrGLSLTypeString(shaderCaps, effectiveType),
Brian Salomon99938a82016-11-21 13:41:08 -050058 this->getName().c_str());
59 } else {
60 SkASSERT(this->getArrayCount() > 0);
61 out->appendf("%s %s[%d]",
Robert Phillips8296e752017-08-25 08:45:21 -040062 GrGLSLTypeString(shaderCaps, effectiveType),
Brian Salomon99938a82016-11-21 13:41:08 -050063 this->getName().c_str(),
64 this->getArrayCount());
65 }
66 } else {
67 out->appendf("%s %s",
Robert Phillips8296e752017-08-25 08:45:21 -040068 GrGLSLTypeString(shaderCaps, effectiveType),
Brian Salomon99938a82016-11-21 13:41:08 -050069 this->getName().c_str());
70 }
71}