blob: 91e2c92890e15bc27cba75afcbd679878ff7b005 [file] [log] [blame]
Martin Radev14a26ae2017-07-24 15:56:29 +03001//
2// Copyright 2017 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// Multiview draw tests:
7// Test issuing multiview Draw* commands.
8//
9
Martin Radevc1d4e552017-08-21 12:01:10 +030010#include "platform/WorkaroundsD3D.h"
Martin Radev14a26ae2017-07-24 15:56:29 +030011#include "test_utils/ANGLETest.h"
12#include "test_utils/gl_raii.h"
13
14using namespace angle;
15
Martin Radev61bd9992017-08-11 13:10:55 +030016namespace
17{
Martin Radevced5c862017-08-17 16:05:29 +030018GLuint CreateSimplePassthroughProgram(int numViews)
Martin Radev61bd9992017-08-11 13:10:55 +030019{
20 const std::string vsSource =
21 "#version 300 es\n"
22 "#extension GL_OVR_multiview : require\n"
Martin Radevced5c862017-08-17 16:05:29 +030023 "layout(num_views = " +
24 ToString(numViews) +
25 ") in;\n"
Martin Radev61bd9992017-08-11 13:10:55 +030026 "layout(location=0) in vec2 vPosition;\n"
27 "void main()\n"
28 "{\n"
Martin Radevced5c862017-08-17 16:05:29 +030029 " gl_PointSize = 1.;\n"
Martin Radev61bd9992017-08-11 13:10:55 +030030 " gl_Position = vec4(vPosition.xy, 0.0, 1.0);\n"
31 "}\n";
32
33 const std::string fsSource =
34 "#version 300 es\n"
35 "#extension GL_OVR_multiview : require\n"
36 "precision mediump float;\n"
37 "out vec4 col;\n"
38 "void main()\n"
39 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +030040 " col = vec4(0,1,0,1);\n"
Martin Radev61bd9992017-08-11 13:10:55 +030041 "}\n";
42 return CompileProgram(vsSource, fsSource);
43}
44
45std::vector<Vector2> ConvertPixelCoordinatesToClipSpace(const std::vector<Vector2I> &pixels,
46 int width,
47 int height)
48{
49 std::vector<Vector2> result(pixels.size());
50 for (size_t i = 0; i < pixels.size(); ++i)
51 {
52 const auto &pixel = pixels[i];
53 float pixelCenterRelativeX = (static_cast<float>(pixel.x()) + .5f) / width;
54 float pixelCenterRelativeY = (static_cast<float>(pixel.y()) + .5f) / height;
55 float xInClipSpace = 2.f * pixelCenterRelativeX - 1.f;
56 float yInClipSpace = 2.f * pixelCenterRelativeY - 1.f;
57 result[i] = Vector2(xInClipSpace, yInClipSpace);
58 }
59 return result;
60}
61} // namespace
62
Martin Radevc1d4e552017-08-21 12:01:10 +030063struct MultiviewImplementationParams : public PlatformParameters
Martin Radev3c25ad02017-08-22 17:36:53 +030064{
Martin Radevc1d4e552017-08-21 12:01:10 +030065 MultiviewImplementationParams(bool forceUseGeometryShaderOnD3D,
66 const EGLPlatformParameters &eglPlatformParameters)
67 : PlatformParameters(3, 0, eglPlatformParameters),
68 mForceUseGeometryShaderOnD3D(forceUseGeometryShaderOnD3D)
69 {
70 }
71 bool mForceUseGeometryShaderOnD3D;
72};
73
74std::ostream &operator<<(std::ostream &os, const MultiviewImplementationParams &params)
75{
76 const PlatformParameters &base = static_cast<const PlatformParameters &>(params);
77 os << base;
78 if (params.mForceUseGeometryShaderOnD3D)
79 {
80 os << "_force_geom_shader";
81 }
82 else
83 {
84 os << "_vertex_shader";
85 }
86 return os;
87}
88
89struct MultiviewTestParams final : public MultiviewImplementationParams
90{
91 MultiviewTestParams(GLenum multiviewLayout,
92 const MultiviewImplementationParams &implementationParams)
93 : MultiviewImplementationParams(implementationParams), mMultiviewLayout(multiviewLayout)
Martin Radev3c25ad02017-08-22 17:36:53 +030094 {
95 ASSERT(multiviewLayout == GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE ||
96 multiviewLayout == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE);
97 }
98 GLenum mMultiviewLayout;
99};
100
101std::ostream &operator<<(std::ostream &os, const MultiviewTestParams &params)
102{
Martin Radevc1d4e552017-08-21 12:01:10 +0300103 const MultiviewImplementationParams &base =
104 static_cast<const MultiviewImplementationParams &>(params);
Martin Radev3c25ad02017-08-22 17:36:53 +0300105 os << base;
106 switch (params.mMultiviewLayout)
107 {
108 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
109 os << "_layered";
110 break;
111 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
112 os << "_side_by_side";
113 break;
114 default:
115 UNREACHABLE();
116 }
117 return os;
118}
119
120class MultiviewDrawTest : public ANGLETestBase
Martin Radev14a26ae2017-07-24 15:56:29 +0300121{
122 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300123 MultiviewDrawTest(const PlatformParameters &params) : ANGLETestBase(params)
Martin Radev14a26ae2017-07-24 15:56:29 +0300124 {
125 setWindowWidth(128);
126 setWindowHeight(128);
127 setWebGLCompatibilityEnabled(true);
128 }
Martin Radev7cf61662017-07-26 17:10:53 +0300129 virtual ~MultiviewDrawTest() {}
Martin Radev14a26ae2017-07-24 15:56:29 +0300130
Martin Radev3c25ad02017-08-22 17:36:53 +0300131 void DrawTestSetUp()
Martin Radev14a26ae2017-07-24 15:56:29 +0300132 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300133 ANGLETestBase::ANGLETestSetUp();
Martin Radev14a26ae2017-07-24 15:56:29 +0300134
135 glRequestExtensionANGLE = reinterpret_cast<PFNGLREQUESTEXTENSIONANGLEPROC>(
136 eglGetProcAddress("glRequestExtensionANGLE"));
137 }
138
139 // Requests the ANGLE_multiview extension and returns true if the operation succeeds.
140 bool requestMultiviewExtension()
141 {
142 if (extensionRequestable("GL_ANGLE_multiview"))
143 {
144 glRequestExtensionANGLE("GL_ANGLE_multiview");
145 }
146
147 if (!extensionEnabled("GL_ANGLE_multiview"))
148 {
149 std::cout << "Test skipped due to missing GL_ANGLE_multiview." << std::endl;
150 return false;
151 }
152 return true;
153 }
154
155 PFNGLREQUESTEXTENSIONANGLEPROC glRequestExtensionANGLE = nullptr;
156};
157
Martin Radev3c25ad02017-08-22 17:36:53 +0300158class MultiviewDrawValidationTest : public MultiviewDrawTest,
159 public ::testing::TestWithParam<PlatformParameters>
Martin Radev7cf61662017-07-26 17:10:53 +0300160{
161 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300162 MultiviewDrawValidationTest() : MultiviewDrawTest(GetParam()) {}
Martin Radev7cf61662017-07-26 17:10:53 +0300163
164 void SetUp() override
165 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300166 MultiviewDrawTest::DrawTestSetUp();
Martin Radev7cf61662017-07-26 17:10:53 +0300167
168 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
169
170 glBindTexture(GL_TEXTURE_2D, mTex2d);
171 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
172
173 glBindVertexArray(mVao);
174
175 const float kVertexData[3] = {0.0f};
176 glBindBuffer(GL_ARRAY_BUFFER, mVbo);
177 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3u, &kVertexData[0], GL_STATIC_DRAW);
178
179 const unsigned int kIndices[3] = {0u, 1u, 2u};
180 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
181 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3, &kIndices[0],
182 GL_STATIC_DRAW);
183 ASSERT_GL_NO_ERROR();
184 }
185
186 GLTexture mTex2d;
187 GLVertexArray mVao;
188 GLBuffer mVbo;
189 GLBuffer mIbo;
190 GLFramebuffer mFramebuffer;
191};
192
Martin Radev3c25ad02017-08-22 17:36:53 +0300193class MultiviewRenderTestBase : public MultiviewDrawTest
Martin Radev8f276e22017-05-30 12:05:52 +0300194{
195 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300196 MultiviewRenderTestBase(const PlatformParameters &params, GLenum multiviewLayout)
197 : MultiviewDrawTest(params),
198 mMultiviewLayout(multiviewLayout),
199 mViewWidth(0),
200 mViewHeight(0),
201 mNumViews(0)
Martin Radev8f276e22017-05-30 12:05:52 +0300202 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300203 }
204 void RenderTestSetUp() { MultiviewDrawTest::DrawTestSetUp(); }
Martin Radev72b4e1e2017-08-31 15:42:56 +0300205 void createFBO(int viewWidth, int height, int numViews, int numLayers, int baseViewIndex)
Martin Radev3c25ad02017-08-22 17:36:53 +0300206 {
Martin Radev72b4e1e2017-08-31 15:42:56 +0300207 ASSERT(numViews + baseViewIndex <= numLayers);
208
Martin Radev3c25ad02017-08-22 17:36:53 +0300209 mViewWidth = viewWidth;
210 mViewHeight = height;
211 mNumViews = numViews;
Martin Radev8f276e22017-05-30 12:05:52 +0300212
Martin Radev0abb7a22017-08-28 15:34:45 +0300213 mColorTexture.reset();
214 mDepthTexture.reset();
215 mDrawFramebuffer.reset();
216 mReadFramebuffer.clear();
217
Martin Radev8f276e22017-05-30 12:05:52 +0300218 // Create color and depth textures.
Martin Radev3c25ad02017-08-22 17:36:53 +0300219 switch (mMultiviewLayout)
Martin Radev8f276e22017-05-30 12:05:52 +0300220 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300221 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
222 {
223 int textureWidth = viewWidth * numViews;
224 glBindTexture(GL_TEXTURE_2D, mColorTexture);
225 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, height, 0, GL_RGBA,
226 GL_UNSIGNED_BYTE, NULL);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
229
230 glBindTexture(GL_TEXTURE_2D, mDepthTexture);
231 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, textureWidth, height, 0,
232 GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
233 glBindTexture(GL_TEXTURE_2D, 0);
234 break;
235 }
236 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
237 glBindTexture(GL_TEXTURE_2D_ARRAY, mColorTexture);
Martin Radev72b4e1e2017-08-31 15:42:56 +0300238 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, viewWidth, height, numLayers, 0,
Martin Radev3c25ad02017-08-22 17:36:53 +0300239 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
240 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
241 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
242
243 glBindTexture(GL_TEXTURE_2D_ARRAY, mDepthTexture);
244 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT32F, viewWidth, height,
Martin Radev72b4e1e2017-08-31 15:42:56 +0300245 numLayers, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
Martin Radev3c25ad02017-08-22 17:36:53 +0300246 glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
247 break;
248 default:
249 UNREACHABLE();
Martin Radev8f276e22017-05-30 12:05:52 +0300250 }
Martin Radev3c25ad02017-08-22 17:36:53 +0300251 ASSERT_GL_NO_ERROR();
252
253 // Create draw framebuffer to be used for multiview rendering.
254 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
255 switch (mMultiviewLayout)
256 {
257 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
258 {
259 std::vector<GLint> viewportOffsets(numViews * 2);
260 for (int i = 0u; i < numViews; ++i)
261 {
262 viewportOffsets[i * 2] = i * viewWidth;
263 viewportOffsets[i * 2 + 1] = 0;
264 }
265 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER,
266 GL_COLOR_ATTACHMENT0, mColorTexture, 0,
267 numViews, &viewportOffsets[0]);
268 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER,
269 GL_DEPTH_ATTACHMENT, mDepthTexture, 0,
270 numViews, &viewportOffsets[0]);
271 break;
272 }
273 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
274 glFramebufferTextureMultiviewLayeredANGLE(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
Martin Radev72b4e1e2017-08-31 15:42:56 +0300275 mColorTexture, 0, baseViewIndex,
276 numViews);
Martin Radev3c25ad02017-08-22 17:36:53 +0300277 glFramebufferTextureMultiviewLayeredANGLE(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
Martin Radev72b4e1e2017-08-31 15:42:56 +0300278 mDepthTexture, 0, baseViewIndex,
279 numViews);
Martin Radev3c25ad02017-08-22 17:36:53 +0300280 break;
281 default:
282 UNREACHABLE();
283 }
Martin Radev8f276e22017-05-30 12:05:52 +0300284
285 GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
286 glDrawBuffers(1, DrawBuffers);
287 ASSERT_GL_NO_ERROR();
288 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER));
289
290 // Create read framebuffer to be used to retrieve the pixel information for testing
291 // purposes.
Martin Radev3c25ad02017-08-22 17:36:53 +0300292 switch (mMultiviewLayout)
293 {
294 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
295 mReadFramebuffer.resize(1);
296 glBindFramebuffer(GL_READ_FRAMEBUFFER, mReadFramebuffer[0]);
297 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
298 mColorTexture, 0);
Martin Radev72b4e1e2017-08-31 15:42:56 +0300299 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE,
300 glCheckFramebufferStatus(GL_READ_FRAMEBUFFER));
Martin Radev3c25ad02017-08-22 17:36:53 +0300301 break;
302 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
Martin Radev72b4e1e2017-08-31 15:42:56 +0300303 mReadFramebuffer.resize(numLayers);
304 for (int i = 0; i < numLayers; ++i)
Martin Radev3c25ad02017-08-22 17:36:53 +0300305 {
Martin Radev72b4e1e2017-08-31 15:42:56 +0300306 glBindFramebuffer(GL_FRAMEBUFFER, mReadFramebuffer[i]);
307 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture,
308 0, i);
309 glClearColor(0, 0, 0, 0);
310 glClear(GL_COLOR_BUFFER_BIT);
311 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE,
312 glCheckFramebufferStatus(GL_READ_FRAMEBUFFER));
Martin Radev3c25ad02017-08-22 17:36:53 +0300313 }
Martin Radev72b4e1e2017-08-31 15:42:56 +0300314 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
Martin Radev3c25ad02017-08-22 17:36:53 +0300315 break;
316 default:
317 UNREACHABLE();
318 }
Martin Radev8f276e22017-05-30 12:05:52 +0300319
320 // Clear the buffers.
Martin Radev3c25ad02017-08-22 17:36:53 +0300321 glViewport(0, 0, viewWidth, height);
322 if (mMultiviewLayout == GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE)
323 {
324 // Enable the scissor test only for side-by-side framebuffers.
325 glEnable(GL_SCISSOR_TEST);
326 glScissor(0, 0, viewWidth, height);
327 }
Martin Radev61bd9992017-08-11 13:10:55 +0300328 glClearColor(0, 0, 0, 1);
Martin Radev8f276e22017-05-30 12:05:52 +0300329 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev3c25ad02017-08-22 17:36:53 +0300330 }
Martin Radev8f276e22017-05-30 12:05:52 +0300331
Martin Radev72b4e1e2017-08-31 15:42:56 +0300332 void createFBO(int viewWidth, int height, int numViews)
333 {
334 createFBO(viewWidth, height, numViews, numViews, 0);
335 }
336
Martin Radev3c25ad02017-08-22 17:36:53 +0300337 GLColor GetViewColor(int x, int y, int view)
338 {
339 switch (mMultiviewLayout)
340 {
341 case GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE:
342 return ReadColor(view * mViewWidth + x, y);
343 case GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE:
344 ASSERT(static_cast<size_t>(view) < mReadFramebuffer.size());
345 glBindFramebuffer(GL_READ_FRAMEBUFFER, mReadFramebuffer[view]);
346 return ReadColor(x, y);
347 default:
348 UNREACHABLE();
349 }
350 return GLColor(0, 0, 0, 0);
Martin Radev8f276e22017-05-30 12:05:52 +0300351 }
352
353 GLTexture mColorTexture;
354 GLTexture mDepthTexture;
355 GLFramebuffer mDrawFramebuffer;
Martin Radev3c25ad02017-08-22 17:36:53 +0300356 std::vector<GLFramebuffer> mReadFramebuffer;
357 GLenum mMultiviewLayout;
358 int mViewWidth;
359 int mViewHeight;
360 int mNumViews;
Martin Radev8f276e22017-05-30 12:05:52 +0300361};
362
Martin Radev3c25ad02017-08-22 17:36:53 +0300363class MultiviewRenderTest : public MultiviewRenderTestBase,
364 public ::testing::TestWithParam<MultiviewTestParams>
Martin Radev8f276e22017-05-30 12:05:52 +0300365{
366 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300367 MultiviewRenderTest() : MultiviewRenderTestBase(GetParam(), GetParam().mMultiviewLayout) {}
368 void SetUp() override { MultiviewRenderTestBase::RenderTestSetUp(); }
Martin Radevc1d4e552017-08-21 12:01:10 +0300369
370 void overrideWorkaroundsD3D(WorkaroundsD3D *workarounds) override
371 {
372 workarounds->selectViewInGeometryShader = GetParam().mForceUseGeometryShaderOnD3D;
373 }
Martin Radev3c25ad02017-08-22 17:36:53 +0300374};
375
376class MultiviewRenderDualViewTest : public MultiviewRenderTest
377{
378 protected:
379 MultiviewRenderDualViewTest() : mProgram(0u) {}
380 ~MultiviewRenderDualViewTest()
Martin Radev8f276e22017-05-30 12:05:52 +0300381 {
382 if (mProgram != 0u)
383 {
384 glDeleteProgram(mProgram);
385 }
386 }
387
388 void SetUp() override
389 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300390 MultiviewRenderTest::SetUp();
Martin Radev8f276e22017-05-30 12:05:52 +0300391
392 if (!requestMultiviewExtension())
393 {
394 return;
395 }
396
397 const std::string vsSource =
398 "#version 300 es\n"
399 "#extension GL_OVR_multiview : require\n"
400 "layout(num_views = 2) in;\n"
401 "in vec4 vPosition;\n"
402 "void main()\n"
403 "{\n"
404 " gl_Position.x = (gl_ViewID_OVR == 0u ? vPosition.x*0.5 + 0.5 : vPosition.x*0.5);\n"
405 " gl_Position.yzw = vPosition.yzw;\n"
406 "}\n";
407
408 const std::string fsSource =
409 "#version 300 es\n"
410 "#extension GL_OVR_multiview : require\n"
411 "precision mediump float;\n"
412 "out vec4 col;\n"
413 "void main()\n"
414 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +0300415 " col = vec4(0,1,0,1);\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300416 "}\n";
417
Martin Radev3c25ad02017-08-22 17:36:53 +0300418 createFBO(2, 1, 2);
Martin Radev61bd9992017-08-11 13:10:55 +0300419 mProgram = CompileProgram(vsSource, fsSource);
420 ASSERT_NE(mProgram, 0u);
Martin Radev8f276e22017-05-30 12:05:52 +0300421 glUseProgram(mProgram);
422 ASSERT_GL_NO_ERROR();
423 }
424
425 void checkOutput()
426 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300427 EXPECT_EQ(GLColor::black, GetViewColor(0, 0, 0));
Martin Radev67a8a012017-09-08 13:03:52 +0300428 EXPECT_EQ(GLColor::green, GetViewColor(1, 0, 0));
429 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev3c25ad02017-08-22 17:36:53 +0300430 EXPECT_EQ(GLColor::black, GetViewColor(1, 0, 1));
Martin Radev8f276e22017-05-30 12:05:52 +0300431 }
432
433 GLuint mProgram;
434};
435
Martin Radev3c25ad02017-08-22 17:36:53 +0300436class MultiviewOcclusionQueryTest : public MultiviewRenderTest
Martin Radev0d671c92017-08-10 16:41:52 +0300437{
438 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300439 MultiviewOcclusionQueryTest() {}
Martin Radev0d671c92017-08-10 16:41:52 +0300440
Geoff Lang8c5b31c2017-09-26 18:07:44 -0400441 bool requestOcclusionQueryExtension()
442 {
443 if (extensionRequestable("GL_EXT_occlusion_query_boolean"))
444 {
445 glRequestExtensionANGLE("GL_EXT_occlusion_query_boolean");
446 }
447
448 if (!extensionEnabled("GL_EXT_occlusion_query_boolean"))
449 {
450 std::cout << "Test skipped due to missing GL_EXT_occlusion_query_boolean." << std::endl;
451 return false;
452 }
453 return true;
454 }
455
Martin Radev0d671c92017-08-10 16:41:52 +0300456 GLuint drawAndRetrieveOcclusionQueryResult(GLuint program)
457 {
Geoff Lang8c5b31c2017-09-26 18:07:44 -0400458 GLQueryEXT query;
459 glBeginQueryEXT(GL_ANY_SAMPLES_PASSED, query);
Martin Radev0d671c92017-08-10 16:41:52 +0300460 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
461 glEndQueryEXT(GL_ANY_SAMPLES_PASSED);
462
463 GLuint result = GL_TRUE;
Geoff Lang8c5b31c2017-09-26 18:07:44 -0400464 glGetQueryObjectuivEXT(query, GL_QUERY_RESULT, &result);
Martin Radev0d671c92017-08-10 16:41:52 +0300465 return result;
466 }
467};
468
Martin Radev3c25ad02017-08-22 17:36:53 +0300469class MultiviewProgramGenerationTest : public MultiviewRenderTest
Martin Radev41ac68e2017-06-06 12:16:58 +0300470{
471 protected:
472 MultiviewProgramGenerationTest() {}
473};
474
Martin Radev3c25ad02017-08-22 17:36:53 +0300475class MultiviewRenderPrimitiveTest : public MultiviewRenderTest
Martin Radev61bd9992017-08-11 13:10:55 +0300476{
477 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300478 MultiviewRenderPrimitiveTest() {}
Martin Radev61bd9992017-08-11 13:10:55 +0300479
480 void setupGeometry(const std::vector<Vector2> &vertexData)
481 {
Martin Radev61bd9992017-08-11 13:10:55 +0300482 glBindBuffer(GL_ARRAY_BUFFER, mVBO);
483 glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(Vector2), vertexData.data(),
484 GL_STATIC_DRAW);
485 glEnableVertexAttribArray(0);
486 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
487 }
488
Martin Radev67a8a012017-09-08 13:03:52 +0300489 void checkGreenChannel(const GLubyte expectedGreenChannelData[])
Martin Radev61bd9992017-08-11 13:10:55 +0300490 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300491 for (int view = 0; view < mNumViews; ++view)
Martin Radev61bd9992017-08-11 13:10:55 +0300492 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300493 for (int w = 0; w < mViewWidth; ++w)
Martin Radev61bd9992017-08-11 13:10:55 +0300494 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300495 for (int h = 0; h < mViewHeight; ++h)
496 {
497 size_t flatIndex =
498 static_cast<size_t>(view * mViewWidth * mViewHeight + mViewWidth * h + w);
Martin Radev67a8a012017-09-08 13:03:52 +0300499 EXPECT_EQ(GLColor(0, expectedGreenChannelData[flatIndex], 0, 255),
Martin Radev3c25ad02017-08-22 17:36:53 +0300500 GetViewColor(w, h, view));
501 }
Martin Radev61bd9992017-08-11 13:10:55 +0300502 }
503 }
504 }
Martin Radev61bd9992017-08-11 13:10:55 +0300505 GLBuffer mVBO;
506};
507
Martin Radev3c25ad02017-08-22 17:36:53 +0300508class MultiviewSideBySideRenderTest : public MultiviewRenderTestBase,
Martin Radevc1d4e552017-08-21 12:01:10 +0300509 public ::testing::TestWithParam<MultiviewImplementationParams>
Martin Radev3c25ad02017-08-22 17:36:53 +0300510{
511 protected:
512 MultiviewSideBySideRenderTest()
513 : MultiviewRenderTestBase(GetParam(), GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE)
514 {
515 }
516 void SetUp() override { MultiviewRenderTestBase::RenderTestSetUp(); }
Martin Radevc1d4e552017-08-21 12:01:10 +0300517 void overrideWorkaroundsD3D(WorkaroundsD3D *workarounds) override
518 {
519 workarounds->selectViewInGeometryShader = GetParam().mForceUseGeometryShaderOnD3D;
520 }
Martin Radev3c25ad02017-08-22 17:36:53 +0300521};
522
Martin Radev72b4e1e2017-08-31 15:42:56 +0300523class MultiviewLayeredRenderTest : public MultiviewRenderTestBase,
Martin Radevc1d4e552017-08-21 12:01:10 +0300524 public ::testing::TestWithParam<MultiviewImplementationParams>
Martin Radev72b4e1e2017-08-31 15:42:56 +0300525{
526 protected:
527 MultiviewLayeredRenderTest()
528 : MultiviewRenderTestBase(GetParam(), GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE)
529 {
530 }
531 void SetUp() override { MultiviewRenderTestBase::RenderTestSetUp(); }
Martin Radevc1d4e552017-08-21 12:01:10 +0300532 void overrideWorkaroundsD3D(WorkaroundsD3D *workarounds) override
533 {
534 workarounds->selectViewInGeometryShader = GetParam().mForceUseGeometryShaderOnD3D;
535 }
Martin Radev72b4e1e2017-08-31 15:42:56 +0300536};
537
Martin Radev14a26ae2017-07-24 15:56:29 +0300538// The test verifies that glDraw*Indirect:
539// 1) generates an INVALID_OPERATION error if the number of views in the draw framebuffer is greater
540// than 1.
541// 2) does not generate any error if the draw framebuffer has exactly 1 view.
Martin Radev7cf61662017-07-26 17:10:53 +0300542TEST_P(MultiviewDrawValidationTest, IndirectDraw)
Martin Radev14a26ae2017-07-24 15:56:29 +0300543{
544 if (!requestMultiviewExtension())
545 {
546 return;
547 }
548
Martin Radev14a26ae2017-07-24 15:56:29 +0300549 const GLint viewportOffsets[4] = {0, 0, 2, 0};
Martin Radev14a26ae2017-07-24 15:56:29 +0300550
551 const std::string fsSource =
552 "#version 300 es\n"
553 "#extension GL_OVR_multiview : require\n"
554 "precision mediump float;\n"
555 "void main()\n"
556 "{}\n";
557
Martin Radev14a26ae2017-07-24 15:56:29 +0300558 GLBuffer commandBuffer;
559 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, commandBuffer);
560 const GLuint commandData[] = {1u, 1u, 0u, 0u, 0u};
561 glBufferData(GL_DRAW_INDIRECT_BUFFER, sizeof(GLuint) * 5u, &commandData[0], GL_STATIC_DRAW);
562 ASSERT_GL_NO_ERROR();
563
Martin Radev14a26ae2017-07-24 15:56:29 +0300564 // Check for a GL_INVALID_OPERATION error with the framebuffer having 2 views.
565 {
566 const std::string &vsSource =
567 "#version 300 es\n"
568 "#extension GL_OVR_multiview : require\n"
569 "layout(num_views = 2) in;\n"
570 "void main()\n"
571 "{}\n";
572 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
573 glUseProgram(program);
574
Martin Radev7cf61662017-07-26 17:10:53 +0300575 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
576 0, 2, &viewportOffsets[0]);
Martin Radev14a26ae2017-07-24 15:56:29 +0300577
578 glDrawArraysIndirect(GL_TRIANGLES, nullptr);
579 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
580
581 glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
582 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
583 }
584
585 // Check that no errors are generated if the number of views is 1.
586 {
587 const std::string &vsSource =
588 "#version 300 es\n"
589 "#extension GL_OVR_multiview : require\n"
590 "layout(num_views = 1) in;\n"
591 "void main()\n"
592 "{}\n";
593 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
594 glUseProgram(program);
595
Martin Radev7cf61662017-07-26 17:10:53 +0300596 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
597 0, 1, &viewportOffsets[0]);
Martin Radev14a26ae2017-07-24 15:56:29 +0300598
599 glDrawArraysIndirect(GL_TRIANGLES, nullptr);
600 EXPECT_GL_NO_ERROR();
601
602 glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
603 EXPECT_GL_NO_ERROR();
604 }
605}
606
Martin Radev7cf61662017-07-26 17:10:53 +0300607// The test verifies that glDraw*:
608// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer and
609// program differs.
610// 2) does not generate any error if the number of views is the same.
Martin Radev7cf61662017-07-26 17:10:53 +0300611TEST_P(MultiviewDrawValidationTest, NumViewsMismatch)
612{
613 if (!requestMultiviewExtension())
614 {
615 return;
616 }
617
618 const GLint viewportOffsets[4] = {0, 0, 2, 0};
619
620 const std::string &vsSource =
621 "#version 300 es\n"
622 "#extension GL_OVR_multiview : require\n"
623 "layout(num_views = 2) in;\n"
624 "void main()\n"
625 "{}\n";
626 const std::string &fsSource =
627 "#version 300 es\n"
628 "#extension GL_OVR_multiview : require\n"
629 "precision mediump float;\n"
630 "void main()\n"
631 "{}\n";
632 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
633 glUseProgram(program);
634
635 // Check for a GL_INVALID_OPERATION error with the framebuffer and program having different
636 // number of views.
637 {
638 // The framebuffer has only 1 view.
639 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
640 0, 1, &viewportOffsets[0]);
641
642 glDrawArrays(GL_TRIANGLES, 0, 3);
643 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
644
645 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
646 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
647 }
648
649 // Check that no errors are generated if the number of views in both program and draw
650 // framebuffer matches.
651 {
652 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
653 0, 2, &viewportOffsets[0]);
654
655 glDrawArrays(GL_TRIANGLES, 0, 3);
656 EXPECT_GL_NO_ERROR();
657
658 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
659 EXPECT_GL_NO_ERROR();
660 }
Martin Radevda8e2572017-09-12 17:21:16 +0300661}
Martin Radev7cf61662017-07-26 17:10:53 +0300662
Martin Radevda8e2572017-09-12 17:21:16 +0300663// The test verifies that glDraw* generates an INVALID_OPERATION error if the program does not use
664// the multiview extension, but the active draw framebuffer has more than one view.
665TEST_P(MultiviewDrawValidationTest, NumViewsMismatchForNonMultiviewProgram)
666{
667 if (!requestMultiviewExtension())
Martin Radev7cf61662017-07-26 17:10:53 +0300668 {
Martin Radevda8e2572017-09-12 17:21:16 +0300669 return;
Martin Radev7cf61662017-07-26 17:10:53 +0300670 }
Martin Radevda8e2572017-09-12 17:21:16 +0300671
672 const std::string &vsSourceNoMultiview =
673 "#version 300 es\n"
674 "void main()\n"
675 "{}\n";
676 const std::string &fsSourceNoMultiview =
677 "#version 300 es\n"
678 "precision mediump float;\n"
679 "void main()\n"
680 "{}\n";
681 ANGLE_GL_PROGRAM(programNoMultiview, vsSourceNoMultiview, fsSourceNoMultiview);
682 glUseProgram(programNoMultiview);
683
684 const GLint viewportOffsets[4] = {0, 0, 2, 0};
685 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d, 0, 2,
686 &viewportOffsets[0]);
687
688 glDrawArrays(GL_TRIANGLES, 0, 3);
689 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
690
691 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
692 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
Martin Radev7cf61662017-07-26 17:10:53 +0300693}
694
Martin Radev7e69f762017-07-27 14:54:13 +0300695// The test verifies that glDraw*:
696// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer is
697// greater than 1 and there is an active transform feedback object.
698// 2) does not generate any error if the number of views in the draw framebuffer is 1.
699TEST_P(MultiviewDrawValidationTest, ActiveTransformFeedback)
700{
701 if (!requestMultiviewExtension())
702 {
703 return;
704 }
705
706 const GLint viewportOffsets[4] = {0, 0, 2, 0};
707
708 const std::string &vsSource =
Olli Etuaho3755c482017-10-13 15:40:26 +0300709 R"(#version 300 es
710 out float tfVarying;
711 void main()
712 {
713 tfVarying = 1.0;
714 })";
Martin Radev7e69f762017-07-27 14:54:13 +0300715 const std::string &fsSource =
Olli Etuaho3755c482017-10-13 15:40:26 +0300716 R"(#version 300 es
717 precision mediump float;
718 void main()
719 {})";
720 std::vector<std::string> tfVaryings;
721 tfVaryings.push_back(std::string("tfVarying"));
722 ANGLE_GL_PROGRAM_TRANSFORM_FEEDBACK(program, vsSource, fsSource, tfVaryings,
723 GL_SEPARATE_ATTRIBS);
Martin Radev7e69f762017-07-27 14:54:13 +0300724 glUseProgram(program);
725
726 GLBuffer tbo;
727 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tbo);
728 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 4u, nullptr, GL_STATIC_DRAW);
729
730 GLTransformFeedback transformFeedback;
731 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedback);
Olli Etuaho3755c482017-10-13 15:40:26 +0300732
733 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo);
734
Martin Radev7e69f762017-07-27 14:54:13 +0300735 glBeginTransformFeedback(GL_TRIANGLES);
736 ASSERT_GL_NO_ERROR();
737
738 // Check that drawArrays generates an error when there is an active transform feedback object
739 // and the number of views in the draw framebuffer is greater than 1.
740 {
741 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
742 0, 2, &viewportOffsets[0]);
743 glDrawArrays(GL_TRIANGLES, 0, 3);
744 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
745 }
746
747 // Check that drawArrays does not generate an error when the number of views in the draw
748 // framebuffer is 1.
749 {
750 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
751 0, 1, &viewportOffsets[0]);
752 glDrawArrays(GL_TRIANGLES, 0, 3);
753 EXPECT_GL_NO_ERROR();
754 }
755
756 glEndTransformFeedback();
757}
758
Martin Radevffe754b2017-07-31 10:38:07 +0300759// The test verifies that glDraw*:
760// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer is
761// greater than 1 and there is an active query for target GL_TIME_ELAPSED_EXT.
762// 2) does not generate any error if the number of views in the draw framebuffer is 1.
763TEST_P(MultiviewDrawValidationTest, ActiveTimeElapsedQuery)
764{
765 if (!requestMultiviewExtension())
766 {
767 return;
768 }
769
770 if (!extensionEnabled("GL_EXT_disjoint_timer_query"))
771 {
772 std::cout << "Test skipped because GL_EXT_disjoint_timer_query is not available."
773 << std::endl;
774 return;
775 }
776
777 const GLint viewportOffsets[4] = {0, 0, 2, 0};
778 const std::string &vsSource =
779 "#version 300 es\n"
780 "void main()\n"
781 "{}\n";
782 const std::string &fsSource =
783 "#version 300 es\n"
784 "precision mediump float;\n"
785 "void main()\n"
786 "{}\n";
787 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
788 glUseProgram(program);
789
790 GLuint query = 0u;
791 glGenQueriesEXT(1, &query);
792 glBeginQueryEXT(GL_TIME_ELAPSED_EXT, query);
793
794 // Check first case.
795 {
796 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
797 0, 2, &viewportOffsets[0]);
798 glDrawArrays(GL_TRIANGLES, 0, 3);
799 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
800 }
801
802 // Check second case.
803 {
804 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
805 0, 1, &viewportOffsets[0]);
806 glDrawArrays(GL_TRIANGLES, 0, 3);
807 EXPECT_GL_NO_ERROR();
808 }
809
810 glEndQueryEXT(GL_TIME_ELAPSED_EXT);
811 glDeleteQueries(1, &query);
812}
813
Martin Radev8f276e22017-05-30 12:05:52 +0300814// The test checks that glDrawArrays can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300815TEST_P(MultiviewRenderDualViewTest, DrawArrays)
Martin Radev8f276e22017-05-30 12:05:52 +0300816{
817 if (!requestMultiviewExtension())
818 {
819 return;
820 }
821 drawQuad(mProgram, "vPosition", 0.0f, 1.0f, true);
822 ASSERT_GL_NO_ERROR();
823
824 checkOutput();
825}
826
827// The test checks that glDrawElements can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300828TEST_P(MultiviewRenderDualViewTest, DrawElements)
Martin Radev8f276e22017-05-30 12:05:52 +0300829{
830 if (!requestMultiviewExtension())
831 {
832 return;
833 }
834 drawIndexedQuad(mProgram, "vPosition", 0.0f, 1.0f, true);
835 ASSERT_GL_NO_ERROR();
836
837 checkOutput();
838}
839
840// The test checks that glDrawRangeElements can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300841TEST_P(MultiviewRenderDualViewTest, DrawRangeElements)
Martin Radev8f276e22017-05-30 12:05:52 +0300842{
843 if (!requestMultiviewExtension())
844 {
845 return;
846 }
847 drawIndexedQuad(mProgram, "vPosition", 0.0f, 1.0f, true, true);
848 ASSERT_GL_NO_ERROR();
849
850 checkOutput();
851}
852
853// The test checks that glDrawArrays can be used to render into four views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300854TEST_P(MultiviewRenderTest, DrawArraysFourViews)
Martin Radev8f276e22017-05-30 12:05:52 +0300855{
856 if (!requestMultiviewExtension())
857 {
858 return;
859 }
860
861 const std::string vsSource =
862 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300863 "#extension GL_OVR_multiview : require\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300864 "layout(num_views = 4) in;\n"
865 "in vec4 vPosition;\n"
866 "void main()\n"
867 "{\n"
868 " if (gl_ViewID_OVR == 0u) {\n"
869 " gl_Position.x = vPosition.x*0.25 - 0.75;\n"
870 " } else if (gl_ViewID_OVR == 1u) {\n"
871 " gl_Position.x = vPosition.x*0.25 - 0.25;\n"
872 " } else if (gl_ViewID_OVR == 2u) {\n"
873 " gl_Position.x = vPosition.x*0.25 + 0.25;\n"
874 " } else {\n"
875 " gl_Position.x = vPosition.x*0.25 + 0.75;\n"
876 " }"
877 " gl_Position.yzw = vPosition.yzw;\n"
878 "}\n";
879
880 const std::string fsSource =
881 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300882 "#extension GL_OVR_multiview : require\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300883 "precision mediump float;\n"
884 "out vec4 col;\n"
885 "void main()\n"
886 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +0300887 " col = vec4(0,1,0,1);\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300888 "}\n";
889
Martin Radev3c25ad02017-08-22 17:36:53 +0300890 createFBO(4, 1, 4);
Martin Radev8f276e22017-05-30 12:05:52 +0300891 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev8f276e22017-05-30 12:05:52 +0300892
893 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
894 ASSERT_GL_NO_ERROR();
895
896 for (int i = 0; i < 4; ++i)
897 {
898 for (int j = 0; j < 4; ++j)
899 {
Martin Radev8f276e22017-05-30 12:05:52 +0300900 if (i == j)
901 {
Martin Radev67a8a012017-09-08 13:03:52 +0300902 EXPECT_EQ(GLColor::green, GetViewColor(j, 0, i));
Martin Radev8f276e22017-05-30 12:05:52 +0300903 }
904 else
905 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300906 EXPECT_EQ(GLColor::black, GetViewColor(j, 0, i));
Martin Radev8f276e22017-05-30 12:05:52 +0300907 }
908 }
909 }
910 EXPECT_GL_NO_ERROR();
911}
912
913// The test checks that glDrawArraysInstanced can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300914TEST_P(MultiviewRenderTest, DrawArraysInstanced)
Martin Radev8f276e22017-05-30 12:05:52 +0300915{
916 if (!requestMultiviewExtension())
917 {
918 return;
919 }
920
921 const std::string vsSource =
922 "#version 300 es\n"
923 "#extension GL_OVR_multiview : require\n"
924 "layout(num_views = 2) in;\n"
925 "in vec4 vPosition;\n"
926 "void main()\n"
927 "{\n"
928 " vec4 p = vPosition;\n"
929 " if (gl_InstanceID == 1){\n"
930 " p.y = .5*p.y + .5;\n"
931 " } else {\n"
932 " p.y = p.y*.5;\n"
933 " }\n"
934 " gl_Position.x = (gl_ViewID_OVR == 0u ? p.x*0.5 + 0.5 : p.x*0.5);\n"
935 " gl_Position.yzw = p.yzw;\n"
936 "}\n";
937
938 const std::string fsSource =
939 "#version 300 es\n"
940 "#extension GL_OVR_multiview : require\n"
941 "precision mediump float;\n"
942 "out vec4 col;\n"
943 "void main()\n"
944 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +0300945 " col = vec4(0,1,0,1);\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300946 "}\n";
947
Martin Radev3c25ad02017-08-22 17:36:53 +0300948 const int kViewWidth = 2;
949 const int kViewHeight = 2;
950 const int kNumViews = 2;
951 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev8f276e22017-05-30 12:05:52 +0300952 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev8f276e22017-05-30 12:05:52 +0300953
Martin Radev67a8a012017-09-08 13:03:52 +0300954 drawQuadInstanced(program, "vPosition", 0.0f, 1.0f, true, 2u);
Martin Radev8f276e22017-05-30 12:05:52 +0300955 ASSERT_GL_NO_ERROR();
956
Martin Radev67a8a012017-09-08 13:03:52 +0300957 const GLubyte expectedGreenChannel[kNumViews][kViewHeight][kViewWidth] = {{{0, 255}, {0, 255}},
958 {{255, 0}, {255, 0}}};
Martin Radev3c25ad02017-08-22 17:36:53 +0300959
960 for (int view = 0; view < 2; ++view)
Martin Radev8f276e22017-05-30 12:05:52 +0300961 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300962 for (int y = 0; y < 2; ++y)
Martin Radev8f276e22017-05-30 12:05:52 +0300963 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300964 for (int x = 0; x < 2; ++x)
965 {
Martin Radev67a8a012017-09-08 13:03:52 +0300966 EXPECT_EQ(GLColor(0, expectedGreenChannel[view][y][x], 0, 255),
Martin Radev3c25ad02017-08-22 17:36:53 +0300967 GetViewColor(x, y, view));
968 }
Martin Radev8f276e22017-05-30 12:05:52 +0300969 }
970 }
971}
972
Martin Radev553590a2017-07-31 16:40:39 +0300973// The test verifies that the attribute divisor is correctly adjusted when drawing with a multi-view
974// program. The test draws 4 instances of a quad each of which covers a single pixel. The x and y
975// offset of each quad are passed as separate attributes which are indexed based on the
976// corresponding attribute divisors. A divisor of 1 is used for the y offset to have all quads
977// drawn vertically next to each other. A divisor of 3 is used for the x offset to have the last
978// quad offsetted by one pixel to the right. Note that the number of views is divisible by 1, but
979// not by 3.
Martin Radev3c25ad02017-08-22 17:36:53 +0300980TEST_P(MultiviewRenderTest, AttribDivisor)
Martin Radev553590a2017-07-31 16:40:39 +0300981{
982 if (!requestMultiviewExtension())
983 {
984 return;
985 }
986
987 const std::string &vsSource =
988 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300989 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +0300990 "layout(num_views = 2) in;\n"
991 "in vec3 vPosition;\n"
992 "in float offsetX;\n"
993 "in float offsetY;\n"
994 "void main()\n"
995 "{\n"
996 " vec4 p = vec4(vPosition, 1.);\n"
Martin Radev0abb7a22017-08-28 15:34:45 +0300997 " p.xy = p.xy * 0.25 - vec2(0.75) + vec2(offsetX, offsetY);\n"
Martin Radev553590a2017-07-31 16:40:39 +0300998 " gl_Position.x = (gl_ViewID_OVR == 0u ? p.x : p.x + 1.0);\n"
999 " gl_Position.yzw = p.yzw;\n"
1000 "}\n";
1001
1002 const std::string &fsSource =
1003 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001004 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +03001005 "precision mediump float;\n"
1006 "out vec4 col;\n"
1007 "void main()\n"
1008 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +03001009 " col = vec4(0,1,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +03001010 "}\n";
Martin Radev3c25ad02017-08-22 17:36:53 +03001011
1012 const int kViewWidth = 4;
1013 const int kViewHeight = 4;
1014 const int kNumViews = 2;
1015 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev553590a2017-07-31 16:40:39 +03001016 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev553590a2017-07-31 16:40:39 +03001017
1018 GLBuffer xOffsetVBO;
1019 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1020 const GLfloat xOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.0f};
1021 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, xOffsetData, GL_STATIC_DRAW);
1022 GLint xOffsetLoc = glGetAttribLocation(program, "offsetX");
1023 glVertexAttribPointer(xOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1024 glVertexAttribDivisor(xOffsetLoc, 3);
1025 glEnableVertexAttribArray(xOffsetLoc);
1026
1027 GLBuffer yOffsetVBO;
1028 glBindBuffer(GL_ARRAY_BUFFER, yOffsetVBO);
1029 const GLfloat yOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.5f};
1030 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, yOffsetData, GL_STATIC_DRAW);
1031 GLint yOffsetLoc = glGetAttribLocation(program, "offsetY");
1032 glVertexAttribDivisor(yOffsetLoc, 1);
1033 glVertexAttribPointer(yOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1034 glEnableVertexAttribArray(yOffsetLoc);
1035
Martin Radev67a8a012017-09-08 13:03:52 +03001036 drawQuadInstanced(program, "vPosition", 0.0f, 1.0f, true, 4u);
Martin Radev553590a2017-07-31 16:40:39 +03001037 ASSERT_GL_NO_ERROR();
1038
Martin Radev67a8a012017-09-08 13:03:52 +03001039 const GLubyte expectedGreenChannel[kNumViews][kViewHeight][kViewWidth] = {
Martin Radev3c25ad02017-08-22 17:36:53 +03001040 {{255, 0, 0, 0}, {255, 0, 0, 0}, {255, 0, 0, 0}, {0, 255, 0, 0}},
1041 {{0, 0, 255, 0}, {0, 0, 255, 0}, {0, 0, 255, 0}, {0, 0, 0, 255}}};
1042 for (int view = 0; view < 2; ++view)
Martin Radev553590a2017-07-31 16:40:39 +03001043 {
Martin Radev3c25ad02017-08-22 17:36:53 +03001044 for (int row = 0; row < 4; ++row)
Martin Radev553590a2017-07-31 16:40:39 +03001045 {
Martin Radev3c25ad02017-08-22 17:36:53 +03001046 for (int col = 0; col < 4; ++col)
1047 {
Martin Radev67a8a012017-09-08 13:03:52 +03001048 EXPECT_EQ(GLColor(0, expectedGreenChannel[view][row][col], 0, 255),
Martin Radev3c25ad02017-08-22 17:36:53 +03001049 GetViewColor(col, row, view));
1050 }
Martin Radev553590a2017-07-31 16:40:39 +03001051 }
1052 }
1053}
1054
1055// Test that different sequences of vertexAttribDivisor, useProgram and bindVertexArray in a
1056// multi-view context propagate the correct divisor to the driver.
Martin Radev3c25ad02017-08-22 17:36:53 +03001057TEST_P(MultiviewRenderTest, DivisorOrderOfOperation)
Martin Radev553590a2017-07-31 16:40:39 +03001058{
1059 if (!requestMultiviewExtension())
1060 {
1061 return;
1062 }
1063
Martin Radev3c25ad02017-08-22 17:36:53 +03001064 createFBO(1, 1, 2);
Martin Radev553590a2017-07-31 16:40:39 +03001065
1066 // Create multiview program.
1067 const std::string &vs =
1068 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001069 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +03001070 "layout(num_views = 2) in;\n"
1071 "layout(location = 0) in vec2 vPosition;\n"
1072 "layout(location = 1) in float offsetX;\n"
1073 "void main()\n"
1074 "{\n"
1075 " vec4 p = vec4(vPosition, 0.0, 1.0);\n"
1076 " p.x += offsetX;\n"
1077 " gl_Position = p;\n"
1078 "}\n";
1079
1080 const std::string &fs =
1081 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001082 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +03001083 "precision mediump float;\n"
1084 "out vec4 col;\n"
1085 "void main()\n"
1086 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +03001087 " col = vec4(0,1,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +03001088 "}\n";
1089
1090 ANGLE_GL_PROGRAM(program, vs, fs);
1091
1092 const std::string &dummyVS =
1093 "#version 300 es\n"
1094 "layout(location = 0) in vec2 vPosition;\n"
1095 "layout(location = 1) in float offsetX;\n"
1096 "void main()\n"
1097 "{\n"
1098 " gl_Position = vec4(vPosition, 0.0, 1.0);\n"
1099 "}\n";
1100
1101 const std::string &dummyFS =
1102 "#version 300 es\n"
1103 "precision mediump float;\n"
1104 "out vec4 col;\n"
1105 "void main()\n"
1106 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001107 " col = vec4(0,0,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +03001108 "}\n";
1109
1110 ANGLE_GL_PROGRAM(dummyProgram, dummyVS, dummyFS);
1111
1112 GLBuffer xOffsetVBO;
1113 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1114 const GLfloat xOffsetData[12] = {0.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f,
1115 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f};
1116 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, xOffsetData, GL_STATIC_DRAW);
1117
1118 GLBuffer vertexVBO;
1119 glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
1120 Vector2 kQuadVertices[6] = {Vector2(-1.f, -1.f), Vector2(1.f, -1.f), Vector2(1.f, 1.f),
1121 Vector2(-1.f, -1.f), Vector2(1.f, 1.f), Vector2(-1.f, 1.f)};
1122 glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
1123
1124 GLVertexArray vao[2];
1125 for (size_t i = 0u; i < 2u; ++i)
1126 {
1127 glBindVertexArray(vao[i]);
1128
1129 glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
1130 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
1131 glEnableVertexAttribArray(0);
1132
1133 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1134 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
1135 glEnableVertexAttribArray(1);
1136 }
1137 ASSERT_GL_NO_ERROR();
1138
1139 glViewport(0, 0, 1, 1);
1140 glScissor(0, 0, 1, 1);
Martin Radeveef80e42017-08-11 14:44:57 +03001141 glEnable(GL_SCISSOR_TEST);
Martin Radev61bd9992017-08-11 13:10:55 +03001142 glClearColor(0, 0, 0, 1);
Martin Radev553590a2017-07-31 16:40:39 +03001143
1144 // Clear the buffers, propagate divisor to the driver, bind the vao and keep it active.
1145 // It is necessary to call draw, so that the divisor is propagated and to guarantee that dirty
1146 // bits are cleared.
1147 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001148 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1149 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001150 glBindVertexArray(vao[0]);
1151 glVertexAttribDivisor(1, 0);
1152 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
1153 glUseProgram(0);
Martin Radev553590a2017-07-31 16:40:39 +03001154 ASSERT_GL_NO_ERROR();
1155
1156 // Check that vertexAttribDivisor uses the number of views to update the divisor.
Martin Radevda8e2572017-09-12 17:21:16 +03001157 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
1158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001159 glUseProgram(program);
1160 glVertexAttribDivisor(1, 1);
1161 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev67a8a012017-09-08 13:03:52 +03001162 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 0));
1163 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001164
1165 // Clear the buffers and propagate divisor to the driver.
1166 // We keep the vao active and propagate the divisor to guarantee that there are no unresolved
1167 // dirty bits when useProgram is called.
1168 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001169 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1170 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001171 glVertexAttribDivisor(1, 1);
1172 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
1173 glUseProgram(0);
Martin Radev553590a2017-07-31 16:40:39 +03001174 ASSERT_GL_NO_ERROR();
1175
1176 // Check that useProgram uses the number of views to update the divisor.
Martin Radevda8e2572017-09-12 17:21:16 +03001177 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
1178 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001179 glUseProgram(program);
1180 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev67a8a012017-09-08 13:03:52 +03001181 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 0));
1182 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001183
1184 // We go through similar steps as before.
1185 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001186 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1187 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001188 glVertexAttribDivisor(1, 1);
1189 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
1190 glUseProgram(0);
Martin Radev553590a2017-07-31 16:40:39 +03001191 ASSERT_GL_NO_ERROR();
1192
1193 // Check that bindVertexArray uses the number of views to update the divisor.
1194 {
1195 // Call useProgram with vao[1] being active to guarantee that useProgram will adjust the
1196 // divisor for vao[1] only.
Martin Radevda8e2572017-09-12 17:21:16 +03001197 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
1198 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001199 glBindVertexArray(vao[1]);
1200 glUseProgram(program);
1201 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev553590a2017-07-31 16:40:39 +03001202 glBindVertexArray(0);
1203 ASSERT_GL_NO_ERROR();
1204 }
1205 // Bind vao[0] after useProgram is called to ensure that bindVertexArray is the call which
1206 // adjusts the divisor.
Martin Radevda8e2572017-09-12 17:21:16 +03001207 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001208 glBindVertexArray(vao[0]);
1209 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev67a8a012017-09-08 13:03:52 +03001210 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 0));
1211 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001212}
1213
Martin Radev0d671c92017-08-10 16:41:52 +03001214// Test that no fragments pass the occlusion query for a multi-view vertex shader which always
1215// transforms geometry to be outside of the clip region.
Martin Radev3c25ad02017-08-22 17:36:53 +03001216TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryNothingVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001217{
Geoff Lang8c5b31c2017-09-26 18:07:44 -04001218 ANGLE_SKIP_TEST_IF(!requestMultiviewExtension());
1219 ANGLE_SKIP_TEST_IF(!requestOcclusionQueryExtension());
Martin Radev0d671c92017-08-10 16:41:52 +03001220
1221 const std::string vsSource =
1222 "#version 300 es\n"
1223 "#extension GL_OVR_multiview : require\n"
1224 "layout(num_views = 2) in;\n"
1225 "in vec3 vPosition;\n"
1226 "void main()\n"
1227 "{\n"
1228 " gl_Position.x = 2.0;\n"
1229 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1230 "}\n";
1231
1232 const std::string fsSource =
1233 "#version 300 es\n"
1234 "#extension GL_OVR_multiview : require\n"
1235 "precision mediump float;\n"
1236 "out vec4 col;\n"
1237 "void main()\n"
1238 "{\n"
1239 " col = vec4(1,0,0,0);\n"
1240 "}\n";
1241 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev3c25ad02017-08-22 17:36:53 +03001242 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001243
1244 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1245 ASSERT_GL_NO_ERROR();
1246 EXPECT_GL_FALSE(result);
1247}
1248
1249// Test that there are fragments passing the occlusion query if only view 0 can produce
1250// output.
Martin Radev3c25ad02017-08-22 17:36:53 +03001251TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryOnlyLeftVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001252{
Geoff Lang8c5b31c2017-09-26 18:07:44 -04001253 ANGLE_SKIP_TEST_IF(!requestMultiviewExtension());
1254 ANGLE_SKIP_TEST_IF(!requestOcclusionQueryExtension());
Martin Radev0d671c92017-08-10 16:41:52 +03001255
1256 const std::string vsSource =
1257 "#version 300 es\n"
1258 "#extension GL_OVR_multiview : require\n"
1259 "layout(num_views = 2) in;\n"
1260 "in vec3 vPosition;\n"
1261 "void main()\n"
1262 "{\n"
1263 " gl_Position.x = gl_ViewID_OVR == 0u ? vPosition.x : 2.0;\n"
1264 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1265 "}\n";
1266
1267 const std::string fsSource =
1268 "#version 300 es\n"
1269 "#extension GL_OVR_multiview : require\n"
1270 "precision mediump float;\n"
1271 "out vec4 col;\n"
1272 "void main()\n"
1273 "{\n"
1274 " col = vec4(1,0,0,0);\n"
1275 "}\n";
1276 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev3c25ad02017-08-22 17:36:53 +03001277 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001278
1279 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1280 ASSERT_GL_NO_ERROR();
1281 EXPECT_GL_TRUE(result);
1282}
1283
1284// Test that there are fragments passing the occlusion query if only view 1 can produce
1285// output.
Martin Radev3c25ad02017-08-22 17:36:53 +03001286TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryOnlyRightVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001287{
Geoff Lang8c5b31c2017-09-26 18:07:44 -04001288 ANGLE_SKIP_TEST_IF(!requestMultiviewExtension());
1289 ANGLE_SKIP_TEST_IF(!requestOcclusionQueryExtension());
Martin Radev0d671c92017-08-10 16:41:52 +03001290
1291 const std::string vsSource =
1292 "#version 300 es\n"
1293 "#extension GL_OVR_multiview : require\n"
1294 "layout(num_views = 2) in;\n"
1295 "in vec3 vPosition;\n"
1296 "void main()\n"
1297 "{\n"
1298 " gl_Position.x = gl_ViewID_OVR == 1u ? vPosition.x : 2.0;\n"
1299 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1300 "}\n";
1301
1302 const std::string fsSource =
1303 "#version 300 es\n"
1304 "#extension GL_OVR_multiview : require\n"
1305 "precision mediump float;\n"
1306 "out vec4 col;\n"
1307 "void main()\n"
1308 "{\n"
1309 " col = vec4(1,0,0,0);\n"
1310 "}\n";
1311 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev3c25ad02017-08-22 17:36:53 +03001312 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001313
1314 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1315 ASSERT_GL_NO_ERROR();
1316 EXPECT_GL_TRUE(result);
1317}
1318
Martin Radev41ac68e2017-06-06 12:16:58 +03001319// Test that a simple multi-view program which doesn't use gl_ViewID_OVR in neither VS nor FS
1320// compiles and links without an error.
1321TEST_P(MultiviewProgramGenerationTest, SimpleProgram)
1322{
1323 if (!requestMultiviewExtension())
1324 {
1325 return;
1326 }
1327
1328 const std::string vsSource =
1329 "#version 300 es\n"
1330 "#extension GL_OVR_multiview : require\n"
1331 "layout(num_views = 2) in;\n"
1332 "void main()\n"
1333 "{\n"
1334 "}\n";
1335
1336 const std::string fsSource =
1337 "#version 300 es\n"
1338 "#extension GL_OVR_multiview : require\n"
1339 "precision mediump float;\n"
1340 "void main()\n"
1341 "{\n"
1342 "}\n";
1343
1344 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1345 glUseProgram(program);
1346
1347 EXPECT_GL_NO_ERROR();
1348}
1349
1350// Test that a simple multi-view program which uses gl_ViewID_OVR only in VS compiles and links
1351// without an error.
1352TEST_P(MultiviewProgramGenerationTest, UseViewIDInVertexShader)
1353{
1354 if (!requestMultiviewExtension())
1355 {
1356 return;
1357 }
1358
1359 const std::string vsSource =
1360 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001361 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001362 "layout(num_views = 2) in;\n"
1363 "void main()\n"
1364 "{\n"
1365 " if (gl_ViewID_OVR == 0u) {\n"
1366 " gl_Position = vec4(1,0,0,1);\n"
1367 " } else {\n"
1368 " gl_Position = vec4(-1,0,0,1);\n"
1369 " }\n"
1370 "}\n";
1371
1372 const std::string fsSource =
1373 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001374 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001375 "precision mediump float;\n"
1376 "void main()\n"
1377 "{\n"
1378 "}\n";
1379
1380 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1381 glUseProgram(program);
1382
1383 EXPECT_GL_NO_ERROR();
1384}
1385
1386// Test that a simple multi-view program which uses gl_ViewID_OVR only in FS compiles and links
1387// without an error.
1388TEST_P(MultiviewProgramGenerationTest, UseViewIDInFragmentShader)
1389{
1390 if (!requestMultiviewExtension())
1391 {
1392 return;
1393 }
1394
1395 const std::string vsSource =
1396 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001397 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001398 "layout(num_views = 2) in;\n"
1399 "void main()\n"
1400 "{\n"
1401 "}\n";
1402
1403 const std::string fsSource =
1404 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001405 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001406 "precision mediump float;\n"
1407 "out vec4 col;\n"
1408 "void main()\n"
1409 "{\n"
1410 " if (gl_ViewID_OVR == 0u) {\n"
1411 " col = vec4(1,0,0,1);\n"
1412 " } else {\n"
1413 " col = vec4(-1,0,0,1);\n"
1414 " }\n"
1415 "}\n";
1416
1417 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1418 glUseProgram(program);
1419
1420 EXPECT_GL_NO_ERROR();
1421}
1422
Martin Radev61bd9992017-08-11 13:10:55 +03001423// The test checks that GL_POINTS is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001424TEST_P(MultiviewRenderPrimitiveTest, Points)
Martin Radev61bd9992017-08-11 13:10:55 +03001425{
1426 if (!requestMultiviewExtension())
1427 {
1428 return;
1429 }
1430
1431 const std::string vsSource =
1432 "#version 300 es\n"
1433 "#extension GL_OVR_multiview : require\n"
1434 "layout(num_views = 2) in;\n"
1435 "layout(location=0) in vec2 vPosition;\n"
1436 "void main()\n"
1437 "{\n"
1438 " gl_PointSize = 1.0;\n"
1439 " gl_Position = vec4(vPosition.xy, 0.0, 1.0);\n"
1440 "}\n";
1441
1442 const std::string fsSource =
1443 "#version 300 es\n"
1444 "#extension GL_OVR_multiview : require\n"
1445 "precision mediump float;\n"
1446 "out vec4 col;\n"
1447 "void main()\n"
1448 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +03001449 " col = vec4(0,1,0,1);\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001450 "}\n";
1451 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1452 glUseProgram(program);
1453
Martin Radev3c25ad02017-08-22 17:36:53 +03001454 const int kViewWidth = 4;
1455 const int kViewHeight = 2;
1456 const int kNumViews = 2;
1457 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001458
1459 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 1)};
1460 std::vector<Vector2> vertexDataInClipSpace =
1461 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1462 setupGeometry(vertexDataInClipSpace);
1463
1464 glDrawArrays(GL_POINTS, 0, 2);
1465
Martin Radev67a8a012017-09-08 13:03:52 +03001466 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
Martin Radev3c25ad02017-08-22 17:36:53 +03001467 {{255, 0, 0, 0}, {0, 0, 0, 255}}, {{255, 0, 0, 0}, {0, 0, 0, 255}}};
Martin Radev67a8a012017-09-08 13:03:52 +03001468 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001469}
1470
1471// The test checks that GL_LINES is correctly rendered.
1472// The behavior of this test is not guaranteed by the spec:
1473// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1474// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1475// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1476// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001477TEST_P(MultiviewRenderPrimitiveTest, Lines)
Martin Radev61bd9992017-08-11 13:10:55 +03001478{
1479 if (!requestMultiviewExtension())
1480 {
1481 return;
1482 }
1483
Martin Radevced5c862017-08-17 16:05:29 +03001484 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001485 ASSERT_NE(program, 0u);
1486 glUseProgram(program);
1487 ASSERT_GL_NO_ERROR();
1488
Martin Radev3c25ad02017-08-22 17:36:53 +03001489 const int kViewWidth = 4;
1490 const int kViewHeight = 2;
1491 const int kNumViews = 2;
1492 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001493
1494 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(4, 0)};
1495 std::vector<Vector2> vertexDataInClipSpace =
1496 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1497 setupGeometry(vertexDataInClipSpace);
1498
1499 glDrawArrays(GL_LINES, 0, 2);
1500
Martin Radev67a8a012017-09-08 13:03:52 +03001501 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
Martin Radev3c25ad02017-08-22 17:36:53 +03001502 {{255, 255, 255, 255}, {0, 0, 0, 0}}, {{255, 255, 255, 255}, {0, 0, 0, 0}}};
Martin Radev67a8a012017-09-08 13:03:52 +03001503 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001504
1505 glDeleteProgram(program);
1506}
1507
1508// The test checks that GL_LINE_STRIP is correctly rendered.
1509// The behavior of this test is not guaranteed by the spec:
1510// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1511// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1512// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1513// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001514TEST_P(MultiviewRenderPrimitiveTest, LineStrip)
Martin Radev61bd9992017-08-11 13:10:55 +03001515{
1516 if (!requestMultiviewExtension())
1517 {
1518 return;
1519 }
1520
Martin Radevced5c862017-08-17 16:05:29 +03001521 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001522 ASSERT_NE(program, 0u);
1523 glUseProgram(program);
1524 ASSERT_GL_NO_ERROR();
1525
Martin Radev3c25ad02017-08-22 17:36:53 +03001526 const int kViewWidth = 4;
1527 const int kViewHeight = 2;
1528 const int kNumViews = 2;
1529 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001530
1531 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 0), Vector2I(3, 2)};
1532 std::vector<Vector2> vertexDataInClipSpace =
1533 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1534 setupGeometry(vertexDataInClipSpace);
1535
1536 glDrawArrays(GL_LINE_STRIP, 0, 3);
1537
Martin Radev67a8a012017-09-08 13:03:52 +03001538 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
Martin Radev3c25ad02017-08-22 17:36:53 +03001539 {{255, 255, 255, 255}, {0, 0, 0, 255}}, {{255, 255, 255, 255}, {0, 0, 0, 255}}};
Martin Radev67a8a012017-09-08 13:03:52 +03001540 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001541
1542 glDeleteProgram(program);
1543}
1544
1545// The test checks that GL_LINE_LOOP is correctly rendered.
1546// The behavior of this test is not guaranteed by the spec:
1547// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1548// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1549// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1550// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001551TEST_P(MultiviewRenderPrimitiveTest, LineLoop)
Martin Radev61bd9992017-08-11 13:10:55 +03001552{
1553 if (!requestMultiviewExtension())
1554 {
1555 return;
1556 }
1557
Martin Radevced5c862017-08-17 16:05:29 +03001558 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001559 ASSERT_NE(program, 0u);
1560 glUseProgram(program);
1561 ASSERT_GL_NO_ERROR();
1562
Martin Radev3c25ad02017-08-22 17:36:53 +03001563 const int kViewWidth = 4;
1564 const int kViewHeight = 4;
1565 const int kNumViews = 2;
1566 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001567
1568 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 0), Vector2I(3, 3),
1569 Vector2I(0, 3)};
1570 std::vector<Vector2> vertexDataInClipSpace =
1571 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 4);
1572 setupGeometry(vertexDataInClipSpace);
1573
1574 glDrawArrays(GL_LINE_LOOP, 0, 4);
1575
Martin Radev67a8a012017-09-08 13:03:52 +03001576 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
Martin Radev3c25ad02017-08-22 17:36:53 +03001577 {{255, 255, 255, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 255, 255, 255}},
1578 {{255, 255, 255, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 255, 255, 255}}};
Martin Radev67a8a012017-09-08 13:03:52 +03001579 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001580
1581 glDeleteProgram(program);
1582}
1583
1584// The test checks that GL_TRIANGLE_STRIP is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001585TEST_P(MultiviewRenderPrimitiveTest, TriangleStrip)
Martin Radev61bd9992017-08-11 13:10:55 +03001586{
1587 if (!requestMultiviewExtension())
1588 {
1589 return;
1590 }
1591
Martin Radevced5c862017-08-17 16:05:29 +03001592 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001593 ASSERT_NE(program, 0u);
1594 glUseProgram(program);
1595 ASSERT_GL_NO_ERROR();
1596
1597 std::vector<Vector2> vertexDataInClipSpace = {Vector2(1.0f, 0.0f), Vector2(0.0f, 0.0f),
1598 Vector2(1.0f, 1.0f), Vector2(0.0f, 1.0f)};
1599 setupGeometry(vertexDataInClipSpace);
1600
Martin Radev3c25ad02017-08-22 17:36:53 +03001601 const int kViewWidth = 2;
1602 const int kViewHeight = 2;
1603 const int kNumViews = 2;
1604 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001605
1606 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1607
Martin Radev67a8a012017-09-08 13:03:52 +03001608 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
1609 {{0, 0}, {0, 255}}, {{0, 0}, {0, 255}}};
1610 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001611
1612 glDeleteProgram(program);
1613}
1614
1615// The test checks that GL_TRIANGLE_FAN is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001616TEST_P(MultiviewRenderPrimitiveTest, TriangleFan)
Martin Radev61bd9992017-08-11 13:10:55 +03001617{
1618 if (!requestMultiviewExtension())
1619 {
1620 return;
1621 }
1622
Martin Radevced5c862017-08-17 16:05:29 +03001623 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001624 ASSERT_NE(program, 0u);
1625 glUseProgram(program);
1626 ASSERT_GL_NO_ERROR();
1627
1628 std::vector<Vector2> vertexDataInClipSpace = {Vector2(0.0f, 0.0f), Vector2(0.0f, 1.0f),
1629 Vector2(1.0f, 1.0f), Vector2(1.0f, 0.0f)};
1630 setupGeometry(vertexDataInClipSpace);
1631
Martin Radev3c25ad02017-08-22 17:36:53 +03001632 const int kViewWidth = 2;
1633 const int kViewHeight = 2;
1634 const int kNumViews = 2;
1635 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001636
1637 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1638
Martin Radev67a8a012017-09-08 13:03:52 +03001639 const GLubyte expectedGreenChannelData[kNumViews][kViewHeight][kViewWidth] = {
1640 {{0, 0}, {0, 255}}, {{0, 0}, {0, 255}}};
1641 checkGreenChannel(expectedGreenChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001642
1643 glDeleteProgram(program);
1644}
1645
1646// Test that rendering enlarged points and lines does not leak fragments outside of the views'
1647// bounds. The test does not rely on the actual line width being greater than 1.0.
Martin Radev3c25ad02017-08-22 17:36:53 +03001648TEST_P(MultiviewSideBySideRenderTest, NoLeakingFragments)
Martin Radev61bd9992017-08-11 13:10:55 +03001649{
1650 if (!requestMultiviewExtension())
1651 {
1652 return;
1653 }
1654
Martin Radev3c25ad02017-08-22 17:36:53 +03001655 createFBO(2, 1, 2);
Martin Radev61bd9992017-08-11 13:10:55 +03001656
1657 GLint viewportOffsets[4] = {1, 0, 3, 0};
1658 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1659 mColorTexture, 0, 2, &viewportOffsets[0]);
1660 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
1661 mDepthTexture, 0, 2, &viewportOffsets[0]);
1662
1663 glViewport(0, 0, 1, 1);
1664 glScissor(0, 0, 1, 1);
1665 glEnable(GL_SCISSOR_TEST);
1666
1667 const std::string vsSource =
1668 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001669 "#extension GL_OVR_multiview : require\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001670 "layout(num_views = 2) in;\n"
1671 "layout(location=0) in vec2 vPosition;\n"
1672 "void main()\n"
1673 "{\n"
1674 " gl_PointSize = 10.0;\n"
1675 " gl_Position = vec4(vPosition.xy, 0.0, 1.0);\n"
1676 "}\n";
1677
1678 const std::string fsSource =
1679 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001680 "#extension GL_OVR_multiview : require\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001681 "precision mediump float;\n"
1682 "out vec4 col;\n"
1683 "void main()\n"
1684 "{\n"
1685 " if (gl_ViewID_OVR == 0u) {\n"
1686 " col = vec4(1,0,0,1);\n"
1687 " } else {\n"
1688 " col = vec4(0,1,0,1);\n"
1689 " }\n"
1690 "}\n";
1691 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1692 glUseProgram(program);
1693
1694 const std::vector<Vector2I> &windowCoordinates = {Vector2I(0, 0), Vector2I(2, 0)};
1695 const std::vector<Vector2> &vertexDataInClipSpace =
1696 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 1, 1);
Martin Radev3c25ad02017-08-22 17:36:53 +03001697
1698 GLBuffer vbo;
1699 glBindBuffer(GL_ARRAY_BUFFER, vbo);
1700 glBufferData(GL_ARRAY_BUFFER, vertexDataInClipSpace.size() * sizeof(Vector2),
1701 vertexDataInClipSpace.data(), GL_STATIC_DRAW);
1702 glEnableVertexAttribArray(0);
1703 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
Martin Radev61bd9992017-08-11 13:10:55 +03001704
1705 // Test rendering points.
1706 {
1707 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1708 glDrawArrays(GL_POINTS, 0, 2);
1709 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
1710 EXPECT_PIXEL_COLOR_EQ(1, 0, GLColor::red);
1711 EXPECT_PIXEL_COLOR_EQ(2, 0, GLColor::black);
1712 EXPECT_PIXEL_COLOR_EQ(3, 0, GLColor::green);
1713 }
1714
1715 // Test rendering lines.
1716 {
1717 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1718 glLineWidth(10.f);
1719 glDrawArrays(GL_LINES, 0, 2);
1720 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
1721 EXPECT_PIXEL_COLOR_EQ(1, 0, GLColor::red);
1722 EXPECT_PIXEL_COLOR_EQ(2, 0, GLColor::black);
1723 EXPECT_PIXEL_COLOR_EQ(3, 0, GLColor::green);
1724 }
1725}
1726
Martin Radev0abb7a22017-08-28 15:34:45 +03001727// Verify that re-linking a program adjusts the attribute divisor.
1728// The test uses instacing to draw for each view a strips of two red quads and two blue quads next
1729// to each other. The quads' position and color depend on the corresponding attribute divisors.
1730TEST_P(MultiviewRenderTest, ProgramRelinkUpdatesAttribDivisor)
1731{
1732 if (!requestMultiviewExtension())
1733 {
1734 return;
1735 }
1736
1737 const int kViewWidth = 4;
1738 const int kViewHeight = 1;
1739 const int kNumViews = 2;
1740
1741 const std::string &fsSource =
1742 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001743 "#extension GL_OVR_multiview : require\n"
Martin Radev0abb7a22017-08-28 15:34:45 +03001744 "precision mediump float;\n"
1745 "in vec4 oColor;\n"
1746 "out vec4 col;\n"
1747 "void main()\n"
1748 "{\n"
1749 " col = oColor;\n"
1750 "}\n";
1751
1752 auto generateVertexShaderSource = [](int numViews) -> std::string {
1753 std::string source =
1754 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001755 "#extension GL_OVR_multiview : require\n"
Martin Radev0abb7a22017-08-28 15:34:45 +03001756 "layout(num_views = " +
1757 ToString(numViews) +
1758 ") in;\n"
1759 "in vec3 vPosition;\n"
1760 "in float vOffsetX;\n"
1761 "in vec4 vColor;\n"
1762 "out vec4 oColor;\n"
1763 "void main()\n"
1764 "{\n"
1765 " vec4 p = vec4(vPosition, 1.);\n"
1766 " p.x = p.x * 0.25 - 0.75 + vOffsetX;\n"
1767 " oColor = vColor;\n"
1768 " gl_Position = p;\n"
1769 "}\n";
1770 return source;
1771 };
1772
1773 std::string vsSource = generateVertexShaderSource(kNumViews);
1774 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1775 glUseProgram(program);
1776
1777 GLint positionLoc;
1778 GLBuffer xOffsetVBO;
1779 GLint xOffsetLoc;
1780 GLBuffer colorVBO;
1781 GLint colorLoc;
1782
1783 {
1784 // Initialize buffers and setup attributes.
1785 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1786 const GLfloat kXOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.5f};
1787 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, kXOffsetData, GL_STATIC_DRAW);
1788 xOffsetLoc = glGetAttribLocation(program, "vOffsetX");
1789 glVertexAttribPointer(xOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1790 glVertexAttribDivisor(xOffsetLoc, 1);
1791 glEnableVertexAttribArray(xOffsetLoc);
1792
1793 glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
1794 const GLColor kColors[2] = {GLColor::red, GLColor::blue};
1795 glBufferData(GL_ARRAY_BUFFER, sizeof(GLColor) * 2, kColors, GL_STATIC_DRAW);
1796 colorLoc = glGetAttribLocation(program, "vColor");
1797 glVertexAttribDivisor(colorLoc, 2);
1798 glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, 0);
1799 glEnableVertexAttribArray(colorLoc);
1800
1801 positionLoc = glGetAttribLocation(program, "vPosition");
1802 }
1803
1804 {
1805 createFBO(kViewWidth, kViewHeight, kNumViews);
1806
Martin Radev67a8a012017-09-08 13:03:52 +03001807 drawQuadInstanced(program, "vPosition", 0.0f, 1.0f, true, 4u);
Martin Radev0abb7a22017-08-28 15:34:45 +03001808 ASSERT_GL_NO_ERROR();
1809
1810 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1811 EXPECT_EQ(GLColor::red, GetViewColor(1, 0, 0));
1812 EXPECT_EQ(GLColor::blue, GetViewColor(2, 0, 0));
1813 EXPECT_EQ(GLColor::blue, GetViewColor(3, 0, 0));
1814 }
1815
1816 {
1817 const int kNewNumViews = 3;
1818 vsSource = generateVertexShaderSource(kNewNumViews);
1819 createFBO(kViewWidth, kViewHeight, kNewNumViews);
1820
1821 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
1822 ASSERT_NE(0u, vs);
1823 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
1824 ASSERT_NE(0u, fs);
1825
1826 GLint numAttachedShaders = 0;
1827 glGetProgramiv(program, GL_ATTACHED_SHADERS, &numAttachedShaders);
1828
1829 GLuint attachedShaders[2] = {0u};
1830 glGetAttachedShaders(program, numAttachedShaders, nullptr, attachedShaders);
1831 for (int i = 0; i < 2; ++i)
1832 {
1833 glDetachShader(program, attachedShaders[i]);
1834 }
1835
1836 glAttachShader(program, vs);
1837 glDeleteShader(vs);
1838
1839 glAttachShader(program, fs);
1840 glDeleteShader(fs);
1841
1842 glBindAttribLocation(program, positionLoc, "vPosition");
1843 glBindAttribLocation(program, xOffsetLoc, "vOffsetX");
1844 glBindAttribLocation(program, colorLoc, "vColor");
1845
1846 glLinkProgram(program);
1847
Martin Radev67a8a012017-09-08 13:03:52 +03001848 drawQuadInstanced(program, "vPosition", 0.0f, 1.0f, true, 4u);
Martin Radev0abb7a22017-08-28 15:34:45 +03001849 ASSERT_GL_NO_ERROR();
1850
1851 for (int i = 0; i < kNewNumViews; ++i)
1852 {
1853 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, i));
1854 EXPECT_EQ(GLColor::red, GetViewColor(1, 0, i));
1855 EXPECT_EQ(GLColor::blue, GetViewColor(2, 0, i));
1856 EXPECT_EQ(GLColor::blue, GetViewColor(3, 0, i));
1857 }
1858 }
1859}
1860
Martin Radevced5c862017-08-17 16:05:29 +03001861// Test that useProgram applies the number of views in computing the final value of the attribute
1862// divisor.
1863TEST_P(MultiviewRenderTest, DivisorUpdatedOnProgramChange)
1864{
1865 if (!requestMultiviewExtension())
1866 {
1867 return;
1868 }
1869
1870 GLVertexArray vao;
1871 glBindVertexArray(vao);
1872 GLBuffer vbo;
1873 glBindBuffer(GL_ARRAY_BUFFER, vbo);
1874 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(1, 0), Vector2I(2, 0),
1875 Vector2I(3, 0)};
1876 std::vector<Vector2> vertexDataInClipSpace =
1877 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 1);
1878 // Fill with x positions so that the resulting clip space coordinate fails the clip test.
1879 glBufferData(GL_ARRAY_BUFFER, sizeof(Vector2) * vertexDataInClipSpace.size(),
1880 vertexDataInClipSpace.data(), GL_STATIC_DRAW);
1881 glEnableVertexAttribArray(0);
1882 glVertexAttribPointer(0, 2, GL_FLOAT, 0, 0, nullptr);
1883 glVertexAttribDivisor(0, 1);
1884 ASSERT_GL_NO_ERROR();
1885
1886 // Create a program and fbo with N views and draw N instances of a point horizontally.
1887 for (int numViews = 2; numViews <= 4; ++numViews)
1888 {
1889 createFBO(4, 1, numViews);
1890 ASSERT_GL_NO_ERROR();
1891
1892 GLuint program = CreateSimplePassthroughProgram(numViews);
1893 ASSERT_NE(program, 0u);
1894 glUseProgram(program);
1895 ASSERT_GL_NO_ERROR();
1896
1897 glDrawArraysInstanced(GL_POINTS, 0, 1, numViews);
1898
1899 for (int view = 0; view < numViews; ++view)
1900 {
1901 for (int j = 0; j < numViews; ++j)
1902 {
Martin Radev67a8a012017-09-08 13:03:52 +03001903 EXPECT_EQ(GLColor::green, GetViewColor(j, 0, view));
Martin Radevced5c862017-08-17 16:05:29 +03001904 }
1905 for (int j = numViews; j < 4; ++j)
1906 {
1907 EXPECT_EQ(GLColor::black, GetViewColor(j, 0, view));
1908 }
1909 }
1910
1911 glDeleteProgram(program);
1912 }
1913}
1914
Martin Radev72b4e1e2017-08-31 15:42:56 +03001915// The test checks that gl_ViewID_OVR is correctly propagated to the fragment shader.
1916TEST_P(MultiviewRenderTest, SelectColorBasedOnViewIDOVR)
1917{
1918 if (!requestMultiviewExtension())
1919 {
1920 return;
1921 }
1922
1923 const std::string vsSource =
1924 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001925 "#extension GL_OVR_multiview : require\n"
Martin Radev72b4e1e2017-08-31 15:42:56 +03001926 "layout(num_views = 3) in;\n"
1927 "in vec3 vPosition;\n"
1928 "void main()\n"
1929 "{\n"
1930 " gl_Position = vec4(vPosition, 1.);\n"
1931 "}\n";
1932
1933 const std::string fsSource =
1934 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001935 "#extension GL_OVR_multiview : require\n"
Martin Radev72b4e1e2017-08-31 15:42:56 +03001936 "precision mediump float;\n"
1937 "out vec4 col;\n"
1938 "void main()\n"
1939 "{\n"
1940 " if (gl_ViewID_OVR == 0u) {\n"
1941 " col = vec4(1,0,0,1);\n"
1942 " } else if (gl_ViewID_OVR == 1u) {\n"
1943 " col = vec4(0,1,0,1);\n"
1944 " } else if (gl_ViewID_OVR == 2u) {\n"
1945 " col = vec4(0,0,1,1);\n"
1946 " } else {\n"
1947 " col = vec4(0,0,0,0);\n"
1948 " }\n"
1949 "}\n";
1950
1951 createFBO(1, 1, 3);
1952 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev72b4e1e2017-08-31 15:42:56 +03001953
1954 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
1955 ASSERT_GL_NO_ERROR();
1956
1957 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1958 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
1959 EXPECT_EQ(GLColor::blue, GetViewColor(0, 0, 2));
1960}
1961
1962// The test checks that the inactive layers of a 2D texture array are not written to by a
1963// multi-view program.
1964TEST_P(MultiviewLayeredRenderTest, RenderToSubrageOfLayers)
1965{
1966 if (!requestMultiviewExtension())
1967 {
1968 return;
1969 }
1970
1971 const std::string vsSource =
1972 "#version 300 es\n"
1973 "#extension GL_OVR_multiview : require\n"
1974 "layout(num_views = 2) in;\n"
1975 "in vec3 vPosition;\n"
1976 "void main()\n"
1977 "{\n"
1978 " gl_Position = vec4(vPosition, 1.);\n"
1979 "}\n";
1980
1981 const std::string fsSource =
1982 "#version 300 es\n"
1983 "#extension GL_OVR_multiview : require\n"
1984 "precision mediump float;\n"
1985 "out vec4 col;\n"
1986 "void main()\n"
1987 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +03001988 " col = vec4(0,1,0,1);\n"
Martin Radev72b4e1e2017-08-31 15:42:56 +03001989 "}\n";
1990
1991 createFBO(1, 1, 2, 4, 1);
1992 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
Martin Radev72b4e1e2017-08-31 15:42:56 +03001993
1994 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
1995 ASSERT_GL_NO_ERROR();
1996
1997 EXPECT_EQ(GLColor::transparentBlack, GetViewColor(0, 0, 0));
Martin Radev67a8a012017-09-08 13:03:52 +03001998 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
1999 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 2));
Martin Radev72b4e1e2017-08-31 15:42:56 +03002000 EXPECT_EQ(GLColor::transparentBlack, GetViewColor(0, 0, 3));
2001}
2002
Martin Radevc1d4e552017-08-21 12:01:10 +03002003// The D3D11 renderer uses a GS whenever the varyings are flat interpolated which can cause
2004// potential bugs if the view is selected in the VS. The test contains a program in which the
2005// gl_InstanceID is passed as a flat varying to the fragment shader where it is used to discard the
2006// fragment if its value is negative. The gl_InstanceID should never be negative and that branch is
2007// never taken. One quad is drawn and the color is selected based on the ViewID - red for view 0 and
2008// green for view 1.
2009TEST_P(MultiviewRenderTest, FlatInterpolation)
Martin Radev3c25ad02017-08-22 17:36:53 +03002010{
Martin Radevc1d4e552017-08-21 12:01:10 +03002011 if (!requestMultiviewExtension())
2012 {
2013 return;
2014 }
2015
Yuly Novikov1de29ab2017-09-07 18:07:23 -04002016 // TODO(mradev): Find out why this fails on Win10 Intel HD 630 D3D11
2017 // (http://anglebug.com/2062)
2018 ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsD3D11());
2019
Martin Radevc1d4e552017-08-21 12:01:10 +03002020 const std::string vsSource =
2021 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03002022 "#extension GL_OVR_multiview : require\n"
Martin Radevc1d4e552017-08-21 12:01:10 +03002023 "layout(num_views = 2) in;\n"
2024 "in vec3 vPosition;\n"
2025 "flat out int oInstanceID;\n"
2026 "void main()\n"
2027 "{\n"
2028 " gl_Position = vec4(vPosition, 1.);\n"
2029 " oInstanceID = gl_InstanceID;\n"
2030 "}\n";
2031
2032 const std::string fsSource =
2033 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03002034 "#extension GL_OVR_multiview : require\n"
Martin Radevc1d4e552017-08-21 12:01:10 +03002035 "precision mediump float;\n"
2036 "flat in int oInstanceID;\n"
2037 "out vec4 col;\n"
2038 "void main()\n"
2039 "{\n"
2040 " if (oInstanceID < 0) {\n"
2041 " discard;\n"
2042 " }\n"
2043 " if (gl_ViewID_OVR == 0u) {\n"
2044 " col = vec4(1,0,0,1);\n"
2045 " } else {\n"
2046 " col = vec4(0,1,0,1);\n"
2047 " }\n"
2048 "}\n";
2049
2050 createFBO(1, 1, 2);
2051 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
2052
2053 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
2054 ASSERT_GL_NO_ERROR();
2055
2056 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
2057 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev3c25ad02017-08-22 17:36:53 +03002058}
2059
Martin Radev265a6d42017-09-12 16:51:37 +03002060// The test is added to cover a bug which resulted in the viewport/scissor and viewport offsets not
2061// being correctly applied.
2062TEST_P(MultiviewSideBySideRenderTest, ViewportOffsetsAppliedBugCoverage)
2063{
2064 if (!requestMultiviewExtension())
2065 {
2066 return;
2067 }
2068
2069 createFBO(1, 1, 2);
2070
2071 // Create multiview program.
2072 const std::string &vs =
2073 "#version 300 es\n"
2074 "#extension GL_OVR_multiview : require\n"
2075 "layout(num_views = 2) in;\n"
2076 "layout(location = 0) in vec3 vPosition;\n"
2077 "void main()\n"
2078 "{\n"
2079 " gl_Position = vec4(vPosition, 1.0);\n"
2080 "}\n";
2081
2082 const std::string &fs =
2083 "#version 300 es\n"
2084 "#extension GL_OVR_multiview : require\n"
2085 "precision mediump float;\n"
2086 "out vec4 col;\n"
2087 "void main()\n"
2088 "{\n"
Martin Radev67a8a012017-09-08 13:03:52 +03002089 " col = vec4(0,1,0,1);\n"
Martin Radev265a6d42017-09-12 16:51:37 +03002090 "}\n";
2091
2092 ANGLE_GL_PROGRAM(program, vs, fs);
2093
2094 glViewport(0, 0, 1, 1);
2095 glScissor(0, 0, 1, 1);
2096 glEnable(GL_SCISSOR_TEST);
2097 glClearColor(0, 0, 0, 1);
2098
2099 // Bind the default FBO and make sure that the state is synchronized.
2100 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2102 ASSERT_GL_NO_ERROR();
2103
2104 // Draw and check that both views are rendered to.
2105 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
2106 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev67a8a012017-09-08 13:03:52 +03002107
Martin Radev265a6d42017-09-12 16:51:37 +03002108 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
Martin Radev67a8a012017-09-08 13:03:52 +03002109 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 0));
2110 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev265a6d42017-09-12 16:51:37 +03002111}
2112
Martin Radevc1d4e552017-08-21 12:01:10 +03002113MultiviewImplementationParams VertexShaderOpenGL()
Martin Radev3c25ad02017-08-22 17:36:53 +03002114{
Martin Radevc1d4e552017-08-21 12:01:10 +03002115 return MultiviewImplementationParams(false, egl_platform::OPENGL());
Martin Radev3c25ad02017-08-22 17:36:53 +03002116}
2117
Martin Radevc1d4e552017-08-21 12:01:10 +03002118MultiviewImplementationParams VertexShaderD3D11()
Martin Radev3c25ad02017-08-22 17:36:53 +03002119{
Martin Radevc1d4e552017-08-21 12:01:10 +03002120 return MultiviewImplementationParams(false, egl_platform::D3D11());
Martin Radev3c25ad02017-08-22 17:36:53 +03002121}
2122
Martin Radevc1d4e552017-08-21 12:01:10 +03002123MultiviewImplementationParams GeomShaderD3D11()
Martin Radev72b4e1e2017-08-31 15:42:56 +03002124{
Martin Radevc1d4e552017-08-21 12:01:10 +03002125 return MultiviewImplementationParams(true, egl_platform::D3D11());
2126}
2127
2128MultiviewTestParams SideBySideVertexShaderOpenGL()
2129{
2130 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, VertexShaderOpenGL());
2131}
2132
2133MultiviewTestParams LayeredVertexShaderOpenGL()
2134{
2135 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, VertexShaderOpenGL());
2136}
2137
2138MultiviewTestParams SideBySideGeomShaderD3D11()
2139{
2140 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, GeomShaderD3D11());
2141}
2142
2143MultiviewTestParams LayeredGeomShaderD3D11()
2144{
2145 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, GeomShaderD3D11());
2146}
2147
2148MultiviewTestParams SideBySideVertexShaderD3D11()
2149{
2150 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, VertexShaderD3D11());
2151}
2152
2153MultiviewTestParams LayeredVertexShaderD3D11()
2154{
2155 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, VertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002156}
2157
Martin Radev8f276e22017-05-30 12:05:52 +03002158ANGLE_INSTANTIATE_TEST(MultiviewDrawValidationTest, ES31_OPENGL());
Martin Radevced5c862017-08-17 16:05:29 +03002159ANGLE_INSTANTIATE_TEST(MultiviewRenderDualViewTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002160 SideBySideVertexShaderOpenGL(),
2161 LayeredVertexShaderOpenGL(),
2162 SideBySideGeomShaderD3D11(),
2163 SideBySideVertexShaderD3D11(),
2164 LayeredGeomShaderD3D11(),
2165 LayeredVertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002166ANGLE_INSTANTIATE_TEST(MultiviewRenderTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002167 SideBySideVertexShaderOpenGL(),
2168 LayeredVertexShaderOpenGL(),
2169 SideBySideGeomShaderD3D11(),
2170 SideBySideVertexShaderD3D11(),
2171 LayeredGeomShaderD3D11(),
2172 LayeredVertexShaderD3D11());
Martin Radevced5c862017-08-17 16:05:29 +03002173ANGLE_INSTANTIATE_TEST(MultiviewOcclusionQueryTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002174 SideBySideVertexShaderOpenGL(),
2175 LayeredVertexShaderOpenGL(),
2176 SideBySideGeomShaderD3D11(),
2177 SideBySideVertexShaderD3D11(),
2178 LayeredGeomShaderD3D11(),
2179 LayeredVertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002180ANGLE_INSTANTIATE_TEST(MultiviewProgramGenerationTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002181 SideBySideVertexShaderOpenGL(),
2182 LayeredVertexShaderOpenGL(),
2183 SideBySideGeomShaderD3D11(),
2184 SideBySideVertexShaderD3D11(),
2185 LayeredGeomShaderD3D11(),
2186 LayeredVertexShaderD3D11());
Martin Radevced5c862017-08-17 16:05:29 +03002187ANGLE_INSTANTIATE_TEST(MultiviewRenderPrimitiveTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002188 SideBySideVertexShaderOpenGL(),
2189 LayeredVertexShaderOpenGL(),
2190 SideBySideGeomShaderD3D11(),
2191 SideBySideVertexShaderD3D11(),
2192 LayeredGeomShaderD3D11(),
2193 LayeredVertexShaderD3D11());
2194ANGLE_INSTANTIATE_TEST(MultiviewSideBySideRenderTest, VertexShaderOpenGL(), GeomShaderD3D11());
2195ANGLE_INSTANTIATE_TEST(MultiviewLayeredRenderTest, VertexShaderOpenGL(), GeomShaderD3D11());