| 1 | /* |
| 2 | * Copyright © 2016 Rob Clark <[email protected]> |
| 3 | * Copyright © 2018 Google, Inc. |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Authors: |
| 7 | * Rob Clark <[email protected]> |
| 8 | */ |
| 9 | |
| 10 | #include "pipe/p_state.h" |
| 11 | #include "util/u_memory.h" |
| 12 | #include "util/u_string.h" |
| 13 | |
| 14 | #include "fd6_context.h" |
| 15 | #include "fd6_pack.h" |
| 16 | #include "fd6_zsa.h" |
| 17 | |
| 18 | /* update lza state based on stencil-test func: |
| 19 | * |
| 20 | * Conceptually the order of the pipeline is: |
| 21 | * |
| 22 | * |
| 23 | * FS -> Alpha-Test -> Stencil-Test -> Depth-Test |
| 24 | * | | |
| 25 | * if wrmask != 0 if wrmask != 0 |
| 26 | * | | |
| 27 | * v v |
| 28 | * Stencil-Write Depth-Write |
| 29 | * |
| 30 | * Because Stencil-Test can have side effects (Stencil-Write) prior |
| 31 | * to depth test, in this case we potentially need to disable early |
| 32 | * lrz-test. See: |
| 33 | * |
| 34 | * https://www.khronos.org/opengl/wiki/Per-Sample_Processing |
| 35 | */ |
| 36 | static void |
| 37 | update_lrz_stencil(struct fd6_zsa_stateobj *so, enum pipe_compare_func func, |
| 38 | bool stencil_write) |
| 39 | { |
| 40 | switch (func) { |
| 41 | case PIPE_FUNC_ALWAYS: |
| 42 | /* nothing to do for LRZ, but for stencil test when stencil- |
| 43 | * write is enabled, we need to disable lrz-test, since |
| 44 | * conceptually stencil test and write happens before depth- |
| 45 | * test: |
| 46 | */ |
| 47 | if (stencil_write) { |
| 48 | so->lrz.enable = false; |
| 49 | so->lrz.test = false; |
| 50 | } |
| 51 | break; |
| 52 | case PIPE_FUNC_NEVER: |
| 53 | /* fragment never passes, disable lrz_write for this draw: */ |
| 54 | so->lrz.write = false; |
| 55 | break; |
| 56 | default: |
| 57 | /* whether the fragment passes or not depends on result |
| 58 | * of stencil test, which we cannot know when doing binning |
| 59 | * pass: |
| 60 | */ |
| 61 | so->lrz.write = false; |
| 62 | /* similarly to the PIPE_FUNC_ALWAY case, if there are side- |
| 63 | * effects from stencil test we need to disable lrz-test. |
| 64 | */ |
| 65 | if (stencil_write) { |
| 66 | so->lrz.enable = false; |
| 67 | so->lrz.test = false; |
| 68 | } |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | template <chip CHIP> |
| 74 | void * |
| 75 | fd6_zsa_state_create(struct pipe_context *pctx, |
| 76 | const struct pipe_depth_stencil_alpha_state *cso) |
| 77 | { |
| 78 | struct fd_context *ctx = fd_context(pctx); |
| 79 | struct fd6_zsa_stateobj *so; |
| 80 | |
| 81 | so = CALLOC_STRUCT(fd6_zsa_stateobj); |
| 82 | if (!so) |
| 83 | return NULL; |
| 84 | |
| 85 | so->base = *cso; |
| 86 | |
| 87 | so->writes_zs = util_writes_depth_stencil(cso); |
| 88 | so->writes_z = util_writes_depth(cso); |
| 89 | |
| 90 | enum adreno_compare_func depth_func = |
| 91 | (enum adreno_compare_func)cso->depth_func; /* maps 1:1 */ |
| 92 | bool force_z_test_enable = false; |
| 93 | |
| 94 | /* On some GPUs it is necessary to enable z test for depth bounds test |
| 95 | * when UBWC is enabled. Otherwise, the GPU would hang. FUNC_ALWAYS is |
| 96 | * required to pass z test. Relevant tests: |
| 97 | * dEQP-VK.pipeline.extended_dynamic_state.two_draws_dynamic.depth_bounds_test_disable |
| 98 | * dEQP-VK.dynamic_state.ds_state.depth_bounds_1 |
| 99 | */ |
| 100 | if (cso->depth_bounds_test && !cso->depth_enabled && |
| 101 | ctx->screen->info->props.depth_bounds_require_depth_test_quirk) { |
| 102 | force_z_test_enable = true; |
| 103 | depth_func = FUNC_ALWAYS; |
| 104 | } |
| 105 | |
| 106 | if (cso->depth_enabled) { |
| 107 | so->lrz.test = true; |
| 108 | |
| 109 | if (cso->depth_writemask) { |
| 110 | so->lrz.write = true; |
| 111 | } |
| 112 | |
| 113 | switch (cso->depth_func) { |
| 114 | case PIPE_FUNC_LESS: |
| 115 | case PIPE_FUNC_LEQUAL: |
| 116 | so->lrz.enable = true; |
| 117 | so->lrz.direction = FD_LRZ_LESS; |
| 118 | break; |
| 119 | |
| 120 | case PIPE_FUNC_GREATER: |
| 121 | case PIPE_FUNC_GEQUAL: |
| 122 | so->lrz.enable = true; |
| 123 | so->lrz.direction = FD_LRZ_GREATER; |
| 124 | break; |
| 125 | |
| 126 | case PIPE_FUNC_NEVER: |
| 127 | so->lrz.enable = true; |
| 128 | so->lrz.write = false; |
| 129 | so->lrz.direction = FD_LRZ_LESS; |
| 130 | break; |
| 131 | |
| 132 | case PIPE_FUNC_ALWAYS: |
| 133 | case PIPE_FUNC_NOTEQUAL: |
| 134 | if (cso->depth_writemask) { |
| 135 | perf_debug_ctx(ctx, "Invalidating LRZ due to ALWAYS/NOTEQUAL with depth write"); |
| 136 | so->lrz.write = false; |
| 137 | so->invalidate_lrz = true; |
| 138 | } else { |
| 139 | perf_debug_ctx(ctx, "Skipping LRZ due to ALWAYS/NOTEQUAL"); |
| 140 | so->lrz.enable = false; |
| 141 | so->lrz.write = false; |
| 142 | } |
| 143 | break; |
| 144 | |
| 145 | case PIPE_FUNC_EQUAL: |
| 146 | so->lrz.enable = false; |
| 147 | so->lrz.write = false; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (cso->stencil[0].enabled) { |
| 153 | const struct pipe_stencil_state *s = &cso->stencil[0]; |
| 154 | |
| 155 | /* stencil test happens before depth test, so without performing |
| 156 | * stencil test we don't really know what the updates to the |
| 157 | * depth buffer will be. |
| 158 | */ |
| 159 | update_lrz_stencil(so, (enum pipe_compare_func)s->func, util_writes_stencil(s)); |
| 160 | |
| 161 | if (cso->stencil[1].enabled) { |
| 162 | const struct pipe_stencil_state *bs = &cso->stencil[1]; |
| 163 | |
| 164 | update_lrz_stencil(so, (enum pipe_compare_func)bs->func, util_writes_stencil(bs)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* Alpha test is functionally a conditional discard, so we can't |
| 169 | * write LRZ before seeing if we end up discarding or not |
| 170 | */ |
| 171 | if (cso->alpha_enabled && (cso->alpha_func != PIPE_FUNC_ALWAYS)) { |
| 172 | so->lrz.write = false; |
| 173 | so->alpha_test = true; |
| 174 | } |
| 175 | |
| 176 | if (cso->depth_bounds_test) { |
| 177 | so->lrz.z_bounds_enable = true; |
| 178 | } |
| 179 | |
| 180 | const struct pipe_stencil_state *fs = &cso->stencil[0]; |
| 181 | const struct pipe_stencil_state *bs = &cso->stencil[1]; |
| 182 | |
| 183 | /* Build the four state permutations (with/without alpha/depth-clamp)*/ |
| 184 | for (int i = 0; i < 4; i++) { |
| 185 | bool depth_clamp_enable = (i & FD6_ZSA_DEPTH_CLAMP); |
| 186 | bool no_alpha = (i & FD6_ZSA_NO_ALPHA); |
| 187 | |
| 188 | fd_crb crb(ctx->pipe, 10); |
| 189 | |
| 190 | crb.add(A6XX_RB_ALPHA_TEST_CNTL( |
| 191 | .alpha_ref = (uint32_t)(cso->alpha_ref_value * 255.0f) & 0xff, |
| 192 | .alpha_test = cso->alpha_enabled && !no_alpha, |
| 193 | .alpha_test_func = (enum adreno_compare_func)cso->alpha_func, |
| 194 | )); |
| 195 | |
| 196 | if (CHIP >= A8XX) { |
| 197 | crb.add(SP_ALPHA_TEST_CNTL(CHIP, |
| 198 | .alpha_test = cso->alpha_enabled && !no_alpha, |
| 199 | )); |
| 200 | } |
| 201 | |
| 202 | crb.add(A6XX_RB_STENCIL_CNTL( |
| 203 | .stencil_enable = fs->enabled, |
| 204 | .stencil_enable_bf = bs->enabled, |
| 205 | .stencil_read = fs->enabled, |
| 206 | .func = (enum adreno_compare_func)fs->func, /* maps 1:1 */ |
| 207 | .fail = fd_stencil_op(fs->fail_op), |
| 208 | .zpass = fd_stencil_op(fs->zpass_op), |
| 209 | .zfail = fd_stencil_op(fs->zfail_op), |
| 210 | .func_bf = (enum adreno_compare_func)bs->func, /* maps 1:1 */ |
| 211 | .fail_bf = fd_stencil_op(bs->fail_op), |
| 212 | .zpass_bf = fd_stencil_op(bs->zpass_op), |
| 213 | .zfail_bf = fd_stencil_op(bs->zfail_op), |
| 214 | )); |
| 215 | |
| 216 | crb.add(GRAS_SU_STENCIL_CNTL(CHIP, cso->stencil[0].enabled)); |
| 217 | crb.add(A6XX_RB_STENCIL_MASK(.mask = fs->valuemask, .bfmask = bs->valuemask)); |
| 218 | crb.add(A6XX_RB_STENCIL_WRITE_MASK(.wrmask = fs->writemask, .bfwrmask = bs->writemask)); |
| 219 | |
| 220 | crb.add(A6XX_RB_DEPTH_CNTL( |
| 221 | .z_test_enable = cso->depth_enabled || force_z_test_enable, |
| 222 | .z_write_enable = cso->depth_writemask, |
| 223 | .zfunc = depth_func, |
| 224 | .z_clamp_enable = depth_clamp_enable || CHIP >= A7XX, |
| 225 | .z_read_enable = cso->depth_enabled || cso->depth_bounds_test, |
| 226 | .z_bounds_enable = cso->depth_bounds_test, |
| 227 | .o_depth_01_clamp_en = CHIP >= A8XX, |
| 228 | )); |
| 229 | |
| 230 | crb.add(GRAS_SU_DEPTH_CNTL(CHIP, cso->depth_enabled)); |
| 231 | |
| 232 | if (CHIP >= A7XX && !depth_clamp_enable) { |
| 233 | crb.add(A6XX_RB_DEPTH_BOUND_MIN(0.0f)); |
| 234 | crb.add(A6XX_RB_DEPTH_BOUND_MAX(1.0f)); |
| 235 | } else { |
| 236 | crb.add(A6XX_RB_DEPTH_BOUND_MIN(cso->depth_bounds_min)); |
| 237 | crb.add(A6XX_RB_DEPTH_BOUND_MAX(cso->depth_bounds_max)); |
| 238 | } |
| 239 | |
| 240 | so->stateobj[i] = crb; |
| 241 | } |
| 242 | |
| 243 | return so; |
| 244 | } |
| 245 | FD_GENX(fd6_zsa_state_create); |
| 246 | |
| 247 | void |
| 248 | fd6_zsa_state_delete(struct pipe_context *pctx, void *hwcso) |
| 249 | { |
| 250 | struct fd6_zsa_stateobj *so = (struct fd6_zsa_stateobj *)hwcso; |
| 251 | |
| 252 | for (int i = 0; i < ARRAY_SIZE(so->stateobj); i++) |
| 253 | fd_ringbuffer_del(so->stateobj[i]); |
| 254 | FREE(hwcso); |
| 255 | } |