summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_pipe_depth.c
blob: b7000d5e3344ea0103a9f140be3903ba303e208f (plain)
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

#include "util/u_math.h"
#include "util/u_memory.h"

#include "brw_context.h"
#include "brw_defines.h"

/* XXX: Fixme - include this to get IZ_ defines
 */
#include "brw_wm.h"

static unsigned brw_translate_compare_func(unsigned func)
{
   switch (func) {
   case PIPE_FUNC_NEVER:
      return BRW_COMPAREFUNCTION_NEVER;
   case PIPE_FUNC_LESS:
      return BRW_COMPAREFUNCTION_LESS;
   case PIPE_FUNC_LEQUAL:
      return BRW_COMPAREFUNCTION_LEQUAL;
   case PIPE_FUNC_GREATER:
      return BRW_COMPAREFUNCTION_GREATER;
   case PIPE_FUNC_GEQUAL:
      return BRW_COMPAREFUNCTION_GEQUAL;
   case PIPE_FUNC_NOTEQUAL:
      return BRW_COMPAREFUNCTION_NOTEQUAL;
   case PIPE_FUNC_EQUAL:
      return BRW_COMPAREFUNCTION_EQUAL;
   case PIPE_FUNC_ALWAYS:
      return BRW_COMPAREFUNCTION_ALWAYS;
   default:
      assert(0);
      return BRW_COMPAREFUNCTION_ALWAYS;
   }
}

static unsigned translate_stencil_op(unsigned op)
{
   switch (op) {
   case PIPE_STENCIL_OP_KEEP:
      return BRW_STENCILOP_KEEP;
   case PIPE_STENCIL_OP_ZERO:
      return BRW_STENCILOP_ZERO;
   case PIPE_STENCIL_OP_REPLACE:
      return BRW_STENCILOP_REPLACE;
   case PIPE_STENCIL_OP_INCR:
      return BRW_STENCILOP_INCRSAT;
   case PIPE_STENCIL_OP_DECR:
      return BRW_STENCILOP_DECRSAT;
   case PIPE_STENCIL_OP_INCR_WRAP:
      return BRW_STENCILOP_INCR;
   case PIPE_STENCIL_OP_DECR_WRAP:
      return BRW_STENCILOP_DECR;
   case PIPE_STENCIL_OP_INVERT:
      return BRW_STENCILOP_INVERT;
   default:
      assert(0);
      return BRW_STENCILOP_ZERO;
   }
}

static void create_bcc_state( struct brw_depth_stencil_state *zstencil,
			      const struct pipe_depth_stencil_alpha_state *templ )
{
   if (templ->stencil[0].enabled) {
      zstencil->cc0.stencil_enable = 1;
      zstencil->cc0.stencil_func =
	 brw_translate_compare_func(templ->stencil[0].func);
      zstencil->cc0.stencil_fail_op =
	 translate_stencil_op(templ->stencil[0].fail_op);
      zstencil->cc0.stencil_pass_depth_fail_op =
	 translate_stencil_op(templ->stencil[0].zfail_op);
      zstencil->cc0.stencil_pass_depth_pass_op =
	 translate_stencil_op(templ->stencil[0].zpass_op);
      zstencil->cc1.stencil_write_mask = templ->stencil[0].writemask;
      zstencil->cc1.stencil_test_mask = templ->stencil[0].valuemask;

      if (templ->stencil[1].enabled) {
	 zstencil->cc0.bf_stencil_enable = 1;
	 zstencil->cc0.bf_stencil_func =
	    brw_translate_compare_func(templ->stencil[1].func);
	 zstencil->cc0.bf_stencil_fail_op =
	    translate_stencil_op(templ->stencil[1].fail_op);
	 zstencil->cc0.bf_stencil_pass_depth_fail_op =
	    translate_stencil_op(templ->stencil[1].zfail_op);
	 zstencil->cc0.bf_stencil_pass_depth_pass_op =
	    translate_stencil_op(templ->stencil[1].zpass_op);
	 zstencil->cc2.bf_stencil_write_mask = templ->stencil[1].writemask;
	 zstencil->cc2.bf_stencil_test_mask = templ->stencil[1].valuemask;
      }

      zstencil->cc0.stencil_write_enable = (zstencil->cc1.stencil_write_mask ||
					    zstencil->cc2.bf_stencil_write_mask);
   }


   if (templ->alpha.enabled) {
      zstencil->cc3.alpha_test = 1;
      zstencil->cc3.alpha_test_func = brw_translate_compare_func(templ->alpha.func);
      zstencil->cc3.alpha_test_format = BRW_ALPHATEST_FORMAT_UNORM8;
      zstencil->cc7.alpha_ref.ub[0] = float_to_ubyte(templ->alpha.ref_value);
   }

   if (templ->depth.enabled) {
      zstencil->cc2.depth_test = 1;
      zstencil->cc2.depth_test_function = brw_translate_compare_func(templ->depth.func);
      zstencil->cc2.depth_write_enable = templ->depth.writemask;
   }
}

static void create_wm_iz_state( struct brw_depth_stencil_state *zstencil )
{
   if (zstencil->cc3.alpha_test)
      zstencil->iz_lookup |= IZ_PS_KILL_ALPHATEST_BIT;

   if (zstencil->cc2.depth_test)
      zstencil->iz_lookup |= IZ_DEPTH_TEST_ENABLE_BIT;

   if (zstencil->cc2.depth_write_enable)
      zstencil->iz_lookup |= IZ_DEPTH_WRITE_ENABLE_BIT;

   if (zstencil->cc0.stencil_enable)
      zstencil->iz_lookup |= IZ_STENCIL_TEST_ENABLE_BIT;

   if (zstencil->cc0.stencil_write_enable)
      zstencil->iz_lookup |= IZ_STENCIL_WRITE_ENABLE_BIT;

}


static void *
brw_create_depth_stencil_state( struct pipe_context *pipe,
				const struct pipe_depth_stencil_alpha_state *templ )
{
   struct brw_depth_stencil_state *zstencil = CALLOC_STRUCT(brw_depth_stencil_state);

   create_bcc_state( zstencil, templ );
   create_wm_iz_state( zstencil );

   return (void *)zstencil;
}


static void brw_bind_depth_stencil_state(struct pipe_context *pipe,
					 void *cso)
{
   struct brw_context *brw = brw_context(pipe);
   brw->curr.zstencil = (const struct brw_depth_stencil_state *)cso;
   brw->state.dirty.mesa |= PIPE_NEW_DEPTH_STENCIL_ALPHA;
}

static void brw_delete_depth_stencil_state(struct pipe_context *pipe,
					   void *cso)
{
   struct brw_context *brw = brw_context(pipe);
   assert((const void *)cso != (const void *)brw->curr.zstencil);
   FREE(cso);
}

static void brw_set_stencil_ref(struct pipe_context *pipe,
                                const struct pipe_stencil_ref *stencil_ref)
{
   struct brw_context *brw = brw_context(pipe);
   brw->curr.cc1_stencil_ref.stencil_ref = stencil_ref->ref_value[0];
   brw->curr.cc1_stencil_ref.bf_stencil_ref = stencil_ref->ref_value[1];

   brw->state.dirty.mesa |= PIPE_NEW_DEPTH_STENCIL_ALPHA;
}

void brw_pipe_depth_stencil_init( struct brw_context *brw )
{
   brw->base.set_stencil_ref = brw_set_stencil_ref;
   brw->base.create_depth_stencil_alpha_state = brw_create_depth_stencil_state;
   brw->base.bind_depth_stencil_alpha_state = brw_bind_depth_stencil_state;
   brw->base.delete_depth_stencil_alpha_state = brw_delete_depth_stencil_state;
}

void brw_pipe_depth_stencil_cleanup( struct brw_context *brw )
{
}