xref: /aosp_15_r20/external/deqp/framework/randomshaders/rsgVariable.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Random Shader Generator
3  * ----------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Variable class.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "rsgVariable.hpp"
25 #include "rsgGeneratorState.hpp"
26 #include "rsgShader.hpp"
27 
28 namespace rsg
29 {
30 
Variable(const VariableType & type,Storage storage,const char * name)31 Variable::Variable(const VariableType &type, Storage storage, const char *name)
32     : m_type(type)
33     , m_storage(storage)
34     , m_name(name)
35     , m_layoutLocation(-1)
36 {
37 }
38 
~Variable(void)39 Variable::~Variable(void)
40 {
41 }
42 
tokenizeDeclaration(GeneratorState & state,TokenStream & str) const43 void Variable::tokenizeDeclaration(GeneratorState &state, TokenStream &str) const
44 {
45     Version targetVersion = state.getProgramParameters().version;
46 
47     // \todo [2011-03-10 pyry] Remove precision hacks once precision handling is implemented
48     switch (m_storage)
49     {
50     case STORAGE_CONST:
51         str << Token::CONST;
52         break;
53     case STORAGE_PARAMETER_IN:
54         str << Token::IN;
55         break;
56     case STORAGE_PARAMETER_OUT:
57         str << Token::OUT;
58         break;
59     case STORAGE_PARAMETER_INOUT:
60         str << Token::INOUT;
61         break;
62 
63     case STORAGE_UNIFORM:
64     {
65         str << Token::UNIFORM;
66         if (m_type.isFloatOrVec() || m_type.isIntOrVec())
67             str << Token::MEDIUM_PRECISION;
68         break;
69     }
70 
71     case STORAGE_SHADER_IN:
72     {
73         if (targetVersion >= VERSION_300)
74         {
75             if (hasLayoutLocation())
76                 str << Token::LAYOUT << Token::LEFT_PAREN << Token::LOCATION << Token::EQUAL << m_layoutLocation
77                     << Token::RIGHT_PAREN;
78 
79             str << Token::IN;
80 
81             if (state.getShader().getType() == Shader::TYPE_FRAGMENT)
82                 str << Token::MEDIUM_PRECISION;
83         }
84         else
85         {
86             DE_ASSERT(!hasLayoutLocation());
87 
88             switch (state.getShader().getType())
89             {
90             case Shader::TYPE_VERTEX:
91                 str << Token::ATTRIBUTE;
92                 break;
93             case Shader::TYPE_FRAGMENT:
94                 str << Token::VARYING << Token::MEDIUM_PRECISION;
95                 break;
96             default:
97                 DE_ASSERT(false);
98                 break;
99             }
100         }
101         break;
102     }
103 
104     case STORAGE_SHADER_OUT:
105     {
106         if (targetVersion >= VERSION_300)
107         {
108             if (hasLayoutLocation())
109                 str << Token::LAYOUT << Token::LEFT_PAREN << Token::LOCATION << Token::EQUAL << m_layoutLocation
110                     << Token::RIGHT_PAREN;
111 
112             str << Token::OUT << Token::MEDIUM_PRECISION;
113         }
114         else
115         {
116             DE_ASSERT(!hasLayoutLocation());
117 
118             if (state.getShader().getType() == Shader::TYPE_VERTEX)
119                 str << Token::VARYING << Token::MEDIUM_PRECISION;
120             else
121                 DE_ASSERT(false);
122         }
123         break;
124     }
125 
126     case STORAGE_LOCAL:
127     default:
128         /* nothing */
129         break;
130     }
131 
132     m_type.tokenizeShortType(str);
133 
134     DE_ASSERT(m_name != "");
135     str << Token(m_name.c_str());
136 }
137 
138 } // namespace rsg
139