glsl: move to compiler/
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_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 "main/framebuffer.h"
31 #include "intel_batchbuffer.h"
32
33 static void
34 upload_sbe_state(struct brw_context *brw)
35 {
36 struct gl_context *ctx = &brw->ctx;
37 /* BRW_NEW_FS_PROG_DATA */
38 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
39 uint32_t dw1;
40 uint32_t point_sprite_enables;
41 uint32_t flat_enables;
42 int i;
43 uint16_t attr_overrides[16];
44 /* _NEW_BUFFERS */
45 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
46 uint32_t point_sprite_origin;
47
48 /* FINISHME: Attribute Swizzle Control Mode? */
49 dw1 = GEN7_SBE_SWIZZLE_ENABLE | num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT;
50
51 /* _NEW_POINT
52 *
53 * Window coordinates in an FBO are inverted, which means point
54 * sprite origin must be inverted.
55 */
56 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
57 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
58 } else {
59 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
60 }
61 dw1 |= point_sprite_origin;
62
63 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM
64 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
65 */
66 uint32_t urb_entry_read_length;
67 uint32_t urb_entry_read_offset;
68 calculate_attr_overrides(brw, attr_overrides, &point_sprite_enables,
69 &flat_enables, &urb_entry_read_length,
70 &urb_entry_read_offset);
71 dw1 |= urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
72 urb_entry_read_offset << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
73
74 BEGIN_BATCH(14);
75 OUT_BATCH(_3DSTATE_SBE << 16 | (14 - 2));
76 OUT_BATCH(dw1);
77
78 /* Output dwords 2 through 9 */
79 for (i = 0; i < 8; i++) {
80 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
81 }
82
83 OUT_BATCH(point_sprite_enables); /* dw10 */
84 OUT_BATCH(flat_enables);
85 OUT_BATCH(0); /* wrapshortest enables 0-7 */
86 OUT_BATCH(0); /* wrapshortest enables 8-15 */
87 ADVANCE_BATCH();
88 }
89
90 const struct brw_tracked_state gen7_sbe_state = {
91 .dirty = {
92 .mesa = _NEW_BUFFERS |
93 _NEW_LIGHT |
94 _NEW_POINT |
95 _NEW_PROGRAM,
96 .brw = BRW_NEW_CONTEXT |
97 BRW_NEW_FRAGMENT_PROGRAM |
98 BRW_NEW_FS_PROG_DATA |
99 BRW_NEW_GEOMETRY_PROGRAM |
100 BRW_NEW_PRIMITIVE |
101 BRW_NEW_VUE_MAP_GEOM_OUT,
102 },
103 .emit = upload_sbe_state,
104 };
105
106 static void
107 upload_sf_state(struct brw_context *brw)
108 {
109 struct gl_context *ctx = &brw->ctx;
110 uint32_t dw1, dw2, dw3;
111 float point_size;
112 /* _NEW_BUFFERS */
113 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
114 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
115
116 dw1 = GEN6_SF_STATISTICS_ENABLE;
117
118 if (brw->sf.viewport_transform_enable)
119 dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
120
121 /* _NEW_BUFFERS */
122 dw1 |= (brw_depthbuffer_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
123
124 /* _NEW_POLYGON */
125 if (ctx->Polygon._FrontBit == render_to_fbo)
126 dw1 |= GEN6_SF_WINDING_CCW;
127
128 if (ctx->Polygon.OffsetFill)
129 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
130
131 if (ctx->Polygon.OffsetLine)
132 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
133
134 if (ctx->Polygon.OffsetPoint)
135 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
136
137 switch (ctx->Polygon.FrontMode) {
138 case GL_FILL:
139 dw1 |= GEN6_SF_FRONT_SOLID;
140 break;
141
142 case GL_LINE:
143 dw1 |= GEN6_SF_FRONT_WIREFRAME;
144 break;
145
146 case GL_POINT:
147 dw1 |= GEN6_SF_FRONT_POINT;
148 break;
149
150 default:
151 unreachable("not reached");
152 }
153
154 switch (ctx->Polygon.BackMode) {
155 case GL_FILL:
156 dw1 |= GEN6_SF_BACK_SOLID;
157 break;
158
159 case GL_LINE:
160 dw1 |= GEN6_SF_BACK_WIREFRAME;
161 break;
162
163 case GL_POINT:
164 dw1 |= GEN6_SF_BACK_POINT;
165 break;
166
167 default:
168 unreachable("not reached");
169 }
170
171 dw2 = 0;
172
173 if (ctx->Polygon.CullFlag) {
174 switch (ctx->Polygon.CullFaceMode) {
175 case GL_FRONT:
176 dw2 |= GEN6_SF_CULL_FRONT;
177 break;
178 case GL_BACK:
179 dw2 |= GEN6_SF_CULL_BACK;
180 break;
181 case GL_FRONT_AND_BACK:
182 dw2 |= GEN6_SF_CULL_BOTH;
183 break;
184 default:
185 unreachable("not reached");
186 }
187 } else {
188 dw2 |= GEN6_SF_CULL_NONE;
189 }
190
191 /* _NEW_SCISSOR */
192 if (ctx->Scissor.EnableFlags)
193 dw2 |= GEN6_SF_SCISSOR_ENABLE;
194
195 /* _NEW_LINE */
196 {
197 uint32_t line_width_u3_7 = brw_get_line_width(brw);
198 dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
199 }
200 if (ctx->Line.SmoothFlag) {
201 dw2 |= GEN6_SF_LINE_AA_ENABLE;
202 dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
203 }
204 if (ctx->Line.StippleFlag && brw->is_haswell) {
205 dw2 |= HSW_SF_LINE_STIPPLE_ENABLE;
206 }
207 /* _NEW_MULTISAMPLE */
208 if (multisampled_fbo && ctx->Multisample.Enabled)
209 dw2 |= GEN6_SF_MSRAST_ON_PATTERN;
210
211 /* FINISHME: Last Pixel Enable? Vertex Sub Pixel Precision Select?
212 */
213
214 dw3 = GEN6_SF_LINE_AA_MODE_TRUE;
215
216 /* _NEW_PROGRAM | _NEW_POINT */
217 if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
218 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
219
220 /* Clamp to ARB_point_parameters user limits */
221 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
222
223 /* Clamp to the hardware limits and convert to fixed point */
224 dw3 |= U_FIXED(CLAMP(point_size, 0.125f, 255.875f), 3);
225
226 /* _NEW_LIGHT */
227 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
228 dw3 |=
229 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
230 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
231 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
232 } else {
233 dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
234 }
235
236 BEGIN_BATCH(7);
237 OUT_BATCH(_3DSTATE_SF << 16 | (7 - 2));
238 OUT_BATCH(dw1);
239 OUT_BATCH(dw2);
240 OUT_BATCH(dw3);
241 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
242 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
243 OUT_BATCH_F(ctx->Polygon.OffsetClamp); /* global depth offset clamp */
244 ADVANCE_BATCH();
245 }
246
247 const struct brw_tracked_state gen7_sf_state = {
248 .dirty = {
249 .mesa = _NEW_BUFFERS |
250 _NEW_LIGHT |
251 _NEW_LINE |
252 _NEW_MULTISAMPLE |
253 _NEW_POINT |
254 _NEW_POLYGON |
255 _NEW_PROGRAM |
256 _NEW_SCISSOR,
257 .brw = BRW_NEW_CONTEXT,
258 },
259 .emit = upload_sf_state,
260 };