intel: s/brw_device_info/gen_device_info/
[mesa.git] / src / mesa / drivers / dri / i965 / brw_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_gs.h"
31 #include "brw_context.h"
32 #include "brw_vec4_gs_visitor.h"
33 #include "brw_state.h"
34 #include "brw_ff_gs.h"
35 #include "brw_nir.h"
36 #include "brw_program.h"
37 #include "compiler/glsl/ir_uniform.h"
38
39 static void
40 brw_gs_debug_recompile(struct brw_context *brw,
41 struct gl_shader_program *shader_prog,
42 const struct brw_gs_prog_key *key)
43 {
44 struct brw_cache_item *c = NULL;
45 const struct brw_gs_prog_key *old_key = NULL;
46 bool found = false;
47
48 perf_debug("Recompiling geometry shader for program %d\n",
49 shader_prog->Name);
50
51 for (unsigned int i = 0; i < brw->cache.size; i++) {
52 for (c = brw->cache.items[i]; c; c = c->next) {
53 if (c->cache_id == BRW_CACHE_GS_PROG) {
54 old_key = c->key;
55
56 if (old_key->program_string_id == key->program_string_id)
57 break;
58 }
59 }
60 if (c)
61 break;
62 }
63
64 if (!c) {
65 perf_debug(" Didn't find previous compile in the shader cache for "
66 "debug\n");
67 return;
68 }
69
70 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
71
72 if (!found) {
73 perf_debug(" Something else\n");
74 }
75 }
76
77 static void
78 assign_gs_binding_table_offsets(const struct gen_device_info *devinfo,
79 const struct gl_shader_program *shader_prog,
80 const struct gl_program *prog,
81 struct brw_gs_prog_data *prog_data)
82 {
83 /* In gen6 we reserve the first BRW_MAX_SOL_BINDINGS entries for transform
84 * feedback surfaces.
85 */
86 uint32_t reserved = devinfo->gen == 6 ? BRW_MAX_SOL_BINDINGS : 0;
87
88 brw_assign_common_binding_table_offsets(MESA_SHADER_GEOMETRY, devinfo,
89 shader_prog, prog,
90 &prog_data->base.base,
91 reserved);
92 }
93
94 bool
95 brw_codegen_gs_prog(struct brw_context *brw,
96 struct gl_shader_program *prog,
97 struct brw_geometry_program *gp,
98 struct brw_gs_prog_key *key)
99 {
100 struct brw_compiler *compiler = brw->intelScreen->compiler;
101 struct brw_stage_state *stage_state = &brw->gs.base;
102 struct brw_gs_prog_data prog_data;
103 bool start_busy = false;
104 double start_time = 0;
105
106 memset(&prog_data, 0, sizeof(prog_data));
107
108 assign_gs_binding_table_offsets(brw->intelScreen->devinfo, prog,
109 &gp->program.Base, &prog_data);
110
111 /* Allocate the references to the uniforms that will end up in the
112 * prog_data associated with the compiled program, and which will be freed
113 * by the state cache.
114 *
115 * Note: param_count needs to be num_uniform_components * 4, since we add
116 * padding around uniform values below vec4 size, so the worst case is that
117 * every uniform is a float which gets padded to the size of a vec4.
118 */
119 struct gl_linked_shader *gs = prog->_LinkedShaders[MESA_SHADER_GEOMETRY];
120 struct brw_shader *bgs = (struct brw_shader *) gs;
121 int param_count = gp->program.Base.nir->num_uniforms / 4;
122
123 prog_data.base.base.param =
124 rzalloc_array(NULL, const gl_constant_value *, param_count);
125 prog_data.base.base.pull_param =
126 rzalloc_array(NULL, const gl_constant_value *, param_count);
127 prog_data.base.base.image_param =
128 rzalloc_array(NULL, struct brw_image_param, gs->NumImages);
129 prog_data.base.base.nr_params = param_count;
130 prog_data.base.base.nr_image_params = gs->NumImages;
131
132 brw_nir_setup_glsl_uniforms(gp->program.Base.nir, prog, &gp->program.Base,
133 &prog_data.base.base,
134 compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
135
136 GLbitfield64 outputs_written = gp->program.Base.OutputsWritten;
137
138 prog_data.base.cull_distance_mask =
139 ((1 << gp->program.Base.CullDistanceArraySize) - 1) <<
140 gp->program.Base.ClipDistanceArraySize;
141
142 brw_compute_vue_map(brw->intelScreen->devinfo,
143 &prog_data.base.vue_map, outputs_written,
144 prog->SeparateShader);
145
146 if (unlikely(INTEL_DEBUG & DEBUG_GS))
147 brw_dump_ir("geometry", prog, gs, NULL);
148
149 int st_index = -1;
150 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
151 st_index = brw_get_shader_time_index(brw, prog, NULL, ST_GS);
152
153 if (unlikely(brw->perf_debug)) {
154 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
155 start_time = get_time();
156 }
157
158 void *mem_ctx = ralloc_context(NULL);
159 unsigned program_size;
160 char *error_str;
161 const unsigned *program =
162 brw_compile_gs(brw->intelScreen->compiler, brw, mem_ctx, key,
163 &prog_data, gs->Program->nir, prog,
164 st_index, &program_size, &error_str);
165 if (program == NULL) {
166 ralloc_strcat(&prog->InfoLog, error_str);
167 _mesa_problem(NULL, "Failed to compile geometry shader: %s\n", error_str);
168
169 ralloc_free(mem_ctx);
170 return false;
171 }
172
173 if (unlikely(brw->perf_debug)) {
174 if (bgs->compiled_once) {
175 brw_gs_debug_recompile(brw, prog, key);
176 }
177 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
178 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
179 (get_time() - start_time) * 1000);
180 }
181 bgs->compiled_once = true;
182 }
183
184 /* Scratch space is used for register spilling */
185 brw_alloc_stage_scratch(brw, stage_state,
186 prog_data.base.base.total_scratch,
187 brw->max_gs_threads);
188
189 brw_upload_cache(&brw->cache, BRW_CACHE_GS_PROG,
190 key, sizeof(*key),
191 program, program_size,
192 &prog_data, sizeof(prog_data),
193 &stage_state->prog_offset, &brw->gs.prog_data);
194 ralloc_free(mem_ctx);
195
196 return true;
197 }
198
199 static bool
200 brw_gs_state_dirty(const struct brw_context *brw)
201 {
202 return brw_state_dirty(brw,
203 _NEW_TEXTURE,
204 BRW_NEW_GEOMETRY_PROGRAM |
205 BRW_NEW_TRANSFORM_FEEDBACK);
206 }
207
208 static void
209 brw_gs_populate_key(struct brw_context *brw,
210 struct brw_gs_prog_key *key)
211 {
212 struct gl_context *ctx = &brw->ctx;
213 struct brw_geometry_program *gp =
214 (struct brw_geometry_program *) brw->geometry_program;
215 struct gl_program *prog = &gp->program.Base;
216
217 memset(key, 0, sizeof(*key));
218
219 key->program_string_id = gp->id;
220
221 /* _NEW_TEXTURE */
222 brw_populate_sampler_prog_key_data(ctx, prog, &key->tex);
223 }
224
225 void
226 brw_upload_gs_prog(struct brw_context *brw)
227 {
228 struct gl_context *ctx = &brw->ctx;
229 struct gl_shader_program **current = ctx->_Shader->CurrentProgram;
230 struct brw_stage_state *stage_state = &brw->gs.base;
231 struct brw_gs_prog_key key;
232 /* BRW_NEW_GEOMETRY_PROGRAM */
233 struct brw_geometry_program *gp =
234 (struct brw_geometry_program *) brw->geometry_program;
235
236 if (!brw_gs_state_dirty(brw))
237 return;
238
239 if (gp == NULL) {
240 /* No geometry shader. Vertex data just passes straight through. */
241 if (brw->gen == 6 &&
242 (brw->ctx.NewDriverState & BRW_NEW_TRANSFORM_FEEDBACK)) {
243 gen6_brw_upload_ff_gs_prog(brw);
244 return;
245 }
246
247 /* Other state atoms had better not try to access prog_data, since
248 * there's no GS program.
249 */
250 brw->gs.prog_data = NULL;
251 brw->gs.base.prog_data = NULL;
252
253 return;
254 }
255
256 brw_gs_populate_key(brw, &key);
257
258 if (!brw_search_cache(&brw->cache, BRW_CACHE_GS_PROG,
259 &key, sizeof(key),
260 &stage_state->prog_offset, &brw->gs.prog_data)) {
261 bool success = brw_codegen_gs_prog(brw, current[MESA_SHADER_GEOMETRY],
262 gp, &key);
263 assert(success);
264 (void)success;
265 }
266 brw->gs.base.prog_data = &brw->gs.prog_data->base.base;
267 }
268
269 bool
270 brw_gs_precompile(struct gl_context *ctx,
271 struct gl_shader_program *shader_prog,
272 struct gl_program *prog)
273 {
274 struct brw_context *brw = brw_context(ctx);
275 struct brw_gs_prog_key key;
276 uint32_t old_prog_offset = brw->gs.base.prog_offset;
277 struct brw_gs_prog_data *old_prog_data = brw->gs.prog_data;
278 bool success;
279
280 struct gl_geometry_program *gp = (struct gl_geometry_program *) prog;
281 struct brw_geometry_program *bgp = brw_geometry_program(gp);
282
283 memset(&key, 0, sizeof(key));
284
285 brw_setup_tex_for_precompile(brw, &key.tex, prog);
286 key.program_string_id = bgp->id;
287
288 success = brw_codegen_gs_prog(brw, shader_prog, bgp, &key);
289
290 brw->gs.base.prog_offset = old_prog_offset;
291 brw->gs.prog_data = old_prog_data;
292
293 return success;
294 }