Merge gerrit/vulkan-cts-1.0.0 into gerrit/vulkan-cts-1.0.1
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / shaderrender / vktShaderRenderBuiltinVarTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  * Copyright (c) 2016 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Shader builtin variable tests.
24  *//*--------------------------------------------------------------------*/
25
26 #include "vktShaderRenderBuiltinVarTests.hpp"
27 #include "vktShaderRender.hpp"
28 #include "gluShaderUtil.hpp"
29 #include "tcuImageCompare.hpp"
30 #include "tcuStringTemplate.hpp"
31 #include "tcuTextureUtil.hpp"
32
33 using namespace std;
34 using namespace tcu;
35 using namespace vk;
36
37 namespace vkt
38 {
39 namespace sr
40 {
41
42 namespace
43 {
44
45 class BuiltinGlFrontFacingCaseInstance : public ShaderRenderCaseInstance
46 {
47 public:
48                                         BuiltinGlFrontFacingCaseInstance        (Context& context);
49
50         TestStatus              iterate                                                         (void);
51         virtual void    setupDefaultInputs                                      (void);
52 };
53
54 BuiltinGlFrontFacingCaseInstance::BuiltinGlFrontFacingCaseInstance (Context& context)
55         : ShaderRenderCaseInstance      (context)
56 {
57 }
58
59 TestStatus BuiltinGlFrontFacingCaseInstance::iterate (void)
60 {
61         const UVec2             viewportSize    = getViewportSize();
62         const int               width                   = viewportSize.x();
63         const int               height                  = viewportSize.y();
64         const RGBA              threshold               (2, 2, 2, 2);
65         Surface                 resImage                (width, height);
66         Surface                 refImage                (width, height);
67         bool                    compareOk               = false;
68         const deUint16  indices[12]             =
69         {
70                 0, 4, 1,
71                 0, 5, 4,
72                 1, 2, 3,
73                 1, 3, 4
74         };
75
76         setup();
77         render(6, 4, indices);
78         copy(resImage.getAccess(), getResultImage().getAccess());
79
80         for (int y = 0; y < refImage.getHeight(); y++)
81         {
82                 for (int x = 0; x < refImage.getWidth()/2; x++)
83                         refImage.setPixel(x, y, RGBA::green());
84
85                 for (int x = refImage.getWidth()/2; x < refImage.getWidth(); x++)
86                         refImage.setPixel(x, y, RGBA::blue());
87         }
88
89         compareOk = pixelThresholdCompare(m_context.getTestContext().getLog(), "Result", "Image comparison result", refImage, resImage, threshold, COMPARE_LOG_RESULT);
90
91         if (compareOk)
92                 return TestStatus::pass("Result image matches reference");
93         else
94                 return TestStatus::fail("Image mismatch");
95 }
96
97 void BuiltinGlFrontFacingCaseInstance::setupDefaultInputs (void)
98 {
99         const float vertices[] =
100         {
101                 -1.0f, -1.0f, 0.0f, 1.0f,
102                  0.0f, -1.0f, 0.0f, 1.0f,
103                  1.0f, -1.0f, 0.0f, 1.0f,
104                  1.0f,  1.0f, 0.0f, 1.0f,
105                  0.0f,  1.0f, 0.0f, 1.0f,
106                 -1.0f,  1.0f, 0.0f, 1.0f
107         };
108
109         addAttribute(0u, VK_FORMAT_R32G32B32A32_SFLOAT, (deUint32)sizeof(float) * 4, 6, vertices);
110 }
111
112 class BuiltinGlFrontFacingCase : public TestCase
113 {
114 public:
115                                                                 BuiltinGlFrontFacingCase        (TestContext& testCtx, const string& name, const string& description);
116         virtual                                         ~BuiltinGlFrontFacingCase       (void);
117
118         void                                            initPrograms                            (SourceCollections& dst) const;
119         TestInstance*                           createInstance                          (Context& context) const;
120
121 private:
122                                                                 BuiltinGlFrontFacingCase        (const BuiltinGlFrontFacingCase&);      // not allowed!
123         BuiltinGlFrontFacingCase&       operator=                                       (const BuiltinGlFrontFacingCase&);      // not allowed!
124 };
125
126 BuiltinGlFrontFacingCase::BuiltinGlFrontFacingCase (TestContext& testCtx, const string& name, const string& description)
127         : TestCase(testCtx, name, description)
128 {
129 }
130
131 BuiltinGlFrontFacingCase::~BuiltinGlFrontFacingCase (void)
132 {
133 }
134
135 void BuiltinGlFrontFacingCase::initPrograms (SourceCollections& dst) const
136 {
137         dst.glslSources.add("vert") << glu::VertexSource(
138                 "#version 310 es\n"
139                 "layout(location = 0) in highp vec4 a_position;\n"
140                 "void main (void)\n"
141                 "{\n"
142                 "       gl_Position = a_position;\n"
143                 "}\n");
144
145         dst.glslSources.add("frag") << glu::FragmentSource(
146                 "#version 310 es\n"
147                 "layout(location = 0) out lowp vec4 o_color;\n"
148                 "void main (void)\n"
149                 "{\n"
150                 "       if (gl_FrontFacing)\n"
151                 "               o_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
152                 "       else\n"
153                 "               o_color = vec4(0.0, 0.0, 1.0, 1.0);\n"
154                 "}\n");
155 }
156
157 TestInstance* BuiltinGlFrontFacingCase::createInstance (Context& context) const
158 {
159         return new BuiltinGlFrontFacingCaseInstance(context);
160 }
161
162 } // anonymous
163
164 TestCaseGroup* createBuiltinVarTests (TestContext& testCtx)
165 {
166         de::MovePtr<TestCaseGroup> varyingGroup(new TestCaseGroup(testCtx, "builtin_var", "Shader builtin variable tests."));
167
168         varyingGroup->addChild(new BuiltinGlFrontFacingCase(testCtx, "gl_frontfacing", "gl_FrontFacing test"));
169
170         return varyingGroup.release();
171 }
172
173 } // sr
174 } // vkt