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
brw_gs_debug_recompile(struct brw_context * brw,struct gl_program * prog,const struct brw_gs_prog_key * key)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
assign_gs_binding_table_offsets(const struct gen_device_info * devinfo,const struct gl_program * prog,struct brw_gs_prog_data * prog_data)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
brw_codegen_gs_prog(struct brw_context * brw,struct brw_program * gp,struct brw_gs_prog_key * key)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 void *mem_ctx = ralloc_context(NULL);
91
92 assign_gs_binding_table_offsets(devinfo, &gp->program, &prog_data);
93
94 brw_nir_setup_glsl_uniforms(mem_ctx, gp->program.nir, &gp->program,
95 &prog_data.base.base,
96 compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
97 brw_nir_analyze_ubo_ranges(compiler, gp->program.nir,
98 prog_data.base.base.ubo_ranges);
99
100 uint64_t outputs_written = gp->program.nir->info.outputs_written;
101
102 brw_compute_vue_map(devinfo,
103 &prog_data.base.vue_map, outputs_written,
104 gp->program.info.separate_shader);
105
106 int st_index = -1;
107 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
108 st_index = brw_get_shader_time_index(brw, &gp->program, ST_GS, true);
109
110 if (unlikely(brw->perf_debug)) {
111 start_busy = brw->batch.last_bo && brw_bo_busy(brw->batch.last_bo);
112 start_time = get_time();
113 }
114
115 char *error_str;
116 const unsigned *program =
117 brw_compile_gs(brw->screen->compiler, brw, mem_ctx, key,
118 &prog_data, gp->program.nir, &gp->program,
119 st_index, &error_str);
120 if (program == NULL) {
121 ralloc_strcat(&gp->program.sh.data->InfoLog, error_str);
122 _mesa_problem(NULL, "Failed to compile geometry shader: %s\n", error_str);
123
124 ralloc_free(mem_ctx);
125 return false;
126 }
127
128 if (unlikely(brw->perf_debug)) {
129 if (gp->compiled_once) {
130 brw_gs_debug_recompile(brw, &gp->program, key);
131 }
132 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
133 perf_debug("GS compile took %.03f ms and stalled the GPU\n",
134 (get_time() - start_time) * 1000);
135 }
136 gp->compiled_once = true;
137 }
138
139 /* Scratch space is used for register spilling */
140 brw_alloc_stage_scratch(brw, stage_state,
141 prog_data.base.base.total_scratch);
142
143 /* The param and pull_param arrays will be freed by the shader cache. */
144 ralloc_steal(NULL, prog_data.base.base.param);
145 ralloc_steal(NULL, prog_data.base.base.pull_param);
146 brw_upload_cache(&brw->cache, BRW_CACHE_GS_PROG,
147 key, sizeof(*key),
148 program, prog_data.base.base.program_size,
149 &prog_data, sizeof(prog_data),
150 &stage_state->prog_offset, &brw->gs.base.prog_data);
151 ralloc_free(mem_ctx);
152
153 return true;
154 }
155
156 static bool
brw_gs_state_dirty(const struct brw_context * brw)157 brw_gs_state_dirty(const struct brw_context *brw)
158 {
159 return brw_state_dirty(brw,
160 _NEW_TEXTURE,
161 BRW_NEW_GEOMETRY_PROGRAM |
162 BRW_NEW_TRANSFORM_FEEDBACK);
163 }
164
165 void
brw_gs_populate_key(struct brw_context * brw,struct brw_gs_prog_key * key)166 brw_gs_populate_key(struct brw_context *brw,
167 struct brw_gs_prog_key *key)
168 {
169 struct gl_context *ctx = &brw->ctx;
170 struct brw_program *gp =
171 (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
172
173 memset(key, 0, sizeof(*key));
174
175 key->program_string_id = gp->id;
176
177 /* _NEW_TEXTURE */
178 brw_populate_sampler_prog_key_data(ctx, &gp->program, &key->tex);
179 }
180
181 void
brw_upload_gs_prog(struct brw_context * brw)182 brw_upload_gs_prog(struct brw_context *brw)
183 {
184 struct brw_stage_state *stage_state = &brw->gs.base;
185 struct brw_gs_prog_key key;
186 /* BRW_NEW_GEOMETRY_PROGRAM */
187 struct brw_program *gp =
188 (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
189
190 if (!brw_gs_state_dirty(brw))
191 return;
192
193 brw_gs_populate_key(brw, &key);
194
195 if (brw_search_cache(&brw->cache, BRW_CACHE_GS_PROG,
196 &key, sizeof(key),
197 &stage_state->prog_offset,
198 &brw->gs.base.prog_data))
199 return;
200
201 if (brw_disk_cache_upload_program(brw, MESA_SHADER_GEOMETRY))
202 return;
203
204 gp = (struct brw_program *) brw->programs[MESA_SHADER_GEOMETRY];
205 gp->id = key.program_string_id;
206
207 MAYBE_UNUSED bool success = brw_codegen_gs_prog(brw, gp, &key);
208 assert(success);
209 }
210
211 bool
brw_gs_precompile(struct gl_context * ctx,struct gl_program * prog)212 brw_gs_precompile(struct gl_context *ctx, struct gl_program *prog)
213 {
214 struct brw_context *brw = brw_context(ctx);
215 struct brw_gs_prog_key key;
216 uint32_t old_prog_offset = brw->gs.base.prog_offset;
217 struct brw_stage_prog_data *old_prog_data = brw->gs.base.prog_data;
218 bool success;
219
220 struct brw_program *bgp = brw_program(prog);
221
222 memset(&key, 0, sizeof(key));
223
224 brw_setup_tex_for_precompile(brw, &key.tex, prog);
225 key.program_string_id = bgp->id;
226
227 success = brw_codegen_gs_prog(brw, bgp, &key);
228
229 brw->gs.base.prog_offset = old_prog_offset;
230 brw->gs.base.prog_data = old_prog_data;
231
232 return success;
233 }
234