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