blob: 7671e36f037feb8f67c02af3232919dc63e161f2 [file] [log] [blame]
Martin Radev4c4c8e72016-08-04 12:25:34 +03001//
2// Copyright 2016 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// ComputeShaderTest:
7// Compute shader specific tests.
8
Xinghua Cao08a8ec82017-12-27 13:31:11 +08009#include <vector>
Martin Radev4c4c8e72016-08-04 12:25:34 +030010#include "test_utils/ANGLETest.h"
11#include "test_utils/gl_raii.h"
Martin Radev4c4c8e72016-08-04 12:25:34 +030012
13using namespace angle;
14
15namespace
16{
17
18class ComputeShaderTest : public ANGLETest
19{
20 protected:
21 ComputeShaderTest() {}
22};
23
24class ComputeShaderTestES3 : public ANGLETest
25{
26 protected:
27 ComputeShaderTestES3() {}
28};
29
30// link a simple compute program. It should be successful.
31TEST_P(ComputeShaderTest, LinkComputeProgram)
32{
33 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +080034 R"(#version 310 es
35 layout(local_size_x=1) in;
36 void main()
37 {\
38 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +030039
40 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
41
42 EXPECT_GL_NO_ERROR();
43}
44
Yunchao He85072e82017-11-14 15:43:28 +080045// Link a simple compute program. Then detach the shader and dispatch compute.
46// It should be successful.
47TEST_P(ComputeShaderTest, DetachShaderAfterLinkSuccess)
48{
49 const std::string csSource =
50 R"(#version 310 es
51 layout(local_size_x=1) in;
52 void main()
53 {
54 })";
55
56 GLuint program = glCreateProgram();
57
58 GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
59 EXPECT_NE(0u, cs);
60
61 glAttachShader(program, cs);
62 glDeleteShader(cs);
63
64 glLinkProgram(program);
65 GLint linkStatus;
66 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
Yunchao He1f679cc2017-11-29 18:06:00 +080067 EXPECT_GL_TRUE(linkStatus);
Yunchao He85072e82017-11-14 15:43:28 +080068
69 glDetachShader(program, cs);
70 EXPECT_GL_NO_ERROR();
71
72 glUseProgram(program);
73 glDispatchCompute(8, 4, 2);
74 EXPECT_GL_NO_ERROR();
75}
76
Martin Radev4c4c8e72016-08-04 12:25:34 +030077// link a simple compute program. There is no local size and linking should fail.
78TEST_P(ComputeShaderTest, LinkComputeProgramNoLocalSizeLinkError)
79{
80 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +080081 R"(#version 310 es
82 void main()
83 {
84 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +030085
86 GLuint program = CompileComputeProgram(csSource, false);
87 EXPECT_EQ(0u, program);
88
89 glDeleteProgram(program);
90
91 EXPECT_GL_NO_ERROR();
92}
93
94// link a simple compute program.
95// make sure that uniforms and uniform samplers get recorded
96TEST_P(ComputeShaderTest, LinkComputeProgramWithUniforms)
97{
98 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +020099 R"(#version 310 es
100 precision mediump sampler2D;
101 layout(local_size_x=1) in;
102 uniform int myUniformInt;
103 uniform sampler2D myUniformSampler;
104 layout(rgba32i) uniform highp writeonly iimage2D imageOut;
105 void main()
106 {
107 int q = myUniformInt;
108 vec4 v = textureLod(myUniformSampler, vec2(0.0), 0.0);
109 imageStore(imageOut, ivec2(0), ivec4(v) * q);
110 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300111
112 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
113
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200114 GLint uniformLoc = glGetUniformLocation(program.get(), "myUniformInt");
115 EXPECT_NE(-1, uniformLoc);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300116
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200117 uniformLoc = glGetUniformLocation(program.get(), "myUniformSampler");
118 EXPECT_NE(-1, uniformLoc);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300119
120 EXPECT_GL_NO_ERROR();
121}
122
123// Attach both compute and non-compute shaders. A link time error should occur.
124// OpenGL ES 3.10, 7.3 Program Objects
125TEST_P(ComputeShaderTest, AttachMultipleShaders)
126{
127 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800128 R"(#version 310 es
129 layout(local_size_x=1) in;
130 void main()
131 {
132 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300133
134 const std::string vsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800135 R"(#version 310 es
136 void main()
137 {
138 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300139
140 const std::string fsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800141 R"(#version 310 es
142 void main()
143 {
144 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300145
146 GLuint program = glCreateProgram();
147
148 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
149 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
150 GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
151
152 EXPECT_NE(0u, vs);
153 EXPECT_NE(0u, fs);
154 EXPECT_NE(0u, cs);
155
156 glAttachShader(program, vs);
157 glDeleteShader(vs);
158
159 glAttachShader(program, fs);
160 glDeleteShader(fs);
161
162 glAttachShader(program, cs);
163 glDeleteShader(cs);
164
165 glLinkProgram(program);
166
167 GLint linkStatus;
168 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
169
Yunchao He1f679cc2017-11-29 18:06:00 +0800170 EXPECT_GL_FALSE(linkStatus);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300171
172 EXPECT_GL_NO_ERROR();
173}
174
175// Attach a vertex, fragment and compute shader.
176// Query for the number of attached shaders and check the count.
177TEST_P(ComputeShaderTest, AttachmentCount)
178{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300179 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800180 R"(#version 310 es
181 layout(local_size_x=1) in;
182 void main()
183 {
184 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300185
186 const std::string vsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800187 R"(#version 310 es
188 void main()
189 {
190 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300191
192 const std::string fsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800193 R"(#version 310 es
194 void main()
195 {
196 })";
Martin Radev4c4c8e72016-08-04 12:25:34 +0300197
198 GLuint program = glCreateProgram();
199
200 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
201 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
202 GLuint cs = CompileShader(GL_COMPUTE_SHADER, csSource);
203
204 EXPECT_NE(0u, vs);
205 EXPECT_NE(0u, fs);
206 EXPECT_NE(0u, cs);
207
208 glAttachShader(program, vs);
209 glDeleteShader(vs);
210
211 glAttachShader(program, fs);
212 glDeleteShader(fs);
213
214 glAttachShader(program, cs);
215 glDeleteShader(cs);
216
217 GLint numAttachedShaders;
218 glGetProgramiv(program, GL_ATTACHED_SHADERS, &numAttachedShaders);
219
220 EXPECT_EQ(3, numAttachedShaders);
221
222 glDeleteProgram(program);
223
224 EXPECT_GL_NO_ERROR();
225}
226
Yunchao Hecddcb592017-11-13 15:27:35 +0800227// Attach a compute shader and link, but start rendering.
228TEST_P(ComputeShaderTest, StartRenderingWithComputeProgram)
229{
230 const std::string csSource =
231 R"(#version 310 es
232 layout(local_size_x=1) in;
233 void main()
234 {
235 })";
236
237 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
238 EXPECT_GL_NO_ERROR();
239
240 glUseProgram(program);
241 glDrawArrays(GL_POINTS, 0, 2);
242 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
243}
244
Yunchao He70b715c2017-11-07 14:59:15 +0800245// Attach a vertex and fragment shader and link, but dispatch compute.
246TEST_P(ComputeShaderTest, DispatchComputeWithRenderingProgram)
247{
248 const std::string vsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800249 R"(#version 310 es
250 void main()
251 {
252 })";
Yunchao He70b715c2017-11-07 14:59:15 +0800253
254 const std::string fsSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800255 R"(#version 310 es
256 void main()
257 {
258 })";
Yunchao He70b715c2017-11-07 14:59:15 +0800259
260 GLuint program = glCreateProgram();
261
262 GLuint vs = CompileShader(GL_VERTEX_SHADER, vsSource);
263 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fsSource);
264
265 EXPECT_NE(0u, vs);
266 EXPECT_NE(0u, fs);
267
268 glAttachShader(program, vs);
269 glDeleteShader(vs);
270
271 glAttachShader(program, fs);
272 glDeleteShader(fs);
273
274 glLinkProgram(program);
275
276 GLint linkStatus;
277 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
Yunchao He1f679cc2017-11-29 18:06:00 +0800278 EXPECT_GL_TRUE(linkStatus);
Yunchao He70b715c2017-11-07 14:59:15 +0800279
280 EXPECT_GL_NO_ERROR();
281
282 glUseProgram(program);
283 glDispatchCompute(8, 4, 2);
284 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
285}
286
Xinghua Caob1239382016-12-13 15:07:05 +0800287// Access all compute shader special variables.
288TEST_P(ComputeShaderTest, AccessAllSpecialVariables)
289{
290 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200291 R"(#version 310 es
292 layout(local_size_x=4, local_size_y=3, local_size_z=2) in;
293 layout(rgba32ui) uniform highp writeonly uimage2D imageOut;
294 void main()
295 {
296 uvec3 temp1 = gl_NumWorkGroups;
297 uvec3 temp2 = gl_WorkGroupSize;
298 uvec3 temp3 = gl_WorkGroupID;
299 uvec3 temp4 = gl_LocalInvocationID;
300 uvec3 temp5 = gl_GlobalInvocationID;
301 uint temp6 = gl_LocalInvocationIndex;
302 imageStore(imageOut, ivec2(gl_LocalInvocationIndex, 0), uvec4(temp1 + temp2 + temp3 + temp4 + temp5, temp6));
303 })";
Xinghua Caob1239382016-12-13 15:07:05 +0800304
305 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
306}
307
308// Access part compute shader special variables.
309TEST_P(ComputeShaderTest, AccessPartSpecialVariables)
310{
311 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200312 R"(#version 310 es
313 layout(local_size_x=4, local_size_y=3, local_size_z=2) in;
314 layout(rgba32ui) uniform highp writeonly uimage2D imageOut;
315 void main()
316 {
317 uvec3 temp1 = gl_WorkGroupSize;
318 uvec3 temp2 = gl_WorkGroupID;
319 uint temp3 = gl_LocalInvocationIndex;
320 imageStore(imageOut, ivec2(gl_LocalInvocationIndex, 0), uvec4(temp1 + temp2, temp3));
321 })";
Xinghua Caob1239382016-12-13 15:07:05 +0800322
323 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
324}
325
Xinghua Cao73badc02017-03-29 19:14:53 +0800326// Use glDispatchCompute to define work group count.
327TEST_P(ComputeShaderTest, DispatchCompute)
Xinghua Cao2b396592017-03-29 15:36:04 +0800328{
329 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200330 R"(#version 310 es
331 layout(local_size_x=4, local_size_y=3, local_size_z=2) in;
332 layout(rgba32ui) uniform highp writeonly uimage2D imageOut;
333 void main()
334 {
335 uvec3 temp = gl_NumWorkGroups;
336 imageStore(imageOut, ivec2(0), uvec4(temp, 0u));
337 })";
Xinghua Cao2b396592017-03-29 15:36:04 +0800338
339 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
340
341 glUseProgram(program.get());
342 glDispatchCompute(8, 4, 2);
343 EXPECT_GL_NO_ERROR();
344}
345
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800346// Use image uniform to write texture in compute shader, and verify the content is expected.
347TEST_P(ComputeShaderTest, BindImageTexture)
348{
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800349 GLTexture mTexture[2];
350 GLFramebuffer mFramebuffer;
351 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800352 R"(#version 310 es
Xinghua Cao27f43212018-02-26 11:05:53 +0800353 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
Yunchao Hecddcb592017-11-13 15:27:35 +0800354 layout(r32ui, binding = 0) writeonly uniform highp uimage2D uImage[2];
355 void main()
356 {
357 imageStore(uImage[0], ivec2(gl_LocalInvocationIndex, gl_WorkGroupID.x), uvec4(100, 0,
358 0, 0));
359 imageStore(uImage[1], ivec2(gl_LocalInvocationIndex, gl_WorkGroupID.x), uvec4(100, 0,
360 0, 0));
361 })";
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800362
363 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
364 glUseProgram(program.get());
Xinghua Cao27f43212018-02-26 11:05:53 +0800365 int width = 1, height = 1;
366 GLuint inputValues[] = {200};
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800367
368 glBindTexture(GL_TEXTURE_2D, mTexture[0]);
369 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, width, height);
370 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED_INTEGER, GL_UNSIGNED_INT,
371 inputValues);
372 EXPECT_GL_NO_ERROR();
373
374 glBindImageTexture(0, mTexture[0], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
375 EXPECT_GL_NO_ERROR();
376
377 glBindTexture(GL_TEXTURE_2D, mTexture[1]);
378 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, width, height);
379 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED_INTEGER, GL_UNSIGNED_INT,
380 inputValues);
381 EXPECT_GL_NO_ERROR();
382
383 glBindImageTexture(1, mTexture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
384 EXPECT_GL_NO_ERROR();
385
Xinghua Cao27f43212018-02-26 11:05:53 +0800386 glDispatchCompute(1, 1, 1);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800387 EXPECT_GL_NO_ERROR();
388
Xinghua Cao27f43212018-02-26 11:05:53 +0800389 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800390 glUseProgram(0);
Xinghua Cao27f43212018-02-26 11:05:53 +0800391 GLuint outputValues[2][1];
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800392 GLuint expectedValue = 100;
393 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
394
395 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture[0],
396 0);
397 EXPECT_GL_NO_ERROR();
398 glReadPixels(0, 0, width, height, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues[0]);
399 EXPECT_GL_NO_ERROR();
400
401 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture[1],
402 0);
403 EXPECT_GL_NO_ERROR();
404 glReadPixels(0, 0, width, height, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues[1]);
405 EXPECT_GL_NO_ERROR();
406
407 for (int i = 0; i < width * height; i++)
408 {
409 EXPECT_EQ(expectedValue, outputValues[0][i]);
410 EXPECT_EQ(expectedValue, outputValues[1][i]);
411 }
412}
413
Xinghua Cao0328b572017-06-26 15:51:36 +0800414// When declare a image array without a binding qualifier, all elements are bound to unit zero.
415TEST_P(ComputeShaderTest, ImageArrayWithoutBindingQualifier)
416{
417 ANGLE_SKIP_TEST_IF(IsD3D11());
418
419 // TODO(xinghua.cao@intel.com): On AMD desktop OpenGL, bind two image variables to unit 0,
420 // only one variable is valid.
Corentin Wallez2bde9192017-07-28 14:15:01 -0400421 ANGLE_SKIP_TEST_IF(IsAMD() && IsDesktopOpenGL());
Xinghua Cao0328b572017-06-26 15:51:36 +0800422
423 GLTexture mTexture;
424 GLFramebuffer mFramebuffer;
425 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800426 R"(#version 310 es
Xinghua Cao27f43212018-02-26 11:05:53 +0800427 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
Yunchao Hecddcb592017-11-13 15:27:35 +0800428 layout(r32ui) writeonly uniform highp uimage2D uImage[2];
429 void main()
430 {
431 imageStore(uImage[0], ivec2(gl_LocalInvocationIndex, 0), uvec4(100, 0, 0, 0));
432 imageStore(uImage[1], ivec2(gl_LocalInvocationIndex, 1), uvec4(100, 0, 0, 0));
433 })";
Xinghua Cao0328b572017-06-26 15:51:36 +0800434
435 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
436 glUseProgram(program.get());
Xinghua Cao27f43212018-02-26 11:05:53 +0800437 constexpr int kTextureWidth = 1, kTextureHeight = 2;
438 GLuint inputValues[] = {200, 200};
Xinghua Cao0328b572017-06-26 15:51:36 +0800439
440 glBindTexture(GL_TEXTURE_2D, mTexture);
441 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, kTextureWidth, kTextureHeight);
442 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kTextureWidth, kTextureHeight, GL_RED_INTEGER,
443 GL_UNSIGNED_INT, inputValues);
444 EXPECT_GL_NO_ERROR();
445
446 glBindImageTexture(0, mTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
447 glDispatchCompute(1, 1, 1);
448 EXPECT_GL_NO_ERROR();
449
Xinghua Cao27f43212018-02-26 11:05:53 +0800450 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
Xinghua Cao0328b572017-06-26 15:51:36 +0800451 glUseProgram(0);
452 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
453
454 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture, 0);
Xinghua Cao27f43212018-02-26 11:05:53 +0800455 GLuint outputValues[kTextureWidth * kTextureHeight];
Xinghua Cao0328b572017-06-26 15:51:36 +0800456 glReadPixels(0, 0, kTextureWidth, kTextureHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
457 outputValues);
458 EXPECT_GL_NO_ERROR();
459
460 GLuint expectedValue = 100;
461 for (int i = 0; i < kTextureWidth * kTextureHeight; i++)
462 {
463 EXPECT_EQ(expectedValue, outputValues[i]);
464 }
465}
466
Xinghua Cao711b7a12017-10-09 13:38:12 +0800467// imageLoad functions
468TEST_P(ComputeShaderTest, ImageLoad)
469{
470 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200471 R"(#version 310 es
472 layout(local_size_x=8) in;
473 layout(rgba8) uniform highp readonly image2D mImage2DInput;
474 layout(rgba16i) uniform highp readonly iimageCube mImageCubeInput;
475 layout(rgba32ui) uniform highp readonly uimage3D mImage3DInput;
476 layout(r32i) uniform highp writeonly iimage2D imageOut;
477 void main()
478 {
479 vec4 result2d = imageLoad(mImage2DInput, ivec2(gl_LocalInvocationID.xy));
480 ivec4 resultCube = imageLoad(mImageCubeInput, ivec3(gl_LocalInvocationID.xyz));
481 uvec4 result3d = imageLoad(mImage3DInput, ivec3(gl_LocalInvocationID.xyz));
482 imageStore(imageOut, ivec2(gl_LocalInvocationIndex, 0), ivec4(result2d) + resultCube + ivec4(result3d));
483 })";
Xinghua Cao711b7a12017-10-09 13:38:12 +0800484
485 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
486 EXPECT_GL_NO_ERROR();
487}
488
489// imageStore functions
490TEST_P(ComputeShaderTest, ImageStore)
491{
492 const std::string csSource =
Yunchao Hecddcb592017-11-13 15:27:35 +0800493 R"(#version 310 es
494 layout(local_size_x=8) in;
495 layout(rgba16f) uniform highp writeonly imageCube mImageCubeOutput;
496 layout(r32f) uniform highp writeonly image3D mImage3DOutput;
497 layout(rgba8ui) uniform highp writeonly uimage2DArray mImage2DArrayOutput;
498 void main()
499 {
500 imageStore(mImageCubeOutput, ivec3(gl_LocalInvocationID.xyz), vec4(0.0));
501 imageStore(mImage3DOutput, ivec3(gl_LocalInvocationID.xyz), vec4(0.0));
502 imageStore(mImage2DArrayOutput, ivec3(gl_LocalInvocationID.xyz), uvec4(0));
503 })";
Xinghua Cao711b7a12017-10-09 13:38:12 +0800504
505 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
506 EXPECT_GL_NO_ERROR();
507}
508
509// imageSize functions
510TEST_P(ComputeShaderTest, ImageSize)
511{
512 const std::string csSource =
Olli Etuaho7caa80e2017-11-14 15:03:14 +0200513 R"(#version 310 es
514 layout(local_size_x=8) in;
515 layout(rgba8) uniform highp readonly imageCube mImageCubeInput;
516 layout(r32i) uniform highp readonly iimage2D mImage2DInput;
517 layout(rgba16ui) uniform highp readonly uimage2DArray mImage2DArrayInput;
518 layout(r32i) uniform highp writeonly iimage2D imageOut;
519 void main()
520 {
521 ivec2 sizeCube = imageSize(mImageCubeInput);
522 ivec2 size2D = imageSize(mImage2DInput);
523 ivec3 size2DArray = imageSize(mImage2DArrayInput);
524 imageStore(imageOut, ivec2(gl_LocalInvocationIndex, 0), ivec4(sizeCube, size2D.x, size2DArray.x));
525 })";
Xinghua Cao711b7a12017-10-09 13:38:12 +0800526
527 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
528 EXPECT_GL_NO_ERROR();
529}
530
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800531// Use image uniform to read and write Texture2D in compute shader, and verify the contents.
Xinghua Cao26143fd2017-11-01 18:19:05 +0800532TEST_P(ComputeShaderTest, BindImageTextureWithTexture2D)
533{
534 GLTexture texture[2];
535 GLFramebuffer framebuffer;
536 const std::string csSource =
537 R"(#version 310 es
Xinghua Cao27f43212018-02-26 11:05:53 +0800538 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
Xinghua Cao26143fd2017-11-01 18:19:05 +0800539 layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
540 layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
541 void main()
542 {
543 uvec4 value = imageLoad(uImage_1, ivec2(gl_LocalInvocationID.xy));
544 imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
545 })";
546
Xinghua Cao27f43212018-02-26 11:05:53 +0800547 constexpr int kWidth = 1, kHeight = 1;
548 constexpr GLuint kInputValues[2][1] = {{200}, {100}};
Xinghua Cao26143fd2017-11-01 18:19:05 +0800549
550 glBindTexture(GL_TEXTURE_2D, texture[0]);
551 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, kWidth, kHeight);
552 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
553 kInputValues[0]);
554 EXPECT_GL_NO_ERROR();
555
556 glBindTexture(GL_TEXTURE_2D, texture[1]);
557 glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, kWidth, kHeight);
558 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
559 kInputValues[1]);
560 EXPECT_GL_NO_ERROR();
561
Xinghua Cao26143fd2017-11-01 18:19:05 +0800562 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
563 glUseProgram(program.get());
564
565 glBindImageTexture(0, texture[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
566 EXPECT_GL_NO_ERROR();
567
568 glBindImageTexture(1, texture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
569 EXPECT_GL_NO_ERROR();
570
571 glDispatchCompute(1, 1, 1);
572 EXPECT_GL_NO_ERROR();
573
Xinghua Cao27f43212018-02-26 11:05:53 +0800574 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
575 GLuint outputValues[kWidth * kHeight];
576 constexpr GLuint expectedValue = 200;
Xinghua Cao26143fd2017-11-01 18:19:05 +0800577 glUseProgram(0);
578 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
579
580 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture[1], 0);
581 EXPECT_GL_NO_ERROR();
582 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
583 EXPECT_GL_NO_ERROR();
584
585 for (int i = 0; i < kWidth * kHeight; i++)
586 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800587 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao26143fd2017-11-01 18:19:05 +0800588 }
589}
590
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800591// Use image uniform to read and write Texture2DArray in compute shader, and verify the contents.
592TEST_P(ComputeShaderTest, BindImageTextureWithTexture2DArray)
593{
594 GLTexture texture[2];
595 GLFramebuffer framebuffer;
596 const std::string csSource =
597 R"(#version 310 es
598 layout(local_size_x=2, local_size_y=2, local_size_z=2) in;
599 layout(r32ui, binding = 0) readonly uniform highp uimage2DArray uImage_1;
600 layout(r32ui, binding = 1) writeonly uniform highp uimage2DArray uImage_2;
601 void main()
602 {
603 uvec4 value = imageLoad(uImage_1, ivec3(gl_LocalInvocationID.xyz));
604 imageStore(uImage_2, ivec3(gl_LocalInvocationID.xyz), value);
605 })";
606
Xinghua Cao27f43212018-02-26 11:05:53 +0800607 constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
608 constexpr GLuint kInputValues[2][2] = {{200, 200}, {100, 100}};
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800609
610 glBindTexture(GL_TEXTURE_2D_ARRAY, texture[0]);
611 glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32UI, kWidth, kHeight, kDepth);
612 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
613 GL_UNSIGNED_INT, kInputValues[0]);
614 EXPECT_GL_NO_ERROR();
615
616 glBindTexture(GL_TEXTURE_2D_ARRAY, texture[1]);
617 glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32UI, kWidth, kHeight, kDepth);
618 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
619 GL_UNSIGNED_INT, kInputValues[1]);
620 EXPECT_GL_NO_ERROR();
621
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800622 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
623 glUseProgram(program.get());
624
625 glBindImageTexture(0, texture[0], 0, GL_TRUE, 0, GL_READ_ONLY, GL_R32UI);
626 EXPECT_GL_NO_ERROR();
627
628 glBindImageTexture(1, texture[1], 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_R32UI);
629 EXPECT_GL_NO_ERROR();
630
631 glDispatchCompute(1, 1, 1);
632 EXPECT_GL_NO_ERROR();
633
Xinghua Cao27f43212018-02-26 11:05:53 +0800634 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
635 GLuint outputValues[kWidth * kHeight];
636 constexpr GLuint expectedValue = 200;
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800637 glUseProgram(0);
638 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
639
640 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
641 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
642 EXPECT_GL_NO_ERROR();
643 glReadBuffer(GL_COLOR_ATTACHMENT0);
644 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
645 EXPECT_GL_NO_ERROR();
646 for (int i = 0; i < kWidth * kHeight; i++)
647 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800648 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800649 }
650 glReadBuffer(GL_COLOR_ATTACHMENT1);
651 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
652 EXPECT_GL_NO_ERROR();
653 for (int i = 0; i < kWidth * kHeight; i++)
654 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800655 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800656 }
657}
658
659// Use image uniform to read and write Texture3D in compute shader, and verify the contents.
660TEST_P(ComputeShaderTest, BindImageTextureWithTexture3D)
661{
662 GLTexture texture[2];
663 GLFramebuffer framebuffer;
664 const std::string csSource =
665 R"(#version 310 es
Xinghua Cao27f43212018-02-26 11:05:53 +0800666 layout(local_size_x=1, local_size_y=1, local_size_z=2) in;
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800667 layout(r32ui, binding = 0) readonly uniform highp uimage3D uImage_1;
668 layout(r32ui, binding = 1) writeonly uniform highp uimage3D uImage_2;
669 void main()
670 {
671 uvec4 value = imageLoad(uImage_1, ivec3(gl_LocalInvocationID.xyz));
672 imageStore(uImage_2, ivec3(gl_LocalInvocationID.xyz), value);
673 })";
674
Xinghua Cao27f43212018-02-26 11:05:53 +0800675 constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
676 constexpr GLuint kInputValues[2][2] = {{200, 200}, {100, 100}};
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800677
678 glBindTexture(GL_TEXTURE_3D, texture[0]);
679 glTexStorage3D(GL_TEXTURE_3D, 1, GL_R32UI, kWidth, kHeight, kDepth);
680 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
681 GL_UNSIGNED_INT, kInputValues[0]);
682 EXPECT_GL_NO_ERROR();
683
684 glBindTexture(GL_TEXTURE_3D, texture[1]);
685 glTexStorage3D(GL_TEXTURE_3D, 1, GL_R32UI, kWidth, kHeight, kDepth);
686 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
687 GL_UNSIGNED_INT, kInputValues[1]);
688 EXPECT_GL_NO_ERROR();
689
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800690 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
691 glUseProgram(program.get());
692
693 glBindImageTexture(0, texture[0], 0, GL_TRUE, 0, GL_READ_ONLY, GL_R32UI);
694 EXPECT_GL_NO_ERROR();
695
696 glBindImageTexture(1, texture[1], 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_R32UI);
697 EXPECT_GL_NO_ERROR();
698
699 glDispatchCompute(1, 1, 1);
700 EXPECT_GL_NO_ERROR();
701
Xinghua Cao27f43212018-02-26 11:05:53 +0800702 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
703 GLuint outputValues[kWidth * kHeight];
704 constexpr GLuint expectedValue = 200;
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800705 glUseProgram(0);
706 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
707
708 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
709 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
710 EXPECT_GL_NO_ERROR();
711 glReadBuffer(GL_COLOR_ATTACHMENT0);
712 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
713 EXPECT_GL_NO_ERROR();
714 for (int i = 0; i < kWidth * kHeight; i++)
715 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800716 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800717 }
718 glReadBuffer(GL_COLOR_ATTACHMENT1);
719 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
720 EXPECT_GL_NO_ERROR();
721 for (int i = 0; i < kWidth * kHeight; i++)
722 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800723 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800724 }
725}
726
727// Use image uniform to read and write TextureCube in compute shader, and verify the contents.
728TEST_P(ComputeShaderTest, BindImageTextureWithTextureCube)
729{
730 GLTexture texture[2];
731 GLFramebuffer framebuffer;
732 const std::string csSource =
733 R"(#version 310 es
Xinghua Cao27f43212018-02-26 11:05:53 +0800734 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800735 layout(r32ui, binding = 0) readonly uniform highp uimageCube uImage_1;
736 layout(r32ui, binding = 1) writeonly uniform highp uimageCube uImage_2;
737 void main()
738 {
739 for (int i = 0; i < 6; i++)
740 {
741 uvec4 value = imageLoad(uImage_1, ivec3(gl_LocalInvocationID.xy, i));
742 imageStore(uImage_2, ivec3(gl_LocalInvocationID.xy, i), value);
743 }
744 })";
745
Xinghua Cao27f43212018-02-26 11:05:53 +0800746 constexpr int kWidth = 1, kHeight = 1;
747 constexpr GLuint kInputValues[2][1] = {{200}, {100}};
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800748
749 glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]);
750 glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_R32UI, kWidth, kHeight);
751 for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
752 face++)
753 {
754 glTexSubImage2D(face, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
755 kInputValues[0]);
756 }
757 EXPECT_GL_NO_ERROR();
758
759 glBindTexture(GL_TEXTURE_CUBE_MAP, texture[1]);
760 glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_R32UI, kWidth, kHeight);
761 for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
762 face++)
763 {
764 glTexSubImage2D(face, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
765 kInputValues[1]);
766 }
767 EXPECT_GL_NO_ERROR();
768
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800769 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
770 glUseProgram(program.get());
771
772 glBindImageTexture(0, texture[0], 0, GL_TRUE, 0, GL_READ_ONLY, GL_R32UI);
773 EXPECT_GL_NO_ERROR();
774
775 glBindImageTexture(1, texture[1], 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_R32UI);
776 EXPECT_GL_NO_ERROR();
777
778 glDispatchCompute(1, 1, 1);
779 EXPECT_GL_NO_ERROR();
780
Xinghua Cao27f43212018-02-26 11:05:53 +0800781 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
782 GLuint outputValues[kWidth * kHeight];
783 constexpr GLuint expectedValue = 200;
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800784 glUseProgram(0);
785 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
786
787 for (GLenum face = 0; face < 6; face++)
788 {
789 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
790 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
791 EXPECT_GL_NO_ERROR();
792 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
793 EXPECT_GL_NO_ERROR();
794
795 for (int i = 0; i < kWidth * kHeight; i++)
796 {
Xinghua Cao27f43212018-02-26 11:05:53 +0800797 EXPECT_EQ(expectedValue, outputValues[i]);
Xinghua Cao08a8ec82017-12-27 13:31:11 +0800798 }
799 }
800}
801
Xinghua Caob745f172018-01-09 16:10:02 +0800802// Use image uniform to read and write one layer of Texture2DArray in compute shader, and verify the
803// contents.
804TEST_P(ComputeShaderTest, BindImageTextureWithOneLayerTexture2DArray)
805{
806 ANGLE_SKIP_TEST_IF(IsD3D11());
807
808 GLTexture texture[2];
809 GLFramebuffer framebuffer;
810 const std::string csSource =
811 R"(#version 310 es
812 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
813 layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
814 layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
815 void main()
816 {
817 uvec4 value = imageLoad(uImage_1, ivec2(gl_LocalInvocationID.xy));
818 imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
819 })";
820
821 constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
822 constexpr int kResultSize = kWidth * kHeight;
823 constexpr GLuint kInputValues[2][2] = {{200, 150}, {100, 50}};
824 constexpr GLuint expectedValue_1 = 200;
825 constexpr GLuint expectedValue_2 = 100;
826 GLuint outputValues[kResultSize];
827
828 glBindTexture(GL_TEXTURE_2D_ARRAY, texture[0]);
829 glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32UI, kWidth, kHeight, kDepth);
830 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
831 GL_UNSIGNED_INT, kInputValues[0]);
832 EXPECT_GL_NO_ERROR();
833
834 glBindTexture(GL_TEXTURE_2D_ARRAY, texture[1]);
835 glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32UI, kWidth, kHeight, kDepth);
836 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
837 GL_UNSIGNED_INT, kInputValues[1]);
838 EXPECT_GL_NO_ERROR();
839
840 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
841 glUseProgram(program.get());
842
843 glBindImageTexture(0, texture[0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
844 EXPECT_GL_NO_ERROR();
845 glBindImageTexture(1, texture[1], 0, GL_FALSE, 1, GL_WRITE_ONLY, GL_R32UI);
846 EXPECT_GL_NO_ERROR();
847 glDispatchCompute(1, 1, 1);
848 EXPECT_GL_NO_ERROR();
849
850 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
851 glUseProgram(0);
852 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
853 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
854 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
855 EXPECT_GL_NO_ERROR();
856 glReadBuffer(GL_COLOR_ATTACHMENT0);
857 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
858 EXPECT_GL_NO_ERROR();
859 for (int i = 0; i < kResultSize; i++)
860 {
861 EXPECT_EQ(expectedValue_2, outputValues[i]);
862 }
863 glReadBuffer(GL_COLOR_ATTACHMENT1);
864 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
865 EXPECT_GL_NO_ERROR();
866 for (int i = 0; i < kResultSize; i++)
867 {
868 EXPECT_EQ(expectedValue_1, outputValues[i]);
869 }
870}
871
872// Use image uniform to read and write one layer of Texture3D in compute shader, and verify the
873// contents.
874TEST_P(ComputeShaderTest, BindImageTextureWithOneLayerTexture3D)
875{
876 ANGLE_SKIP_TEST_IF(IsD3D11());
877 GLTexture texture[2];
878 GLFramebuffer framebuffer;
879 const std::string csSource =
880 R"(#version 310 es
881 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
882 layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
883 layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
884 void main()
885 {
886 uvec4 value = imageLoad(uImage_1, ivec2(gl_LocalInvocationID.xy));
887 imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
888 })";
889
890 constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
891 constexpr int kResultSize = kWidth * kHeight;
892 constexpr GLuint kInputValues[2][2] = {{200, 150}, {100, 50}};
893 constexpr GLuint expectedValue_1 = 150;
894 constexpr GLuint expectedValue_2 = 50;
895 GLuint outputValues[kResultSize];
896
897 glBindTexture(GL_TEXTURE_3D, texture[0]);
898 glTexStorage3D(GL_TEXTURE_3D, 1, GL_R32UI, kWidth, kHeight, kDepth);
899 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
900 GL_UNSIGNED_INT, kInputValues[0]);
901 EXPECT_GL_NO_ERROR();
902
903 glBindTexture(GL_TEXTURE_3D, texture[1]);
904 glTexStorage3D(GL_TEXTURE_3D, 1, GL_R32UI, kWidth, kHeight, kDepth);
905 glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, kWidth, kHeight, kDepth, GL_RED_INTEGER,
906 GL_UNSIGNED_INT, kInputValues[1]);
907 EXPECT_GL_NO_ERROR();
908
909 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
910 glUseProgram(program.get());
911
912 glBindImageTexture(0, texture[0], 0, GL_FALSE, 1, GL_READ_ONLY, GL_R32UI);
913 EXPECT_GL_NO_ERROR();
914 glBindImageTexture(1, texture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
915 EXPECT_GL_NO_ERROR();
916 glDispatchCompute(1, 1, 1);
917 EXPECT_GL_NO_ERROR();
918
919 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
920 glUseProgram(0);
921 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
922 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
923 glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
924 EXPECT_GL_NO_ERROR();
925 glReadBuffer(GL_COLOR_ATTACHMENT0);
926 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
927 EXPECT_GL_NO_ERROR();
928 for (int i = 0; i < kResultSize; i++)
929 {
930 EXPECT_EQ(expectedValue_1, outputValues[i]);
931 }
932 glReadBuffer(GL_COLOR_ATTACHMENT1);
933 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
934 EXPECT_GL_NO_ERROR();
935 for (int i = 0; i < kResultSize; i++)
936 {
937 EXPECT_EQ(expectedValue_2, outputValues[i]);
938 }
939}
940
941// Use image uniform to read and write one layer of TextureCube in compute shader, and verify the
942// contents.
943TEST_P(ComputeShaderTest, BindImageTextureWithOneLayerTextureCube)
944{
945 ANGLE_SKIP_TEST_IF(IsD3D11());
946
947 GLTexture texture[2];
948 GLFramebuffer framebuffer;
949 const std::string csSource =
950 R"(#version 310 es
951 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
952 layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
953 layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
954 void main()
955 {
956 uvec4 value = imageLoad(uImage_1, ivec2(gl_LocalInvocationID.xy));
957 imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
958 })";
959
960 constexpr int kWidth = 1, kHeight = 1;
961 constexpr int kResultSize = kWidth * kHeight;
962 constexpr GLuint kInputValues[2][1] = {{200}, {100}};
963 constexpr GLuint expectedValue_1 = 200;
964 constexpr GLuint expectedValue_2 = 100;
965 GLuint outputValues[kResultSize];
966
967 glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]);
968 glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_R32UI, kWidth, kHeight);
969 for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
970 face++)
971 {
972 glTexSubImage2D(face, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
973 kInputValues[0]);
974 }
975 EXPECT_GL_NO_ERROR();
976
977 glBindTexture(GL_TEXTURE_CUBE_MAP, texture[1]);
978 glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_R32UI, kWidth, kHeight);
979 for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
980 face++)
981 {
982 glTexSubImage2D(face, 0, 0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
983 kInputValues[1]);
984 }
985 EXPECT_GL_NO_ERROR();
986
987 ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
988 glUseProgram(program.get());
989
990 glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
991 EXPECT_GL_NO_ERROR();
992 glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
993 EXPECT_GL_NO_ERROR();
994 glDispatchCompute(1, 1, 1);
995 EXPECT_GL_NO_ERROR();
996
997 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
998 glUseProgram(0);
999 glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
1000
1001 for (GLenum face = 0; face < 6; face++)
1002 {
1003 glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1004 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
1005 EXPECT_GL_NO_ERROR();
1006 glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
1007 EXPECT_GL_NO_ERROR();
1008
1009 if (face == 4)
1010 {
1011 for (int i = 0; i < kResultSize; i++)
1012 {
1013 EXPECT_EQ(expectedValue_1, outputValues[i]);
1014 }
1015 }
1016 else
1017 {
1018 for (int i = 0; i < kResultSize; i++)
1019 {
1020 EXPECT_EQ(expectedValue_2, outputValues[i]);
1021 }
1022 }
1023 }
1024}
1025
Jiawei Shao6ae51612018-02-23 14:03:25 +08001026// Verify an INVALID_OPERATION error is reported when querying GL_COMPUTE_WORK_GROUP_SIZE for a
1027// program which has not been linked successfully or which does not contain objects to form a
1028// compute shader.
1029TEST_P(ComputeShaderTest, QueryComputeWorkGroupSize)
1030{
1031 const std::string vsSource =
1032 R"(#version 310 es
1033 void main()
1034 {
1035 })";
1036
1037 const std::string fsSource =
1038 R"(#version 310 es
1039 void main()
1040 {
1041 })";
1042
1043 GLint workGroupSize[3];
1044
1045 ANGLE_GL_PROGRAM(graphicsProgram, vsSource, fsSource);
1046 glGetProgramiv(graphicsProgram, GL_COMPUTE_WORK_GROUP_SIZE, workGroupSize);
1047 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1048
1049 GLuint computeProgram = glCreateProgram();
1050 GLShader computeShader(GL_COMPUTE_SHADER);
1051 glAttachShader(computeProgram, computeShader);
1052 glLinkProgram(computeProgram);
1053 glDetachShader(computeProgram, computeShader);
1054
1055 GLint linkStatus;
1056 glGetProgramiv(computeProgram, GL_LINK_STATUS, &linkStatus);
1057 ASSERT_GL_FALSE(linkStatus);
1058
1059 glGetProgramiv(computeProgram, GL_COMPUTE_WORK_GROUP_SIZE, workGroupSize);
1060 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1061
1062 glDeleteProgram(computeProgram);
1063
1064 ASSERT_GL_NO_ERROR();
1065}
1066
Martin Radev4c4c8e72016-08-04 12:25:34 +03001067// Check that it is not possible to create a compute shader when the context does not support ES
1068// 3.10
1069TEST_P(ComputeShaderTestES3, NotSupported)
1070{
1071 GLuint computeShaderHandle = glCreateShader(GL_COMPUTE_SHADER);
1072 EXPECT_EQ(0u, computeShaderHandle);
1073 EXPECT_GL_ERROR(GL_INVALID_ENUM);
1074}
1075
Xinghua Caob1239382016-12-13 15:07:05 +08001076ANGLE_INSTANTIATE_TEST(ComputeShaderTest, ES31_OPENGL(), ES31_OPENGLES(), ES31_D3D11());
Martin Radev4c4c8e72016-08-04 12:25:34 +03001077ANGLE_INSTANTIATE_TEST(ComputeShaderTestES3, ES3_OPENGL(), ES3_OPENGLES());
1078
1079} // namespace