|
| 1 | +// |
| 2 | +// Copyright 2015 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 | + |
| 7 | +#include "test_utils/ANGLETest.h" |
| 8 | + |
| 9 | +#include "test_utils/gl_raii.h" |
| 10 | +#include "util/shader_utils.h" |
| 11 | + |
| 12 | +using namespace angle; |
| 13 | + |
| 14 | +namespace |
| 15 | +{ |
| 16 | +class TimeoutDrawTest : public ANGLETest<> |
| 17 | +{ |
| 18 | + protected: |
| 19 | + TimeoutDrawTest() |
| 20 | + { |
| 21 | + setWindowWidth(128); |
| 22 | + setWindowHeight(128); |
| 23 | + setConfigRedBits(8); |
| 24 | + setConfigGreenBits(8); |
| 25 | + setConfigBlueBits(8); |
| 26 | + setConfigAlphaBits(8); |
| 27 | + // Tests should skip if robustness not supported, but this can be done only after |
| 28 | + // Metal supports robustness. |
| 29 | + if (IsEGLClientExtensionEnabled("EGL_EXT_create_context_robustness")) |
| 30 | + { |
| 31 | + setContextResetStrategy(EGL_LOSE_CONTEXT_ON_RESET_EXT); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + setContextResetStrategy(EGL_NO_RESET_NOTIFICATION_EXT); |
| 36 | + } |
| 37 | + } |
| 38 | + void testSetUp() override |
| 39 | + { |
| 40 | + glClear(GL_COLOR_BUFFER_BIT); |
| 41 | + glFinish(); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// Tests that trivial infinite loops in vertex shaders hang instead of progress. |
| 46 | +TEST_P(TimeoutDrawTest, TrivialInfiniteLoopVS) |
| 47 | +{ |
| 48 | + constexpr char kVS[] = R"(precision highp float; |
| 49 | +attribute vec4 a_position; |
| 50 | +void main() |
| 51 | +{ |
| 52 | + for (;;) {} |
| 53 | + gl_Position = a_position; |
| 54 | +})"; |
| 55 | + ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red()); |
| 56 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 57 | + glFinish(); |
| 58 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 59 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 60 | +} |
| 61 | + |
| 62 | +// Tests that trivial infinite loops in fragment shaders hang instead of progress. |
| 63 | +TEST_P(TimeoutDrawTest, TrivialInfiniteLoopFS) |
| 64 | +{ |
| 65 | + constexpr char kFS[] = R"(precision mediump float; |
| 66 | +void main() |
| 67 | +{ |
| 68 | + for (;;) {} |
| 69 | + gl_FragColor = vec4(1, 0, 0, 1); |
| 70 | +})"; |
| 71 | + ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS); |
| 72 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 73 | + glFinish(); |
| 74 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 75 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +// Tests that infinite loops based on user-supplied values in vertex shaders hang instead of progress. |
| 80 | +// Otherwise optimizer would be able to assume something about the domain of the user-supplied value. |
| 81 | +TEST_P(TimeoutDrawTest, DynamicInfiniteLoopVS) |
| 82 | +{ |
| 83 | + constexpr char kVS[] = R"(precision highp float; |
| 84 | +attribute vec4 a_position; |
| 85 | +uniform int f; |
| 86 | +void main() |
| 87 | +{ |
| 88 | + for (;f != 0;) {} |
| 89 | + gl_Position = a_position; |
| 90 | +})"; |
| 91 | + ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red()); |
| 92 | + |
| 93 | + glUseProgram(program); |
| 94 | + GLint uniformLocation = glGetUniformLocation(program, "f"); |
| 95 | + glUniform1i(uniformLocation, 77); |
| 96 | + |
| 97 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 98 | + glFinish(); |
| 99 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 100 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 101 | +} |
| 102 | + |
| 103 | +// Tests that infinite loops based on user-supplied values in fragment shaders hang instead of progress. |
| 104 | +// Otherwise optimizer would be able to assume something about the domain of the user-supplied value. |
| 105 | +TEST_P(TimeoutDrawTest, DynamicInfiniteLoopFS) |
| 106 | +{ |
| 107 | + constexpr char kFS[] = R"(precision mediump float; |
| 108 | +uniform int f; |
| 109 | +void main() |
| 110 | +{ |
| 111 | + for (;f != 0;) {} |
| 112 | + gl_FragColor = vec4(1, 0, 0, 1); |
| 113 | +})"; |
| 114 | + ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS); |
| 115 | + glUseProgram(program); |
| 116 | + GLint uniformLocation = glGetUniformLocation(program, "f"); |
| 117 | + glUniform1i(uniformLocation, 88); |
| 118 | + EXPECT_GL_NO_ERROR(); |
| 119 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 120 | + glFinish(); |
| 121 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 122 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 123 | +} |
| 124 | + |
| 125 | +// Tests that infinite loops based on user-supplied values in vertex shaders hang instead of progress. |
| 126 | +// Otherwise optimizer would be able to assume something about the domain of the user-supplied value. |
| 127 | +// Explicit value break variant. |
| 128 | +TEST_P(TimeoutDrawTest, DynamicInfiniteLoop2VS) |
| 129 | +{ |
| 130 | + constexpr char kVS[] = R"(precision highp float; |
| 131 | +attribute vec4 a_position; |
| 132 | +uniform int f; |
| 133 | +void main() |
| 134 | +{ |
| 135 | + for (;;) { if (f <= 1) break; } |
| 136 | + gl_Position = a_position; |
| 137 | +})"; |
| 138 | + ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red()); |
| 139 | + glUseProgram(program); |
| 140 | + GLint uniformLocation = glGetUniformLocation(program, "f"); |
| 141 | + glUniform1i(uniformLocation, 66); |
| 142 | + EXPECT_GL_NO_ERROR(); |
| 143 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 144 | + glFinish(); |
| 145 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 146 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 147 | +} |
| 148 | + |
| 149 | +// Tests that infinite loops based on user-supplied values in fragment shaders hang instead of progress. |
| 150 | +// Otherwise optimizer would be able to assume something about the domain of the user-supplied value. |
| 151 | +// Explicit value break variant. |
| 152 | +TEST_P(TimeoutDrawTest, DynamicInfiniteLoop2FS) |
| 153 | +{ |
| 154 | + constexpr char kFS[] = R"(precision mediump float; |
| 155 | +uniform float f; |
| 156 | +void main() |
| 157 | +{ |
| 158 | + for (;;) { if (f < 0.1) break; } |
| 159 | + gl_FragColor = vec4(1, 0, f, 1); |
| 160 | +})"; |
| 161 | + ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), kFS); |
| 162 | + glUseProgram(program); |
| 163 | + GLint uniformLocation = glGetUniformLocation(program, "f"); |
| 164 | + glUniform1f(uniformLocation, .5f); |
| 165 | + EXPECT_GL_NO_ERROR(); |
| 166 | + drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f); |
| 167 | + glFinish(); |
| 168 | + EXPECT_GL_ERROR(GL_CONTEXT_LOST); |
| 169 | + EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack); // Should read through client buffer since context should be lost. |
| 170 | +} |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +ANGLE_INSTANTIATE_TEST(TimeoutDrawTest, |
| 175 | + WithRobustness(ES2_METAL().enable(Feature::InjectAsmStatementIntoLoopBodies)), |
| 176 | + WithRobustness(ES3_METAL().enable(Feature::InjectAsmStatementIntoLoopBodies))); |
0 commit comments