i965: Move pre-draw resolve buffers to dd::UpdateState
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_sf_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 "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "brw_util.h"
28 #include "main/macros.h"
29 #include "main/fbobject.h"
30 #include "intel_batchbuffer.h"
31
32 static void
33 upload_sbe(struct brw_context *brw)
34 {
35 struct gl_context *ctx = &brw->ctx;
36 /* CACHE_NEW_WM_PROG */
37 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
38 uint16_t attr_overrides[VARYING_SLOT_MAX];
39 uint32_t urb_entry_read_length;
40 uint32_t point_sprite_enables;
41 uint32_t flat_enables;
42
43 uint32_t dw1 =
44 GEN7_SBE_SWIZZLE_ENABLE |
45 num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT;
46
47 /* _NEW_BUFFERS */
48 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
49
50 /* _NEW_POINT
51 *
52 * Window coordinates in an FBO are inverted, which means point
53 * sprite origin must be inverted.
54 */
55 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
56 dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
57 else
58 dw1 |= GEN6_SF_POINT_SPRITE_UPPERLEFT;
59
60 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM |
61 * CACHE_NEW_WM_PROG
62 */
63 calculate_attr_overrides(brw, attr_overrides,
64 &point_sprite_enables,
65 &flat_enables,
66 &urb_entry_read_length);
67
68 /* Typically, the URB entry read length and offset should be programmed in
69 * 3DSTATE_VS and 3DSTATE_GS; SBE inherits it from the last active stage
70 * which produces geometry. However, we don't know the proper value until
71 * we call calculate_attr_overrides().
72 *
73 * To fit with our existing code, we override the inherited values and
74 * specify it here directly, as we did on previous generations.
75 */
76 dw1 |=
77 urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
78 BRW_SF_URB_ENTRY_READ_OFFSET << GEN8_SBE_URB_ENTRY_READ_OFFSET_SHIFT |
79 GEN8_SBE_FORCE_URB_ENTRY_READ_LENGTH |
80 GEN8_SBE_FORCE_URB_ENTRY_READ_OFFSET;
81
82 BEGIN_BATCH(4);
83 OUT_BATCH(_3DSTATE_SBE << 16 | (4 - 2));
84 OUT_BATCH(dw1);
85 OUT_BATCH(point_sprite_enables);
86 OUT_BATCH(flat_enables);
87 ADVANCE_BATCH();
88
89 BEGIN_BATCH(11);
90 OUT_BATCH(_3DSTATE_SBE_SWIZ << 16 | (11 - 2));
91
92 /* Output DWords 1 through 8: */
93 for (int i = 0; i < 8; i++) {
94 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
95 }
96
97 OUT_BATCH(0); /* wrapshortest enables 0-7 */
98 OUT_BATCH(0); /* wrapshortest enables 8-15 */
99 ADVANCE_BATCH();
100 }
101
102 const struct brw_tracked_state gen8_sbe_state = {
103 .dirty = {
104 .mesa = _NEW_BUFFERS | _NEW_LIGHT | _NEW_POINT | _NEW_PROGRAM,
105 .brw = BRW_NEW_CONTEXT |
106 BRW_NEW_FRAGMENT_PROGRAM |
107 BRW_NEW_VUE_MAP_GEOM_OUT,
108 .cache = CACHE_NEW_WM_PROG,
109 },
110 .emit = upload_sbe,
111 };
112
113 static void
114 upload_sf(struct brw_context *brw)
115 {
116 struct gl_context *ctx = &brw->ctx;
117 uint32_t dw1 = 0, dw2 = 0, dw3 = 0;
118 float point_size;
119
120 dw1 = GEN6_SF_STATISTICS_ENABLE;
121
122 if (brw->sf.viewport_transform_enable)
123 dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
124
125 /* _NEW_LINE */
126 uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
127 if (line_width_u3_7 == 0)
128 line_width_u3_7 = 1;
129 dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
130
131 if (ctx->Line.SmoothFlag) {
132 dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
133 }
134
135 /* Clamp to ARB_point_parameters user limits */
136 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
137
138 /* Clamp to the hardware limits and convert to fixed point */
139 dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
140
141 /* _NEW_PROGRAM | _NEW_POINT */
142 if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
143 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
144
145 /* _NEW_POINT | _NEW_MULTISAMPLE */
146 if ((ctx->Point.SmoothFlag || ctx->Multisample._Enabled) &&
147 !ctx->Point.PointSprite) {
148 dw3 |= GEN8_SF_SMOOTH_POINT_ENABLE;
149 }
150
151 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
152
153 /* _NEW_LIGHT */
154 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
155 dw3 |= (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
156 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
157 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
158 } else {
159 dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
160 }
161
162 BEGIN_BATCH(4);
163 OUT_BATCH(_3DSTATE_SF << 16 | (4 - 2));
164 OUT_BATCH(dw1);
165 OUT_BATCH(dw2);
166 OUT_BATCH(dw3);
167 ADVANCE_BATCH();
168 }
169
170 const struct brw_tracked_state gen8_sf_state = {
171 .dirty = {
172 .mesa = _NEW_LIGHT |
173 _NEW_PROGRAM |
174 _NEW_LINE |
175 _NEW_MULTISAMPLE |
176 _NEW_POINT,
177 .brw = BRW_NEW_CONTEXT,
178 .cache = 0,
179 },
180 .emit = upload_sf,
181 };
182
183 static void
184 upload_raster(struct brw_context *brw)
185 {
186 struct gl_context *ctx = &brw->ctx;
187 uint32_t dw1 = 0;
188
189 /* _NEW_BUFFERS */
190 bool render_to_fbo = _mesa_is_user_fbo(brw->ctx.DrawBuffer);
191
192 /* _NEW_POLYGON */
193 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
194 dw1 |= GEN8_RASTER_FRONT_WINDING_CCW;
195
196 if (ctx->Polygon.CullFlag) {
197 switch (ctx->Polygon.CullFaceMode) {
198 case GL_FRONT:
199 dw1 |= GEN8_RASTER_CULL_FRONT;
200 break;
201 case GL_BACK:
202 dw1 |= GEN8_RASTER_CULL_BACK;
203 break;
204 case GL_FRONT_AND_BACK:
205 dw1 |= GEN8_RASTER_CULL_BOTH;
206 break;
207 default:
208 unreachable("not reached");
209 }
210 } else {
211 dw1 |= GEN8_RASTER_CULL_NONE;
212 }
213
214 /* _NEW_POINT */
215 if (ctx->Point.SmoothFlag)
216 dw1 |= GEN8_RASTER_SMOOTH_POINT_ENABLE;
217
218 if (ctx->Multisample._Enabled)
219 dw1 |= GEN8_RASTER_API_MULTISAMPLE_ENABLE;
220
221 if (ctx->Polygon.OffsetFill)
222 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
223
224 if (ctx->Polygon.OffsetLine)
225 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
226
227 if (ctx->Polygon.OffsetPoint)
228 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
229
230 switch (ctx->Polygon.FrontMode) {
231 case GL_FILL:
232 dw1 |= GEN6_SF_FRONT_SOLID;
233 break;
234 case GL_LINE:
235 dw1 |= GEN6_SF_FRONT_WIREFRAME;
236 break;
237 case GL_POINT:
238 dw1 |= GEN6_SF_FRONT_POINT;
239 break;
240
241 default:
242 unreachable("not reached");
243 }
244
245 switch (ctx->Polygon.BackMode) {
246 case GL_FILL:
247 dw1 |= GEN6_SF_BACK_SOLID;
248 break;
249 case GL_LINE:
250 dw1 |= GEN6_SF_BACK_WIREFRAME;
251 break;
252 case GL_POINT:
253 dw1 |= GEN6_SF_BACK_POINT;
254 break;
255 default:
256 unreachable("not reached");
257 }
258
259 /* _NEW_LINE */
260 if (ctx->Line.SmoothFlag)
261 dw1 |= GEN8_RASTER_LINE_AA_ENABLE;
262
263 /* _NEW_SCISSOR */
264 if (ctx->Scissor.EnableFlags)
265 dw1 |= GEN8_RASTER_SCISSOR_ENABLE;
266
267 /* _NEW_TRANSFORM */
268 if (!ctx->Transform.DepthClamp)
269 dw1 |= GEN8_RASTER_VIEWPORT_Z_CLIP_TEST_ENABLE;
270
271 BEGIN_BATCH(5);
272 OUT_BATCH(_3DSTATE_RASTER << 16 | (5 - 2));
273 OUT_BATCH(dw1);
274 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
275 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
276 OUT_BATCH_F(0.0);
277 ADVANCE_BATCH();
278 }
279
280 const struct brw_tracked_state gen8_raster_state = {
281 .dirty = {
282 .mesa = _NEW_BUFFERS |
283 _NEW_LINE |
284 _NEW_MULTISAMPLE |
285 _NEW_POINT |
286 _NEW_POLYGON |
287 _NEW_SCISSOR |
288 _NEW_TRANSFORM,
289 .brw = BRW_NEW_CONTEXT,
290 .cache = 0,
291 },
292 .emit = upload_raster,
293 };