i965/vs: Add support for pull constant loads for uniform arrays.
[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
58 memset(&c, 0, sizeof(c));
59 memcpy(&c.key, key, sizeof(*key));
60
61 mem_ctx = ralloc_context(NULL);
62
63 brw_init_compile(brw, &c.func, mem_ctx);
64 c.vp = vp;
65
66 c.prog_data.outputs_written = vp->program.Base.OutputsWritten;
67 c.prog_data.inputs_read = vp->program.Base.InputsRead;
68
69 if (c.key.copy_edgeflag) {
70 c.prog_data.outputs_written |= BITFIELD64_BIT(VERT_RESULT_EDGE);
71 c.prog_data.inputs_read |= 1<<VERT_ATTRIB_EDGEFLAG;
72 }
73
74 /* Put dummy slots into the VUE for the SF to put the replaced
75 * point sprite coords in. We shouldn't need these dummy slots,
76 * which take up precious URB space, but it would mean that the SF
77 * doesn't get nice aligned pairs of input coords into output
78 * coords, which would be a pain to handle.
79 */
80 for (i = 0; i < 8; i++) {
81 if (c.key.point_coord_replace & (1 << i))
82 c.prog_data.outputs_written |= BITFIELD64_BIT(VERT_RESULT_TEX0 + i);
83 }
84
85 if (0) {
86 _mesa_fprint_program_opt(stdout, &c.vp->program.Base, PROG_PRINT_DEBUG,
87 GL_TRUE);
88 }
89
90 /* Emit GEN4 code.
91 */
92 if (brw->new_vs_backend && prog) {
93 if (!brw_vs_emit(prog, &c)) {
94 ralloc_free(mem_ctx);
95 return false;
96 }
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 return true;
135 }
136
137
138 static void brw_upload_vs_prog(struct brw_context *brw)
139 {
140 struct gl_context *ctx = &brw->intel.ctx;
141 struct brw_vs_prog_key key;
142 struct brw_vertex_program *vp =
143 (struct brw_vertex_program *)brw->vertex_program;
144 int i;
145
146 memset(&key, 0, sizeof(key));
147
148 /* Just upload the program verbatim for now. Always send it all
149 * the inputs it asks for, whether they are varying or not.
150 */
151 key.program_string_id = vp->id;
152 key.nr_userclip = brw_count_bits(ctx->Transform.ClipPlanesEnabled);
153 key.copy_edgeflag = (ctx->Polygon.FrontMode != GL_FILL ||
154 ctx->Polygon.BackMode != GL_FILL);
155 key.two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
156
157 /* _NEW_LIGHT | _NEW_BUFFERS */
158 key.clamp_vertex_color = ctx->Light._ClampVertexColor;
159
160 /* _NEW_POINT */
161 if (ctx->Point.PointSprite) {
162 for (i = 0; i < 8; i++) {
163 if (ctx->Point.CoordReplace[i])
164 key.point_coord_replace |= (1 << i);
165 }
166 }
167
168 /* BRW_NEW_VERTICES */
169 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
170 if (vp->program.Base.InputsRead & (1 << i) &&
171 brw->vb.inputs[i].glarray->Type == GL_FIXED) {
172 key.gl_fixed_input_size[i] = brw->vb.inputs[i].glarray->Size;
173 }
174 }
175
176 if (!brw_search_cache(&brw->cache, BRW_VS_PROG,
177 &key, sizeof(key),
178 &brw->vs.prog_offset, &brw->vs.prog_data)) {
179 bool success = do_vs_prog(brw, ctx->Shader.CurrentVertexProgram,
180 vp, &key);
181
182 assert(success);
183 }
184 brw->vs.constant_map = ((int8_t *)brw->vs.prog_data +
185 sizeof(*brw->vs.prog_data));
186 }
187
188 /* See brw_vs.c:
189 */
190 const struct brw_tracked_state brw_vs_prog = {
191 .dirty = {
192 .mesa = (_NEW_TRANSFORM | _NEW_POLYGON | _NEW_POINT | _NEW_LIGHT |
193 _NEW_BUFFERS),
194 .brw = (BRW_NEW_VERTEX_PROGRAM |
195 BRW_NEW_VERTICES),
196 .cache = 0
197 },
198 .prepare = brw_upload_vs_prog
199 };
200
201 bool
202 brw_vs_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
203 {
204 struct brw_context *brw = brw_context(ctx);
205 struct brw_vs_prog_key key;
206 struct gl_vertex_program *vp = prog->VertexProgram;
207 struct brw_vertex_program *bvp = brw_vertex_program(vp);
208 uint32_t old_prog_offset = brw->vs.prog_offset;
209 struct brw_vs_prog_data *old_prog_data = brw->vs.prog_data;
210 bool success;
211
212 if (!vp)
213 return true;
214
215 memset(&key, 0, sizeof(key));
216
217 key.program_string_id = bvp->id;
218 key.clamp_vertex_color = true;
219
220 success = do_vs_prog(brw, prog, bvp, &key);
221
222 brw->vs.prog_offset = old_prog_offset;
223 brw->vs.prog_data = old_prog_data;
224
225 return success;
226 }