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