[965] Replace our own depth constants in intel context with GL context ones.
[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 GLcontext *ctx = &brw->intel.ctx;
42 const GLfloat depth_scale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
43 struct brw_sf_viewport sfv;
44
45 memset(&sfv, 0, sizeof(sfv));
46
47 if (brw->intel.driDrawable)
48 {
49 /* _NEW_VIEWPORT, BRW_NEW_METAOPS */
50
51 if (!brw->metaops.active) {
52 const GLfloat *v = ctx->Viewport._WindowMap.m;
53
54 sfv.viewport.m00 = v[MAT_SX];
55 sfv.viewport.m11 = - v[MAT_SY];
56 sfv.viewport.m22 = v[MAT_SZ] * depth_scale;
57 sfv.viewport.m30 = v[MAT_TX];
58 sfv.viewport.m31 = - v[MAT_TY] + brw->intel.driDrawable->h;
59 sfv.viewport.m32 = v[MAT_TZ] * depth_scale;
60 }
61 else {
62 sfv.viewport.m00 = 1;
63 sfv.viewport.m11 = - 1;
64 sfv.viewport.m22 = 1;
65 sfv.viewport.m30 = 0;
66 sfv.viewport.m31 = brw->intel.driDrawable->h;
67 sfv.viewport.m32 = 0;
68 }
69 }
70
71 /* XXX: what state for this? */
72 if (brw->intel.driDrawable)
73 {
74 intelScreenPrivate *screen = brw->intel.intelScreen;
75 /* _NEW_SCISSOR */
76 GLint x = brw->attribs.Scissor->X;
77 GLint y = brw->attribs.Scissor->Y;
78 GLuint w = brw->attribs.Scissor->Width;
79 GLuint h = brw->attribs.Scissor->Height;
80
81 GLint x1 = x;
82 GLint y1 = brw->intel.driDrawable->h - (y + h);
83 GLint x2 = x + w - 1;
84 GLint y2 = y1 + h - 1;
85
86 if (x1 < 0) x1 = 0;
87 if (y1 < 0) y1 = 0;
88 if (x2 < 0) x2 = 0;
89 if (y2 < 0) y2 = 0;
90
91 if (x2 >= screen->width) x2 = screen->width-1;
92 if (y2 >= screen->height) y2 = screen->height-1;
93 if (x1 >= screen->width) x1 = screen->width-1;
94 if (y1 >= screen->height) y1 = screen->height-1;
95
96 sfv.scissor.xmin = x1;
97 sfv.scissor.xmax = x2;
98 sfv.scissor.ymin = y1;
99 sfv.scissor.ymax = y2;
100 }
101
102 dri_bo_unreference(brw->sf.vp_bo);
103 brw->sf.vp_bo = brw_cache_data( &brw->cache, BRW_SF_VP, &sfv, NULL, 0 );
104 }
105
106 const struct brw_tracked_state brw_sf_vp = {
107 .dirty = {
108 .mesa = (_NEW_VIEWPORT |
109 _NEW_SCISSOR),
110 .brw = BRW_NEW_METAOPS,
111 .cache = 0
112 },
113 .update = upload_sf_vp
114 };
115
116
117
118 static void upload_sf_unit( struct brw_context *brw )
119 {
120 struct brw_sf_unit_state sf;
121 memset(&sf, 0, sizeof(sf));
122 dri_bo *reloc_bufs[2];
123
124 /* CACHE_NEW_SF_PROG */
125 sf.thread0.grf_reg_count = ALIGN(brw->sf.prog_data->total_grf, 16) / 16 - 1;
126 sf.thread0.kernel_start_pointer = brw->sf.prog_bo->offset >> 6; /* reloc */
127 sf.thread3.urb_entry_read_length = brw->sf.prog_data->urb_read_length;
128
129 sf.thread1.floating_point_mode = BRW_FLOATING_POINT_NON_IEEE_754;
130 sf.thread3.dispatch_grf_start_reg = 3;
131 sf.thread3.urb_entry_read_offset = 1;
132
133 /* BRW_NEW_URB_FENCE */
134 sf.thread4.nr_urb_entries = brw->urb.nr_sf_entries;
135 sf.thread4.urb_entry_allocation_size = brw->urb.sfsize - 1;
136 sf.thread4.max_threads = MIN2(12, brw->urb.nr_sf_entries / 2) - 1;
137
138 if (INTEL_DEBUG & DEBUG_SINGLE_THREAD)
139 sf.thread4.max_threads = 0;
140
141 if (INTEL_DEBUG & DEBUG_STATS)
142 sf.thread4.stats_enable = 1;
143
144 /* CACHE_NEW_SF_VP */
145 sf.sf5.sf_viewport_state_offset = brw->sf.vp_bo->offset >> 5; /* reloc */
146
147 sf.sf5.viewport_transform = 1;
148
149 /* _NEW_SCISSOR */
150 if (brw->attribs.Scissor->Enabled)
151 sf.sf6.scissor = 1;
152
153 /* _NEW_POLYGON */
154 if (brw->attribs.Polygon->FrontFace == GL_CCW)
155 sf.sf5.front_winding = BRW_FRONTWINDING_CCW;
156 else
157 sf.sf5.front_winding = BRW_FRONTWINDING_CW;
158
159 if (brw->attribs.Polygon->CullFlag) {
160 switch (brw->attribs.Polygon->CullFaceMode) {
161 case GL_FRONT:
162 sf.sf6.cull_mode = BRW_CULLMODE_FRONT;
163 break;
164 case GL_BACK:
165 sf.sf6.cull_mode = BRW_CULLMODE_BACK;
166 break;
167 case GL_FRONT_AND_BACK:
168 sf.sf6.cull_mode = BRW_CULLMODE_BOTH;
169 break;
170 default:
171 assert(0);
172 break;
173 }
174 }
175 else
176 sf.sf6.cull_mode = BRW_CULLMODE_NONE;
177
178
179 /* _NEW_LINE */
180 /* XXX use ctx->Const.Min/MaxLineWidth here */
181 sf.sf6.line_width = CLAMP(brw->attribs.Line->Width, 1.0, 5.0) * (1<<1);
182
183 sf.sf6.line_endcap_aa_region_width = 1;
184 if (brw->attribs.Line->SmoothFlag)
185 sf.sf6.aa_enable = 1;
186 else if (sf.sf6.line_width <= 0x2)
187 sf.sf6.line_width = 0;
188
189 /* _NEW_POINT */
190 sf.sf6.point_rast_rule = 1; /* opengl conventions */
191 /* XXX clamp max depends on AA vs. non-AA */
192
193 sf.sf7.sprite_point = brw->attribs.Point->PointSprite;
194 sf.sf7.point_size = CLAMP(brw->attribs.Point->Size, 1.0, 255.0) * (1<<3);
195 sf.sf7.use_point_size_state = !brw->attribs.Point->_Attenuated;
196
197 /* might be BRW_NEW_PRIMITIVE if we have to adjust pv for polygons:
198 */
199 sf.sf7.trifan_pv = 2;
200 sf.sf7.linestrip_pv = 1;
201 sf.sf7.tristrip_pv = 2;
202 sf.sf7.line_last_pixel_enable = 0;
203
204 /* Set bias for OpenGL rasterization rules:
205 */
206 sf.sf6.dest_org_vbias = 0x8;
207 sf.sf6.dest_org_hbias = 0x8;
208
209 reloc_bufs[0] = brw->sf.prog_bo;
210 reloc_bufs[1] = brw->sf.vp_bo;
211
212 brw->sf.thread0_delta = sf.thread0.grf_reg_count << 1;
213 brw->sf.sf5_delta = sf.sf5.front_winding | (sf.sf5.viewport_transform << 1);
214
215 dri_bo_unreference(brw->sf.state_bo);
216 brw->sf.state_bo = brw_cache_data( &brw->cache, BRW_SF_UNIT, &sf,
217 reloc_bufs, 2 );
218 }
219
220 static void emit_reloc_sf_unit(struct brw_context *brw)
221 {
222 /* Emit SF program relocation */
223 dri_emit_reloc(brw->sf.state_bo,
224 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
225 brw->sf.thread0_delta,
226 offsetof(struct brw_sf_unit_state, thread0),
227 brw->sf.prog_bo);
228
229 /* Emit SF viewport relocation */
230 dri_emit_reloc(brw->sf.state_bo,
231 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
232 brw->sf.sf5_delta,
233 offsetof(struct brw_sf_unit_state, sf5),
234 brw->sf.vp_bo);
235 }
236
237 const struct brw_tracked_state brw_sf_unit = {
238 .dirty = {
239 .mesa = (_NEW_POLYGON |
240 _NEW_LINE |
241 _NEW_POINT |
242 _NEW_SCISSOR),
243 .brw = (BRW_NEW_URB_FENCE |
244 BRW_NEW_METAOPS),
245 .cache = (CACHE_NEW_SF_VP |
246 CACHE_NEW_SF_PROG)
247 },
248 .update = upload_sf_unit,
249 .emit_reloc = emit_reloc_sf_unit,
250 };