Always apply flat qualifier to double inputs, same as int/uint
[platform/upstream/VK-GL-CTS.git] / external / openglcts / modules / common / glcShaderRenderCase.hpp
1 #ifndef _GLCSHADERRENDERCASE_HPP
2 #define _GLCSHADERRENDERCASE_HPP
3 /*-------------------------------------------------------------------------
4  * OpenGL Conformance Test Suite
5  * -----------------------------
6  *
7  * Copyright (c) 2016 Google Inc.
8  * Copyright (c) 2016 The Khronos Group Inc.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */ /*!
23  * \file
24  * \brief Shader execute test.
25  */ /*-------------------------------------------------------------------*/
26
27 #include "glcTestCase.hpp"
28 #include "gluContextInfo.hpp"
29 #include "gluRenderContext.hpp"
30 #include "gluShaderProgram.hpp"
31 #include "tcuDefs.hpp"
32 #include "tcuMatrix.hpp"
33 #include "tcuSurface.hpp"
34 #include "tcuTexture.hpp"
35 #include "tcuVector.hpp"
36
37 #include <sstream>
38 #include <string>
39
40 namespace glu
41 {
42 class RenderContext;
43 class Texture2D;
44 class TextureCube;
45 class Texture2DArray;
46 class Texture3D;
47 } // glu
48
49 namespace deqp
50 {
51
52 // LineStream
53
54 class LineStream
55 {
56 public:
57         LineStream(int indent = 0)
58         {
59                 m_indent = indent;
60         }
61         ~LineStream(void)
62         {
63         }
64
65         const char* str(void) const
66         {
67                 m_string = m_stream.str();
68                 return m_string.c_str();
69         }
70         LineStream& operator<<(const char* line)
71         {
72                 for (int i = 0; i < m_indent; i++)
73                 {
74                         m_stream << "\t";
75                 }
76                 m_stream << line << "\n";
77                 return *this;
78         }
79
80 private:
81         int                                     m_indent;
82         std::ostringstream  m_stream;
83         mutable std::string m_string;
84 };
85
86 class QuadGrid;
87
88 // TextureBinding
89
90 class TextureBinding
91 {
92 public:
93         enum Type
94         {
95                 TYPE_NONE = 0,
96                 TYPE_2D,
97                 TYPE_CUBE_MAP,
98                 TYPE_2D_ARRAY,
99                 TYPE_3D,
100
101                 TYPE_LAST
102         };
103
104         TextureBinding(const glu::Texture2D* tex2D, const tcu::Sampler& sampler);
105         TextureBinding(const glu::TextureCube* texCube, const tcu::Sampler& sampler);
106         TextureBinding(const glu::Texture2DArray* tex2DArray, const tcu::Sampler& sampler);
107         TextureBinding(const glu::Texture3D* tex3D, const tcu::Sampler& sampler);
108         TextureBinding(void);
109
110         void setSampler(const tcu::Sampler& sampler);
111         void setTexture(const glu::Texture2D* tex2D);
112         void setTexture(const glu::TextureCube* texCube);
113         void setTexture(const glu::Texture2DArray* tex2DArray);
114         void setTexture(const glu::Texture3D* tex3D);
115
116         Type getType(void) const
117         {
118                 return m_type;
119         }
120         const tcu::Sampler& getSampler(void) const
121         {
122                 return m_sampler;
123         }
124         const glu::Texture2D* get2D(void) const
125         {
126                 DE_ASSERT(getType() == TYPE_2D);
127                 return m_binding.tex2D;
128         }
129         const glu::TextureCube* getCube(void) const
130         {
131                 DE_ASSERT(getType() == TYPE_CUBE_MAP);
132                 return m_binding.texCube;
133         }
134         const glu::Texture2DArray* get2DArray(void) const
135         {
136                 DE_ASSERT(getType() == TYPE_2D_ARRAY);
137                 return m_binding.tex2DArray;
138         }
139         const glu::Texture3D* get3D(void) const
140         {
141                 DE_ASSERT(getType() == TYPE_3D);
142                 return m_binding.tex3D;
143         }
144
145 private:
146         Type             m_type;
147         tcu::Sampler m_sampler;
148         union {
149                 const glu::Texture2D*     tex2D;
150                 const glu::TextureCube* texCube;
151                 const glu::Texture2DArray* tex2DArray;
152                 const glu::Texture3D*     tex3D;
153         } m_binding;
154 };
155
156 // ShaderEvalContext.
157
158 class ShaderEvalContext
159 {
160 public:
161         // Limits.
162         enum
163         {
164                 MAX_USER_ATTRIBS = 4,
165                 MAX_TEXTURES     = 4,
166         };
167
168         struct ShaderSampler
169         {
170                 tcu::Sampler                       sampler;
171                 const tcu::Texture2D*     tex2D;
172                 const tcu::TextureCube* texCube;
173                 const tcu::Texture2DArray* tex2DArray;
174                 const tcu::Texture3D*     tex3D;
175
176                 inline ShaderSampler(void) : tex2D(DE_NULL), texCube(DE_NULL), tex2DArray(DE_NULL), tex3D(DE_NULL)
177                 {
178                 }
179         };
180
181         ShaderEvalContext(const QuadGrid& quadGrid);
182         ~ShaderEvalContext(void);
183
184         void reset(float sx, float sy);
185
186         // Inputs.
187         tcu::Vec4 coords;
188         tcu::Vec4 unitCoords;
189         tcu::Vec4 constCoords;
190
191         tcu::Vec4        in[MAX_USER_ATTRIBS];
192         ShaderSampler textures[MAX_TEXTURES];
193
194         // Output.
195         tcu::Vec4 color;
196         bool      isDiscarded;
197
198         // Functions.
199         inline void discard(void)
200         {
201                 isDiscarded = true;
202         }
203         tcu::Vec4 texture2D(int unitNdx, const tcu::Vec2& coords);
204
205 private:
206         const QuadGrid& quadGrid;
207 };
208
209 // ShaderEvalFunc.
210
211 typedef void (*ShaderEvalFunc)(ShaderEvalContext& c);
212
213 inline void evalCoordsPassthroughX(ShaderEvalContext& c)
214 {
215         c.color.x() = c.coords.x();
216 }
217 inline void evalCoordsPassthroughXY(ShaderEvalContext& c)
218 {
219         c.color.xy() = c.coords.swizzle(0, 1);
220 }
221 inline void evalCoordsPassthroughXYZ(ShaderEvalContext& c)
222 {
223         c.color.xyz() = c.coords.swizzle(0, 1, 2);
224 }
225 inline void evalCoordsPassthrough(ShaderEvalContext& c)
226 {
227         c.color = c.coords;
228 }
229 inline void evalCoordsSwizzleWZYX(ShaderEvalContext& c)
230 {
231         c.color = c.coords.swizzle(3, 2, 1, 0);
232 }
233
234 // ShaderEvaluator
235 // Either inherit a class with overridden evaluate() or just pass in an evalFunc.
236
237 class ShaderEvaluator
238 {
239 public:
240         ShaderEvaluator(void);
241         ShaderEvaluator(ShaderEvalFunc evalFunc);
242         virtual ~ShaderEvaluator(void);
243
244         virtual void evaluate(ShaderEvalContext& ctx);
245
246 private:
247         ShaderEvaluator(const ShaderEvaluator&);                        // not allowed!
248         ShaderEvaluator& operator=(const ShaderEvaluator&); // not allowed!
249
250         ShaderEvalFunc m_evalFunc;
251 };
252
253 // ShaderRenderCase.
254
255 class ShaderRenderCase : public tcu::TestCase
256 {
257 public:
258         ShaderRenderCase(tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo,
259                                          const char* name, const char* description, bool isVertexCase, ShaderEvalFunc evalFunc);
260         ShaderRenderCase(tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const glu::ContextInfo& ctxInfo,
261                                          const char* name, const char* description, bool isVertexCase, ShaderEvaluator& evaluator);
262         virtual ~ShaderRenderCase(void);
263
264         void init(void);
265         void deinit(void);
266
267         IterateResult iterate(void);
268
269 protected:
270         virtual void setup(deUint32 programID);
271         virtual void setupUniforms(deUint32 programID, const tcu::Vec4& constCoords);
272
273         tcu::IVec2 getViewportSize(void) const;
274
275 private:
276         ShaderRenderCase(const ShaderRenderCase&);                        // not allowed!
277         ShaderRenderCase& operator=(const ShaderRenderCase&); // not allowed!
278
279         void setupDefaultInputs(int programID);
280
281         void render(tcu::Surface& result, int programID, const QuadGrid& quadGrid);
282         void computeVertexReference(tcu::Surface& result, const QuadGrid& quadGrid);
283         void computeFragmentReference(tcu::Surface& result, const QuadGrid& quadGrid);
284         bool compareImages(const tcu::Surface& resImage, const tcu::Surface& refImage, float errorThreshold);
285
286 protected:
287         glu::RenderContext&             m_renderCtx;
288         const glu::ContextInfo& m_ctxInfo;
289
290         bool                     m_isVertexCase;
291         ShaderEvaluator  m_defaultEvaluator;
292         ShaderEvaluator& m_evaluator;
293         std::string              m_vertShaderSource;
294         std::string              m_fragShaderSource;
295         tcu::Vec4                m_clearColor;
296
297         std::vector<tcu::Mat4>          m_userAttribTransforms;
298         std::vector<TextureBinding> m_textures;
299
300         glu::ShaderProgram* m_program;
301 };
302
303 // Helpers.
304 // \todo [2012-04-10 pyry] Move these to separate utility?
305
306 const char* getIntUniformName(int number);
307 const char* getFloatUniformName(int number);
308 const char* getFloatFractionUniformName(int number);
309
310 void setupDefaultUniforms(const glu::RenderContext& context, deUint32 programID);
311
312 } // deqp
313
314 #endif // _GLCSHADERRENDERCASE_HPP