1 //
2 // Copyright 2020 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 // Test that invokes a usecase where there is a feedback loop but the framebuffer
8 // depth attachment is only read from
9 
10 #include "test_utils/ANGLETest.h"
11 #include "test_utils/gl_raii.h"
12 
13 using namespace angle;
14 
15 class ReadOnlyFeedbackLoopTest : public ANGLETest
16 {
17   protected:
ReadOnlyFeedbackLoopTest()18     ReadOnlyFeedbackLoopTest()
19     {
20         setWindowWidth(256);
21         setWindowHeight(256);
22         setConfigRedBits(8);
23         setConfigGreenBits(8);
24         setConfigBlueBits(8);
25         setConfigAlphaBits(8);
26     }
27 
testSetUp()28     void testSetUp() override
29     {
30         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
31         glClearDepthf(1.0f);
32         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
33         glDepthRangef(-1.0f, 1.0f);
34 
35         glEnable(GL_BLEND);
36         glDisable(GL_DEPTH_TEST);
37 
38         ASSERT_GL_NO_ERROR();
39     }
40 };
41 
42 // Fill out a depth texture to specific values and use it both as a sampler and a depth texture
43 // with depth write disabled. This is to test a "read-only feedback loop" that needs to be
44 // supported to match industry standard.
TEST_P(ReadOnlyFeedbackLoopTest,DepthFeedbackLoop)45 TEST_P(ReadOnlyFeedbackLoopTest, DepthFeedbackLoop)
46 {
47     // TODO - Add support for readonly feedback loops (http://anglebug.com/4778)
48     ANGLE_SKIP_TEST_IF(true);
49 
50     const GLuint width  = getWindowWidth();
51     const GLuint height = getWindowHeight();
52 
53     GLTexture colorTex;
54     GLTexture depthTex;
55     GLTexture finalTex;
56 
57     GLFramebuffer gbufferFbo;
58     GLFramebuffer finalFbo;
59 
60     ANGLE_GL_PROGRAM(colorFillProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
61     ANGLE_GL_PROGRAM(textureFillProgram, essl1_shaders::vs::Texture2D(),
62                      essl1_shaders::fs::Texture2D());
63 
64     glBindTexture(GL_TEXTURE_2D, colorTex);
65     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
66     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
67     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
68     EXPECT_GL_NO_ERROR();
69 
70     glBindTexture(GL_TEXTURE_2D, depthTex);
71     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT,
72                  GL_UNSIGNED_INT, nullptr);
73     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
74     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75     EXPECT_GL_NO_ERROR();
76 
77     glBindFramebuffer(GL_FRAMEBUFFER, gbufferFbo);
78     EXPECT_GL_NO_ERROR();
79 
80     // Attach a color and depth texture to the FBO
81     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
82     EXPECT_GL_NO_ERROR();
83     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);
84     EXPECT_GL_NO_ERROR();
85 
86     ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
87 
88     // Set the color texture to blue and depth texture to 1.0f
89     glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
90     glClearDepthf(1.0f);
91     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92     ASSERT_GL_NO_ERROR();
93 
94     // Enable Depth test with passing always to write depth.
95     glEnable(GL_DEPTH_TEST);
96     glDepthMask(GL_TRUE);
97     glDepthFunc(GL_ALWAYS);
98 
99     // Fill the middle of the depth texture with 0.0f. while the border remains 1.0f as
100     // previously cleared.
101     const GLfloat depthValue = 0.0f;
102     drawQuad(colorFillProgram, essl1_shaders::PositionAttrib(), depthValue, 0.6f);
103 
104     EXPECT_GL_NO_ERROR();
105 
106     glBindTexture(GL_TEXTURE_2D, finalTex);
107     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
108     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
109     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
110     EXPECT_GL_NO_ERROR();
111 
112     glBindFramebuffer(GL_FRAMEBUFFER, finalFbo);
113     EXPECT_GL_NO_ERROR();
114 
115     // Enable Depth test without depth write.
116     glEnable(GL_DEPTH_TEST);
117     glDepthMask(GL_FALSE);
118     glDepthFunc(GL_GREATER);
119 
120     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, finalTex, 0);
121     EXPECT_GL_NO_ERROR();
122     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);
123     EXPECT_GL_NO_ERROR();
124     ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
125 
126     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 
128     glBindTexture(GL_TEXTURE_2D, depthTex);
129 
130     // Fill finalTex with values read from depthTex. This should work even though depthTex
131     // is also bound as the depth attachment, because depth write is disabled.
132     // The write to finalTex only succeeds for the middle region due to depth test.
133     drawQuad(textureFillProgram, essl1_shaders::PositionAttrib(), 0.7f, 1.0f);
134 
135     // Copy finalTex to default framebuffer for verification. Depth values written in the first
136     // draw call are expected in the middle, while the clear value in the clear before the
137     // second draw call are expected at the border.
138     glBindFramebuffer(GL_FRAMEBUFFER, 0);
139     glDisable(GL_DEPTH_TEST);
140     glDepthMask(GL_FALSE);
141     glBindTexture(GL_TEXTURE_2D, finalTex);
142     drawQuad(textureFillProgram, essl1_shaders::PositionAttrib(), 0.0f, 1.0f);
143     EXPECT_GL_NO_ERROR();
144 
145     GLint depthColorValue = (depthValue)*128 + 128;
146     EXPECT_EQ(depthColorValue, angle::ReadColor(width / 2, height / 2).R);
147     EXPECT_PIXEL_EQ(0, 0, 0, 0, 255, 255);
148 }
149 
150 // Instantiate the test for ES2 and ES3.
151 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(ReadOnlyFeedbackLoopTest);
152