Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 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 | // SimpleOperationTest: |
| 7 | // Basic GL commands such as linking a program, initializing a buffer, etc. |
| 8 | |
Corentin Wallez | d3970de | 2015-05-14 11:07:48 -0400 | [diff] [blame] | 9 | #include "test_utils/ANGLETest.h" |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 10 | |
| 11 | #include <vector> |
| 12 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 13 | #include "random_utils.h" |
| 14 | #include "test_utils/gl_raii.h" |
| 15 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 16 | using namespace angle; |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 17 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 21 | class SimpleOperationTest : public ANGLETest |
| 22 | { |
| 23 | protected: |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 24 | SimpleOperationTest() |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 25 | { |
| 26 | setWindowWidth(128); |
| 27 | setWindowHeight(128); |
| 28 | setConfigRedBits(8); |
| 29 | setConfigGreenBits(8); |
| 30 | setConfigBlueBits(8); |
| 31 | setConfigAlphaBits(8); |
| 32 | } |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 33 | |
| 34 | void verifyBuffer(const std::vector<uint8_t> &data, GLenum binding); |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 35 | }; |
| 36 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 37 | void SimpleOperationTest::verifyBuffer(const std::vector<uint8_t> &data, GLenum binding) |
| 38 | { |
| 39 | if (!extensionEnabled("GL_EXT_map_buffer_range")) |
| 40 | { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | uint8_t *mapPointer = |
| 45 | static_cast<uint8_t *>(glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, 1024, GL_MAP_READ_BIT)); |
| 46 | ASSERT_GL_NO_ERROR(); |
| 47 | |
| 48 | std::vector<uint8_t> readbackData(data.size()); |
| 49 | memcpy(readbackData.data(), mapPointer, data.size()); |
| 50 | glUnmapBufferOES(GL_ARRAY_BUFFER); |
| 51 | |
| 52 | EXPECT_EQ(data, readbackData); |
| 53 | } |
| 54 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 55 | TEST_P(SimpleOperationTest, CompileVertexShader) |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 56 | { |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 57 | const std::string source = |
| 58 | R"(attribute vec4 a_input; |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 59 | void main() |
| 60 | { |
| 61 | gl_Position = a_input; |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 62 | })"; |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 63 | |
| 64 | GLuint shader = CompileShader(GL_VERTEX_SHADER, source); |
| 65 | EXPECT_NE(shader, 0u); |
| 66 | glDeleteShader(shader); |
| 67 | |
| 68 | EXPECT_GL_NO_ERROR(); |
| 69 | } |
| 70 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 71 | TEST_P(SimpleOperationTest, CompileFragmentShader) |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 72 | { |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 73 | const std::string source = |
| 74 | R"(precision mediump float; |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 75 | varying vec4 v_input; |
| 76 | void main() |
| 77 | { |
| 78 | gl_FragColor = v_input; |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 79 | })"; |
Geoff Lang | f1e8592 | 2015-02-23 14:40:04 -0500 | [diff] [blame] | 80 | |
| 81 | GLuint shader = CompileShader(GL_FRAGMENT_SHADER, source); |
| 82 | EXPECT_NE(shader, 0u); |
| 83 | glDeleteShader(shader); |
| 84 | |
| 85 | EXPECT_GL_NO_ERROR(); |
| 86 | } |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 87 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 88 | TEST_P(SimpleOperationTest, LinkProgram) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 89 | { |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 90 | const std::string vsSource = |
| 91 | R"(void main() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 92 | { |
| 93 | gl_Position = vec4(1.0, 1.0, 1.0, 1.0); |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 94 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 95 | |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 96 | const std::string fsSource = |
| 97 | R"(void main() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 98 | { |
| 99 | gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 100 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 101 | |
| 102 | GLuint program = CompileProgram(vsSource, fsSource); |
| 103 | EXPECT_NE(program, 0u); |
| 104 | glDeleteProgram(program); |
| 105 | |
| 106 | EXPECT_GL_NO_ERROR(); |
| 107 | } |
| 108 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 109 | TEST_P(SimpleOperationTest, LinkProgramWithUniforms) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 110 | { |
Jamie Madill | b8353b0 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 111 | if (IsVulkan()) |
| 112 | { |
| 113 | // TODO(jmadill): Complete Vulkan implementation. |
| 114 | std::cout << "Test skipped on Vulkan." << std::endl; |
| 115 | return; |
| 116 | } |
| 117 | |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 118 | const std::string vsSource = |
| 119 | R"(void main() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 120 | { |
| 121 | gl_Position = vec4(1.0, 1.0, 1.0, 1.0); |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 122 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 123 | |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 124 | const std::string fsSource = |
| 125 | R"(precision mediump float; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 126 | uniform vec4 u_input; |
| 127 | void main() |
| 128 | { |
| 129 | gl_FragColor = u_input; |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 130 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 131 | |
| 132 | GLuint program = CompileProgram(vsSource, fsSource); |
| 133 | EXPECT_NE(program, 0u); |
| 134 | |
| 135 | GLint uniformLoc = glGetUniformLocation(program, "u_input"); |
| 136 | EXPECT_NE(-1, uniformLoc); |
| 137 | |
| 138 | glDeleteProgram(program); |
| 139 | |
| 140 | EXPECT_GL_NO_ERROR(); |
| 141 | } |
| 142 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 143 | TEST_P(SimpleOperationTest, LinkProgramWithAttributes) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 144 | { |
Jamie Madill | b8353b0 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 145 | if (IsVulkan()) |
| 146 | { |
| 147 | // TODO(jmadill): Complete Vulkan implementation. |
| 148 | std::cout << "Test skipped on Vulkan." << std::endl; |
| 149 | return; |
| 150 | } |
| 151 | |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 152 | const std::string vsSource = |
| 153 | R"(attribute vec4 a_input; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 154 | void main() |
| 155 | { |
| 156 | gl_Position = a_input; |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 157 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 158 | |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 159 | const std::string fsSource = |
| 160 | R"(void main() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 161 | { |
| 162 | gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); |
Olli Etuaho | a20af6d | 2017-09-18 13:32:29 +0300 | [diff] [blame^] | 163 | })"; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 164 | |
| 165 | GLuint program = CompileProgram(vsSource, fsSource); |
| 166 | EXPECT_NE(program, 0u); |
| 167 | |
| 168 | GLint attribLoc = glGetAttribLocation(program, "a_input"); |
| 169 | EXPECT_NE(-1, attribLoc); |
| 170 | |
| 171 | glDeleteProgram(program); |
| 172 | |
| 173 | EXPECT_GL_NO_ERROR(); |
| 174 | } |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 175 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 176 | TEST_P(SimpleOperationTest, BufferDataWithData) |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 177 | { |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 178 | GLBuffer buffer; |
| 179 | glBindBuffer(GL_ARRAY_BUFFER, buffer.get()); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 180 | |
| 181 | std::vector<uint8_t> data(1024); |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 182 | FillVectorWithRandomUBytes(&data); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 183 | glBufferData(GL_ARRAY_BUFFER, data.size(), &data[0], GL_STATIC_DRAW); |
| 184 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 185 | verifyBuffer(data, GL_ARRAY_BUFFER); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 186 | |
| 187 | EXPECT_GL_NO_ERROR(); |
| 188 | } |
| 189 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 190 | TEST_P(SimpleOperationTest, BufferDataWithNoData) |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 191 | { |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 192 | GLBuffer buffer; |
| 193 | glBindBuffer(GL_ARRAY_BUFFER, buffer.get()); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 194 | glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_STATIC_DRAW); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 195 | |
| 196 | EXPECT_GL_NO_ERROR(); |
| 197 | } |
| 198 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 199 | TEST_P(SimpleOperationTest, BufferSubData) |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 200 | { |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 201 | GLBuffer buffer; |
| 202 | glBindBuffer(GL_ARRAY_BUFFER, buffer.get()); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 203 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 204 | constexpr size_t bufferSize = 1024; |
| 205 | std::vector<uint8_t> data(bufferSize); |
| 206 | FillVectorWithRandomUBytes(&data); |
| 207 | |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 208 | glBufferData(GL_ARRAY_BUFFER, bufferSize, nullptr, GL_STATIC_DRAW); |
| 209 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 210 | constexpr size_t subDataCount = 16; |
| 211 | constexpr size_t sliceSize = bufferSize / subDataCount; |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 212 | for (size_t i = 0; i < subDataCount; i++) |
| 213 | { |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 214 | size_t offset = i * sliceSize; |
| 215 | glBufferSubData(GL_ARRAY_BUFFER, offset, sliceSize, &data[offset]); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Jamie Madill | cc6ac25 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 218 | verifyBuffer(data, GL_ARRAY_BUFFER); |
Geoff Lang | 36c7901 | 2015-02-24 11:47:20 -0500 | [diff] [blame] | 219 | |
| 220 | EXPECT_GL_NO_ERROR(); |
| 221 | } |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 222 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 223 | // Simple quad test. |
| 224 | TEST_P(SimpleOperationTest, DrawQuad) |
| 225 | { |
| 226 | const std::string &vertexShader = |
| 227 | "attribute vec3 position;\n" |
| 228 | "void main()\n" |
| 229 | "{\n" |
| 230 | " gl_Position = vec4(position, 1);\n" |
| 231 | "}"; |
| 232 | const std::string &fragmentShader = |
| 233 | "void main()\n" |
| 234 | "{\n" |
| 235 | " gl_FragColor = vec4(0, 1, 0, 1);\n" |
| 236 | "}"; |
| 237 | ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader); |
| 238 | |
| 239 | drawQuad(program.get(), "position", 0.5f, 1.0f, true); |
| 240 | |
| 241 | EXPECT_GL_NO_ERROR(); |
| 242 | EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green); |
| 243 | } |
| 244 | |
| 245 | // Simple repeatd draw and swap test. |
| 246 | TEST_P(SimpleOperationTest, DrawQuadAndSwap) |
| 247 | { |
| 248 | const std::string &vertexShader = |
| 249 | "attribute vec3 position;\n" |
| 250 | "void main()\n" |
| 251 | "{\n" |
| 252 | " gl_Position = vec4(position, 1);\n" |
| 253 | "}"; |
| 254 | const std::string &fragmentShader = |
| 255 | "void main()\n" |
| 256 | "{\n" |
| 257 | " gl_FragColor = vec4(0, 1, 0, 1);\n" |
| 258 | "}"; |
| 259 | ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader); |
| 260 | |
| 261 | for (int i = 0; i < 8; ++i) |
| 262 | { |
| 263 | drawQuad(program.get(), "position", 0.5f, 1.0f, true); |
| 264 | EXPECT_GL_NO_ERROR(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 265 | EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 266 | swapBuffers(); |
| 267 | } |
| 268 | |
| 269 | EXPECT_GL_NO_ERROR(); |
| 270 | } |
| 271 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 272 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 273 | ANGLE_INSTANTIATE_TEST(SimpleOperationTest, |
| 274 | ES2_D3D9(), |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 275 | ES2_D3D11(EGL_EXPERIMENTAL_PRESENT_PATH_COPY_ANGLE), |
| 276 | ES2_D3D11(EGL_EXPERIMENTAL_PRESENT_PATH_FAST_ANGLE), |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 277 | ES3_D3D11(), |
| 278 | ES2_OPENGL(), |
| 279 | ES3_OPENGL(), |
| 280 | ES2_OPENGLES(), |
Jamie Madill | b8353b0 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 281 | ES3_OPENGLES(), |
| 282 | ES2_VULKAN()); |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 283 | |
| 284 | } // namespace |