ES31: Implement memoryBarrier on D3D backend

Because D3D11 makes a coherent write, so memoryBarrier is not needed on
D3D backend.
This patch also simples some test cases and modifies some cases errors when
accessing memory after an incoherent write, but does not use memoryBarrier.

BUG=angleproject:2280
TEST=angle_end2end_tests.ComputeShaderTest.*

Change-Id: Iee5d105a0b6d8534aded03fdaaefc909864d33a4
Reviewed-on: https://chromium-review.googlesource.com/937023
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/tests/gl_tests/ComputeShaderTest.cpp b/src/tests/gl_tests/ComputeShaderTest.cpp
index d86fff1..7671e36 100644
--- a/src/tests/gl_tests/ComputeShaderTest.cpp
+++ b/src/tests/gl_tests/ComputeShaderTest.cpp
@@ -350,7 +350,7 @@
     GLFramebuffer mFramebuffer;
     const std::string csSource =
         R"(#version 310 es
-        layout(local_size_x=2, local_size_y=2, local_size_z=1) in;
+        layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
         layout(r32ui, binding = 0) writeonly uniform highp uimage2D uImage[2];
         void main()
         {
@@ -362,8 +362,8 @@
 
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
-    int width = 4, height = 2;
-    GLuint inputValues[] = {200, 200, 200, 200, 200, 200, 200, 200};
+    int width = 1, height = 1;
+    GLuint inputValues[] = {200};
 
     glBindTexture(GL_TEXTURE_2D, mTexture[0]);
     glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, width, height);
@@ -383,11 +383,12 @@
     glBindImageTexture(1, mTexture[1], 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32UI);
     EXPECT_GL_NO_ERROR();
 
-    glDispatchCompute(2, 1, 1);
+    glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
     glUseProgram(0);
-    GLuint outputValues[2][8];
+    GLuint outputValues[2][1];
     GLuint expectedValue = 100;
     glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
 
@@ -423,7 +424,7 @@
     GLFramebuffer mFramebuffer;
     const std::string csSource =
         R"(#version 310 es
-        layout(local_size_x=2, local_size_y=2, local_size_z=1) in;
+        layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
         layout(r32ui) writeonly uniform highp uimage2D uImage[2];
         void main()
         {
@@ -433,8 +434,8 @@
 
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
-    constexpr int kTextureWidth = 4, kTextureHeight = 2;
-    GLuint inputValues[] = {200, 200, 200, 200, 200, 200, 200, 200};
+    constexpr int kTextureWidth = 1, kTextureHeight = 2;
+    GLuint inputValues[] = {200, 200};
 
     glBindTexture(GL_TEXTURE_2D, mTexture);
     glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, kTextureWidth, kTextureHeight);
@@ -446,11 +447,12 @@
     glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
     glUseProgram(0);
     glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
 
     glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture, 0);
-    GLuint outputValues[8];
+    GLuint outputValues[kTextureWidth * kTextureHeight];
     glReadPixels(0, 0, kTextureWidth, kTextureHeight, GL_RED_INTEGER, GL_UNSIGNED_INT,
                  outputValues);
     EXPECT_GL_NO_ERROR();
@@ -533,7 +535,7 @@
     GLFramebuffer framebuffer;
     const std::string csSource =
         R"(#version 310 es
-        layout(local_size_x=4, local_size_y=2, local_size_z=1) in;
+        layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
         layout(r32ui, binding = 0) readonly uniform highp uimage2D uImage_1;
         layout(r32ui, binding = 1) writeonly uniform highp uimage2D uImage_2;
         void main()
@@ -542,9 +544,8 @@
             imageStore(uImage_2, ivec2(gl_LocalInvocationID.xy), value);
         })";
 
-    constexpr int kWidth = 4, kHeight = 2;
-    constexpr GLuint kInputValues[2][8] = {{200, 200, 200, 200, 200, 200, 200, 200},
-                                           {100, 100, 100, 100, 100, 100, 100, 100}};
+    constexpr int kWidth = 1, kHeight = 1;
+    constexpr GLuint kInputValues[2][1] = {{200}, {100}};
 
     glBindTexture(GL_TEXTURE_2D, texture[0]);
     glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, kWidth, kHeight);
@@ -558,30 +559,6 @@
                     kInputValues[1]);
     EXPECT_GL_NO_ERROR();
 
-    glUseProgram(0);
-    GLuint outputValues[8];
-    constexpr GLuint expectedValue_1 = 200;
-    constexpr GLuint expectedValue_2 = 100;
-    glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
-
-    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture[0], 0);
-    EXPECT_GL_NO_ERROR();
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
-    }
-
-    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture[1], 0);
-    EXPECT_GL_NO_ERROR();
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_2, outputValues[i]);
-    }
-
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
 
@@ -594,6 +571,9 @@
     glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
+    GLuint outputValues[kWidth * kHeight];
+    constexpr GLuint expectedValue = 200;
     glUseProgram(0);
     glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
 
@@ -604,7 +584,7 @@
 
     for (int i = 0; i < kWidth * kHeight; i++)
     {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
+        EXPECT_EQ(expectedValue, outputValues[i]);
     }
 }
 
@@ -624,9 +604,8 @@
             imageStore(uImage_2, ivec3(gl_LocalInvocationID.xyz), value);
         })";
 
-    constexpr int kWidth = 2, kHeight = 2, kDepth = 2;
-    constexpr GLuint kInputValues[2][8] = {{200, 200, 200, 200, 200, 200, 200, 200},
-                                           {100, 100, 100, 100, 100, 100, 100, 100}};
+    constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
+    constexpr GLuint kInputValues[2][2] = {{200, 200}, {100, 100}};
 
     glBindTexture(GL_TEXTURE_2D_ARRAY, texture[0]);
     glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R32UI, kWidth, kHeight, kDepth);
@@ -640,49 +619,6 @@
                     GL_UNSIGNED_INT, kInputValues[1]);
     EXPECT_GL_NO_ERROR();
 
-    glUseProgram(0);
-    GLuint outputValues[4];
-    constexpr GLuint expectedValue_1 = 200;
-    constexpr GLuint expectedValue_2 = 100;
-    glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
-
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[0], 0, 0);
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[0], 0, 1);
-    EXPECT_GL_NO_ERROR();
-
-    glReadBuffer(GL_COLOR_ATTACHMENT0);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
-    }
-
-    glReadBuffer(GL_COLOR_ATTACHMENT1);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
-    }
-
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
-    EXPECT_GL_NO_ERROR();
-
-    glReadBuffer(GL_COLOR_ATTACHMENT0);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_2, outputValues[i]);
-    }
-
-    glReadBuffer(GL_COLOR_ATTACHMENT1);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_2, outputValues[i]);
-    }
-
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
 
@@ -695,6 +631,9 @@
     glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
+    GLuint outputValues[kWidth * kHeight];
+    constexpr GLuint expectedValue = 200;
     glUseProgram(0);
     glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
 
@@ -706,14 +645,14 @@
     EXPECT_GL_NO_ERROR();
     for (int i = 0; i < kWidth * kHeight; i++)
     {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
+        EXPECT_EQ(expectedValue, outputValues[i]);
     }
     glReadBuffer(GL_COLOR_ATTACHMENT1);
     glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
     EXPECT_GL_NO_ERROR();
     for (int i = 0; i < kWidth * kHeight; i++)
     {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
+        EXPECT_EQ(expectedValue, outputValues[i]);
     }
 }
 
@@ -724,7 +663,7 @@
     GLFramebuffer framebuffer;
     const std::string csSource =
         R"(#version 310 es
-        layout(local_size_x=2, local_size_y=2, local_size_z=2) in;
+        layout(local_size_x=1, local_size_y=1, local_size_z=2) in;
         layout(r32ui, binding = 0) readonly uniform highp uimage3D uImage_1;
         layout(r32ui, binding = 1) writeonly uniform highp uimage3D uImage_2;
         void main()
@@ -733,9 +672,8 @@
             imageStore(uImage_2, ivec3(gl_LocalInvocationID.xyz), value);
         })";
 
-    constexpr int kWidth = 2, kHeight = 2, kDepth = 2;
-    constexpr GLuint kInputValues[2][8] = {{200, 200, 200, 200, 200, 200, 200, 200},
-                                           {100, 100, 100, 100, 100, 100, 100, 100}};
+    constexpr int kWidth = 1, kHeight = 1, kDepth = 2;
+    constexpr GLuint kInputValues[2][2] = {{200, 200}, {100, 100}};
 
     glBindTexture(GL_TEXTURE_3D, texture[0]);
     glTexStorage3D(GL_TEXTURE_3D, 1, GL_R32UI, kWidth, kHeight, kDepth);
@@ -749,49 +687,6 @@
                     GL_UNSIGNED_INT, kInputValues[1]);
     EXPECT_GL_NO_ERROR();
 
-    glUseProgram(0);
-    GLuint outputValues[4];
-    constexpr GLuint expectedValue_1 = 200;
-    constexpr GLuint expectedValue_2 = 100;
-    glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
-
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[0], 0, 0);
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[0], 0, 1);
-    EXPECT_GL_NO_ERROR();
-
-    glReadBuffer(GL_COLOR_ATTACHMENT0);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
-    }
-
-    glReadBuffer(GL_COLOR_ATTACHMENT1);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
-    }
-
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture[1], 0, 0);
-    glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, texture[1], 0, 1);
-    EXPECT_GL_NO_ERROR();
-
-    glReadBuffer(GL_COLOR_ATTACHMENT0);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_2, outputValues[i]);
-    }
-
-    glReadBuffer(GL_COLOR_ATTACHMENT1);
-    glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-    for (int i = 0; i < kWidth * kHeight; i++)
-    {
-        EXPECT_EQ(expectedValue_2, outputValues[i]);
-    }
-
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
 
@@ -804,6 +699,9 @@
     glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
+    GLuint outputValues[kWidth * kHeight];
+    constexpr GLuint expectedValue = 200;
     glUseProgram(0);
     glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
 
@@ -815,14 +713,14 @@
     EXPECT_GL_NO_ERROR();
     for (int i = 0; i < kWidth * kHeight; i++)
     {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
+        EXPECT_EQ(expectedValue, outputValues[i]);
     }
     glReadBuffer(GL_COLOR_ATTACHMENT1);
     glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
     EXPECT_GL_NO_ERROR();
     for (int i = 0; i < kWidth * kHeight; i++)
     {
-        EXPECT_EQ(expectedValue_1, outputValues[i]);
+        EXPECT_EQ(expectedValue, outputValues[i]);
     }
 }
 
@@ -833,7 +731,7 @@
     GLFramebuffer framebuffer;
     const std::string csSource =
         R"(#version 310 es
-        layout(local_size_x=2, local_size_y=2, local_size_z=1) in;
+        layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
         layout(r32ui, binding = 0) readonly uniform highp uimageCube uImage_1;
         layout(r32ui, binding = 1) writeonly uniform highp uimageCube uImage_2;
         void main()
@@ -845,8 +743,8 @@
             }
         })";
 
-    constexpr int kWidth = 2, kHeight = 2;
-    constexpr GLuint kInputValues[2][4] = {{200, 200, 200, 200}, {100, 100, 100, 100}};
+    constexpr int kWidth = 1, kHeight = 1;
+    constexpr GLuint kInputValues[2][1] = {{200}, {100}};
 
     glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]);
     glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_R32UI, kWidth, kHeight);
@@ -868,36 +766,6 @@
     }
     EXPECT_GL_NO_ERROR();
 
-    glUseProgram(0);
-    GLuint outputValues[4];
-    constexpr GLuint expectedValue_1 = 200;
-    constexpr GLuint expectedValue_2 = 100;
-    glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
-    for (GLenum face = 0; face < 6; face++)
-    {
-        glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                               GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[0], 0);
-        EXPECT_GL_NO_ERROR();
-        glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-        EXPECT_GL_NO_ERROR();
-
-        for (int i = 0; i < kWidth * kHeight; i++)
-        {
-            EXPECT_EQ(expectedValue_1, outputValues[i]);
-        }
-
-        glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
-                               GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
-        EXPECT_GL_NO_ERROR();
-        glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
-        EXPECT_GL_NO_ERROR();
-
-        for (int i = 0; i < kWidth * kHeight; i++)
-        {
-            EXPECT_EQ(expectedValue_2, outputValues[i]);
-        }
-    }
-
     ANGLE_GL_COMPUTE_PROGRAM(program, csSource);
     glUseProgram(program.get());
 
@@ -910,6 +778,9 @@
     glDispatchCompute(1, 1, 1);
     EXPECT_GL_NO_ERROR();
 
+    glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
+    GLuint outputValues[kWidth * kHeight];
+    constexpr GLuint expectedValue = 200;
     glUseProgram(0);
     glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
 
@@ -923,7 +794,7 @@
 
         for (int i = 0; i < kWidth * kHeight; i++)
         {
-            EXPECT_EQ(expectedValue_1, outputValues[i]);
+            EXPECT_EQ(expectedValue, outputValues[i]);
         }
     }
 }