[965] Replace various alignment code with a shared ALIGN() macro.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sf_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "brw_defines.h"
37 #include "macros.h"
38
39 static void upload_sf_vp(struct brw_context *brw)
40 {
41 struct brw_sf_viewport sfv;
42
43 memset(&sfv, 0, sizeof(sfv));
44
45 if (brw->intel.driDrawable)
46 {
47 /* _NEW_VIEWPORT, BRW_NEW_METAOPS */
48
49 if (!brw->metaops.active) {
50 const GLfloat *v = brw->intel.ctx.Viewport._WindowMap.m;
51
52 sfv.viewport.m00 = v[MAT_SX];
53 sfv.viewport.m11 = - v[MAT_SY];
54 sfv.viewport.m22 = v[MAT_SZ] * brw->intel.depth_scale;
55 sfv.viewport.m30 = v[MAT_TX];
56 sfv.viewport.m31 = - v[MAT_TY] + brw->intel.driDrawable->h;
57 sfv.viewport.m32 = v[MAT_TZ] * brw->intel.depth_scale;
58 }
59 else {
60 sfv.viewport.m00 = 1;
61 sfv.viewport.m11 = - 1;
62 sfv.viewport.m22 = 1;
63 sfv.viewport.m30 = 0;
64 sfv.viewport.m31 = brw->intel.driDrawable->h;
65 sfv.viewport.m32 = 0;
66 }
67 }
68
69 /* XXX: what state for this? */
70 if (brw->intel.driDrawable)
71 {
72 intelScreenPrivate *screen = brw->intel.intelScreen;
73 /* _NEW_SCISSOR */
74 GLint x = brw->attribs.Scissor->X;
75 GLint y = brw->attribs.Scissor->Y;
76 GLuint w = brw->attribs.Scissor->Width;
77 GLuint h = brw->attribs.Scissor->Height;
78
79 GLint x1 = x;
80 GLint y1 = brw->intel.driDrawable->h - (y + h);
81 GLint x2 = x + w - 1;
82 GLint y2 = y1 + h - 1;
83
84 if (x1 < 0) x1 = 0;
85 if (y1 < 0) y1 = 0;
86 if (x2 < 0) x2 = 0;
87 if (y2 < 0) y2 = 0;
88
89 if (x2 >= screen->width) x2 = screen->width-1;
90 if (y2 >= screen->height) y2 = screen->height-1;
91 if (x1 >= screen->width) x1 = screen->width-1;
92 if (y1 >= screen->height) y1 = screen->height-1;
93
94 sfv.scissor.xmin = x1;
95 sfv.scissor.xmax = x2;
96 sfv.scissor.ymin = y1;
97 sfv.scissor.ymax = y2;
98 }
99
100 brw->sf.vp_gs_offset = brw_cache_data( &brw->cache[BRW_SF_VP], &sfv );
101 }
102
103 const struct brw_tracked_state brw_sf_vp = {
104 .dirty = {
105 .mesa = (_NEW_VIEWPORT |
106 _NEW_SCISSOR),
107 .brw = BRW_NEW_METAOPS,
108 .cache = 0
109 },
110 .update = upload_sf_vp
111 };
112
113
114
115 static void upload_sf_unit( struct brw_context *brw )
116 {
117 struct brw_sf_unit_state sf;
118 memset(&sf, 0, sizeof(sf));
119
120 /* CACHE_NEW_SF_PROG */
121 sf.thread0.grf_reg_count = ALIGN(brw->sf.prog_data->total_grf, 16) / 16 - 1;
122 sf.thread0.kernel_start_pointer = brw->sf.prog_gs_offset >> 6;
123 sf.thread3.urb_entry_read_length = brw->sf.prog_data->urb_read_length;
124
125 sf.thread1.floating_point_mode = BRW_FLOATING_POINT_NON_IEEE_754;
126 sf.thread3.dispatch_grf_start_reg = 3;
127 sf.thread3.urb_entry_read_offset = 1;
128
129 /* BRW_NEW_URB_FENCE */
130 sf.thread4.nr_urb_entries = brw->urb.nr_sf_entries;
131 sf.thread4.urb_entry_allocation_size = brw->urb.sfsize - 1;
132 sf.thread4.max_threads = MIN2(12, brw->urb.nr_sf_entries / 2) - 1;
133
134 if (INTEL_DEBUG & DEBUG_SINGLE_THREAD)
135 sf.thread4.max_threads = 0;
136
137 if (INTEL_DEBUG & DEBUG_STATS)
138 sf.thread4.stats_enable = 1;
139
140 /* CACHE_NEW_SF_VP */
141 sf.sf5.sf_viewport_state_offset = brw->sf.vp_gs_offset >> 5;
142
143 sf.sf5.viewport_transform = 1;
144
145 /* _NEW_SCISSOR */
146 if (brw->attribs.Scissor->Enabled)
147 sf.sf6.scissor = 1;
148
149 /* _NEW_POLYGON */
150 if (brw->attribs.Polygon->FrontFace == GL_CCW)
151 sf.sf5.front_winding = BRW_FRONTWINDING_CCW;
152 else
153 sf.sf5.front_winding = BRW_FRONTWINDING_CW;
154
155 if (brw->attribs.Polygon->CullFlag) {
156 switch (brw->attribs.Polygon->CullFaceMode) {
157 case GL_FRONT:
158 sf.sf6.cull_mode = BRW_CULLMODE_FRONT;
159 break;
160 case GL_BACK:
161 sf.sf6.cull_mode = BRW_CULLMODE_BACK;
162 break;
163 case GL_FRONT_AND_BACK:
164 sf.sf6.cull_mode = BRW_CULLMODE_BOTH;
165 break;
166 default:
167 assert(0);
168 break;
169 }
170 }
171 else
172 sf.sf6.cull_mode = BRW_CULLMODE_NONE;
173
174
175 /* _NEW_LINE */
176 /* XXX use ctx->Const.Min/MaxLineWidth here */
177 sf.sf6.line_width = CLAMP(brw->attribs.Line->Width, 1.0, 5.0) * (1<<1);
178
179 sf.sf6.line_endcap_aa_region_width = 1;
180 if (brw->attribs.Line->SmoothFlag)
181 sf.sf6.aa_enable = 1;
182 else if (sf.sf6.line_width <= 0x2)
183 sf.sf6.line_width = 0;
184
185 /* _NEW_POINT */
186 sf.sf6.point_rast_rule = 1; /* opengl conventions */
187 /* XXX clamp max depends on AA vs. non-AA */
188
189 sf.sf7.sprite_point = brw->attribs.Point->PointSprite;
190 sf.sf7.point_size = CLAMP(brw->attribs.Point->Size, 1.0, 255.0) * (1<<3);
191 sf.sf7.use_point_size_state = !brw->attribs.Point->_Attenuated;
192
193 /* might be BRW_NEW_PRIMITIVE if we have to adjust pv for polygons:
194 */
195 sf.sf7.trifan_pv = 2;
196 sf.sf7.linestrip_pv = 1;
197 sf.sf7.tristrip_pv = 2;
198 sf.sf7.line_last_pixel_enable = 0;
199
200 /* Set bias for OpenGL rasterization rules:
201 */
202 sf.sf6.dest_org_vbias = 0x8;
203 sf.sf6.dest_org_hbias = 0x8;
204
205 brw->sf.state_gs_offset = brw_cache_data( &brw->cache[BRW_SF_UNIT], &sf );
206 }
207
208
209 const struct brw_tracked_state brw_sf_unit = {
210 .dirty = {
211 .mesa = (_NEW_POLYGON |
212 _NEW_LINE |
213 _NEW_POINT |
214 _NEW_SCISSOR),
215 .brw = (BRW_NEW_URB_FENCE |
216 BRW_NEW_METAOPS),
217 .cache = (CACHE_NEW_SF_VP |
218 CACHE_NEW_SF_PROG)
219 },
220 .update = upload_sf_unit
221 };
222
223