i965/vec4: Only zero out unused message components when there are any.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_gs.c
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_vec4_gs.c
26 *
27 * State atom for client-programmable geometry shaders, and support code.
28 */
29
30 #include "brw_vec4_gs.h"
31 #include "brw_context.h"
32 #include "brw_vec4_gs_visitor.h"
33 #include "brw_state.h"
34
35
36 static bool
37 do_gs_prog(struct brw_context *brw,
38 struct gl_shader_program *prog,
39 struct brw_geometry_program *gp,
40 struct brw_gs_prog_key *key)
41 {
42 struct brw_stage_state *stage_state = &brw->gs.base;
43 struct brw_gs_compile c;
44 memset(&c, 0, sizeof(c));
45 c.key = *key;
46 c.gp = gp;
47
48 /* Allocate the references to the uniforms that will end up in the
49 * prog_data associated with the compiled program, and which will be freed
50 * by the state cache.
51 *
52 * Note: param_count needs to be num_uniform_components * 4, since we add
53 * padding around uniform values below vec4 size, so the worst case is that
54 * every uniform is a float which gets padded to the size of a vec4.
55 */
56 struct gl_shader *gs = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
57 int param_count = gs->num_uniform_components * 4;
58
59 /* We also upload clip plane data as uniforms */
60 param_count += MAX_CLIP_PLANES * 4;
61
62 c.prog_data.base.param = rzalloc_array(NULL, const float *, param_count);
63 c.prog_data.base.pull_param = rzalloc_array(NULL, const float *, param_count);
64
65 brw_compute_vue_map(brw, &c.prog_data.base.vue_map,
66 gp->program.Base.OutputsWritten,
67 c.key.base.userclip_active);
68
69 /* Compute the output vertex size.
70 *
71 * From the Ivy Bridge PRM, Vol2 Part1 7.2.1.1 STATE_GS - Output Vertex
72 * Size (p168):
73 *
74 * [0,62] indicating [1,63] 16B units
75 *
76 * Specifies the size of each vertex stored in the GS output entry
77 * (following any Control Header data) as a number of 128-bit units
78 * (minus one).
79 *
80 * Programming Restrictions: The vertex size must be programmed as a
81 * multiple of 32B units with the following exception: Rendering is
82 * disabled (as per SOL stage state) and the vertex size output by the
83 * GS thread is 16B.
84 *
85 * If rendering is enabled (as per SOL state) the vertex size must be
86 * programmed as a multiple of 32B units. In other words, the only time
87 * software can program a vertex size with an odd number of 16B units
88 * is when rendering is disabled.
89 *
90 * Note: B=bytes in the above text.
91 *
92 * It doesn't seem worth the extra trouble to optimize the case where the
93 * vertex size is 16B (especially since this would require special-casing
94 * the GEN assembly that writes to the URB). So we just set the vertex
95 * size to a multiple of 32B (2 vec4's) in all cases.
96 *
97 * The maximum output vertex size is 62*16 = 992 bytes (31 hwords). We
98 * budget that as follows:
99 *
100 * 512 bytes for varyings (a varying component is 4 bytes and
101 * gl_MaxGeometryOutputComponents = 128)
102 * 16 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
103 * bytes)
104 * 16 bytes overhead for gl_Position (we allocate it a slot in the VUE
105 * even if it's not used)
106 * 32 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
107 * whenever clip planes are enabled, even if the shader doesn't
108 * write to gl_ClipDistance)
109 * 16 bytes overhead since the VUE size must be a multiple of 32 bytes
110 * (see below)--this causes up to 1 VUE slot to be wasted
111 * 400 bytes available for varying packing overhead
112 *
113 * Worst-case varying packing overhead is 3/4 of a varying slot (12 bytes)
114 * per interpolation type, so this is plenty.
115 *
116 */
117 unsigned output_vertex_size_bytes = c.prog_data.base.vue_map.num_slots * 16;
118 assert(output_vertex_size_bytes <= GEN7_MAX_GS_OUTPUT_VERTEX_SIZE_BYTES);
119 c.prog_data.output_vertex_size_hwords =
120 ALIGN(output_vertex_size_bytes, 32) / 32;
121
122 /* Compute URB entry size. The maximum allowed URB entry size is 32k.
123 * That divides up as follows:
124 *
125 * 64 bytes for the control data header (cut indices or StreamID bits)
126 * 4096 bytes for varyings (a varying component is 4 bytes and
127 * gl_MaxGeometryTotalOutputComponents = 1024)
128 * 4096 bytes overhead for VARYING_SLOT_PSIZ (each varying slot is 16
129 * bytes/vertex and gl_MaxGeometryOutputVertices is 256)
130 * 4096 bytes overhead for gl_Position (we allocate it a slot in the VUE
131 * even if it's not used)
132 * 8192 bytes overhead for gl_ClipDistance (we allocate it 2 VUE slots
133 * whenever clip planes are enabled, even if the shader doesn't
134 * write to gl_ClipDistance)
135 * 4096 bytes overhead since the VUE size must be a multiple of 32
136 * bytes (see above)--this causes up to 1 VUE slot to be wasted
137 * 8128 bytes available for varying packing overhead
138 *
139 * Worst-case varying packing overhead is 3/4 of a varying slot per
140 * interpolation type, which works out to 3072 bytes, so this would allow
141 * us to accommodate 2 interpolation types without any danger of running
142 * out of URB space.
143 *
144 * In practice, the risk of running out of URB space is very small, since
145 * the above figures are all worst-case, and most of them scale with the
146 * number of output vertices. So we'll just calculate the amount of space
147 * we need, and if it's too large, fail to compile.
148 */
149 unsigned output_size_bytes =
150 c.prog_data.output_vertex_size_hwords * 32 * gp->program.VerticesOut;
151
152 assert(output_size_bytes >= 1);
153 if (output_size_bytes > GEN7_MAX_GS_URB_ENTRY_SIZE_BYTES)
154 return false;
155
156 /* URB entry sizes are stored as a multiple of 64 bytes. */
157 c.prog_data.base.urb_entry_size = ALIGN(output_size_bytes, 64) / 64;
158
159 c.prog_data.output_topology = prim_to_hw_prim[gp->program.OutputType];
160
161 /* GS inputs are read from the VUE 256 bits (2 vec4's) at a time, so we
162 * need to program a URB read length of ceiling(num_slots / 2).
163 */
164 c.prog_data.base.urb_read_length = (c.key.input_vue_map.num_slots + 1) / 2;
165
166 void *mem_ctx = ralloc_context(NULL);
167 unsigned program_size;
168 const unsigned *program =
169 brw_gs_emit(brw, prog, &c, mem_ctx, &program_size);
170 if (program == NULL) {
171 ralloc_free(mem_ctx);
172 return false;
173 }
174
175 /* Scratch space is used for register spilling */
176 if (c.base.last_scratch) {
177 perf_debug("Geometry shader triggered register spilling. "
178 "Try reducing the number of live vec4 values to "
179 "improve performance.\n");
180
181 c.prog_data.base.total_scratch
182 = brw_get_scratch_size(c.base.last_scratch*REG_SIZE);
183
184 brw_get_scratch_bo(brw, &stage_state->scratch_bo,
185 c.prog_data.base.total_scratch * brw->max_gs_threads);
186 }
187
188 brw_upload_cache(&brw->cache, BRW_GS_PROG,
189 &c.key, sizeof(c.key),
190 program, program_size,
191 &c.prog_data, sizeof(c.prog_data),
192 &stage_state->prog_offset, &brw->gs.prog_data);
193 ralloc_free(mem_ctx);
194
195 return true;
196 }
197
198
199 static void
200 brw_upload_gs_prog(struct brw_context *brw)
201 {
202 struct gl_context *ctx = &brw->ctx;
203 struct brw_stage_state *stage_state = &brw->gs.base;
204 struct brw_gs_prog_key key;
205 /* BRW_NEW_GEOMETRY_PROGRAM */
206 struct brw_geometry_program *gp =
207 (struct brw_geometry_program *) brw->geometry_program;
208
209 if (gp == NULL) {
210 /* No geometry shader. Vertex data just passes straight through. */
211 if (brw->state.dirty.brw & BRW_NEW_VUE_MAP_VS) {
212 brw->vue_map_geom_out = brw->vue_map_vs;
213 brw->state.dirty.brw |= BRW_NEW_VUE_MAP_GEOM_OUT;
214 }
215 return;
216 }
217
218 struct gl_program *prog = &gp->program.Base;
219
220 memset(&key, 0, sizeof(key));
221
222 key.base.program_string_id = gp->id;
223 brw_setup_vec4_key_clip_info(brw, &key.base, gp->program.UsesClipDistance);
224
225 /* _NEW_LIGHT | _NEW_BUFFERS */
226 key.base.clamp_vertex_color = ctx->Light._ClampVertexColor;
227
228 /* _NEW_TEXTURE */
229 brw_populate_sampler_prog_key_data(ctx, prog, stage_state->sampler_count,
230 &key.base.tex);
231
232 /* BRW_NEW_VUE_MAP_VS */
233 key.input_vue_map = brw->vue_map_vs;
234
235 if (!brw_search_cache(&brw->cache, BRW_GS_PROG,
236 &key, sizeof(key),
237 &stage_state->prog_offset, &brw->gs.prog_data)) {
238 bool success = do_gs_prog(brw, ctx->Shader.CurrentGeometryProgram,
239 gp, &key);
240 assert(success);
241 }
242 if (memcmp(&brw->vs.prog_data->base.vue_map, &brw->vue_map_geom_out,
243 sizeof(brw->vue_map_geom_out)) != 0) {
244 brw->vue_map_geom_out = brw->gs.prog_data->base.vue_map;
245 brw->state.dirty.brw |= BRW_NEW_VUE_MAP_GEOM_OUT;
246 }
247 }
248
249
250 const struct brw_tracked_state brw_gs_prog = {
251 .dirty = {
252 .mesa = (_NEW_LIGHT | _NEW_BUFFERS | _NEW_TEXTURE),
253 .brw = BRW_NEW_GEOMETRY_PROGRAM | BRW_NEW_VUE_MAP_VS,
254 },
255 .emit = brw_upload_gs_prog
256 };
257
258
259 bool
260 brw_gs_prog_data_compare(const void *in_a, const void *in_b,
261 int aux_size, const void *in_key)
262 {
263 const struct brw_gs_prog_data *a = in_a;
264 const struct brw_gs_prog_data *b = in_b;
265
266 /* Compare the base vec4 structure. */
267 if (!brw_vec4_prog_data_compare(&a->base, &b->base))
268 return false;
269
270 /* Compare the rest of the struct. */
271 const unsigned offset = sizeof(struct brw_vec4_prog_data);
272 if (memcmp(((char *) &a) + offset, ((char *) &b) + offset,
273 sizeof(struct brw_gs_prog_data) - offset)) {
274 return false;
275 }
276
277 return true;
278 }
279
280
281 void
282 brw_gs_prog_data_free(const void *in_prog_data)
283 {
284 const struct brw_gs_prog_data *prog_data = in_prog_data;
285
286 brw_vec4_prog_data_free(&prog_data->base);
287 }