i965: Rename (vs|wm)_max_threads to max_(vs|wm)_threads for consistency.
[profile/ivi/mesa.git] / src / mesa / drivers / dri / i965 / gen7_wm_state.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <stdbool.h>
25 #include "brw_context.h"
26 #include "brw_state.h"
27 #include "brw_defines.h"
28 #include "brw_util.h"
29 #include "brw_wm.h"
30 #include "program/prog_parameter.h"
31 #include "program/prog_statevars.h"
32 #include "intel_batchbuffer.h"
33
34 static void
35 upload_wm_state(struct brw_context *brw)
36 {
37    struct intel_context *intel = &brw->intel;
38    struct gl_context *ctx = &intel->ctx;
39    const struct brw_fragment_program *fp =
40       brw_fragment_program_const(brw->fragment_program);
41    bool writes_depth = false;
42    uint32_t dw1;
43
44    dw1 = 0;
45    dw1 |= GEN7_WM_STATISTICS_ENABLE;
46    dw1 |= GEN7_WM_LINE_AA_WIDTH_1_0;
47    dw1 |= GEN7_WM_LINE_END_CAP_AA_WIDTH_0_5;
48
49    /* _NEW_LINE */
50    if (ctx->Line.StippleFlag)
51       dw1 |= GEN7_WM_LINE_STIPPLE_ENABLE;
52
53    /* _NEW_POLYGON */
54    if (ctx->Polygon.StippleFlag)
55       dw1 |= GEN7_WM_POLYGON_STIPPLE_ENABLE;
56
57    /* BRW_NEW_FRAGMENT_PROGRAM */
58    if (fp->program.Base.InputsRead & (1 << FRAG_ATTRIB_WPOS))
59       dw1 |= GEN7_WM_USES_SOURCE_DEPTH | GEN7_WM_USES_SOURCE_W;
60    if (fp->program.Base.OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {
61       writes_depth = true;
62       dw1 |= GEN7_WM_PSCDEPTH_ON;
63    }
64
65    /* _NEW_COLOR */
66    if (fp->program.UsesKill || ctx->Color.AlphaEnabled)
67       dw1 |= GEN7_WM_KILL_ENABLE;
68
69    /* _NEW_BUFFERS */
70    if (brw_color_buffer_write_enabled(brw) || writes_depth ||
71        dw1 & GEN7_WM_KILL_ENABLE) {
72       dw1 |= GEN7_WM_DISPATCH_ENABLE;
73    }
74
75    dw1 |= GEN7_WM_PERSPECTIVE_PIXEL_BARYCENTRIC;
76
77    BEGIN_BATCH(3);
78    OUT_BATCH(_3DSTATE_WM << 16 | (3 - 2));
79    OUT_BATCH(dw1);
80    OUT_BATCH(0);
81    ADVANCE_BATCH();
82 }
83
84 const struct brw_tracked_state gen7_wm_state = {
85    .dirty = {
86       .mesa  = (_NEW_LINE | _NEW_POLYGON |
87                 _NEW_COLOR | _NEW_BUFFERS),
88       .brw   = (BRW_NEW_FRAGMENT_PROGRAM |
89                 BRW_NEW_URB_FENCE |
90                 BRW_NEW_BATCH),
91       .cache = 0,
92    },
93    .emit = upload_wm_state,
94 };
95
96 static void
97 upload_ps_state(struct brw_context *brw)
98 {
99    struct intel_context *intel = &brw->intel;
100    uint32_t dw2, dw4, dw5;
101
102    BEGIN_BATCH(2);
103    OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS_PS << 16 | (2 - 2));
104    OUT_BATCH(brw->wm.bind_bo_offset);
105    ADVANCE_BATCH();
106
107    /* CACHE_NEW_SAMPLER */
108    BEGIN_BATCH(2);
109    OUT_BATCH(_3DSTATE_SAMPLER_STATE_POINTERS_PS << 16 | (2 - 2));
110    OUT_BATCH(brw->wm.sampler_offset);
111    ADVANCE_BATCH();
112
113    /* CACHE_NEW_WM_PROG */
114    if (brw->wm.prog_data->nr_params == 0) {
115       /* Disable the push constant buffers. */
116       BEGIN_BATCH(7);
117       OUT_BATCH(_3DSTATE_CONSTANT_PS << 16 | (7 - 2));
118       OUT_BATCH(0);
119       OUT_BATCH(0);
120       OUT_BATCH(0);
121       OUT_BATCH(0);
122       OUT_BATCH(0);
123       OUT_BATCH(0);
124       ADVANCE_BATCH();
125    } else {
126       BEGIN_BATCH(7);
127       OUT_BATCH(_3DSTATE_CONSTANT_PS << 16 | (7 - 2));
128
129       OUT_BATCH(ALIGN(brw->wm.prog_data->nr_params,
130                       brw->wm.prog_data->dispatch_width) / 8);
131       OUT_BATCH(0);
132       /* Pointer to the WM constant buffer.  Covered by the set of
133        * state flags from gen7_prepare_wm_constants
134        */
135       OUT_BATCH(brw->wm.push_const_offset);
136       OUT_BATCH(0);
137       OUT_BATCH(0);
138       OUT_BATCH(0);
139       ADVANCE_BATCH();
140    }
141
142    dw2 = dw4 = dw5 = 0;
143
144    dw2 |= (ALIGN(brw->wm.sampler_count, 4) / 4) << GEN7_PS_SAMPLER_COUNT_SHIFT;
145
146    /* BRW_NEW_NR_WM_SURFACES */
147    dw2 |= brw->wm.nr_surfaces << GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT;
148
149    /* OpenGL non-ieee floating point mode */
150    dw2 |= GEN7_PS_FLOATING_POINT_MODE_ALT;
151
152    /* CACHE_NEW_SAMPLER */
153    dw4 |= (brw->max_wm_threads - 1) << GEN7_PS_MAX_THREADS_SHIFT;
154
155    /* CACHE_NEW_WM_PROG */
156    if (brw->wm.prog_data->nr_params > 0)
157       dw4 |= GEN7_PS_PUSH_CONSTANT_ENABLE;
158
159    /* BRW_NEW_FRAGMENT_PROGRAM */
160    if (brw->fragment_program->Base.InputsRead != 0)
161       dw4 |= GEN7_PS_ATTRIBUTE_ENABLE;
162
163    if (brw->wm.prog_data->dispatch_width == 8)
164       dw4 |= GEN7_PS_8_DISPATCH_ENABLE;
165    else
166       dw4 |= GEN7_PS_16_DISPATCH_ENABLE;
167
168    /* BRW_NEW_CURBE_OFFSETS */
169    dw5 |= (brw->wm.prog_data->first_curbe_grf <<
170            GEN7_PS_DISPATCH_START_GRF_SHIFT_0);
171
172    BEGIN_BATCH(8);
173    OUT_BATCH(_3DSTATE_PS << 16 | (8 - 2));
174    OUT_BATCH(brw->wm.prog_offset);
175    OUT_BATCH(dw2);
176    if (brw->wm.prog_data->total_scratch) {
177       OUT_RELOC(brw->wm.scratch_bo,
178                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
179                 ffs(brw->wm.prog_data->total_scratch) - 11);
180    } else {
181       OUT_BATCH(0);
182    }
183    OUT_BATCH(dw4);
184    OUT_BATCH(dw5);
185    OUT_BATCH(0); /* kernel 1 pointer */
186    OUT_BATCH(brw->wm.prog_offset + brw->wm.prog_data->prog_offset_16);
187    ADVANCE_BATCH();
188 }
189
190 const struct brw_tracked_state gen7_ps_state = {
191    .dirty = {
192       .mesa  = _NEW_PROGRAM_CONSTANTS,
193       .brw   = (BRW_NEW_CURBE_OFFSETS |
194                 BRW_NEW_FRAGMENT_PROGRAM |
195                 BRW_NEW_NR_WM_SURFACES |
196                 BRW_NEW_PS_BINDING_TABLE |
197                 BRW_NEW_URB_FENCE |
198                 BRW_NEW_BATCH),
199       .cache = (CACHE_NEW_SAMPLER |
200                 CACHE_NEW_WM_PROG)
201    },
202    .emit = upload_ps_state,
203 };