1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2012-2015 Etnaviv Project
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the
12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
13b8e80941Smrg * of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Wladimir J. van der Laan <laanwj@gmail.com>
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#include "etnaviv_zsa.h"
28b8e80941Smrg
29b8e80941Smrg#include "etnaviv_context.h"
30b8e80941Smrg#include "etnaviv_screen.h"
31b8e80941Smrg#include "etnaviv_translate.h"
32b8e80941Smrg#include "util/u_memory.h"
33b8e80941Smrg
34b8e80941Smrg#include "hw/common.xml.h"
35b8e80941Smrg
36b8e80941Smrgvoid *
37b8e80941Smrgetna_zsa_state_create(struct pipe_context *pctx,
38b8e80941Smrg                      const struct pipe_depth_stencil_alpha_state *so)
39b8e80941Smrg{
40b8e80941Smrg   struct etna_context *ctx = etna_context(pctx);
41b8e80941Smrg   struct etna_zsa_state *cs = CALLOC_STRUCT(etna_zsa_state);
42b8e80941Smrg
43b8e80941Smrg   if (!cs)
44b8e80941Smrg      return NULL;
45b8e80941Smrg
46b8e80941Smrg   cs->base = *so;
47b8e80941Smrg
48b8e80941Smrg   /* XXX does stencil[0] / stencil[1] order depend on rs->front_ccw? */
49b8e80941Smrg   bool early_z = !VIV_FEATURE(ctx->screen, chipFeatures, NO_EARLY_Z);
50b8e80941Smrg   bool disable_zs =
51b8e80941Smrg      (!so->depth.enabled || so->depth.func == PIPE_FUNC_ALWAYS) &&
52b8e80941Smrg      !so->depth.writemask;
53b8e80941Smrg
54b8e80941Smrg/* Set operations to KEEP if write mask is 0.
55b8e80941Smrg * When we don't do this, the depth buffer is written for the entire primitive
56b8e80941Smrg * instead of just where the stencil condition holds (GC600 rev 0x0019, without
57b8e80941Smrg * feature CORRECT_STENCIL).
58b8e80941Smrg * Not sure if this is a hardware bug or just a strange edge case. */
59b8e80941Smrg#if 0 /* TODO: It looks like a hardware bug */
60b8e80941Smrg    for(int i=0; i<2; ++i)
61b8e80941Smrg    {
62b8e80941Smrg        if(so->stencil[i].writemask == 0)
63b8e80941Smrg        {
64b8e80941Smrg            so->stencil[i].fail_op = so->stencil[i].zfail_op = so->stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
65b8e80941Smrg        }
66b8e80941Smrg    }
67b8e80941Smrg#endif
68b8e80941Smrg
69b8e80941Smrg   /* Determine whether to enable early z reject. Don't enable it when any of
70b8e80941Smrg    * the stencil-modifying functions is used. */
71b8e80941Smrg   if (so->stencil[0].enabled) {
72b8e80941Smrg      if (so->stencil[0].func != PIPE_FUNC_ALWAYS ||
73b8e80941Smrg          (so->stencil[1].enabled && so->stencil[1].func != PIPE_FUNC_ALWAYS))
74b8e80941Smrg         disable_zs = false;
75b8e80941Smrg
76b8e80941Smrg      if (so->stencil[0].fail_op != PIPE_STENCIL_OP_KEEP ||
77b8e80941Smrg          so->stencil[0].zfail_op != PIPE_STENCIL_OP_KEEP ||
78b8e80941Smrg          so->stencil[0].zpass_op != PIPE_STENCIL_OP_KEEP) {
79b8e80941Smrg         disable_zs = early_z = false;
80b8e80941Smrg      } else if (so->stencil[1].enabled) {
81b8e80941Smrg         if (so->stencil[1].fail_op != PIPE_STENCIL_OP_KEEP ||
82b8e80941Smrg             so->stencil[1].zfail_op != PIPE_STENCIL_OP_KEEP ||
83b8e80941Smrg             so->stencil[1].zpass_op != PIPE_STENCIL_OP_KEEP) {
84b8e80941Smrg            disable_zs = early_z = false;
85b8e80941Smrg         }
86b8e80941Smrg      }
87b8e80941Smrg   }
88b8e80941Smrg
89b8e80941Smrg   /* Disable early z reject when no depth test is enabled.
90b8e80941Smrg    * This avoids having to sample depth even though we know it's going to
91b8e80941Smrg    * succeed. */
92b8e80941Smrg   if (so->depth.enabled == false || so->depth.func == PIPE_FUNC_ALWAYS)
93b8e80941Smrg      early_z = false;
94b8e80941Smrg
95b8e80941Smrg   /* compare funcs have 1 to 1 mapping */
96b8e80941Smrg   cs->PE_DEPTH_CONFIG =
97b8e80941Smrg      VIVS_PE_DEPTH_CONFIG_DEPTH_FUNC(so->depth.enabled ? so->depth.func
98b8e80941Smrg                                                        : PIPE_FUNC_ALWAYS) |
99b8e80941Smrg      COND(so->depth.writemask, VIVS_PE_DEPTH_CONFIG_WRITE_ENABLE) |
100b8e80941Smrg      COND(early_z, VIVS_PE_DEPTH_CONFIG_EARLY_Z) |
101b8e80941Smrg      /* this bit changed meaning with HALTI5: */
102b8e80941Smrg      COND(disable_zs && ctx->specs.halti < 5, VIVS_PE_DEPTH_CONFIG_DISABLE_ZS);
103b8e80941Smrg   cs->PE_ALPHA_OP =
104b8e80941Smrg      COND(so->alpha.enabled, VIVS_PE_ALPHA_OP_ALPHA_TEST) |
105b8e80941Smrg      VIVS_PE_ALPHA_OP_ALPHA_FUNC(so->alpha.func) |
106b8e80941Smrg      VIVS_PE_ALPHA_OP_ALPHA_REF(etna_cfloat_to_uint8(so->alpha.ref_value));
107b8e80941Smrg   cs->PE_STENCIL_OP =
108b8e80941Smrg      VIVS_PE_STENCIL_OP_FUNC_FRONT(so->stencil[0].func) |
109b8e80941Smrg      VIVS_PE_STENCIL_OP_FUNC_BACK(so->stencil[1].func) |
110b8e80941Smrg      VIVS_PE_STENCIL_OP_FAIL_FRONT(translate_stencil_op(so->stencil[0].fail_op)) |
111b8e80941Smrg      VIVS_PE_STENCIL_OP_FAIL_BACK(translate_stencil_op(so->stencil[1].fail_op)) |
112b8e80941Smrg      VIVS_PE_STENCIL_OP_DEPTH_FAIL_FRONT(translate_stencil_op(so->stencil[0].zfail_op)) |
113b8e80941Smrg      VIVS_PE_STENCIL_OP_DEPTH_FAIL_BACK(translate_stencil_op(so->stencil[1].zfail_op)) |
114b8e80941Smrg      VIVS_PE_STENCIL_OP_PASS_FRONT(translate_stencil_op(so->stencil[0].zpass_op)) |
115b8e80941Smrg      VIVS_PE_STENCIL_OP_PASS_BACK(translate_stencil_op(so->stencil[1].zpass_op));
116b8e80941Smrg   cs->PE_STENCIL_CONFIG =
117b8e80941Smrg      translate_stencil_mode(so->stencil[0].enabled, so->stencil[1].enabled) |
118b8e80941Smrg      VIVS_PE_STENCIL_CONFIG_MASK_FRONT(so->stencil[0].valuemask) |
119b8e80941Smrg      VIVS_PE_STENCIL_CONFIG_WRITE_MASK_FRONT(so->stencil[0].writemask);
120b8e80941Smrg   /* XXX back masks in VIVS_PE_DEPTH_CONFIG_EXT? */
121b8e80941Smrg   /* XXX VIVS_PE_STENCIL_CONFIG_REF_FRONT comes from pipe_stencil_ref */
122b8e80941Smrg
123b8e80941Smrg   /* XXX does alpha/stencil test affect PE_COLOR_FORMAT_OVERWRITE? */
124b8e80941Smrg   return cs;
125b8e80941Smrg}
126