Hash :
1bfeb8a5
        
        Author :
  
        
        Date :
2025-03-03T00:00:00
        
      
D3D11: Set SV_Position interpolation for conservative depth HLSL requires centroid interpolation for PS input position if conservative depth output is used and the shader is executing at pixel-frequency. Bug: angleproject:397720825 Change-Id: I2b314e39e40a63cb58e2bdef99ffa842ddaaff0d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6336827 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// FragDepthTest:
//   Tests the correctness of gl_FragDepth usage.
//
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
using namespace angle;
class FragDepthTest : public ANGLETest<>
{
  protected:
    struct FragDepthTestResources
    {
        GLuint program;
        GLint depthLocation;
        GLTexture colorTexture;
        GLTexture depthTexture;
        GLFramebuffer framebuffer;
    };
    void setupResources(FragDepthTestResources &resources)
    {
        // Writes a fixed depth value and green.
        constexpr char kFS[] =
            R"(#version 300 es
            precision highp float;
            layout(location = 0) out vec4 fragColor;
            uniform float u_depth;
            void main(){
                gl_FragDepth = u_depth;
                fragColor = vec4(0.0, 1.0, 0.0, 1.0);
            })";
        resources.program       = CompileProgram(essl3_shaders::vs::Simple(), kFS);
        resources.depthLocation = glGetUniformLocation(resources.program, "u_depth");
        glBindTexture(GL_TEXTURE_2D, resources.colorTexture);
        glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glBindTexture(GL_TEXTURE_2D, resources.depthTexture);
        glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1, 1);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glBindFramebuffer(GL_FRAMEBUFFER, resources.framebuffer);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                               resources.colorTexture, 0);
    }
    void cleanupResources(FragDepthTestResources &resources) { glDeleteProgram(resources.program); }
    void checkDepthWritten(float expectedDepth, float fsDepth, bool bindDepthBuffer)
    {
        FragDepthTestResources resources;
        setupResources(resources);
        ASSERT_NE(0u, resources.program);
        ASSERT_NE(-1, resources.depthLocation);
        ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
        ASSERT_GL_NO_ERROR();
        glBindFramebuffer(GL_FRAMEBUFFER, resources.framebuffer);
        if (bindDepthBuffer)
        {
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
                                   resources.depthTexture, 0);
            EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
            // Clear to the expected depth so it will be compared to the FS depth with
            // DepthFunc(GL_EQUAL)
            glClearDepthf(expectedDepth);
            glDepthFunc(GL_EQUAL);
            glEnable(GL_DEPTH_TEST);
        }
        else
        {
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
            EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
            glDisable(GL_DEPTH_TEST);
        }
        glUseProgram(resources.program);
        // Clear to red, the FS will write green on success
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        // Clear to the expected depth so it will be compared to the FS depth with
        // DepthFunc(GL_EQUAL)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUniform1f(resources.depthLocation, fsDepth);
        drawQuad(resources.program, "a_position", 0.0f);
        EXPECT_GL_NO_ERROR();
        EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
        cleanupResources(resources);
    }
};
// Test that writing to gl_FragDepth works
TEST_P(FragDepthTest, DepthBufferBound)
{
    checkDepthWritten(0.5f, 0.5f, true);
}
// Test that writing to gl_FragDepth with no depth buffer works.
TEST_P(FragDepthTest, DepthBufferUnbound)
{
    // Depth test is disabled, so the expected depth should not matter.
    checkDepthWritten(0.f, 0.5f, false);
}
// Test that fragment shader depth writes to a no-depth framebuffer do not fail
// after using a depth-enabled framebuffer with the same program.
TEST_P(FragDepthTest, SwitchToNoDepthFramebuffer)
{
    constexpr char kFS[] = R"(#version 300 es
    out highp vec4 color;
    void main() {
      color = vec4(1.0, 0.0, 0.0, 1.0);
      gl_FragDepth = 1.0;
    })";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    // Draw to a depth-enabled framebuffer first
    GLFramebuffer fbo1;
    glBindFramebuffer(GL_FRAMEBUFFER, fbo1);
    GLRenderbuffer rb10;
    glBindRenderbuffer(GL_RENDERBUFFER, rb10);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 128, 128);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb10);
    GLRenderbuffer rb1d;
    glBindRenderbuffer(GL_RENDERBUFFER, rb1d);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32F, 128, 128);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb1d);
    EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
    drawQuad(program, "a_position", 0.0f);
    EXPECT_GL_NO_ERROR();
    // Draw to a no-depth framebuffer using the same program
    GLFramebuffer fbo2;
    glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
    GLRenderbuffer rb20;
    glBindRenderbuffer(GL_RENDERBUFFER, rb20);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_R8, 64, 64);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb20);
    EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
    drawQuad(program, "a_position", 0.0f);
    EXPECT_GL_NO_ERROR();
}
class FragDepthRedeclarationTest : public ANGLETest<>
{};
// Test gl_FragDepth redeclared as vertex output
TEST_P(FragDepthRedeclarationTest, VSOutput)
{
    constexpr char kVS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
out highp float gl_FragDepth;
void main() {
    gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    GLProgram prg;
    prg.makeRaster(kVS, essl3_shaders::fs::Red());
    EXPECT_FALSE(prg.valid());
}
// Test gl_FragDepth redeclared as fragment input
TEST_P(FragDepthRedeclarationTest, FSInput)
{
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: enable
out highp vec4 color;
in highp float gl_FragDepth;
void main() {
    color = vec4(gl_FragDepth, 0.0, 0.0, 1.0);
})";
    GLProgram prg;
    prg.makeRaster(essl3_shaders::vs::Simple(), kFS);
    EXPECT_FALSE(prg.valid());
}
// Test gl_FragDepth redeclaration with no layout qualifier
TEST_P(FragDepthRedeclarationTest, NoLayout)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_any layout qualifier
TEST_P(FragDepthRedeclarationTest, Any)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_any) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_greater layout qualifier
TEST_P(FragDepthRedeclarationTest, Greater)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_greater) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_less layout qualifier
TEST_P(FragDepthRedeclarationTest, Less)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_less) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_unchanged layout qualifier
TEST_P(FragDepthRedeclarationTest, Unchanged)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_unchanged) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = 1.0;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_any layout qualifier and gl_FragCoord
TEST_P(FragDepthRedeclarationTest, AnyWithFragCoord)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_any) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = cos(gl_FragCoord.z);
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_greater layout qualifier and gl_FragCoord
TEST_P(FragDepthRedeclarationTest, GreaterWithFragCoord)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_greater) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = gl_FragCoord.z + 0.5;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_less layout qualifier and gl_FragCoord
TEST_P(FragDepthRedeclarationTest, LessWithFragCoord)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_less) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = gl_FragCoord.z - 0.5;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
// Test gl_FragDepth redeclaration with depth_unchanged layout qualifier and gl_FragCoord
TEST_P(FragDepthRedeclarationTest, UnchangedWithFragCoord)
{
    ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_conservative_depth"));
    constexpr char kFS[] = R"(#version 300 es
#extension GL_EXT_conservative_depth: require
out highp vec4 color;
layout (depth_unchanged) out highp float gl_FragDepth;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
    gl_FragDepth = gl_FragCoord.z;
})";
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
    drawQuad(program, essl3_shaders::PositionAttrib(), 0.0f);
    EXPECT_GL_NO_ERROR();
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FragDepthTest);
ANGLE_INSTANTIATE_TEST_ES3(FragDepthTest);
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FragDepthRedeclarationTest);
ANGLE_INSTANTIATE_TEST_ES3(FragDepthRedeclarationTest);