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