blob: 193c08ef87160c9e016d5ee52bbf34d8d6cb4067 [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"
40 " col = vec4(1,0,0,1);\n"
41 "}\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 Radev61bd9992017-08-11 13:10:55 +0300415 " col = vec4(1,0,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));
428 EXPECT_EQ(GLColor::red, GetViewColor(1, 0, 0));
429 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
430 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
441 GLuint drawAndRetrieveOcclusionQueryResult(GLuint program)
442 {
443 GLuint query;
444 glGenQueries(1, &query);
445 glBeginQuery(GL_ANY_SAMPLES_PASSED, query);
446 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
447 glEndQueryEXT(GL_ANY_SAMPLES_PASSED);
448
449 GLuint result = GL_TRUE;
450 glGetQueryObjectuiv(query, GL_QUERY_RESULT, &result);
451 return result;
452 }
453};
454
Martin Radev3c25ad02017-08-22 17:36:53 +0300455class MultiviewProgramGenerationTest : public MultiviewRenderTest
Martin Radev41ac68e2017-06-06 12:16:58 +0300456{
457 protected:
458 MultiviewProgramGenerationTest() {}
459};
460
Martin Radev3c25ad02017-08-22 17:36:53 +0300461class MultiviewRenderPrimitiveTest : public MultiviewRenderTest
Martin Radev61bd9992017-08-11 13:10:55 +0300462{
463 protected:
Martin Radev3c25ad02017-08-22 17:36:53 +0300464 MultiviewRenderPrimitiveTest() {}
Martin Radev61bd9992017-08-11 13:10:55 +0300465
466 void setupGeometry(const std::vector<Vector2> &vertexData)
467 {
Martin Radev61bd9992017-08-11 13:10:55 +0300468 glBindBuffer(GL_ARRAY_BUFFER, mVBO);
469 glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(Vector2), vertexData.data(),
470 GL_STATIC_DRAW);
471 glEnableVertexAttribArray(0);
472 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
473 }
474
Martin Radev3c25ad02017-08-22 17:36:53 +0300475 void checkRedChannel(const GLubyte expectedRedChannelData[])
Martin Radev61bd9992017-08-11 13:10:55 +0300476 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300477 for (int view = 0; view < mNumViews; ++view)
Martin Radev61bd9992017-08-11 13:10:55 +0300478 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300479 for (int w = 0; w < mViewWidth; ++w)
Martin Radev61bd9992017-08-11 13:10:55 +0300480 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300481 for (int h = 0; h < mViewHeight; ++h)
482 {
483 size_t flatIndex =
484 static_cast<size_t>(view * mViewWidth * mViewHeight + mViewWidth * h + w);
485 EXPECT_EQ(GLColor(expectedRedChannelData[flatIndex], 0, 0, 255),
486 GetViewColor(w, h, view));
487 }
Martin Radev61bd9992017-08-11 13:10:55 +0300488 }
489 }
490 }
Martin Radev61bd9992017-08-11 13:10:55 +0300491 GLBuffer mVBO;
492};
493
Martin Radev3c25ad02017-08-22 17:36:53 +0300494class MultiviewSideBySideRenderTest : public MultiviewRenderTestBase,
Martin Radevc1d4e552017-08-21 12:01:10 +0300495 public ::testing::TestWithParam<MultiviewImplementationParams>
Martin Radev3c25ad02017-08-22 17:36:53 +0300496{
497 protected:
498 MultiviewSideBySideRenderTest()
499 : MultiviewRenderTestBase(GetParam(), GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE)
500 {
501 }
502 void SetUp() override { MultiviewRenderTestBase::RenderTestSetUp(); }
Martin Radevc1d4e552017-08-21 12:01:10 +0300503 void overrideWorkaroundsD3D(WorkaroundsD3D *workarounds) override
504 {
505 workarounds->selectViewInGeometryShader = GetParam().mForceUseGeometryShaderOnD3D;
506 }
Martin Radev3c25ad02017-08-22 17:36:53 +0300507};
508
Martin Radev72b4e1e2017-08-31 15:42:56 +0300509class MultiviewLayeredRenderTest : public MultiviewRenderTestBase,
Martin Radevc1d4e552017-08-21 12:01:10 +0300510 public ::testing::TestWithParam<MultiviewImplementationParams>
Martin Radev72b4e1e2017-08-31 15:42:56 +0300511{
512 protected:
513 MultiviewLayeredRenderTest()
514 : MultiviewRenderTestBase(GetParam(), GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE)
515 {
516 }
517 void SetUp() override { MultiviewRenderTestBase::RenderTestSetUp(); }
Martin Radevc1d4e552017-08-21 12:01:10 +0300518 void overrideWorkaroundsD3D(WorkaroundsD3D *workarounds) override
519 {
520 workarounds->selectViewInGeometryShader = GetParam().mForceUseGeometryShaderOnD3D;
521 }
Martin Radev72b4e1e2017-08-31 15:42:56 +0300522};
523
Martin Radev14a26ae2017-07-24 15:56:29 +0300524// The test verifies that glDraw*Indirect:
525// 1) generates an INVALID_OPERATION error if the number of views in the draw framebuffer is greater
526// than 1.
527// 2) does not generate any error if the draw framebuffer has exactly 1 view.
Martin Radev7cf61662017-07-26 17:10:53 +0300528TEST_P(MultiviewDrawValidationTest, IndirectDraw)
Martin Radev14a26ae2017-07-24 15:56:29 +0300529{
530 if (!requestMultiviewExtension())
531 {
532 return;
533 }
534
Martin Radev14a26ae2017-07-24 15:56:29 +0300535 const GLint viewportOffsets[4] = {0, 0, 2, 0};
Martin Radev14a26ae2017-07-24 15:56:29 +0300536
537 const std::string fsSource =
538 "#version 300 es\n"
539 "#extension GL_OVR_multiview : require\n"
540 "precision mediump float;\n"
541 "void main()\n"
542 "{}\n";
543
Martin Radev14a26ae2017-07-24 15:56:29 +0300544 GLBuffer commandBuffer;
545 glBindBuffer(GL_DRAW_INDIRECT_BUFFER, commandBuffer);
546 const GLuint commandData[] = {1u, 1u, 0u, 0u, 0u};
547 glBufferData(GL_DRAW_INDIRECT_BUFFER, sizeof(GLuint) * 5u, &commandData[0], GL_STATIC_DRAW);
548 ASSERT_GL_NO_ERROR();
549
Martin Radev14a26ae2017-07-24 15:56:29 +0300550 // Check for a GL_INVALID_OPERATION error with the framebuffer having 2 views.
551 {
552 const std::string &vsSource =
553 "#version 300 es\n"
554 "#extension GL_OVR_multiview : require\n"
555 "layout(num_views = 2) in;\n"
556 "void main()\n"
557 "{}\n";
558 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
559 glUseProgram(program);
560
Martin Radev7cf61662017-07-26 17:10:53 +0300561 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
562 0, 2, &viewportOffsets[0]);
Martin Radev14a26ae2017-07-24 15:56:29 +0300563
564 glDrawArraysIndirect(GL_TRIANGLES, nullptr);
565 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
566
567 glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
568 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
569 }
570
571 // Check that no errors are generated if the number of views is 1.
572 {
573 const std::string &vsSource =
574 "#version 300 es\n"
575 "#extension GL_OVR_multiview : require\n"
576 "layout(num_views = 1) in;\n"
577 "void main()\n"
578 "{}\n";
579 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
580 glUseProgram(program);
581
Martin Radev7cf61662017-07-26 17:10:53 +0300582 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
583 0, 1, &viewportOffsets[0]);
Martin Radev14a26ae2017-07-24 15:56:29 +0300584
585 glDrawArraysIndirect(GL_TRIANGLES, nullptr);
586 EXPECT_GL_NO_ERROR();
587
588 glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr);
589 EXPECT_GL_NO_ERROR();
590 }
591}
592
Martin Radev7cf61662017-07-26 17:10:53 +0300593// The test verifies that glDraw*:
594// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer and
595// program differs.
596// 2) does not generate any error if the number of views is the same.
Martin Radev7cf61662017-07-26 17:10:53 +0300597TEST_P(MultiviewDrawValidationTest, NumViewsMismatch)
598{
599 if (!requestMultiviewExtension())
600 {
601 return;
602 }
603
604 const GLint viewportOffsets[4] = {0, 0, 2, 0};
605
606 const std::string &vsSource =
607 "#version 300 es\n"
608 "#extension GL_OVR_multiview : require\n"
609 "layout(num_views = 2) in;\n"
610 "void main()\n"
611 "{}\n";
612 const std::string &fsSource =
613 "#version 300 es\n"
614 "#extension GL_OVR_multiview : require\n"
615 "precision mediump float;\n"
616 "void main()\n"
617 "{}\n";
618 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
619 glUseProgram(program);
620
621 // Check for a GL_INVALID_OPERATION error with the framebuffer and program having different
622 // number of views.
623 {
624 // The framebuffer has only 1 view.
625 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
626 0, 1, &viewportOffsets[0]);
627
628 glDrawArrays(GL_TRIANGLES, 0, 3);
629 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
630
631 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
632 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
633 }
634
635 // Check that no errors are generated if the number of views in both program and draw
636 // framebuffer matches.
637 {
638 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
639 0, 2, &viewportOffsets[0]);
640
641 glDrawArrays(GL_TRIANGLES, 0, 3);
642 EXPECT_GL_NO_ERROR();
643
644 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
645 EXPECT_GL_NO_ERROR();
646 }
Martin Radevda8e2572017-09-12 17:21:16 +0300647}
Martin Radev7cf61662017-07-26 17:10:53 +0300648
Martin Radevda8e2572017-09-12 17:21:16 +0300649// The test verifies that glDraw* generates an INVALID_OPERATION error if the program does not use
650// the multiview extension, but the active draw framebuffer has more than one view.
651TEST_P(MultiviewDrawValidationTest, NumViewsMismatchForNonMultiviewProgram)
652{
653 if (!requestMultiviewExtension())
Martin Radev7cf61662017-07-26 17:10:53 +0300654 {
Martin Radevda8e2572017-09-12 17:21:16 +0300655 return;
Martin Radev7cf61662017-07-26 17:10:53 +0300656 }
Martin Radevda8e2572017-09-12 17:21:16 +0300657
658 const std::string &vsSourceNoMultiview =
659 "#version 300 es\n"
660 "void main()\n"
661 "{}\n";
662 const std::string &fsSourceNoMultiview =
663 "#version 300 es\n"
664 "precision mediump float;\n"
665 "void main()\n"
666 "{}\n";
667 ANGLE_GL_PROGRAM(programNoMultiview, vsSourceNoMultiview, fsSourceNoMultiview);
668 glUseProgram(programNoMultiview);
669
670 const GLint viewportOffsets[4] = {0, 0, 2, 0};
671 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d, 0, 2,
672 &viewportOffsets[0]);
673
674 glDrawArrays(GL_TRIANGLES, 0, 3);
675 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
676
677 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
678 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
Martin Radev7cf61662017-07-26 17:10:53 +0300679}
680
Martin Radev7e69f762017-07-27 14:54:13 +0300681// The test verifies that glDraw*:
682// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer is
683// greater than 1 and there is an active transform feedback object.
684// 2) does not generate any error if the number of views in the draw framebuffer is 1.
685TEST_P(MultiviewDrawValidationTest, ActiveTransformFeedback)
686{
687 if (!requestMultiviewExtension())
688 {
689 return;
690 }
691
692 const GLint viewportOffsets[4] = {0, 0, 2, 0};
693
694 const std::string &vsSource =
695 "#version 300 es\n"
696 "void main()\n"
697 "{}\n";
698 const std::string &fsSource =
699 "#version 300 es\n"
700 "precision mediump float;\n"
701 "void main()\n"
702 "{}\n";
703 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
704 glUseProgram(program);
705
706 GLBuffer tbo;
707 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tbo);
708 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 4u, nullptr, GL_STATIC_DRAW);
709
710 GLTransformFeedback transformFeedback;
711 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, transformFeedback);
712 glBeginTransformFeedback(GL_TRIANGLES);
713 ASSERT_GL_NO_ERROR();
714
715 // Check that drawArrays generates an error when there is an active transform feedback object
716 // and the number of views in the draw framebuffer is greater than 1.
717 {
718 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
719 0, 2, &viewportOffsets[0]);
720 glDrawArrays(GL_TRIANGLES, 0, 3);
721 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
722 }
723
724 // Check that drawArrays does not generate an error when the number of views in the draw
725 // framebuffer is 1.
726 {
727 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
728 0, 1, &viewportOffsets[0]);
729 glDrawArrays(GL_TRIANGLES, 0, 3);
730 EXPECT_GL_NO_ERROR();
731 }
732
733 glEndTransformFeedback();
734}
735
Martin Radevffe754b2017-07-31 10:38:07 +0300736// The test verifies that glDraw*:
737// 1) generates an INVALID_OPERATION error if the number of views in the active draw framebuffer is
738// greater than 1 and there is an active query for target GL_TIME_ELAPSED_EXT.
739// 2) does not generate any error if the number of views in the draw framebuffer is 1.
740TEST_P(MultiviewDrawValidationTest, ActiveTimeElapsedQuery)
741{
742 if (!requestMultiviewExtension())
743 {
744 return;
745 }
746
747 if (!extensionEnabled("GL_EXT_disjoint_timer_query"))
748 {
749 std::cout << "Test skipped because GL_EXT_disjoint_timer_query is not available."
750 << std::endl;
751 return;
752 }
753
754 const GLint viewportOffsets[4] = {0, 0, 2, 0};
755 const std::string &vsSource =
756 "#version 300 es\n"
757 "void main()\n"
758 "{}\n";
759 const std::string &fsSource =
760 "#version 300 es\n"
761 "precision mediump float;\n"
762 "void main()\n"
763 "{}\n";
764 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
765 glUseProgram(program);
766
767 GLuint query = 0u;
768 glGenQueriesEXT(1, &query);
769 glBeginQueryEXT(GL_TIME_ELAPSED_EXT, query);
770
771 // Check first case.
772 {
773 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
774 0, 2, &viewportOffsets[0]);
775 glDrawArrays(GL_TRIANGLES, 0, 3);
776 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
777 }
778
779 // Check second case.
780 {
781 glFramebufferTextureMultiviewSideBySideANGLE(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mTex2d,
782 0, 1, &viewportOffsets[0]);
783 glDrawArrays(GL_TRIANGLES, 0, 3);
784 EXPECT_GL_NO_ERROR();
785 }
786
787 glEndQueryEXT(GL_TIME_ELAPSED_EXT);
788 glDeleteQueries(1, &query);
789}
790
Martin Radev8f276e22017-05-30 12:05:52 +0300791// The test checks that glDrawArrays can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300792TEST_P(MultiviewRenderDualViewTest, DrawArrays)
Martin Radev8f276e22017-05-30 12:05:52 +0300793{
794 if (!requestMultiviewExtension())
795 {
796 return;
797 }
798 drawQuad(mProgram, "vPosition", 0.0f, 1.0f, true);
799 ASSERT_GL_NO_ERROR();
800
801 checkOutput();
802}
803
804// The test checks that glDrawElements can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300805TEST_P(MultiviewRenderDualViewTest, DrawElements)
Martin Radev8f276e22017-05-30 12:05:52 +0300806{
807 if (!requestMultiviewExtension())
808 {
809 return;
810 }
811 drawIndexedQuad(mProgram, "vPosition", 0.0f, 1.0f, true);
812 ASSERT_GL_NO_ERROR();
813
814 checkOutput();
815}
816
817// The test checks that glDrawRangeElements can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300818TEST_P(MultiviewRenderDualViewTest, DrawRangeElements)
Martin Radev8f276e22017-05-30 12:05:52 +0300819{
820 if (!requestMultiviewExtension())
821 {
822 return;
823 }
824 drawIndexedQuad(mProgram, "vPosition", 0.0f, 1.0f, true, true);
825 ASSERT_GL_NO_ERROR();
826
827 checkOutput();
828}
829
830// The test checks that glDrawArrays can be used to render into four views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300831TEST_P(MultiviewRenderTest, DrawArraysFourViews)
Martin Radev8f276e22017-05-30 12:05:52 +0300832{
833 if (!requestMultiviewExtension())
834 {
835 return;
836 }
837
838 const std::string vsSource =
839 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300840 "#extension GL_OVR_multiview : require\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300841 "layout(num_views = 4) in;\n"
842 "in vec4 vPosition;\n"
843 "void main()\n"
844 "{\n"
845 " if (gl_ViewID_OVR == 0u) {\n"
846 " gl_Position.x = vPosition.x*0.25 - 0.75;\n"
847 " } else if (gl_ViewID_OVR == 1u) {\n"
848 " gl_Position.x = vPosition.x*0.25 - 0.25;\n"
849 " } else if (gl_ViewID_OVR == 2u) {\n"
850 " gl_Position.x = vPosition.x*0.25 + 0.25;\n"
851 " } else {\n"
852 " gl_Position.x = vPosition.x*0.25 + 0.75;\n"
853 " }"
854 " gl_Position.yzw = vPosition.yzw;\n"
855 "}\n";
856
857 const std::string fsSource =
858 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300859 "#extension GL_OVR_multiview : require\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300860 "precision mediump float;\n"
861 "out vec4 col;\n"
862 "void main()\n"
863 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +0300864 " col = vec4(1,0,0,1);\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300865 "}\n";
866
Martin Radev3c25ad02017-08-22 17:36:53 +0300867 createFBO(4, 1, 4);
Martin Radev8f276e22017-05-30 12:05:52 +0300868 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
869 glUseProgram(program);
870
871 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
872 ASSERT_GL_NO_ERROR();
873
874 for (int i = 0; i < 4; ++i)
875 {
876 for (int j = 0; j < 4; ++j)
877 {
Martin Radev8f276e22017-05-30 12:05:52 +0300878 if (i == j)
879 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300880 EXPECT_EQ(GLColor::red, GetViewColor(j, 0, i));
Martin Radev8f276e22017-05-30 12:05:52 +0300881 }
882 else
883 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300884 EXPECT_EQ(GLColor::black, GetViewColor(j, 0, i));
Martin Radev8f276e22017-05-30 12:05:52 +0300885 }
886 }
887 }
888 EXPECT_GL_NO_ERROR();
889}
890
891// The test checks that glDrawArraysInstanced can be used to render into two views.
Martin Radev3c25ad02017-08-22 17:36:53 +0300892TEST_P(MultiviewRenderTest, DrawArraysInstanced)
Martin Radev8f276e22017-05-30 12:05:52 +0300893{
894 if (!requestMultiviewExtension())
895 {
896 return;
897 }
898
899 const std::string vsSource =
900 "#version 300 es\n"
901 "#extension GL_OVR_multiview : require\n"
902 "layout(num_views = 2) in;\n"
903 "in vec4 vPosition;\n"
904 "void main()\n"
905 "{\n"
906 " vec4 p = vPosition;\n"
907 " if (gl_InstanceID == 1){\n"
908 " p.y = .5*p.y + .5;\n"
909 " } else {\n"
910 " p.y = p.y*.5;\n"
911 " }\n"
912 " gl_Position.x = (gl_ViewID_OVR == 0u ? p.x*0.5 + 0.5 : p.x*0.5);\n"
913 " gl_Position.yzw = p.yzw;\n"
914 "}\n";
915
916 const std::string fsSource =
917 "#version 300 es\n"
918 "#extension GL_OVR_multiview : require\n"
919 "precision mediump float;\n"
920 "out vec4 col;\n"
921 "void main()\n"
922 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +0300923 " col = vec4(1,0,0,1);\n"
Martin Radev8f276e22017-05-30 12:05:52 +0300924 "}\n";
925
Martin Radev3c25ad02017-08-22 17:36:53 +0300926 const int kViewWidth = 2;
927 const int kViewHeight = 2;
928 const int kNumViews = 2;
929 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev8f276e22017-05-30 12:05:52 +0300930 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
931 glUseProgram(program);
932
933 drawQuad(program, "vPosition", 0.0f, 1.0f, true, true, 2);
934 ASSERT_GL_NO_ERROR();
935
Martin Radev3c25ad02017-08-22 17:36:53 +0300936 const GLubyte expectedRedChannel[kNumViews][kViewHeight][kViewWidth] = {{{0, 255}, {0, 255}},
937 {{255, 0}, {255, 0}}};
938
939 for (int view = 0; view < 2; ++view)
Martin Radev8f276e22017-05-30 12:05:52 +0300940 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300941 for (int y = 0; y < 2; ++y)
Martin Radev8f276e22017-05-30 12:05:52 +0300942 {
Martin Radev3c25ad02017-08-22 17:36:53 +0300943 for (int x = 0; x < 2; ++x)
944 {
945 EXPECT_EQ(GLColor(expectedRedChannel[view][y][x], 0, 0, 255),
946 GetViewColor(x, y, view));
947 }
Martin Radev8f276e22017-05-30 12:05:52 +0300948 }
949 }
950}
951
Martin Radev553590a2017-07-31 16:40:39 +0300952// The test verifies that the attribute divisor is correctly adjusted when drawing with a multi-view
953// program. The test draws 4 instances of a quad each of which covers a single pixel. The x and y
954// offset of each quad are passed as separate attributes which are indexed based on the
955// corresponding attribute divisors. A divisor of 1 is used for the y offset to have all quads
956// drawn vertically next to each other. A divisor of 3 is used for the x offset to have the last
957// quad offsetted by one pixel to the right. Note that the number of views is divisible by 1, but
958// not by 3.
Martin Radev3c25ad02017-08-22 17:36:53 +0300959TEST_P(MultiviewRenderTest, AttribDivisor)
Martin Radev553590a2017-07-31 16:40:39 +0300960{
961 if (!requestMultiviewExtension())
962 {
963 return;
964 }
965
966 const std::string &vsSource =
967 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300968 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +0300969 "layout(num_views = 2) in;\n"
970 "in vec3 vPosition;\n"
971 "in float offsetX;\n"
972 "in float offsetY;\n"
973 "void main()\n"
974 "{\n"
975 " vec4 p = vec4(vPosition, 1.);\n"
Martin Radev0abb7a22017-08-28 15:34:45 +0300976 " p.xy = p.xy * 0.25 - vec2(0.75) + vec2(offsetX, offsetY);\n"
Martin Radev553590a2017-07-31 16:40:39 +0300977 " gl_Position.x = (gl_ViewID_OVR == 0u ? p.x : p.x + 1.0);\n"
978 " gl_Position.yzw = p.yzw;\n"
979 "}\n";
980
981 const std::string &fsSource =
982 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +0300983 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +0300984 "precision mediump float;\n"
985 "out vec4 col;\n"
986 "void main()\n"
987 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +0300988 " col = vec4(1,0,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +0300989 "}\n";
Martin Radev3c25ad02017-08-22 17:36:53 +0300990
991 const int kViewWidth = 4;
992 const int kViewHeight = 4;
993 const int kNumViews = 2;
994 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev553590a2017-07-31 16:40:39 +0300995 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
996 glUseProgram(program);
997
998 GLBuffer xOffsetVBO;
999 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1000 const GLfloat xOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.0f};
1001 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, xOffsetData, GL_STATIC_DRAW);
1002 GLint xOffsetLoc = glGetAttribLocation(program, "offsetX");
1003 glVertexAttribPointer(xOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1004 glVertexAttribDivisor(xOffsetLoc, 3);
1005 glEnableVertexAttribArray(xOffsetLoc);
1006
1007 GLBuffer yOffsetVBO;
1008 glBindBuffer(GL_ARRAY_BUFFER, yOffsetVBO);
1009 const GLfloat yOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.5f};
1010 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, yOffsetData, GL_STATIC_DRAW);
1011 GLint yOffsetLoc = glGetAttribLocation(program, "offsetY");
1012 glVertexAttribDivisor(yOffsetLoc, 1);
1013 glVertexAttribPointer(yOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1014 glEnableVertexAttribArray(yOffsetLoc);
1015
1016 drawQuad(program, "vPosition", 0.0f, 1.0f, true, true, 4);
1017 ASSERT_GL_NO_ERROR();
1018
Martin Radev3c25ad02017-08-22 17:36:53 +03001019 const GLubyte expectedRedChannel[kNumViews][kViewHeight][kViewWidth] = {
1020 {{255, 0, 0, 0}, {255, 0, 0, 0}, {255, 0, 0, 0}, {0, 255, 0, 0}},
1021 {{0, 0, 255, 0}, {0, 0, 255, 0}, {0, 0, 255, 0}, {0, 0, 0, 255}}};
1022 for (int view = 0; view < 2; ++view)
Martin Radev553590a2017-07-31 16:40:39 +03001023 {
Martin Radev3c25ad02017-08-22 17:36:53 +03001024 for (int row = 0; row < 4; ++row)
Martin Radev553590a2017-07-31 16:40:39 +03001025 {
Martin Radev3c25ad02017-08-22 17:36:53 +03001026 for (int col = 0; col < 4; ++col)
1027 {
1028 EXPECT_EQ(GLColor(expectedRedChannel[view][row][col], 0, 0, 255),
1029 GetViewColor(col, row, view));
1030 }
Martin Radev553590a2017-07-31 16:40:39 +03001031 }
1032 }
1033}
1034
1035// Test that different sequences of vertexAttribDivisor, useProgram and bindVertexArray in a
1036// multi-view context propagate the correct divisor to the driver.
Martin Radev3c25ad02017-08-22 17:36:53 +03001037TEST_P(MultiviewRenderTest, DivisorOrderOfOperation)
Martin Radev553590a2017-07-31 16:40:39 +03001038{
1039 if (!requestMultiviewExtension())
1040 {
1041 return;
1042 }
1043
Martin Radev3c25ad02017-08-22 17:36:53 +03001044 createFBO(1, 1, 2);
Martin Radev553590a2017-07-31 16:40:39 +03001045
1046 // Create multiview program.
1047 const std::string &vs =
1048 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001049 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +03001050 "layout(num_views = 2) in;\n"
1051 "layout(location = 0) in vec2 vPosition;\n"
1052 "layout(location = 1) in float offsetX;\n"
1053 "void main()\n"
1054 "{\n"
1055 " vec4 p = vec4(vPosition, 0.0, 1.0);\n"
1056 " p.x += offsetX;\n"
1057 " gl_Position = p;\n"
1058 "}\n";
1059
1060 const std::string &fs =
1061 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001062 "#extension GL_OVR_multiview : require\n"
Martin Radev553590a2017-07-31 16:40:39 +03001063 "precision mediump float;\n"
1064 "out vec4 col;\n"
1065 "void main()\n"
1066 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001067 " col = vec4(1,0,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +03001068 "}\n";
1069
1070 ANGLE_GL_PROGRAM(program, vs, fs);
1071
1072 const std::string &dummyVS =
1073 "#version 300 es\n"
1074 "layout(location = 0) in vec2 vPosition;\n"
1075 "layout(location = 1) in float offsetX;\n"
1076 "void main()\n"
1077 "{\n"
1078 " gl_Position = vec4(vPosition, 0.0, 1.0);\n"
1079 "}\n";
1080
1081 const std::string &dummyFS =
1082 "#version 300 es\n"
1083 "precision mediump float;\n"
1084 "out vec4 col;\n"
1085 "void main()\n"
1086 "{\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001087 " col = vec4(0,0,0,1);\n"
Martin Radev553590a2017-07-31 16:40:39 +03001088 "}\n";
1089
1090 ANGLE_GL_PROGRAM(dummyProgram, dummyVS, dummyFS);
1091
1092 GLBuffer xOffsetVBO;
1093 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1094 const GLfloat xOffsetData[12] = {0.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f,
1095 4.0f, 4.0f, 4.0f, 4.0f, 4.0f, 4.0f};
1096 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, xOffsetData, GL_STATIC_DRAW);
1097
1098 GLBuffer vertexVBO;
1099 glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
1100 Vector2 kQuadVertices[6] = {Vector2(-1.f, -1.f), Vector2(1.f, -1.f), Vector2(1.f, 1.f),
1101 Vector2(-1.f, -1.f), Vector2(1.f, 1.f), Vector2(-1.f, 1.f)};
1102 glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW);
1103
1104 GLVertexArray vao[2];
1105 for (size_t i = 0u; i < 2u; ++i)
1106 {
1107 glBindVertexArray(vao[i]);
1108
1109 glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
1110 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
1111 glEnableVertexAttribArray(0);
1112
1113 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1114 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
1115 glEnableVertexAttribArray(1);
1116 }
1117 ASSERT_GL_NO_ERROR();
1118
1119 glViewport(0, 0, 1, 1);
1120 glScissor(0, 0, 1, 1);
Martin Radeveef80e42017-08-11 14:44:57 +03001121 glEnable(GL_SCISSOR_TEST);
Martin Radev61bd9992017-08-11 13:10:55 +03001122 glClearColor(0, 0, 0, 1);
Martin Radev553590a2017-07-31 16:40:39 +03001123
1124 // Clear the buffers, propagate divisor to the driver, bind the vao and keep it active.
1125 // It is necessary to call draw, so that the divisor is propagated and to guarantee that dirty
1126 // bits are cleared.
1127 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001128 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001130 glBindVertexArray(vao[0]);
1131 glVertexAttribDivisor(1, 0);
1132 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
1133 glUseProgram(0);
Martin Radev553590a2017-07-31 16:40:39 +03001134 ASSERT_GL_NO_ERROR();
1135
1136 // Check that vertexAttribDivisor uses the number of views to update the divisor.
Martin Radevda8e2572017-09-12 17:21:16 +03001137 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
1138 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001139 glUseProgram(program);
1140 glVertexAttribDivisor(1, 1);
1141 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev3c25ad02017-08-22 17:36:53 +03001142 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1143 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001144
1145 // Clear the buffers and propagate divisor to the driver.
1146 // We keep the vao active and propagate the divisor to guarantee that there are no unresolved
1147 // dirty bits when useProgram is called.
1148 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001149 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1150 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001151 glVertexAttribDivisor(1, 1);
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 useProgram 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 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev3c25ad02017-08-22 17:36:53 +03001161 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1162 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001163
1164 // We go through similar steps as before.
1165 glUseProgram(dummyProgram);
Martin Radevda8e2572017-09-12 17:21:16 +03001166 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001168 glVertexAttribDivisor(1, 1);
1169 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
1170 glUseProgram(0);
Martin Radev553590a2017-07-31 16:40:39 +03001171 ASSERT_GL_NO_ERROR();
1172
1173 // Check that bindVertexArray uses the number of views to update the divisor.
1174 {
1175 // Call useProgram with vao[1] being active to guarantee that useProgram will adjust the
1176 // divisor for vao[1] only.
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 glBindVertexArray(vao[1]);
1180 glUseProgram(program);
1181 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev553590a2017-07-31 16:40:39 +03001182 glBindVertexArray(0);
1183 ASSERT_GL_NO_ERROR();
1184 }
1185 // Bind vao[0] after useProgram is called to ensure that bindVertexArray is the call which
1186 // adjusts the divisor.
Martin Radevda8e2572017-09-12 17:21:16 +03001187 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Martin Radev553590a2017-07-31 16:40:39 +03001188 glBindVertexArray(vao[0]);
1189 glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 1);
Martin Radev3c25ad02017-08-22 17:36:53 +03001190 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1191 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
Martin Radev553590a2017-07-31 16:40:39 +03001192}
1193
Martin Radev0d671c92017-08-10 16:41:52 +03001194// Test that no fragments pass the occlusion query for a multi-view vertex shader which always
1195// transforms geometry to be outside of the clip region.
Martin Radev3c25ad02017-08-22 17:36:53 +03001196TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryNothingVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001197{
1198 if (!requestMultiviewExtension())
1199 {
1200 return;
1201 }
1202
1203 const std::string vsSource =
1204 "#version 300 es\n"
1205 "#extension GL_OVR_multiview : require\n"
1206 "layout(num_views = 2) in;\n"
1207 "in vec3 vPosition;\n"
1208 "void main()\n"
1209 "{\n"
1210 " gl_Position.x = 2.0;\n"
1211 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1212 "}\n";
1213
1214 const std::string fsSource =
1215 "#version 300 es\n"
1216 "#extension GL_OVR_multiview : require\n"
1217 "precision mediump float;\n"
1218 "out vec4 col;\n"
1219 "void main()\n"
1220 "{\n"
1221 " col = vec4(1,0,0,0);\n"
1222 "}\n";
1223 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1224 glUseProgram(program);
Martin Radev3c25ad02017-08-22 17:36:53 +03001225 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001226
1227 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1228 ASSERT_GL_NO_ERROR();
1229 EXPECT_GL_FALSE(result);
1230}
1231
1232// Test that there are fragments passing the occlusion query if only view 0 can produce
1233// output.
Martin Radev3c25ad02017-08-22 17:36:53 +03001234TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryOnlyLeftVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001235{
1236 if (!requestMultiviewExtension())
1237 {
1238 return;
1239 }
1240
1241 const std::string vsSource =
1242 "#version 300 es\n"
1243 "#extension GL_OVR_multiview : require\n"
1244 "layout(num_views = 2) in;\n"
1245 "in vec3 vPosition;\n"
1246 "void main()\n"
1247 "{\n"
1248 " gl_Position.x = gl_ViewID_OVR == 0u ? vPosition.x : 2.0;\n"
1249 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1250 "}\n";
1251
1252 const std::string fsSource =
1253 "#version 300 es\n"
1254 "#extension GL_OVR_multiview : require\n"
1255 "precision mediump float;\n"
1256 "out vec4 col;\n"
1257 "void main()\n"
1258 "{\n"
1259 " col = vec4(1,0,0,0);\n"
1260 "}\n";
1261 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1262 glUseProgram(program);
Martin Radev3c25ad02017-08-22 17:36:53 +03001263 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001264
1265 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1266 ASSERT_GL_NO_ERROR();
1267 EXPECT_GL_TRUE(result);
1268}
1269
1270// Test that there are fragments passing the occlusion query if only view 1 can produce
1271// output.
Martin Radev3c25ad02017-08-22 17:36:53 +03001272TEST_P(MultiviewOcclusionQueryTest, OcclusionQueryOnlyRightVisible)
Martin Radev0d671c92017-08-10 16:41:52 +03001273{
1274 if (!requestMultiviewExtension())
1275 {
1276 return;
1277 }
1278
1279 const std::string vsSource =
1280 "#version 300 es\n"
1281 "#extension GL_OVR_multiview : require\n"
1282 "layout(num_views = 2) in;\n"
1283 "in vec3 vPosition;\n"
1284 "void main()\n"
1285 "{\n"
1286 " gl_Position.x = gl_ViewID_OVR == 1u ? vPosition.x : 2.0;\n"
1287 " gl_Position.yzw = vec3(vPosition.yz, 1.);\n"
1288 "}\n";
1289
1290 const std::string fsSource =
1291 "#version 300 es\n"
1292 "#extension GL_OVR_multiview : require\n"
1293 "precision mediump float;\n"
1294 "out vec4 col;\n"
1295 "void main()\n"
1296 "{\n"
1297 " col = vec4(1,0,0,0);\n"
1298 "}\n";
1299 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1300 glUseProgram(program);
Martin Radev3c25ad02017-08-22 17:36:53 +03001301 createFBO(1, 1, 2);
Martin Radev0d671c92017-08-10 16:41:52 +03001302
1303 GLuint result = drawAndRetrieveOcclusionQueryResult(program);
1304 ASSERT_GL_NO_ERROR();
1305 EXPECT_GL_TRUE(result);
1306}
1307
Martin Radev41ac68e2017-06-06 12:16:58 +03001308// Test that a simple multi-view program which doesn't use gl_ViewID_OVR in neither VS nor FS
1309// compiles and links without an error.
1310TEST_P(MultiviewProgramGenerationTest, SimpleProgram)
1311{
1312 if (!requestMultiviewExtension())
1313 {
1314 return;
1315 }
1316
1317 const std::string vsSource =
1318 "#version 300 es\n"
1319 "#extension GL_OVR_multiview : require\n"
1320 "layout(num_views = 2) in;\n"
1321 "void main()\n"
1322 "{\n"
1323 "}\n";
1324
1325 const std::string fsSource =
1326 "#version 300 es\n"
1327 "#extension GL_OVR_multiview : require\n"
1328 "precision mediump float;\n"
1329 "void main()\n"
1330 "{\n"
1331 "}\n";
1332
1333 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1334 glUseProgram(program);
1335
1336 EXPECT_GL_NO_ERROR();
1337}
1338
1339// Test that a simple multi-view program which uses gl_ViewID_OVR only in VS compiles and links
1340// without an error.
1341TEST_P(MultiviewProgramGenerationTest, UseViewIDInVertexShader)
1342{
1343 if (!requestMultiviewExtension())
1344 {
1345 return;
1346 }
1347
1348 const std::string vsSource =
1349 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001350 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001351 "layout(num_views = 2) in;\n"
1352 "void main()\n"
1353 "{\n"
1354 " if (gl_ViewID_OVR == 0u) {\n"
1355 " gl_Position = vec4(1,0,0,1);\n"
1356 " } else {\n"
1357 " gl_Position = vec4(-1,0,0,1);\n"
1358 " }\n"
1359 "}\n";
1360
1361 const std::string fsSource =
1362 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001363 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001364 "precision mediump float;\n"
1365 "void main()\n"
1366 "{\n"
1367 "}\n";
1368
1369 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1370 glUseProgram(program);
1371
1372 EXPECT_GL_NO_ERROR();
1373}
1374
1375// Test that a simple multi-view program which uses gl_ViewID_OVR only in FS compiles and links
1376// without an error.
1377TEST_P(MultiviewProgramGenerationTest, UseViewIDInFragmentShader)
1378{
1379 if (!requestMultiviewExtension())
1380 {
1381 return;
1382 }
1383
1384 const std::string vsSource =
1385 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001386 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001387 "layout(num_views = 2) in;\n"
1388 "void main()\n"
1389 "{\n"
1390 "}\n";
1391
1392 const std::string fsSource =
1393 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001394 "#extension GL_OVR_multiview : require\n"
Martin Radev41ac68e2017-06-06 12:16:58 +03001395 "precision mediump float;\n"
1396 "out vec4 col;\n"
1397 "void main()\n"
1398 "{\n"
1399 " if (gl_ViewID_OVR == 0u) {\n"
1400 " col = vec4(1,0,0,1);\n"
1401 " } else {\n"
1402 " col = vec4(-1,0,0,1);\n"
1403 " }\n"
1404 "}\n";
1405
1406 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1407 glUseProgram(program);
1408
1409 EXPECT_GL_NO_ERROR();
1410}
1411
Martin Radev61bd9992017-08-11 13:10:55 +03001412// The test checks that GL_POINTS is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001413TEST_P(MultiviewRenderPrimitiveTest, Points)
Martin Radev61bd9992017-08-11 13:10:55 +03001414{
1415 if (!requestMultiviewExtension())
1416 {
1417 return;
1418 }
1419
1420 const std::string vsSource =
1421 "#version 300 es\n"
1422 "#extension GL_OVR_multiview : require\n"
1423 "layout(num_views = 2) in;\n"
1424 "layout(location=0) in vec2 vPosition;\n"
1425 "void main()\n"
1426 "{\n"
1427 " gl_PointSize = 1.0;\n"
1428 " gl_Position = vec4(vPosition.xy, 0.0, 1.0);\n"
1429 "}\n";
1430
1431 const std::string fsSource =
1432 "#version 300 es\n"
1433 "#extension GL_OVR_multiview : require\n"
1434 "precision mediump float;\n"
1435 "out vec4 col;\n"
1436 "void main()\n"
1437 "{\n"
1438 " col = vec4(1,0,0,1);\n"
1439 "}\n";
1440 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1441 glUseProgram(program);
1442
Martin Radev3c25ad02017-08-22 17:36:53 +03001443 const int kViewWidth = 4;
1444 const int kViewHeight = 2;
1445 const int kNumViews = 2;
1446 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001447
1448 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 1)};
1449 std::vector<Vector2> vertexDataInClipSpace =
1450 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1451 setupGeometry(vertexDataInClipSpace);
1452
1453 glDrawArrays(GL_POINTS, 0, 2);
1454
Martin Radev3c25ad02017-08-22 17:36:53 +03001455 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {
1456 {{255, 0, 0, 0}, {0, 0, 0, 255}}, {{255, 0, 0, 0}, {0, 0, 0, 255}}};
1457 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001458}
1459
1460// The test checks that GL_LINES is correctly rendered.
1461// The behavior of this test is not guaranteed by the spec:
1462// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1463// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1464// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1465// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001466TEST_P(MultiviewRenderPrimitiveTest, Lines)
Martin Radev61bd9992017-08-11 13:10:55 +03001467{
1468 if (!requestMultiviewExtension())
1469 {
1470 return;
1471 }
1472
Martin Radevced5c862017-08-17 16:05:29 +03001473 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001474 ASSERT_NE(program, 0u);
1475 glUseProgram(program);
1476 ASSERT_GL_NO_ERROR();
1477
Martin Radev3c25ad02017-08-22 17:36:53 +03001478 const int kViewWidth = 4;
1479 const int kViewHeight = 2;
1480 const int kNumViews = 2;
1481 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001482
1483 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(4, 0)};
1484 std::vector<Vector2> vertexDataInClipSpace =
1485 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1486 setupGeometry(vertexDataInClipSpace);
1487
1488 glDrawArrays(GL_LINES, 0, 2);
1489
Martin Radev3c25ad02017-08-22 17:36:53 +03001490 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {
1491 {{255, 255, 255, 255}, {0, 0, 0, 0}}, {{255, 255, 255, 255}, {0, 0, 0, 0}}};
1492 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001493
1494 glDeleteProgram(program);
1495}
1496
1497// The test checks that GL_LINE_STRIP is correctly rendered.
1498// The behavior of this test is not guaranteed by the spec:
1499// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1500// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1501// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1502// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001503TEST_P(MultiviewRenderPrimitiveTest, LineStrip)
Martin Radev61bd9992017-08-11 13:10:55 +03001504{
1505 if (!requestMultiviewExtension())
1506 {
1507 return;
1508 }
1509
Martin Radevced5c862017-08-17 16:05:29 +03001510 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001511 ASSERT_NE(program, 0u);
1512 glUseProgram(program);
1513 ASSERT_GL_NO_ERROR();
1514
Martin Radev3c25ad02017-08-22 17:36:53 +03001515 const int kViewWidth = 4;
1516 const int kViewHeight = 2;
1517 const int kNumViews = 2;
1518 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001519
1520 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 0), Vector2I(3, 2)};
1521 std::vector<Vector2> vertexDataInClipSpace =
1522 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 2);
1523 setupGeometry(vertexDataInClipSpace);
1524
1525 glDrawArrays(GL_LINE_STRIP, 0, 3);
1526
Martin Radev3c25ad02017-08-22 17:36:53 +03001527 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {
1528 {{255, 255, 255, 255}, {0, 0, 0, 255}}, {{255, 255, 255, 255}, {0, 0, 0, 255}}};
1529 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001530
1531 glDeleteProgram(program);
1532}
1533
1534// The test checks that GL_LINE_LOOP is correctly rendered.
1535// The behavior of this test is not guaranteed by the spec:
1536// OpenGL ES 3.0.5 (November 3, 2016), Section 3.5.1 Basic Line Segment Rasterization:
1537// "The coordinates of a fragment produced by the algorithm may not deviate by more than one unit in
1538// either x or y window coordinates from a corresponding fragment produced by the diamond-exit
1539// rule."
Martin Radev3c25ad02017-08-22 17:36:53 +03001540TEST_P(MultiviewRenderPrimitiveTest, LineLoop)
Martin Radev61bd9992017-08-11 13:10:55 +03001541{
1542 if (!requestMultiviewExtension())
1543 {
1544 return;
1545 }
1546
Martin Radevced5c862017-08-17 16:05:29 +03001547 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001548 ASSERT_NE(program, 0u);
1549 glUseProgram(program);
1550 ASSERT_GL_NO_ERROR();
1551
Martin Radev3c25ad02017-08-22 17:36:53 +03001552 const int kViewWidth = 4;
1553 const int kViewHeight = 4;
1554 const int kNumViews = 2;
1555 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001556
1557 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(3, 0), Vector2I(3, 3),
1558 Vector2I(0, 3)};
1559 std::vector<Vector2> vertexDataInClipSpace =
1560 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 4);
1561 setupGeometry(vertexDataInClipSpace);
1562
1563 glDrawArrays(GL_LINE_LOOP, 0, 4);
1564
Martin Radev3c25ad02017-08-22 17:36:53 +03001565 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {
1566 {{255, 255, 255, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 255, 255, 255}},
1567 {{255, 255, 255, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 255, 255, 255}}};
1568 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001569
1570 glDeleteProgram(program);
1571}
1572
1573// The test checks that GL_TRIANGLE_STRIP is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001574TEST_P(MultiviewRenderPrimitiveTest, TriangleStrip)
Martin Radev61bd9992017-08-11 13:10:55 +03001575{
1576 if (!requestMultiviewExtension())
1577 {
1578 return;
1579 }
1580
Martin Radevced5c862017-08-17 16:05:29 +03001581 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001582 ASSERT_NE(program, 0u);
1583 glUseProgram(program);
1584 ASSERT_GL_NO_ERROR();
1585
1586 std::vector<Vector2> vertexDataInClipSpace = {Vector2(1.0f, 0.0f), Vector2(0.0f, 0.0f),
1587 Vector2(1.0f, 1.0f), Vector2(0.0f, 1.0f)};
1588 setupGeometry(vertexDataInClipSpace);
1589
Martin Radev3c25ad02017-08-22 17:36:53 +03001590 const int kViewWidth = 2;
1591 const int kViewHeight = 2;
1592 const int kNumViews = 2;
1593 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001594
1595 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1596
Martin Radev3c25ad02017-08-22 17:36:53 +03001597 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {{{0, 0}, {0, 255}},
1598 {{0, 0}, {0, 255}}};
1599 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001600
1601 glDeleteProgram(program);
1602}
1603
1604// The test checks that GL_TRIANGLE_FAN is correctly rendered.
Martin Radev3c25ad02017-08-22 17:36:53 +03001605TEST_P(MultiviewRenderPrimitiveTest, TriangleFan)
Martin Radev61bd9992017-08-11 13:10:55 +03001606{
1607 if (!requestMultiviewExtension())
1608 {
1609 return;
1610 }
1611
Martin Radevced5c862017-08-17 16:05:29 +03001612 GLuint program = CreateSimplePassthroughProgram(2);
Martin Radev61bd9992017-08-11 13:10:55 +03001613 ASSERT_NE(program, 0u);
1614 glUseProgram(program);
1615 ASSERT_GL_NO_ERROR();
1616
1617 std::vector<Vector2> vertexDataInClipSpace = {Vector2(0.0f, 0.0f), Vector2(0.0f, 1.0f),
1618 Vector2(1.0f, 1.0f), Vector2(1.0f, 0.0f)};
1619 setupGeometry(vertexDataInClipSpace);
1620
Martin Radev3c25ad02017-08-22 17:36:53 +03001621 const int kViewWidth = 2;
1622 const int kViewHeight = 2;
1623 const int kNumViews = 2;
1624 createFBO(kViewWidth, kViewHeight, kNumViews);
Martin Radev61bd9992017-08-11 13:10:55 +03001625
1626 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1627
Martin Radev3c25ad02017-08-22 17:36:53 +03001628 const GLubyte expectedRedChannelData[kNumViews][kViewHeight][kViewWidth] = {{{0, 0}, {0, 255}},
1629 {{0, 0}, {0, 255}}};
1630 checkRedChannel(expectedRedChannelData[0][0]);
Martin Radev61bd9992017-08-11 13:10:55 +03001631
1632 glDeleteProgram(program);
1633}
1634
1635// Test that rendering enlarged points and lines does not leak fragments outside of the views'
1636// bounds. The test does not rely on the actual line width being greater than 1.0.
Martin Radev3c25ad02017-08-22 17:36:53 +03001637TEST_P(MultiviewSideBySideRenderTest, NoLeakingFragments)
Martin Radev61bd9992017-08-11 13:10:55 +03001638{
1639 if (!requestMultiviewExtension())
1640 {
1641 return;
1642 }
1643
Martin Radev3c25ad02017-08-22 17:36:53 +03001644 createFBO(2, 1, 2);
Martin Radev61bd9992017-08-11 13:10:55 +03001645
1646 GLint viewportOffsets[4] = {1, 0, 3, 0};
1647 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1648 mColorTexture, 0, 2, &viewportOffsets[0]);
1649 glFramebufferTextureMultiviewSideBySideANGLE(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
1650 mDepthTexture, 0, 2, &viewportOffsets[0]);
1651
1652 glViewport(0, 0, 1, 1);
1653 glScissor(0, 0, 1, 1);
1654 glEnable(GL_SCISSOR_TEST);
1655
1656 const std::string vsSource =
1657 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001658 "#extension GL_OVR_multiview : require\n"
Martin Radev61bd9992017-08-11 13:10:55 +03001659 "layout(num_views = 2) in;\n"
1660 "layout(location=0) in vec2 vPosition;\n"
1661 "void main()\n"
1662 "{\n"
1663 " gl_PointSize = 10.0;\n"
1664 " gl_Position = vec4(vPosition.xy, 0.0, 1.0);\n"
1665 "}\n";
1666
1667 const std::string fsSource =
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 "precision mediump float;\n"
1671 "out vec4 col;\n"
1672 "void main()\n"
1673 "{\n"
1674 " if (gl_ViewID_OVR == 0u) {\n"
1675 " col = vec4(1,0,0,1);\n"
1676 " } else {\n"
1677 " col = vec4(0,1,0,1);\n"
1678 " }\n"
1679 "}\n";
1680 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1681 glUseProgram(program);
1682
1683 const std::vector<Vector2I> &windowCoordinates = {Vector2I(0, 0), Vector2I(2, 0)};
1684 const std::vector<Vector2> &vertexDataInClipSpace =
1685 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 1, 1);
Martin Radev3c25ad02017-08-22 17:36:53 +03001686
1687 GLBuffer vbo;
1688 glBindBuffer(GL_ARRAY_BUFFER, vbo);
1689 glBufferData(GL_ARRAY_BUFFER, vertexDataInClipSpace.size() * sizeof(Vector2),
1690 vertexDataInClipSpace.data(), GL_STATIC_DRAW);
1691 glEnableVertexAttribArray(0);
1692 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
Martin Radev61bd9992017-08-11 13:10:55 +03001693
1694 // Test rendering points.
1695 {
1696 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1697 glDrawArrays(GL_POINTS, 0, 2);
1698 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
1699 EXPECT_PIXEL_COLOR_EQ(1, 0, GLColor::red);
1700 EXPECT_PIXEL_COLOR_EQ(2, 0, GLColor::black);
1701 EXPECT_PIXEL_COLOR_EQ(3, 0, GLColor::green);
1702 }
1703
1704 // Test rendering lines.
1705 {
1706 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1707 glLineWidth(10.f);
1708 glDrawArrays(GL_LINES, 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
Martin Radev0abb7a22017-08-28 15:34:45 +03001716// Verify that re-linking a program adjusts the attribute divisor.
1717// The test uses instacing to draw for each view a strips of two red quads and two blue quads next
1718// to each other. The quads' position and color depend on the corresponding attribute divisors.
1719TEST_P(MultiviewRenderTest, ProgramRelinkUpdatesAttribDivisor)
1720{
1721 if (!requestMultiviewExtension())
1722 {
1723 return;
1724 }
1725
1726 const int kViewWidth = 4;
1727 const int kViewHeight = 1;
1728 const int kNumViews = 2;
1729
1730 const std::string &fsSource =
1731 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001732 "#extension GL_OVR_multiview : require\n"
Martin Radev0abb7a22017-08-28 15:34:45 +03001733 "precision mediump float;\n"
1734 "in vec4 oColor;\n"
1735 "out vec4 col;\n"
1736 "void main()\n"
1737 "{\n"
1738 " col = oColor;\n"
1739 "}\n";
1740
1741 auto generateVertexShaderSource = [](int numViews) -> std::string {
1742 std::string source =
1743 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001744 "#extension GL_OVR_multiview : require\n"
Martin Radev0abb7a22017-08-28 15:34:45 +03001745 "layout(num_views = " +
1746 ToString(numViews) +
1747 ") in;\n"
1748 "in vec3 vPosition;\n"
1749 "in float vOffsetX;\n"
1750 "in vec4 vColor;\n"
1751 "out vec4 oColor;\n"
1752 "void main()\n"
1753 "{\n"
1754 " vec4 p = vec4(vPosition, 1.);\n"
1755 " p.x = p.x * 0.25 - 0.75 + vOffsetX;\n"
1756 " oColor = vColor;\n"
1757 " gl_Position = p;\n"
1758 "}\n";
1759 return source;
1760 };
1761
1762 std::string vsSource = generateVertexShaderSource(kNumViews);
1763 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1764 glUseProgram(program);
1765
1766 GLint positionLoc;
1767 GLBuffer xOffsetVBO;
1768 GLint xOffsetLoc;
1769 GLBuffer colorVBO;
1770 GLint colorLoc;
1771
1772 {
1773 // Initialize buffers and setup attributes.
1774 glBindBuffer(GL_ARRAY_BUFFER, xOffsetVBO);
1775 const GLfloat kXOffsetData[4] = {0.0f, 0.5f, 1.0f, 1.5f};
1776 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4, kXOffsetData, GL_STATIC_DRAW);
1777 xOffsetLoc = glGetAttribLocation(program, "vOffsetX");
1778 glVertexAttribPointer(xOffsetLoc, 1, GL_FLOAT, GL_FALSE, 0, 0);
1779 glVertexAttribDivisor(xOffsetLoc, 1);
1780 glEnableVertexAttribArray(xOffsetLoc);
1781
1782 glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
1783 const GLColor kColors[2] = {GLColor::red, GLColor::blue};
1784 glBufferData(GL_ARRAY_BUFFER, sizeof(GLColor) * 2, kColors, GL_STATIC_DRAW);
1785 colorLoc = glGetAttribLocation(program, "vColor");
1786 glVertexAttribDivisor(colorLoc, 2);
1787 glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, 0);
1788 glEnableVertexAttribArray(colorLoc);
1789
1790 positionLoc = glGetAttribLocation(program, "vPosition");
1791 }
1792
1793 {
1794 createFBO(kViewWidth, kViewHeight, kNumViews);
1795
1796 drawQuad(program, "vPosition", 0.0f, 1.0f, true, true, 4);
1797 ASSERT_GL_NO_ERROR();
1798
1799 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1800 EXPECT_EQ(GLColor::red, GetViewColor(1, 0, 0));
1801 EXPECT_EQ(GLColor::blue, GetViewColor(2, 0, 0));
1802 EXPECT_EQ(GLColor::blue, GetViewColor(3, 0, 0));
1803 }
1804
1805 {
1806 const int kNewNumViews = 3;
1807 vsSource = generateVertexShaderSource(kNewNumViews);
1808 createFBO(kViewWidth, kViewHeight, kNewNumViews);
1809
1810 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
1811 ASSERT_NE(0u, vs);
1812 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
1813 ASSERT_NE(0u, fs);
1814
1815 GLint numAttachedShaders = 0;
1816 glGetProgramiv(program, GL_ATTACHED_SHADERS, &numAttachedShaders);
1817
1818 GLuint attachedShaders[2] = {0u};
1819 glGetAttachedShaders(program, numAttachedShaders, nullptr, attachedShaders);
1820 for (int i = 0; i < 2; ++i)
1821 {
1822 glDetachShader(program, attachedShaders[i]);
1823 }
1824
1825 glAttachShader(program, vs);
1826 glDeleteShader(vs);
1827
1828 glAttachShader(program, fs);
1829 glDeleteShader(fs);
1830
1831 glBindAttribLocation(program, positionLoc, "vPosition");
1832 glBindAttribLocation(program, xOffsetLoc, "vOffsetX");
1833 glBindAttribLocation(program, colorLoc, "vColor");
1834
1835 glLinkProgram(program);
1836
1837 drawQuad(program, "vPosition", 0.0f, 1.0f, true, true, 4);
1838 ASSERT_GL_NO_ERROR();
1839
1840 for (int i = 0; i < kNewNumViews; ++i)
1841 {
1842 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, i));
1843 EXPECT_EQ(GLColor::red, GetViewColor(1, 0, i));
1844 EXPECT_EQ(GLColor::blue, GetViewColor(2, 0, i));
1845 EXPECT_EQ(GLColor::blue, GetViewColor(3, 0, i));
1846 }
1847 }
1848}
1849
Martin Radevced5c862017-08-17 16:05:29 +03001850// Test that useProgram applies the number of views in computing the final value of the attribute
1851// divisor.
1852TEST_P(MultiviewRenderTest, DivisorUpdatedOnProgramChange)
1853{
1854 if (!requestMultiviewExtension())
1855 {
1856 return;
1857 }
1858
1859 GLVertexArray vao;
1860 glBindVertexArray(vao);
1861 GLBuffer vbo;
1862 glBindBuffer(GL_ARRAY_BUFFER, vbo);
1863 std::vector<Vector2I> windowCoordinates = {Vector2I(0, 0), Vector2I(1, 0), Vector2I(2, 0),
1864 Vector2I(3, 0)};
1865 std::vector<Vector2> vertexDataInClipSpace =
1866 ConvertPixelCoordinatesToClipSpace(windowCoordinates, 4, 1);
1867 // Fill with x positions so that the resulting clip space coordinate fails the clip test.
1868 glBufferData(GL_ARRAY_BUFFER, sizeof(Vector2) * vertexDataInClipSpace.size(),
1869 vertexDataInClipSpace.data(), GL_STATIC_DRAW);
1870 glEnableVertexAttribArray(0);
1871 glVertexAttribPointer(0, 2, GL_FLOAT, 0, 0, nullptr);
1872 glVertexAttribDivisor(0, 1);
1873 ASSERT_GL_NO_ERROR();
1874
1875 // Create a program and fbo with N views and draw N instances of a point horizontally.
1876 for (int numViews = 2; numViews <= 4; ++numViews)
1877 {
1878 createFBO(4, 1, numViews);
1879 ASSERT_GL_NO_ERROR();
1880
1881 GLuint program = CreateSimplePassthroughProgram(numViews);
1882 ASSERT_NE(program, 0u);
1883 glUseProgram(program);
1884 ASSERT_GL_NO_ERROR();
1885
1886 glDrawArraysInstanced(GL_POINTS, 0, 1, numViews);
1887
1888 for (int view = 0; view < numViews; ++view)
1889 {
1890 for (int j = 0; j < numViews; ++j)
1891 {
1892 EXPECT_EQ(GLColor::red, GetViewColor(j, 0, view));
1893 }
1894 for (int j = numViews; j < 4; ++j)
1895 {
1896 EXPECT_EQ(GLColor::black, GetViewColor(j, 0, view));
1897 }
1898 }
1899
1900 glDeleteProgram(program);
1901 }
1902}
1903
Martin Radev72b4e1e2017-08-31 15:42:56 +03001904// The test checks that gl_ViewID_OVR is correctly propagated to the fragment shader.
1905TEST_P(MultiviewRenderTest, SelectColorBasedOnViewIDOVR)
1906{
1907 if (!requestMultiviewExtension())
1908 {
1909 return;
1910 }
1911
1912 const std::string vsSource =
1913 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001914 "#extension GL_OVR_multiview : require\n"
Martin Radev72b4e1e2017-08-31 15:42:56 +03001915 "layout(num_views = 3) in;\n"
1916 "in vec3 vPosition;\n"
1917 "void main()\n"
1918 "{\n"
1919 " gl_Position = vec4(vPosition, 1.);\n"
1920 "}\n";
1921
1922 const std::string fsSource =
1923 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03001924 "#extension GL_OVR_multiview : require\n"
Martin Radev72b4e1e2017-08-31 15:42:56 +03001925 "precision mediump float;\n"
1926 "out vec4 col;\n"
1927 "void main()\n"
1928 "{\n"
1929 " if (gl_ViewID_OVR == 0u) {\n"
1930 " col = vec4(1,0,0,1);\n"
1931 " } else if (gl_ViewID_OVR == 1u) {\n"
1932 " col = vec4(0,1,0,1);\n"
1933 " } else if (gl_ViewID_OVR == 2u) {\n"
1934 " col = vec4(0,0,1,1);\n"
1935 " } else {\n"
1936 " col = vec4(0,0,0,0);\n"
1937 " }\n"
1938 "}\n";
1939
1940 createFBO(1, 1, 3);
1941 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1942 glUseProgram(program);
1943
1944 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
1945 ASSERT_GL_NO_ERROR();
1946
1947 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
1948 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
1949 EXPECT_EQ(GLColor::blue, GetViewColor(0, 0, 2));
1950}
1951
1952// The test checks that the inactive layers of a 2D texture array are not written to by a
1953// multi-view program.
1954TEST_P(MultiviewLayeredRenderTest, RenderToSubrageOfLayers)
1955{
1956 if (!requestMultiviewExtension())
1957 {
1958 return;
1959 }
1960
1961 const std::string vsSource =
1962 "#version 300 es\n"
1963 "#extension GL_OVR_multiview : require\n"
1964 "layout(num_views = 2) in;\n"
1965 "in vec3 vPosition;\n"
1966 "void main()\n"
1967 "{\n"
1968 " gl_Position = vec4(vPosition, 1.);\n"
1969 "}\n";
1970
1971 const std::string fsSource =
1972 "#version 300 es\n"
1973 "#extension GL_OVR_multiview : require\n"
1974 "precision mediump float;\n"
1975 "out vec4 col;\n"
1976 "void main()\n"
1977 "{\n"
1978 " col = vec4(1,0,0,1);\n"
1979 "}\n";
1980
1981 createFBO(1, 1, 2, 4, 1);
1982 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
1983 glUseProgram(program);
1984
1985 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
1986 ASSERT_GL_NO_ERROR();
1987
1988 EXPECT_EQ(GLColor::transparentBlack, GetViewColor(0, 0, 0));
1989 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
1990 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 2));
1991 EXPECT_EQ(GLColor::transparentBlack, GetViewColor(0, 0, 3));
1992}
1993
Martin Radevc1d4e552017-08-21 12:01:10 +03001994// The D3D11 renderer uses a GS whenever the varyings are flat interpolated which can cause
1995// potential bugs if the view is selected in the VS. The test contains a program in which the
1996// gl_InstanceID is passed as a flat varying to the fragment shader where it is used to discard the
1997// fragment if its value is negative. The gl_InstanceID should never be negative and that branch is
1998// never taken. One quad is drawn and the color is selected based on the ViewID - red for view 0 and
1999// green for view 1.
2000TEST_P(MultiviewRenderTest, FlatInterpolation)
Martin Radev3c25ad02017-08-22 17:36:53 +03002001{
Martin Radevc1d4e552017-08-21 12:01:10 +03002002 if (!requestMultiviewExtension())
2003 {
2004 return;
2005 }
2006
Yuly Novikov1de29ab2017-09-07 18:07:23 -04002007 // TODO(mradev): Find out why this fails on Win10 Intel HD 630 D3D11
2008 // (http://anglebug.com/2062)
2009 ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsD3D11());
2010
Martin Radevc1d4e552017-08-21 12:01:10 +03002011 const std::string vsSource =
2012 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03002013 "#extension GL_OVR_multiview : require\n"
Martin Radevc1d4e552017-08-21 12:01:10 +03002014 "layout(num_views = 2) in;\n"
2015 "in vec3 vPosition;\n"
2016 "flat out int oInstanceID;\n"
2017 "void main()\n"
2018 "{\n"
2019 " gl_Position = vec4(vPosition, 1.);\n"
2020 " oInstanceID = gl_InstanceID;\n"
2021 "}\n";
2022
2023 const std::string fsSource =
2024 "#version 300 es\n"
Olli Etuahoec3a9cb2017-09-07 12:18:01 +03002025 "#extension GL_OVR_multiview : require\n"
Martin Radevc1d4e552017-08-21 12:01:10 +03002026 "precision mediump float;\n"
2027 "flat in int oInstanceID;\n"
2028 "out vec4 col;\n"
2029 "void main()\n"
2030 "{\n"
2031 " if (oInstanceID < 0) {\n"
2032 " discard;\n"
2033 " }\n"
2034 " if (gl_ViewID_OVR == 0u) {\n"
2035 " col = vec4(1,0,0,1);\n"
2036 " } else {\n"
2037 " col = vec4(0,1,0,1);\n"
2038 " }\n"
2039 "}\n";
2040
2041 createFBO(1, 1, 2);
2042 ANGLE_GL_PROGRAM(program, vsSource, fsSource);
2043
2044 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
2045 ASSERT_GL_NO_ERROR();
2046
2047 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
2048 EXPECT_EQ(GLColor::green, GetViewColor(0, 0, 1));
Martin Radev3c25ad02017-08-22 17:36:53 +03002049}
2050
Martin Radev265a6d42017-09-12 16:51:37 +03002051// The test is added to cover a bug which resulted in the viewport/scissor and viewport offsets not
2052// being correctly applied.
2053TEST_P(MultiviewSideBySideRenderTest, ViewportOffsetsAppliedBugCoverage)
2054{
2055 if (!requestMultiviewExtension())
2056 {
2057 return;
2058 }
2059
2060 createFBO(1, 1, 2);
2061
2062 // Create multiview program.
2063 const std::string &vs =
2064 "#version 300 es\n"
2065 "#extension GL_OVR_multiview : require\n"
2066 "layout(num_views = 2) in;\n"
2067 "layout(location = 0) in vec3 vPosition;\n"
2068 "void main()\n"
2069 "{\n"
2070 " gl_Position = vec4(vPosition, 1.0);\n"
2071 "}\n";
2072
2073 const std::string &fs =
2074 "#version 300 es\n"
2075 "#extension GL_OVR_multiview : require\n"
2076 "precision mediump float;\n"
2077 "out vec4 col;\n"
2078 "void main()\n"
2079 "{\n"
2080 " col = vec4(1,0,0,1);\n"
2081 "}\n";
2082
2083 ANGLE_GL_PROGRAM(program, vs, fs);
2084
2085 glViewport(0, 0, 1, 1);
2086 glScissor(0, 0, 1, 1);
2087 glEnable(GL_SCISSOR_TEST);
2088 glClearColor(0, 0, 0, 1);
2089
2090 // Bind the default FBO and make sure that the state is synchronized.
2091 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2092 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2093 ASSERT_GL_NO_ERROR();
2094
2095 // Draw and check that both views are rendered to.
2096 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mDrawFramebuffer);
2097 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2098 glUseProgram(program);
2099 drawQuad(program, "vPosition", 0.0f, 1.0f, true);
2100 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 0));
2101 EXPECT_EQ(GLColor::red, GetViewColor(0, 0, 1));
2102}
2103
Martin Radevc1d4e552017-08-21 12:01:10 +03002104MultiviewImplementationParams VertexShaderOpenGL()
Martin Radev3c25ad02017-08-22 17:36:53 +03002105{
Martin Radevc1d4e552017-08-21 12:01:10 +03002106 return MultiviewImplementationParams(false, egl_platform::OPENGL());
Martin Radev3c25ad02017-08-22 17:36:53 +03002107}
2108
Martin Radevc1d4e552017-08-21 12:01:10 +03002109MultiviewImplementationParams VertexShaderD3D11()
Martin Radev3c25ad02017-08-22 17:36:53 +03002110{
Martin Radevc1d4e552017-08-21 12:01:10 +03002111 return MultiviewImplementationParams(false, egl_platform::D3D11());
Martin Radev3c25ad02017-08-22 17:36:53 +03002112}
2113
Martin Radevc1d4e552017-08-21 12:01:10 +03002114MultiviewImplementationParams GeomShaderD3D11()
Martin Radev72b4e1e2017-08-31 15:42:56 +03002115{
Martin Radevc1d4e552017-08-21 12:01:10 +03002116 return MultiviewImplementationParams(true, egl_platform::D3D11());
2117}
2118
2119MultiviewTestParams SideBySideVertexShaderOpenGL()
2120{
2121 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, VertexShaderOpenGL());
2122}
2123
2124MultiviewTestParams LayeredVertexShaderOpenGL()
2125{
2126 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, VertexShaderOpenGL());
2127}
2128
2129MultiviewTestParams SideBySideGeomShaderD3D11()
2130{
2131 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, GeomShaderD3D11());
2132}
2133
2134MultiviewTestParams LayeredGeomShaderD3D11()
2135{
2136 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, GeomShaderD3D11());
2137}
2138
2139MultiviewTestParams SideBySideVertexShaderD3D11()
2140{
2141 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_SIDE_BY_SIDE_ANGLE, VertexShaderD3D11());
2142}
2143
2144MultiviewTestParams LayeredVertexShaderD3D11()
2145{
2146 return MultiviewTestParams(GL_FRAMEBUFFER_MULTIVIEW_LAYERED_ANGLE, VertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002147}
2148
Martin Radev8f276e22017-05-30 12:05:52 +03002149ANGLE_INSTANTIATE_TEST(MultiviewDrawValidationTest, ES31_OPENGL());
Martin Radevced5c862017-08-17 16:05:29 +03002150ANGLE_INSTANTIATE_TEST(MultiviewRenderDualViewTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002151 SideBySideVertexShaderOpenGL(),
2152 LayeredVertexShaderOpenGL(),
2153 SideBySideGeomShaderD3D11(),
2154 SideBySideVertexShaderD3D11(),
2155 LayeredGeomShaderD3D11(),
2156 LayeredVertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002157ANGLE_INSTANTIATE_TEST(MultiviewRenderTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002158 SideBySideVertexShaderOpenGL(),
2159 LayeredVertexShaderOpenGL(),
2160 SideBySideGeomShaderD3D11(),
2161 SideBySideVertexShaderD3D11(),
2162 LayeredGeomShaderD3D11(),
2163 LayeredVertexShaderD3D11());
Martin Radevced5c862017-08-17 16:05:29 +03002164ANGLE_INSTANTIATE_TEST(MultiviewOcclusionQueryTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002165 SideBySideVertexShaderOpenGL(),
2166 LayeredVertexShaderOpenGL(),
2167 SideBySideGeomShaderD3D11(),
2168 SideBySideVertexShaderD3D11(),
2169 LayeredGeomShaderD3D11(),
2170 LayeredVertexShaderD3D11());
Martin Radev72b4e1e2017-08-31 15:42:56 +03002171ANGLE_INSTANTIATE_TEST(MultiviewProgramGenerationTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002172 SideBySideVertexShaderOpenGL(),
2173 LayeredVertexShaderOpenGL(),
2174 SideBySideGeomShaderD3D11(),
2175 SideBySideVertexShaderD3D11(),
2176 LayeredGeomShaderD3D11(),
2177 LayeredVertexShaderD3D11());
Martin Radevced5c862017-08-17 16:05:29 +03002178ANGLE_INSTANTIATE_TEST(MultiviewRenderPrimitiveTest,
Martin Radevc1d4e552017-08-21 12:01:10 +03002179 SideBySideVertexShaderOpenGL(),
2180 LayeredVertexShaderOpenGL(),
2181 SideBySideGeomShaderD3D11(),
2182 SideBySideVertexShaderD3D11(),
2183 LayeredGeomShaderD3D11(),
2184 LayeredVertexShaderD3D11());
2185ANGLE_INSTANTIATE_TEST(MultiviewSideBySideRenderTest, VertexShaderOpenGL(), GeomShaderD3D11());
2186ANGLE_INSTANTIATE_TEST(MultiviewLayeredRenderTest, VertexShaderOpenGL(), GeomShaderD3D11());