i965: Start adding the VS visitor and codegen.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vs.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 #include "main/compiler.h"
34 #include "brw_context.h"
35 #include "brw_vs.h"
36 #include "brw_util.h"
37 #include "brw_state.h"
38 #include "program/prog_print.h"
39 #include "program/prog_parameter.h"
40
41 #include "../glsl/ralloc.h"
42
43 static void do_vs_prog( struct brw_context *brw,
44 struct brw_vertex_program *vp,
45 struct brw_vs_prog_key *key )
46 {
47 struct gl_context *ctx = &brw->intel.ctx;
48 GLuint program_size;
49 const GLuint *program;
50 struct brw_vs_compile c;
51 void *mem_ctx;
52 int aux_size;
53 int i;
54 static int new_vs = -1;
55
56 memset(&c, 0, sizeof(c));
57 memcpy(&c.key, key, sizeof(*key));
58
59 mem_ctx = ralloc_context(NULL);
60
61 brw_init_compile(brw, &c.func, mem_ctx);
62 c.vp = vp;
63
64 c.prog_data.outputs_written = vp->program.Base.OutputsWritten;
65 c.prog_data.inputs_read = vp->program.Base.InputsRead;
66
67 if (c.key.copy_edgeflag) {
68 c.prog_data.outputs_written |= BITFIELD64_BIT(VERT_RESULT_EDGE);
69 c.prog_data.inputs_read |= 1<<VERT_ATTRIB_EDGEFLAG;
70 }
71
72 /* Put dummy slots into the VUE for the SF to put the replaced
73 * point sprite coords in. We shouldn't need these dummy slots,
74 * which take up precious URB space, but it would mean that the SF
75 * doesn't get nice aligned pairs of input coords into output
76 * coords, which would be a pain to handle.
77 */
78 for (i = 0; i < 8; i++) {
79 if (c.key.point_coord_replace & (1 << i))
80 c.prog_data.outputs_written |= BITFIELD64_BIT(VERT_RESULT_TEX0 + i);
81 }
82
83 if (0) {
84 _mesa_fprint_program_opt(stdout, &c.vp->program.Base, PROG_PRINT_DEBUG,
85 GL_TRUE);
86 }
87
88 /* Emit GEN4 code.
89 */
90 if (new_vs == -1)
91 new_vs = getenv("INTEL_NEW_VS") != NULL;
92
93 if (new_vs) {
94 if (!brw_vs_emit(&c))
95 brw_old_vs_emit(&c);
96 } else {
97 brw_old_vs_emit(&c);
98 }
99
100 /* get the program
101 */
102 program = brw_get_program(&c.func, &program_size);
103
104 /* We upload from &c.prog_data including the constant_map assuming
105 * they're packed together. It would be nice to have a
106 * compile-time assert macro here.
107 */
108 assert(c.constant_map == (int8_t *)&c.prog_data +
109 sizeof(c.prog_data));
110 assert(ctx->Const.VertexProgram.MaxNativeParameters ==
111 ARRAY_SIZE(c.constant_map));
112 (void) ctx;
113
114 aux_size = sizeof(c.prog_data);
115 /* constant_map */
116 aux_size += c.vp->program.Base.Parameters->NumParameters;
117
118 brw_upload_cache(&brw->cache, BRW_VS_PROG,
119 &c.key, sizeof(c.key),
120 program, program_size,
121 &c.prog_data, aux_size,
122 &brw->vs.prog_offset, &brw->vs.prog_data);
123 ralloc_free(mem_ctx);
124 }
125
126
127 static void brw_upload_vs_prog(struct brw_context *brw)
128 {
129 struct gl_context *ctx = &brw->intel.ctx;
130 struct brw_vs_prog_key key;
131 struct brw_vertex_program *vp =
132 (struct brw_vertex_program *)brw->vertex_program;
133 int i;
134
135 memset(&key, 0, sizeof(key));
136
137 /* Just upload the program verbatim for now. Always send it all
138 * the inputs it asks for, whether they are varying or not.
139 */
140 key.program_string_id = vp->id;
141 key.nr_userclip = brw_count_bits(ctx->Transform.ClipPlanesEnabled);
142 key.copy_edgeflag = (ctx->Polygon.FrontMode != GL_FILL ||
143 ctx->Polygon.BackMode != GL_FILL);
144 key.two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
145
146 /* _NEW_LIGHT | _NEW_BUFFERS */
147 key.clamp_vertex_color = ctx->Light._ClampVertexColor;
148
149 /* _NEW_POINT */
150 if (ctx->Point.PointSprite) {
151 for (i = 0; i < 8; i++) {
152 if (ctx->Point.CoordReplace[i])
153 key.point_coord_replace |= (1 << i);
154 }
155 }
156
157 /* BRW_NEW_VERTICES */
158 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
159 if (vp->program.Base.InputsRead & (1 << i) &&
160 brw->vb.inputs[i].glarray->Type == GL_FIXED) {
161 key.gl_fixed_input_size[i] = brw->vb.inputs[i].glarray->Size;
162 }
163 }
164
165 if (!brw_search_cache(&brw->cache, BRW_VS_PROG,
166 &key, sizeof(key),
167 &brw->vs.prog_offset, &brw->vs.prog_data)) {
168 do_vs_prog(brw, vp, &key);
169 }
170 brw->vs.constant_map = ((int8_t *)brw->vs.prog_data +
171 sizeof(*brw->vs.prog_data));
172 }
173
174
175 /* See brw_vs.c:
176 */
177 const struct brw_tracked_state brw_vs_prog = {
178 .dirty = {
179 .mesa = (_NEW_TRANSFORM | _NEW_POLYGON | _NEW_POINT | _NEW_LIGHT |
180 _NEW_BUFFERS),
181 .brw = (BRW_NEW_VERTEX_PROGRAM |
182 BRW_NEW_VERTICES),
183 .cache = 0
184 },
185 .prepare = brw_upload_vs_prog
186 };